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 const unsigned int safeoutvolt
[] = {
64 static inline void max8997_set_gpio(struct max8997_data
*max8997
)
66 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
67 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
68 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
70 gpio_set_value(max8997
->buck125_gpios
[0], set1
);
71 gpio_set_value(max8997
->buck125_gpios
[1], set2
);
72 gpio_set_value(max8997
->buck125_gpios
[2], set3
);
75 struct voltage_map_desc
{
81 /* Voltage maps in uV */
82 static const struct voltage_map_desc ldo_voltage_map_desc
= {
83 .min
= 800000, .max
= 3950000, .step
= 50000,
84 }; /* LDO1 ~ 18, 21 all */
86 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
87 .min
= 650000, .max
= 2225000, .step
= 25000,
88 }; /* Buck1, 2, 4, 5 */
90 static const struct voltage_map_desc buck37_voltage_map_desc
= {
91 .min
= 750000, .max
= 3900000, .step
= 50000,
94 /* current map in uA */
95 static const struct voltage_map_desc charger_current_map_desc
= {
96 .min
= 200000, .max
= 950000, .step
= 50000,
99 static const struct voltage_map_desc topoff_current_map_desc
= {
100 .min
= 50000, .max
= 200000, .step
= 10000,
103 static const struct voltage_map_desc
*reg_voltage_map
[] = {
104 [MAX8997_LDO1
] = &ldo_voltage_map_desc
,
105 [MAX8997_LDO2
] = &ldo_voltage_map_desc
,
106 [MAX8997_LDO3
] = &ldo_voltage_map_desc
,
107 [MAX8997_LDO4
] = &ldo_voltage_map_desc
,
108 [MAX8997_LDO5
] = &ldo_voltage_map_desc
,
109 [MAX8997_LDO6
] = &ldo_voltage_map_desc
,
110 [MAX8997_LDO7
] = &ldo_voltage_map_desc
,
111 [MAX8997_LDO8
] = &ldo_voltage_map_desc
,
112 [MAX8997_LDO9
] = &ldo_voltage_map_desc
,
113 [MAX8997_LDO10
] = &ldo_voltage_map_desc
,
114 [MAX8997_LDO11
] = &ldo_voltage_map_desc
,
115 [MAX8997_LDO12
] = &ldo_voltage_map_desc
,
116 [MAX8997_LDO13
] = &ldo_voltage_map_desc
,
117 [MAX8997_LDO14
] = &ldo_voltage_map_desc
,
118 [MAX8997_LDO15
] = &ldo_voltage_map_desc
,
119 [MAX8997_LDO16
] = &ldo_voltage_map_desc
,
120 [MAX8997_LDO17
] = &ldo_voltage_map_desc
,
121 [MAX8997_LDO18
] = &ldo_voltage_map_desc
,
122 [MAX8997_LDO21
] = &ldo_voltage_map_desc
,
123 [MAX8997_BUCK1
] = &buck1245_voltage_map_desc
,
124 [MAX8997_BUCK2
] = &buck1245_voltage_map_desc
,
125 [MAX8997_BUCK3
] = &buck37_voltage_map_desc
,
126 [MAX8997_BUCK4
] = &buck1245_voltage_map_desc
,
127 [MAX8997_BUCK5
] = &buck1245_voltage_map_desc
,
128 [MAX8997_BUCK6
] = NULL
,
129 [MAX8997_BUCK7
] = &buck37_voltage_map_desc
,
130 [MAX8997_EN32KHZ_AP
] = NULL
,
131 [MAX8997_EN32KHZ_CP
] = NULL
,
132 [MAX8997_ENVICHG
] = NULL
,
133 [MAX8997_ESAFEOUT1
] = NULL
,
134 [MAX8997_ESAFEOUT2
] = NULL
,
135 [MAX8997_CHARGER_CV
] = NULL
,
136 [MAX8997_CHARGER
] = &charger_current_map_desc
,
137 [MAX8997_CHARGER_TOPOFF
] = &topoff_current_map_desc
,
140 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
141 unsigned int selector
)
143 int rid
= rdev_get_id(rdev
);
145 if (rid
!= MAX8997_CHARGER_CV
)
152 return 4000000 + 20000 * (selector
- 0x01);
162 static int max8997_list_voltage(struct regulator_dev
*rdev
,
163 unsigned int selector
)
165 const struct voltage_map_desc
*desc
;
166 int rid
= rdev_get_id(rdev
);
169 if (rid
>= ARRAY_SIZE(reg_voltage_map
) ||
173 desc
= reg_voltage_map
[rid
];
177 val
= desc
->min
+ desc
->step
* selector
;
184 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
185 int *reg
, int *mask
, int *pattern
)
187 int rid
= rdev_get_id(rdev
);
190 case MAX8997_LDO1
... MAX8997_LDO21
:
191 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
196 *reg
= MAX8997_REG_BUCK1CTRL
;
201 *reg
= MAX8997_REG_BUCK2CTRL
;
206 *reg
= MAX8997_REG_BUCK3CTRL
;
211 *reg
= MAX8997_REG_BUCK4CTRL
;
216 *reg
= MAX8997_REG_BUCK5CTRL
;
221 *reg
= MAX8997_REG_BUCK6CTRL
;
226 *reg
= MAX8997_REG_BUCK7CTRL
;
230 case MAX8997_EN32KHZ_AP
... MAX8997_EN32KHZ_CP
:
231 *reg
= MAX8997_REG_MAINCON1
;
232 *mask
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
233 *pattern
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
235 case MAX8997_ENVICHG
:
236 *reg
= MAX8997_REG_MBCCTRL1
;
240 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
241 *reg
= MAX8997_REG_SAFEOUTCTRL
;
242 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
243 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
245 case MAX8997_CHARGER
:
246 *reg
= MAX8997_REG_MBCCTRL2
;
251 /* Not controllable or not exists */
258 static int max8997_reg_is_enabled(struct regulator_dev
*rdev
)
260 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
261 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
262 int ret
, reg
, mask
, pattern
;
265 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
269 ret
= max8997_read_reg(i2c
, reg
, &val
);
273 return (val
& mask
) == pattern
;
276 static int max8997_reg_enable(struct regulator_dev
*rdev
)
278 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
279 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
280 int ret
, reg
, mask
, pattern
;
282 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
286 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
289 static int max8997_reg_disable(struct regulator_dev
*rdev
)
291 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
292 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
293 int ret
, reg
, mask
, pattern
;
295 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
299 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
302 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
303 int *_reg
, int *_shift
, int *_mask
)
305 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
306 int rid
= rdev_get_id(rdev
);
307 int reg
, shift
= 0, mask
= 0x3f;
310 case MAX8997_LDO1
... MAX8997_LDO21
:
311 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
314 reg
= MAX8997_REG_BUCK1DVS1
;
315 if (max8997
->buck1_gpiodvs
)
316 reg
+= max8997
->buck125_gpioindex
;
319 reg
= MAX8997_REG_BUCK2DVS1
;
320 if (max8997
->buck2_gpiodvs
)
321 reg
+= max8997
->buck125_gpioindex
;
324 reg
= MAX8997_REG_BUCK3DVS
;
327 reg
= MAX8997_REG_BUCK4DVS
;
330 reg
= MAX8997_REG_BUCK5DVS1
;
331 if (max8997
->buck5_gpiodvs
)
332 reg
+= max8997
->buck125_gpioindex
;
335 reg
= MAX8997_REG_BUCK7DVS
;
337 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
338 reg
= MAX8997_REG_SAFEOUTCTRL
;
339 shift
= (rid
== MAX8997_ESAFEOUT2
) ? 2 : 0;
342 case MAX8997_CHARGER_CV
:
343 reg
= MAX8997_REG_MBCCTRL3
;
347 case MAX8997_CHARGER
:
348 reg
= MAX8997_REG_MBCCTRL4
;
352 case MAX8997_CHARGER_TOPOFF
:
353 reg
= MAX8997_REG_MBCCTRL5
;
368 static int max8997_get_voltage_sel(struct regulator_dev
*rdev
)
370 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
371 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
372 int reg
, shift
, mask
, ret
;
375 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
379 ret
= max8997_read_reg(i2c
, reg
, &val
);
389 static inline int max8997_get_voltage_proper_val(
390 const struct voltage_map_desc
*desc
,
391 int min_vol
, int max_vol
)
398 if (max_vol
< desc
->min
|| min_vol
> desc
->max
)
401 if (min_vol
< desc
->min
)
404 i
= DIV_ROUND_UP(min_vol
- desc
->min
, desc
->step
);
406 if (desc
->min
+ desc
->step
* i
> max_vol
)
412 static int max8997_set_voltage_charger_cv(struct regulator_dev
*rdev
,
413 int min_uV
, int max_uV
, unsigned *selector
)
415 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
416 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
417 int rid
= rdev_get_id(rdev
);
419 int reg
, shift
= 0, mask
, ret
= 0;
422 if (rid
!= MAX8997_CHARGER_CV
)
425 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
429 if (max_uV
< 4000000 || min_uV
> 4350000)
432 if (min_uV
<= 4000000) {
433 if (max_uV
>= 4000000)
437 } else if (min_uV
<= 4200000 && max_uV
>= 4200000)
440 lb
= (min_uV
- 4000001) / 20000 + 2;
441 ub
= (max_uV
- 4000000) / 20000 + 1;
458 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
);
464 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
465 * BUCK1, 2, and 5 are available if they are not controlled by gpio
467 static int max8997_set_voltage_ldobuck(struct regulator_dev
*rdev
,
468 int min_uV
, int max_uV
, unsigned *selector
)
470 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
471 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
472 const struct voltage_map_desc
*desc
;
473 int rid
= rdev_get_id(rdev
);
474 int i
, reg
, shift
, mask
, ret
;
477 case MAX8997_LDO1
... MAX8997_LDO21
:
479 case MAX8997_BUCK1
... MAX8997_BUCK5
:
485 case MAX8997_CHARGER
:
487 case MAX8997_CHARGER_TOPOFF
:
493 desc
= reg_voltage_map
[rid
];
495 i
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
499 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
503 ret
= max8997_update_reg(i2c
, reg
, i
<< shift
, mask
<< shift
);
509 static int max8997_set_voltage_buck_time_sel(struct regulator_dev
*rdev
,
510 unsigned int old_selector
,
511 unsigned int new_selector
)
513 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
514 int rid
= rdev_get_id(rdev
);
515 const struct voltage_map_desc
*desc
= reg_voltage_map
[rid
];
517 /* Delay is required only if the voltage is increasing */
518 if (old_selector
>= new_selector
)
521 /* No need to delay if gpio_dvs_mode */
524 if (max8997
->buck1_gpiodvs
)
528 if (max8997
->buck2_gpiodvs
)
532 if (max8997
->buck5_gpiodvs
)
542 return DIV_ROUND_UP(desc
->step
* (new_selector
- old_selector
),
543 max8997
->ramp_delay
* 1000);
550 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
552 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
553 * of one of the bucks may affect that of another buck, which is the side
554 * effect of the change (set_voltage). This function examines the GPIO-DVS
555 * configurations and checks whether such side-effect exists.
557 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
558 u8 new_val
, int *best
)
560 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
561 int rid
= rdev_get_id(rdev
);
563 bool buckx_gpiodvs
[3];
565 int min_side_effect
= INT_MAX
;
584 buckx_val
[0] = max8997
->buck1_vol
;
585 buckx_val
[1] = max8997
->buck2_vol
;
586 buckx_val
[2] = max8997
->buck5_vol
;
587 buckx_gpiodvs
[0] = max8997
->buck1_gpiodvs
;
588 buckx_gpiodvs
[1] = max8997
->buck2_gpiodvs
;
589 buckx_gpiodvs
[2] = max8997
->buck5_gpiodvs
;
591 for (i
= 0; i
< 8; i
++) {
594 if (new_val
!= (buckx_val
[rid
])[i
]) {
600 for (others
= 0; others
< 3; others
++) {
605 if (buckx_gpiodvs
[others
] == false)
606 continue; /* Not affected */
607 diff
= (buckx_val
[others
])[i
] -
608 (buckx_val
[others
])[max8997
->buck125_gpioindex
];
610 side_effect
[i
] += diff
;
612 side_effect
[i
] -= diff
;
614 if (side_effect
[i
] == 0) {
616 return 0; /* NO SIDE EFFECT! Use This! */
618 if (side_effect
[i
] < min_side_effect
) {
619 min_side_effect
= side_effect
[i
];
627 return side_effect
[*best
];
631 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
632 * max8997_set_voltage_ldobuck to do the job.
634 static int max8997_set_voltage_buck(struct regulator_dev
*rdev
,
635 int min_uV
, int max_uV
, unsigned *selector
)
637 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
638 int rid
= rdev_get_id(rdev
);
639 const struct voltage_map_desc
*desc
;
640 int new_val
, new_idx
, damage
, tmp_val
, tmp_idx
, tmp_dmg
;
641 bool gpio_dvs_mode
= false;
643 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
648 if (max8997
->buck1_gpiodvs
)
649 gpio_dvs_mode
= true;
652 if (max8997
->buck2_gpiodvs
)
653 gpio_dvs_mode
= true;
656 if (max8997
->buck5_gpiodvs
)
657 gpio_dvs_mode
= true;
662 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
665 desc
= reg_voltage_map
[rid
];
666 new_val
= max8997_get_voltage_proper_val(desc
, min_uV
, max_uV
);
674 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
678 if (tmp_dmg
> damage
) {
685 } while (desc
->min
+ desc
->step
* new_val
<= desc
->max
);
690 if (max8997
->ignore_gpiodvs_side_effect
== false)
693 dev_warn(&rdev
->dev
, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
694 " %d -> %d\n", max8997
->buck125_gpioindex
, tmp_idx
);
697 if (new_idx
< 0 || new_val
< 0)
700 max8997
->buck125_gpioindex
= new_idx
;
701 max8997_set_gpio(max8997
);
707 /* For SAFEOUT1 and SAFEOUT2 */
708 static int max8997_set_voltage_safeout_sel(struct regulator_dev
*rdev
,
711 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
712 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
713 int rid
= rdev_get_id(rdev
);
714 int reg
, shift
= 0, mask
, ret
;
716 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
719 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
723 return max8997_update_reg(i2c
, reg
, selector
<< shift
, mask
<< shift
);
726 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
728 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
729 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
730 int ret
, reg
, mask
, pattern
;
731 int rid
= rdev_get_id(rdev
);
733 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
737 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
739 if (rid
== MAX8997_LDO1
||
740 rid
== MAX8997_LDO10
||
741 rid
== MAX8997_LDO21
) {
742 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
744 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
747 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
748 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
750 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
753 static struct regulator_ops max8997_ldo_ops
= {
754 .list_voltage
= max8997_list_voltage
,
755 .is_enabled
= max8997_reg_is_enabled
,
756 .enable
= max8997_reg_enable
,
757 .disable
= max8997_reg_disable
,
758 .get_voltage_sel
= max8997_get_voltage_sel
,
759 .set_voltage
= max8997_set_voltage_ldobuck
,
760 .set_suspend_disable
= max8997_reg_disable_suspend
,
763 static struct regulator_ops max8997_buck_ops
= {
764 .list_voltage
= max8997_list_voltage
,
765 .is_enabled
= max8997_reg_is_enabled
,
766 .enable
= max8997_reg_enable
,
767 .disable
= max8997_reg_disable
,
768 .get_voltage_sel
= max8997_get_voltage_sel
,
769 .set_voltage
= max8997_set_voltage_buck
,
770 .set_voltage_time_sel
= max8997_set_voltage_buck_time_sel
,
771 .set_suspend_disable
= max8997_reg_disable_suspend
,
774 static struct regulator_ops max8997_fixedvolt_ops
= {
775 .list_voltage
= max8997_list_voltage
,
776 .is_enabled
= max8997_reg_is_enabled
,
777 .enable
= max8997_reg_enable
,
778 .disable
= max8997_reg_disable
,
779 .set_suspend_disable
= max8997_reg_disable_suspend
,
782 static struct regulator_ops max8997_safeout_ops
= {
783 .list_voltage
= regulator_list_voltage_table
,
784 .is_enabled
= max8997_reg_is_enabled
,
785 .enable
= max8997_reg_enable
,
786 .disable
= max8997_reg_disable
,
787 .get_voltage_sel
= max8997_get_voltage_sel
,
788 .set_voltage_sel
= max8997_set_voltage_safeout_sel
,
789 .set_suspend_disable
= max8997_reg_disable_suspend
,
792 static struct regulator_ops max8997_fixedstate_ops
= {
793 .list_voltage
= max8997_list_voltage_charger_cv
,
794 .get_voltage_sel
= max8997_get_voltage_sel
,
795 .set_voltage
= max8997_set_voltage_charger_cv
,
798 static int max8997_set_current_limit(struct regulator_dev
*rdev
,
799 int min_uA
, int max_uA
)
802 int rid
= rdev_get_id(rdev
);
804 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
807 /* Reuse max8997_set_voltage_ldobuck to set current_limit. */
808 return max8997_set_voltage_ldobuck(rdev
, min_uA
, max_uA
, &dummy
);
811 static int max8997_get_current_limit(struct regulator_dev
*rdev
)
813 int sel
, rid
= rdev_get_id(rdev
);
815 if (rid
!= MAX8997_CHARGER
&& rid
!= MAX8997_CHARGER_TOPOFF
)
818 sel
= max8997_get_voltage_sel(rdev
);
822 /* Reuse max8997_list_voltage to get current_limit. */
823 return max8997_list_voltage(rdev
, sel
);
826 static struct regulator_ops max8997_charger_ops
= {
827 .is_enabled
= max8997_reg_is_enabled
,
828 .enable
= max8997_reg_enable
,
829 .disable
= max8997_reg_disable
,
830 .get_current_limit
= max8997_get_current_limit
,
831 .set_current_limit
= max8997_set_current_limit
,
834 static struct regulator_ops max8997_charger_fixedstate_ops
= {
835 .get_current_limit
= max8997_get_current_limit
,
836 .set_current_limit
= max8997_set_current_limit
,
839 #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\
841 .id = MAX8997_##_name, \
843 .type = REGULATOR_VOLTAGE, \
844 .owner = THIS_MODULE, \
847 #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\
849 .id = MAX8997_##_name, \
851 .type = REGULATOR_CURRENT, \
852 .owner = THIS_MODULE, \
855 static struct regulator_desc regulators
[] = {
856 MAX8997_VOLTAGE_REGULATOR(LDO1
, max8997_ldo_ops
),
857 MAX8997_VOLTAGE_REGULATOR(LDO2
, max8997_ldo_ops
),
858 MAX8997_VOLTAGE_REGULATOR(LDO3
, max8997_ldo_ops
),
859 MAX8997_VOLTAGE_REGULATOR(LDO4
, max8997_ldo_ops
),
860 MAX8997_VOLTAGE_REGULATOR(LDO5
, max8997_ldo_ops
),
861 MAX8997_VOLTAGE_REGULATOR(LDO6
, max8997_ldo_ops
),
862 MAX8997_VOLTAGE_REGULATOR(LDO7
, max8997_ldo_ops
),
863 MAX8997_VOLTAGE_REGULATOR(LDO8
, max8997_ldo_ops
),
864 MAX8997_VOLTAGE_REGULATOR(LDO9
, max8997_ldo_ops
),
865 MAX8997_VOLTAGE_REGULATOR(LDO10
, max8997_ldo_ops
),
866 MAX8997_VOLTAGE_REGULATOR(LDO11
, max8997_ldo_ops
),
867 MAX8997_VOLTAGE_REGULATOR(LDO12
, max8997_ldo_ops
),
868 MAX8997_VOLTAGE_REGULATOR(LDO13
, max8997_ldo_ops
),
869 MAX8997_VOLTAGE_REGULATOR(LDO14
, max8997_ldo_ops
),
870 MAX8997_VOLTAGE_REGULATOR(LDO15
, max8997_ldo_ops
),
871 MAX8997_VOLTAGE_REGULATOR(LDO16
, max8997_ldo_ops
),
872 MAX8997_VOLTAGE_REGULATOR(LDO17
, max8997_ldo_ops
),
873 MAX8997_VOLTAGE_REGULATOR(LDO18
, max8997_ldo_ops
),
874 MAX8997_VOLTAGE_REGULATOR(LDO21
, max8997_ldo_ops
),
875 MAX8997_VOLTAGE_REGULATOR(BUCK1
, max8997_buck_ops
),
876 MAX8997_VOLTAGE_REGULATOR(BUCK2
, max8997_buck_ops
),
877 MAX8997_VOLTAGE_REGULATOR(BUCK3
, max8997_buck_ops
),
878 MAX8997_VOLTAGE_REGULATOR(BUCK4
, max8997_buck_ops
),
879 MAX8997_VOLTAGE_REGULATOR(BUCK5
, max8997_buck_ops
),
880 MAX8997_VOLTAGE_REGULATOR(BUCK6
, max8997_fixedvolt_ops
),
881 MAX8997_VOLTAGE_REGULATOR(BUCK7
, max8997_buck_ops
),
882 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP
, max8997_fixedvolt_ops
),
883 MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP
, max8997_fixedvolt_ops
),
884 MAX8997_VOLTAGE_REGULATOR(ENVICHG
, max8997_fixedvolt_ops
),
885 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1
, max8997_safeout_ops
),
886 MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2
, max8997_safeout_ops
),
887 MAX8997_VOLTAGE_REGULATOR(CHARGER_CV
, max8997_fixedstate_ops
),
888 MAX8997_CURRENT_REGULATOR(CHARGER
, max8997_charger_ops
),
889 MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF
,
890 max8997_charger_fixedstate_ops
),
894 static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device
*pdev
,
895 struct max8997_platform_data
*pdata
,
896 struct device_node
*pmic_np
)
900 for (i
= 0; i
< 3; i
++) {
901 gpio
= of_get_named_gpio(pmic_np
,
902 "max8997,pmic-buck125-dvs-gpios", i
);
903 if (!gpio_is_valid(gpio
)) {
904 dev_err(&pdev
->dev
, "invalid gpio[%d]: %d\n", i
, gpio
);
907 pdata
->buck125_gpios
[i
] = gpio
;
912 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
913 struct max8997_platform_data
*pdata
)
915 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
916 struct device_node
*pmic_np
, *regulators_np
, *reg_np
;
917 struct max8997_regulator_data
*rdata
;
918 unsigned int i
, dvs_voltage_nr
= 1, ret
;
920 pmic_np
= of_node_get(iodev
->dev
->of_node
);
922 dev_err(&pdev
->dev
, "could not find pmic sub-node\n");
926 regulators_np
= of_find_node_by_name(pmic_np
, "regulators");
927 if (!regulators_np
) {
928 dev_err(&pdev
->dev
, "could not find regulators sub-node\n");
932 /* count the number of regulators to be supported in pmic */
933 pdata
->num_regulators
= of_get_child_count(regulators_np
);
935 rdata
= devm_kzalloc(&pdev
->dev
, sizeof(*rdata
) *
936 pdata
->num_regulators
, GFP_KERNEL
);
938 of_node_put(regulators_np
);
939 dev_err(&pdev
->dev
, "could not allocate memory for regulator data\n");
943 pdata
->regulators
= rdata
;
944 for_each_child_of_node(regulators_np
, reg_np
) {
945 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++)
946 if (!of_node_cmp(reg_np
->name
, regulators
[i
].name
))
949 if (i
== ARRAY_SIZE(regulators
)) {
950 dev_warn(&pdev
->dev
, "don't know how to configure regulator %s\n",
956 rdata
->initdata
= of_get_regulator_init_data(&pdev
->dev
,
958 rdata
->reg_node
= reg_np
;
961 of_node_put(regulators_np
);
963 if (of_get_property(pmic_np
, "max8997,pmic-buck1-uses-gpio-dvs", NULL
))
964 pdata
->buck1_gpiodvs
= true;
966 if (of_get_property(pmic_np
, "max8997,pmic-buck2-uses-gpio-dvs", NULL
))
967 pdata
->buck2_gpiodvs
= true;
969 if (of_get_property(pmic_np
, "max8997,pmic-buck5-uses-gpio-dvs", NULL
))
970 pdata
->buck5_gpiodvs
= true;
972 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
973 pdata
->buck5_gpiodvs
) {
974 ret
= max8997_pmic_dt_parse_dvs_gpio(pdev
, pdata
, pmic_np
);
978 if (of_property_read_u32(pmic_np
,
979 "max8997,pmic-buck125-default-dvs-idx",
980 &pdata
->buck125_default_idx
)) {
981 pdata
->buck125_default_idx
= 0;
983 if (pdata
->buck125_default_idx
>= 8) {
984 pdata
->buck125_default_idx
= 0;
985 dev_info(&pdev
->dev
, "invalid value for default dvs index, using 0 instead\n");
989 if (of_get_property(pmic_np
,
990 "max8997,pmic-ignore-gpiodvs-side-effect", NULL
))
991 pdata
->ignore_gpiodvs_side_effect
= true;
996 if (of_property_read_u32_array(pmic_np
,
997 "max8997,pmic-buck1-dvs-voltage",
998 pdata
->buck1_voltage
, dvs_voltage_nr
)) {
999 dev_err(&pdev
->dev
, "buck1 voltages not specified\n");
1003 if (of_property_read_u32_array(pmic_np
,
1004 "max8997,pmic-buck2-dvs-voltage",
1005 pdata
->buck2_voltage
, dvs_voltage_nr
)) {
1006 dev_err(&pdev
->dev
, "buck2 voltages not specified\n");
1010 if (of_property_read_u32_array(pmic_np
,
1011 "max8997,pmic-buck5-dvs-voltage",
1012 pdata
->buck5_voltage
, dvs_voltage_nr
)) {
1013 dev_err(&pdev
->dev
, "buck5 voltages not specified\n");
1020 static int max8997_pmic_dt_parse_pdata(struct platform_device
*pdev
,
1021 struct max8997_platform_data
*pdata
)
1025 #endif /* CONFIG_OF */
1027 static int max8997_pmic_probe(struct platform_device
*pdev
)
1029 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
1030 struct max8997_platform_data
*pdata
= iodev
->pdata
;
1031 struct regulator_config config
= { };
1032 struct regulator_dev
**rdev
;
1033 struct max8997_data
*max8997
;
1034 struct i2c_client
*i2c
;
1035 int i
, ret
, size
, nr_dvs
;
1036 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
1039 dev_err(&pdev
->dev
, "No platform init data supplied.\n");
1043 if (iodev
->dev
->of_node
) {
1044 ret
= max8997_pmic_dt_parse_pdata(pdev
, pdata
);
1049 max8997
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_data
),
1054 size
= sizeof(struct regulator_dev
*) * pdata
->num_regulators
;
1055 max8997
->rdev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
1059 rdev
= max8997
->rdev
;
1060 max8997
->dev
= &pdev
->dev
;
1061 max8997
->iodev
= iodev
;
1062 max8997
->num_regulators
= pdata
->num_regulators
;
1063 platform_set_drvdata(pdev
, max8997
);
1064 i2c
= max8997
->iodev
->i2c
;
1066 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
1067 max8997
->buck1_gpiodvs
= pdata
->buck1_gpiodvs
;
1068 max8997
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
1069 max8997
->buck5_gpiodvs
= pdata
->buck5_gpiodvs
;
1070 memcpy(max8997
->buck125_gpios
, pdata
->buck125_gpios
, sizeof(int) * 3);
1071 max8997
->ignore_gpiodvs_side_effect
= pdata
->ignore_gpiodvs_side_effect
;
1073 nr_dvs
= (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1074 pdata
->buck5_gpiodvs
) ? 8 : 1;
1076 for (i
= 0; i
< nr_dvs
; i
++) {
1077 max8997
->buck1_vol
[i
] = ret
=
1078 max8997_get_voltage_proper_val(
1079 &buck1245_voltage_map_desc
,
1080 pdata
->buck1_voltage
[i
],
1081 pdata
->buck1_voltage
[i
] +
1082 buck1245_voltage_map_desc
.step
);
1086 max8997
->buck2_vol
[i
] = ret
=
1087 max8997_get_voltage_proper_val(
1088 &buck1245_voltage_map_desc
,
1089 pdata
->buck2_voltage
[i
],
1090 pdata
->buck2_voltage
[i
] +
1091 buck1245_voltage_map_desc
.step
);
1095 max8997
->buck5_vol
[i
] = ret
=
1096 max8997_get_voltage_proper_val(
1097 &buck1245_voltage_map_desc
,
1098 pdata
->buck5_voltage
[i
],
1099 pdata
->buck5_voltage
[i
] +
1100 buck1245_voltage_map_desc
.step
);
1104 if (max_buck1
< max8997
->buck1_vol
[i
])
1105 max_buck1
= max8997
->buck1_vol
[i
];
1106 if (max_buck2
< max8997
->buck2_vol
[i
])
1107 max_buck2
= max8997
->buck2_vol
[i
];
1108 if (max_buck5
< max8997
->buck5_vol
[i
])
1109 max_buck5
= max8997
->buck5_vol
[i
];
1112 /* For the safety, set max voltage before setting up */
1113 for (i
= 0; i
< 8; i
++) {
1114 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1116 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1118 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1122 /* Initialize all the DVS related BUCK registers */
1123 for (i
= 0; i
< nr_dvs
; i
++) {
1124 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1125 max8997
->buck1_vol
[i
],
1127 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1128 max8997
->buck2_vol
[i
],
1130 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1131 max8997
->buck5_vol
[i
],
1136 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1137 * If at least one of them cares, set gpios.
1139 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1140 pdata
->buck5_gpiodvs
) {
1142 if (!gpio_is_valid(pdata
->buck125_gpios
[0]) ||
1143 !gpio_is_valid(pdata
->buck125_gpios
[1]) ||
1144 !gpio_is_valid(pdata
->buck125_gpios
[2])) {
1145 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
1150 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[0],
1155 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[1],
1160 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck125_gpios
[2],
1165 gpio_direction_output(pdata
->buck125_gpios
[0],
1166 (max8997
->buck125_gpioindex
>> 2)
1168 gpio_direction_output(pdata
->buck125_gpios
[1],
1169 (max8997
->buck125_gpioindex
>> 1)
1171 gpio_direction_output(pdata
->buck125_gpios
[2],
1172 (max8997
->buck125_gpioindex
>> 0)
1176 /* DVS-GPIO disabled */
1177 max8997_update_reg(i2c
, MAX8997_REG_BUCK1CTRL
, (pdata
->buck1_gpiodvs
) ?
1178 (1 << 1) : (0 << 1), 1 << 1);
1179 max8997_update_reg(i2c
, MAX8997_REG_BUCK2CTRL
, (pdata
->buck2_gpiodvs
) ?
1180 (1 << 1) : (0 << 1), 1 << 1);
1181 max8997_update_reg(i2c
, MAX8997_REG_BUCK5CTRL
, (pdata
->buck5_gpiodvs
) ?
1182 (1 << 1) : (0 << 1), 1 << 1);
1185 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1186 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1188 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1189 const struct voltage_map_desc
*desc
;
1190 int id
= pdata
->regulators
[i
].id
;
1192 desc
= reg_voltage_map
[id
];
1194 regulators
[id
].n_voltages
=
1195 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1196 } else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
) {
1197 regulators
[id
].volt_table
= safeoutvolt
;
1198 regulators
[id
].n_voltages
= ARRAY_SIZE(safeoutvolt
);
1199 } else if (id
== MAX8997_CHARGER_CV
) {
1200 regulators
[id
].n_voltages
= 16;
1203 config
.dev
= max8997
->dev
;
1204 config
.init_data
= pdata
->regulators
[i
].initdata
;
1205 config
.driver_data
= max8997
;
1206 config
.of_node
= pdata
->regulators
[i
].reg_node
;
1208 rdev
[i
] = regulator_register(®ulators
[id
], &config
);
1209 if (IS_ERR(rdev
[i
])) {
1210 ret
= PTR_ERR(rdev
[i
]);
1211 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1221 regulator_unregister(rdev
[i
]);
1226 static int max8997_pmic_remove(struct platform_device
*pdev
)
1228 struct max8997_data
*max8997
= platform_get_drvdata(pdev
);
1229 struct regulator_dev
**rdev
= max8997
->rdev
;
1232 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1233 regulator_unregister(rdev
[i
]);
1237 static const struct platform_device_id max8997_pmic_id
[] = {
1238 { "max8997-pmic", 0},
1241 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1243 static struct platform_driver max8997_pmic_driver
= {
1245 .name
= "max8997-pmic",
1246 .owner
= THIS_MODULE
,
1248 .probe
= max8997_pmic_probe
,
1249 .remove
= max8997_pmic_remove
,
1250 .id_table
= max8997_pmic_id
,
1253 static int __init
max8997_pmic_init(void)
1255 return platform_driver_register(&max8997_pmic_driver
);
1257 subsys_initcall(max8997_pmic_init
);
1259 static void __exit
max8997_pmic_cleanup(void)
1261 platform_driver_unregister(&max8997_pmic_driver
);
1263 module_exit(max8997_pmic_cleanup
);
1265 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1266 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1267 MODULE_LICENSE("GPL");