f6918bff9dff89930081a85e7e1c215ac42df646
[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 #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
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 twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
565 {
566         struct twlreg_info      *info = rdev_get_drvdata(rdev);
567
568         return ((info->min_mV + (index * 100)) * 1000);
569 }
570
571 static int
572 twl6030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
573                        unsigned *selector)
574 {
575         struct twlreg_info      *info = rdev_get_drvdata(rdev);
576         int                     vsel;
577
578         if ((min_uV/1000 < info->min_mV) || (max_uV/1000 > info->max_mV))
579                 return -EDOM;
580
581         /*
582          * Use the below formula to calculate vsel
583          * mV = 1000mv + 100mv * (vsel - 1)
584          */
585         vsel = (min_uV/1000 - 1000)/100 + 1;
586         *selector = vsel;
587         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE, vsel);
588
589 }
590
591 static int twl6030ldo_get_voltage(struct regulator_dev *rdev)
592 {
593         struct twlreg_info      *info = rdev_get_drvdata(rdev);
594         int             vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
595                                                                 VREG_VOLTAGE);
596
597         if (vsel < 0)
598                 return vsel;
599
600         /*
601          * Use the below formula to calculate vsel
602          * mV = 1000mv + 100mv * (vsel - 1)
603          */
604         return (1000 + (100 * (vsel - 1))) * 1000;
605 }
606
607 static struct regulator_ops twl6030ldo_ops = {
608         .list_voltage   = twl6030ldo_list_voltage,
609
610         .set_voltage    = twl6030ldo_set_voltage,
611         .get_voltage    = twl6030ldo_get_voltage,
612
613         .enable         = twl6030reg_enable,
614         .disable        = twl6030reg_disable,
615         .is_enabled     = twl6030reg_is_enabled,
616
617         .set_mode       = twl6030reg_set_mode,
618
619         .get_status     = twl6030reg_get_status,
620 };
621
622 /*----------------------------------------------------------------------*/
623
624 /*
625  * Fixed voltage LDOs don't have a VSEL field to update.
626  */
627 static int twlfixed_list_voltage(struct regulator_dev *rdev, unsigned index)
628 {
629         struct twlreg_info      *info = rdev_get_drvdata(rdev);
630
631         return info->min_mV * 1000;
632 }
633
634 static int twlfixed_get_voltage(struct regulator_dev *rdev)
635 {
636         struct twlreg_info      *info = rdev_get_drvdata(rdev);
637
638         return info->min_mV * 1000;
639 }
640
641 static struct regulator_ops twl4030fixed_ops = {
642         .list_voltage   = twlfixed_list_voltage,
643
644         .get_voltage    = twlfixed_get_voltage,
645
646         .enable         = twl4030reg_enable,
647         .disable        = twl4030reg_disable,
648         .is_enabled     = twl4030reg_is_enabled,
649
650         .set_mode       = twl4030reg_set_mode,
651
652         .get_status     = twl4030reg_get_status,
653 };
654
655 static struct regulator_ops twl6030fixed_ops = {
656         .list_voltage   = twlfixed_list_voltage,
657
658         .get_voltage    = twlfixed_get_voltage,
659
660         .enable         = twl6030reg_enable,
661         .disable        = twl6030reg_disable,
662         .is_enabled     = twl6030reg_is_enabled,
663
664         .set_mode       = twl6030reg_set_mode,
665
666         .get_status     = twl6030reg_get_status,
667 };
668
669 static struct regulator_ops twl6030_fixed_resource = {
670         .enable         = twl6030reg_enable,
671         .disable        = twl6030reg_disable,
672         .is_enabled     = twl6030reg_is_enabled,
673         .get_status     = twl6030reg_get_status,
674 };
675
676 /*
677  * SMPS status and control
678  */
679
680 static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
681 {
682         struct twlreg_info      *info = rdev_get_drvdata(rdev);
683
684         int voltage = 0;
685
686         switch (info->flags) {
687         case SMPS_OFFSET_EN:
688                 voltage = 100000;
689                 /* fall through */
690         case 0:
691                 switch (index) {
692                 case 0:
693                         voltage = 0;
694                         break;
695                 case 58:
696                         voltage = 1350 * 1000;
697                         break;
698                 case 59:
699                         voltage = 1500 * 1000;
700                         break;
701                 case 60:
702                         voltage = 1800 * 1000;
703                         break;
704                 case 61:
705                         voltage = 1900 * 1000;
706                         break;
707                 case 62:
708                         voltage = 2100 * 1000;
709                         break;
710                 default:
711                         voltage += (600000 + (12500 * (index - 1)));
712                 }
713                 break;
714         case SMPS_EXTENDED_EN:
715                 switch (index) {
716                 case 0:
717                         voltage = 0;
718                         break;
719                 case 58:
720                         voltage = 2084 * 1000;
721                         break;
722                 case 59:
723                         voltage = 2315 * 1000;
724                         break;
725                 case 60:
726                         voltage = 2778 * 1000;
727                         break;
728                 case 61:
729                         voltage = 2932 * 1000;
730                         break;
731                 case 62:
732                         voltage = 3241 * 1000;
733                         break;
734                 default:
735                         voltage = (1852000 + (38600 * (index - 1)));
736                 }
737                 break;
738         case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
739                 switch (index) {
740                 case 0:
741                         voltage = 0;
742                         break;
743                 case 58:
744                         voltage = 4167 * 1000;
745                         break;
746                 case 59:
747                         voltage = 2315 * 1000;
748                         break;
749                 case 60:
750                         voltage = 2778 * 1000;
751                         break;
752                 case 61:
753                         voltage = 2932 * 1000;
754                         break;
755                 case 62:
756                         voltage = 3241 * 1000;
757                         break;
758                 default:
759                         voltage = (2161000 + (38600 * (index - 1)));
760                 }
761                 break;
762         }
763
764         return voltage;
765 }
766
767 static int
768 twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
769                         unsigned int *selector)
770 {
771         struct twlreg_info      *info = rdev_get_drvdata(rdev);
772         int     vsel = 0;
773
774         switch (info->flags) {
775         case 0:
776                 if (min_uV == 0)
777                         vsel = 0;
778                 else if ((min_uV >= 600000) && (max_uV <= 1300000)) {
779                         vsel = (min_uV - 600000) / 125;
780                         if (vsel % 100)
781                                 vsel += 100;
782                         vsel /= 100;
783                         vsel++;
784                 }
785                 /* Values 1..57 for vsel are linear and can be calculated
786                  * values 58..62 are non linear.
787                  */
788                 else if ((min_uV > 1900000) && (max_uV >= 2100000))
789                         vsel = 62;
790                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
791                         vsel = 61;
792                 else if ((min_uV > 1500000) && (max_uV >= 1800000))
793                         vsel = 60;
794                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
795                         vsel = 59;
796                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
797                         vsel = 58;
798                 else
799                         return -EINVAL;
800                 break;
801         case SMPS_OFFSET_EN:
802                 if (min_uV == 0)
803                         vsel = 0;
804                 else if ((min_uV >= 700000) && (max_uV <= 1420000)) {
805                         vsel = (min_uV - 700000) / 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 > 1350000) && (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_EXTENDED_EN:
828                 if (min_uV == 0)
829                         vsel = 0;
830                 else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
831                         vsel = (min_uV - 1852000) / 386;
832                         if (vsel % 100)
833                                 vsel += 100;
834                         vsel /= 100;
835                         vsel++;
836                 }
837                 break;
838         case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
839                 if (min_uV == 0)
840                         vsel = 0;
841                 else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
842                         vsel = (min_uV - 1852000) / 386;
843                         if (vsel % 100)
844                                 vsel += 100;
845                         vsel /= 100;
846                         vsel++;
847                 }
848                 break;
849         }
850
851         *selector = vsel;
852
853         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
854                                                         vsel);
855 }
856
857 static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
858 {
859         struct twlreg_info      *info = rdev_get_drvdata(rdev);
860
861         return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
862 }
863
864 static struct regulator_ops twlsmps_ops = {
865         .list_voltage           = twl6030smps_list_voltage,
866
867         .set_voltage            = twl6030smps_set_voltage,
868         .get_voltage_sel        = twl6030smps_get_voltage_sel,
869
870         .enable                 = twl6030reg_enable,
871         .disable                = twl6030reg_disable,
872         .is_enabled             = twl6030reg_is_enabled,
873
874         .set_mode               = twl6030reg_set_mode,
875
876         .get_status             = twl6030reg_get_status,
877 };
878
879 /*----------------------------------------------------------------------*/
880
881 #define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
882                         remap_conf) \
883                 TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
884                         remap_conf, TWL4030, twl4030fixed_ops)
885 #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
886                 TWL_FIXED_LDO(label, offset, mVolts, 0x0, turnon_delay, \
887                         0x0, TWL6030, twl6030fixed_ops)
888
889 #define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) { \
890         .base = offset, \
891         .id = num, \
892         .table_len = ARRAY_SIZE(label##_VSEL_table), \
893         .table = label##_VSEL_table, \
894         .delay = turnon_delay, \
895         .remap = remap_conf, \
896         .desc = { \
897                 .name = #label, \
898                 .id = TWL4030_REG_##label, \
899                 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
900                 .ops = &twl4030ldo_ops, \
901                 .type = REGULATOR_VOLTAGE, \
902                 .owner = THIS_MODULE, \
903                 }, \
904         }
905
906 #define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
907         { \
908         .base = offset, \
909         .id = num, \
910         .delay = turnon_delay, \
911         .remap = remap_conf, \
912         .desc = { \
913                 .name = #label, \
914                 .id = TWL4030_REG_##label, \
915                 .ops = &twl4030smps_ops, \
916                 .type = REGULATOR_VOLTAGE, \
917                 .owner = THIS_MODULE, \
918                 }, \
919         }
920
921 #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
922         .base = offset, \
923         .min_mV = min_mVolts, \
924         .max_mV = max_mVolts, \
925         .desc = { \
926                 .name = #label, \
927                 .id = TWL6030_REG_##label, \
928                 .n_voltages = (max_mVolts - min_mVolts)/100 + 1, \
929                 .ops = &twl6030ldo_ops, \
930                 .type = REGULATOR_VOLTAGE, \
931                 .owner = THIS_MODULE, \
932                 }, \
933         }
934
935 #define TWL6025_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) { \
936         .base = offset, \
937         .min_mV = min_mVolts, \
938         .max_mV = max_mVolts, \
939         .desc = { \
940                 .name = #label, \
941                 .id = TWL6025_REG_##label, \
942                 .n_voltages = ((max_mVolts - min_mVolts)/100) + 1, \
943                 .ops = &twl6030ldo_ops, \
944                 .type = REGULATOR_VOLTAGE, \
945                 .owner = THIS_MODULE, \
946                 }, \
947         }
948
949 #define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
950                 family, operations) { \
951         .base = offset, \
952         .id = num, \
953         .min_mV = mVolts, \
954         .delay = turnon_delay, \
955         .remap = remap_conf, \
956         .desc = { \
957                 .name = #label, \
958                 .id = family##_REG_##label, \
959                 .n_voltages = 1, \
960                 .ops = &operations, \
961                 .type = REGULATOR_VOLTAGE, \
962                 .owner = THIS_MODULE, \
963                 }, \
964         }
965
966 #define TWL6030_FIXED_RESOURCE(label, offset, turnon_delay) { \
967         .base = offset, \
968         .delay = turnon_delay, \
969         .desc = { \
970                 .name = #label, \
971                 .id = TWL6030_REG_##label, \
972                 .ops = &twl6030_fixed_resource, \
973                 .type = REGULATOR_VOLTAGE, \
974                 .owner = THIS_MODULE, \
975                 }, \
976         }
977
978 #define TWL6025_ADJUSTABLE_SMPS(label, offset) { \
979         .base = offset, \
980         .min_mV = 600, \
981         .max_mV = 2100, \
982         .desc = { \
983                 .name = #label, \
984                 .id = TWL6025_REG_##label, \
985                 .n_voltages = 63, \
986                 .ops = &twlsmps_ops, \
987                 .type = REGULATOR_VOLTAGE, \
988                 .owner = THIS_MODULE, \
989                 }, \
990         }
991
992 /*
993  * We list regulators here if systems need some level of
994  * software control over them after boot.
995  */
996 static struct twlreg_info twl_regs[] = {
997         TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08),
998         TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08),
999         TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08),
1000         TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08),
1001         TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08),
1002         TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08),
1003         TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08),
1004         TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00),
1005         TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08),
1006         TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00),
1007         TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08),
1008         TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08),
1009         TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08),
1010         TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08),
1011         TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08),
1012         TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08),
1013         TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08),
1014         TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08),
1015         TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08),
1016         TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08),
1017         /* VUSBCP is managed *only* by the USB subchip */
1018
1019         /* 6030 REG with base as PMC Slave Misc : 0x0030 */
1020         /* Turnon-delay and remap configuration values for 6030 are not
1021            verified since the specification is not public */
1022         TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300),
1023         TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300),
1024         TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300),
1025         TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300),
1026         TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300),
1027         TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300),
1028         TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0),
1029         TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0),
1030         TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0),
1031         TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0),
1032         TWL6030_FIXED_RESOURCE(CLK32KG, 0x8C, 0),
1033
1034         /* 6025 are renamed compared to 6030 versions */
1035         TWL6025_ADJUSTABLE_LDO(LDO2, 0x54, 1000, 3300),
1036         TWL6025_ADJUSTABLE_LDO(LDO4, 0x58, 1000, 3300),
1037         TWL6025_ADJUSTABLE_LDO(LDO3, 0x5c, 1000, 3300),
1038         TWL6025_ADJUSTABLE_LDO(LDO5, 0x68, 1000, 3300),
1039         TWL6025_ADJUSTABLE_LDO(LDO1, 0x6c, 1000, 3300),
1040         TWL6025_ADJUSTABLE_LDO(LDO7, 0x74, 1000, 3300),
1041         TWL6025_ADJUSTABLE_LDO(LDO6, 0x60, 1000, 3300),
1042         TWL6025_ADJUSTABLE_LDO(LDOLN, 0x64, 1000, 3300),
1043         TWL6025_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000, 3300),
1044
1045         TWL6025_ADJUSTABLE_SMPS(SMPS3, 0x34),
1046         TWL6025_ADJUSTABLE_SMPS(SMPS4, 0x10),
1047         TWL6025_ADJUSTABLE_SMPS(VIO, 0x16),
1048 };
1049
1050 static u8 twl_get_smps_offset(void)
1051 {
1052         u8 value;
1053
1054         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1055                         TWL6030_SMPS_OFFSET);
1056         return value;
1057 }
1058
1059 static u8 twl_get_smps_mult(void)
1060 {
1061         u8 value;
1062
1063         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
1064                         TWL6030_SMPS_MULT);
1065         return value;
1066 }
1067
1068 static int __devinit twlreg_probe(struct platform_device *pdev)
1069 {
1070         int                             i;
1071         struct twlreg_info              *info;
1072         struct regulator_init_data      *initdata;
1073         struct regulation_constraints   *c;
1074         struct regulator_dev            *rdev;
1075         struct twl_regulator_driver_data        *drvdata;
1076
1077         for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
1078                 if (twl_regs[i].desc.id != pdev->id)
1079                         continue;
1080                 info = twl_regs + i;
1081                 break;
1082         }
1083         if (!info)
1084                 return -ENODEV;
1085
1086         initdata = pdev->dev.platform_data;
1087         if (!initdata)
1088                 return -EINVAL;
1089
1090         drvdata = initdata->driver_data;
1091
1092         if (!drvdata)
1093                 return -EINVAL;
1094
1095         /* copy the driver data into regulator data */
1096         info->features = drvdata->features;
1097         info->data = drvdata->data;
1098         info->set_voltage = drvdata->set_voltage;
1099         info->get_voltage = drvdata->get_voltage;
1100
1101         /* Constrain board-specific capabilities according to what
1102          * this driver and the chip itself can actually do.
1103          */
1104         c = &initdata->constraints;
1105         c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
1106         c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
1107                                 | REGULATOR_CHANGE_MODE
1108                                 | REGULATOR_CHANGE_STATUS;
1109         switch (pdev->id) {
1110         case TWL4030_REG_VIO:
1111         case TWL4030_REG_VDD1:
1112         case TWL4030_REG_VDD2:
1113         case TWL4030_REG_VPLL1:
1114         case TWL4030_REG_VINTANA1:
1115         case TWL4030_REG_VINTANA2:
1116         case TWL4030_REG_VINTDIG:
1117                 c->always_on = true;
1118                 break;
1119         default:
1120                 break;
1121         }
1122
1123         switch (pdev->id) {
1124         case TWL6025_REG_SMPS3:
1125                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
1126                         info->flags |= SMPS_EXTENDED_EN;
1127                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
1128                         info->flags |= SMPS_OFFSET_EN;
1129                 break;
1130         case TWL6025_REG_SMPS4:
1131                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
1132                         info->flags |= SMPS_EXTENDED_EN;
1133                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
1134                         info->flags |= SMPS_OFFSET_EN;
1135                 break;
1136         case TWL6025_REG_VIO:
1137                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
1138                         info->flags |= SMPS_EXTENDED_EN;
1139                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
1140                         info->flags |= SMPS_OFFSET_EN;
1141                 break;
1142         }
1143
1144         rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
1145         if (IS_ERR(rdev)) {
1146                 dev_err(&pdev->dev, "can't register %s, %ld\n",
1147                                 info->desc.name, PTR_ERR(rdev));
1148                 return PTR_ERR(rdev);
1149         }
1150         platform_set_drvdata(pdev, rdev);
1151
1152         if (twl_class_is_4030())
1153                 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
1154                                                 info->remap);
1155
1156         /* NOTE:  many regulators support short-circuit IRQs (presentable
1157          * as REGULATOR_OVER_CURRENT notifications?) configured via:
1158          *  - SC_CONFIG
1159          *  - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
1160          *  - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
1161          *  - IT_CONFIG
1162          */
1163
1164         return 0;
1165 }
1166
1167 static int __devexit twlreg_remove(struct platform_device *pdev)
1168 {
1169         regulator_unregister(platform_get_drvdata(pdev));
1170         return 0;
1171 }
1172
1173 MODULE_ALIAS("platform:twl_reg");
1174
1175 static struct platform_driver twlreg_driver = {
1176         .probe          = twlreg_probe,
1177         .remove         = __devexit_p(twlreg_remove),
1178         /* NOTE: short name, to work around driver model truncation of
1179          * "twl_regulator.12" (and friends) to "twl_regulator.1".
1180          */
1181         .driver.name    = "twl_reg",
1182         .driver.owner   = THIS_MODULE,
1183 };
1184
1185 static int __init twlreg_init(void)
1186 {
1187         return platform_driver_register(&twlreg_driver);
1188 }
1189 subsys_initcall(twlreg_init);
1190
1191 static void __exit twlreg_exit(void)
1192 {
1193         platform_driver_unregister(&twlreg_driver);
1194 }
1195 module_exit(twlreg_exit)
1196
1197 MODULE_DESCRIPTION("TWL regulator driver");
1198 MODULE_LICENSE("GPL");