1 // SPDX-License-Identifier: GPL-2.0+
3 // max77802.c - Regulator driver for the Maxim 77802
5 // Copyright (C) 2013-2014 Google, Inc
6 // Simon Glass <sjg@chromium.org>
8 // Copyright (C) 2012 Samsung Electronics
9 // Chiwoong Byun <woong.byun@samsung.com>
10 // Jonghwa Lee <jonghwa3.lee@samsung.com>
12 // This driver is based on max8997.c
14 #include <linux/kernel.h>
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/slab.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/mfd/max77686.h>
26 #include <linux/mfd/max77686-private.h>
27 #include <dt-bindings/regulator/maxim,max77802.h>
29 /* Default ramp delay in case it is not manually set */
30 #define MAX77802_RAMP_DELAY 100000 /* uV/us */
32 #define MAX77802_OPMODE_SHIFT_LDO 6
33 #define MAX77802_OPMODE_BUCK234_SHIFT 4
34 #define MAX77802_OPMODE_MASK 0x3
36 #define MAX77802_VSEL_MASK 0x3F
37 #define MAX77802_DVS_VSEL_MASK 0xFF
39 #define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
40 #define MAX77802_RAMP_RATE_SHIFT_2BIT 6
41 #define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
42 #define MAX77802_RAMP_RATE_SHIFT_4BIT 4
44 #define MAX77802_STATUS_OFF 0x0
45 #define MAX77802_OFF_PWRREQ 0x1
46 #define MAX77802_LP_PWRREQ 0x2
48 /* MAX77802 has two register formats: 2-bit and 4-bit */
49 static const unsigned int ramp_table_77802_2bit[] = {
56 static unsigned int ramp_table_77802_4bit[] = {
57 1000, 2000, 3030, 4000,
58 5000, 5880, 7140, 8330,
59 9090, 10000, 11110, 12500,
60 16670, 25000, 50000, 100000,
63 struct max77802_regulator_prv {
64 /* Array indexed by regulator id */
65 unsigned int opmode[MAX77802_REG_MAX];
68 static inline unsigned int max77802_map_mode(unsigned int mode)
70 return mode == MAX77802_OPMODE_NORMAL ?
71 REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
74 static int max77802_get_opmode_shift(int id)
76 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
77 id <= MAX77802_BUCK10))
80 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
81 return MAX77802_OPMODE_BUCK234_SHIFT;
83 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
84 return MAX77802_OPMODE_SHIFT_LDO;
90 * max77802_set_suspend_disable - Disable the regulator during system suspend
91 * @rdev: regulator to mark as disabled
93 * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
94 * Configure the regulator so the PMIC will turn it OFF during system suspend.
96 static int max77802_set_suspend_disable(struct regulator_dev *rdev)
98 unsigned int val = MAX77802_OFF_PWRREQ;
99 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
100 int id = rdev_get_id(rdev);
101 int shift = max77802_get_opmode_shift(id);
103 max77802->opmode[id] = val;
104 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
105 rdev->desc->enable_mask, val << shift);
109 * Some LDOs support Low Power Mode while the system is running.
113 static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
115 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
116 int id = rdev_get_id(rdev);
118 int shift = max77802_get_opmode_shift(id);
121 case REGULATOR_MODE_STANDBY:
122 val = MAX77802_OPMODE_LP; /* ON in Low Power Mode */
124 case REGULATOR_MODE_NORMAL:
125 val = MAX77802_OPMODE_NORMAL; /* ON in Normal Mode */
128 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
129 rdev->desc->name, mode);
133 max77802->opmode[id] = val;
134 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
135 rdev->desc->enable_mask, val << shift);
138 static unsigned max77802_get_mode(struct regulator_dev *rdev)
140 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
141 int id = rdev_get_id(rdev);
143 return max77802_map_mode(max77802->opmode[id]);
147 * max77802_set_suspend_mode - set regulator opmode when the system is suspended
148 * @rdev: regulator to change mode
149 * @mode: operating mode to be set
151 * Will set the operating mode for the regulators during system suspend.
152 * This function is valid for the three different enable control logics:
154 * Enable Control Logic1 by PWRREQ (BUCK 2-4 and LDOs 2, 4-19, 22-35)
155 * Enable Control Logic2 by PWRREQ (LDOs 1, 20, 21)
156 * Enable Control Logic3 by PWRREQ (LDO 3)
158 * If setting the regulator mode fails, the function only warns but does
159 * not return an error code to avoid the regulator core to stop setting
160 * the operating mode for the remaining regulators.
162 static int max77802_set_suspend_mode(struct regulator_dev *rdev,
165 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
166 int id = rdev_get_id(rdev);
168 int shift = max77802_get_opmode_shift(id);
171 * If the regulator has been disabled for suspend
172 * then is invalid to try setting a suspend mode.
174 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
175 dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
176 rdev->desc->name, mode);
181 case REGULATOR_MODE_STANDBY:
183 * If the regulator opmode is normal then enable
184 * ON in Low Power Mode by PWRREQ. If the mode is
185 * already Low Power then no action is required.
187 if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
188 val = MAX77802_LP_PWRREQ;
192 case REGULATOR_MODE_NORMAL:
194 * If the regulator operating mode is Low Power then
195 * normal is not a valid opmode in suspend. If the
196 * mode is already normal then no action is required.
198 if (max77802->opmode[id] == MAX77802_OPMODE_LP)
199 dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
200 rdev->desc->name, mode);
203 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
204 rdev->desc->name, mode);
208 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
209 rdev->desc->enable_mask, val << shift);
212 static int max77802_enable(struct regulator_dev *rdev)
214 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
215 int id = rdev_get_id(rdev);
216 int shift = max77802_get_opmode_shift(id);
218 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
219 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
221 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
222 rdev->desc->enable_mask,
223 max77802->opmode[id] << shift);
226 static int max77802_find_ramp_value(struct regulator_dev *rdev,
227 const unsigned int limits[], int size,
228 unsigned int ramp_delay)
232 for (i = 0; i < size; i++) {
233 if (ramp_delay <= limits[i])
237 /* Use maximum value for no ramp control */
238 dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n",
239 rdev->desc->name, ramp_delay);
243 /* Used for BUCKs 2-4 */
244 static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev,
247 int id = rdev_get_id(rdev);
248 unsigned int ramp_value;
250 if (id > MAX77802_BUCK4) {
252 "%s: regulator: ramp delay not supported\n",
256 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit,
257 ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay);
259 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
260 MAX77802_RAMP_RATE_MASK_2BIT,
261 ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT);
265 static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev,
268 unsigned int ramp_value;
270 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit,
271 ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay);
273 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
274 MAX77802_RAMP_RATE_MASK_4BIT,
275 ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT);
279 * LDOs 2, 4-19, 22-35
281 static const struct regulator_ops max77802_ldo_ops_logic1 = {
282 .list_voltage = regulator_list_voltage_linear,
283 .map_voltage = regulator_map_voltage_linear,
284 .is_enabled = regulator_is_enabled_regmap,
285 .enable = max77802_enable,
286 .disable = regulator_disable_regmap,
287 .get_voltage_sel = regulator_get_voltage_sel_regmap,
288 .set_voltage_sel = regulator_set_voltage_sel_regmap,
289 .set_voltage_time_sel = regulator_set_voltage_time_sel,
290 .set_suspend_disable = max77802_set_suspend_disable,
291 .set_suspend_mode = max77802_set_suspend_mode,
297 static const struct regulator_ops max77802_ldo_ops_logic2 = {
298 .list_voltage = regulator_list_voltage_linear,
299 .map_voltage = regulator_map_voltage_linear,
300 .is_enabled = regulator_is_enabled_regmap,
301 .enable = max77802_enable,
302 .disable = regulator_disable_regmap,
303 .get_voltage_sel = regulator_get_voltage_sel_regmap,
304 .set_voltage_sel = regulator_set_voltage_sel_regmap,
305 .set_voltage_time_sel = regulator_set_voltage_time_sel,
306 .set_mode = max77802_set_mode,
307 .get_mode = max77802_get_mode,
308 .set_suspend_mode = max77802_set_suspend_mode,
312 static const struct regulator_ops max77802_buck_16_dvs_ops = {
313 .list_voltage = regulator_list_voltage_linear,
314 .map_voltage = regulator_map_voltage_linear,
315 .is_enabled = regulator_is_enabled_regmap,
316 .enable = max77802_enable,
317 .disable = regulator_disable_regmap,
318 .get_voltage_sel = regulator_get_voltage_sel_regmap,
319 .set_voltage_sel = regulator_set_voltage_sel_regmap,
320 .set_voltage_time_sel = regulator_set_voltage_time_sel,
321 .set_ramp_delay = max77802_set_ramp_delay_4bit,
322 .set_suspend_disable = max77802_set_suspend_disable,
326 static const struct regulator_ops max77802_buck_234_ops = {
327 .list_voltage = regulator_list_voltage_linear,
328 .map_voltage = regulator_map_voltage_linear,
329 .is_enabled = regulator_is_enabled_regmap,
330 .enable = max77802_enable,
331 .disable = regulator_disable_regmap,
332 .get_voltage_sel = regulator_get_voltage_sel_regmap,
333 .set_voltage_sel = regulator_set_voltage_sel_regmap,
334 .set_voltage_time_sel = regulator_set_voltage_time_sel,
335 .set_ramp_delay = max77802_set_ramp_delay_2bit,
336 .set_suspend_disable = max77802_set_suspend_disable,
337 .set_suspend_mode = max77802_set_suspend_mode,
341 static const struct regulator_ops max77802_buck_dvs_ops = {
342 .list_voltage = regulator_list_voltage_linear,
343 .map_voltage = regulator_map_voltage_linear,
344 .is_enabled = regulator_is_enabled_regmap,
345 .enable = max77802_enable,
346 .disable = regulator_disable_regmap,
347 .get_voltage_sel = regulator_get_voltage_sel_regmap,
348 .set_voltage_sel = regulator_set_voltage_sel_regmap,
349 .set_voltage_time_sel = regulator_set_voltage_time_sel,
350 .set_ramp_delay = max77802_set_ramp_delay_2bit,
351 .set_suspend_disable = max77802_set_suspend_disable,
354 /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
355 #define regulator_77802_desc_p_ldo(num, supply, log) { \
357 .of_match = of_match_ptr("LDO"#num), \
358 .regulators_node = of_match_ptr("regulators"), \
359 .id = MAX77802_LDO##num, \
360 .supply_name = "inl"#supply, \
361 .ops = &max77802_ldo_ops_logic##log, \
362 .type = REGULATOR_VOLTAGE, \
363 .owner = THIS_MODULE, \
366 .ramp_delay = MAX77802_RAMP_DELAY, \
367 .n_voltages = 1 << 6, \
368 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
369 .vsel_mask = MAX77802_VSEL_MASK, \
370 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
371 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
372 .of_map_mode = max77802_map_mode, \
375 /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
376 #define regulator_77802_desc_n_ldo(num, supply, log) { \
378 .of_match = of_match_ptr("LDO"#num), \
379 .regulators_node = of_match_ptr("regulators"), \
380 .id = MAX77802_LDO##num, \
381 .supply_name = "inl"#supply, \
382 .ops = &max77802_ldo_ops_logic##log, \
383 .type = REGULATOR_VOLTAGE, \
384 .owner = THIS_MODULE, \
387 .ramp_delay = MAX77802_RAMP_DELAY, \
388 .n_voltages = 1 << 6, \
389 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
390 .vsel_mask = MAX77802_VSEL_MASK, \
391 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
392 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
393 .of_map_mode = max77802_map_mode, \
397 #define regulator_77802_desc_16_buck(num) { \
398 .name = "BUCK"#num, \
399 .of_match = of_match_ptr("BUCK"#num), \
400 .regulators_node = of_match_ptr("regulators"), \
401 .id = MAX77802_BUCK##num, \
402 .supply_name = "inb"#num, \
403 .ops = &max77802_buck_16_dvs_ops, \
404 .type = REGULATOR_VOLTAGE, \
405 .owner = THIS_MODULE, \
408 .ramp_delay = MAX77802_RAMP_DELAY, \
409 .n_voltages = 1 << 8, \
410 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
411 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
412 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
413 .enable_mask = MAX77802_OPMODE_MASK, \
414 .of_map_mode = max77802_map_mode, \
418 #define regulator_77802_desc_234_buck(num) { \
419 .name = "BUCK"#num, \
420 .of_match = of_match_ptr("BUCK"#num), \
421 .regulators_node = of_match_ptr("regulators"), \
422 .id = MAX77802_BUCK##num, \
423 .supply_name = "inb"#num, \
424 .ops = &max77802_buck_234_ops, \
425 .type = REGULATOR_VOLTAGE, \
426 .owner = THIS_MODULE, \
429 .ramp_delay = MAX77802_RAMP_DELAY, \
430 .n_voltages = 0x91, \
431 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
432 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
433 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
434 .enable_mask = MAX77802_OPMODE_MASK << \
435 MAX77802_OPMODE_BUCK234_SHIFT, \
436 .of_map_mode = max77802_map_mode, \
440 #define regulator_77802_desc_buck5(num) { \
441 .name = "BUCK"#num, \
442 .of_match = of_match_ptr("BUCK"#num), \
443 .regulators_node = of_match_ptr("regulators"), \
444 .id = MAX77802_BUCK##num, \
445 .supply_name = "inb"#num, \
446 .ops = &max77802_buck_dvs_ops, \
447 .type = REGULATOR_VOLTAGE, \
448 .owner = THIS_MODULE, \
451 .ramp_delay = MAX77802_RAMP_DELAY, \
452 .n_voltages = 1 << 6, \
453 .vsel_reg = MAX77802_REG_BUCK5OUT, \
454 .vsel_mask = MAX77802_VSEL_MASK, \
455 .enable_reg = MAX77802_REG_BUCK5CTRL, \
456 .enable_mask = MAX77802_OPMODE_MASK, \
457 .of_map_mode = max77802_map_mode, \
461 #define regulator_77802_desc_buck7_10(num) { \
462 .name = "BUCK"#num, \
463 .of_match = of_match_ptr("BUCK"#num), \
464 .regulators_node = of_match_ptr("regulators"), \
465 .id = MAX77802_BUCK##num, \
466 .supply_name = "inb"#num, \
467 .ops = &max77802_buck_dvs_ops, \
468 .type = REGULATOR_VOLTAGE, \
469 .owner = THIS_MODULE, \
472 .ramp_delay = MAX77802_RAMP_DELAY, \
473 .n_voltages = 1 << 6, \
474 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
475 .vsel_mask = MAX77802_VSEL_MASK, \
476 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
477 .enable_mask = MAX77802_OPMODE_MASK, \
478 .of_map_mode = max77802_map_mode, \
481 static const struct regulator_desc regulators[] = {
482 regulator_77802_desc_16_buck(1),
483 regulator_77802_desc_234_buck(2),
484 regulator_77802_desc_234_buck(3),
485 regulator_77802_desc_234_buck(4),
486 regulator_77802_desc_buck5(5),
487 regulator_77802_desc_16_buck(6),
488 regulator_77802_desc_buck7_10(7),
489 regulator_77802_desc_buck7_10(8),
490 regulator_77802_desc_buck7_10(9),
491 regulator_77802_desc_buck7_10(10),
492 regulator_77802_desc_n_ldo(1, 10, 2),
493 regulator_77802_desc_n_ldo(2, 10, 1),
494 regulator_77802_desc_p_ldo(3, 3, 2),
495 regulator_77802_desc_p_ldo(4, 6, 1),
496 regulator_77802_desc_p_ldo(5, 3, 1),
497 regulator_77802_desc_p_ldo(6, 3, 1),
498 regulator_77802_desc_p_ldo(7, 3, 1),
499 regulator_77802_desc_n_ldo(8, 1, 1),
500 regulator_77802_desc_p_ldo(9, 5, 1),
501 regulator_77802_desc_p_ldo(10, 4, 1),
502 regulator_77802_desc_p_ldo(11, 4, 1),
503 regulator_77802_desc_p_ldo(12, 9, 1),
504 regulator_77802_desc_p_ldo(13, 4, 1),
505 regulator_77802_desc_p_ldo(14, 4, 1),
506 regulator_77802_desc_n_ldo(15, 1, 1),
507 regulator_77802_desc_n_ldo(17, 2, 1),
508 regulator_77802_desc_p_ldo(18, 7, 1),
509 regulator_77802_desc_p_ldo(19, 5, 1),
510 regulator_77802_desc_p_ldo(20, 7, 2),
511 regulator_77802_desc_p_ldo(21, 6, 2),
512 regulator_77802_desc_p_ldo(23, 9, 1),
513 regulator_77802_desc_p_ldo(24, 6, 1),
514 regulator_77802_desc_p_ldo(25, 9, 1),
515 regulator_77802_desc_p_ldo(26, 9, 1),
516 regulator_77802_desc_n_ldo(27, 2, 1),
517 regulator_77802_desc_p_ldo(28, 7, 1),
518 regulator_77802_desc_p_ldo(29, 7, 1),
519 regulator_77802_desc_n_ldo(30, 2, 1),
520 regulator_77802_desc_p_ldo(32, 9, 1),
521 regulator_77802_desc_p_ldo(33, 6, 1),
522 regulator_77802_desc_p_ldo(34, 9, 1),
523 regulator_77802_desc_n_ldo(35, 2, 1),
526 static int max77802_pmic_probe(struct platform_device *pdev)
528 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
529 struct max77802_regulator_prv *max77802;
531 struct regulator_config config = { };
533 max77802 = devm_kzalloc(&pdev->dev,
534 sizeof(struct max77802_regulator_prv),
539 config.dev = iodev->dev;
540 config.regmap = iodev->regmap;
541 config.driver_data = max77802;
542 platform_set_drvdata(pdev, max77802);
544 for (i = 0; i < MAX77802_REG_MAX; i++) {
545 struct regulator_dev *rdev;
546 int id = regulators[i].id;
547 int shift = max77802_get_opmode_shift(id);
550 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
553 "cannot read current mode for %d\n", i);
554 val = MAX77802_OPMODE_NORMAL;
556 val = val >> shift & MAX77802_OPMODE_MASK;
560 * If the regulator is disabled and the system warm rebooted,
561 * the hardware reports OFF as the regulator operating mode.
562 * Default to operating mode NORMAL in that case.
564 if (val == MAX77802_STATUS_OFF)
565 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
567 max77802->opmode[id] = val;
569 rdev = devm_regulator_register(&pdev->dev,
570 ®ulators[i], &config);
574 "regulator init failed for %d: %d\n", i, ret);
582 static const struct platform_device_id max77802_pmic_id[] = {
583 {"max77802-pmic", 0},
586 MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
588 static struct platform_driver max77802_pmic_driver = {
590 .name = "max77802-pmic",
592 .probe = max77802_pmic_probe,
593 .id_table = max77802_pmic_id,
596 module_platform_driver(max77802_pmic_driver);
598 MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
599 MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
600 MODULE_LICENSE("GPL");