rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / regulator / max77686.c
blob20935b1a6ed4fd85a4061f0d80fe94dfc529b9f6
1 /*
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 {
62 RAMP_RATE_13P75MV,
63 RAMP_RATE_27P5MV,
64 RAMP_RATE_55MV,
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)
76 unsigned int val;
77 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
78 int ret, id = rdev_get_id(rdev);
80 if (id == MAX77686_BUCK1)
81 val = 0x1;
82 else
83 val = 0x1 << MAX77686_OPMODE_BUCK234_SHIFT;
85 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
86 rdev->desc->enable_mask, val);
87 if (ret)
88 return ret;
90 max77686->opmode[id] = val;
91 return 0;
94 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
95 static int max77686_set_suspend_mode(struct regulator_dev *rdev,
96 unsigned int mode)
98 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
99 unsigned int val;
100 int ret, id = rdev_get_id(rdev);
102 /* BUCK[5-9] doesn't support this feature */
103 if (id >= MAX77686_BUCK5)
104 return 0;
106 switch (mode) {
107 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
108 val = 0x2 << MAX77686_OPMODE_SHIFT;
109 break;
110 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
111 val = 0x3 << MAX77686_OPMODE_SHIFT;
112 break;
113 default:
114 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
115 rdev->desc->name, mode);
116 return -EINVAL;
119 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
120 rdev->desc->enable_mask, val);
121 if (ret)
122 return ret;
124 max77686->opmode[id] = val;
125 return 0;
128 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
129 static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
130 unsigned int mode)
132 unsigned int val;
133 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
134 int ret;
136 switch (mode) {
137 case REGULATOR_MODE_STANDBY: /* switch off */
138 val = 0x1 << MAX77686_OPMODE_SHIFT;
139 break;
140 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
141 val = 0x2 << MAX77686_OPMODE_SHIFT;
142 break;
143 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
144 val = 0x3 << MAX77686_OPMODE_SHIFT;
145 break;
146 default:
147 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
148 rdev->desc->name, mode);
149 return -EINVAL;
152 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
153 rdev->desc->enable_mask, val);
154 if (ret)
155 return ret;
157 max77686->opmode[rdev_get_id(rdev)] = val;
158 return 0;
161 static int max77686_enable(struct regulator_dev *rdev)
163 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
165 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
166 rdev->desc->enable_mask,
167 max77686->opmode[rdev_get_id(rdev)]);
170 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
172 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
174 switch (ramp_delay) {
175 case 1 ... 13750:
176 ramp_value = RAMP_RATE_13P75MV;
177 break;
178 case 13751 ... 27500:
179 ramp_value = RAMP_RATE_27P5MV;
180 break;
181 case 27501 ... 55000:
182 ramp_value = RAMP_RATE_55MV;
183 break;
184 case 55001 ... 100000:
185 break;
186 default:
187 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
188 rdev->desc->name, ramp_delay);
191 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
192 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
195 static struct regulator_ops max77686_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_set_suspend_mode,
207 static struct regulator_ops max77686_ldo_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_mode = max77686_ldo_set_suspend_mode,
219 static struct regulator_ops max77686_buck1_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_suspend_disable = max77686_buck_set_suspend_disable,
231 static struct regulator_ops max77686_buck_dvs_ops = {
232 .list_voltage = regulator_list_voltage_linear,
233 .map_voltage = regulator_map_voltage_linear,
234 .is_enabled = regulator_is_enabled_regmap,
235 .enable = max77686_enable,
236 .disable = regulator_disable_regmap,
237 .get_voltage_sel = regulator_get_voltage_sel_regmap,
238 .set_voltage_sel = regulator_set_voltage_sel_regmap,
239 .set_voltage_time_sel = regulator_set_voltage_time_sel,
240 .set_ramp_delay = max77686_set_ramp_delay,
241 .set_suspend_disable = max77686_buck_set_suspend_disable,
244 #define regulator_desc_ldo(num) { \
245 .name = "LDO"#num, \
246 .id = MAX77686_LDO##num, \
247 .ops = &max77686_ops, \
248 .type = REGULATOR_VOLTAGE, \
249 .owner = THIS_MODULE, \
250 .min_uV = MAX77686_LDO_MINUV, \
251 .uV_step = MAX77686_LDO_UVSTEP, \
252 .ramp_delay = MAX77686_RAMP_DELAY, \
253 .n_voltages = MAX77686_VSEL_MASK + 1, \
254 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
255 .vsel_mask = MAX77686_VSEL_MASK, \
256 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
257 .enable_mask = MAX77686_OPMODE_MASK \
258 << MAX77686_OPMODE_SHIFT, \
260 #define regulator_desc_lpm_ldo(num) { \
261 .name = "LDO"#num, \
262 .id = MAX77686_LDO##num, \
263 .ops = &max77686_ldo_ops, \
264 .type = REGULATOR_VOLTAGE, \
265 .owner = THIS_MODULE, \
266 .min_uV = MAX77686_LDO_MINUV, \
267 .uV_step = MAX77686_LDO_UVSTEP, \
268 .ramp_delay = MAX77686_RAMP_DELAY, \
269 .n_voltages = MAX77686_VSEL_MASK + 1, \
270 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
271 .vsel_mask = MAX77686_VSEL_MASK, \
272 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
273 .enable_mask = MAX77686_OPMODE_MASK \
274 << MAX77686_OPMODE_SHIFT, \
276 #define regulator_desc_ldo_low(num) { \
277 .name = "LDO"#num, \
278 .id = MAX77686_LDO##num, \
279 .ops = &max77686_ldo_ops, \
280 .type = REGULATOR_VOLTAGE, \
281 .owner = THIS_MODULE, \
282 .min_uV = MAX77686_LDO_LOW_MINUV, \
283 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
284 .ramp_delay = MAX77686_RAMP_DELAY, \
285 .n_voltages = MAX77686_VSEL_MASK + 1, \
286 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
287 .vsel_mask = MAX77686_VSEL_MASK, \
288 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
289 .enable_mask = MAX77686_OPMODE_MASK \
290 << MAX77686_OPMODE_SHIFT, \
292 #define regulator_desc_ldo1_low(num) { \
293 .name = "LDO"#num, \
294 .id = MAX77686_LDO##num, \
295 .ops = &max77686_ops, \
296 .type = REGULATOR_VOLTAGE, \
297 .owner = THIS_MODULE, \
298 .min_uV = MAX77686_LDO_LOW_MINUV, \
299 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
300 .ramp_delay = MAX77686_RAMP_DELAY, \
301 .n_voltages = MAX77686_VSEL_MASK + 1, \
302 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
303 .vsel_mask = MAX77686_VSEL_MASK, \
304 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
305 .enable_mask = MAX77686_OPMODE_MASK \
306 << MAX77686_OPMODE_SHIFT, \
308 #define regulator_desc_buck(num) { \
309 .name = "BUCK"#num, \
310 .id = MAX77686_BUCK##num, \
311 .ops = &max77686_ops, \
312 .type = REGULATOR_VOLTAGE, \
313 .owner = THIS_MODULE, \
314 .min_uV = MAX77686_BUCK_MINUV, \
315 .uV_step = MAX77686_BUCK_UVSTEP, \
316 .ramp_delay = MAX77686_RAMP_DELAY, \
317 .n_voltages = MAX77686_VSEL_MASK + 1, \
318 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
319 .vsel_mask = MAX77686_VSEL_MASK, \
320 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
321 .enable_mask = MAX77686_OPMODE_MASK, \
323 #define regulator_desc_buck1(num) { \
324 .name = "BUCK"#num, \
325 .id = MAX77686_BUCK##num, \
326 .ops = &max77686_buck1_ops, \
327 .type = REGULATOR_VOLTAGE, \
328 .owner = THIS_MODULE, \
329 .min_uV = MAX77686_BUCK_MINUV, \
330 .uV_step = MAX77686_BUCK_UVSTEP, \
331 .ramp_delay = MAX77686_RAMP_DELAY, \
332 .n_voltages = MAX77686_VSEL_MASK + 1, \
333 .vsel_reg = MAX77686_REG_BUCK1OUT, \
334 .vsel_mask = MAX77686_VSEL_MASK, \
335 .enable_reg = MAX77686_REG_BUCK1CTRL, \
336 .enable_mask = MAX77686_OPMODE_MASK, \
338 #define regulator_desc_buck_dvs(num) { \
339 .name = "BUCK"#num, \
340 .id = MAX77686_BUCK##num, \
341 .ops = &max77686_buck_dvs_ops, \
342 .type = REGULATOR_VOLTAGE, \
343 .owner = THIS_MODULE, \
344 .min_uV = MAX77686_DVS_MINUV, \
345 .uV_step = MAX77686_DVS_UVSTEP, \
346 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
347 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
348 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
349 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
350 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
351 .enable_mask = MAX77686_OPMODE_MASK \
352 << MAX77686_OPMODE_BUCK234_SHIFT, \
355 static struct regulator_desc regulators[] = {
356 regulator_desc_ldo1_low(1),
357 regulator_desc_ldo_low(2),
358 regulator_desc_ldo(3),
359 regulator_desc_ldo(4),
360 regulator_desc_ldo(5),
361 regulator_desc_ldo_low(6),
362 regulator_desc_ldo_low(7),
363 regulator_desc_ldo_low(8),
364 regulator_desc_ldo(9),
365 regulator_desc_lpm_ldo(10),
366 regulator_desc_lpm_ldo(11),
367 regulator_desc_lpm_ldo(12),
368 regulator_desc_ldo(13),
369 regulator_desc_lpm_ldo(14),
370 regulator_desc_ldo_low(15),
371 regulator_desc_lpm_ldo(16),
372 regulator_desc_ldo(17),
373 regulator_desc_ldo(18),
374 regulator_desc_ldo(19),
375 regulator_desc_ldo(20),
376 regulator_desc_ldo(21),
377 regulator_desc_ldo(22),
378 regulator_desc_ldo(23),
379 regulator_desc_ldo(24),
380 regulator_desc_ldo(25),
381 regulator_desc_ldo(26),
382 regulator_desc_buck1(1),
383 regulator_desc_buck_dvs(2),
384 regulator_desc_buck_dvs(3),
385 regulator_desc_buck_dvs(4),
386 regulator_desc_buck(5),
387 regulator_desc_buck(6),
388 regulator_desc_buck(7),
389 regulator_desc_buck(8),
390 regulator_desc_buck(9),
393 #ifdef CONFIG_OF
394 static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
395 struct max77686_platform_data *pdata)
397 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
398 struct device_node *pmic_np, *regulators_np;
399 struct max77686_regulator_data *rdata;
400 struct of_regulator_match rmatch;
401 unsigned int i;
403 pmic_np = iodev->dev->of_node;
404 regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
405 if (!regulators_np) {
406 dev_err(&pdev->dev, "could not find regulators sub-node\n");
407 return -EINVAL;
410 pdata->num_regulators = ARRAY_SIZE(regulators);
411 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
412 pdata->num_regulators, GFP_KERNEL);
413 if (!rdata) {
414 dev_err(&pdev->dev,
415 "could not allocate memory for regulator data\n");
416 return -ENOMEM;
419 for (i = 0; i < pdata->num_regulators; i++) {
420 rmatch.name = regulators[i].name;
421 rmatch.init_data = NULL;
422 rmatch.of_node = NULL;
423 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
424 rdata[i].initdata = rmatch.init_data;
425 rdata[i].of_node = rmatch.of_node;
428 pdata->regulators = rdata;
430 return 0;
432 #else
433 static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
434 struct max77686_platform_data *pdata)
436 return 0;
438 #endif /* CONFIG_OF */
440 static int max77686_pmic_probe(struct platform_device *pdev)
442 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
443 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
444 struct max77686_data *max77686;
445 int i, ret = 0;
446 struct regulator_config config = { };
448 dev_dbg(&pdev->dev, "%s\n", __func__);
450 if (!pdata) {
451 dev_err(&pdev->dev, "no platform data found for regulator\n");
452 return -ENODEV;
455 if (iodev->dev->of_node) {
456 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
457 if (ret)
458 return ret;
461 if (pdata->num_regulators != MAX77686_REGULATORS) {
462 dev_err(&pdev->dev,
463 "Invalid initial data for regulator's initialiation\n");
464 return -EINVAL;
467 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
468 GFP_KERNEL);
469 if (!max77686)
470 return -ENOMEM;
472 config.dev = &pdev->dev;
473 config.regmap = iodev->regmap;
474 config.driver_data = max77686;
475 platform_set_drvdata(pdev, max77686);
477 for (i = 0; i < MAX77686_REGULATORS; i++) {
478 config.init_data = pdata->regulators[i].initdata;
479 config.of_node = pdata->regulators[i].of_node;
481 max77686->opmode[i] = regulators[i].enable_mask;
482 max77686->rdev[i] = regulator_register(&regulators[i], &config);
483 if (IS_ERR(max77686->rdev[i])) {
484 ret = PTR_ERR(max77686->rdev[i]);
485 dev_err(&pdev->dev,
486 "regulator init failed for %d\n", i);
487 max77686->rdev[i] = NULL;
488 goto err;
492 return 0;
493 err:
494 while (--i >= 0)
495 regulator_unregister(max77686->rdev[i]);
496 return ret;
499 static int max77686_pmic_remove(struct platform_device *pdev)
501 struct max77686_data *max77686 = platform_get_drvdata(pdev);
502 int i;
504 for (i = 0; i < MAX77686_REGULATORS; i++)
505 regulator_unregister(max77686->rdev[i]);
507 return 0;
510 static const struct platform_device_id max77686_pmic_id[] = {
511 {"max77686-pmic", 0},
512 { },
514 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
516 static struct platform_driver max77686_pmic_driver = {
517 .driver = {
518 .name = "max77686-pmic",
519 .owner = THIS_MODULE,
521 .probe = max77686_pmic_probe,
522 .remove = max77686_pmic_remove,
523 .id_table = max77686_pmic_id,
526 static int __init max77686_pmic_init(void)
528 return platform_driver_register(&max77686_pmic_driver);
530 subsys_initcall(max77686_pmic_init);
532 static void __exit max77686_pmic_cleanup(void)
534 platform_driver_unregister(&max77686_pmic_driver);
536 module_exit(max77686_pmic_cleanup);
538 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
539 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
540 MODULE_LICENSE("GPL");