1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/bug.h>
16#include <linux/err.h>
17#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/regulator/of_regulator.h>
23#include <linux/mfd/max77686.h>
24#include <linux/mfd/max77686-private.h>
25#include <dt-bindings/regulator/maxim,max77802.h>
26
27
28#define MAX77802_RAMP_DELAY 100000
29
30#define MAX77802_OPMODE_SHIFT_LDO 6
31#define MAX77802_OPMODE_BUCK234_SHIFT 4
32#define MAX77802_OPMODE_MASK 0x3
33
34#define MAX77802_VSEL_MASK 0x3F
35#define MAX77802_DVS_VSEL_MASK 0xFF
36
37#define MAX77802_RAMP_RATE_MASK_2BIT 0xC0
38#define MAX77802_RAMP_RATE_SHIFT_2BIT 6
39#define MAX77802_RAMP_RATE_MASK_4BIT 0xF0
40#define MAX77802_RAMP_RATE_SHIFT_4BIT 4
41
42#define MAX77802_STATUS_OFF 0x0
43#define MAX77802_OFF_PWRREQ 0x1
44#define MAX77802_LP_PWRREQ 0x2
45
46static const unsigned int max77802_buck234_ramp_table[] = {
47 12500,
48 25000,
49 50000,
50 100000,
51};
52
53static const unsigned int max77802_buck16_ramp_table[] = {
54 1000, 2000, 3030, 4000,
55 5000, 5880, 7140, 8330,
56 9090, 10000, 11110, 12500,
57 16670, 25000, 50000, 100000,
58};
59
60struct max77802_regulator_prv {
61
62 unsigned int opmode[MAX77802_REG_MAX];
63};
64
65static inline unsigned int max77802_map_mode(unsigned int mode)
66{
67 return mode == MAX77802_OPMODE_NORMAL ?
68 REGULATOR_MODE_NORMAL : REGULATOR_MODE_STANDBY;
69}
70
71static int max77802_get_opmode_shift(int id)
72{
73 if (id == MAX77802_BUCK1 || (id >= MAX77802_BUCK5 &&
74 id <= MAX77802_BUCK10))
75 return 0;
76
77 if (id >= MAX77802_BUCK2 && id <= MAX77802_BUCK4)
78 return MAX77802_OPMODE_BUCK234_SHIFT;
79
80 if (id >= MAX77802_LDO1 && id <= MAX77802_LDO35)
81 return MAX77802_OPMODE_SHIFT_LDO;
82
83 return -EINVAL;
84}
85
86
87
88
89
90
91
92
93static int max77802_set_suspend_disable(struct regulator_dev *rdev)
94{
95 unsigned int val = MAX77802_OFF_PWRREQ;
96 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
97 int id = rdev_get_id(rdev);
98 int shift = max77802_get_opmode_shift(id);
99
100 max77802->opmode[id] = val;
101 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
102 rdev->desc->enable_mask, val << shift);
103}
104
105
106
107
108
109
110static int max77802_set_mode(struct regulator_dev *rdev, unsigned int mode)
111{
112 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
113 int id = rdev_get_id(rdev);
114 unsigned int val;
115 int shift = max77802_get_opmode_shift(id);
116
117 switch (mode) {
118 case REGULATOR_MODE_STANDBY:
119 val = MAX77802_OPMODE_LP;
120 break;
121 case REGULATOR_MODE_NORMAL:
122 val = MAX77802_OPMODE_NORMAL;
123 break;
124 default:
125 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
126 rdev->desc->name, mode);
127 return -EINVAL;
128 }
129
130 max77802->opmode[id] = val;
131 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
132 rdev->desc->enable_mask, val << shift);
133}
134
135static unsigned max77802_get_mode(struct regulator_dev *rdev)
136{
137 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
138 int id = rdev_get_id(rdev);
139
140 return max77802_map_mode(max77802->opmode[id]);
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159static int max77802_set_suspend_mode(struct regulator_dev *rdev,
160 unsigned int mode)
161{
162 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
163 int id = rdev_get_id(rdev);
164 unsigned int val;
165 int shift = max77802_get_opmode_shift(id);
166
167
168
169
170
171 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ) {
172 dev_warn(&rdev->dev, "%s: is disabled, mode: 0x%x not set\n",
173 rdev->desc->name, mode);
174 return 0;
175 }
176
177 switch (mode) {
178 case REGULATOR_MODE_STANDBY:
179
180
181
182
183
184 if (max77802->opmode[id] == MAX77802_OPMODE_NORMAL)
185 val = MAX77802_LP_PWRREQ;
186 else
187 return 0;
188 break;
189 case REGULATOR_MODE_NORMAL:
190
191
192
193
194
195 if (max77802->opmode[id] == MAX77802_OPMODE_LP)
196 dev_warn(&rdev->dev, "%s: in Low Power: 0x%x invalid\n",
197 rdev->desc->name, mode);
198 return 0;
199 default:
200 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
201 rdev->desc->name, mode);
202 return -EINVAL;
203 }
204
205 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
206 rdev->desc->enable_mask, val << shift);
207}
208
209static int max77802_enable(struct regulator_dev *rdev)
210{
211 struct max77802_regulator_prv *max77802 = rdev_get_drvdata(rdev);
212 int id = rdev_get_id(rdev);
213 int shift = max77802_get_opmode_shift(id);
214
215 if (max77802->opmode[id] == MAX77802_OFF_PWRREQ)
216 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
217
218 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
219 rdev->desc->enable_mask,
220 max77802->opmode[id] << shift);
221}
222
223
224
225
226static const struct regulator_ops max77802_ldo_ops_logic1 = {
227 .list_voltage = regulator_list_voltage_linear,
228 .map_voltage = regulator_map_voltage_linear,
229 .is_enabled = regulator_is_enabled_regmap,
230 .enable = max77802_enable,
231 .disable = regulator_disable_regmap,
232 .get_voltage_sel = regulator_get_voltage_sel_regmap,
233 .set_voltage_sel = regulator_set_voltage_sel_regmap,
234 .set_voltage_time_sel = regulator_set_voltage_time_sel,
235 .set_suspend_disable = max77802_set_suspend_disable,
236 .set_suspend_mode = max77802_set_suspend_mode,
237};
238
239
240
241
242static const struct regulator_ops max77802_ldo_ops_logic2 = {
243 .list_voltage = regulator_list_voltage_linear,
244 .map_voltage = regulator_map_voltage_linear,
245 .is_enabled = regulator_is_enabled_regmap,
246 .enable = max77802_enable,
247 .disable = regulator_disable_regmap,
248 .get_voltage_sel = regulator_get_voltage_sel_regmap,
249 .set_voltage_sel = regulator_set_voltage_sel_regmap,
250 .set_voltage_time_sel = regulator_set_voltage_time_sel,
251 .set_mode = max77802_set_mode,
252 .get_mode = max77802_get_mode,
253 .set_suspend_mode = max77802_set_suspend_mode,
254};
255
256
257static const struct regulator_ops max77802_buck_16_dvs_ops = {
258 .list_voltage = regulator_list_voltage_linear,
259 .map_voltage = regulator_map_voltage_linear,
260 .is_enabled = regulator_is_enabled_regmap,
261 .enable = max77802_enable,
262 .disable = regulator_disable_regmap,
263 .get_voltage_sel = regulator_get_voltage_sel_regmap,
264 .set_voltage_sel = regulator_set_voltage_sel_regmap,
265 .set_voltage_time_sel = regulator_set_voltage_time_sel,
266 .set_ramp_delay = regulator_set_ramp_delay_regmap,
267 .set_suspend_disable = max77802_set_suspend_disable,
268};
269
270
271static const struct regulator_ops max77802_buck_234_ops = {
272 .list_voltage = regulator_list_voltage_linear,
273 .map_voltage = regulator_map_voltage_linear,
274 .is_enabled = regulator_is_enabled_regmap,
275 .enable = max77802_enable,
276 .disable = regulator_disable_regmap,
277 .get_voltage_sel = regulator_get_voltage_sel_regmap,
278 .set_voltage_sel = regulator_set_voltage_sel_regmap,
279 .set_voltage_time_sel = regulator_set_voltage_time_sel,
280 .set_ramp_delay = regulator_set_ramp_delay_regmap,
281 .set_suspend_disable = max77802_set_suspend_disable,
282 .set_suspend_mode = max77802_set_suspend_mode,
283};
284
285
286static const struct regulator_ops max77802_buck_dvs_ops = {
287 .list_voltage = regulator_list_voltage_linear,
288 .map_voltage = regulator_map_voltage_linear,
289 .is_enabled = regulator_is_enabled_regmap,
290 .enable = max77802_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_disable = max77802_set_suspend_disable,
296};
297
298
299#define regulator_77802_desc_p_ldo(num, supply, log) { \
300 .name = "LDO"#num, \
301 .of_match = of_match_ptr("LDO"#num), \
302 .regulators_node = of_match_ptr("regulators"), \
303 .id = MAX77802_LDO##num, \
304 .supply_name = "inl"#supply, \
305 .ops = &max77802_ldo_ops_logic##log, \
306 .type = REGULATOR_VOLTAGE, \
307 .owner = THIS_MODULE, \
308 .min_uV = 800000, \
309 .uV_step = 50000, \
310 .ramp_delay = MAX77802_RAMP_DELAY, \
311 .n_voltages = 1 << 6, \
312 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
313 .vsel_mask = MAX77802_VSEL_MASK, \
314 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
315 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
316 .of_map_mode = max77802_map_mode, \
317}
318
319
320#define regulator_77802_desc_n_ldo(num, supply, log) { \
321 .name = "LDO"#num, \
322 .of_match = of_match_ptr("LDO"#num), \
323 .regulators_node = of_match_ptr("regulators"), \
324 .id = MAX77802_LDO##num, \
325 .supply_name = "inl"#supply, \
326 .ops = &max77802_ldo_ops_logic##log, \
327 .type = REGULATOR_VOLTAGE, \
328 .owner = THIS_MODULE, \
329 .min_uV = 800000, \
330 .uV_step = 25000, \
331 .ramp_delay = MAX77802_RAMP_DELAY, \
332 .n_voltages = 1 << 6, \
333 .vsel_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
334 .vsel_mask = MAX77802_VSEL_MASK, \
335 .enable_reg = MAX77802_REG_LDO1CTRL1 + num - 1, \
336 .enable_mask = MAX77802_OPMODE_MASK << MAX77802_OPMODE_SHIFT_LDO, \
337 .of_map_mode = max77802_map_mode, \
338}
339
340
341#define regulator_77802_desc_16_buck(num) { \
342 .name = "BUCK"#num, \
343 .of_match = of_match_ptr("BUCK"#num), \
344 .regulators_node = of_match_ptr("regulators"), \
345 .id = MAX77802_BUCK##num, \
346 .supply_name = "inb"#num, \
347 .ops = &max77802_buck_16_dvs_ops, \
348 .type = REGULATOR_VOLTAGE, \
349 .owner = THIS_MODULE, \
350 .min_uV = 612500, \
351 .uV_step = 6250, \
352 .ramp_delay = MAX77802_RAMP_DELAY, \
353 .n_voltages = 1 << 8, \
354 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
355 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
356 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL, \
357 .enable_mask = MAX77802_OPMODE_MASK, \
358 .ramp_reg = MAX77802_REG_BUCK ## num ## CTRL, \
359 .ramp_mask = MAX77802_RAMP_RATE_MASK_4BIT, \
360 .ramp_delay_table = max77802_buck16_ramp_table, \
361 .n_ramp_values = ARRAY_SIZE(max77802_buck16_ramp_table), \
362 .of_map_mode = max77802_map_mode, \
363}
364
365
366#define regulator_77802_desc_234_buck(num) { \
367 .name = "BUCK"#num, \
368 .of_match = of_match_ptr("BUCK"#num), \
369 .regulators_node = of_match_ptr("regulators"), \
370 .id = MAX77802_BUCK##num, \
371 .supply_name = "inb"#num, \
372 .ops = &max77802_buck_234_ops, \
373 .type = REGULATOR_VOLTAGE, \
374 .owner = THIS_MODULE, \
375 .min_uV = 600000, \
376 .uV_step = 6250, \
377 .ramp_delay = MAX77802_RAMP_DELAY, \
378 .n_voltages = 0x91, \
379 .vsel_reg = MAX77802_REG_BUCK ## num ## DVS1, \
380 .vsel_mask = MAX77802_DVS_VSEL_MASK, \
381 .enable_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
382 .enable_mask = MAX77802_OPMODE_MASK << \
383 MAX77802_OPMODE_BUCK234_SHIFT, \
384 .ramp_reg = MAX77802_REG_BUCK ## num ## CTRL1, \
385 .ramp_mask = MAX77802_RAMP_RATE_MASK_2BIT, \
386 .ramp_delay_table = max77802_buck234_ramp_table, \
387 .n_ramp_values = ARRAY_SIZE(max77802_buck234_ramp_table), \
388 .of_map_mode = max77802_map_mode, \
389}
390
391
392#define regulator_77802_desc_buck5(num) { \
393 .name = "BUCK"#num, \
394 .of_match = of_match_ptr("BUCK"#num), \
395 .regulators_node = of_match_ptr("regulators"), \
396 .id = MAX77802_BUCK##num, \
397 .supply_name = "inb"#num, \
398 .ops = &max77802_buck_dvs_ops, \
399 .type = REGULATOR_VOLTAGE, \
400 .owner = THIS_MODULE, \
401 .min_uV = 750000, \
402 .uV_step = 50000, \
403 .ramp_delay = MAX77802_RAMP_DELAY, \
404 .n_voltages = 1 << 6, \
405 .vsel_reg = MAX77802_REG_BUCK5OUT, \
406 .vsel_mask = MAX77802_VSEL_MASK, \
407 .enable_reg = MAX77802_REG_BUCK5CTRL, \
408 .enable_mask = MAX77802_OPMODE_MASK, \
409 .of_map_mode = max77802_map_mode, \
410}
411
412
413#define regulator_77802_desc_buck7_10(num) { \
414 .name = "BUCK"#num, \
415 .of_match = of_match_ptr("BUCK"#num), \
416 .regulators_node = of_match_ptr("regulators"), \
417 .id = MAX77802_BUCK##num, \
418 .supply_name = "inb"#num, \
419 .ops = &max77802_buck_dvs_ops, \
420 .type = REGULATOR_VOLTAGE, \
421 .owner = THIS_MODULE, \
422 .min_uV = 750000, \
423 .uV_step = 50000, \
424 .ramp_delay = MAX77802_RAMP_DELAY, \
425 .n_voltages = 1 << 6, \
426 .vsel_reg = MAX77802_REG_BUCK7OUT + (num - 7) * 3, \
427 .vsel_mask = MAX77802_VSEL_MASK, \
428 .enable_reg = MAX77802_REG_BUCK7CTRL + (num - 7) * 3, \
429 .enable_mask = MAX77802_OPMODE_MASK, \
430 .of_map_mode = max77802_map_mode, \
431}
432
433static const struct regulator_desc regulators[] = {
434 regulator_77802_desc_16_buck(1),
435 regulator_77802_desc_234_buck(2),
436 regulator_77802_desc_234_buck(3),
437 regulator_77802_desc_234_buck(4),
438 regulator_77802_desc_buck5(5),
439 regulator_77802_desc_16_buck(6),
440 regulator_77802_desc_buck7_10(7),
441 regulator_77802_desc_buck7_10(8),
442 regulator_77802_desc_buck7_10(9),
443 regulator_77802_desc_buck7_10(10),
444 regulator_77802_desc_n_ldo(1, 10, 2),
445 regulator_77802_desc_n_ldo(2, 10, 1),
446 regulator_77802_desc_p_ldo(3, 3, 2),
447 regulator_77802_desc_p_ldo(4, 6, 1),
448 regulator_77802_desc_p_ldo(5, 3, 1),
449 regulator_77802_desc_p_ldo(6, 3, 1),
450 regulator_77802_desc_p_ldo(7, 3, 1),
451 regulator_77802_desc_n_ldo(8, 1, 1),
452 regulator_77802_desc_p_ldo(9, 5, 1),
453 regulator_77802_desc_p_ldo(10, 4, 1),
454 regulator_77802_desc_p_ldo(11, 4, 1),
455 regulator_77802_desc_p_ldo(12, 9, 1),
456 regulator_77802_desc_p_ldo(13, 4, 1),
457 regulator_77802_desc_p_ldo(14, 4, 1),
458 regulator_77802_desc_n_ldo(15, 1, 1),
459 regulator_77802_desc_n_ldo(17, 2, 1),
460 regulator_77802_desc_p_ldo(18, 7, 1),
461 regulator_77802_desc_p_ldo(19, 5, 1),
462 regulator_77802_desc_p_ldo(20, 7, 2),
463 regulator_77802_desc_p_ldo(21, 6, 2),
464 regulator_77802_desc_p_ldo(23, 9, 1),
465 regulator_77802_desc_p_ldo(24, 6, 1),
466 regulator_77802_desc_p_ldo(25, 9, 1),
467 regulator_77802_desc_p_ldo(26, 9, 1),
468 regulator_77802_desc_n_ldo(27, 2, 1),
469 regulator_77802_desc_p_ldo(28, 7, 1),
470 regulator_77802_desc_p_ldo(29, 7, 1),
471 regulator_77802_desc_n_ldo(30, 2, 1),
472 regulator_77802_desc_p_ldo(32, 9, 1),
473 regulator_77802_desc_p_ldo(33, 6, 1),
474 regulator_77802_desc_p_ldo(34, 9, 1),
475 regulator_77802_desc_n_ldo(35, 2, 1),
476};
477
478static int max77802_pmic_probe(struct platform_device *pdev)
479{
480 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
481 struct max77802_regulator_prv *max77802;
482 int i, val;
483 struct regulator_config config = { };
484
485 max77802 = devm_kzalloc(&pdev->dev,
486 sizeof(struct max77802_regulator_prv),
487 GFP_KERNEL);
488 if (!max77802)
489 return -ENOMEM;
490
491 config.dev = iodev->dev;
492 config.regmap = iodev->regmap;
493 config.driver_data = max77802;
494 platform_set_drvdata(pdev, max77802);
495
496 for (i = 0; i < MAX77802_REG_MAX; i++) {
497 struct regulator_dev *rdev;
498 int id = regulators[i].id;
499 int shift = max77802_get_opmode_shift(id);
500 int ret;
501
502 ret = regmap_read(iodev->regmap, regulators[i].enable_reg, &val);
503 if (ret < 0) {
504 dev_warn(&pdev->dev,
505 "cannot read current mode for %d\n", i);
506 val = MAX77802_OPMODE_NORMAL;
507 } else {
508 val = val >> shift & MAX77802_OPMODE_MASK;
509 }
510
511
512
513
514
515
516 if (val == MAX77802_STATUS_OFF)
517 max77802->opmode[id] = MAX77802_OPMODE_NORMAL;
518 else
519 max77802->opmode[id] = val;
520
521 rdev = devm_regulator_register(&pdev->dev,
522 ®ulators[i], &config);
523 if (IS_ERR(rdev)) {
524 ret = PTR_ERR(rdev);
525 dev_err(&pdev->dev,
526 "regulator init failed for %d: %d\n", i, ret);
527 return ret;
528 }
529 }
530
531 return 0;
532}
533
534static const struct platform_device_id max77802_pmic_id[] = {
535 {"max77802-pmic", 0},
536 { },
537};
538MODULE_DEVICE_TABLE(platform, max77802_pmic_id);
539
540static struct platform_driver max77802_pmic_driver = {
541 .driver = {
542 .name = "max77802-pmic",
543 },
544 .probe = max77802_pmic_probe,
545 .id_table = max77802_pmic_id,
546};
547
548module_platform_driver(max77802_pmic_driver);
549
550MODULE_DESCRIPTION("MAXIM 77802 Regulator Driver");
551MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
552MODULE_LICENSE("GPL");
553