Merge tag 'regmap-v3.10-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6.git] / drivers / regulator / ab8500-ext.c
blobb4d45472aae6badbe9d63363d6427cad98af8fe4
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/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/mfd/abx500.h>
23 #include <linux/mfd/abx500/ab8500.h>
24 #include <linux/regulator/ab8500.h>
26 /**
27 * struct ab8500_ext_regulator_info - ab8500 regulator information
28 * @dev: device pointer
29 * @desc: regulator description
30 * @rdev: regulator device
31 * @cfg: regulator configuration (extension of regulator FW configuration)
32 * @update_bank: bank to control on/off
33 * @update_reg: register to control on/off
34 * @update_mask: mask to enable/disable and set mode of regulator
35 * @update_val: bits holding the regulator current mode
36 * @update_val_hp: bits to set EN pin active (LPn pin deactive)
37 * normally this means high power mode
38 * @update_val_lp: bits to set EN pin active and LPn pin active
39 * normally this means low power mode
40 * @update_val_hw: bits to set regulator pins in HW control
41 * SysClkReq pins and logic will choose mode
43 struct ab8500_ext_regulator_info {
44 struct device *dev;
45 struct regulator_desc desc;
46 struct regulator_dev *rdev;
47 struct ab8500_ext_regulator_cfg *cfg;
48 u8 update_bank;
49 u8 update_reg;
50 u8 update_mask;
51 u8 update_val;
52 u8 update_val_hp;
53 u8 update_val_lp;
54 u8 update_val_hw;
57 static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
59 int ret;
60 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
61 u8 regval;
63 if (info == NULL) {
64 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
65 return -EINVAL;
69 * To satisfy both HW high power request and SW request, the regulator
70 * must be on in high power.
72 if (info->cfg && info->cfg->hwreq)
73 regval = info->update_val_hp;
74 else
75 regval = info->update_val;
77 ret = abx500_mask_and_set_register_interruptible(info->dev,
78 info->update_bank, info->update_reg,
79 info->update_mask, regval);
80 if (ret < 0) {
81 dev_err(rdev_get_dev(info->rdev),
82 "couldn't set enable bits for regulator\n");
83 return ret;
86 dev_dbg(rdev_get_dev(rdev),
87 "%s-enable (bank, reg, mask, value): 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
88 info->desc.name, info->update_bank, info->update_reg,
89 info->update_mask, regval);
91 return 0;
94 static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
96 int ret;
97 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
98 u8 regval;
100 if (info == NULL) {
101 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
102 return -EINVAL;
106 * Set the regulator in HW request mode if configured
108 if (info->cfg && info->cfg->hwreq)
109 regval = info->update_val_hw;
110 else
111 regval = 0;
113 ret = abx500_mask_and_set_register_interruptible(info->dev,
114 info->update_bank, info->update_reg,
115 info->update_mask, regval);
116 if (ret < 0) {
117 dev_err(rdev_get_dev(info->rdev),
118 "couldn't set disable bits for regulator\n");
119 return ret;
122 dev_dbg(rdev_get_dev(rdev), "%s-disable (bank, reg, mask, value):"
123 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
124 info->desc.name, info->update_bank, info->update_reg,
125 info->update_mask, regval);
127 return 0;
130 static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
132 int ret;
133 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
134 u8 regval;
136 if (info == NULL) {
137 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
138 return -EINVAL;
141 ret = abx500_get_register_interruptible(info->dev,
142 info->update_bank, info->update_reg, &regval);
143 if (ret < 0) {
144 dev_err(rdev_get_dev(rdev),
145 "couldn't read 0x%x register\n", info->update_reg);
146 return ret;
149 dev_dbg(rdev_get_dev(rdev), "%s-is_enabled (bank, reg, mask, value):"
150 " 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
151 info->desc.name, info->update_bank, info->update_reg,
152 info->update_mask, regval);
154 if (((regval & info->update_mask) == info->update_val_lp) ||
155 ((regval & info->update_mask) == info->update_val_hp))
156 return 1;
157 else
158 return 0;
161 static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
162 unsigned int mode)
164 int ret = 0;
165 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
166 u8 regval;
168 if (info == NULL) {
169 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
170 return -EINVAL;
173 switch (mode) {
174 case REGULATOR_MODE_NORMAL:
175 regval = info->update_val_hp;
176 break;
177 case REGULATOR_MODE_IDLE:
178 regval = info->update_val_lp;
179 break;
181 default:
182 return -EINVAL;
185 /* If regulator is enabled and info->cfg->hwreq is set, the regulator
186 must be on in high power, so we don't need to write the register with
187 the same value.
189 if (ab8500_ext_regulator_is_enabled(rdev) &&
190 !(info->cfg && info->cfg->hwreq)) {
191 ret = abx500_mask_and_set_register_interruptible(info->dev,
192 info->update_bank, info->update_reg,
193 info->update_mask, regval);
194 if (ret < 0) {
195 dev_err(rdev_get_dev(rdev),
196 "Could not set regulator mode.\n");
197 return ret;
200 dev_dbg(rdev_get_dev(rdev),
201 "%s-set_mode (bank, reg, mask, value): "
202 "0x%x, 0x%x, 0x%x, 0x%x\n",
203 info->desc.name, info->update_bank, info->update_reg,
204 info->update_mask, regval);
207 info->update_val = regval;
209 return 0;
212 static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
214 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
215 int ret;
217 if (info == NULL) {
218 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
219 return -EINVAL;
222 if (info->update_val == info->update_val_hp)
223 ret = REGULATOR_MODE_NORMAL;
224 else if (info->update_val == info->update_val_lp)
225 ret = REGULATOR_MODE_IDLE;
226 else
227 ret = -EINVAL;
229 return ret;
232 static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
233 unsigned selector)
235 struct regulation_constraints *regu_constraints = rdev->constraints;
237 if (regu_constraints == NULL) {
238 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
239 return -EINVAL;
241 /* return the uV for the fixed regulators */
242 if (regu_constraints->min_uV && regu_constraints->max_uV) {
243 if (regu_constraints->min_uV == regu_constraints->max_uV)
244 return regu_constraints->min_uV;
246 return -EINVAL;
249 static struct regulator_ops ab8500_ext_regulator_ops = {
250 .enable = ab8500_ext_regulator_enable,
251 .disable = ab8500_ext_regulator_disable,
252 .is_enabled = ab8500_ext_regulator_is_enabled,
253 .set_mode = ab8500_ext_regulator_set_mode,
254 .get_mode = ab8500_ext_regulator_get_mode,
255 .list_voltage = ab8500_ext_list_voltage,
258 static struct ab8500_ext_regulator_info
259 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
260 [AB8500_EXT_SUPPLY1] = {
261 .desc = {
262 .name = "VEXTSUPPLY1",
263 .ops = &ab8500_ext_regulator_ops,
264 .type = REGULATOR_VOLTAGE,
265 .id = AB8500_EXT_SUPPLY1,
266 .owner = THIS_MODULE,
267 .n_voltages = 1,
269 .update_bank = 0x04,
270 .update_reg = 0x08,
271 .update_mask = 0x03,
272 .update_val = 0x01,
273 .update_val_hp = 0x01,
274 .update_val_lp = 0x03,
275 .update_val_hw = 0x02,
277 [AB8500_EXT_SUPPLY2] = {
278 .desc = {
279 .name = "VEXTSUPPLY2",
280 .ops = &ab8500_ext_regulator_ops,
281 .type = REGULATOR_VOLTAGE,
282 .id = AB8500_EXT_SUPPLY2,
283 .owner = THIS_MODULE,
284 .n_voltages = 1,
286 .update_bank = 0x04,
287 .update_reg = 0x08,
288 .update_mask = 0x0c,
289 .update_val = 0x04,
290 .update_val_hp = 0x04,
291 .update_val_lp = 0x0c,
292 .update_val_hw = 0x08,
294 [AB8500_EXT_SUPPLY3] = {
295 .desc = {
296 .name = "VEXTSUPPLY3",
297 .ops = &ab8500_ext_regulator_ops,
298 .type = REGULATOR_VOLTAGE,
299 .id = AB8500_EXT_SUPPLY3,
300 .owner = THIS_MODULE,
301 .n_voltages = 1,
303 .update_bank = 0x04,
304 .update_reg = 0x08,
305 .update_mask = 0x30,
306 .update_val = 0x10,
307 .update_val_hp = 0x10,
308 .update_val_lp = 0x30,
309 .update_val_hw = 0x20,
313 int ab8500_ext_regulator_init(struct platform_device *pdev)
315 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
316 struct ab8500_platform_data *ppdata;
317 struct ab8500_regulator_platform_data *pdata;
318 struct regulator_config config = { };
319 int i, err;
321 if (!ab8500) {
322 dev_err(&pdev->dev, "null mfd parent\n");
323 return -EINVAL;
325 ppdata = dev_get_platdata(ab8500->dev);
326 if (!ppdata) {
327 dev_err(&pdev->dev, "null parent pdata\n");
328 return -EINVAL;
331 pdata = ppdata->regulator;
332 if (!pdata) {
333 dev_err(&pdev->dev, "null pdata\n");
334 return -EINVAL;
337 /* make sure the platform data has the correct size */
338 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
339 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
340 return -EINVAL;
343 /* check for AB8500 2.x */
344 if (is_ab8500_2p0_or_earlier(ab8500)) {
345 struct ab8500_ext_regulator_info *info;
347 /* VextSupply3LPn is inverted on AB8500 2.x */
348 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
349 info->update_val = 0x30;
350 info->update_val_hp = 0x30;
351 info->update_val_lp = 0x10;
354 /* register all regulators */
355 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
356 struct ab8500_ext_regulator_info *info = NULL;
358 /* assign per-regulator data */
359 info = &ab8500_ext_regulator_info[i];
360 info->dev = &pdev->dev;
361 info->cfg = (struct ab8500_ext_regulator_cfg *)
362 pdata->ext_regulator[i].driver_data;
364 config.dev = &pdev->dev;
365 config.init_data = &pdata->ext_regulator[i];
366 config.driver_data = info;
368 /* register regulator with framework */
369 info->rdev = regulator_register(&info->desc, &config);
370 if (IS_ERR(info->rdev)) {
371 err = PTR_ERR(info->rdev);
372 dev_err(&pdev->dev, "failed to register regulator %s\n",
373 info->desc.name);
374 /* when we fail, un-register all earlier regulators */
375 while (--i >= 0) {
376 info = &ab8500_ext_regulator_info[i];
377 regulator_unregister(info->rdev);
379 return err;
382 dev_dbg(rdev_get_dev(info->rdev),
383 "%s-probed\n", info->desc.name);
386 return 0;
389 void ab8500_ext_regulator_exit(struct platform_device *pdev)
391 int i;
393 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
394 struct ab8500_ext_regulator_info *info = NULL;
395 info = &ab8500_ext_regulator_info[i];
397 dev_vdbg(rdev_get_dev(info->rdev),
398 "%s-remove\n", info->desc.name);
400 regulator_unregister(info->rdev);
404 MODULE_LICENSE("GPL v2");
405 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
406 MODULE_DESCRIPTION("AB8500 external regulator driver");
407 MODULE_ALIAS("platform:ab8500-ext-regulator");