2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
8 * AB8500 peripheral regulators
10 * AB8500 supports the following regulators,
11 * LDOs - VAUDIO, VANAMIC2/2, VDIGMIC, VINTCORE12, VTVOUT,
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
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>
24 #include <linux/mfd/abx500.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27 #include <linux/regulator/ab8500.h>
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)
37 * @update_bank: bank to control on/off
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
41 * @voltage_bank: bank to control regulator voltage
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
47 struct ab8500_regulator_info
{
49 struct regulator_desc desc
;
50 struct ab8500
*ab8500
;
51 struct regulator_dev
*regulator
;
62 int const *supported_voltages
;
66 /* voltage tables for the vauxn/vintcore supplies */
67 static const int ldo_vauxn_voltages
[] = {
86 static const int ldo_vintcore_voltages
[] = {
96 static int ab8500_regulator_enable(struct regulator_dev
*rdev
)
98 int regulator_id
, ret
;
99 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
101 regulator_id
= rdev_get_id(rdev
);
102 if (regulator_id
>= AB8500_NUM_REGULATORS
)
105 ret
= abx500_mask_and_set_register_interruptible(info
->dev
,
106 info
->update_bank
, info
->update_reg
, info
->mask
, info
->enable
);
108 dev_err(rdev_get_dev(rdev
),
109 "couldn't set enable bits for regulator\n");
113 static int ab8500_regulator_disable(struct regulator_dev
*rdev
)
115 int regulator_id
, ret
;
116 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
118 regulator_id
= rdev_get_id(rdev
);
119 if (regulator_id
>= AB8500_NUM_REGULATORS
)
122 ret
= abx500_mask_and_set_register_interruptible(info
->dev
,
123 info
->update_bank
, info
->update_reg
, info
->mask
, 0x0);
125 dev_err(rdev_get_dev(rdev
),
126 "couldn't set disable bits for regulator\n");
130 static int ab8500_regulator_is_enabled(struct regulator_dev
*rdev
)
132 int regulator_id
, ret
;
133 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
136 regulator_id
= rdev_get_id(rdev
);
137 if (regulator_id
>= AB8500_NUM_REGULATORS
)
140 ret
= abx500_get_register_interruptible(info
->dev
,
141 info
->update_bank
, info
->update_reg
, &value
);
143 dev_err(rdev_get_dev(rdev
),
144 "couldn't read 0x%x register\n", info
->update_reg
);
148 if (value
& info
->mask
)
154 static int ab8500_list_voltage(struct regulator_dev
*rdev
, unsigned selector
)
157 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
159 regulator_id
= rdev_get_id(rdev
);
160 if (regulator_id
>= AB8500_NUM_REGULATORS
)
163 /* return the uV for the fixed regulators */
165 return info
->fixed_uV
;
167 if (selector
>= info
->voltages_len
)
170 return info
->supported_voltages
[selector
];
173 static int ab8500_regulator_get_voltage(struct regulator_dev
*rdev
)
175 int regulator_id
, ret
;
176 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
179 regulator_id
= rdev_get_id(rdev
);
180 if (regulator_id
>= AB8500_NUM_REGULATORS
)
183 ret
= abx500_get_register_interruptible(info
->dev
, info
->voltage_bank
,
184 info
->voltage_reg
, &value
);
186 dev_err(rdev_get_dev(rdev
),
187 "couldn't read voltage reg for regulator\n");
191 /* vintcore has a different layout */
192 value
&= info
->voltage_mask
;
193 if (regulator_id
== AB8500_LDO_INTCORE
)
194 ret
= info
->supported_voltages
[value
>> 0x3];
196 ret
= info
->supported_voltages
[value
];
201 static int ab8500_get_best_voltage_index(struct regulator_dev
*rdev
,
202 int min_uV
, int max_uV
)
204 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
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
))
217 static int ab8500_regulator_set_voltage(struct regulator_dev
*rdev
,
218 int min_uV
, int max_uV
)
220 int regulator_id
, ret
;
221 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
223 regulator_id
= rdev_get_id(rdev
);
224 if (regulator_id
>= AB8500_NUM_REGULATORS
)
227 /* get the appropriate voltages within the range */
228 ret
= ab8500_get_best_voltage_index(rdev
, min_uV
, max_uV
);
230 dev_err(rdev_get_dev(rdev
),
231 "couldn't get best voltage for regulator\n");
235 /* set the registers for the request */
236 ret
= abx500_mask_and_set_register_interruptible(info
->dev
,
237 info
->voltage_bank
, info
->voltage_reg
,
238 info
->voltage_mask
, (u8
)ret
);
240 dev_err(rdev_get_dev(rdev
),
241 "couldn't set voltage reg for regulator\n");
246 static 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
,
255 static int ab8500_fixed_get_voltage(struct regulator_dev
*rdev
)
258 struct ab8500_regulator_info
*info
= rdev_get_drvdata(rdev
);
260 regulator_id
= rdev_get_id(rdev
);
261 if (regulator_id
>= AB8500_NUM_REGULATORS
)
264 return info
->fixed_uV
;
267 static 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
,
275 #define AB8500_LDO(_id, min, max, bank, reg, reg_mask, \
276 reg_enable, volt_bank, volt_reg, volt_mask, \
277 voltages, len_volts) \
280 .name = "LDO-" #_id, \
281 .ops = &ab8500_regulator_ops, \
282 .type = REGULATOR_VOLTAGE, \
283 .id = AB8500_LDO_##_id, \
284 .owner = THIS_MODULE, \
286 .min_uV = (min) * 1000, \
287 .max_uV = (max) * 1000, \
288 .update_bank = bank, \
291 .enable = reg_enable, \
292 .voltage_bank = volt_bank, \
293 .voltage_reg = volt_reg, \
294 .voltage_mask = volt_mask, \
295 .supported_voltages = voltages, \
296 .voltages_len = len_volts, \
300 #define AB8500_FIXED_LDO(_id, fixed, bank, reg, \
301 reg_mask, reg_enable) \
304 .name = "LDO-" #_id, \
305 .ops = &ab8500_ldo_fixed_ops, \
306 .type = REGULATOR_VOLTAGE, \
307 .id = AB8500_LDO_##_id, \
308 .owner = THIS_MODULE, \
310 .fixed_uV = fixed * 1000, \
311 .update_bank = bank, \
314 .enable = reg_enable, \
317 static struct ab8500_regulator_info ab8500_regulator_info
[] = {
319 * Variable Voltage LDOs
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
324 AB8500_LDO(AUX1
, 1100, 3300, 0x04, 0x09, 0x3, 0x1, 0x04, 0x1f, 0xf,
325 ldo_vauxn_voltages
, ARRAY_SIZE(ldo_vauxn_voltages
)),
326 AB8500_LDO(AUX2
, 1100, 3300, 0x04, 0x09, 0xc, 0x4, 0x04, 0x20, 0xf,
327 ldo_vauxn_voltages
, ARRAY_SIZE(ldo_vauxn_voltages
)),
328 AB8500_LDO(AUX3
, 1100, 3300, 0x04, 0x0a, 0x3, 0x1, 0x04, 0x21, 0xf,
329 ldo_vauxn_voltages
, ARRAY_SIZE(ldo_vauxn_voltages
)),
330 AB8500_LDO(INTCORE
, 1100, 3300, 0x03, 0x80, 0x4, 0x4, 0x03, 0x80, 0x38,
331 ldo_vintcore_voltages
, ARRAY_SIZE(ldo_vintcore_voltages
)),
335 * name, o/p uV, ctrl bank, ctrl reg, enable, disable
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),
345 static inline struct ab8500_regulator_info
*find_regulator_info(int id
)
347 struct ab8500_regulator_info
*info
;
350 for (i
= 0; i
< ARRAY_SIZE(ab8500_regulator_info
); i
++) {
351 info
= &ab8500_regulator_info
[i
];
352 if (info
->desc
.id
== id
)
358 static __devinit
int ab8500_regulator_probe(struct platform_device
*pdev
)
360 struct ab8500
*ab8500
= dev_get_drvdata(pdev
->dev
.parent
);
361 struct ab8500_platform_data
*pdata
;
365 dev_err(&pdev
->dev
, "null mfd parent\n");
368 pdata
= dev_get_platdata(ab8500
->dev
);
370 /* register all regulators */
371 for (i
= 0; i
< ARRAY_SIZE(ab8500_regulator_info
); i
++) {
372 struct ab8500_regulator_info
*info
= NULL
;
374 /* assign per-regulator data */
375 info
= &ab8500_regulator_info
[i
];
376 info
->dev
= &pdev
->dev
;
377 info
->ab8500
= ab8500
;
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",
385 /* when we fail, un-register all earlier regulators */
387 info
= &ab8500_regulator_info
[i
];
388 regulator_unregister(info
->regulator
);
397 static __devexit
int ab8500_regulator_remove(struct platform_device
*pdev
)
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
);
410 static struct platform_driver ab8500_regulator_driver
= {
411 .probe
= ab8500_regulator_probe
,
412 .remove
= __devexit_p(ab8500_regulator_remove
),
414 .name
= "ab8500-regulator",
415 .owner
= THIS_MODULE
,
419 static int __init
ab8500_regulator_init(void)
423 ret
= platform_driver_register(&ab8500_regulator_driver
);
425 pr_err("Failed to register ab8500 regulator: %d\n", ret
);
429 subsys_initcall(ab8500_regulator_init
);
431 static void __exit
ab8500_regulator_exit(void)
433 platform_driver_unregister(&ab8500_regulator_driver
);
435 module_exit(ab8500_regulator_exit
);
437 MODULE_LICENSE("GPL v2");
438 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
439 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
440 MODULE_ALIAS("platform:ab8500-regulator");