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 mV */
  73static const struct voltage_map_desc ldo_voltage_map_desc = {
  74        .min = 800,     .max = 3950,    .step = 50,
  75}; /* LDO1 ~ 18, 21 all */
  76
  77static const struct voltage_map_desc buck1245_voltage_map_desc = {
  78        .min = 650,     .max = 2225,    .step = 25,
  79}; /* Buck1, 2, 4, 5 */
  80
  81static const struct voltage_map_desc buck37_voltage_map_desc = {
  82        .min = 750,     .max = 3900,    .step = 50,
  83}; /* Buck3, 7 */
  84
  85/* current map in mA */
  86static const struct voltage_map_desc charger_current_map_desc = {
  87        .min = 200,     .max = 950,     .step = 50,
  88};
  89
  90static const struct voltage_map_desc topoff_current_map_desc = {
  91        .min = 50,      .max = 200,     .step = 10,
  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 * 1000;
 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        int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
 487        const struct voltage_map_desc *desc;
 488        int rid = rdev_get_id(rdev);
 489        int i, reg, shift, mask, ret;
 490
 491        switch (rid) {
 492        case MAX8997_LDO1 ... MAX8997_LDO21:
 493                break;
 494        case MAX8997_BUCK1 ... MAX8997_BUCK5:
 495                break;
 496        case MAX8997_BUCK6:
 497                return -EINVAL;
 498        case MAX8997_BUCK7:
 499                break;
 500        case MAX8997_CHARGER:
 501                break;
 502        case MAX8997_CHARGER_TOPOFF:
 503                break;
 504        default:
 505                return -EINVAL;
 506        }
 507
 508        desc = reg_voltage_map[rid];
 509
 510        i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
 511        if (i < 0)
 512                return i;
 513
 514        ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
 515        if (ret)
 516                return ret;
 517
 518        ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
 519        *selector = i;
 520
 521        return ret;
 522}
 523
 524static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev *rdev,
 525                                                unsigned int old_selector,
 526                                                unsigned int new_selector)
 527{
 528        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 529        int rid = rdev_get_id(rdev);
 530        const struct voltage_map_desc *desc = reg_voltage_map[rid];
 531
 532        /* Delay is required only if the voltage is increasing */
 533        if (old_selector >= new_selector)
 534                return 0;
 535
 536        /* No need to delay if gpio_dvs_mode */
 537        switch (rid) {
 538        case MAX8997_BUCK1:
 539                if (max8997->buck1_gpiodvs)
 540                        return 0;
 541                break;
 542        case MAX8997_BUCK2:
 543                if (max8997->buck2_gpiodvs)
 544                        return 0;
 545                break;
 546        case MAX8997_BUCK5:
 547                if (max8997->buck5_gpiodvs)
 548                        return 0;
 549                break;
 550        }
 551
 552        switch (rid) {
 553        case MAX8997_BUCK1:
 554        case MAX8997_BUCK2:
 555        case MAX8997_BUCK4:
 556        case MAX8997_BUCK5:
 557                return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
 558                                    max8997->ramp_delay);
 559        }
 560
 561        return 0;
 562}
 563
 564/*
 565 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
 566 *
 567 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
 568 * of one of the bucks may affect that of another buck, which is the side
 569 * effect of the change (set_voltage). This function examines the GPIO-DVS
 570 * configurations and checks whether such side-effect exists.
 571 */
 572static int max8997_assess_side_effect(struct regulator_dev *rdev,
 573                u8 new_val, int *best)
 574{
 575        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 576        int rid = rdev_get_id(rdev);
 577        u8 *buckx_val[3];
 578        bool buckx_gpiodvs[3];
 579        int side_effect[8];
 580        int min_side_effect = INT_MAX;
 581        int i;
 582
 583        *best = -1;
 584
 585        switch (rid) {
 586        case MAX8997_BUCK1:
 587                rid = 0;
 588                break;
 589        case MAX8997_BUCK2:
 590                rid = 1;
 591                break;
 592        case MAX8997_BUCK5:
 593                rid = 2;
 594                break;
 595        default:
 596                return -EINVAL;
 597        }
 598
 599        buckx_val[0] = max8997->buck1_vol;
 600        buckx_val[1] = max8997->buck2_vol;
 601        buckx_val[2] = max8997->buck5_vol;
 602        buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
 603        buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
 604        buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
 605
 606        for (i = 0; i < 8; i++) {
 607                int others;
 608
 609                if (new_val != (buckx_val[rid])[i]) {
 610                        side_effect[i] = -1;
 611                        continue;
 612                }
 613
 614                side_effect[i] = 0;
 615                for (others = 0; others < 3; others++) {
 616                        int diff;
 617
 618                        if (others == rid)
 619                                continue;
 620                        if (buckx_gpiodvs[others] == false)
 621                                continue; /* Not affected */
 622                        diff = (buckx_val[others])[i] -
 623                                (buckx_val[others])[max8997->buck125_gpioindex];
 624                        if (diff > 0)
 625                                side_effect[i] += diff;
 626                        else if (diff < 0)
 627                                side_effect[i] -= diff;
 628                }
 629                if (side_effect[i] == 0) {
 630                        *best = i;
 631                        return 0; /* NO SIDE EFFECT! Use This! */
 632                }
 633                if (side_effect[i] < min_side_effect) {
 634                        min_side_effect = side_effect[i];
 635                        *best = i;
 636                }
 637        }
 638
 639        if (*best == -1)
 640                return -EINVAL;
 641
 642        return side_effect[*best];
 643}
 644
 645/*
 646 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
 647 * max8997_set_voltage_ldobuck to do the job.
 648 */
 649static int max8997_set_voltage_buck(struct regulator_dev *rdev,
 650                int min_uV, int max_uV, unsigned *selector)
 651{
 652        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 653        int rid = rdev_get_id(rdev);
 654        const struct voltage_map_desc *desc;
 655        int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
 656        bool gpio_dvs_mode = false;
 657        int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
 658
 659        if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
 660                return -EINVAL;
 661
 662        switch (rid) {
 663        case MAX8997_BUCK1:
 664                if (max8997->buck1_gpiodvs)
 665                        gpio_dvs_mode = true;
 666                break;
 667        case MAX8997_BUCK2:
 668                if (max8997->buck2_gpiodvs)
 669                        gpio_dvs_mode = true;
 670                break;
 671        case MAX8997_BUCK5:
 672                if (max8997->buck5_gpiodvs)
 673                        gpio_dvs_mode = true;
 674                break;
 675        }
 676
 677        if (!gpio_dvs_mode)
 678                return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
 679                                                selector);
 680
 681        desc = reg_voltage_map[rid];
 682        new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
 683        if (new_val < 0)
 684                return new_val;
 685
 686        tmp_dmg = INT_MAX;
 687        tmp_idx = -1;
 688        tmp_val = -1;
 689        do {
 690                damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
 691                if (damage == 0)
 692                        goto out;
 693
 694                if (tmp_dmg > damage) {
 695                        tmp_idx = new_idx;
 696                        tmp_val = new_val;
 697                        tmp_dmg = damage;
 698                }
 699
 700                new_val++;
 701        } while (desc->min + desc->step * new_val <= desc->max);
 702
 703        new_idx = tmp_idx;
 704        new_val = tmp_val;
 705
 706        if (max8997->ignore_gpiodvs_side_effect == false)
 707                return -EINVAL;
 708
 709        dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
 710                        " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
 711
 712out:
 713        if (new_idx < 0 || new_val < 0)
 714                return -EINVAL;
 715
 716        max8997->buck125_gpioindex = new_idx;
 717        max8997_set_gpio(max8997);
 718        *selector = new_val;
 719
 720        return 0;
 721}
 722
 723static const int safeoutvolt[] = {
 724        3300000,
 725        4850000,
 726        4900000,
 727        4950000,
 728};
 729
 730/* For SAFEOUT1 and SAFEOUT2 */
 731static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
 732                int min_uV, int max_uV, unsigned *selector)
 733{
 734        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 735        struct i2c_client *i2c = max8997->iodev->i2c;
 736        int rid = rdev_get_id(rdev);
 737        int reg, shift = 0, mask, ret;
 738        int i = 0;
 739        u8 val;
 740
 741        if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
 742                return -EINVAL;
 743
 744        for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
 745                if (min_uV <= safeoutvolt[i] &&
 746                                max_uV >= safeoutvolt[i])
 747                        break;
 748        }
 749
 750        if (i >= ARRAY_SIZE(safeoutvolt))
 751                return -EINVAL;
 752
 753        if (i == 0)
 754                val = 0x3;
 755        else
 756                val = i - 1;
 757
 758        ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
 759        if (ret)
 760                return ret;
 761
 762        ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
 763        *selector = val;
 764
 765        return ret;
 766}
 767
 768static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
 769{
 770        struct max8997_data *max8997 = rdev_get_drvdata(rdev);
 771        struct i2c_client *i2c = max8997->iodev->i2c;
 772        int ret, reg, mask, pattern;
 773        int rid = rdev_get_id(rdev);
 774
 775        ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
 776        if (ret)
 777                return ret;
 778
 779        max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
 780
 781        if (rid == MAX8997_LDO1 ||
 782                        rid == MAX8997_LDO10 ||
 783                        rid == MAX8997_LDO21) {
 784                dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
 785                                rdev->desc->name);
 786                return max8997_update_reg(i2c, reg, 0x40, mask);
 787        }
 788
 789        dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
 790                        rdev->desc->name, max8997->saved_states[rid] & mask,
 791                        (~pattern) & mask);
 792        return max8997_update_reg(i2c, reg, ~pattern, mask);
 793}
 794
 795static struct regulator_ops max8997_ldo_ops = {
 796        .list_voltage           = max8997_list_voltage,
 797        .is_enabled             = max8997_reg_is_enabled,
 798        .enable                 = max8997_reg_enable,
 799        .disable                = max8997_reg_disable,
 800        .get_voltage_sel        = max8997_get_voltage_sel,
 801        .set_voltage            = max8997_set_voltage_ldobuck,
 802        .set_voltage_time_sel   = max8997_set_voltage_ldobuck_time_sel,
 803        .set_suspend_disable    = max8997_reg_disable_suspend,
 804};
 805
 806static struct regulator_ops max8997_buck_ops = {
 807        .list_voltage           = max8997_list_voltage,
 808        .is_enabled             = max8997_reg_is_enabled,
 809        .enable                 = max8997_reg_enable,
 810        .disable                = max8997_reg_disable,
 811        .get_voltage_sel        = max8997_get_voltage_sel,
 812        .set_voltage            = max8997_set_voltage_buck,
 813        .set_voltage_time_sel   = max8997_set_voltage_ldobuck_time_sel,
 814        .set_suspend_disable    = max8997_reg_disable_suspend,
 815};
 816
 817static struct regulator_ops max8997_fixedvolt_ops = {
 818        .list_voltage           = max8997_list_voltage,
 819        .is_enabled             = max8997_reg_is_enabled,
 820        .enable                 = max8997_reg_enable,
 821        .disable                = max8997_reg_disable,
 822        .set_suspend_disable    = max8997_reg_disable_suspend,
 823};
 824
 825static struct regulator_ops max8997_safeout_ops = {
 826        .list_voltage           = max8997_list_voltage_safeout,
 827        .is_enabled             = max8997_reg_is_enabled,
 828        .enable                 = max8997_reg_enable,
 829        .disable                = max8997_reg_disable,
 830        .get_voltage_sel        = max8997_get_voltage_sel,
 831        .set_voltage            = max8997_set_voltage_safeout,
 832        .set_suspend_disable    = max8997_reg_disable_suspend,
 833};
 834
 835static struct regulator_ops max8997_fixedstate_ops = {
 836        .list_voltage           = max8997_list_voltage_charger_cv,
 837        .get_voltage_sel        = max8997_get_voltage_sel,
 838        .set_voltage            = max8997_set_voltage_charger_cv,
 839};
 840
 841static int max8997_set_current_limit(struct regulator_dev *rdev,
 842                                     int min_uA, int max_uA)
 843{
 844        unsigned dummy;
 845        int rid = rdev_get_id(rdev);
 846
 847        if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
 848                return -EINVAL;
 849
 850        /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
 851        return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
 852}
 853
 854static int max8997_get_current_limit(struct regulator_dev *rdev)
 855{
 856        int sel, rid = rdev_get_id(rdev);
 857
 858        if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
 859                return -EINVAL;
 860
 861        sel = max8997_get_voltage_sel(rdev);
 862        if (sel < 0)
 863                return sel;
 864
 865        /* Reuse max8997_list_voltage to get current_limit. */
 866        return max8997_list_voltage(rdev, sel);
 867}
 868
 869static struct regulator_ops max8997_charger_ops = {
 870        .is_enabled             = max8997_reg_is_enabled,
 871        .enable                 = max8997_reg_enable,
 872        .disable                = max8997_reg_disable,
 873        .get_current_limit      = max8997_get_current_limit,
 874        .set_current_limit      = max8997_set_current_limit,
 875};
 876
 877static struct regulator_ops max8997_charger_fixedstate_ops = {
 878        .get_current_limit      = max8997_get_current_limit,
 879        .set_current_limit      = max8997_set_current_limit,
 880};
 881
 882#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
 883        .name           = #_name,               \
 884        .id             = MAX8997_##_name,      \
 885        .ops            = &_ops,                \
 886        .type           = REGULATOR_VOLTAGE,    \
 887        .owner          = THIS_MODULE,          \
 888}
 889
 890#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
 891        .name           = #_name,               \
 892        .id             = MAX8997_##_name,      \
 893        .ops            = &_ops,                \
 894        .type           = REGULATOR_CURRENT,    \
 895        .owner          = THIS_MODULE,          \
 896}
 897
 898static struct regulator_desc regulators[] = {
 899        MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
 900        MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
 901        MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
 902        MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
 903        MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
 904        MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
 905        MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
 906        MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
 907        MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
 908        MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
 909        MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
 910        MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
 911        MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
 912        MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
 913        MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
 914        MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
 915        MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
 916        MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
 917        MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
 918        MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
 919        MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
 920        MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
 921        MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
 922        MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
 923        MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
 924        MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
 925        MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
 926        MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
 927        MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
 928        MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
 929        MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
 930        MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
 931        MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
 932        MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
 933                                  max8997_charger_fixedstate_ops),
 934};
 935
 936static __devinit int max8997_pmic_probe(struct platform_device *pdev)
 937{
 938        struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
 939        struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
 940        struct regulator_config config = { };
 941        struct regulator_dev **rdev;
 942        struct max8997_data *max8997;
 943        struct i2c_client *i2c;
 944        int i, ret, size;
 945        u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
 946
 947        if (!pdata) {
 948                dev_err(pdev->dev.parent, "No platform init data supplied.\n");
 949                return -ENODEV;
 950        }
 951
 952        max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
 953                               GFP_KERNEL);
 954        if (!max8997)
 955                return -ENOMEM;
 956
 957        size = sizeof(struct regulator_dev *) * pdata->num_regulators;
 958        max8997->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
 959        if (!max8997->rdev)
 960                return -ENOMEM;
 961
 962        rdev = max8997->rdev;
 963        max8997->dev = &pdev->dev;
 964        max8997->iodev = iodev;
 965        max8997->num_regulators = pdata->num_regulators;
 966        platform_set_drvdata(pdev, max8997);
 967        i2c = max8997->iodev->i2c;
 968
 969        max8997->buck125_gpioindex = pdata->buck125_default_idx;
 970        max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
 971        max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
 972        max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
 973        memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
 974        max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
 975
 976        for (i = 0; i < 8; i++) {
 977                max8997->buck1_vol[i] = ret =
 978                        max8997_get_voltage_proper_val(
 979                                        &buck1245_voltage_map_desc,
 980                                        pdata->buck1_voltage[i] / 1000,
 981                                        pdata->buck1_voltage[i] / 1000 +
 982                                        buck1245_voltage_map_desc.step);
 983                if (ret < 0)
 984                        goto err_out;
 985
 986                max8997->buck2_vol[i] = ret =
 987                        max8997_get_voltage_proper_val(
 988                                        &buck1245_voltage_map_desc,
 989                                        pdata->buck2_voltage[i] / 1000,
 990                                        pdata->buck2_voltage[i] / 1000 +
 991                                        buck1245_voltage_map_desc.step);
 992                if (ret < 0)
 993                        goto err_out;
 994
 995                max8997->buck5_vol[i] = ret =
 996                        max8997_get_voltage_proper_val(
 997                                        &buck1245_voltage_map_desc,
 998                                        pdata->buck5_voltage[i] / 1000,
 999                                        pdata->buck5_voltage[i] / 1000 +
1000                                        buck1245_voltage_map_desc.step);
1001                if (ret < 0)
1002                        goto err_out;
1003
1004                if (max_buck1 < max8997->buck1_vol[i])
1005                        max_buck1 = max8997->buck1_vol[i];
1006                if (max_buck2 < max8997->buck2_vol[i])
1007                        max_buck2 = max8997->buck2_vol[i];
1008                if (max_buck5 < max8997->buck5_vol[i])
1009                        max_buck5 = max8997->buck5_vol[i];
1010        }
1011
1012        /* For the safety, set max voltage before setting up */
1013        for (i = 0; i < 8; i++) {
1014                max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1015                                max_buck1, 0x3f);
1016                max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1017                                max_buck2, 0x3f);
1018                max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1019                                max_buck5, 0x3f);
1020        }
1021
1022        /*
1023         * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1024         * If at least one of them cares, set gpios.
1025         */
1026        if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1027                        pdata->buck5_gpiodvs) {
1028
1029                if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1030                                !gpio_is_valid(pdata->buck125_gpios[1]) ||
1031                                !gpio_is_valid(pdata->buck125_gpios[2])) {
1032                        dev_err(&pdev->dev, "GPIO NOT VALID\n");
1033                        ret = -EINVAL;
1034                        goto err_out;
1035                }
1036
1037                ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0],
1038                                        "MAX8997 SET1");
1039                if (ret)
1040                        goto err_out;
1041
1042                ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1],
1043                                        "MAX8997 SET2");
1044                if (ret)
1045                        goto err_out;
1046
1047                ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2],
1048                                "MAX8997 SET3");
1049                if (ret)
1050                        goto err_out;
1051
1052                gpio_direction_output(pdata->buck125_gpios[0],
1053                                (max8997->buck125_gpioindex >> 2)
1054                                & 0x1); /* SET1 */
1055                gpio_direction_output(pdata->buck125_gpios[1],
1056                                (max8997->buck125_gpioindex >> 1)
1057                                & 0x1); /* SET2 */
1058                gpio_direction_output(pdata->buck125_gpios[2],
1059                                (max8997->buck125_gpioindex >> 0)
1060                                & 0x1); /* SET3 */
1061        }
1062
1063        /* DVS-GPIO disabled */
1064        max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1065                        (1 << 1) : (0 << 1), 1 << 1);
1066        max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1067                        (1 << 1) : (0 << 1), 1 << 1);
1068        max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1069                        (1 << 1) : (0 << 1), 1 << 1);
1070
1071        /* Initialize all the DVS related BUCK registers */
1072        for (i = 0; i < 8; i++) {
1073                max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1074                                max8997->buck1_vol[i],
1075                                0x3f);
1076                max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1077                                max8997->buck2_vol[i],
1078                                0x3f);
1079                max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1080                                max8997->buck5_vol[i],
1081                                0x3f);
1082        }
1083
1084        /* Misc Settings */
1085        max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1086        max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1087
1088        for (i = 0; i < pdata->num_regulators; i++) {
1089                const struct voltage_map_desc *desc;
1090                int id = pdata->regulators[i].id;
1091
1092                desc = reg_voltage_map[id];
1093                if (desc)
1094                        regulators[id].n_voltages =
1095                                (desc->max - desc->min) / desc->step + 1;
1096                else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1097                        regulators[id].n_voltages = 4;
1098                else if (id == MAX8997_CHARGER_CV)
1099                        regulators[id].n_voltages = 16;
1100
1101                config.dev = max8997->dev;
1102                config.init_data = pdata->regulators[i].initdata;
1103                config.driver_data = max8997;
1104
1105                rdev[i] = regulator_register(&regulators[id], &config);
1106                if (IS_ERR(rdev[i])) {
1107                        ret = PTR_ERR(rdev[i]);
1108                        dev_err(max8997->dev, "regulator init failed for %d\n",
1109                                        id);
1110                        rdev[i] = NULL;
1111                        goto err;
1112                }
1113        }
1114
1115        return 0;
1116err:
1117        while (--i >= 0)
1118                regulator_unregister(rdev[i]);
1119err_out:
1120        return ret;
1121}
1122
1123static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1124{
1125        struct max8997_data *max8997 = platform_get_drvdata(pdev);
1126        struct regulator_dev **rdev = max8997->rdev;
1127        int i;
1128
1129        for (i = 0; i < max8997->num_regulators; i++)
1130                regulator_unregister(rdev[i]);
1131        return 0;
1132}
1133
1134static const struct platform_device_id max8997_pmic_id[] = {
1135        { "max8997-pmic", 0},
1136        { },
1137};
1138MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1139
1140static struct platform_driver max8997_pmic_driver = {
1141        .driver = {
1142                .name = "max8997-pmic",
1143                .owner = THIS_MODULE,
1144        },
1145        .probe = max8997_pmic_probe,
1146        .remove = __devexit_p(max8997_pmic_remove),
1147        .id_table = max8997_pmic_id,
1148};
1149
1150static int __init max8997_pmic_init(void)
1151{
1152        return platform_driver_register(&max8997_pmic_driver);
1153}
1154subsys_initcall(max8997_pmic_init);
1155
1156static void __exit max8997_pmic_cleanup(void)
1157{
1158        platform_driver_unregister(&max8997_pmic_driver);
1159}
1160module_exit(max8997_pmic_cleanup);
1161
1162MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1163MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1164MODULE_LICENSE("GPL");
1165
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.