Commit | Line | Data |
---|---|---|
80b022e2 JL |
1 | /* |
2 | * max77693.c - Regulator driver for the Maxim 77693 | |
3 | * | |
4 | * Copyright (C) 2013 Samsung Electronics | |
5 | * Jonghwa Lee <jonghwa3.lee@samsung.com> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | * | |
21 | * This driver is based on max77686.c | |
22 | */ | |
23 | ||
24 | #include <linux/err.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/export.h> | |
29 | #include <linux/regulator/driver.h> | |
30 | #include <linux/regulator/machine.h> | |
31 | #include <linux/mfd/max77693.h> | |
61b305cd | 32 | #include <linux/mfd/max77693-common.h> |
80b022e2 JL |
33 | #include <linux/mfd/max77693-private.h> |
34 | #include <linux/regulator/of_regulator.h> | |
d0540f91 | 35 | #include <linux/regmap.h> |
80b022e2 | 36 | |
5b5e771f KK |
37 | /* Charger regulator differences between MAX77693 and MAX77843 */ |
38 | struct chg_reg_data { | |
39 | unsigned int linear_reg; | |
40 | unsigned int linear_mask; | |
41 | unsigned int uA_step; | |
42 | unsigned int min_sel; | |
43 | }; | |
80b022e2 | 44 | |
80b022e2 JL |
45 | /* |
46 | * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA | |
47 | * 0x00, 0x01, 0x2, 0x03 = 60 mA | |
48 | * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA | |
49 | */ | |
50 | static int max77693_chg_get_current_limit(struct regulator_dev *rdev) | |
51 | { | |
5b5e771f | 52 | const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev); |
80b022e2 JL |
53 | unsigned int chg_min_uA = rdev->constraints->min_uA; |
54 | unsigned int chg_max_uA = rdev->constraints->max_uA; | |
d0540f91 | 55 | unsigned int reg, sel; |
80b022e2 JL |
56 | unsigned int val; |
57 | int ret; | |
58 | ||
5b5e771f | 59 | ret = regmap_read(rdev->regmap, reg_data->linear_reg, ®); |
80b022e2 JL |
60 | if (ret < 0) |
61 | return ret; | |
62 | ||
5b5e771f | 63 | sel = reg & reg_data->linear_mask; |
80b022e2 JL |
64 | |
65 | /* the first four codes for charger current are all 60mA */ | |
5b5e771f | 66 | if (sel <= reg_data->min_sel) |
80b022e2 JL |
67 | sel = 0; |
68 | else | |
5b5e771f | 69 | sel -= reg_data->min_sel; |
80b022e2 | 70 | |
5b5e771f | 71 | val = chg_min_uA + reg_data->uA_step * sel; |
80b022e2 JL |
72 | if (val > chg_max_uA) |
73 | return -EINVAL; | |
74 | ||
75 | return val; | |
76 | } | |
77 | ||
78 | static int max77693_chg_set_current_limit(struct regulator_dev *rdev, | |
79 | int min_uA, int max_uA) | |
80 | { | |
5b5e771f | 81 | const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev); |
80b022e2 JL |
82 | unsigned int chg_min_uA = rdev->constraints->min_uA; |
83 | int sel = 0; | |
84 | ||
5b5e771f | 85 | while (chg_min_uA + reg_data->uA_step * sel < min_uA) |
80b022e2 JL |
86 | sel++; |
87 | ||
5b5e771f | 88 | if (chg_min_uA + reg_data->uA_step * sel > max_uA) |
80b022e2 JL |
89 | return -EINVAL; |
90 | ||
91 | /* the first four codes for charger current are all 60mA */ | |
5b5e771f | 92 | sel += reg_data->min_sel; |
80b022e2 | 93 | |
5b5e771f | 94 | return regmap_write(rdev->regmap, reg_data->linear_reg, sel); |
80b022e2 JL |
95 | } |
96 | /* end of CHARGER regulator ops */ | |
97 | ||
98 | static const unsigned int max77693_safeout_table[] = { | |
99 | 4850000, | |
100 | 4900000, | |
101 | 4950000, | |
102 | 3300000, | |
103 | }; | |
104 | ||
105 | static struct regulator_ops max77693_safeout_ops = { | |
106 | .list_voltage = regulator_list_voltage_table, | |
107 | .is_enabled = regulator_is_enabled_regmap, | |
108 | .enable = regulator_enable_regmap, | |
109 | .disable = regulator_disable_regmap, | |
110 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
111 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
112 | }; | |
113 | ||
114 | static struct regulator_ops max77693_charger_ops = { | |
39d23308 | 115 | .is_enabled = regulator_is_enabled_regmap, |
80b022e2 JL |
116 | .enable = regulator_enable_regmap, |
117 | .disable = regulator_disable_regmap, | |
118 | .get_current_limit = max77693_chg_get_current_limit, | |
119 | .set_current_limit = max77693_chg_set_current_limit, | |
120 | }; | |
121 | ||
122 | #define regulator_desc_esafeout(_num) { \ | |
123 | .name = "ESAFEOUT"#_num, \ | |
124 | .id = MAX77693_ESAFEOUT##_num, \ | |
222d0f04 KK |
125 | .of_match = of_match_ptr("ESAFEOUT"#_num), \ |
126 | .regulators_node = of_match_ptr("regulators"), \ | |
80b022e2 JL |
127 | .n_voltages = 4, \ |
128 | .ops = &max77693_safeout_ops, \ | |
129 | .type = REGULATOR_VOLTAGE, \ | |
52f48bf3 | 130 | .owner = THIS_MODULE, \ |
80b022e2 JL |
131 | .volt_table = max77693_safeout_table, \ |
132 | .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ | |
133 | .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \ | |
134 | .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ | |
135 | .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \ | |
136 | } | |
137 | ||
2515b24c | 138 | static const struct regulator_desc regulators[] = { |
80b022e2 JL |
139 | regulator_desc_esafeout(1), |
140 | regulator_desc_esafeout(2), | |
141 | { | |
142 | .name = "CHARGER", | |
143 | .id = MAX77693_CHARGER, | |
222d0f04 KK |
144 | .of_match = of_match_ptr("CHARGER"), |
145 | .regulators_node = of_match_ptr("regulators"), | |
80b022e2 JL |
146 | .ops = &max77693_charger_ops, |
147 | .type = REGULATOR_CURRENT, | |
148 | .owner = THIS_MODULE, | |
149 | .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00, | |
150 | .enable_mask = CHG_CNFG_00_CHG_MASK | | |
151 | CHG_CNFG_00_BUCK_MASK, | |
39d23308 | 152 | .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK, |
80b022e2 JL |
153 | }, |
154 | }; | |
155 | ||
5b5e771f KK |
156 | static const struct chg_reg_data max77693_chg_reg_data = { |
157 | .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09, | |
158 | .linear_mask = CHG_CNFG_09_CHGIN_ILIM_MASK, | |
159 | .uA_step = 20000, | |
160 | .min_sel = 3, | |
161 | }; | |
162 | ||
80b022e2 JL |
163 | static int max77693_pmic_probe(struct platform_device *pdev) |
164 | { | |
165 | struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent); | |
222d0f04 | 166 | int i; |
ca0c37a0 | 167 | struct regulator_config config = { }; |
80b022e2 | 168 | |
222d0f04 | 169 | config.dev = iodev->dev; |
80b022e2 | 170 | config.regmap = iodev->regmap; |
5b5e771f | 171 | config.driver_data = (void *)&max77693_chg_reg_data; |
80b022e2 | 172 | |
222d0f04 | 173 | for (i = 0; i < ARRAY_SIZE(regulators); i++) { |
640c24a7 | 174 | struct regulator_dev *rdev; |
80b022e2 | 175 | |
640c24a7 | 176 | rdev = devm_regulator_register(&pdev->dev, |
222d0f04 | 177 | ®ulators[i], &config); |
640c24a7 KK |
178 | if (IS_ERR(rdev)) { |
179 | dev_err(&pdev->dev, | |
222d0f04 | 180 | "Failed to initialize regulator-%d\n", i); |
640c24a7 | 181 | return PTR_ERR(rdev); |
80b022e2 JL |
182 | } |
183 | } | |
184 | ||
80b022e2 JL |
185 | return 0; |
186 | } | |
187 | ||
188 | static const struct platform_device_id max77693_pmic_id[] = { | |
5b5e771f | 189 | { "max77693-pmic", TYPE_MAX77693 }, |
80b022e2 JL |
190 | {}, |
191 | }; | |
192 | ||
193 | MODULE_DEVICE_TABLE(platform, max77693_pmic_id); | |
194 | ||
195 | static struct platform_driver max77693_pmic_driver = { | |
196 | .driver = { | |
197 | .name = "max77693-pmic", | |
80b022e2 JL |
198 | }, |
199 | .probe = max77693_pmic_probe, | |
80b022e2 JL |
200 | .id_table = max77693_pmic_id, |
201 | }; | |
202 | ||
203 | module_platform_driver(max77693_pmic_driver); | |
204 | ||
205 | MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver"); | |
206 | MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>"); | |
207 | MODULE_LICENSE("GPL"); |