2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
8 * AB8500 peripheral regulators
10 * AB8500 supports the following regulators,
11 * LDOs - VAUDIO, VANAMIC2/2, VDIGMIC, VINTCORE12, VTVOUT,
14 * for DB8500 cut 1.0 and previous versions of the silicon, all accesses
15 * to registers are through the DB8500 SPI. In cut 1.1 onwards, these
16 * accesses are through the DB8500 PRCMU I2C
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/mfd/ab8500.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/regulator/ab8500.h>
29 * struct ab8500_regulator_info - ab8500 regulator information
30 * @desc: regulator description
31 * @ab8500: ab8500 parent
32 * @regulator_dev: regulator device
33 * @max_uV: maximum voltage (for variable voltage supplies)
34 * @min_uV: minimum voltage (for variable voltage supplies)
35 * @fixed_uV: typical voltage (for fixed voltage supplies)
36 * @update_reg: register to control on/off
37 * @mask: mask to enable/disable regulator
38 * @enable: bits to enable the regulator in normal(high power) mode
39 * @voltage_reg: register to control regulator voltage
40 * @voltage_mask: mask to control regulator voltage
41 * @supported_voltages: supported voltage table
42 * @voltages_len: number of supported voltages for the regulator
44 struct ab8500_regulator_info {
46 struct regulator_desc desc;
47 struct ab8500 *ab8500;
48 struct regulator_dev *regulator;
57 int const *supported_voltages;
61 /* voltage tables for the vauxn/vintcore supplies */
62 static const int ldo_vauxn_voltages[] = {
81 static const int ldo_vintcore_voltages[] = {
91 static int ab8500_regulator_enable(struct regulator_dev *rdev)
93 int regulator_id, ret;
94 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
96 regulator_id = rdev_get_id(rdev);
97 if (regulator_id >= AB8500_NUM_REGULATORS)
100 ret = ab8500_set_bits(info->ab8500, info->update_reg,
101 info->mask, info->enable);
103 dev_err(rdev_get_dev(rdev),
104 "couldn't set enable bits for regulator\n");
108 static int ab8500_regulator_disable(struct regulator_dev *rdev)
110 int regulator_id, ret;
111 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
113 regulator_id = rdev_get_id(rdev);
114 if (regulator_id >= AB8500_NUM_REGULATORS)
117 ret = ab8500_set_bits(info->ab8500, info->update_reg,
120 dev_err(rdev_get_dev(rdev),
121 "couldn't set disable bits for regulator\n");
125 static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
127 int regulator_id, ret;
128 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
130 regulator_id = rdev_get_id(rdev);
131 if (regulator_id >= AB8500_NUM_REGULATORS)
134 ret = ab8500_read(info->ab8500, info->update_reg);
136 dev_err(rdev_get_dev(rdev),
137 "couldn't read 0x%x register\n", info->update_reg);
141 if (ret & info->mask)
147 static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
150 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
152 regulator_id = rdev_get_id(rdev);
153 if (regulator_id >= AB8500_NUM_REGULATORS)
156 /* return the uV for the fixed regulators */
158 return info->fixed_uV;
160 if (selector > info->voltages_len)
163 return info->supported_voltages[selector];
166 static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
168 int regulator_id, ret, val;
169 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
171 regulator_id = rdev_get_id(rdev);
172 if (regulator_id >= AB8500_NUM_REGULATORS)
175 ret = ab8500_read(info->ab8500, info->voltage_reg);
177 dev_err(rdev_get_dev(rdev),
178 "couldn't read voltage reg for regulator\n");
182 /* vintcore has a different layout */
183 val = ret & info->voltage_mask;
184 if (regulator_id == AB8500_LDO_INTCORE)
185 ret = info->supported_voltages[val >> 0x3];
187 ret = info->supported_voltages[val];
192 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
193 int min_uV, int max_uV)
195 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
198 /* check the supported voltage */
199 for (i = 0; i < info->voltages_len; i++) {
200 if ((info->supported_voltages[i] >= min_uV) &&
201 (info->supported_voltages[i] <= max_uV))
208 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
209 int min_uV, int max_uV)
211 int regulator_id, ret;
212 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
214 regulator_id = rdev_get_id(rdev);
215 if (regulator_id >= AB8500_NUM_REGULATORS)
218 /* get the appropriate voltages within the range */
219 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
221 dev_err(rdev_get_dev(rdev),
222 "couldn't get best voltage for regulator\n");
226 /* set the registers for the request */
227 ret = ab8500_set_bits(info->ab8500, info->voltage_reg,
228 info->voltage_mask, ret);
230 dev_err(rdev_get_dev(rdev),
231 "couldn't set voltage reg for regulator\n");
236 static struct regulator_ops ab8500_regulator_ops = {
237 .enable = ab8500_regulator_enable,
238 .disable = ab8500_regulator_disable,
239 .is_enabled = ab8500_regulator_is_enabled,
240 .get_voltage = ab8500_regulator_get_voltage,
241 .set_voltage = ab8500_regulator_set_voltage,
242 .list_voltage = ab8500_list_voltage,
245 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
248 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
250 regulator_id = rdev_get_id(rdev);
251 if (regulator_id >= AB8500_NUM_REGULATORS)
254 return info->fixed_uV;
257 static struct regulator_ops ab8500_ldo_fixed_ops = {
258 .enable = ab8500_regulator_enable,
259 .disable = ab8500_regulator_disable,
260 .is_enabled = ab8500_regulator_is_enabled,
261 .get_voltage = ab8500_fixed_get_voltage,
262 .list_voltage = ab8500_list_voltage,
265 #define AB8500_LDO(_id, min, max, reg, reg_mask, reg_enable, \
266 volt_reg, volt_mask, voltages, \
270 .name = "LDO-" #_id, \
271 .ops = &ab8500_regulator_ops, \
272 .type = REGULATOR_VOLTAGE, \
273 .id = AB8500_LDO_##_id, \
274 .owner = THIS_MODULE, \
276 .min_uV = (min) * 1000, \
277 .max_uV = (max) * 1000, \
280 .enable = reg_enable, \
281 .voltage_reg = volt_reg, \
282 .voltage_mask = volt_mask, \
283 .supported_voltages = voltages, \
284 .voltages_len = len_volts, \
288 #define AB8500_FIXED_LDO(_id, fixed, reg, reg_mask, \
292 .name = "LDO-" #_id, \
293 .ops = &ab8500_ldo_fixed_ops, \
294 .type = REGULATOR_VOLTAGE, \
295 .id = AB8500_LDO_##_id, \
296 .owner = THIS_MODULE, \
298 .fixed_uV = fixed * 1000, \
301 .enable = reg_enable, \
304 static struct ab8500_regulator_info ab8500_regulator_info[] = {
306 * Variable Voltage LDOs
307 * name, min uV, max uV, ctrl reg, reg mask, enable mask,
308 * volt ctrl reg, volt ctrl mask, volt table, num supported volts
310 AB8500_LDO(AUX1, 1100, 3300, 0x0409, 0x3, 0x1, 0x041f, 0xf,
311 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
312 AB8500_LDO(AUX2, 1100, 3300, 0x0409, 0xc, 0x4, 0x0420, 0xf,
313 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
314 AB8500_LDO(AUX3, 1100, 3300, 0x040a, 0x3, 0x1, 0x0421, 0xf,
315 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
316 AB8500_LDO(INTCORE, 1100, 3300, 0x0380, 0x4, 0x4, 0x0380, 0x38,
317 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
321 * name, o/p uV, ctrl reg, enable, disable
323 AB8500_FIXED_LDO(TVOUT, 2000, 0x0380, 0x2, 0x2),
324 AB8500_FIXED_LDO(AUDIO, 2000, 0x0383, 0x2, 0x2),
325 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x0383, 0x4, 0x4),
326 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x0383, 0x8, 0x8),
327 AB8500_FIXED_LDO(DMIC, 1800, 0x0383, 0x10, 0x10),
328 AB8500_FIXED_LDO(ANA, 1200, 0x0383, 0xc, 0x4),
331 static inline struct ab8500_regulator_info *find_regulator_info(int id)
333 struct ab8500_regulator_info *info;
336 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
337 info = &ab8500_regulator_info[i];
338 if (info->desc.id == id)
344 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
346 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
347 struct ab8500_platform_data *pdata = dev_get_platdata(ab8500->dev);
351 dev_err(&pdev->dev, "null mfd parent\n");
355 /* register all regulators */
356 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
357 struct ab8500_regulator_info *info = NULL;
359 /* assign per-regulator data */
360 info = &ab8500_regulator_info[i];
361 info->dev = &pdev->dev;
362 info->ab8500 = ab8500;
364 info->regulator = regulator_register(&info->desc, &pdev->dev,
365 pdata->regulator[i], info);
366 if (IS_ERR(info->regulator)) {
367 err = PTR_ERR(info->regulator);
368 dev_err(&pdev->dev, "failed to register regulator %s\n",
370 /* when we fail, un-register all earlier regulators */
373 info = &ab8500_regulator_info[i];
374 regulator_unregister(info->regulator);
384 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
388 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
389 struct ab8500_regulator_info *info = NULL;
390 info = &ab8500_regulator_info[i];
391 regulator_unregister(info->regulator);
397 static struct platform_driver ab8500_regulator_driver = {
398 .probe = ab8500_regulator_probe,
399 .remove = __devexit_p(ab8500_regulator_remove),
401 .name = "ab8500-regulator",
402 .owner = THIS_MODULE,
406 static int __init ab8500_regulator_init(void)
410 ret = platform_driver_register(&ab8500_regulator_driver);
412 pr_err("Failed to register ab8500 regulator: %d\n", ret);
416 subsys_initcall(ab8500_regulator_init);
418 static void __exit ab8500_regulator_exit(void)
420 platform_driver_unregister(&ab8500_regulator_driver);
422 module_exit(ab8500_regulator_exit);
424 MODULE_LICENSE("GPL v2");
425 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
426 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
427 MODULE_ALIAS("platform:ab8500-regulator");