2 * max77686.c - Regulator driver for the Maxim 77686
4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@smasung.com>
6 * Jonghwa Lee <jonghwa3.lee@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This driver is based on max8997.c
25 #include <linux/kernel.h>
26 #include <linux/bug.h>
27 #include <linux/delay.h>
28 #include <linux/err.h>
29 #include <linux/gpio.h>
30 #include <linux/slab.h>
31 #include <linux/platform_device.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/regulator/of_regulator.h>
35 #include <linux/mfd/max77686.h>
36 #include <linux/mfd/max77686-private.h>
38 #define MAX77686_LDO_MINUV 800000
39 #define MAX77686_LDO_UVSTEP 50000
40 #define MAX77686_LDO_LOW_MINUV 800000
41 #define MAX77686_LDO_LOW_UVSTEP 25000
42 #define MAX77686_BUCK_MINUV 750000
43 #define MAX77686_BUCK_UVSTEP 50000
44 #define MAX77686_RAMP_DELAY 100000 /* uV/us */
45 #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
46 #define MAX77686_DVS_MINUV 600000
47 #define MAX77686_DVS_UVSTEP 12500
49 #define MAX77686_OPMODE_SHIFT 6
50 #define MAX77686_OPMODE_BUCK234_SHIFT 4
51 #define MAX77686_OPMODE_MASK 0x3
53 #define MAX77686_VSEL_MASK 0x3F
54 #define MAX77686_DVS_VSEL_MASK 0xFF
56 #define MAX77686_RAMP_RATE_MASK 0xC0
58 #define MAX77686_REGULATORS MAX77686_REG_MAX
59 #define MAX77686_LDOS 26
61 enum max77686_ramp_rate
{
65 RAMP_RATE_NO_CTRL
, /* 100mV/us */
68 struct max77686_data
{
69 struct regulator_dev
*rdev
[MAX77686_REGULATORS
];
70 unsigned int opmode
[MAX77686_REGULATORS
];
73 /* Some BUCKS supports Normal[ON/OFF] mode during suspend */
74 static int max77686_buck_set_suspend_disable(struct regulator_dev
*rdev
)
77 struct max77686_data
*max77686
= rdev_get_drvdata(rdev
);
79 if (rdev
->desc
->id
== MAX77686_BUCK1
)
82 val
= 0x1 << MAX77686_OPMODE_BUCK234_SHIFT
;
84 max77686
->opmode
[rdev
->desc
->id
] = val
;
85 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
86 rdev
->desc
->enable_mask
,
90 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
91 static int max77686_set_suspend_mode(struct regulator_dev
*rdev
,
94 struct max77686_data
*max77686
= rdev_get_drvdata(rdev
);
97 /* BUCK[5-9] doesn't support this feature */
98 if (rdev
->desc
->id
>= MAX77686_BUCK5
)
102 case REGULATOR_MODE_IDLE
: /* ON in LP Mode */
103 val
= 0x2 << MAX77686_OPMODE_SHIFT
;
105 case REGULATOR_MODE_NORMAL
: /* ON in Normal Mode */
106 val
= 0x3 << MAX77686_OPMODE_SHIFT
;
109 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
110 rdev
->desc
->name
, mode
);
114 max77686
->opmode
[rdev
->desc
->id
] = val
;
115 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
116 rdev
->desc
->enable_mask
,
120 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
121 static int max77686_ldo_set_suspend_mode(struct regulator_dev
*rdev
,
125 struct max77686_data
*max77686
= rdev_get_drvdata(rdev
);
128 case REGULATOR_MODE_STANDBY
: /* switch off */
129 val
= 0x1 << MAX77686_OPMODE_SHIFT
;
131 case REGULATOR_MODE_IDLE
: /* ON in LP Mode */
132 val
= 0x2 << MAX77686_OPMODE_SHIFT
;
134 case REGULATOR_MODE_NORMAL
: /* ON in Normal Mode */
135 val
= 0x3 << MAX77686_OPMODE_SHIFT
;
138 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
139 rdev
->desc
->name
, mode
);
143 max77686
->opmode
[rdev
->desc
->id
] = val
;
144 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
145 rdev
->desc
->enable_mask
,
149 static int max77686_enable(struct regulator_dev
*rdev
)
151 struct max77686_data
*max77686
= rdev_get_drvdata(rdev
);
153 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
154 rdev
->desc
->enable_mask
,
155 max77686
->opmode
[rdev
->desc
->id
]);
158 static int max77686_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
160 unsigned int ramp_value
= RAMP_RATE_NO_CTRL
;
162 switch (ramp_delay
) {
164 ramp_value
= RAMP_RATE_13P75MV
;
166 case 13751 ... 27500:
167 ramp_value
= RAMP_RATE_27P5MV
;
169 case 27501 ... 55000:
170 ramp_value
= RAMP_RATE_55MV
;
172 case 55001 ... 100000:
175 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
176 rdev
->desc
->name
, ramp_delay
);
179 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
180 MAX77686_RAMP_RATE_MASK
, ramp_value
<< 6);
183 static struct regulator_ops max77686_ops
= {
184 .list_voltage
= regulator_list_voltage_linear
,
185 .map_voltage
= regulator_map_voltage_linear
,
186 .is_enabled
= regulator_is_enabled_regmap
,
187 .enable
= max77686_enable
,
188 .disable
= regulator_disable_regmap
,
189 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
190 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
191 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
192 .set_suspend_mode
= max77686_set_suspend_mode
,
195 static struct regulator_ops max77686_ldo_ops
= {
196 .list_voltage
= regulator_list_voltage_linear
,
197 .map_voltage
= regulator_map_voltage_linear
,
198 .is_enabled
= regulator_is_enabled_regmap
,
199 .enable
= max77686_enable
,
200 .disable
= regulator_disable_regmap
,
201 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
202 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
203 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
204 .set_suspend_mode
= max77686_ldo_set_suspend_mode
,
207 static struct regulator_ops max77686_buck1_ops
= {
208 .list_voltage
= regulator_list_voltage_linear
,
209 .map_voltage
= regulator_map_voltage_linear
,
210 .is_enabled
= regulator_is_enabled_regmap
,
211 .enable
= max77686_enable
,
212 .disable
= regulator_disable_regmap
,
213 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
214 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
215 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
216 .set_suspend_disable
= max77686_buck_set_suspend_disable
,
219 static struct regulator_ops max77686_buck_dvs_ops
= {
220 .list_voltage
= regulator_list_voltage_linear
,
221 .map_voltage
= regulator_map_voltage_linear
,
222 .is_enabled
= regulator_is_enabled_regmap
,
223 .enable
= max77686_enable
,
224 .disable
= regulator_disable_regmap
,
225 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
226 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
227 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
228 .set_ramp_delay
= max77686_set_ramp_delay
,
229 .set_suspend_disable
= max77686_buck_set_suspend_disable
,
232 #define regulator_desc_ldo(num) { \
234 .id = MAX77686_LDO##num, \
235 .ops = &max77686_ops, \
236 .type = REGULATOR_VOLTAGE, \
237 .owner = THIS_MODULE, \
238 .min_uV = MAX77686_LDO_MINUV, \
239 .uV_step = MAX77686_LDO_UVSTEP, \
240 .ramp_delay = MAX77686_RAMP_DELAY, \
241 .n_voltages = MAX77686_VSEL_MASK + 1, \
242 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
243 .vsel_mask = MAX77686_VSEL_MASK, \
244 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
245 .enable_mask = MAX77686_OPMODE_MASK \
246 << MAX77686_OPMODE_SHIFT, \
248 #define regulator_desc_lpm_ldo(num) { \
250 .id = MAX77686_LDO##num, \
251 .ops = &max77686_ldo_ops, \
252 .type = REGULATOR_VOLTAGE, \
253 .owner = THIS_MODULE, \
254 .min_uV = MAX77686_LDO_MINUV, \
255 .uV_step = MAX77686_LDO_UVSTEP, \
256 .ramp_delay = MAX77686_RAMP_DELAY, \
257 .n_voltages = MAX77686_VSEL_MASK + 1, \
258 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
259 .vsel_mask = MAX77686_VSEL_MASK, \
260 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
261 .enable_mask = MAX77686_OPMODE_MASK \
262 << MAX77686_OPMODE_SHIFT, \
264 #define regulator_desc_ldo_low(num) { \
266 .id = MAX77686_LDO##num, \
267 .ops = &max77686_ldo_ops, \
268 .type = REGULATOR_VOLTAGE, \
269 .owner = THIS_MODULE, \
270 .min_uV = MAX77686_LDO_LOW_MINUV, \
271 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
272 .ramp_delay = MAX77686_RAMP_DELAY, \
273 .n_voltages = MAX77686_VSEL_MASK + 1, \
274 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
275 .vsel_mask = MAX77686_VSEL_MASK, \
276 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
277 .enable_mask = MAX77686_OPMODE_MASK \
278 << MAX77686_OPMODE_SHIFT, \
280 #define regulator_desc_ldo1_low(num) { \
282 .id = MAX77686_LDO##num, \
283 .ops = &max77686_ops, \
284 .type = REGULATOR_VOLTAGE, \
285 .owner = THIS_MODULE, \
286 .min_uV = MAX77686_LDO_LOW_MINUV, \
287 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
288 .ramp_delay = MAX77686_RAMP_DELAY, \
289 .n_voltages = MAX77686_VSEL_MASK + 1, \
290 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
291 .vsel_mask = MAX77686_VSEL_MASK, \
292 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
293 .enable_mask = MAX77686_OPMODE_MASK \
294 << MAX77686_OPMODE_SHIFT, \
296 #define regulator_desc_buck(num) { \
297 .name = "BUCK"#num, \
298 .id = MAX77686_BUCK##num, \
299 .ops = &max77686_ops, \
300 .type = REGULATOR_VOLTAGE, \
301 .owner = THIS_MODULE, \
302 .min_uV = MAX77686_BUCK_MINUV, \
303 .uV_step = MAX77686_BUCK_UVSTEP, \
304 .ramp_delay = MAX77686_RAMP_DELAY, \
305 .n_voltages = MAX77686_VSEL_MASK + 1, \
306 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
307 .vsel_mask = MAX77686_VSEL_MASK, \
308 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
309 .enable_mask = MAX77686_OPMODE_MASK, \
311 #define regulator_desc_buck1(num) { \
312 .name = "BUCK"#num, \
313 .id = MAX77686_BUCK##num, \
314 .ops = &max77686_buck1_ops, \
315 .type = REGULATOR_VOLTAGE, \
316 .owner = THIS_MODULE, \
317 .min_uV = MAX77686_BUCK_MINUV, \
318 .uV_step = MAX77686_BUCK_UVSTEP, \
319 .ramp_delay = MAX77686_RAMP_DELAY, \
320 .n_voltages = MAX77686_VSEL_MASK + 1, \
321 .vsel_reg = MAX77686_REG_BUCK1OUT, \
322 .vsel_mask = MAX77686_VSEL_MASK, \
323 .enable_reg = MAX77686_REG_BUCK1CTRL, \
324 .enable_mask = MAX77686_OPMODE_MASK, \
326 #define regulator_desc_buck_dvs(num) { \
327 .name = "BUCK"#num, \
328 .id = MAX77686_BUCK##num, \
329 .ops = &max77686_buck_dvs_ops, \
330 .type = REGULATOR_VOLTAGE, \
331 .owner = THIS_MODULE, \
332 .min_uV = MAX77686_DVS_MINUV, \
333 .uV_step = MAX77686_DVS_UVSTEP, \
334 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
335 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
336 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
337 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
338 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
339 .enable_mask = MAX77686_OPMODE_MASK \
340 << MAX77686_OPMODE_BUCK234_SHIFT, \
343 static struct regulator_desc regulators
[] = {
344 regulator_desc_ldo1_low(1),
345 regulator_desc_ldo_low(2),
346 regulator_desc_ldo(3),
347 regulator_desc_ldo(4),
348 regulator_desc_ldo(5),
349 regulator_desc_ldo_low(6),
350 regulator_desc_ldo_low(7),
351 regulator_desc_ldo_low(8),
352 regulator_desc_ldo(9),
353 regulator_desc_lpm_ldo(10),
354 regulator_desc_lpm_ldo(11),
355 regulator_desc_lpm_ldo(12),
356 regulator_desc_ldo(13),
357 regulator_desc_lpm_ldo(14),
358 regulator_desc_ldo_low(15),
359 regulator_desc_lpm_ldo(16),
360 regulator_desc_ldo(17),
361 regulator_desc_ldo(18),
362 regulator_desc_ldo(19),
363 regulator_desc_ldo(20),
364 regulator_desc_ldo(21),
365 regulator_desc_ldo(22),
366 regulator_desc_ldo(23),
367 regulator_desc_ldo(24),
368 regulator_desc_ldo(25),
369 regulator_desc_ldo(26),
370 regulator_desc_buck1(1),
371 regulator_desc_buck_dvs(2),
372 regulator_desc_buck_dvs(3),
373 regulator_desc_buck_dvs(4),
374 regulator_desc_buck(5),
375 regulator_desc_buck(6),
376 regulator_desc_buck(7),
377 regulator_desc_buck(8),
378 regulator_desc_buck(9),
382 static int max77686_pmic_dt_parse_pdata(struct platform_device
*pdev
,
383 struct max77686_platform_data
*pdata
)
385 struct max77686_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
386 struct device_node
*pmic_np
, *regulators_np
;
387 struct max77686_regulator_data
*rdata
;
388 struct of_regulator_match rmatch
;
391 pmic_np
= iodev
->dev
->of_node
;
392 regulators_np
= of_find_node_by_name(pmic_np
, "voltage-regulators");
393 if (!regulators_np
) {
394 dev_err(&pdev
->dev
, "could not find regulators sub-node\n");
398 pdata
->num_regulators
= ARRAY_SIZE(regulators
);
399 rdata
= devm_kzalloc(&pdev
->dev
, sizeof(*rdata
) *
400 pdata
->num_regulators
, GFP_KERNEL
);
403 "could not allocate memory for regulator data\n");
407 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
408 rmatch
.name
= regulators
[i
].name
;
409 rmatch
.init_data
= NULL
;
410 rmatch
.of_node
= NULL
;
411 of_regulator_match(&pdev
->dev
, regulators_np
, &rmatch
, 1);
412 rdata
[i
].initdata
= rmatch
.init_data
;
413 rdata
[i
].of_node
= rmatch
.of_node
;
416 pdata
->regulators
= rdata
;
421 static int max77686_pmic_dt_parse_pdata(struct platform_device
*pdev
,
422 struct max77686_platform_data
*pdata
)
426 #endif /* CONFIG_OF */
428 static int max77686_pmic_probe(struct platform_device
*pdev
)
430 struct max77686_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
431 struct max77686_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
432 struct max77686_data
*max77686
;
434 struct regulator_config config
= { };
436 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
439 dev_err(&pdev
->dev
, "no platform data found for regulator\n");
443 if (iodev
->dev
->of_node
) {
444 ret
= max77686_pmic_dt_parse_pdata(pdev
, pdata
);
449 if (pdata
->num_regulators
!= MAX77686_REGULATORS
) {
451 "Invalid initial data for regulator's initialiation\n");
455 max77686
= devm_kzalloc(&pdev
->dev
, sizeof(struct max77686_data
),
460 config
.dev
= &pdev
->dev
;
461 config
.regmap
= iodev
->regmap
;
462 config
.driver_data
= max77686
;
463 platform_set_drvdata(pdev
, max77686
);
465 for (i
= 0; i
< MAX77686_REGULATORS
; i
++) {
466 config
.init_data
= pdata
->regulators
[i
].initdata
;
467 config
.of_node
= pdata
->regulators
[i
].of_node
;
469 max77686
->opmode
[i
] = regulators
[i
].enable_mask
;
470 max77686
->rdev
[i
] = regulator_register(®ulators
[i
], &config
);
471 if (IS_ERR(max77686
->rdev
[i
])) {
472 ret
= PTR_ERR(max77686
->rdev
[i
]);
474 "regulator init failed for %d\n", i
);
475 max77686
->rdev
[i
] = NULL
;
483 regulator_unregister(max77686
->rdev
[i
]);
487 static int max77686_pmic_remove(struct platform_device
*pdev
)
489 struct max77686_data
*max77686
= platform_get_drvdata(pdev
);
492 for (i
= 0; i
< MAX77686_REGULATORS
; i
++)
493 regulator_unregister(max77686
->rdev
[i
]);
498 static const struct platform_device_id max77686_pmic_id
[] = {
499 {"max77686-pmic", 0},
502 MODULE_DEVICE_TABLE(platform
, max77686_pmic_id
);
504 static struct platform_driver max77686_pmic_driver
= {
506 .name
= "max77686-pmic",
507 .owner
= THIS_MODULE
,
509 .probe
= max77686_pmic_probe
,
510 .remove
= max77686_pmic_remove
,
511 .id_table
= max77686_pmic_id
,
514 static int __init
max77686_pmic_init(void)
516 return platform_driver_register(&max77686_pmic_driver
);
518 subsys_initcall(max77686_pmic_init
);
520 static void __exit
max77686_pmic_cleanup(void)
522 platform_driver_unregister(&max77686_pmic_driver
);
524 module_exit(max77686_pmic_cleanup
);
526 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
527 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
528 MODULE_LICENSE("GPL");