linux/drivers/regulator/max8998.c
<<
>>
Prefs
   1/*
   2 * max8998.c - Voltage regulator driver for the Maxim 8998
   3 *
   4 *  Copyright (C) 2009-2010 Samsung Electronics
   5 *  Kyungmin Park <kyungmin.park@samsung.com>
   6 *  Marek Szyprowski <m.szyprowski@samsung.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/init.h>
  25#include <linux/i2c.h>
  26#include <linux/err.h>
  27#include <linux/gpio.h>
  28#include <linux/slab.h>
  29#include <linux/interrupt.h>
  30#include <linux/mutex.h>
  31#include <linux/of.h>
  32#include <linux/of_gpio.h>
  33#include <linux/platform_device.h>
  34#include <linux/regulator/driver.h>
  35#include <linux/regulator/of_regulator.h>
  36#include <linux/mfd/max8998.h>
  37#include <linux/mfd/max8998-private.h>
  38
  39struct max8998_data {
  40        struct device           *dev;
  41        struct max8998_dev      *iodev;
  42        int                     num_regulators;
  43        struct regulator_dev    **rdev;
  44        u8                      buck1_vol[4]; /* voltages for selection */
  45        u8                      buck2_vol[2];
  46        unsigned int            buck1_idx; /* index to last changed voltage */
  47                                           /* value in a set */
  48        unsigned int            buck2_idx;
  49};
  50
  51struct voltage_map_desc {
  52        int min;
  53        int max;
  54        int step;
  55};
  56
  57/* Voltage maps in uV*/
  58static const struct voltage_map_desc ldo23_voltage_map_desc = {
  59        .min = 800000,  .step = 50000,  .max = 1300000,
  60};
  61static const struct voltage_map_desc ldo456711_voltage_map_desc = {
  62        .min = 1600000, .step = 100000, .max = 3600000,
  63};
  64static const struct voltage_map_desc ldo8_voltage_map_desc = {
  65        .min = 3000000, .step = 100000, .max = 3600000,
  66};
  67static const struct voltage_map_desc ldo9_voltage_map_desc = {
  68        .min = 2800000, .step = 100000, .max = 3100000,
  69};
  70static const struct voltage_map_desc ldo10_voltage_map_desc = {
  71        .min = 950000,  .step = 50000,  .max = 1300000,
  72};
  73static const struct voltage_map_desc ldo1213_voltage_map_desc = {
  74        .min = 800000,  .step = 100000, .max = 3300000,
  75};
  76static const struct voltage_map_desc ldo1415_voltage_map_desc = {
  77        .min = 1200000, .step = 100000, .max = 3300000,
  78};
  79static const struct voltage_map_desc ldo1617_voltage_map_desc = {
  80        .min = 1600000, .step = 100000, .max = 3600000,
  81};
  82static const struct voltage_map_desc buck12_voltage_map_desc = {
  83        .min = 750000,  .step = 25000,  .max = 1525000,
  84};
  85static const struct voltage_map_desc buck3_voltage_map_desc = {
  86        .min = 1600000, .step = 100000, .max = 3600000,
  87};
  88static const struct voltage_map_desc buck4_voltage_map_desc = {
  89        .min = 800000,  .step = 100000, .max = 2300000,
  90};
  91
  92static const struct voltage_map_desc *ldo_voltage_map[] = {
  93        NULL,
  94        NULL,
  95        &ldo23_voltage_map_desc,        /* LDO2 */
  96        &ldo23_voltage_map_desc,        /* LDO3 */
  97        &ldo456711_voltage_map_desc,    /* LDO4 */
  98        &ldo456711_voltage_map_desc,    /* LDO5 */
  99        &ldo456711_voltage_map_desc,    /* LDO6 */
 100        &ldo456711_voltage_map_desc,    /* LDO7 */
 101        &ldo8_voltage_map_desc,         /* LDO8 */
 102        &ldo9_voltage_map_desc,         /* LDO9 */
 103        &ldo10_voltage_map_desc,        /* LDO10 */
 104        &ldo456711_voltage_map_desc,    /* LDO11 */
 105        &ldo1213_voltage_map_desc,      /* LDO12 */
 106        &ldo1213_voltage_map_desc,      /* LDO13 */
 107        &ldo1415_voltage_map_desc,      /* LDO14 */
 108        &ldo1415_voltage_map_desc,      /* LDO15 */
 109        &ldo1617_voltage_map_desc,      /* LDO16 */
 110        &ldo1617_voltage_map_desc,      /* LDO17 */
 111        &buck12_voltage_map_desc,       /* BUCK1 */
 112        &buck12_voltage_map_desc,       /* BUCK2 */
 113        &buck3_voltage_map_desc,        /* BUCK3 */
 114        &buck4_voltage_map_desc,        /* BUCK4 */
 115};
 116
 117static int max8998_get_enable_register(struct regulator_dev *rdev,
 118                                        int *reg, int *shift)
 119{
 120        int ldo = rdev_get_id(rdev);
 121
 122        switch (ldo) {
 123        case MAX8998_LDO2 ... MAX8998_LDO5:
 124                *reg = MAX8998_REG_ONOFF1;
 125                *shift = 3 - (ldo - MAX8998_LDO2);
 126                break;
 127        case MAX8998_LDO6 ... MAX8998_LDO13:
 128                *reg = MAX8998_REG_ONOFF2;
 129                *shift = 7 - (ldo - MAX8998_LDO6);
 130                break;
 131        case MAX8998_LDO14 ... MAX8998_LDO17:
 132                *reg = MAX8998_REG_ONOFF3;
 133                *shift = 7 - (ldo - MAX8998_LDO14);
 134                break;
 135        case MAX8998_BUCK1 ... MAX8998_BUCK4:
 136                *reg = MAX8998_REG_ONOFF1;
 137                *shift = 7 - (ldo - MAX8998_BUCK1);
 138                break;
 139        case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
 140                *reg = MAX8998_REG_ONOFF4;
 141                *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
 142                break;
 143        case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
 144                *reg = MAX8998_REG_CHGR2;
 145                *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
 146                break;
 147        default:
 148                return -EINVAL;
 149        }
 150
 151        return 0;
 152}
 153
 154static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
 155{
 156        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 157        struct i2c_client *i2c = max8998->iodev->i2c;
 158        int ret, reg, shift = 8;
 159        u8 val;
 160
 161        ret = max8998_get_enable_register(rdev, &reg, &shift);
 162        if (ret)
 163                return ret;
 164
 165        ret = max8998_read_reg(i2c, reg, &val);
 166        if (ret)
 167                return ret;
 168
 169        return val & (1 << shift);
 170}
 171
 172static int max8998_ldo_enable(struct regulator_dev *rdev)
 173{
 174        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 175        struct i2c_client *i2c = max8998->iodev->i2c;
 176        int reg, shift = 8, ret;
 177
 178        ret = max8998_get_enable_register(rdev, &reg, &shift);
 179        if (ret)
 180                return ret;
 181
 182        return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
 183}
 184
 185static int max8998_ldo_disable(struct regulator_dev *rdev)
 186{
 187        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 188        struct i2c_client *i2c = max8998->iodev->i2c;
 189        int reg, shift = 8, ret;
 190
 191        ret = max8998_get_enable_register(rdev, &reg, &shift);
 192        if (ret)
 193                return ret;
 194
 195        return max8998_update_reg(i2c, reg, 0, 1<<shift);
 196}
 197
 198static int max8998_get_voltage_register(struct regulator_dev *rdev,
 199                                int *_reg, int *_shift, int *_mask)
 200{
 201        int ldo = rdev_get_id(rdev);
 202        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 203        int reg, shift = 0, mask = 0xff;
 204
 205        switch (ldo) {
 206        case MAX8998_LDO2 ... MAX8998_LDO3:
 207                reg = MAX8998_REG_LDO2_LDO3;
 208                mask = 0xf;
 209                if (ldo == MAX8998_LDO2)
 210                        shift = 4;
 211                else
 212                        shift = 0;
 213                break;
 214        case MAX8998_LDO4 ... MAX8998_LDO7:
 215                reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
 216                break;
 217        case MAX8998_LDO8 ... MAX8998_LDO9:
 218                reg = MAX8998_REG_LDO8_LDO9;
 219                mask = 0xf;
 220                if (ldo == MAX8998_LDO8)
 221                        shift = 4;
 222                else
 223                        shift = 0;
 224                break;
 225        case MAX8998_LDO10 ... MAX8998_LDO11:
 226                reg = MAX8998_REG_LDO10_LDO11;
 227                if (ldo == MAX8998_LDO10) {
 228                        shift = 5;
 229                        mask = 0x7;
 230                } else {
 231                        shift = 0;
 232                        mask = 0x1f;
 233                }
 234                break;
 235        case MAX8998_LDO12 ... MAX8998_LDO17:
 236                reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
 237                break;
 238        case MAX8998_BUCK1:
 239                reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
 240                break;
 241        case MAX8998_BUCK2:
 242                reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
 243                break;
 244        case MAX8998_BUCK3:
 245                reg = MAX8998_REG_BUCK3;
 246                break;
 247        case MAX8998_BUCK4:
 248                reg = MAX8998_REG_BUCK4;
 249                break;
 250        default:
 251                return -EINVAL;
 252        }
 253
 254        *_reg = reg;
 255        *_shift = shift;
 256        *_mask = mask;
 257
 258        return 0;
 259}
 260
 261static int max8998_get_voltage_sel(struct regulator_dev *rdev)
 262{
 263        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 264        struct i2c_client *i2c = max8998->iodev->i2c;
 265        int reg, shift = 0, mask, ret;
 266        u8 val;
 267
 268        ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
 269        if (ret)
 270                return ret;
 271
 272        ret = max8998_read_reg(i2c, reg, &val);
 273        if (ret)
 274                return ret;
 275
 276        val >>= shift;
 277        val &= mask;
 278
 279        return val;
 280}
 281
 282static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
 283                                       unsigned selector)
 284{
 285        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 286        struct i2c_client *i2c = max8998->iodev->i2c;
 287        int reg, shift = 0, mask, ret;
 288
 289        ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
 290        if (ret)
 291                return ret;
 292
 293        ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
 294
 295        return ret;
 296}
 297
 298static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
 299{
 300        gpio_set_value(gpio1, v & 0x1);
 301        gpio_set_value(gpio2, (v >> 1) & 0x1);
 302}
 303
 304static inline void buck2_gpio_set(int gpio, int v)
 305{
 306        gpio_set_value(gpio, v & 0x1);
 307}
 308
 309static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
 310                                        unsigned selector)
 311{
 312        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 313        struct max8998_platform_data *pdata =
 314                dev_get_platdata(max8998->iodev->dev);
 315        struct i2c_client *i2c = max8998->iodev->i2c;
 316        int buck = rdev_get_id(rdev);
 317        int reg, shift = 0, mask, ret, j;
 318        static u8 buck1_last_val;
 319
 320        ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
 321        if (ret)
 322                return ret;
 323
 324        switch (buck) {
 325        case MAX8998_BUCK1:
 326                dev_dbg(max8998->dev,
 327                        "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
 328                        "buck1_vol3:%d, buck1_vol4:%d\n",
 329                        selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
 330                        max8998->buck1_vol[2], max8998->buck1_vol[3]);
 331
 332                if (gpio_is_valid(pdata->buck1_set1) &&
 333                    gpio_is_valid(pdata->buck1_set2)) {
 334
 335                        /* check if requested voltage */
 336                        /* value is already defined */
 337                        for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
 338                                if (max8998->buck1_vol[j] == selector) {
 339                                        max8998->buck1_idx = j;
 340                                        buck1_gpio_set(pdata->buck1_set1,
 341                                                       pdata->buck1_set2, j);
 342                                        goto buck1_exit;
 343                                }
 344                        }
 345
 346                        if (pdata->buck_voltage_lock)
 347                                return -EINVAL;
 348
 349                        /* no predefine regulator found */
 350                        max8998->buck1_idx = (buck1_last_val % 2) + 2;
 351                        dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
 352                                max8998->buck1_idx);
 353                        max8998->buck1_vol[max8998->buck1_idx] = selector;
 354                        ret = max8998_get_voltage_register(rdev, &reg,
 355                                                           &shift,
 356                                                           &mask);
 357                        ret = max8998_write_reg(i2c, reg, selector);
 358                        buck1_gpio_set(pdata->buck1_set1,
 359                                       pdata->buck1_set2, max8998->buck1_idx);
 360                        buck1_last_val++;
 361buck1_exit:
 362                        dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
 363                                i2c->name, gpio_get_value(pdata->buck1_set1),
 364                                gpio_get_value(pdata->buck1_set2));
 365                        break;
 366                } else {
 367                        ret = max8998_write_reg(i2c, reg, selector);
 368                }
 369                break;
 370
 371        case MAX8998_BUCK2:
 372                dev_dbg(max8998->dev,
 373                        "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
 374                        selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
 375                if (gpio_is_valid(pdata->buck2_set3)) {
 376
 377                        /* check if requested voltage */
 378                        /* value is already defined */
 379                        for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
 380                                if (max8998->buck2_vol[j] == selector) {
 381                                        max8998->buck2_idx = j;
 382                                        buck2_gpio_set(pdata->buck2_set3, j);
 383                                        goto buck2_exit;
 384                                }
 385                        }
 386
 387                        if (pdata->buck_voltage_lock)
 388                                return -EINVAL;
 389
 390                        max8998_get_voltage_register(rdev,
 391                                        &reg, &shift, &mask);
 392                        ret = max8998_write_reg(i2c, reg, selector);
 393                        max8998->buck2_vol[max8998->buck2_idx] = selector;
 394                        buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
 395buck2_exit:
 396                        dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
 397                                gpio_get_value(pdata->buck2_set3));
 398                } else {
 399                        ret = max8998_write_reg(i2c, reg, selector);
 400                }
 401                break;
 402
 403        case MAX8998_BUCK3:
 404        case MAX8998_BUCK4:
 405                ret = max8998_update_reg(i2c, reg, selector<<shift,
 406                                         mask<<shift);
 407                break;
 408        }
 409
 410        return ret;
 411}
 412
 413static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
 414                                             unsigned int old_selector,
 415                                             unsigned int new_selector)
 416{
 417        struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 418        struct i2c_client *i2c = max8998->iodev->i2c;
 419        const struct voltage_map_desc *desc;
 420        int buck = rdev_get_id(rdev);
 421        u8 val = 0;
 422        int difference, ret;
 423
 424        if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
 425                return -EINVAL;
 426
 427        desc = ldo_voltage_map[buck];
 428
 429        /* Voltage stabilization */
 430        ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
 431        if (ret)
 432                return ret;
 433
 434        /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
 435        /* MAX8998 has ENRAMP bit implemented, so test it*/
 436        if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
 437                return 0;
 438
 439        difference = (new_selector - old_selector) * desc->step / 1000;
 440        if (difference > 0)
 441                return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
 442
 443        return 0;
 444}
 445
 446static struct regulator_ops max8998_ldo_ops = {
 447        .list_voltage           = regulator_list_voltage_linear,
 448        .map_voltage            = regulator_map_voltage_linear,
 449        .is_enabled             = max8998_ldo_is_enabled,
 450        .enable                 = max8998_ldo_enable,
 451        .disable                = max8998_ldo_disable,
 452        .get_voltage_sel        = max8998_get_voltage_sel,
 453        .set_voltage_sel        = max8998_set_voltage_ldo_sel,
 454};
 455
 456static struct regulator_ops max8998_buck_ops = {
 457        .list_voltage           = regulator_list_voltage_linear,
 458        .map_voltage            = regulator_map_voltage_linear,
 459        .is_enabled             = max8998_ldo_is_enabled,
 460        .enable                 = max8998_ldo_enable,
 461        .disable                = max8998_ldo_disable,
 462        .get_voltage_sel        = max8998_get_voltage_sel,
 463        .set_voltage_sel        = max8998_set_voltage_buck_sel,
 464        .set_voltage_time_sel   = max8998_set_voltage_buck_time_sel,
 465};
 466
 467static struct regulator_ops max8998_others_ops = {
 468        .is_enabled             = max8998_ldo_is_enabled,
 469        .enable                 = max8998_ldo_enable,
 470        .disable                = max8998_ldo_disable,
 471};
 472
 473static struct regulator_desc regulators[] = {
 474        {
 475                .name           = "LDO2",
 476                .id             = MAX8998_LDO2,
 477                .ops            = &max8998_ldo_ops,
 478                .type           = REGULATOR_VOLTAGE,
 479                .owner          = THIS_MODULE,
 480        }, {
 481                .name           = "LDO3",
 482                .id             = MAX8998_LDO3,
 483                .ops            = &max8998_ldo_ops,
 484                .type           = REGULATOR_VOLTAGE,
 485                .owner          = THIS_MODULE,
 486        }, {
 487                .name           = "LDO4",
 488                .id             = MAX8998_LDO4,
 489                .ops            = &max8998_ldo_ops,
 490                .type           = REGULATOR_VOLTAGE,
 491                .owner          = THIS_MODULE,
 492        }, {
 493                .name           = "LDO5",
 494                .id             = MAX8998_LDO5,
 495                .ops            = &max8998_ldo_ops,
 496                .type           = REGULATOR_VOLTAGE,
 497                .owner          = THIS_MODULE,
 498        }, {
 499                .name           = "LDO6",
 500                .id             = MAX8998_LDO6,
 501                .ops            = &max8998_ldo_ops,
 502                .type           = REGULATOR_VOLTAGE,
 503                .owner          = THIS_MODULE,
 504        }, {
 505                .name           = "LDO7",
 506                .id             = MAX8998_LDO7,
 507                .ops            = &max8998_ldo_ops,
 508                .type           = REGULATOR_VOLTAGE,
 509                .owner          = THIS_MODULE,
 510        }, {
 511                .name           = "LDO8",
 512                .id             = MAX8998_LDO8,
 513                .ops            = &max8998_ldo_ops,
 514                .type           = REGULATOR_VOLTAGE,
 515                .owner          = THIS_MODULE,
 516        }, {
 517                .name           = "LDO9",
 518                .id             = MAX8998_LDO9,
 519                .ops            = &max8998_ldo_ops,
 520                .type           = REGULATOR_VOLTAGE,
 521                .owner          = THIS_MODULE,
 522        }, {
 523                .name           = "LDO10",
 524                .id             = MAX8998_LDO10,
 525                .ops            = &max8998_ldo_ops,
 526                .type           = REGULATOR_VOLTAGE,
 527                .owner          = THIS_MODULE,
 528        }, {
 529                .name           = "LDO11",
 530                .id             = MAX8998_LDO11,
 531                .ops            = &max8998_ldo_ops,
 532                .type           = REGULATOR_VOLTAGE,
 533                .owner          = THIS_MODULE,
 534        }, {
 535                .name           = "LDO12",
 536                .id             = MAX8998_LDO12,
 537                .ops            = &max8998_ldo_ops,
 538                .type           = REGULATOR_VOLTAGE,
 539                .owner          = THIS_MODULE,
 540        }, {
 541                .name           = "LDO13",
 542                .id             = MAX8998_LDO13,
 543                .ops            = &max8998_ldo_ops,
 544                .type           = REGULATOR_VOLTAGE,
 545                .owner          = THIS_MODULE,
 546        }, {
 547                .name           = "LDO14",
 548                .id             = MAX8998_LDO14,
 549                .ops            = &max8998_ldo_ops,
 550                .type           = REGULATOR_VOLTAGE,
 551                .owner          = THIS_MODULE,
 552        }, {
 553                .name           = "LDO15",
 554                .id             = MAX8998_LDO15,
 555                .ops            = &max8998_ldo_ops,
 556                .type           = REGULATOR_VOLTAGE,
 557                .owner          = THIS_MODULE,
 558        }, {
 559                .name           = "LDO16",
 560                .id             = MAX8998_LDO16,
 561                .ops            = &max8998_ldo_ops,
 562                .type           = REGULATOR_VOLTAGE,
 563                .owner          = THIS_MODULE,
 564        }, {
 565                .name           = "LDO17",
 566                .id             = MAX8998_LDO17,
 567                .ops            = &max8998_ldo_ops,
 568                .type           = REGULATOR_VOLTAGE,
 569                .owner          = THIS_MODULE,
 570        }, {
 571                .name           = "BUCK1",
 572                .id             = MAX8998_BUCK1,
 573                .ops            = &max8998_buck_ops,
 574                .type           = REGULATOR_VOLTAGE,
 575                .owner          = THIS_MODULE,
 576        }, {
 577                .name           = "BUCK2",
 578                .id             = MAX8998_BUCK2,
 579                .ops            = &max8998_buck_ops,
 580                .type           = REGULATOR_VOLTAGE,
 581                .owner          = THIS_MODULE,
 582        }, {
 583                .name           = "BUCK3",
 584                .id             = MAX8998_BUCK3,
 585                .ops            = &max8998_buck_ops,
 586                .type           = REGULATOR_VOLTAGE,
 587                .owner          = THIS_MODULE,
 588        }, {
 589                .name           = "BUCK4",
 590                .id             = MAX8998_BUCK4,
 591                .ops            = &max8998_buck_ops,
 592                .type           = REGULATOR_VOLTAGE,
 593                .owner          = THIS_MODULE,
 594        }, {
 595                .name           = "EN32KHz-AP",
 596                .id             = MAX8998_EN32KHZ_AP,
 597                .ops            = &max8998_others_ops,
 598                .type           = REGULATOR_VOLTAGE,
 599                .owner          = THIS_MODULE,
 600        }, {
 601                .name           = "EN32KHz-CP",
 602                .id             = MAX8998_EN32KHZ_CP,
 603                .ops            = &max8998_others_ops,
 604                .type           = REGULATOR_VOLTAGE,
 605                .owner          = THIS_MODULE,
 606        }, {
 607                .name           = "ENVICHG",
 608                .id             = MAX8998_ENVICHG,
 609                .ops            = &max8998_others_ops,
 610                .type           = REGULATOR_VOLTAGE,
 611                .owner          = THIS_MODULE,
 612        }, {
 613                .name           = "ESAFEOUT1",
 614                .id             = MAX8998_ESAFEOUT1,
 615                .ops            = &max8998_others_ops,
 616                .type           = REGULATOR_VOLTAGE,
 617                .owner          = THIS_MODULE,
 618        }, {
 619                .name           = "ESAFEOUT2",
 620                .id             = MAX8998_ESAFEOUT2,
 621                .ops            = &max8998_others_ops,
 622                .type           = REGULATOR_VOLTAGE,
 623                .owner          = THIS_MODULE,
 624        }
 625};
 626
 627static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
 628                        struct max8998_platform_data *pdata,
 629                        struct device_node *pmic_np)
 630{
 631        int gpio;
 632
 633        gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
 634        if (!gpio_is_valid(gpio)) {
 635                dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
 636                return -EINVAL;
 637        }
 638        pdata->buck1_set1 = gpio;
 639
 640        gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
 641        if (!gpio_is_valid(gpio)) {
 642                dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
 643                return -EINVAL;
 644        }
 645        pdata->buck1_set2 = gpio;
 646
 647        gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
 648        if (!gpio_is_valid(gpio)) {
 649                dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
 650                return -EINVAL;
 651        }
 652        pdata->buck2_set3 = gpio;
 653
 654        return 0;
 655}
 656
 657static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
 658                                        struct max8998_platform_data *pdata)
 659{
 660        struct device_node *pmic_np = iodev->dev->of_node;
 661        struct device_node *regulators_np, *reg_np;
 662        struct max8998_regulator_data *rdata;
 663        unsigned int i;
 664        int ret;
 665
 666        regulators_np = of_get_child_by_name(pmic_np, "regulators");
 667        if (!regulators_np) {
 668                dev_err(iodev->dev, "could not find regulators sub-node\n");
 669                return -EINVAL;
 670        }
 671
 672        /* count the number of regulators to be supported in pmic */
 673        pdata->num_regulators = of_get_child_count(regulators_np);
 674
 675        rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
 676                                pdata->num_regulators, GFP_KERNEL);
 677        if (!rdata)
 678                return -ENOMEM;
 679
 680        pdata->regulators = rdata;
 681        for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
 682                reg_np = of_get_child_by_name(regulators_np,
 683                                                        regulators[i].name);
 684                if (!reg_np)
 685                        continue;
 686
 687                rdata->id = regulators[i].id;
 688                rdata->initdata = of_get_regulator_init_data(
 689                                                        iodev->dev, reg_np);
 690                rdata->reg_node = reg_np;
 691                ++rdata;
 692        }
 693        pdata->num_regulators = rdata - pdata->regulators;
 694
 695        ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
 696        if (ret)
 697                return -EINVAL;
 698
 699        if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
 700                pdata->buck_voltage_lock = true;
 701
 702        ret = of_property_read_u32(pmic_np,
 703                                        "max8998,pmic-buck1-default-dvs-idx",
 704                                        &pdata->buck1_default_idx);
 705        if (!ret && pdata->buck1_default_idx >= 4) {
 706                pdata->buck1_default_idx = 0;
 707                dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
 708        }
 709
 710        ret = of_property_read_u32(pmic_np,
 711                                        "max8998,pmic-buck2-default-dvs-idx",
 712                                        &pdata->buck2_default_idx);
 713        if (!ret && pdata->buck2_default_idx >= 2) {
 714                pdata->buck2_default_idx = 0;
 715                dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
 716        }
 717
 718        ret = of_property_read_u32_array(pmic_np,
 719                                        "max8998,pmic-buck1-dvs-voltage",
 720                                        pdata->buck1_voltage,
 721                                        ARRAY_SIZE(pdata->buck1_voltage));
 722        if (ret) {
 723                dev_err(iodev->dev, "buck1 voltages not specified\n");
 724                return -EINVAL;
 725        }
 726
 727        ret = of_property_read_u32_array(pmic_np,
 728                                        "max8998,pmic-buck2-dvs-voltage",
 729                                        pdata->buck2_voltage,
 730                                        ARRAY_SIZE(pdata->buck2_voltage));
 731        if (ret) {
 732                dev_err(iodev->dev, "buck2 voltages not specified\n");
 733                return -EINVAL;
 734        }
 735
 736        return 0;
 737}
 738
 739static int max8998_pmic_probe(struct platform_device *pdev)
 740{
 741        struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
 742        struct max8998_platform_data *pdata = iodev->pdata;
 743        struct regulator_config config = { };
 744        struct regulator_dev **rdev;
 745        struct max8998_data *max8998;
 746        struct i2c_client *i2c;
 747        int i, ret, size;
 748        unsigned int v;
 749
 750        if (!pdata) {
 751                dev_err(pdev->dev.parent, "No platform init data supplied\n");
 752                return -ENODEV;
 753        }
 754
 755        if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
 756                ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
 757                if (ret)
 758                        return ret;
 759        }
 760
 761        max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
 762                               GFP_KERNEL);
 763        if (!max8998)
 764                return -ENOMEM;
 765
 766        size = sizeof(struct regulator_dev *) * pdata->num_regulators;
 767        max8998->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
 768        if (!max8998->rdev)
 769                return -ENOMEM;
 770
 771        rdev = max8998->rdev;
 772        max8998->dev = &pdev->dev;
 773        max8998->iodev = iodev;
 774        max8998->num_regulators = pdata->num_regulators;
 775        platform_set_drvdata(pdev, max8998);
 776        i2c = max8998->iodev->i2c;
 777
 778        max8998->buck1_idx = pdata->buck1_default_idx;
 779        max8998->buck2_idx = pdata->buck2_default_idx;
 780
 781        /* NOTE: */
 782        /* For unused GPIO NOT marked as -1 (thereof equal to 0)  WARN_ON */
 783        /* will be displayed */
 784
 785        /* Check if MAX8998 voltage selection GPIOs are defined */
 786        if (gpio_is_valid(pdata->buck1_set1) &&
 787            gpio_is_valid(pdata->buck1_set2)) {
 788                /* Check if SET1 is not equal to 0 */
 789                if (!pdata->buck1_set1) {
 790                        dev_err(&pdev->dev,
 791                                "MAX8998 SET1 GPIO defined as 0 !\n");
 792                        WARN_ON(!pdata->buck1_set1);
 793                        ret = -EIO;
 794                        goto err_out;
 795                }
 796                /* Check if SET2 is not equal to 0 */
 797                if (!pdata->buck1_set2) {
 798                        dev_err(&pdev->dev,
 799                                "MAX8998 SET2 GPIO defined as 0 !\n");
 800                        WARN_ON(!pdata->buck1_set2);
 801                        ret = -EIO;
 802                        goto err_out;
 803                }
 804
 805                gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
 806                gpio_direction_output(pdata->buck1_set1,
 807                                      max8998->buck1_idx & 0x1);
 808
 809
 810                gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
 811                gpio_direction_output(pdata->buck1_set2,
 812                                      (max8998->buck1_idx >> 1) & 0x1);
 813
 814                /* Set predefined values for BUCK1 registers */
 815                for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
 816                        i = 0;
 817                        while (buck12_voltage_map_desc.min +
 818                               buck12_voltage_map_desc.step*i
 819                               < pdata->buck1_voltage[v])
 820                                i++;
 821
 822                        max8998->buck1_vol[v] = i;
 823                        ret = max8998_write_reg(i2c,
 824                                        MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
 825                        if (ret)
 826                                goto err_out;
 827                }
 828        }
 829
 830        if (gpio_is_valid(pdata->buck2_set3)) {
 831                /* Check if SET3 is not equal to 0 */
 832                if (!pdata->buck2_set3) {
 833                        dev_err(&pdev->dev,
 834                                "MAX8998 SET3 GPIO defined as 0 !\n");
 835                        WARN_ON(!pdata->buck2_set3);
 836                        ret = -EIO;
 837                        goto err_out;
 838                }
 839                gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
 840                gpio_direction_output(pdata->buck2_set3,
 841                                      max8998->buck2_idx & 0x1);
 842
 843                /* Set predefined values for BUCK2 registers */
 844                for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
 845                        i = 0;
 846                        while (buck12_voltage_map_desc.min +
 847                               buck12_voltage_map_desc.step*i
 848                               < pdata->buck2_voltage[v])
 849                                i++;
 850
 851                        max8998->buck2_vol[v] = i;
 852                        ret = max8998_write_reg(i2c,
 853                                        MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
 854                        if (ret)
 855                                goto err_out;
 856                }
 857        }
 858
 859        for (i = 0; i < pdata->num_regulators; i++) {
 860                const struct voltage_map_desc *desc;
 861                int id = pdata->regulators[i].id;
 862                int index = id - MAX8998_LDO2;
 863
 864                desc = ldo_voltage_map[id];
 865                if (desc && regulators[index].ops != &max8998_others_ops) {
 866                        int count = (desc->max - desc->min) / desc->step + 1;
 867
 868                        regulators[index].n_voltages = count;
 869                        regulators[index].min_uV = desc->min;
 870                        regulators[index].uV_step = desc->step;
 871                }
 872
 873                config.dev = max8998->dev;
 874                config.of_node = pdata->regulators[i].reg_node;
 875                config.init_data = pdata->regulators[i].initdata;
 876                config.driver_data = max8998;
 877
 878                rdev[i] = regulator_register(&regulators[index], &config);
 879                if (IS_ERR(rdev[i])) {
 880                        ret = PTR_ERR(rdev[i]);
 881                        dev_err(max8998->dev, "regulator %s init failed (%d)\n",
 882                                                regulators[index].name, ret);
 883                        rdev[i] = NULL;
 884                        goto err;
 885                }
 886        }
 887
 888
 889        return 0;
 890err:
 891        while (--i >= 0)
 892                regulator_unregister(rdev[i]);
 893err_out:
 894        return ret;
 895}
 896
 897static int max8998_pmic_remove(struct platform_device *pdev)
 898{
 899        struct max8998_data *max8998 = platform_get_drvdata(pdev);
 900        struct regulator_dev **rdev = max8998->rdev;
 901        int i;
 902
 903        for (i = 0; i < max8998->num_regulators; i++)
 904                regulator_unregister(rdev[i]);
 905        return 0;
 906}
 907
 908static const struct platform_device_id max8998_pmic_id[] = {
 909        { "max8998-pmic", TYPE_MAX8998 },
 910        { "lp3974-pmic", TYPE_LP3974 },
 911        { }
 912};
 913MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
 914
 915static struct platform_driver max8998_pmic_driver = {
 916        .driver = {
 917                .name = "max8998-pmic",
 918                .owner = THIS_MODULE,
 919        },
 920        .probe = max8998_pmic_probe,
 921        .remove = max8998_pmic_remove,
 922        .id_table = max8998_pmic_id,
 923};
 924
 925static int __init max8998_pmic_init(void)
 926{
 927        return platform_driver_register(&max8998_pmic_driver);
 928}
 929subsys_initcall(max8998_pmic_init);
 930
 931static void __exit max8998_pmic_cleanup(void)
 932{
 933        platform_driver_unregister(&max8998_pmic_driver);
 934}
 935module_exit(max8998_pmic_cleanup);
 936
 937MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
 938MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
 939MODULE_LICENSE("GPL");
 940
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.