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