irqchip: crossbar: Introduce centralized check for crossbar write
[linux/fpc-iii.git] / drivers / regulator / ab8500-ext.c
blob29c0faaf8eba4e94f34d8848d91b7d0d85dea2ec
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
6 * Authors: Bengt Jonsson <bengt.g.jonsson@stericsson.com>
8 * This file is based on drivers/regulator/ab8500.c
10 * AB8500 external regulators
12 * ab8500-ext supports the following regulators:
13 * - VextSupply3
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/mfd/abx500.h>
25 #include <linux/mfd/abx500/ab8500.h>
26 #include <linux/regulator/ab8500.h>
28 /**
29 * struct ab8500_ext_regulator_info - ab8500 regulator information
30 * @dev: device pointer
31 * @desc: regulator description
32 * @rdev: regulator device
33 * @cfg: regulator configuration (extension of regulator FW configuration)
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
38 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
39 * normally this means high power mode
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
45 struct ab8500_ext_regulator_info {
46 struct device *dev;
47 struct regulator_desc desc;
48 struct regulator_dev *rdev;
49 struct ab8500_ext_regulator_cfg *cfg;
50 u8 update_bank;
51 u8 update_reg;
52 u8 update_mask;
53 u8 update_val;
54 u8 update_val_hp;
55 u8 update_val_lp;
56 u8 update_val_hw;
59 static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
61 int ret;
62 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
63 u8 regval;
65 if (info == NULL) {
66 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
67 return -EINVAL;
71 * To satisfy both HW high power request and SW request, the regulator
72 * must be on in high power.
74 if (info->cfg && info->cfg->hwreq)
75 regval = info->update_val_hp;
76 else
77 regval = info->update_val;
79 ret = abx500_mask_and_set_register_interruptible(info->dev,
80 info->update_bank, info->update_reg,
81 info->update_mask, regval);
82 if (ret < 0) {
83 dev_err(rdev_get_dev(info->rdev),
84 "couldn't set enable bits for regulator\n");
85 return ret;
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);
93 return 0;
96 static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
98 int ret;
99 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
100 u8 regval;
102 if (info == NULL) {
103 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
104 return -EINVAL;
108 * Set the regulator in HW request mode if configured
110 if (info->cfg && info->cfg->hwreq)
111 regval = info->update_val_hw;
112 else
113 regval = 0;
115 ret = abx500_mask_and_set_register_interruptible(info->dev,
116 info->update_bank, info->update_reg,
117 info->update_mask, regval);
118 if (ret < 0) {
119 dev_err(rdev_get_dev(info->rdev),
120 "couldn't set disable bits for regulator\n");
121 return ret;
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,
127 info->update_mask, regval);
129 return 0;
132 static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
134 int ret;
135 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
136 u8 regval;
138 if (info == NULL) {
139 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
140 return -EINVAL;
143 ret = abx500_get_register_interruptible(info->dev,
144 info->update_bank, info->update_reg, &regval);
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;
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);
156 if (((regval & info->update_mask) == info->update_val_lp) ||
157 ((regval & info->update_mask) == info->update_val_hp))
158 return 1;
159 else
160 return 0;
163 static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
164 unsigned int mode)
166 int ret = 0;
167 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
168 u8 regval;
170 if (info == NULL) {
171 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
172 return -EINVAL;
175 switch (mode) {
176 case REGULATOR_MODE_NORMAL:
177 regval = info->update_val_hp;
178 break;
179 case REGULATOR_MODE_IDLE:
180 regval = info->update_val_lp;
181 break;
183 default:
184 return -EINVAL;
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.
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) {
197 dev_err(rdev_get_dev(rdev),
198 "Could not set regulator mode.\n");
199 return ret;
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);
209 info->update_val = regval;
211 return 0;
214 static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
216 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
217 int ret;
219 if (info == NULL) {
220 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
221 return -EINVAL;
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;
231 return ret;
234 static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
235 int max_uV, unsigned *selector)
237 struct regulation_constraints *regu_constraints = rdev->constraints;
239 if (!regu_constraints) {
240 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
241 return -EINVAL;
244 if (regu_constraints->min_uV == min_uV &&
245 regu_constraints->max_uV == max_uV)
246 return 0;
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);
253 return -EINVAL;
256 static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
257 unsigned selector)
259 struct regulation_constraints *regu_constraints = rdev->constraints;
261 if (regu_constraints == NULL) {
262 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
263 return -EINVAL;
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;
270 return -EINVAL;
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,
279 .set_voltage = ab8500_ext_set_voltage,
280 .list_voltage = ab8500_ext_list_voltage,
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,
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,
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,
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,
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,
328 .update_bank = 0x04,
329 .update_reg = 0x08,
330 .update_mask = 0x30,
331 .update_val = 0x10,
332 .update_val_hp = 0x10,
333 .update_val_lp = 0x30,
334 .update_val_hw = 0x20,
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, },
344 static int ab8500_ext_regulator_probe(struct platform_device *pdev)
346 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
347 struct ab8500_platform_data *ppdata;
348 struct ab8500_regulator_platform_data *pdata;
349 struct device_node *np = pdev->dev.of_node;
350 struct regulator_config config = { };
351 int i, err;
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;
364 if (!ab8500) {
365 dev_err(&pdev->dev, "null mfd parent\n");
366 return -EINVAL;
369 ppdata = dev_get_platdata(ab8500->dev);
370 if (!ppdata) {
371 dev_err(&pdev->dev, "null parent pdata\n");
372 return -EINVAL;
375 pdata = ppdata->regulator;
376 if (!pdata) {
377 dev_err(&pdev->dev, "null pdata\n");
378 return -EINVAL;
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;
387 /* check for AB8500 2.x */
388 if (is_ab8500_2p0_or_earlier(ab8500)) {
389 struct ab8500_ext_regulator_info *info;
391 /* VextSupply3LPn is inverted on AB8500 2.x */
392 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
393 info->update_val = 0x30;
394 info->update_val_hp = 0x30;
395 info->update_val_lp = 0x10;
398 /* register all regulators */
399 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
400 struct ab8500_ext_regulator_info *info = NULL;
402 /* assign per-regulator data */
403 info = &ab8500_ext_regulator_info[i];
404 info->dev = &pdev->dev;
405 info->cfg = (struct ab8500_ext_regulator_cfg *)
406 pdata->ext_regulator[i].driver_data;
408 config.dev = &pdev->dev;
409 config.driver_data = info;
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];
415 /* register regulator with framework */
416 info->rdev = devm_regulator_register(&pdev->dev, &info->desc,
417 &config);
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);
422 return err;
425 dev_dbg(rdev_get_dev(info->rdev),
426 "%s-probed\n", info->desc.name);
429 return 0;
432 static struct platform_driver ab8500_ext_regulator_driver = {
433 .probe = ab8500_ext_regulator_probe,
434 .driver = {
435 .name = "ab8500-ext-regulator",
436 .owner = THIS_MODULE,
440 static int __init ab8500_ext_regulator_init(void)
442 int ret;
444 ret = platform_driver_register(&ab8500_ext_regulator_driver);
445 if (ret)
446 pr_err("Failed to register ab8500 ext regulator: %d\n", ret);
448 return ret;
450 subsys_initcall(ab8500_ext_regulator_init);
452 static void __exit ab8500_ext_regulator_exit(void)
454 platform_driver_unregister(&ab8500_ext_regulator_driver);
456 module_exit(ab8500_ext_regulator_exit);
458 MODULE_LICENSE("GPL v2");
459 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
460 MODULE_DESCRIPTION("AB8500 external regulator driver");
461 MODULE_ALIAS("platform:ab8500-ext-regulator");