blob: 51ff569384df9e99aea1e0a06de9a0535f4ed9f0 [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
84static const int ldo_vintcore_voltages[] = {
85 1200000,
86 1225000,
87 1250000,
88 1275000,
89 1300000,
90 1325000,
91 1350000,
92};
93
94static int ab8500_regulator_enable(struct regulator_dev *rdev)
95{
96 int regulator_id, ret;
97 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
98
99 regulator_id = rdev_get_id(rdev);
100 if (regulator_id >= AB8500_NUM_REGULATORS)
101 return -EINVAL;
102
Mattias Wallin47c16972010-09-10 17:47:56 +0200103 ret = abx500_mask_and_set_register_interruptible(info->dev,
104 info->update_bank, info->update_reg, info->mask, info->enable);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530105 if (ret < 0)
106 dev_err(rdev_get_dev(rdev),
107 "couldn't set enable bits for regulator\n");
108 return ret;
109}
110
111static int ab8500_regulator_disable(struct regulator_dev *rdev)
112{
113 int regulator_id, ret;
114 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
115
116 regulator_id = rdev_get_id(rdev);
117 if (regulator_id >= AB8500_NUM_REGULATORS)
118 return -EINVAL;
119
Mattias Wallin47c16972010-09-10 17:47:56 +0200120 ret = abx500_mask_and_set_register_interruptible(info->dev,
121 info->update_bank, info->update_reg, info->mask, 0x0);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530122 if (ret < 0)
123 dev_err(rdev_get_dev(rdev),
124 "couldn't set disable bits for regulator\n");
125 return ret;
126}
127
128static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
129{
130 int regulator_id, ret;
131 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
Mattias Wallin47c16972010-09-10 17:47:56 +0200132 u8 value;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530133
134 regulator_id = rdev_get_id(rdev);
135 if (regulator_id >= AB8500_NUM_REGULATORS)
136 return -EINVAL;
137
Mattias Wallin47c16972010-09-10 17:47:56 +0200138 ret = abx500_get_register_interruptible(info->dev,
139 info->update_bank, info->update_reg, &value);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530140 if (ret < 0) {
141 dev_err(rdev_get_dev(rdev),
142 "couldn't read 0x%x register\n", info->update_reg);
143 return ret;
144 }
145
Mattias Wallin47c16972010-09-10 17:47:56 +0200146 if (value & info->mask)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530147 return true;
148 else
149 return false;
150}
151
152static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
153{
154 int regulator_id;
155 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
156
157 regulator_id = rdev_get_id(rdev);
158 if (regulator_id >= AB8500_NUM_REGULATORS)
159 return -EINVAL;
160
161 /* return the uV for the fixed regulators */
162 if (info->fixed_uV)
163 return info->fixed_uV;
164
Axel Lin49990e62010-09-04 23:06:41 +0800165 if (selector >= info->voltages_len)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530166 return -EINVAL;
167
168 return info->supported_voltages[selector];
169}
170
171static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
172{
Mattias Wallin47c16972010-09-10 17:47:56 +0200173 int regulator_id, ret;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530174 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
Mattias Wallin47c16972010-09-10 17:47:56 +0200175 u8 value;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530176
177 regulator_id = rdev_get_id(rdev);
178 if (regulator_id >= AB8500_NUM_REGULATORS)
179 return -EINVAL;
180
Mattias Wallin47c16972010-09-10 17:47:56 +0200181 ret = abx500_get_register_interruptible(info->dev, info->voltage_bank,
182 info->voltage_reg, &value);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530183 if (ret < 0) {
184 dev_err(rdev_get_dev(rdev),
185 "couldn't read voltage reg for regulator\n");
186 return ret;
187 }
188
189 /* vintcore has a different layout */
Mattias Wallin47c16972010-09-10 17:47:56 +0200190 value &= info->voltage_mask;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530191 if (regulator_id == AB8500_LDO_INTCORE)
Mattias Wallin47c16972010-09-10 17:47:56 +0200192 ret = info->supported_voltages[value >> 0x3];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530193 else
Mattias Wallin47c16972010-09-10 17:47:56 +0200194 ret = info->supported_voltages[value];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530195
196 return ret;
197}
198
199static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
200 int min_uV, int max_uV)
201{
202 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
203 int i;
204
205 /* check the supported voltage */
206 for (i = 0; i < info->voltages_len; i++) {
207 if ((info->supported_voltages[i] >= min_uV) &&
208 (info->supported_voltages[i] <= max_uV))
209 return i;
210 }
211
212 return -EINVAL;
213}
214
215static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000216 int min_uV, int max_uV,
217 unsigned *selector)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530218{
219 int regulator_id, ret;
220 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
221
222 regulator_id = rdev_get_id(rdev);
223 if (regulator_id >= AB8500_NUM_REGULATORS)
224 return -EINVAL;
225
226 /* get the appropriate voltages within the range */
227 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
228 if (ret < 0) {
229 dev_err(rdev_get_dev(rdev),
230 "couldn't get best voltage for regulator\n");
231 return ret;
232 }
233
Mark Brown3a93f2a2010-11-10 14:38:29 +0000234 *selector = ret;
235
Sundar R IYERc789ca22010-07-13 21:48:56 +0530236 /* set the registers for the request */
Mattias Wallin47c16972010-09-10 17:47:56 +0200237 ret = abx500_mask_and_set_register_interruptible(info->dev,
238 info->voltage_bank, info->voltage_reg,
239 info->voltage_mask, (u8)ret);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530240 if (ret < 0)
241 dev_err(rdev_get_dev(rdev),
242 "couldn't set voltage reg for regulator\n");
243
244 return ret;
245}
246
247static struct regulator_ops ab8500_regulator_ops = {
248 .enable = ab8500_regulator_enable,
249 .disable = ab8500_regulator_disable,
250 .is_enabled = ab8500_regulator_is_enabled,
251 .get_voltage = ab8500_regulator_get_voltage,
252 .set_voltage = ab8500_regulator_set_voltage,
253 .list_voltage = ab8500_list_voltage,
254};
255
256static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
257{
258 int regulator_id;
259 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
260
261 regulator_id = rdev_get_id(rdev);
262 if (regulator_id >= AB8500_NUM_REGULATORS)
263 return -EINVAL;
264
265 return info->fixed_uV;
266}
267
268static struct regulator_ops ab8500_ldo_fixed_ops = {
269 .enable = ab8500_regulator_enable,
270 .disable = ab8500_regulator_disable,
271 .is_enabled = ab8500_regulator_is_enabled,
272 .get_voltage = ab8500_fixed_get_voltage,
273 .list_voltage = ab8500_list_voltage,
274};
275
Mattias Wallin47c16972010-09-10 17:47:56 +0200276#define AB8500_LDO(_id, min, max, bank, reg, reg_mask, \
277 reg_enable, volt_bank, volt_reg, volt_mask, \
278 voltages, len_volts) \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530279{ \
280 .desc = { \
281 .name = "LDO-" #_id, \
282 .ops = &ab8500_regulator_ops, \
283 .type = REGULATOR_VOLTAGE, \
284 .id = AB8500_LDO_##_id, \
285 .owner = THIS_MODULE, \
286 }, \
287 .min_uV = (min) * 1000, \
288 .max_uV = (max) * 1000, \
Mattias Wallin47c16972010-09-10 17:47:56 +0200289 .update_bank = bank, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530290 .update_reg = reg, \
291 .mask = reg_mask, \
292 .enable = reg_enable, \
Mattias Wallin47c16972010-09-10 17:47:56 +0200293 .voltage_bank = volt_bank, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530294 .voltage_reg = volt_reg, \
295 .voltage_mask = volt_mask, \
296 .supported_voltages = voltages, \
297 .voltages_len = len_volts, \
298 .fixed_uV = 0, \
299}
300
Mattias Wallin47c16972010-09-10 17:47:56 +0200301#define AB8500_FIXED_LDO(_id, fixed, bank, reg, \
302 reg_mask, reg_enable) \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530303{ \
304 .desc = { \
305 .name = "LDO-" #_id, \
306 .ops = &ab8500_ldo_fixed_ops, \
307 .type = REGULATOR_VOLTAGE, \
308 .id = AB8500_LDO_##_id, \
309 .owner = THIS_MODULE, \
310 }, \
311 .fixed_uV = fixed * 1000, \
Mattias Wallin47c16972010-09-10 17:47:56 +0200312 .update_bank = bank, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530313 .update_reg = reg, \
314 .mask = reg_mask, \
315 .enable = reg_enable, \
316}
317
318static struct ab8500_regulator_info ab8500_regulator_info[] = {
319 /*
320 * Variable Voltage LDOs
Mattias Wallin47c16972010-09-10 17:47:56 +0200321 * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
322 * volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
323 * num supported volts
Sundar R IYERc789ca22010-07-13 21:48:56 +0530324 */
Mattias Wallin47c16972010-09-10 17:47:56 +0200325 AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530326 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
Mattias Wallin47c16972010-09-10 17:47:56 +0200327 AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530328 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
Mattias Wallin47c16972010-09-10 17:47:56 +0200329 AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530330 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
Bengt Jonsson65e03ed2010-12-10 11:08:41 +0100331 AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x44, 0x4, 0x03, 0x80, 0x38,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530332 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
333
334 /*
335 * Fixed Voltage LDOs
Mattias Wallin47c16972010-09-10 17:47:56 +0200336 * name, o/p uV, ctrl bank, ctrl reg, enable, disable
Sundar R IYERc789ca22010-07-13 21:48:56 +0530337 */
Bengt Jonsson65e03ed2010-12-10 11:08:41 +0100338 AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x82, 0x2),
Mattias Wallin47c16972010-09-10 17:47:56 +0200339 AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x2, 0x2),
Bengt Jonsson65e03ed2010-12-10 11:08:41 +0100340 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
341 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
342 AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x04, 0x04),
343 AB8500_FIXED_LDO(ANA, 1200, 0x04, 0x06, 0xc, 0x4),
Sundar R IYERc789ca22010-07-13 21:48:56 +0530344};
345
Sundar R IYERc789ca22010-07-13 21:48:56 +0530346static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
347{
348 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
Dan Carpenteraf54dec2010-08-14 11:03:16 +0200349 struct ab8500_platform_data *pdata;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530350 int i, err;
351
352 if (!ab8500) {
353 dev_err(&pdev->dev, "null mfd parent\n");
354 return -EINVAL;
355 }
Dan Carpenteraf54dec2010-08-14 11:03:16 +0200356 pdata = dev_get_platdata(ab8500->dev);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530357
Bengt Jonssoncb189b02010-12-10 11:08:40 +0100358 /* make sure the platform data has the correct size */
359 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
360 dev_err(&pdev->dev, "platform configuration error\n");
361 return -EINVAL;
362 }
363
Sundar R IYERc789ca22010-07-13 21:48:56 +0530364 /* register all regulators */
365 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
366 struct ab8500_regulator_info *info = NULL;
367
368 /* assign per-regulator data */
369 info = &ab8500_regulator_info[i];
370 info->dev = &pdev->dev;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530371
372 info->regulator = regulator_register(&info->desc, &pdev->dev,
Bengt Jonssoncb189b02010-12-10 11:08:40 +0100373 &pdata->regulator[i], info);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530374 if (IS_ERR(info->regulator)) {
375 err = PTR_ERR(info->regulator);
376 dev_err(&pdev->dev, "failed to register regulator %s\n",
377 info->desc.name);
378 /* when we fail, un-register all earlier regulators */
Axel Lind4876a32010-08-14 21:44:04 +0800379 while (--i >= 0) {
Sundar R IYERc789ca22010-07-13 21:48:56 +0530380 info = &ab8500_regulator_info[i];
381 regulator_unregister(info->regulator);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530382 }
383 return err;
384 }
385 }
386
387 return 0;
388}
389
390static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
391{
392 int i;
393
394 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
395 struct ab8500_regulator_info *info = NULL;
396 info = &ab8500_regulator_info[i];
397 regulator_unregister(info->regulator);
398 }
399
400 return 0;
401}
402
403static struct platform_driver ab8500_regulator_driver = {
404 .probe = ab8500_regulator_probe,
405 .remove = __devexit_p(ab8500_regulator_remove),
406 .driver = {
407 .name = "ab8500-regulator",
408 .owner = THIS_MODULE,
409 },
410};
411
412static int __init ab8500_regulator_init(void)
413{
414 int ret;
415
416 ret = platform_driver_register(&ab8500_regulator_driver);
417 if (ret != 0)
418 pr_err("Failed to register ab8500 regulator: %d\n", ret);
419
420 return ret;
421}
422subsys_initcall(ab8500_regulator_init);
423
424static void __exit ab8500_regulator_exit(void)
425{
426 platform_driver_unregister(&ab8500_regulator_driver);
427}
428module_exit(ab8500_regulator_exit);
429
430MODULE_LICENSE("GPL v2");
431MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
432MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
433MODULE_ALIAS("platform:ab8500-regulator");