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 */
45 int buck125_gpioindex
;
47 u8 saved_states
[MAX8997_REG_MAX
];
50 static inline void max8997_set_gpio(struct max8997_data
*max8997
)
52 struct max8997_platform_data
*pdata
=
53 dev_get_platdata(max8997
->iodev
->dev
);
54 int set3
= (max8997
->buck125_gpioindex
) & 0x1;
55 int set2
= ((max8997
->buck125_gpioindex
) >> 1) & 0x1;
56 int set1
= ((max8997
->buck125_gpioindex
) >> 2) & 0x1;
58 gpio_set_value(pdata
->buck125_gpios
[0], set1
);
59 gpio_set_value(pdata
->buck125_gpios
[1], set2
);
60 gpio_set_value(pdata
->buck125_gpios
[2], set3
);
63 struct voltage_map_desc
{
70 /* Voltage maps in mV */
71 static const struct voltage_map_desc ldo_voltage_map_desc
= {
72 .min
= 800, .max
= 3950, .step
= 50, .n_bits
= 6,
73 }; /* LDO1 ~ 18, 21 all */
75 static const struct voltage_map_desc buck1245_voltage_map_desc
= {
76 .min
= 650, .max
= 2225, .step
= 25, .n_bits
= 6,
77 }; /* Buck1, 2, 4, 5 */
79 static const struct voltage_map_desc buck37_voltage_map_desc
= {
80 .min
= 750, .max
= 3900, .step
= 50, .n_bits
= 6,
83 /* current map in mA */
84 static const struct voltage_map_desc charger_current_map_desc
= {
85 .min
= 200, .max
= 950, .step
= 50, .n_bits
= 4,
88 static const struct voltage_map_desc topoff_current_map_desc
= {
89 .min
= 50, .max
= 200, .step
= 10, .n_bits
= 4,
92 static const struct voltage_map_desc
*reg_voltage_map
[] = {
93 [MAX8997_LDO1
] = &ldo_voltage_map_desc
,
94 [MAX8997_LDO2
] = &ldo_voltage_map_desc
,
95 [MAX8997_LDO3
] = &ldo_voltage_map_desc
,
96 [MAX8997_LDO4
] = &ldo_voltage_map_desc
,
97 [MAX8997_LDO5
] = &ldo_voltage_map_desc
,
98 [MAX8997_LDO6
] = &ldo_voltage_map_desc
,
99 [MAX8997_LDO7
] = &ldo_voltage_map_desc
,
100 [MAX8997_LDO8
] = &ldo_voltage_map_desc
,
101 [MAX8997_LDO9
] = &ldo_voltage_map_desc
,
102 [MAX8997_LDO10
] = &ldo_voltage_map_desc
,
103 [MAX8997_LDO11
] = &ldo_voltage_map_desc
,
104 [MAX8997_LDO12
] = &ldo_voltage_map_desc
,
105 [MAX8997_LDO13
] = &ldo_voltage_map_desc
,
106 [MAX8997_LDO14
] = &ldo_voltage_map_desc
,
107 [MAX8997_LDO15
] = &ldo_voltage_map_desc
,
108 [MAX8997_LDO16
] = &ldo_voltage_map_desc
,
109 [MAX8997_LDO17
] = &ldo_voltage_map_desc
,
110 [MAX8997_LDO18
] = &ldo_voltage_map_desc
,
111 [MAX8997_LDO21
] = &ldo_voltage_map_desc
,
112 [MAX8997_BUCK1
] = &buck1245_voltage_map_desc
,
113 [MAX8997_BUCK2
] = &buck1245_voltage_map_desc
,
114 [MAX8997_BUCK3
] = &buck37_voltage_map_desc
,
115 [MAX8997_BUCK4
] = &buck1245_voltage_map_desc
,
116 [MAX8997_BUCK5
] = &buck1245_voltage_map_desc
,
117 [MAX8997_BUCK6
] = NULL
,
118 [MAX8997_BUCK7
] = &buck37_voltage_map_desc
,
119 [MAX8997_EN32KHZ_AP
] = NULL
,
120 [MAX8997_EN32KHZ_CP
] = NULL
,
121 [MAX8997_ENVICHG
] = NULL
,
122 [MAX8997_ESAFEOUT1
] = NULL
,
123 [MAX8997_ESAFEOUT2
] = NULL
,
124 [MAX8997_CHARGER_CV
] = NULL
,
125 [MAX8997_CHARGER
] = &charger_current_map_desc
,
126 [MAX8997_CHARGER_TOPOFF
] = &topoff_current_map_desc
,
129 static inline int max8997_get_rid(struct regulator_dev
*rdev
)
131 return rdev_get_id(rdev
);
134 static int max8997_list_voltage_safeout(struct regulator_dev
*rdev
,
135 unsigned int selector
)
137 int rid
= max8997_get_rid(rdev
);
139 if (rid
== MAX8997_ESAFEOUT1
|| rid
== MAX8997_ESAFEOUT2
) {
157 static int max8997_list_voltage_charger_cv(struct regulator_dev
*rdev
,
158 unsigned int selector
)
160 int rid
= max8997_get_rid(rdev
);
162 if (rid
!= MAX8997_CHARGER_CV
)
169 return 4000000 + 20000 * (selector
- 0x01);
179 static int max8997_list_voltage(struct regulator_dev
*rdev
,
180 unsigned int selector
)
182 const struct voltage_map_desc
*desc
;
183 int rid
= max8997_get_rid(rdev
);
186 if (rid
>= ARRAY_SIZE(reg_voltage_map
) ||
190 desc
= reg_voltage_map
[rid
];
194 val
= desc
->min
+ desc
->step
* selector
;
201 static int max8997_get_enable_register(struct regulator_dev
*rdev
,
202 int *reg
, int *mask
, int *pattern
)
204 int rid
= max8997_get_rid(rdev
);
207 case MAX8997_LDO1
... MAX8997_LDO21
:
208 *reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
213 *reg
= MAX8997_REG_BUCK1CTRL
;
218 *reg
= MAX8997_REG_BUCK2CTRL
;
223 *reg
= MAX8997_REG_BUCK3CTRL
;
228 *reg
= MAX8997_REG_BUCK4CTRL
;
233 *reg
= MAX8997_REG_BUCK5CTRL
;
238 *reg
= MAX8997_REG_BUCK6CTRL
;
243 *reg
= MAX8997_REG_BUCK7CTRL
;
247 case MAX8997_EN32KHZ_AP
... MAX8997_EN32KHZ_CP
:
248 *reg
= MAX8997_REG_MAINCON1
;
249 *mask
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
250 *pattern
= 0x01 << (rid
- MAX8997_EN32KHZ_AP
);
252 case MAX8997_ENVICHG
:
253 *reg
= MAX8997_REG_MBCCTRL1
;
257 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
258 *reg
= MAX8997_REG_SAFEOUTCTRL
;
259 *mask
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
260 *pattern
= 0x40 << (rid
- MAX8997_ESAFEOUT1
);
262 case MAX8997_CHARGER
:
263 *reg
= MAX8997_REG_MBCCTRL2
;
268 /* Not controllable or not exists */
276 static int max8997_reg_is_enabled(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
;
283 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
285 return 1; /* "not controllable" */
289 ret
= max8997_read_reg(i2c
, reg
, &val
);
293 return (val
& mask
) == pattern
;
296 static int max8997_reg_enable(struct regulator_dev
*rdev
)
298 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
299 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
300 int ret
, reg
, mask
, pattern
;
302 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
306 return max8997_update_reg(i2c
, reg
, pattern
, mask
);
309 static int max8997_reg_disable(struct regulator_dev
*rdev
)
311 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
312 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
313 int ret
, reg
, mask
, pattern
;
315 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
319 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
322 static int max8997_get_voltage_register(struct regulator_dev
*rdev
,
323 int *_reg
, int *_shift
, int *_mask
)
325 int rid
= max8997_get_rid(rdev
);
326 int reg
, shift
= 0, mask
= 0x3f;
329 case MAX8997_LDO1
... MAX8997_LDO21
:
330 reg
= MAX8997_REG_LDO1CTRL
+ (rid
- MAX8997_LDO1
);
333 reg
= MAX8997_REG_BUCK1DVS1
;
336 reg
= MAX8997_REG_BUCK2DVS1
;
339 reg
= MAX8997_REG_BUCK3DVS
;
342 reg
= MAX8997_REG_BUCK4DVS
;
345 reg
= MAX8997_REG_BUCK5DVS1
;
348 reg
= MAX8997_REG_BUCK7DVS
;
350 case MAX8997_ESAFEOUT1
... MAX8997_ESAFEOUT2
:
351 reg
= MAX8997_REG_SAFEOUTCTRL
;
352 shift
= (rid
== MAX8997_ESAFEOUT2
) ? 2 : 0;
355 case MAX8997_CHARGER_CV
:
356 reg
= MAX8997_REG_MBCCTRL3
;
360 case MAX8997_CHARGER
:
361 reg
= MAX8997_REG_MBCCTRL4
;
365 case MAX8997_CHARGER_TOPOFF
:
366 reg
= MAX8997_REG_MBCCTRL5
;
381 static int max8997_get_voltage(struct regulator_dev
*rdev
)
383 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
384 struct max8997_platform_data
*pdata
=
385 dev_get_platdata(max8997
->iodev
->dev
);
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
&& pdata
->buck1_gpiodvs
) ||
396 (rid
== MAX8997_BUCK2
&& pdata
->buck2_gpiodvs
) ||
397 (rid
== MAX8997_BUCK5
&& pdata
->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(desc
->step
* (i
- org
) / max8997
->ramp_delay
);
554 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
556 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
557 * of one of the bucks may affect that of another buck, which is the side
558 * effect of the change (set_voltage). This function examines the GPIO-DVS
559 * configurations and checks whether such side-effect exists.
561 static int max8997_assess_side_effect(struct regulator_dev
*rdev
,
562 u8 new_val
, int *best
)
564 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
565 struct max8997_platform_data
*pdata
=
566 dev_get_platdata(max8997
->iodev
->dev
);
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] = pdata
->buck1_gpiodvs
;
594 buckx_gpiodvs
[1] = pdata
->buck2_gpiodvs
;
595 buckx_gpiodvs
[2] = pdata
->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 struct max8997_platform_data
*pdata
=
645 dev_get_platdata(max8997
->iodev
->dev
);
646 int rid
= max8997_get_rid(rdev
);
647 const struct voltage_map_desc
*desc
;
648 int new_val
, new_idx
, damage
, tmp_val
, tmp_idx
, tmp_dmg
;
649 bool gpio_dvs_mode
= false;
650 int min_vol
= min_uV
/ 1000, max_vol
= max_uV
/ 1000;
652 if (rid
< MAX8997_BUCK1
|| rid
> MAX8997_BUCK7
)
657 if (pdata
->buck1_gpiodvs
)
658 gpio_dvs_mode
= true;
661 if (pdata
->buck2_gpiodvs
)
662 gpio_dvs_mode
= true;
665 if (pdata
->buck5_gpiodvs
)
666 gpio_dvs_mode
= true;
671 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
,
674 desc
= reg_voltage_map
[rid
];
675 new_val
= max8997_get_voltage_proper_val(desc
, min_vol
, max_vol
);
683 damage
= max8997_assess_side_effect(rdev
, new_val
, &new_idx
);
687 if (tmp_dmg
> damage
) {
694 } while (desc
->min
+ desc
->step
+ new_val
<= desc
->max
);
699 if (pdata
->ignore_gpiodvs_side_effect
== false)
702 dev_warn(&rdev
->dev
, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
703 " %d -> %d\n", max8997
->buck125_gpioindex
, tmp_idx
);
706 if (new_idx
< 0 || new_val
< 0)
709 max8997
->buck125_gpioindex
= new_idx
;
710 max8997_set_gpio(max8997
);
716 static const int safeoutvolt
[] = {
723 /* For SAFEOUT1 and SAFEOUT2 */
724 static int max8997_set_voltage_safeout(struct regulator_dev
*rdev
,
725 int min_uV
, int max_uV
, unsigned *selector
)
727 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
728 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
729 int rid
= max8997_get_rid(rdev
);
730 int reg
, shift
= 0, mask
, ret
;
734 if (rid
!= MAX8997_ESAFEOUT1
&& rid
!= MAX8997_ESAFEOUT2
)
737 for (i
= 0; i
< ARRAY_SIZE(safeoutvolt
); i
++) {
738 if (min_uV
<= safeoutvolt
[i
] &&
739 max_uV
>= safeoutvolt
[i
])
743 if (i
>= ARRAY_SIZE(safeoutvolt
))
751 ret
= max8997_get_voltage_register(rdev
, ®
, &shift
, &mask
);
755 ret
= max8997_update_reg(i2c
, reg
, val
<< shift
, mask
<< shift
);
761 static int max8997_reg_enable_suspend(struct regulator_dev
*rdev
)
766 static int max8997_reg_disable_suspend(struct regulator_dev
*rdev
)
768 struct max8997_data
*max8997
= rdev_get_drvdata(rdev
);
769 struct i2c_client
*i2c
= max8997
->iodev
->i2c
;
770 int ret
, reg
, mask
, pattern
;
771 int rid
= max8997_get_rid(rdev
);
773 ret
= max8997_get_enable_register(rdev
, ®
, &mask
, &pattern
);
777 max8997_read_reg(i2c
, reg
, &max8997
->saved_states
[rid
]);
779 if (rid
== MAX8997_LDO1
||
780 rid
== MAX8997_LDO10
||
781 rid
== MAX8997_LDO21
) {
782 dev_dbg(&rdev
->dev
, "Conditional Power-Off for %s\n",
784 return max8997_update_reg(i2c
, reg
, 0x40, mask
);
787 dev_dbg(&rdev
->dev
, "Full Power-Off for %s (%xh -> %xh)\n",
788 rdev
->desc
->name
, max8997
->saved_states
[rid
] & mask
,
790 return max8997_update_reg(i2c
, reg
, ~pattern
, mask
);
793 static struct regulator_ops max8997_ldo_ops
= {
794 .list_voltage
= max8997_list_voltage
,
795 .is_enabled
= max8997_reg_is_enabled
,
796 .enable
= max8997_reg_enable
,
797 .disable
= max8997_reg_disable
,
798 .get_voltage
= max8997_get_voltage
,
799 .set_voltage
= max8997_set_voltage_ldobuck
,
800 .set_suspend_enable
= max8997_reg_enable_suspend
,
801 .set_suspend_disable
= max8997_reg_disable_suspend
,
804 static struct regulator_ops max8997_buck_ops
= {
805 .list_voltage
= max8997_list_voltage
,
806 .is_enabled
= max8997_reg_is_enabled
,
807 .enable
= max8997_reg_enable
,
808 .disable
= max8997_reg_disable
,
809 .get_voltage
= max8997_get_voltage
,
810 .set_voltage
= max8997_set_voltage_buck
,
811 .set_suspend_enable
= max8997_reg_enable_suspend
,
812 .set_suspend_disable
= max8997_reg_disable_suspend
,
815 static struct regulator_ops max8997_fixedvolt_ops
= {
816 .list_voltage
= max8997_list_voltage
,
817 .is_enabled
= max8997_reg_is_enabled
,
818 .enable
= max8997_reg_enable
,
819 .disable
= max8997_reg_disable
,
820 .set_suspend_enable
= max8997_reg_enable_suspend
,
821 .set_suspend_disable
= max8997_reg_disable_suspend
,
824 static struct regulator_ops max8997_safeout_ops
= {
825 .list_voltage
= max8997_list_voltage_safeout
,
826 .is_enabled
= max8997_reg_is_enabled
,
827 .enable
= max8997_reg_enable
,
828 .disable
= max8997_reg_disable
,
829 .get_voltage
= max8997_get_voltage
,
830 .set_voltage
= max8997_set_voltage_safeout
,
831 .set_suspend_enable
= max8997_reg_enable_suspend
,
832 .set_suspend_disable
= max8997_reg_disable_suspend
,
835 static struct regulator_ops max8997_fixedstate_ops
= {
836 .list_voltage
= max8997_list_voltage_charger_cv
,
837 .get_voltage
= max8997_get_voltage
,
838 .set_voltage
= max8997_set_voltage_charger_cv
,
841 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev
*rdev
,
842 int min_uV
, int max_uV
)
846 return max8997_set_voltage_ldobuck(rdev
, min_uV
, max_uV
, &dummy
);
850 static struct regulator_ops max8997_charger_ops
= {
851 .is_enabled
= max8997_reg_is_enabled
,
852 .enable
= max8997_reg_enable
,
853 .disable
= max8997_reg_disable
,
854 .get_current_limit
= max8997_get_voltage
,
855 .set_current_limit
= max8997_set_voltage_ldobuck_wrap
,
858 static struct regulator_ops max8997_charger_fixedstate_ops
= {
859 .is_enabled
= max8997_reg_is_enabled
,
860 .get_current_limit
= max8997_get_voltage
,
861 .set_current_limit
= max8997_set_voltage_ldobuck_wrap
,
864 #define regulator_desc_ldo(num) { \
866 .id = MAX8997_LDO##num, \
867 .ops = &max8997_ldo_ops, \
868 .type = REGULATOR_VOLTAGE, \
869 .owner = THIS_MODULE, \
871 #define regulator_desc_buck(num) { \
872 .name = "BUCK"#num, \
873 .id = MAX8997_BUCK##num, \
874 .ops = &max8997_buck_ops, \
875 .type = REGULATOR_VOLTAGE, \
876 .owner = THIS_MODULE, \
879 static struct regulator_desc regulators
[] = {
880 regulator_desc_ldo(1),
881 regulator_desc_ldo(2),
882 regulator_desc_ldo(3),
883 regulator_desc_ldo(4),
884 regulator_desc_ldo(5),
885 regulator_desc_ldo(6),
886 regulator_desc_ldo(7),
887 regulator_desc_ldo(8),
888 regulator_desc_ldo(9),
889 regulator_desc_ldo(10),
890 regulator_desc_ldo(11),
891 regulator_desc_ldo(12),
892 regulator_desc_ldo(13),
893 regulator_desc_ldo(14),
894 regulator_desc_ldo(15),
895 regulator_desc_ldo(16),
896 regulator_desc_ldo(17),
897 regulator_desc_ldo(18),
898 regulator_desc_ldo(21),
899 regulator_desc_buck(1),
900 regulator_desc_buck(2),
901 regulator_desc_buck(3),
902 regulator_desc_buck(4),
903 regulator_desc_buck(5),
907 .ops
= &max8997_fixedvolt_ops
,
908 .type
= REGULATOR_VOLTAGE
,
909 .owner
= THIS_MODULE
,
911 regulator_desc_buck(7),
913 .name
= "EN32KHz AP",
914 .id
= MAX8997_EN32KHZ_AP
,
915 .ops
= &max8997_fixedvolt_ops
,
916 .type
= REGULATOR_VOLTAGE
,
917 .owner
= THIS_MODULE
,
919 .name
= "EN32KHz CP",
920 .id
= MAX8997_EN32KHZ_CP
,
921 .ops
= &max8997_fixedvolt_ops
,
922 .type
= REGULATOR_VOLTAGE
,
923 .owner
= THIS_MODULE
,
926 .id
= MAX8997_ENVICHG
,
927 .ops
= &max8997_fixedvolt_ops
,
928 .type
= REGULATOR_VOLTAGE
,
929 .owner
= THIS_MODULE
,
932 .id
= MAX8997_ESAFEOUT1
,
933 .ops
= &max8997_safeout_ops
,
934 .type
= REGULATOR_VOLTAGE
,
935 .owner
= THIS_MODULE
,
938 .id
= MAX8997_ESAFEOUT2
,
939 .ops
= &max8997_safeout_ops
,
940 .type
= REGULATOR_VOLTAGE
,
941 .owner
= THIS_MODULE
,
943 .name
= "CHARGER CV",
944 .id
= MAX8997_CHARGER_CV
,
945 .ops
= &max8997_fixedstate_ops
,
946 .type
= REGULATOR_VOLTAGE
,
947 .owner
= THIS_MODULE
,
950 .id
= MAX8997_CHARGER
,
951 .ops
= &max8997_charger_ops
,
952 .type
= REGULATOR_CURRENT
,
953 .owner
= THIS_MODULE
,
955 .name
= "CHARGER TOPOFF",
956 .id
= MAX8997_CHARGER_TOPOFF
,
957 .ops
= &max8997_charger_fixedstate_ops
,
958 .type
= REGULATOR_CURRENT
,
959 .owner
= THIS_MODULE
,
963 static __devinit
int max8997_pmic_probe(struct platform_device
*pdev
)
965 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
966 struct max8997_platform_data
*pdata
= dev_get_platdata(iodev
->dev
);
967 struct regulator_dev
**rdev
;
968 struct max8997_data
*max8997
;
969 struct i2c_client
*i2c
;
971 u8 max_buck1
= 0, max_buck2
= 0, max_buck5
= 0;
974 dev_err(pdev
->dev
.parent
, "No platform init data supplied.\n");
978 max8997
= kzalloc(sizeof(struct max8997_data
), GFP_KERNEL
);
982 size
= sizeof(struct regulator_dev
*) * pdata
->num_regulators
;
983 max8997
->rdev
= kzalloc(size
, GFP_KERNEL
);
984 if (!max8997
->rdev
) {
989 rdev
= max8997
->rdev
;
990 max8997
->dev
= &pdev
->dev
;
991 max8997
->iodev
= iodev
;
992 max8997
->num_regulators
= pdata
->num_regulators
;
993 platform_set_drvdata(pdev
, max8997
);
994 i2c
= max8997
->iodev
->i2c
;
996 max8997
->buck125_gpioindex
= pdata
->buck125_default_idx
;
998 for (i
= 0; i
< 8; i
++) {
999 max8997
->buck1_vol
[i
] = ret
=
1000 max8997_get_voltage_proper_val(
1001 &buck1245_voltage_map_desc
,
1002 pdata
->buck1_voltage
[i
] / 1000,
1003 pdata
->buck1_voltage
[i
] / 1000 +
1004 buck1245_voltage_map_desc
.step
);
1008 max8997
->buck2_vol
[i
] = ret
=
1009 max8997_get_voltage_proper_val(
1010 &buck1245_voltage_map_desc
,
1011 pdata
->buck2_voltage
[i
] / 1000,
1012 pdata
->buck2_voltage
[i
] / 1000 +
1013 buck1245_voltage_map_desc
.step
);
1017 max8997
->buck5_vol
[i
] = ret
=
1018 max8997_get_voltage_proper_val(
1019 &buck1245_voltage_map_desc
,
1020 pdata
->buck5_voltage
[i
] / 1000,
1021 pdata
->buck5_voltage
[i
] / 1000 +
1022 buck1245_voltage_map_desc
.step
);
1026 if (max_buck1
< max8997
->buck1_vol
[i
])
1027 max_buck1
= max8997
->buck1_vol
[i
];
1028 if (max_buck2
< max8997
->buck2_vol
[i
])
1029 max_buck2
= max8997
->buck2_vol
[i
];
1030 if (max_buck5
< max8997
->buck5_vol
[i
])
1031 max_buck5
= max8997
->buck5_vol
[i
];
1034 /* For the safety, set max voltage before setting up */
1035 for (i
= 0; i
< 8; i
++) {
1036 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS(i
+ 1),
1038 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS(i
+ 1),
1040 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS(i
+ 1),
1045 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1046 * If at least one of them cares, set gpios.
1048 if (pdata
->buck1_gpiodvs
|| pdata
->buck2_gpiodvs
||
1049 pdata
->buck5_gpiodvs
) {
1050 bool gpio1set
= false, gpio2set
= false;
1052 if (!gpio_is_valid(pdata
->buck125_gpios
[0]) ||
1053 !gpio_is_valid(pdata
->buck125_gpios
[1]) ||
1054 !gpio_is_valid(pdata
->buck125_gpios
[2])) {
1055 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
1060 ret
= gpio_request(pdata
->buck125_gpios
[0],
1063 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1070 ret
= gpio_request(pdata
->buck125_gpios
[1],
1073 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1077 gpio_free(pdata
->buck125_gpios
[0]);
1082 ret
= gpio_request(pdata
->buck125_gpios
[2],
1085 dev_warn(&pdev
->dev
, "Duplicated gpio request"
1089 gpio_free(pdata
->buck125_gpios
[0]);
1091 gpio_free(pdata
->buck125_gpios
[1]);
1095 gpio_direction_output(pdata
->buck125_gpios
[0],
1096 (max8997
->buck125_gpioindex
>> 2)
1098 gpio_direction_output(pdata
->buck125_gpios
[1],
1099 (max8997
->buck125_gpioindex
>> 1)
1101 gpio_direction_output(pdata
->buck125_gpios
[2],
1102 (max8997
->buck125_gpioindex
>> 0)
1107 /* DVS-GPIO disabled */
1108 max8997_update_reg(i2c
, MAX8997_REG_BUCK1CTRL
, (pdata
->buck1_gpiodvs
) ?
1109 (1 << 1) : (0 << 1), 1 << 1);
1110 max8997_update_reg(i2c
, MAX8997_REG_BUCK2CTRL
, (pdata
->buck2_gpiodvs
) ?
1111 (1 << 1) : (0 << 1), 1 << 1);
1112 max8997_update_reg(i2c
, MAX8997_REG_BUCK5CTRL
, (pdata
->buck5_gpiodvs
) ?
1113 (1 << 1) : (0 << 1), 1 << 1);
1115 /* Initialize all the DVS related BUCK registers */
1116 for (i
= 0; i
< 8; i
++) {
1117 max8997_update_reg(i2c
, MAX8997_REG_BUCK1DVS(i
+ 1),
1118 max8997
->buck1_vol
[i
],
1120 max8997_update_reg(i2c
, MAX8997_REG_BUCK2DVS(i
+ 1),
1121 max8997
->buck2_vol
[i
],
1123 max8997_update_reg(i2c
, MAX8997_REG_BUCK5DVS(i
+ 1),
1124 max8997
->buck5_vol
[i
],
1128 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
1129 const struct voltage_map_desc
*desc
;
1130 int id
= pdata
->regulators
[i
].id
;
1132 desc
= reg_voltage_map
[id
];
1134 regulators
[id
].n_voltages
=
1135 (desc
->max
- desc
->min
) / desc
->step
+ 1;
1136 else if (id
== MAX8997_ESAFEOUT1
|| id
== MAX8997_ESAFEOUT2
)
1137 regulators
[id
].n_voltages
= 4;
1138 else if (id
== MAX8997_CHARGER_CV
)
1139 regulators
[id
].n_voltages
= 16;
1141 rdev
[i
] = regulator_register(®ulators
[id
], max8997
->dev
,
1142 pdata
->regulators
[i
].initdata
, max8997
);
1143 if (IS_ERR(rdev
[i
])) {
1144 ret
= PTR_ERR(rdev
[i
]);
1145 dev_err(max8997
->dev
, "regulator init failed for %d\n",
1153 max8997
->ramp_delay
= 10; /* set 10mV/us, which is the default */
1154 max8997_write_reg(i2c
, MAX8997_REG_BUCKRAMP
, (0xf << 4) | 0x9);
1158 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1160 regulator_unregister(rdev
[i
]);
1162 kfree(max8997
->rdev
);
1168 static int __devexit
max8997_pmic_remove(struct platform_device
*pdev
)
1170 struct max8997_data
*max8997
= platform_get_drvdata(pdev
);
1171 struct regulator_dev
**rdev
= max8997
->rdev
;
1174 for (i
= 0; i
< max8997
->num_regulators
; i
++)
1176 regulator_unregister(rdev
[i
]);
1178 kfree(max8997
->rdev
);
1184 static const struct platform_device_id max8997_pmic_id
[] = {
1185 { "max8997-pmic", 0},
1188 MODULE_DEVICE_TABLE(platform
, max8997_pmic_id
);
1190 static struct platform_driver max8997_pmic_driver
= {
1192 .name
= "max8997-pmic",
1193 .owner
= THIS_MODULE
,
1195 .probe
= max8997_pmic_probe
,
1196 .remove
= __devexit_p(max8997_pmic_remove
),
1197 .id_table
= max8997_pmic_id
,
1200 static int __init
max8997_pmic_init(void)
1202 return platform_driver_register(&max8997_pmic_driver
);
1204 subsys_initcall(max8997_pmic_init
);
1206 static void __exit
max8997_pmic_cleanup(void)
1208 platform_driver_unregister(&max8997_pmic_driver
);
1210 module_exit(max8997_pmic_cleanup
);
1212 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1213 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1214 MODULE_LICENSE("GPL");