Commit | Line | Data |
---|---|---|
fa16a5c1 DB |
1 | /* |
2 | * twl4030-regulator.c -- support regulators in twl4030 family chips | |
3 | * | |
4 | * Copyright (C) 2008 David Brownell | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/err.h> | |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/regulator/driver.h> | |
17 | #include <linux/regulator/machine.h> | |
18 | #include <linux/i2c/twl4030.h> | |
19 | ||
20 | ||
21 | /* | |
22 | * The TWL4030/TW5030/TPS659x0 family chips include power management, a | |
23 | * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions | |
24 | * include an audio codec, battery charger, and more voltage regulators. | |
25 | * These chips are often used in OMAP-based systems. | |
26 | * | |
27 | * This driver implements software-based resource control for various | |
28 | * voltage regulators. This is usually augmented with state machine | |
29 | * based control. | |
30 | */ | |
31 | ||
32 | struct twlreg_info { | |
33 | /* start of regulator's PM_RECEIVER control register bank */ | |
34 | u8 base; | |
35 | ||
36 | /* twl4030 resource ID, for resource control state machine */ | |
37 | u8 id; | |
38 | ||
39 | /* voltage in mV = table[VSEL]; table_len must be a power-of-two */ | |
40 | u8 table_len; | |
41 | const u16 *table; | |
42 | ||
43 | /* chip constraints on regulator behavior */ | |
44 | u16 min_mV; | |
fa16a5c1 DB |
45 | |
46 | /* used by regulator core */ | |
47 | struct regulator_desc desc; | |
48 | }; | |
49 | ||
50 | ||
51 | /* LDO control registers ... offset is from the base of its register bank. | |
52 | * The first three registers of all power resource banks help hardware to | |
53 | * manage the various resource groups. | |
54 | */ | |
55 | #define VREG_GRP 0 | |
56 | #define VREG_TYPE 1 | |
57 | #define VREG_REMAP 2 | |
58 | #define VREG_DEDICATED 3 /* LDO control */ | |
59 | ||
60 | ||
61 | static inline int | |
62 | twl4030reg_read(struct twlreg_info *info, unsigned offset) | |
63 | { | |
64 | u8 value; | |
65 | int status; | |
66 | ||
67 | status = twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER, | |
68 | &value, info->base + offset); | |
69 | return (status < 0) ? status : value; | |
70 | } | |
71 | ||
72 | static inline int | |
73 | twl4030reg_write(struct twlreg_info *info, unsigned offset, u8 value) | |
74 | { | |
75 | return twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, | |
76 | value, info->base + offset); | |
77 | } | |
78 | ||
79 | /*----------------------------------------------------------------------*/ | |
80 | ||
81 | /* generic power resource operations, which work on all regulators */ | |
82 | ||
83 | static int twl4030reg_grp(struct regulator_dev *rdev) | |
84 | { | |
85 | return twl4030reg_read(rdev_get_drvdata(rdev), VREG_GRP); | |
86 | } | |
87 | ||
88 | /* | |
89 | * Enable/disable regulators by joining/leaving the P1 (processor) group. | |
90 | * We assume nobody else is updating the DEV_GRP registers. | |
91 | */ | |
92 | ||
93 | #define P3_GRP BIT(7) /* "peripherals" */ | |
94 | #define P2_GRP BIT(6) /* secondary processor, modem, etc */ | |
95 | #define P1_GRP BIT(5) /* CPU/Linux */ | |
96 | ||
97 | static int twl4030reg_is_enabled(struct regulator_dev *rdev) | |
98 | { | |
99 | int state = twl4030reg_grp(rdev); | |
100 | ||
101 | if (state < 0) | |
102 | return state; | |
103 | ||
104 | return (state & P1_GRP) != 0; | |
105 | } | |
106 | ||
107 | static int twl4030reg_enable(struct regulator_dev *rdev) | |
108 | { | |
109 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
110 | int grp; | |
111 | ||
112 | grp = twl4030reg_read(info, VREG_GRP); | |
113 | if (grp < 0) | |
114 | return grp; | |
115 | ||
116 | grp |= P1_GRP; | |
117 | return twl4030reg_write(info, VREG_GRP, grp); | |
118 | } | |
119 | ||
120 | static int twl4030reg_disable(struct regulator_dev *rdev) | |
121 | { | |
122 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
123 | int grp; | |
124 | ||
125 | grp = twl4030reg_read(info, VREG_GRP); | |
126 | if (grp < 0) | |
127 | return grp; | |
128 | ||
129 | grp &= ~P1_GRP; | |
130 | return twl4030reg_write(info, VREG_GRP, grp); | |
131 | } | |
132 | ||
133 | static int twl4030reg_get_status(struct regulator_dev *rdev) | |
134 | { | |
135 | int state = twl4030reg_grp(rdev); | |
136 | ||
137 | if (state < 0) | |
138 | return state; | |
139 | state &= 0x0f; | |
140 | ||
141 | /* assume state != WARM_RESET; we'd not be running... */ | |
142 | if (!state) | |
143 | return REGULATOR_STATUS_OFF; | |
144 | return (state & BIT(3)) | |
145 | ? REGULATOR_STATUS_NORMAL | |
146 | : REGULATOR_STATUS_STANDBY; | |
147 | } | |
148 | ||
149 | static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode) | |
150 | { | |
151 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
152 | unsigned message; | |
153 | int status; | |
154 | ||
155 | /* We can only set the mode through state machine commands... */ | |
156 | switch (mode) { | |
157 | case REGULATOR_MODE_NORMAL: | |
158 | message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE); | |
159 | break; | |
160 | case REGULATOR_MODE_STANDBY: | |
161 | message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP); | |
162 | break; | |
163 | default: | |
164 | return -EINVAL; | |
165 | } | |
166 | ||
167 | /* Ensure the resource is associated with some group */ | |
168 | status = twl4030reg_grp(rdev); | |
169 | if (status < 0) | |
170 | return status; | |
171 | if (!(status & (P3_GRP | P2_GRP | P1_GRP))) | |
172 | return -EACCES; | |
173 | ||
174 | status = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, | |
175 | message >> 8, 0x15 /* PB_WORD_MSB */ ); | |
176 | if (status >= 0) | |
177 | return status; | |
178 | ||
179 | return twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, | |
180 | message, 0x16 /* PB_WORD_LSB */ ); | |
181 | } | |
182 | ||
183 | /*----------------------------------------------------------------------*/ | |
184 | ||
185 | /* | |
186 | * Support for adjustable-voltage LDOs uses a four bit (or less) voltage | |
187 | * select field in its control register. We use tables indexed by VSEL | |
188 | * to record voltages in milliVolts. (Accuracy is about three percent.) | |
189 | * | |
190 | * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon; | |
191 | * currently handled by listing two slightly different VAUX2 regulators, | |
192 | * only one of which will be configured. | |
193 | * | |
194 | * VSEL values documented as "TI cannot support these values" are flagged | |
195 | * in these tables as UNSUP() values; we normally won't assign them. | |
196 | */ | |
197 | #ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED | |
198 | #define UNSUP_MASK 0x0000 | |
199 | #else | |
200 | #define UNSUP_MASK 0x8000 | |
201 | #endif | |
202 | ||
203 | #define UNSUP(x) (UNSUP_MASK | (x)) | |
204 | #define IS_UNSUP(x) (UNSUP_MASK & (x)) | |
205 | #define LDO_MV(x) (~UNSUP_MASK & (x)) | |
206 | ||
207 | ||
208 | static const u16 VAUX1_VSEL_table[] = { | |
209 | UNSUP(1500), UNSUP(1800), 2500, 2800, | |
210 | 3000, 3000, 3000, 3000, | |
211 | }; | |
212 | static const u16 VAUX2_4030_VSEL_table[] = { | |
213 | UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300, | |
214 | 1500, 1800, UNSUP(1850), 2500, | |
215 | UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000), | |
216 | UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150), | |
217 | }; | |
218 | static const u16 VAUX2_VSEL_table[] = { | |
219 | 1700, 1700, 1900, 1300, | |
220 | 1500, 1800, 2000, 2500, | |
221 | 2100, 2800, 2200, 2300, | |
222 | 2400, 2400, 2400, 2400, | |
223 | }; | |
224 | static const u16 VAUX3_VSEL_table[] = { | |
225 | 1500, 1800, 2500, 2800, | |
226 | UNSUP(3000), UNSUP(3000), UNSUP(3000), UNSUP(3000), | |
227 | }; | |
228 | static const u16 VAUX4_VSEL_table[] = { | |
229 | 700, 1000, 1200, UNSUP(1300), | |
230 | 1500, 1800, UNSUP(1850), 2500, | |
231 | }; | |
232 | static const u16 VMMC1_VSEL_table[] = { | |
233 | 1850, 2850, 3000, 3150, | |
234 | }; | |
235 | static const u16 VMMC2_VSEL_table[] = { | |
236 | UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300), | |
237 | UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500), | |
238 | 2600, 2800, 2850, 3000, | |
239 | 3150, 3150, 3150, 3150, | |
240 | }; | |
241 | static const u16 VPLL1_VSEL_table[] = { | |
242 | 1000, 1200, 1300, 1800, | |
243 | UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000), | |
244 | }; | |
245 | static const u16 VPLL2_VSEL_table[] = { | |
246 | 700, 1000, 1200, 1300, | |
247 | UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500), | |
248 | UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000), | |
249 | UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150), | |
250 | }; | |
251 | static const u16 VSIM_VSEL_table[] = { | |
252 | UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800, | |
253 | 2800, 3000, 3000, 3000, | |
254 | }; | |
255 | static const u16 VDAC_VSEL_table[] = { | |
256 | 1200, 1300, 1800, 1800, | |
257 | }; | |
258 | ||
259 | ||
66b659e6 DB |
260 | static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index) |
261 | { | |
262 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
263 | int mV = info->table[index]; | |
264 | ||
265 | return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000); | |
266 | } | |
267 | ||
fa16a5c1 DB |
268 | static int |
269 | twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV) | |
270 | { | |
271 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
272 | int vsel; | |
273 | ||
274 | for (vsel = 0; vsel < info->table_len; vsel++) { | |
275 | int mV = info->table[vsel]; | |
276 | int uV; | |
277 | ||
278 | if (IS_UNSUP(mV)) | |
279 | continue; | |
280 | uV = LDO_MV(mV) * 1000; | |
281 | ||
66b659e6 DB |
282 | /* REVISIT for VAUX2, first match may not be best/lowest */ |
283 | ||
fa16a5c1 DB |
284 | /* use the first in-range value */ |
285 | if (min_uV <= uV && uV <= max_uV) | |
286 | return twl4030reg_write(info, VREG_DEDICATED, vsel); | |
287 | } | |
288 | ||
289 | return -EDOM; | |
290 | } | |
291 | ||
292 | static int twl4030ldo_get_voltage(struct regulator_dev *rdev) | |
293 | { | |
294 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
295 | int vsel = twl4030reg_read(info, VREG_DEDICATED); | |
296 | ||
297 | if (vsel < 0) | |
298 | return vsel; | |
299 | ||
300 | vsel &= info->table_len - 1; | |
301 | return LDO_MV(info->table[vsel]) * 1000; | |
302 | } | |
303 | ||
304 | static struct regulator_ops twl4030ldo_ops = { | |
66b659e6 DB |
305 | .list_voltage = twl4030ldo_list_voltage, |
306 | ||
fa16a5c1 DB |
307 | .set_voltage = twl4030ldo_set_voltage, |
308 | .get_voltage = twl4030ldo_get_voltage, | |
309 | ||
310 | .enable = twl4030reg_enable, | |
311 | .disable = twl4030reg_disable, | |
312 | .is_enabled = twl4030reg_is_enabled, | |
313 | ||
314 | .set_mode = twl4030reg_set_mode, | |
315 | ||
316 | .get_status = twl4030reg_get_status, | |
317 | }; | |
318 | ||
319 | /*----------------------------------------------------------------------*/ | |
320 | ||
321 | /* | |
322 | * Fixed voltage LDOs don't have a VSEL field to update. | |
323 | */ | |
66b659e6 DB |
324 | static int twl4030fixed_list_voltage(struct regulator_dev *rdev, unsigned index) |
325 | { | |
326 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
327 | ||
328 | return info->min_mV * 1000; | |
329 | } | |
330 | ||
fa16a5c1 DB |
331 | static int twl4030fixed_get_voltage(struct regulator_dev *rdev) |
332 | { | |
333 | struct twlreg_info *info = rdev_get_drvdata(rdev); | |
334 | ||
335 | return info->min_mV * 1000; | |
336 | } | |
337 | ||
338 | static struct regulator_ops twl4030fixed_ops = { | |
66b659e6 DB |
339 | .list_voltage = twl4030fixed_list_voltage, |
340 | ||
fa16a5c1 DB |
341 | .get_voltage = twl4030fixed_get_voltage, |
342 | ||
343 | .enable = twl4030reg_enable, | |
344 | .disable = twl4030reg_disable, | |
345 | .is_enabled = twl4030reg_is_enabled, | |
346 | ||
347 | .set_mode = twl4030reg_set_mode, | |
348 | ||
349 | .get_status = twl4030reg_get_status, | |
350 | }; | |
351 | ||
352 | /*----------------------------------------------------------------------*/ | |
353 | ||
354 | #define TWL_ADJUSTABLE_LDO(label, offset, num) { \ | |
355 | .base = offset, \ | |
356 | .id = num, \ | |
357 | .table_len = ARRAY_SIZE(label##_VSEL_table), \ | |
358 | .table = label##_VSEL_table, \ | |
359 | .desc = { \ | |
360 | .name = #label, \ | |
361 | .id = TWL4030_REG_##label, \ | |
66b659e6 | 362 | .n_voltages = ARRAY_SIZE(label##_VSEL_table), \ |
fa16a5c1 DB |
363 | .ops = &twl4030ldo_ops, \ |
364 | .type = REGULATOR_VOLTAGE, \ | |
365 | .owner = THIS_MODULE, \ | |
366 | }, \ | |
367 | } | |
368 | ||
369 | #define TWL_FIXED_LDO(label, offset, mVolts, num) { \ | |
370 | .base = offset, \ | |
371 | .id = num, \ | |
372 | .min_mV = mVolts, \ | |
fa16a5c1 DB |
373 | .desc = { \ |
374 | .name = #label, \ | |
375 | .id = TWL4030_REG_##label, \ | |
66b659e6 | 376 | .n_voltages = 1, \ |
fa16a5c1 DB |
377 | .ops = &twl4030fixed_ops, \ |
378 | .type = REGULATOR_VOLTAGE, \ | |
379 | .owner = THIS_MODULE, \ | |
380 | }, \ | |
381 | } | |
382 | ||
383 | /* | |
384 | * We list regulators here if systems need some level of | |
385 | * software control over them after boot. | |
386 | */ | |
387 | static struct twlreg_info twl4030_regs[] = { | |
388 | TWL_ADJUSTABLE_LDO(VAUX1, 0x17, 1), | |
389 | TWL_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2), | |
390 | TWL_ADJUSTABLE_LDO(VAUX2, 0x1b, 2), | |
391 | TWL_ADJUSTABLE_LDO(VAUX3, 0x1f, 3), | |
392 | TWL_ADJUSTABLE_LDO(VAUX4, 0x23, 4), | |
393 | TWL_ADJUSTABLE_LDO(VMMC1, 0x27, 5), | |
394 | TWL_ADJUSTABLE_LDO(VMMC2, 0x2b, 6), | |
395 | /* | |
396 | TWL_ADJUSTABLE_LDO(VPLL1, 0x2f, 7), | |
397 | TWL_ADJUSTABLE_LDO(VPLL2, 0x33, 8), | |
398 | */ | |
399 | TWL_ADJUSTABLE_LDO(VSIM, 0x37, 9), | |
400 | TWL_ADJUSTABLE_LDO(VDAC, 0x3b, 10), | |
401 | /* | |
402 | TWL_ADJUSTABLE_LDO(VINTANA1, 0x3f, 11), | |
403 | TWL_ADJUSTABLE_LDO(VINTANA2, 0x43, 12), | |
404 | TWL_ADJUSTABLE_LDO(VINTDIG, 0x47, 13), | |
405 | TWL_SMPS(VIO, 0x4b, 14), | |
406 | TWL_SMPS(VDD1, 0x55, 15), | |
407 | TWL_SMPS(VDD2, 0x63, 16), | |
408 | */ | |
409 | TWL_FIXED_LDO(VUSB1V5, 0x71, 1500, 17), | |
410 | TWL_FIXED_LDO(VUSB1V8, 0x74, 1800, 18), | |
411 | TWL_FIXED_LDO(VUSB3V1, 0x77, 3100, 19), | |
412 | /* VUSBCP is managed *only* by the USB subchip */ | |
413 | }; | |
414 | ||
415 | static int twl4030reg_probe(struct platform_device *pdev) | |
416 | { | |
417 | int i; | |
418 | struct twlreg_info *info; | |
419 | struct regulator_init_data *initdata; | |
420 | struct regulation_constraints *c; | |
421 | struct regulator_dev *rdev; | |
fa16a5c1 DB |
422 | |
423 | for (i = 0, info = NULL; i < ARRAY_SIZE(twl4030_regs); i++) { | |
424 | if (twl4030_regs[i].desc.id != pdev->id) | |
425 | continue; | |
426 | info = twl4030_regs + i; | |
fa16a5c1 DB |
427 | break; |
428 | } | |
429 | if (!info) | |
430 | return -ENODEV; | |
431 | ||
432 | initdata = pdev->dev.platform_data; | |
433 | if (!initdata) | |
434 | return -EINVAL; | |
435 | ||
436 | /* Constrain board-specific capabilities according to what | |
437 | * this driver and the chip itself can actually do. | |
438 | */ | |
439 | c = &initdata->constraints; | |
fa16a5c1 DB |
440 | c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY; |
441 | c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE | |
442 | | REGULATOR_CHANGE_MODE | |
443 | | REGULATOR_CHANGE_STATUS; | |
444 | ||
445 | rdev = regulator_register(&info->desc, &pdev->dev, initdata, info); | |
446 | if (IS_ERR(rdev)) { | |
447 | dev_err(&pdev->dev, "can't register %s, %ld\n", | |
448 | info->desc.name, PTR_ERR(rdev)); | |
449 | return PTR_ERR(rdev); | |
450 | } | |
451 | platform_set_drvdata(pdev, rdev); | |
452 | ||
453 | /* NOTE: many regulators support short-circuit IRQs (presentable | |
454 | * as REGULATOR_OVER_CURRENT notifications?) configured via: | |
455 | * - SC_CONFIG | |
456 | * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4) | |
457 | * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2) | |
458 | * - IT_CONFIG | |
459 | */ | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
464 | static int __devexit twl4030reg_remove(struct platform_device *pdev) | |
465 | { | |
466 | regulator_unregister(platform_get_drvdata(pdev)); | |
467 | return 0; | |
468 | } | |
469 | ||
470 | MODULE_ALIAS("platform:twl4030_reg"); | |
471 | ||
472 | static struct platform_driver twl4030reg_driver = { | |
473 | .probe = twl4030reg_probe, | |
474 | .remove = __devexit_p(twl4030reg_remove), | |
475 | /* NOTE: short name, to work around driver model truncation of | |
476 | * "twl4030_regulator.12" (and friends) to "twl4030_regulator.1". | |
477 | */ | |
478 | .driver.name = "twl4030_reg", | |
479 | .driver.owner = THIS_MODULE, | |
480 | }; | |
481 | ||
482 | static int __init twl4030reg_init(void) | |
483 | { | |
fa16a5c1 DB |
484 | return platform_driver_register(&twl4030reg_driver); |
485 | } | |
486 | subsys_initcall(twl4030reg_init); | |
487 | ||
488 | static void __exit twl4030reg_exit(void) | |
489 | { | |
490 | platform_driver_unregister(&twl4030reg_driver); | |
491 | } | |
492 | module_exit(twl4030reg_exit) | |
493 | ||
494 | MODULE_DESCRIPTION("TWL4030 regulator driver"); | |
495 | MODULE_LICENSE("GPL"); |