Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / regulator / max77693-regulator.c
blobcfbb9512e48623429899cbe0785332b0a1171734
1 /*
2 * max77693.c - Regulator driver for the Maxim 77693 and 77843
4 * Copyright (C) 2013-2015 Samsung Electronics
5 * Jonghwa Lee <jonghwa3.lee@samsung.com>
6 * Krzysztof Kozlowski <krzk@kernel.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This driver is based on max77686.c
25 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/platform_device.h>
28 #include <linux/module.h>
29 #include <linux/export.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/mfd/max77693.h>
33 #include <linux/mfd/max77693-common.h>
34 #include <linux/mfd/max77693-private.h>
35 #include <linux/mfd/max77843-private.h>
36 #include <linux/regulator/of_regulator.h>
37 #include <linux/regmap.h>
40 * ID for MAX77843 regulators.
41 * There is no need for such for MAX77693.
43 enum max77843_regulator_type {
44 MAX77843_SAFEOUT1 = 0,
45 MAX77843_SAFEOUT2,
46 MAX77843_CHARGER,
48 MAX77843_NUM,
51 /* Register differences between chargers: MAX77693 and MAX77843 */
52 struct chg_reg_data {
53 unsigned int linear_reg;
54 unsigned int linear_mask;
55 unsigned int uA_step;
56 unsigned int min_sel;
60 * MAX77693 CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA
61 * 0x00, 0x01, 0x2, 0x03 = 60 mA
62 * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA
63 * Actually for MAX77693 the driver manipulates the maximum input current,
64 * not the fast charge current (output). This should be fixed.
66 * On MAX77843 the calculation formula is the same (except values).
67 * Fortunately it properly manipulates the fast charge current.
69 static int max77693_chg_get_current_limit(struct regulator_dev *rdev)
71 const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
72 unsigned int chg_min_uA = rdev->constraints->min_uA;
73 unsigned int chg_max_uA = rdev->constraints->max_uA;
74 unsigned int reg, sel;
75 unsigned int val;
76 int ret;
78 ret = regmap_read(rdev->regmap, reg_data->linear_reg, &reg);
79 if (ret < 0)
80 return ret;
82 sel = reg & reg_data->linear_mask;
84 /* the first four codes for charger current are all 60mA */
85 if (sel <= reg_data->min_sel)
86 sel = 0;
87 else
88 sel -= reg_data->min_sel;
90 val = chg_min_uA + reg_data->uA_step * sel;
91 if (val > chg_max_uA)
92 return -EINVAL;
94 return val;
97 static int max77693_chg_set_current_limit(struct regulator_dev *rdev,
98 int min_uA, int max_uA)
100 const struct chg_reg_data *reg_data = rdev_get_drvdata(rdev);
101 unsigned int chg_min_uA = rdev->constraints->min_uA;
102 int sel = 0;
104 while (chg_min_uA + reg_data->uA_step * sel < min_uA)
105 sel++;
107 if (chg_min_uA + reg_data->uA_step * sel > max_uA)
108 return -EINVAL;
110 /* the first four codes for charger current are all 60mA */
111 sel += reg_data->min_sel;
113 return regmap_write(rdev->regmap, reg_data->linear_reg, sel);
115 /* end of CHARGER regulator ops */
117 /* Returns regmap suitable for given regulator on chosen device */
118 static struct regmap *max77693_get_regmap(enum max77693_types type,
119 struct max77693_dev *max77693,
120 int reg_id)
122 if (type == TYPE_MAX77693)
123 return max77693->regmap;
125 /* Else: TYPE_MAX77843 */
126 switch (reg_id) {
127 case MAX77843_SAFEOUT1:
128 case MAX77843_SAFEOUT2:
129 return max77693->regmap;
130 case MAX77843_CHARGER:
131 return max77693->regmap_chg;
132 default:
133 return max77693->regmap;
137 static const unsigned int max77693_safeout_table[] = {
138 4850000,
139 4900000,
140 4950000,
141 3300000,
144 static struct regulator_ops max77693_safeout_ops = {
145 .list_voltage = regulator_list_voltage_table,
146 .is_enabled = regulator_is_enabled_regmap,
147 .enable = regulator_enable_regmap,
148 .disable = regulator_disable_regmap,
149 .get_voltage_sel = regulator_get_voltage_sel_regmap,
150 .set_voltage_sel = regulator_set_voltage_sel_regmap,
153 static struct regulator_ops max77693_charger_ops = {
154 .is_enabled = regulator_is_enabled_regmap,
155 .enable = regulator_enable_regmap,
156 .disable = regulator_disable_regmap,
157 .get_current_limit = max77693_chg_get_current_limit,
158 .set_current_limit = max77693_chg_set_current_limit,
161 #define max77693_regulator_desc_esafeout(_num) { \
162 .name = "ESAFEOUT"#_num, \
163 .id = MAX77693_ESAFEOUT##_num, \
164 .of_match = of_match_ptr("ESAFEOUT"#_num), \
165 .regulators_node = of_match_ptr("regulators"), \
166 .n_voltages = 4, \
167 .ops = &max77693_safeout_ops, \
168 .type = REGULATOR_VOLTAGE, \
169 .owner = THIS_MODULE, \
170 .volt_table = max77693_safeout_table, \
171 .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
172 .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \
173 .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \
174 .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \
177 static const struct regulator_desc max77693_supported_regulators[] = {
178 max77693_regulator_desc_esafeout(1),
179 max77693_regulator_desc_esafeout(2),
181 .name = "CHARGER",
182 .id = MAX77693_CHARGER,
183 .of_match = of_match_ptr("CHARGER"),
184 .regulators_node = of_match_ptr("regulators"),
185 .ops = &max77693_charger_ops,
186 .type = REGULATOR_CURRENT,
187 .owner = THIS_MODULE,
188 .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00,
189 .enable_mask = CHG_CNFG_00_CHG_MASK |
190 CHG_CNFG_00_BUCK_MASK,
191 .enable_val = CHG_CNFG_00_CHG_MASK | CHG_CNFG_00_BUCK_MASK,
195 static const struct chg_reg_data max77693_chg_reg_data = {
196 .linear_reg = MAX77693_CHG_REG_CHG_CNFG_09,
197 .linear_mask = CHG_CNFG_09_CHGIN_ILIM_MASK,
198 .uA_step = 20000,
199 .min_sel = 3,
202 #define max77843_regulator_desc_esafeout(num) { \
203 .name = "SAFEOUT" # num, \
204 .id = MAX77843_SAFEOUT ## num, \
205 .ops = &max77693_safeout_ops, \
206 .of_match = of_match_ptr("SAFEOUT" # num), \
207 .regulators_node = of_match_ptr("regulators"), \
208 .type = REGULATOR_VOLTAGE, \
209 .owner = THIS_MODULE, \
210 .n_voltages = ARRAY_SIZE(max77693_safeout_table), \
211 .volt_table = max77693_safeout_table, \
212 .enable_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
213 .enable_mask = MAX77843_REG_SAFEOUTCTRL_ENSAFEOUT ## num, \
214 .vsel_reg = MAX77843_SYS_REG_SAFEOUTCTRL, \
215 .vsel_mask = MAX77843_REG_SAFEOUTCTRL_SAFEOUT ## num ## _MASK, \
218 static const struct regulator_desc max77843_supported_regulators[] = {
219 [MAX77843_SAFEOUT1] = max77843_regulator_desc_esafeout(1),
220 [MAX77843_SAFEOUT2] = max77843_regulator_desc_esafeout(2),
221 [MAX77843_CHARGER] = {
222 .name = "CHARGER",
223 .id = MAX77843_CHARGER,
224 .ops = &max77693_charger_ops,
225 .of_match = of_match_ptr("CHARGER"),
226 .regulators_node = of_match_ptr("regulators"),
227 .type = REGULATOR_CURRENT,
228 .owner = THIS_MODULE,
229 .enable_reg = MAX77843_CHG_REG_CHG_CNFG_00,
230 .enable_mask = MAX77843_CHG_MASK,
231 .enable_val = MAX77843_CHG_MASK,
235 static const struct chg_reg_data max77843_chg_reg_data = {
236 .linear_reg = MAX77843_CHG_REG_CHG_CNFG_02,
237 .linear_mask = MAX77843_CHG_FAST_CHG_CURRENT_MASK,
238 .uA_step = MAX77843_CHG_FAST_CHG_CURRENT_STEP,
239 .min_sel = 2,
242 static int max77693_pmic_probe(struct platform_device *pdev)
244 enum max77693_types type = platform_get_device_id(pdev)->driver_data;
245 struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent);
246 const struct regulator_desc *regulators;
247 unsigned int regulators_size;
248 int i;
249 struct regulator_config config = { };
251 config.dev = iodev->dev;
253 switch (type) {
254 case TYPE_MAX77693:
255 regulators = max77693_supported_regulators;
256 regulators_size = ARRAY_SIZE(max77693_supported_regulators);
257 config.driver_data = (void *)&max77693_chg_reg_data;
258 break;
259 case TYPE_MAX77843:
260 regulators = max77843_supported_regulators;
261 regulators_size = ARRAY_SIZE(max77843_supported_regulators);
262 config.driver_data = (void *)&max77843_chg_reg_data;
263 break;
264 default:
265 dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
266 return -ENODEV;
269 for (i = 0; i < regulators_size; i++) {
270 struct regulator_dev *rdev;
272 config.regmap = max77693_get_regmap(type, iodev,
273 regulators[i].id);
275 rdev = devm_regulator_register(&pdev->dev,
276 &regulators[i], &config);
277 if (IS_ERR(rdev)) {
278 dev_err(&pdev->dev,
279 "Failed to initialize regulator-%d\n", i);
280 return PTR_ERR(rdev);
284 return 0;
287 static const struct platform_device_id max77693_pmic_id[] = {
288 { "max77693-pmic", TYPE_MAX77693 },
289 { "max77843-regulator", TYPE_MAX77843 },
293 MODULE_DEVICE_TABLE(platform, max77693_pmic_id);
295 static struct platform_driver max77693_pmic_driver = {
296 .driver = {
297 .name = "max77693-pmic",
299 .probe = max77693_pmic_probe,
300 .id_table = max77693_pmic_id,
303 static int __init max77693_pmic_init(void)
305 return platform_driver_register(&max77693_pmic_driver);
307 subsys_initcall(max77693_pmic_init);
309 static void __exit max77693_pmic_cleanup(void)
311 platform_driver_unregister(&max77693_pmic_driver);
313 module_exit(max77693_pmic_cleanup);
315 MODULE_DESCRIPTION("MAXIM 77693/77843 regulator driver");
316 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
317 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
318 MODULE_LICENSE("GPL");