blob: ccb0bfd71f232edae0a2590bf64a5d9e5178629a [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 *
Bengt Jonssone1159e62010-12-10 11:08:44 +01006 * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7 * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
Sundar R IYERc789ca22010-07-13 21:48:56 +05308 *
9 * AB8500 peripheral regulators
10 *
Bengt Jonssone1159e62010-12-10 11:08:44 +010011 * AB8500 supports the following regulators:
12 * VAUX1/2/3, VINTCORE, VTVOUT, VAUDIO, VAMIC1/2, VDMIC, VANA
Sundar R IYERc789ca22010-07-13 21:48:56 +053013 */
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/err.h>
17#include <linux/platform_device.h>
18#include <linux/mfd/ab8500.h>
Mattias Wallin47c16972010-09-10 17:47:56 +020019#include <linux/mfd/abx500.h>
Sundar R IYERc789ca22010-07-13 21:48:56 +053020#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/regulator/ab8500.h>
23
24/**
25 * struct ab8500_regulator_info - ab8500 regulator information
Bengt Jonssone1159e62010-12-10 11:08:44 +010026 * @dev: device pointer
Sundar R IYERc789ca22010-07-13 21:48:56 +053027 * @desc: regulator description
Sundar R IYERc789ca22010-07-13 21:48:56 +053028 * @regulator_dev: regulator device
29 * @max_uV: maximum voltage (for variable voltage supplies)
30 * @min_uV: minimum voltage (for variable voltage supplies)
31 * @fixed_uV: typical voltage (for fixed voltage supplies)
Mattias Wallin47c16972010-09-10 17:47:56 +020032 * @update_bank: bank to control on/off
Sundar R IYERc789ca22010-07-13 21:48:56 +053033 * @update_reg: register to control on/off
Bengt Jonssone1159e62010-12-10 11:08:44 +010034 * @update_mask: mask to enable/disable regulator
35 * @update_val_enable: bits to enable the regulator in normal (high power) mode
Mattias Wallin47c16972010-09-10 17:47:56 +020036 * @voltage_bank: bank to control regulator voltage
Sundar R IYERc789ca22010-07-13 21:48:56 +053037 * @voltage_reg: register to control regulator voltage
38 * @voltage_mask: mask to control regulator voltage
Bengt Jonssone1159e62010-12-10 11:08:44 +010039 * @voltages: supported voltage table
Sundar R IYERc789ca22010-07-13 21:48:56 +053040 * @voltages_len: number of supported voltages for the regulator
41 */
42struct ab8500_regulator_info {
43 struct device *dev;
44 struct regulator_desc desc;
Sundar R IYERc789ca22010-07-13 21:48:56 +053045 struct regulator_dev *regulator;
46 int max_uV;
47 int min_uV;
48 int fixed_uV;
Mattias Wallin47c16972010-09-10 17:47:56 +020049 u8 update_bank;
50 u8 update_reg;
Bengt Jonssone1159e62010-12-10 11:08:44 +010051 u8 update_mask;
52 u8 update_val_enable;
Mattias Wallin47c16972010-09-10 17:47:56 +020053 u8 voltage_bank;
54 u8 voltage_reg;
55 u8 voltage_mask;
Bengt Jonssone1159e62010-12-10 11:08:44 +010056 int const *voltages;
Sundar R IYERc789ca22010-07-13 21:48:56 +053057 int voltages_len;
58};
59
60/* voltage tables for the vauxn/vintcore supplies */
61static const int ldo_vauxn_voltages[] = {
62 1100000,
63 1200000,
64 1300000,
65 1400000,
66 1500000,
67 1800000,
68 1850000,
69 1900000,
70 2500000,
71 2650000,
72 2700000,
73 2750000,
74 2800000,
75 2900000,
76 3000000,
77 3300000,
78};
79
Bengt Jonsson2b751512010-12-10 11:08:43 +010080static const int ldo_vaux3_voltages[] = {
81 1200000,
82 1500000,
83 1800000,
84 2100000,
85 2500000,
86 2750000,
87 2790000,
88 2910000,
89};
90
Sundar R IYERc789ca22010-07-13 21:48:56 +053091static const int ldo_vintcore_voltages[] = {
92 1200000,
93 1225000,
94 1250000,
95 1275000,
96 1300000,
97 1325000,
98 1350000,
99};
100
101static int ab8500_regulator_enable(struct regulator_dev *rdev)
102{
103 int regulator_id, ret;
104 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
105
106 regulator_id = rdev_get_id(rdev);
107 if (regulator_id >= AB8500_NUM_REGULATORS)
108 return -EINVAL;
109
Mattias Wallin47c16972010-09-10 17:47:56 +0200110 ret = abx500_mask_and_set_register_interruptible(info->dev,
Bengt Jonssone1159e62010-12-10 11:08:44 +0100111 info->update_bank, info->update_reg,
112 info->update_mask, info->update_val_enable);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530113 if (ret < 0)
114 dev_err(rdev_get_dev(rdev),
115 "couldn't set enable bits for regulator\n");
116 return ret;
117}
118
119static int ab8500_regulator_disable(struct regulator_dev *rdev)
120{
121 int regulator_id, ret;
122 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
123
124 regulator_id = rdev_get_id(rdev);
125 if (regulator_id >= AB8500_NUM_REGULATORS)
126 return -EINVAL;
127
Mattias Wallin47c16972010-09-10 17:47:56 +0200128 ret = abx500_mask_and_set_register_interruptible(info->dev,
Bengt Jonssone1159e62010-12-10 11:08:44 +0100129 info->update_bank, info->update_reg,
130 info->update_mask, 0x0);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530131 if (ret < 0)
132 dev_err(rdev_get_dev(rdev),
133 "couldn't set disable bits for regulator\n");
134 return ret;
135}
136
137static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
138{
139 int regulator_id, ret;
140 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
Mattias Wallin47c16972010-09-10 17:47:56 +0200141 u8 value;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530142
143 regulator_id = rdev_get_id(rdev);
144 if (regulator_id >= AB8500_NUM_REGULATORS)
145 return -EINVAL;
146
Mattias Wallin47c16972010-09-10 17:47:56 +0200147 ret = abx500_get_register_interruptible(info->dev,
148 info->update_bank, info->update_reg, &value);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530149 if (ret < 0) {
150 dev_err(rdev_get_dev(rdev),
151 "couldn't read 0x%x register\n", info->update_reg);
152 return ret;
153 }
154
Bengt Jonssone1159e62010-12-10 11:08:44 +0100155 if (value & info->update_mask)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530156 return true;
157 else
158 return false;
159}
160
161static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
162{
163 int regulator_id;
164 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
165
166 regulator_id = rdev_get_id(rdev);
167 if (regulator_id >= AB8500_NUM_REGULATORS)
168 return -EINVAL;
169
170 /* return the uV for the fixed regulators */
171 if (info->fixed_uV)
172 return info->fixed_uV;
173
Axel Lin49990e62010-09-04 23:06:41 +0800174 if (selector >= info->voltages_len)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530175 return -EINVAL;
176
Bengt Jonssone1159e62010-12-10 11:08:44 +0100177 return info->voltages[selector];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530178}
179
180static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
181{
Mattias Wallin47c16972010-09-10 17:47:56 +0200182 int regulator_id, ret;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530183 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
Mattias Wallin47c16972010-09-10 17:47:56 +0200184 u8 value;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530185
186 regulator_id = rdev_get_id(rdev);
187 if (regulator_id >= AB8500_NUM_REGULATORS)
188 return -EINVAL;
189
Mattias Wallin47c16972010-09-10 17:47:56 +0200190 ret = abx500_get_register_interruptible(info->dev, info->voltage_bank,
191 info->voltage_reg, &value);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530192 if (ret < 0) {
193 dev_err(rdev_get_dev(rdev),
194 "couldn't read voltage reg for regulator\n");
195 return ret;
196 }
197
198 /* vintcore has a different layout */
Mattias Wallin47c16972010-09-10 17:47:56 +0200199 value &= info->voltage_mask;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530200 if (regulator_id == AB8500_LDO_INTCORE)
Bengt Jonssone1159e62010-12-10 11:08:44 +0100201 ret = info->voltages[value >> 0x3];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530202 else
Bengt Jonssone1159e62010-12-10 11:08:44 +0100203 ret = info->voltages[value];
Sundar R IYERc789ca22010-07-13 21:48:56 +0530204
205 return ret;
206}
207
208static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
209 int min_uV, int max_uV)
210{
211 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
212 int i;
213
214 /* check the supported voltage */
215 for (i = 0; i < info->voltages_len; i++) {
Bengt Jonssone1159e62010-12-10 11:08:44 +0100216 if ((info->voltages[i] >= min_uV) &&
217 (info->voltages[i] <= max_uV))
Sundar R IYERc789ca22010-07-13 21:48:56 +0530218 return i;
219 }
220
221 return -EINVAL;
222}
223
224static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000225 int min_uV, int max_uV,
226 unsigned *selector)
Sundar R IYERc789ca22010-07-13 21:48:56 +0530227{
228 int regulator_id, ret;
229 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
230
231 regulator_id = rdev_get_id(rdev);
232 if (regulator_id >= AB8500_NUM_REGULATORS)
233 return -EINVAL;
234
235 /* get the appropriate voltages within the range */
236 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
237 if (ret < 0) {
238 dev_err(rdev_get_dev(rdev),
239 "couldn't get best voltage for regulator\n");
240 return ret;
241 }
242
Mark Brown3a93f2a2010-11-10 14:38:29 +0000243 *selector = ret;
244
Sundar R IYERc789ca22010-07-13 21:48:56 +0530245 /* set the registers for the request */
Mattias Wallin47c16972010-09-10 17:47:56 +0200246 ret = abx500_mask_and_set_register_interruptible(info->dev,
247 info->voltage_bank, info->voltage_reg,
248 info->voltage_mask, (u8)ret);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530249 if (ret < 0)
250 dev_err(rdev_get_dev(rdev),
251 "couldn't set voltage reg for regulator\n");
252
253 return ret;
254}
255
256static struct regulator_ops ab8500_regulator_ops = {
257 .enable = ab8500_regulator_enable,
258 .disable = ab8500_regulator_disable,
259 .is_enabled = ab8500_regulator_is_enabled,
260 .get_voltage = ab8500_regulator_get_voltage,
261 .set_voltage = ab8500_regulator_set_voltage,
262 .list_voltage = ab8500_list_voltage,
263};
264
265static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
266{
267 int regulator_id;
268 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
269
270 regulator_id = rdev_get_id(rdev);
271 if (regulator_id >= AB8500_NUM_REGULATORS)
272 return -EINVAL;
273
274 return info->fixed_uV;
275}
276
277static struct regulator_ops ab8500_ldo_fixed_ops = {
278 .enable = ab8500_regulator_enable,
279 .disable = ab8500_regulator_disable,
280 .is_enabled = ab8500_regulator_is_enabled,
281 .get_voltage = ab8500_fixed_get_voltage,
282 .list_voltage = ab8500_list_voltage,
283};
284
Bengt Jonssone1159e62010-12-10 11:08:44 +0100285#define AB8500_LDO(_id, _min_mV, _max_mV, \
286 _u_bank, _u_reg, _u_mask, _u_val_enable, \
287 _v_bank, _v_reg, _v_mask, _v_table, _v_table_len) \
288[AB8500_LDO_##_id] = { \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530289 .desc = { \
Bengt Jonssone1159e62010-12-10 11:08:44 +0100290 .name = "LDO-" #_id, \
291 .ops = &ab8500_regulator_ops, \
292 .type = REGULATOR_VOLTAGE, \
293 .id = AB8500_LDO_##_id, \
294 .owner = THIS_MODULE, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530295 }, \
Bengt Jonssone1159e62010-12-10 11:08:44 +0100296 .min_uV = (_min_mV) * 1000, \
297 .max_uV = (_max_mV) * 1000, \
298 .update_bank = _u_bank, \
299 .update_reg = _u_reg, \
300 .update_mask = _u_mask, \
301 .update_val_enable = _u_val_enable, \
302 .voltage_bank = _v_bank, \
303 .voltage_reg = _v_reg, \
304 .voltage_mask = _v_mask, \
305 .voltages = _v_table, \
306 .voltages_len = _v_table_len, \
307 .fixed_uV = 0, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530308}
309
Bengt Jonssone1159e62010-12-10 11:08:44 +0100310#define AB8500_FIXED_LDO(_id, _fixed_mV, \
311 _u_bank, _u_reg, _u_mask, _u_val_enable) \
312[AB8500_LDO_##_id] = { \
313 .desc = { \
314 .name = "LDO-" #_id, \
315 .ops = &ab8500_ldo_fixed_ops, \
316 .type = REGULATOR_VOLTAGE, \
317 .id = AB8500_LDO_##_id, \
318 .owner = THIS_MODULE, \
319 }, \
320 .fixed_uV = (_fixed_mV) * 1000, \
321 .update_bank = _u_bank, \
322 .update_reg = _u_reg, \
323 .update_mask = _u_mask, \
324 .update_val_enable = _u_val_enable, \
Sundar R IYERc789ca22010-07-13 21:48:56 +0530325}
326
327static struct ab8500_regulator_info ab8500_regulator_info[] = {
328 /*
Bengt Jonssone1159e62010-12-10 11:08:44 +0100329 * Variable Voltage Regulators
330 * name, min mV, max mV,
331 * update bank, reg, mask, enable val
332 * volt bank, reg, mask, table, table length
Sundar R IYERc789ca22010-07-13 21:48:56 +0530333 */
Bengt Jonssone1159e62010-12-10 11:08:44 +0100334 AB8500_LDO(AUX1, 1100, 3300,
335 0x04, 0x09, 0x03, 0x01, 0x04, 0x1f, 0x0f,
336 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
337 AB8500_LDO(AUX2, 1100, 3300,
338 0x04, 0x09, 0x0c, 0x04, 0x04, 0x20, 0x0f,
339 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
340 AB8500_LDO(AUX3, 1100, 3300,
341 0x04, 0x0a, 0x03, 0x01, 0x04, 0x21, 0x07,
342 ldo_vaux3_voltages, ARRAY_SIZE(ldo_vaux3_voltages)),
343 AB8500_LDO(INTCORE, 1100, 3300,
344 0x03, 0x80, 0x44, 0x04, 0x03, 0x80, 0x38,
Sundar R IYERc789ca22010-07-13 21:48:56 +0530345 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
346
347 /*
Bengt Jonssone1159e62010-12-10 11:08:44 +0100348 * Fixed Voltage Regulators
349 * name, fixed mV,
350 * update bank, reg, mask, enable val
Sundar R IYERc789ca22010-07-13 21:48:56 +0530351 */
Bengt Jonssone1159e62010-12-10 11:08:44 +0100352 AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x82, 0x02),
353 AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x02, 0x02),
354 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
355 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
356 AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x04, 0x04),
357 AB8500_FIXED_LDO(ANA, 1200, 0x04, 0x06, 0x0c, 0x04),
Sundar R IYERc789ca22010-07-13 21:48:56 +0530358};
359
Sundar R IYERc789ca22010-07-13 21:48:56 +0530360static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
361{
362 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
Dan Carpenteraf54dec2010-08-14 11:03:16 +0200363 struct ab8500_platform_data *pdata;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530364 int i, err;
365
366 if (!ab8500) {
367 dev_err(&pdev->dev, "null mfd parent\n");
368 return -EINVAL;
369 }
Dan Carpenteraf54dec2010-08-14 11:03:16 +0200370 pdata = dev_get_platdata(ab8500->dev);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530371
Bengt Jonssoncb189b02010-12-10 11:08:40 +0100372 /* make sure the platform data has the correct size */
373 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
374 dev_err(&pdev->dev, "platform configuration error\n");
375 return -EINVAL;
376 }
377
Sundar R IYERc789ca22010-07-13 21:48:56 +0530378 /* register all regulators */
379 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
380 struct ab8500_regulator_info *info = NULL;
381
382 /* assign per-regulator data */
383 info = &ab8500_regulator_info[i];
384 info->dev = &pdev->dev;
Sundar R IYERc789ca22010-07-13 21:48:56 +0530385
Bengt Jonsson2b751512010-12-10 11:08:43 +0100386 /* fix for hardware before ab8500v2.0 */
387 if (abx500_get_chip_id(info->dev) < 0x20) {
388 if (info->desc.id == AB8500_LDO_AUX3) {
389 info->desc.n_voltages =
390 ARRAY_SIZE(ldo_vauxn_voltages);
Bengt Jonssone1159e62010-12-10 11:08:44 +0100391 info->voltages = ldo_vauxn_voltages;
Bengt Jonsson2b751512010-12-10 11:08:43 +0100392 info->voltages_len =
393 ARRAY_SIZE(ldo_vauxn_voltages);
394 info->voltage_mask = 0xf;
395 }
396 }
397
398 /* register regulator with framework */
Sundar R IYERc789ca22010-07-13 21:48:56 +0530399 info->regulator = regulator_register(&info->desc, &pdev->dev,
Bengt Jonssoncb189b02010-12-10 11:08:40 +0100400 &pdata->regulator[i], info);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530401 if (IS_ERR(info->regulator)) {
402 err = PTR_ERR(info->regulator);
403 dev_err(&pdev->dev, "failed to register regulator %s\n",
404 info->desc.name);
405 /* when we fail, un-register all earlier regulators */
Axel Lind4876a32010-08-14 21:44:04 +0800406 while (--i >= 0) {
Sundar R IYERc789ca22010-07-13 21:48:56 +0530407 info = &ab8500_regulator_info[i];
408 regulator_unregister(info->regulator);
Sundar R IYERc789ca22010-07-13 21:48:56 +0530409 }
410 return err;
411 }
412 }
413
414 return 0;
415}
416
417static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
418{
419 int i;
420
421 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
422 struct ab8500_regulator_info *info = NULL;
423 info = &ab8500_regulator_info[i];
424 regulator_unregister(info->regulator);
425 }
426
427 return 0;
428}
429
430static struct platform_driver ab8500_regulator_driver = {
431 .probe = ab8500_regulator_probe,
432 .remove = __devexit_p(ab8500_regulator_remove),
433 .driver = {
434 .name = "ab8500-regulator",
435 .owner = THIS_MODULE,
436 },
437};
438
439static int __init ab8500_regulator_init(void)
440{
441 int ret;
442
443 ret = platform_driver_register(&ab8500_regulator_driver);
444 if (ret != 0)
445 pr_err("Failed to register ab8500 regulator: %d\n", ret);
446
447 return ret;
448}
449subsys_initcall(ab8500_regulator_init);
450
451static void __exit ab8500_regulator_exit(void)
452{
453 platform_driver_unregister(&ab8500_regulator_driver);
454}
455module_exit(ab8500_regulator_exit);
456
457MODULE_LICENSE("GPL v2");
458MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
459MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
460MODULE_ALIAS("platform:ab8500-regulator");