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:
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>
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 {
45 struct regulator_desc desc;
46 struct regulator_dev *rdev;
47 struct ab8500_ext_regulator_cfg *cfg;
57 static int ab8500_ext_regulator_enable(struct regulator_dev *rdev)
60 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
64 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
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;
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);
81 dev_err(rdev_get_dev(info->rdev),
82 "couldn't set enable bits for regulator\n");
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);
94 static int ab8500_ext_regulator_disable(struct regulator_dev *rdev)
97 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
101 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
106 * Set the regulator in HW request mode if configured
108 if (info->cfg && info->cfg->hwreq)
109 regval = info->update_val_hw;
113 ret = abx500_mask_and_set_register_interruptible(info->dev,
114 info->update_bank, info->update_reg,
115 info->update_mask, regval);
117 dev_err(rdev_get_dev(info->rdev),
118 "couldn't set disable bits for regulator\n");
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);
130 static int ab8500_ext_regulator_is_enabled(struct regulator_dev *rdev)
133 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
137 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
141 ret = abx500_get_register_interruptible(info->dev,
142 info->update_bank, info->update_reg, ®val);
144 dev_err(rdev_get_dev(rdev),
145 "couldn't read 0x%x register\n", info->update_reg);
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))
161 static int ab8500_ext_regulator_set_mode(struct regulator_dev *rdev,
165 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
169 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
174 case REGULATOR_MODE_NORMAL:
175 regval = info->update_val_hp;
177 case REGULATOR_MODE_IDLE:
178 regval = info->update_val_lp;
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
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);
195 dev_err(rdev_get_dev(rdev),
196 "Could not set regulator mode.\n");
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;
212 static unsigned int ab8500_ext_regulator_get_mode(struct regulator_dev *rdev)
214 struct ab8500_ext_regulator_info *info = rdev_get_drvdata(rdev);
218 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
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;
232 static int ab8500_ext_set_voltage(struct regulator_dev *rdev, int min_uV,
233 int max_uV, unsigned *selector)
235 struct regulation_constraints *regu_constraints = rdev->constraints;
237 if (!regu_constraints) {
238 dev_err(rdev_get_dev(rdev), "No regulator constraints\n");
242 if (regu_constraints->min_uV == min_uV &&
243 regu_constraints->max_uV == max_uV)
246 dev_err(rdev_get_dev(rdev),
247 "Requested min %duV max %duV != constrained min %duV max %duV\n",
249 regu_constraints->min_uV, regu_constraints->max_uV);
254 static int ab8500_ext_list_voltage(struct regulator_dev *rdev,
257 struct regulation_constraints *regu_constraints = rdev->constraints;
259 if (regu_constraints == NULL) {
260 dev_err(rdev_get_dev(rdev), "regulator constraints null pointer\n");
263 /* return the uV for the fixed regulators */
264 if (regu_constraints->min_uV && regu_constraints->max_uV) {
265 if (regu_constraints->min_uV == regu_constraints->max_uV)
266 return regu_constraints->min_uV;
271 static struct regulator_ops ab8500_ext_regulator_ops = {
272 .enable = ab8500_ext_regulator_enable,
273 .disable = ab8500_ext_regulator_disable,
274 .is_enabled = ab8500_ext_regulator_is_enabled,
275 .set_mode = ab8500_ext_regulator_set_mode,
276 .get_mode = ab8500_ext_regulator_get_mode,
277 .set_voltage = ab8500_ext_set_voltage,
278 .list_voltage = ab8500_ext_list_voltage,
281 static struct ab8500_ext_regulator_info
282 ab8500_ext_regulator_info[AB8500_NUM_EXT_REGULATORS] = {
283 [AB8500_EXT_SUPPLY1] = {
285 .name = "VEXTSUPPLY1",
286 .ops = &ab8500_ext_regulator_ops,
287 .type = REGULATOR_VOLTAGE,
288 .id = AB8500_EXT_SUPPLY1,
289 .owner = THIS_MODULE,
296 .update_val_hp = 0x01,
297 .update_val_lp = 0x03,
298 .update_val_hw = 0x02,
300 [AB8500_EXT_SUPPLY2] = {
302 .name = "VEXTSUPPLY2",
303 .ops = &ab8500_ext_regulator_ops,
304 .type = REGULATOR_VOLTAGE,
305 .id = AB8500_EXT_SUPPLY2,
306 .owner = THIS_MODULE,
313 .update_val_hp = 0x04,
314 .update_val_lp = 0x0c,
315 .update_val_hw = 0x08,
317 [AB8500_EXT_SUPPLY3] = {
319 .name = "VEXTSUPPLY3",
320 .ops = &ab8500_ext_regulator_ops,
321 .type = REGULATOR_VOLTAGE,
322 .id = AB8500_EXT_SUPPLY3,
323 .owner = THIS_MODULE,
330 .update_val_hp = 0x10,
331 .update_val_lp = 0x30,
332 .update_val_hw = 0x20,
336 int ab8500_ext_regulator_init(struct platform_device *pdev)
338 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
339 struct ab8500_platform_data *ppdata;
340 struct ab8500_regulator_platform_data *pdata;
341 struct regulator_config config = { };
345 dev_err(&pdev->dev, "null mfd parent\n");
348 ppdata = dev_get_platdata(ab8500->dev);
350 dev_err(&pdev->dev, "null parent pdata\n");
354 pdata = ppdata->regulator;
356 dev_err(&pdev->dev, "null pdata\n");
360 /* make sure the platform data has the correct size */
361 if (pdata->num_ext_regulator != ARRAY_SIZE(ab8500_ext_regulator_info)) {
362 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
366 /* check for AB8500 2.x */
367 if (is_ab8500_2p0_or_earlier(ab8500)) {
368 struct ab8500_ext_regulator_info *info;
370 /* VextSupply3LPn is inverted on AB8500 2.x */
371 info = &ab8500_ext_regulator_info[AB8500_EXT_SUPPLY3];
372 info->update_val = 0x30;
373 info->update_val_hp = 0x30;
374 info->update_val_lp = 0x10;
377 /* register all regulators */
378 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
379 struct ab8500_ext_regulator_info *info = NULL;
381 /* assign per-regulator data */
382 info = &ab8500_ext_regulator_info[i];
383 info->dev = &pdev->dev;
384 info->cfg = (struct ab8500_ext_regulator_cfg *)
385 pdata->ext_regulator[i].driver_data;
387 config.dev = &pdev->dev;
388 config.init_data = &pdata->ext_regulator[i];
389 config.driver_data = info;
391 /* register regulator with framework */
392 info->rdev = regulator_register(&info->desc, &config);
393 if (IS_ERR(info->rdev)) {
394 err = PTR_ERR(info->rdev);
395 dev_err(&pdev->dev, "failed to register regulator %s\n",
397 /* when we fail, un-register all earlier regulators */
399 info = &ab8500_ext_regulator_info[i];
400 regulator_unregister(info->rdev);
405 dev_dbg(rdev_get_dev(info->rdev),
406 "%s-probed\n", info->desc.name);
412 void ab8500_ext_regulator_exit(struct platform_device *pdev)
416 for (i = 0; i < ARRAY_SIZE(ab8500_ext_regulator_info); i++) {
417 struct ab8500_ext_regulator_info *info = NULL;
418 info = &ab8500_ext_regulator_info[i];
420 dev_vdbg(rdev_get_dev(info->rdev),
421 "%s-remove\n", info->desc.name);
423 regulator_unregister(info->rdev);
427 MODULE_LICENSE("GPL v2");
428 MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
429 MODULE_DESCRIPTION("AB8500 external regulator driver");
430 MODULE_ALIAS("platform:ab8500-ext-regulator");