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);
78 int id = rdev_get_id(rdev);
80 if (id == MAX77686_BUCK1)
83 val = 0x1 << MAX77686_OPMODE_BUCK234_SHIFT;
85 max77686->opmode[id] = val;
86 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
87 rdev->desc->enable_mask,
91 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
92 static int max77686_set_suspend_mode(struct regulator_dev *rdev,
95 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
97 int id = rdev_get_id(rdev);
99 /* BUCK[5-9] doesn't support this feature */
100 if (id >= MAX77686_BUCK5)
104 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
105 val = 0x2 << MAX77686_OPMODE_SHIFT;
107 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
108 val = 0x3 << MAX77686_OPMODE_SHIFT;
111 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
112 rdev->desc->name, mode);
116 max77686->opmode[id] = val;
117 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
118 rdev->desc->enable_mask,
122 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
123 static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
127 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
130 case REGULATOR_MODE_STANDBY: /* switch off */
131 val = 0x1 << MAX77686_OPMODE_SHIFT;
133 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
134 val = 0x2 << MAX77686_OPMODE_SHIFT;
136 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
137 val = 0x3 << MAX77686_OPMODE_SHIFT;
140 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
141 rdev->desc->name, mode);
145 max77686->opmode[rdev_get_id(rdev)] = val;
146 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
147 rdev->desc->enable_mask,
151 static int max77686_enable(struct regulator_dev *rdev)
153 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
155 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
156 rdev->desc->enable_mask,
157 max77686->opmode[rdev_get_id(rdev)]);
160 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
162 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
164 switch (ramp_delay) {
166 ramp_value = RAMP_RATE_13P75MV;
168 case 13751 ... 27500:
169 ramp_value = RAMP_RATE_27P5MV;
171 case 27501 ... 55000:
172 ramp_value = RAMP_RATE_55MV;
174 case 55001 ... 100000:
177 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
178 rdev->desc->name, ramp_delay);
181 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
182 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
185 static struct regulator_ops max77686_ops = {
186 .list_voltage = regulator_list_voltage_linear,
187 .map_voltage = regulator_map_voltage_linear,
188 .is_enabled = regulator_is_enabled_regmap,
189 .enable = max77686_enable,
190 .disable = regulator_disable_regmap,
191 .get_voltage_sel = regulator_get_voltage_sel_regmap,
192 .set_voltage_sel = regulator_set_voltage_sel_regmap,
193 .set_voltage_time_sel = regulator_set_voltage_time_sel,
194 .set_suspend_mode = max77686_set_suspend_mode,
197 static struct regulator_ops max77686_ldo_ops = {
198 .list_voltage = regulator_list_voltage_linear,
199 .map_voltage = regulator_map_voltage_linear,
200 .is_enabled = regulator_is_enabled_regmap,
201 .enable = max77686_enable,
202 .disable = regulator_disable_regmap,
203 .get_voltage_sel = regulator_get_voltage_sel_regmap,
204 .set_voltage_sel = regulator_set_voltage_sel_regmap,
205 .set_voltage_time_sel = regulator_set_voltage_time_sel,
206 .set_suspend_mode = max77686_ldo_set_suspend_mode,
209 static struct regulator_ops max77686_buck1_ops = {
210 .list_voltage = regulator_list_voltage_linear,
211 .map_voltage = regulator_map_voltage_linear,
212 .is_enabled = regulator_is_enabled_regmap,
213 .enable = max77686_enable,
214 .disable = regulator_disable_regmap,
215 .get_voltage_sel = regulator_get_voltage_sel_regmap,
216 .set_voltage_sel = regulator_set_voltage_sel_regmap,
217 .set_voltage_time_sel = regulator_set_voltage_time_sel,
218 .set_suspend_disable = max77686_buck_set_suspend_disable,
221 static struct regulator_ops max77686_buck_dvs_ops = {
222 .list_voltage = regulator_list_voltage_linear,
223 .map_voltage = regulator_map_voltage_linear,
224 .is_enabled = regulator_is_enabled_regmap,
225 .enable = max77686_enable,
226 .disable = regulator_disable_regmap,
227 .get_voltage_sel = regulator_get_voltage_sel_regmap,
228 .set_voltage_sel = regulator_set_voltage_sel_regmap,
229 .set_voltage_time_sel = regulator_set_voltage_time_sel,
230 .set_ramp_delay = max77686_set_ramp_delay,
231 .set_suspend_disable = max77686_buck_set_suspend_disable,
234 #define regulator_desc_ldo(num) { \
236 .id = MAX77686_LDO##num, \
237 .ops = &max77686_ops, \
238 .type = REGULATOR_VOLTAGE, \
239 .owner = THIS_MODULE, \
240 .min_uV = MAX77686_LDO_MINUV, \
241 .uV_step = MAX77686_LDO_UVSTEP, \
242 .ramp_delay = MAX77686_RAMP_DELAY, \
243 .n_voltages = MAX77686_VSEL_MASK + 1, \
244 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
245 .vsel_mask = MAX77686_VSEL_MASK, \
246 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
247 .enable_mask = MAX77686_OPMODE_MASK \
248 << MAX77686_OPMODE_SHIFT, \
250 #define regulator_desc_lpm_ldo(num) { \
252 .id = MAX77686_LDO##num, \
253 .ops = &max77686_ldo_ops, \
254 .type = REGULATOR_VOLTAGE, \
255 .owner = THIS_MODULE, \
256 .min_uV = MAX77686_LDO_MINUV, \
257 .uV_step = MAX77686_LDO_UVSTEP, \
258 .ramp_delay = MAX77686_RAMP_DELAY, \
259 .n_voltages = MAX77686_VSEL_MASK + 1, \
260 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
261 .vsel_mask = MAX77686_VSEL_MASK, \
262 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
263 .enable_mask = MAX77686_OPMODE_MASK \
264 << MAX77686_OPMODE_SHIFT, \
266 #define regulator_desc_ldo_low(num) { \
268 .id = MAX77686_LDO##num, \
269 .ops = &max77686_ldo_ops, \
270 .type = REGULATOR_VOLTAGE, \
271 .owner = THIS_MODULE, \
272 .min_uV = MAX77686_LDO_LOW_MINUV, \
273 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
274 .ramp_delay = MAX77686_RAMP_DELAY, \
275 .n_voltages = MAX77686_VSEL_MASK + 1, \
276 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
277 .vsel_mask = MAX77686_VSEL_MASK, \
278 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
279 .enable_mask = MAX77686_OPMODE_MASK \
280 << MAX77686_OPMODE_SHIFT, \
282 #define regulator_desc_ldo1_low(num) { \
284 .id = MAX77686_LDO##num, \
285 .ops = &max77686_ops, \
286 .type = REGULATOR_VOLTAGE, \
287 .owner = THIS_MODULE, \
288 .min_uV = MAX77686_LDO_LOW_MINUV, \
289 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
290 .ramp_delay = MAX77686_RAMP_DELAY, \
291 .n_voltages = MAX77686_VSEL_MASK + 1, \
292 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
293 .vsel_mask = MAX77686_VSEL_MASK, \
294 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
295 .enable_mask = MAX77686_OPMODE_MASK \
296 << MAX77686_OPMODE_SHIFT, \
298 #define regulator_desc_buck(num) { \
299 .name = "BUCK"#num, \
300 .id = MAX77686_BUCK##num, \
301 .ops = &max77686_ops, \
302 .type = REGULATOR_VOLTAGE, \
303 .owner = THIS_MODULE, \
304 .min_uV = MAX77686_BUCK_MINUV, \
305 .uV_step = MAX77686_BUCK_UVSTEP, \
306 .ramp_delay = MAX77686_RAMP_DELAY, \
307 .n_voltages = MAX77686_VSEL_MASK + 1, \
308 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
309 .vsel_mask = MAX77686_VSEL_MASK, \
310 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
311 .enable_mask = MAX77686_OPMODE_MASK, \
313 #define regulator_desc_buck1(num) { \
314 .name = "BUCK"#num, \
315 .id = MAX77686_BUCK##num, \
316 .ops = &max77686_buck1_ops, \
317 .type = REGULATOR_VOLTAGE, \
318 .owner = THIS_MODULE, \
319 .min_uV = MAX77686_BUCK_MINUV, \
320 .uV_step = MAX77686_BUCK_UVSTEP, \
321 .ramp_delay = MAX77686_RAMP_DELAY, \
322 .n_voltages = MAX77686_VSEL_MASK + 1, \
323 .vsel_reg = MAX77686_REG_BUCK1OUT, \
324 .vsel_mask = MAX77686_VSEL_MASK, \
325 .enable_reg = MAX77686_REG_BUCK1CTRL, \
326 .enable_mask = MAX77686_OPMODE_MASK, \
328 #define regulator_desc_buck_dvs(num) { \
329 .name = "BUCK"#num, \
330 .id = MAX77686_BUCK##num, \
331 .ops = &max77686_buck_dvs_ops, \
332 .type = REGULATOR_VOLTAGE, \
333 .owner = THIS_MODULE, \
334 .min_uV = MAX77686_DVS_MINUV, \
335 .uV_step = MAX77686_DVS_UVSTEP, \
336 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
337 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
338 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
339 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
340 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
341 .enable_mask = MAX77686_OPMODE_MASK \
342 << MAX77686_OPMODE_BUCK234_SHIFT, \
345 static struct regulator_desc regulators[] = {
346 regulator_desc_ldo1_low(1),
347 regulator_desc_ldo_low(2),
348 regulator_desc_ldo(3),
349 regulator_desc_ldo(4),
350 regulator_desc_ldo(5),
351 regulator_desc_ldo_low(6),
352 regulator_desc_ldo_low(7),
353 regulator_desc_ldo_low(8),
354 regulator_desc_ldo(9),
355 regulator_desc_lpm_ldo(10),
356 regulator_desc_lpm_ldo(11),
357 regulator_desc_lpm_ldo(12),
358 regulator_desc_ldo(13),
359 regulator_desc_lpm_ldo(14),
360 regulator_desc_ldo_low(15),
361 regulator_desc_lpm_ldo(16),
362 regulator_desc_ldo(17),
363 regulator_desc_ldo(18),
364 regulator_desc_ldo(19),
365 regulator_desc_ldo(20),
366 regulator_desc_ldo(21),
367 regulator_desc_ldo(22),
368 regulator_desc_ldo(23),
369 regulator_desc_ldo(24),
370 regulator_desc_ldo(25),
371 regulator_desc_ldo(26),
372 regulator_desc_buck1(1),
373 regulator_desc_buck_dvs(2),
374 regulator_desc_buck_dvs(3),
375 regulator_desc_buck_dvs(4),
376 regulator_desc_buck(5),
377 regulator_desc_buck(6),
378 regulator_desc_buck(7),
379 regulator_desc_buck(8),
380 regulator_desc_buck(9),
384 static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
385 struct max77686_platform_data *pdata)
387 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
388 struct device_node *pmic_np, *regulators_np;
389 struct max77686_regulator_data *rdata;
390 struct of_regulator_match rmatch;
393 pmic_np = iodev->dev->of_node;
394 regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
395 if (!regulators_np) {
396 dev_err(&pdev->dev, "could not find regulators sub-node\n");
400 pdata->num_regulators = ARRAY_SIZE(regulators);
401 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
402 pdata->num_regulators, GFP_KERNEL);
405 "could not allocate memory for regulator data\n");
409 for (i = 0; i < pdata->num_regulators; i++) {
410 rmatch.name = regulators[i].name;
411 rmatch.init_data = NULL;
412 rmatch.of_node = NULL;
413 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
414 rdata[i].initdata = rmatch.init_data;
415 rdata[i].of_node = rmatch.of_node;
418 pdata->regulators = rdata;
423 static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
424 struct max77686_platform_data *pdata)
428 #endif /* CONFIG_OF */
430 static int max77686_pmic_probe(struct platform_device *pdev)
432 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
433 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
434 struct max77686_data *max77686;
436 struct regulator_config config = { };
438 dev_dbg(&pdev->dev, "%s\n", __func__);
441 dev_err(&pdev->dev, "no platform data found for regulator\n");
445 if (iodev->dev->of_node) {
446 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
451 if (pdata->num_regulators != MAX77686_REGULATORS) {
453 "Invalid initial data for regulator's initialiation\n");
457 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
462 config.dev = &pdev->dev;
463 config.regmap = iodev->regmap;
464 config.driver_data = max77686;
465 platform_set_drvdata(pdev, max77686);
467 for (i = 0; i < MAX77686_REGULATORS; i++) {
468 config.init_data = pdata->regulators[i].initdata;
469 config.of_node = pdata->regulators[i].of_node;
471 max77686->opmode[i] = regulators[i].enable_mask;
472 max77686->rdev[i] = regulator_register(®ulators[i], &config);
473 if (IS_ERR(max77686->rdev[i])) {
474 ret = PTR_ERR(max77686->rdev[i]);
476 "regulator init failed for %d\n", i);
477 max77686->rdev[i] = NULL;
485 regulator_unregister(max77686->rdev[i]);
489 static int max77686_pmic_remove(struct platform_device *pdev)
491 struct max77686_data *max77686 = platform_get_drvdata(pdev);
494 for (i = 0; i < MAX77686_REGULATORS; i++)
495 regulator_unregister(max77686->rdev[i]);
500 static const struct platform_device_id max77686_pmic_id[] = {
501 {"max77686-pmic", 0},
504 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
506 static struct platform_driver max77686_pmic_driver = {
508 .name = "max77686-pmic",
509 .owner = THIS_MODULE,
511 .probe = max77686_pmic_probe,
512 .remove = max77686_pmic_remove,
513 .id_table = max77686_pmic_id,
516 static int __init max77686_pmic_init(void)
518 return platform_driver_register(&max77686_pmic_driver);
520 subsys_initcall(max77686_pmic_init);
522 static void __exit max77686_pmic_cleanup(void)
524 platform_driver_unregister(&max77686_pmic_driver);
526 module_exit(max77686_pmic_cleanup);
528 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
529 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
530 MODULE_LICENSE("GPL");