2 * max77802.c - Regulator driver for the Maxim 77802
4 * Copyright (C) 2013-2014 Google, Inc
5 * Simon Glass <sjg@chromium.org>
7 * Copyright (C) 2012 Samsung Electronics
8 * Chiwoong Byun <woong.byun@smasung.com>
9 * Jonghwa Lee <jonghwa3.lee@samsung.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * This driver is based on max8997.c
24 #include <linux/kernel.h>
25 #include <linux/bug.h>
26 #include <linux/err.h>
27 #include <linux/gpio.h>
28 #include <linux/slab.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/regulator/of_regulator.h>
34 #include <linux/mfd/max77686.h>
35 #include <linux/mfd/max77686-private.h>
36 #include <dt-bindings/regulator/maxim,max77802.h>
38 /* Default ramp delay in case it is not manually set */
39 #define MAX77802_RAMP_DELAY 100000 /* uV/us */
41 #define MAX77802_OPMODE_SHIFT_LDO 6
42 #define MAX77802_OPMODE_BUCK234_SHIFT 4
43 #define MAX77802_OPMODE_MASK 0x3
45 #define MAX77802_VSEL_MASK 0x3F
46 #define MAX77802_DVS_VSEL_MASK 0xFF
48 #define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
49 #define MAX77802_RAMP_RATE_SHIFT_2BIT 6
50 #define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
51 #define MAX77802_RAMP_RATE_SHIFT_4BIT 4
53 #define MAX77802_STATUS_OFF 0x0
54 #define MAX77802_OFF_PWRREQ 0x1
55 #define MAX77802_LP_PWRREQ 0x2
57 /* MAX77802 has two register formats: 2-bit and 4-bit */
58 static const unsigned int ramp_table_77802_2bit[] = {
65 static unsigned int ramp_table_77802_4bit[] = {
66 1000, 2000, 3030, 4000,
67 5000, 5880, 7140, 8330,
68 9090, 10000, 11110, 12500,
69 16670, 25000, 50000, 100000,
72 struct max77802_regulator_prv {
73 /* Array indexed by regulator id */
74 unsigned int opmode[MAX77802_REG_MAX];
77 static inline unsigned int max77802_map_mode(unsigned int mode)
79 return mode == MAX77802_OPMODE_NORMAL ?
80 REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
83 static int max77802_get_opmode_shift(int id)
85 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
86 id <= MAX77802_BUCK10))
89 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
90 return MAX77802_OPMODE_BUCK234_SHIFT;
92 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
93 return MAX77802_OPMODE_SHIFT_LDO;
99 * max77802_set_suspend_disable - Disable the regulator during system suspend
100 * @rdev: regulator to mark as disabled
102 * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
103 * Configure the regulator so the PMIC will turn it OFF during system suspend.
105 static int max77802_set_suspend_disable(struct regulator_dev *rdev)
107 unsigned int val = MAX77802_OFF_PWRREQ;
108 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
109 int id = rdev_get_id(rdev);
110 int shift = max77802_get_opmode_shift(id);
112 max77802->opmode[id] = val;
113 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
114 rdev->desc->enable_mask, val << shift);
118 * Some LDOs support Low Power Mode while the system is running.
122 static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
124 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
125 int id = rdev_get_id(rdev);
127 int shift = max77802_get_opmode_shift(id);
130 case REGULATOR_MODE_STANDBY:
131 val = MAX77802_OPMODE_LP; /* ON in Low Power Mode */
133 case REGULATOR_MODE_NORMAL:
134 val = MAX77802_OPMODE_NORMAL; /* ON in Normal Mode */
137 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
138 rdev->desc->name, mode);
142 max77802->opmode[id] = val;
143 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
144 rdev->desc->enable_mask, val << shift);
147 static unsigned max77802_get_mode(struct regulator_dev *rdev)
149 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
150 int id = rdev_get_id(rdev);
152 return max77802_map_mode(max77802->opmode[id]);
156 * max77802_set_suspend_mode - set regulator opmode when the system is suspended
157 * @rdev: regulator to change mode
158 * @mode: operating mode to be set
160 * Will set the operating mode for the regulators during system suspend.
161 * This function is valid for the three different enable control logics:
163 * Enable Control Logic1 by PWRREQ (BUCK 2-4 and LDOs 2, 4-19, 22-35)
164 * Enable Control Logic2 by PWRREQ (LDOs 1, 20, 21)
165 * Enable Control Logic3 by PWRREQ (LDO 3)
167 * If setting the regulator mode fails, the function only warns but does
168 * not return an error code to avoid the regulator core to stop setting
169 * the operating mode for the remaining regulators.
171 static int max77802_set_suspend_mode(struct regulator_dev *rdev,
174 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
175 int id = rdev_get_id(rdev);
177 int shift = max77802_get_opmode_shift(id);
180 * If the regulator has been disabled for suspend
181 * then is invalid to try setting a suspend mode.
183 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
184 dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
185 rdev->desc->name, mode);
190 case REGULATOR_MODE_STANDBY:
192 * If the regulator opmode is normal then enable
193 * ON in Low Power Mode by PWRREQ. If the mode is
194 * already Low Power then no action is required.
196 if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
197 val = MAX77802_LP_PWRREQ;
201 case REGULATOR_MODE_NORMAL:
203 * If the regulator operating mode is Low Power then
204 * normal is not a valid opmode in suspend. If the
205 * mode is already normal then no action is required.
207 if (max77802->opmode[id] == MAX77802_OPMODE_LP)
208 dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
209 rdev->desc->name, mode);
212 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
213 rdev->desc->name, mode);
217 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
218 rdev->desc->enable_mask, val << shift);
221 static int max77802_enable(struct regulator_dev *rdev)
223 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
224 int id = rdev_get_id(rdev);
225 int shift = max77802_get_opmode_shift(id);
227 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
228 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
230 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
231 rdev->desc->enable_mask,
232 max77802->opmode[id] << shift);
235 static int max77802_find_ramp_value(struct regulator_dev *rdev,
236 const unsigned int limits[], int size,
237 unsigned int ramp_delay)
241 for (i = 0; i < size; i++) {
242 if (ramp_delay <= limits[i])
246 /* Use maximum value for no ramp control */
247 dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n",
248 rdev->desc->name, ramp_delay);
252 /* Used for BUCKs 2-4 */
253 static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev,
256 int id = rdev_get_id(rdev);
257 unsigned int ramp_value;
259 if (id > MAX77802_BUCK4) {
261 "%s: regulator: ramp delay not supported\n",
265 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit,
266 ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay);
268 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
269 MAX77802_RAMP_RATE_MASK_2BIT,
270 ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT);
274 static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev,
277 unsigned int ramp_value;
279 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit,
280 ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay);
282 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
283 MAX77802_RAMP_RATE_MASK_4BIT,
284 ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT);
288 * LDOs 2, 4-19, 22-35
290 static struct regulator_ops max77802_ldo_ops_logic1 = {
291 .list_voltage = regulator_list_voltage_linear,
292 .map_voltage = regulator_map_voltage_linear,
293 .is_enabled = regulator_is_enabled_regmap,
294 .enable = max77802_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_disable = max77802_set_suspend_disable,
300 .set_suspend_mode = max77802_set_suspend_mode,
306 static struct regulator_ops max77802_ldo_ops_logic2 = {
307 .list_voltage = regulator_list_voltage_linear,
308 .map_voltage = regulator_map_voltage_linear,
309 .is_enabled = regulator_is_enabled_regmap,
310 .enable = max77802_enable,
311 .disable = regulator_disable_regmap,
312 .get_voltage_sel = regulator_get_voltage_sel_regmap,
313 .set_voltage_sel = regulator_set_voltage_sel_regmap,
314 .set_voltage_time_sel = regulator_set_voltage_time_sel,
315 .set_mode = max77802_set_mode,
316 .get_mode = max77802_get_mode,
317 .set_suspend_mode = max77802_set_suspend_mode,
321 static struct regulator_ops max77802_buck_16_dvs_ops = {
322 .list_voltage = regulator_list_voltage_linear,
323 .map_voltage = regulator_map_voltage_linear,
324 .is_enabled = regulator_is_enabled_regmap,
325 .enable = max77802_enable,
326 .disable = regulator_disable_regmap,
327 .get_voltage_sel = regulator_get_voltage_sel_regmap,
328 .set_voltage_sel = regulator_set_voltage_sel_regmap,
329 .set_voltage_time_sel = regulator_set_voltage_time_sel,
330 .set_ramp_delay = max77802_set_ramp_delay_4bit,
331 .set_suspend_disable = max77802_set_suspend_disable,
335 static struct regulator_ops max77802_buck_234_ops = {
336 .list_voltage = regulator_list_voltage_linear,
337 .map_voltage = regulator_map_voltage_linear,
338 .is_enabled = regulator_is_enabled_regmap,
339 .enable = max77802_enable,
340 .disable = regulator_disable_regmap,
341 .get_voltage_sel = regulator_get_voltage_sel_regmap,
342 .set_voltage_sel = regulator_set_voltage_sel_regmap,
343 .set_voltage_time_sel = regulator_set_voltage_time_sel,
344 .set_ramp_delay = max77802_set_ramp_delay_2bit,
345 .set_suspend_disable = max77802_set_suspend_disable,
346 .set_suspend_mode = max77802_set_suspend_mode,
350 static struct regulator_ops max77802_buck_dvs_ops = {
351 .list_voltage = regulator_list_voltage_linear,
352 .map_voltage = regulator_map_voltage_linear,
353 .is_enabled = regulator_is_enabled_regmap,
354 .enable = max77802_enable,
355 .disable = regulator_disable_regmap,
356 .get_voltage_sel = regulator_get_voltage_sel_regmap,
357 .set_voltage_sel = regulator_set_voltage_sel_regmap,
358 .set_voltage_time_sel = regulator_set_voltage_time_sel,
359 .set_ramp_delay = max77802_set_ramp_delay_2bit,
360 .set_suspend_disable = max77802_set_suspend_disable,
363 /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
364 #define regulator_77802_desc_p_ldo(num, supply, log) { \
366 .of_match = of_match_ptr("LDO"#num), \
367 .regulators_node = of_match_ptr("regulators"), \
368 .id = MAX77802_LDO##num, \
369 .supply_name = "inl"#supply, \
370 .ops = &max77802_ldo_ops_logic##log, \
371 .type = REGULATOR_VOLTAGE, \
372 .owner = THIS_MODULE, \
375 .ramp_delay = MAX77802_RAMP_DELAY, \
376 .n_voltages = 1 << 6, \
377 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
378 .vsel_mask = MAX77802_VSEL_MASK, \
379 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
380 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
381 .of_map_mode = max77802_map_mode, \
384 /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
385 #define regulator_77802_desc_n_ldo(num, supply, log) { \
387 .of_match = of_match_ptr("LDO"#num), \
388 .regulators_node = of_match_ptr("regulators"), \
389 .id = MAX77802_LDO##num, \
390 .supply_name = "inl"#supply, \
391 .ops = &max77802_ldo_ops_logic##log, \
392 .type = REGULATOR_VOLTAGE, \
393 .owner = THIS_MODULE, \
396 .ramp_delay = MAX77802_RAMP_DELAY, \
397 .n_voltages = 1 << 6, \
398 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
399 .vsel_mask = MAX77802_VSEL_MASK, \
400 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
401 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
402 .of_map_mode = max77802_map_mode, \
406 #define regulator_77802_desc_16_buck(num) { \
407 .name = "BUCK"#num, \
408 .of_match = of_match_ptr("BUCK"#num), \
409 .regulators_node = of_match_ptr("regulators"), \
410 .id = MAX77802_BUCK##num, \
411 .supply_name = "inb"#num, \
412 .ops = &max77802_buck_16_dvs_ops, \
413 .type = REGULATOR_VOLTAGE, \
414 .owner = THIS_MODULE, \
417 .ramp_delay = MAX77802_RAMP_DELAY, \
418 .n_voltages = 1 << 8, \
419 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
420 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
421 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
422 .enable_mask = MAX77802_OPMODE_MASK, \
423 .of_map_mode = max77802_map_mode, \
427 #define regulator_77802_desc_234_buck(num) { \
428 .name = "BUCK"#num, \
429 .of_match = of_match_ptr("BUCK"#num), \
430 .regulators_node = of_match_ptr("regulators"), \
431 .id = MAX77802_BUCK##num, \
432 .supply_name = "inb"#num, \
433 .ops = &max77802_buck_234_ops, \
434 .type = REGULATOR_VOLTAGE, \
435 .owner = THIS_MODULE, \
438 .ramp_delay = MAX77802_RAMP_DELAY, \
439 .n_voltages = 0x91, \
440 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
441 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
442 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
443 .enable_mask = MAX77802_OPMODE_MASK << \
444 MAX77802_OPMODE_BUCK234_SHIFT, \
445 .of_map_mode = max77802_map_mode, \
449 #define regulator_77802_desc_buck5(num) { \
450 .name = "BUCK"#num, \
451 .of_match = of_match_ptr("BUCK"#num), \
452 .regulators_node = of_match_ptr("regulators"), \
453 .id = MAX77802_BUCK##num, \
454 .supply_name = "inb"#num, \
455 .ops = &max77802_buck_dvs_ops, \
456 .type = REGULATOR_VOLTAGE, \
457 .owner = THIS_MODULE, \
460 .ramp_delay = MAX77802_RAMP_DELAY, \
461 .n_voltages = 1 << 6, \
462 .vsel_reg = MAX77802_REG_BUCK5OUT, \
463 .vsel_mask = MAX77802_VSEL_MASK, \
464 .enable_reg = MAX77802_REG_BUCK5CTRL, \
465 .enable_mask = MAX77802_OPMODE_MASK, \
466 .of_map_mode = max77802_map_mode, \
470 #define regulator_77802_desc_buck7_10(num) { \
471 .name = "BUCK"#num, \
472 .of_match = of_match_ptr("BUCK"#num), \
473 .regulators_node = of_match_ptr("regulators"), \
474 .id = MAX77802_BUCK##num, \
475 .supply_name = "inb"#num, \
476 .ops = &max77802_buck_dvs_ops, \
477 .type = REGULATOR_VOLTAGE, \
478 .owner = THIS_MODULE, \
481 .ramp_delay = MAX77802_RAMP_DELAY, \
482 .n_voltages = 1 << 6, \
483 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
484 .vsel_mask = MAX77802_VSEL_MASK, \
485 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
486 .enable_mask = MAX77802_OPMODE_MASK, \
487 .of_map_mode = max77802_map_mode, \
490 static const struct regulator_desc regulators[] = {
491 regulator_77802_desc_16_buck(1),
492 regulator_77802_desc_234_buck(2),
493 regulator_77802_desc_234_buck(3),
494 regulator_77802_desc_234_buck(4),
495 regulator_77802_desc_buck5(5),
496 regulator_77802_desc_16_buck(6),
497 regulator_77802_desc_buck7_10(7),
498 regulator_77802_desc_buck7_10(8),
499 regulator_77802_desc_buck7_10(9),
500 regulator_77802_desc_buck7_10(10),
501 regulator_77802_desc_n_ldo(1, 10, 2),
502 regulator_77802_desc_n_ldo(2, 10, 1),
503 regulator_77802_desc_p_ldo(3, 3, 2),
504 regulator_77802_desc_p_ldo(4, 6, 1),
505 regulator_77802_desc_p_ldo(5, 3, 1),
506 regulator_77802_desc_p_ldo(6, 3, 1),
507 regulator_77802_desc_p_ldo(7, 3, 1),
508 regulator_77802_desc_n_ldo(8, 1, 1),
509 regulator_77802_desc_p_ldo(9, 5, 1),
510 regulator_77802_desc_p_ldo(10, 4, 1),
511 regulator_77802_desc_p_ldo(11, 4, 1),
512 regulator_77802_desc_p_ldo(12, 9, 1),
513 regulator_77802_desc_p_ldo(13, 4, 1),
514 regulator_77802_desc_p_ldo(14, 4, 1),
515 regulator_77802_desc_n_ldo(15, 1, 1),
516 regulator_77802_desc_n_ldo(17, 2, 1),
517 regulator_77802_desc_p_ldo(18, 7, 1),
518 regulator_77802_desc_p_ldo(19, 5, 1),
519 regulator_77802_desc_p_ldo(20, 7, 2),
520 regulator_77802_desc_p_ldo(21, 6, 2),
521 regulator_77802_desc_p_ldo(23, 9, 1),
522 regulator_77802_desc_p_ldo(24, 6, 1),
523 regulator_77802_desc_p_ldo(25, 9, 1),
524 regulator_77802_desc_p_ldo(26, 9, 1),
525 regulator_77802_desc_n_ldo(27, 2, 1),
526 regulator_77802_desc_p_ldo(28, 7, 1),
527 regulator_77802_desc_p_ldo(29, 7, 1),
528 regulator_77802_desc_n_ldo(30, 2, 1),
529 regulator_77802_desc_p_ldo(32, 9, 1),
530 regulator_77802_desc_p_ldo(33, 6, 1),
531 regulator_77802_desc_p_ldo(34, 9, 1),
532 regulator_77802_desc_n_ldo(35, 2, 1),
535 static int max77802_pmic_probe(struct platform_device *pdev)
537 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
538 struct max77802_regulator_prv *max77802;
540 struct regulator_config config = { };
542 max77802 = devm_kzalloc(&pdev->dev,
543 sizeof(struct max77802_regulator_prv),
548 config.dev = iodev->dev;
549 config.regmap = iodev->regmap;
550 config.driver_data = max77802;
551 platform_set_drvdata(pdev, max77802);
553 for (i = 0; i < MAX77802_REG_MAX; i++) {
554 struct regulator_dev *rdev;
555 int id = regulators[i].id;
556 int shift = max77802_get_opmode_shift(id);
559 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
562 "cannot read current mode for %d\n", i);
563 val = MAX77802_OPMODE_NORMAL;
565 val = val >> shift & MAX77802_OPMODE_MASK;
569 * If the regulator is disabled and the system warm rebooted,
570 * the hardware reports OFF as the regulator operating mode.
571 * Default to operating mode NORMAL in that case.
573 if (val == MAX77802_STATUS_OFF)
574 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
576 max77802->opmode[id] = val;
578 rdev = devm_regulator_register(&pdev->dev,
579 ®ulators[i], &config);
583 "regulator init failed for %d: %d\n", i, ret);
591 static const struct platform_device_id max77802_pmic_id[] = {
592 {"max77802-pmic", 0},
595 MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
597 static struct platform_driver max77802_pmic_driver = {
599 .name = "max77802-pmic",
601 .probe = max77802_pmic_probe,
602 .id_table = max77802_pmic_id,
605 module_platform_driver(max77802_pmic_driver);
607 MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
608 MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
609 MODULE_LICENSE("GPL");