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 */
275 static int max8997_reg_is_enabled(struct regulator_dev *rdev)
277 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
278 struct i2c_client *i2c = max8997->iodev->i2c;
279 int ret, reg, mask, pattern;
282 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
284 return 1; /* "not controllable" */
288 ret = max8997_read_reg(i2c, reg, &val);
292 return (val & mask) == pattern;
295 static int max8997_reg_enable(struct regulator_dev *rdev)
297 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
298 struct i2c_client *i2c = max8997->iodev->i2c;
299 int ret, reg, mask, pattern;
301 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
305 return max8997_update_reg(i2c, reg, pattern, mask);
308 static int max8997_reg_disable(struct regulator_dev *rdev)
310 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
311 struct i2c_client *i2c = max8997->iodev->i2c;
312 int ret, reg, mask, pattern;
314 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
318 return max8997_update_reg(i2c, reg, ~pattern, mask);
321 static int max8997_get_voltage_register(struct regulator_dev *rdev,
322 int *_reg, int *_shift, int *_mask)
324 int rid = max8997_get_rid(rdev);
325 int reg, shift = 0, mask = 0x3f;
328 case MAX8997_LDO1 ... MAX8997_LDO21:
329 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
332 reg = MAX8997_REG_BUCK1DVS1;
335 reg = MAX8997_REG_BUCK2DVS1;
338 reg = MAX8997_REG_BUCK3DVS;
341 reg = MAX8997_REG_BUCK4DVS;
344 reg = MAX8997_REG_BUCK5DVS1;
347 reg = MAX8997_REG_BUCK7DVS;
349 case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
350 reg = MAX8997_REG_SAFEOUTCTRL;
351 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
354 case MAX8997_CHARGER_CV:
355 reg = MAX8997_REG_MBCCTRL3;
359 case MAX8997_CHARGER:
360 reg = MAX8997_REG_MBCCTRL4;
364 case MAX8997_CHARGER_TOPOFF:
365 reg = MAX8997_REG_MBCCTRL5;
380 static int max8997_get_voltage(struct regulator_dev *rdev)
382 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
383 struct max8997_platform_data *pdata =
384 dev_get_platdata(max8997->iodev->dev);
385 struct i2c_client *i2c = max8997->iodev->i2c;
386 int reg, shift, mask, ret;
387 int rid = max8997_get_rid(rdev);
390 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
394 if ((rid == MAX8997_BUCK1 && pdata->buck1_gpiodvs) ||
395 (rid == MAX8997_BUCK2 && pdata->buck2_gpiodvs) ||
396 (rid == MAX8997_BUCK5 && pdata->buck5_gpiodvs))
397 reg += max8997->buck125_gpioindex;
399 ret = max8997_read_reg(i2c, reg, &val);
406 if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
407 return rdev->desc->ops->list_voltage(rdev, val);
410 * max8997_list_voltage returns value for any rdev with voltage_map,
411 * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
412 * list_voltage ops (they are current regulators).
414 return max8997_list_voltage(rdev, val);
417 static inline int max8997_get_voltage_proper_val(
418 const struct voltage_map_desc *desc,
419 int min_vol, int max_vol)
426 if (max_vol < desc->min || min_vol > desc->max)
429 while (desc->min + desc->step * i < min_vol &&
430 desc->min + desc->step * i < desc->max)
433 if (desc->min + desc->step * i > max_vol)
436 if (i >= (1 << desc->n_bits))
442 static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
443 int min_uV, int max_uV, unsigned *selector)
445 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
446 struct i2c_client *i2c = max8997->iodev->i2c;
447 int rid = max8997_get_rid(rdev);
449 int reg, shift = 0, mask, ret = 0;
452 if (rid != MAX8997_CHARGER_CV)
455 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
459 if (max_uV < 4000000 || min_uV > 4350000)
462 if (min_uV <= 4000000) {
463 if (max_uV >= 4000000)
467 } else if (min_uV <= 4200000 && max_uV >= 4200000)
470 lb = (min_uV - 4000001) / 20000 + 2;
471 ub = (max_uV - 4000000) / 20000 + 1;
488 ret = max8997_update_reg(i2c, reg, val << shift, mask);
494 * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
495 * BUCK1, 2, and 5 are available if they are not controlled by gpio
497 static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
498 int min_uV, int max_uV, unsigned *selector)
500 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
501 struct i2c_client *i2c = max8997->iodev->i2c;
502 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
503 const struct voltage_map_desc *desc;
504 int rid = max8997_get_rid(rdev);
505 int reg, shift = 0, mask, ret;
510 case MAX8997_LDO1 ... MAX8997_LDO21:
512 case MAX8997_BUCK1 ... MAX8997_BUCK5:
518 case MAX8997_CHARGER:
520 case MAX8997_CHARGER_TOPOFF:
526 desc = reg_voltage_map[rid];
528 i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
532 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
536 max8997_read_reg(i2c, reg, &org);
537 org = (org & mask) >> shift;
539 ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
542 if (rid == MAX8997_BUCK1 || rid == MAX8997_BUCK2 ||
543 rid == MAX8997_BUCK4 || rid == MAX8997_BUCK5) {
544 /* If the voltage is increasing */
546 udelay(desc->step * (i - org) / max8997->ramp_delay);
553 * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
555 * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
556 * of one of the bucks may affect that of another buck, which is the side
557 * effect of the change (set_voltage). This function examines the GPIO-DVS
558 * configurations and checks whether such side-effect exists.
560 static int max8997_assess_side_effect(struct regulator_dev *rdev,
561 u8 new_val, int *best)
563 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
564 struct max8997_platform_data *pdata =
565 dev_get_platdata(max8997->iodev->dev);
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] = pdata->buck1_gpiodvs;
593 buckx_gpiodvs[1] = pdata->buck2_gpiodvs;
594 buckx_gpiodvs[2] = pdata->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 struct max8997_platform_data *pdata =
644 dev_get_platdata(max8997->iodev->dev);
645 int rid = max8997_get_rid(rdev);
646 const struct voltage_map_desc *desc;
647 int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
648 bool gpio_dvs_mode = false;
649 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
651 if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
656 if (pdata->buck1_gpiodvs)
657 gpio_dvs_mode = true;
660 if (pdata->buck2_gpiodvs)
661 gpio_dvs_mode = true;
664 if (pdata->buck5_gpiodvs)
665 gpio_dvs_mode = true;
670 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
673 desc = reg_voltage_map[rid];
674 new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
682 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
686 if (tmp_dmg > damage) {
693 } while (desc->min + desc->step + new_val <= desc->max);
698 if (pdata->ignore_gpiodvs_side_effect == false)
701 dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
702 " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
705 if (new_idx < 0 || new_val < 0)
708 max8997->buck125_gpioindex = new_idx;
709 max8997_set_gpio(max8997);
715 static const int safeoutvolt[] = {
722 /* For SAFEOUT1 and SAFEOUT2 */
723 static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
724 int min_uV, int max_uV, unsigned *selector)
726 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
727 struct i2c_client *i2c = max8997->iodev->i2c;
728 int rid = max8997_get_rid(rdev);
729 int reg, shift = 0, mask, ret;
733 if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
736 for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
737 if (min_uV <= safeoutvolt[i] &&
738 max_uV >= safeoutvolt[i])
742 if (i >= ARRAY_SIZE(safeoutvolt))
750 ret = max8997_get_voltage_register(rdev, ®, &shift, &mask);
754 ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
760 static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
765 static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
767 struct max8997_data *max8997 = rdev_get_drvdata(rdev);
768 struct i2c_client *i2c = max8997->iodev->i2c;
769 int ret, reg, mask, pattern;
770 int rid = max8997_get_rid(rdev);
772 ret = max8997_get_enable_register(rdev, ®, &mask, &pattern);
776 max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
778 if (rid == MAX8997_LDO1 ||
779 rid == MAX8997_LDO10 ||
780 rid == MAX8997_LDO21) {
781 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
783 return max8997_update_reg(i2c, reg, 0x40, mask);
786 dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
787 rdev->desc->name, max8997->saved_states[rid] & mask,
789 return max8997_update_reg(i2c, reg, ~pattern, mask);
792 static struct regulator_ops max8997_ldo_ops = {
793 .list_voltage = max8997_list_voltage,
794 .is_enabled = max8997_reg_is_enabled,
795 .enable = max8997_reg_enable,
796 .disable = max8997_reg_disable,
797 .get_voltage = max8997_get_voltage,
798 .set_voltage = max8997_set_voltage_ldobuck,
799 .set_suspend_enable = max8997_reg_enable_suspend,
800 .set_suspend_disable = max8997_reg_disable_suspend,
803 static struct regulator_ops max8997_buck_ops = {
804 .list_voltage = max8997_list_voltage,
805 .is_enabled = max8997_reg_is_enabled,
806 .enable = max8997_reg_enable,
807 .disable = max8997_reg_disable,
808 .get_voltage = max8997_get_voltage,
809 .set_voltage = max8997_set_voltage_buck,
810 .set_suspend_enable = max8997_reg_enable_suspend,
811 .set_suspend_disable = max8997_reg_disable_suspend,
814 static struct regulator_ops max8997_fixedvolt_ops = {
815 .list_voltage = max8997_list_voltage,
816 .is_enabled = max8997_reg_is_enabled,
817 .enable = max8997_reg_enable,
818 .disable = max8997_reg_disable,
819 .set_suspend_enable = max8997_reg_enable_suspend,
820 .set_suspend_disable = max8997_reg_disable_suspend,
823 static struct regulator_ops max8997_safeout_ops = {
824 .list_voltage = max8997_list_voltage_safeout,
825 .is_enabled = max8997_reg_is_enabled,
826 .enable = max8997_reg_enable,
827 .disable = max8997_reg_disable,
828 .get_voltage = max8997_get_voltage,
829 .set_voltage = max8997_set_voltage_safeout,
830 .set_suspend_enable = max8997_reg_enable_suspend,
831 .set_suspend_disable = max8997_reg_disable_suspend,
834 static struct regulator_ops max8997_fixedstate_ops = {
835 .list_voltage = max8997_list_voltage_charger_cv,
836 .get_voltage = max8997_get_voltage,
837 .set_voltage = max8997_set_voltage_charger_cv,
840 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev *rdev,
841 int min_uV, int max_uV)
845 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, &dummy);
849 static struct regulator_ops max8997_charger_ops = {
850 .is_enabled = max8997_reg_is_enabled,
851 .enable = max8997_reg_enable,
852 .disable = max8997_reg_disable,
853 .get_current_limit = max8997_get_voltage,
854 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
857 static struct regulator_ops max8997_charger_fixedstate_ops = {
858 .is_enabled = max8997_reg_is_enabled,
859 .get_current_limit = max8997_get_voltage,
860 .set_current_limit = max8997_set_voltage_ldobuck_wrap,
863 #define regulator_desc_ldo(num) { \
865 .id = MAX8997_LDO##num, \
866 .ops = &max8997_ldo_ops, \
867 .type = REGULATOR_VOLTAGE, \
868 .owner = THIS_MODULE, \
870 #define regulator_desc_buck(num) { \
871 .name = "BUCK"#num, \
872 .id = MAX8997_BUCK##num, \
873 .ops = &max8997_buck_ops, \
874 .type = REGULATOR_VOLTAGE, \
875 .owner = THIS_MODULE, \
878 static struct regulator_desc regulators[] = {
879 regulator_desc_ldo(1),
880 regulator_desc_ldo(2),
881 regulator_desc_ldo(3),
882 regulator_desc_ldo(4),
883 regulator_desc_ldo(5),
884 regulator_desc_ldo(6),
885 regulator_desc_ldo(7),
886 regulator_desc_ldo(8),
887 regulator_desc_ldo(9),
888 regulator_desc_ldo(10),
889 regulator_desc_ldo(11),
890 regulator_desc_ldo(12),
891 regulator_desc_ldo(13),
892 regulator_desc_ldo(14),
893 regulator_desc_ldo(15),
894 regulator_desc_ldo(16),
895 regulator_desc_ldo(17),
896 regulator_desc_ldo(18),
897 regulator_desc_ldo(21),
898 regulator_desc_buck(1),
899 regulator_desc_buck(2),
900 regulator_desc_buck(3),
901 regulator_desc_buck(4),
902 regulator_desc_buck(5),
906 .ops = &max8997_fixedvolt_ops,
907 .type = REGULATOR_VOLTAGE,
908 .owner = THIS_MODULE,
910 regulator_desc_buck(7),
912 .name = "EN32KHz AP",
913 .id = MAX8997_EN32KHZ_AP,
914 .ops = &max8997_fixedvolt_ops,
915 .type = REGULATOR_VOLTAGE,
916 .owner = THIS_MODULE,
918 .name = "EN32KHz CP",
919 .id = MAX8997_EN32KHZ_CP,
920 .ops = &max8997_fixedvolt_ops,
921 .type = REGULATOR_VOLTAGE,
922 .owner = THIS_MODULE,
925 .id = MAX8997_ENVICHG,
926 .ops = &max8997_fixedvolt_ops,
927 .type = REGULATOR_VOLTAGE,
928 .owner = THIS_MODULE,
931 .id = MAX8997_ESAFEOUT1,
932 .ops = &max8997_safeout_ops,
933 .type = REGULATOR_VOLTAGE,
934 .owner = THIS_MODULE,
937 .id = MAX8997_ESAFEOUT2,
938 .ops = &max8997_safeout_ops,
939 .type = REGULATOR_VOLTAGE,
940 .owner = THIS_MODULE,
942 .name = "CHARGER CV",
943 .id = MAX8997_CHARGER_CV,
944 .ops = &max8997_fixedstate_ops,
945 .type = REGULATOR_VOLTAGE,
946 .owner = THIS_MODULE,
949 .id = MAX8997_CHARGER,
950 .ops = &max8997_charger_ops,
951 .type = REGULATOR_CURRENT,
952 .owner = THIS_MODULE,
954 .name = "CHARGER TOPOFF",
955 .id = MAX8997_CHARGER_TOPOFF,
956 .ops = &max8997_charger_fixedstate_ops,
957 .type = REGULATOR_CURRENT,
958 .owner = THIS_MODULE,
962 static __devinit int max8997_pmic_probe(struct platform_device *pdev)
964 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
965 struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
966 struct regulator_dev **rdev;
967 struct max8997_data *max8997;
968 struct i2c_client *i2c;
970 u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
973 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
977 max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
981 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
982 max8997->rdev = kzalloc(size, GFP_KERNEL);
983 if (!max8997->rdev) {
988 rdev = max8997->rdev;
989 max8997->dev = &pdev->dev;
990 max8997->iodev = iodev;
991 max8997->num_regulators = pdata->num_regulators;
992 platform_set_drvdata(pdev, max8997);
993 i2c = max8997->iodev->i2c;
995 max8997->buck125_gpioindex = pdata->buck125_default_idx;
997 for (i = 0; i < 8; i++) {
998 max8997->buck1_vol[i] = ret =
999 max8997_get_voltage_proper_val(
1000 &buck1245_voltage_map_desc,
1001 pdata->buck1_voltage[i] / 1000,
1002 pdata->buck1_voltage[i] / 1000 +
1003 buck1245_voltage_map_desc.step);
1007 max8997->buck2_vol[i] = ret =
1008 max8997_get_voltage_proper_val(
1009 &buck1245_voltage_map_desc,
1010 pdata->buck2_voltage[i] / 1000,
1011 pdata->buck2_voltage[i] / 1000 +
1012 buck1245_voltage_map_desc.step);
1016 max8997->buck5_vol[i] = ret =
1017 max8997_get_voltage_proper_val(
1018 &buck1245_voltage_map_desc,
1019 pdata->buck5_voltage[i] / 1000,
1020 pdata->buck5_voltage[i] / 1000 +
1021 buck1245_voltage_map_desc.step);
1025 if (max_buck1 < max8997->buck1_vol[i])
1026 max_buck1 = max8997->buck1_vol[i];
1027 if (max_buck2 < max8997->buck2_vol[i])
1028 max_buck2 = max8997->buck2_vol[i];
1029 if (max_buck5 < max8997->buck5_vol[i])
1030 max_buck5 = max8997->buck5_vol[i];
1033 /* For the safety, set max voltage before setting up */
1034 for (i = 0; i < 8; i++) {
1035 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1037 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1039 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1044 * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1045 * If at least one of them cares, set gpios.
1047 if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1048 pdata->buck5_gpiodvs) {
1049 bool gpio1set = false, gpio2set = false;
1051 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1052 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1053 !gpio_is_valid(pdata->buck125_gpios[2])) {
1054 dev_err(&pdev->dev, "GPIO NOT VALID\n");
1059 ret = gpio_request(pdata->buck125_gpios[0],
1062 dev_warn(&pdev->dev, "Duplicated gpio request"
1069 ret = gpio_request(pdata->buck125_gpios[1],
1072 dev_warn(&pdev->dev, "Duplicated gpio request"
1076 gpio_free(pdata->buck125_gpios[0]);
1081 ret = gpio_request(pdata->buck125_gpios[2],
1084 dev_warn(&pdev->dev, "Duplicated gpio request"
1088 gpio_free(pdata->buck125_gpios[0]);
1090 gpio_free(pdata->buck125_gpios[1]);
1094 gpio_direction_output(pdata->buck125_gpios[0],
1095 (max8997->buck125_gpioindex >> 2)
1097 gpio_direction_output(pdata->buck125_gpios[1],
1098 (max8997->buck125_gpioindex >> 1)
1100 gpio_direction_output(pdata->buck125_gpios[2],
1101 (max8997->buck125_gpioindex >> 0)
1106 /* DVS-GPIO disabled */
1107 max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1108 (1 << 1) : (0 << 1), 1 << 1);
1109 max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1110 (1 << 1) : (0 << 1), 1 << 1);
1111 max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1112 (1 << 1) : (0 << 1), 1 << 1);
1114 /* Initialize all the DVS related BUCK registers */
1115 for (i = 0; i < 8; i++) {
1116 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i,
1117 max8997->buck1_vol[i],
1119 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i,
1120 max8997->buck2_vol[i],
1122 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i,
1123 max8997->buck5_vol[i],
1127 for (i = 0; i < pdata->num_regulators; i++) {
1128 const struct voltage_map_desc *desc;
1129 int id = pdata->regulators[i].id;
1131 desc = reg_voltage_map[id];
1133 regulators[id].n_voltages =
1134 (desc->max - desc->min) / desc->step + 1;
1135 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1136 regulators[id].n_voltages = 4;
1137 else if (id == MAX8997_CHARGER_CV)
1138 regulators[id].n_voltages = 16;
1140 rdev[i] = regulator_register(®ulators[id], max8997->dev,
1141 pdata->regulators[i].initdata, max8997);
1142 if (IS_ERR(rdev[i])) {
1143 ret = PTR_ERR(rdev[i]);
1144 dev_err(max8997->dev, "regulator init failed for %d\n",
1152 max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1153 max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1157 for (i = 0; i < max8997->num_regulators; i++)
1159 regulator_unregister(rdev[i]);
1161 kfree(max8997->rdev);
1167 static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1169 struct max8997_data *max8997 = platform_get_drvdata(pdev);
1170 struct regulator_dev **rdev = max8997->rdev;
1173 for (i = 0; i < max8997->num_regulators; i++)
1175 regulator_unregister(rdev[i]);
1177 kfree(max8997->rdev);
1183 static const struct platform_device_id max8997_pmic_id[] = {
1184 { "max8997-pmic", 0},
1187 MODULE_DEVICE_TABLE(platform, max8997_pmic_id);
1189 static struct platform_driver max8997_pmic_driver = {
1191 .name = "max8997-pmic",
1192 .owner = THIS_MODULE,
1194 .probe = max8997_pmic_probe,
1195 .remove = __devexit_p(max8997_pmic_remove),
1196 .id_table = max8997_pmic_id,
1199 static int __init max8997_pmic_init(void)
1201 return platform_driver_register(&max8997_pmic_driver);
1203 subsys_initcall(max8997_pmic_init);
1205 static void __exit max8997_pmic_cleanup(void)
1207 platform_driver_unregister(&max8997_pmic_driver);
1209 module_exit(max8997_pmic_cleanup);
1211 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1212 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1213 MODULE_LICENSE("GPL");