1 // SPDX-License-Identifier: GPL-2.0+
3 // max8997.c - Regulator driver for the Maxim 8997/8966
5 // Copyright (C) 2011 Samsung Electronics
6 // MyungJoo Ham <myungjoo.ham@samsung.com>
8 // This driver is based on max8998.c
10 #include <linux/bug.h>
11 #include <linux/cleanup.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/mfd/max8997.h>
20 #include <linux/mfd/max8997-private.h>
21 #include <linux/regulator/of_regulator.h>
25 struct max8997_dev
*iodev
;
27 int ramp_delay
; /* in mV/us */
35 struct gpio_desc
*buck125_gpiods
[3];
36 int buck125_gpioindex
;
37 bool ignore_gpiodvs_side_effect
;
39 u8 saved_states
[MAX8997_REG_MAX
];
42 static const unsigned int safeoutvolt
[] = {
49 static inline void max8997_set_gpio(struct max8997_data
*max8997
)
51 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
52 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
53 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
55 gpiod_set_value(max8997
->buck125_gpiods
[0], set1
);
56 gpiod_set_value(max8997
->buck125_gpiods
[1], set2
);
57 gpiod_set_value(max8997
->buck125_gpiods
[2], set3
);
60 struct voltage_map_desc
{
66 /* Voltage maps in uV */
67 static const struct voltage_map_desc ldo_voltage_map_desc
= {
68 .min
= 800000, .max
= 3950000, .step
= 50000,
69 }; /* LDO1 ~ 18, 21 all */
71 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
72 .min
= 650000, .max
= 2225000, .step
= 25000,
73 }; /* Buck1, 2, 4, 5 */
75 static const struct voltage_map_desc buck37_voltage_map_desc
= {
76 .min
= 750000, .max
= 3900000, .step
= 50000,
79 /* current map in uA */
80 static const struct voltage_map_desc charger_current_map_desc
= {
81 .min
= 200000, .max
= 950000, .step
= 50000,
84 static const struct voltage_map_desc topoff_current_map_desc
= {
85 .min
= 50000, .max
= 200000, .step
= 10000,
88 static const struct voltage_map_desc
*reg_voltage_map
[] = {
89 [MAX8997_LDO1
] = &ldo_voltage_map_desc
,
90 [MAX8997_LDO2
] = &ldo_voltage_map_desc
,
91 [MAX8997_LDO3
] = &ldo_voltage_map_desc
,
92 [MAX8997_LDO4
] = &ldo_voltage_map_desc
,
93 [MAX8997_LDO5
] = &ldo_voltage_map_desc
,
94 [MAX8997_LDO6
] = &ldo_voltage_map_desc
,
95 [MAX8997_LDO7
] = &ldo_voltage_map_desc
,
96 [MAX8997_LDO8
] = &ldo_voltage_map_desc
,
97 [MAX8997_LDO9
] = &ldo_voltage_map_desc
,
98 [MAX8997_LDO10
] = &ldo_voltage_map_desc
,
99 [MAX8997_LDO11
] = &ldo_voltage_map_desc
,
100 [MAX8997_LDO12
] = &ldo_voltage_map_desc
,
101 [MAX8997_LDO13
] = &ldo_voltage_map_desc
,
102 [MAX8997_LDO14
] = &ldo_voltage_map_desc
,
103 [MAX8997_LDO15
] = &ldo_voltage_map_desc
,
104 [MAX8997_LDO16
] = &ldo_voltage_map_desc
,
105 [MAX8997_LDO17
] = &ldo_voltage_map_desc
,
106 [MAX8997_LDO18
] = &ldo_voltage_map_desc
,
107 [MAX8997_LDO21
] = &ldo_voltage_map_desc
,
108 [MAX8997_BUCK1
] = &buck1245_voltage_map_desc
,
109 [MAX8997_BUCK2
] = &buck1245_voltage_map_desc
,
110 [MAX8997_BUCK3
] = &buck37_voltage_map_desc
,
111 [MAX8997_BUCK4
] = &buck1245_voltage_map_desc
,
112 [MAX8997_BUCK5
] = &buck1245_voltage_map_desc
,
113 [MAX8997_BUCK6
] = NULL
,
114 [MAX8997_BUCK7
] = &buck37_voltage_map_desc
,
115 [MAX8997_EN32KHZ_AP
] = NULL
,
116 [MAX8997_EN32KHZ_CP
] = NULL
,
117 [MAX8997_ENVICHG
] = NULL
,
118 [MAX8997_ESAFEOUT1
] = NULL
,
119 [MAX8997_ESAFEOUT2
] = NULL
,
120 [MAX8997_CHARGER_CV
] = NULL
,
121 [MAX8997_CHARGER
] = &charger_current_map_desc
,
122 [MAX8997_CHARGER_TOPOFF
] = &topoff_current_map_desc
,
125 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
126 unsigned int selector
)
128 int rid
= rdev_get_id(rdev
);
130 if (rid
!= MAX8997_CHARGER_CV
)
137 return 4000000 + 20000 * (selector
- 0x01);
147 static int max8997_list_voltage(struct regulator_dev
*rdev
,
148 unsigned int selector
)
150 const struct voltage_map_desc
*desc
;
151 int rid
= rdev_get_id(rdev
);
154 if (rid
< 0 || rid
>= ARRAY_SIZE(reg_voltage_map
))
157 desc
= reg_voltage_map
[rid
];
161 val
= desc
->min
+ desc
->step
* selector
;
168 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
169 int *reg
, int *mask
, int *pattern
)
171 int rid
= rdev_get_id(rdev
);
174 case MAX8997_LDO1
... MAX8997_LDO21
:
175 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
180 *reg
= MAX8997_REG_BUCK1CTRL
;
185 *reg
= MAX8997_REG_BUCK2CTRL
;
190 *reg
= MAX8997_REG_BUCK3CTRL
;
195 *reg
= MAX8997_REG_BUCK4CTRL
;
200 *reg
= MAX8997_REG_BUCK5CTRL
;
205 *reg
= MAX8997_REG_BUCK6CTRL
;
210 *reg
= MAX8997_REG_BUCK7CTRL
;
214 case MAX8997_EN32KHZ_AP
... MAX8997_EN32KHZ_CP
:
215 *reg
= MAX8997_REG_MAINCON1
;
216 *mask
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
217 *pattern
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
219 case MAX8997_ENVICHG
:
220 *reg
= MAX8997_REG_MBCCTRL1
;
224 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
225 *reg
= MAX8997_REG_SAFEOUTCTRL
;
226 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
227 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
229 case MAX8997_CHARGER
:
230 *reg
= MAX8997_REG_MBCCTRL2
;
235 /* Not controllable or not exists */
242 static int max8997_reg_is_enabled(struct regulator_dev
*rdev
)
244 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
245 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
246 int ret
, reg
, mask
, pattern
;
249 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
253 ret
= max8997_read_reg(i2c
, reg
, &val
);
257 return (val
& mask
) == pattern
;
260 static int max8997_reg_enable(struct regulator_dev
*rdev
)
262 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
263 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
264 int ret
, reg
, mask
, pattern
;
266 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
270 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
273 static int max8997_reg_disable(struct regulator_dev
*rdev
)
275 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
276 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
277 int ret
, reg
, mask
, pattern
;
279 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
283 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
286 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
287 int *_reg
, int *_shift
, int *_mask
)
289 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
290 int rid
= rdev_get_id(rdev
);
291 int reg
, shift
= 0, mask
= 0x3f;
294 case MAX8997_LDO1
... MAX8997_LDO21
:
295 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
298 reg
= MAX8997_REG_BUCK1DVS1
;
299 if (max8997
->buck1_gpiodvs
)
300 reg
+= max8997
->buck125_gpioindex
;
303 reg
= MAX8997_REG_BUCK2DVS1
;
304 if (max8997
->buck2_gpiodvs
)
305 reg
+= max8997
->buck125_gpioindex
;
308 reg
= MAX8997_REG_BUCK3DVS
;
311 reg
= MAX8997_REG_BUCK4DVS
;
314 reg
= MAX8997_REG_BUCK5DVS1
;
315 if (max8997
->buck5_gpiodvs
)
316 reg
+= max8997
->buck125_gpioindex
;
319 reg
= MAX8997_REG_BUCK7DVS
;
321 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
322 reg
= MAX8997_REG_SAFEOUTCTRL
;
323 shift
= (rid
== MAX8997_ESAFEOUT2
) ? 2 : 0;
326 case MAX8997_CHARGER_CV
:
327 reg
= MAX8997_REG_MBCCTRL3
;
331 case MAX8997_CHARGER
:
332 reg
= MAX8997_REG_MBCCTRL4
;
336 case MAX8997_CHARGER_TOPOFF
:
337 reg
= MAX8997_REG_MBCCTRL5
;
352 static int max8997_get_voltage_sel(struct regulator_dev
*rdev
)
354 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
355 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
356 int reg
, shift
, mask
, ret
;
359 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
363 ret
= max8997_read_reg(i2c
, reg
, &val
);
373 static inline int max8997_get_voltage_proper_val(
374 const struct voltage_map_desc
*desc
,
375 int min_vol
, int max_vol
)
382 if (max_vol
< desc
->min
|| min_vol
> desc
->max
)
385 if (min_vol
< desc
->min
)
388 i
= DIV_ROUND_UP(min_vol
- desc
->min
, desc
->step
);
390 if (desc
->min
+ desc
->step
* i
> max_vol
)
396 static int max8997_set_voltage_charger_cv(struct regulator_dev
*rdev
,
397 int min_uV
, int max_uV
, unsigned *selector
)
399 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
400 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
401 int rid
= rdev_get_id(rdev
);
403 int reg
, shift
= 0, mask
, ret
= 0;
406 if (rid
!= MAX8997_CHARGER_CV
)
409 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
413 if (max_uV
< 4000000 || min_uV
> 4350000)
416 if (min_uV
<= 4000000)
418 else if (min_uV
<= 4200000 && max_uV
>= 4200000)
421 lb
= (min_uV
- 4000001) / 20000 + 2;
422 ub
= (max_uV
- 4000000) / 20000 + 1;
439 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
);
445 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
446 * BUCK1, 2, and 5 are available if they are not controlled by gpio
448 static int max8997_set_voltage_ldobuck(struct regulator_dev
*rdev
,
449 int min_uV
, int max_uV
, unsigned *selector
)
451 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
452 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
453 const struct voltage_map_desc
*desc
;
454 int rid
= rdev_get_id(rdev
);
455 int i
, reg
, shift
, mask
, ret
;
458 case MAX8997_LDO1
... MAX8997_LDO21
:
460 case MAX8997_BUCK1
... MAX8997_BUCK5
:
466 case MAX8997_CHARGER
:
468 case MAX8997_CHARGER_TOPOFF
:
474 desc
= reg_voltage_map
[rid
];
476 i
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
480 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
484 ret
= max8997_update_reg(i2c
, reg
, i
<< shift
, mask
<< shift
);
490 static int max8997_set_voltage_buck_time_sel(struct regulator_dev
*rdev
,
491 unsigned int old_selector
,
492 unsigned int new_selector
)
494 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
495 int rid
= rdev_get_id(rdev
);
496 const struct voltage_map_desc
*desc
= reg_voltage_map
[rid
];
498 /* Delay is required only if the voltage is increasing */
499 if (old_selector
>= new_selector
)
502 /* No need to delay if gpio_dvs_mode */
505 if (max8997
->buck1_gpiodvs
)
509 if (max8997
->buck2_gpiodvs
)
513 if (max8997
->buck5_gpiodvs
)
523 return DIV_ROUND_UP(desc
->step
* (new_selector
- old_selector
),
524 max8997
->ramp_delay
* 1000);
531 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
533 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
534 * of one of the bucks may affect that of another buck, which is the side
535 * effect of the change (set_voltage). This function examines the GPIO-DVS
536 * configurations and checks whether such side-effect exists.
538 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
539 u8 new_val
, int *best
)
541 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
542 int rid
= rdev_get_id(rdev
);
544 bool buckx_gpiodvs
[3];
546 int min_side_effect
= INT_MAX
;
565 buckx_val
[0] = max8997
->buck1_vol
;
566 buckx_val
[1] = max8997
->buck2_vol
;
567 buckx_val
[2] = max8997
->buck5_vol
;
568 buckx_gpiodvs
[0] = max8997
->buck1_gpiodvs
;
569 buckx_gpiodvs
[1] = max8997
->buck2_gpiodvs
;
570 buckx_gpiodvs
[2] = max8997
->buck5_gpiodvs
;
572 for (i
= 0; i
< 8; i
++) {
575 if (new_val
!= (buckx_val
[rid
])[i
]) {
581 for (others
= 0; others
< 3; others
++) {
586 if (buckx_gpiodvs
[others
] == false)
587 continue; /* Not affected */
588 diff
= (buckx_val
[others
])[i
] -
589 (buckx_val
[others
])[max8997
->buck125_gpioindex
];
591 side_effect
[i
] += diff
;
593 side_effect
[i
] -= diff
;
595 if (side_effect
[i
] == 0) {
597 return 0; /* NO SIDE EFFECT! Use This! */
599 if (side_effect
[i
] < min_side_effect
) {
600 min_side_effect
= side_effect
[i
];
608 return side_effect
[*best
];
612 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
613 * max8997_set_voltage_ldobuck to do the job.
615 static int max8997_set_voltage_buck(struct regulator_dev
*rdev
,
616 int min_uV
, int max_uV
, unsigned *selector
)
618 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
619 int rid
= rdev_get_id(rdev
);
620 const struct voltage_map_desc
*desc
;
621 int new_val
, new_idx
, damage
, tmp_val
, tmp_idx
, tmp_dmg
;
622 bool gpio_dvs_mode
= false;
624 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
629 if (max8997
->buck1_gpiodvs
)
630 gpio_dvs_mode
= true;
633 if (max8997
->buck2_gpiodvs
)
634 gpio_dvs_mode
= true;
637 if (max8997
->buck5_gpiodvs
)
638 gpio_dvs_mode
= true;
643 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
646 desc
= reg_voltage_map
[rid
];
647 new_val
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
655 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
659 if (tmp_dmg
> damage
) {
666 } while (desc
->min
+ desc
->step
* new_val
<= desc
->max
);
671 if (max8997
->ignore_gpiodvs_side_effect
== false)
675 "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET: %d -> %d\n",
676 max8997
->buck125_gpioindex
, tmp_idx
);
679 if (new_idx
< 0 || new_val
< 0)
682 max8997
->buck125_gpioindex
= new_idx
;
683 max8997_set_gpio(max8997
);
689 /* For SAFEOUT1 and SAFEOUT2 */
690 static int max8997_set_voltage_safeout_sel(struct regulator_dev
*rdev
,
693 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
694 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
695 int rid
= rdev_get_id(rdev
);
696 int reg
, shift
= 0, mask
, ret
;
698 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
701 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
705 return max8997_update_reg(i2c
, reg
, selector
<< shift
, mask
<< shift
);
708 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
710 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
711 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
712 int ret
, reg
, mask
, pattern
;
713 int rid
= rdev_get_id(rdev
);
715 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
719 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
721 if (rid
== MAX8997_LDO1
||
722 rid
== MAX8997_LDO10
||
723 rid
== MAX8997_LDO21
) {
724 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
726 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
729 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
730 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
732 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
735 static const struct regulator_ops max8997_ldo_ops
= {
736 .list_voltage
= max8997_list_voltage
,
737 .is_enabled
= max8997_reg_is_enabled
,
738 .enable
= max8997_reg_enable
,
739 .disable
= max8997_reg_disable
,
740 .get_voltage_sel
= max8997_get_voltage_sel
,
741 .set_voltage
= max8997_set_voltage_ldobuck
,
742 .set_suspend_disable
= max8997_reg_disable_suspend
,
745 static const struct regulator_ops max8997_buck_ops
= {
746 .list_voltage
= max8997_list_voltage
,
747 .is_enabled
= max8997_reg_is_enabled
,
748 .enable
= max8997_reg_enable
,
749 .disable
= max8997_reg_disable
,
750 .get_voltage_sel
= max8997_get_voltage_sel
,
751 .set_voltage
= max8997_set_voltage_buck
,
752 .set_voltage_time_sel
= max8997_set_voltage_buck_time_sel
,
753 .set_suspend_disable
= max8997_reg_disable_suspend
,
756 static const struct regulator_ops max8997_fixedvolt_ops
= {
757 .list_voltage
= max8997_list_voltage
,
758 .is_enabled
= max8997_reg_is_enabled
,
759 .enable
= max8997_reg_enable
,
760 .disable
= max8997_reg_disable
,
761 .set_suspend_disable
= max8997_reg_disable_suspend
,
764 static const struct regulator_ops max8997_safeout_ops
= {
765 .list_voltage
= regulator_list_voltage_table
,
766 .is_enabled
= max8997_reg_is_enabled
,
767 .enable
= max8997_reg_enable
,
768 .disable
= max8997_reg_disable
,
769 .get_voltage_sel
= max8997_get_voltage_sel
,
770 .set_voltage_sel
= max8997_set_voltage_safeout_sel
,
771 .set_suspend_disable
= max8997_reg_disable_suspend
,
774 static const struct regulator_ops max8997_fixedstate_ops
= {
775 .list_voltage
= max8997_list_voltage_charger_cv
,
776 .get_voltage_sel
= max8997_get_voltage_sel
,
777 .set_voltage
= max8997_set_voltage_charger_cv
,
780 static int max8997_set_current_limit(struct regulator_dev
*rdev
,
781 int min_uA
, int max_uA
)
784 int rid
= rdev_get_id(rdev
);
786 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
789 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
790 return max8997_set_voltage_ldobuck(rdev
, min_uA
, max_uA
, &dummy
);
793 static int max8997_get_current_limit(struct regulator_dev
*rdev
)
795 int sel
, rid
= rdev_get_id(rdev
);
797 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
800 sel
= max8997_get_voltage_sel(rdev
);
804 /* Reuse max8997_list_voltage to get current_limit. */
805 return max8997_list_voltage(rdev
, sel
);
808 static const struct regulator_ops max8997_charger_ops
= {
809 .is_enabled
= max8997_reg_is_enabled
,
810 .enable
= max8997_reg_enable
,
811 .disable
= max8997_reg_disable
,
812 .get_current_limit
= max8997_get_current_limit
,
813 .set_current_limit
= max8997_set_current_limit
,
816 static const struct regulator_ops max8997_charger_fixedstate_ops
= {
817 .get_current_limit
= max8997_get_current_limit
,
818 .set_current_limit
= max8997_set_current_limit
,
821 #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
823 .id = MAX8997_##_name, \
825 .type = REGULATOR_VOLTAGE, \
826 .owner = THIS_MODULE, \
829 #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
831 .id = MAX8997_##_name, \
833 .type = REGULATOR_CURRENT, \
834 .owner = THIS_MODULE, \
837 static struct regulator_desc regulators
[] = {
838 MAX8997_VOLTAGE_REGULATOR(LDO1
, max8997_ldo_ops
),
839 MAX8997_VOLTAGE_REGULATOR(LDO2
, max8997_ldo_ops
),
840 MAX8997_VOLTAGE_REGULATOR(LDO3
, max8997_ldo_ops
),
841 MAX8997_VOLTAGE_REGULATOR(LDO4
, max8997_ldo_ops
),
842 MAX8997_VOLTAGE_REGULATOR(LDO5
, max8997_ldo_ops
),
843 MAX8997_VOLTAGE_REGULATOR(LDO6
, max8997_ldo_ops
),
844 MAX8997_VOLTAGE_REGULATOR(LDO7
, max8997_ldo_ops
),
845 MAX8997_VOLTAGE_REGULATOR(LDO8
, max8997_ldo_ops
),
846 MAX8997_VOLTAGE_REGULATOR(LDO9
, max8997_ldo_ops
),
847 MAX8997_VOLTAGE_REGULATOR(LDO10
, max8997_ldo_ops
),
848 MAX8997_VOLTAGE_REGULATOR(LDO11
, max8997_ldo_ops
),
849 MAX8997_VOLTAGE_REGULATOR(LDO12
, max8997_ldo_ops
),
850 MAX8997_VOLTAGE_REGULATOR(LDO13
, max8997_ldo_ops
),
851 MAX8997_VOLTAGE_REGULATOR(LDO14
, max8997_ldo_ops
),
852 MAX8997_VOLTAGE_REGULATOR(LDO15
, max8997_ldo_ops
),
853 MAX8997_VOLTAGE_REGULATOR(LDO16
, max8997_ldo_ops
),
854 MAX8997_VOLTAGE_REGULATOR(LDO17
, max8997_ldo_ops
),
855 MAX8997_VOLTAGE_REGULATOR(LDO18
, max8997_ldo_ops
),
856 MAX8997_VOLTAGE_REGULATOR(LDO21
, max8997_ldo_ops
),
857 MAX8997_VOLTAGE_REGULATOR(BUCK1
, max8997_buck_ops
),
858 MAX8997_VOLTAGE_REGULATOR(BUCK2
, max8997_buck_ops
),
859 MAX8997_VOLTAGE_REGULATOR(BUCK3
, max8997_buck_ops
),
860 MAX8997_VOLTAGE_REGULATOR(BUCK4
, max8997_buck_ops
),
861 MAX8997_VOLTAGE_REGULATOR(BUCK5
, max8997_buck_ops
),
862 MAX8997_VOLTAGE_REGULATOR(BUCK6
, max8997_fixedvolt_ops
),
863 MAX8997_VOLTAGE_REGULATOR(BUCK7
, max8997_buck_ops
),
864 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP
, max8997_fixedvolt_ops
),
865 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP
, max8997_fixedvolt_ops
),
866 MAX8997_VOLTAGE_REGULATOR(ENVICHG
, max8997_fixedvolt_ops
),
867 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1
, max8997_safeout_ops
),
868 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2
, max8997_safeout_ops
),
869 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV
, max8997_fixedstate_ops
),
870 MAX8997_CURRENT_REGULATOR(CHARGER
, max8997_charger_ops
),
871 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF
,
872 max8997_charger_fixedstate_ops
),
876 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
877 struct max8997_platform_data
*pdata
)
879 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
880 struct device_node
*pmic_np
, *reg_np
;
881 struct max8997_regulator_data
*rdata
;
882 unsigned int i
, dvs_voltage_nr
= 1;
884 pmic_np
= iodev
->dev
->of_node
;
886 dev_err(&pdev
->dev
, "could not find pmic sub-node\n");
890 struct device_node
*regulators_np
__free(device_node
) = of_get_child_by_name(pmic_np
,
892 if (!regulators_np
) {
893 dev_err(&pdev
->dev
, "could not find regulators sub-node\n");
897 /* count the number of regulators to be supported in pmic */
898 pdata
->num_regulators
= of_get_child_count(regulators_np
);
900 rdata
= devm_kcalloc(&pdev
->dev
,
901 pdata
->num_regulators
, sizeof(*rdata
),
906 pdata
->regulators
= rdata
;
907 for_each_child_of_node(regulators_np
, reg_np
) {
908 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++)
909 if (of_node_name_eq(reg_np
, regulators
[i
].name
))
912 if (i
== ARRAY_SIZE(regulators
)) {
913 dev_warn(&pdev
->dev
, "don't know how to configure regulator %pOFn\n",
919 rdata
->initdata
= of_get_regulator_init_data(&pdev
->dev
,
922 rdata
->reg_node
= reg_np
;
926 pdata
->buck1_gpiodvs
= of_property_read_bool(pmic_np
, "max8997,pmic-buck1-uses-gpio-dvs");
927 pdata
->buck2_gpiodvs
= of_property_read_bool(pmic_np
, "max8997,pmic-buck2-uses-gpio-dvs");
928 pdata
->buck5_gpiodvs
= of_property_read_bool(pmic_np
, "max8997,pmic-buck5-uses-gpio-dvs");
930 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
931 pdata
->buck5_gpiodvs
) {
932 if (of_property_read_u32(pmic_np
,
933 "max8997,pmic-buck125-default-dvs-idx",
934 &pdata
->buck125_default_idx
)) {
935 pdata
->buck125_default_idx
= 0;
937 if (pdata
->buck125_default_idx
>= 8) {
938 pdata
->buck125_default_idx
= 0;
939 dev_info(&pdev
->dev
, "invalid value for default dvs index, using 0 instead\n");
943 pdata
->ignore_gpiodvs_side_effect
= of_property_read_bool(pmic_np
,
944 "max8997,pmic-ignore-gpiodvs-side-effect");
949 if (of_property_read_u32_array(pmic_np
,
950 "max8997,pmic-buck1-dvs-voltage",
951 pdata
->buck1_voltage
, dvs_voltage_nr
)) {
952 dev_err(&pdev
->dev
, "buck1 voltages not specified\n");
956 if (of_property_read_u32_array(pmic_np
,
957 "max8997,pmic-buck2-dvs-voltage",
958 pdata
->buck2_voltage
, dvs_voltage_nr
)) {
959 dev_err(&pdev
->dev
, "buck2 voltages not specified\n");
963 if (of_property_read_u32_array(pmic_np
,
964 "max8997,pmic-buck5-dvs-voltage",
965 pdata
->buck5_voltage
, dvs_voltage_nr
)) {
966 dev_err(&pdev
->dev
, "buck5 voltages not specified\n");
973 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
974 struct max8997_platform_data
*pdata
)
978 #endif /* CONFIG_OF */
980 static int max8997_pmic_probe(struct platform_device
*pdev
)
982 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
983 struct max8997_platform_data
*pdata
= iodev
->pdata
;
984 struct regulator_config config
= { };
985 struct regulator_dev
*rdev
;
986 struct max8997_data
*max8997
;
987 struct i2c_client
*i2c
;
989 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
992 dev_err(&pdev
->dev
, "No platform init data supplied.\n");
996 if (iodev
->dev
->of_node
) {
997 ret
= max8997_pmic_dt_parse_pdata(pdev
, pdata
);
1002 max8997
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_data
),
1007 max8997
->dev
= &pdev
->dev
;
1008 max8997
->iodev
= iodev
;
1009 max8997
->num_regulators
= pdata
->num_regulators
;
1010 platform_set_drvdata(pdev
, max8997
);
1011 i2c
= max8997
->iodev
->i2c
;
1013 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
1014 max8997
->buck1_gpiodvs
= pdata
->buck1_gpiodvs
;
1015 max8997
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
1016 max8997
->buck5_gpiodvs
= pdata
->buck5_gpiodvs
;
1017 max8997
->ignore_gpiodvs_side_effect
= pdata
->ignore_gpiodvs_side_effect
;
1019 nr_dvs
= (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1020 pdata
->buck5_gpiodvs
) ? 8 : 1;
1022 for (i
= 0; i
< nr_dvs
; i
++) {
1023 max8997
->buck1_vol
[i
] = ret
=
1024 max8997_get_voltage_proper_val(
1025 &buck1245_voltage_map_desc
,
1026 pdata
->buck1_voltage
[i
],
1027 pdata
->buck1_voltage
[i
] +
1028 buck1245_voltage_map_desc
.step
);
1032 max8997
->buck2_vol
[i
] = ret
=
1033 max8997_get_voltage_proper_val(
1034 &buck1245_voltage_map_desc
,
1035 pdata
->buck2_voltage
[i
],
1036 pdata
->buck2_voltage
[i
] +
1037 buck1245_voltage_map_desc
.step
);
1041 max8997
->buck5_vol
[i
] = ret
=
1042 max8997_get_voltage_proper_val(
1043 &buck1245_voltage_map_desc
,
1044 pdata
->buck5_voltage
[i
],
1045 pdata
->buck5_voltage
[i
] +
1046 buck1245_voltage_map_desc
.step
);
1050 if (max_buck1
< max8997
->buck1_vol
[i
])
1051 max_buck1
= max8997
->buck1_vol
[i
];
1052 if (max_buck2
< max8997
->buck2_vol
[i
])
1053 max_buck2
= max8997
->buck2_vol
[i
];
1054 if (max_buck5
< max8997
->buck5_vol
[i
])
1055 max_buck5
= max8997
->buck5_vol
[i
];
1058 /* For the safety, set max voltage before setting up */
1059 for (i
= 0; i
< 8; i
++) {
1060 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1062 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1064 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1068 /* Initialize all the DVS related BUCK registers */
1069 for (i
= 0; i
< nr_dvs
; i
++) {
1070 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1071 max8997
->buck1_vol
[i
],
1073 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1074 max8997
->buck2_vol
[i
],
1076 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1077 max8997
->buck5_vol
[i
],
1082 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1083 * If at least one of them cares, set gpios.
1085 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1086 pdata
->buck5_gpiodvs
) {
1087 const char *gpio_names
[3] = {"MAX8997 SET1", "MAX8997 SET2", "MAX8997 SET3"};
1089 for (i
= 0; i
< 3; i
++) {
1090 enum gpiod_flags flags
;
1092 if (max8997
->buck125_gpioindex
& BIT(2 - i
))
1093 flags
= GPIOD_OUT_HIGH
;
1095 flags
= GPIOD_OUT_LOW
;
1097 max8997
->buck125_gpiods
[i
] = devm_gpiod_get_index(iodev
->dev
,
1098 "max8997,pmic-buck125-dvs",
1101 if (IS_ERR(max8997
->buck125_gpiods
[i
])) {
1102 ret
= PTR_ERR(max8997
->buck125_gpiods
[i
]);
1103 return dev_err_probe(iodev
->dev
, ret
, "cant get GPIO %d (%d)\n",
1106 gpiod_set_consumer_name(max8997
->buck125_gpiods
[i
], gpio_names
[i
]);
1110 /* DVS-GPIO disabled */
1111 max8997_update_reg(i2c
, MAX8997_REG_BUCK1CTRL
, (pdata
->buck1_gpiodvs
) ?
1112 (1 << 1) : (0 << 1), 1 << 1);
1113 max8997_update_reg(i2c
, MAX8997_REG_BUCK2CTRL
, (pdata
->buck2_gpiodvs
) ?
1114 (1 << 1) : (0 << 1), 1 << 1);
1115 max8997_update_reg(i2c
, MAX8997_REG_BUCK5CTRL
, (pdata
->buck5_gpiodvs
) ?
1116 (1 << 1) : (0 << 1), 1 << 1);
1119 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1120 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1122 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1123 const struct voltage_map_desc
*desc
;
1124 int id
= pdata
->regulators
[i
].id
;
1126 desc
= reg_voltage_map
[id
];
1128 regulators
[id
].n_voltages
=
1129 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1130 } else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
) {
1131 regulators
[id
].volt_table
= safeoutvolt
;
1132 regulators
[id
].n_voltages
= ARRAY_SIZE(safeoutvolt
);
1133 } else if (id
== MAX8997_CHARGER_CV
) {
1134 regulators
[id
].n_voltages
= 16;
1137 config
.dev
= max8997
->dev
;
1138 config
.init_data
= pdata
->regulators
[i
].initdata
;
1139 config
.driver_data
= max8997
;
1140 config
.of_node
= pdata
->regulators
[i
].reg_node
;
1142 rdev
= devm_regulator_register(&pdev
->dev
, ®ulators
[id
],
1145 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1147 return PTR_ERR(rdev
);
1154 static const struct platform_device_id max8997_pmic_id
[] = {
1155 { "max8997-pmic", 0},
1158 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1160 static struct platform_driver max8997_pmic_driver
= {
1162 .name
= "max8997-pmic",
1163 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1165 .probe
= max8997_pmic_probe
,
1166 .id_table
= max8997_pmic_id
,
1169 static int __init
max8997_pmic_init(void)
1171 return platform_driver_register(&max8997_pmic_driver
);
1173 subsys_initcall(max8997_pmic_init
);
1175 static void __exit
max8997_pmic_cleanup(void)
1177 platform_driver_unregister(&max8997_pmic_driver
);
1179 module_exit(max8997_pmic_cleanup
);
1181 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1182 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1183 MODULE_LICENSE("GPL");