1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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>
37
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
45#define MAX77686_DVS_ENABLE_TIME 22
46#define MAX77686_RAMP_DELAY 100000
47#define MAX77686_DVS_RAMP_DELAY 27500
48#define MAX77686_DVS_MINUV 600000
49#define MAX77686_DVS_UVSTEP 12500
50
51
52
53
54
55#define MAX77686_GPIO_CONTROL 0x0
56
57
58
59
60#define MAX77686_LDO_LOWPOWER 0x1
61
62
63
64
65
66#define MAX77686_OFF_PWRREQ 0x1
67
68#define MAX77686_LDO_LOWPOWER_PWRREQ 0x2
69
70#define MAX77686_BUCK_LOWPOWER 0x2
71#define MAX77686_NORMAL 0x3
72
73#define MAX77686_OPMODE_SHIFT 6
74#define MAX77686_OPMODE_BUCK234_SHIFT 4
75#define MAX77686_OPMODE_MASK 0x3
76
77#define MAX77686_VSEL_MASK 0x3F
78#define MAX77686_DVS_VSEL_MASK 0xFF
79
80#define MAX77686_RAMP_RATE_MASK 0xC0
81
82#define MAX77686_REGULATORS MAX77686_REG_MAX
83#define MAX77686_LDOS 26
84
85enum max77686_ramp_rate {
86 RAMP_RATE_13P75MV,
87 RAMP_RATE_27P5MV,
88 RAMP_RATE_55MV,
89 RAMP_RATE_NO_CTRL,
90};
91
92struct max77686_data {
93 DECLARE_BITMAP(gpio_enabled, MAX77686_REGULATORS);
94
95
96 unsigned int opmode[MAX77686_REGULATORS];
97};
98
99static unsigned int max77686_get_opmode_shift(int id)
100{
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
109 return MAX77686_OPMODE_SHIFT;
110 }
111}
112
113
114
115
116
117
118
119static unsigned int max77686_map_normal_mode(struct max77686_data *max77686,
120 int id)
121{
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;
128 }
129
130 return MAX77686_NORMAL;
131}
132
133
134static int max77686_set_suspend_disable(struct regulator_dev *rdev)
135{
136 unsigned int val, shift;
137 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
138 int ret, id = rdev_get_id(rdev);
139
140 shift = max77686_get_opmode_shift(id);
141 val = MAX77686_OFF_PWRREQ;
142
143 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
144 rdev->desc->enable_mask, val << shift);
145 if (ret)
146 return ret;
147
148 max77686->opmode[id] = val;
149 return 0;
150}
151
152
153static int max77686_set_suspend_mode(struct regulator_dev *rdev,
154 unsigned int mode)
155{
156 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
157 unsigned int val;
158 int ret, id = rdev_get_id(rdev);
159
160
161 if (id >= MAX77686_BUCK5)
162 return 0;
163
164 switch (mode) {
165 case REGULATOR_MODE_IDLE:
166 val = MAX77686_LDO_LOWPOWER_PWRREQ;
167 break;
168 case REGULATOR_MODE_NORMAL:
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;
175 }
176
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;
182
183 max77686->opmode[id] = val;
184 return 0;
185}
186
187
188static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
189 unsigned int mode)
190{
191 unsigned int val;
192 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
193 int ret, id = rdev_get_id(rdev);
194
195 switch (mode) {
196 case REGULATOR_MODE_STANDBY:
197 val = MAX77686_OFF_PWRREQ;
198 break;
199 case REGULATOR_MODE_IDLE:
200 val = MAX77686_LDO_LOWPOWER_PWRREQ;
201 break;
202 case REGULATOR_MODE_NORMAL:
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;
209 }
210
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;
216
217 max77686->opmode[id] = val;
218 return 0;
219}
220
221static int max77686_enable(struct regulator_dev *rdev)
222{
223 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
224 unsigned int shift;
225 int id = rdev_get_id(rdev);
226
227 shift = max77686_get_opmode_shift(id);
228
229 if (max77686->opmode[id] == MAX77686_OFF_PWRREQ)
230 max77686->opmode[id] = max77686_map_normal_mode(max77686, id);
231
232 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
233 rdev->desc->enable_mask,
234 max77686->opmode[id] << shift);
235}
236
237static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
238{
239 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
240
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);
256 }
257
258 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
259 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
260}
261
262static int max77686_of_parse_cb(struct device_node *np,
263 const struct regulator_desc *desc,
264 struct regulator_config *config)
265{
266 struct max77686_data *max77686 = config->driver_data;
267
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;
279 }
280
281 if (gpio_is_valid(config->ena_gpio)) {
282 set_bit(desc->id, max77686->gpio_enabled);
283
284 return regmap_update_bits(config->regmap, desc->enable_reg,
285 desc->enable_mask,
286 MAX77686_GPIO_CONTROL);
287 }
288
289 return 0;
290}
291
292static 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,
302};
303
304static 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,
315};
316
317static 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,
327};
328
329static 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,
340};
341
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, \
360}
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, \
378}
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, \
396}
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, \
414}
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, \
433}
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, \
451}
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, \
470}
471
472static 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),
508};
509
510static int max77686_pmic_probe(struct platform_device *pdev)
511{
512 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
513 struct max77686_data *max77686;
514 int i;
515 struct regulator_config config = { };
516
517 dev_dbg(&pdev->dev, "%s\n", __func__);
518
519 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
520 GFP_KERNEL);
521 if (!max77686)
522 return -ENOMEM;
523
524 config.dev = iodev->dev;
525 config.regmap = iodev->regmap;
526 config.driver_data = max77686;
527 platform_set_drvdata(pdev, max77686);
528
529 for (i = 0; i < MAX77686_REGULATORS; i++) {
530 struct regulator_dev *rdev;
531 int id = regulators[i].id;
532
533 max77686->opmode[id] = MAX77686_NORMAL;
534 rdev = devm_regulator_register(&pdev->dev,
535 ®ulators[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;
541 }
542 }
543
544 return 0;
545}
546
547static const struct platform_device_id max77686_pmic_id[] = {
548 {"max77686-pmic", 0},
549 { },
550};
551MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
552
553static struct platform_driver max77686_pmic_driver = {
554 .driver = {
555 .name = "max77686-pmic",
556 },
557 .probe = max77686_pmic_probe,
558 .id_table = max77686_pmic_id,
559};
560
561module_platform_driver(max77686_pmic_driver);
562
563MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
564MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
565MODULE_LICENSE("GPL");
566