blob: 7ff8bb22d569bfc602195253984a3b9f941168a1 [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
Graeme Gregory4d94aee2011-05-22 21:21:23 +010054 u8 flags;
55
David Brownellfa16a5c2009-02-08 10:37:06 -080056 /* used by regulator core */
57 struct regulator_desc desc;
Graeme Gregory4d94aee2011-05-22 21:21:23 +010058
59 /* chip specific features */
60 unsigned long features;
Tero Kristo63bfff42012-02-16 12:27:52 +020061
62 /*
63 * optional override functions for voltage set/get
64 * these are currently only used for SMPS regulators
65 */
66 int (*get_voltage)(void *data);
67 int (*set_voltage)(void *data, int target_uV);
68
69 /* data passed from board for external get/set voltage */
70 void *data;
David Brownellfa16a5c2009-02-08 10:37:06 -080071};
72
73
74/* LDO control registers ... offset is from the base of its register bank.
75 * The first three registers of all power resource banks help hardware to
76 * manage the various resource groups.
77 */
Rajendra Nayak441a4502009-12-13 22:19:23 +010078/* Common offset in TWL4030/6030 */
David Brownellfa16a5c2009-02-08 10:37:06 -080079#define VREG_GRP 0
Rajendra Nayak441a4502009-12-13 22:19:23 +010080/* TWL4030 register offsets */
David Brownellfa16a5c2009-02-08 10:37:06 -080081#define VREG_TYPE 1
82#define VREG_REMAP 2
83#define VREG_DEDICATED 3 /* LDO control */
Tero Kristoba305e32011-11-28 16:53:19 +020084#define VREG_VOLTAGE_SMPS_4030 9
Rajendra Nayak441a4502009-12-13 22:19:23 +010085/* TWL6030 register offsets */
86#define VREG_TRANS 1
87#define VREG_STATE 2
88#define VREG_VOLTAGE 3
Graeme Gregory4d94aee2011-05-22 21:21:23 +010089#define VREG_VOLTAGE_SMPS 4
Rajendra Nayak441a4502009-12-13 22:19:23 +010090/* TWL6030 Misc register offsets */
91#define VREG_BC_ALL 1
92#define VREG_BC_REF 2
93#define VREG_BC_PROC 3
94#define VREG_BC_CLK_RST 4
David Brownellfa16a5c2009-02-08 10:37:06 -080095
Saquib Herman21657ebf2011-04-01 10:22:42 +053096/* TWL6030 LDO register values for CFG_STATE */
97#define TWL6030_CFG_STATE_OFF 0x00
98#define TWL6030_CFG_STATE_ON 0x01
Saquib Herman9a0244a2011-04-01 10:22:45 +053099#define TWL6030_CFG_STATE_OFF2 0x02
100#define TWL6030_CFG_STATE_SLEEP 0x03
Saquib Herman21657ebf2011-04-01 10:22:42 +0530101#define TWL6030_CFG_STATE_GRP_SHIFT 5
Saquib Hermanb2456772011-04-01 10:22:44 +0530102#define TWL6030_CFG_STATE_APP_SHIFT 2
103#define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
104#define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
105 TWL6030_CFG_STATE_APP_SHIFT)
Saquib Herman21657ebf2011-04-01 10:22:42 +0530106
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100107/* Flags for SMPS Voltage reading */
108#define SMPS_OFFSET_EN BIT(0)
109#define SMPS_EXTENDED_EN BIT(1)
110
111/* twl6025 SMPS EPROM values */
112#define TWL6030_SMPS_OFFSET 0xB0
113#define TWL6030_SMPS_MULT 0xB3
114#define SMPS_MULTOFFSET_SMPS4 BIT(0)
115#define SMPS_MULTOFFSET_VIO BIT(1)
116#define SMPS_MULTOFFSET_SMPS3 BIT(6)
117
David Brownellfa16a5c2009-02-08 10:37:06 -0800118static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +0100119twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
David Brownellfa16a5c2009-02-08 10:37:06 -0800120{
121 u8 value;
122 int status;
123
Rajendra Nayak441a4502009-12-13 22:19:23 +0100124 status = twl_i2c_read_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -0800125 &value, info->base + offset);
126 return (status < 0) ? status : value;
127}
128
129static inline int
Rajendra Nayak441a4502009-12-13 22:19:23 +0100130twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
131 u8 value)
David Brownellfa16a5c2009-02-08 10:37:06 -0800132{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100133 return twl_i2c_write_u8(slave_subgp,
David Brownellfa16a5c2009-02-08 10:37:06 -0800134 value, info->base + offset);
135}
136
137/*----------------------------------------------------------------------*/
138
139/* generic power resource operations, which work on all regulators */
140
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100141static int twlreg_grp(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800142{
Rajendra Nayak441a4502009-12-13 22:19:23 +0100143 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
144 VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800145}
146
147/*
148 * Enable/disable regulators by joining/leaving the P1 (processor) group.
149 * We assume nobody else is updating the DEV_GRP registers.
150 */
Rajendra Nayak441a4502009-12-13 22:19:23 +0100151/* definition for 4030 family */
152#define P3_GRP_4030 BIT(7) /* "peripherals" */
153#define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
154#define P1_GRP_4030 BIT(5) /* CPU/Linux */
155/* definition for 6030 family */
156#define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
157#define P2_GRP_6030 BIT(1) /* "peripherals" */
158#define P1_GRP_6030 BIT(0) /* CPU/Linux */
David Brownellfa16a5c2009-02-08 10:37:06 -0800159
Saquib Hermanb2456772011-04-01 10:22:44 +0530160static int twl4030reg_is_enabled(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800161{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100162 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800163
164 if (state < 0)
165 return state;
166
Saquib Hermanb2456772011-04-01 10:22:44 +0530167 return state & P1_GRP_4030;
168}
169
170static int twl6030reg_is_enabled(struct regulator_dev *rdev)
171{
172 struct twlreg_info *info = rdev_get_drvdata(rdev);
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100173 int grp = 0, val;
Saquib Hermanb2456772011-04-01 10:22:44 +0530174
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100175 if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
176 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
Saquib Hermanb2456772011-04-01 10:22:44 +0530177 if (grp < 0)
178 return grp;
179
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100180 if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
181 grp &= P1_GRP_6030;
182 else
183 grp = 1;
Saquib Hermanb2456772011-04-01 10:22:44 +0530184
185 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
186 val = TWL6030_CFG_STATE_APP(val);
187
188 return grp && (val == TWL6030_CFG_STATE_ON);
David Brownellfa16a5c2009-02-08 10:37:06 -0800189}
190
Balaji T Kf8c29402011-05-20 19:03:51 +0530191static int twl4030reg_enable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800192{
193 struct twlreg_info *info = rdev_get_drvdata(rdev);
194 int grp;
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200195 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800196
Rajendra Nayak441a4502009-12-13 22:19:23 +0100197 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800198 if (grp < 0)
199 return grp;
200
Balaji T Kf8c29402011-05-20 19:03:51 +0530201 grp |= P1_GRP_4030;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100202
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200203 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
204
Balaji T Kf8c29402011-05-20 19:03:51 +0530205 udelay(info->delay);
206
207 return ret;
208}
209
210static int twl6030reg_enable(struct regulator_dev *rdev)
211{
212 struct twlreg_info *info = rdev_get_drvdata(rdev);
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100213 int grp = 0;
Balaji T Kf8c29402011-05-20 19:03:51 +0530214 int ret;
215
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100216 if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
217 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
Balaji T Kf8c29402011-05-20 19:03:51 +0530218 if (grp < 0)
219 return grp;
220
221 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
222 grp << TWL6030_CFG_STATE_GRP_SHIFT |
223 TWL6030_CFG_STATE_ON);
Saquib Herman21657ebf2011-04-01 10:22:42 +0530224
Juha Keski-Saari53b8a9d2009-12-16 14:55:26 +0200225 udelay(info->delay);
226
227 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800228}
229
Balaji T K0ff38972011-05-20 19:03:52 +0530230static int twl4030reg_disable(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800231{
232 struct twlreg_info *info = rdev_get_drvdata(rdev);
233 int grp;
Saquib Herman21657ebf2011-04-01 10:22:42 +0530234 int ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800235
Rajendra Nayak441a4502009-12-13 22:19:23 +0100236 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
David Brownellfa16a5c2009-02-08 10:37:06 -0800237 if (grp < 0)
238 return grp;
239
Balaji T K0ff38972011-05-20 19:03:52 +0530240 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100241
Saquib Herman21657ebf2011-04-01 10:22:42 +0530242 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
243
Balaji T K0ff38972011-05-20 19:03:52 +0530244 return ret;
245}
246
247static int twl6030reg_disable(struct regulator_dev *rdev)
248{
249 struct twlreg_info *info = rdev_get_drvdata(rdev);
250 int grp = 0;
251 int ret;
252
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100253 if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
254 grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
Balaji T K0ff38972011-05-20 19:03:52 +0530255
256 /* For 6030, set the off state for all grps enabled */
257 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
258 (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
259 TWL6030_CFG_STATE_OFF);
Saquib Herman21657ebf2011-04-01 10:22:42 +0530260
261 return ret;
David Brownellfa16a5c2009-02-08 10:37:06 -0800262}
263
Saquib Herman9a0244a2011-04-01 10:22:45 +0530264static int twl4030reg_get_status(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800265{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100266 int state = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800267
268 if (state < 0)
269 return state;
270 state &= 0x0f;
271
272 /* assume state != WARM_RESET; we'd not be running... */
273 if (!state)
274 return REGULATOR_STATUS_OFF;
275 return (state & BIT(3))
276 ? REGULATOR_STATUS_NORMAL
277 : REGULATOR_STATUS_STANDBY;
278}
279
Saquib Herman9a0244a2011-04-01 10:22:45 +0530280static int twl6030reg_get_status(struct regulator_dev *rdev)
281{
282 struct twlreg_info *info = rdev_get_drvdata(rdev);
283 int val;
284
285 val = twlreg_grp(rdev);
286 if (val < 0)
287 return val;
288
289 val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
290
291 switch (TWL6030_CFG_STATE_APP(val)) {
292 case TWL6030_CFG_STATE_ON:
293 return REGULATOR_STATUS_NORMAL;
294
295 case TWL6030_CFG_STATE_SLEEP:
296 return REGULATOR_STATUS_STANDBY;
297
298 case TWL6030_CFG_STATE_OFF:
299 case TWL6030_CFG_STATE_OFF2:
300 default:
301 break;
302 }
303
304 return REGULATOR_STATUS_OFF;
305}
306
Saquib Herman1a399622011-04-01 10:22:46 +0530307static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
David Brownellfa16a5c2009-02-08 10:37:06 -0800308{
309 struct twlreg_info *info = rdev_get_drvdata(rdev);
310 unsigned message;
311 int status;
312
313 /* We can only set the mode through state machine commands... */
314 switch (mode) {
315 case REGULATOR_MODE_NORMAL:
316 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
317 break;
318 case REGULATOR_MODE_STANDBY:
319 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
320 break;
321 default:
322 return -EINVAL;
323 }
324
325 /* Ensure the resource is associated with some group */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100326 status = twlreg_grp(rdev);
David Brownellfa16a5c2009-02-08 10:37:06 -0800327 if (status < 0)
328 return status;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100329 if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
David Brownellfa16a5c2009-02-08 10:37:06 -0800330 return -EACCES;
331
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100332 status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800333 message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
334 if (status < 0)
David Brownellfa16a5c2009-02-08 10:37:06 -0800335 return status;
336
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100337 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
Axel Linb9e26bc2010-10-22 16:38:22 +0800338 message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
David Brownellfa16a5c2009-02-08 10:37:06 -0800339}
340
Saquib Herman1a399622011-04-01 10:22:46 +0530341static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
342{
343 struct twlreg_info *info = rdev_get_drvdata(rdev);
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100344 int grp = 0;
Saquib Herman1a399622011-04-01 10:22:46 +0530345 int val;
346
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100347 if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
348 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
Saquib Herman1a399622011-04-01 10:22:46 +0530349
350 if (grp < 0)
351 return grp;
352
353 /* Compose the state register settings */
354 val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
355 /* We can only set the mode through state machine commands... */
356 switch (mode) {
357 case REGULATOR_MODE_NORMAL:
358 val |= TWL6030_CFG_STATE_ON;
359 break;
360 case REGULATOR_MODE_STANDBY:
361 val |= TWL6030_CFG_STATE_SLEEP;
362 break;
363
364 default:
365 return -EINVAL;
366 }
367
368 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
369}
370
David Brownellfa16a5c2009-02-08 10:37:06 -0800371/*----------------------------------------------------------------------*/
372
373/*
374 * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
375 * select field in its control register. We use tables indexed by VSEL
376 * to record voltages in milliVolts. (Accuracy is about three percent.)
377 *
378 * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
379 * currently handled by listing two slightly different VAUX2 regulators,
380 * only one of which will be configured.
381 *
382 * VSEL values documented as "TI cannot support these values" are flagged
383 * in these tables as UNSUP() values; we normally won't assign them.
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200384 *
385 * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
386 * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
David Brownellfa16a5c2009-02-08 10:37:06 -0800387 */
388#ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
389#define UNSUP_MASK 0x0000
390#else
391#define UNSUP_MASK 0x8000
392#endif
393
394#define UNSUP(x) (UNSUP_MASK | (x))
395#define IS_UNSUP(x) (UNSUP_MASK & (x))
396#define LDO_MV(x) (~UNSUP_MASK & (x))
397
398
399static const u16 VAUX1_VSEL_table[] = {
400 UNSUP(1500), UNSUP(1800), 2500, 2800,
401 3000, 3000, 3000, 3000,
402};
403static const u16 VAUX2_4030_VSEL_table[] = {
404 UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
405 1500, 1800, UNSUP(1850), 2500,
406 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
407 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
408};
409static const u16 VAUX2_VSEL_table[] = {
410 1700, 1700, 1900, 1300,
411 1500, 1800, 2000, 2500,
412 2100, 2800, 2200, 2300,
413 2400, 2400, 2400, 2400,
414};
415static const u16 VAUX3_VSEL_table[] = {
416 1500, 1800, 2500, 2800,
Adrian Hunterd6bb69c2009-03-06 14:51:30 +0200417 3000, 3000, 3000, 3000,
David Brownellfa16a5c2009-02-08 10:37:06 -0800418};
419static const u16 VAUX4_VSEL_table[] = {
420 700, 1000, 1200, UNSUP(1300),
421 1500, 1800, UNSUP(1850), 2500,
David Brownell1897e742009-03-10 11:51:15 -0800422 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
423 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
David Brownellfa16a5c2009-02-08 10:37:06 -0800424};
425static const u16 VMMC1_VSEL_table[] = {
426 1850, 2850, 3000, 3150,
427};
428static const u16 VMMC2_VSEL_table[] = {
429 UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
430 UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
431 2600, 2800, 2850, 3000,
432 3150, 3150, 3150, 3150,
433};
434static const u16 VPLL1_VSEL_table[] = {
435 1000, 1200, 1300, 1800,
436 UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
437};
438static const u16 VPLL2_VSEL_table[] = {
439 700, 1000, 1200, 1300,
440 UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
441 UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
442 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
443};
444static const u16 VSIM_VSEL_table[] = {
445 UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
446 2800, 3000, 3000, 3000,
447};
448static const u16 VDAC_VSEL_table[] = {
449 1200, 1300, 1800, 1800,
450};
Juha Keski-Saari07fc4932009-12-16 15:27:55 +0200451static const u16 VDD1_VSEL_table[] = {
452 800, 1450,
453};
454static const u16 VDD2_VSEL_table[] = {
455 800, 1450, 1500,
456};
457static const u16 VIO_VSEL_table[] = {
458 1800, 1850,
459};
460static const u16 VINTANA2_VSEL_table[] = {
461 2500, 2750,
462};
David Brownellfa16a5c2009-02-08 10:37:06 -0800463
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530464static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800465{
466 struct twlreg_info *info = rdev_get_drvdata(rdev);
467 int mV = info->table[index];
468
469 return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
470}
471
David Brownellfa16a5c2009-02-08 10:37:06 -0800472static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000473twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
474 unsigned *selector)
David Brownellfa16a5c2009-02-08 10:37:06 -0800475{
476 struct twlreg_info *info = rdev_get_drvdata(rdev);
477 int vsel;
478
479 for (vsel = 0; vsel < info->table_len; vsel++) {
480 int mV = info->table[vsel];
481 int uV;
482
483 if (IS_UNSUP(mV))
484 continue;
485 uV = LDO_MV(mV) * 1000;
486
David Brownell66b659e2009-02-26 11:50:14 -0800487 /* REVISIT for VAUX2, first match may not be best/lowest */
488
David Brownellfa16a5c2009-02-08 10:37:06 -0800489 /* use the first in-range value */
Mark Brown3a93f2a2010-11-10 14:38:29 +0000490 if (min_uV <= uV && uV <= max_uV) {
491 *selector = vsel;
Rajendra Nayak441a4502009-12-13 22:19:23 +0100492 return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
493 VREG_VOLTAGE, vsel);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000494 }
David Brownellfa16a5c2009-02-08 10:37:06 -0800495 }
496
497 return -EDOM;
498}
499
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530500static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800501{
502 struct twlreg_info *info = rdev_get_drvdata(rdev);
Rajendra Nayak441a4502009-12-13 22:19:23 +0100503 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
504 VREG_VOLTAGE);
David Brownellfa16a5c2009-02-08 10:37:06 -0800505
506 if (vsel < 0)
507 return vsel;
508
509 vsel &= info->table_len - 1;
510 return LDO_MV(info->table[vsel]) * 1000;
511}
512
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530513static struct regulator_ops twl4030ldo_ops = {
514 .list_voltage = twl4030ldo_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800515
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530516 .set_voltage = twl4030ldo_set_voltage,
517 .get_voltage = twl4030ldo_get_voltage,
518
Balaji T Kf8c29402011-05-20 19:03:51 +0530519 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530520 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530521 .is_enabled = twl4030reg_is_enabled,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530522
Saquib Herman1a399622011-04-01 10:22:46 +0530523 .set_mode = twl4030reg_set_mode,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530524
Saquib Herman9a0244a2011-04-01 10:22:45 +0530525 .get_status = twl4030reg_get_status,
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530526};
527
Tero Kristoba305e32011-11-28 16:53:19 +0200528static int
529twl4030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
530 unsigned *selector)
531{
532 struct twlreg_info *info = rdev_get_drvdata(rdev);
533 int vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
534
Tero Kristo63bfff42012-02-16 12:27:52 +0200535 if (info->set_voltage) {
536 return info->set_voltage(info->data, min_uV);
537 } else {
538 twlreg_write(info, TWL_MODULE_PM_RECEIVER,
539 VREG_VOLTAGE_SMPS_4030, vsel);
540 }
541
Tero Kristoba305e32011-11-28 16:53:19 +0200542 return 0;
543}
544
545static int twl4030smps_get_voltage(struct regulator_dev *rdev)
546{
547 struct twlreg_info *info = rdev_get_drvdata(rdev);
Tero Kristo63bfff42012-02-16 12:27:52 +0200548 int vsel;
549
550 if (info->get_voltage)
551 return info->get_voltage(info->data);
552
553 vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
Tero Kristoba305e32011-11-28 16:53:19 +0200554 VREG_VOLTAGE_SMPS_4030);
555
556 return vsel * 12500 + 600000;
557}
558
559static struct regulator_ops twl4030smps_ops = {
560 .set_voltage = twl4030smps_set_voltage,
561 .get_voltage = twl4030smps_get_voltage,
562};
563
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530564static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
565{
566 struct twlreg_info *info = rdev_get_drvdata(rdev);
567
568 return ((info->min_mV + (index * 100)) * 1000);
569}
570
571static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000572twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
573 unsigned *selector)
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530574{
575 struct twlreg_info *info = rdev_get_drvdata(rdev);
576 int vsel;
577
578 if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
579 return -EDOM;
580
581 /*
582 * Use the below formula to calculate vsel
583 * mV = 1000mv + 100mv * (vsel - 1)
584 */
585 vsel = (min_uV/1000 - 1000)/100 + 1;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000586 *selector = vsel;
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530587 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
588
589}
590
591static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
592{
593 struct twlreg_info *info = rdev_get_drvdata(rdev);
594 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
595 VREG_VOLTAGE);
596
597 if (vsel < 0)
598 return vsel;
599
600 /*
601 * Use the below formula to calculate vsel
602 * mV = 1000mv + 100mv * (vsel - 1)
603 */
604 return (1000 + (100 * (vsel - 1))) * 1000;
605}
606
607static struct regulator_ops twl6030ldo_ops = {
608 .list_voltage = twl6030ldo_list_voltage,
609
610 .set_voltage = twl6030ldo_set_voltage,
611 .get_voltage = twl6030ldo_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800612
Balaji T Kf8c29402011-05-20 19:03:51 +0530613 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530614 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530615 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800616
Saquib Herman1a399622011-04-01 10:22:46 +0530617 .set_mode = twl6030reg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800618
Saquib Herman9a0244a2011-04-01 10:22:45 +0530619 .get_status = twl6030reg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800620};
621
622/*----------------------------------------------------------------------*/
623
624/*
625 * Fixed voltage LDOs don't have a VSEL field to update.
626 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100627static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800628{
629 struct twlreg_info *info = rdev_get_drvdata(rdev);
630
631 return info->min_mV * 1000;
632}
633
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100634static int twlfixed_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800635{
636 struct twlreg_info *info = rdev_get_drvdata(rdev);
637
638 return info->min_mV * 1000;
639}
640
Saquib Hermanb2456772011-04-01 10:22:44 +0530641static struct regulator_ops twl4030fixed_ops = {
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100642 .list_voltage = twlfixed_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800643
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100644 .get_voltage = twlfixed_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800645
Balaji T Kf8c29402011-05-20 19:03:51 +0530646 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530647 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530648 .is_enabled = twl4030reg_is_enabled,
649
Saquib Herman1a399622011-04-01 10:22:46 +0530650 .set_mode = twl4030reg_set_mode,
Saquib Hermanb2456772011-04-01 10:22:44 +0530651
Saquib Herman9a0244a2011-04-01 10:22:45 +0530652 .get_status = twl4030reg_get_status,
Saquib Hermanb2456772011-04-01 10:22:44 +0530653};
654
655static struct regulator_ops twl6030fixed_ops = {
656 .list_voltage = twlfixed_list_voltage,
657
658 .get_voltage = twlfixed_get_voltage,
659
Balaji T Kf8c29402011-05-20 19:03:51 +0530660 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530661 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530662 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800663
Saquib Herman1a399622011-04-01 10:22:46 +0530664 .set_mode = twl6030reg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800665
Saquib Herman9a0244a2011-04-01 10:22:45 +0530666 .get_status = twl6030reg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800667};
668
Balaji T K8e6de4a2011-02-10 18:44:50 +0530669static struct regulator_ops twl6030_fixed_resource = {
Balaji T Kf8c29402011-05-20 19:03:51 +0530670 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530671 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530672 .is_enabled = twl6030reg_is_enabled,
Saquib Herman9a0244a2011-04-01 10:22:45 +0530673 .get_status = twl6030reg_get_status,
Balaji T K8e6de4a2011-02-10 18:44:50 +0530674};
675
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100676/*
677 * SMPS status and control
678 */
679
680static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
681{
682 struct twlreg_info *info = rdev_get_drvdata(rdev);
683
684 int voltage = 0;
685
686 switch (info->flags) {
687 case SMPS_OFFSET_EN:
688 voltage = 100000;
689 /* fall through */
690 case 0:
691 switch (index) {
692 case 0:
693 voltage = 0;
694 break;
695 case 58:
696 voltage = 1350 * 1000;
697 break;
698 case 59:
699 voltage = 1500 * 1000;
700 break;
701 case 60:
702 voltage = 1800 * 1000;
703 break;
704 case 61:
705 voltage = 1900 * 1000;
706 break;
707 case 62:
708 voltage = 2100 * 1000;
709 break;
710 default:
711 voltage += (600000 + (12500 * (index - 1)));
712 }
713 break;
714 case SMPS_EXTENDED_EN:
715 switch (index) {
716 case 0:
717 voltage = 0;
718 break;
719 case 58:
720 voltage = 2084 * 1000;
721 break;
722 case 59:
723 voltage = 2315 * 1000;
724 break;
725 case 60:
726 voltage = 2778 * 1000;
727 break;
728 case 61:
729 voltage = 2932 * 1000;
730 break;
731 case 62:
732 voltage = 3241 * 1000;
733 break;
734 default:
735 voltage = (1852000 + (38600 * (index - 1)));
736 }
737 break;
738 case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
739 switch (index) {
740 case 0:
741 voltage = 0;
742 break;
743 case 58:
744 voltage = 4167 * 1000;
745 break;
746 case 59:
747 voltage = 2315 * 1000;
748 break;
749 case 60:
750 voltage = 2778 * 1000;
751 break;
752 case 61:
753 voltage = 2932 * 1000;
754 break;
755 case 62:
756 voltage = 3241 * 1000;
757 break;
758 default:
759 voltage = (2161000 + (38600 * (index - 1)));
760 }
761 break;
762 }
763
764 return voltage;
765}
766
767static int
768twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
769 unsigned int *selector)
770{
771 struct twlreg_info *info = rdev_get_drvdata(rdev);
772 int vsel = 0;
773
774 switch (info->flags) {
775 case 0:
776 if (min_uV == 0)
777 vsel = 0;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530778 else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
779 int calc_uV;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100780 vsel = (min_uV - 600000) / 125;
781 if (vsel % 100)
782 vsel += 100;
783 vsel /= 100;
784 vsel++;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530785 calc_uV = twl6030smps_list_voltage(rdev, vsel);
786 if (calc_uV > max_uV)
787 return -EINVAL;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100788 }
789 /* Values 1..57 for vsel are linear and can be calculated
790 * values 58..62 are non linear.
791 */
792 else if ((min_uV > 1900000) && (max_uV >= 2100000))
793 vsel = 62;
794 else if ((min_uV > 1800000) && (max_uV >= 1900000))
795 vsel = 61;
796 else if ((min_uV > 1500000) && (max_uV >= 1800000))
797 vsel = 60;
798 else if ((min_uV > 1350000) && (max_uV >= 1500000))
799 vsel = 59;
800 else if ((min_uV > 1300000) && (max_uV >= 1350000))
801 vsel = 58;
802 else
803 return -EINVAL;
804 break;
805 case SMPS_OFFSET_EN:
806 if (min_uV == 0)
807 vsel = 0;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530808 else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
809 int calc_uV;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100810 vsel = (min_uV - 700000) / 125;
811 if (vsel % 100)
812 vsel += 100;
813 vsel /= 100;
814 vsel++;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530815 calc_uV = twl6030smps_list_voltage(rdev, vsel);
816 if (calc_uV > max_uV)
817 return -EINVAL;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100818 }
819 /* Values 1..57 for vsel are linear and can be calculated
820 * values 58..62 are non linear.
821 */
822 else if ((min_uV > 1900000) && (max_uV >= 2100000))
823 vsel = 62;
824 else if ((min_uV > 1800000) && (max_uV >= 1900000))
825 vsel = 61;
826 else if ((min_uV > 1350000) && (max_uV >= 1800000))
827 vsel = 60;
828 else if ((min_uV > 1350000) && (max_uV >= 1500000))
829 vsel = 59;
830 else if ((min_uV > 1300000) && (max_uV >= 1350000))
831 vsel = 58;
832 else
833 return -EINVAL;
834 break;
835 case SMPS_EXTENDED_EN:
836 if (min_uV == 0)
837 vsel = 0;
838 else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
839 vsel = (min_uV - 1852000) / 386;
840 if (vsel % 100)
841 vsel += 100;
842 vsel /= 100;
843 vsel++;
844 }
845 break;
846 case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
847 if (min_uV == 0)
848 vsel = 0;
849 else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530850 vsel = (min_uV - 2161000) / 386;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100851 if (vsel % 100)
852 vsel += 100;
853 vsel /= 100;
854 vsel++;
855 }
856 break;
857 }
858
859 *selector = vsel;
860
861 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
862 vsel);
863}
864
865static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
866{
867 struct twlreg_info *info = rdev_get_drvdata(rdev);
868
869 return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
870}
871
872static struct regulator_ops twlsmps_ops = {
873 .list_voltage = twl6030smps_list_voltage,
874
875 .set_voltage = twl6030smps_set_voltage,
876 .get_voltage_sel = twl6030smps_get_voltage_sel,
877
878 .enable = twl6030reg_enable,
879 .disable = twl6030reg_disable,
880 .is_enabled = twl6030reg_is_enabled,
881
882 .set_mode = twl6030reg_set_mode,
883
884 .get_status = twl6030reg_get_status,
885};
886
David Brownellfa16a5c2009-02-08 10:37:06 -0800887/*----------------------------------------------------------------------*/
888
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200889#define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
890 remap_conf) \
891 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530892 remap_conf, TWL4030, twl4030fixed_ops)
Ambresh Kaf8b2442011-07-09 19:02:21 -0700893#define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
894 TWL_FIXED_LDO(label, offset, mVolts, 0x0, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530895 0x0, TWL6030, twl6030fixed_ops)
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100896
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530897#define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800898 .base = offset, \
899 .id = num, \
900 .table_len = ARRAY_SIZE(label##_VSEL_table), \
901 .table = label##_VSEL_table, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200902 .delay = turnon_delay, \
903 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800904 .desc = { \
905 .name = #label, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530906 .id = TWL4030_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800907 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530908 .ops = &twl4030ldo_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800909 .type = REGULATOR_VOLTAGE, \
910 .owner = THIS_MODULE, \
911 }, \
912 }
913
Tero Kristoba305e32011-11-28 16:53:19 +0200914#define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
915 { \
916 .base = offset, \
917 .id = num, \
918 .delay = turnon_delay, \
919 .remap = remap_conf, \
920 .desc = { \
921 .name = #label, \
922 .id = TWL4030_REG_##label, \
923 .ops = &twl4030smps_ops, \
924 .type = REGULATOR_VOLTAGE, \
925 .owner = THIS_MODULE, \
926 }, \
927 }
928
Ambresh Kaf8b2442011-07-09 19:02:21 -0700929#define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530930 .base = offset, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530931 .min_mV = min_mVolts, \
932 .max_mV = max_mVolts, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530933 .desc = { \
934 .name = #label, \
935 .id = TWL6030_REG_##label, \
Colin Cross7736f112011-05-27 12:25:27 -0700936 .n_voltages = (max_mVolts - min_mVolts)/100 + 1, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530937 .ops = &twl6030ldo_ops, \
938 .type = REGULATOR_VOLTAGE, \
939 .owner = THIS_MODULE, \
940 }, \
941 }
942
Ambresh Kaf8b2442011-07-09 19:02:21 -0700943#define TWL6025_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100944 .base = offset, \
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100945 .min_mV = min_mVolts, \
946 .max_mV = max_mVolts, \
947 .desc = { \
948 .name = #label, \
949 .id = TWL6025_REG_##label, \
950 .n_voltages = ((max_mVolts - min_mVolts)/100) + 1, \
951 .ops = &twl6030ldo_ops, \
952 .type = REGULATOR_VOLTAGE, \
953 .owner = THIS_MODULE, \
954 }, \
955 }
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530956
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200957#define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530958 family, operations) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800959 .base = offset, \
960 .id = num, \
961 .min_mV = mVolts, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200962 .delay = turnon_delay, \
963 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800964 .desc = { \
965 .name = #label, \
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100966 .id = family##_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800967 .n_voltages = 1, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530968 .ops = &operations, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800969 .type = REGULATOR_VOLTAGE, \
970 .owner = THIS_MODULE, \
971 }, \
972 }
973
Ambresh Kaf8b2442011-07-09 19:02:21 -0700974#define TWL6030_FIXED_RESOURCE(label, offset, turnon_delay) { \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530975 .base = offset, \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530976 .delay = turnon_delay, \
Balaji T K8e6de4a2011-02-10 18:44:50 +0530977 .desc = { \
978 .name = #label, \
979 .id = TWL6030_REG_##label, \
980 .ops = &twl6030_fixed_resource, \
981 .type = REGULATOR_VOLTAGE, \
982 .owner = THIS_MODULE, \
983 }, \
984 }
985
Ambresh Kaf8b2442011-07-09 19:02:21 -0700986#define TWL6025_ADJUSTABLE_SMPS(label, offset) { \
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100987 .base = offset, \
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100988 .min_mV = 600, \
989 .max_mV = 2100, \
990 .desc = { \
991 .name = #label, \
992 .id = TWL6025_REG_##label, \
993 .n_voltages = 63, \
994 .ops = &twlsmps_ops, \
995 .type = REGULATOR_VOLTAGE, \
996 .owner = THIS_MODULE, \
997 }, \
998 }
999
David Brownellfa16a5c2009-02-08 10:37:06 -08001000/*
1001 * We list regulators here if systems need some level of
1002 * software control over them after boot.
1003 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001004static struct twlreg_info twl_regs[] = {
Juha Keski-Saari045f9722009-12-16 14:49:52 +02001005 TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
1006 TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
1007 TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
1008 TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
1009 TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
1010 TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
1011 TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
1012 TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
1013 TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
1014 TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
1015 TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
1016 TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
1017 TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
1018 TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
1019 TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
Tero Kristoba305e32011-11-28 16:53:19 +02001020 TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08),
1021 TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08),
Juha Keski-Saari045f9722009-12-16 14:49:52 +02001022 TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
1023 TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
1024 TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
David Brownellfa16a5c2009-02-08 10:37:06 -08001025 /* VUSBCP is managed *only* by the USB subchip */
Rajendra Nayak441a4502009-12-13 22:19:23 +01001026
1027 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
Juha Keski-Saari045f9722009-12-16 14:49:52 +02001028 /* Turnon-delay and remap configuration values for 6030 are not
1029 verified since the specification is not public */
Ambresh Kaf8b2442011-07-09 19:02:21 -07001030 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300),
1031 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300),
1032 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300),
1033 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300),
1034 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300),
1035 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300),
1036 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0),
1037 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0),
1038 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0),
1039 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0),
1040 TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 0),
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001041
1042 /* 6025 are renamed compared to 6030 versions */
Ambresh Kaf8b2442011-07-09 19:02:21 -07001043 TWL6025_ADJUSTABLE_LDO(LDO2, 0x54, 1000, 3300),
1044 TWL6025_ADJUSTABLE_LDO(LDO4, 0x58, 1000, 3300),
1045 TWL6025_ADJUSTABLE_LDO(LDO3, 0x5c, 1000, 3300),
1046 TWL6025_ADJUSTABLE_LDO(LDO5, 0x68, 1000, 3300),
1047 TWL6025_ADJUSTABLE_LDO(LDO1, 0x6c, 1000, 3300),
1048 TWL6025_ADJUSTABLE_LDO(LDO7, 0x74, 1000, 3300),
1049 TWL6025_ADJUSTABLE_LDO(LDO6, 0x60, 1000, 3300),
1050 TWL6025_ADJUSTABLE_LDO(LDOLN, 0x64, 1000, 3300),
1051 TWL6025_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000, 3300),
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001052
Ambresh Kaf8b2442011-07-09 19:02:21 -07001053 TWL6025_ADJUSTABLE_SMPS(SMPS3, 0x34),
1054 TWL6025_ADJUSTABLE_SMPS(SMPS4, 0x10),
1055 TWL6025_ADJUSTABLE_SMPS(VIO, 0x16),
David Brownellfa16a5c2009-02-08 10:37:06 -08001056};
1057
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001058static u8 twl_get_smps_offset(void)
1059{
1060 u8 value;
1061
1062 twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1063 TWL6030_SMPS_OFFSET);
1064 return value;
1065}
1066
1067static u8 twl_get_smps_mult(void)
1068{
1069 u8 value;
1070
1071 twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1072 TWL6030_SMPS_MULT);
1073 return value;
1074}
1075
Dmitry Torokhov24c29022010-02-23 23:38:01 -08001076static int __devinit twlreg_probe(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -08001077{
1078 int i;
1079 struct twlreg_info *info;
1080 struct regulator_init_data *initdata;
1081 struct regulation_constraints *c;
1082 struct regulator_dev *rdev;
Tero Kristo63bfff42012-02-16 12:27:52 +02001083 struct twl_regulator_driver_data *drvdata;
David Brownellfa16a5c2009-02-08 10:37:06 -08001084
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001085 for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
1086 if (twl_regs[i].desc.id != pdev->id)
David Brownellfa16a5c2009-02-08 10:37:06 -08001087 continue;
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001088 info = twl_regs + i;
David Brownellfa16a5c2009-02-08 10:37:06 -08001089 break;
1090 }
1091 if (!info)
1092 return -ENODEV;
1093
1094 initdata = pdev->dev.platform_data;
1095 if (!initdata)
1096 return -EINVAL;
1097
Tero Kristo63bfff42012-02-16 12:27:52 +02001098 drvdata = initdata->driver_data;
1099
1100 if (!drvdata)
1101 return -EINVAL;
1102
1103 /* copy the driver data into regulator data */
1104 info->features = drvdata->features;
1105 info->data = drvdata->data;
1106 info->set_voltage = drvdata->set_voltage;
1107 info->get_voltage = drvdata->get_voltage;
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001108
David Brownellfa16a5c2009-02-08 10:37:06 -08001109 /* Constrain board-specific capabilities according to what
1110 * this driver and the chip itself can actually do.
1111 */
1112 c = &initdata->constraints;
David Brownellfa16a5c2009-02-08 10:37:06 -08001113 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1114 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
1115 | REGULATOR_CHANGE_MODE
1116 | REGULATOR_CHANGE_STATUS;
Juha Keski-Saari205e5cd2009-12-16 15:27:56 +02001117 switch (pdev->id) {
1118 case TWL4030_REG_VIO:
1119 case TWL4030_REG_VDD1:
1120 case TWL4030_REG_VDD2:
1121 case TWL4030_REG_VPLL1:
1122 case TWL4030_REG_VINTANA1:
1123 case TWL4030_REG_VINTANA2:
1124 case TWL4030_REG_VINTDIG:
1125 c->always_on = true;
1126 break;
1127 default:
1128 break;
1129 }
David Brownellfa16a5c2009-02-08 10:37:06 -08001130
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001131 switch (pdev->id) {
1132 case TWL6025_REG_SMPS3:
1133 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
1134 info->flags |= SMPS_EXTENDED_EN;
1135 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
1136 info->flags |= SMPS_OFFSET_EN;
1137 break;
1138 case TWL6025_REG_SMPS4:
1139 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
1140 info->flags |= SMPS_EXTENDED_EN;
1141 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
1142 info->flags |= SMPS_OFFSET_EN;
1143 break;
1144 case TWL6025_REG_VIO:
1145 if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
1146 info->flags |= SMPS_EXTENDED_EN;
1147 if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
1148 info->flags |= SMPS_OFFSET_EN;
1149 break;
1150 }
1151
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05301152 rdev = regulator_register(&info->desc, &pdev->dev, initdata, info, NULL);
David Brownellfa16a5c2009-02-08 10:37:06 -08001153 if (IS_ERR(rdev)) {
1154 dev_err(&pdev->dev, "can't register %s, %ld\n",
1155 info->desc.name, PTR_ERR(rdev));
1156 return PTR_ERR(rdev);
1157 }
1158 platform_set_drvdata(pdev, rdev);
1159
Saquib Herman776dc922011-04-01 10:22:43 +05301160 if (twl_class_is_4030())
1161 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
Juha Keski-Saari30010fa2009-12-16 15:27:58 +02001162 info->remap);
1163
David Brownellfa16a5c2009-02-08 10:37:06 -08001164 /* NOTE: many regulators support short-circuit IRQs (presentable
1165 * as REGULATOR_OVER_CURRENT notifications?) configured via:
1166 * - SC_CONFIG
1167 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
1168 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
1169 * - IT_CONFIG
1170 */
1171
1172 return 0;
1173}
1174
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001175static int __devexit twlreg_remove(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -08001176{
1177 regulator_unregister(platform_get_drvdata(pdev));
1178 return 0;
1179}
1180
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001181MODULE_ALIAS("platform:twl_reg");
David Brownellfa16a5c2009-02-08 10:37:06 -08001182
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001183static struct platform_driver twlreg_driver = {
1184 .probe = twlreg_probe,
1185 .remove = __devexit_p(twlreg_remove),
David Brownellfa16a5c2009-02-08 10:37:06 -08001186 /* NOTE: short name, to work around driver model truncation of
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001187 * "twl_regulator.12" (and friends) to "twl_regulator.1".
David Brownellfa16a5c2009-02-08 10:37:06 -08001188 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001189 .driver.name = "twl_reg",
David Brownellfa16a5c2009-02-08 10:37:06 -08001190 .driver.owner = THIS_MODULE,
1191};
1192
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001193static int __init twlreg_init(void)
David Brownellfa16a5c2009-02-08 10:37:06 -08001194{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001195 return platform_driver_register(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -08001196}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001197subsys_initcall(twlreg_init);
David Brownellfa16a5c2009-02-08 10:37:06 -08001198
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001199static void __exit twlreg_exit(void)
David Brownellfa16a5c2009-02-08 10:37:06 -08001200{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001201 platform_driver_unregister(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -08001202}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001203module_exit(twlreg_exit)
David Brownellfa16a5c2009-02-08 10:37:06 -08001204
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001205MODULE_DESCRIPTION("TWL regulator driver");
David Brownellfa16a5c2009-02-08 10:37:06 -08001206MODULE_LICENSE("GPL");