1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25static 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, ®_data);
34 if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
35 return 0;
36 max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, ®_data);
37 if ((reg_data & STATUS3_CGMBC_MASK) == 0)
38 return 0;
39
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;
50 struct max14577 *max14577 = rdev_get_drvdata(rdev);
51 const struct maxim_charger_current *limits =
52 &maxim_charger_currents[max14577->dev_type];
53
54 if (rdev_get_id(rdev) != MAX14577_CHARGER)
55 return -EINVAL;
56
57 max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, ®_data);
58
59 if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
60 return limits->min;
61
62 reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
63 CHGCTRL4_MBCICHWRCH_SHIFT);
64 return limits->high_start + reg_data * limits->high_step;
65}
66
67static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
68 int min_uA, int max_uA)
69{
70 u8 reg_data;
71 int ret;
72 struct max14577 *max14577 = rdev_get_drvdata(rdev);
73 const struct maxim_charger_current *limits =
74 &maxim_charger_currents[max14577->dev_type];
75
76 if (rdev_get_id(rdev) != MAX14577_CHARGER)
77 return -EINVAL;
78
79 ret = maxim_charger_calc_reg_current(limits, min_uA, max_uA, ®_data);
80 if (ret)
81 return ret;
82
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
103#define MAX14577_SAFEOUT_REG { \
104 .name = "SAFEOUT", \
105 .of_match = of_match_ptr("SAFEOUT"), \
106 .regulators_node = of_match_ptr("regulators"), \
107 .id = MAX14577_SAFEOUT, \
108 .ops = &max14577_safeout_ops, \
109 .type = REGULATOR_VOLTAGE, \
110 .owner = THIS_MODULE, \
111 .n_voltages = 1, \
112 .min_uV = MAX14577_REGULATOR_SAFEOUT_VOLTAGE, \
113 .enable_reg = MAX14577_REG_CONTROL2, \
114 .enable_mask = CTRL2_SFOUTORD_MASK, \
115}
116#define MAX14577_CHARGER_REG { \
117 .name = "CHARGER", \
118 .of_match = of_match_ptr("CHARGER"), \
119 .regulators_node = of_match_ptr("regulators"), \
120 .id = MAX14577_CHARGER, \
121 .ops = &max14577_charger_ops, \
122 .type = REGULATOR_CURRENT, \
123 .owner = THIS_MODULE, \
124 .enable_reg = MAX14577_CHG_REG_CHG_CTRL2, \
125 .enable_mask = CHGCTRL2_MBCHOSTEN_MASK, \
126}
127
128static const struct regulator_desc max14577_supported_regulators[] = {
129 [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
130 [MAX14577_CHARGER] = MAX14577_CHARGER_REG,
131};
132
133static struct regulator_ops max77836_ldo_ops = {
134 .is_enabled = regulator_is_enabled_regmap,
135 .enable = regulator_enable_regmap,
136 .disable = regulator_disable_regmap,
137 .list_voltage = regulator_list_voltage_linear,
138 .map_voltage = regulator_map_voltage_linear,
139 .get_voltage_sel = regulator_get_voltage_sel_regmap,
140 .set_voltage_sel = regulator_set_voltage_sel_regmap,
141
142};
143
144#define MAX77836_LDO_REG(num) { \
145 .name = "LDO" # num, \
146 .of_match = of_match_ptr("LDO" # num), \
147 .regulators_node = of_match_ptr("regulators"), \
148 .id = MAX77836_LDO ## num, \
149 .ops = &max77836_ldo_ops, \
150 .type = REGULATOR_VOLTAGE, \
151 .owner = THIS_MODULE, \
152 .n_voltages = MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM, \
153 .min_uV = MAX77836_REGULATOR_LDO_VOLTAGE_MIN, \
154 .uV_step = MAX77836_REGULATOR_LDO_VOLTAGE_STEP, \
155 .enable_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \
156 .enable_mask = MAX77836_CNFG1_LDO_PWRMD_MASK, \
157 .vsel_reg = MAX77836_LDO_REG_CNFG1_LDO ## num, \
158 .vsel_mask = MAX77836_CNFG1_LDO_TV_MASK, \
159}
160
161static const struct regulator_desc max77836_supported_regulators[] = {
162 [MAX14577_SAFEOUT] = MAX14577_SAFEOUT_REG,
163 [MAX14577_CHARGER] = MAX14577_CHARGER_REG,
164 [MAX77836_LDO1] = MAX77836_LDO_REG(1),
165 [MAX77836_LDO2] = MAX77836_LDO_REG(2),
166};
167
168#ifdef CONFIG_OF
169static struct of_regulator_match max14577_regulator_matches[] = {
170 { .name = "SAFEOUT", },
171 { .name = "CHARGER", },
172};
173
174static struct of_regulator_match max77836_regulator_matches[] = {
175 { .name = "SAFEOUT", },
176 { .name = "CHARGER", },
177 { .name = "LDO1", },
178 { .name = "LDO2", },
179};
180
181static inline struct regulator_init_data *match_init_data(int index,
182 enum maxim_device_type dev_type)
183{
184 switch (dev_type) {
185 case MAXIM_DEVICE_TYPE_MAX77836:
186 return max77836_regulator_matches[index].init_data;
187
188 case MAXIM_DEVICE_TYPE_MAX14577:
189 default:
190 return max14577_regulator_matches[index].init_data;
191 }
192}
193
194static inline struct device_node *match_of_node(int index,
195 enum maxim_device_type dev_type)
196{
197 switch (dev_type) {
198 case MAXIM_DEVICE_TYPE_MAX77836:
199 return max77836_regulator_matches[index].of_node;
200
201 case MAXIM_DEVICE_TYPE_MAX14577:
202 default:
203 return max14577_regulator_matches[index].of_node;
204 }
205}
206#else
207static inline struct regulator_init_data *match_init_data(int index,
208 enum maxim_device_type dev_type)
209{
210 return NULL;
211}
212
213static inline struct device_node *match_of_node(int index,
214 enum maxim_device_type dev_type)
215{
216 return NULL;
217}
218#endif
219
220
221
222
223
224
225
226static struct regmap *max14577_get_regmap(struct max14577 *max14577,
227 int reg_id)
228{
229 switch (max14577->dev_type) {
230 case MAXIM_DEVICE_TYPE_MAX77836:
231 switch (reg_id) {
232 case MAX77836_SAFEOUT ... MAX77836_CHARGER:
233 return max14577->regmap;
234 default:
235
236 return max14577->regmap_pmic;
237 }
238
239 case MAXIM_DEVICE_TYPE_MAX14577:
240 default:
241 return max14577->regmap;
242 }
243}
244
245static int max14577_regulator_probe(struct platform_device *pdev)
246{
247 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
248 struct max14577_platform_data *pdata = dev_get_platdata(max14577->dev);
249 int i, ret = 0;
250 struct regulator_config config = {};
251 const struct regulator_desc *supported_regulators;
252 unsigned int supported_regulators_size;
253 enum maxim_device_type dev_type = max14577->dev_type;
254
255 switch (dev_type) {
256 case MAXIM_DEVICE_TYPE_MAX77836:
257 supported_regulators = max77836_supported_regulators;
258 supported_regulators_size = ARRAY_SIZE(max77836_supported_regulators);
259 break;
260 case MAXIM_DEVICE_TYPE_MAX14577:
261 default:
262 supported_regulators = max14577_supported_regulators;
263 supported_regulators_size = ARRAY_SIZE(max14577_supported_regulators);
264 }
265
266 config.dev = max14577->dev;
267 config.driver_data = max14577;
268
269 for (i = 0; i < supported_regulators_size; i++) {
270 struct regulator_dev *regulator;
271
272
273
274
275 if (pdata && pdata->regulators) {
276 config.init_data = pdata->regulators[i].initdata;
277 config.of_node = pdata->regulators[i].of_node;
278 } else {
279 config.init_data = match_init_data(i, dev_type);
280 config.of_node = match_of_node(i, dev_type);
281 }
282 config.regmap = max14577_get_regmap(max14577,
283 supported_regulators[i].id);
284
285 regulator = devm_regulator_register(&pdev->dev,
286 &supported_regulators[i], &config);
287 if (IS_ERR(regulator)) {
288 ret = PTR_ERR(regulator);
289 dev_err(&pdev->dev,
290 "Regulator init failed for %d/%s with error: %d\n",
291 i, supported_regulators[i].name, ret);
292 return ret;
293 }
294 }
295
296 return ret;
297}
298
299static const struct platform_device_id max14577_regulator_id[] = {
300 { "max14577-regulator", MAXIM_DEVICE_TYPE_MAX14577, },
301 { "max77836-regulator", MAXIM_DEVICE_TYPE_MAX77836, },
302 { }
303};
304MODULE_DEVICE_TABLE(platform, max14577_regulator_id);
305
306static struct platform_driver max14577_regulator_driver = {
307 .driver = {
308 .name = "max14577-regulator",
309 },
310 .probe = max14577_regulator_probe,
311 .id_table = max14577_regulator_id,
312};
313
314static int __init max14577_regulator_init(void)
315{
316 BUILD_BUG_ON(ARRAY_SIZE(max14577_supported_regulators) != MAX14577_REGULATOR_NUM);
317 BUILD_BUG_ON(ARRAY_SIZE(max77836_supported_regulators) != MAX77836_REGULATOR_NUM);
318
319 BUILD_BUG_ON(MAX77836_REGULATOR_LDO_VOLTAGE_MIN +
320 (MAX77836_REGULATOR_LDO_VOLTAGE_STEP *
321 (MAX77836_REGULATOR_LDO_VOLTAGE_STEPS_NUM - 1)) !=
322 MAX77836_REGULATOR_LDO_VOLTAGE_MAX);
323
324 return platform_driver_register(&max14577_regulator_driver);
325}
326subsys_initcall(max14577_regulator_init);
327
328static void __exit max14577_regulator_exit(void)
329{
330 platform_driver_unregister(&max14577_regulator_driver);
331}
332module_exit(max14577_regulator_exit);
333
334MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
335MODULE_DESCRIPTION("Maxim 14577/77836 regulator driver");
336MODULE_LICENSE("GPL");
337MODULE_ALIAS("platform:max14577-regulator");
338