1/* 2 * max77686.c - Regulator driver for the Maxim 77686 3 * 4 * Copyright (C) 2012 Samsung Electronics 5 * Chiwoong Byun <woong.byun@smasung.com> 6 * Jonghwa Lee <jonghwa3.lee@samsung.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * 22 * This driver is based on max8997.c 23 */ 24 25#include <linux/kernel.h> 26#include <linux/bug.h> 27#include <linux/err.h> 28#include <linux/gpio.h> 29#include <linux/of_gpio.h> 30#include <linux/slab.h> 31#include <linux/platform_device.h> 32#include <linux/regulator/driver.h> 33#include <linux/regulator/machine.h> 34#include <linux/regulator/of_regulator.h> 35#include <linux/mfd/max77686.h> 36#include <linux/mfd/max77686-private.h> 37 38#define MAX77686_LDO_MINUV 800000 39#define MAX77686_LDO_UVSTEP 50000 40#define MAX77686_LDO_LOW_MINUV 800000 41#define MAX77686_LDO_LOW_UVSTEP 25000 42#define MAX77686_BUCK_MINUV 750000 43#define MAX77686_BUCK_UVSTEP 50000 44#define MAX77686_RAMP_DELAY 100000 /* uV/us */ 45#define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */ 46#define MAX77686_DVS_MINUV 600000 47#define MAX77686_DVS_UVSTEP 12500 48 49/* 50 * Value for configuring buck[89] and LDO{20,21,22} as GPIO control. 51 * It is the same as 'off' for other regulators. 52 */ 53#define MAX77686_GPIO_CONTROL 0x0 54/* 55 * Values used for configuring LDOs and bucks. 56 * Forcing low power mode: LDO1, 3-5, 9, 13, 17-26 57 */ 58#define MAX77686_LDO_LOWPOWER 0x1 59/* 60 * On/off controlled by PWRREQ: 61 * - LDO2, 6-8, 10-12, 14-16 62 * - buck[1234] 63 */ 64#define MAX77686_OFF_PWRREQ 0x1 65/* Low power mode controlled by PWRREQ: All LDOs */ 66#define MAX77686_LDO_LOWPOWER_PWRREQ 0x2 67/* Forcing low power mode: buck[234] */ 68#define MAX77686_BUCK_LOWPOWER 0x2 69#define MAX77686_NORMAL 0x3 70 71#define MAX77686_OPMODE_SHIFT 6 72#define MAX77686_OPMODE_BUCK234_SHIFT 4 73#define MAX77686_OPMODE_MASK 0x3 74 75#define MAX77686_VSEL_MASK 0x3F 76#define MAX77686_DVS_VSEL_MASK 0xFF 77 78#define MAX77686_RAMP_RATE_MASK 0xC0 79 80#define MAX77686_REGULATORS MAX77686_REG_MAX 81#define MAX77686_LDOS 26 82 83enum max77686_ramp_rate { 84 RAMP_RATE_13P75MV, 85 RAMP_RATE_27P5MV, 86 RAMP_RATE_55MV, 87 RAMP_RATE_NO_CTRL, /* 100mV/us */ 88}; 89 90struct max77686_data { 91 DECLARE_BITMAP(gpio_enabled, MAX77686_REGULATORS); 92 93 /* Array indexed by regulator id */ 94 unsigned int opmode[MAX77686_REGULATORS]; 95}; 96 97static unsigned int max77686_get_opmode_shift(int id) 98{ 99 switch (id) { 100 case MAX77686_BUCK1: 101 case MAX77686_BUCK5 ... MAX77686_BUCK9: 102 return 0; 103 case MAX77686_BUCK2 ... MAX77686_BUCK4: 104 return MAX77686_OPMODE_BUCK234_SHIFT; 105 default: 106 /* all LDOs */ 107 return MAX77686_OPMODE_SHIFT; 108 } 109} 110 111/* 112 * When regulator is configured for GPIO control then it 113 * replaces "normal" mode. Any change from low power mode to normal 114 * should actually change to GPIO control. 115 * Map normal mode to proper value for such regulators. 116 */ 117static unsigned int max77686_map_normal_mode(struct max77686_data *max77686, 118 int id) 119{ 120 switch (id) { 121 case MAX77686_BUCK8: 122 case MAX77686_BUCK9: 123 case MAX77686_LDO20 ... MAX77686_LDO22: 124 if (test_bit(id, max77686->gpio_enabled)) 125 return MAX77686_GPIO_CONTROL; 126 } 127 128 return MAX77686_NORMAL; 129} 130 131/* Some BUCKs and LDOs supports Normal[ON/OFF] mode during suspend */ 132static int max77686_set_suspend_disable(struct regulator_dev *rdev) 133{ 134 unsigned int val, shift; 135 struct max77686_data *max77686 = rdev_get_drvdata(rdev); 136 int ret, id = rdev_get_id(rdev); 137 138 shift = max77686_get_opmode_shift(id); 139 val = MAX77686_OFF_PWRREQ; 140 141 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 142 rdev->desc->enable_mask, val << shift); 143 if (ret) 144 return ret; 145 146 max77686->opmode[id] = val; 147 return 0; 148} 149 150/* Some LDOs supports [LPM/Normal]ON mode during suspend state */ 151static int max77686_set_suspend_mode(struct regulator_dev *rdev, 152 unsigned int mode) 153{ 154 struct max77686_data *max77686 = rdev_get_drvdata(rdev); 155 unsigned int val; 156 int ret, id = rdev_get_id(rdev); 157 158 /* BUCK[5-9] doesn't support this feature */ 159 if (id >= MAX77686_BUCK5) 160 return 0; 161 162 switch (mode) { 163 case REGULATOR_MODE_IDLE: /* ON in LP Mode */ 164 val = MAX77686_LDO_LOWPOWER_PWRREQ; 165 break; 166 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */ 167 val = max77686_map_normal_mode(max77686, id); 168 break; 169 default: 170 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n", 171 rdev->desc->name, mode); 172 return -EINVAL; 173 } 174 175 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 176 rdev->desc->enable_mask, 177 val << MAX77686_OPMODE_SHIFT); 178 if (ret) 179 return ret; 180 181 max77686->opmode[id] = val; 182 return 0; 183} 184 185/* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */ 186static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev, 187 unsigned int mode) 188{ 189 unsigned int val; 190 struct max77686_data *max77686 = rdev_get_drvdata(rdev); 191 int ret, id = rdev_get_id(rdev); 192 193 switch (mode) { 194 case REGULATOR_MODE_STANDBY: /* switch off */ 195 val = MAX77686_OFF_PWRREQ; 196 break; 197 case REGULATOR_MODE_IDLE: /* ON in LP Mode */ 198 val = MAX77686_LDO_LOWPOWER_PWRREQ; 199 break; 200 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */ 201 val = max77686_map_normal_mode(max77686, id); 202 break; 203 default: 204 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n", 205 rdev->desc->name, mode); 206 return -EINVAL; 207 } 208 209 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 210 rdev->desc->enable_mask, 211 val << MAX77686_OPMODE_SHIFT); 212 if (ret) 213 return ret; 214 215 max77686->opmode[id] = val; 216 return 0; 217} 218 219static int max77686_enable(struct regulator_dev *rdev) 220{ 221 struct max77686_data *max77686 = rdev_get_drvdata(rdev); 222 unsigned int shift; 223 int id = rdev_get_id(rdev); 224 225 shift = max77686_get_opmode_shift(id); 226 227 if (max77686->opmode[id] == MAX77686_OFF_PWRREQ) 228 max77686->opmode[id] = max77686_map_normal_mode(max77686, id); 229 230 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 231 rdev->desc->enable_mask, 232 max77686->opmode[id] << shift); 233} 234 235static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) 236{ 237 unsigned int ramp_value = RAMP_RATE_NO_CTRL; 238 239 switch (ramp_delay) { 240 case 1 ... 13750: 241 ramp_value = RAMP_RATE_13P75MV; 242 break; 243 case 13751 ... 27500: 244 ramp_value = RAMP_RATE_27P5MV; 245 break; 246 case 27501 ... 55000: 247 ramp_value = RAMP_RATE_55MV; 248 break; 249 case 55001 ... 100000: 250 break; 251 default: 252 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n", 253 rdev->desc->name, ramp_delay); 254 } 255 256 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 257 MAX77686_RAMP_RATE_MASK, ramp_value << 6); 258} 259 260static int max77686_of_parse_cb(struct device_node *np, 261 const struct regulator_desc *desc, 262 struct regulator_config *config) 263{ 264 struct max77686_data *max77686 = config->driver_data; 265 266 switch (desc->id) { 267 case MAX77686_BUCK8: 268 case MAX77686_BUCK9: 269 case MAX77686_LDO20 ... MAX77686_LDO22: 270 config->ena_gpio = of_get_named_gpio(np, 271 "maxim,ena-gpios", 0); 272 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH; 273 config->ena_gpio_initialized = true; 274 break; 275 default: 276 return 0; 277 } 278 279 if (gpio_is_valid(config->ena_gpio)) { 280 set_bit(desc->id, max77686->gpio_enabled); 281 282 return regmap_update_bits(config->regmap, desc->enable_reg, 283 desc->enable_mask, 284 MAX77686_GPIO_CONTROL); 285 } 286 287 return 0; 288} 289 290static struct regulator_ops max77686_ops = { 291 .list_voltage = regulator_list_voltage_linear, 292 .map_voltage = regulator_map_voltage_linear, 293 .is_enabled = regulator_is_enabled_regmap, 294 .enable = max77686_enable, 295 .disable = regulator_disable_regmap, 296 .get_voltage_sel = regulator_get_voltage_sel_regmap, 297 .set_voltage_sel = regulator_set_voltage_sel_regmap, 298 .set_voltage_time_sel = regulator_set_voltage_time_sel, 299 .set_suspend_mode = max77686_set_suspend_mode, 300}; 301 302static struct regulator_ops max77686_ldo_ops = { 303 .list_voltage = regulator_list_voltage_linear, 304 .map_voltage = regulator_map_voltage_linear, 305 .is_enabled = regulator_is_enabled_regmap, 306 .enable = max77686_enable, 307 .disable = regulator_disable_regmap, 308 .get_voltage_sel = regulator_get_voltage_sel_regmap, 309 .set_voltage_sel = regulator_set_voltage_sel_regmap, 310 .set_voltage_time_sel = regulator_set_voltage_time_sel, 311 .set_suspend_mode = max77686_ldo_set_suspend_mode, 312 .set_suspend_disable = max77686_set_suspend_disable, 313}; 314 315static struct regulator_ops max77686_buck1_ops = { 316 .list_voltage = regulator_list_voltage_linear, 317 .map_voltage = regulator_map_voltage_linear, 318 .is_enabled = regulator_is_enabled_regmap, 319 .enable = max77686_enable, 320 .disable = regulator_disable_regmap, 321 .get_voltage_sel = regulator_get_voltage_sel_regmap, 322 .set_voltage_sel = regulator_set_voltage_sel_regmap, 323 .set_voltage_time_sel = regulator_set_voltage_time_sel, 324 .set_suspend_disable = max77686_set_suspend_disable, 325}; 326 327static struct regulator_ops max77686_buck_dvs_ops = { 328 .list_voltage = regulator_list_voltage_linear, 329 .map_voltage = regulator_map_voltage_linear, 330 .is_enabled = regulator_is_enabled_regmap, 331 .enable = max77686_enable, 332 .disable = regulator_disable_regmap, 333 .get_voltage_sel = regulator_get_voltage_sel_regmap, 334 .set_voltage_sel = regulator_set_voltage_sel_regmap, 335 .set_voltage_time_sel = regulator_set_voltage_time_sel, 336 .set_ramp_delay = max77686_set_ramp_delay, 337 .set_suspend_disable = max77686_set_suspend_disable, 338}; 339 340#define regulator_desc_ldo(num) { \ 341 .name = "LDO"#num, \ 342 .of_match = of_match_ptr("LDO"#num), \ 343 .regulators_node = of_match_ptr("voltage-regulators"), \ 344 .of_parse_cb = max77686_of_parse_cb, \ 345 .id = MAX77686_LDO##num, \ 346 .ops = &max77686_ops, \ 347 .type = REGULATOR_VOLTAGE, \ 348 .owner = THIS_MODULE, \ 349 .min_uV = MAX77686_LDO_MINUV, \ 350 .uV_step = MAX77686_LDO_UVSTEP, \ 351 .ramp_delay = MAX77686_RAMP_DELAY, \ 352 .n_voltages = MAX77686_VSEL_MASK + 1, \ 353 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 354 .vsel_mask = MAX77686_VSEL_MASK, \ 355 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 356 .enable_mask = MAX77686_OPMODE_MASK \ 357 << MAX77686_OPMODE_SHIFT, \ 358} 359#define regulator_desc_lpm_ldo(num) { \ 360 .name = "LDO"#num, \ 361 .of_match = of_match_ptr("LDO"#num), \ 362 .regulators_node = of_match_ptr("voltage-regulators"), \ 363 .id = MAX77686_LDO##num, \ 364 .ops = &max77686_ldo_ops, \ 365 .type = REGULATOR_VOLTAGE, \ 366 .owner = THIS_MODULE, \ 367 .min_uV = MAX77686_LDO_MINUV, \ 368 .uV_step = MAX77686_LDO_UVSTEP, \ 369 .ramp_delay = MAX77686_RAMP_DELAY, \ 370 .n_voltages = MAX77686_VSEL_MASK + 1, \ 371 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 372 .vsel_mask = MAX77686_VSEL_MASK, \ 373 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 374 .enable_mask = MAX77686_OPMODE_MASK \ 375 << MAX77686_OPMODE_SHIFT, \ 376} 377#define regulator_desc_ldo_low(num) { \ 378 .name = "LDO"#num, \ 379 .of_match = of_match_ptr("LDO"#num), \ 380 .regulators_node = of_match_ptr("voltage-regulators"), \ 381 .id = MAX77686_LDO##num, \ 382 .ops = &max77686_ldo_ops, \ 383 .type = REGULATOR_VOLTAGE, \ 384 .owner = THIS_MODULE, \ 385 .min_uV = MAX77686_LDO_LOW_MINUV, \ 386 .uV_step = MAX77686_LDO_LOW_UVSTEP, \ 387 .ramp_delay = MAX77686_RAMP_DELAY, \ 388 .n_voltages = MAX77686_VSEL_MASK + 1, \ 389 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 390 .vsel_mask = MAX77686_VSEL_MASK, \ 391 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 392 .enable_mask = MAX77686_OPMODE_MASK \ 393 << MAX77686_OPMODE_SHIFT, \ 394} 395#define regulator_desc_ldo1_low(num) { \ 396 .name = "LDO"#num, \ 397 .of_match = of_match_ptr("LDO"#num), \ 398 .regulators_node = of_match_ptr("voltage-regulators"), \ 399 .id = MAX77686_LDO##num, \ 400 .ops = &max77686_ops, \ 401 .type = REGULATOR_VOLTAGE, \ 402 .owner = THIS_MODULE, \ 403 .min_uV = MAX77686_LDO_LOW_MINUV, \ 404 .uV_step = MAX77686_LDO_LOW_UVSTEP, \ 405 .ramp_delay = MAX77686_RAMP_DELAY, \ 406 .n_voltages = MAX77686_VSEL_MASK + 1, \ 407 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 408 .vsel_mask = MAX77686_VSEL_MASK, \ 409 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \ 410 .enable_mask = MAX77686_OPMODE_MASK \ 411 << MAX77686_OPMODE_SHIFT, \ 412} 413#define regulator_desc_buck(num) { \ 414 .name = "BUCK"#num, \ 415 .of_match = of_match_ptr("BUCK"#num), \ 416 .regulators_node = of_match_ptr("voltage-regulators"), \ 417 .of_parse_cb = max77686_of_parse_cb, \ 418 .id = MAX77686_BUCK##num, \ 419 .ops = &max77686_ops, \ 420 .type = REGULATOR_VOLTAGE, \ 421 .owner = THIS_MODULE, \ 422 .min_uV = MAX77686_BUCK_MINUV, \ 423 .uV_step = MAX77686_BUCK_UVSTEP, \ 424 .ramp_delay = MAX77686_RAMP_DELAY, \ 425 .n_voltages = MAX77686_VSEL_MASK + 1, \ 426 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \ 427 .vsel_mask = MAX77686_VSEL_MASK, \ 428 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \ 429 .enable_mask = MAX77686_OPMODE_MASK, \ 430} 431#define regulator_desc_buck1(num) { \ 432 .name = "BUCK"#num, \ 433 .of_match = of_match_ptr("BUCK"#num), \ 434 .regulators_node = of_match_ptr("voltage-regulators"), \ 435 .id = MAX77686_BUCK##num, \ 436 .ops = &max77686_buck1_ops, \ 437 .type = REGULATOR_VOLTAGE, \ 438 .owner = THIS_MODULE, \ 439 .min_uV = MAX77686_BUCK_MINUV, \ 440 .uV_step = MAX77686_BUCK_UVSTEP, \ 441 .ramp_delay = MAX77686_RAMP_DELAY, \ 442 .n_voltages = MAX77686_VSEL_MASK + 1, \ 443 .vsel_reg = MAX77686_REG_BUCK1OUT, \ 444 .vsel_mask = MAX77686_VSEL_MASK, \ 445 .enable_reg = MAX77686_REG_BUCK1CTRL, \ 446 .enable_mask = MAX77686_OPMODE_MASK, \ 447} 448#define regulator_desc_buck_dvs(num) { \ 449 .name = "BUCK"#num, \ 450 .of_match = of_match_ptr("BUCK"#num), \ 451 .regulators_node = of_match_ptr("voltage-regulators"), \ 452 .id = MAX77686_BUCK##num, \ 453 .ops = &max77686_buck_dvs_ops, \ 454 .type = REGULATOR_VOLTAGE, \ 455 .owner = THIS_MODULE, \ 456 .min_uV = MAX77686_DVS_MINUV, \ 457 .uV_step = MAX77686_DVS_UVSTEP, \ 458 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \ 459 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \ 460 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \ 461 .vsel_mask = MAX77686_DVS_VSEL_MASK, \ 462 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \ 463 .enable_mask = MAX77686_OPMODE_MASK \ 464 << MAX77686_OPMODE_BUCK234_SHIFT, \ 465} 466 467static const struct regulator_desc regulators[] = { 468 regulator_desc_ldo1_low(1), 469 regulator_desc_ldo_low(2), 470 regulator_desc_ldo(3), 471 regulator_desc_ldo(4), 472 regulator_desc_ldo(5), 473 regulator_desc_ldo_low(6), 474 regulator_desc_ldo_low(7), 475 regulator_desc_ldo_low(8), 476 regulator_desc_ldo(9), 477 regulator_desc_lpm_ldo(10), 478 regulator_desc_lpm_ldo(11), 479 regulator_desc_lpm_ldo(12), 480 regulator_desc_ldo(13), 481 regulator_desc_lpm_ldo(14), 482 regulator_desc_ldo_low(15), 483 regulator_desc_lpm_ldo(16), 484 regulator_desc_ldo(17), 485 regulator_desc_ldo(18), 486 regulator_desc_ldo(19), 487 regulator_desc_ldo(20), 488 regulator_desc_ldo(21), 489 regulator_desc_ldo(22), 490 regulator_desc_ldo(23), 491 regulator_desc_ldo(24), 492 regulator_desc_ldo(25), 493 regulator_desc_ldo(26), 494 regulator_desc_buck1(1), 495 regulator_desc_buck_dvs(2), 496 regulator_desc_buck_dvs(3), 497 regulator_desc_buck_dvs(4), 498 regulator_desc_buck(5), 499 regulator_desc_buck(6), 500 regulator_desc_buck(7), 501 regulator_desc_buck(8), 502 regulator_desc_buck(9), 503}; 504 505static int max77686_pmic_probe(struct platform_device *pdev) 506{ 507 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent); 508 struct max77686_data *max77686; 509 int i; 510 struct regulator_config config = { }; 511 512 dev_dbg(&pdev->dev, "%s\n", __func__); 513 514 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data), 515 GFP_KERNEL); 516 if (!max77686) 517 return -ENOMEM; 518 519 config.dev = iodev->dev; 520 config.regmap = iodev->regmap; 521 config.driver_data = max77686; 522 platform_set_drvdata(pdev, max77686); 523 524 for (i = 0; i < MAX77686_REGULATORS; i++) { 525 struct regulator_dev *rdev; 526 int id = regulators[i].id; 527 528 max77686->opmode[id] = MAX77686_NORMAL; 529 rdev = devm_regulator_register(&pdev->dev, 530 ®ulators[i], &config); 531 if (IS_ERR(rdev)) { 532 int ret = PTR_ERR(rdev); 533 dev_err(&pdev->dev, 534 "regulator init failed for %d: %d\n", i, ret); 535 return ret; 536 } 537 } 538 539 return 0; 540} 541 542static const struct platform_device_id max77686_pmic_id[] = { 543 {"max77686-pmic", 0}, 544 { }, 545}; 546MODULE_DEVICE_TABLE(platform, max77686_pmic_id); 547 548static struct platform_driver max77686_pmic_driver = { 549 .driver = { 550 .name = "max77686-pmic", 551 }, 552 .probe = max77686_pmic_probe, 553 .id_table = max77686_pmic_id, 554}; 555 556static int __init max77686_pmic_init(void) 557{ 558 return platform_driver_register(&max77686_pmic_driver); 559} 560subsys_initcall(max77686_pmic_init); 561 562static void __exit max77686_pmic_cleanup(void) 563{ 564 platform_driver_unregister(&max77686_pmic_driver); 565} 566module_exit(max77686_pmic_cleanup); 567 568MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver"); 569MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>"); 570MODULE_LICENSE("GPL"); 571