blob: b3678d289619330ffca48f338b9ddf362cd76986 [file] [log] [blame]
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +01001/*
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +02002 * max14577.c - Regulator driver for the Maxim 14577/77836
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +01003 *
Krzysztof Kozlowski4d047d62014-01-28 13:18:25 +01004 * Copyright (C) 2013,2014 Samsung Electronics
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +01005 * Krzysztof Kozlowski <k.kozlowski@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
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/mfd/max14577.h>
22#include <linux/mfd/max14577-private.h>
23#include <linux/regulator/of_regulator.h>
24
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010025static int max14577_reg_is_enabled(struct regulator_dev *rdev)
26{
27 int rid = rdev_get_id(rdev);
28 struct regmap *rmap = rdev->regmap;
29 u8 reg_data;
30
31 switch (rid) {
32 case MAX14577_CHARGER:
33 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
34 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
35 return 0;
36 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
37 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
38 return 0;
39 /* MBCHOSTEN and CGMBC are on */
40 return 1;
41 default:
42 return -EINVAL;
43 }
44}
45
46static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
47{
48 u8 reg_data;
49 struct regmap *rmap = rdev->regmap;
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020050 struct max14577 *max14577 = rdev_get_drvdata(rdev);
51 const struct maxim_charger_current *limits =
52 &maxim_charger_currents[max14577->dev_type];
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010053
54 if (rdev_get_id(rdev) != MAX14577_CHARGER)
55 return -EINVAL;
56
57 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, &reg_data);
58
59 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020060 return limits->min;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010061
62 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
63 CHGCTRL4_MBCICHWRCH_SHIFT);
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020064 return limits->high_start + reg_data * limits->high_step;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010065}
66
67static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
68 int min_uA, int max_uA)
69{
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010070 u8 reg_data;
Krzysztof Kozlowskib8f139f2014-09-12 08:53:56 +020071 int ret;
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +020072 struct max14577 *max14577 = rdev_get_drvdata(rdev);
73 const struct maxim_charger_current *limits =
74 &maxim_charger_currents[max14577->dev_type];
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010075
76 if (rdev_get_id(rdev) != MAX14577_CHARGER)
77 return -EINVAL;
78
Krzysztof Kozlowskib8f139f2014-09-12 08:53:56 +020079 ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, &reg_data);
80 if (ret)
81 return ret;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +010082
83 return max14577_update_reg(rdev->regmap, MAX14577_CHG_REG_CHG_CTRL4,
84 CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
85 reg_data);
86}
87
88static struct regulator_ops max14577_safeout_ops = {
89 .is_enabled = regulator_is_enabled_regmap,
90 .enable = regulator_enable_regmap,
91 .disable = regulator_disable_regmap,
92 .list_voltage = regulator_list_voltage_linear,
93};
94
95static struct regulator_ops max14577_charger_ops = {
96 .is_enabled = max14577_reg_is_enabled,
97 .enable = regulator_enable_regmap,
98 .disable = regulator_disable_regmap,
99 .get_current_limit = max14577_reg_get_current_limit,
100 .set_current_limit = max14577_reg_set_current_limit,
101};
102
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200103static const struct regulator_desc max14577_supported_regulators[] = {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100104 [MAX14577_SAFEOUT] = {
105 .name = "SAFEOUT",
Beomho Seoe951cee2014-12-19 18:04:46 +0900106 .of_match = of_match_ptr("SAFEOUT"),
107 .regulators_node = of_match_ptr("regulators"),
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100108 .id = MAX14577_SAFEOUT,
109 .ops = &max14577_safeout_ops,
110 .type = REGULATOR_VOLTAGE,
111 .owner = THIS_MODULE,
112 .n_voltages = 1,
113 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
114 .enable_reg = MAX14577_REG_CONTROL2,
115 .enable_mask = CTRL2_SFOUTORD_MASK,
116 },
117 [MAX14577_CHARGER] = {
118 .name = "CHARGER",
Beomho Seoe951cee2014-12-19 18:04:46 +0900119 .of_match = of_match_ptr("CHARGER"),
120 .regulators_node = of_match_ptr("regulators"),
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100121 .id = MAX14577_CHARGER,
122 .ops = &max14577_charger_ops,
123 .type = REGULATOR_CURRENT,
124 .owner = THIS_MODULE,
125 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2,
126 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK,
127 },
128};
129
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200130static struct regulator_ops max77836_ldo_ops = {
131 .is_enabled = regulator_is_enabled_regmap,
132 .enable = regulator_enable_regmap,
133 .disable = regulator_disable_regmap,
134 .list_voltage = regulator_list_voltage_linear,
135 .map_voltage = regulator_map_voltage_linear,
136 .get_voltage_sel = regulator_get_voltage_sel_regmap,
137 .set_voltage_sel = regulator_set_voltage_sel_regmap,
138 /* TODO: add .set_suspend_mode */
139};
140
141static const struct regulator_desc max77836_supported_regulators[] = {
142 [MAX14577_SAFEOUT] = {
143 .name = "SAFEOUT",
Beomho Seoe951cee2014-12-19 18:04:46 +0900144 .of_match = of_match_ptr("SAFEOUT"),
145 .regulators_node = of_match_ptr("regulators"),
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200146 .id = MAX14577_SAFEOUT,
147 .ops = &max14577_safeout_ops,
148 .type = REGULATOR_VOLTAGE,
149 .owner = THIS_MODULE,
150 .n_voltages = 1,
151 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE,
152 .enable_reg = MAX14577_REG_CONTROL2,
153 .enable_mask = CTRL2_SFOUTORD_MASK,
154 },
155 [MAX14577_CHARGER] = {
156 .name = "CHARGER",
Beomho Seoe951cee2014-12-19 18:04:46 +0900157 .of_match = of_match_ptr("CHARGER"),
158 .regulators_node = of_match_ptr("regulators"),
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200159 .id = MAX14577_CHARGER,
160 .ops = &max14577_charger_ops,
161 .type = REGULATOR_CURRENT,
162 .owner = THIS_MODULE,
163 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2,
164 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK,
165 },
166 [MAX77836_LDO1] = {
167 .name = "LDO1",
Beomho Seoe951cee2014-12-19 18:04:46 +0900168 .of_match = of_match_ptr("LDO1"),
169 .regulators_node = of_match_ptr("regulators"),
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200170 .id = MAX77836_LDO1,
171 .ops = &max77836_ldo_ops,
172 .type = REGULATOR_VOLTAGE,
173 .owner = THIS_MODULE,
174 .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
175 .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
176 .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
177 .enable_reg = MAX77836_LDO_REG_CNFG1_LDO1,
178 .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK,
179 .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO1,
180 .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK,
181 },
182 [MAX77836_LDO2] = {
183 .name = "LDO2",
Beomho Seoe951cee2014-12-19 18:04:46 +0900184 .of_match = of_match_ptr("LDO2"),
185 .regulators_node = of_match_ptr("regulators"),
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200186 .id = MAX77836_LDO2,
187 .ops = &max77836_ldo_ops,
188 .type = REGULATOR_VOLTAGE,
189 .owner = THIS_MODULE,
190 .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM,
191 .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN,
192 .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP,
193 .enable_reg = MAX77836_LDO_REG_CNFG1_LDO2,
194 .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK,
195 .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO2,
196 .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK,
197 },
198};
199
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100200#ifdef CONFIG_OF
201static struct of_regulator_match max14577_regulator_matches[] = {
202 { .name = "SAFEOUT", },
203 { .name = "CHARGER", },
204};
205
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200206static struct of_regulator_match max77836_regulator_matches[] = {
207 { .name = "SAFEOUT", },
208 { .name = "CHARGER", },
209 { .name = "LDO1", },
210 { .name = "LDO2", },
211};
212
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200213static inline struct regulator_init_data *match_init_data(int index,
214 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100215{
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200216 switch (dev_type) {
217 case MAXIM_DEVICE_TYPE_MAX77836:
218 return max77836_regulator_matches[index].init_data;
219
220 case MAXIM_DEVICE_TYPE_MAX14577:
221 default:
222 return max14577_regulator_matches[index].init_data;
223 }
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100224}
225
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200226static inline struct device_node *match_of_node(int index,
227 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100228{
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200229 switch (dev_type) {
230 case MAXIM_DEVICE_TYPE_MAX77836:
231 return max77836_regulator_matches[index].of_node;
232
233 case MAXIM_DEVICE_TYPE_MAX14577:
234 default:
235 return max14577_regulator_matches[index].of_node;
236 }
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100237}
238#else /* CONFIG_OF */
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200239static inline struct regulator_init_data *match_init_data(int index,
240 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100241{
242 return NULL;
243}
244
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200245static inline struct device_node *match_of_node(int index,
246 enum maxim_device_type dev_type)
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100247{
248 return NULL;
249}
250#endif /* CONFIG_OF */
251
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200252/**
253 * Registers for regulators of max77836 use different I2C slave addresses so
254 * different regmaps must be used for them.
255 *
256 * Returns proper regmap for accessing regulator passed by id.
257 */
258static struct regmap *max14577_get_regmap(struct max14577 *max14577,
259 int reg_id)
260{
261 switch (max14577->dev_type) {
262 case MAXIM_DEVICE_TYPE_MAX77836:
263 switch (reg_id) {
264 case MAX77836_SAFEOUT ... MAX77836_CHARGER:
265 return max14577->regmap;
266 default:
267 /* MAX77836_LDO1 ... MAX77836_LDO2 */
268 return max14577->regmap_pmic;
269 }
270
271 case MAXIM_DEVICE_TYPE_MAX14577:
272 default:
273 return max14577->regmap;
274 }
275}
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100276
277static int max14577_regulator_probe(struct platform_device *pdev)
278{
279 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
280 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
Beomho Seoe951cee2014-12-19 18:04:46 +0900281 int i, ret = 0;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100282 struct regulator_config config = {};
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200283 const struct regulator_desc *supported_regulators;
284 unsigned int supported_regulators_size;
285 enum maxim_device_type dev_type = max14577->dev_type;
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100286
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200287 switch (dev_type) {
288 case MAXIM_DEVICE_TYPE_MAX77836:
289 supported_regulators = max77836_supported_regulators;
290 supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
291 break;
292 case MAXIM_DEVICE_TYPE_MAX14577:
293 default:
294 supported_regulators = max14577_supported_regulators;
295 supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
296 }
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100297
Beomho Seoe951cee2014-12-19 18:04:46 +0900298 config.dev = max14577->dev;
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200299 config.driver_data = max14577;
300
301 for (i = 0; i < supported_regulators_size; i++) {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100302 struct regulator_dev *regulator;
303 /*
304 * Index of supported_regulators[] is also the id and must
305 * match index of pdata->regulators[].
306 */
307 if (pdata && pdata->regulators) {
308 config.init_data = pdata->regulators[i].initdata;
309 config.of_node = pdata->regulators[i].of_node;
310 } else {
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200311 config.init_data = match_init_data(i, dev_type);
312 config.of_node = match_of_node(i, dev_type);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100313 }
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200314 config.regmap = max14577_get_regmap(max14577,
315 supported_regulators[i].id);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100316
317 regulator = devm_regulator_register(&pdev->dev,
318 &supported_regulators[i], &config);
319 if (IS_ERR(regulator)) {
320 ret = PTR_ERR(regulator);
321 dev_err(&pdev->dev,
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200322 "Regulator init failed for %d/%s with error: %d\n",
323 i, supported_regulators[i].name, ret);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100324 return ret;
325 }
326 }
327
328 return ret;
329}
330
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200331static const struct platform_device_id max14577_regulator_id[] = {
332 { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
333 { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
334 { }
335};
336MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
337
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100338static struct platform_driver max14577_regulator_driver = {
339 .driver = {
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100340 .name = "max14577-regulator",
341 },
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200342 .probe = max14577_regulator_probe,
343 .id_table = max14577_regulator_id,
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100344};
345
346static int __init max14577_regulator_init(void)
347{
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200348 BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
349 BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
350
351 BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
352 (MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
353 (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
354 MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100355
356 return platform_driver_register(&max14577_regulator_driver);
357}
358subsys_initcall(max14577_regulator_init);
359
360static void __exit max14577_regulator_exit(void)
361{
362 platform_driver_unregister(&max14577_regulator_driver);
363}
364module_exit(max14577_regulator_exit);
365
366MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
Krzysztof Kozlowski8a82b402014-04-14 11:17:20 +0200367MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
Krzysztof Kozlowskib0902bb2013-12-06 12:32:13 +0100368MODULE_LICENSE("GPL");
Axel Lin9f45a3d2013-12-23 14:50:53 +0800369MODULE_ALIAS("platform:max14577-regulator");