regulator: twl: add twl6030 set_mode
[linux-3.10.git] / drivers / regulator / twl-regulator.c
1 /*
2  * twl-regulator.c -- support regulators in twl4030/twl6030 family chips
3  *
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>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/i2c/twl.h>
20
21
22 /*
23  * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
24  * 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
33 struct twlreg_info {
34         /* start of regulator's PM_RECEIVER control register bank */
35         u8                      base;
36
37         /* twl resource ID, for resource control state machine */
38         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
44         /* regulator specific turn-on delay */
45         u16                     delay;
46
47         /* State REMAP default configuration */
48         u8                      remap;
49
50         /* chip constraints on regulator behavior */
51         u16                     min_mV;
52         u16                     max_mV;
53
54         /* used by regulator core */
55         struct regulator_desc   desc;
56 };
57
58
59 /* LDO control registers ... offset is from the base of its register bank.
60  * The first three registers of all power resource banks help hardware to
61  * manage the various resource groups.
62  */
63 /* Common offset in TWL4030/6030 */
64 #define VREG_GRP                0
65 /* TWL4030 register offsets */
66 #define VREG_TYPE               1
67 #define VREG_REMAP              2
68 #define VREG_DEDICATED          3       /* LDO control */
69 /* TWL6030 register offsets */
70 #define VREG_TRANS              1
71 #define VREG_STATE              2
72 #define VREG_VOLTAGE            3
73 /* TWL6030 Misc register offsets */
74 #define VREG_BC_ALL             1
75 #define VREG_BC_REF             2
76 #define VREG_BC_PROC            3
77 #define VREG_BC_CLK_RST         4
78
79 /* TWL6030 LDO register values for CFG_STATE */
80 #define TWL6030_CFG_STATE_OFF   0x00
81 #define TWL6030_CFG_STATE_ON    0x01
82 #define TWL6030_CFG_STATE_OFF2  0x02
83 #define TWL6030_CFG_STATE_SLEEP 0x03
84 #define TWL6030_CFG_STATE_GRP_SHIFT     5
85 #define TWL6030_CFG_STATE_APP_SHIFT     2
86 #define TWL6030_CFG_STATE_APP_MASK      (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
87 #define TWL6030_CFG_STATE_APP(v)        (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
88                                                 TWL6030_CFG_STATE_APP_SHIFT)
89
90 static inline int
91 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
92 {
93         u8 value;
94         int status;
95
96         status = twl_i2c_read_u8(slave_subgp,
97                         &value, info->base + offset);
98         return (status < 0) ? status : value;
99 }
100
101 static inline int
102 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
103                                                  u8 value)
104 {
105         return twl_i2c_write_u8(slave_subgp,
106                         value, info->base + offset);
107 }
108
109 /*----------------------------------------------------------------------*/
110
111 /* generic power resource operations, which work on all regulators */
112
113 static int twlreg_grp(struct regulator_dev *rdev)
114 {
115         return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
116                                                                  VREG_GRP);
117 }
118
119 /*
120  * Enable/disable regulators by joining/leaving the P1 (processor) group.
121  * We assume nobody else is updating the DEV_GRP registers.
122  */
123 /* definition for 4030 family */
124 #define P3_GRP_4030     BIT(7)          /* "peripherals" */
125 #define P2_GRP_4030     BIT(6)          /* secondary processor, modem, etc */
126 #define P1_GRP_4030     BIT(5)          /* CPU/Linux */
127 /* definition for 6030 family */
128 #define P3_GRP_6030     BIT(2)          /* secondary processor, modem, etc */
129 #define P2_GRP_6030     BIT(1)          /* "peripherals" */
130 #define P1_GRP_6030     BIT(0)          /* CPU/Linux */
131
132 static int twl4030reg_is_enabled(struct regulator_dev *rdev)
133 {
134         int     state = twlreg_grp(rdev);
135
136         if (state < 0)
137                 return state;
138
139         return state & P1_GRP_4030;
140 }
141
142 static int twl6030reg_is_enabled(struct regulator_dev *rdev)
143 {
144         struct twlreg_info      *info = rdev_get_drvdata(rdev);
145         int                     grp, val;
146
147         grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
148         if (grp < 0)
149                 return grp;
150
151         grp &= P1_GRP_6030;
152
153         val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
154         val = TWL6030_CFG_STATE_APP(val);
155
156         return grp && (val == TWL6030_CFG_STATE_ON);
157 }
158
159 static int twlreg_enable(struct regulator_dev *rdev)
160 {
161         struct twlreg_info      *info = rdev_get_drvdata(rdev);
162         int                     grp;
163         int                     ret;
164
165         grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
166         if (grp < 0)
167                 return grp;
168
169         if (twl_class_is_4030())
170                 grp |= P1_GRP_4030;
171         else
172                 grp |= P1_GRP_6030;
173
174         ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
175
176         if (!ret && twl_class_is_6030())
177                 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
178                                 grp << TWL6030_CFG_STATE_GRP_SHIFT |
179                                 TWL6030_CFG_STATE_ON);
180
181         udelay(info->delay);
182
183         return ret;
184 }
185
186 static int twlreg_disable(struct regulator_dev *rdev)
187 {
188         struct twlreg_info      *info = rdev_get_drvdata(rdev);
189         int                     grp;
190         int                     ret;
191
192         grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
193         if (grp < 0)
194                 return grp;
195
196         /* For 6030, set the off state for all grps enabled */
197         if (twl_class_is_6030()) {
198                 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
199                         (grp & (P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030)) <<
200                                 TWL6030_CFG_STATE_GRP_SHIFT |
201                         TWL6030_CFG_STATE_OFF);
202                 if (ret)
203                         return ret;
204         }
205
206         if (twl_class_is_4030())
207                 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
208         else
209                 grp &= ~(P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030);
210
211         ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
212
213         /* Next, associate cleared grp in state register */
214         if (!ret && twl_class_is_6030())
215                 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
216                                 grp << TWL6030_CFG_STATE_GRP_SHIFT |
217                                 TWL6030_CFG_STATE_OFF);
218
219         return ret;
220 }
221
222 static int twl4030reg_get_status(struct regulator_dev *rdev)
223 {
224         int     state = twlreg_grp(rdev);
225
226         if (state < 0)
227                 return state;
228         state &= 0x0f;
229
230         /* assume state != WARM_RESET; we'd not be running...  */
231         if (!state)
232                 return REGULATOR_STATUS_OFF;
233         return (state & BIT(3))
234                 ? REGULATOR_STATUS_NORMAL
235                 : REGULATOR_STATUS_STANDBY;
236 }
237
238 static int twl6030reg_get_status(struct regulator_dev *rdev)
239 {
240         struct twlreg_info      *info = rdev_get_drvdata(rdev);
241         int                     val;
242
243         val = twlreg_grp(rdev);
244         if (val < 0)
245                 return val;
246
247         val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
248
249         switch (TWL6030_CFG_STATE_APP(val)) {
250         case TWL6030_CFG_STATE_ON:
251                 return REGULATOR_STATUS_NORMAL;
252
253         case TWL6030_CFG_STATE_SLEEP:
254                 return REGULATOR_STATUS_STANDBY;
255
256         case TWL6030_CFG_STATE_OFF:
257         case TWL6030_CFG_STATE_OFF2:
258         default:
259                 break;
260         }
261
262         return REGULATOR_STATUS_OFF;
263 }
264
265 static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
266 {
267         struct twlreg_info      *info = rdev_get_drvdata(rdev);
268         unsigned                message;
269         int                     status;
270
271         /* We can only set the mode through state machine commands... */
272         switch (mode) {
273         case REGULATOR_MODE_NORMAL:
274                 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
275                 break;
276         case REGULATOR_MODE_STANDBY:
277                 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
278                 break;
279         default:
280                 return -EINVAL;
281         }
282
283         /* Ensure the resource is associated with some group */
284         status = twlreg_grp(rdev);
285         if (status < 0)
286                 return status;
287         if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
288                 return -EACCES;
289
290         status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
291                         message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
292         if (status < 0)
293                 return status;
294
295         return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
296                         message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
297 }
298
299 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
300 {
301         struct twlreg_info      *info = rdev_get_drvdata(rdev);
302         int grp;
303         int val;
304
305         grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
306
307         if (grp < 0)
308                 return grp;
309
310         /* Compose the state register settings */
311         val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
312         /* We can only set the mode through state machine commands... */
313         switch (mode) {
314         case REGULATOR_MODE_NORMAL:
315                 val |= TWL6030_CFG_STATE_ON;
316                 break;
317         case REGULATOR_MODE_STANDBY:
318                 val |= TWL6030_CFG_STATE_SLEEP;
319                 break;
320
321         default:
322                 return -EINVAL;
323         }
324
325         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
326 }
327
328 /*----------------------------------------------------------------------*/
329
330 /*
331  * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
332  * select field in its control register.   We use tables indexed by VSEL
333  * to record voltages in milliVolts.  (Accuracy is about three percent.)
334  *
335  * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
336  * currently handled by listing two slightly different VAUX2 regulators,
337  * only one of which will be configured.
338  *
339  * VSEL values documented as "TI cannot support these values" are flagged
340  * in these tables as UNSUP() values; we normally won't assign them.
341  *
342  * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
343  * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
344  */
345 #ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
346 #define UNSUP_MASK      0x0000
347 #else
348 #define UNSUP_MASK      0x8000
349 #endif
350
351 #define UNSUP(x)        (UNSUP_MASK | (x))
352 #define IS_UNSUP(x)     (UNSUP_MASK & (x))
353 #define LDO_MV(x)       (~UNSUP_MASK & (x))
354
355
356 static const u16 VAUX1_VSEL_table[] = {
357         UNSUP(1500), UNSUP(1800), 2500, 2800,
358         3000, 3000, 3000, 3000,
359 };
360 static const u16 VAUX2_4030_VSEL_table[] = {
361         UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
362         1500, 1800, UNSUP(1850), 2500,
363         UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
364         UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
365 };
366 static const u16 VAUX2_VSEL_table[] = {
367         1700, 1700, 1900, 1300,
368         1500, 1800, 2000, 2500,
369         2100, 2800, 2200, 2300,
370         2400, 2400, 2400, 2400,
371 };
372 static const u16 VAUX3_VSEL_table[] = {
373         1500, 1800, 2500, 2800,
374         3000, 3000, 3000, 3000,
375 };
376 static const u16 VAUX4_VSEL_table[] = {
377         700, 1000, 1200, UNSUP(1300),
378         1500, 1800, UNSUP(1850), 2500,
379         UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
380         UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
381 };
382 static const u16 VMMC1_VSEL_table[] = {
383         1850, 2850, 3000, 3150,
384 };
385 static const u16 VMMC2_VSEL_table[] = {
386         UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
387         UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
388         2600, 2800, 2850, 3000,
389         3150, 3150, 3150, 3150,
390 };
391 static const u16 VPLL1_VSEL_table[] = {
392         1000, 1200, 1300, 1800,
393         UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
394 };
395 static const u16 VPLL2_VSEL_table[] = {
396         700, 1000, 1200, 1300,
397         UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
398         UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
399         UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
400 };
401 static const u16 VSIM_VSEL_table[] = {
402         UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
403         2800, 3000, 3000, 3000,
404 };
405 static const u16 VDAC_VSEL_table[] = {
406         1200, 1300, 1800, 1800,
407 };
408 static const u16 VDD1_VSEL_table[] = {
409         800, 1450,
410 };
411 static const u16 VDD2_VSEL_table[] = {
412         800, 1450, 1500,
413 };
414 static const u16 VIO_VSEL_table[] = {
415         1800, 1850,
416 };
417 static const u16 VINTANA2_VSEL_table[] = {
418         2500, 2750,
419 };
420
421 static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
422 {
423         struct twlreg_info      *info = rdev_get_drvdata(rdev);
424         int                     mV = info->table[index];
425
426         return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
427 }
428
429 static int
430 twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
431                        unsigned *selector)
432 {
433         struct twlreg_info      *info = rdev_get_drvdata(rdev);
434         int                     vsel;
435
436         for (vsel = 0; vsel < info->table_len; vsel++) {
437                 int mV = info->table[vsel];
438                 int uV;
439
440                 if (IS_UNSUP(mV))
441                         continue;
442                 uV = LDO_MV(mV) * 1000;
443
444                 /* REVISIT for VAUX2, first match may not be best/lowest */
445
446                 /* use the first in-range value */
447                 if (min_uV <= uV && uV <= max_uV) {
448                         *selector = vsel;
449                         return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
450                                                         VREG_VOLTAGE, vsel);
451                 }
452         }
453
454         return -EDOM;
455 }
456
457 static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
458 {
459         struct twlreg_info      *info = rdev_get_drvdata(rdev);
460         int             vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
461                                                                 VREG_VOLTAGE);
462
463         if (vsel < 0)
464                 return vsel;
465
466         vsel &= info->table_len - 1;
467         return LDO_MV(info->table[vsel]) * 1000;
468 }
469
470 static struct regulator_ops twl4030ldo_ops = {
471         .list_voltage   = twl4030ldo_list_voltage,
472
473         .set_voltage    = twl4030ldo_set_voltage,
474         .get_voltage    = twl4030ldo_get_voltage,
475
476         .enable         = twlreg_enable,
477         .disable        = twlreg_disable,
478         .is_enabled     = twl4030reg_is_enabled,
479
480         .set_mode       = twl4030reg_set_mode,
481
482         .get_status     = twl4030reg_get_status,
483 };
484
485 static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
486 {
487         struct twlreg_info      *info = rdev_get_drvdata(rdev);
488
489         return ((info->min_mV + (index * 100)) * 1000);
490 }
491
492 static int
493 twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
494                        unsigned *selector)
495 {
496         struct twlreg_info      *info = rdev_get_drvdata(rdev);
497         int                     vsel;
498
499         if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
500                 return -EDOM;
501
502         /*
503          * Use the below formula to calculate vsel
504          * mV = 1000mv + 100mv * (vsel - 1)
505          */
506         vsel = (min_uV/1000 - 1000)/100 + 1;
507         *selector = vsel;
508         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
509
510 }
511
512 static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
513 {
514         struct twlreg_info      *info = rdev_get_drvdata(rdev);
515         int             vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
516                                                                 VREG_VOLTAGE);
517
518         if (vsel < 0)
519                 return vsel;
520
521         /*
522          * Use the below formula to calculate vsel
523          * mV = 1000mv + 100mv * (vsel - 1)
524          */
525         return (1000 + (100 * (vsel - 1))) * 1000;
526 }
527
528 static struct regulator_ops twl6030ldo_ops = {
529         .list_voltage   = twl6030ldo_list_voltage,
530
531         .set_voltage    = twl6030ldo_set_voltage,
532         .get_voltage    = twl6030ldo_get_voltage,
533
534         .enable         = twlreg_enable,
535         .disable        = twlreg_disable,
536         .is_enabled     = twl6030reg_is_enabled,
537
538         .set_mode       = twl6030reg_set_mode,
539
540         .get_status     = twl6030reg_get_status,
541 };
542
543 /*----------------------------------------------------------------------*/
544
545 /*
546  * Fixed voltage LDOs don't have a VSEL field to update.
547  */
548 static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
549 {
550         struct twlreg_info      *info = rdev_get_drvdata(rdev);
551
552         return info->min_mV * 1000;
553 }
554
555 static int twlfixed_get_voltage(struct regulator_dev *rdev)
556 {
557         struct twlreg_info      *info = rdev_get_drvdata(rdev);
558
559         return info->min_mV * 1000;
560 }
561
562 static struct regulator_ops twl4030fixed_ops = {
563         .list_voltage   = twlfixed_list_voltage,
564
565         .get_voltage    = twlfixed_get_voltage,
566
567         .enable         = twlreg_enable,
568         .disable        = twlreg_disable,
569         .is_enabled     = twl4030reg_is_enabled,
570
571         .set_mode       = twl4030reg_set_mode,
572
573         .get_status     = twl4030reg_get_status,
574 };
575
576 static struct regulator_ops twl6030fixed_ops = {
577         .list_voltage   = twlfixed_list_voltage,
578
579         .get_voltage    = twlfixed_get_voltage,
580
581         .enable         = twlreg_enable,
582         .disable        = twlreg_disable,
583         .is_enabled     = twl6030reg_is_enabled,
584
585         .set_mode       = twl6030reg_set_mode,
586
587         .get_status     = twl6030reg_get_status,
588 };
589
590 static struct regulator_ops twl6030_fixed_resource = {
591         .enable         = twlreg_enable,
592         .disable        = twlreg_disable,
593         .is_enabled     = twl6030reg_is_enabled,
594         .get_status     = twl6030reg_get_status,
595 };
596
597 /*----------------------------------------------------------------------*/
598
599 #define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
600                         remap_conf) \
601                 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
602                         remap_conf, TWL4030, twl4030fixed_ops)
603 #define TWL6030_FIXED_LDO(label, offset, mVolts, num, turnon_delay) \
604                 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
605                         0x0, TWL6030, twl6030fixed_ops)
606
607 #define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
608         .base = offset, \
609         .id = num, \
610         .table_len = ARRAY_SIZE(label##_VSEL_table), \
611         .table = label##_VSEL_table, \
612         .delay = turnon_delay, \
613         .remap = remap_conf, \
614         .desc = { \
615                 .name = #label, \
616                 .id = TWL4030_REG_##label, \
617                 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
618                 .ops = &twl4030ldo_ops, \
619                 .type = REGULATOR_VOLTAGE, \
620                 .owner = THIS_MODULE, \
621                 }, \
622         }
623
624 #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts, num) { \
625         .base = offset, \
626         .id = num, \
627         .min_mV = min_mVolts, \
628         .max_mV = max_mVolts, \
629         .desc = { \
630                 .name = #label, \
631                 .id = TWL6030_REG_##label, \
632                 .n_voltages = (max_mVolts - min_mVolts)/100, \
633                 .ops = &twl6030ldo_ops, \
634                 .type = REGULATOR_VOLTAGE, \
635                 .owner = THIS_MODULE, \
636                 }, \
637         }
638
639
640 #define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
641                 family, operations) { \
642         .base = offset, \
643         .id = num, \
644         .min_mV = mVolts, \
645         .delay = turnon_delay, \
646         .remap = remap_conf, \
647         .desc = { \
648                 .name = #label, \
649                 .id = family##_REG_##label, \
650                 .n_voltages = 1, \
651                 .ops = &operations, \
652                 .type = REGULATOR_VOLTAGE, \
653                 .owner = THIS_MODULE, \
654                 }, \
655         }
656
657 #define TWL6030_FIXED_RESOURCE(label, offset, num, turnon_delay) { \
658         .base = offset, \
659         .id = num, \
660         .delay = turnon_delay, \
661         .desc = { \
662                 .name = #label, \
663                 .id = TWL6030_REG_##label, \
664                 .ops = &twl6030_fixed_resource, \
665                 .type = REGULATOR_VOLTAGE, \
666                 .owner = THIS_MODULE, \
667                 }, \
668         }
669
670 /*
671  * We list regulators here if systems need some level of
672  * software control over them after boot.
673  */
674 static struct twlreg_info twl_regs[] = {
675         TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
676         TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
677         TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
678         TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
679         TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
680         TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
681         TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
682         TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
683         TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
684         TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
685         TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
686         TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
687         TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
688         TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
689         TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
690         TWL4030_ADJUSTABLE_LDO(VDD1, 0x55, 15, 1000, 0x08),
691         TWL4030_ADJUSTABLE_LDO(VDD2, 0x63, 16, 1000, 0x08),
692         TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
693         TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
694         TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
695         /* VUSBCP is managed *only* by the USB subchip */
696
697         /* 6030 REG with base as PMC Slave Misc : 0x0030 */
698         /* Turnon-delay and remap configuration values for 6030 are not
699            verified since the specification is not public */
700         TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300, 1),
701         TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300, 2),
702         TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300, 3),
703         TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300, 4),
704         TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300, 5),
705         TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300, 7),
706         TWL6030_FIXED_LDO(VANA, 0x50, 2100, 15, 0),
707         TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 16, 0),
708         TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 17, 0),
709         TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 18, 0),
710         TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 48, 0),
711 };
712
713 static int __devinit twlreg_probe(struct platform_device *pdev)
714 {
715         int                             i;
716         struct twlreg_info              *info;
717         struct regulator_init_data      *initdata;
718         struct regulation_constraints   *c;
719         struct regulator_dev            *rdev;
720
721         for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
722                 if (twl_regs[i].desc.id != pdev->id)
723                         continue;
724                 info = twl_regs + i;
725                 break;
726         }
727         if (!info)
728                 return -ENODEV;
729
730         initdata = pdev->dev.platform_data;
731         if (!initdata)
732                 return -EINVAL;
733
734         /* Constrain board-specific capabilities according to what
735          * this driver and the chip itself can actually do.
736          */
737         c = &initdata->constraints;
738         c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
739         c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
740                                 | REGULATOR_CHANGE_MODE
741                                 | REGULATOR_CHANGE_STATUS;
742         switch (pdev->id) {
743         case TWL4030_REG_VIO:
744         case TWL4030_REG_VDD1:
745         case TWL4030_REG_VDD2:
746         case TWL4030_REG_VPLL1:
747         case TWL4030_REG_VINTANA1:
748         case TWL4030_REG_VINTANA2:
749         case TWL4030_REG_VINTDIG:
750                 c->always_on = true;
751                 break;
752         default:
753                 break;
754         }
755
756         rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
757         if (IS_ERR(rdev)) {
758                 dev_err(&pdev->dev, "can't register %s, %ld\n",
759                                 info->desc.name, PTR_ERR(rdev));
760                 return PTR_ERR(rdev);
761         }
762         platform_set_drvdata(pdev, rdev);
763
764         if (twl_class_is_4030())
765                 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
766                                                 info->remap);
767
768         /* NOTE:  many regulators support short-circuit IRQs (presentable
769          * as REGULATOR_OVER_CURRENT notifications?) configured via:
770          *  - SC_CONFIG
771          *  - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
772          *  - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
773          *  - IT_CONFIG
774          */
775
776         return 0;
777 }
778
779 static int __devexit twlreg_remove(struct platform_device *pdev)
780 {
781         regulator_unregister(platform_get_drvdata(pdev));
782         return 0;
783 }
784
785 MODULE_ALIAS("platform:twl_reg");
786
787 static struct platform_driver twlreg_driver = {
788         .probe          = twlreg_probe,
789         .remove         = __devexit_p(twlreg_remove),
790         /* NOTE: short name, to work around driver model truncation of
791          * "twl_regulator.12" (and friends) to "twl_regulator.1".
792          */
793         .driver.name    = "twl_reg",
794         .driver.owner   = THIS_MODULE,
795 };
796
797 static int __init twlreg_init(void)
798 {
799         return platform_driver_register(&twlreg_driver);
800 }
801 subsys_initcall(twlreg_init);
802
803 static void __exit twlreg_exit(void)
804 {
805         platform_driver_unregister(&twlreg_driver);
806 }
807 module_exit(twlreg_exit)
808
809 MODULE_DESCRIPTION("TWL regulator driver");
810 MODULE_LICENSE("GPL");