regulator: Report actual configured voltage to set_voltage()
[deliverable/linux.git] / drivers / regulator / ab8500.c
CommitLineData
c789ca20
SI
1/*
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>
47c16975 24#include <linux/mfd/abx500.h>
c789ca20
SI
25#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
32 * @ab8500: ab8500 parent
33 * @regulator_dev: regulator device
34 * @max_uV: maximum voltage (for variable voltage supplies)
35 * @min_uV: minimum voltage (for variable voltage supplies)
36 * @fixed_uV: typical voltage (for fixed voltage supplies)
47c16975 37 * @update_bank: bank to control on/off
c789ca20
SI
38 * @update_reg: register to control on/off
39 * @mask: mask to enable/disable regulator
40 * @enable: bits to enable the regulator in normal(high power) mode
47c16975 41 * @voltage_bank: bank to control regulator voltage
c789ca20
SI
42 * @voltage_reg: register to control regulator voltage
43 * @voltage_mask: mask to control regulator voltage
44 * @supported_voltages: supported voltage table
45 * @voltages_len: number of supported voltages for the regulator
46 */
47struct ab8500_regulator_info {
48 struct device *dev;
49 struct regulator_desc desc;
50 struct ab8500 *ab8500;
51 struct regulator_dev *regulator;
52 int max_uV;
53 int min_uV;
54 int fixed_uV;
47c16975
MW
55 u8 update_bank;
56 u8 update_reg;
57 u8 mask;
58 u8 enable;
59 u8 voltage_bank;
60 u8 voltage_reg;
61 u8 voltage_mask;
c789ca20
SI
62 int const *supported_voltages;
63 int voltages_len;
64};
65
66/* voltage tables for the vauxn/vintcore supplies */
67static const int ldo_vauxn_voltages[] = {
68 1100000,
69 1200000,
70 1300000,
71 1400000,
72 1500000,
73 1800000,
74 1850000,
75 1900000,
76 2500000,
77 2650000,
78 2700000,
79 2750000,
80 2800000,
81 2900000,
82 3000000,
83 3300000,
84};
85
86static const int ldo_vintcore_voltages[] = {
87 1200000,
88 1225000,
89 1250000,
90 1275000,
91 1300000,
92 1325000,
93 1350000,
94};
95
96static int ab8500_regulator_enable(struct regulator_dev *rdev)
97{
98 int regulator_id, ret;
99 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
100
101 regulator_id = rdev_get_id(rdev);
102 if (regulator_id >= AB8500_NUM_REGULATORS)
103 return -EINVAL;
104
47c16975
MW
105 ret = abx500_mask_and_set_register_interruptible(info->dev,
106 info->update_bank, info->update_reg, info->mask, info->enable);
c789ca20
SI
107 if (ret < 0)
108 dev_err(rdev_get_dev(rdev),
109 "couldn't set enable bits for regulator\n");
110 return ret;
111}
112
113static int ab8500_regulator_disable(struct regulator_dev *rdev)
114{
115 int regulator_id, ret;
116 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
117
118 regulator_id = rdev_get_id(rdev);
119 if (regulator_id >= AB8500_NUM_REGULATORS)
120 return -EINVAL;
121
47c16975
MW
122 ret = abx500_mask_and_set_register_interruptible(info->dev,
123 info->update_bank, info->update_reg, info->mask, 0x0);
c789ca20
SI
124 if (ret < 0)
125 dev_err(rdev_get_dev(rdev),
126 "couldn't set disable bits for regulator\n");
127 return ret;
128}
129
130static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
131{
132 int regulator_id, ret;
133 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
47c16975 134 u8 value;
c789ca20
SI
135
136 regulator_id = rdev_get_id(rdev);
137 if (regulator_id >= AB8500_NUM_REGULATORS)
138 return -EINVAL;
139
47c16975
MW
140 ret = abx500_get_register_interruptible(info->dev,
141 info->update_bank, info->update_reg, &value);
c789ca20
SI
142 if (ret < 0) {
143 dev_err(rdev_get_dev(rdev),
144 "couldn't read 0x%x register\n", info->update_reg);
145 return ret;
146 }
147
47c16975 148 if (value & info->mask)
c789ca20
SI
149 return true;
150 else
151 return false;
152}
153
154static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
155{
156 int regulator_id;
157 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
158
159 regulator_id = rdev_get_id(rdev);
160 if (regulator_id >= AB8500_NUM_REGULATORS)
161 return -EINVAL;
162
163 /* return the uV for the fixed regulators */
164 if (info->fixed_uV)
165 return info->fixed_uV;
166
49990e6e 167 if (selector >= info->voltages_len)
c789ca20
SI
168 return -EINVAL;
169
170 return info->supported_voltages[selector];
171}
172
173static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
174{
47c16975 175 int regulator_id, ret;
c789ca20 176 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
47c16975 177 u8 value;
c789ca20
SI
178
179 regulator_id = rdev_get_id(rdev);
180 if (regulator_id >= AB8500_NUM_REGULATORS)
181 return -EINVAL;
182
47c16975
MW
183 ret = abx500_get_register_interruptible(info->dev, info->voltage_bank,
184 info->voltage_reg, &value);
c789ca20
SI
185 if (ret < 0) {
186 dev_err(rdev_get_dev(rdev),
187 "couldn't read voltage reg for regulator\n");
188 return ret;
189 }
190
191 /* vintcore has a different layout */
47c16975 192 value &= info->voltage_mask;
c789ca20 193 if (regulator_id == AB8500_LDO_INTCORE)
47c16975 194 ret = info->supported_voltages[value >> 0x3];
c789ca20 195 else
47c16975 196 ret = info->supported_voltages[value];
c789ca20
SI
197
198 return ret;
199}
200
201static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
202 int min_uV, int max_uV)
203{
204 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
205 int i;
206
207 /* check the supported voltage */
208 for (i = 0; i < info->voltages_len; i++) {
209 if ((info->supported_voltages[i] >= min_uV) &&
210 (info->supported_voltages[i] <= max_uV))
211 return i;
212 }
213
214 return -EINVAL;
215}
216
217static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
3a93f2a9
MB
218 int min_uV, int max_uV,
219 unsigned *selector)
c789ca20
SI
220{
221 int regulator_id, ret;
222 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
223
224 regulator_id = rdev_get_id(rdev);
225 if (regulator_id >= AB8500_NUM_REGULATORS)
226 return -EINVAL;
227
228 /* get the appropriate voltages within the range */
229 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
230 if (ret < 0) {
231 dev_err(rdev_get_dev(rdev),
232 "couldn't get best voltage for regulator\n");
233 return ret;
234 }
235
3a93f2a9
MB
236 *selector = ret;
237
c789ca20 238 /* set the registers for the request */
47c16975
MW
239 ret = abx500_mask_and_set_register_interruptible(info->dev,
240 info->voltage_bank, info->voltage_reg,
241 info->voltage_mask, (u8)ret);
c789ca20
SI
242 if (ret < 0)
243 dev_err(rdev_get_dev(rdev),
244 "couldn't set voltage reg for regulator\n");
245
246 return ret;
247}
248
249static struct regulator_ops ab8500_regulator_ops = {
250 .enable = ab8500_regulator_enable,
251 .disable = ab8500_regulator_disable,
252 .is_enabled = ab8500_regulator_is_enabled,
253 .get_voltage = ab8500_regulator_get_voltage,
254 .set_voltage = ab8500_regulator_set_voltage,
255 .list_voltage = ab8500_list_voltage,
256};
257
258static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
259{
260 int regulator_id;
261 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
262
263 regulator_id = rdev_get_id(rdev);
264 if (regulator_id >= AB8500_NUM_REGULATORS)
265 return -EINVAL;
266
267 return info->fixed_uV;
268}
269
270static struct regulator_ops ab8500_ldo_fixed_ops = {
271 .enable = ab8500_regulator_enable,
272 .disable = ab8500_regulator_disable,
273 .is_enabled = ab8500_regulator_is_enabled,
274 .get_voltage = ab8500_fixed_get_voltage,
275 .list_voltage = ab8500_list_voltage,
276};
277
47c16975
MW
278#define AB8500_LDO(_id, min, max, bank, reg, reg_mask, \
279 reg_enable, volt_bank, volt_reg, volt_mask, \
280 voltages, len_volts) \
c789ca20
SI
281{ \
282 .desc = { \
283 .name = "LDO-" #_id, \
284 .ops = &ab8500_regulator_ops, \
285 .type = REGULATOR_VOLTAGE, \
286 .id = AB8500_LDO_##_id, \
287 .owner = THIS_MODULE, \
288 }, \
289 .min_uV = (min) * 1000, \
290 .max_uV = (max) * 1000, \
47c16975 291 .update_bank = bank, \
c789ca20
SI
292 .update_reg = reg, \
293 .mask = reg_mask, \
294 .enable = reg_enable, \
47c16975 295 .voltage_bank = volt_bank, \
c789ca20
SI
296 .voltage_reg = volt_reg, \
297 .voltage_mask = volt_mask, \
298 .supported_voltages = voltages, \
299 .voltages_len = len_volts, \
300 .fixed_uV = 0, \
301}
302
47c16975
MW
303#define AB8500_FIXED_LDO(_id, fixed, bank, reg, \
304 reg_mask, reg_enable) \
c789ca20
SI
305{ \
306 .desc = { \
307 .name = "LDO-" #_id, \
308 .ops = &ab8500_ldo_fixed_ops, \
309 .type = REGULATOR_VOLTAGE, \
310 .id = AB8500_LDO_##_id, \
311 .owner = THIS_MODULE, \
312 }, \
313 .fixed_uV = fixed * 1000, \
47c16975 314 .update_bank = bank, \
c789ca20
SI
315 .update_reg = reg, \
316 .mask = reg_mask, \
317 .enable = reg_enable, \
318}
319
320static struct ab8500_regulator_info ab8500_regulator_info[] = {
321 /*
322 * Variable Voltage LDOs
47c16975
MW
323 * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
324 * volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
325 * num supported volts
c789ca20 326 */
47c16975 327 AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
c789ca20 328 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
47c16975 329 AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
c789ca20 330 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
47c16975 331 AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
c789ca20 332 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
47c16975 333 AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x4, 0x4, 0x03, 0x80, 0x38,
c789ca20
SI
334 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
335
336 /*
337 * Fixed Voltage LDOs
47c16975 338 * name, o/p uV, ctrl bank, ctrl reg, enable, disable
c789ca20 339 */
47c16975
MW
340 AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x2, 0x2),
341 AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x2, 0x2),
342 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x4, 0x4),
343 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x8, 0x8),
344 AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x10, 0x10),
345 AB8500_FIXED_LDO(ANA, 1200, 0x03, 0x83, 0xc, 0x4),
c789ca20
SI
346};
347
348static inline struct ab8500_regulator_info *find_regulator_info(int id)
349{
350 struct ab8500_regulator_info *info;
351 int i;
352
353 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
354 info = &ab8500_regulator_info[i];
355 if (info->desc.id == id)
356 return info;
357 }
358 return NULL;
359}
360
361static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
362{
363 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
af54decd 364 struct ab8500_platform_data *pdata;
c789ca20
SI
365 int i, err;
366
367 if (!ab8500) {
368 dev_err(&pdev->dev, "null mfd parent\n");
369 return -EINVAL;
370 }
af54decd 371 pdata = dev_get_platdata(ab8500->dev);
c789ca20
SI
372
373 /* register all regulators */
374 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
375 struct ab8500_regulator_info *info = NULL;
376
377 /* assign per-regulator data */
378 info = &ab8500_regulator_info[i];
379 info->dev = &pdev->dev;
380 info->ab8500 = ab8500;
381
382 info->regulator = regulator_register(&info->desc, &pdev->dev,
383 pdata->regulator[i], info);
384 if (IS_ERR(info->regulator)) {
385 err = PTR_ERR(info->regulator);
386 dev_err(&pdev->dev, "failed to register regulator %s\n",
387 info->desc.name);
388 /* when we fail, un-register all earlier regulators */
d4876a3b 389 while (--i >= 0) {
c789ca20
SI
390 info = &ab8500_regulator_info[i];
391 regulator_unregister(info->regulator);
c789ca20
SI
392 }
393 return err;
394 }
395 }
396
397 return 0;
398}
399
400static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
401{
402 int i;
403
404 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
405 struct ab8500_regulator_info *info = NULL;
406 info = &ab8500_regulator_info[i];
407 regulator_unregister(info->regulator);
408 }
409
410 return 0;
411}
412
413static struct platform_driver ab8500_regulator_driver = {
414 .probe = ab8500_regulator_probe,
415 .remove = __devexit_p(ab8500_regulator_remove),
416 .driver = {
417 .name = "ab8500-regulator",
418 .owner = THIS_MODULE,
419 },
420};
421
422static int __init ab8500_regulator_init(void)
423{
424 int ret;
425
426 ret = platform_driver_register(&ab8500_regulator_driver);
427 if (ret != 0)
428 pr_err("Failed to register ab8500 regulator: %d\n", ret);
429
430 return ret;
431}
432subsys_initcall(ab8500_regulator_init);
433
434static void __exit ab8500_regulator_exit(void)
435{
436 platform_driver_unregister(&ab8500_regulator_driver);
437}
438module_exit(ab8500_regulator_exit);
439
440MODULE_LICENSE("GPL v2");
441MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
442MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
443MODULE_ALIAS("platform:ab8500-regulator");
This page took 0.068251 seconds and 5 git commands to generate.