1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/bug.h>
25#include <linux/err.h>
26#include <linux/gpio.h>
27#include <linux/of_gpio.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/platform_device.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
33#include <linux/mfd/max8997.h>
34#include <linux/mfd/max8997-private.h>
35#include <linux/regulator/of_regulator.h>
36
37struct max8997_data {
38 struct device *dev;
39 struct max8997_dev *iodev;
40 int num_regulators;
41 int ramp_delay;
42
43 bool buck1_gpiodvs;
44 bool buck2_gpiodvs;
45 bool buck5_gpiodvs;
46 u8 buck1_vol[8];
47 u8 buck2_vol[8];
48 u8 buck5_vol[8];
49 int buck125_gpios[3];
50 int buck125_gpioindex;
51 bool ignore_gpiodvs_side_effect;
52
53 u8 saved_states[MAX8997_REG_MAX];
54};
55
56static const unsigned int safeoutvolt[] = {
57 4850000,
58 4900000,
59 4950000,
60 3300000,
61};
62
63static inline void max8997_set_gpio(struct max8997_data *max8997)
64{
65 int set3 = (max8997->buck125_gpioindex) & 0x1;
66 int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
67 int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
68
69 gpio_set_value(max8997->buck125_gpios[0], set1);
70 gpio_set_value(max8997->buck125_gpios[1], set2);
71 gpio_set_value(max8997->buck125_gpios[2], set3);
72}
73
74struct voltage_map_desc {
75 int min;
76 int max;
77 int step;
78};
79
80
81static const struct voltage_map_desc ldo_voltage_map_desc = {
82 .min = 800000, .max = 3950000, .step = 50000,
83};
84
85static const struct voltage_map_desc buck1245_voltage_map_desc = {
86 .min = 650000, .max = 2225000, .step = 25000,
87};
88
89static const struct voltage_map_desc buck37_voltage_map_desc = {
90 .min = 750000, .max = 3900000, .step = 50000,
91};
92
93
94static const struct voltage_map_desc charger_current_map_desc = {
95 .min = 200000, .max = 950000, .step = 50000,
96};
97
98static const struct voltage_map_desc topoff_current_map_desc = {
99 .min = 50000, .max = 200000, .step = 10000,
100};
101
102static const struct voltage_map_desc *reg_voltage_map[] = {
103 [MAX8997_LDO1] = &ldo_voltage_map_desc,
104 [MAX8997_LDO2] = &ldo_voltage_map_desc,
105 [MAX8997_LDO3] = &ldo_voltage_map_desc,
106 [MAX8997_LDO4] = &ldo_voltage_map_desc,
107 [MAX8997_LDO5] = &ldo_voltage_map_desc,
108 [MAX8997_LDO6] = &ldo_voltage_map_desc,
109 [MAX8997_LDO7] = &ldo_voltage_map_desc,
110 [MAX8997_LDO8] = &ldo_voltage_map_desc,
111 [MAX8997_LDO9] = &ldo_voltage_map_desc,
112 [MAX8997_LDO10] = &ldo_voltage_map_desc,
113 [MAX8997_LDO11] = &ldo_voltage_map_desc,
114 [MAX8997_LDO12] = &ldo_voltage_map_desc,
115 [MAX8997_LDO13] = &ldo_voltage_map_desc,
116 [MAX8997_LDO14] = &ldo_voltage_map_desc,
117 [MAX8997_LDO15] = &ldo_voltage_map_desc,
118 [MAX8997_LDO16] = &ldo_voltage_map_desc,
119 [MAX8997_LDO17] = &ldo_voltage_map_desc,
120 [MAX8997_LDO18] = &ldo_voltage_map_desc,
121 [MAX8997_LDO21] = &ldo_voltage_map_desc,
122 [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
123 [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
124 [MAX8997_BUCK3] = &buck37_voltage_map_desc,
125 [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
126 [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
127 [MAX8997_BUCK6] = NULL,
128 [MAX8997_BUCK7] = &buck37_voltage_map_desc,
129 [MAX8997_EN32KHZ_AP] = NULL,
130 [MAX8997_EN32KHZ_CP] = NULL,
131 [MAX8997_ENVICHG] = NULL,
132 [MAX8997_ESAFEOUT1] = NULL,
133 [MAX8997_ESAFEOUT2] = NULL,
134 [MAX8997_CHARGER_CV] = NULL,
135 [MAX8997_CHARGER] = &charger_current_map_desc,
136 [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
137};
138
139static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
140 unsigned int selector)
141{
142 int rid = rdev_get_id(rdev);
143
144 if (rid != MAX8997_CHARGER_CV)
145 goto err;
146
147 switch (selector) {
148 case 0x00:
149 return 4200000;
150 case 0x01 ... 0x0E:
151 return 4000000 + 20000 * (selector - 0x01);
152 case 0x0F:
153 return 4350000;
154 default:
155 return -EINVAL;
156 }
157err:
158 return -EINVAL;
159}
160
161static int max8997_list_voltage(struct regulator_dev *rdev,
162 unsigned int selector)
163{
164 const struct voltage_map_desc *desc;
165 int rid = rdev_get_id(rdev);
166 int val;
167
168 if (rid >= ARRAY_SIZE(reg_voltage_map) ||
169 rid < 0)
170 return -EINVAL;
171
172 desc = reg_voltage_map[rid];
173 if (desc == NULL)
174 return -EINVAL;
175
176 val = desc->min + desc->step * selector;
177 if (val > desc->max)
178 return -EINVAL;
179
180 return val;
181}
182
183static int max8997_get_enable_register(struct regulator_dev *rdev,
184 int *reg, int *mask, int *pattern)
185{
186 int rid = rdev_get_id(rdev);
187
188 switch (rid) {
189 case MAX8997_LDO1 ... MAX8997_LDO21:
190 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
191 *mask = 0xC0;
192 *pattern = 0xC0;
193 break;
194 case MAX8997_BUCK1:
195 *reg = MAX8997_REG_BUCK1CTRL;
196 *mask = 0x01;
197 *pattern = 0x01;
198 break;
199 case MAX8997_BUCK2:
200 *reg = MAX8997_REG_BUCK2CTRL;
201 *mask = 0x01;
202 *pattern = 0x01;
203 break;
204 case MAX8997_BUCK3:
205 *reg = MAX8997_REG_BUCK3CTRL;
206 *mask = 0x01;
207 *pattern = 0x01;
208 break;
209 case MAX8997_BUCK4:
210 *reg = MAX8997_REG_BUCK4CTRL;
211 *mask = 0x01;
212 *pattern = 0x01;
213 break;
214 case MAX8997_BUCK5:
215 *reg = MAX8997_REG_BUCK5CTRL;
216 *mask = 0x01;
217 *pattern = 0x01;
218 break;
219 case MAX8997_BUCK6:
220 *reg = MAX8997_REG_BUCK6CTRL;
221 *mask = 0x01;
222 *pattern = 0x01;
223 break;
224 case MAX8997_BUCK7:
225 *reg = MAX8997_REG_BUCK7CTRL;
226 *mask = 0x01;
227 *pattern = 0x01;
228 break;
229 case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
230 *reg = MAX8997_REG_MAINCON1;
231 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
232 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
233 break;
234 case MAX8997_ENVICHG:
235 *reg = MAX8997_REG_MBCCTRL1;
236 *mask = 0x80;
237 *pattern = 0x80;
238 break;
239 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
240 *reg = MAX8997_REG_SAFEOUTCTRL;
241 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
242 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
243 break;
244 case MAX8997_CHARGER:
245 *reg = MAX8997_REG_MBCCTRL2;
246 *mask = 0x40;
247 *pattern = 0x40;
248 break;
249 default:
250
251 return -EINVAL;
252 }
253
254 return 0;
255}
256
257static int max8997_reg_is_enabled(struct regulator_dev *rdev)
258{
259 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
260 struct i2c_client *i2c = max8997->iodev->i2c;
261 int ret, reg, mask, pattern;
262 u8 val;
263
264 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
265 if (ret)
266 return ret;
267
268 ret = max8997_read_reg(i2c, reg, &val);
269 if (ret)
270 return ret;
271
272 return (val & mask) == pattern;
273}
274
275static int max8997_reg_enable(struct regulator_dev *rdev)
276{
277 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
278 struct i2c_client *i2c = max8997->iodev->i2c;
279 int ret, reg, mask, pattern;
280
281 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
282 if (ret)
283 return ret;
284
285 return max8997_update_reg(i2c, reg, pattern, mask);
286}
287
288static int max8997_reg_disable(struct regulator_dev *rdev)
289{
290 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
291 struct i2c_client *i2c = max8997->iodev->i2c;
292 int ret, reg, mask, pattern;
293
294 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
295 if (ret)
296 return ret;
297
298 return max8997_update_reg(i2c, reg, ~pattern, mask);
299}
300
301static int max8997_get_voltage_register(struct regulator_dev *rdev,
302 int *_reg, int *_shift, int *_mask)
303{
304 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
305 int rid = rdev_get_id(rdev);
306 int reg, shift = 0, mask = 0x3f;
307
308 switch (rid) {
309 case MAX8997_LDO1 ... MAX8997_LDO21:
310 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
311 break;
312 case MAX8997_BUCK1:
313 reg = MAX8997_REG_BUCK1DVS1;
314 if (max8997->buck1_gpiodvs)
315 reg += max8997->buck125_gpioindex;
316 break;
317 case MAX8997_BUCK2:
318 reg = MAX8997_REG_BUCK2DVS1;
319 if (max8997->buck2_gpiodvs)
320 reg += max8997->buck125_gpioindex;
321 break;
322 case MAX8997_BUCK3:
323 reg = MAX8997_REG_BUCK3DVS;
324 break;
325 case MAX8997_BUCK4:
326 reg = MAX8997_REG_BUCK4DVS;
327 break;
328 case MAX8997_BUCK5:
329 reg = MAX8997_REG_BUCK5DVS1;
330 if (max8997->buck5_gpiodvs)
331 reg += max8997->buck125_gpioindex;
332 break;
333 case MAX8997_BUCK7:
334 reg = MAX8997_REG_BUCK7DVS;
335 break;
336 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
337 reg = MAX8997_REG_SAFEOUTCTRL;
338 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
339 mask = 0x3;
340 break;
341 case MAX8997_CHARGER_CV:
342 reg = MAX8997_REG_MBCCTRL3;
343 shift = 0;
344 mask = 0xf;
345 break;
346 case MAX8997_CHARGER:
347 reg = MAX8997_REG_MBCCTRL4;
348 shift = 0;
349 mask = 0xf;
350 break;
351 case MAX8997_CHARGER_TOPOFF:
352 reg = MAX8997_REG_MBCCTRL5;
353 shift = 0;
354 mask = 0xf;
355 break;
356 default:
357 return -EINVAL;
358 }
359
360 *_reg = reg;
361 *_shift = shift;
362 *_mask = mask;
363
364 return 0;
365}
366
367static int max8997_get_voltage_sel(struct regulator_dev *rdev)
368{
369 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
370 struct i2c_client *i2c = max8997->iodev->i2c;
371 int reg, shift, mask, ret;
372 u8 val;
373
374 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
375 if (ret)
376 return ret;
377
378 ret = max8997_read_reg(i2c, reg, &val);
379 if (ret)
380 return ret;
381
382 val >>= shift;
383 val &= mask;
384
385 return val;
386}
387
388static inline int max8997_get_voltage_proper_val(
389 const struct voltage_map_desc *desc,
390 int min_vol, int max_vol)
391{
392 int i;
393
394 if (desc == NULL)
395 return -EINVAL;
396
397 if (max_vol < desc->min || min_vol > desc->max)
398 return -EINVAL;
399
400 if (min_vol < desc->min)
401 min_vol = desc->min;
402
403 i = DIV_ROUND_UP(min_vol - desc->min, desc->step);
404
405 if (desc->min + desc->step * i > max_vol)
406 return -EINVAL;
407
408 return i;
409}
410
411static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
412 int min_uV, int max_uV, unsigned *selector)
413{
414 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
415 struct i2c_client *i2c = max8997->iodev->i2c;
416 int rid = rdev_get_id(rdev);
417 int lb, ub;
418 int reg, shift = 0, mask, ret = 0;
419 u8 val = 0x0;
420
421 if (rid != MAX8997_CHARGER_CV)
422 return -EINVAL;
423
424 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
425 if (ret)
426 return ret;
427
428 if (max_uV < 4000000 || min_uV > 4350000)
429 return -EINVAL;
430
431 if (min_uV <= 4000000)
432 val = 0x1;
433 else if (min_uV <= 4200000 && max_uV >= 4200000)
434 val = 0x0;
435 else {
436 lb = (min_uV - 4000001) / 20000 + 2;
437 ub = (max_uV - 4000000) / 20000 + 1;
438
439 if (lb > ub)
440 return -EINVAL;
441
442 if (lb < 0xf)
443 val = lb;
444 else {
445 if (ub >= 0xf)
446 val = 0xf;
447 else
448 return -EINVAL;
449 }
450 }
451
452 *selector = val;
453
454 ret = max8997_update_reg(i2c, reg, val << shift, mask);
455
456 return ret;
457}
458
459
460
461
462
463static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
464 int min_uV, int max_uV, unsigned *selector)
465{
466 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
467 struct i2c_client *i2c = max8997->iodev->i2c;
468 const struct voltage_map_desc *desc;
469 int rid = rdev_get_id(rdev);
470 int i, reg, shift, mask, ret;
471
472 switch (rid) {
473 case MAX8997_LDO1 ... MAX8997_LDO21:
474 break;
475 case MAX8997_BUCK1 ... MAX8997_BUCK5:
476 break;
477 case MAX8997_BUCK6:
478 return -EINVAL;
479 case MAX8997_BUCK7:
480 break;
481 case MAX8997_CHARGER:
482 break;
483 case MAX8997_CHARGER_TOPOFF:
484 break;
485 default:
486 return -EINVAL;
487 }
488
489 desc = reg_voltage_map[rid];
490
491 i = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
492 if (i < 0)
493 return i;
494
495 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
496 if (ret)
497 return ret;
498
499 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
500 *selector = i;
501
502 return ret;
503}
504
505static int max8997_set_voltage_buck_time_sel(struct regulator_dev *rdev,
506 unsigned int old_selector,
507 unsigned int new_selector)
508{
509 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
510 int rid = rdev_get_id(rdev);
511 const struct voltage_map_desc *desc = reg_voltage_map[rid];
512
513
514 if (old_selector >= new_selector)
515 return 0;
516
517
518 switch (rid) {
519 case MAX8997_BUCK1:
520 if (max8997->buck1_gpiodvs)
521 return 0;
522 break;
523 case MAX8997_BUCK2:
524 if (max8997->buck2_gpiodvs)
525 return 0;
526 break;
527 case MAX8997_BUCK5:
528 if (max8997->buck5_gpiodvs)
529 return 0;
530 break;
531 }
532
533 switch (rid) {
534 case MAX8997_BUCK1:
535 case MAX8997_BUCK2:
536 case MAX8997_BUCK4:
537 case MAX8997_BUCK5:
538 return DIV_ROUND_UP(desc->step * (new_selector - old_selector),
539 max8997->ramp_delay * 1000);
540 }
541
542 return 0;
543}
544
545
546
547
548
549
550
551
552
553static int max8997_assess_side_effect(struct regulator_dev *rdev,
554 u8 new_val, int *best)
555{
556 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
557 int rid = rdev_get_id(rdev);
558 u8 *buckx_val[3];
559 bool buckx_gpiodvs[3];
560 int side_effect[8];
561 int min_side_effect = INT_MAX;
562 int i;
563
564 *best = -1;
565
566 switch (rid) {
567 case MAX8997_BUCK1:
568 rid = 0;
569 break;
570 case MAX8997_BUCK2:
571 rid = 1;
572 break;
573 case MAX8997_BUCK5:
574 rid = 2;
575 break;
576 default:
577 return -EINVAL;
578 }
579
580 buckx_val[0] = max8997->buck1_vol;
581 buckx_val[1] = max8997->buck2_vol;
582 buckx_val[2] = max8997->buck5_vol;
583 buckx_gpiodvs[0] = max8997->buck1_gpiodvs;
584 buckx_gpiodvs[1] = max8997->buck2_gpiodvs;
585 buckx_gpiodvs[2] = max8997->buck5_gpiodvs;
586
587 for (i = 0; i < 8; i++) {
588 int others;
589
590 if (new_val != (buckx_val[rid])[i]) {
591 side_effect[i] = -1;
592 continue;
593 }
594
595 side_effect[i] = 0;
596 for (others = 0; others < 3; others++) {
597 int diff;
598
599 if (others == rid)
600 continue;
601 if (buckx_gpiodvs[others] == false)
602 continue;
603 diff = (buckx_val[others])[i] -
604 (buckx_val[others])[max8997->buck125_gpioindex];
605 if (diff > 0)
606 side_effect[i] += diff;
607 else if (diff < 0)
608 side_effect[i] -= diff;
609 }
610 if (side_effect[i] == 0) {
611 *best = i;
612 return 0;
613 }
614 if (side_effect[i] < min_side_effect) {
615 min_side_effect = side_effect[i];
616 *best = i;
617 }
618 }
619
620 if (*best == -1)
621 return -EINVAL;
622
623 return side_effect[*best];
624}
625
626
627
628
629
630static int max8997_set_voltage_buck(struct regulator_dev *rdev,
631 int min_uV, int max_uV, unsigned *selector)
632{
633 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
634 int rid = rdev_get_id(rdev);
635 const struct voltage_map_desc *desc;
636 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
637 bool gpio_dvs_mode = false;
638
639 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
640 return -EINVAL;
641
642 switch (rid) {
643 case MAX8997_BUCK1:
644 if (max8997->buck1_gpiodvs)
645 gpio_dvs_mode = true;
646 break;
647 case MAX8997_BUCK2:
648 if (max8997->buck2_gpiodvs)
649 gpio_dvs_mode = true;
650 break;
651 case MAX8997_BUCK5:
652 if (max8997->buck5_gpiodvs)
653 gpio_dvs_mode = true;
654 break;
655 }
656
657 if (!gpio_dvs_mode)
658 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
659 selector);
660
661 desc = reg_voltage_map[rid];
662 new_val = max8997_get_voltage_proper_val(desc, min_uV, max_uV);
663 if (new_val < 0)
664 return new_val;
665
666 tmp_dmg = INT_MAX;
667 tmp_idx = -1;
668 tmp_val = -1;
669 do {
670 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
671 if (damage == 0)
672 goto out;
673
674 if (tmp_dmg > damage) {
675 tmp_idx = new_idx;
676 tmp_val = new_val;
677 tmp_dmg = damage;
678 }
679
680 new_val++;
681 } while (desc->min + desc->step * new_val <= desc->max);
682
683 new_idx = tmp_idx;
684 new_val = tmp_val;
685
686 if (max8997->ignore_gpiodvs_side_effect == false)
687 return -EINVAL;
688
689 dev_warn(&rdev->dev,
690 "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET: %d -> %d\n",
691 max8997->buck125_gpioindex, tmp_idx);
692
693out:
694 if (new_idx < 0 || new_val < 0)
695 return -EINVAL;
696
697 max8997->buck125_gpioindex = new_idx;
698 max8997_set_gpio(max8997);
699 *selector = new_val;
700
701 return 0;
702}
703
704
705static int max8997_set_voltage_safeout_sel(struct regulator_dev *rdev,
706 unsigned selector)
707{
708 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
709 struct i2c_client *i2c = max8997->iodev->i2c;
710 int rid = rdev_get_id(rdev);
711 int reg, shift = 0, mask, ret;
712
713 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
714 return -EINVAL;
715
716 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
717 if (ret)
718 return ret;
719
720 return max8997_update_reg(i2c, reg, selector << shift, mask << shift);
721}
722
723static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
724{
725 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
726 struct i2c_client *i2c = max8997->iodev->i2c;
727 int ret, reg, mask, pattern;
728 int rid = rdev_get_id(rdev);
729
730 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
731 if (ret)
732 return ret;
733
734 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
735
736 if (rid == MAX8997_LDO1 ||
737 rid == MAX8997_LDO10 ||
738 rid == MAX8997_LDO21) {
739 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
740 rdev->desc->name);
741 return max8997_update_reg(i2c, reg, 0x40, mask);
742 }
743
744 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
745 rdev->desc->name, max8997->saved_states[rid] & mask,
746 (~pattern) & mask);
747 return max8997_update_reg(i2c, reg, ~pattern, mask);
748}
749
750static struct regulator_ops max8997_ldo_ops = {
751 .list_voltage = max8997_list_voltage,
752 .is_enabled = max8997_reg_is_enabled,
753 .enable = max8997_reg_enable,
754 .disable = max8997_reg_disable,
755 .get_voltage_sel = max8997_get_voltage_sel,
756 .set_voltage = max8997_set_voltage_ldobuck,
757 .set_suspend_disable = max8997_reg_disable_suspend,
758};
759
760static struct regulator_ops max8997_buck_ops = {
761 .list_voltage = max8997_list_voltage,
762 .is_enabled = max8997_reg_is_enabled,
763 .enable = max8997_reg_enable,
764 .disable = max8997_reg_disable,
765 .get_voltage_sel = max8997_get_voltage_sel,
766 .set_voltage = max8997_set_voltage_buck,
767 .set_voltage_time_sel = max8997_set_voltage_buck_time_sel,
768 .set_suspend_disable = max8997_reg_disable_suspend,
769};
770
771static struct regulator_ops max8997_fixedvolt_ops = {
772 .list_voltage = max8997_list_voltage,
773 .is_enabled = max8997_reg_is_enabled,
774 .enable = max8997_reg_enable,
775 .disable = max8997_reg_disable,
776 .set_suspend_disable = max8997_reg_disable_suspend,
777};
778
779static struct regulator_ops max8997_safeout_ops = {
780 .list_voltage = regulator_list_voltage_table,
781 .is_enabled = max8997_reg_is_enabled,
782 .enable = max8997_reg_enable,
783 .disable = max8997_reg_disable,
784 .get_voltage_sel = max8997_get_voltage_sel,
785 .set_voltage_sel = max8997_set_voltage_safeout_sel,
786 .set_suspend_disable = max8997_reg_disable_suspend,
787};
788
789static struct regulator_ops max8997_fixedstate_ops = {
790 .list_voltage = max8997_list_voltage_charger_cv,
791 .get_voltage_sel = max8997_get_voltage_sel,
792 .set_voltage = max8997_set_voltage_charger_cv,
793};
794
795static int max8997_set_current_limit(struct regulator_dev *rdev,
796 int min_uA, int max_uA)
797{
798 unsigned dummy;
799 int rid = rdev_get_id(rdev);
800
801 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
802 return -EINVAL;
803
804
805 return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy);
806}
807
808static int max8997_get_current_limit(struct regulator_dev *rdev)
809{
810 int sel, rid = rdev_get_id(rdev);
811
812 if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF)
813 return -EINVAL;
814
815 sel = max8997_get_voltage_sel(rdev);
816 if (sel < 0)
817 return sel;
818
819
820 return max8997_list_voltage(rdev, sel);
821}
822
823static struct regulator_ops max8997_charger_ops = {
824 .is_enabled = max8997_reg_is_enabled,
825 .enable = max8997_reg_enable,
826 .disable = max8997_reg_disable,
827 .get_current_limit = max8997_get_current_limit,
828 .set_current_limit = max8997_set_current_limit,
829};
830
831static struct regulator_ops max8997_charger_fixedstate_ops = {
832 .get_current_limit = max8997_get_current_limit,
833 .set_current_limit = max8997_set_current_limit,
834};
835
836#define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
837 .name = #_name, \
838 .id = MAX8997_##_name, \
839 .ops = &_ops, \
840 .type = REGULATOR_VOLTAGE, \
841 .owner = THIS_MODULE, \
842}
843
844#define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
845 .name = #_name, \
846 .id = MAX8997_##_name, \
847 .ops = &_ops, \
848 .type = REGULATOR_CURRENT, \
849 .owner = THIS_MODULE, \
850}
851
852static struct regulator_desc regulators[] = {
853 MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops),
854 MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops),
855 MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops),
856 MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops),
857 MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops),
858 MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops),
859 MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops),
860 MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops),
861 MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops),
862 MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops),
863 MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops),
864 MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops),
865 MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops),
866 MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops),
867 MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops),
868 MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops),
869 MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops),
870 MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops),
871 MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops),
872 MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops),
873 MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops),
874 MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops),
875 MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops),
876 MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops),
877 MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops),
878 MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops),
879 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops),
880 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops),
881 MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops),
882 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops),
883 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops),
884 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops),
885 MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops),
886 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF,
887 max8997_charger_fixedstate_ops),
888};
889
890#ifdef CONFIG_OF
891static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device *pdev,
892 struct max8997_platform_data *pdata,
893 struct device_node *pmic_np)
894{
895 int i, gpio;
896
897 for (i = 0; i < 3; i++) {
898 gpio = of_get_named_gpio(pmic_np,
899 "max8997,pmic-buck125-dvs-gpios", i);
900 if (!gpio_is_valid(gpio)) {
901 dev_err(&pdev->dev, "invalid gpio[%d]: %d\n", i, gpio);
902 return -EINVAL;
903 }
904 pdata->buck125_gpios[i] = gpio;
905 }
906 return 0;
907}
908
909static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev,
910 struct max8997_platform_data *pdata)
911{
912 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
913 struct device_node *pmic_np, *regulators_np, *reg_np;
914 struct max8997_regulator_data *rdata;
915 unsigned int i, dvs_voltage_nr = 1, ret;
916
917 pmic_np = iodev->dev->of_node;
918 if (!pmic_np) {
919 dev_err(&pdev->dev, "could not find pmic sub-node\n");
920 return -ENODEV;
921 }
922
923 regulators_np = of_get_child_by_name(pmic_np, "regulators");
924 if (!regulators_np) {
925 dev_err(&pdev->dev, "could not find regulators sub-node\n");
926 return -EINVAL;
927 }
928
929
930 pdata->num_regulators = of_get_child_count(regulators_np);
931
932 rdata = devm_kcalloc(&pdev->dev,
933 pdata->num_regulators, sizeof(*rdata),
934 GFP_KERNEL);
935 if (!rdata) {
936 of_node_put(regulators_np);
937 return -ENOMEM;
938 }
939
940 pdata->regulators = rdata;
941 for_each_child_of_node(regulators_np, reg_np) {
942 for (i = 0; i < ARRAY_SIZE(regulators); i++)
943 if (!of_node_cmp(reg_np->name, regulators[i].name))
944 break;
945
946 if (i == ARRAY_SIZE(regulators)) {
947 dev_warn(&pdev->dev, "don't know how to configure regulator %s\n",
948 reg_np->name);
949 continue;
950 }
951
952 rdata->id = i;
953 rdata->initdata = of_get_regulator_init_data(&pdev->dev,
954 reg_np,
955 ®ulators[i]);
956 rdata->reg_node = reg_np;
957 rdata++;
958 }
959 of_node_put(regulators_np);
960
961 if (of_get_property(pmic_np, "max8997,pmic-buck1-uses-gpio-dvs", NULL))
962 pdata->buck1_gpiodvs = true;
963
964 if (of_get_property(pmic_np, "max8997,pmic-buck2-uses-gpio-dvs", NULL))
965 pdata->buck2_gpiodvs = true;
966
967 if (of_get_property(pmic_np, "max8997,pmic-buck5-uses-gpio-dvs", NULL))
968 pdata->buck5_gpiodvs = true;
969
970 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
971 pdata->buck5_gpiodvs) {
972 ret = max8997_pmic_dt_parse_dvs_gpio(pdev, pdata, pmic_np);
973 if (ret)
974 return -EINVAL;
975
976 if (of_property_read_u32(pmic_np,
977 "max8997,pmic-buck125-default-dvs-idx",
978 &pdata->buck125_default_idx)) {
979 pdata->buck125_default_idx = 0;
980 } else {
981 if (pdata->buck125_default_idx >= 8) {
982 pdata->buck125_default_idx = 0;
983 dev_info(&pdev->dev, "invalid value for default dvs index, using 0 instead\n");
984 }
985 }
986
987 if (of_get_property(pmic_np,
988 "max8997,pmic-ignore-gpiodvs-side-effect", NULL))
989 pdata->ignore_gpiodvs_side_effect = true;
990
991 dvs_voltage_nr = 8;
992 }
993
994 if (of_property_read_u32_array(pmic_np,
995 "max8997,pmic-buck1-dvs-voltage",
996 pdata->buck1_voltage, dvs_voltage_nr)) {
997 dev_err(&pdev->dev, "buck1 voltages not specified\n");
998 return -EINVAL;
999 }
1000
1001 if (of_property_read_u32_array(pmic_np,
1002 "max8997,pmic-buck2-dvs-voltage",
1003 pdata->buck2_voltage, dvs_voltage_nr)) {
1004 dev_err(&pdev->dev, "buck2 voltages not specified\n");
1005 return -EINVAL;
1006 }
1007
1008 if (of_property_read_u32_array(pmic_np,
1009 "max8997,pmic-buck5-dvs-voltage",
1010 pdata->buck5_voltage, dvs_voltage_nr)) {
1011 dev_err(&pdev->dev, "buck5 voltages not specified\n");
1012 return -EINVAL;
1013 }
1014
1015 return 0;
1016}
1017#else
1018static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev,
1019 struct max8997_platform_data *pdata)
1020{
1021 return 0;
1022}
1023#endif
1024
1025static int max8997_pmic_probe(struct platform_device *pdev)
1026{
1027 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
1028 struct max8997_platform_data *pdata = iodev->pdata;
1029 struct regulator_config config = { };
1030 struct regulator_dev *rdev;
1031 struct max8997_data *max8997;
1032 struct i2c_client *i2c;
1033 int i, ret, nr_dvs;
1034 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
1035
1036 if (!pdata) {
1037 dev_err(&pdev->dev, "No platform init data supplied.\n");
1038 return -ENODEV;
1039 }
1040
1041 if (iodev->dev->of_node) {
1042 ret = max8997_pmic_dt_parse_pdata(pdev, pdata);
1043 if (ret)
1044 return ret;
1045 }
1046
1047 max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data),
1048 GFP_KERNEL);
1049 if (!max8997)
1050 return -ENOMEM;
1051
1052 max8997->dev = &pdev->dev;
1053 max8997->iodev = iodev;
1054 max8997->num_regulators = pdata->num_regulators;
1055 platform_set_drvdata(pdev, max8997);
1056 i2c = max8997->iodev->i2c;
1057
1058 max8997->buck125_gpioindex = pdata->buck125_default_idx;
1059 max8997->buck1_gpiodvs = pdata->buck1_gpiodvs;
1060 max8997->buck2_gpiodvs = pdata->buck2_gpiodvs;
1061 max8997->buck5_gpiodvs = pdata->buck5_gpiodvs;
1062 memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3);
1063 max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect;
1064
1065 nr_dvs = (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1066 pdata->buck5_gpiodvs) ? 8 : 1;
1067
1068 for (i = 0; i < nr_dvs; i++) {
1069 max8997->buck1_vol[i] = ret =
1070 max8997_get_voltage_proper_val(
1071 &buck1245_voltage_map_desc,
1072 pdata->buck1_voltage[i],
1073 pdata->buck1_voltage[i] +
1074 buck1245_voltage_map_desc.step);
1075 if (ret < 0)
1076 return ret;
1077
1078 max8997->buck2_vol[i] = ret =
1079 max8997_get_voltage_proper_val(
1080 &buck1245_voltage_map_desc,
1081 pdata->buck2_voltage[i],
1082 pdata->buck2_voltage[i] +
1083 buck1245_voltage_map_desc.step);
1084 if (ret < 0)
1085 return ret;
1086
1087 max8997->buck5_vol[i] = ret =
1088 max8997_get_voltage_proper_val(
1089 &buck1245_voltage_map_desc,
1090 pdata->buck5_voltage[i],
1091 pdata->buck5_voltage[i] +
1092 buck1245_voltage_map_desc.step);
1093 if (ret < 0)
1094 return ret;
1095
1096 if (max_buck1 < max8997->buck1_vol[i])
1097 max_buck1 = max8997->buck1_vol[i];
1098 if (max_buck2 < max8997->buck2_vol[i])
1099 max_buck2 = max8997->buck2_vol[i];
1100 if (max_buck5 < max8997->buck5_vol[i])
1101 max_buck5 = max8997->buck5_vol[i];
1102 }
1103
1104
1105 for (i = 0; i < 8; i++) {
1106 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1107 max_buck1, 0x3f);
1108 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1109 max_buck2, 0x3f);
1110 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1111 max_buck5, 0x3f);
1112 }
1113
1114
1115 for (i = 0; i < nr_dvs; i++) {
1116 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1117 max8997->buck1_vol[i],
1118 0x3f);
1119 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1120 max8997->buck2_vol[i],
1121 0x3f);
1122 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1123 max8997->buck5_vol[i],
1124 0x3f);
1125 }
1126
1127
1128
1129
1130
1131 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1132 pdata->buck5_gpiodvs) {
1133
1134 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1135 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1136 !gpio_is_valid(pdata->buck125_gpios[2])) {
1137 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1138 return -EINVAL;
1139 }
1140
1141 ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0],
1142 "MAX8997 SET1");
1143 if (ret)
1144 return ret;
1145
1146 ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1],
1147 "MAX8997 SET2");
1148 if (ret)
1149 return ret;
1150
1151 ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2],
1152 "MAX8997 SET3");
1153 if (ret)
1154 return ret;
1155
1156 gpio_direction_output(pdata->buck125_gpios[0],
1157 (max8997->buck125_gpioindex >> 2)
1158 & 0x1);
1159 gpio_direction_output(pdata->buck125_gpios[1],
1160 (max8997->buck125_gpioindex >> 1)
1161 & 0x1);
1162 gpio_direction_output(pdata->buck125_gpios[2],
1163 (max8997->buck125_gpioindex >> 0)
1164 & 0x1);
1165 }
1166
1167
1168 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1169 (1 << 1) : (0 << 1), 1 << 1);
1170 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1171 (1 << 1) : (0 << 1), 1 << 1);
1172 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1173 (1 << 1) : (0 << 1), 1 << 1);
1174
1175
1176 max8997->ramp_delay = 10;
1177 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1178
1179 for (i = 0; i < pdata->num_regulators; i++) {
1180 const struct voltage_map_desc *desc;
1181 int id = pdata->regulators[i].id;
1182
1183 desc = reg_voltage_map[id];
1184 if (desc) {
1185 regulators[id].n_voltages =
1186 (desc->max - desc->min) / desc->step + 1;
1187 } else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2) {
1188 regulators[id].volt_table = safeoutvolt;
1189 regulators[id].n_voltages = ARRAY_SIZE(safeoutvolt);
1190 } else if (id == MAX8997_CHARGER_CV) {
1191 regulators[id].n_voltages = 16;
1192 }
1193
1194 config.dev = max8997->dev;
1195 config.init_data = pdata->regulators[i].initdata;
1196 config.driver_data = max8997;
1197 config.of_node = pdata->regulators[i].reg_node;
1198
1199 rdev = devm_regulator_register(&pdev->dev, ®ulators[id],
1200 &config);
1201 if (IS_ERR(rdev)) {
1202 dev_err(max8997->dev, "regulator init failed for %d\n",
1203 id);
1204 return PTR_ERR(rdev);
1205 }
1206 }
1207
1208 return 0;
1209}
1210
1211static const struct platform_device_id max8997_pmic_id[] = {
1212 { "max8997-pmic", 0},
1213 { },
1214};
1215MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1216
1217static struct platform_driver max8997_pmic_driver = {
1218 .driver = {
1219 .name = "max8997-pmic",
1220 },
1221 .probe = max8997_pmic_probe,
1222 .id_table = max8997_pmic_id,
1223};
1224
1225static int __init max8997_pmic_init(void)
1226{
1227 return platform_driver_register(&max8997_pmic_driver);
1228}
1229subsys_initcall(max8997_pmic_init);
1230
1231static void __exit max8997_pmic_cleanup(void)
1232{
1233 platform_driver_unregister(&max8997_pmic_driver);
1234}
1235module_exit(max8997_pmic_cleanup);
1236
1237MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1238MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1239MODULE_LICENSE("GPL");
1240