Commit | Line | Data |
---|---|---|
f1e64f90 GL |
1 | /* |
2 | * AS3711 PMIC regulator driver, using DCDC Step Down and LDO supplies | |
3 | * | |
4 | * Copyright (C) 2012 Renesas Electronics Corporation | |
5 | * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the version 2 of the GNU General Public License as | |
9 | * published by the Free Software Foundation | |
10 | */ | |
11 | ||
12 | #include <linux/err.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/mfd/as3711.h> | |
15 | #include <linux/module.h> | |
416d6759 | 16 | #include <linux/of.h> |
f1e64f90 GL |
17 | #include <linux/platform_device.h> |
18 | #include <linux/regmap.h> | |
19 | #include <linux/regulator/driver.h> | |
416d6759 | 20 | #include <linux/regulator/of_regulator.h> |
f1e64f90 GL |
21 | #include <linux/slab.h> |
22 | ||
23 | struct as3711_regulator_info { | |
24 | struct regulator_desc desc; | |
25 | unsigned int max_uV; | |
26 | }; | |
27 | ||
28 | struct as3711_regulator { | |
29 | struct as3711_regulator_info *reg_info; | |
30 | struct regulator_dev *rdev; | |
31 | }; | |
32 | ||
33 | static int as3711_list_voltage_sd(struct regulator_dev *rdev, | |
34 | unsigned int selector) | |
35 | { | |
36 | if (selector >= rdev->desc->n_voltages) | |
37 | return -EINVAL; | |
38 | ||
39 | if (!selector) | |
40 | return 0; | |
41 | if (selector < 0x41) | |
42 | return 600000 + selector * 12500; | |
43 | if (selector < 0x71) | |
44 | return 1400000 + (selector - 0x40) * 25000; | |
45 | return 2600000 + (selector - 0x70) * 50000; | |
46 | } | |
47 | ||
48 | static int as3711_list_voltage_aldo(struct regulator_dev *rdev, | |
49 | unsigned int selector) | |
50 | { | |
51 | if (selector >= rdev->desc->n_voltages) | |
52 | return -EINVAL; | |
53 | ||
54 | if (selector < 0x10) | |
55 | return 1200000 + selector * 50000; | |
56 | return 1800000 + (selector - 0x10) * 100000; | |
57 | } | |
58 | ||
59 | static int as3711_list_voltage_dldo(struct regulator_dev *rdev, | |
60 | unsigned int selector) | |
61 | { | |
62 | if (selector >= rdev->desc->n_voltages || | |
63 | (selector > 0x10 && selector < 0x20)) | |
64 | return -EINVAL; | |
65 | ||
66 | if (selector < 0x11) | |
67 | return 900000 + selector * 50000; | |
68 | return 1750000 + (selector - 0x20) * 50000; | |
69 | } | |
70 | ||
71 | static int as3711_bound_check(struct regulator_dev *rdev, | |
72 | int *min_uV, int *max_uV) | |
73 | { | |
16ed9f07 AL |
74 | struct as3711_regulator *reg = rdev_get_drvdata(rdev); |
75 | struct as3711_regulator_info *info = reg->reg_info; | |
f1e64f90 GL |
76 | |
77 | dev_dbg(&rdev->dev, "%s(), %d, %d, %d\n", __func__, | |
78 | *min_uV, rdev->desc->min_uV, info->max_uV); | |
79 | ||
80 | if (*max_uV < *min_uV || | |
16ed9f07 | 81 | *min_uV > info->max_uV || rdev->desc->min_uV > *max_uV) |
f1e64f90 GL |
82 | return -EINVAL; |
83 | ||
84 | if (rdev->desc->n_voltages == 1) | |
85 | return 0; | |
86 | ||
87 | if (*max_uV > info->max_uV) | |
88 | *max_uV = info->max_uV; | |
89 | ||
90 | if (*min_uV < rdev->desc->min_uV) | |
91 | *min_uV = rdev->desc->min_uV; | |
92 | ||
93 | return *min_uV; | |
94 | } | |
95 | ||
96 | static int as3711_sel_check(int min, int max, int bottom, int step) | |
97 | { | |
7a4beda0 | 98 | int sel, voltage; |
f1e64f90 GL |
99 | |
100 | /* Round up min, when dividing: keeps us within the range */ | |
7a4beda0 AL |
101 | sel = DIV_ROUND_UP(min - bottom, step); |
102 | voltage = sel * step + bottom; | |
f1e64f90 | 103 | pr_debug("%s(): select %d..%d in %d+N*%d: %d\n", __func__, |
7a4beda0 AL |
104 | min, max, bottom, step, sel); |
105 | if (voltage > max) | |
106 | return -EINVAL; | |
107 | ||
108 | return sel; | |
f1e64f90 GL |
109 | } |
110 | ||
111 | static int as3711_map_voltage_sd(struct regulator_dev *rdev, | |
112 | int min_uV, int max_uV) | |
113 | { | |
114 | int ret; | |
115 | ||
116 | ret = as3711_bound_check(rdev, &min_uV, &max_uV); | |
117 | if (ret <= 0) | |
118 | return ret; | |
119 | ||
120 | if (min_uV <= 1400000) | |
121 | return as3711_sel_check(min_uV, max_uV, 600000, 12500); | |
122 | ||
123 | if (min_uV <= 2600000) | |
124 | return as3711_sel_check(min_uV, max_uV, 1400000, 25000) + 0x40; | |
125 | ||
126 | return as3711_sel_check(min_uV, max_uV, 2600000, 50000) + 0x70; | |
127 | } | |
128 | ||
129 | /* | |
130 | * The regulator API supports 4 modes of operataion: FAST, NORMAL, IDLE and | |
131 | * STANDBY. We map them in the following way to AS3711 SD1-4 DCDC modes: | |
132 | * FAST: sdX_fast=1 | |
133 | * NORMAL: low_noise=1 | |
134 | * IDLE: low_noise=0 | |
135 | */ | |
136 | ||
137 | static int as3711_set_mode_sd(struct regulator_dev *rdev, unsigned int mode) | |
138 | { | |
139 | unsigned int fast_bit = rdev->desc->enable_mask, | |
140 | low_noise_bit = fast_bit << 4; | |
141 | u8 val; | |
142 | ||
143 | switch (mode) { | |
144 | case REGULATOR_MODE_FAST: | |
145 | val = fast_bit | low_noise_bit; | |
146 | break; | |
147 | case REGULATOR_MODE_NORMAL: | |
148 | val = low_noise_bit; | |
149 | break; | |
150 | case REGULATOR_MODE_IDLE: | |
151 | val = 0; | |
152 | break; | |
153 | default: | |
154 | return -EINVAL; | |
155 | } | |
156 | ||
157 | return regmap_update_bits(rdev->regmap, AS3711_SD_CONTROL_1, | |
158 | low_noise_bit | fast_bit, val); | |
159 | } | |
160 | ||
161 | static unsigned int as3711_get_mode_sd(struct regulator_dev *rdev) | |
162 | { | |
163 | unsigned int fast_bit = rdev->desc->enable_mask, | |
164 | low_noise_bit = fast_bit << 4, mask = fast_bit | low_noise_bit; | |
165 | unsigned int val; | |
166 | int ret = regmap_read(rdev->regmap, AS3711_SD_CONTROL_1, &val); | |
167 | ||
168 | if (ret < 0) | |
169 | return ret; | |
170 | ||
171 | if ((val & mask) == mask) | |
172 | return REGULATOR_MODE_FAST; | |
173 | ||
174 | if ((val & mask) == low_noise_bit) | |
175 | return REGULATOR_MODE_NORMAL; | |
176 | ||
177 | if (!(val & mask)) | |
178 | return REGULATOR_MODE_IDLE; | |
179 | ||
180 | return -EINVAL; | |
181 | } | |
182 | ||
183 | static int as3711_map_voltage_aldo(struct regulator_dev *rdev, | |
184 | int min_uV, int max_uV) | |
185 | { | |
186 | int ret; | |
187 | ||
188 | ret = as3711_bound_check(rdev, &min_uV, &max_uV); | |
189 | if (ret <= 0) | |
190 | return ret; | |
191 | ||
192 | if (min_uV <= 1800000) | |
193 | return as3711_sel_check(min_uV, max_uV, 1200000, 50000); | |
194 | ||
195 | return as3711_sel_check(min_uV, max_uV, 1800000, 100000) + 0x10; | |
196 | } | |
197 | ||
198 | static int as3711_map_voltage_dldo(struct regulator_dev *rdev, | |
199 | int min_uV, int max_uV) | |
200 | { | |
201 | int ret; | |
202 | ||
203 | ret = as3711_bound_check(rdev, &min_uV, &max_uV); | |
204 | if (ret <= 0) | |
205 | return ret; | |
206 | ||
207 | if (min_uV <= 1700000) | |
208 | return as3711_sel_check(min_uV, max_uV, 900000, 50000); | |
209 | ||
210 | return as3711_sel_check(min_uV, max_uV, 1750000, 50000) + 0x20; | |
211 | } | |
212 | ||
213 | static struct regulator_ops as3711_sd_ops = { | |
214 | .is_enabled = regulator_is_enabled_regmap, | |
215 | .enable = regulator_enable_regmap, | |
216 | .disable = regulator_disable_regmap, | |
217 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
218 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
219 | .list_voltage = as3711_list_voltage_sd, | |
220 | .map_voltage = as3711_map_voltage_sd, | |
221 | .get_mode = as3711_get_mode_sd, | |
222 | .set_mode = as3711_set_mode_sd, | |
223 | }; | |
224 | ||
225 | static struct regulator_ops as3711_aldo_ops = { | |
226 | .is_enabled = regulator_is_enabled_regmap, | |
227 | .enable = regulator_enable_regmap, | |
228 | .disable = regulator_disable_regmap, | |
229 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
230 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
231 | .list_voltage = as3711_list_voltage_aldo, | |
232 | .map_voltage = as3711_map_voltage_aldo, | |
233 | }; | |
234 | ||
235 | static struct regulator_ops as3711_dldo_ops = { | |
236 | .is_enabled = regulator_is_enabled_regmap, | |
237 | .enable = regulator_enable_regmap, | |
238 | .disable = regulator_disable_regmap, | |
239 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
240 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
241 | .list_voltage = as3711_list_voltage_dldo, | |
242 | .map_voltage = as3711_map_voltage_dldo, | |
243 | }; | |
244 | ||
245 | #define AS3711_REG(_id, _en_reg, _en_bit, _vmask, _vshift, _min_uV, _max_uV, _sfx) \ | |
246 | [AS3711_REGULATOR_ ## _id] = { \ | |
247 | .desc = { \ | |
248 | .name = "as3711-regulator-" # _id, \ | |
249 | .id = AS3711_REGULATOR_ ## _id, \ | |
250 | .n_voltages = (_vmask + 1), \ | |
251 | .ops = &as3711_ ## _sfx ## _ops, \ | |
252 | .type = REGULATOR_VOLTAGE, \ | |
253 | .owner = THIS_MODULE, \ | |
254 | .vsel_reg = AS3711_ ## _id ## _VOLTAGE, \ | |
255 | .vsel_mask = _vmask << _vshift, \ | |
256 | .enable_reg = AS3711_ ## _en_reg, \ | |
257 | .enable_mask = BIT(_en_bit), \ | |
258 | .min_uV = _min_uV, \ | |
259 | }, \ | |
260 | .max_uV = _max_uV, \ | |
261 | } | |
262 | ||
263 | static struct as3711_regulator_info as3711_reg_info[] = { | |
264 | AS3711_REG(SD_1, SD_CONTROL, 0, 0x7f, 0, 612500, 3350000, sd), | |
265 | AS3711_REG(SD_2, SD_CONTROL, 1, 0x7f, 0, 612500, 3350000, sd), | |
266 | AS3711_REG(SD_3, SD_CONTROL, 2, 0x7f, 0, 612500, 3350000, sd), | |
267 | AS3711_REG(SD_4, SD_CONTROL, 3, 0x7f, 0, 612500, 3350000, sd), | |
268 | AS3711_REG(LDO_1, LDO_1_VOLTAGE, 7, 0x1f, 0, 1200000, 3300000, aldo), | |
269 | AS3711_REG(LDO_2, LDO_2_VOLTAGE, 7, 0x1f, 0, 1200000, 3300000, aldo), | |
270 | AS3711_REG(LDO_3, LDO_3_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo), | |
271 | AS3711_REG(LDO_4, LDO_4_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo), | |
272 | AS3711_REG(LDO_5, LDO_5_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo), | |
273 | AS3711_REG(LDO_6, LDO_6_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo), | |
274 | AS3711_REG(LDO_7, LDO_7_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo), | |
275 | AS3711_REG(LDO_8, LDO_8_VOLTAGE, 7, 0x3f, 0, 900000, 3300000, dldo), | |
276 | /* StepUp output voltage depends on supplying regulator */ | |
277 | }; | |
278 | ||
279 | #define AS3711_REGULATOR_NUM ARRAY_SIZE(as3711_reg_info) | |
280 | ||
416d6759 GL |
281 | static const char *as3711_regulator_of_names[AS3711_REGULATOR_NUM] = { |
282 | [AS3711_REGULATOR_SD_1] = "sd1", | |
283 | [AS3711_REGULATOR_SD_2] = "sd2", | |
284 | [AS3711_REGULATOR_SD_3] = "sd3", | |
285 | [AS3711_REGULATOR_SD_4] = "sd4", | |
286 | [AS3711_REGULATOR_LDO_1] = "ldo1", | |
287 | [AS3711_REGULATOR_LDO_2] = "ldo2", | |
288 | [AS3711_REGULATOR_LDO_3] = "ldo3", | |
289 | [AS3711_REGULATOR_LDO_4] = "ldo4", | |
290 | [AS3711_REGULATOR_LDO_5] = "ldo5", | |
291 | [AS3711_REGULATOR_LDO_6] = "ldo6", | |
292 | [AS3711_REGULATOR_LDO_7] = "ldo7", | |
293 | [AS3711_REGULATOR_LDO_8] = "ldo8", | |
294 | }; | |
295 | ||
296 | static int as3711_regulator_parse_dt(struct device *dev, | |
297 | struct device_node **of_node, const int count) | |
298 | { | |
299 | struct as3711_regulator_pdata *pdata = dev_get_platdata(dev); | |
300 | struct device_node *regulators = | |
301 | of_find_node_by_name(dev->parent->of_node, "regulators"); | |
302 | struct of_regulator_match *matches, *match; | |
303 | int ret, i; | |
304 | ||
305 | if (!regulators) { | |
306 | dev_err(dev, "regulator node not found\n"); | |
307 | return -ENODEV; | |
308 | } | |
309 | ||
310 | matches = devm_kzalloc(dev, sizeof(*matches) * count, GFP_KERNEL); | |
311 | if (!matches) | |
312 | return -ENOMEM; | |
313 | ||
314 | for (i = 0, match = matches; i < count; i++, match++) { | |
315 | match->name = as3711_regulator_of_names[i]; | |
316 | match->driver_data = as3711_reg_info + i; | |
317 | } | |
318 | ||
319 | ret = of_regulator_match(dev->parent, regulators, matches, count); | |
320 | of_node_put(regulators); | |
321 | if (ret < 0) { | |
322 | dev_err(dev, "Error parsing regulator init data: %d\n", ret); | |
323 | return ret; | |
324 | } | |
325 | ||
326 | for (i = 0, match = matches; i < count; i++, match++) | |
327 | if (match->of_node) { | |
328 | pdata->init_data[i] = match->init_data; | |
329 | of_node[i] = match->of_node; | |
330 | } | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
f1e64f90 GL |
335 | static int as3711_regulator_probe(struct platform_device *pdev) |
336 | { | |
337 | struct as3711_regulator_pdata *pdata = dev_get_platdata(&pdev->dev); | |
338 | struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent); | |
339 | struct regulator_init_data *reg_data; | |
340 | struct regulator_config config = {.dev = &pdev->dev,}; | |
341 | struct as3711_regulator *reg = NULL; | |
342 | struct as3711_regulator *regs; | |
416d6759 | 343 | struct device_node *of_node[AS3711_REGULATOR_NUM] = {}; |
f1e64f90 GL |
344 | struct regulator_dev *rdev; |
345 | struct as3711_regulator_info *ri; | |
346 | int ret; | |
347 | int id; | |
348 | ||
416d6759 GL |
349 | if (!pdata) { |
350 | dev_err(&pdev->dev, "No platform data...\n"); | |
351 | return -ENODEV; | |
352 | } | |
353 | ||
354 | if (pdev->dev.parent->of_node) { | |
355 | ret = as3711_regulator_parse_dt(&pdev->dev, of_node, AS3711_REGULATOR_NUM); | |
356 | if (ret < 0) { | |
357 | dev_err(&pdev->dev, "DT parsing failed: %d\n", ret); | |
358 | return ret; | |
359 | } | |
360 | } | |
f1e64f90 GL |
361 | |
362 | regs = devm_kzalloc(&pdev->dev, AS3711_REGULATOR_NUM * | |
363 | sizeof(struct as3711_regulator), GFP_KERNEL); | |
364 | if (!regs) { | |
365 | dev_err(&pdev->dev, "Memory allocation failed exiting..\n"); | |
366 | return -ENOMEM; | |
367 | } | |
368 | ||
369 | for (id = 0, ri = as3711_reg_info; id < AS3711_REGULATOR_NUM; ++id, ri++) { | |
416d6759 | 370 | reg_data = pdata->init_data[id]; |
f1e64f90 GL |
371 | |
372 | /* No need to register if there is no regulator data */ | |
09ca50d2 | 373 | if (!reg_data) |
f1e64f90 GL |
374 | continue; |
375 | ||
376 | reg = ®s[id]; | |
377 | reg->reg_info = ri; | |
378 | ||
379 | config.init_data = reg_data; | |
380 | config.driver_data = reg; | |
381 | config.regmap = as3711->regmap; | |
416d6759 | 382 | config.of_node = of_node[id]; |
f1e64f90 GL |
383 | |
384 | rdev = regulator_register(&ri->desc, &config); | |
385 | if (IS_ERR(rdev)) { | |
386 | dev_err(&pdev->dev, "Failed to register regulator %s\n", | |
387 | ri->desc.name); | |
388 | ret = PTR_ERR(rdev); | |
389 | goto eregreg; | |
390 | } | |
391 | reg->rdev = rdev; | |
392 | } | |
393 | platform_set_drvdata(pdev, regs); | |
394 | return 0; | |
395 | ||
396 | eregreg: | |
397 | while (--id >= 0) | |
398 | regulator_unregister(regs[id].rdev); | |
399 | ||
400 | return ret; | |
401 | } | |
402 | ||
403 | static int as3711_regulator_remove(struct platform_device *pdev) | |
404 | { | |
405 | struct as3711_regulator *regs = platform_get_drvdata(pdev); | |
406 | int id; | |
407 | ||
408 | for (id = 0; id < AS3711_REGULATOR_NUM; ++id) | |
409 | regulator_unregister(regs[id].rdev); | |
410 | return 0; | |
411 | } | |
412 | ||
413 | static struct platform_driver as3711_regulator_driver = { | |
414 | .driver = { | |
415 | .name = "as3711-regulator", | |
416 | .owner = THIS_MODULE, | |
417 | }, | |
418 | .probe = as3711_regulator_probe, | |
419 | .remove = as3711_regulator_remove, | |
420 | }; | |
421 | ||
422 | static int __init as3711_regulator_init(void) | |
423 | { | |
424 | return platform_driver_register(&as3711_regulator_driver); | |
425 | } | |
426 | subsys_initcall(as3711_regulator_init); | |
427 | ||
428 | static void __exit as3711_regulator_exit(void) | |
429 | { | |
430 | platform_driver_unregister(&as3711_regulator_driver); | |
431 | } | |
432 | module_exit(as3711_regulator_exit); | |
433 | ||
434 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); | |
435 | MODULE_DESCRIPTION("AS3711 regulator driver"); | |
436 | MODULE_ALIAS("platform:as3711-regulator"); | |
437 | MODULE_LICENSE("GPL v2"); |