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,
51 /* Register differences between chargers: MAX77693 and MAX77843 */
53 unsigned int linear_reg;
54 unsigned int linear_mask;
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;
78 ret = regmap_read(rdev->regmap, reg_data->linear_reg, ®);
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)
88 sel -= reg_data->min_sel;
90 val = chg_min_uA + reg_data->uA_step * sel;
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;
104 while (chg_min_uA + reg_data->uA_step * sel < min_uA)
107 if (chg_min_uA + reg_data->uA_step * sel > max_uA)
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,
122 if (type == TYPE_MAX77693)
123 return max77693->regmap;
125 /* Else: TYPE_MAX77843 */
127 case MAX77843_SAFEOUT1:
128 case MAX77843_SAFEOUT2:
129 return max77693->regmap;
130 case MAX77843_CHARGER:
131 return max77693->regmap_chg;
133 return max77693->regmap;
137 static const unsigned int max77693_safeout_table[] = {
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"), \
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),
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,
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] = {
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,
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;
249 struct regulator_config config = { };
251 config.dev = iodev->dev;
255 regulators = max77693_supported_regulators;
256 regulators_size = ARRAY_SIZE(max77693_supported_regulators);
257 config.driver_data = (void *)&max77693_chg_reg_data;
260 regulators = max77843_supported_regulators;
261 regulators_size = ARRAY_SIZE(max77843_supported_regulators);
262 config.driver_data = (void *)&max77843_chg_reg_data;
265 dev_err(&pdev->dev, "Unsupported device type: %u\n", type);
269 for (i = 0; i < regulators_size; i++) {
270 struct regulator_dev *rdev;
272 config.regmap = max77693_get_regmap(type, iodev,
275 rdev = devm_regulator_register(&pdev->dev,
276 ®ulators[i], &config);
279 "Failed to initialize regulator-%d\n", i);
280 return PTR_ERR(rdev);
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 = {
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");