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