blob: 9a3dc7973ae1cded1542a51586eb212577d6f18c [file] [log] [blame]
Sundar R IYERc789ca22010-07-13 21:48:56 +05301/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License v2
5 *
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7 *
8 * AB8500 peripheral regulators
9 *
10 * AB8500 supports the following regulators,
11 * LDOs - VAUDIO, VANAMIC2/2, VDIGMIC, VINTCORE12, VTVOUT,
12 * VAUX1/2/3, VANA
13 *
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
17 *
18 */
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>
Mattias Wallin47c16972010-09-10 17:47:56 +020024#include <linux/mfd/abx500.h>
Sundar R IYERc789ca22010-07-13 21:48:56 +053025#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27#include <linux/regulator/ab8500.h>
28
29/**
30 * struct ab8500_regulator_info - ab8500 regulator information
31 * @desc: regulator description
Sundar R IYERc789ca22010-07-13 21:48:56 +053032 * @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)
Mattias Wallin47c16972010-09-10 17:47:56 +020036 * @update_bank: bank to control on/off
Sundar R IYERc789ca22010-07-13 21:48:56 +053037 * @update_reg: register to control on/off
38 * @mask: mask to enable/disable regulator
39 * @enable: bits to enable the regulator in normal(high power) mode
Mattias Wallin47c16972010-09-10 17:47:56 +020040 * @voltage_bank: bank to control regulator voltage
Sundar R IYERc789ca22010-07-13 21:48:56 +053041 * @voltage_reg: register to control regulator voltage
42 * @voltage_mask: mask to control regulator voltage
43 * @supported_voltages: supported voltage table
44 * @voltages_len: number of supported voltages for the regulator
45 */
46struct ab8500_regulator_info {
47 struct device *dev;
48 struct regulator_desc desc;
Sundar R IYERc789ca22010-07-13 21:48:56 +053049 struct regulator_dev *regulator;
50 int max_uV;
51 int min_uV;
52 int fixed_uV;
Mattias Wallin47c16972010-09-10 17:47:56 +020053 u8 update_bank;
54 u8 update_reg;
55 u8 mask;
56 u8 enable;
57 u8 voltage_bank;
58 u8 voltage_reg;
59 u8 voltage_mask;
Sundar R IYERc789ca22010-07-13 21:48:56 +053060 int const *supported_voltages;
61 int voltages_len;
62};
63
64/* voltage tables for the vauxn/vintcore supplies */
65static const int ldo_vauxn_voltages[] = {
66 1100000,
67 1200000,
68 1300000,
69 1400000,
70 1500000,
71 1800000,
72 1850000,
73 1900000,
74 2500000,
75 2650000,
76 2700000,
77 2750000,
78 2800000,
79 2900000,
80 3000000,
81 3300000,
82};
83
Bengt Jonsson2b751512010-12-10 11:08:43 +010084static const int ldo_vaux3_voltages[] = {
85 1200000,
86 1500000,
87 1800000,
88 2100000,
89 2500000,
90 2750000,
91 2790000,
92 2910000,
93};
94
Sundar R IYERc789ca22010-07-13 21:48:56 +053095static const int ldo_vintcore_voltages[] = {
96 1200000,
97 1225000,
98 1250000,
99 1275000,
100 1300000,
101 1325000,
102 1350000,
103};
104
105static int ab8500_regulator_enable(struct regulator_dev *rdev)
106{
107 int regulator_id, ret;
108 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
109
110 regulator_id = rdev_get_id(rdev);
111 if (regulator_id >= AB8500_NUM_REGULATORS)
112 return -EINVAL;
113
Mattias Wallin47c16972010-09-10 17:47:56 +0200114 ret = abx500_mask_and_set_register_interruptible(info->dev,
115 info->update_bank, info->update_reg, info->mask, info->enable);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530116 if (ret < 0)
117 dev_err(rdev_get_dev(rdev),
118 "couldn't set enable bits for regulator\n");
119 return ret;
120}
121
122static int ab8500_regulator_disable(struct regulator_dev *rdev)
123{
124 int regulator_id, ret;
125 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
126
127 regulator_id = rdev_get_id(rdev);
128 if (regulator_id >= AB8500_NUM_REGULATORS)
129 return -EINVAL;
130
Mattias Wallin47c16972010-09-10 17:47:56 +0200131 ret = abx500_mask_and_set_register_interruptible(info->dev,
132 info->update_bank, info->update_reg, info->mask, 0x0);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530133 if (ret < 0)
134 dev_err(rdev_get_dev(rdev),
135 "couldn't set disable bits for regulator\n");
136 return ret;
137}
138
139static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
140{
141 int regulator_id, ret;
142 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
Mattias Wallin47c16972010-09-10 17:47:56 +0200143 u8 value;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530144
145 regulator_id = rdev_get_id(rdev);
146 if (regulator_id >= AB8500_NUM_REGULATORS)
147 return -EINVAL;
148
Mattias Wallin47c16972010-09-10 17:47:56 +0200149 ret = abx500_get_register_interruptible(info->dev,
150 info->update_bank, info->update_reg, &value);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530151 if (ret < 0) {
152 dev_err(rdev_get_dev(rdev),
153 "couldn't read 0x%x register\n", info->update_reg);
154 return ret;
155 }
156
Mattias Wallin47c16972010-09-10 17:47:56 +0200157 if (value & info->mask)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530158 return true;
159 else
160 return false;
161}
162
163static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
164{
165 int regulator_id;
166 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
167
168 regulator_id = rdev_get_id(rdev);
169 if (regulator_id >= AB8500_NUM_REGULATORS)
170 return -EINVAL;
171
172 /* return the uV for the fixed regulators */
173 if (info->fixed_uV)
174 return info->fixed_uV;
175
Axel Lin49990e62010-09-04 23:06:41 +0800176 if (selector >= info->voltages_len)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530177 return -EINVAL;
178
179 return info->supported_voltages[selector];
180}
181
182static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
183{
Mattias Wallin47c16972010-09-10 17:47:56 +0200184 int regulator_id, ret;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530185 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
Mattias Wallin47c16972010-09-10 17:47:56 +0200186 u8 value;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530187
188 regulator_id = rdev_get_id(rdev);
189 if (regulator_id >= AB8500_NUM_REGULATORS)
190 return -EINVAL;
191
Mattias Wallin47c16972010-09-10 17:47:56 +0200192 ret = abx500_get_register_interruptible(info->dev, info->voltage_bank,
193 info->voltage_reg, &value);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530194 if (ret < 0) {
195 dev_err(rdev_get_dev(rdev),
196 "couldn't read voltage reg for regulator\n");
197 return ret;
198 }
199
200 /* vintcore has a different layout */
Mattias Wallin47c16972010-09-10 17:47:56 +0200201 value &= info->voltage_mask;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530202 if (regulator_id == AB8500_LDO_INTCORE)
Mattias Wallin47c16972010-09-10 17:47:56 +0200203 ret = info->supported_voltages[value >> 0x3];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530204 else
Mattias Wallin47c16972010-09-10 17:47:56 +0200205 ret = info->supported_voltages[value];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530206
207 return ret;
208}
209
210static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
211 int min_uV, int max_uV)
212{
213 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
214 int i;
215
216 /* check the supported voltage */
217 for (i = 0; i < info->voltages_len; i++) {
218 if ((info->supported_voltages[i] >= min_uV) &&
219 (info->supported_voltages[i] <= max_uV))
220 return i;
221 }
222
223 return -EINVAL;
224}
225
226static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000227 int min_uV, int max_uV,
228 unsigned *selector)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530229{
230 int regulator_id, ret;
231 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
232
233 regulator_id = rdev_get_id(rdev);
234 if (regulator_id >= AB8500_NUM_REGULATORS)
235 return -EINVAL;
236
237 /* get the appropriate voltages within the range */
238 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
239 if (ret < 0) {
240 dev_err(rdev_get_dev(rdev),
241 "couldn't get best voltage for regulator\n");
242 return ret;
243 }
244
Mark Brown3a93f2a2010-11-10 14:38:29 +0000245 *selector = ret;
246
Sundar R IYERc789ca22010-07-13 21:48:56 +0530247 /* set the registers for the request */
Mattias Wallin47c16972010-09-10 17:47:56 +0200248 ret = abx500_mask_and_set_register_interruptible(info->dev,
249 info->voltage_bank, info->voltage_reg,
250 info->voltage_mask, (u8)ret);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530251 if (ret < 0)
252 dev_err(rdev_get_dev(rdev),
253 "couldn't set voltage reg for regulator\n");
254
255 return ret;
256}
257
258static struct regulator_ops ab8500_regulator_ops = {
259 .enable = ab8500_regulator_enable,
260 .disable = ab8500_regulator_disable,
261 .is_enabled = ab8500_regulator_is_enabled,
262 .get_voltage = ab8500_regulator_get_voltage,
263 .set_voltage = ab8500_regulator_set_voltage,
264 .list_voltage = ab8500_list_voltage,
265};
266
267static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
268{
269 int regulator_id;
270 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
271
272 regulator_id = rdev_get_id(rdev);
273 if (regulator_id >= AB8500_NUM_REGULATORS)
274 return -EINVAL;
275
276 return info->fixed_uV;
277}
278
279static struct regulator_ops ab8500_ldo_fixed_ops = {
280 .enable = ab8500_regulator_enable,
281 .disable = ab8500_regulator_disable,
282 .is_enabled = ab8500_regulator_is_enabled,
283 .get_voltage = ab8500_fixed_get_voltage,
284 .list_voltage = ab8500_list_voltage,
285};
286
Mattias Wallin47c16972010-09-10 17:47:56 +0200287#define AB8500_LDO(_id, min, max, bank, reg, reg_mask, \
288 reg_enable, volt_bank, volt_reg, volt_mask, \
289 voltages, len_volts) \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530290{ \
291 .desc = { \
292 .name = "LDO-" #_id, \
293 .ops = &ab8500_regulator_ops, \
294 .type = REGULATOR_VOLTAGE, \
295 .id = AB8500_LDO_##_id, \
296 .owner = THIS_MODULE, \
297 }, \
298 .min_uV = (min) * 1000, \
299 .max_uV = (max) * 1000, \
Mattias Wallin47c16972010-09-10 17:47:56 +0200300 .update_bank = bank, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530301 .update_reg = reg, \
302 .mask = reg_mask, \
303 .enable = reg_enable, \
Mattias Wallin47c16972010-09-10 17:47:56 +0200304 .voltage_bank = volt_bank, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530305 .voltage_reg = volt_reg, \
306 .voltage_mask = volt_mask, \
307 .supported_voltages = voltages, \
308 .voltages_len = len_volts, \
309 .fixed_uV = 0, \
310}
311
Mattias Wallin47c16972010-09-10 17:47:56 +0200312#define AB8500_FIXED_LDO(_id, fixed, bank, reg, \
313 reg_mask, reg_enable) \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530314{ \
315 .desc = { \
316 .name = "LDO-" #_id, \
317 .ops = &ab8500_ldo_fixed_ops, \
318 .type = REGULATOR_VOLTAGE, \
319 .id = AB8500_LDO_##_id, \
320 .owner = THIS_MODULE, \
321 }, \
322 .fixed_uV = fixed * 1000, \
Mattias Wallin47c16972010-09-10 17:47:56 +0200323 .update_bank = bank, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530324 .update_reg = reg, \
325 .mask = reg_mask, \
326 .enable = reg_enable, \
327}
328
329static struct ab8500_regulator_info ab8500_regulator_info[] = {
330 /*
331 * Variable Voltage LDOs
Mattias Wallin47c16972010-09-10 17:47:56 +0200332 * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
333 * volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
334 * num supported volts
Sundar R IYERc789ca22010-07-13 21:48:56 +0530335 */
Mattias Wallin47c16972010-09-10 17:47:56 +0200336 AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530337 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
Mattias Wallin47c16972010-09-10 17:47:56 +0200338 AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530339 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
Bengt Jonsson2b751512010-12-10 11:08:43 +0100340 AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0x7,
341 ldo_vaux3_voltages, ARRAY_SIZE(ldo_vaux3_voltages)),
Bengt Jonsson65e03ed2010-12-10 11:08:41 +0100342 AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x44, 0x4, 0x03, 0x80, 0x38,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530343 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
344
345 /*
346 * Fixed Voltage LDOs
Mattias Wallin47c16972010-09-10 17:47:56 +0200347 * name, o/p uV, ctrl bank, ctrl reg, enable, disable
Sundar R IYERc789ca22010-07-13 21:48:56 +0530348 */
Bengt Jonsson65e03ed2010-12-10 11:08:41 +0100349 AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x82, 0x2),
Mattias Wallin47c16972010-09-10 17:47:56 +0200350 AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x2, 0x2),
Bengt Jonsson65e03ed2010-12-10 11:08:41 +0100351 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
352 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
353 AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x04, 0x04),
354 AB8500_FIXED_LDO(ANA, 1200, 0x04, 0x06, 0xc, 0x4),
Sundar R IYERc789ca22010-07-13 21:48:56 +0530355};
356
Sundar R IYERc789ca22010-07-13 21:48:56 +0530357static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
358{
359 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
Dan Carpenteraf54dec2010-08-14 11:03:16 +0200360 struct ab8500_platform_data *pdata;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530361 int i, err;
362
363 if (!ab8500) {
364 dev_err(&pdev->dev, "null mfd parent\n");
365 return -EINVAL;
366 }
Dan Carpenteraf54dec2010-08-14 11:03:16 +0200367 pdata = dev_get_platdata(ab8500->dev);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530368
Bengt Jonssoncb189b02010-12-10 11:08:40 +0100369 /* make sure the platform data has the correct size */
370 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
371 dev_err(&pdev->dev, "platform configuration error\n");
372 return -EINVAL;
373 }
374
Sundar R IYERc789ca22010-07-13 21:48:56 +0530375 /* register all regulators */
376 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
377 struct ab8500_regulator_info *info = NULL;
378
379 /* assign per-regulator data */
380 info = &ab8500_regulator_info[i];
381 info->dev = &pdev->dev;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530382
Bengt Jonsson2b751512010-12-10 11:08:43 +0100383 /* fix for hardware before ab8500v2.0 */
384 if (abx500_get_chip_id(info->dev) < 0x20) {
385 if (info->desc.id == AB8500_LDO_AUX3) {
386 info->desc.n_voltages =
387 ARRAY_SIZE(ldo_vauxn_voltages);
388 info->supported_voltages = ldo_vauxn_voltages;
389 info->voltages_len =
390 ARRAY_SIZE(ldo_vauxn_voltages);
391 info->voltage_mask = 0xf;
392 }
393 }
394
395 /* register regulator with framework */
Sundar R IYERc789ca22010-07-13 21:48:56 +0530396 info->regulator = regulator_register(&info->desc, &pdev->dev,
Bengt Jonssoncb189b02010-12-10 11:08:40 +0100397 &pdata->regulator[i], info);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530398 if (IS_ERR(info->regulator)) {
399 err = PTR_ERR(info->regulator);
400 dev_err(&pdev->dev, "failed to register regulator %s\n",
401 info->desc.name);
402 /* when we fail, un-register all earlier regulators */
Axel Lind4876a32010-08-14 21:44:04 +0800403 while (--i >= 0) {
Sundar R IYERc789ca22010-07-13 21:48:56 +0530404 info = &ab8500_regulator_info[i];
405 regulator_unregister(info->regulator);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530406 }
407 return err;
408 }
409 }
410
411 return 0;
412}
413
414static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
415{
416 int i;
417
418 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
419 struct ab8500_regulator_info *info = NULL;
420 info = &ab8500_regulator_info[i];
421 regulator_unregister(info->regulator);
422 }
423
424 return 0;
425}
426
427static struct platform_driver ab8500_regulator_driver = {
428 .probe = ab8500_regulator_probe,
429 .remove = __devexit_p(ab8500_regulator_remove),
430 .driver = {
431 .name = "ab8500-regulator",
432 .owner = THIS_MODULE,
433 },
434};
435
436static int __init ab8500_regulator_init(void)
437{
438 int ret;
439
440 ret = platform_driver_register(&ab8500_regulator_driver);
441 if (ret != 0)
442 pr_err("Failed to register ab8500 regulator: %d\n", ret);
443
444 return ret;
445}
446subsys_initcall(ab8500_regulator_init);
447
448static void __exit ab8500_regulator_exit(void)
449{
450 platform_driver_unregister(&ab8500_regulator_driver);
451}
452module_exit(ab8500_regulator_exit);
453
454MODULE_LICENSE("GPL v2");
455MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
456MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
457MODULE_ALIAS("platform:ab8500-regulator");