perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / regulator / max77686-regulator.c
blob8020eb57374a1b32d3a1c322483c5a0bb308c514
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // max77686.c - Regulator driver for the Maxim 77686
4 //
5 // Copyright (C) 2012 Samsung Electronics
6 // Chiwoong Byun <woong.byun@samsung.com>
7 // Jonghwa Lee <jonghwa3.lee@samsung.com>
8 //
9 // This driver is based on max8997.c
11 #include <linux/kernel.h>
12 #include <linux/bug.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/mfd/max77686.h>
21 #include <linux/mfd/max77686-private.h>
23 #define MAX77686_LDO_MINUV 800000
24 #define MAX77686_LDO_UVSTEP 50000
25 #define MAX77686_LDO_LOW_MINUV 800000
26 #define MAX77686_LDO_LOW_UVSTEP 25000
27 #define MAX77686_BUCK_MINUV 750000
28 #define MAX77686_BUCK_UVSTEP 50000
29 #define MAX77686_BUCK_ENABLE_TIME 40 /* us */
30 #define MAX77686_DVS_ENABLE_TIME 22 /* us */
31 #define MAX77686_RAMP_DELAY 100000 /* uV/us */
32 #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
33 #define MAX77686_DVS_MINUV 600000
34 #define MAX77686_DVS_UVSTEP 12500
37 * Value for configuring buck[89] and LDO{20,21,22} as GPIO control.
38 * It is the same as 'off' for other regulators.
40 #define MAX77686_GPIO_CONTROL 0x0
42 * Values used for configuring LDOs and bucks.
43 * Forcing low power mode: LDO1, 3-5, 9, 13, 17-26
45 #define MAX77686_LDO_LOWPOWER 0x1
47 * On/off controlled by PWRREQ:
48 * - LDO2, 6-8, 10-12, 14-16
49 * - buck[1234]
51 #define MAX77686_OFF_PWRREQ 0x1
52 /* Low power mode controlled by PWRREQ: All LDOs */
53 #define MAX77686_LDO_LOWPOWER_PWRREQ 0x2
54 /* Forcing low power mode: buck[234] */
55 #define MAX77686_BUCK_LOWPOWER 0x2
56 #define MAX77686_NORMAL 0x3
58 #define MAX77686_OPMODE_SHIFT 6
59 #define MAX77686_OPMODE_BUCK234_SHIFT 4
60 #define MAX77686_OPMODE_MASK 0x3
62 #define MAX77686_VSEL_MASK 0x3F
63 #define MAX77686_DVS_VSEL_MASK 0xFF
65 #define MAX77686_RAMP_RATE_MASK 0xC0
67 #define MAX77686_REGULATORS MAX77686_REG_MAX
68 #define MAX77686_LDOS 26
70 enum max77686_ramp_rate {
71 RAMP_RATE_13P75MV,
72 RAMP_RATE_27P5MV,
73 RAMP_RATE_55MV,
74 RAMP_RATE_NO_CTRL, /* 100mV/us */
77 struct max77686_data {
78 struct device *dev;
79 DECLARE_BITMAP(gpio_enabled, MAX77686_REGULATORS);
81 /* Array indexed by regulator id */
82 unsigned int opmode[MAX77686_REGULATORS];
85 static unsigned int max77686_get_opmode_shift(int id)
87 switch (id) {
88 case MAX77686_BUCK1:
89 case MAX77686_BUCK5 ... MAX77686_BUCK9:
90 return 0;
91 case MAX77686_BUCK2 ... MAX77686_BUCK4:
92 return MAX77686_OPMODE_BUCK234_SHIFT;
93 default:
94 /* all LDOs */
95 return MAX77686_OPMODE_SHIFT;
100 * When regulator is configured for GPIO control then it
101 * replaces "normal" mode. Any change from low power mode to normal
102 * should actually change to GPIO control.
103 * Map normal mode to proper value for such regulators.
105 static unsigned int max77686_map_normal_mode(struct max77686_data *max77686,
106 int id)
108 switch (id) {
109 case MAX77686_BUCK8:
110 case MAX77686_BUCK9:
111 case MAX77686_LDO20 ... MAX77686_LDO22:
112 if (test_bit(id, max77686->gpio_enabled))
113 return MAX77686_GPIO_CONTROL;
116 return MAX77686_NORMAL;
119 /* Some BUCKs and LDOs supports Normal[ON/OFF] mode during suspend */
120 static int max77686_set_suspend_disable(struct regulator_dev *rdev)
122 unsigned int val, shift;
123 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
124 int ret, id = rdev_get_id(rdev);
126 shift = max77686_get_opmode_shift(id);
127 val = MAX77686_OFF_PWRREQ;
129 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
130 rdev->desc->enable_mask, val << shift);
131 if (ret)
132 return ret;
134 max77686->opmode[id] = val;
135 return 0;
138 /* Some LDOs supports [LPM/Normal]ON mode during suspend state */
139 static int max77686_set_suspend_mode(struct regulator_dev *rdev,
140 unsigned int mode)
142 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
143 unsigned int val;
144 int ret, id = rdev_get_id(rdev);
146 /* BUCK[5-9] doesn't support this feature */
147 if (id >= MAX77686_BUCK5)
148 return 0;
150 switch (mode) {
151 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
152 val = MAX77686_LDO_LOWPOWER_PWRREQ;
153 break;
154 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
155 val = max77686_map_normal_mode(max77686, id);
156 break;
157 default:
158 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
159 rdev->desc->name, mode);
160 return -EINVAL;
163 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
164 rdev->desc->enable_mask,
165 val << MAX77686_OPMODE_SHIFT);
166 if (ret)
167 return ret;
169 max77686->opmode[id] = val;
170 return 0;
173 /* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
174 static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
175 unsigned int mode)
177 unsigned int val;
178 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
179 int ret, id = rdev_get_id(rdev);
181 switch (mode) {
182 case REGULATOR_MODE_STANDBY: /* switch off */
183 val = MAX77686_OFF_PWRREQ;
184 break;
185 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
186 val = MAX77686_LDO_LOWPOWER_PWRREQ;
187 break;
188 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
189 val = max77686_map_normal_mode(max77686, id);
190 break;
191 default:
192 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
193 rdev->desc->name, mode);
194 return -EINVAL;
197 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
198 rdev->desc->enable_mask,
199 val << MAX77686_OPMODE_SHIFT);
200 if (ret)
201 return ret;
203 max77686->opmode[id] = val;
204 return 0;
207 static int max77686_enable(struct regulator_dev *rdev)
209 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
210 unsigned int shift;
211 int id = rdev_get_id(rdev);
213 shift = max77686_get_opmode_shift(id);
215 if (max77686->opmode[id] == MAX77686_OFF_PWRREQ)
216 max77686->opmode[id] = max77686_map_normal_mode(max77686, id);
218 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
219 rdev->desc->enable_mask,
220 max77686->opmode[id] << shift);
223 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
225 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
227 switch (ramp_delay) {
228 case 1 ... 13750:
229 ramp_value = RAMP_RATE_13P75MV;
230 break;
231 case 13751 ... 27500:
232 ramp_value = RAMP_RATE_27P5MV;
233 break;
234 case 27501 ... 55000:
235 ramp_value = RAMP_RATE_55MV;
236 break;
237 case 55001 ... 100000:
238 break;
239 default:
240 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
241 rdev->desc->name, ramp_delay);
244 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
245 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
248 static int max77686_of_parse_cb(struct device_node *np,
249 const struct regulator_desc *desc,
250 struct regulator_config *config)
252 struct max77686_data *max77686 = config->driver_data;
253 int ret;
255 switch (desc->id) {
256 case MAX77686_BUCK8:
257 case MAX77686_BUCK9:
258 case MAX77686_LDO20 ... MAX77686_LDO22:
259 config->ena_gpiod = gpiod_get_from_of_node(np,
260 "maxim,ena",
262 GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
263 "max77686-regulator");
264 if (IS_ERR(config->ena_gpiod))
265 config->ena_gpiod = NULL;
266 break;
267 default:
268 return 0;
271 if (config->ena_gpiod) {
272 set_bit(desc->id, max77686->gpio_enabled);
274 ret = regmap_update_bits(config->regmap, desc->enable_reg,
275 desc->enable_mask,
276 MAX77686_GPIO_CONTROL);
277 if (ret) {
278 gpiod_put(config->ena_gpiod);
279 config->ena_gpiod = NULL;
283 return 0;
286 static const struct regulator_ops max77686_ops = {
287 .list_voltage = regulator_list_voltage_linear,
288 .map_voltage = regulator_map_voltage_linear,
289 .is_enabled = regulator_is_enabled_regmap,
290 .enable = max77686_enable,
291 .disable = regulator_disable_regmap,
292 .get_voltage_sel = regulator_get_voltage_sel_regmap,
293 .set_voltage_sel = regulator_set_voltage_sel_regmap,
294 .set_voltage_time_sel = regulator_set_voltage_time_sel,
295 .set_suspend_mode = max77686_set_suspend_mode,
298 static const struct regulator_ops max77686_ldo_ops = {
299 .list_voltage = regulator_list_voltage_linear,
300 .map_voltage = regulator_map_voltage_linear,
301 .is_enabled = regulator_is_enabled_regmap,
302 .enable = max77686_enable,
303 .disable = regulator_disable_regmap,
304 .get_voltage_sel = regulator_get_voltage_sel_regmap,
305 .set_voltage_sel = regulator_set_voltage_sel_regmap,
306 .set_voltage_time_sel = regulator_set_voltage_time_sel,
307 .set_suspend_mode = max77686_ldo_set_suspend_mode,
308 .set_suspend_disable = max77686_set_suspend_disable,
311 static const struct regulator_ops max77686_buck1_ops = {
312 .list_voltage = regulator_list_voltage_linear,
313 .map_voltage = regulator_map_voltage_linear,
314 .is_enabled = regulator_is_enabled_regmap,
315 .enable = max77686_enable,
316 .disable = regulator_disable_regmap,
317 .get_voltage_sel = regulator_get_voltage_sel_regmap,
318 .set_voltage_sel = regulator_set_voltage_sel_regmap,
319 .set_voltage_time_sel = regulator_set_voltage_time_sel,
320 .set_suspend_disable = max77686_set_suspend_disable,
323 static const struct regulator_ops max77686_buck_dvs_ops = {
324 .list_voltage = regulator_list_voltage_linear,
325 .map_voltage = regulator_map_voltage_linear,
326 .is_enabled = regulator_is_enabled_regmap,
327 .enable = max77686_enable,
328 .disable = regulator_disable_regmap,
329 .get_voltage_sel = regulator_get_voltage_sel_regmap,
330 .set_voltage_sel = regulator_set_voltage_sel_regmap,
331 .set_voltage_time_sel = regulator_set_voltage_time_sel,
332 .set_ramp_delay = max77686_set_ramp_delay,
333 .set_suspend_disable = max77686_set_suspend_disable,
336 #define regulator_desc_ldo(num) { \
337 .name = "LDO"#num, \
338 .of_match = of_match_ptr("LDO"#num), \
339 .regulators_node = of_match_ptr("voltage-regulators"), \
340 .of_parse_cb = max77686_of_parse_cb, \
341 .id = MAX77686_LDO##num, \
342 .ops = &max77686_ops, \
343 .type = REGULATOR_VOLTAGE, \
344 .owner = THIS_MODULE, \
345 .min_uV = MAX77686_LDO_MINUV, \
346 .uV_step = MAX77686_LDO_UVSTEP, \
347 .ramp_delay = MAX77686_RAMP_DELAY, \
348 .n_voltages = MAX77686_VSEL_MASK + 1, \
349 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
350 .vsel_mask = MAX77686_VSEL_MASK, \
351 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
352 .enable_mask = MAX77686_OPMODE_MASK \
353 << MAX77686_OPMODE_SHIFT, \
355 #define regulator_desc_lpm_ldo(num) { \
356 .name = "LDO"#num, \
357 .of_match = of_match_ptr("LDO"#num), \
358 .regulators_node = of_match_ptr("voltage-regulators"), \
359 .id = MAX77686_LDO##num, \
360 .ops = &max77686_ldo_ops, \
361 .type = REGULATOR_VOLTAGE, \
362 .owner = THIS_MODULE, \
363 .min_uV = MAX77686_LDO_MINUV, \
364 .uV_step = MAX77686_LDO_UVSTEP, \
365 .ramp_delay = MAX77686_RAMP_DELAY, \
366 .n_voltages = MAX77686_VSEL_MASK + 1, \
367 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
368 .vsel_mask = MAX77686_VSEL_MASK, \
369 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
370 .enable_mask = MAX77686_OPMODE_MASK \
371 << MAX77686_OPMODE_SHIFT, \
373 #define regulator_desc_ldo_low(num) { \
374 .name = "LDO"#num, \
375 .of_match = of_match_ptr("LDO"#num), \
376 .regulators_node = of_match_ptr("voltage-regulators"), \
377 .id = MAX77686_LDO##num, \
378 .ops = &max77686_ldo_ops, \
379 .type = REGULATOR_VOLTAGE, \
380 .owner = THIS_MODULE, \
381 .min_uV = MAX77686_LDO_LOW_MINUV, \
382 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
383 .ramp_delay = MAX77686_RAMP_DELAY, \
384 .n_voltages = MAX77686_VSEL_MASK + 1, \
385 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
386 .vsel_mask = MAX77686_VSEL_MASK, \
387 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
388 .enable_mask = MAX77686_OPMODE_MASK \
389 << MAX77686_OPMODE_SHIFT, \
391 #define regulator_desc_ldo1_low(num) { \
392 .name = "LDO"#num, \
393 .of_match = of_match_ptr("LDO"#num), \
394 .regulators_node = of_match_ptr("voltage-regulators"), \
395 .id = MAX77686_LDO##num, \
396 .ops = &max77686_ops, \
397 .type = REGULATOR_VOLTAGE, \
398 .owner = THIS_MODULE, \
399 .min_uV = MAX77686_LDO_LOW_MINUV, \
400 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
401 .ramp_delay = MAX77686_RAMP_DELAY, \
402 .n_voltages = MAX77686_VSEL_MASK + 1, \
403 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
404 .vsel_mask = MAX77686_VSEL_MASK, \
405 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
406 .enable_mask = MAX77686_OPMODE_MASK \
407 << MAX77686_OPMODE_SHIFT, \
409 #define regulator_desc_buck(num) { \
410 .name = "BUCK"#num, \
411 .of_match = of_match_ptr("BUCK"#num), \
412 .regulators_node = of_match_ptr("voltage-regulators"), \
413 .of_parse_cb = max77686_of_parse_cb, \
414 .id = MAX77686_BUCK##num, \
415 .ops = &max77686_ops, \
416 .type = REGULATOR_VOLTAGE, \
417 .owner = THIS_MODULE, \
418 .min_uV = MAX77686_BUCK_MINUV, \
419 .uV_step = MAX77686_BUCK_UVSTEP, \
420 .ramp_delay = MAX77686_RAMP_DELAY, \
421 .enable_time = MAX77686_BUCK_ENABLE_TIME, \
422 .n_voltages = MAX77686_VSEL_MASK + 1, \
423 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
424 .vsel_mask = MAX77686_VSEL_MASK, \
425 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
426 .enable_mask = MAX77686_OPMODE_MASK, \
428 #define regulator_desc_buck1(num) { \
429 .name = "BUCK"#num, \
430 .of_match = of_match_ptr("BUCK"#num), \
431 .regulators_node = of_match_ptr("voltage-regulators"), \
432 .id = MAX77686_BUCK##num, \
433 .ops = &max77686_buck1_ops, \
434 .type = REGULATOR_VOLTAGE, \
435 .owner = THIS_MODULE, \
436 .min_uV = MAX77686_BUCK_MINUV, \
437 .uV_step = MAX77686_BUCK_UVSTEP, \
438 .ramp_delay = MAX77686_RAMP_DELAY, \
439 .enable_time = MAX77686_BUCK_ENABLE_TIME, \
440 .n_voltages = MAX77686_VSEL_MASK + 1, \
441 .vsel_reg = MAX77686_REG_BUCK1OUT, \
442 .vsel_mask = MAX77686_VSEL_MASK, \
443 .enable_reg = MAX77686_REG_BUCK1CTRL, \
444 .enable_mask = MAX77686_OPMODE_MASK, \
446 #define regulator_desc_buck_dvs(num) { \
447 .name = "BUCK"#num, \
448 .of_match = of_match_ptr("BUCK"#num), \
449 .regulators_node = of_match_ptr("voltage-regulators"), \
450 .id = MAX77686_BUCK##num, \
451 .ops = &max77686_buck_dvs_ops, \
452 .type = REGULATOR_VOLTAGE, \
453 .owner = THIS_MODULE, \
454 .min_uV = MAX77686_DVS_MINUV, \
455 .uV_step = MAX77686_DVS_UVSTEP, \
456 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
457 .enable_time = MAX77686_DVS_ENABLE_TIME, \
458 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
459 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
460 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
461 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
462 .enable_mask = MAX77686_OPMODE_MASK \
463 << MAX77686_OPMODE_BUCK234_SHIFT, \
466 static const struct regulator_desc regulators[] = {
467 regulator_desc_ldo1_low(1),
468 regulator_desc_ldo_low(2),
469 regulator_desc_ldo(3),
470 regulator_desc_ldo(4),
471 regulator_desc_ldo(5),
472 regulator_desc_ldo_low(6),
473 regulator_desc_ldo_low(7),
474 regulator_desc_ldo_low(8),
475 regulator_desc_ldo(9),
476 regulator_desc_lpm_ldo(10),
477 regulator_desc_lpm_ldo(11),
478 regulator_desc_lpm_ldo(12),
479 regulator_desc_ldo(13),
480 regulator_desc_lpm_ldo(14),
481 regulator_desc_ldo_low(15),
482 regulator_desc_lpm_ldo(16),
483 regulator_desc_ldo(17),
484 regulator_desc_ldo(18),
485 regulator_desc_ldo(19),
486 regulator_desc_ldo(20),
487 regulator_desc_ldo(21),
488 regulator_desc_ldo(22),
489 regulator_desc_ldo(23),
490 regulator_desc_ldo(24),
491 regulator_desc_ldo(25),
492 regulator_desc_ldo(26),
493 regulator_desc_buck1(1),
494 regulator_desc_buck_dvs(2),
495 regulator_desc_buck_dvs(3),
496 regulator_desc_buck_dvs(4),
497 regulator_desc_buck(5),
498 regulator_desc_buck(6),
499 regulator_desc_buck(7),
500 regulator_desc_buck(8),
501 regulator_desc_buck(9),
504 static int max77686_pmic_probe(struct platform_device *pdev)
506 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
507 struct max77686_data *max77686;
508 int i;
509 struct regulator_config config = { };
511 dev_dbg(&pdev->dev, "%s\n", __func__);
513 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
514 GFP_KERNEL);
515 if (!max77686)
516 return -ENOMEM;
518 max77686->dev = &pdev->dev;
519 config.dev = iodev->dev;
520 config.regmap = iodev->regmap;
521 config.driver_data = max77686;
522 platform_set_drvdata(pdev, max77686);
524 for (i = 0; i < MAX77686_REGULATORS; i++) {
525 struct regulator_dev *rdev;
526 int id = regulators[i].id;
528 max77686->opmode[id] = MAX77686_NORMAL;
529 rdev = devm_regulator_register(&pdev->dev,
530 &regulators[i], &config);
531 if (IS_ERR(rdev)) {
532 int ret = PTR_ERR(rdev);
533 dev_err(&pdev->dev,
534 "regulator init failed for %d: %d\n", i, ret);
535 return ret;
539 return 0;
542 static const struct platform_device_id max77686_pmic_id[] = {
543 {"max77686-pmic", 0},
544 { },
546 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
548 static struct platform_driver max77686_pmic_driver = {
549 .driver = {
550 .name = "max77686-pmic",
552 .probe = max77686_pmic_probe,
553 .id_table = max77686_pmic_id,
556 module_platform_driver(max77686_pmic_driver);
558 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
559 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
560 MODULE_LICENSE("GPL");