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/delay.h>
26 #include <linux/err.h>
27 #include <linux/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>
38 struct max8997_dev
*iodev
;
40 struct regulator_dev
**rdev
;
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 inline void max8997_set_gpio(struct max8997_data
*max8997
)
58 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
59 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
60 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
62 gpio_set_value(max8997
->buck125_gpios
[0], set1
);
63 gpio_set_value(max8997
->buck125_gpios
[1], set2
);
64 gpio_set_value(max8997
->buck125_gpios
[2], set3
);
67 struct voltage_map_desc
{
74 /* Voltage maps in mV */
75 static const struct voltage_map_desc ldo_voltage_map_desc
= {
76 .min
= 800, .max
= 3950, .step
= 50, .n_bits
= 6,
77 }; /* LDO1 ~ 18, 21 all */
79 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
80 .min
= 650, .max
= 2225, .step
= 25, .n_bits
= 6,
81 }; /* Buck1, 2, 4, 5 */
83 static const struct voltage_map_desc buck37_voltage_map_desc
= {
84 .min
= 750, .max
= 3900, .step
= 50, .n_bits
= 6,
87 /* current map in mA */
88 static const struct voltage_map_desc charger_current_map_desc
= {
89 .min
= 200, .max
= 950, .step
= 50, .n_bits
= 4,
92 static const struct voltage_map_desc topoff_current_map_desc
= {
93 .min
= 50, .max
= 200, .step
= 10, .n_bits
= 4,
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 inline int max8997_get_rid(struct regulator_dev
*rdev
)
135 return rdev_get_id(rdev
);
138 static int max8997_list_voltage_safeout(struct regulator_dev
*rdev
,
139 unsigned int selector
)
141 int rid
= max8997_get_rid(rdev
);
143 if (rid
== MAX8997_ESAFEOUT1
|| rid
== MAX8997_ESAFEOUT2
) {
161 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
162 unsigned int selector
)
164 int rid
= max8997_get_rid(rdev
);
166 if (rid
!= MAX8997_CHARGER_CV
)
173 return 4000000 + 20000 * (selector
- 0x01);
183 static int max8997_list_voltage(struct regulator_dev
*rdev
,
184 unsigned int selector
)
186 const struct voltage_map_desc
*desc
;
187 int rid
= max8997_get_rid(rdev
);
190 if (rid
>= ARRAY_SIZE(reg_voltage_map
) ||
194 desc
= reg_voltage_map
[rid
];
198 val
= desc
->min
+ desc
->step
* selector
;
205 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
206 int *reg
, int *mask
, int *pattern
)
208 int rid
= max8997_get_rid(rdev
);
211 case MAX8997_LDO1
... MAX8997_LDO21
:
212 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
217 *reg
= MAX8997_REG_BUCK1CTRL
;
222 *reg
= MAX8997_REG_BUCK2CTRL
;
227 *reg
= MAX8997_REG_BUCK3CTRL
;
232 *reg
= MAX8997_REG_BUCK4CTRL
;
237 *reg
= MAX8997_REG_BUCK5CTRL
;
242 *reg
= MAX8997_REG_BUCK6CTRL
;
247 *reg
= MAX8997_REG_BUCK7CTRL
;
251 case MAX8997_EN32KHZ_AP
... MAX8997_EN32KHZ_CP
:
252 *reg
= MAX8997_REG_MAINCON1
;
253 *mask
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
254 *pattern
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
256 case MAX8997_ENVICHG
:
257 *reg
= MAX8997_REG_MBCCTRL1
;
261 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
262 *reg
= MAX8997_REG_SAFEOUTCTRL
;
263 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
264 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
266 case MAX8997_CHARGER
:
267 *reg
= MAX8997_REG_MBCCTRL2
;
272 /* Not controllable or not exists */
279 static int max8997_reg_is_enabled(struct regulator_dev
*rdev
)
281 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
282 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
283 int ret
, reg
, mask
, pattern
;
286 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
288 return 1; /* "not controllable" */
292 ret
= max8997_read_reg(i2c
, reg
, &val
);
296 return (val
& mask
) == pattern
;
299 static int max8997_reg_enable(struct regulator_dev
*rdev
)
301 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
302 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
303 int ret
, reg
, mask
, pattern
;
305 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
309 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
312 static int max8997_reg_disable(struct regulator_dev
*rdev
)
314 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
315 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
316 int ret
, reg
, mask
, pattern
;
318 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
322 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
325 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
326 int *_reg
, int *_shift
, int *_mask
)
328 int rid
= max8997_get_rid(rdev
);
329 int reg
, shift
= 0, mask
= 0x3f;
332 case MAX8997_LDO1
... MAX8997_LDO21
:
333 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
336 reg
= MAX8997_REG_BUCK1DVS1
;
339 reg
= MAX8997_REG_BUCK2DVS1
;
342 reg
= MAX8997_REG_BUCK3DVS
;
345 reg
= MAX8997_REG_BUCK4DVS
;
348 reg
= MAX8997_REG_BUCK5DVS1
;
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(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
;
389 int rid
= max8997_get_rid(rdev
);
392 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
396 if ((rid
== MAX8997_BUCK1
&& max8997
->buck1_gpiodvs
) ||
397 (rid
== MAX8997_BUCK2
&& max8997
->buck2_gpiodvs
) ||
398 (rid
== MAX8997_BUCK5
&& max8997
->buck5_gpiodvs
))
399 reg
+= max8997
->buck125_gpioindex
;
401 ret
= max8997_read_reg(i2c
, reg
, &val
);
408 if (rdev
->desc
&& rdev
->desc
->ops
&& rdev
->desc
->ops
->list_voltage
)
409 return rdev
->desc
->ops
->list_voltage(rdev
, val
);
412 * max8997_list_voltage returns value for any rdev with voltage_map,
413 * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
414 * list_voltage ops (they are current regulators).
416 return max8997_list_voltage(rdev
, val
);
419 static inline int max8997_get_voltage_proper_val(
420 const struct voltage_map_desc
*desc
,
421 int min_vol
, int max_vol
)
428 if (max_vol
< desc
->min
|| min_vol
> desc
->max
)
431 while (desc
->min
+ desc
->step
* i
< min_vol
&&
432 desc
->min
+ desc
->step
* i
< desc
->max
)
435 if (desc
->min
+ desc
->step
* i
> max_vol
)
438 if (i
>= (1 << desc
->n_bits
))
444 static int max8997_set_voltage_charger_cv(struct regulator_dev
*rdev
,
445 int min_uV
, int max_uV
, unsigned *selector
)
447 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
448 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
449 int rid
= max8997_get_rid(rdev
);
451 int reg
, shift
= 0, mask
, ret
= 0;
454 if (rid
!= MAX8997_CHARGER_CV
)
457 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
461 if (max_uV
< 4000000 || min_uV
> 4350000)
464 if (min_uV
<= 4000000) {
465 if (max_uV
>= 4000000)
469 } else if (min_uV
<= 4200000 && max_uV
>= 4200000)
472 lb
= (min_uV
- 4000001) / 20000 + 2;
473 ub
= (max_uV
- 4000000) / 20000 + 1;
490 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
);
496 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
497 * BUCK1, 2, and 5 are available if they are not controlled by gpio
499 static int max8997_set_voltage_ldobuck(struct regulator_dev
*rdev
,
500 int min_uV
, int max_uV
, unsigned *selector
)
502 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
503 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
504 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
505 const struct voltage_map_desc
*desc
;
506 int rid
= max8997_get_rid(rdev
);
507 int reg
, shift
= 0, mask
, ret
;
512 case MAX8997_LDO1
... MAX8997_LDO21
:
514 case MAX8997_BUCK1
... MAX8997_BUCK5
:
520 case MAX8997_CHARGER
:
522 case MAX8997_CHARGER_TOPOFF
:
528 desc
= reg_voltage_map
[rid
];
530 i
= max8997_get_voltage_proper_val(desc
, min_vol
, max_vol
);
534 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
538 max8997_read_reg(i2c
, reg
, &org
);
539 org
= (org
& mask
) >> shift
;
541 ret
= max8997_update_reg(i2c
, reg
, i
<< shift
, mask
<< shift
);
544 if (rid
== MAX8997_BUCK1
|| rid
== MAX8997_BUCK2
||
545 rid
== MAX8997_BUCK4
|| rid
== MAX8997_BUCK5
) {
546 /* If the voltage is increasing */
548 udelay(DIV_ROUND_UP(desc
->step
* (i
- org
),
549 max8997
->ramp_delay
));
556 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
558 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
559 * of one of the bucks may affect that of another buck, which is the side
560 * effect of the change (set_voltage). This function examines the GPIO-DVS
561 * configurations and checks whether such side-effect exists.
563 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
564 u8 new_val
, int *best
)
566 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
567 int rid
= max8997_get_rid(rdev
);
569 bool buckx_gpiodvs
[3];
571 int min_side_effect
= INT_MAX
;
590 buckx_val
[0] = max8997
->buck1_vol
;
591 buckx_val
[1] = max8997
->buck2_vol
;
592 buckx_val
[2] = max8997
->buck5_vol
;
593 buckx_gpiodvs
[0] = max8997
->buck1_gpiodvs
;
594 buckx_gpiodvs
[1] = max8997
->buck2_gpiodvs
;
595 buckx_gpiodvs
[2] = max8997
->buck5_gpiodvs
;
597 for (i
= 0; i
< 8; i
++) {
600 if (new_val
!= (buckx_val
[rid
])[i
]) {
606 for (others
= 0; others
< 3; others
++) {
611 if (buckx_gpiodvs
[others
] == false)
612 continue; /* Not affected */
613 diff
= (buckx_val
[others
])[i
] -
614 (buckx_val
[others
])[max8997
->buck125_gpioindex
];
616 side_effect
[i
] += diff
;
618 side_effect
[i
] -= diff
;
620 if (side_effect
[i
] == 0) {
622 return 0; /* NO SIDE EFFECT! Use This! */
624 if (side_effect
[i
] < min_side_effect
) {
625 min_side_effect
= side_effect
[i
];
633 return side_effect
[*best
];
637 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
638 * max8997_set_voltage_ldobuck to do the job.
640 static int max8997_set_voltage_buck(struct regulator_dev
*rdev
,
641 int min_uV
, int max_uV
, unsigned *selector
)
643 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
644 int rid
= max8997_get_rid(rdev
);
645 const struct voltage_map_desc
*desc
;
646 int new_val
, new_idx
, damage
, tmp_val
, tmp_idx
, tmp_dmg
;
647 bool gpio_dvs_mode
= false;
648 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
650 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
655 if (max8997
->buck1_gpiodvs
)
656 gpio_dvs_mode
= true;
659 if (max8997
->buck2_gpiodvs
)
660 gpio_dvs_mode
= true;
663 if (max8997
->buck5_gpiodvs
)
664 gpio_dvs_mode
= true;
669 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
672 desc
= reg_voltage_map
[rid
];
673 new_val
= max8997_get_voltage_proper_val(desc
, min_vol
, max_vol
);
681 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
685 if (tmp_dmg
> damage
) {
692 } while (desc
->min
+ desc
->step
+ new_val
<= desc
->max
);
697 if (max8997
->ignore_gpiodvs_side_effect
== false)
700 dev_warn(&rdev
->dev
, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
701 " %d -> %d\n", max8997
->buck125_gpioindex
, tmp_idx
);
704 if (new_idx
< 0 || new_val
< 0)
707 max8997
->buck125_gpioindex
= new_idx
;
708 max8997_set_gpio(max8997
);
714 static const int safeoutvolt
[] = {
721 /* For SAFEOUT1 and SAFEOUT2 */
722 static int max8997_set_voltage_safeout(struct regulator_dev
*rdev
,
723 int min_uV
, int max_uV
, unsigned *selector
)
725 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
726 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
727 int rid
= max8997_get_rid(rdev
);
728 int reg
, shift
= 0, mask
, ret
;
732 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
735 for (i
= 0; i
< ARRAY_SIZE(safeoutvolt
); i
++) {
736 if (min_uV
<= safeoutvolt
[i
] &&
737 max_uV
>= safeoutvolt
[i
])
741 if (i
>= ARRAY_SIZE(safeoutvolt
))
749 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
753 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
<< shift
);
759 static int max8997_reg_enable_suspend(struct regulator_dev
*rdev
)
764 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
766 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
767 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
768 int ret
, reg
, mask
, pattern
;
769 int rid
= max8997_get_rid(rdev
);
771 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
775 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
777 if (rid
== MAX8997_LDO1
||
778 rid
== MAX8997_LDO10
||
779 rid
== MAX8997_LDO21
) {
780 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
782 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
785 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
786 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
788 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
791 static struct regulator_ops max8997_ldo_ops
= {
792 .list_voltage
= max8997_list_voltage
,
793 .is_enabled
= max8997_reg_is_enabled
,
794 .enable
= max8997_reg_enable
,
795 .disable
= max8997_reg_disable
,
796 .get_voltage
= max8997_get_voltage
,
797 .set_voltage
= max8997_set_voltage_ldobuck
,
798 .set_suspend_enable
= max8997_reg_enable_suspend
,
799 .set_suspend_disable
= max8997_reg_disable_suspend
,
802 static struct regulator_ops max8997_buck_ops
= {
803 .list_voltage
= max8997_list_voltage
,
804 .is_enabled
= max8997_reg_is_enabled
,
805 .enable
= max8997_reg_enable
,
806 .disable
= max8997_reg_disable
,
807 .get_voltage
= max8997_get_voltage
,
808 .set_voltage
= max8997_set_voltage_buck
,
809 .set_suspend_enable
= max8997_reg_enable_suspend
,
810 .set_suspend_disable
= max8997_reg_disable_suspend
,
813 static struct regulator_ops max8997_fixedvolt_ops
= {
814 .list_voltage
= max8997_list_voltage
,
815 .is_enabled
= max8997_reg_is_enabled
,
816 .enable
= max8997_reg_enable
,
817 .disable
= max8997_reg_disable
,
818 .set_suspend_enable
= max8997_reg_enable_suspend
,
819 .set_suspend_disable
= max8997_reg_disable_suspend
,
822 static struct regulator_ops max8997_safeout_ops
= {
823 .list_voltage
= max8997_list_voltage_safeout
,
824 .is_enabled
= max8997_reg_is_enabled
,
825 .enable
= max8997_reg_enable
,
826 .disable
= max8997_reg_disable
,
827 .get_voltage
= max8997_get_voltage
,
828 .set_voltage
= max8997_set_voltage_safeout
,
829 .set_suspend_enable
= max8997_reg_enable_suspend
,
830 .set_suspend_disable
= max8997_reg_disable_suspend
,
833 static struct regulator_ops max8997_fixedstate_ops
= {
834 .list_voltage
= max8997_list_voltage_charger_cv
,
835 .get_voltage
= max8997_get_voltage
,
836 .set_voltage
= max8997_set_voltage_charger_cv
,
839 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev
*rdev
,
840 int min_uV
, int max_uV
)
844 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
, &dummy
);
848 static struct regulator_ops max8997_charger_ops
= {
849 .is_enabled
= max8997_reg_is_enabled
,
850 .enable
= max8997_reg_enable
,
851 .disable
= max8997_reg_disable
,
852 .get_current_limit
= max8997_get_voltage
,
853 .set_current_limit
= max8997_set_voltage_ldobuck_wrap
,
856 static struct regulator_ops max8997_charger_fixedstate_ops
= {
857 .is_enabled
= max8997_reg_is_enabled
,
858 .get_current_limit
= max8997_get_voltage
,
859 .set_current_limit
= max8997_set_voltage_ldobuck_wrap
,
862 #define regulator_desc_ldo(num) { \
864 .id = MAX8997_LDO##num, \
865 .ops = &max8997_ldo_ops, \
866 .type = REGULATOR_VOLTAGE, \
867 .owner = THIS_MODULE, \
869 #define regulator_desc_buck(num) { \
870 .name = "BUCK"#num, \
871 .id = MAX8997_BUCK##num, \
872 .ops = &max8997_buck_ops, \
873 .type = REGULATOR_VOLTAGE, \
874 .owner = THIS_MODULE, \
877 static struct regulator_desc regulators
[] = {
878 regulator_desc_ldo(1),
879 regulator_desc_ldo(2),
880 regulator_desc_ldo(3),
881 regulator_desc_ldo(4),
882 regulator_desc_ldo(5),
883 regulator_desc_ldo(6),
884 regulator_desc_ldo(7),
885 regulator_desc_ldo(8),
886 regulator_desc_ldo(9),
887 regulator_desc_ldo(10),
888 regulator_desc_ldo(11),
889 regulator_desc_ldo(12),
890 regulator_desc_ldo(13),
891 regulator_desc_ldo(14),
892 regulator_desc_ldo(15),
893 regulator_desc_ldo(16),
894 regulator_desc_ldo(17),
895 regulator_desc_ldo(18),
896 regulator_desc_ldo(21),
897 regulator_desc_buck(1),
898 regulator_desc_buck(2),
899 regulator_desc_buck(3),
900 regulator_desc_buck(4),
901 regulator_desc_buck(5),
905 .ops
= &max8997_fixedvolt_ops
,
906 .type
= REGULATOR_VOLTAGE
,
907 .owner
= THIS_MODULE
,
909 regulator_desc_buck(7),
911 .name
= "EN32KHz AP",
912 .id
= MAX8997_EN32KHZ_AP
,
913 .ops
= &max8997_fixedvolt_ops
,
914 .type
= REGULATOR_VOLTAGE
,
915 .owner
= THIS_MODULE
,
917 .name
= "EN32KHz CP",
918 .id
= MAX8997_EN32KHZ_CP
,
919 .ops
= &max8997_fixedvolt_ops
,
920 .type
= REGULATOR_VOLTAGE
,
921 .owner
= THIS_MODULE
,
924 .id
= MAX8997_ENVICHG
,
925 .ops
= &max8997_fixedvolt_ops
,
926 .type
= REGULATOR_VOLTAGE
,
927 .owner
= THIS_MODULE
,
930 .id
= MAX8997_ESAFEOUT1
,
931 .ops
= &max8997_safeout_ops
,
932 .type
= REGULATOR_VOLTAGE
,
933 .owner
= THIS_MODULE
,
936 .id
= MAX8997_ESAFEOUT2
,
937 .ops
= &max8997_safeout_ops
,
938 .type
= REGULATOR_VOLTAGE
,
939 .owner
= THIS_MODULE
,
941 .name
= "CHARGER CV",
942 .id
= MAX8997_CHARGER_CV
,
943 .ops
= &max8997_fixedstate_ops
,
944 .type
= REGULATOR_VOLTAGE
,
945 .owner
= THIS_MODULE
,
948 .id
= MAX8997_CHARGER
,
949 .ops
= &max8997_charger_ops
,
950 .type
= REGULATOR_CURRENT
,
951 .owner
= THIS_MODULE
,
953 .name
= "CHARGER TOPOFF",
954 .id
= MAX8997_CHARGER_TOPOFF
,
955 .ops
= &max8997_charger_fixedstate_ops
,
956 .type
= REGULATOR_CURRENT
,
957 .owner
= THIS_MODULE
,
961 static __devinit
int max8997_pmic_probe(struct platform_device
*pdev
)
963 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
964 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
965 struct regulator_dev
**rdev
;
966 struct max8997_data
*max8997
;
967 struct i2c_client
*i2c
;
969 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
972 dev_err(pdev
->dev
.parent
, "No platform init data supplied.\n");
976 max8997
= kzalloc(sizeof(struct max8997_data
), GFP_KERNEL
);
980 size
= sizeof(struct regulator_dev
*) * pdata
->num_regulators
;
981 max8997
->rdev
= kzalloc(size
, GFP_KERNEL
);
982 if (!max8997
->rdev
) {
987 rdev
= max8997
->rdev
;
988 max8997
->dev
= &pdev
->dev
;
989 max8997
->iodev
= iodev
;
990 max8997
->num_regulators
= pdata
->num_regulators
;
991 platform_set_drvdata(pdev
, max8997
);
992 i2c
= max8997
->iodev
->i2c
;
994 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
995 max8997
->buck1_gpiodvs
= pdata
->buck1_gpiodvs
;
996 max8997
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
997 max8997
->buck5_gpiodvs
= pdata
->buck5_gpiodvs
;
998 memcpy(max8997
->buck125_gpios
, pdata
->buck125_gpios
, sizeof(int) * 3);
999 max8997
->ignore_gpiodvs_side_effect
= pdata
->ignore_gpiodvs_side_effect
;
1001 for (i
= 0; i
< 8; i
++) {
1002 max8997
->buck1_vol
[i
] = ret
=
1003 max8997_get_voltage_proper_val(
1004 &buck1245_voltage_map_desc
,
1005 pdata
->buck1_voltage
[i
] / 1000,
1006 pdata
->buck1_voltage
[i
] / 1000 +
1007 buck1245_voltage_map_desc
.step
);
1011 max8997
->buck2_vol
[i
] = ret
=
1012 max8997_get_voltage_proper_val(
1013 &buck1245_voltage_map_desc
,
1014 pdata
->buck2_voltage
[i
] / 1000,
1015 pdata
->buck2_voltage
[i
] / 1000 +
1016 buck1245_voltage_map_desc
.step
);
1020 max8997
->buck5_vol
[i
] = ret
=
1021 max8997_get_voltage_proper_val(
1022 &buck1245_voltage_map_desc
,
1023 pdata
->buck5_voltage
[i
] / 1000,
1024 pdata
->buck5_voltage
[i
] / 1000 +
1025 buck1245_voltage_map_desc
.step
);
1029 if (max_buck1
< max8997
->buck1_vol
[i
])
1030 max_buck1
= max8997
->buck1_vol
[i
];
1031 if (max_buck2
< max8997
->buck2_vol
[i
])
1032 max_buck2
= max8997
->buck2_vol
[i
];
1033 if (max_buck5
< max8997
->buck5_vol
[i
])
1034 max_buck5
= max8997
->buck5_vol
[i
];
1037 /* For the safety, set max voltage before setting up */
1038 for (i
= 0; i
< 8; i
++) {
1039 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1041 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1043 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1048 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1049 * If at least one of them cares, set gpios.
1051 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1052 pdata
->buck5_gpiodvs
) {
1053 bool gpio1set
= false, gpio2set
= false;
1055 if (!gpio_is_valid(pdata
->buck125_gpios
[0]) ||
1056 !gpio_is_valid(pdata
->buck125_gpios
[1]) ||
1057 !gpio_is_valid(pdata
->buck125_gpios
[2])) {
1058 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
1063 ret
= gpio_request(pdata
->buck125_gpios
[0],
1066 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1073 ret
= gpio_request(pdata
->buck125_gpios
[1],
1076 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1080 gpio_free(pdata
->buck125_gpios
[0]);
1085 ret
= gpio_request(pdata
->buck125_gpios
[2],
1088 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1092 gpio_free(pdata
->buck125_gpios
[0]);
1094 gpio_free(pdata
->buck125_gpios
[1]);
1098 gpio_direction_output(pdata
->buck125_gpios
[0],
1099 (max8997
->buck125_gpioindex
>> 2)
1101 gpio_direction_output(pdata
->buck125_gpios
[1],
1102 (max8997
->buck125_gpioindex
>> 1)
1104 gpio_direction_output(pdata
->buck125_gpios
[2],
1105 (max8997
->buck125_gpioindex
>> 0)
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);
1118 /* Initialize all the DVS related BUCK registers */
1119 for (i
= 0; i
< 8; i
++) {
1120 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1121 max8997
->buck1_vol
[i
],
1123 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1124 max8997
->buck2_vol
[i
],
1126 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1127 max8997
->buck5_vol
[i
],
1132 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1133 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1135 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1136 const struct voltage_map_desc
*desc
;
1137 int id
= pdata
->regulators
[i
].id
;
1139 desc
= reg_voltage_map
[id
];
1141 regulators
[id
].n_voltages
=
1142 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1143 else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
)
1144 regulators
[id
].n_voltages
= 4;
1145 else if (id
== MAX8997_CHARGER_CV
)
1146 regulators
[id
].n_voltages
= 16;
1148 rdev
[i
] = regulator_register(®ulators
[id
], max8997
->dev
,
1149 pdata
->regulators
[i
].initdata
, max8997
, NULL
);
1150 if (IS_ERR(rdev
[i
])) {
1151 ret
= PTR_ERR(rdev
[i
]);
1152 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1161 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1163 regulator_unregister(rdev
[i
]);
1165 kfree(max8997
->rdev
);
1171 static int __devexit
max8997_pmic_remove(struct platform_device
*pdev
)
1173 struct max8997_data
*max8997
= platform_get_drvdata(pdev
);
1174 struct regulator_dev
**rdev
= max8997
->rdev
;
1177 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1179 regulator_unregister(rdev
[i
]);
1181 kfree(max8997
->rdev
);
1187 static const struct platform_device_id max8997_pmic_id
[] = {
1188 { "max8997-pmic", 0},
1191 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1193 static struct platform_driver max8997_pmic_driver
= {
1195 .name
= "max8997-pmic",
1196 .owner
= THIS_MODULE
,
1198 .probe
= max8997_pmic_probe
,
1199 .remove
= __devexit_p(max8997_pmic_remove
),
1200 .id_table
= max8997_pmic_id
,
1203 static int __init
max8997_pmic_init(void)
1205 return platform_driver_register(&max8997_pmic_driver
);
1207 subsys_initcall(max8997_pmic_init
);
1209 static void __exit
max8997_pmic_cleanup(void)
1211 platform_driver_unregister(&max8997_pmic_driver
);
1213 module_exit(max8997_pmic_cleanup
);
1215 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1216 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1217 MODULE_LICENSE("GPL");