blob: cae168f0f7b32ad9cd97b8036f720f7e5b0757b4 [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
82#define TWL6030_CFG_STATE_GRP_SHIFT 5
Saquib Hermanb2456772011-04-01 10:22:44 +053083#define TWL6030_CFG_STATE_APP_SHIFT 2
84#define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
85#define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
86 TWL6030_CFG_STATE_APP_SHIFT)
Saquib Herman21657ebf2011-04-01 10:22:42 +053087
David Brownellfa16a5c2009-02-08 10:37:06 -080088static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +010089twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
David Brownellfa16a5c2009-02-08 10:37:06 -080090{
91 u8 value;
92 int status;
93
Rajendra Nayak441a4502009-12-13 22:19:23 +010094 status = twl_i2c_read_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -080095 &value, info->base + offset);
96 return (status < 0) ? status : value;
97}
98
99static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +0100100twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
101 u8 value)
David Brownellfa16a5c2009-02-08 10:37:06 -0800102{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100103 return twl_i2c_write_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -0800104 value, info->base + offset);
105}
106
107/*----------------------------------------------------------------------*/
108
109/* generic power resource operations, which work on all regulators */
110
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100111static int twlreg_grp(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800112{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100113 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
114 VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800115}
116
117/*
118 * Enable/disable regulators by joining/leaving the P1 (processor) group.
119 * We assume nobody else is updating the DEV_GRP registers.
120 */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100121/* definition for 4030 family */
122#define P3_GRP_4030 BIT(7) /* "peripherals" */
123#define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
124#define P1_GRP_4030 BIT(5) /* CPU/Linux */
125/* definition for 6030 family */
126#define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
127#define P2_GRP_6030 BIT(1) /* "peripherals" */
128#define P1_GRP_6030 BIT(0) /* CPU/Linux */
David Brownellfa16a5c2009-02-08 10:37:06 -0800129
Saquib Hermanb2456772011-04-01 10:22:44 +0530130static int twl4030reg_is_enabled(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800131{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100132 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800133
134 if (state < 0)
135 return state;
136
Saquib Hermanb2456772011-04-01 10:22:44 +0530137 return state & P1_GRP_4030;
138}
139
140static int twl6030reg_is_enabled(struct regulator_dev *rdev)
141{
142 struct twlreg_info *info = rdev_get_drvdata(rdev);
143 int grp, val;
144
145 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
146 if (grp < 0)
147 return grp;
148
149 grp &= P1_GRP_6030;
150
151 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
152 val = TWL6030_CFG_STATE_APP(val);
153
154 return grp && (val == TWL6030_CFG_STATE_ON);
David Brownellfa16a5c2009-02-08 10:37:06 -0800155}
156
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100157static int twlreg_enable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800158{
159 struct twlreg_info *info = rdev_get_drvdata(rdev);
160 int grp;
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200161 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800162
Rajendra Nayak441a4502009-12-13 22:19:23 +0100163 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800164 if (grp < 0)
165 return grp;
166
Rajendra Nayak441a4502009-12-13 22:19:23 +0100167 if (twl_class_is_4030())
168 grp |= P1_GRP_4030;
169 else
170 grp |= P1_GRP_6030;
171
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200172 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
173
Saquib Herman21657ebf2011-04-01 10:22:42 +0530174 if (!ret && twl_class_is_6030())
175 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
176 grp << TWL6030_CFG_STATE_GRP_SHIFT |
177 TWL6030_CFG_STATE_ON);
178
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200179 udelay(info->delay);
180
181 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800182}
183
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100184static int twlreg_disable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800185{
186 struct twlreg_info *info = rdev_get_drvdata(rdev);
187 int grp;
Saquib Herman21657ebf2011-04-01 10:22:42 +0530188 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800189
Rajendra Nayak441a4502009-12-13 22:19:23 +0100190 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800191 if (grp < 0)
192 return grp;
193
Saquib Herman21657ebf2011-04-01 10:22:42 +0530194 /* For 6030, set the off state for all grps enabled */
195 if (twl_class_is_6030()) {
196 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
197 (grp & (P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030)) <<
198 TWL6030_CFG_STATE_GRP_SHIFT |
199 TWL6030_CFG_STATE_OFF);
200 if (ret)
201 return ret;
202 }
203
Rajendra Nayak441a4502009-12-13 22:19:23 +0100204 if (twl_class_is_4030())
Juha Keski-Saaricf9836f2009-12-16 15:28:00 +0200205 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100206 else
Juha Keski-Saaricf9836f2009-12-16 15:28:00 +0200207 grp &= ~(P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030);
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
211 /* Next, associate cleared grp in state register */
212 if (!ret && twl_class_is_6030())
213 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
214 grp << TWL6030_CFG_STATE_GRP_SHIFT |
215 TWL6030_CFG_STATE_OFF);
216
217 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800218}
219
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100220static int twlreg_get_status(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800221{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100222 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800223
Rajendra Nayak441a4502009-12-13 22:19:23 +0100224 if (twl_class_is_6030())
225 return 0; /* FIXME return for 6030 regulator */
226
David Brownellfa16a5c2009-02-08 10:37:06 -0800227 if (state < 0)
228 return state;
229 state &= 0x0f;
230
231 /* assume state != WARM_RESET; we'd not be running... */
232 if (!state)
233 return REGULATOR_STATUS_OFF;
234 return (state & BIT(3))
235 ? REGULATOR_STATUS_NORMAL
236 : REGULATOR_STATUS_STANDBY;
237}
238
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100239static int twlreg_set_mode(struct regulator_dev *rdev, unsigned mode)
David Brownellfa16a5c2009-02-08 10:37:06 -0800240{
241 struct twlreg_info *info = rdev_get_drvdata(rdev);
242 unsigned message;
243 int status;
244
Rajendra Nayak441a4502009-12-13 22:19:23 +0100245 if (twl_class_is_6030())
246 return 0; /* FIXME return for 6030 regulator */
247
David Brownellfa16a5c2009-02-08 10:37:06 -0800248 /* We can only set the mode through state machine commands... */
249 switch (mode) {
250 case REGULATOR_MODE_NORMAL:
251 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
252 break;
253 case REGULATOR_MODE_STANDBY:
254 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
255 break;
256 default:
257 return -EINVAL;
258 }
259
260 /* Ensure the resource is associated with some group */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100261 status = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800262 if (status < 0)
263 return status;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100264 if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
David Brownellfa16a5c2009-02-08 10:37:06 -0800265 return -EACCES;
266
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100267 status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800268 message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
269 if (status < 0)
David Brownellfa16a5c2009-02-08 10:37:06 -0800270 return status;
271
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100272 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800273 message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
David Brownellfa16a5c2009-02-08 10:37:06 -0800274}
275
276/*----------------------------------------------------------------------*/
277
278/*
279 * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
280 * select field in its control register. We use tables indexed by VSEL
281 * to record voltages in milliVolts. (Accuracy is about three percent.)
282 *
283 * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
284 * currently handled by listing two slightly different VAUX2 regulators,
285 * only one of which will be configured.
286 *
287 * VSEL values documented as "TI cannot support these values" are flagged
288 * in these tables as UNSUP() values; we normally won't assign them.
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200289 *
290 * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
291 * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
David Brownellfa16a5c2009-02-08 10:37:06 -0800292 */
293#ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
294#define UNSUP_MASK 0x0000
295#else
296#define UNSUP_MASK 0x8000
297#endif
298
299#define UNSUP(x) (UNSUP_MASK | (x))
300#define IS_UNSUP(x) (UNSUP_MASK & (x))
301#define LDO_MV(x) (~UNSUP_MASK & (x))
302
303
304static const u16 VAUX1_VSEL_table[] = {
305 UNSUP(1500), UNSUP(1800), 2500, 2800,
306 3000, 3000, 3000, 3000,
307};
308static const u16 VAUX2_4030_VSEL_table[] = {
309 UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
310 1500, 1800, UNSUP(1850), 2500,
311 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
312 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
313};
314static const u16 VAUX2_VSEL_table[] = {
315 1700, 1700, 1900, 1300,
316 1500, 1800, 2000, 2500,
317 2100, 2800, 2200, 2300,
318 2400, 2400, 2400, 2400,
319};
320static const u16 VAUX3_VSEL_table[] = {
321 1500, 1800, 2500, 2800,
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200322 3000, 3000, 3000, 3000,
David Brownellfa16a5c2009-02-08 10:37:06 -0800323};
324static const u16 VAUX4_VSEL_table[] = {
325 700, 1000, 1200, UNSUP(1300),
326 1500, 1800, UNSUP(1850), 2500,
David Brownell1897e742009-03-10 11:51:15 -0800327 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
328 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
David Brownellfa16a5c2009-02-08 10:37:06 -0800329};
330static const u16 VMMC1_VSEL_table[] = {
331 1850, 2850, 3000, 3150,
332};
333static const u16 VMMC2_VSEL_table[] = {
334 UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
335 UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
336 2600, 2800, 2850, 3000,
337 3150, 3150, 3150, 3150,
338};
339static const u16 VPLL1_VSEL_table[] = {
340 1000, 1200, 1300, 1800,
341 UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
342};
343static const u16 VPLL2_VSEL_table[] = {
344 700, 1000, 1200, 1300,
345 UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
346 UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
347 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
348};
349static const u16 VSIM_VSEL_table[] = {
350 UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
351 2800, 3000, 3000, 3000,
352};
353static const u16 VDAC_VSEL_table[] = {
354 1200, 1300, 1800, 1800,
355};
Juha Keski-Saari07fc4932009-12-16 15:27:55 +0200356static const u16 VDD1_VSEL_table[] = {
357 800, 1450,
358};
359static const u16 VDD2_VSEL_table[] = {
360 800, 1450, 1500,
361};
362static const u16 VIO_VSEL_table[] = {
363 1800, 1850,
364};
365static const u16 VINTANA2_VSEL_table[] = {
366 2500, 2750,
367};
David Brownellfa16a5c2009-02-08 10:37:06 -0800368
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530369static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800370{
371 struct twlreg_info *info = rdev_get_drvdata(rdev);
372 int mV = info->table[index];
373
374 return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
375}
376
David Brownellfa16a5c2009-02-08 10:37:06 -0800377static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000378twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
379 unsigned *selector)
David Brownellfa16a5c2009-02-08 10:37:06 -0800380{
381 struct twlreg_info *info = rdev_get_drvdata(rdev);
382 int vsel;
383
384 for (vsel = 0; vsel < info->table_len; vsel++) {
385 int mV = info->table[vsel];
386 int uV;
387
388 if (IS_UNSUP(mV))
389 continue;
390 uV = LDO_MV(mV) * 1000;
391
David Brownell66b659e2009-02-26 11:50:14 -0800392 /* REVISIT for VAUX2, first match may not be best/lowest */
393
David Brownellfa16a5c2009-02-08 10:37:06 -0800394 /* use the first in-range value */
Mark Brown3a93f2a2010-11-10 14:38:29 +0000395 if (min_uV <= uV && uV <= max_uV) {
396 *selector = vsel;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100397 return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
398 VREG_VOLTAGE, vsel);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000399 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800400 }
401
402 return -EDOM;
403}
404
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530405static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800406{
407 struct twlreg_info *info = rdev_get_drvdata(rdev);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100408 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
409 VREG_VOLTAGE);
David Brownellfa16a5c2009-02-08 10:37:06 -0800410
411 if (vsel < 0)
412 return vsel;
413
414 vsel &= info->table_len - 1;
415 return LDO_MV(info->table[vsel]) * 1000;
416}
417
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530418static struct regulator_ops twl4030ldo_ops = {
419 .list_voltage = twl4030ldo_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800420
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530421 .set_voltage = twl4030ldo_set_voltage,
422 .get_voltage = twl4030ldo_get_voltage,
423
424 .enable = twlreg_enable,
425 .disable = twlreg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530426 .is_enabled = twl4030reg_is_enabled,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530427
428 .set_mode = twlreg_set_mode,
429
430 .get_status = twlreg_get_status,
431};
432
433static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
434{
435 struct twlreg_info *info = rdev_get_drvdata(rdev);
436
437 return ((info->min_mV + (index * 100)) * 1000);
438}
439
440static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000441twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
442 unsigned *selector)
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530443{
444 struct twlreg_info *info = rdev_get_drvdata(rdev);
445 int vsel;
446
447 if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
448 return -EDOM;
449
450 /*
451 * Use the below formula to calculate vsel
452 * mV = 1000mv + 100mv * (vsel - 1)
453 */
454 vsel = (min_uV/1000 - 1000)/100 + 1;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000455 *selector = vsel;
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530456 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
457
458}
459
460static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
461{
462 struct twlreg_info *info = rdev_get_drvdata(rdev);
463 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
464 VREG_VOLTAGE);
465
466 if (vsel < 0)
467 return vsel;
468
469 /*
470 * Use the below formula to calculate vsel
471 * mV = 1000mv + 100mv * (vsel - 1)
472 */
473 return (1000 + (100 * (vsel - 1))) * 1000;
474}
475
476static struct regulator_ops twl6030ldo_ops = {
477 .list_voltage = twl6030ldo_list_voltage,
478
479 .set_voltage = twl6030ldo_set_voltage,
480 .get_voltage = twl6030ldo_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800481
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100482 .enable = twlreg_enable,
483 .disable = twlreg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530484 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800485
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100486 .set_mode = twlreg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800487
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100488 .get_status = twlreg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800489};
490
491/*----------------------------------------------------------------------*/
492
493/*
494 * Fixed voltage LDOs don't have a VSEL field to update.
495 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100496static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800497{
498 struct twlreg_info *info = rdev_get_drvdata(rdev);
499
500 return info->min_mV * 1000;
501}
502
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100503static int twlfixed_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800504{
505 struct twlreg_info *info = rdev_get_drvdata(rdev);
506
507 return info->min_mV * 1000;
508}
509
Saquib Hermanb2456772011-04-01 10:22:44 +0530510static struct regulator_ops twl4030fixed_ops = {
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100511 .list_voltage = twlfixed_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800512
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100513 .get_voltage = twlfixed_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800514
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100515 .enable = twlreg_enable,
516 .disable = twlreg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530517 .is_enabled = twl4030reg_is_enabled,
518
519 .set_mode = twlreg_set_mode,
520
521 .get_status = twlreg_get_status,
522};
523
524static struct regulator_ops twl6030fixed_ops = {
525 .list_voltage = twlfixed_list_voltage,
526
527 .get_voltage = twlfixed_get_voltage,
528
529 .enable = twlreg_enable,
530 .disable = twlreg_disable,
531 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800532
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100533 .set_mode = twlreg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800534
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100535 .get_status = twlreg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800536};
537
Balaji T K8e6de4a2011-02-10 18:44:50 +0530538static struct regulator_ops twl6030_fixed_resource = {
539 .enable = twlreg_enable,
540 .disable = twlreg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530541 .is_enabled = twl6030reg_is_enabled,
Balaji T K8e6de4a2011-02-10 18:44:50 +0530542 .get_status = twlreg_get_status,
543};
544
David Brownellfa16a5c2009-02-08 10:37:06 -0800545/*----------------------------------------------------------------------*/
546
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200547#define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
548 remap_conf) \
549 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530550 remap_conf, TWL4030, twl4030fixed_ops)
Saquib Herman776dc922011-04-01 10:22:43 +0530551#define TWL6030_FIXED_LDO(label, offset, mVolts, num, turnon_delay) \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200552 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530553 0x0, TWL6030, twl6030fixed_ops)
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100554
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530555#define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800556 .base = offset, \
557 .id = num, \
558 .table_len = ARRAY_SIZE(label##_VSEL_table), \
559 .table = label##_VSEL_table, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200560 .delay = turnon_delay, \
561 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800562 .desc = { \
563 .name = #label, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530564 .id = TWL4030_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800565 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530566 .ops = &twl4030ldo_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800567 .type = REGULATOR_VOLTAGE, \
568 .owner = THIS_MODULE, \
569 }, \
570 }
571
Saquib Herman776dc922011-04-01 10:22:43 +0530572#define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts, num) { \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530573 .base = offset, \
574 .id = num, \
575 .min_mV = min_mVolts, \
576 .max_mV = max_mVolts, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530577 .desc = { \
578 .name = #label, \
579 .id = TWL6030_REG_##label, \
580 .n_voltages = (max_mVolts - min_mVolts)/100, \
581 .ops = &twl6030ldo_ops, \
582 .type = REGULATOR_VOLTAGE, \
583 .owner = THIS_MODULE, \
584 }, \
585 }
586
587
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200588#define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530589 family, operations) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800590 .base = offset, \
591 .id = num, \
592 .min_mV = mVolts, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200593 .delay = turnon_delay, \
594 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800595 .desc = { \
596 .name = #label, \
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100597 .id = family##_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800598 .n_voltages = 1, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530599 .ops = &operations, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800600 .type = REGULATOR_VOLTAGE, \
601 .owner = THIS_MODULE, \
602 }, \
603 }
604
Saquib Herman776dc922011-04-01 10:22:43 +0530605#define TWL6030_FIXED_RESOURCE(label, offset, num, turnon_delay) { \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530606 .base = offset, \
607 .id = num, \
608 .delay = turnon_delay, \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530609 .desc = { \
610 .name = #label, \
611 .id = TWL6030_REG_##label, \
612 .ops = &twl6030_fixed_resource, \
613 .type = REGULATOR_VOLTAGE, \
614 .owner = THIS_MODULE, \
615 }, \
616 }
617
David Brownellfa16a5c2009-02-08 10:37:06 -0800618/*
619 * We list regulators here if systems need some level of
620 * software control over them after boot.
621 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100622static struct twlreg_info twl_regs[] = {
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200623 TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
624 TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
625 TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
626 TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
627 TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
628 TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
629 TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
630 TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
631 TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
632 TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
633 TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
634 TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
635 TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
636 TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
637 TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
638 TWL4030_ADJUSTABLE_LDO(VDD1, 0x55, 15, 1000, 0x08),
639 TWL4030_ADJUSTABLE_LDO(VDD2, 0x63, 16, 1000, 0x08),
640 TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
641 TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
642 TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
David Brownellfa16a5c2009-02-08 10:37:06 -0800643 /* VUSBCP is managed *only* by the USB subchip */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100644
645 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200646 /* Turnon-delay and remap configuration values for 6030 are not
647 verified since the specification is not public */
Saquib Herman776dc922011-04-01 10:22:43 +0530648 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300, 1),
649 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300, 2),
650 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300, 3),
651 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300, 4),
652 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300, 5),
653 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300, 7),
654 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 15, 0),
655 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 16, 0),
656 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 17, 0),
657 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 18, 0),
658 TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 48, 0),
David Brownellfa16a5c2009-02-08 10:37:06 -0800659};
660
Dmitry Torokhov24c29022010-02-23 23:38:01 -0800661static int __devinit twlreg_probe(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800662{
663 int i;
664 struct twlreg_info *info;
665 struct regulator_init_data *initdata;
666 struct regulation_constraints *c;
667 struct regulator_dev *rdev;
David Brownellfa16a5c2009-02-08 10:37:06 -0800668
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100669 for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
670 if (twl_regs[i].desc.id != pdev->id)
David Brownellfa16a5c2009-02-08 10:37:06 -0800671 continue;
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100672 info = twl_regs + i;
David Brownellfa16a5c2009-02-08 10:37:06 -0800673 break;
674 }
675 if (!info)
676 return -ENODEV;
677
678 initdata = pdev->dev.platform_data;
679 if (!initdata)
680 return -EINVAL;
681
682 /* Constrain board-specific capabilities according to what
683 * this driver and the chip itself can actually do.
684 */
685 c = &initdata->constraints;
David Brownellfa16a5c2009-02-08 10:37:06 -0800686 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
687 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
688 | REGULATOR_CHANGE_MODE
689 | REGULATOR_CHANGE_STATUS;
Juha Keski-Saari205e5cd2009-12-16 15:27:56 +0200690 switch (pdev->id) {
691 case TWL4030_REG_VIO:
692 case TWL4030_REG_VDD1:
693 case TWL4030_REG_VDD2:
694 case TWL4030_REG_VPLL1:
695 case TWL4030_REG_VINTANA1:
696 case TWL4030_REG_VINTANA2:
697 case TWL4030_REG_VINTDIG:
698 c->always_on = true;
699 break;
700 default:
701 break;
702 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800703
704 rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
705 if (IS_ERR(rdev)) {
706 dev_err(&pdev->dev, "can't register %s, %ld\n",
707 info->desc.name, PTR_ERR(rdev));
708 return PTR_ERR(rdev);
709 }
710 platform_set_drvdata(pdev, rdev);
711
Saquib Herman776dc922011-04-01 10:22:43 +0530712 if (twl_class_is_4030())
713 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
Juha Keski-Saari30010fa2009-12-16 15:27:58 +0200714 info->remap);
715
David Brownellfa16a5c2009-02-08 10:37:06 -0800716 /* NOTE: many regulators support short-circuit IRQs (presentable
717 * as REGULATOR_OVER_CURRENT notifications?) configured via:
718 * - SC_CONFIG
719 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
720 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
721 * - IT_CONFIG
722 */
723
724 return 0;
725}
726
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100727static int __devexit twlreg_remove(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800728{
729 regulator_unregister(platform_get_drvdata(pdev));
730 return 0;
731}
732
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100733MODULE_ALIAS("platform:twl_reg");
David Brownellfa16a5c2009-02-08 10:37:06 -0800734
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100735static struct platform_driver twlreg_driver = {
736 .probe = twlreg_probe,
737 .remove = __devexit_p(twlreg_remove),
David Brownellfa16a5c2009-02-08 10:37:06 -0800738 /* NOTE: short name, to work around driver model truncation of
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100739 * "twl_regulator.12" (and friends) to "twl_regulator.1".
David Brownellfa16a5c2009-02-08 10:37:06 -0800740 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100741 .driver.name = "twl_reg",
David Brownellfa16a5c2009-02-08 10:37:06 -0800742 .driver.owner = THIS_MODULE,
743};
744
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100745static int __init twlreg_init(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800746{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100747 return platform_driver_register(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800748}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100749subsys_initcall(twlreg_init);
David Brownellfa16a5c2009-02-08 10:37:06 -0800750
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100751static void __exit twlreg_exit(void)
David Brownellfa16a5c2009-02-08 10:37:06 -0800752{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100753 platform_driver_unregister(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -0800754}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100755module_exit(twlreg_exit)
David Brownellfa16a5c2009-02-08 10:37:06 -0800756
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100757MODULE_DESCRIPTION("TWL regulator driver");
David Brownellfa16a5c2009-02-08 10:37:06 -0800758MODULE_LICENSE("GPL");