]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/regulator/ab8500.c
ixgbe: move adapter into pci_dev driver data instead of netdev
[net-next-2.6.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,
218 int min_uV, int max_uV)
219{
220 int regulator_id, ret;
221 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
222
223 regulator_id = rdev_get_id(rdev);
224 if (regulator_id >= AB8500_NUM_REGULATORS)
225 return -EINVAL;
226
227 /* get the appropriate voltages within the range */
228 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
229 if (ret < 0) {
230 dev_err(rdev_get_dev(rdev),
231 "couldn't get best voltage for regulator\n");
232 return ret;
233 }
234
235 /* set the registers for the request */
47c16975
MW
236 ret = abx500_mask_and_set_register_interruptible(info->dev,
237 info->voltage_bank, info->voltage_reg,
238 info->voltage_mask, (u8)ret);
c789ca20
SI
239 if (ret < 0)
240 dev_err(rdev_get_dev(rdev),
241 "couldn't set voltage reg for regulator\n");
242
243 return ret;
244}
245
246static struct regulator_ops ab8500_regulator_ops = {
247 .enable = ab8500_regulator_enable,
248 .disable = ab8500_regulator_disable,
249 .is_enabled = ab8500_regulator_is_enabled,
250 .get_voltage = ab8500_regulator_get_voltage,
251 .set_voltage = ab8500_regulator_set_voltage,
252 .list_voltage = ab8500_list_voltage,
253};
254
255static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
256{
257 int regulator_id;
258 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
259
260 regulator_id = rdev_get_id(rdev);
261 if (regulator_id >= AB8500_NUM_REGULATORS)
262 return -EINVAL;
263
264 return info->fixed_uV;
265}
266
267static struct regulator_ops ab8500_ldo_fixed_ops = {
268 .enable = ab8500_regulator_enable,
269 .disable = ab8500_regulator_disable,
270 .is_enabled = ab8500_regulator_is_enabled,
271 .get_voltage = ab8500_fixed_get_voltage,
272 .list_voltage = ab8500_list_voltage,
273};
274
47c16975
MW
275#define AB8500_LDO(_id, min, max, bank, reg, reg_mask, \
276 reg_enable, volt_bank, volt_reg, volt_mask, \
277 voltages, len_volts) \
c789ca20
SI
278{ \
279 .desc = { \
280 .name = "LDO-" #_id, \
281 .ops = &ab8500_regulator_ops, \
282 .type = REGULATOR_VOLTAGE, \
283 .id = AB8500_LDO_##_id, \
284 .owner = THIS_MODULE, \
285 }, \
286 .min_uV = (min) * 1000, \
287 .max_uV = (max) * 1000, \
47c16975 288 .update_bank = bank, \
c789ca20
SI
289 .update_reg = reg, \
290 .mask = reg_mask, \
291 .enable = reg_enable, \
47c16975 292 .voltage_bank = volt_bank, \
c789ca20
SI
293 .voltage_reg = volt_reg, \
294 .voltage_mask = volt_mask, \
295 .supported_voltages = voltages, \
296 .voltages_len = len_volts, \
297 .fixed_uV = 0, \
298}
299
47c16975
MW
300#define AB8500_FIXED_LDO(_id, fixed, bank, reg, \
301 reg_mask, reg_enable) \
c789ca20
SI
302{ \
303 .desc = { \
304 .name = "LDO-" #_id, \
305 .ops = &ab8500_ldo_fixed_ops, \
306 .type = REGULATOR_VOLTAGE, \
307 .id = AB8500_LDO_##_id, \
308 .owner = THIS_MODULE, \
309 }, \
310 .fixed_uV = fixed * 1000, \
47c16975 311 .update_bank = bank, \
c789ca20
SI
312 .update_reg = reg, \
313 .mask = reg_mask, \
314 .enable = reg_enable, \
315}
316
317static struct ab8500_regulator_info ab8500_regulator_info[] = {
318 /*
319 * Variable Voltage LDOs
47c16975
MW
320 * name, min uV, max uV, ctrl bank, ctrl reg, reg mask, enable mask,
321 * volt ctrl bank, volt ctrl reg, volt ctrl mask, volt table,
322 * num supported volts
c789ca20 323 */
47c16975 324 AB8500_LDO(AUX1, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
c789ca20 325 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
47c16975 326 AB8500_LDO(AUX2, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
c789ca20 327 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
47c16975 328 AB8500_LDO(AUX3, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
c789ca20 329 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
47c16975 330 AB8500_LDO(INTCORE, 1100, 3300, 0x03, 0x80, 0x4, 0x4, 0x03, 0x80, 0x38,
c789ca20
SI
331 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
332
333 /*
334 * Fixed Voltage LDOs
47c16975 335 * name, o/p uV, ctrl bank, ctrl reg, enable, disable
c789ca20 336 */
47c16975
MW
337 AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x2, 0x2),
338 AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x2, 0x2),
339 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x4, 0x4),
340 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x8, 0x8),
341 AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x10, 0x10),
342 AB8500_FIXED_LDO(ANA, 1200, 0x03, 0x83, 0xc, 0x4),
c789ca20
SI
343};
344
345static inline struct ab8500_regulator_info *find_regulator_info(int id)
346{
347 struct ab8500_regulator_info *info;
348 int i;
349
350 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
351 info = &ab8500_regulator_info[i];
352 if (info->desc.id == id)
353 return info;
354 }
355 return NULL;
356}
357
358static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
359{
360 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
af54decd 361 struct ab8500_platform_data *pdata;
c789ca20
SI
362 int i, err;
363
364 if (!ab8500) {
365 dev_err(&pdev->dev, "null mfd parent\n");
366 return -EINVAL;
367 }
af54decd 368 pdata = dev_get_platdata(ab8500->dev);
c789ca20
SI
369
370 /* register all regulators */
371 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
372 struct ab8500_regulator_info *info = NULL;
373
374 /* assign per-regulator data */
375 info = &ab8500_regulator_info[i];
376 info->dev = &pdev->dev;
377 info->ab8500 = ab8500;
378
379 info->regulator = regulator_register(&info->desc, &pdev->dev,
380 pdata->regulator[i], info);
381 if (IS_ERR(info->regulator)) {
382 err = PTR_ERR(info->regulator);
383 dev_err(&pdev->dev, "failed to register regulator %s\n",
384 info->desc.name);
385 /* when we fail, un-register all earlier regulators */
d4876a3b 386 while (--i >= 0) {
c789ca20
SI
387 info = &ab8500_regulator_info[i];
388 regulator_unregister(info->regulator);
c789ca20
SI
389 }
390 return err;
391 }
392 }
393
394 return 0;
395}
396
397static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
398{
399 int i;
400
401 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
402 struct ab8500_regulator_info *info = NULL;
403 info = &ab8500_regulator_info[i];
404 regulator_unregister(info->regulator);
405 }
406
407 return 0;
408}
409
410static struct platform_driver ab8500_regulator_driver = {
411 .probe = ab8500_regulator_probe,
412 .remove = __devexit_p(ab8500_regulator_remove),
413 .driver = {
414 .name = "ab8500-regulator",
415 .owner = THIS_MODULE,
416 },
417};
418
419static int __init ab8500_regulator_init(void)
420{
421 int ret;
422
423 ret = platform_driver_register(&ab8500_regulator_driver);
424 if (ret != 0)
425 pr_err("Failed to register ab8500 regulator: %d\n", ret);
426
427 return ret;
428}
429subsys_initcall(ab8500_regulator_init);
430
431static void __exit ab8500_regulator_exit(void)
432{
433 platform_driver_unregister(&ab8500_regulator_driver);
434}
435module_exit(ab8500_regulator_exit);
436
437MODULE_LICENSE("GPL v2");
438MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
439MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
440MODULE_ALIAS("platform:ab8500-regulator");