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/slab.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/mfd/max77686.h>
24 #include <linux/mfd/max77686-private.h>
25 #include <dt-bindings/regulator/maxim,max77802.h>
27 /* Default ramp delay in case it is not manually set */
28 #define MAX77802_RAMP_DELAY 100000 /* uV/us */
30 #define MAX77802_OPMODE_SHIFT_LDO 6
31 #define MAX77802_OPMODE_BUCK234_SHIFT 4
32 #define MAX77802_OPMODE_MASK 0x3
34 #define MAX77802_VSEL_MASK 0x3F
35 #define MAX77802_DVS_VSEL_MASK 0xFF
37 #define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
38 #define MAX77802_RAMP_RATE_SHIFT_2BIT 6
39 #define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
40 #define MAX77802_RAMP_RATE_SHIFT_4BIT 4
42 #define MAX77802_STATUS_OFF 0x0
43 #define MAX77802_OFF_PWRREQ 0x1
44 #define MAX77802_LP_PWRREQ 0x2
46 static const unsigned int max77802_buck234_ramp_table[] = {
53 static const unsigned int max77802_buck16_ramp_table[] = {
54 1000, 2000, 3030, 4000,
55 5000, 5880, 7140, 8330,
56 9090, 10000, 11110, 12500,
57 16670, 25000, 50000, 100000,
60 struct max77802_regulator_prv {
61 /* Array indexed by regulator id */
62 unsigned int opmode[MAX77802_REG_MAX];
65 static inline unsigned int max77802_map_mode(unsigned int mode)
67 return mode == MAX77802_OPMODE_NORMAL ?
68 REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
71 static int max77802_get_opmode_shift(int id)
73 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
74 id <= MAX77802_BUCK10))
77 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
78 return MAX77802_OPMODE_BUCK234_SHIFT;
80 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
81 return MAX77802_OPMODE_SHIFT_LDO;
87 * max77802_set_suspend_disable - Disable the regulator during system suspend
88 * @rdev: regulator to mark as disabled
90 * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
91 * Configure the regulator so the PMIC will turn it OFF during system suspend.
93 static int max77802_set_suspend_disable(struct regulator_dev *rdev)
95 unsigned int val = MAX77802_OFF_PWRREQ;
96 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
97 int id = rdev_get_id(rdev);
98 int shift = max77802_get_opmode_shift(id);
100 max77802->opmode[id] = val;
101 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
102 rdev->desc->enable_mask, val << shift);
106 * Some LDOs support Low Power Mode while the system is running.
110 static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
112 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
113 int id = rdev_get_id(rdev);
115 int shift = max77802_get_opmode_shift(id);
118 case REGULATOR_MODE_STANDBY:
119 val = MAX77802_OPMODE_LP; /* ON in Low Power Mode */
121 case REGULATOR_MODE_NORMAL:
122 val = MAX77802_OPMODE_NORMAL; /* ON in Normal Mode */
125 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
126 rdev->desc->name, mode);
130 max77802->opmode[id] = val;
131 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
132 rdev->desc->enable_mask, val << shift);
135 static unsigned max77802_get_mode(struct regulator_dev *rdev)
137 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
138 int id = rdev_get_id(rdev);
140 return max77802_map_mode(max77802->opmode[id]);
144 * max77802_set_suspend_mode - set regulator opmode when the system is suspended
145 * @rdev: regulator to change mode
146 * @mode: operating mode to be set
148 * Will set the operating mode for the regulators during system suspend.
149 * This function is valid for the three different enable control logics:
151 * Enable Control Logic1 by PWRREQ (BUCK 2-4 and LDOs 2, 4-19, 22-35)
152 * Enable Control Logic2 by PWRREQ (LDOs 1, 20, 21)
153 * Enable Control Logic3 by PWRREQ (LDO 3)
155 * If setting the regulator mode fails, the function only warns but does
156 * not return an error code to avoid the regulator core to stop setting
157 * the operating mode for the remaining regulators.
159 static int max77802_set_suspend_mode(struct regulator_dev *rdev,
162 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
163 int id = rdev_get_id(rdev);
165 int shift = max77802_get_opmode_shift(id);
168 * If the regulator has been disabled for suspend
169 * then is invalid to try setting a suspend mode.
171 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
172 dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
173 rdev->desc->name, mode);
178 case REGULATOR_MODE_STANDBY:
180 * If the regulator opmode is normal then enable
181 * ON in Low Power Mode by PWRREQ. If the mode is
182 * already Low Power then no action is required.
184 if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
185 val = MAX77802_LP_PWRREQ;
189 case REGULATOR_MODE_NORMAL:
191 * If the regulator operating mode is Low Power then
192 * normal is not a valid opmode in suspend. If the
193 * mode is already normal then no action is required.
195 if (max77802->opmode[id] == MAX77802_OPMODE_LP)
196 dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
197 rdev->desc->name, mode);
200 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
201 rdev->desc->name, mode);
205 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
206 rdev->desc->enable_mask, val << shift);
209 static int max77802_enable(struct regulator_dev *rdev)
211 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
212 int id = rdev_get_id(rdev);
213 int shift = max77802_get_opmode_shift(id);
215 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
216 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
218 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
219 rdev->desc->enable_mask,
220 max77802->opmode[id] << shift);
224 * LDOs 2, 4-19, 22-35
226 static const struct regulator_ops max77802_ldo_ops_logic1 = {
227 .list_voltage = regulator_list_voltage_linear,
228 .map_voltage = regulator_map_voltage_linear,
229 .is_enabled = regulator_is_enabled_regmap,
230 .enable = max77802_enable,
231 .disable = regulator_disable_regmap,
232 .get_voltage_sel = regulator_get_voltage_sel_regmap,
233 .set_voltage_sel = regulator_set_voltage_sel_regmap,
234 .set_voltage_time_sel = regulator_set_voltage_time_sel,
235 .set_suspend_disable = max77802_set_suspend_disable,
236 .set_suspend_mode = max77802_set_suspend_mode,
242 static const struct regulator_ops max77802_ldo_ops_logic2 = {
243 .list_voltage = regulator_list_voltage_linear,
244 .map_voltage = regulator_map_voltage_linear,
245 .is_enabled = regulator_is_enabled_regmap,
246 .enable = max77802_enable,
247 .disable = regulator_disable_regmap,
248 .get_voltage_sel = regulator_get_voltage_sel_regmap,
249 .set_voltage_sel = regulator_set_voltage_sel_regmap,
250 .set_voltage_time_sel = regulator_set_voltage_time_sel,
251 .set_mode = max77802_set_mode,
252 .get_mode = max77802_get_mode,
253 .set_suspend_mode = max77802_set_suspend_mode,
257 static const struct regulator_ops max77802_buck_16_dvs_ops = {
258 .list_voltage = regulator_list_voltage_linear,
259 .map_voltage = regulator_map_voltage_linear,
260 .is_enabled = regulator_is_enabled_regmap,
261 .enable = max77802_enable,
262 .disable = regulator_disable_regmap,
263 .get_voltage_sel = regulator_get_voltage_sel_regmap,
264 .set_voltage_sel = regulator_set_voltage_sel_regmap,
265 .set_voltage_time_sel = regulator_set_voltage_time_sel,
266 .set_ramp_delay = regulator_set_ramp_delay_regmap,
267 .set_suspend_disable = max77802_set_suspend_disable,
271 static const struct regulator_ops max77802_buck_234_ops = {
272 .list_voltage = regulator_list_voltage_linear,
273 .map_voltage = regulator_map_voltage_linear,
274 .is_enabled = regulator_is_enabled_regmap,
275 .enable = max77802_enable,
276 .disable = regulator_disable_regmap,
277 .get_voltage_sel = regulator_get_voltage_sel_regmap,
278 .set_voltage_sel = regulator_set_voltage_sel_regmap,
279 .set_voltage_time_sel = regulator_set_voltage_time_sel,
280 .set_ramp_delay = regulator_set_ramp_delay_regmap,
281 .set_suspend_disable = max77802_set_suspend_disable,
282 .set_suspend_mode = max77802_set_suspend_mode,
286 static const struct regulator_ops max77802_buck_dvs_ops = {
287 .list_voltage = regulator_list_voltage_linear,
288 .map_voltage = regulator_map_voltage_linear,
289 .is_enabled = regulator_is_enabled_regmap,
290 .enable = max77802_enable,
291 .disable = regulator_disable_regmap,
292 .get_voltage_sel = regulator_get_voltage_sel_regmap,
293 .set_voltage_sel = regulator_set_voltage_sel_regmap,
294 .set_voltage_time_sel = regulator_set_voltage_time_sel,
295 .set_suspend_disable = max77802_set_suspend_disable,
298 /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
299 #define regulator_77802_desc_p_ldo(num, supply, log) { \
301 .of_match = of_match_ptr("LDO"#num), \
302 .regulators_node = of_match_ptr("regulators"), \
303 .id = MAX77802_LDO##num, \
304 .supply_name = "inl"#supply, \
305 .ops = &max77802_ldo_ops_logic##log, \
306 .type = REGULATOR_VOLTAGE, \
307 .owner = THIS_MODULE, \
310 .ramp_delay = MAX77802_RAMP_DELAY, \
311 .n_voltages = 1 << 6, \
312 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
313 .vsel_mask = MAX77802_VSEL_MASK, \
314 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
315 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
316 .of_map_mode = max77802_map_mode, \
319 /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
320 #define regulator_77802_desc_n_ldo(num, supply, log) { \
322 .of_match = of_match_ptr("LDO"#num), \
323 .regulators_node = of_match_ptr("regulators"), \
324 .id = MAX77802_LDO##num, \
325 .supply_name = "inl"#supply, \
326 .ops = &max77802_ldo_ops_logic##log, \
327 .type = REGULATOR_VOLTAGE, \
328 .owner = THIS_MODULE, \
331 .ramp_delay = MAX77802_RAMP_DELAY, \
332 .n_voltages = 1 << 6, \
333 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
334 .vsel_mask = MAX77802_VSEL_MASK, \
335 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
336 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
337 .of_map_mode = max77802_map_mode, \
341 #define regulator_77802_desc_16_buck(num) { \
342 .name = "BUCK"#num, \
343 .of_match = of_match_ptr("BUCK"#num), \
344 .regulators_node = of_match_ptr("regulators"), \
345 .id = MAX77802_BUCK##num, \
346 .supply_name = "inb"#num, \
347 .ops = &max77802_buck_16_dvs_ops, \
348 .type = REGULATOR_VOLTAGE, \
349 .owner = THIS_MODULE, \
352 .ramp_delay = MAX77802_RAMP_DELAY, \
353 .n_voltages = 1 << 8, \
354 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
355 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
356 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
357 .enable_mask = MAX77802_OPMODE_MASK, \
358 .ramp_reg = MAX77802_REG_BUCK ## num ## CTRL, \
359 .ramp_mask = MAX77802_RAMP_RATE_MASK_4BIT, \
360 .ramp_delay_table = max77802_buck16_ramp_table, \
361 .n_ramp_values = ARRAY_SIZE(max77802_buck16_ramp_table), \
362 .of_map_mode = max77802_map_mode, \
366 #define regulator_77802_desc_234_buck(num) { \
367 .name = "BUCK"#num, \
368 .of_match = of_match_ptr("BUCK"#num), \
369 .regulators_node = of_match_ptr("regulators"), \
370 .id = MAX77802_BUCK##num, \
371 .supply_name = "inb"#num, \
372 .ops = &max77802_buck_234_ops, \
373 .type = REGULATOR_VOLTAGE, \
374 .owner = THIS_MODULE, \
377 .ramp_delay = MAX77802_RAMP_DELAY, \
378 .n_voltages = 0x91, \
379 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
380 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
381 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
382 .enable_mask = MAX77802_OPMODE_MASK << \
383 MAX77802_OPMODE_BUCK234_SHIFT, \
384 .ramp_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
385 .ramp_mask = MAX77802_RAMP_RATE_MASK_2BIT, \
386 .ramp_delay_table = max77802_buck234_ramp_table, \
387 .n_ramp_values = ARRAY_SIZE(max77802_buck234_ramp_table), \
388 .of_map_mode = max77802_map_mode, \
392 #define regulator_77802_desc_buck5(num) { \
393 .name = "BUCK"#num, \
394 .of_match = of_match_ptr("BUCK"#num), \
395 .regulators_node = of_match_ptr("regulators"), \
396 .id = MAX77802_BUCK##num, \
397 .supply_name = "inb"#num, \
398 .ops = &max77802_buck_dvs_ops, \
399 .type = REGULATOR_VOLTAGE, \
400 .owner = THIS_MODULE, \
403 .ramp_delay = MAX77802_RAMP_DELAY, \
404 .n_voltages = 1 << 6, \
405 .vsel_reg = MAX77802_REG_BUCK5OUT, \
406 .vsel_mask = MAX77802_VSEL_MASK, \
407 .enable_reg = MAX77802_REG_BUCK5CTRL, \
408 .enable_mask = MAX77802_OPMODE_MASK, \
409 .of_map_mode = max77802_map_mode, \
413 #define regulator_77802_desc_buck7_10(num) { \
414 .name = "BUCK"#num, \
415 .of_match = of_match_ptr("BUCK"#num), \
416 .regulators_node = of_match_ptr("regulators"), \
417 .id = MAX77802_BUCK##num, \
418 .supply_name = "inb"#num, \
419 .ops = &max77802_buck_dvs_ops, \
420 .type = REGULATOR_VOLTAGE, \
421 .owner = THIS_MODULE, \
424 .ramp_delay = MAX77802_RAMP_DELAY, \
425 .n_voltages = 1 << 6, \
426 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
427 .vsel_mask = MAX77802_VSEL_MASK, \
428 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
429 .enable_mask = MAX77802_OPMODE_MASK, \
430 .of_map_mode = max77802_map_mode, \
433 static const struct regulator_desc regulators[] = {
434 regulator_77802_desc_16_buck(1),
435 regulator_77802_desc_234_buck(2),
436 regulator_77802_desc_234_buck(3),
437 regulator_77802_desc_234_buck(4),
438 regulator_77802_desc_buck5(5),
439 regulator_77802_desc_16_buck(6),
440 regulator_77802_desc_buck7_10(7),
441 regulator_77802_desc_buck7_10(8),
442 regulator_77802_desc_buck7_10(9),
443 regulator_77802_desc_buck7_10(10),
444 regulator_77802_desc_n_ldo(1, 10, 2),
445 regulator_77802_desc_n_ldo(2, 10, 1),
446 regulator_77802_desc_p_ldo(3, 3, 2),
447 regulator_77802_desc_p_ldo(4, 6, 1),
448 regulator_77802_desc_p_ldo(5, 3, 1),
449 regulator_77802_desc_p_ldo(6, 3, 1),
450 regulator_77802_desc_p_ldo(7, 3, 1),
451 regulator_77802_desc_n_ldo(8, 1, 1),
452 regulator_77802_desc_p_ldo(9, 5, 1),
453 regulator_77802_desc_p_ldo(10, 4, 1),
454 regulator_77802_desc_p_ldo(11, 4, 1),
455 regulator_77802_desc_p_ldo(12, 9, 1),
456 regulator_77802_desc_p_ldo(13, 4, 1),
457 regulator_77802_desc_p_ldo(14, 4, 1),
458 regulator_77802_desc_n_ldo(15, 1, 1),
459 regulator_77802_desc_n_ldo(17, 2, 1),
460 regulator_77802_desc_p_ldo(18, 7, 1),
461 regulator_77802_desc_p_ldo(19, 5, 1),
462 regulator_77802_desc_p_ldo(20, 7, 2),
463 regulator_77802_desc_p_ldo(21, 6, 2),
464 regulator_77802_desc_p_ldo(23, 9, 1),
465 regulator_77802_desc_p_ldo(24, 6, 1),
466 regulator_77802_desc_p_ldo(25, 9, 1),
467 regulator_77802_desc_p_ldo(26, 9, 1),
468 regulator_77802_desc_n_ldo(27, 2, 1),
469 regulator_77802_desc_p_ldo(28, 7, 1),
470 regulator_77802_desc_p_ldo(29, 7, 1),
471 regulator_77802_desc_n_ldo(30, 2, 1),
472 regulator_77802_desc_p_ldo(32, 9, 1),
473 regulator_77802_desc_p_ldo(33, 6, 1),
474 regulator_77802_desc_p_ldo(34, 9, 1),
475 regulator_77802_desc_n_ldo(35, 2, 1),
478 static int max77802_pmic_probe(struct platform_device *pdev)
480 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
481 struct max77802_regulator_prv *max77802;
483 struct regulator_config config = { };
485 max77802 = devm_kzalloc(&pdev->dev,
486 sizeof(struct max77802_regulator_prv),
491 config.dev = iodev->dev;
492 config.regmap = iodev->regmap;
493 config.driver_data = max77802;
494 platform_set_drvdata(pdev, max77802);
496 for (i = 0; i < MAX77802_REG_MAX; i++) {
497 struct regulator_dev *rdev;
498 int id = regulators[i].id;
499 int shift = max77802_get_opmode_shift(id);
502 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
505 "cannot read current mode for %d\n", i);
506 val = MAX77802_OPMODE_NORMAL;
508 val = val >> shift & MAX77802_OPMODE_MASK;
512 * If the regulator is disabled and the system warm rebooted,
513 * the hardware reports OFF as the regulator operating mode.
514 * Default to operating mode NORMAL in that case.
516 if (val == MAX77802_STATUS_OFF)
517 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
519 max77802->opmode[id] = val;
521 rdev = devm_regulator_register(&pdev->dev,
522 ®ulators[i], &config);
526 "regulator init failed for %d: %d\n", i, ret);
534 static const struct platform_device_id max77802_pmic_id[] = {
535 {"max77802-pmic", 0},
538 MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
540 static struct platform_driver max77802_pmic_driver = {
542 .name = "max77802-pmic",
544 .probe = max77802_pmic_probe,
545 .id_table = max77802_pmic_id,
548 module_platform_driver(max77802_pmic_driver);
550 MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
551 MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
552 MODULE_LICENSE("GPL");