Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.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         u8                      flags;
55
56         /* used by regulator core */
57         struct regulator_desc   desc;
58
59         /* chip specific features */
60         unsigned long           features;
61
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;
71 };
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  */
78 /* Common offset in TWL4030/6030 */
79 #define VREG_GRP                0
80 /* TWL4030 register offsets */
81 #define VREG_TYPE               1
82 #define VREG_REMAP              2
83 #define VREG_DEDICATED          3       /* LDO control */
84 #define VREG_VOLTAGE_SMPS_4030  9
85 /* TWL6030 register offsets */
86 #define VREG_TRANS              1
87 #define VREG_STATE              2
88 #define VREG_VOLTAGE            3
89 #define VREG_VOLTAGE_SMPS       4
90 /* 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
95
96 /* TWL6030 LDO register values for CFG_STATE */
97 #define TWL6030_CFG_STATE_OFF   0x00
98 #define TWL6030_CFG_STATE_ON    0x01
99 #define TWL6030_CFG_STATE_OFF2  0x02
100 #define TWL6030_CFG_STATE_SLEEP 0x03
101 #define TWL6030_CFG_STATE_GRP_SHIFT     5
102 #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)
106
107 /* 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
118 static inline int
119 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
120 {
121         u8 value;
122         int status;
123
124         status = twl_i2c_read_u8(slave_subgp,
125                         &value, info->base + offset);
126         return (status < 0) ? status : value;
127 }
128
129 static inline int
130 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
131                                                  u8 value)
132 {
133         return twl_i2c_write_u8(slave_subgp,
134                         value, info->base + offset);
135 }
136
137 /*----------------------------------------------------------------------*/
138
139 /* generic power resource operations, which work on all regulators */
140
141 static int twlreg_grp(struct regulator_dev *rdev)
142 {
143         return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
144                                                                  VREG_GRP);
145 }
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  */
151 /* 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 */
159
160 static int twl4030reg_is_enabled(struct regulator_dev *rdev)
161 {
162         int     state = twlreg_grp(rdev);
163
164         if (state < 0)
165                 return state;
166
167         return state & P1_GRP_4030;
168 }
169
170 static int twl6030reg_is_enabled(struct regulator_dev *rdev)
171 {
172         struct twlreg_info      *info = rdev_get_drvdata(rdev);
173         int                     grp = 0, val;
174
175         if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
176                 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
177         if (grp < 0)
178                 return grp;
179
180         if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
181                 grp &= P1_GRP_6030;
182         else
183                 grp = 1;
184
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);
189 }
190
191 static int twl4030reg_enable(struct regulator_dev *rdev)
192 {
193         struct twlreg_info      *info = rdev_get_drvdata(rdev);
194         int                     grp;
195         int                     ret;
196
197         grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
198         if (grp < 0)
199                 return grp;
200
201         grp |= P1_GRP_4030;
202
203         ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
204
205         udelay(info->delay);
206
207         return ret;
208 }
209
210 static int twl6030reg_enable(struct regulator_dev *rdev)
211 {
212         struct twlreg_info      *info = rdev_get_drvdata(rdev);
213         int                     grp = 0;
214         int                     ret;
215
216         if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
217                 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
218         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);
224
225         udelay(info->delay);
226
227         return ret;
228 }
229
230 static int twl4030reg_disable(struct regulator_dev *rdev)
231 {
232         struct twlreg_info      *info = rdev_get_drvdata(rdev);
233         int                     grp;
234         int                     ret;
235
236         grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
237         if (grp < 0)
238                 return grp;
239
240         grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
241
242         ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
243
244         return ret;
245 }
246
247 static 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
253         if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
254                 grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
255
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);
260
261         return ret;
262 }
263
264 static int twl4030reg_get_status(struct regulator_dev *rdev)
265 {
266         int     state = twlreg_grp(rdev);
267
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
280 static 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
307 static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
308 {
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 */
326         status = twlreg_grp(rdev);
327         if (status < 0)
328                 return status;
329         if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
330                 return -EACCES;
331
332         status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
333                         message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
334         if (status < 0)
335                 return status;
336
337         return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
338                         message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
339 }
340
341 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
342 {
343         struct twlreg_info      *info = rdev_get_drvdata(rdev);
344         int grp = 0;
345         int val;
346
347         if (!(twl_class_is_6030() && (info->features & TWL6025_SUBCLASS)))
348                 grp = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_GRP);
349
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
371 /*----------------------------------------------------------------------*/
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.
384  *
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.
387  */
388 #if 1 //def 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
399 static const u16 VAUX1_VSEL_table[] = {
400         UNSUP(1500), UNSUP(1800), 2500, 2800,
401         3000, 3000, 3000, 3000,
402 };
403 static 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 };
409 static 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 };
415 static const u16 VAUX3_VSEL_table[] = {
416         1500, 1800, 2500, 2800,
417         3000, 3000, 3000, 3000,
418 };
419 static const u16 VAUX4_VSEL_table[] = {
420         700, 1000, 1200, UNSUP(1300),
421         1500, 1800, UNSUP(1850), 2500,
422         UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
423         UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
424 };
425 static const u16 VMMC1_VSEL_table[] = {
426         1850, 2850, 3000, 3150,
427 };
428 static 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 };
434 static const u16 VPLL1_VSEL_table[] = {
435         1000, 1200, 1300, 1800,
436         UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
437 };
438 static 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 };
444 static const u16 VSIM_VSEL_table[] = {
445         UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
446         2800, 3000, 3000, 3000,
447 };
448 static const u16 VDAC_VSEL_table[] = {
449         1200, 1300, 1800, 1800,
450 };
451 static const u16 VDD1_VSEL_table[] = {
452         800, 1450,
453 };
454 static const u16 VDD2_VSEL_table[] = {
455         800, 1450, 1500,
456 };
457 static const u16 VIO_VSEL_table[] = {
458         1800, 1850,
459 };
460 static const u16 VINTANA2_VSEL_table[] = {
461         2500, 2750,
462 };
463
464 static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
465 {
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
472 static int
473 twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
474                        unsigned *selector)
475 {
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
487                 /* REVISIT for VAUX2, first match may not be best/lowest */
488
489                 /* use the first in-range value */
490                 if (min_uV <= uV && uV <= max_uV) {
491                         *selector = vsel;
492                         return twlreg_write(info, TWL_MODULE_PM_RECEIVER,
493                                                         VREG_VOLTAGE, vsel);
494                 }
495         }
496
497         return -EDOM;
498 }
499
500 static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
501 {
502         struct twlreg_info      *info = rdev_get_drvdata(rdev);
503         int             vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
504                                                                 VREG_VOLTAGE);
505
506         if (vsel < 0)
507                 return vsel;
508
509         vsel &= info->table_len - 1;
510         return LDO_MV(info->table[vsel]) * 1000;
511 }
512
513 static struct regulator_ops twl4030ldo_ops = {
514         .list_voltage   = twl4030ldo_list_voltage,
515
516         .set_voltage    = twl4030ldo_set_voltage,
517         .get_voltage    = twl4030ldo_get_voltage,
518
519         .enable         = twl4030reg_enable,
520         .disable        = twl4030reg_disable,
521         .is_enabled     = twl4030reg_is_enabled,
522
523         .set_mode       = twl4030reg_set_mode,
524
525         .get_status     = twl4030reg_get_status,
526 };
527
528 static int
529 twl4030smps_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
535         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
542         return 0;
543 }
544
545 static int twl4030smps_get_voltage(struct regulator_dev *rdev)
546 {
547         struct twlreg_info *info = rdev_get_drvdata(rdev);
548         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,
554                 VREG_VOLTAGE_SMPS_4030);
555
556         return vsel * 12500 + 600000;
557 }
558
559 static struct regulator_ops twl4030smps_ops = {
560         .set_voltage    = twl4030smps_set_voltage,
561         .get_voltage    = twl4030smps_get_voltage,
562 };
563
564 static 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
575 static 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
585 static struct regulator_ops twl6030coresmps_ops = {
586         .set_voltage    = twl6030coresmps_set_voltage,
587         .get_voltage    = twl6030coresmps_get_voltage,
588 };
589
590 static 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
597 static int
598 twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
599                        unsigned *selector)
600 {
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;
612         *selector = vsel;
613         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
614
615 }
616
617 static 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
633 static struct regulator_ops twl6030ldo_ops = {
634         .list_voltage   = twl6030ldo_list_voltage,
635
636         .set_voltage    = twl6030ldo_set_voltage,
637         .get_voltage    = twl6030ldo_get_voltage,
638
639         .enable         = twl6030reg_enable,
640         .disable        = twl6030reg_disable,
641         .is_enabled     = twl6030reg_is_enabled,
642
643         .set_mode       = twl6030reg_set_mode,
644
645         .get_status     = twl6030reg_get_status,
646 };
647
648 /*----------------------------------------------------------------------*/
649
650 /*
651  * Fixed voltage LDOs don't have a VSEL field to update.
652  */
653 static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
654 {
655         struct twlreg_info      *info = rdev_get_drvdata(rdev);
656
657         return info->min_mV * 1000;
658 }
659
660 static int twlfixed_get_voltage(struct regulator_dev *rdev)
661 {
662         struct twlreg_info      *info = rdev_get_drvdata(rdev);
663
664         return info->min_mV * 1000;
665 }
666
667 static struct regulator_ops twl4030fixed_ops = {
668         .list_voltage   = twlfixed_list_voltage,
669
670         .get_voltage    = twlfixed_get_voltage,
671
672         .enable         = twl4030reg_enable,
673         .disable        = twl4030reg_disable,
674         .is_enabled     = twl4030reg_is_enabled,
675
676         .set_mode       = twl4030reg_set_mode,
677
678         .get_status     = twl4030reg_get_status,
679 };
680
681 static struct regulator_ops twl6030fixed_ops = {
682         .list_voltage   = twlfixed_list_voltage,
683
684         .get_voltage    = twlfixed_get_voltage,
685
686         .enable         = twl6030reg_enable,
687         .disable        = twl6030reg_disable,
688         .is_enabled     = twl6030reg_is_enabled,
689
690         .set_mode       = twl6030reg_set_mode,
691
692         .get_status     = twl6030reg_get_status,
693 };
694
695 static struct regulator_ops twl6030_fixed_resource = {
696         .enable         = twl6030reg_enable,
697         .disable        = twl6030reg_disable,
698         .is_enabled     = twl6030reg_is_enabled,
699         .get_status     = twl6030reg_get_status,
700 };
701
702 /*
703  * SMPS status and control
704  */
705
706 static 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
793 static int
794 twl6030smps_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;
804                 else if ((min_uV >= 600000) && (max_uV <= 1300000)) {
805                         vsel = (min_uV - 600000) / 125;
806                         if (vsel % 100)
807                                 vsel += 100;
808                         vsel /= 100;
809                         vsel++;
810                 }
811                 /* Values 1..57 for vsel are linear and can be calculated
812                  * values 58..62 are non linear.
813                  */
814                 else if ((min_uV > 1900000) && (max_uV >= 2100000))
815                         vsel = 62;
816                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
817                         vsel = 61;
818                 else if ((min_uV > 1500000) && (max_uV >= 1800000))
819                         vsel = 60;
820                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
821                         vsel = 59;
822                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
823                         vsel = 58;
824                 else
825                         return -EINVAL;
826                 break;
827         case SMPS_OFFSET_EN:
828                 if (min_uV == 0)
829                         vsel = 0;
830                 else if ((min_uV >= 700000) && (max_uV <= 1420000)) {
831                         vsel = (min_uV - 700000) / 125;
832                         if (vsel % 100)
833                                 vsel += 100;
834                         vsel /= 100;
835                         vsel++;
836                 }
837                 /* Values 1..57 for vsel are linear and can be calculated
838                  * values 58..62 are non linear.
839                  */
840                 else if ((min_uV > 1900000) && (max_uV >= 2100000))
841                         vsel = 62;
842                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
843                         vsel = 61;
844                 else if ((min_uV > 1350000) && (max_uV >= 1800000))
845                         vsel = 60;
846                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
847                         vsel = 59;
848                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
849                         vsel = 58;
850                 else
851                         return -EINVAL;
852                 break;
853         case SMPS_EXTENDED_EN:
854                 if (min_uV == 0)
855                         vsel = 0;
856                 else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
857                         vsel = (min_uV - 1852000) / 386;
858                         if (vsel % 100)
859                                 vsel += 100;
860                         vsel /= 100;
861                         vsel++;
862                 }
863                 break;
864         case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
865                 if (min_uV == 0)
866                         vsel = 0;
867                 else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
868                         vsel = (min_uV - 1852000) / 386;
869                         if (vsel % 100)
870                                 vsel += 100;
871                         vsel /= 100;
872                         vsel++;
873                 }
874                 break;
875         }
876
877         *selector = vsel;
878
879         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
880                                                         vsel);
881 }
882
883 static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
884 {
885         struct twlreg_info      *info = rdev_get_drvdata(rdev);
886
887         return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
888 }
889
890 static struct regulator_ops twlsmps_ops = {
891         .list_voltage           = twl6030smps_list_voltage,
892
893         .set_voltage            = twl6030smps_set_voltage,
894         .get_voltage_sel        = twl6030smps_get_voltage_sel,
895
896         .enable                 = twl6030reg_enable,
897         .disable                = twl6030reg_disable,
898         .is_enabled             = twl6030reg_is_enabled,
899
900         .set_mode               = twl6030reg_set_mode,
901
902         .get_status             = twl6030reg_get_status,
903 };
904
905 /*----------------------------------------------------------------------*/
906
907 #define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
908                         remap_conf) \
909                 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
910                         remap_conf, TWL4030, twl4030fixed_ops)
911 #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
912                 TWL_FIXED_LDO(label, offset, mVolts, 0x0, turnon_delay, \
913                         0x0, TWL6030, twl6030fixed_ops)
914
915 #define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
916         .base = offset, \
917         .id = num, \
918         .table_len = ARRAY_SIZE(label##_VSEL_table), \
919         .table = label##_VSEL_table, \
920         .delay = turnon_delay, \
921         .remap = remap_conf, \
922         .desc = { \
923                 .name = #label, \
924                 .id = TWL4030_REG_##label, \
925                 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
926                 .ops = &twl4030ldo_ops, \
927                 .type = REGULATOR_VOLTAGE, \
928                 .owner = THIS_MODULE, \
929                 }, \
930         }
931
932 #define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
933         { \
934         .base = offset, \
935         .id = num, \
936         .delay = turnon_delay, \
937         .remap = remap_conf, \
938         .desc = { \
939                 .name = #label, \
940                 .id = TWL4030_REG_##label, \
941                 .ops = &twl4030smps_ops, \
942                 .type = REGULATOR_VOLTAGE, \
943                 .owner = THIS_MODULE, \
944                 }, \
945         }
946
947 #define TWL6030_ADJUSTABLE_SMPS(label) { \
948         .desc = { \
949                 .name = #label, \
950                 .id = TWL6030_REG_##label, \
951                 .ops = &twl6030coresmps_ops, \
952                 .type = REGULATOR_VOLTAGE, \
953                 .owner = THIS_MODULE, \
954                 }, \
955         }
956
957 #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
958         .base = offset, \
959         .min_mV = min_mVolts, \
960         .max_mV = max_mVolts, \
961         .desc = { \
962                 .name = #label, \
963                 .id = TWL6030_REG_##label, \
964                 .n_voltages = (max_mVolts - min_mVolts)/100 + 1, \
965                 .ops = &twl6030ldo_ops, \
966                 .type = REGULATOR_VOLTAGE, \
967                 .owner = THIS_MODULE, \
968                 }, \
969         }
970
971 #define TWL6025_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
972         .base = offset, \
973         .min_mV = min_mVolts, \
974         .max_mV = max_mVolts, \
975         .desc = { \
976                 .name = #label, \
977                 .id = TWL6025_REG_##label, \
978                 .n_voltages = ((max_mVolts - min_mVolts)/100) + 1, \
979                 .ops = &twl6030ldo_ops, \
980                 .type = REGULATOR_VOLTAGE, \
981                 .owner = THIS_MODULE, \
982                 }, \
983         }
984
985 #define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
986                 family, operations) { \
987         .base = offset, \
988         .id = num, \
989         .min_mV = mVolts, \
990         .delay = turnon_delay, \
991         .remap = remap_conf, \
992         .desc = { \
993                 .name = #label, \
994                 .id = family##_REG_##label, \
995                 .n_voltages = 1, \
996                 .ops = &operations, \
997                 .type = REGULATOR_VOLTAGE, \
998                 .owner = THIS_MODULE, \
999                 }, \
1000         }
1001
1002 #define TWL6030_FIXED_RESOURCE(label, offset, turnon_delay) { \
1003         .base = offset, \
1004         .delay = turnon_delay, \
1005         .desc = { \
1006                 .name = #label, \
1007                 .id = TWL6030_REG_##label, \
1008                 .ops = &twl6030_fixed_resource, \
1009                 .type = REGULATOR_VOLTAGE, \
1010                 .owner = THIS_MODULE, \
1011                 }, \
1012         }
1013
1014 #define TWL6025_ADJUSTABLE_SMPS(label, offset) { \
1015         .base = offset, \
1016         .min_mV = 600, \
1017         .max_mV = 2100, \
1018         .desc = { \
1019                 .name = #label, \
1020                 .id = TWL6025_REG_##label, \
1021                 .n_voltages = 63, \
1022                 .ops = &twlsmps_ops, \
1023                 .type = REGULATOR_VOLTAGE, \
1024                 .owner = THIS_MODULE, \
1025                 }, \
1026         }
1027
1028 /*
1029  * We list regulators here if systems need some level of
1030  * software control over them after boot.
1031  */
1032 static struct twlreg_info twl_regs[] = {
1033         TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
1034         TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
1035         TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
1036         TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
1037         TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
1038         TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
1039         TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
1040         TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
1041         TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
1042         TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
1043         TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
1044         TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
1045         TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
1046         TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
1047         TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
1048         TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08),
1049         TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08),
1050         TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
1051         TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
1052         TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
1053         /* VUSBCP is managed *only* by the USB subchip */
1054
1055         /* 6030 REG with base as PMC Slave Misc : 0x0030 */
1056         /* Turnon-delay and remap configuration values for 6030 are not
1057            verified since the specification is not public */
1058         TWL6030_ADJUSTABLE_SMPS(VDD1),
1059         TWL6030_ADJUSTABLE_SMPS(VDD2),
1060         TWL6030_ADJUSTABLE_SMPS(VDD3),
1061         TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300),
1062         TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300),
1063         TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300),
1064         TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300),
1065         TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300),
1066         TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300),
1067         TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0),
1068         TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0),
1069         TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0),
1070         TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0),
1071         TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 0),
1072
1073         /* 6025 are renamed compared to 6030 versions */
1074         TWL6025_ADJUSTABLE_LDO(LDO2, 0x54, 1000, 3300),
1075         TWL6025_ADJUSTABLE_LDO(LDO4, 0x58, 1000, 3300),
1076         TWL6025_ADJUSTABLE_LDO(LDO3, 0x5c, 1000, 3300),
1077         TWL6025_ADJUSTABLE_LDO(LDO5, 0x68, 1000, 3300),
1078         TWL6025_ADJUSTABLE_LDO(LDO1, 0x6c, 1000, 3300),
1079         TWL6025_ADJUSTABLE_LDO(LDO7, 0x74, 1000, 3300),
1080         TWL6025_ADJUSTABLE_LDO(LDO6, 0x60, 1000, 3300),
1081         TWL6025_ADJUSTABLE_LDO(LDOLN, 0x64, 1000, 3300),
1082         TWL6025_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000, 3300),
1083
1084         TWL6025_ADJUSTABLE_SMPS(SMPS3, 0x34),
1085         TWL6025_ADJUSTABLE_SMPS(SMPS4, 0x10),
1086         TWL6025_ADJUSTABLE_SMPS(VIO, 0x16),
1087 };
1088
1089 static u8 twl_get_smps_offset(void)
1090 {
1091         u8 value;
1092
1093         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1094                         TWL6030_SMPS_OFFSET);
1095         return value;
1096 }
1097
1098 static u8 twl_get_smps_mult(void)
1099 {
1100         u8 value;
1101
1102         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1103                         TWL6030_SMPS_MULT);
1104         return value;
1105 }
1106
1107 static int __devinit twlreg_probe(struct platform_device *pdev)
1108 {
1109         int                             i;
1110         struct twlreg_info              *info;
1111         struct regulator_init_data      *initdata;
1112         struct regulation_constraints   *c;
1113         struct regulator_dev            *rdev;
1114         struct twl_regulator_driver_data        *drvdata;
1115
1116         for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
1117                 if (twl_regs[i].desc.id != pdev->id)
1118                         continue;
1119                 info = twl_regs + i;
1120                 break;
1121         }
1122         if (!info)
1123                 return -ENODEV;
1124
1125         initdata = pdev->dev.platform_data;
1126         if (!initdata)
1127                 return -EINVAL;
1128
1129         drvdata = initdata->driver_data;
1130
1131         if (!drvdata)
1132                 return -EINVAL;
1133
1134         /* copy the driver data into regulator data */
1135         info->features = drvdata->features;
1136         info->data = drvdata->data;
1137         info->set_voltage = drvdata->set_voltage;
1138         info->get_voltage = drvdata->get_voltage;
1139
1140         /* Constrain board-specific capabilities according to what
1141          * this driver and the chip itself can actually do.
1142          */
1143         c = &initdata->constraints;
1144         c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1145         c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
1146                                 | REGULATOR_CHANGE_MODE
1147                                 | REGULATOR_CHANGE_STATUS;
1148         switch (pdev->id) {
1149         case TWL4030_REG_VIO:
1150         case TWL4030_REG_VDD1:
1151         case TWL4030_REG_VDD2:
1152         case TWL4030_REG_VPLL1:
1153         case TWL4030_REG_VINTANA1:
1154         case TWL4030_REG_VINTANA2:
1155         case TWL4030_REG_VINTDIG:
1156                 c->always_on = true;
1157                 break;
1158         default:
1159                 break;
1160         }
1161
1162         switch (pdev->id) {
1163         case TWL6025_REG_SMPS3:
1164                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
1165                         info->flags |= SMPS_EXTENDED_EN;
1166                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
1167                         info->flags |= SMPS_OFFSET_EN;
1168                 break;
1169         case TWL6025_REG_SMPS4:
1170                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
1171                         info->flags |= SMPS_EXTENDED_EN;
1172                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
1173                         info->flags |= SMPS_OFFSET_EN;
1174                 break;
1175         case TWL6025_REG_VIO:
1176                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
1177                         info->flags |= SMPS_EXTENDED_EN;
1178                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
1179                         info->flags |= SMPS_OFFSET_EN;
1180                 break;
1181         }
1182
1183         rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
1184         if (IS_ERR(rdev)) {
1185                 dev_err(&pdev->dev, "can't register %s, %ld\n",
1186                                 info->desc.name, PTR_ERR(rdev));
1187                 return PTR_ERR(rdev);
1188         }
1189         platform_set_drvdata(pdev, rdev);
1190
1191         if (twl_class_is_4030())
1192                 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
1193                                                 info->remap);
1194
1195         /* NOTE:  many regulators support short-circuit IRQs (presentable
1196          * as REGULATOR_OVER_CURRENT notifications?) configured via:
1197          *  - SC_CONFIG
1198          *  - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
1199          *  - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
1200          *  - IT_CONFIG
1201          */
1202
1203         return 0;
1204 }
1205
1206 static int __devexit twlreg_remove(struct platform_device *pdev)
1207 {
1208         regulator_unregister(platform_get_drvdata(pdev));
1209         return 0;
1210 }
1211
1212 MODULE_ALIAS("platform:twl_reg");
1213
1214 static struct platform_driver twlreg_driver = {
1215         .probe          = twlreg_probe,
1216         .remove         = __devexit_p(twlreg_remove),
1217         /* NOTE: short name, to work around driver model truncation of
1218          * "twl_regulator.12" (and friends) to "twl_regulator.1".
1219          */
1220         .driver.name    = "twl_reg",
1221         .driver.owner   = THIS_MODULE,
1222 };
1223
1224 static int __init twlreg_init(void)
1225 {
1226         return platform_driver_register(&twlreg_driver);
1227 }
1228 subsys_initcall(twlreg_init);
1229
1230 static void __exit twlreg_exit(void)
1231 {
1232         platform_driver_unregister(&twlreg_driver);
1233 }
1234 module_exit(twlreg_exit)
1235
1236 MODULE_DESCRIPTION("TWL regulator driver");
1237 MODULE_LICENSE("GPL");