blob: 6a292852a3583899a1d1c1a2c3236e76aea6d118 [file] [log] [blame]
David Brownellfa16a5c2009-02-08 10:37:06 -08001/*
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01002 * twl-regulator.c -- support regulators in twl4030/twl6030 family chips
David Brownellfa16a5c2009-02-08 10:37:06 -08003 *
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>
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +020015#include <linux/delay.h>
David Brownellfa16a5c2009-02-08 10:37:06 -080016#include <linux/platform_device.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/machine.h>
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010019#include <linux/i2c/twl.h>
David Brownellfa16a5c2009-02-08 10:37:06 -080020
21
22/*
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +010023 * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
David Brownellfa16a5c2009-02-08 10:37:06 -080024 * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions
25 * include an audio codec, battery charger, and more voltage regulators.
26 * These chips are often used in OMAP-based systems.
27 *
28 * This driver implements software-based resource control for various
29 * voltage regulators. This is usually augmented with state machine
30 * based control.
31 */
32
33struct twlreg_info {
34 /* start of regulator's PM_RECEIVER control register bank */
35 u8 base;
36
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +010037 /* twl resource ID, for resource control state machine */
David Brownellfa16a5c2009-02-08 10:37:06 -080038 u8 id;
39
40 /* voltage in mV = table[VSEL]; table_len must be a power-of-two */
41 u8 table_len;
42 const u16 *table;
43
Juha Keski-Saari045f9722009-12-16 14:49:52 +020044 /* regulator specific turn-on delay */
45 u16 delay;
46
47 /* State REMAP default configuration */
48 u8 remap;
49
David Brownellfa16a5c2009-02-08 10:37:06 -080050 /* chip constraints on regulator behavior */
51 u16 min_mV;
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +053052 u16 max_mV;
David Brownellfa16a5c2009-02-08 10:37:06 -080053
54 /* used by regulator core */
55 struct regulator_desc desc;
56};
57
58
59/* LDO control registers ... offset is from the base of its register bank.
60 * The first three registers of all power resource banks help hardware to
61 * manage the various resource groups.
62 */
Rajendra Nayak441a4502009-12-13 22:19:23 +010063/* Common offset in TWL4030/6030 */
David Brownellfa16a5c2009-02-08 10:37:06 -080064#define VREG_GRP 0
Rajendra Nayak441a4502009-12-13 22:19:23 +010065/* TWL4030 register offsets */
David Brownellfa16a5c2009-02-08 10:37:06 -080066#define VREG_TYPE 1
67#define VREG_REMAP 2
68#define VREG_DEDICATED 3 /* LDO control */
Rajendra Nayak441a4502009-12-13 22:19:23 +010069/* TWL6030 register offsets */
70#define VREG_TRANS 1
71#define VREG_STATE 2
72#define VREG_VOLTAGE 3
73/* TWL6030 Misc register offsets */
74#define VREG_BC_ALL 1
75#define VREG_BC_REF 2
76#define VREG_BC_PROC 3
77#define VREG_BC_CLK_RST 4
David Brownellfa16a5c2009-02-08 10:37:06 -080078
79static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +010080twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
David Brownellfa16a5c2009-02-08 10:37:06 -080081{
82 u8 value;
83 int status;
84
Rajendra Nayak441a4502009-12-13 22:19:23 +010085 status = twl_i2c_read_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -080086 &value, info->base + offset);
87 return (status < 0) ? status : value;
88}
89
90static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +010091twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
92 u8 value)
David Brownellfa16a5c2009-02-08 10:37:06 -080093{
Rajendra Nayak441a4502009-12-13 22:19:23 +010094 return twl_i2c_write_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -080095 value, info->base + offset);
96}
97
98/*----------------------------------------------------------------------*/
99
100/* generic power resource operations, which work on all regulators */
101
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100102static int twlreg_grp(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800103{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100104 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
105 VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800106}
107
108/*
109 * Enable/disable regulators by joining/leaving the P1 (processor) group.
110 * We assume nobody else is updating the DEV_GRP registers.
111 */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100112/* definition for 4030 family */
113#define P3_GRP_4030 BIT(7) /* "peripherals" */
114#define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
115#define P1_GRP_4030 BIT(5) /* CPU/Linux */
116/* definition for 6030 family */
117#define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
118#define P2_GRP_6030 BIT(1) /* "peripherals" */
119#define P1_GRP_6030 BIT(0) /* CPU/Linux */
David Brownellfa16a5c2009-02-08 10:37:06 -0800120
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100121static int twlreg_is_enabled(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800122{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100123 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800124
125 if (state < 0)
126 return state;
127
Rajendra Nayak441a4502009-12-13 22:19:23 +0100128 if (twl_class_is_4030())
129 state &= P1_GRP_4030;
130 else
131 state &= P1_GRP_6030;
132 return state;
David Brownellfa16a5c2009-02-08 10:37:06 -0800133}
134
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100135static int twlreg_enable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800136{
137 struct twlreg_info *info = rdev_get_drvdata(rdev);
138 int grp;
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200139 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800140
Rajendra Nayak441a4502009-12-13 22:19:23 +0100141 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800142 if (grp < 0)
143 return grp;
144
Rajendra Nayak441a4502009-12-13 22:19:23 +0100145 if (twl_class_is_4030())
146 grp |= P1_GRP_4030;
147 else
148 grp |= P1_GRP_6030;
149
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200150 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
151
152 udelay(info->delay);
153
154 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800155}
156
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100157static int twlreg_disable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800158{
159 struct twlreg_info *info = rdev_get_drvdata(rdev);
160 int grp;
161
Rajendra Nayak441a4502009-12-13 22:19:23 +0100162 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800163 if (grp < 0)
164 return grp;
165
Rajendra Nayak441a4502009-12-13 22:19:23 +0100166 if (twl_class_is_4030())
Juha Keski-Saaricf9836f2009-12-16 15:28:00 +0200167 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100168 else
Juha Keski-Saaricf9836f2009-12-16 15:28:00 +0200169 grp &= ~(P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100170
171 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
David Brownellfa16a5c2009-02-08 10:37:06 -0800172}
173
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100174static int twlreg_get_status(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800175{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100176 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800177
Rajendra Nayak441a4502009-12-13 22:19:23 +0100178 if (twl_class_is_6030())
179 return 0; /* FIXME return for 6030 regulator */
180
David Brownellfa16a5c2009-02-08 10:37:06 -0800181 if (state < 0)
182 return state;
183 state &= 0x0f;
184
185 /* assume state != WARM_RESET; we'd not be running... */
186 if (!state)
187 return REGULATOR_STATUS_OFF;
188 return (state & BIT(3))
189 ? REGULATOR_STATUS_NORMAL
190 : REGULATOR_STATUS_STANDBY;
191}
192
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100193static int twlreg_set_mode(struct regulator_dev *rdev, unsigned mode)
David Brownellfa16a5c2009-02-08 10:37:06 -0800194{
195 struct twlreg_info *info = rdev_get_drvdata(rdev);
196 unsigned message;
197 int status;
198
Rajendra Nayak441a4502009-12-13 22:19:23 +0100199 if (twl_class_is_6030())
200 return 0; /* FIXME return for 6030 regulator */
201
David Brownellfa16a5c2009-02-08 10:37:06 -0800202 /* We can only set the mode through state machine commands... */
203 switch (mode) {
204 case REGULATOR_MODE_NORMAL:
205 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
206 break;
207 case REGULATOR_MODE_STANDBY:
208 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
209 break;
210 default:
211 return -EINVAL;
212 }
213
214 /* Ensure the resource is associated with some group */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100215 status = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800216 if (status < 0)
217 return status;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100218 if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
David Brownellfa16a5c2009-02-08 10:37:06 -0800219 return -EACCES;
220
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100221 status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800222 message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
223 if (status < 0)
David Brownellfa16a5c2009-02-08 10:37:06 -0800224 return status;
225
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100226 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800227 message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
David Brownellfa16a5c2009-02-08 10:37:06 -0800228}
229
230/*----------------------------------------------------------------------*/
231
232/*
233 * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
234 * select field in its control register. We use tables indexed by VSEL
235 * to record voltages in milliVolts. (Accuracy is about three percent.)
236 *
237 * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
238 * currently handled by listing two slightly different VAUX2 regulators,
239 * only one of which will be configured.
240 *
241 * VSEL values documented as "TI cannot support these values" are flagged
242 * in these tables as UNSUP() values; we normally won't assign them.
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200243 *
244 * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
245 * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
David Brownellfa16a5c2009-02-08 10:37:06 -0800246 */
247#ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
248#define UNSUP_MASK 0x0000
249#else
250#define UNSUP_MASK 0x8000
251#endif
252
253#define UNSUP(x) (UNSUP_MASK | (x))
254#define IS_UNSUP(x) (UNSUP_MASK & (x))
255#define LDO_MV(x) (~UNSUP_MASK & (x))
256
257
258static const u16 VAUX1_VSEL_table[] = {
259 UNSUP(1500), UNSUP(1800), 2500, 2800,
260 3000, 3000, 3000, 3000,
261};
262static const u16 VAUX2_4030_VSEL_table[] = {
263 UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
264 1500, 1800, UNSUP(1850), 2500,
265 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
266 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
267};
268static const u16 VAUX2_VSEL_table[] = {
269 1700, 1700, 1900, 1300,
270 1500, 1800, 2000, 2500,
271 2100, 2800, 2200, 2300,
272 2400, 2400, 2400, 2400,
273};
274static const u16 VAUX3_VSEL_table[] = {
275 1500, 1800, 2500, 2800,
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200276 3000, 3000, 3000, 3000,
David Brownellfa16a5c2009-02-08 10:37:06 -0800277};
278static const u16 VAUX4_VSEL_table[] = {
279 700, 1000, 1200, UNSUP(1300),
280 1500, 1800, UNSUP(1850), 2500,
David Brownell1897e742009-03-10 11:51:15 -0800281 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
282 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
David Brownellfa16a5c2009-02-08 10:37:06 -0800283};
284static const u16 VMMC1_VSEL_table[] = {
285 1850, 2850, 3000, 3150,
286};
287static const u16 VMMC2_VSEL_table[] = {
288 UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
289 UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
290 2600, 2800, 2850, 3000,
291 3150, 3150, 3150, 3150,
292};
293static const u16 VPLL1_VSEL_table[] = {
294 1000, 1200, 1300, 1800,
295 UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
296};
297static const u16 VPLL2_VSEL_table[] = {
298 700, 1000, 1200, 1300,
299 UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
300 UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
301 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
302};
303static const u16 VSIM_VSEL_table[] = {
304 UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
305 2800, 3000, 3000, 3000,
306};
307static const u16 VDAC_VSEL_table[] = {
308 1200, 1300, 1800, 1800,
309};
Juha Keski-Saari07fc4932009-12-16 15:27:55 +0200310static const u16 VDD1_VSEL_table[] = {
311 800, 1450,
312};
313static const u16 VDD2_VSEL_table[] = {
314 800, 1450, 1500,
315};
316static const u16 VIO_VSEL_table[] = {
317 1800, 1850,
318};
319static const u16 VINTANA2_VSEL_table[] = {
320 2500, 2750,
321};
David Brownellfa16a5c2009-02-08 10:37:06 -0800322
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530323static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800324{
325 struct twlreg_info *info = rdev_get_drvdata(rdev);
326 int mV = info->table[index];
327
328 return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
329}
330
David Brownellfa16a5c2009-02-08 10:37:06 -0800331static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000332twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
333 unsigned *selector)
David Brownellfa16a5c2009-02-08 10:37:06 -0800334{
335 struct twlreg_info *info = rdev_get_drvdata(rdev);
336 int vsel;
337
338 for (vsel = 0; vsel < info->table_len; vsel++) {
339 int mV = info->table[vsel];
340 int uV;
341
342 if (IS_UNSUP(mV))
343 continue;
344 uV = LDO_MV(mV) * 1000;
345
David Brownell66b659e2009-02-26 11:50:14 -0800346 /* REVISIT for VAUX2, first match may not be best/lowest */
347
David Brownellfa16a5c2009-02-08 10:37:06 -0800348 /* use the first in-range value */
Mark Brown3a93f2a2010-11-10 14:38:29 +0000349 if (min_uV <= uV && uV <= max_uV) {
350 *selector = vsel;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100351 return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
352 VREG_VOLTAGE, vsel);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000353 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800354 }
355
356 return -EDOM;
357}
358
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530359static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800360{
361 struct twlreg_info *info = rdev_get_drvdata(rdev);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100362 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
363 VREG_VOLTAGE);
David Brownellfa16a5c2009-02-08 10:37:06 -0800364
365 if (vsel < 0)
366 return vsel;
367
368 vsel &= info->table_len - 1;
369 return LDO_MV(info->table[vsel]) * 1000;
370}
371
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530372static struct regulator_ops twl4030ldo_ops = {
373 .list_voltage = twl4030ldo_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800374
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530375 .set_voltage = twl4030ldo_set_voltage,
376 .get_voltage = twl4030ldo_get_voltage,
377
378 .enable = twlreg_enable,
379 .disable = twlreg_disable,
380 .is_enabled = twlreg_is_enabled,
381
382 .set_mode = twlreg_set_mode,
383
384 .get_status = twlreg_get_status,
385};
386
387static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
388{
389 struct twlreg_info *info = rdev_get_drvdata(rdev);
390
391 return ((info->min_mV + (index * 100)) * 1000);
392}
393
394static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000395twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
396 unsigned *selector)
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530397{
398 struct twlreg_info *info = rdev_get_drvdata(rdev);
399 int vsel;
400
401 if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
402 return -EDOM;
403
404 /*
405 * Use the below formula to calculate vsel
406 * mV = 1000mv + 100mv * (vsel - 1)
407 */
408 vsel = (min_uV/1000 - 1000)/100 + 1;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000409 *selector = vsel;
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530410 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
411
412}
413
414static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
415{
416 struct twlreg_info *info = rdev_get_drvdata(rdev);
417 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
418 VREG_VOLTAGE);
419
420 if (vsel < 0)
421 return vsel;
422
423 /*
424 * Use the below formula to calculate vsel
425 * mV = 1000mv + 100mv * (vsel - 1)
426 */
427 return (1000 + (100 * (vsel - 1))) * 1000;
428}
429
430static struct regulator_ops twl6030ldo_ops = {
431 .list_voltage = twl6030ldo_list_voltage,
432
433 .set_voltage = twl6030ldo_set_voltage,
434 .get_voltage = twl6030ldo_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800435
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100436 .enable = twlreg_enable,
437 .disable = twlreg_disable,
438 .is_enabled = twlreg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800439
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100440 .set_mode = twlreg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800441
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100442 .get_status = twlreg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800443};
444
445/*----------------------------------------------------------------------*/
446
447/*
448 * Fixed voltage LDOs don't have a VSEL field to update.
449 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100450static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800451{
452 struct twlreg_info *info = rdev_get_drvdata(rdev);
453
454 return info->min_mV * 1000;
455}
456
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100457static int twlfixed_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800458{
459 struct twlreg_info *info = rdev_get_drvdata(rdev);
460
461 return info->min_mV * 1000;
462}
463
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100464static struct regulator_ops twlfixed_ops = {
465 .list_voltage = twlfixed_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800466
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100467 .get_voltage = twlfixed_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800468
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100469 .enable = twlreg_enable,
470 .disable = twlreg_disable,
471 .is_enabled = twlreg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800472
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100473 .set_mode = twlreg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800474
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100475 .get_status = twlreg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800476};
477
Balaji T K8e6de4a2011-02-10 18:44:50 +0530478static struct regulator_ops twl6030_fixed_resource = {
479 .enable = twlreg_enable,
480 .disable = twlreg_disable,
481 .is_enabled = twlreg_is_enabled,
482 .get_status = twlreg_get_status,
483};
484
David Brownellfa16a5c2009-02-08 10:37:06 -0800485/*----------------------------------------------------------------------*/
486
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200487#define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
488 remap_conf) \
489 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
490 remap_conf, TWL4030)
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200491#define TWL6030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
492 remap_conf) \
493 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
494 remap_conf, TWL6030)
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100495
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530496#define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800497 .base = offset, \
498 .id = num, \
499 .table_len = ARRAY_SIZE(label##_VSEL_table), \
500 .table = label##_VSEL_table, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200501 .delay = turnon_delay, \
502 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800503 .desc = { \
504 .name = #label, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530505 .id = TWL4030_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800506 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530507 .ops = &twl4030ldo_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800508 .type = REGULATOR_VOLTAGE, \
509 .owner = THIS_MODULE, \
510 }, \
511 }
512
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530513#define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts, num, \
514 remap_conf) { \
515 .base = offset, \
516 .id = num, \
517 .min_mV = min_mVolts, \
518 .max_mV = max_mVolts, \
519 .remap = remap_conf, \
520 .desc = { \
521 .name = #label, \
522 .id = TWL6030_REG_##label, \
523 .n_voltages = (max_mVolts - min_mVolts)/100, \
524 .ops = &twl6030ldo_ops, \
525 .type = REGULATOR_VOLTAGE, \
526 .owner = THIS_MODULE, \
527 }, \
528 }
529
530
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200531#define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
532 family) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800533 .base = offset, \
534 .id = num, \
535 .min_mV = mVolts, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200536 .delay = turnon_delay, \
537 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800538 .desc = { \
539 .name = #label, \
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100540 .id = family##_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800541 .n_voltages = 1, \
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100542 .ops = &twlfixed_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800543 .type = REGULATOR_VOLTAGE, \
544 .owner = THIS_MODULE, \
545 }, \
546 }
547
Balaji T K8e6de4a2011-02-10 18:44:50 +0530548#define TWL6030_FIXED_RESOURCE(label, offset, num, turnon_delay, remap_conf) { \
549 .base = offset, \
550 .id = num, \
551 .delay = turnon_delay, \
552 .remap = remap_conf, \
553 .desc = { \
554 .name = #label, \
555 .id = TWL6030_REG_##label, \
556 .ops = &twl6030_fixed_resource, \
557 .type = REGULATOR_VOLTAGE, \
558 .owner = THIS_MODULE, \
559 }, \
560 }
561
David Brownellfa16a5c2009-02-08 10:37:06 -0800562/*
563 * We list regulators here if systems need some level of
564 * software control over them after boot.
565 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100566static struct twlreg_info twl_regs[] = {
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200567 TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
568 TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
569 TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
570 TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
571 TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
572 TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
573 TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
574 TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
575 TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
576 TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
577 TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
578 TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
579 TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
580 TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
581 TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
582 TWL4030_ADJUSTABLE_LDO(VDD1, 0x55, 15, 1000, 0x08),
583 TWL4030_ADJUSTABLE_LDO(VDD2, 0x63, 16, 1000, 0x08),
584 TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
585 TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
586 TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
David Brownellfa16a5c2009-02-08 10:37:06 -0800587 /* VUSBCP is managed *only* by the USB subchip */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100588
589 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200590 /* Turnon-delay and remap configuration values for 6030 are not
591 verified since the specification is not public */
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530592 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300, 1, 0x21),
593 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300, 2, 0x21),
594 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300, 3, 0x21),
595 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300, 4, 0x21),
596 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300, 5, 0x21),
597 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300, 7, 0x21),
Rajendra Nayak2ebcf632010-02-17 20:54:15 +0530598 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 15, 0, 0x21),
599 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 16, 0, 0x21),
600 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 17, 0, 0x21),
Balaji T K8e6de4a2011-02-10 18:44:50 +0530601 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 18, 0, 0x21),
602 TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 48, 0, 0x21),
David Brownellfa16a5c2009-02-08 10:37:06 -0800603};
604
Dmitry Torokhov24c29022010-02-23 23:38:01 -0800605static int __devinit twlreg_probe(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800606{
607 int i;
608 struct twlreg_info *info;
609 struct regulator_init_data *initdata;
610 struct regulation_constraints *c;
611 struct regulator_dev *rdev;
David Brownellfa16a5c2009-02-08 10:37:06 -0800612
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100613 for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
614 if (twl_regs[i].desc.id != pdev->id)
David Brownellfa16a5c2009-02-08 10:37:06 -0800615 continue;
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100616 info = twl_regs + i;
David Brownellfa16a5c2009-02-08 10:37:06 -0800617 break;
618 }
619 if (!info)
620 return -ENODEV;
621
622 initdata = pdev->dev.platform_data;
623 if (!initdata)
624 return -EINVAL;
625
626 /* Constrain board-specific capabilities according to what
627 * this driver and the chip itself can actually do.
628 */
629 c = &initdata->constraints;
David Brownellfa16a5c2009-02-08 10:37:06 -0800630 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
631 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
632 | REGULATOR_CHANGE_MODE
633 | REGULATOR_CHANGE_STATUS;
Juha Keski-Saari205e5cd2009-12-16 15:27:56 +0200634 switch (pdev->id) {
635 case TWL4030_REG_VIO:
636 case TWL4030_REG_VDD1:
637 case TWL4030_REG_VDD2:
638 case TWL4030_REG_VPLL1:
639 case TWL4030_REG_VINTANA1:
640 case TWL4030_REG_VINTANA2:
641 case TWL4030_REG_VINTDIG:
642 c->always_on = true;
643 break;
644 default:
645 break;
646 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800647
648 rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
649 if (IS_ERR(rdev)) {
650 dev_err(&pdev->dev, "can't register %s, %ld\n",
651 info->desc.name, PTR_ERR(rdev));
652 return PTR_ERR(rdev);
653 }
654 platform_set_drvdata(pdev, rdev);
655
Juha Keski-Saari30010fa2009-12-16 15:27:58 +0200656 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
657 info->remap);
658
David Brownellfa16a5c2009-02-08 10:37:06 -0800659 /* NOTE: many regulators support short-circuit IRQs (presentable
660 * as REGULATOR_OVER_CURRENT notifications?) configured via:
661 * - SC_CONFIG
662 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
663 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
664 * - IT_CONFIG
665 */
666
667 return 0;
668}
669
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100670static int __devexit twlreg_remove(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800671{
672 regulator_unregister(platform_get_drvdata(pdev));
673 return 0;
674}
675
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100676MODULE_ALIAS("platform:twl_reg");
David Brownellfa16a5c2009-02-08 10:37:06 -0800677
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100678static struct platform_driver twlreg_driver = {
679 .probe = twlreg_probe,
680 .remove = __devexit_p(twlreg_remove),
David Brownellfa16a5c2009-02-08 10:37:06 -0800681 /* NOTE: short name, to work around driver model truncation of
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100682 * "twl_regulator.12" (and friends) to "twl_regulator.1".
David Brownellfa16a5c2009-02-08 10:37:06 -0800683 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100684 .driver.name = "twl_reg",
David Brownellfa16a5c2009-02-08 10:37:06 -0800685 .driver.owner = THIS_MODULE,
686};
687
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100688static int __init twlreg_init(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800689{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100690 return platform_driver_register(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800691}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100692subsys_initcall(twlreg_init);
David Brownellfa16a5c2009-02-08 10:37:06 -0800693
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100694static void __exit twlreg_exit(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800695{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100696 platform_driver_unregister(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800697}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100698module_exit(twlreg_exit)
David Brownellfa16a5c2009-02-08 10:37:06 -0800699
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100700MODULE_DESCRIPTION("TWL regulator driver");
David Brownellfa16a5c2009-02-08 10:37:06 -0800701MODULE_LICENSE("GPL");