Commit | Line | Data |
---|---|---|
d1a82001 LJ |
1 | /* |
2 | * Copyright (C) ST-Ericsson SA 2010 | |
3 | * | |
4 | * License Terms: GNU General Public License v2 | |
5 | * | |
6 | * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com> | |
7 | * | |
8 | * This file is based on drivers/regulator/ab8500.c | |
9 | * | |
10 | * AB8500 external regulators | |
11 | * | |
12 | * ab8500-ext supports the following regulators: | |
13 | * - VextSupply3 | |
14 | */ | |
15 | #include <linux/init.h> | |
16 | #include <linux/kernel.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/module.h> | |
30aa4b26 | 19 | #include <linux/of.h> |
d1a82001 LJ |
20 | #include <linux/platform_device.h> |
21 | #include <linux/regulator/driver.h> | |
22 | #include <linux/regulator/machine.h> | |
30aa4b26 | 23 | #include <linux/regulator/of_regulator.h> |
d1a82001 LJ |
24 | #include <linux/mfd/abx500.h> |
25 | #include <linux/mfd/abx500/ab8500.h> | |
26 | #include <linux/regulator/ab8500.h> | |
27 | ||
28 | /** | |
29 | * struct ab8500_ext_regulator_info - ab8500 regulator information | |
30 | * @dev: device pointer | |
31 | * @desc: regulator description | |
32 | * @rdev: regulator device | |
18bc2b39 | 33 | * @cfg: regulator configuration (extension of regulator FW configuration) |
d1a82001 LJ |
34 | * @update_bank: bank to control on/off |
35 | * @update_reg: register to control on/off | |
36 | * @update_mask: mask to enable/disable and set mode of regulator | |
37 | * @update_val: bits holding the regulator current mode | |
18bc2b39 | 38 | * @update_val_hp: bits to set EN pin active (LPn pin deactive) |
d1a82001 | 39 | * normally this means high power mode |
18bc2b39 BJ |
40 | * @update_val_lp: bits to set EN pin active and LPn pin active |
41 | * normally this means low power mode | |
42 | * @update_val_hw: bits to set regulator pins in HW control | |
43 | * SysClkReq pins and logic will choose mode | |
d1a82001 LJ |
44 | */ |
45 | struct ab8500_ext_regulator_info { | |
46 | struct device *dev; | |
47 | struct regulator_desc desc; | |
48 | struct regulator_dev *rdev; | |
18bc2b39 | 49 | struct ab8500_ext_regulator_cfg *cfg; |
d1a82001 LJ |
50 | u8 update_bank; |
51 | u8 update_reg; | |
52 | u8 update_mask; | |
53 | u8 update_val; | |
18bc2b39 BJ |
54 | u8 update_val_hp; |
55 | u8 update_val_lp; | |
56 | u8 update_val_hw; | |
d1a82001 LJ |
57 | }; |
58 | ||
7384744d | 59 | static int ab8500_ext_regulator_enable(struct regulator_dev *rdev) |
d1a82001 LJ |
60 | { |
61 | int ret; | |
7384744d AL |
62 | struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev); |
63 | u8 regval; | |
d1a82001 | 64 | |
7384744d AL |
65 | if (info == NULL) { |
66 | dev_err(rdev_get_dev(rdev), "regulator info null pointer\n"); | |
67 | return -EINVAL; | |
68 | } | |
18bc2b39 BJ |
69 | |
70 | /* | |
71 | * To satisfy both HW high power request and SW request, the regulator | |
72 | * must be on in high power. | |
73 | */ | |
74 | if (info->cfg && info->cfg->hwreq) | |
7384744d AL |
75 | regval = info->update_val_hp; |
76 | else | |
77 | regval = info->update_val; | |
d1a82001 LJ |
78 | |
79 | ret = abx500_mask_and_set_register_interruptible(info->dev, | |
80 | info->update_bank, info->update_reg, | |
7384744d | 81 | info->update_mask, regval); |
37daa8ae | 82 | if (ret < 0) { |
d1a82001 LJ |
83 | dev_err(rdev_get_dev(info->rdev), |
84 | "couldn't set enable bits for regulator\n"); | |
37daa8ae AL |
85 | return ret; |
86 | } | |
d1a82001 | 87 | |
7384744d AL |
88 | dev_dbg(rdev_get_dev(rdev), |
89 | "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", | |
90 | info->desc.name, info->update_bank, info->update_reg, | |
91 | info->update_mask, regval); | |
92 | ||
93 | return 0; | |
d1a82001 LJ |
94 | } |
95 | ||
7384744d | 96 | static int ab8500_ext_regulator_disable(struct regulator_dev *rdev) |
d1a82001 LJ |
97 | { |
98 | int ret; | |
99 | struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev); | |
18bc2b39 | 100 | u8 regval; |
d1a82001 LJ |
101 | |
102 | if (info == NULL) { | |
103 | dev_err(rdev_get_dev(rdev), "regulator info null pointer\n"); | |
104 | return -EINVAL; | |
105 | } | |
106 | ||
18bc2b39 BJ |
107 | /* |
108 | * Set the regulator in HW request mode if configured | |
109 | */ | |
110 | if (info->cfg && info->cfg->hwreq) | |
7384744d AL |
111 | regval = info->update_val_hw; |
112 | else | |
113 | regval = 0; | |
18bc2b39 | 114 | |
d1a82001 LJ |
115 | ret = abx500_mask_and_set_register_interruptible(info->dev, |
116 | info->update_bank, info->update_reg, | |
7384744d | 117 | info->update_mask, regval); |
37daa8ae | 118 | if (ret < 0) { |
d1a82001 LJ |
119 | dev_err(rdev_get_dev(info->rdev), |
120 | "couldn't set disable bits for regulator\n"); | |
37daa8ae AL |
121 | return ret; |
122 | } | |
d1a82001 | 123 | |
d1a82001 LJ |
124 | dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):" |
125 | " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", | |
126 | info->desc.name, info->update_bank, info->update_reg, | |
18bc2b39 | 127 | info->update_mask, regval); |
d1a82001 | 128 | |
7384744d | 129 | return 0; |
d1a82001 LJ |
130 | } |
131 | ||
132 | static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev) | |
133 | { | |
134 | int ret; | |
135 | struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev); | |
136 | u8 regval; | |
137 | ||
138 | if (info == NULL) { | |
139 | dev_err(rdev_get_dev(rdev), "regulator info null pointer\n"); | |
140 | return -EINVAL; | |
141 | } | |
142 | ||
143 | ret = abx500_get_register_interruptible(info->dev, | |
144 | info->update_bank, info->update_reg, ®val); | |
145 | if (ret < 0) { | |
146 | dev_err(rdev_get_dev(rdev), | |
147 | "couldn't read 0x%x register\n", info->update_reg); | |
148 | return ret; | |
149 | } | |
150 | ||
151 | dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):" | |
152 | " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", | |
153 | info->desc.name, info->update_bank, info->update_reg, | |
154 | info->update_mask, regval); | |
155 | ||
18bc2b39 BJ |
156 | if (((regval & info->update_mask) == info->update_val_lp) || |
157 | ((regval & info->update_mask) == info->update_val_hp)) | |
9ab51a0e | 158 | return 1; |
d1a82001 | 159 | else |
9ab51a0e | 160 | return 0; |
d1a82001 LJ |
161 | } |
162 | ||
163 | static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev, | |
164 | unsigned int mode) | |
165 | { | |
166 | int ret = 0; | |
167 | struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev); | |
66511fa4 | 168 | u8 regval; |
d1a82001 LJ |
169 | |
170 | if (info == NULL) { | |
171 | dev_err(rdev_get_dev(rdev), "regulator info null pointer\n"); | |
172 | return -EINVAL; | |
173 | } | |
174 | ||
175 | switch (mode) { | |
176 | case REGULATOR_MODE_NORMAL: | |
66511fa4 | 177 | regval = info->update_val_hp; |
d1a82001 LJ |
178 | break; |
179 | case REGULATOR_MODE_IDLE: | |
66511fa4 | 180 | regval = info->update_val_lp; |
d1a82001 LJ |
181 | break; |
182 | ||
183 | default: | |
184 | return -EINVAL; | |
185 | } | |
186 | ||
66511fa4 AL |
187 | /* If regulator is enabled and info->cfg->hwreq is set, the regulator |
188 | must be on in high power, so we don't need to write the register with | |
189 | the same value. | |
190 | */ | |
191 | if (ab8500_ext_regulator_is_enabled(rdev) && | |
192 | !(info->cfg && info->cfg->hwreq)) { | |
193 | ret = abx500_mask_and_set_register_interruptible(info->dev, | |
194 | info->update_bank, info->update_reg, | |
195 | info->update_mask, regval); | |
196 | if (ret < 0) { | |
d1a82001 LJ |
197 | dev_err(rdev_get_dev(rdev), |
198 | "Could not set regulator mode.\n"); | |
66511fa4 AL |
199 | return ret; |
200 | } | |
d1a82001 LJ |
201 | |
202 | dev_dbg(rdev_get_dev(rdev), | |
203 | "%s-set_mode (bank, reg, mask, value): " | |
204 | "0x%x, 0x%x, 0x%x, 0x%x\n", | |
205 | info->desc.name, info->update_bank, info->update_reg, | |
206 | info->update_mask, regval); | |
207 | } | |
208 | ||
66511fa4 AL |
209 | info->update_val = regval; |
210 | ||
211 | return 0; | |
d1a82001 LJ |
212 | } |
213 | ||
214 | static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev) | |
215 | { | |
216 | struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev); | |
217 | int ret; | |
218 | ||
219 | if (info == NULL) { | |
220 | dev_err(rdev_get_dev(rdev), "regulator info null pointer\n"); | |
221 | return -EINVAL; | |
222 | } | |
223 | ||
224 | if (info->update_val == info->update_val_hp) | |
225 | ret = REGULATOR_MODE_NORMAL; | |
226 | else if (info->update_val == info->update_val_lp) | |
227 | ret = REGULATOR_MODE_IDLE; | |
228 | else | |
229 | ret = -EINVAL; | |
230 | ||
231 | return ret; | |
232 | } | |
233 | ||
33fb8802 LJ |
234 | static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV, |
235 | int max_uV, unsigned *selector) | |
236 | { | |
237 | struct regulation_constraints *regu_constraints = rdev->constraints; | |
238 | ||
239 | if (!regu_constraints) { | |
240 | dev_err(rdev_get_dev(rdev), "No regulator constraints\n"); | |
241 | return -EINVAL; | |
242 | } | |
243 | ||
244 | if (regu_constraints->min_uV == min_uV && | |
245 | regu_constraints->max_uV == max_uV) | |
246 | return 0; | |
247 | ||
248 | dev_err(rdev_get_dev(rdev), | |
249 | "Requested min %duV max %duV != constrained min %duV max %duV\n", | |
250 | min_uV, max_uV, | |
251 | regu_constraints->min_uV, regu_constraints->max_uV); | |
252 | ||
253 | return -EINVAL; | |
254 | } | |
255 | ||
d1a82001 LJ |
256 | static int ab8500_ext_list_voltage(struct regulator_dev *rdev, |
257 | unsigned selector) | |
258 | { | |
259 | struct regulation_constraints *regu_constraints = rdev->constraints; | |
260 | ||
261 | if (regu_constraints == NULL) { | |
262 | dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n"); | |
263 | return -EINVAL; | |
264 | } | |
265 | /* return the uV for the fixed regulators */ | |
266 | if (regu_constraints->min_uV && regu_constraints->max_uV) { | |
267 | if (regu_constraints->min_uV == regu_constraints->max_uV) | |
268 | return regu_constraints->min_uV; | |
269 | } | |
270 | return -EINVAL; | |
271 | } | |
272 | ||
273 | static struct regulator_ops ab8500_ext_regulator_ops = { | |
274 | .enable = ab8500_ext_regulator_enable, | |
275 | .disable = ab8500_ext_regulator_disable, | |
276 | .is_enabled = ab8500_ext_regulator_is_enabled, | |
277 | .set_mode = ab8500_ext_regulator_set_mode, | |
278 | .get_mode = ab8500_ext_regulator_get_mode, | |
33fb8802 | 279 | .set_voltage = ab8500_ext_set_voltage, |
d1a82001 LJ |
280 | .list_voltage = ab8500_ext_list_voltage, |
281 | }; | |
282 | ||
d1a82001 LJ |
283 | static struct ab8500_ext_regulator_info |
284 | ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = { | |
285 | [AB8500_EXT_SUPPLY1] = { | |
286 | .desc = { | |
287 | .name = "VEXTSUPPLY1", | |
288 | .ops = &ab8500_ext_regulator_ops, | |
289 | .type = REGULATOR_VOLTAGE, | |
290 | .id = AB8500_EXT_SUPPLY1, | |
291 | .owner = THIS_MODULE, | |
292 | .n_voltages = 1, | |
293 | }, | |
294 | .update_bank = 0x04, | |
295 | .update_reg = 0x08, | |
296 | .update_mask = 0x03, | |
297 | .update_val = 0x01, | |
298 | .update_val_hp = 0x01, | |
299 | .update_val_lp = 0x03, | |
300 | .update_val_hw = 0x02, | |
301 | }, | |
302 | [AB8500_EXT_SUPPLY2] = { | |
303 | .desc = { | |
304 | .name = "VEXTSUPPLY2", | |
305 | .ops = &ab8500_ext_regulator_ops, | |
306 | .type = REGULATOR_VOLTAGE, | |
307 | .id = AB8500_EXT_SUPPLY2, | |
308 | .owner = THIS_MODULE, | |
309 | .n_voltages = 1, | |
310 | }, | |
311 | .update_bank = 0x04, | |
312 | .update_reg = 0x08, | |
313 | .update_mask = 0x0c, | |
314 | .update_val = 0x04, | |
315 | .update_val_hp = 0x04, | |
316 | .update_val_lp = 0x0c, | |
317 | .update_val_hw = 0x08, | |
318 | }, | |
319 | [AB8500_EXT_SUPPLY3] = { | |
320 | .desc = { | |
321 | .name = "VEXTSUPPLY3", | |
322 | .ops = &ab8500_ext_regulator_ops, | |
323 | .type = REGULATOR_VOLTAGE, | |
324 | .id = AB8500_EXT_SUPPLY3, | |
325 | .owner = THIS_MODULE, | |
326 | .n_voltages = 1, | |
327 | }, | |
328 | .update_bank = 0x04, | |
329 | .update_reg = 0x08, | |
330 | .update_mask = 0x30, | |
331 | .update_val = 0x10, | |
18bc2b39 BJ |
332 | .update_val_hp = 0x10, |
333 | .update_val_lp = 0x30, | |
334 | .update_val_hw = 0x20, | |
d1a82001 LJ |
335 | }, |
336 | }; | |
337 | ||
30aa4b26 LJ |
338 | static struct of_regulator_match ab8500_ext_regulator_match[] = { |
339 | { .name = "ab8500_ext1", .driver_data = (void *) AB8500_EXT_SUPPLY1, }, | |
340 | { .name = "ab8500_ext2", .driver_data = (void *) AB8500_EXT_SUPPLY2, }, | |
341 | { .name = "ab8500_ext3", .driver_data = (void *) AB8500_EXT_SUPPLY3, }, | |
342 | }; | |
343 | ||
08d49f43 | 344 | static int ab8500_ext_regulator_probe(struct platform_device *pdev) |
d1a82001 LJ |
345 | { |
346 | struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent); | |
347 | struct ab8500_platform_data *ppdata; | |
348 | struct ab8500_regulator_platform_data *pdata; | |
30aa4b26 | 349 | struct device_node *np = pdev->dev.of_node; |
d1a82001 LJ |
350 | struct regulator_config config = { }; |
351 | int i, err; | |
352 | ||
30aa4b26 LJ |
353 | if (np) { |
354 | err = of_regulator_match(&pdev->dev, np, | |
355 | ab8500_ext_regulator_match, | |
356 | ARRAY_SIZE(ab8500_ext_regulator_match)); | |
357 | if (err < 0) { | |
358 | dev_err(&pdev->dev, | |
359 | "Error parsing regulator init data: %d\n", err); | |
360 | return err; | |
361 | } | |
362 | } | |
363 | ||
d1a82001 LJ |
364 | if (!ab8500) { |
365 | dev_err(&pdev->dev, "null mfd parent\n"); | |
366 | return -EINVAL; | |
367 | } | |
30aa4b26 | 368 | |
d1a82001 LJ |
369 | ppdata = dev_get_platdata(ab8500->dev); |
370 | if (!ppdata) { | |
371 | dev_err(&pdev->dev, "null parent pdata\n"); | |
372 | return -EINVAL; | |
373 | } | |
374 | ||
375 | pdata = ppdata->regulator; | |
376 | if (!pdata) { | |
377 | dev_err(&pdev->dev, "null pdata\n"); | |
378 | return -EINVAL; | |
379 | } | |
380 | ||
381 | /* make sure the platform data has the correct size */ | |
382 | if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) { | |
383 | dev_err(&pdev->dev, "Configuration error: size mismatch.\n"); | |
384 | return -EINVAL; | |
385 | } | |
386 | ||
387 | /* check for AB8500 2.x */ | |
a6324709 | 388 | if (is_ab8500_2p0_or_earlier(ab8500)) { |
d1a82001 LJ |
389 | struct ab8500_ext_regulator_info *info; |
390 | ||
391 | /* VextSupply3LPn is inverted on AB8500 2.x */ | |
392 | info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3]; | |
393 | info->update_val = 0x30; | |
18bc2b39 BJ |
394 | info->update_val_hp = 0x30; |
395 | info->update_val_lp = 0x10; | |
d1a82001 LJ |
396 | } |
397 | ||
398 | /* register all regulators */ | |
399 | for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) { | |
400 | struct ab8500_ext_regulator_info *info = NULL; | |
401 | ||
402 | /* assign per-regulator data */ | |
403 | info = &ab8500_ext_regulator_info[i]; | |
404 | info->dev = &pdev->dev; | |
18bc2b39 BJ |
405 | info->cfg = (struct ab8500_ext_regulator_cfg *) |
406 | pdata->ext_regulator[i].driver_data; | |
d1a82001 LJ |
407 | |
408 | config.dev = &pdev->dev; | |
d1a82001 | 409 | config.driver_data = info; |
30aa4b26 LJ |
410 | config.of_node = ab8500_ext_regulator_match[i].of_node; |
411 | config.init_data = (np) ? | |
412 | ab8500_ext_regulator_match[i].init_data : | |
413 | &pdata->ext_regulator[i]; | |
d1a82001 LJ |
414 | |
415 | /* register regulator with framework */ | |
a4a6b9de JH |
416 | info->rdev = devm_regulator_register(&pdev->dev, &info->desc, |
417 | &config); | |
d1a82001 LJ |
418 | if (IS_ERR(info->rdev)) { |
419 | err = PTR_ERR(info->rdev); | |
420 | dev_err(&pdev->dev, "failed to register regulator %s\n", | |
421 | info->desc.name); | |
d1a82001 LJ |
422 | return err; |
423 | } | |
424 | ||
425 | dev_dbg(rdev_get_dev(info->rdev), | |
426 | "%s-probed\n", info->desc.name); | |
427 | } | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
5a49b4a5 LJ |
432 | static struct platform_driver ab8500_ext_regulator_driver = { |
433 | .probe = ab8500_ext_regulator_probe, | |
5a49b4a5 LJ |
434 | .driver = { |
435 | .name = "ab8500-ext-regulator", | |
5a49b4a5 LJ |
436 | }, |
437 | }; | |
438 | ||
439 | static int __init ab8500_ext_regulator_init(void) | |
440 | { | |
441 | int ret; | |
442 | ||
443 | ret = platform_driver_register(&ab8500_ext_regulator_driver); | |
444 | if (ret) | |
445 | pr_err("Failed to register ab8500 ext regulator: %d\n", ret); | |
446 | ||
447 | return ret; | |
448 | } | |
449 | subsys_initcall(ab8500_ext_regulator_init); | |
450 | ||
451 | static void __exit ab8500_ext_regulator_exit(void) | |
452 | { | |
453 | platform_driver_unregister(&ab8500_ext_regulator_driver); | |
d1a82001 | 454 | } |
5a49b4a5 | 455 | module_exit(ab8500_ext_regulator_exit); |
d1a82001 LJ |
456 | |
457 | MODULE_LICENSE("GPL v2"); | |
458 | MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>"); | |
459 | MODULE_DESCRIPTION("AB8500 external regulator driver"); | |
460 | MODULE_ALIAS("platform:ab8500-ext-regulator"); |