linux/drivers/regulator/max8997.c
<<
>>
Prefs
   1/*
   2 * max8997.c - Regulator driver for the Maxim 8997/8966
   3 *
   4 * Copyright (C) 2011 Samsung Electronics
   5 * MyungJoo Ham <myungjoo.ham@smasung.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 *
  21 * This driver is based on max8998.c
  22 */
  23
  24#include <linux/bug.h>
  25#include <linux/err.h>
  26#include <linux/gpio.h>
  27#include <linux/slab.h>
  28#include <linux/module.h>
  29#include <linux/platform_device.h>
  30#include <linux/regulator/driver.h>
  31#include <linux/regulator/machine.h>
  32#include <linux/mfd/max8997.h>
  33#include <linux/mfd/max8997-private.h>
  34
  35struct max8997_data {
  36        struct device *dev;
  37        struct max8997_dev *iodev;
  38        int num_regulators;
  39        struct regulator_dev **rdev;
  40        int ramp_delay; /* in mV/us */
  41
  42        bool buck1_gpiodvs;
  43        bool buck2_gpiodvs;
  44        bool buck5_gpiodvs;
  45        u8 buck1_vol[8];
  46        u8 buck2_vol[8];
  47        u8 buck5_vol[8];
  48        int buck125_gpios[3];
  49        int buck125_gpioindex;
  50        bool ignore_gpiodvs_side_effect;
  51
  52        u8 saved_states[MAX8997_REG_MAX];
  53};
  54
  55static inline void max8997_set_gpio(struct max8997_data *max8997)
  56{
  57        int set3 = (max8997->buck125_gpioindex) & 0x1;
  58        int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
  59        int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
  60
  61        gpio_set_value(max8997->buck125_gpios[0], set1);
  62        gpio_set_value(max8997->buck125_gpios[1], set2);
  63        gpio_set_value(max8997->buck125_gpios[2], set3);
  64}
  65
  66struct voltage_map_desc {
  67        int min;
  68        int max;
  69        int step;
  70};
  71
  72/* Voltage maps in uV */
  73static const struct voltage_map_desc ldo_voltage_map_desc = {
  74        .min = 800000,  .max = 3950000, .step = 50000,
  75}; /* LDO1 ~ 18, 21 all */
  76
  77static const struct voltage_map_desc buck1245_voltage_map_desc = {
  78        .min = 650000,  .max = 2225000, .step = 25000,
  79}; /* Buck1, 2, 4, 5 */
  80
  81static const struct voltage_map_desc buck37_voltage_map_desc = {
  82        .min = 750000,  .max = 3900000, .step = 50000,
  83}; /* Buck3, 7 */
  84
  85/* current map in uA */
  86static const struct voltage_map_desc charger_current_map_desc = {
  87        .min = 200000,  .max = 950000,  .step = 50000,
  88};
  89
  90static const struct voltage_map_desc topoff_current_map_desc = {
  91        .min = 50000,   .max = 200000,  .step = 10000,
  92};
  93
  94static const struct voltage_map_desc *reg_voltage_map[] = {
  95        [MAX8997_LDO1] = &ldo_voltage_map_desc,
  96        [MAX8997_LDO2] = &ldo_voltage_map_desc,
  97        [MAX8997_LDO3] = &ldo_voltage_map_desc,
  98        [MAX8997_LDO4] = &ldo_voltage_map_desc,
  99        [MAX8997_LDO5] = &ldo_voltage_map_desc,
 100        [MAX8997_LDO6] = &ldo_voltage_map_desc,
 101        [MAX8997_LDO7] = &ldo_voltage_map_desc,
 102        [MAX8997_LDO8] = &ldo_voltage_map_desc,
 103        [MAX8997_LDO9] = &ldo_voltage_map_desc,
 104        [MAX8997_LDO10] = &ldo_voltage_map_desc,
 105        [MAX8997_LDO11] = &ldo_voltage_map_desc,
 106        [MAX8997_LDO12] = &ldo_voltage_map_desc,
 107        [MAX8997_LDO13] = &ldo_voltage_map_desc,
 108        [MAX8997_LDO14] = &ldo_voltage_map_desc,
 109        [MAX8997_LDO15] = &ldo_voltage_map_desc,
 110        [MAX8997_LDO16] = &ldo_voltage_map_desc,
 111        [MAX8997_LDO17] = &ldo_voltage_map_desc,
 112        [MAX8997_LDO18] = &ldo_voltage_map_desc,
 113        [MAX8997_LDO21] = &ldo_voltage_map_desc,
 114        [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
 115        [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
 116        [MAX8997_BUCK3] = &buck37_voltage_map_desc,
 117        [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
 118        [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
 119        [MAX8997_BUCK6] = NULL,
 120        [MAX8997_BUCK7] = &buck37_voltage_map_desc,
 121        [MAX8997_EN32KHZ_AP] = NULL,
 122        [MAX8997_EN32KHZ_CP] = NULL,
 123        [MAX8997_ENVICHG] = NULL,
 124        [MAX8997_ESAFEOUT1] = NULL,
 125        [MAX8997_ESAFEOUT2] = NULL,
 126        [MAX8997_CHARGER_CV] = NULL,
 127        [MAX8997_CHARGER] = &charger_current_map_desc,
 128        [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
 129};
 130
 131static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
 132                unsigned int selector)
 133{
 134        int rid = rdev_get_id(rdev);
 135
 136        if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
 137                switch (selector) {
 138                case 0:
 139                        return 4850000;
 140                case 1:
 141                        return 4900000;
 142                case 2:
 143                        return 4950000;
 144                case 3:
 145                        return 3300000;
 146                default:
 147                        return -EINVAL;
 148                }
 149        }
 150
 151        return -EINVAL;
 152}
 153
 154static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
 155                unsigned int selector)
 156{
 157        int rid = rdev_get_id(rdev);
 158
 159        if (rid != MAX8997_CHARGER_CV)
 160                goto err;
 161
 162        switch (selector) {
 163        case 0x00:
 164                return 4200000;
 165        case 0x01 ... 0x0E:
 166                return 4000000 + 20000 * (selector - 0x01);
 167        case 0x0F:
 168                return 4350000;
 169        default:
 170                return -EINVAL;
 171        }
 172err:
 173        return -EINVAL;
 174}
 175
 176static int max8997_list_voltage(struct regulator_dev *rdev,
 177                unsigned int selector)
 178{
 179        const struct voltage_map_desc *desc;
 180        int rid = rdev_get_id(rdev);
 181        int val;
 182
 183        if (rid >= ARRAY_SIZE(reg_voltage_map) ||
 184                        rid < 0)
 185                return -EINVAL;
 186
 187        desc = reg_voltage_map[rid];
 188        if (desc == NULL)
 189                return -EINVAL;
 190
 191        val = desc->min + desc->step * selector;
 192        if (val > desc->max)
 193                return -EINVAL;
 194
 195        return val;
 196}
 197
 198static int max8997_get_enable_register(struct regulator_dev *rdev,
 199                int *reg, int *mask, int *pattern)
 200{
 201        int rid = rdev_get_id(rdev);
 202
 203        switch (rid) {
 204        case MAX8997_LDO1 ... MAX8997_LDO21:
 205                *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
 206                *mask = 0xC0;
 207                *pattern = 0xC0;
 208                break;
 209        case MAX8997_BUCK1:
 210                *reg = MAX8997_REG_BUCK1CTRL;
 211                *mask = 0x01;
 212                *pattern = 0x01;
 213                break;
 214        case MAX8997_BUCK2:
 215                *reg = MAX8997_REG_BUCK2CTRL;
 216                *mask = 0x01;
 217                *pattern = 0x01;
 218                break;
 219        case MAX8997_BUCK3:
 220                *reg = MAX8997_REG_BUCK3CTRL;
 221                *mask = 0x01;
 222                *pattern = 0x01;
 223                break;
 224        case MAX8997_BUCK4:
 225                *reg = MAX8997_REG_BUCK4CTRL;
 226                *mask = 0x01;
 227                *pattern = 0x01;
 228                break;
 229        case MAX8997_BUCK5:
 230                *reg = MAX8997_REG_BUCK5CTRL;
 231                *mask = 0x01;
 232                *pattern = 0x01;
 233                break;
 234        case MAX8997_BUCK6:
 235                *reg = MAX8997_REG_BUCK6CTRL;
 236                *mask = 0x01;
 237                *pattern = 0x01;
 238                break;
 239        case MAX8997_BUCK7:
 240                *reg = MAX8997_REG_BUCK7CTRL;
 241                *mask = 0x01;
 242                *pattern = 0x01;
 243                break;
 244        case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
 245                *reg = MAX8997_REG_MAINCON1;
 246                *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
 247                *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
 248                break;
 249        case MAX8997_ENVICHG:
 250                *reg = MAX8997_REG_MBCCTRL1;
 251                *mask = 0x80;
 252                *pattern = 0x80;
 253                break;
 254        case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
 255                *reg = MAX8997_REG_SAFEOUTCTRL;
 256                *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
 257                *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
 258                break;
 259        case MAX8997_CHARGER:
 260                *reg = MAX8997_REG_MBCCTRL2;
 261                *mask = 0x40;
 262                *pattern = 0x40;
 263                break;
 264        default:
 265                /* Not controllable or not exists */
 266                return -EINVAL;
 267        }
 268
 269        return 0;
 270}
 271
 272static int max8997_reg_is_enabled(struct regulator_dev *rdev)
 273{
 274        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 275        struct i2c_client *i2c = max8997->iodev->i2c;
 276        int ret, reg, mask, pattern;
 277        u8 val;
 278
 279        ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
 280        if (ret)
 281                return ret;
 282
 283        ret = max8997_read_reg(i2c, reg, &val);
 284        if (ret)
 285                return ret;
 286
 287        return (val & mask) == pattern;
 288}
 289
 290static int max8997_reg_enable(struct regulator_dev *rdev)
 291{
 292        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 293        struct i2c_client *i2c = max8997->iodev->i2c;
 294        int ret, reg, mask, pattern;
 295
 296        ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
 297        if (ret)
 298                return ret;
 299
 300        return max8997_update_reg(i2c, reg, pattern, mask);
 301}
 302
 303static int max8997_reg_disable(struct regulator_dev *rdev)
 304{
 305        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 306        struct i2c_client *i2c = max8997->iodev->i2c;
 307        int ret, reg, mask, pattern;
 308
 309        ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
 310        if (ret)
 311                return ret;
 312
 313        return max8997_update_reg(i2c, reg, ~pattern, mask);
 314}
 315
 316static int max8997_get_voltage_register(struct regulator_dev *rdev,
 317                int *_reg, int *_shift, int *_mask)
 318{
 319        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 320        int rid = rdev_get_id(rdev);
 321        int reg, shift = 0, mask = 0x3f;
 322
 323        switch (rid) {
 324        case MAX8997_LDO1 ... MAX8997_LDO21:
 325                reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
 326                break;
 327        case MAX8997_BUCK1:
 328                reg = MAX8997_REG_BUCK1DVS1;
 329                if (max8997->buck1_gpiodvs)
 330                        reg += max8997->buck125_gpioindex;
 331                break;
 332        case MAX8997_BUCK2:
 333                reg = MAX8997_REG_BUCK2DVS1;
 334                if (max8997->buck2_gpiodvs)
 335                        reg += max8997->buck125_gpioindex;
 336                break;
 337        case MAX8997_BUCK3:
 338                reg = MAX8997_REG_BUCK3DVS;
 339                break;
 340        case MAX8997_BUCK4:
 341                reg = MAX8997_REG_BUCK4DVS;
 342                break;
 343        case MAX8997_BUCK5:
 344                reg = MAX8997_REG_BUCK5DVS1;
 345                if (max8997->buck5_gpiodvs)
 346                        reg += max8997->buck125_gpioindex;
 347                break;
 348        case MAX8997_BUCK7:
 349                reg = MAX8997_REG_BUCK7DVS;
 350                break;
 351        case MAX8997_ESAFEOUT1 ...  MAX8997_ESAFEOUT2:
 352                reg = MAX8997_REG_SAFEOUTCTRL;
 353                shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
 354                mask = 0x3;
 355                break;
 356        case MAX8997_CHARGER_CV:
 357                reg = MAX8997_REG_MBCCTRL3;
 358                shift = 0;
 359                mask = 0xf;
 360                break;
 361        case MAX8997_CHARGER:
 362                reg = MAX8997_REG_MBCCTRL4;
 363                shift = 0;
 364                mask = 0xf;
 365                break;
 366        case MAX8997_CHARGER_TOPOFF:
 367                reg = MAX8997_REG_MBCCTRL5;
 368                shift = 0;
 369                mask = 0xf;
 370                break;
 371        default:
 372                return -EINVAL;
 373        }
 374
 375        *_reg = reg;
 376        *_shift = shift;
 377        *_mask = mask;
 378
 379        return 0;
 380}
 381
 382static int max8997_get_voltage_sel(struct regulator_dev *rdev)
 383{
 384        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 385        struct i2c_client *i2c = max8997->iodev->i2c;
 386        int reg, shift, mask, ret;
 387        u8 val;
 388
 389        ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
 390        if (ret)
 391                return ret;
 392
 393        ret = max8997_read_reg(i2c, reg, &val);
 394        if (ret)
 395                return ret;
 396
 397        val >>= shift;
 398        val &= mask;
 399
 400        return val;
 401}
 402
 403static inline int max8997_get_voltage_proper_val(
 404                const struct voltage_map_desc *desc,
 405                int min_vol, int max_vol)
 406{
 407        int i;
 408
 409        if (desc == NULL)
 410                return -EINVAL;
 411
 412        if (max_vol < desc->min || min_vol > desc->max)
 413                return -EINVAL;
 414
 415        if (min_vol < desc->min)
 416                min_vol = desc->min;
 417
 418        i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
 419
 420        if (desc->min + desc->step * i > max_vol)
 421                return -EINVAL;
 422
 423        return i;
 424}
 425
 426static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
 427                int min_uV, int max_uV, unsigned *selector)
 428{
 429        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 430        struct i2c_client *i2c = max8997->iodev->i2c;
 431        int rid = rdev_get_id(rdev);
 432        int lb, ub;
 433        int reg, shift = 0, mask, ret = 0;
 434        u8 val = 0x0;
 435
 436        if (rid != MAX8997_CHARGER_CV)
 437                return -EINVAL;
 438
 439        ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
 440        if (ret)
 441                return ret;
 442
 443        if (max_uV < 4000000 || min_uV > 4350000)
 444                return -EINVAL;
 445
 446        if (min_uV <= 4000000) {
 447                if (max_uV >= 4000000)
 448                        return -EINVAL;
 449                else
 450                        val = 0x1;
 451        } else if (min_uV <= 4200000 && max_uV >= 4200000)
 452                val = 0x0;
 453        else {
 454                lb = (min_uV - 4000001) / 20000 + 2;
 455                ub = (max_uV - 4000000) / 20000 + 1;
 456
 457                if (lb > ub)
 458                        return -EINVAL;
 459
 460                if (lb < 0xf)
 461                        val = lb;
 462                else {
 463                        if (ub >= 0xf)
 464                                val = 0xf;
 465                        else
 466                                return -EINVAL;
 467                }
 468        }
 469
 470        *selector = val;
 471
 472        ret = max8997_update_reg(i2c, reg, val << shift, mask);
 473
 474        return ret;
 475}
 476
 477/*
 478 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
 479 * BUCK1, 2, and 5 are available if they are not controlled by gpio
 480 */
 481static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
 482                int min_uV, int max_uV, unsigned *selector)
 483{
 484        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 485        struct i2c_client *i2c = max8997->iodev->i2c;
 486        const struct voltage_map_desc *desc;
 487        int rid = rdev_get_id(rdev);
 488        int i, reg, shift, mask, ret;
 489
 490        switch (rid) {
 491        case MAX8997_LDO1 ... MAX8997_LDO21:
 492                break;
 493        case MAX8997_BUCK1 ... MAX8997_BUCK5:
 494                break;
 495        case MAX8997_BUCK6:
 496                return -EINVAL;
 497        case MAX8997_BUCK7:
 498                break;
 499        case MAX8997_CHARGER:
 500                break;
 501        case MAX8997_CHARGER_TOPOFF:
 502                break;
 503        default:
 504                return -EINVAL;
 505        }
 506
 507        desc = reg_voltage_map[rid];
 508
 509        i = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
 510        if (i < 0)
 511                return i;
 512
 513        ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
 514        if (ret)
 515                return ret;
 516
 517        ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
 518        *selector = i;
 519
 520        return ret;
 521}
 522
 523static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
 524                                                unsigned int old_selector,
 525                                                unsigned int new_selector)
 526{
 527        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 528        int rid = rdev_get_id(rdev);
 529        const struct voltage_map_desc *desc = reg_voltage_map[rid];
 530
 531        /* Delay is required only if the voltage is increasing */
 532        if (old_selector >= new_selector)
 533                return 0;
 534
 535        /* No need to delay if gpio_dvs_mode */
 536        switch (rid) {
 537        case MAX8997_BUCK1:
 538                if (max8997->buck1_gpiodvs)
 539                        return 0;
 540                break;
 541        case MAX8997_BUCK2:
 542                if (max8997->buck2_gpiodvs)
 543                        return 0;
 544                break;
 545        case MAX8997_BUCK5:
 546                if (max8997->buck5_gpiodvs)
 547                        return 0;
 548                break;
 549        }
 550
 551        switch (rid) {
 552        case MAX8997_BUCK1:
 553        case MAX8997_BUCK2:
 554        case MAX8997_BUCK4:
 555        case MAX8997_BUCK5:
 556                return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
 557                                    max8997->ramp_delay * 1000);
 558        }
 559
 560        return 0;
 561}
 562
 563/*
 564 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
 565 *
 566 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
 567 * of one of the bucks may affect that of another buck, which is the side
 568 * effect of the change (set_voltage). This function examines the GPIO-DVS
 569 * configurations and checks whether such side-effect exists.
 570 */
 571static int max8997_assess_side_effect(struct regulator_dev *rdev,
 572                u8 new_val, int *best)
 573{
 574        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 575        int rid = rdev_get_id(rdev);
 576        u8 *buckx_val[3];
 577        bool buckx_gpiodvs[3];
 578        int side_effect[8];
 579        int min_side_effect = INT_MAX;
 580        int i;
 581
 582        *best = -1;
 583
 584        switch (rid) {
 585        case MAX8997_BUCK1:
 586                rid = 0;
 587                break;
 588        case MAX8997_BUCK2:
 589                rid = 1;
 590                break;
 591        case MAX8997_BUCK5:
 592                rid = 2;
 593                break;
 594        default:
 595                return -EINVAL;
 596        }
 597
 598        buckx_val[0] = max8997->buck1_vol;
 599        buckx_val[1] = max8997->buck2_vol;
 600        buckx_val[2] = max8997->buck5_vol;
 601        buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
 602        buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
 603        buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
 604
 605        for (i = 0; i < 8; i++) {
 606                int others;
 607
 608                if (new_val != (buckx_val[rid])[i]) {
 609                        side_effect[i] = -1;
 610                        continue;
 611                }
 612
 613                side_effect[i] = 0;
 614                for (others = 0; others < 3; others++) {
 615                        int diff;
 616
 617                        if (others == rid)
 618                                continue;
 619                        if (buckx_gpiodvs[others] == false)
 620                                continue; /* Not affected */
 621                        diff = (buckx_val[others])[i] -
 622                                (buckx_val[others])[max8997->buck125_gpioindex];
 623                        if (diff > 0)
 624                                side_effect[i] += diff;
 625                        else if (diff < 0)
 626                                side_effect[i] -= diff;
 627                }
 628                if (side_effect[i] == 0) {
 629                        *best = i;
 630                        return 0; /* NO SIDE EFFECT! Use This! */
 631                }
 632                if (side_effect[i] < min_side_effect) {
 633                        min_side_effect = side_effect[i];
 634                        *best = i;
 635                }
 636        }
 637
 638        if (*best == -1)
 639                return -EINVAL;
 640
 641        return side_effect[*best];
 642}
 643
 644/*
 645 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
 646 * max8997_set_voltage_ldobuck to do the job.
 647 */
 648static int max8997_set_voltage_buck(struct regulator_dev *rdev,
 649                int min_uV, int max_uV, unsigned *selector)
 650{
 651        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 652        int rid = rdev_get_id(rdev);
 653        const struct voltage_map_desc *desc;
 654        int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
 655        bool gpio_dvs_mode = false;
 656
 657        if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
 658                return -EINVAL;
 659
 660        switch (rid) {
 661        case MAX8997_BUCK1:
 662                if (max8997->buck1_gpiodvs)
 663                        gpio_dvs_mode = true;
 664                break;
 665        case MAX8997_BUCK2:
 666                if (max8997->buck2_gpiodvs)
 667                        gpio_dvs_mode = true;
 668                break;
 669        case MAX8997_BUCK5:
 670                if (max8997->buck5_gpiodvs)
 671                        gpio_dvs_mode = true;
 672                break;
 673        }
 674
 675        if (!gpio_dvs_mode)
 676                return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
 677                                                selector);
 678
 679        desc = reg_voltage_map[rid];
 680        new_val = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
 681        if (new_val < 0)
 682                return new_val;
 683
 684        tmp_dmg = INT_MAX;
 685        tmp_idx = -1;
 686        tmp_val = -1;
 687        do {
 688                damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
 689                if (damage == 0)
 690                        goto out;
 691
 692                if (tmp_dmg > damage) {
 693                        tmp_idx = new_idx;
 694                        tmp_val = new_val;
 695                        tmp_dmg = damage;
 696                }
 697
 698                new_val++;
 699        } while (desc->min + desc->step * new_val <= desc->max);
 700
 701        new_idx = tmp_idx;
 702        new_val = tmp_val;
 703
 704        if (max8997->ignore_gpiodvs_side_effect == false)
 705                return -EINVAL;
 706
 707        dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
 708                        " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
 709
 710out:
 711        if (new_idx < 0 || new_val < 0)
 712                return -EINVAL;
 713
 714        max8997->buck125_gpioindex = new_idx;
 715        max8997_set_gpio(max8997);
 716        *selector = new_val;
 717
 718        return 0;
 719}
 720
 721static const int safeoutvolt[] = {
 722        3300000,
 723        4850000,
 724        4900000,
 725        4950000,
 726};
 727
 728/* For SAFEOUT1 and SAFEOUT2 */
 729static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
 730                int min_uV, int max_uV, unsigned *selector)
 731{
 732        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 733        struct i2c_client *i2c = max8997->iodev->i2c;
 734        int rid = rdev_get_id(rdev);
 735        int reg, shift = 0, mask, ret;
 736        int i = 0;
 737        u8 val;
 738
 739        if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
 740                return -EINVAL;
 741
 742        for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
 743                if (min_uV <= safeoutvolt[i] &&
 744                                max_uV >= safeoutvolt[i])
 745                        break;
 746        }
 747
 748        if (i >= ARRAY_SIZE(safeoutvolt))
 749                return -EINVAL;
 750
 751        if (i == 0)
 752                val = 0x3;
 753        else
 754                val = i - 1;
 755
 756        ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
 757        if (ret)
 758                return ret;
 759
 760        ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
 761        *selector = val;
 762
 763        return ret;
 764}
 765
 766static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
 767{
 768        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 769        struct i2c_client *i2c = max8997->iodev->i2c;
 770        int ret, reg, mask, pattern;
 771        int rid = rdev_get_id(rdev);
 772
 773        ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
 774        if (ret)
 775                return ret;
 776
 777        max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
 778
 779        if (rid == MAX8997_LDO1 ||
 780                        rid == MAX8997_LDO10 ||
 781                        rid == MAX8997_LDO21) {
 782                dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
 783                                rdev->desc->name);
 784                return max8997_update_reg(i2c, reg, 0x40, mask);
 785        }
 786
 787        dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
 788                        rdev->desc->name, max8997->saved_states[rid] & mask,
 789                        (~pattern) & mask);
 790        return max8997_update_reg(i2c, reg, ~pattern, mask);
 791}
 792
 793static struct regulator_ops max8997_ldo_ops = {
 794        .list_voltage           = max8997_list_voltage,
 795        .is_enabled             = max8997_reg_is_enabled,
 796        .enable                 = max8997_reg_enable,
 797        .disable                = max8997_reg_disable,
 798        .get_voltage_sel        = max8997_get_voltage_sel,
 799        .set_voltage            = max8997_set_voltage_ldobuck,
 800        .set_voltage_time_sel   = max8997_set_voltage_ldobuck_time_sel,
 801        .set_suspend_disable    = max8997_reg_disable_suspend,
 802};
 803
 804static struct regulator_ops max8997_buck_ops = {
 805        .list_voltage           = max8997_list_voltage,
 806        .is_enabled             = max8997_reg_is_enabled,
 807        .enable                 = max8997_reg_enable,
 808        .disable                = max8997_reg_disable,
 809        .get_voltage_sel        = max8997_get_voltage_sel,
 810        .set_voltage            = max8997_set_voltage_buck,
 811        .set_voltage_time_sel   = max8997_set_voltage_ldobuck_time_sel,
 812        .set_suspend_disable    = max8997_reg_disable_suspend,
 813};
 814
 815static struct regulator_ops max8997_fixedvolt_ops = {
 816        .list_voltage           = max8997_list_voltage,
 817        .is_enabled             = max8997_reg_is_enabled,
 818        .enable                 = max8997_reg_enable,
 819        .disable                = max8997_reg_disable,
 820        .set_suspend_disable    = max8997_reg_disable_suspend,
 821};
 822
 823static struct regulator_ops max8997_safeout_ops = {
 824        .list_voltage           = max8997_list_voltage_safeout,
 825        .is_enabled             = max8997_reg_is_enabled,
 826        .enable                 = max8997_reg_enable,
 827        .disable                = max8997_reg_disable,
 828        .get_voltage_sel        = max8997_get_voltage_sel,
 829        .set_voltage            = max8997_set_voltage_safeout,
 830        .set_suspend_disable    = max8997_reg_disable_suspend,
 831};
 832
 833static struct regulator_ops max8997_fixedstate_ops = {
 834        .list_voltage           = max8997_list_voltage_charger_cv,
 835        .get_voltage_sel        = max8997_get_voltage_sel,
 836        .set_voltage            = max8997_set_voltage_charger_cv,
 837};
 838
 839static int max8997_set_current_limit(struct regulator_dev *rdev,
 840                                     int min_uA, int max_uA)
 841{
 842        unsigned dummy;
 843        int rid = rdev_get_id(rdev);
 844
 845        if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
 846                return -EINVAL;
 847
 848        /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
 849        return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
 850}
 851
 852static int max8997_get_current_limit(struct regulator_dev *rdev)
 853{
 854        int sel, rid = rdev_get_id(rdev);
 855
 856        if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
 857                return -EINVAL;
 858
 859        sel = max8997_get_voltage_sel(rdev);
 860        if (sel < 0)
 861                return sel;
 862
 863        /* Reuse max8997_list_voltage to get current_limit. */
 864        return max8997_list_voltage(rdev, sel);
 865}
 866
 867static struct regulator_ops max8997_charger_ops = {
 868        .is_enabled             = max8997_reg_is_enabled,
 869        .enable                 = max8997_reg_enable,
 870        .disable                = max8997_reg_disable,
 871        .get_current_limit      = max8997_get_current_limit,
 872        .set_current_limit      = max8997_set_current_limit,
 873};
 874
 875static struct regulator_ops max8997_charger_fixedstate_ops = {
 876        .get_current_limit      = max8997_get_current_limit,
 877        .set_current_limit      = max8997_set_current_limit,
 878};
 879
 880#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
 881        .name           = #_name,               \
 882        .id             = MAX8997_##_name,      \
 883        .ops            = &_ops,                \
 884        .type           = REGULATOR_VOLTAGE,    \
 885        .owner          = THIS_MODULE,          \
 886}
 887
 888#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
 889        .name           = #_name,               \
 890        .id             = MAX8997_##_name,      \
 891        .ops            = &_ops,                \
 892        .type           = REGULATOR_CURRENT,    \
 893        .owner          = THIS_MODULE,          \
 894}
 895
 896static struct regulator_desc regulators[] = {
 897        MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
 898        MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
 899        MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
 900        MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
 901        MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
 902        MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
 903        MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
 904        MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
 905        MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
 906        MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
 907        MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
 908        MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
 909        MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
 910        MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
 911        MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
 912        MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
 913        MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
 914        MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
 915        MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
 916        MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
 917        MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
 918        MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
 919        MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
 920        MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
 921        MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
 922        MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
 923        MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
 924        MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
 925        MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
 926        MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
 927        MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
 928        MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
 929        MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
 930        MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
 931                                  max8997_charger_fixedstate_ops),
 932};
 933
 934static __devinit int max8997_pmic_probe(struct platform_device *pdev)
 935{
 936        struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
 937        struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
 938        struct regulator_config config = { };
 939        struct regulator_dev **rdev;
 940        struct max8997_data *max8997;
 941        struct i2c_client *i2c;
 942        int i, ret, size;
 943        u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
 944
 945        if (!pdata) {
 946                dev_err(pdev->dev.parent, "No platform init data supplied.\n");
 947                return -ENODEV;
 948        }
 949
 950        max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
 951                               GFP_KERNEL);
 952        if (!max8997)
 953                return -ENOMEM;
 954
 955        size = sizeof(struct regulator_dev *) * pdata->num_regulators;
 956        max8997->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
 957        if (!max8997->rdev)
 958                return -ENOMEM;
 959
 960        rdev = max8997->rdev;
 961        max8997->dev = &pdev->dev;
 962        max8997->iodev = iodev;
 963        max8997->num_regulators = pdata->num_regulators;
 964        platform_set_drvdata(pdev, max8997);
 965        i2c = max8997->iodev->i2c;
 966
 967        max8997->buck125_gpioindex = pdata->buck125_default_idx;
 968        max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
 969        max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
 970        max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
 971        memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
 972        max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
 973
 974        for (i = 0; i < 8; i++) {
 975                max8997->buck1_vol[i] = ret =
 976                        max8997_get_voltage_proper_val(
 977                                        &buck1245_voltage_map_desc,
 978                                        pdata->buck1_voltage[i],
 979                                        pdata->buck1_voltage[i] +
 980                                        buck1245_voltage_map_desc.step);
 981                if (ret < 0)
 982                        goto err_out;
 983
 984                max8997->buck2_vol[i] = ret =
 985                        max8997_get_voltage_proper_val(
 986                                        &buck1245_voltage_map_desc,
 987                                        pdata->buck2_voltage[i],
 988                                        pdata->buck2_voltage[i] +
 989                                        buck1245_voltage_map_desc.step);
 990                if (ret < 0)
 991                        goto err_out;
 992
 993                max8997->buck5_vol[i] = ret =
 994                        max8997_get_voltage_proper_val(
 995                                        &buck1245_voltage_map_desc,
 996                                        pdata->buck5_voltage[i],
 997                                        pdata->buck5_voltage[i] +
 998                                        buck1245_voltage_map_desc.step);
 999                if (ret < 0)
1000                        goto err_out;
1001
1002                if (max_buck1 < max8997->buck1_vol[i])
1003                        max_buck1 = max8997->buck1_vol[i];
1004                if (max_buck2 < max8997->buck2_vol[i])
1005                        max_buck2 = max8997->buck2_vol[i];
1006                if (max_buck5 < max8997->buck5_vol[i])
1007                        max_buck5 = max8997->buck5_vol[i];
1008        }
1009
1010        /* For the safety, set max voltage before setting up */
1011        for (i = 0; i < 8; i++) {
1012                max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1013                                max_buck1, 0x3f);
1014                max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1015                                max_buck2, 0x3f);
1016                max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1017                                max_buck5, 0x3f);
1018        }
1019
1020        /*
1021         * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1022         * If at least one of them cares, set gpios.
1023         */
1024        if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1025                        pdata->buck5_gpiodvs) {
1026
1027                if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1028                                !gpio_is_valid(pdata->buck125_gpios[1]) ||
1029                                !gpio_is_valid(pdata->buck125_gpios[2])) {
1030                        dev_err(&pdev->dev, "GPIO NOT VALID\n");
1031                        ret = -EINVAL;
1032                        goto err_out;
1033                }
1034
1035                ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0],
1036                                        "MAX8997 SET1");
1037                if (ret)
1038                        goto err_out;
1039
1040                ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1],
1041                                        "MAX8997 SET2");
1042                if (ret)
1043                        goto err_out;
1044
1045                ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2],
1046                                "MAX8997 SET3");
1047                if (ret)
1048                        goto err_out;
1049
1050                gpio_direction_output(pdata->buck125_gpios[0],
1051                                (max8997->buck125_gpioindex >> 2)
1052                                & 0x1); /* SET1 */
1053                gpio_direction_output(pdata->buck125_gpios[1],
1054                                (max8997->buck125_gpioindex >> 1)
1055                                & 0x1); /* SET2 */
1056                gpio_direction_output(pdata->buck125_gpios[2],
1057                                (max8997->buck125_gpioindex >> 0)
1058                                & 0x1); /* SET3 */
1059        }
1060
1061        /* DVS-GPIO disabled */
1062        max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1063                        (1 << 1) : (0 << 1), 1 << 1);
1064        max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1065                        (1 << 1) : (0 << 1), 1 << 1);
1066        max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1067                        (1 << 1) : (0 << 1), 1 << 1);
1068
1069        /* Initialize all the DVS related BUCK registers */
1070        for (i = 0; i < 8; i++) {
1071                max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1072                                max8997->buck1_vol[i],
1073                                0x3f);
1074                max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1075                                max8997->buck2_vol[i],
1076                                0x3f);
1077                max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1078                                max8997->buck5_vol[i],
1079                                0x3f);
1080        }
1081
1082        /* Misc Settings */
1083        max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1084        max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1085
1086        for (i = 0; i < pdata->num_regulators; i++) {
1087                const struct voltage_map_desc *desc;
1088                int id = pdata->regulators[i].id;
1089
1090                desc = reg_voltage_map[id];
1091                if (desc)
1092                        regulators[id].n_voltages =
1093                                (desc->max - desc->min) / desc->step + 1;
1094                else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1095                        regulators[id].n_voltages = 4;
1096                else if (id == MAX8997_CHARGER_CV)
1097                        regulators[id].n_voltages = 16;
1098
1099                config.dev = max8997->dev;
1100                config.init_data = pdata->regulators[i].initdata;
1101                config.driver_data = max8997;
1102
1103                rdev[i] = regulator_register(&regulators[id], &config);
1104                if (IS_ERR(rdev[i])) {
1105                        ret = PTR_ERR(rdev[i]);
1106                        dev_err(max8997->dev, "regulator init failed for %d\n",
1107                                        id);
1108                        rdev[i] = NULL;
1109                        goto err;
1110                }
1111        }
1112
1113        return 0;
1114err:
1115        while (--i >= 0)
1116                regulator_unregister(rdev[i]);
1117err_out:
1118        return ret;
1119}
1120
1121static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1122{
1123        struct max8997_data *max8997 = platform_get_drvdata(pdev);
1124        struct regulator_dev **rdev = max8997->rdev;
1125        int i;
1126
1127        for (i = 0; i < max8997->num_regulators; i++)
1128                regulator_unregister(rdev[i]);
1129        return 0;
1130}
1131
1132static const struct platform_device_id max8997_pmic_id[] = {
1133        { "max8997-pmic", 0},
1134        { },
1135};
1136MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1137
1138static struct platform_driver max8997_pmic_driver = {
1139        .driver = {
1140                .name = "max8997-pmic",
1141                .owner = THIS_MODULE,
1142        },
1143        .probe = max8997_pmic_probe,
1144        .remove = __devexit_p(max8997_pmic_remove),
1145        .id_table = max8997_pmic_id,
1146};
1147
1148static int __init max8997_pmic_init(void)
1149{
1150        return platform_driver_register(&max8997_pmic_driver);
1151}
1152subsys_initcall(max8997_pmic_init);
1153
1154static void __exit max8997_pmic_cleanup(void)
1155{
1156        platform_driver_unregister(&max8997_pmic_driver);
1157}
1158module_exit(max8997_pmic_cleanup);
1159
1160MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1161MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1162MODULE_LICENSE("GPL");
1163
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.