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/platform_device.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/mfd/max8997.h>
33 #include <linux/mfd/max8997-private.h>
37 struct max8997_dev
*iodev
;
39 struct regulator_dev
**rdev
;
40 int ramp_delay
; /* in mV/us */
49 int buck125_gpioindex
;
50 bool ignore_gpiodvs_side_effect
;
52 u8 saved_states
[MAX8997_REG_MAX
];
55 static inline void max8997_set_gpio(struct max8997_data
*max8997
)
57 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
58 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
59 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
61 gpio_set_value(max8997
->buck125_gpios
[0], set1
);
62 gpio_set_value(max8997
->buck125_gpios
[1], set2
);
63 gpio_set_value(max8997
->buck125_gpios
[2], set3
);
66 struct voltage_map_desc
{
73 /* Voltage maps in mV */
74 static const struct voltage_map_desc ldo_voltage_map_desc
= {
75 .min
= 800, .max
= 3950, .step
= 50, .n_bits
= 6,
76 }; /* LDO1 ~ 18, 21 all */
78 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
79 .min
= 650, .max
= 2225, .step
= 25, .n_bits
= 6,
80 }; /* Buck1, 2, 4, 5 */
82 static const struct voltage_map_desc buck37_voltage_map_desc
= {
83 .min
= 750, .max
= 3900, .step
= 50, .n_bits
= 6,
86 /* current map in mA */
87 static const struct voltage_map_desc charger_current_map_desc
= {
88 .min
= 200, .max
= 950, .step
= 50, .n_bits
= 4,
91 static const struct voltage_map_desc topoff_current_map_desc
= {
92 .min
= 50, .max
= 200, .step
= 10, .n_bits
= 4,
95 static const struct voltage_map_desc
*reg_voltage_map
[] = {
96 [MAX8997_LDO1
] = &ldo_voltage_map_desc
,
97 [MAX8997_LDO2
] = &ldo_voltage_map_desc
,
98 [MAX8997_LDO3
] = &ldo_voltage_map_desc
,
99 [MAX8997_LDO4
] = &ldo_voltage_map_desc
,
100 [MAX8997_LDO5
] = &ldo_voltage_map_desc
,
101 [MAX8997_LDO6
] = &ldo_voltage_map_desc
,
102 [MAX8997_LDO7
] = &ldo_voltage_map_desc
,
103 [MAX8997_LDO8
] = &ldo_voltage_map_desc
,
104 [MAX8997_LDO9
] = &ldo_voltage_map_desc
,
105 [MAX8997_LDO10
] = &ldo_voltage_map_desc
,
106 [MAX8997_LDO11
] = &ldo_voltage_map_desc
,
107 [MAX8997_LDO12
] = &ldo_voltage_map_desc
,
108 [MAX8997_LDO13
] = &ldo_voltage_map_desc
,
109 [MAX8997_LDO14
] = &ldo_voltage_map_desc
,
110 [MAX8997_LDO15
] = &ldo_voltage_map_desc
,
111 [MAX8997_LDO16
] = &ldo_voltage_map_desc
,
112 [MAX8997_LDO17
] = &ldo_voltage_map_desc
,
113 [MAX8997_LDO18
] = &ldo_voltage_map_desc
,
114 [MAX8997_LDO21
] = &ldo_voltage_map_desc
,
115 [MAX8997_BUCK1
] = &buck1245_voltage_map_desc
,
116 [MAX8997_BUCK2
] = &buck1245_voltage_map_desc
,
117 [MAX8997_BUCK3
] = &buck37_voltage_map_desc
,
118 [MAX8997_BUCK4
] = &buck1245_voltage_map_desc
,
119 [MAX8997_BUCK5
] = &buck1245_voltage_map_desc
,
120 [MAX8997_BUCK6
] = NULL
,
121 [MAX8997_BUCK7
] = &buck37_voltage_map_desc
,
122 [MAX8997_EN32KHZ_AP
] = NULL
,
123 [MAX8997_EN32KHZ_CP
] = NULL
,
124 [MAX8997_ENVICHG
] = NULL
,
125 [MAX8997_ESAFEOUT1
] = NULL
,
126 [MAX8997_ESAFEOUT2
] = NULL
,
127 [MAX8997_CHARGER_CV
] = NULL
,
128 [MAX8997_CHARGER
] = &charger_current_map_desc
,
129 [MAX8997_CHARGER_TOPOFF
] = &topoff_current_map_desc
,
132 static inline int max8997_get_rid(struct regulator_dev
*rdev
)
134 return rdev_get_id(rdev
);
137 static int max8997_list_voltage_safeout(struct regulator_dev
*rdev
,
138 unsigned int selector
)
140 int rid
= max8997_get_rid(rdev
);
142 if (rid
== MAX8997_ESAFEOUT1
|| rid
== MAX8997_ESAFEOUT2
) {
160 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
161 unsigned int selector
)
163 int rid
= max8997_get_rid(rdev
);
165 if (rid
!= MAX8997_CHARGER_CV
)
172 return 4000000 + 20000 * (selector
- 0x01);
182 static int max8997_list_voltage(struct regulator_dev
*rdev
,
183 unsigned int selector
)
185 const struct voltage_map_desc
*desc
;
186 int rid
= max8997_get_rid(rdev
);
189 if (rid
>= ARRAY_SIZE(reg_voltage_map
) ||
193 desc
= reg_voltage_map
[rid
];
197 val
= desc
->min
+ desc
->step
* selector
;
204 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
205 int *reg
, int *mask
, int *pattern
)
207 int rid
= max8997_get_rid(rdev
);
210 case MAX8997_LDO1
... MAX8997_LDO21
:
211 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
216 *reg
= MAX8997_REG_BUCK1CTRL
;
221 *reg
= MAX8997_REG_BUCK2CTRL
;
226 *reg
= MAX8997_REG_BUCK3CTRL
;
231 *reg
= MAX8997_REG_BUCK4CTRL
;
236 *reg
= MAX8997_REG_BUCK5CTRL
;
241 *reg
= MAX8997_REG_BUCK6CTRL
;
246 *reg
= MAX8997_REG_BUCK7CTRL
;
250 case MAX8997_EN32KHZ_AP
... MAX8997_EN32KHZ_CP
:
251 *reg
= MAX8997_REG_MAINCON1
;
252 *mask
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
253 *pattern
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
255 case MAX8997_ENVICHG
:
256 *reg
= MAX8997_REG_MBCCTRL1
;
260 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
261 *reg
= MAX8997_REG_SAFEOUTCTRL
;
262 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
263 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
265 case MAX8997_CHARGER
:
266 *reg
= MAX8997_REG_MBCCTRL2
;
271 /* Not controllable or not exists */
278 static int max8997_reg_is_enabled(struct regulator_dev
*rdev
)
280 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
281 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
282 int ret
, reg
, mask
, pattern
;
285 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
287 return 1; /* "not controllable" */
291 ret
= max8997_read_reg(i2c
, reg
, &val
);
295 return (val
& mask
) == pattern
;
298 static int max8997_reg_enable(struct regulator_dev
*rdev
)
300 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
301 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
302 int ret
, reg
, mask
, pattern
;
304 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
308 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
311 static int max8997_reg_disable(struct regulator_dev
*rdev
)
313 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
314 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
315 int ret
, reg
, mask
, pattern
;
317 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
321 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
324 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
325 int *_reg
, int *_shift
, int *_mask
)
327 int rid
= max8997_get_rid(rdev
);
328 int reg
, shift
= 0, mask
= 0x3f;
331 case MAX8997_LDO1
... MAX8997_LDO21
:
332 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
335 reg
= MAX8997_REG_BUCK1DVS1
;
338 reg
= MAX8997_REG_BUCK2DVS1
;
341 reg
= MAX8997_REG_BUCK3DVS
;
344 reg
= MAX8997_REG_BUCK4DVS
;
347 reg
= MAX8997_REG_BUCK5DVS1
;
350 reg
= MAX8997_REG_BUCK7DVS
;
352 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
353 reg
= MAX8997_REG_SAFEOUTCTRL
;
354 shift
= (rid
== MAX8997_ESAFEOUT2
) ? 2 : 0;
357 case MAX8997_CHARGER_CV
:
358 reg
= MAX8997_REG_MBCCTRL3
;
362 case MAX8997_CHARGER
:
363 reg
= MAX8997_REG_MBCCTRL4
;
367 case MAX8997_CHARGER_TOPOFF
:
368 reg
= MAX8997_REG_MBCCTRL5
;
383 static int max8997_get_voltage(struct regulator_dev
*rdev
)
385 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
386 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
387 int reg
, shift
, mask
, ret
;
388 int rid
= max8997_get_rid(rdev
);
391 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
395 if ((rid
== MAX8997_BUCK1
&& max8997
->buck1_gpiodvs
) ||
396 (rid
== MAX8997_BUCK2
&& max8997
->buck2_gpiodvs
) ||
397 (rid
== MAX8997_BUCK5
&& max8997
->buck5_gpiodvs
))
398 reg
+= max8997
->buck125_gpioindex
;
400 ret
= max8997_read_reg(i2c
, reg
, &val
);
407 if (rdev
->desc
&& rdev
->desc
->ops
&& rdev
->desc
->ops
->list_voltage
)
408 return rdev
->desc
->ops
->list_voltage(rdev
, val
);
411 * max8997_list_voltage returns value for any rdev with voltage_map,
412 * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
413 * list_voltage ops (they are current regulators).
415 return max8997_list_voltage(rdev
, val
);
418 static inline int max8997_get_voltage_proper_val(
419 const struct voltage_map_desc
*desc
,
420 int min_vol
, int max_vol
)
427 if (max_vol
< desc
->min
|| min_vol
> desc
->max
)
430 while (desc
->min
+ desc
->step
* i
< min_vol
&&
431 desc
->min
+ desc
->step
* i
< desc
->max
)
434 if (desc
->min
+ desc
->step
* i
> max_vol
)
437 if (i
>= (1 << desc
->n_bits
))
443 static int max8997_set_voltage_charger_cv(struct regulator_dev
*rdev
,
444 int min_uV
, int max_uV
, unsigned *selector
)
446 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
447 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
448 int rid
= max8997_get_rid(rdev
);
450 int reg
, shift
= 0, mask
, ret
= 0;
453 if (rid
!= MAX8997_CHARGER_CV
)
456 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
460 if (max_uV
< 4000000 || min_uV
> 4350000)
463 if (min_uV
<= 4000000) {
464 if (max_uV
>= 4000000)
468 } else if (min_uV
<= 4200000 && max_uV
>= 4200000)
471 lb
= (min_uV
- 4000001) / 20000 + 2;
472 ub
= (max_uV
- 4000000) / 20000 + 1;
489 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
);
495 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
496 * BUCK1, 2, and 5 are available if they are not controlled by gpio
498 static int max8997_set_voltage_ldobuck(struct regulator_dev
*rdev
,
499 int min_uV
, int max_uV
, unsigned *selector
)
501 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
502 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
503 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
504 const struct voltage_map_desc
*desc
;
505 int rid
= max8997_get_rid(rdev
);
506 int reg
, shift
= 0, mask
, ret
;
511 case MAX8997_LDO1
... MAX8997_LDO21
:
513 case MAX8997_BUCK1
... MAX8997_BUCK5
:
519 case MAX8997_CHARGER
:
521 case MAX8997_CHARGER_TOPOFF
:
527 desc
= reg_voltage_map
[rid
];
529 i
= max8997_get_voltage_proper_val(desc
, min_vol
, max_vol
);
533 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
537 max8997_read_reg(i2c
, reg
, &org
);
538 org
= (org
& mask
) >> shift
;
540 ret
= max8997_update_reg(i2c
, reg
, i
<< shift
, mask
<< shift
);
543 if (rid
== MAX8997_BUCK1
|| rid
== MAX8997_BUCK2
||
544 rid
== MAX8997_BUCK4
|| rid
== MAX8997_BUCK5
) {
545 /* If the voltage is increasing */
547 udelay(DIV_ROUND_UP(desc
->step
* (i
- org
),
548 max8997
->ramp_delay
));
555 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
557 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
558 * of one of the bucks may affect that of another buck, which is the side
559 * effect of the change (set_voltage). This function examines the GPIO-DVS
560 * configurations and checks whether such side-effect exists.
562 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
563 u8 new_val
, int *best
)
565 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
566 int rid
= max8997_get_rid(rdev
);
568 bool buckx_gpiodvs
[3];
570 int min_side_effect
= INT_MAX
;
589 buckx_val
[0] = max8997
->buck1_vol
;
590 buckx_val
[1] = max8997
->buck2_vol
;
591 buckx_val
[2] = max8997
->buck5_vol
;
592 buckx_gpiodvs
[0] = max8997
->buck1_gpiodvs
;
593 buckx_gpiodvs
[1] = max8997
->buck2_gpiodvs
;
594 buckx_gpiodvs
[2] = max8997
->buck5_gpiodvs
;
596 for (i
= 0; i
< 8; i
++) {
599 if (new_val
!= (buckx_val
[rid
])[i
]) {
605 for (others
= 0; others
< 3; others
++) {
610 if (buckx_gpiodvs
[others
] == false)
611 continue; /* Not affected */
612 diff
= (buckx_val
[others
])[i
] -
613 (buckx_val
[others
])[max8997
->buck125_gpioindex
];
615 side_effect
[i
] += diff
;
617 side_effect
[i
] -= diff
;
619 if (side_effect
[i
] == 0) {
621 return 0; /* NO SIDE EFFECT! Use This! */
623 if (side_effect
[i
] < min_side_effect
) {
624 min_side_effect
= side_effect
[i
];
632 return side_effect
[*best
];
636 * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
637 * max8997_set_voltage_ldobuck to do the job.
639 static int max8997_set_voltage_buck(struct regulator_dev
*rdev
,
640 int min_uV
, int max_uV
, unsigned *selector
)
642 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
643 int rid
= max8997_get_rid(rdev
);
644 const struct voltage_map_desc
*desc
;
645 int new_val
, new_idx
, damage
, tmp_val
, tmp_idx
, tmp_dmg
;
646 bool gpio_dvs_mode
= false;
647 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
649 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
654 if (max8997
->buck1_gpiodvs
)
655 gpio_dvs_mode
= true;
658 if (max8997
->buck2_gpiodvs
)
659 gpio_dvs_mode
= true;
662 if (max8997
->buck5_gpiodvs
)
663 gpio_dvs_mode
= true;
668 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
671 desc
= reg_voltage_map
[rid
];
672 new_val
= max8997_get_voltage_proper_val(desc
, min_vol
, max_vol
);
680 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
684 if (tmp_dmg
> damage
) {
691 } while (desc
->min
+ desc
->step
+ new_val
<= desc
->max
);
696 if (max8997
->ignore_gpiodvs_side_effect
== false)
699 dev_warn(&rdev
->dev
, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
700 " %d -> %d\n", max8997
->buck125_gpioindex
, tmp_idx
);
703 if (new_idx
< 0 || new_val
< 0)
706 max8997
->buck125_gpioindex
= new_idx
;
707 max8997_set_gpio(max8997
);
713 static const int safeoutvolt
[] = {
720 /* For SAFEOUT1 and SAFEOUT2 */
721 static int max8997_set_voltage_safeout(struct regulator_dev
*rdev
,
722 int min_uV
, int max_uV
, unsigned *selector
)
724 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
725 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
726 int rid
= max8997_get_rid(rdev
);
727 int reg
, shift
= 0, mask
, ret
;
731 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
734 for (i
= 0; i
< ARRAY_SIZE(safeoutvolt
); i
++) {
735 if (min_uV
<= safeoutvolt
[i
] &&
736 max_uV
>= safeoutvolt
[i
])
740 if (i
>= ARRAY_SIZE(safeoutvolt
))
748 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
752 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
<< shift
);
758 static int max8997_reg_enable_suspend(struct regulator_dev
*rdev
)
763 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
765 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
766 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
767 int ret
, reg
, mask
, pattern
;
768 int rid
= max8997_get_rid(rdev
);
770 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
774 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
776 if (rid
== MAX8997_LDO1
||
777 rid
== MAX8997_LDO10
||
778 rid
== MAX8997_LDO21
) {
779 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
781 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
784 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
785 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
787 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
790 static struct regulator_ops max8997_ldo_ops
= {
791 .list_voltage
= max8997_list_voltage
,
792 .is_enabled
= max8997_reg_is_enabled
,
793 .enable
= max8997_reg_enable
,
794 .disable
= max8997_reg_disable
,
795 .get_voltage
= max8997_get_voltage
,
796 .set_voltage
= max8997_set_voltage_ldobuck
,
797 .set_suspend_enable
= max8997_reg_enable_suspend
,
798 .set_suspend_disable
= max8997_reg_disable_suspend
,
801 static struct regulator_ops max8997_buck_ops
= {
802 .list_voltage
= max8997_list_voltage
,
803 .is_enabled
= max8997_reg_is_enabled
,
804 .enable
= max8997_reg_enable
,
805 .disable
= max8997_reg_disable
,
806 .get_voltage
= max8997_get_voltage
,
807 .set_voltage
= max8997_set_voltage_buck
,
808 .set_suspend_enable
= max8997_reg_enable_suspend
,
809 .set_suspend_disable
= max8997_reg_disable_suspend
,
812 static struct regulator_ops max8997_fixedvolt_ops
= {
813 .list_voltage
= max8997_list_voltage
,
814 .is_enabled
= max8997_reg_is_enabled
,
815 .enable
= max8997_reg_enable
,
816 .disable
= max8997_reg_disable
,
817 .set_suspend_enable
= max8997_reg_enable_suspend
,
818 .set_suspend_disable
= max8997_reg_disable_suspend
,
821 static struct regulator_ops max8997_safeout_ops
= {
822 .list_voltage
= max8997_list_voltage_safeout
,
823 .is_enabled
= max8997_reg_is_enabled
,
824 .enable
= max8997_reg_enable
,
825 .disable
= max8997_reg_disable
,
826 .get_voltage
= max8997_get_voltage
,
827 .set_voltage
= max8997_set_voltage_safeout
,
828 .set_suspend_enable
= max8997_reg_enable_suspend
,
829 .set_suspend_disable
= max8997_reg_disable_suspend
,
832 static struct regulator_ops max8997_fixedstate_ops
= {
833 .list_voltage
= max8997_list_voltage_charger_cv
,
834 .get_voltage
= max8997_get_voltage
,
835 .set_voltage
= max8997_set_voltage_charger_cv
,
838 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev
*rdev
,
839 int min_uV
, int max_uV
)
843 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
, &dummy
);
847 static struct regulator_ops max8997_charger_ops
= {
848 .is_enabled
= max8997_reg_is_enabled
,
849 .enable
= max8997_reg_enable
,
850 .disable
= max8997_reg_disable
,
851 .get_current_limit
= max8997_get_voltage
,
852 .set_current_limit
= max8997_set_voltage_ldobuck_wrap
,
855 static struct regulator_ops max8997_charger_fixedstate_ops
= {
856 .is_enabled
= max8997_reg_is_enabled
,
857 .get_current_limit
= max8997_get_voltage
,
858 .set_current_limit
= max8997_set_voltage_ldobuck_wrap
,
861 #define regulator_desc_ldo(num) { \
863 .id = MAX8997_LDO##num, \
864 .ops = &max8997_ldo_ops, \
865 .type = REGULATOR_VOLTAGE, \
866 .owner = THIS_MODULE, \
868 #define regulator_desc_buck(num) { \
869 .name = "BUCK"#num, \
870 .id = MAX8997_BUCK##num, \
871 .ops = &max8997_buck_ops, \
872 .type = REGULATOR_VOLTAGE, \
873 .owner = THIS_MODULE, \
876 static struct regulator_desc regulators
[] = {
877 regulator_desc_ldo(1),
878 regulator_desc_ldo(2),
879 regulator_desc_ldo(3),
880 regulator_desc_ldo(4),
881 regulator_desc_ldo(5),
882 regulator_desc_ldo(6),
883 regulator_desc_ldo(7),
884 regulator_desc_ldo(8),
885 regulator_desc_ldo(9),
886 regulator_desc_ldo(10),
887 regulator_desc_ldo(11),
888 regulator_desc_ldo(12),
889 regulator_desc_ldo(13),
890 regulator_desc_ldo(14),
891 regulator_desc_ldo(15),
892 regulator_desc_ldo(16),
893 regulator_desc_ldo(17),
894 regulator_desc_ldo(18),
895 regulator_desc_ldo(21),
896 regulator_desc_buck(1),
897 regulator_desc_buck(2),
898 regulator_desc_buck(3),
899 regulator_desc_buck(4),
900 regulator_desc_buck(5),
904 .ops
= &max8997_fixedvolt_ops
,
905 .type
= REGULATOR_VOLTAGE
,
906 .owner
= THIS_MODULE
,
908 regulator_desc_buck(7),
910 .name
= "EN32KHz AP",
911 .id
= MAX8997_EN32KHZ_AP
,
912 .ops
= &max8997_fixedvolt_ops
,
913 .type
= REGULATOR_VOLTAGE
,
914 .owner
= THIS_MODULE
,
916 .name
= "EN32KHz CP",
917 .id
= MAX8997_EN32KHZ_CP
,
918 .ops
= &max8997_fixedvolt_ops
,
919 .type
= REGULATOR_VOLTAGE
,
920 .owner
= THIS_MODULE
,
923 .id
= MAX8997_ENVICHG
,
924 .ops
= &max8997_fixedvolt_ops
,
925 .type
= REGULATOR_VOLTAGE
,
926 .owner
= THIS_MODULE
,
929 .id
= MAX8997_ESAFEOUT1
,
930 .ops
= &max8997_safeout_ops
,
931 .type
= REGULATOR_VOLTAGE
,
932 .owner
= THIS_MODULE
,
935 .id
= MAX8997_ESAFEOUT2
,
936 .ops
= &max8997_safeout_ops
,
937 .type
= REGULATOR_VOLTAGE
,
938 .owner
= THIS_MODULE
,
940 .name
= "CHARGER CV",
941 .id
= MAX8997_CHARGER_CV
,
942 .ops
= &max8997_fixedstate_ops
,
943 .type
= REGULATOR_VOLTAGE
,
944 .owner
= THIS_MODULE
,
947 .id
= MAX8997_CHARGER
,
948 .ops
= &max8997_charger_ops
,
949 .type
= REGULATOR_CURRENT
,
950 .owner
= THIS_MODULE
,
952 .name
= "CHARGER TOPOFF",
953 .id
= MAX8997_CHARGER_TOPOFF
,
954 .ops
= &max8997_charger_fixedstate_ops
,
955 .type
= REGULATOR_CURRENT
,
956 .owner
= THIS_MODULE
,
960 static __devinit
int max8997_pmic_probe(struct platform_device
*pdev
)
962 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
963 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
964 struct regulator_dev
**rdev
;
965 struct max8997_data
*max8997
;
966 struct i2c_client
*i2c
;
968 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
971 dev_err(pdev
->dev
.parent
, "No platform init data supplied.\n");
975 max8997
= kzalloc(sizeof(struct max8997_data
), GFP_KERNEL
);
979 size
= sizeof(struct regulator_dev
*) * pdata
->num_regulators
;
980 max8997
->rdev
= kzalloc(size
, GFP_KERNEL
);
981 if (!max8997
->rdev
) {
986 rdev
= max8997
->rdev
;
987 max8997
->dev
= &pdev
->dev
;
988 max8997
->iodev
= iodev
;
989 max8997
->num_regulators
= pdata
->num_regulators
;
990 platform_set_drvdata(pdev
, max8997
);
991 i2c
= max8997
->iodev
->i2c
;
993 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
994 max8997
->buck1_gpiodvs
= pdata
->buck1_gpiodvs
;
995 max8997
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
996 max8997
->buck5_gpiodvs
= pdata
->buck5_gpiodvs
;
997 memcpy(max8997
->buck125_gpios
, pdata
->buck125_gpios
, sizeof(int) * 3);
998 max8997
->ignore_gpiodvs_side_effect
= pdata
->ignore_gpiodvs_side_effect
;
1000 for (i
= 0; i
< 8; i
++) {
1001 max8997
->buck1_vol
[i
] = ret
=
1002 max8997_get_voltage_proper_val(
1003 &buck1245_voltage_map_desc
,
1004 pdata
->buck1_voltage
[i
] / 1000,
1005 pdata
->buck1_voltage
[i
] / 1000 +
1006 buck1245_voltage_map_desc
.step
);
1010 max8997
->buck2_vol
[i
] = ret
=
1011 max8997_get_voltage_proper_val(
1012 &buck1245_voltage_map_desc
,
1013 pdata
->buck2_voltage
[i
] / 1000,
1014 pdata
->buck2_voltage
[i
] / 1000 +
1015 buck1245_voltage_map_desc
.step
);
1019 max8997
->buck5_vol
[i
] = ret
=
1020 max8997_get_voltage_proper_val(
1021 &buck1245_voltage_map_desc
,
1022 pdata
->buck5_voltage
[i
] / 1000,
1023 pdata
->buck5_voltage
[i
] / 1000 +
1024 buck1245_voltage_map_desc
.step
);
1028 if (max_buck1
< max8997
->buck1_vol
[i
])
1029 max_buck1
= max8997
->buck1_vol
[i
];
1030 if (max_buck2
< max8997
->buck2_vol
[i
])
1031 max_buck2
= max8997
->buck2_vol
[i
];
1032 if (max_buck5
< max8997
->buck5_vol
[i
])
1033 max_buck5
= max8997
->buck5_vol
[i
];
1036 /* For the safety, set max voltage before setting up */
1037 for (i
= 0; i
< 8; i
++) {
1038 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1040 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1042 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1047 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1048 * If at least one of them cares, set gpios.
1050 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1051 pdata
->buck5_gpiodvs
) {
1052 bool gpio1set
= false, gpio2set
= false;
1054 if (!gpio_is_valid(pdata
->buck125_gpios
[0]) ||
1055 !gpio_is_valid(pdata
->buck125_gpios
[1]) ||
1056 !gpio_is_valid(pdata
->buck125_gpios
[2])) {
1057 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
1062 ret
= gpio_request(pdata
->buck125_gpios
[0],
1065 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1072 ret
= gpio_request(pdata
->buck125_gpios
[1],
1075 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1079 gpio_free(pdata
->buck125_gpios
[0]);
1084 ret
= gpio_request(pdata
->buck125_gpios
[2],
1087 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1091 gpio_free(pdata
->buck125_gpios
[0]);
1093 gpio_free(pdata
->buck125_gpios
[1]);
1097 gpio_direction_output(pdata
->buck125_gpios
[0],
1098 (max8997
->buck125_gpioindex
>> 2)
1100 gpio_direction_output(pdata
->buck125_gpios
[1],
1101 (max8997
->buck125_gpioindex
>> 1)
1103 gpio_direction_output(pdata
->buck125_gpios
[2],
1104 (max8997
->buck125_gpioindex
>> 0)
1109 /* DVS-GPIO disabled */
1110 max8997_update_reg(i2c
, MAX8997_REG_BUCK1CTRL
, (pdata
->buck1_gpiodvs
) ?
1111 (1 << 1) : (0 << 1), 1 << 1);
1112 max8997_update_reg(i2c
, MAX8997_REG_BUCK2CTRL
, (pdata
->buck2_gpiodvs
) ?
1113 (1 << 1) : (0 << 1), 1 << 1);
1114 max8997_update_reg(i2c
, MAX8997_REG_BUCK5CTRL
, (pdata
->buck5_gpiodvs
) ?
1115 (1 << 1) : (0 << 1), 1 << 1);
1117 /* Initialize all the DVS related BUCK registers */
1118 for (i
= 0; i
< 8; i
++) {
1119 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS1
+ i
,
1120 max8997
->buck1_vol
[i
],
1122 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS1
+ i
,
1123 max8997
->buck2_vol
[i
],
1125 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS1
+ i
,
1126 max8997
->buck5_vol
[i
],
1131 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1132 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1134 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1135 const struct voltage_map_desc
*desc
;
1136 int id
= pdata
->regulators
[i
].id
;
1138 desc
= reg_voltage_map
[id
];
1140 regulators
[id
].n_voltages
=
1141 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1142 else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
)
1143 regulators
[id
].n_voltages
= 4;
1144 else if (id
== MAX8997_CHARGER_CV
)
1145 regulators
[id
].n_voltages
= 16;
1147 rdev
[i
] = regulator_register(®ulators
[id
], max8997
->dev
,
1148 pdata
->regulators
[i
].initdata
, max8997
);
1149 if (IS_ERR(rdev
[i
])) {
1150 ret
= PTR_ERR(rdev
[i
]);
1151 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1160 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1162 regulator_unregister(rdev
[i
]);
1164 kfree(max8997
->rdev
);
1170 static int __devexit
max8997_pmic_remove(struct platform_device
*pdev
)
1172 struct max8997_data
*max8997
= platform_get_drvdata(pdev
);
1173 struct regulator_dev
**rdev
= max8997
->rdev
;
1176 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1178 regulator_unregister(rdev
[i
]);
1180 kfree(max8997
->rdev
);
1186 static const struct platform_device_id max8997_pmic_id
[] = {
1187 { "max8997-pmic", 0},
1190 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1192 static struct platform_driver max8997_pmic_driver
= {
1194 .name
= "max8997-pmic",
1195 .owner
= THIS_MODULE
,
1197 .probe
= max8997_pmic_probe
,
1198 .remove
= __devexit_p(max8997_pmic_remove
),
1199 .id_table
= max8997_pmic_id
,
1202 static int __init
max8997_pmic_init(void)
1204 return platform_driver_register(&max8997_pmic_driver
);
1206 subsys_initcall(max8997_pmic_init
);
1208 static void __exit
max8997_pmic_cleanup(void)
1210 platform_driver_unregister(&max8997_pmic_driver
);
1212 module_exit(max8997_pmic_cleanup
);
1214 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1215 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1216 MODULE_LICENSE("GPL");