blob: 4702b8a7e0ef8b79e8c8f030f4a8bac80b14772d [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
Saquib Herman21657ebf2011-04-01 10:22:42 +053079/* TWL6030 LDO register values for CFG_STATE */
80#define TWL6030_CFG_STATE_OFF 0x00
81#define TWL6030_CFG_STATE_ON 0x01
Saquib Herman9a0244a2011-04-01 10:22:45 +053082#define TWL6030_CFG_STATE_OFF2 0x02
83#define TWL6030_CFG_STATE_SLEEP 0x03
Saquib Herman21657ebf2011-04-01 10:22:42 +053084#define TWL6030_CFG_STATE_GRP_SHIFT 5
Saquib Hermanb2456772011-04-01 10:22:44 +053085#define TWL6030_CFG_STATE_APP_SHIFT 2
86#define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
87#define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
88 TWL6030_CFG_STATE_APP_SHIFT)
Saquib Herman21657ebf2011-04-01 10:22:42 +053089
David Brownellfa16a5c2009-02-08 10:37:06 -080090static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +010091twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
David Brownellfa16a5c2009-02-08 10:37:06 -080092{
93 u8 value;
94 int status;
95
Rajendra Nayak441a4502009-12-13 22:19:23 +010096 status = twl_i2c_read_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -080097 &value, info->base + offset);
98 return (status < 0) ? status : value;
99}
100
101static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +0100102twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
103 u8 value)
David Brownellfa16a5c2009-02-08 10:37:06 -0800104{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100105 return twl_i2c_write_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -0800106 value, info->base + offset);
107}
108
109/*----------------------------------------------------------------------*/
110
111/* generic power resource operations, which work on all regulators */
112
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100113static int twlreg_grp(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800114{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100115 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
116 VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800117}
118
119/*
120 * Enable/disable regulators by joining/leaving the P1 (processor) group.
121 * We assume nobody else is updating the DEV_GRP registers.
122 */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100123/* definition for 4030 family */
124#define P3_GRP_4030 BIT(7) /* "peripherals" */
125#define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
126#define P1_GRP_4030 BIT(5) /* CPU/Linux */
127/* definition for 6030 family */
128#define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
129#define P2_GRP_6030 BIT(1) /* "peripherals" */
130#define P1_GRP_6030 BIT(0) /* CPU/Linux */
David Brownellfa16a5c2009-02-08 10:37:06 -0800131
Saquib Hermanb2456772011-04-01 10:22:44 +0530132static int twl4030reg_is_enabled(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800133{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100134 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800135
136 if (state < 0)
137 return state;
138
Saquib Hermanb2456772011-04-01 10:22:44 +0530139 return state & P1_GRP_4030;
140}
141
142static int twl6030reg_is_enabled(struct regulator_dev *rdev)
143{
144 struct twlreg_info *info = rdev_get_drvdata(rdev);
145 int grp, val;
146
147 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
148 if (grp < 0)
149 return grp;
150
151 grp &= P1_GRP_6030;
152
153 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
154 val = TWL6030_CFG_STATE_APP(val);
155
156 return grp && (val == TWL6030_CFG_STATE_ON);
David Brownellfa16a5c2009-02-08 10:37:06 -0800157}
158
Balaji T Kf8c29402011-05-20 19:03:51 +0530159static int twl4030reg_enable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800160{
161 struct twlreg_info *info = rdev_get_drvdata(rdev);
162 int grp;
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200163 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800164
Rajendra Nayak441a4502009-12-13 22:19:23 +0100165 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800166 if (grp < 0)
167 return grp;
168
Balaji T Kf8c29402011-05-20 19:03:51 +0530169 grp |= P1_GRP_4030;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100170
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200171 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
172
Balaji T Kf8c29402011-05-20 19:03:51 +0530173 udelay(info->delay);
174
175 return ret;
176}
177
178static int twl6030reg_enable(struct regulator_dev *rdev)
179{
180 struct twlreg_info *info = rdev_get_drvdata(rdev);
181 int grp;
182 int ret;
183
184 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
185 if (grp < 0)
186 return grp;
187
188 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
189 grp << TWL6030_CFG_STATE_GRP_SHIFT |
190 TWL6030_CFG_STATE_ON);
Saquib Herman21657ebf2011-04-01 10:22:42 +0530191
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200192 udelay(info->delay);
193
194 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800195}
196
Balaji T K0ff38972011-05-20 19:03:52 +0530197static int twl4030reg_disable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800198{
199 struct twlreg_info *info = rdev_get_drvdata(rdev);
200 int grp;
Saquib Herman21657ebf2011-04-01 10:22:42 +0530201 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800202
Rajendra Nayak441a4502009-12-13 22:19:23 +0100203 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800204 if (grp < 0)
205 return grp;
206
Balaji T K0ff38972011-05-20 19:03:52 +0530207 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100208
Saquib Herman21657ebf2011-04-01 10:22:42 +0530209 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
210
Balaji T K0ff38972011-05-20 19:03:52 +0530211 return ret;
212}
213
214static int twl6030reg_disable(struct regulator_dev *rdev)
215{
216 struct twlreg_info *info = rdev_get_drvdata(rdev);
217 int grp = 0;
218 int ret;
219
220 grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
221
222 /* For 6030, set the off state for all grps enabled */
223 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
224 (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
225 TWL6030_CFG_STATE_OFF);
Saquib Herman21657ebf2011-04-01 10:22:42 +0530226
227 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800228}
229
Saquib Herman9a0244a2011-04-01 10:22:45 +0530230static int twl4030reg_get_status(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800231{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100232 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800233
234 if (state < 0)
235 return state;
236 state &= 0x0f;
237
238 /* assume state != WARM_RESET; we'd not be running... */
239 if (!state)
240 return REGULATOR_STATUS_OFF;
241 return (state & BIT(3))
242 ? REGULATOR_STATUS_NORMAL
243 : REGULATOR_STATUS_STANDBY;
244}
245
Saquib Herman9a0244a2011-04-01 10:22:45 +0530246static int twl6030reg_get_status(struct regulator_dev *rdev)
247{
248 struct twlreg_info *info = rdev_get_drvdata(rdev);
249 int val;
250
251 val = twlreg_grp(rdev);
252 if (val < 0)
253 return val;
254
255 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
256
257 switch (TWL6030_CFG_STATE_APP(val)) {
258 case TWL6030_CFG_STATE_ON:
259 return REGULATOR_STATUS_NORMAL;
260
261 case TWL6030_CFG_STATE_SLEEP:
262 return REGULATOR_STATUS_STANDBY;
263
264 case TWL6030_CFG_STATE_OFF:
265 case TWL6030_CFG_STATE_OFF2:
266 default:
267 break;
268 }
269
270 return REGULATOR_STATUS_OFF;
271}
272
Saquib Herman1a399622011-04-01 10:22:46 +0530273static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
David Brownellfa16a5c2009-02-08 10:37:06 -0800274{
275 struct twlreg_info *info = rdev_get_drvdata(rdev);
276 unsigned message;
277 int status;
278
279 /* We can only set the mode through state machine commands... */
280 switch (mode) {
281 case REGULATOR_MODE_NORMAL:
282 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
283 break;
284 case REGULATOR_MODE_STANDBY:
285 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
286 break;
287 default:
288 return -EINVAL;
289 }
290
291 /* Ensure the resource is associated with some group */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100292 status = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800293 if (status < 0)
294 return status;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100295 if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
David Brownellfa16a5c2009-02-08 10:37:06 -0800296 return -EACCES;
297
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100298 status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800299 message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
300 if (status < 0)
David Brownellfa16a5c2009-02-08 10:37:06 -0800301 return status;
302
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100303 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800304 message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
David Brownellfa16a5c2009-02-08 10:37:06 -0800305}
306
Saquib Herman1a399622011-04-01 10:22:46 +0530307static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
308{
309 struct twlreg_info *info = rdev_get_drvdata(rdev);
310 int grp;
311 int val;
312
313 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
314
315 if (grp < 0)
316 return grp;
317
318 /* Compose the state register settings */
319 val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
320 /* We can only set the mode through state machine commands... */
321 switch (mode) {
322 case REGULATOR_MODE_NORMAL:
323 val |= TWL6030_CFG_STATE_ON;
324 break;
325 case REGULATOR_MODE_STANDBY:
326 val |= TWL6030_CFG_STATE_SLEEP;
327 break;
328
329 default:
330 return -EINVAL;
331 }
332
333 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
334}
335
David Brownellfa16a5c2009-02-08 10:37:06 -0800336/*----------------------------------------------------------------------*/
337
338/*
339 * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
340 * select field in its control register. We use tables indexed by VSEL
341 * to record voltages in milliVolts. (Accuracy is about three percent.)
342 *
343 * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
344 * currently handled by listing two slightly different VAUX2 regulators,
345 * only one of which will be configured.
346 *
347 * VSEL values documented as "TI cannot support these values" are flagged
348 * in these tables as UNSUP() values; we normally won't assign them.
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200349 *
350 * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
351 * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
David Brownellfa16a5c2009-02-08 10:37:06 -0800352 */
353#ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
354#define UNSUP_MASK 0x0000
355#else
356#define UNSUP_MASK 0x8000
357#endif
358
359#define UNSUP(x) (UNSUP_MASK | (x))
360#define IS_UNSUP(x) (UNSUP_MASK & (x))
361#define LDO_MV(x) (~UNSUP_MASK & (x))
362
363
364static const u16 VAUX1_VSEL_table[] = {
365 UNSUP(1500), UNSUP(1800), 2500, 2800,
366 3000, 3000, 3000, 3000,
367};
368static const u16 VAUX2_4030_VSEL_table[] = {
369 UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
370 1500, 1800, UNSUP(1850), 2500,
371 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
372 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
373};
374static const u16 VAUX2_VSEL_table[] = {
375 1700, 1700, 1900, 1300,
376 1500, 1800, 2000, 2500,
377 2100, 2800, 2200, 2300,
378 2400, 2400, 2400, 2400,
379};
380static const u16 VAUX3_VSEL_table[] = {
381 1500, 1800, 2500, 2800,
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200382 3000, 3000, 3000, 3000,
David Brownellfa16a5c2009-02-08 10:37:06 -0800383};
384static const u16 VAUX4_VSEL_table[] = {
385 700, 1000, 1200, UNSUP(1300),
386 1500, 1800, UNSUP(1850), 2500,
David Brownell1897e742009-03-10 11:51:15 -0800387 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
388 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
David Brownellfa16a5c2009-02-08 10:37:06 -0800389};
390static const u16 VMMC1_VSEL_table[] = {
391 1850, 2850, 3000, 3150,
392};
393static const u16 VMMC2_VSEL_table[] = {
394 UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
395 UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
396 2600, 2800, 2850, 3000,
397 3150, 3150, 3150, 3150,
398};
399static const u16 VPLL1_VSEL_table[] = {
400 1000, 1200, 1300, 1800,
401 UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
402};
403static const u16 VPLL2_VSEL_table[] = {
404 700, 1000, 1200, 1300,
405 UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
406 UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
407 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
408};
409static const u16 VSIM_VSEL_table[] = {
410 UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
411 2800, 3000, 3000, 3000,
412};
413static const u16 VDAC_VSEL_table[] = {
414 1200, 1300, 1800, 1800,
415};
Juha Keski-Saari07fc4932009-12-16 15:27:55 +0200416static const u16 VDD1_VSEL_table[] = {
417 800, 1450,
418};
419static const u16 VDD2_VSEL_table[] = {
420 800, 1450, 1500,
421};
422static const u16 VIO_VSEL_table[] = {
423 1800, 1850,
424};
425static const u16 VINTANA2_VSEL_table[] = {
426 2500, 2750,
427};
David Brownellfa16a5c2009-02-08 10:37:06 -0800428
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530429static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800430{
431 struct twlreg_info *info = rdev_get_drvdata(rdev);
432 int mV = info->table[index];
433
434 return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
435}
436
David Brownellfa16a5c2009-02-08 10:37:06 -0800437static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000438twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
439 unsigned *selector)
David Brownellfa16a5c2009-02-08 10:37:06 -0800440{
441 struct twlreg_info *info = rdev_get_drvdata(rdev);
442 int vsel;
443
444 for (vsel = 0; vsel < info->table_len; vsel++) {
445 int mV = info->table[vsel];
446 int uV;
447
448 if (IS_UNSUP(mV))
449 continue;
450 uV = LDO_MV(mV) * 1000;
451
David Brownell66b659e2009-02-26 11:50:14 -0800452 /* REVISIT for VAUX2, first match may not be best/lowest */
453
David Brownellfa16a5c2009-02-08 10:37:06 -0800454 /* use the first in-range value */
Mark Brown3a93f2a2010-11-10 14:38:29 +0000455 if (min_uV <= uV && uV <= max_uV) {
456 *selector = vsel;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100457 return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
458 VREG_VOLTAGE, vsel);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000459 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800460 }
461
462 return -EDOM;
463}
464
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530465static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800466{
467 struct twlreg_info *info = rdev_get_drvdata(rdev);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100468 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
469 VREG_VOLTAGE);
David Brownellfa16a5c2009-02-08 10:37:06 -0800470
471 if (vsel < 0)
472 return vsel;
473
474 vsel &= info->table_len - 1;
475 return LDO_MV(info->table[vsel]) * 1000;
476}
477
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530478static struct regulator_ops twl4030ldo_ops = {
479 .list_voltage = twl4030ldo_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800480
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530481 .set_voltage = twl4030ldo_set_voltage,
482 .get_voltage = twl4030ldo_get_voltage,
483
Balaji T Kf8c29402011-05-20 19:03:51 +0530484 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530485 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530486 .is_enabled = twl4030reg_is_enabled,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530487
Saquib Herman1a399622011-04-01 10:22:46 +0530488 .set_mode = twl4030reg_set_mode,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530489
Saquib Herman9a0244a2011-04-01 10:22:45 +0530490 .get_status = twl4030reg_get_status,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530491};
492
493static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
494{
495 struct twlreg_info *info = rdev_get_drvdata(rdev);
496
497 return ((info->min_mV + (index * 100)) * 1000);
498}
499
500static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000501twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
502 unsigned *selector)
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530503{
504 struct twlreg_info *info = rdev_get_drvdata(rdev);
505 int vsel;
506
507 if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
508 return -EDOM;
509
510 /*
511 * Use the below formula to calculate vsel
512 * mV = 1000mv + 100mv * (vsel - 1)
513 */
514 vsel = (min_uV/1000 - 1000)/100 + 1;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000515 *selector = vsel;
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530516 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
517
518}
519
520static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
521{
522 struct twlreg_info *info = rdev_get_drvdata(rdev);
523 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
524 VREG_VOLTAGE);
525
526 if (vsel < 0)
527 return vsel;
528
529 /*
530 * Use the below formula to calculate vsel
531 * mV = 1000mv + 100mv * (vsel - 1)
532 */
533 return (1000 + (100 * (vsel - 1))) * 1000;
534}
535
536static struct regulator_ops twl6030ldo_ops = {
537 .list_voltage = twl6030ldo_list_voltage,
538
539 .set_voltage = twl6030ldo_set_voltage,
540 .get_voltage = twl6030ldo_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800541
Balaji T Kf8c29402011-05-20 19:03:51 +0530542 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530543 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530544 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800545
Saquib Herman1a399622011-04-01 10:22:46 +0530546 .set_mode = twl6030reg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800547
Saquib Herman9a0244a2011-04-01 10:22:45 +0530548 .get_status = twl6030reg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800549};
550
551/*----------------------------------------------------------------------*/
552
553/*
554 * Fixed voltage LDOs don't have a VSEL field to update.
555 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100556static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800557{
558 struct twlreg_info *info = rdev_get_drvdata(rdev);
559
560 return info->min_mV * 1000;
561}
562
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100563static int twlfixed_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800564{
565 struct twlreg_info *info = rdev_get_drvdata(rdev);
566
567 return info->min_mV * 1000;
568}
569
Saquib Hermanb2456772011-04-01 10:22:44 +0530570static struct regulator_ops twl4030fixed_ops = {
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100571 .list_voltage = twlfixed_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800572
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100573 .get_voltage = twlfixed_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800574
Balaji T Kf8c29402011-05-20 19:03:51 +0530575 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530576 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530577 .is_enabled = twl4030reg_is_enabled,
578
Saquib Herman1a399622011-04-01 10:22:46 +0530579 .set_mode = twl4030reg_set_mode,
Saquib Hermanb2456772011-04-01 10:22:44 +0530580
Saquib Herman9a0244a2011-04-01 10:22:45 +0530581 .get_status = twl4030reg_get_status,
Saquib Hermanb2456772011-04-01 10:22:44 +0530582};
583
584static struct regulator_ops twl6030fixed_ops = {
585 .list_voltage = twlfixed_list_voltage,
586
587 .get_voltage = twlfixed_get_voltage,
588
Balaji T Kf8c29402011-05-20 19:03:51 +0530589 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530590 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530591 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800592
Saquib Herman1a399622011-04-01 10:22:46 +0530593 .set_mode = twl6030reg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800594
Saquib Herman9a0244a2011-04-01 10:22:45 +0530595 .get_status = twl6030reg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800596};
597
Balaji T K8e6de4a2011-02-10 18:44:50 +0530598static struct regulator_ops twl6030_fixed_resource = {
Balaji T Kf8c29402011-05-20 19:03:51 +0530599 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530600 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530601 .is_enabled = twl6030reg_is_enabled,
Saquib Herman9a0244a2011-04-01 10:22:45 +0530602 .get_status = twl6030reg_get_status,
Balaji T K8e6de4a2011-02-10 18:44:50 +0530603};
604
David Brownellfa16a5c2009-02-08 10:37:06 -0800605/*----------------------------------------------------------------------*/
606
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200607#define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
608 remap_conf) \
609 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530610 remap_conf, TWL4030, twl4030fixed_ops)
Saquib Herman776dc922011-04-01 10:22:43 +0530611#define TWL6030_FIXED_LDO(label, offset, mVolts, num, turnon_delay) \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200612 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530613 0x0, TWL6030, twl6030fixed_ops)
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100614
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530615#define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800616 .base = offset, \
617 .id = num, \
618 .table_len = ARRAY_SIZE(label##_VSEL_table), \
619 .table = label##_VSEL_table, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200620 .delay = turnon_delay, \
621 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800622 .desc = { \
623 .name = #label, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530624 .id = TWL4030_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800625 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530626 .ops = &twl4030ldo_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800627 .type = REGULATOR_VOLTAGE, \
628 .owner = THIS_MODULE, \
629 }, \
630 }
631
Saquib Herman776dc922011-04-01 10:22:43 +0530632#define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts, num) { \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530633 .base = offset, \
634 .id = num, \
635 .min_mV = min_mVolts, \
636 .max_mV = max_mVolts, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530637 .desc = { \
638 .name = #label, \
639 .id = TWL6030_REG_##label, \
640 .n_voltages = (max_mVolts - min_mVolts)/100, \
641 .ops = &twl6030ldo_ops, \
642 .type = REGULATOR_VOLTAGE, \
643 .owner = THIS_MODULE, \
644 }, \
645 }
646
647
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200648#define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530649 family, operations) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800650 .base = offset, \
651 .id = num, \
652 .min_mV = mVolts, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200653 .delay = turnon_delay, \
654 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800655 .desc = { \
656 .name = #label, \
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100657 .id = family##_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800658 .n_voltages = 1, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530659 .ops = &operations, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800660 .type = REGULATOR_VOLTAGE, \
661 .owner = THIS_MODULE, \
662 }, \
663 }
664
Saquib Herman776dc922011-04-01 10:22:43 +0530665#define TWL6030_FIXED_RESOURCE(label, offset, num, turnon_delay) { \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530666 .base = offset, \
667 .id = num, \
668 .delay = turnon_delay, \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530669 .desc = { \
670 .name = #label, \
671 .id = TWL6030_REG_##label, \
672 .ops = &twl6030_fixed_resource, \
673 .type = REGULATOR_VOLTAGE, \
674 .owner = THIS_MODULE, \
675 }, \
676 }
677
David Brownellfa16a5c2009-02-08 10:37:06 -0800678/*
679 * We list regulators here if systems need some level of
680 * software control over them after boot.
681 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100682static struct twlreg_info twl_regs[] = {
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200683 TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
684 TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
685 TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
686 TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
687 TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
688 TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
689 TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
690 TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
691 TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
692 TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
693 TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
694 TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
695 TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
696 TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
697 TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
698 TWL4030_ADJUSTABLE_LDO(VDD1, 0x55, 15, 1000, 0x08),
699 TWL4030_ADJUSTABLE_LDO(VDD2, 0x63, 16, 1000, 0x08),
700 TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
701 TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
702 TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
David Brownellfa16a5c2009-02-08 10:37:06 -0800703 /* VUSBCP is managed *only* by the USB subchip */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100704
705 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200706 /* Turnon-delay and remap configuration values for 6030 are not
707 verified since the specification is not public */
Saquib Herman776dc922011-04-01 10:22:43 +0530708 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300, 1),
709 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300, 2),
710 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300, 3),
711 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300, 4),
712 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300, 5),
713 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300, 7),
714 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 15, 0),
715 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 16, 0),
716 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 17, 0),
717 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 18, 0),
718 TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 48, 0),
David Brownellfa16a5c2009-02-08 10:37:06 -0800719};
720
Dmitry Torokhov24c29022010-02-23 23:38:01 -0800721static int __devinit twlreg_probe(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800722{
723 int i;
724 struct twlreg_info *info;
725 struct regulator_init_data *initdata;
726 struct regulation_constraints *c;
727 struct regulator_dev *rdev;
David Brownellfa16a5c2009-02-08 10:37:06 -0800728
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100729 for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
730 if (twl_regs[i].desc.id != pdev->id)
David Brownellfa16a5c2009-02-08 10:37:06 -0800731 continue;
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100732 info = twl_regs + i;
David Brownellfa16a5c2009-02-08 10:37:06 -0800733 break;
734 }
735 if (!info)
736 return -ENODEV;
737
738 initdata = pdev->dev.platform_data;
739 if (!initdata)
740 return -EINVAL;
741
742 /* Constrain board-specific capabilities according to what
743 * this driver and the chip itself can actually do.
744 */
745 c = &initdata->constraints;
David Brownellfa16a5c2009-02-08 10:37:06 -0800746 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
747 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
748 | REGULATOR_CHANGE_MODE
749 | REGULATOR_CHANGE_STATUS;
Juha Keski-Saari205e5cd2009-12-16 15:27:56 +0200750 switch (pdev->id) {
751 case TWL4030_REG_VIO:
752 case TWL4030_REG_VDD1:
753 case TWL4030_REG_VDD2:
754 case TWL4030_REG_VPLL1:
755 case TWL4030_REG_VINTANA1:
756 case TWL4030_REG_VINTANA2:
757 case TWL4030_REG_VINTDIG:
758 c->always_on = true;
759 break;
760 default:
761 break;
762 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800763
764 rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
765 if (IS_ERR(rdev)) {
766 dev_err(&pdev->dev, "can't register %s, %ld\n",
767 info->desc.name, PTR_ERR(rdev));
768 return PTR_ERR(rdev);
769 }
770 platform_set_drvdata(pdev, rdev);
771
Saquib Herman776dc922011-04-01 10:22:43 +0530772 if (twl_class_is_4030())
773 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
Juha Keski-Saari30010fa2009-12-16 15:27:58 +0200774 info->remap);
775
David Brownellfa16a5c2009-02-08 10:37:06 -0800776 /* NOTE: many regulators support short-circuit IRQs (presentable
777 * as REGULATOR_OVER_CURRENT notifications?) configured via:
778 * - SC_CONFIG
779 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
780 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
781 * - IT_CONFIG
782 */
783
784 return 0;
785}
786
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100787static int __devexit twlreg_remove(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800788{
789 regulator_unregister(platform_get_drvdata(pdev));
790 return 0;
791}
792
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100793MODULE_ALIAS("platform:twl_reg");
David Brownellfa16a5c2009-02-08 10:37:06 -0800794
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100795static struct platform_driver twlreg_driver = {
796 .probe = twlreg_probe,
797 .remove = __devexit_p(twlreg_remove),
David Brownellfa16a5c2009-02-08 10:37:06 -0800798 /* NOTE: short name, to work around driver model truncation of
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100799 * "twl_regulator.12" (and friends) to "twl_regulator.1".
David Brownellfa16a5c2009-02-08 10:37:06 -0800800 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100801 .driver.name = "twl_reg",
David Brownellfa16a5c2009-02-08 10:37:06 -0800802 .driver.owner = THIS_MODULE,
803};
804
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100805static int __init twlreg_init(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800806{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100807 return platform_driver_register(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800808}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100809subsys_initcall(twlreg_init);
David Brownellfa16a5c2009-02-08 10:37:06 -0800810
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100811static void __exit twlreg_exit(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800812{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100813 platform_driver_unregister(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800814}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100815module_exit(twlreg_exit)
David Brownellfa16a5c2009-02-08 10:37:06 -0800816
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100817MODULE_DESCRIPTION("TWL regulator driver");
David Brownellfa16a5c2009-02-08 10:37:06 -0800818MODULE_LICENSE("GPL");