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