2 * max8997.c - Regulator driver for the Maxim 8997/8966
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@smasung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This driver is based on max8998.c
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>
39 struct max8997_dev
*iodev
;
41 struct regulator_dev
**rdev
;
42 int ramp_delay
; /* in mV/us */
51 int buck125_gpioindex
;
52 bool ignore_gpiodvs_side_effect
;
54 u8 saved_states
[MAX8997_REG_MAX
];
57 static inline void max8997_set_gpio(struct max8997_data
*max8997
)
59 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
60 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
61 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
63 gpio_set_value(max8997
->buck125_gpios
[0], set1
);
64 gpio_set_value(max8997
->buck125_gpios
[1], set2
);
65 gpio_set_value(max8997
->buck125_gpios
[2], set3
);
68 struct voltage_map_desc
{
74 /* Voltage maps in uV */
75 static const struct voltage_map_desc ldo_voltage_map_desc
= {
76 .min
= 800000, .max
= 3950000, .step
= 50000,
77 }; /* LDO1 ~ 18, 21 all */
79 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
80 .min
= 650000, .max
= 2225000, .step
= 25000,
81 }; /* Buck1, 2, 4, 5 */
83 static const struct voltage_map_desc buck37_voltage_map_desc
= {
84 .min
= 750000, .max
= 3900000, .step
= 50000,
87 /* current map in uA */
88 static const struct voltage_map_desc charger_current_map_desc
= {
89 .min
= 200000, .max
= 950000, .step
= 50000,
92 static const struct voltage_map_desc topoff_current_map_desc
= {
93 .min
= 50000, .max
= 200000, .step
= 10000,
96 static const struct voltage_map_desc
*reg_voltage_map
[] = {
97 [MAX8997_LDO1
] = &ldo_voltage_map_desc
,
98 [MAX8997_LDO2
] = &ldo_voltage_map_desc
,
99 [MAX8997_LDO3
] = &ldo_voltage_map_desc
,
100 [MAX8997_LDO4
] = &ldo_voltage_map_desc
,
101 [MAX8997_LDO5
] = &ldo_voltage_map_desc
,
102 [MAX8997_LDO6
] = &ldo_voltage_map_desc
,
103 [MAX8997_LDO7
] = &ldo_voltage_map_desc
,
104 [MAX8997_LDO8
] = &ldo_voltage_map_desc
,
105 [MAX8997_LDO9
] = &ldo_voltage_map_desc
,
106 [MAX8997_LDO10
] = &ldo_voltage_map_desc
,
107 [MAX8997_LDO11
] = &ldo_voltage_map_desc
,
108 [MAX8997_LDO12
] = &ldo_voltage_map_desc
,
109 [MAX8997_LDO13
] = &ldo_voltage_map_desc
,
110 [MAX8997_LDO14
] = &ldo_voltage_map_desc
,
111 [MAX8997_LDO15
] = &ldo_voltage_map_desc
,
112 [MAX8997_LDO16
] = &ldo_voltage_map_desc
,
113 [MAX8997_LDO17
] = &ldo_voltage_map_desc
,
114 [MAX8997_LDO18
] = &ldo_voltage_map_desc
,
115 [MAX8997_LDO21
] = &ldo_voltage_map_desc
,
116 [MAX8997_BUCK1
] = &buck1245_voltage_map_desc
,
117 [MAX8997_BUCK2
] = &buck1245_voltage_map_desc
,
118 [MAX8997_BUCK3
] = &buck37_voltage_map_desc
,
119 [MAX8997_BUCK4
] = &buck1245_voltage_map_desc
,
120 [MAX8997_BUCK5
] = &buck1245_voltage_map_desc
,
121 [MAX8997_BUCK6
] = NULL
,
122 [MAX8997_BUCK7
] = &buck37_voltage_map_desc
,
123 [MAX8997_EN32KHZ_AP
] = NULL
,
124 [MAX8997_EN32KHZ_CP
] = NULL
,
125 [MAX8997_ENVICHG
] = NULL
,
126 [MAX8997_ESAFEOUT1
] = NULL
,
127 [MAX8997_ESAFEOUT2
] = NULL
,
128 [MAX8997_CHARGER_CV
] = NULL
,
129 [MAX8997_CHARGER
] = &charger_current_map_desc
,
130 [MAX8997_CHARGER_TOPOFF
] = &topoff_current_map_desc
,
133 static int max8997_list_voltage_safeout(struct regulator_dev
*rdev
,
134 unsigned int selector
)
136 int rid
= rdev_get_id(rdev
);
138 if (rid
== MAX8997_ESAFEOUT1
|| rid
== MAX8997_ESAFEOUT2
) {
156 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
157 unsigned int selector
)
159 int rid
= rdev_get_id(rdev
);
161 if (rid
!= MAX8997_CHARGER_CV
)
168 return 4000000 + 20000 * (selector
- 0x01);
178 static int max8997_list_voltage(struct regulator_dev
*rdev
,
179 unsigned int selector
)
181 const struct voltage_map_desc
*desc
;
182 int rid
= rdev_get_id(rdev
);
185 if (rid
>= ARRAY_SIZE(reg_voltage_map
) ||
189 desc
= reg_voltage_map
[rid
];
193 val
= desc
->min
+ desc
->step
* selector
;
200 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
201 int *reg
, int *mask
, int *pattern
)
203 int rid
= rdev_get_id(rdev
);
206 case MAX8997_LDO1
... MAX8997_LDO21
:
207 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
212 *reg
= MAX8997_REG_BUCK1CTRL
;
217 *reg
= MAX8997_REG_BUCK2CTRL
;
222 *reg
= MAX8997_REG_BUCK3CTRL
;
227 *reg
= MAX8997_REG_BUCK4CTRL
;
232 *reg
= MAX8997_REG_BUCK5CTRL
;
237 *reg
= MAX8997_REG_BUCK6CTRL
;
242 *reg
= MAX8997_REG_BUCK7CTRL
;
246 case MAX8997_EN32KHZ_AP
... MAX8997_EN32KHZ_CP
:
247 *reg
= MAX8997_REG_MAINCON1
;
248 *mask
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
249 *pattern
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
251 case MAX8997_ENVICHG
:
252 *reg
= MAX8997_REG_MBCCTRL1
;
256 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
257 *reg
= MAX8997_REG_SAFEOUTCTRL
;
258 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
259 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
261 case MAX8997_CHARGER
:
262 *reg
= MAX8997_REG_MBCCTRL2
;
267 /* Not controllable or not exists */
274 static int max8997_reg_is_enabled(struct regulator_dev
*rdev
)
276 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
277 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
278 int ret
, reg
, mask
, pattern
;
281 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
285 ret
= max8997_read_reg(i2c
, reg
, &val
);
289 return (val
& mask
) == pattern
;
292 static int max8997_reg_enable(struct regulator_dev
*rdev
)
294 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
295 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
296 int ret
, reg
, mask
, pattern
;
298 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
302 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
305 static int max8997_reg_disable(struct regulator_dev
*rdev
)
307 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
308 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
309 int ret
, reg
, mask
, pattern
;
311 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
315 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
318 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
319 int *_reg
, int *_shift
, int *_mask
)
321 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
322 int rid
= rdev_get_id(rdev
);
323 int reg
, shift
= 0, mask
= 0x3f;
326 case MAX8997_LDO1
... MAX8997_LDO21
:
327 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
330 reg
= MAX8997_REG_BUCK1DVS1
;
331 if (max8997
->buck1_gpiodvs
)
332 reg
+= max8997
->buck125_gpioindex
;
335 reg
= MAX8997_REG_BUCK2DVS1
;
336 if (max8997
->buck2_gpiodvs
)
337 reg
+= max8997
->buck125_gpioindex
;
340 reg
= MAX8997_REG_BUCK3DVS
;
343 reg
= MAX8997_REG_BUCK4DVS
;
346 reg
= MAX8997_REG_BUCK5DVS1
;
347 if (max8997
->buck5_gpiodvs
)
348 reg
+= max8997
->buck125_gpioindex
;
351 reg
= MAX8997_REG_BUCK7DVS
;
353 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
354 reg
= MAX8997_REG_SAFEOUTCTRL
;
355 shift
= (rid
== MAX8997_ESAFEOUT2
) ? 2 : 0;
358 case MAX8997_CHARGER_CV
:
359 reg
= MAX8997_REG_MBCCTRL3
;
363 case MAX8997_CHARGER
:
364 reg
= MAX8997_REG_MBCCTRL4
;
368 case MAX8997_CHARGER_TOPOFF
:
369 reg
= MAX8997_REG_MBCCTRL5
;
384 static int max8997_get_voltage_sel(struct regulator_dev
*rdev
)
386 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
387 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
388 int reg
, shift
, mask
, ret
;
391 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
395 ret
= max8997_read_reg(i2c
, reg
, &val
);
405 static inline int max8997_get_voltage_proper_val(
406 const struct voltage_map_desc
*desc
,
407 int min_vol
, int max_vol
)
414 if (max_vol
< desc
->min
|| min_vol
> desc
->max
)
417 if (min_vol
< desc
->min
)
420 i
= DIV_ROUND_UP(min_vol
- desc
->min
, desc
->step
);
422 if (desc
->min
+ desc
->step
* i
> max_vol
)
428 static int max8997_set_voltage_charger_cv(struct regulator_dev
*rdev
,
429 int min_uV
, int max_uV
, unsigned *selector
)
431 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
432 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
433 int rid
= rdev_get_id(rdev
);
435 int reg
, shift
= 0, mask
, ret
= 0;
438 if (rid
!= MAX8997_CHARGER_CV
)
441 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
445 if (max_uV
< 4000000 || min_uV
> 4350000)
448 if (min_uV
<= 4000000) {
449 if (max_uV
>= 4000000)
453 } else if (min_uV
<= 4200000 && max_uV
>= 4200000)
456 lb
= (min_uV
- 4000001) / 20000 + 2;
457 ub
= (max_uV
- 4000000) / 20000 + 1;
474 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
);
480 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
481 * BUCK1, 2, and 5 are available if they are not controlled by gpio
483 static int max8997_set_voltage_ldobuck(struct regulator_dev
*rdev
,
484 int min_uV
, int max_uV
, unsigned *selector
)
486 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
487 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
488 const struct voltage_map_desc
*desc
;
489 int rid
= rdev_get_id(rdev
);
490 int i
, reg
, shift
, mask
, ret
;
493 case MAX8997_LDO1
... MAX8997_LDO21
:
495 case MAX8997_BUCK1
... MAX8997_BUCK5
:
501 case MAX8997_CHARGER
:
503 case MAX8997_CHARGER_TOPOFF
:
509 desc
= reg_voltage_map
[rid
];
511 i
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
515 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
519 ret
= max8997_update_reg(i2c
, reg
, i
<< shift
, mask
<< shift
);
525 static int max8997_set_voltage_ldobuck_time_sel(struct regulator_dev
*rdev
,
526 unsigned int old_selector
,
527 unsigned int new_selector
)
529 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
530 int rid
= rdev_get_id(rdev
);
531 const struct voltage_map_desc
*desc
= reg_voltage_map
[rid
];
533 /* Delay is required only if the voltage is increasing */
534 if (old_selector
>= new_selector
)
537 /* No need to delay if gpio_dvs_mode */
540 if (max8997
->buck1_gpiodvs
)
544 if (max8997
->buck2_gpiodvs
)
548 if (max8997
->buck5_gpiodvs
)
558 return DIV_ROUND_UP(desc
->step
* (new_selector
- old_selector
),
559 max8997
->ramp_delay
* 1000);
566 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
568 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
569 * of one of the bucks may affect that of another buck, which is the side
570 * effect of the change (set_voltage). This function examines the GPIO-DVS
571 * configurations and checks whether such side-effect exists.
573 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
574 u8 new_val
, int *best
)
576 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
577 int rid
= rdev_get_id(rdev
);
579 bool buckx_gpiodvs
[3];
581 int min_side_effect
= INT_MAX
;
600 buckx_val
[0] = max8997
->buck1_vol
;
601 buckx_val
[1] = max8997
->buck2_vol
;
602 buckx_val
[2] = max8997
->buck5_vol
;
603 buckx_gpiodvs
[0] = max8997
->buck1_gpiodvs
;
604 buckx_gpiodvs
[1] = max8997
->buck2_gpiodvs
;
605 buckx_gpiodvs
[2] = max8997
->buck5_gpiodvs
;
607 for (i
= 0; i
< 8; i
++) {
610 if (new_val
!= (buckx_val
[rid
])[i
]) {
616 for (others
= 0; others
< 3; others
++) {
621 if (buckx_gpiodvs
[others
] == false)
622 continue; /* Not affected */
623 diff
= (buckx_val
[others
])[i
] -
624 (buckx_val
[others
])[max8997
->buck125_gpioindex
];
626 side_effect
[i
] += diff
;
628 side_effect
[i
] -= diff
;
630 if (side_effect
[i
] == 0) {
632 return 0; /* NO SIDE EFFECT! Use This! */
634 if (side_effect
[i
] < min_side_effect
) {
635 min_side_effect
= side_effect
[i
];
643 return side_effect
[*best
];
647 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
648 * max8997_set_voltage_ldobuck to do the job.
650 static int max8997_set_voltage_buck(struct regulator_dev
*rdev
,
651 int min_uV
, int max_uV
, unsigned *selector
)
653 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
654 int rid
= rdev_get_id(rdev
);
655 const struct voltage_map_desc
*desc
;
656 int new_val
, new_idx
, damage
, tmp_val
, tmp_idx
, tmp_dmg
;
657 bool gpio_dvs_mode
= false;
659 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
664 if (max8997
->buck1_gpiodvs
)
665 gpio_dvs_mode
= true;
668 if (max8997
->buck2_gpiodvs
)
669 gpio_dvs_mode
= true;
672 if (max8997
->buck5_gpiodvs
)
673 gpio_dvs_mode
= true;
678 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
681 desc
= reg_voltage_map
[rid
];
682 new_val
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
690 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
694 if (tmp_dmg
> damage
) {
701 } while (desc
->min
+ desc
->step
* new_val
<= desc
->max
);
706 if (max8997
->ignore_gpiodvs_side_effect
== false)
709 dev_warn(&rdev
->dev
, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
710 " %d -> %d\n", max8997
->buck125_gpioindex
, tmp_idx
);
713 if (new_idx
< 0 || new_val
< 0)
716 max8997
->buck125_gpioindex
= new_idx
;
717 max8997_set_gpio(max8997
);
723 static const int safeoutvolt
[] = {
730 /* For SAFEOUT1 and SAFEOUT2 */
731 static int max8997_set_voltage_safeout(struct regulator_dev
*rdev
,
732 int min_uV
, int max_uV
, unsigned *selector
)
734 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
735 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
736 int rid
= rdev_get_id(rdev
);
737 int reg
, shift
= 0, mask
, ret
;
741 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
744 for (i
= 0; i
< ARRAY_SIZE(safeoutvolt
); i
++) {
745 if (min_uV
<= safeoutvolt
[i
] &&
746 max_uV
>= safeoutvolt
[i
])
750 if (i
>= ARRAY_SIZE(safeoutvolt
))
758 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
762 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
<< shift
);
768 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
770 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
771 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
772 int ret
, reg
, mask
, pattern
;
773 int rid
= rdev_get_id(rdev
);
775 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
779 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
781 if (rid
== MAX8997_LDO1
||
782 rid
== MAX8997_LDO10
||
783 rid
== MAX8997_LDO21
) {
784 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
786 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
789 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
790 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
792 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
795 static struct regulator_ops max8997_ldo_ops
= {
796 .list_voltage
= max8997_list_voltage
,
797 .is_enabled
= max8997_reg_is_enabled
,
798 .enable
= max8997_reg_enable
,
799 .disable
= max8997_reg_disable
,
800 .get_voltage_sel
= max8997_get_voltage_sel
,
801 .set_voltage
= max8997_set_voltage_ldobuck
,
802 .set_voltage_time_sel
= max8997_set_voltage_ldobuck_time_sel
,
803 .set_suspend_disable
= max8997_reg_disable_suspend
,
806 static struct regulator_ops max8997_buck_ops
= {
807 .list_voltage
= max8997_list_voltage
,
808 .is_enabled
= max8997_reg_is_enabled
,
809 .enable
= max8997_reg_enable
,
810 .disable
= max8997_reg_disable
,
811 .get_voltage_sel
= max8997_get_voltage_sel
,
812 .set_voltage
= max8997_set_voltage_buck
,
813 .set_voltage_time_sel
= max8997_set_voltage_ldobuck_time_sel
,
814 .set_suspend_disable
= max8997_reg_disable_suspend
,
817 static struct regulator_ops max8997_fixedvolt_ops
= {
818 .list_voltage
= max8997_list_voltage
,
819 .is_enabled
= max8997_reg_is_enabled
,
820 .enable
= max8997_reg_enable
,
821 .disable
= max8997_reg_disable
,
822 .set_suspend_disable
= max8997_reg_disable_suspend
,
825 static struct regulator_ops max8997_safeout_ops
= {
826 .list_voltage
= max8997_list_voltage_safeout
,
827 .is_enabled
= max8997_reg_is_enabled
,
828 .enable
= max8997_reg_enable
,
829 .disable
= max8997_reg_disable
,
830 .get_voltage_sel
= max8997_get_voltage_sel
,
831 .set_voltage
= max8997_set_voltage_safeout
,
832 .set_suspend_disable
= max8997_reg_disable_suspend
,
835 static struct regulator_ops max8997_fixedstate_ops
= {
836 .list_voltage
= max8997_list_voltage_charger_cv
,
837 .get_voltage_sel
= max8997_get_voltage_sel
,
838 .set_voltage
= max8997_set_voltage_charger_cv
,
841 static int max8997_set_current_limit(struct regulator_dev
*rdev
,
842 int min_uA
, int max_uA
)
845 int rid
= rdev_get_id(rdev
);
847 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
850 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
851 return max8997_set_voltage_ldobuck(rdev
, min_uA
, max_uA
, &dummy
);
854 static int max8997_get_current_limit(struct regulator_dev
*rdev
)
856 int sel
, rid
= rdev_get_id(rdev
);
858 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
861 sel
= max8997_get_voltage_sel(rdev
);
865 /* Reuse max8997_list_voltage to get current_limit. */
866 return max8997_list_voltage(rdev
, sel
);
869 static struct regulator_ops max8997_charger_ops
= {
870 .is_enabled
= max8997_reg_is_enabled
,
871 .enable
= max8997_reg_enable
,
872 .disable
= max8997_reg_disable
,
873 .get_current_limit
= max8997_get_current_limit
,
874 .set_current_limit
= max8997_set_current_limit
,
877 static struct regulator_ops max8997_charger_fixedstate_ops
= {
878 .get_current_limit
= max8997_get_current_limit
,
879 .set_current_limit
= max8997_set_current_limit
,
882 #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
884 .id = MAX8997_##_name, \
886 .type = REGULATOR_VOLTAGE, \
887 .owner = THIS_MODULE, \
890 #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
892 .id = MAX8997_##_name, \
894 .type = REGULATOR_CURRENT, \
895 .owner = THIS_MODULE, \
898 static struct regulator_desc regulators
[] = {
899 MAX8997_VOLTAGE_REGULATOR(LDO1
, max8997_ldo_ops
),
900 MAX8997_VOLTAGE_REGULATOR(LDO2
, max8997_ldo_ops
),
901 MAX8997_VOLTAGE_REGULATOR(LDO3
, max8997_ldo_ops
),
902 MAX8997_VOLTAGE_REGULATOR(LDO4
, max8997_ldo_ops
),
903 MAX8997_VOLTAGE_REGULATOR(LDO5
, max8997_ldo_ops
),
904 MAX8997_VOLTAGE_REGULATOR(LDO6
, max8997_ldo_ops
),
905 MAX8997_VOLTAGE_REGULATOR(LDO7
, max8997_ldo_ops
),
906 MAX8997_VOLTAGE_REGULATOR(LDO8
, max8997_ldo_ops
),
907 MAX8997_VOLTAGE_REGULATOR(LDO9
, max8997_ldo_ops
),
908 MAX8997_VOLTAGE_REGULATOR(LDO10
, max8997_ldo_ops
),
909 MAX8997_VOLTAGE_REGULATOR(LDO11
, max8997_ldo_ops
),
910 MAX8997_VOLTAGE_REGULATOR(LDO12
, max8997_ldo_ops
),
911 MAX8997_VOLTAGE_REGULATOR(LDO13
, max8997_ldo_ops
),
912 MAX8997_VOLTAGE_REGULATOR(LDO14
, max8997_ldo_ops
),
913 MAX8997_VOLTAGE_REGULATOR(LDO15
, max8997_ldo_ops
),
914 MAX8997_VOLTAGE_REGULATOR(LDO16
, max8997_ldo_ops
),
915 MAX8997_VOLTAGE_REGULATOR(LDO17
, max8997_ldo_ops
),
916 MAX8997_VOLTAGE_REGULATOR(LDO18
, max8997_ldo_ops
),
917 MAX8997_VOLTAGE_REGULATOR(LDO21
, max8997_ldo_ops
),
918 MAX8997_VOLTAGE_REGULATOR(BUCK1
, max8997_buck_ops
),
919 MAX8997_VOLTAGE_REGULATOR(BUCK2
, max8997_buck_ops
),
920 MAX8997_VOLTAGE_REGULATOR(BUCK3
, max8997_buck_ops
),
921 MAX8997_VOLTAGE_REGULATOR(BUCK4
, max8997_buck_ops
),
922 MAX8997_VOLTAGE_REGULATOR(BUCK5
, max8997_buck_ops
),
923 MAX8997_VOLTAGE_REGULATOR(BUCK6
, max8997_fixedvolt_ops
),
924 MAX8997_VOLTAGE_REGULATOR(BUCK7
, max8997_buck_ops
),
925 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP
, max8997_fixedvolt_ops
),
926 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP
, max8997_fixedvolt_ops
),
927 MAX8997_VOLTAGE_REGULATOR(ENVICHG
, max8997_fixedvolt_ops
),
928 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1
, max8997_safeout_ops
),
929 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2
, max8997_safeout_ops
),
930 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV
, max8997_fixedstate_ops
),
931 MAX8997_CURRENT_REGULATOR(CHARGER
, max8997_charger_ops
),
932 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF
,
933 max8997_charger_fixedstate_ops
),
937 static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device
*pdev
,
938 struct max8997_platform_data
*pdata
,
939 struct device_node
*pmic_np
)
943 for (i
= 0; i
< 3; i
++) {
944 gpio
= of_get_named_gpio(pmic_np
,
945 "max8997,pmic-buck125-dvs-gpios", i
);
946 if (!gpio_is_valid(gpio
)) {
947 dev_err(&pdev
->dev
, "invalid gpio[%d]: %d\n", i
, gpio
);
950 pdata
->buck125_gpios
[i
] = gpio
;
955 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
956 struct max8997_platform_data
*pdata
)
958 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
959 struct device_node
*pmic_np
, *regulators_np
, *reg_np
;
960 struct max8997_regulator_data
*rdata
;
961 unsigned int i
, dvs_voltage_nr
= 1, ret
;
963 pmic_np
= iodev
->dev
->of_node
;
965 dev_err(&pdev
->dev
, "could not find pmic sub-node\n");
969 regulators_np
= of_find_node_by_name(pmic_np
, "regulators");
970 if (!regulators_np
) {
971 dev_err(&pdev
->dev
, "could not find regulators sub-node\n");
975 /* count the number of regulators to be supported in pmic */
976 pdata
->num_regulators
= 0;
977 for_each_child_of_node(regulators_np
, reg_np
)
978 pdata
->num_regulators
++;
980 rdata
= devm_kzalloc(&pdev
->dev
, sizeof(*rdata
) *
981 pdata
->num_regulators
, GFP_KERNEL
);
983 dev_err(&pdev
->dev
, "could not allocate memory for regulator data\n");
987 pdata
->regulators
= rdata
;
988 for_each_child_of_node(regulators_np
, reg_np
) {
989 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++)
990 if (!of_node_cmp(reg_np
->name
, regulators
[i
].name
))
993 if (i
== ARRAY_SIZE(regulators
)) {
994 dev_warn(&pdev
->dev
, "don't know how to configure regulator %s\n",
1000 rdata
->initdata
= of_get_regulator_init_data(&pdev
->dev
,
1002 rdata
->reg_node
= reg_np
;
1006 if (of_get_property(pmic_np
, "max8997,pmic-buck1-uses-gpio-dvs", NULL
))
1007 pdata
->buck1_gpiodvs
= true;
1009 if (of_get_property(pmic_np
, "max8997,pmic-buck2-uses-gpio-dvs", NULL
))
1010 pdata
->buck2_gpiodvs
= true;
1012 if (of_get_property(pmic_np
, "max8997,pmic-buck5-uses-gpio-dvs", NULL
))
1013 pdata
->buck5_gpiodvs
= true;
1015 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1016 pdata
->buck5_gpiodvs
) {
1017 ret
= max8997_pmic_dt_parse_dvs_gpio(pdev
, pdata
, pmic_np
);
1021 if (of_property_read_u32(pmic_np
,
1022 "max8997,pmic-buck125-default-dvs-idx",
1023 &pdata
->buck125_default_idx
)) {
1024 pdata
->buck125_default_idx
= 0;
1026 if (pdata
->buck125_default_idx
>= 8) {
1027 pdata
->buck125_default_idx
= 0;
1028 dev_info(&pdev
->dev
, "invalid value for default dvs index, using 0 instead\n");
1032 if (of_get_property(pmic_np
,
1033 "max8997,pmic-ignore-gpiodvs-side-effect", NULL
))
1034 pdata
->ignore_gpiodvs_side_effect
= true;
1039 if (of_property_read_u32_array(pmic_np
,
1040 "max8997,pmic-buck1-dvs-voltage",
1041 pdata
->buck1_voltage
, dvs_voltage_nr
)) {
1042 dev_err(&pdev
->dev
, "buck1 voltages not specified\n");
1046 if (of_property_read_u32_array(pmic_np
,
1047 "max8997,pmic-buck2-dvs-voltage",
1048 pdata
->buck2_voltage
, dvs_voltage_nr
)) {
1049 dev_err(&pdev
->dev
, "buck2 voltages not specified\n");
1053 if (of_property_read_u32_array(pmic_np
,
1054 "max8997,pmic-buck5-dvs-voltage",
1055 pdata
->buck5_voltage
, dvs_voltage_nr
)) {
1056 dev_err(&pdev
->dev
, "buck5 voltages not specified\n");
1063 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
1064 struct max8997_platform_data
*pdata
)
1068 #endif /* CONFIG_OF */
1070 static int max8997_pmic_probe(struct platform_device
*pdev
)
1072 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
1073 struct max8997_platform_data
*pdata
= iodev
->pdata
;
1074 struct regulator_config config
= { };
1075 struct regulator_dev
**rdev
;
1076 struct max8997_data
*max8997
;
1077 struct i2c_client
*i2c
;
1078 int i
, ret
, size
, nr_dvs
;
1079 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
1081 if (IS_ERR_OR_NULL(pdata
)) {
1082 dev_err(pdev
->dev
.parent
, "No platform init data supplied.\n");
1086 if (iodev
->dev
->of_node
) {
1087 ret
= max8997_pmic_dt_parse_pdata(pdev
, pdata
);
1092 max8997
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_data
),
1097 size
= sizeof(struct regulator_dev
*) * pdata
->num_regulators
;
1098 max8997
->rdev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
1102 rdev
= max8997
->rdev
;
1103 max8997
->dev
= &pdev
->dev
;
1104 max8997
->iodev
= iodev
;
1105 max8997
->num_regulators
= pdata
->num_regulators
;
1106 platform_set_drvdata(pdev
, max8997
);
1107 i2c
= max8997
->iodev
->i2c
;
1109 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
1110 max8997
->buck1_gpiodvs
= pdata
->buck1_gpiodvs
;
1111 max8997
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
1112 max8997
->buck5_gpiodvs
= pdata
->buck5_gpiodvs
;
1113 memcpy(max8997
->buck125_gpios
, pdata
->buck125_gpios
, sizeof(int) * 3);
1114 max8997
->ignore_gpiodvs_side_effect
= pdata
->ignore_gpiodvs_side_effect
;
1116 nr_dvs
= (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1117 pdata
->buck5_gpiodvs
) ? 8 : 1;
1119 for (i
= 0; i
< nr_dvs
; i
++) {
1120 max8997
->buck1_vol
[i
] = ret
=
1121 max8997_get_voltage_proper_val(
1122 &buck1245_voltage_map_desc
,
1123 pdata
->buck1_voltage
[i
],
1124 pdata
->buck1_voltage
[i
] +
1125 buck1245_voltage_map_desc
.step
);
1129 max8997
->buck2_vol
[i
] = ret
=
1130 max8997_get_voltage_proper_val(
1131 &buck1245_voltage_map_desc
,
1132 pdata
->buck2_voltage
[i
],
1133 pdata
->buck2_voltage
[i
] +
1134 buck1245_voltage_map_desc
.step
);
1138 max8997
->buck5_vol
[i
] = ret
=
1139 max8997_get_voltage_proper_val(
1140 &buck1245_voltage_map_desc
,
1141 pdata
->buck5_voltage
[i
],
1142 pdata
->buck5_voltage
[i
] +
1143 buck1245_voltage_map_desc
.step
);
1147 if (max_buck1
< max8997
->buck1_vol
[i
])
1148 max_buck1
= max8997
->buck1_vol
[i
];
1149 if (max_buck2
< max8997
->buck2_vol
[i
])
1150 max_buck2
= max8997
->buck2_vol
[i
];
1151 if (max_buck5
< max8997
->buck5_vol
[i
])
1152 max_buck5
= max8997
->buck5_vol
[i
];
1155 /* For the safety, set max voltage before setting up */
1156 for (i
= 0; i
< 8; i
++) {
1157 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1159 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1161 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1165 /* Initialize all the DVS related BUCK registers */
1166 for (i
= 0; i
< nr_dvs
; i
++) {
1167 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1168 max8997
->buck1_vol
[i
],
1170 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1171 max8997
->buck2_vol
[i
],
1173 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1174 max8997
->buck5_vol
[i
],
1179 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1180 * If at least one of them cares, set gpios.
1182 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1183 pdata
->buck5_gpiodvs
) {
1185 if (!gpio_is_valid(pdata
->buck125_gpios
[0]) ||
1186 !gpio_is_valid(pdata
->buck125_gpios
[1]) ||
1187 !gpio_is_valid(pdata
->buck125_gpios
[2])) {
1188 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
1193 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[0],
1198 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[1],
1203 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[2],
1208 gpio_direction_output(pdata
->buck125_gpios
[0],
1209 (max8997
->buck125_gpioindex
>> 2)
1211 gpio_direction_output(pdata
->buck125_gpios
[1],
1212 (max8997
->buck125_gpioindex
>> 1)
1214 gpio_direction_output(pdata
->buck125_gpios
[2],
1215 (max8997
->buck125_gpioindex
>> 0)
1219 /* DVS-GPIO disabled */
1220 max8997_update_reg(i2c
, MAX8997_REG_BUCK1CTRL
, (pdata
->buck1_gpiodvs
) ?
1221 (1 << 1) : (0 << 1), 1 << 1);
1222 max8997_update_reg(i2c
, MAX8997_REG_BUCK2CTRL
, (pdata
->buck2_gpiodvs
) ?
1223 (1 << 1) : (0 << 1), 1 << 1);
1224 max8997_update_reg(i2c
, MAX8997_REG_BUCK5CTRL
, (pdata
->buck5_gpiodvs
) ?
1225 (1 << 1) : (0 << 1), 1 << 1);
1228 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1229 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1231 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1232 const struct voltage_map_desc
*desc
;
1233 int id
= pdata
->regulators
[i
].id
;
1235 desc
= reg_voltage_map
[id
];
1237 regulators
[id
].n_voltages
=
1238 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1239 else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
)
1240 regulators
[id
].n_voltages
= 4;
1241 else if (id
== MAX8997_CHARGER_CV
)
1242 regulators
[id
].n_voltages
= 16;
1244 config
.dev
= max8997
->dev
;
1245 config
.init_data
= pdata
->regulators
[i
].initdata
;
1246 config
.driver_data
= max8997
;
1247 config
.of_node
= pdata
->regulators
[i
].reg_node
;
1249 rdev
[i
] = regulator_register(®ulators
[id
], &config
);
1250 if (IS_ERR(rdev
[i
])) {
1251 ret
= PTR_ERR(rdev
[i
]);
1252 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1262 regulator_unregister(rdev
[i
]);
1267 static int max8997_pmic_remove(struct platform_device
*pdev
)
1269 struct max8997_data
*max8997
= platform_get_drvdata(pdev
);
1270 struct regulator_dev
**rdev
= max8997
->rdev
;
1273 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1274 regulator_unregister(rdev
[i
]);
1278 static const struct platform_device_id max8997_pmic_id
[] = {
1279 { "max8997-pmic", 0},
1282 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1284 static struct platform_driver max8997_pmic_driver
= {
1286 .name
= "max8997-pmic",
1287 .owner
= THIS_MODULE
,
1289 .probe
= max8997_pmic_probe
,
1290 .remove
= max8997_pmic_remove
,
1291 .id_table
= max8997_pmic_id
,
1294 static int __init
max8997_pmic_init(void)
1296 return platform_driver_register(&max8997_pmic_driver
);
1298 subsys_initcall(max8997_pmic_init
);
1300 static void __exit
max8997_pmic_cleanup(void)
1302 platform_driver_unregister(&max8997_pmic_driver
);
1304 module_exit(max8997_pmic_cleanup
);
1306 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1307 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1308 MODULE_LICENSE("GPL");