blob: 8611282f1d20025ee47d2e5c131f149cacf3fad2 [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
Tero Kristo34a38442012-02-28 15:09:10 +0530564static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
565 int max_uV, unsigned *selector)
566{
567 struct twlreg_info *info = rdev_get_drvdata(rdev);
568
569 if (info->set_voltage)
570 return info->set_voltage(info->data, min_uV);
571
572 return -ENODEV;
573}
574
575static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
576{
577 struct twlreg_info *info = rdev_get_drvdata(rdev);
578
579 if (info->get_voltage)
580 return info->get_voltage(info->data);
581
582 return -ENODEV;
583}
584
585static struct regulator_ops twl6030coresmps_ops = {
586 .set_voltage = twl6030coresmps_set_voltage,
587 .get_voltage = twl6030coresmps_get_voltage,
588};
589
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530590static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
591{
592 struct twlreg_info *info = rdev_get_drvdata(rdev);
593
594 return ((info->min_mV + (index * 100)) * 1000);
595}
596
597static int
Mark Brown3a93f2a2010-11-10 14:38:29 +0000598twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
599 unsigned *selector)
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530600{
601 struct twlreg_info *info = rdev_get_drvdata(rdev);
602 int vsel;
603
604 if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
605 return -EDOM;
606
607 /*
608 * Use the below formula to calculate vsel
609 * mV = 1000mv + 100mv * (vsel - 1)
610 */
611 vsel = (min_uV/1000 - 1000)/100 + 1;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000612 *selector = vsel;
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530613 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
614
615}
616
617static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
618{
619 struct twlreg_info *info = rdev_get_drvdata(rdev);
620 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
621 VREG_VOLTAGE);
622
623 if (vsel < 0)
624 return vsel;
625
626 /*
627 * Use the below formula to calculate vsel
628 * mV = 1000mv + 100mv * (vsel - 1)
629 */
630 return (1000 + (100 * (vsel - 1))) * 1000;
631}
632
633static struct regulator_ops twl6030ldo_ops = {
634 .list_voltage = twl6030ldo_list_voltage,
635
636 .set_voltage = twl6030ldo_set_voltage,
637 .get_voltage = twl6030ldo_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800638
Balaji T Kf8c29402011-05-20 19:03:51 +0530639 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530640 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530641 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800642
Saquib Herman1a399622011-04-01 10:22:46 +0530643 .set_mode = twl6030reg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800644
Saquib Herman9a0244a2011-04-01 10:22:45 +0530645 .get_status = twl6030reg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800646};
647
648/*----------------------------------------------------------------------*/
649
650/*
651 * Fixed voltage LDOs don't have a VSEL field to update.
652 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100653static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
David Brownell66b659e2009-02-26 11:50:14 -0800654{
655 struct twlreg_info *info = rdev_get_drvdata(rdev);
656
657 return info->min_mV * 1000;
658}
659
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100660static int twlfixed_get_voltage(struct regulator_dev *rdev)
David Brownellfa16a5c2009-02-08 10:37:06 -0800661{
662 struct twlreg_info *info = rdev_get_drvdata(rdev);
663
664 return info->min_mV * 1000;
665}
666
Saquib Hermanb2456772011-04-01 10:22:44 +0530667static struct regulator_ops twl4030fixed_ops = {
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100668 .list_voltage = twlfixed_list_voltage,
David Brownell66b659e2009-02-26 11:50:14 -0800669
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100670 .get_voltage = twlfixed_get_voltage,
David Brownellfa16a5c2009-02-08 10:37:06 -0800671
Balaji T Kf8c29402011-05-20 19:03:51 +0530672 .enable = twl4030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530673 .disable = twl4030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530674 .is_enabled = twl4030reg_is_enabled,
675
Saquib Herman1a399622011-04-01 10:22:46 +0530676 .set_mode = twl4030reg_set_mode,
Saquib Hermanb2456772011-04-01 10:22:44 +0530677
Saquib Herman9a0244a2011-04-01 10:22:45 +0530678 .get_status = twl4030reg_get_status,
Saquib Hermanb2456772011-04-01 10:22:44 +0530679};
680
681static struct regulator_ops twl6030fixed_ops = {
682 .list_voltage = twlfixed_list_voltage,
683
684 .get_voltage = twlfixed_get_voltage,
685
Balaji T Kf8c29402011-05-20 19:03:51 +0530686 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530687 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530688 .is_enabled = twl6030reg_is_enabled,
David Brownellfa16a5c2009-02-08 10:37:06 -0800689
Saquib Herman1a399622011-04-01 10:22:46 +0530690 .set_mode = twl6030reg_set_mode,
David Brownellfa16a5c2009-02-08 10:37:06 -0800691
Saquib Herman9a0244a2011-04-01 10:22:45 +0530692 .get_status = twl6030reg_get_status,
David Brownellfa16a5c2009-02-08 10:37:06 -0800693};
694
Balaji T K8e6de4a2011-02-10 18:44:50 +0530695static struct regulator_ops twl6030_fixed_resource = {
Balaji T Kf8c29402011-05-20 19:03:51 +0530696 .enable = twl6030reg_enable,
Balaji T K0ff38972011-05-20 19:03:52 +0530697 .disable = twl6030reg_disable,
Saquib Hermanb2456772011-04-01 10:22:44 +0530698 .is_enabled = twl6030reg_is_enabled,
Saquib Herman9a0244a2011-04-01 10:22:45 +0530699 .get_status = twl6030reg_get_status,
Balaji T K8e6de4a2011-02-10 18:44:50 +0530700};
701
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100702/*
703 * SMPS status and control
704 */
705
706static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
707{
708 struct twlreg_info *info = rdev_get_drvdata(rdev);
709
710 int voltage = 0;
711
712 switch (info->flags) {
713 case SMPS_OFFSET_EN:
714 voltage = 100000;
715 /* fall through */
716 case 0:
717 switch (index) {
718 case 0:
719 voltage = 0;
720 break;
721 case 58:
722 voltage = 1350 * 1000;
723 break;
724 case 59:
725 voltage = 1500 * 1000;
726 break;
727 case 60:
728 voltage = 1800 * 1000;
729 break;
730 case 61:
731 voltage = 1900 * 1000;
732 break;
733 case 62:
734 voltage = 2100 * 1000;
735 break;
736 default:
737 voltage += (600000 + (12500 * (index - 1)));
738 }
739 break;
740 case SMPS_EXTENDED_EN:
741 switch (index) {
742 case 0:
743 voltage = 0;
744 break;
745 case 58:
746 voltage = 2084 * 1000;
747 break;
748 case 59:
749 voltage = 2315 * 1000;
750 break;
751 case 60:
752 voltage = 2778 * 1000;
753 break;
754 case 61:
755 voltage = 2932 * 1000;
756 break;
757 case 62:
758 voltage = 3241 * 1000;
759 break;
760 default:
761 voltage = (1852000 + (38600 * (index - 1)));
762 }
763 break;
764 case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
765 switch (index) {
766 case 0:
767 voltage = 0;
768 break;
769 case 58:
770 voltage = 4167 * 1000;
771 break;
772 case 59:
773 voltage = 2315 * 1000;
774 break;
775 case 60:
776 voltage = 2778 * 1000;
777 break;
778 case 61:
779 voltage = 2932 * 1000;
780 break;
781 case 62:
782 voltage = 3241 * 1000;
783 break;
784 default:
785 voltage = (2161000 + (38600 * (index - 1)));
786 }
787 break;
788 }
789
790 return voltage;
791}
792
793static int
794twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
795 unsigned int *selector)
796{
797 struct twlreg_info *info = rdev_get_drvdata(rdev);
798 int vsel = 0;
799
800 switch (info->flags) {
801 case 0:
802 if (min_uV == 0)
803 vsel = 0;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530804 else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
805 int calc_uV;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100806 vsel = (min_uV - 600000) / 125;
807 if (vsel % 100)
808 vsel += 100;
809 vsel /= 100;
810 vsel++;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530811 calc_uV = twl6030smps_list_voltage(rdev, vsel);
812 if (calc_uV > max_uV)
813 return -EINVAL;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100814 }
815 /* Values 1..57 for vsel are linear and can be calculated
816 * values 58..62 are non linear.
817 */
818 else if ((min_uV > 1900000) && (max_uV >= 2100000))
819 vsel = 62;
820 else if ((min_uV > 1800000) && (max_uV >= 1900000))
821 vsel = 61;
822 else if ((min_uV > 1500000) && (max_uV >= 1800000))
823 vsel = 60;
824 else if ((min_uV > 1350000) && (max_uV >= 1500000))
825 vsel = 59;
826 else if ((min_uV > 1300000) && (max_uV >= 1350000))
827 vsel = 58;
828 else
829 return -EINVAL;
830 break;
831 case SMPS_OFFSET_EN:
832 if (min_uV == 0)
833 vsel = 0;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530834 else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
835 int calc_uV;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100836 vsel = (min_uV - 700000) / 125;
837 if (vsel % 100)
838 vsel += 100;
839 vsel /= 100;
840 vsel++;
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530841 calc_uV = twl6030smps_list_voltage(rdev, vsel);
842 if (calc_uV > max_uV)
843 return -EINVAL;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100844 }
845 /* Values 1..57 for vsel are linear and can be calculated
846 * values 58..62 are non linear.
847 */
848 else if ((min_uV > 1900000) && (max_uV >= 2100000))
849 vsel = 62;
850 else if ((min_uV > 1800000) && (max_uV >= 1900000))
851 vsel = 61;
852 else if ((min_uV > 1350000) && (max_uV >= 1800000))
853 vsel = 60;
854 else if ((min_uV > 1350000) && (max_uV >= 1500000))
855 vsel = 59;
856 else if ((min_uV > 1300000) && (max_uV >= 1350000))
857 vsel = 58;
858 else
859 return -EINVAL;
860 break;
861 case SMPS_EXTENDED_EN:
862 if (min_uV == 0)
863 vsel = 0;
864 else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
865 vsel = (min_uV - 1852000) / 386;
866 if (vsel % 100)
867 vsel += 100;
868 vsel /= 100;
869 vsel++;
870 }
871 break;
872 case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
873 if (min_uV == 0)
874 vsel = 0;
875 else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
Laxman Dewangana33b6e52012-02-03 12:54:38 +0530876 vsel = (min_uV - 2161000) / 386;
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100877 if (vsel % 100)
878 vsel += 100;
879 vsel /= 100;
880 vsel++;
881 }
882 break;
883 }
884
885 *selector = vsel;
886
887 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
888 vsel);
889}
890
891static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
892{
893 struct twlreg_info *info = rdev_get_drvdata(rdev);
894
895 return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
896}
897
898static struct regulator_ops twlsmps_ops = {
899 .list_voltage = twl6030smps_list_voltage,
900
901 .set_voltage = twl6030smps_set_voltage,
902 .get_voltage_sel = twl6030smps_get_voltage_sel,
903
904 .enable = twl6030reg_enable,
905 .disable = twl6030reg_disable,
906 .is_enabled = twl6030reg_is_enabled,
907
908 .set_mode = twl6030reg_set_mode,
909
910 .get_status = twl6030reg_get_status,
911};
912
David Brownellfa16a5c2009-02-08 10:37:06 -0800913/*----------------------------------------------------------------------*/
914
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200915#define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
916 remap_conf) \
917 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530918 remap_conf, TWL4030, twl4030fixed_ops)
Ambresh Kaf8b2442011-07-09 19:02:21 -0700919#define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
920 TWL_FIXED_LDO(label, offset, mVolts, 0x0, turnon_delay, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530921 0x0, TWL6030, twl6030fixed_ops)
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +0100922
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530923#define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800924 .base = offset, \
925 .id = num, \
926 .table_len = ARRAY_SIZE(label##_VSEL_table), \
927 .table = label##_VSEL_table, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200928 .delay = turnon_delay, \
929 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800930 .desc = { \
931 .name = #label, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530932 .id = TWL4030_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -0800933 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530934 .ops = &twl4030ldo_ops, \
David Brownellfa16a5c2009-02-08 10:37:06 -0800935 .type = REGULATOR_VOLTAGE, \
936 .owner = THIS_MODULE, \
937 }, \
938 }
939
Tero Kristoba305e32011-11-28 16:53:19 +0200940#define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
941 { \
942 .base = offset, \
943 .id = num, \
944 .delay = turnon_delay, \
945 .remap = remap_conf, \
946 .desc = { \
947 .name = #label, \
948 .id = TWL4030_REG_##label, \
949 .ops = &twl4030smps_ops, \
950 .type = REGULATOR_VOLTAGE, \
951 .owner = THIS_MODULE, \
952 }, \
953 }
954
Tero Kristo34a38442012-02-28 15:09:10 +0530955#define TWL6030_ADJUSTABLE_SMPS(label) { \
956 .desc = { \
957 .name = #label, \
958 .id = TWL6030_REG_##label, \
959 .ops = &twl6030coresmps_ops, \
960 .type = REGULATOR_VOLTAGE, \
961 .owner = THIS_MODULE, \
962 }, \
963 }
964
Ambresh Kaf8b2442011-07-09 19:02:21 -0700965#define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530966 .base = offset, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530967 .min_mV = min_mVolts, \
968 .max_mV = max_mVolts, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530969 .desc = { \
970 .name = #label, \
971 .id = TWL6030_REG_##label, \
Colin Cross7736f112011-05-27 12:25:27 -0700972 .n_voltages = (max_mVolts - min_mVolts)/100 + 1, \
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530973 .ops = &twl6030ldo_ops, \
974 .type = REGULATOR_VOLTAGE, \
975 .owner = THIS_MODULE, \
976 }, \
977 }
978
Ambresh Kaf8b2442011-07-09 19:02:21 -0700979#define TWL6025_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100980 .base = offset, \
Graeme Gregory4d94aee2011-05-22 21:21:23 +0100981 .min_mV = min_mVolts, \
982 .max_mV = max_mVolts, \
983 .desc = { \
984 .name = #label, \
985 .id = TWL6025_REG_##label, \
986 .n_voltages = ((max_mVolts - min_mVolts)/100) + 1, \
987 .ops = &twl6030ldo_ops, \
988 .type = REGULATOR_VOLTAGE, \
989 .owner = THIS_MODULE, \
990 }, \
991 }
Rajendra Nayak3e3d3be2010-04-22 14:18:32 +0530992
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200993#define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
Saquib Hermanb2456772011-04-01 10:22:44 +0530994 family, operations) { \
David Brownellfa16a5c2009-02-08 10:37:06 -0800995 .base = offset, \
996 .id = num, \
997 .min_mV = mVolts, \
Juha Keski-Saari045f9722009-12-16 14:49:52 +0200998 .delay = turnon_delay, \
999 .remap = remap_conf, \
David Brownellfa16a5c2009-02-08 10:37:06 -08001000 .desc = { \
1001 .name = #label, \
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001002 .id = family##_REG_##label, \
David Brownell66b659e2009-02-26 11:50:14 -08001003 .n_voltages = 1, \
Saquib Hermanb2456772011-04-01 10:22:44 +05301004 .ops = &operations, \
David Brownellfa16a5c2009-02-08 10:37:06 -08001005 .type = REGULATOR_VOLTAGE, \
1006 .owner = THIS_MODULE, \
1007 }, \
1008 }
1009
Ambresh Kaf8b2442011-07-09 19:02:21 -07001010#define TWL6030_FIXED_RESOURCE(label, offset, turnon_delay) { \
Balaji T K8e6de4a2011-02-10 18:44:50 +05301011 .base = offset, \
Balaji T K8e6de4a2011-02-10 18:44:50 +05301012 .delay = turnon_delay, \
Balaji T K8e6de4a2011-02-10 18:44:50 +05301013 .desc = { \
1014 .name = #label, \
1015 .id = TWL6030_REG_##label, \
1016 .ops = &twl6030_fixed_resource, \
1017 .type = REGULATOR_VOLTAGE, \
1018 .owner = THIS_MODULE, \
1019 }, \
1020 }
1021
Ambresh Kaf8b2442011-07-09 19:02:21 -07001022#define TWL6025_ADJUSTABLE_SMPS(label, offset) { \
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001023 .base = offset, \
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001024 .min_mV = 600, \
1025 .max_mV = 2100, \
1026 .desc = { \
1027 .name = #label, \
1028 .id = TWL6025_REG_##label, \
1029 .n_voltages = 63, \
1030 .ops = &twlsmps_ops, \
1031 .type = REGULATOR_VOLTAGE, \
1032 .owner = THIS_MODULE, \
1033 }, \
1034 }
1035
David Brownellfa16a5c2009-02-08 10:37:06 -08001036/*
1037 * We list regulators here if systems need some level of
1038 * software control over them after boot.
1039 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001040static struct twlreg_info twl_regs[] = {
Juha Keski-Saari045f9722009-12-16 14:49:52 +02001041 TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
1042 TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
1043 TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
1044 TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
1045 TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
1046 TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
1047 TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
1048 TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
1049 TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
1050 TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
1051 TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
1052 TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
1053 TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
1054 TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
1055 TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
Tero Kristoba305e32011-11-28 16:53:19 +02001056 TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08),
1057 TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08),
Juha Keski-Saari045f9722009-12-16 14:49:52 +02001058 TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
1059 TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
1060 TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
David Brownellfa16a5c2009-02-08 10:37:06 -08001061 /* VUSBCP is managed *only* by the USB subchip */
Rajendra Nayak441a4502009-12-13 22:19:23 +01001062
1063 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
Juha Keski-Saari045f9722009-12-16 14:49:52 +02001064 /* Turnon-delay and remap configuration values for 6030 are not
1065 verified since the specification is not public */
Tero Kristo34a38442012-02-28 15:09:10 +05301066 TWL6030_ADJUSTABLE_SMPS(VDD1),
1067 TWL6030_ADJUSTABLE_SMPS(VDD2),
1068 TWL6030_ADJUSTABLE_SMPS(VDD3),
Ambresh Kaf8b2442011-07-09 19:02:21 -07001069 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300),
1070 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300),
1071 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300),
1072 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300),
1073 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300),
1074 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300),
1075 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0),
1076 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0),
1077 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0),
1078 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0),
1079 TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 0),
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001080
1081 /* 6025 are renamed compared to 6030 versions */
Ambresh Kaf8b2442011-07-09 19:02:21 -07001082 TWL6025_ADJUSTABLE_LDO(LDO2, 0x54, 1000, 3300),
1083 TWL6025_ADJUSTABLE_LDO(LDO4, 0x58, 1000, 3300),
1084 TWL6025_ADJUSTABLE_LDO(LDO3, 0x5c, 1000, 3300),
1085 TWL6025_ADJUSTABLE_LDO(LDO5, 0x68, 1000, 3300),
1086 TWL6025_ADJUSTABLE_LDO(LDO1, 0x6c, 1000, 3300),
1087 TWL6025_ADJUSTABLE_LDO(LDO7, 0x74, 1000, 3300),
1088 TWL6025_ADJUSTABLE_LDO(LDO6, 0x60, 1000, 3300),
1089 TWL6025_ADJUSTABLE_LDO(LDOLN, 0x64, 1000, 3300),
1090 TWL6025_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000, 3300),
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001091
Ambresh Kaf8b2442011-07-09 19:02:21 -07001092 TWL6025_ADJUSTABLE_SMPS(SMPS3, 0x34),
1093 TWL6025_ADJUSTABLE_SMPS(SMPS4, 0x10),
1094 TWL6025_ADJUSTABLE_SMPS(VIO, 0x16),
David Brownellfa16a5c2009-02-08 10:37:06 -08001095};
1096
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001097static u8 twl_get_smps_offset(void)
1098{
1099 u8 value;
1100
1101 twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1102 TWL6030_SMPS_OFFSET);
1103 return value;
1104}
1105
1106static u8 twl_get_smps_mult(void)
1107{
1108 u8 value;
1109
1110 twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1111 TWL6030_SMPS_MULT);
1112 return value;
1113}
1114
Dmitry Torokhov24c29022010-02-23 23:38:01 -08001115static int __devinit twlreg_probe(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -08001116{
1117 int i;
1118 struct twlreg_info *info;
1119 struct regulator_init_data *initdata;
1120 struct regulation_constraints *c;
1121 struct regulator_dev *rdev;
Tero Kristo63bfff42012-02-16 12:27:52 +02001122 struct twl_regulator_driver_data *drvdata;
David Brownellfa16a5c2009-02-08 10:37:06 -08001123
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001124 for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
1125 if (twl_regs[i].desc.id != pdev->id)
David Brownellfa16a5c2009-02-08 10:37:06 -08001126 continue;
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001127 info = twl_regs + i;
David Brownellfa16a5c2009-02-08 10:37:06 -08001128 break;
1129 }
1130 if (!info)
1131 return -ENODEV;
1132
1133 initdata = pdev->dev.platform_data;
1134 if (!initdata)
1135 return -EINVAL;
1136
Tero Kristo63bfff42012-02-16 12:27:52 +02001137 drvdata = initdata->driver_data;
1138
1139 if (!drvdata)
1140 return -EINVAL;
1141
1142 /* copy the driver data into regulator data */
1143 info->features = drvdata->features;
1144 info->data = drvdata->data;
1145 info->set_voltage = drvdata->set_voltage;
1146 info->get_voltage = drvdata->get_voltage;
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001147
David Brownellfa16a5c2009-02-08 10:37:06 -08001148 /* Constrain board-specific capabilities according to what
1149 * this driver and the chip itself can actually do.
1150 */
1151 c = &initdata->constraints;
David Brownellfa16a5c2009-02-08 10:37:06 -08001152 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1153 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
1154 | REGULATOR_CHANGE_MODE
1155 | REGULATOR_CHANGE_STATUS;
Juha Keski-Saari205e5cd2009-12-16 15:27:56 +02001156 switch (pdev->id) {
1157 case TWL4030_REG_VIO:
1158 case TWL4030_REG_VDD1:
1159 case TWL4030_REG_VDD2:
1160 case TWL4030_REG_VPLL1:
1161 case TWL4030_REG_VINTANA1:
1162 case TWL4030_REG_VINTANA2:
1163 case TWL4030_REG_VINTDIG:
1164 c->always_on = true;
1165 break;
1166 default:
1167 break;
1168 }
David Brownellfa16a5c2009-02-08 10:37:06 -08001169
Graeme Gregory4d94aee2011-05-22 21:21:23 +01001170 switch (pdev->id) {
1171 case TWL6025_REG_SMPS3:
1172 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
1173 info->flags |= SMPS_EXTENDED_EN;
1174 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
1175 info->flags |= SMPS_OFFSET_EN;
1176 break;
1177 case TWL6025_REG_SMPS4:
1178 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
1179 info->flags |= SMPS_EXTENDED_EN;
1180 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
1181 info->flags |= SMPS_OFFSET_EN;
1182 break;
1183 case TWL6025_REG_VIO:
1184 if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
1185 info->flags |= SMPS_EXTENDED_EN;
1186 if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
1187 info->flags |= SMPS_OFFSET_EN;
1188 break;
1189 }
1190
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05301191 rdev = regulator_register(&info->desc, &pdev->dev, initdata, info, NULL);
David Brownellfa16a5c2009-02-08 10:37:06 -08001192 if (IS_ERR(rdev)) {
1193 dev_err(&pdev->dev, "can't register %s, %ld\n",
1194 info->desc.name, PTR_ERR(rdev));
1195 return PTR_ERR(rdev);
1196 }
1197 platform_set_drvdata(pdev, rdev);
1198
Saquib Herman776dc922011-04-01 10:22:43 +05301199 if (twl_class_is_4030())
1200 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
Juha Keski-Saari30010fa2009-12-16 15:27:58 +02001201 info->remap);
1202
David Brownellfa16a5c2009-02-08 10:37:06 -08001203 /* NOTE: many regulators support short-circuit IRQs (presentable
1204 * as REGULATOR_OVER_CURRENT notifications?) configured via:
1205 * - SC_CONFIG
1206 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
1207 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
1208 * - IT_CONFIG
1209 */
1210
1211 return 0;
1212}
1213
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001214static int __devexit twlreg_remove(struct platform_device *pdev)
David Brownellfa16a5c2009-02-08 10:37:06 -08001215{
1216 regulator_unregister(platform_get_drvdata(pdev));
1217 return 0;
1218}
1219
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001220MODULE_ALIAS("platform:twl_reg");
David Brownellfa16a5c2009-02-08 10:37:06 -08001221
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001222static struct platform_driver twlreg_driver = {
1223 .probe = twlreg_probe,
1224 .remove = __devexit_p(twlreg_remove),
David Brownellfa16a5c2009-02-08 10:37:06 -08001225 /* NOTE: short name, to work around driver model truncation of
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001226 * "twl_regulator.12" (and friends) to "twl_regulator.1".
David Brownellfa16a5c2009-02-08 10:37:06 -08001227 */
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001228 .driver.name = "twl_reg",
David Brownellfa16a5c2009-02-08 10:37:06 -08001229 .driver.owner = THIS_MODULE,
1230};
1231
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001232static int __init twlreg_init(void)
David Brownellfa16a5c2009-02-08 10:37:06 -08001233{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001234 return platform_driver_register(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -08001235}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001236subsys_initcall(twlreg_init);
David Brownellfa16a5c2009-02-08 10:37:06 -08001237
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001238static void __exit twlreg_exit(void)
David Brownellfa16a5c2009-02-08 10:37:06 -08001239{
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001240 platform_driver_unregister(&twlreg_driver);
David Brownellfa16a5c2009-02-08 10:37:06 -08001241}
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001242module_exit(twlreg_exit)
David Brownellfa16a5c2009-02-08 10:37:06 -08001243
Rajendra Nayakc4aa6f32009-12-13 21:36:49 +01001244MODULE_DESCRIPTION("TWL regulator driver");
David Brownellfa16a5c2009-02-08 10:37:06 -08001245MODULE_LICENSE("GPL");