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>
37 /* Default ramp delay in case it is not manually set */
38 #define MAX77802_RAMP_DELAY 100000 /* uV/us */
40 #define MAX77802_OPMODE_SHIFT_LDO 6
41 #define MAX77802_OPMODE_BUCK234_SHIFT 4
42 #define MAX77802_OPMODE_MASK 0x3
44 #define MAX77802_VSEL_MASK 0x3F
45 #define MAX77802_DVS_VSEL_MASK 0xFF
47 #define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
48 #define MAX77802_RAMP_RATE_SHIFT_2BIT 6
49 #define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
50 #define MAX77802_RAMP_RATE_SHIFT_4BIT 4
52 #define MAX77802_OFF_PWRREQ 0x1
54 /* MAX77802 has two register formats: 2-bit and 4-bit */
55 static const unsigned int ramp_table_77802_2bit[] = {
62 static unsigned int ramp_table_77802_4bit[] = {
63 1000, 2000, 3030, 4000,
64 5000, 5880, 7140, 8330,
65 9090, 10000, 11110, 12500,
66 16670, 25000, 50000, 100000,
69 struct max77802_regulator_prv {
70 unsigned int opmode[MAX77802_REG_MAX];
73 static int max77802_get_opmode_shift(int id)
75 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
76 id <= MAX77802_BUCK10))
79 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
80 return MAX77802_OPMODE_BUCK234_SHIFT;
82 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
83 return MAX77802_OPMODE_SHIFT_LDO;
89 * max77802_set_suspend_disable - Disable the regulator during system suspend
90 * @rdev: regulator to mark as disabled
92 * All regulators expect LDO 1, 3, 20 and 21 support OFF by PWRREQ.
93 * Configure the regulator so the PMIC will turn it OFF during system suspend.
95 static int max77802_set_suspend_disable(struct regulator_dev *rdev)
97 unsigned int val = MAX77802_OFF_PWRREQ;
98 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
99 int id = rdev_get_id(rdev);
100 int shift = max77802_get_opmode_shift(id);
102 max77802->opmode[id] = val;
103 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
104 rdev->desc->enable_mask, val << shift);
108 * Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state
109 * (Enable Control Logic1 by PWRREQ)
111 * LDOs 2, 4-19, 22-35.
114 static int max77802_ldo_set_suspend_mode_logic1(struct regulator_dev *rdev,
117 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
118 int id = rdev_get_id(rdev);
120 int shift = max77802_get_opmode_shift(id);
123 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
124 val = MAX77802_OPMODE_LP;
126 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
127 val = MAX77802_OPMODE_NORMAL;
129 case REGULATOR_MODE_STANDBY: /* ON/OFF by PWRREQ */
130 val = MAX77802_OPMODE_STANDBY;
133 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
134 rdev->desc->name, mode);
138 max77802->opmode[id] = val;
139 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
140 rdev->desc->enable_mask, val << shift);
144 * Mode 1 (Output[ON/OFF] by PWRREQ) is not supported on some LDOs
145 * (Enable Control Logic2 by PWRREQ)
147 * LDOs 1, 20, 21, and 3,
150 static int max77802_ldo_set_suspend_mode_logic2(struct regulator_dev *rdev,
153 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
154 int id = rdev_get_id(rdev);
156 int shift = max77802_get_opmode_shift(id);
159 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
160 val = MAX77802_OPMODE_LP;
162 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
163 val = MAX77802_OPMODE_NORMAL;
166 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
167 rdev->desc->name, mode);
171 max77802->opmode[id] = val;
172 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
173 rdev->desc->enable_mask, val << shift);
176 static int max77802_enable(struct regulator_dev *rdev)
178 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
179 int id = rdev_get_id(rdev);
180 int shift = max77802_get_opmode_shift(id);
182 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
183 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
185 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
186 rdev->desc->enable_mask,
187 max77802->opmode[id] << shift);
190 static int max77802_find_ramp_value(struct regulator_dev *rdev,
191 const unsigned int limits[], int size,
192 unsigned int ramp_delay)
196 for (i = 0; i < size; i++) {
197 if (ramp_delay <= limits[i])
201 /* Use maximum value for no ramp control */
202 dev_warn(&rdev->dev, "%s: ramp_delay: %d not supported, setting 100000\n",
203 rdev->desc->name, ramp_delay);
207 /* Used for BUCKs 2-4 */
208 static int max77802_set_ramp_delay_2bit(struct regulator_dev *rdev,
211 int id = rdev_get_id(rdev);
212 unsigned int ramp_value;
214 if (id > MAX77802_BUCK4) {
216 "%s: regulator: ramp delay not supported\n",
220 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_2bit,
221 ARRAY_SIZE(ramp_table_77802_2bit), ramp_delay);
223 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
224 MAX77802_RAMP_RATE_MASK_2BIT,
225 ramp_value << MAX77802_RAMP_RATE_SHIFT_2BIT);
229 static int max77802_set_ramp_delay_4bit(struct regulator_dev *rdev,
232 unsigned int ramp_value;
234 ramp_value = max77802_find_ramp_value(rdev, ramp_table_77802_4bit,
235 ARRAY_SIZE(ramp_table_77802_4bit), ramp_delay);
237 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
238 MAX77802_RAMP_RATE_MASK_4BIT,
239 ramp_value << MAX77802_RAMP_RATE_SHIFT_4BIT);
243 * LDOs 2, 4-19, 22-35
245 static struct regulator_ops max77802_ldo_ops_logic1 = {
246 .list_voltage = regulator_list_voltage_linear,
247 .map_voltage = regulator_map_voltage_linear,
248 .is_enabled = regulator_is_enabled_regmap,
249 .enable = max77802_enable,
250 .disable = regulator_disable_regmap,
251 .get_voltage_sel = regulator_get_voltage_sel_regmap,
252 .set_voltage_sel = regulator_set_voltage_sel_regmap,
253 .set_voltage_time_sel = regulator_set_voltage_time_sel,
254 .set_suspend_enable = max77802_enable,
255 .set_suspend_disable = max77802_set_suspend_disable,
256 .set_suspend_mode = max77802_ldo_set_suspend_mode_logic1,
262 static struct regulator_ops max77802_ldo_ops_logic2 = {
263 .list_voltage = regulator_list_voltage_linear,
264 .map_voltage = regulator_map_voltage_linear,
265 .is_enabled = regulator_is_enabled_regmap,
266 .enable = max77802_enable,
267 .disable = regulator_disable_regmap,
268 .get_voltage_sel = regulator_get_voltage_sel_regmap,
269 .set_voltage_sel = regulator_set_voltage_sel_regmap,
270 .set_voltage_time_sel = regulator_set_voltage_time_sel,
271 .set_suspend_mode = max77802_ldo_set_suspend_mode_logic2,
275 static struct regulator_ops max77802_buck_16_dvs_ops = {
276 .list_voltage = regulator_list_voltage_linear,
277 .map_voltage = regulator_map_voltage_linear,
278 .is_enabled = regulator_is_enabled_regmap,
279 .enable = max77802_enable,
280 .disable = regulator_disable_regmap,
281 .get_voltage_sel = regulator_get_voltage_sel_regmap,
282 .set_voltage_sel = regulator_set_voltage_sel_regmap,
283 .set_voltage_time_sel = regulator_set_voltage_time_sel,
284 .set_ramp_delay = max77802_set_ramp_delay_4bit,
285 .set_suspend_enable = max77802_enable,
286 .set_suspend_disable = max77802_set_suspend_disable,
290 static struct regulator_ops max77802_buck_234_ops = {
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_ramp_delay = max77802_set_ramp_delay_2bit,
300 .set_suspend_enable = max77802_enable,
301 .set_suspend_disable = max77802_set_suspend_disable,
305 static struct regulator_ops max77802_buck_dvs_ops = {
306 .list_voltage = regulator_list_voltage_linear,
307 .map_voltage = regulator_map_voltage_linear,
308 .is_enabled = regulator_is_enabled_regmap,
309 .enable = max77802_enable,
310 .disable = regulator_disable_regmap,
311 .get_voltage_sel = regulator_get_voltage_sel_regmap,
312 .set_voltage_sel = regulator_set_voltage_sel_regmap,
313 .set_voltage_time_sel = regulator_set_voltage_time_sel,
314 .set_ramp_delay = max77802_set_ramp_delay_2bit,
315 .set_suspend_enable = max77802_enable,
316 .set_suspend_disable = max77802_set_suspend_disable,
319 /* LDOs 3-7, 9-14, 18-26, 28, 29, 32-34 */
320 #define regulator_77802_desc_p_ldo(num, supply, log) { \
322 .id = MAX77802_LDO##num, \
323 .supply_name = "inl"#supply, \
324 .ops = &max77802_ldo_ops_logic##log, \
325 .type = REGULATOR_VOLTAGE, \
326 .owner = THIS_MODULE, \
329 .ramp_delay = MAX77802_RAMP_DELAY, \
330 .n_voltages = 1 << 6, \
331 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
332 .vsel_mask = MAX77802_VSEL_MASK, \
333 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
334 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
337 /* LDOs 1, 2, 8, 15, 17, 27, 30, 35 */
338 #define regulator_77802_desc_n_ldo(num, supply, log) { \
340 .id = MAX77802_LDO##num, \
341 .supply_name = "inl"#supply, \
342 .ops = &max77802_ldo_ops_logic##log, \
343 .type = REGULATOR_VOLTAGE, \
344 .owner = THIS_MODULE, \
347 .ramp_delay = MAX77802_RAMP_DELAY, \
348 .n_voltages = 1 << 6, \
349 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
350 .vsel_mask = MAX77802_VSEL_MASK, \
351 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
352 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
356 #define regulator_77802_desc_16_buck(num) { \
357 .name = "BUCK"#num, \
358 .id = MAX77802_BUCK##num, \
359 .supply_name = "inb"#num, \
360 .ops = &max77802_buck_16_dvs_ops, \
361 .type = REGULATOR_VOLTAGE, \
362 .owner = THIS_MODULE, \
365 .ramp_delay = MAX77802_RAMP_DELAY, \
366 .n_voltages = 1 << 8, \
367 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
368 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
369 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
370 .enable_mask = MAX77802_OPMODE_MASK, \
374 #define regulator_77802_desc_234_buck(num) { \
375 .name = "BUCK"#num, \
376 .id = MAX77802_BUCK##num, \
377 .supply_name = "inb"#num, \
378 .ops = &max77802_buck_234_ops, \
379 .type = REGULATOR_VOLTAGE, \
380 .owner = THIS_MODULE, \
383 .ramp_delay = MAX77802_RAMP_DELAY, \
384 .n_voltages = 0x91, \
385 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
386 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
387 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
388 .enable_mask = MAX77802_OPMODE_MASK << \
389 MAX77802_OPMODE_BUCK234_SHIFT, \
393 #define regulator_77802_desc_buck5(num) { \
394 .name = "BUCK"#num, \
395 .id = MAX77802_BUCK##num, \
396 .supply_name = "inb"#num, \
397 .ops = &max77802_buck_dvs_ops, \
398 .type = REGULATOR_VOLTAGE, \
399 .owner = THIS_MODULE, \
402 .ramp_delay = MAX77802_RAMP_DELAY, \
403 .n_voltages = 1 << 6, \
404 .vsel_reg = MAX77802_REG_BUCK5OUT, \
405 .vsel_mask = MAX77802_VSEL_MASK, \
406 .enable_reg = MAX77802_REG_BUCK5CTRL, \
407 .enable_mask = MAX77802_OPMODE_MASK, \
411 #define regulator_77802_desc_buck7_10(num) { \
412 .name = "BUCK"#num, \
413 .id = MAX77802_BUCK##num, \
414 .supply_name = "inb"#num, \
415 .ops = &max77802_buck_dvs_ops, \
416 .type = REGULATOR_VOLTAGE, \
417 .owner = THIS_MODULE, \
420 .ramp_delay = MAX77802_RAMP_DELAY, \
421 .n_voltages = 1 << 6, \
422 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
423 .vsel_mask = MAX77802_VSEL_MASK, \
424 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
425 .enable_mask = MAX77802_OPMODE_MASK, \
428 static struct regulator_desc regulators[] = {
429 regulator_77802_desc_16_buck(1),
430 regulator_77802_desc_234_buck(2),
431 regulator_77802_desc_234_buck(3),
432 regulator_77802_desc_234_buck(4),
433 regulator_77802_desc_buck5(5),
434 regulator_77802_desc_16_buck(6),
435 regulator_77802_desc_buck7_10(7),
436 regulator_77802_desc_buck7_10(8),
437 regulator_77802_desc_buck7_10(9),
438 regulator_77802_desc_buck7_10(10),
439 regulator_77802_desc_n_ldo(1, 10, 2),
440 regulator_77802_desc_n_ldo(2, 10, 1),
441 regulator_77802_desc_p_ldo(3, 3, 2),
442 regulator_77802_desc_p_ldo(4, 6, 1),
443 regulator_77802_desc_p_ldo(5, 3, 1),
444 regulator_77802_desc_p_ldo(6, 3, 1),
445 regulator_77802_desc_p_ldo(7, 3, 1),
446 regulator_77802_desc_n_ldo(8, 1, 1),
447 regulator_77802_desc_p_ldo(9, 5, 1),
448 regulator_77802_desc_p_ldo(10, 4, 1),
449 regulator_77802_desc_p_ldo(11, 4, 1),
450 regulator_77802_desc_p_ldo(12, 9, 1),
451 regulator_77802_desc_p_ldo(13, 4, 1),
452 regulator_77802_desc_p_ldo(14, 4, 1),
453 regulator_77802_desc_n_ldo(15, 1, 1),
454 regulator_77802_desc_n_ldo(17, 2, 1),
455 regulator_77802_desc_p_ldo(18, 7, 1),
456 regulator_77802_desc_p_ldo(19, 5, 1),
457 regulator_77802_desc_p_ldo(20, 7, 2),
458 regulator_77802_desc_p_ldo(21, 6, 2),
459 regulator_77802_desc_p_ldo(23, 9, 1),
460 regulator_77802_desc_p_ldo(24, 6, 1),
461 regulator_77802_desc_p_ldo(25, 9, 1),
462 regulator_77802_desc_p_ldo(26, 9, 1),
463 regulator_77802_desc_n_ldo(27, 2, 1),
464 regulator_77802_desc_p_ldo(28, 7, 1),
465 regulator_77802_desc_p_ldo(29, 7, 1),
466 regulator_77802_desc_n_ldo(30, 2, 1),
467 regulator_77802_desc_p_ldo(32, 9, 1),
468 regulator_77802_desc_p_ldo(33, 6, 1),
469 regulator_77802_desc_p_ldo(34, 9, 1),
470 regulator_77802_desc_n_ldo(35, 2, 1),
474 static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev,
475 struct max77686_platform_data *pdata)
477 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
478 struct device_node *pmic_np, *regulators_np;
479 struct max77686_regulator_data *rdata;
480 struct of_regulator_match rmatch;
483 pmic_np = iodev->dev->of_node;
484 regulators_np = of_get_child_by_name(pmic_np, "regulators");
485 if (!regulators_np) {
486 dev_err(&pdev->dev, "could not find regulators sub-node\n");
490 pdata->num_regulators = ARRAY_SIZE(regulators);
491 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
492 pdata->num_regulators, GFP_KERNEL);
494 of_node_put(regulators_np);
498 for (i = 0; i < pdata->num_regulators; i++) {
499 rmatch.name = regulators[i].name;
500 rmatch.init_data = NULL;
501 rmatch.of_node = NULL;
502 if (of_regulator_match(&pdev->dev, regulators_np, &rmatch,
504 dev_warn(&pdev->dev, "No matching regulator for '%s'\n",
508 rdata[i].initdata = rmatch.init_data;
509 rdata[i].of_node = rmatch.of_node;
510 rdata[i].id = regulators[i].id;
513 pdata->regulators = rdata;
514 of_node_put(regulators_np);
519 static int max77802_pmic_dt_parse_pdata(struct platform_device *pdev,
520 struct max77686_platform_data *pdata)
524 #endif /* CONFIG_OF */
526 static int max77802_pmic_probe(struct platform_device *pdev)
528 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
529 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
530 struct max77802_regulator_prv *max77802;
532 struct regulator_config config = { };
534 /* This is allocated by the MFD driver */
536 dev_err(&pdev->dev, "no platform data found for regulator\n");
540 max77802 = devm_kzalloc(&pdev->dev,
541 sizeof(struct max77802_regulator_prv),
546 if (iodev->dev->of_node) {
547 ret = max77802_pmic_dt_parse_pdata(pdev, pdata);
552 config.dev = iodev->dev;
553 config.regmap = iodev->regmap;
554 config.driver_data = max77802;
555 platform_set_drvdata(pdev, max77802);
557 for (i = 0; i < MAX77802_REG_MAX; i++) {
558 struct regulator_dev *rdev;
559 int id = pdata->regulators[i].id;
560 int shift = max77802_get_opmode_shift(id);
562 config.init_data = pdata->regulators[i].initdata;
563 config.of_node = pdata->regulators[i].of_node;
565 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
566 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_OPMODE_OFF)
574 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
576 max77802->opmode[id] = val;
578 rdev = devm_regulator_register(&pdev->dev,
579 ®ulators[i], &config);
582 "regulator init failed for %d\n", i);
583 return PTR_ERR(rdev);
590 static const struct platform_device_id max77802_pmic_id[] = {
591 {"max77802-pmic", 0},
594 MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
596 static struct platform_driver max77802_pmic_driver = {
598 .name = "max77802-pmic",
599 .owner = THIS_MODULE,
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");