2 * max8997.c - Regulator driver for the Maxim 8997/8966
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.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 int ramp_delay
; /* in mV/us */
50 int buck125_gpioindex
;
51 bool ignore_gpiodvs_side_effect
;
53 u8 saved_states
[MAX8997_REG_MAX
];
56 static const unsigned int safeoutvolt
[] = {
63 static inline void max8997_set_gpio(struct max8997_data
*max8997
)
65 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
66 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
67 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
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
);
74 struct voltage_map_desc
{
80 /* Voltage maps in uV */
81 static const struct voltage_map_desc ldo_voltage_map_desc
= {
82 .min
= 800000, .max
= 3950000, .step
= 50000,
83 }; /* LDO1 ~ 18, 21 all */
85 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
86 .min
= 650000, .max
= 2225000, .step
= 25000,
87 }; /* Buck1, 2, 4, 5 */
89 static const struct voltage_map_desc buck37_voltage_map_desc
= {
90 .min
= 750000, .max
= 3900000, .step
= 50000,
93 /* current map in uA */
94 static const struct voltage_map_desc charger_current_map_desc
= {
95 .min
= 200000, .max
= 950000, .step
= 50000,
98 static const struct voltage_map_desc topoff_current_map_desc
= {
99 .min
= 50000, .max
= 200000, .step
= 10000,
102 static 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
,
139 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
140 unsigned int selector
)
142 int rid
= rdev_get_id(rdev
);
144 if (rid
!= MAX8997_CHARGER_CV
)
151 return 4000000 + 20000 * (selector
- 0x01);
161 static int max8997_list_voltage(struct regulator_dev
*rdev
,
162 unsigned int selector
)
164 const struct voltage_map_desc
*desc
;
165 int rid
= rdev_get_id(rdev
);
168 if (rid
>= ARRAY_SIZE(reg_voltage_map
) ||
172 desc
= reg_voltage_map
[rid
];
176 val
= desc
->min
+ desc
->step
* selector
;
183 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
184 int *reg
, int *mask
, int *pattern
)
186 int rid
= rdev_get_id(rdev
);
189 case MAX8997_LDO1
... MAX8997_LDO21
:
190 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
195 *reg
= MAX8997_REG_BUCK1CTRL
;
200 *reg
= MAX8997_REG_BUCK2CTRL
;
205 *reg
= MAX8997_REG_BUCK3CTRL
;
210 *reg
= MAX8997_REG_BUCK4CTRL
;
215 *reg
= MAX8997_REG_BUCK5CTRL
;
220 *reg
= MAX8997_REG_BUCK6CTRL
;
225 *reg
= MAX8997_REG_BUCK7CTRL
;
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
);
234 case MAX8997_ENVICHG
:
235 *reg
= MAX8997_REG_MBCCTRL1
;
239 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
240 *reg
= MAX8997_REG_SAFEOUTCTRL
;
241 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
242 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
244 case MAX8997_CHARGER
:
245 *reg
= MAX8997_REG_MBCCTRL2
;
250 /* Not controllable or not exists */
257 static int max8997_reg_is_enabled(struct regulator_dev
*rdev
)
259 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
260 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
261 int ret
, reg
, mask
, pattern
;
264 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
268 ret
= max8997_read_reg(i2c
, reg
, &val
);
272 return (val
& mask
) == pattern
;
275 static int max8997_reg_enable(struct regulator_dev
*rdev
)
277 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
278 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
279 int ret
, reg
, mask
, pattern
;
281 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
285 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
288 static int max8997_reg_disable(struct regulator_dev
*rdev
)
290 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
291 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
292 int ret
, reg
, mask
, pattern
;
294 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
298 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
301 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
302 int *_reg
, int *_shift
, int *_mask
)
304 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
305 int rid
= rdev_get_id(rdev
);
306 int reg
, shift
= 0, mask
= 0x3f;
309 case MAX8997_LDO1
... MAX8997_LDO21
:
310 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
313 reg
= MAX8997_REG_BUCK1DVS1
;
314 if (max8997
->buck1_gpiodvs
)
315 reg
+= max8997
->buck125_gpioindex
;
318 reg
= MAX8997_REG_BUCK2DVS1
;
319 if (max8997
->buck2_gpiodvs
)
320 reg
+= max8997
->buck125_gpioindex
;
323 reg
= MAX8997_REG_BUCK3DVS
;
326 reg
= MAX8997_REG_BUCK4DVS
;
329 reg
= MAX8997_REG_BUCK5DVS1
;
330 if (max8997
->buck5_gpiodvs
)
331 reg
+= max8997
->buck125_gpioindex
;
334 reg
= MAX8997_REG_BUCK7DVS
;
336 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
337 reg
= MAX8997_REG_SAFEOUTCTRL
;
338 shift
= (rid
== MAX8997_ESAFEOUT2
) ? 2 : 0;
341 case MAX8997_CHARGER_CV
:
342 reg
= MAX8997_REG_MBCCTRL3
;
346 case MAX8997_CHARGER
:
347 reg
= MAX8997_REG_MBCCTRL4
;
351 case MAX8997_CHARGER_TOPOFF
:
352 reg
= MAX8997_REG_MBCCTRL5
;
367 static int max8997_get_voltage_sel(struct regulator_dev
*rdev
)
369 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
370 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
371 int reg
, shift
, mask
, ret
;
374 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
378 ret
= max8997_read_reg(i2c
, reg
, &val
);
388 static inline int max8997_get_voltage_proper_val(
389 const struct voltage_map_desc
*desc
,
390 int min_vol
, int max_vol
)
397 if (max_vol
< desc
->min
|| min_vol
> desc
->max
)
400 if (min_vol
< desc
->min
)
403 i
= DIV_ROUND_UP(min_vol
- desc
->min
, desc
->step
);
405 if (desc
->min
+ desc
->step
* i
> max_vol
)
411 static int max8997_set_voltage_charger_cv(struct regulator_dev
*rdev
,
412 int min_uV
, int max_uV
, unsigned *selector
)
414 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
415 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
416 int rid
= rdev_get_id(rdev
);
418 int reg
, shift
= 0, mask
, ret
= 0;
421 if (rid
!= MAX8997_CHARGER_CV
)
424 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
428 if (max_uV
< 4000000 || min_uV
> 4350000)
431 if (min_uV
<= 4000000)
433 else if (min_uV
<= 4200000 && max_uV
>= 4200000)
436 lb
= (min_uV
- 4000001) / 20000 + 2;
437 ub
= (max_uV
- 4000000) / 20000 + 1;
454 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
);
460 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
461 * BUCK1, 2, and 5 are available if they are not controlled by gpio
463 static int max8997_set_voltage_ldobuck(struct regulator_dev
*rdev
,
464 int min_uV
, int max_uV
, unsigned *selector
)
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
;
473 case MAX8997_LDO1
... MAX8997_LDO21
:
475 case MAX8997_BUCK1
... MAX8997_BUCK5
:
481 case MAX8997_CHARGER
:
483 case MAX8997_CHARGER_TOPOFF
:
489 desc
= reg_voltage_map
[rid
];
491 i
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
495 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
499 ret
= max8997_update_reg(i2c
, reg
, i
<< shift
, mask
<< shift
);
505 static int max8997_set_voltage_buck_time_sel(struct regulator_dev
*rdev
,
506 unsigned int old_selector
,
507 unsigned int new_selector
)
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
];
513 /* Delay is required only if the voltage is increasing */
514 if (old_selector
>= new_selector
)
517 /* No need to delay if gpio_dvs_mode */
520 if (max8997
->buck1_gpiodvs
)
524 if (max8997
->buck2_gpiodvs
)
528 if (max8997
->buck5_gpiodvs
)
538 return DIV_ROUND_UP(desc
->step
* (new_selector
- old_selector
),
539 max8997
->ramp_delay
* 1000);
546 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
548 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
549 * of one of the bucks may affect that of another buck, which is the side
550 * effect of the change (set_voltage). This function examines the GPIO-DVS
551 * configurations and checks whether such side-effect exists.
553 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
554 u8 new_val
, int *best
)
556 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
557 int rid
= rdev_get_id(rdev
);
559 bool buckx_gpiodvs
[3];
561 int min_side_effect
= INT_MAX
;
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
;
587 for (i
= 0; i
< 8; i
++) {
590 if (new_val
!= (buckx_val
[rid
])[i
]) {
596 for (others
= 0; others
< 3; others
++) {
601 if (buckx_gpiodvs
[others
] == false)
602 continue; /* Not affected */
603 diff
= (buckx_val
[others
])[i
] -
604 (buckx_val
[others
])[max8997
->buck125_gpioindex
];
606 side_effect
[i
] += diff
;
608 side_effect
[i
] -= diff
;
610 if (side_effect
[i
] == 0) {
612 return 0; /* NO SIDE EFFECT! Use This! */
614 if (side_effect
[i
] < min_side_effect
) {
615 min_side_effect
= side_effect
[i
];
623 return side_effect
[*best
];
627 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
628 * max8997_set_voltage_ldobuck to do the job.
630 static int max8997_set_voltage_buck(struct regulator_dev
*rdev
,
631 int min_uV
, int max_uV
, unsigned *selector
)
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;
639 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
644 if (max8997
->buck1_gpiodvs
)
645 gpio_dvs_mode
= true;
648 if (max8997
->buck2_gpiodvs
)
649 gpio_dvs_mode
= true;
652 if (max8997
->buck5_gpiodvs
)
653 gpio_dvs_mode
= true;
658 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
661 desc
= reg_voltage_map
[rid
];
662 new_val
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
670 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
674 if (tmp_dmg
> damage
) {
681 } while (desc
->min
+ desc
->step
* new_val
<= desc
->max
);
686 if (max8997
->ignore_gpiodvs_side_effect
== false)
690 "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET: %d -> %d\n",
691 max8997
->buck125_gpioindex
, tmp_idx
);
694 if (new_idx
< 0 || new_val
< 0)
697 max8997
->buck125_gpioindex
= new_idx
;
698 max8997_set_gpio(max8997
);
704 /* For SAFEOUT1 and SAFEOUT2 */
705 static int max8997_set_voltage_safeout_sel(struct regulator_dev
*rdev
,
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
;
713 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
716 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
720 return max8997_update_reg(i2c
, reg
, selector
<< shift
, mask
<< shift
);
723 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
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
);
730 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
734 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
736 if (rid
== MAX8997_LDO1
||
737 rid
== MAX8997_LDO10
||
738 rid
== MAX8997_LDO21
) {
739 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
741 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
744 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
745 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
747 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
750 static 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
,
760 static 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
,
771 static 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
,
779 static 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
,
789 static 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
,
795 static int max8997_set_current_limit(struct regulator_dev
*rdev
,
796 int min_uA
, int max_uA
)
799 int rid
= rdev_get_id(rdev
);
801 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
804 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
805 return max8997_set_voltage_ldobuck(rdev
, min_uA
, max_uA
, &dummy
);
808 static int max8997_get_current_limit(struct regulator_dev
*rdev
)
810 int sel
, rid
= rdev_get_id(rdev
);
812 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
815 sel
= max8997_get_voltage_sel(rdev
);
819 /* Reuse max8997_list_voltage to get current_limit. */
820 return max8997_list_voltage(rdev
, sel
);
823 static 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
,
831 static struct regulator_ops max8997_charger_fixedstate_ops
= {
832 .get_current_limit
= max8997_get_current_limit
,
833 .set_current_limit
= max8997_set_current_limit
,
836 #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
838 .id = MAX8997_##_name, \
840 .type = REGULATOR_VOLTAGE, \
841 .owner = THIS_MODULE, \
844 #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
846 .id = MAX8997_##_name, \
848 .type = REGULATOR_CURRENT, \
849 .owner = THIS_MODULE, \
852 static 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
),
891 static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device
*pdev
,
892 struct max8997_platform_data
*pdata
,
893 struct device_node
*pmic_np
)
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
);
904 pdata
->buck125_gpios
[i
] = gpio
;
909 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
910 struct max8997_platform_data
*pdata
)
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
;
917 pmic_np
= iodev
->dev
->of_node
;
919 dev_err(&pdev
->dev
, "could not find pmic sub-node\n");
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");
929 /* count the number of regulators to be supported in pmic */
930 pdata
->num_regulators
= of_get_child_count(regulators_np
);
932 rdata
= devm_kzalloc(&pdev
->dev
, sizeof(*rdata
) *
933 pdata
->num_regulators
, GFP_KERNEL
);
935 of_node_put(regulators_np
);
939 pdata
->regulators
= rdata
;
940 for_each_child_of_node(regulators_np
, reg_np
) {
941 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++)
942 if (!of_node_cmp(reg_np
->name
, regulators
[i
].name
))
945 if (i
== ARRAY_SIZE(regulators
)) {
946 dev_warn(&pdev
->dev
, "don't know how to configure regulator %s\n",
952 rdata
->initdata
= of_get_regulator_init_data(&pdev
->dev
,
955 rdata
->reg_node
= reg_np
;
958 of_node_put(regulators_np
);
960 if (of_get_property(pmic_np
, "max8997,pmic-buck1-uses-gpio-dvs", NULL
))
961 pdata
->buck1_gpiodvs
= true;
963 if (of_get_property(pmic_np
, "max8997,pmic-buck2-uses-gpio-dvs", NULL
))
964 pdata
->buck2_gpiodvs
= true;
966 if (of_get_property(pmic_np
, "max8997,pmic-buck5-uses-gpio-dvs", NULL
))
967 pdata
->buck5_gpiodvs
= true;
969 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
970 pdata
->buck5_gpiodvs
) {
971 ret
= max8997_pmic_dt_parse_dvs_gpio(pdev
, pdata
, pmic_np
);
975 if (of_property_read_u32(pmic_np
,
976 "max8997,pmic-buck125-default-dvs-idx",
977 &pdata
->buck125_default_idx
)) {
978 pdata
->buck125_default_idx
= 0;
980 if (pdata
->buck125_default_idx
>= 8) {
981 pdata
->buck125_default_idx
= 0;
982 dev_info(&pdev
->dev
, "invalid value for default dvs index, using 0 instead\n");
986 if (of_get_property(pmic_np
,
987 "max8997,pmic-ignore-gpiodvs-side-effect", NULL
))
988 pdata
->ignore_gpiodvs_side_effect
= true;
993 if (of_property_read_u32_array(pmic_np
,
994 "max8997,pmic-buck1-dvs-voltage",
995 pdata
->buck1_voltage
, dvs_voltage_nr
)) {
996 dev_err(&pdev
->dev
, "buck1 voltages not specified\n");
1000 if (of_property_read_u32_array(pmic_np
,
1001 "max8997,pmic-buck2-dvs-voltage",
1002 pdata
->buck2_voltage
, dvs_voltage_nr
)) {
1003 dev_err(&pdev
->dev
, "buck2 voltages not specified\n");
1007 if (of_property_read_u32_array(pmic_np
,
1008 "max8997,pmic-buck5-dvs-voltage",
1009 pdata
->buck5_voltage
, dvs_voltage_nr
)) {
1010 dev_err(&pdev
->dev
, "buck5 voltages not specified\n");
1017 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
1018 struct max8997_platform_data
*pdata
)
1022 #endif /* CONFIG_OF */
1024 static int max8997_pmic_probe(struct platform_device
*pdev
)
1026 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
1027 struct max8997_platform_data
*pdata
= iodev
->pdata
;
1028 struct regulator_config config
= { };
1029 struct regulator_dev
*rdev
;
1030 struct max8997_data
*max8997
;
1031 struct i2c_client
*i2c
;
1033 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
1036 dev_err(&pdev
->dev
, "No platform init data supplied.\n");
1040 if (iodev
->dev
->of_node
) {
1041 ret
= max8997_pmic_dt_parse_pdata(pdev
, pdata
);
1046 max8997
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_data
),
1051 max8997
->dev
= &pdev
->dev
;
1052 max8997
->iodev
= iodev
;
1053 max8997
->num_regulators
= pdata
->num_regulators
;
1054 platform_set_drvdata(pdev
, max8997
);
1055 i2c
= max8997
->iodev
->i2c
;
1057 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
1058 max8997
->buck1_gpiodvs
= pdata
->buck1_gpiodvs
;
1059 max8997
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
1060 max8997
->buck5_gpiodvs
= pdata
->buck5_gpiodvs
;
1061 memcpy(max8997
->buck125_gpios
, pdata
->buck125_gpios
, sizeof(int) * 3);
1062 max8997
->ignore_gpiodvs_side_effect
= pdata
->ignore_gpiodvs_side_effect
;
1064 nr_dvs
= (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1065 pdata
->buck5_gpiodvs
) ? 8 : 1;
1067 for (i
= 0; i
< nr_dvs
; i
++) {
1068 max8997
->buck1_vol
[i
] = ret
=
1069 max8997_get_voltage_proper_val(
1070 &buck1245_voltage_map_desc
,
1071 pdata
->buck1_voltage
[i
],
1072 pdata
->buck1_voltage
[i
] +
1073 buck1245_voltage_map_desc
.step
);
1077 max8997
->buck2_vol
[i
] = ret
=
1078 max8997_get_voltage_proper_val(
1079 &buck1245_voltage_map_desc
,
1080 pdata
->buck2_voltage
[i
],
1081 pdata
->buck2_voltage
[i
] +
1082 buck1245_voltage_map_desc
.step
);
1086 max8997
->buck5_vol
[i
] = ret
=
1087 max8997_get_voltage_proper_val(
1088 &buck1245_voltage_map_desc
,
1089 pdata
->buck5_voltage
[i
],
1090 pdata
->buck5_voltage
[i
] +
1091 buck1245_voltage_map_desc
.step
);
1095 if (max_buck1
< max8997
->buck1_vol
[i
])
1096 max_buck1
= max8997
->buck1_vol
[i
];
1097 if (max_buck2
< max8997
->buck2_vol
[i
])
1098 max_buck2
= max8997
->buck2_vol
[i
];
1099 if (max_buck5
< max8997
->buck5_vol
[i
])
1100 max_buck5
= max8997
->buck5_vol
[i
];
1103 /* For the safety, set max voltage before setting up */
1104 for (i
= 0; i
< 8; i
++) {
1105 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1107 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1109 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1113 /* Initialize all the DVS related BUCK registers */
1114 for (i
= 0; i
< nr_dvs
; i
++) {
1115 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1116 max8997
->buck1_vol
[i
],
1118 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1119 max8997
->buck2_vol
[i
],
1121 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1122 max8997
->buck5_vol
[i
],
1127 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1128 * If at least one of them cares, set gpios.
1130 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1131 pdata
->buck5_gpiodvs
) {
1133 if (!gpio_is_valid(pdata
->buck125_gpios
[0]) ||
1134 !gpio_is_valid(pdata
->buck125_gpios
[1]) ||
1135 !gpio_is_valid(pdata
->buck125_gpios
[2])) {
1136 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
1140 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[0],
1145 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[1],
1150 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[2],
1155 gpio_direction_output(pdata
->buck125_gpios
[0],
1156 (max8997
->buck125_gpioindex
>> 2)
1158 gpio_direction_output(pdata
->buck125_gpios
[1],
1159 (max8997
->buck125_gpioindex
>> 1)
1161 gpio_direction_output(pdata
->buck125_gpios
[2],
1162 (max8997
->buck125_gpioindex
>> 0)
1166 /* DVS-GPIO disabled */
1167 max8997_update_reg(i2c
, MAX8997_REG_BUCK1CTRL
, (pdata
->buck1_gpiodvs
) ?
1168 (1 << 1) : (0 << 1), 1 << 1);
1169 max8997_update_reg(i2c
, MAX8997_REG_BUCK2CTRL
, (pdata
->buck2_gpiodvs
) ?
1170 (1 << 1) : (0 << 1), 1 << 1);
1171 max8997_update_reg(i2c
, MAX8997_REG_BUCK5CTRL
, (pdata
->buck5_gpiodvs
) ?
1172 (1 << 1) : (0 << 1), 1 << 1);
1175 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1176 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1178 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1179 const struct voltage_map_desc
*desc
;
1180 int id
= pdata
->regulators
[i
].id
;
1182 desc
= reg_voltage_map
[id
];
1184 regulators
[id
].n_voltages
=
1185 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1186 } else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
) {
1187 regulators
[id
].volt_table
= safeoutvolt
;
1188 regulators
[id
].n_voltages
= ARRAY_SIZE(safeoutvolt
);
1189 } else if (id
== MAX8997_CHARGER_CV
) {
1190 regulators
[id
].n_voltages
= 16;
1193 config
.dev
= max8997
->dev
;
1194 config
.init_data
= pdata
->regulators
[i
].initdata
;
1195 config
.driver_data
= max8997
;
1196 config
.of_node
= pdata
->regulators
[i
].reg_node
;
1198 rdev
= devm_regulator_register(&pdev
->dev
, ®ulators
[id
],
1201 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1203 return PTR_ERR(rdev
);
1210 static const struct platform_device_id max8997_pmic_id
[] = {
1211 { "max8997-pmic", 0},
1214 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1216 static struct platform_driver max8997_pmic_driver
= {
1218 .name
= "max8997-pmic",
1220 .probe
= max8997_pmic_probe
,
1221 .id_table
= max8997_pmic_id
,
1224 static int __init
max8997_pmic_init(void)
1226 return platform_driver_register(&max8997_pmic_driver
);
1228 subsys_initcall(max8997_pmic_init
);
1230 static void __exit
max8997_pmic_cleanup(void)
1232 platform_driver_unregister(&max8997_pmic_driver
);
1234 module_exit(max8997_pmic_cleanup
);
1236 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1237 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1238 MODULE_LICENSE("GPL");