Linux 4.16.11
[linux/fpc-iii.git] / drivers / regulator / max77686-regulator.c
blobc301f37334758599fc98ba615a9159a62c9ba708
1 /*
2 * max77686.c - Regulator driver for the Maxim 77686
4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@samsung.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/err.h>
28 #include <linux/gpio.h>
29 #include <linux/of_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_BUCK_ENABLE_TIME 40 /* us */
45 #define MAX77686_DVS_ENABLE_TIME 22 /* us */
46 #define MAX77686_RAMP_DELAY 100000 /* uV/us */
47 #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
48 #define MAX77686_DVS_MINUV 600000
49 #define MAX77686_DVS_UVSTEP 12500
52 * Value for configuring buck[89] and LDO{20,21,22} as GPIO control.
53 * It is the same as 'off' for other regulators.
55 #define MAX77686_GPIO_CONTROL 0x0
57 * Values used for configuring LDOs and bucks.
58 * Forcing low power mode: LDO1, 3-5, 9, 13, 17-26
60 #define MAX77686_LDO_LOWPOWER 0x1
62 * On/off controlled by PWRREQ:
63 * - LDO2, 6-8, 10-12, 14-16
64 * - buck[1234]
66 #define MAX77686_OFF_PWRREQ 0x1
67 /* Low power mode controlled by PWRREQ: All LDOs */
68 #define MAX77686_LDO_LOWPOWER_PWRREQ 0x2
69 /* Forcing low power mode: buck[234] */
70 #define MAX77686_BUCK_LOWPOWER 0x2
71 #define MAX77686_NORMAL 0x3
73 #define MAX77686_OPMODE_SHIFT 6
74 #define MAX77686_OPMODE_BUCK234_SHIFT 4
75 #define MAX77686_OPMODE_MASK 0x3
77 #define MAX77686_VSEL_MASK 0x3F
78 #define MAX77686_DVS_VSEL_MASK 0xFF
80 #define MAX77686_RAMP_RATE_MASK 0xC0
82 #define MAX77686_REGULATORS MAX77686_REG_MAX
83 #define MAX77686_LDOS 26
85 enum max77686_ramp_rate {
86 RAMP_RATE_13P75MV,
87 RAMP_RATE_27P5MV,
88 RAMP_RATE_55MV,
89 RAMP_RATE_NO_CTRL, /* 100mV/us */
92 struct max77686_data {
93 DECLARE_BITMAP(gpio_enabled, MAX77686_REGULATORS);
95 /* Array indexed by regulator id */
96 unsigned int opmode[MAX77686_REGULATORS];
99 static unsigned int max77686_get_opmode_shift(int id)
101 switch (id) {
102 case MAX77686_BUCK1:
103 case MAX77686_BUCK5 ... MAX77686_BUCK9:
104 return 0;
105 case MAX77686_BUCK2 ... MAX77686_BUCK4:
106 return MAX77686_OPMODE_BUCK234_SHIFT;
107 default:
108 /* all LDOs */
109 return MAX77686_OPMODE_SHIFT;
114 * When regulator is configured for GPIO control then it
115 * replaces "normal" mode. Any change from low power mode to normal
116 * should actually change to GPIO control.
117 * Map normal mode to proper value for such regulators.
119 static unsigned int max77686_map_normal_mode(struct max77686_data *max77686,
120 int id)
122 switch (id) {
123 case MAX77686_BUCK8:
124 case MAX77686_BUCK9:
125 case MAX77686_LDO20 ... MAX77686_LDO22:
126 if (test_bit(id, max77686->gpio_enabled))
127 return MAX77686_GPIO_CONTROL;
130 return MAX77686_NORMAL;
133 /* Some BUCKs and LDOs supports Normal[ON/OFF] mode during suspend */
134 static int max77686_set_suspend_disable(struct regulator_dev *rdev)
136 unsigned int val, shift;
137 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
138 int ret, id = rdev_get_id(rdev);
140 shift = max77686_get_opmode_shift(id);
141 val = MAX77686_OFF_PWRREQ;
143 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
144 rdev->desc->enable_mask, val << shift);
145 if (ret)
146 return ret;
148 max77686->opmode[id] = val;
149 return 0;
152 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
153 static int max77686_set_suspend_mode(struct regulator_dev *rdev,
154 unsigned int mode)
156 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
157 unsigned int val;
158 int ret, id = rdev_get_id(rdev);
160 /* BUCK[5-9] doesn't support this feature */
161 if (id >= MAX77686_BUCK5)
162 return 0;
164 switch (mode) {
165 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
166 val = MAX77686_LDO_LOWPOWER_PWRREQ;
167 break;
168 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
169 val = max77686_map_normal_mode(max77686, id);
170 break;
171 default:
172 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
173 rdev->desc->name, mode);
174 return -EINVAL;
177 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
178 rdev->desc->enable_mask,
179 val << MAX77686_OPMODE_SHIFT);
180 if (ret)
181 return ret;
183 max77686->opmode[id] = val;
184 return 0;
187 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
188 static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
189 unsigned int mode)
191 unsigned int val;
192 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
193 int ret, id = rdev_get_id(rdev);
195 switch (mode) {
196 case REGULATOR_MODE_STANDBY: /* switch off */
197 val = MAX77686_OFF_PWRREQ;
198 break;
199 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
200 val = MAX77686_LDO_LOWPOWER_PWRREQ;
201 break;
202 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
203 val = max77686_map_normal_mode(max77686, id);
204 break;
205 default:
206 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
207 rdev->desc->name, mode);
208 return -EINVAL;
211 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
212 rdev->desc->enable_mask,
213 val << MAX77686_OPMODE_SHIFT);
214 if (ret)
215 return ret;
217 max77686->opmode[id] = val;
218 return 0;
221 static int max77686_enable(struct regulator_dev *rdev)
223 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
224 unsigned int shift;
225 int id = rdev_get_id(rdev);
227 shift = max77686_get_opmode_shift(id);
229 if (max77686->opmode[id] == MAX77686_OFF_PWRREQ)
230 max77686->opmode[id] = max77686_map_normal_mode(max77686, id);
232 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
233 rdev->desc->enable_mask,
234 max77686->opmode[id] << shift);
237 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
239 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
241 switch (ramp_delay) {
242 case 1 ... 13750:
243 ramp_value = RAMP_RATE_13P75MV;
244 break;
245 case 13751 ... 27500:
246 ramp_value = RAMP_RATE_27P5MV;
247 break;
248 case 27501 ... 55000:
249 ramp_value = RAMP_RATE_55MV;
250 break;
251 case 55001 ... 100000:
252 break;
253 default:
254 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
255 rdev->desc->name, ramp_delay);
258 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
259 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
262 static int max77686_of_parse_cb(struct device_node *np,
263 const struct regulator_desc *desc,
264 struct regulator_config *config)
266 struct max77686_data *max77686 = config->driver_data;
268 switch (desc->id) {
269 case MAX77686_BUCK8:
270 case MAX77686_BUCK9:
271 case MAX77686_LDO20 ... MAX77686_LDO22:
272 config->ena_gpio = of_get_named_gpio(np,
273 "maxim,ena-gpios", 0);
274 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
275 config->ena_gpio_initialized = true;
276 break;
277 default:
278 return 0;
281 if (gpio_is_valid(config->ena_gpio)) {
282 set_bit(desc->id, max77686->gpio_enabled);
284 return regmap_update_bits(config->regmap, desc->enable_reg,
285 desc->enable_mask,
286 MAX77686_GPIO_CONTROL);
289 return 0;
292 static const struct regulator_ops max77686_ops = {
293 .list_voltage = regulator_list_voltage_linear,
294 .map_voltage = regulator_map_voltage_linear,
295 .is_enabled = regulator_is_enabled_regmap,
296 .enable = max77686_enable,
297 .disable = regulator_disable_regmap,
298 .get_voltage_sel = regulator_get_voltage_sel_regmap,
299 .set_voltage_sel = regulator_set_voltage_sel_regmap,
300 .set_voltage_time_sel = regulator_set_voltage_time_sel,
301 .set_suspend_mode = max77686_set_suspend_mode,
304 static const struct regulator_ops max77686_ldo_ops = {
305 .list_voltage = regulator_list_voltage_linear,
306 .map_voltage = regulator_map_voltage_linear,
307 .is_enabled = regulator_is_enabled_regmap,
308 .enable = max77686_enable,
309 .disable = regulator_disable_regmap,
310 .get_voltage_sel = regulator_get_voltage_sel_regmap,
311 .set_voltage_sel = regulator_set_voltage_sel_regmap,
312 .set_voltage_time_sel = regulator_set_voltage_time_sel,
313 .set_suspend_mode = max77686_ldo_set_suspend_mode,
314 .set_suspend_disable = max77686_set_suspend_disable,
317 static const struct regulator_ops max77686_buck1_ops = {
318 .list_voltage = regulator_list_voltage_linear,
319 .map_voltage = regulator_map_voltage_linear,
320 .is_enabled = regulator_is_enabled_regmap,
321 .enable = max77686_enable,
322 .disable = regulator_disable_regmap,
323 .get_voltage_sel = regulator_get_voltage_sel_regmap,
324 .set_voltage_sel = regulator_set_voltage_sel_regmap,
325 .set_voltage_time_sel = regulator_set_voltage_time_sel,
326 .set_suspend_disable = max77686_set_suspend_disable,
329 static const struct regulator_ops max77686_buck_dvs_ops = {
330 .list_voltage = regulator_list_voltage_linear,
331 .map_voltage = regulator_map_voltage_linear,
332 .is_enabled = regulator_is_enabled_regmap,
333 .enable = max77686_enable,
334 .disable = regulator_disable_regmap,
335 .get_voltage_sel = regulator_get_voltage_sel_regmap,
336 .set_voltage_sel = regulator_set_voltage_sel_regmap,
337 .set_voltage_time_sel = regulator_set_voltage_time_sel,
338 .set_ramp_delay = max77686_set_ramp_delay,
339 .set_suspend_disable = max77686_set_suspend_disable,
342 #define regulator_desc_ldo(num) { \
343 .name = "LDO"#num, \
344 .of_match = of_match_ptr("LDO"#num), \
345 .regulators_node = of_match_ptr("voltage-regulators"), \
346 .of_parse_cb = max77686_of_parse_cb, \
347 .id = MAX77686_LDO##num, \
348 .ops = &max77686_ops, \
349 .type = REGULATOR_VOLTAGE, \
350 .owner = THIS_MODULE, \
351 .min_uV = MAX77686_LDO_MINUV, \
352 .uV_step = MAX77686_LDO_UVSTEP, \
353 .ramp_delay = MAX77686_RAMP_DELAY, \
354 .n_voltages = MAX77686_VSEL_MASK + 1, \
355 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
356 .vsel_mask = MAX77686_VSEL_MASK, \
357 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
358 .enable_mask = MAX77686_OPMODE_MASK \
359 << MAX77686_OPMODE_SHIFT, \
361 #define regulator_desc_lpm_ldo(num) { \
362 .name = "LDO"#num, \
363 .of_match = of_match_ptr("LDO"#num), \
364 .regulators_node = of_match_ptr("voltage-regulators"), \
365 .id = MAX77686_LDO##num, \
366 .ops = &max77686_ldo_ops, \
367 .type = REGULATOR_VOLTAGE, \
368 .owner = THIS_MODULE, \
369 .min_uV = MAX77686_LDO_MINUV, \
370 .uV_step = MAX77686_LDO_UVSTEP, \
371 .ramp_delay = MAX77686_RAMP_DELAY, \
372 .n_voltages = MAX77686_VSEL_MASK + 1, \
373 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
374 .vsel_mask = MAX77686_VSEL_MASK, \
375 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
376 .enable_mask = MAX77686_OPMODE_MASK \
377 << MAX77686_OPMODE_SHIFT, \
379 #define regulator_desc_ldo_low(num) { \
380 .name = "LDO"#num, \
381 .of_match = of_match_ptr("LDO"#num), \
382 .regulators_node = of_match_ptr("voltage-regulators"), \
383 .id = MAX77686_LDO##num, \
384 .ops = &max77686_ldo_ops, \
385 .type = REGULATOR_VOLTAGE, \
386 .owner = THIS_MODULE, \
387 .min_uV = MAX77686_LDO_LOW_MINUV, \
388 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
389 .ramp_delay = MAX77686_RAMP_DELAY, \
390 .n_voltages = MAX77686_VSEL_MASK + 1, \
391 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
392 .vsel_mask = MAX77686_VSEL_MASK, \
393 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
394 .enable_mask = MAX77686_OPMODE_MASK \
395 << MAX77686_OPMODE_SHIFT, \
397 #define regulator_desc_ldo1_low(num) { \
398 .name = "LDO"#num, \
399 .of_match = of_match_ptr("LDO"#num), \
400 .regulators_node = of_match_ptr("voltage-regulators"), \
401 .id = MAX77686_LDO##num, \
402 .ops = &max77686_ops, \
403 .type = REGULATOR_VOLTAGE, \
404 .owner = THIS_MODULE, \
405 .min_uV = MAX77686_LDO_LOW_MINUV, \
406 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
407 .ramp_delay = MAX77686_RAMP_DELAY, \
408 .n_voltages = MAX77686_VSEL_MASK + 1, \
409 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
410 .vsel_mask = MAX77686_VSEL_MASK, \
411 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
412 .enable_mask = MAX77686_OPMODE_MASK \
413 << MAX77686_OPMODE_SHIFT, \
415 #define regulator_desc_buck(num) { \
416 .name = "BUCK"#num, \
417 .of_match = of_match_ptr("BUCK"#num), \
418 .regulators_node = of_match_ptr("voltage-regulators"), \
419 .of_parse_cb = max77686_of_parse_cb, \
420 .id = MAX77686_BUCK##num, \
421 .ops = &max77686_ops, \
422 .type = REGULATOR_VOLTAGE, \
423 .owner = THIS_MODULE, \
424 .min_uV = MAX77686_BUCK_MINUV, \
425 .uV_step = MAX77686_BUCK_UVSTEP, \
426 .ramp_delay = MAX77686_RAMP_DELAY, \
427 .enable_time = MAX77686_BUCK_ENABLE_TIME, \
428 .n_voltages = MAX77686_VSEL_MASK + 1, \
429 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
430 .vsel_mask = MAX77686_VSEL_MASK, \
431 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
432 .enable_mask = MAX77686_OPMODE_MASK, \
434 #define regulator_desc_buck1(num) { \
435 .name = "BUCK"#num, \
436 .of_match = of_match_ptr("BUCK"#num), \
437 .regulators_node = of_match_ptr("voltage-regulators"), \
438 .id = MAX77686_BUCK##num, \
439 .ops = &max77686_buck1_ops, \
440 .type = REGULATOR_VOLTAGE, \
441 .owner = THIS_MODULE, \
442 .min_uV = MAX77686_BUCK_MINUV, \
443 .uV_step = MAX77686_BUCK_UVSTEP, \
444 .ramp_delay = MAX77686_RAMP_DELAY, \
445 .enable_time = MAX77686_BUCK_ENABLE_TIME, \
446 .n_voltages = MAX77686_VSEL_MASK + 1, \
447 .vsel_reg = MAX77686_REG_BUCK1OUT, \
448 .vsel_mask = MAX77686_VSEL_MASK, \
449 .enable_reg = MAX77686_REG_BUCK1CTRL, \
450 .enable_mask = MAX77686_OPMODE_MASK, \
452 #define regulator_desc_buck_dvs(num) { \
453 .name = "BUCK"#num, \
454 .of_match = of_match_ptr("BUCK"#num), \
455 .regulators_node = of_match_ptr("voltage-regulators"), \
456 .id = MAX77686_BUCK##num, \
457 .ops = &max77686_buck_dvs_ops, \
458 .type = REGULATOR_VOLTAGE, \
459 .owner = THIS_MODULE, \
460 .min_uV = MAX77686_DVS_MINUV, \
461 .uV_step = MAX77686_DVS_UVSTEP, \
462 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
463 .enable_time = MAX77686_DVS_ENABLE_TIME, \
464 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
465 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
466 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
467 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
468 .enable_mask = MAX77686_OPMODE_MASK \
469 << MAX77686_OPMODE_BUCK234_SHIFT, \
472 static const struct regulator_desc regulators[] = {
473 regulator_desc_ldo1_low(1),
474 regulator_desc_ldo_low(2),
475 regulator_desc_ldo(3),
476 regulator_desc_ldo(4),
477 regulator_desc_ldo(5),
478 regulator_desc_ldo_low(6),
479 regulator_desc_ldo_low(7),
480 regulator_desc_ldo_low(8),
481 regulator_desc_ldo(9),
482 regulator_desc_lpm_ldo(10),
483 regulator_desc_lpm_ldo(11),
484 regulator_desc_lpm_ldo(12),
485 regulator_desc_ldo(13),
486 regulator_desc_lpm_ldo(14),
487 regulator_desc_ldo_low(15),
488 regulator_desc_lpm_ldo(16),
489 regulator_desc_ldo(17),
490 regulator_desc_ldo(18),
491 regulator_desc_ldo(19),
492 regulator_desc_ldo(20),
493 regulator_desc_ldo(21),
494 regulator_desc_ldo(22),
495 regulator_desc_ldo(23),
496 regulator_desc_ldo(24),
497 regulator_desc_ldo(25),
498 regulator_desc_ldo(26),
499 regulator_desc_buck1(1),
500 regulator_desc_buck_dvs(2),
501 regulator_desc_buck_dvs(3),
502 regulator_desc_buck_dvs(4),
503 regulator_desc_buck(5),
504 regulator_desc_buck(6),
505 regulator_desc_buck(7),
506 regulator_desc_buck(8),
507 regulator_desc_buck(9),
510 static int max77686_pmic_probe(struct platform_device *pdev)
512 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
513 struct max77686_data *max77686;
514 int i;
515 struct regulator_config config = { };
517 dev_dbg(&pdev->dev, "%s\n", __func__);
519 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
520 GFP_KERNEL);
521 if (!max77686)
522 return -ENOMEM;
524 config.dev = iodev->dev;
525 config.regmap = iodev->regmap;
526 config.driver_data = max77686;
527 platform_set_drvdata(pdev, max77686);
529 for (i = 0; i < MAX77686_REGULATORS; i++) {
530 struct regulator_dev *rdev;
531 int id = regulators[i].id;
533 max77686->opmode[id] = MAX77686_NORMAL;
534 rdev = devm_regulator_register(&pdev->dev,
535 &regulators[i], &config);
536 if (IS_ERR(rdev)) {
537 int ret = PTR_ERR(rdev);
538 dev_err(&pdev->dev,
539 "regulator init failed for %d: %d\n", i, ret);
540 return ret;
544 return 0;
547 static const struct platform_device_id max77686_pmic_id[] = {
548 {"max77686-pmic", 0},
549 { },
551 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
553 static struct platform_driver max77686_pmic_driver = {
554 .driver = {
555 .name = "max77686-pmic",
557 .probe = max77686_pmic_probe,
558 .id_table = max77686_pmic_id,
561 module_platform_driver(max77686_pmic_driver);
563 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
564 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
565 MODULE_LICENSE("GPL");