Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-microblaze.git] / drivers / regulator / twl6030-regulator.c
1 /*
2  * Split TWL6030 logic from twl-regulator.c:
3  * Copyright (C) 2008 David Brownell
4  *
5  * Copyright (C) 2016 Nicolae Rosia <nicolae.rosia@gmail.com>
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/mfd/twl.h>
25 #include <linux/delay.h>
26
27 struct twlreg_info {
28         /* start of regulator's PM_RECEIVER control register bank */
29         u8                      base;
30
31         /* twl resource ID, for resource control state machine */
32         u8                      id;
33
34         u8                      flags;
35
36         /* used by regulator core */
37         struct regulator_desc   desc;
38
39         /* chip specific features */
40         unsigned long           features;
41
42         /* data passed from board for external get/set voltage */
43         void                    *data;
44 };
45
46
47 /* LDO control registers ... offset is from the base of its register bank.
48  * The first three registers of all power resource banks help hardware to
49  * manage the various resource groups.
50  */
51 /* Common offset in TWL4030/6030 */
52 #define VREG_GRP                0
53 /* TWL6030 register offsets */
54 #define VREG_TRANS              1
55 #define VREG_STATE              2
56 #define VREG_VOLTAGE            3
57 #define VREG_VOLTAGE_SMPS       4
58 /* TWL6030 Misc register offsets */
59 #define VREG_BC_ALL             1
60 #define VREG_BC_REF             2
61 #define VREG_BC_PROC            3
62 #define VREG_BC_CLK_RST         4
63
64 /* TWL6030 LDO register values for CFG_STATE */
65 #define TWL6030_CFG_STATE_OFF   0x00
66 #define TWL6030_CFG_STATE_ON    0x01
67 #define TWL6030_CFG_STATE_OFF2  0x02
68 #define TWL6030_CFG_STATE_SLEEP 0x03
69 #define TWL6030_CFG_STATE_GRP_SHIFT     5
70 #define TWL6030_CFG_STATE_APP_SHIFT     2
71 #define TWL6030_CFG_STATE_APP_MASK      (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
72 #define TWL6030_CFG_STATE_APP(v)        (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
73                                                 TWL6030_CFG_STATE_APP_SHIFT)
74
75 /* Flags for SMPS Voltage reading */
76 #define SMPS_OFFSET_EN          BIT(0)
77 #define SMPS_EXTENDED_EN        BIT(1)
78
79 /* twl6032 SMPS EPROM values */
80 #define TWL6030_SMPS_OFFSET             0xB0
81 #define TWL6030_SMPS_MULT               0xB3
82 #define SMPS_MULTOFFSET_SMPS4   BIT(0)
83 #define SMPS_MULTOFFSET_VIO     BIT(1)
84 #define SMPS_MULTOFFSET_SMPS3   BIT(6)
85
86 static inline int
87 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
88 {
89         u8 value;
90         int status;
91
92         status = twl_i2c_read_u8(slave_subgp,
93                         &value, info->base + offset);
94         return (status < 0) ? status : value;
95 }
96
97 static inline int
98 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
99                                                  u8 value)
100 {
101         return twl_i2c_write_u8(slave_subgp,
102                         value, info->base + offset);
103 }
104
105 /* generic power resource operations, which work on all regulators */
106 static int twlreg_grp(struct regulator_dev *rdev)
107 {
108         return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
109                                                                  VREG_GRP);
110 }
111
112 /*
113  * Enable/disable regulators by joining/leaving the P1 (processor) group.
114  * We assume nobody else is updating the DEV_GRP registers.
115  */
116 /* definition for 6030 family */
117 #define P3_GRP_6030     BIT(2)          /* secondary processor, modem, etc */
118 #define P2_GRP_6030     BIT(1)          /* "peripherals" */
119 #define P1_GRP_6030     BIT(0)          /* CPU/Linux */
120
121 static int twl6030reg_is_enabled(struct regulator_dev *rdev)
122 {
123         struct twlreg_info      *info = rdev_get_drvdata(rdev);
124         int                     grp = 0, val;
125
126         if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS))) {
127                 grp = twlreg_grp(rdev);
128                 if (grp < 0)
129                         return grp;
130                 grp &= P1_GRP_6030;
131         } else {
132                 grp = 1;
133         }
134
135         val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
136         val = TWL6030_CFG_STATE_APP(val);
137
138         return grp && (val == TWL6030_CFG_STATE_ON);
139 }
140
141 #define PB_I2C_BUSY     BIT(0)
142 #define PB_I2C_BWEN     BIT(1)
143
144
145 static int twl6030reg_enable(struct regulator_dev *rdev)
146 {
147         struct twlreg_info      *info = rdev_get_drvdata(rdev);
148         int                     grp = 0;
149         int                     ret;
150
151         if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
152                 grp = twlreg_grp(rdev);
153         if (grp < 0)
154                 return grp;
155
156         ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
157                         grp << TWL6030_CFG_STATE_GRP_SHIFT |
158                         TWL6030_CFG_STATE_ON);
159         return ret;
160 }
161
162 static int twl6030reg_disable(struct regulator_dev *rdev)
163 {
164         struct twlreg_info      *info = rdev_get_drvdata(rdev);
165         int                     grp = 0;
166         int                     ret;
167
168         if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
169                 grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
170
171         /* For 6030, set the off state for all grps enabled */
172         ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
173                         (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
174                         TWL6030_CFG_STATE_OFF);
175
176         return ret;
177 }
178
179 static int twl6030reg_get_status(struct regulator_dev *rdev)
180 {
181         struct twlreg_info      *info = rdev_get_drvdata(rdev);
182         int                     val;
183
184         val = twlreg_grp(rdev);
185         if (val < 0)
186                 return val;
187
188         val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
189
190         switch (TWL6030_CFG_STATE_APP(val)) {
191         case TWL6030_CFG_STATE_ON:
192                 return REGULATOR_STATUS_NORMAL;
193
194         case TWL6030_CFG_STATE_SLEEP:
195                 return REGULATOR_STATUS_STANDBY;
196
197         case TWL6030_CFG_STATE_OFF:
198         case TWL6030_CFG_STATE_OFF2:
199         default:
200                 break;
201         }
202
203         return REGULATOR_STATUS_OFF;
204 }
205
206 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
207 {
208         struct twlreg_info      *info = rdev_get_drvdata(rdev);
209         int grp = 0;
210         int val;
211
212         if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
213                 grp = twlreg_grp(rdev);
214
215         if (grp < 0)
216                 return grp;
217
218         /* Compose the state register settings */
219         val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
220         /* We can only set the mode through state machine commands... */
221         switch (mode) {
222         case REGULATOR_MODE_NORMAL:
223                 val |= TWL6030_CFG_STATE_ON;
224                 break;
225         case REGULATOR_MODE_STANDBY:
226                 val |= TWL6030_CFG_STATE_SLEEP;
227                 break;
228
229         default:
230                 return -EINVAL;
231         }
232
233         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
234 }
235
236 static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
237         int max_uV, unsigned *selector)
238 {
239         return -ENODEV;
240 }
241
242 static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
243 {
244         return -ENODEV;
245 }
246
247 static const struct regulator_ops twl6030coresmps_ops = {
248         .set_voltage    = twl6030coresmps_set_voltage,
249         .get_voltage    = twl6030coresmps_get_voltage,
250 };
251
252 static int
253 twl6030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
254 {
255         struct twlreg_info      *info = rdev_get_drvdata(rdev);
256
257         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
258                             selector);
259 }
260
261 static int twl6030ldo_get_voltage_sel(struct regulator_dev *rdev)
262 {
263         struct twlreg_info      *info = rdev_get_drvdata(rdev);
264         int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
265
266         return vsel;
267 }
268
269 static const struct regulator_ops twl6030ldo_ops = {
270         .list_voltage   = regulator_list_voltage_linear_range,
271
272         .set_voltage_sel = twl6030ldo_set_voltage_sel,
273         .get_voltage_sel = twl6030ldo_get_voltage_sel,
274
275         .enable         = twl6030reg_enable,
276         .disable        = twl6030reg_disable,
277         .is_enabled     = twl6030reg_is_enabled,
278
279         .set_mode       = twl6030reg_set_mode,
280
281         .get_status     = twl6030reg_get_status,
282 };
283
284 static const struct regulator_ops twl6030fixed_ops = {
285         .list_voltage   = regulator_list_voltage_linear,
286
287         .enable         = twl6030reg_enable,
288         .disable        = twl6030reg_disable,
289         .is_enabled     = twl6030reg_is_enabled,
290
291         .set_mode       = twl6030reg_set_mode,
292
293         .get_status     = twl6030reg_get_status,
294 };
295
296 /*
297  * SMPS status and control
298  */
299
300 static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
301 {
302         struct twlreg_info      *info = rdev_get_drvdata(rdev);
303
304         int voltage = 0;
305
306         switch (info->flags) {
307         case SMPS_OFFSET_EN:
308                 voltage = 100000;
309                 /* fall through */
310         case 0:
311                 switch (index) {
312                 case 0:
313                         voltage = 0;
314                         break;
315                 case 58:
316                         voltage = 1350 * 1000;
317                         break;
318                 case 59:
319                         voltage = 1500 * 1000;
320                         break;
321                 case 60:
322                         voltage = 1800 * 1000;
323                         break;
324                 case 61:
325                         voltage = 1900 * 1000;
326                         break;
327                 case 62:
328                         voltage = 2100 * 1000;
329                         break;
330                 default:
331                         voltage += (600000 + (12500 * (index - 1)));
332                 }
333                 break;
334         case SMPS_EXTENDED_EN:
335                 switch (index) {
336                 case 0:
337                         voltage = 0;
338                         break;
339                 case 58:
340                         voltage = 2084 * 1000;
341                         break;
342                 case 59:
343                         voltage = 2315 * 1000;
344                         break;
345                 case 60:
346                         voltage = 2778 * 1000;
347                         break;
348                 case 61:
349                         voltage = 2932 * 1000;
350                         break;
351                 case 62:
352                         voltage = 3241 * 1000;
353                         break;
354                 default:
355                         voltage = (1852000 + (38600 * (index - 1)));
356                 }
357                 break;
358         case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
359                 switch (index) {
360                 case 0:
361                         voltage = 0;
362                         break;
363                 case 58:
364                         voltage = 4167 * 1000;
365                         break;
366                 case 59:
367                         voltage = 2315 * 1000;
368                         break;
369                 case 60:
370                         voltage = 2778 * 1000;
371                         break;
372                 case 61:
373                         voltage = 2932 * 1000;
374                         break;
375                 case 62:
376                         voltage = 3241 * 1000;
377                         break;
378                 default:
379                         voltage = (2161000 + (38600 * (index - 1)));
380                 }
381                 break;
382         }
383
384         return voltage;
385 }
386
387 static int twl6030smps_map_voltage(struct regulator_dev *rdev, int min_uV,
388                                    int max_uV)
389 {
390         struct twlreg_info *info = rdev_get_drvdata(rdev);
391         int vsel = 0;
392
393         switch (info->flags) {
394         case 0:
395                 if (min_uV == 0)
396                         vsel = 0;
397                 else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
398                         vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
399                         vsel++;
400                 }
401                 /* Values 1..57 for vsel are linear and can be calculated
402                  * values 58..62 are non linear.
403                  */
404                 else if ((min_uV > 1900000) && (min_uV <= 2100000))
405                         vsel = 62;
406                 else if ((min_uV > 1800000) && (min_uV <= 1900000))
407                         vsel = 61;
408                 else if ((min_uV > 1500000) && (min_uV <= 1800000))
409                         vsel = 60;
410                 else if ((min_uV > 1350000) && (min_uV <= 1500000))
411                         vsel = 59;
412                 else if ((min_uV > 1300000) && (min_uV <= 1350000))
413                         vsel = 58;
414                 else
415                         return -EINVAL;
416                 break;
417         case SMPS_OFFSET_EN:
418                 if (min_uV == 0)
419                         vsel = 0;
420                 else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
421                         vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
422                         vsel++;
423                 }
424                 /* Values 1..57 for vsel are linear and can be calculated
425                  * values 58..62 are non linear.
426                  */
427                 else if ((min_uV > 1900000) && (min_uV <= 2100000))
428                         vsel = 62;
429                 else if ((min_uV > 1800000) && (min_uV <= 1900000))
430                         vsel = 61;
431                 else if ((min_uV > 1500000) && (min_uV <= 1800000))
432                         vsel = 60;
433                 else if ((min_uV > 1350000) && (min_uV <= 1500000))
434                         vsel = 59;
435                 else
436                         return -EINVAL;
437                 break;
438         case SMPS_EXTENDED_EN:
439                 if (min_uV == 0) {
440                         vsel = 0;
441                 } else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
442                         vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
443                         vsel++;
444                 }
445                 break;
446         case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
447                 if (min_uV == 0) {
448                         vsel = 0;
449                 } else if ((min_uV >= 2161000) && (min_uV <= 4321000)) {
450                         vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
451                         vsel++;
452                 }
453                 break;
454         }
455
456         return vsel;
457 }
458
459 static int twl6030smps_set_voltage_sel(struct regulator_dev *rdev,
460                                        unsigned int selector)
461 {
462         struct twlreg_info *info = rdev_get_drvdata(rdev);
463
464         return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
465                             selector);
466 }
467
468 static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
469 {
470         struct twlreg_info      *info = rdev_get_drvdata(rdev);
471
472         return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
473 }
474
475 static const struct regulator_ops twlsmps_ops = {
476         .list_voltage           = twl6030smps_list_voltage,
477         .map_voltage            = twl6030smps_map_voltage,
478
479         .set_voltage_sel        = twl6030smps_set_voltage_sel,
480         .get_voltage_sel        = twl6030smps_get_voltage_sel,
481
482         .enable                 = twl6030reg_enable,
483         .disable                = twl6030reg_disable,
484         .is_enabled             = twl6030reg_is_enabled,
485
486         .set_mode               = twl6030reg_set_mode,
487
488         .get_status             = twl6030reg_get_status,
489 };
490
491 /*----------------------------------------------------------------------*/
492 static const struct regulator_linear_range twl6030ldo_linear_range[] = {
493         REGULATOR_LINEAR_RANGE(0, 0, 0, 0),
494         REGULATOR_LINEAR_RANGE(1000000, 1, 24, 100000),
495         REGULATOR_LINEAR_RANGE(2750000, 31, 31, 0),
496 };
497
498 #define TWL6030_ADJUSTABLE_SMPS(label) \
499 static const struct twlreg_info TWL6030_INFO_##label = { \
500         .desc = { \
501                 .name = #label, \
502                 .id = TWL6030_REG_##label, \
503                 .ops = &twl6030coresmps_ops, \
504                 .type = REGULATOR_VOLTAGE, \
505                 .owner = THIS_MODULE, \
506                 }, \
507         }
508
509 #define TWL6030_ADJUSTABLE_LDO(label, offset) \
510 static const struct twlreg_info TWL6030_INFO_##label = { \
511         .base = offset, \
512         .desc = { \
513                 .name = #label, \
514                 .id = TWL6030_REG_##label, \
515                 .n_voltages = 32, \
516                 .linear_ranges = twl6030ldo_linear_range, \
517                 .n_linear_ranges = ARRAY_SIZE(twl6030ldo_linear_range), \
518                 .ops = &twl6030ldo_ops, \
519                 .type = REGULATOR_VOLTAGE, \
520                 .owner = THIS_MODULE, \
521                 }, \
522         }
523
524 #define TWL6032_ADJUSTABLE_LDO(label, offset) \
525 static const struct twlreg_info TWL6032_INFO_##label = { \
526         .base = offset, \
527         .desc = { \
528                 .name = #label, \
529                 .id = TWL6032_REG_##label, \
530                 .n_voltages = 32, \
531                 .linear_ranges = twl6030ldo_linear_range, \
532                 .n_linear_ranges = ARRAY_SIZE(twl6030ldo_linear_range), \
533                 .ops = &twl6030ldo_ops, \
534                 .type = REGULATOR_VOLTAGE, \
535                 .owner = THIS_MODULE, \
536                 }, \
537         }
538
539 #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
540 static const struct twlreg_info TWLFIXED_INFO_##label = { \
541         .base = offset, \
542         .id = 0, \
543         .desc = { \
544                 .name = #label, \
545                 .id = TWL6030##_REG_##label, \
546                 .n_voltages = 1, \
547                 .ops = &twl6030fixed_ops, \
548                 .type = REGULATOR_VOLTAGE, \
549                 .owner = THIS_MODULE, \
550                 .min_uV = mVolts * 1000, \
551                 .enable_time = turnon_delay, \
552                 .of_map_mode = NULL, \
553                 }, \
554         }
555
556 #define TWL6032_ADJUSTABLE_SMPS(label, offset) \
557 static const struct twlreg_info TWLSMPS_INFO_##label = { \
558         .base = offset, \
559         .desc = { \
560                 .name = #label, \
561                 .id = TWL6032_REG_##label, \
562                 .n_voltages = 63, \
563                 .ops = &twlsmps_ops, \
564                 .type = REGULATOR_VOLTAGE, \
565                 .owner = THIS_MODULE, \
566                 }, \
567         }
568
569 /* VUSBCP is managed *only* by the USB subchip */
570 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
571 /* Turnon-delay and remap configuration values for 6030 are not
572    verified since the specification is not public */
573 TWL6030_ADJUSTABLE_SMPS(VDD1);
574 TWL6030_ADJUSTABLE_SMPS(VDD2);
575 TWL6030_ADJUSTABLE_SMPS(VDD3);
576 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54);
577 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58);
578 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c);
579 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68);
580 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c);
581 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74);
582 /* 6025 are renamed compared to 6030 versions */
583 TWL6032_ADJUSTABLE_LDO(LDO2, 0x54);
584 TWL6032_ADJUSTABLE_LDO(LDO4, 0x58);
585 TWL6032_ADJUSTABLE_LDO(LDO3, 0x5c);
586 TWL6032_ADJUSTABLE_LDO(LDO5, 0x68);
587 TWL6032_ADJUSTABLE_LDO(LDO1, 0x6c);
588 TWL6032_ADJUSTABLE_LDO(LDO7, 0x74);
589 TWL6032_ADJUSTABLE_LDO(LDO6, 0x60);
590 TWL6032_ADJUSTABLE_LDO(LDOLN, 0x64);
591 TWL6032_ADJUSTABLE_LDO(LDOUSB, 0x70);
592 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0);
593 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0);
594 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0);
595 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0);
596 TWL6030_FIXED_LDO(V1V8, 0x16, 1800, 0);
597 TWL6030_FIXED_LDO(V2V1, 0x1c, 2100, 0);
598 TWL6032_ADJUSTABLE_SMPS(SMPS3, 0x34);
599 TWL6032_ADJUSTABLE_SMPS(SMPS4, 0x10);
600 TWL6032_ADJUSTABLE_SMPS(VIO, 0x16);
601
602 static u8 twl_get_smps_offset(void)
603 {
604         u8 value;
605
606         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
607                         TWL6030_SMPS_OFFSET);
608         return value;
609 }
610
611 static u8 twl_get_smps_mult(void)
612 {
613         u8 value;
614
615         twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
616                         TWL6030_SMPS_MULT);
617         return value;
618 }
619
620 #define TWL_OF_MATCH(comp, family, label) \
621         { \
622                 .compatible = comp, \
623                 .data = &family##_INFO_##label, \
624         }
625
626 #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
627 #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
628 #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
629 #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
630
631 static const struct of_device_id twl_of_match[] = {
632         TWL6030_OF_MATCH("ti,twl6030-vdd1", VDD1),
633         TWL6030_OF_MATCH("ti,twl6030-vdd2", VDD2),
634         TWL6030_OF_MATCH("ti,twl6030-vdd3", VDD3),
635         TWL6030_OF_MATCH("ti,twl6030-vaux1", VAUX1_6030),
636         TWL6030_OF_MATCH("ti,twl6030-vaux2", VAUX2_6030),
637         TWL6030_OF_MATCH("ti,twl6030-vaux3", VAUX3_6030),
638         TWL6030_OF_MATCH("ti,twl6030-vmmc", VMMC),
639         TWL6030_OF_MATCH("ti,twl6030-vpp", VPP),
640         TWL6030_OF_MATCH("ti,twl6030-vusim", VUSIM),
641         TWL6032_OF_MATCH("ti,twl6032-ldo2", LDO2),
642         TWL6032_OF_MATCH("ti,twl6032-ldo4", LDO4),
643         TWL6032_OF_MATCH("ti,twl6032-ldo3", LDO3),
644         TWL6032_OF_MATCH("ti,twl6032-ldo5", LDO5),
645         TWL6032_OF_MATCH("ti,twl6032-ldo1", LDO1),
646         TWL6032_OF_MATCH("ti,twl6032-ldo7", LDO7),
647         TWL6032_OF_MATCH("ti,twl6032-ldo6", LDO6),
648         TWL6032_OF_MATCH("ti,twl6032-ldoln", LDOLN),
649         TWL6032_OF_MATCH("ti,twl6032-ldousb", LDOUSB),
650         TWLFIXED_OF_MATCH("ti,twl6030-vana", VANA),
651         TWLFIXED_OF_MATCH("ti,twl6030-vcxio", VCXIO),
652         TWLFIXED_OF_MATCH("ti,twl6030-vdac", VDAC),
653         TWLFIXED_OF_MATCH("ti,twl6030-vusb", VUSB),
654         TWLFIXED_OF_MATCH("ti,twl6030-v1v8", V1V8),
655         TWLFIXED_OF_MATCH("ti,twl6030-v2v1", V2V1),
656         TWLSMPS_OF_MATCH("ti,twl6032-smps3", SMPS3),
657         TWLSMPS_OF_MATCH("ti,twl6032-smps4", SMPS4),
658         TWLSMPS_OF_MATCH("ti,twl6032-vio", VIO),
659         {},
660 };
661 MODULE_DEVICE_TABLE(of, twl_of_match);
662
663 static int twlreg_probe(struct platform_device *pdev)
664 {
665         int id;
666         struct twlreg_info              *info;
667         const struct twlreg_info        *template;
668         struct regulator_init_data      *initdata;
669         struct regulation_constraints   *c;
670         struct regulator_dev            *rdev;
671         struct regulator_config         config = { };
672
673         template = of_device_get_match_data(&pdev->dev);
674         if (!template)
675                 return -ENODEV;
676
677         id = template->desc.id;
678         initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
679                                                 &template->desc);
680         if (!initdata)
681                 return -EINVAL;
682
683         info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
684         if (!info)
685                 return -ENOMEM;
686
687         /* Constrain board-specific capabilities according to what
688          * this driver and the chip itself can actually do.
689          */
690         c = &initdata->constraints;
691         c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
692         c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
693                                 | REGULATOR_CHANGE_MODE
694                                 | REGULATOR_CHANGE_STATUS;
695
696         switch (id) {
697         case TWL6032_REG_SMPS3:
698                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
699                         info->flags |= SMPS_EXTENDED_EN;
700                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
701                         info->flags |= SMPS_OFFSET_EN;
702                 break;
703         case TWL6032_REG_SMPS4:
704                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
705                         info->flags |= SMPS_EXTENDED_EN;
706                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
707                         info->flags |= SMPS_OFFSET_EN;
708                 break;
709         case TWL6032_REG_VIO:
710                 if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
711                         info->flags |= SMPS_EXTENDED_EN;
712                 if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
713                         info->flags |= SMPS_OFFSET_EN;
714                 break;
715         }
716
717         config.dev = &pdev->dev;
718         config.init_data = initdata;
719         config.driver_data = info;
720         config.of_node = pdev->dev.of_node;
721
722         rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
723         if (IS_ERR(rdev)) {
724                 dev_err(&pdev->dev, "can't register %s, %ld\n",
725                                 info->desc.name, PTR_ERR(rdev));
726                 return PTR_ERR(rdev);
727         }
728         platform_set_drvdata(pdev, rdev);
729
730         /* NOTE:  many regulators support short-circuit IRQs (presentable
731          * as REGULATOR_OVER_CURRENT notifications?) configured via:
732          *  - SC_CONFIG
733          *  - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
734          *  - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
735          *  - IT_CONFIG
736          */
737
738         return 0;
739 }
740
741 MODULE_ALIAS("platform:twl6030_reg");
742
743 static struct platform_driver twlreg_driver = {
744         .probe          = twlreg_probe,
745         /* NOTE: short name, to work around driver model truncation of
746          * "twl_regulator.12" (and friends) to "twl_regulator.1".
747          */
748         .driver  = {
749                 .name  = "twl6030_reg",
750                 .of_match_table = of_match_ptr(twl_of_match),
751         },
752 };
753
754 static int __init twlreg_init(void)
755 {
756         return platform_driver_register(&twlreg_driver);
757 }
758 subsys_initcall(twlreg_init);
759
760 static void __exit twlreg_exit(void)
761 {
762         platform_driver_unregister(&twlreg_driver);
763 }
764 module_exit(twlreg_exit)
765
766 MODULE_DESCRIPTION("TWL6030 regulator driver");
767 MODULE_LICENSE("GPL");