regulator: tps80031: Check configuraton for LDO2-Track mode
[linux-2.6.git] / drivers / regulator / tps80031-regulator.c
1 /*
2  * driver/regulator/tps80031-regulator.c
3  *
4  * Regulator driver for TI TPS80031
5  *
6  * Copyright (C) 2011 NVIDIA Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/regulator/tps80031-regulator.h>
33 #include <linux/mfd/tps80031.h>
34
35 /* Flags for DCDC Voltage reading */
36 #define DCDC_OFFSET_EN          BIT(0)
37 #define DCDC_EXTENDED_EN        BIT(1)
38 #define TRACK_MODE_ENABLE       BIT(2)
39
40 #define SMPS_MULTOFFSET_VIO     BIT(1)
41 #define SMPS_MULTOFFSET_SMPS1   BIT(3)
42 #define SMPS_MULTOFFSET_SMPS2   BIT(4)
43 #define SMPS_MULTOFFSET_SMPS3   BIT(6)
44 #define SMPS_MULTOFFSET_SMPS4   BIT(0)
45
46 #define PMC_SMPS_OFFSET_ADD     0xE0
47 #define PMC_SMPS_MULT_ADD       0xE3
48
49 #define STATE_OFF               0x00
50 #define STATE_ON                0x01
51 #define STATE_MASK              0x03
52
53 #define TRANS_SLEEP_OFF         0x00
54 #define TRANS_SLEEP_ON          0x04
55 #define TRANS_SLEEP_MASK        0x0C
56
57 #define SMPS_CMD_MASK           0xC0
58 #define SMPS_VSEL_MASK          0x3F
59 #define LDO_VSEL_MASK           0x1F
60
61 #define TPS80031_MISC2_ADD      0xE5
62 #define MISC2_LDOUSB_IN_VSYS    0x10
63 #define MISC2_LDOUSB_IN_PMID    0x08
64 #define MISC2_LDOUSB_IN_MASK    0x18
65
66 #define MISC2_LDO3_SEL_VIB_VAL  BIT(0)
67 #define MISC2_LDO3_SEL_VIB_MASK 0x1
68
69 #define CHARGERUSB_CTRL3_ADD    0xEA
70 #define BOOST_HW_PWR_EN         BIT(5)
71 #define BOOST_HW_PWR_EN_MASK    BIT(5)
72
73 #define CHARGERUSB_CTRL1_ADD    0xE8
74 #define OPA_MODE_EN             BIT(6)
75 #define OPA_MODE_EN_MASK        BIT(6)
76
77 #define USB_VBUS_CTRL_SET       0x04
78 #define USB_VBUS_CTRL_CLR       0x05
79 #define VBUS_DISCHRG            0x20
80
81 #define EXT_PWR_REQ (PWR_REQ_INPUT_PREQ1 | PWR_REQ_INPUT_PREQ2 | \
82                         PWR_REQ_INPUT_PREQ3)
83
84 struct tps80031_regulator {
85
86         /* Regulator register address.*/
87         u8              trans_reg;
88         u8              state_reg;
89         u8              force_reg;
90         u8              volt_reg;
91         u8              volt_id;
92         uint8_t         trans_reg_cache;
93         uint8_t         state_reg_cache;
94         uint8_t         force_reg_cache;
95         uint8_t         volt_reg_cache;
96
97         /* twl resource ID, for resource control state machine */
98         u8                      id;
99
100         /* chip constraints on regulator behavior */
101         u16                     min_mV;
102         u16                     max_mV;
103
104         /* regulator specific turn-on delay */
105         int                     delay;
106
107         u8                      flags;
108         unsigned int            platform_flags;
109         enum tps80031_ext_control ext_pwr_ctrl;
110
111         /* used by regulator core */
112         struct regulator_desc   desc;
113
114         /* Device */
115         struct device           *dev;
116
117         /*Power request bits */
118         int preq_bit;
119 };
120
121 static inline struct device *to_tps80031_dev(struct regulator_dev *rdev)
122 {
123         return rdev_get_dev(rdev)->parent->parent;
124 }
125
126 static int tps80031_regulator_enable_time(struct regulator_dev *rdev)
127 {
128         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
129
130         return ri->delay;
131 }
132
133 static u8 tps80031_get_smps_offset(struct device *parent)
134 {
135         u8 value;
136         int ret;
137
138         ret = tps80031_read(parent, SLAVE_ID1, PMC_SMPS_OFFSET_ADD, &value);
139         if (ret < 0) {
140                 dev_err(parent, "Error in reading smps offset register\n");
141                 return 0;
142         }
143         return value;
144 }
145
146 static u8 tps80031_get_smps_mult(struct device *parent)
147 {
148         u8 value;
149         int ret;
150
151         ret = tps80031_read(parent, SLAVE_ID1, PMC_SMPS_MULT_ADD, &value);
152         if (ret < 0) {
153                 dev_err(parent, "Error in reading smps mult register\n");
154                 return 0;
155         }
156         return value;
157 }
158
159 static int tps80031_reg_is_enabled(struct regulator_dev *rdev)
160 {
161         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
162
163         if (ri->ext_pwr_ctrl & EXT_PWR_REQ)
164                 return true;
165         return ((ri->state_reg_cache & STATE_MASK) == STATE_ON);
166 }
167
168 static int tps80031_reg_enable(struct regulator_dev *rdev)
169 {
170         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
171         struct device *parent = to_tps80031_dev(rdev);
172         int ret;
173         uint8_t reg_val;
174
175         if (ri->ext_pwr_ctrl & EXT_PWR_REQ)
176                 return 0;
177
178         reg_val = (ri->state_reg_cache & ~STATE_MASK) |
179                                         (STATE_ON & STATE_MASK);
180         ret = tps80031_write(parent, SLAVE_ID1, ri->state_reg, reg_val);
181         if (ret < 0) {
182                 dev_err(&rdev->dev, "Error in writing the STATE register\n");
183                 return ret;
184         }
185         ri->state_reg_cache = reg_val;
186         udelay(ri->delay);
187         return ret;
188 }
189
190 static int tps80031_reg_disable(struct regulator_dev *rdev)
191 {
192         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
193         struct device *parent = to_tps80031_dev(rdev);
194         int ret;
195         uint8_t reg_val;
196
197         if (ri->ext_pwr_ctrl & EXT_PWR_REQ)
198                 return 0;
199
200         reg_val = (ri->state_reg_cache & ~STATE_MASK) |
201                                         (STATE_OFF & STATE_MASK);
202         ret = tps80031_write(parent, SLAVE_ID1, ri->state_reg, reg_val);
203         if (ret < 0)
204                 dev_err(&rdev->dev, "Error in writing the STATE register\n");
205         else
206                 ri->state_reg_cache = reg_val;
207         return ret;
208 }
209
210 /*
211  * DCDC status and control
212  */
213 static int tps80031dcdc_list_voltage(struct regulator_dev *rdev, unsigned index)
214 {
215         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
216         int voltage = 0;
217
218         switch (ri->flags) {
219         case 0:
220                 if (index == 0)
221                         voltage = 0;
222                 else if (index < 58)
223                         voltage = (600000 + (12500 * (index - 1)));
224                 else if (index == 58)
225                         voltage = 1350 * 1000;
226                 else if (index == 59)
227                         voltage = 1500 * 1000;
228                 else if (index == 60)
229                         voltage = 1800 * 1000;
230                 else if (index == 61)
231                         voltage = 1900 * 1000;
232                 else if (index == 62)
233                         voltage = 2100 * 1000;
234                 break;
235
236         case DCDC_OFFSET_EN:
237                 if (index == 0)
238                         voltage = 0;
239                 else if (index < 58)
240                         voltage = (700000 + (12500 * (index - 1)));
241                 else if (index == 58)
242                         voltage = 1350 * 1000;
243                 else if (index == 59)
244                         voltage = 1500 * 1000;
245                 else if (index == 60)
246                         voltage = 1800 * 1000;
247                 else if (index == 61)
248                         voltage = 1900 * 1000;
249                 else if (index == 62)
250                         voltage = 2100 * 1000;
251                 break;
252
253         case DCDC_EXTENDED_EN:
254                 if (index == 0)
255                         voltage = 0;
256                 else if (index < 58)
257                         voltage = (1852000 + (38600 * (index - 1)));
258                 else if (index == 58)
259                         voltage = 2084 * 1000;
260                 else if (index == 59)
261                         voltage = 2315 * 1000;
262                 else if (index == 60)
263                         voltage = 2778 * 1000;
264                 else if (index == 61)
265                         voltage = 2932 * 1000;
266                 else if (index == 62)
267                         voltage = 3241 * 1000;
268                 break;
269
270         case DCDC_OFFSET_EN|DCDC_EXTENDED_EN:
271                 if (index == 0)
272                         voltage = 0;
273                 else if (index < 58)
274                         voltage = (2161000 + (38600 * (index - 1)));
275                 else if (index == 58)
276                         voltage = 4167 * 1000;
277                 else if (index == 59)
278                         voltage = 2315 * 1000;
279                 else if (index == 60)
280                         voltage = 2778 * 1000;
281                 else if (index == 61)
282                         voltage = 2932 * 1000;
283                 else if (index == 62)
284                         voltage = 3241 * 1000;
285                 break;
286         }
287
288         return voltage;
289 }
290
291 static int __tps80031_dcdc_set_voltage(struct device *parent,
292                                        struct tps80031_regulator *ri,
293                                        int min_uV, int max_uV,
294                                        unsigned *selector)
295 {
296         int vsel = 0;
297         int ret;
298
299         switch (ri->flags) {
300         case 0:
301                 if (min_uV == 0)
302                         vsel = 0;
303                 else if ((min_uV >= 600000) && (max_uV <= 1300000)) {
304                         vsel = (min_uV - 600000) / 125;
305                         if (vsel % 100)
306                                 vsel += 100;
307                         vsel /= 100;
308                         vsel++;
309                 } else if ((min_uV > 1900000) && (max_uV >= 2100000))
310                         vsel = 62;
311                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
312                         vsel = 61;
313                 else if ((min_uV > 1500000) && (max_uV >= 1800000))
314                         vsel = 60;
315                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
316                         vsel = 59;
317                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
318                         vsel = 58;
319                 else
320                         return -EINVAL;
321                 break;
322
323         case DCDC_OFFSET_EN:
324                 if (min_uV == 0)
325                         vsel = 0;
326                 else if ((min_uV >= 700000) && (max_uV <= 1420000)) {
327                         vsel = (min_uV - 600000) / 125;
328                         if (vsel % 100)
329                                 vsel += 100;
330                         vsel /= 100;
331                         vsel++;
332                 } else if ((min_uV > 1900000) && (max_uV >= 2100000))
333                         vsel = 62;
334                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
335                         vsel = 61;
336                 else if ((min_uV > 1350000) && (max_uV >= 1800000))
337                         vsel = 60;
338                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
339                         vsel = 59;
340                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
341                         vsel = 58;
342                 else
343                         return -EINVAL;
344                 break;
345
346         case DCDC_EXTENDED_EN:
347                 if (min_uV == 0)
348                         vsel = 0;
349                 else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
350                         vsel = (min_uV - 1852000) / 386;
351                         if (vsel % 100)
352                                 vsel += 100;
353                         vsel /= 100;
354                         vsel++;
355                 }
356                 break;
357
358         case DCDC_OFFSET_EN|DCDC_EXTENDED_EN:
359                 if (min_uV == 0)
360                         vsel = 0;
361                 else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
362                         vsel = (min_uV - 1852000) / 386;
363                         if (vsel % 100)
364                                 vsel += 100;
365                         vsel /= 100;
366                         vsel++;
367                 }
368                 break;
369         }
370
371         if (selector)
372                 *selector = vsel;
373
374         if (ri->force_reg) {
375                 if (((ri->force_reg_cache >> 6) & 0x3) == 0) {
376                         ret = tps80031_write(parent, ri->volt_id,
377                                                 ri->force_reg, vsel);
378                         if (ret < 0)
379                                 dev_err(ri->dev, "Error in writing the "
380                                                 "force register\n");
381                         else
382                                 ri->force_reg_cache = vsel;
383                         return ret;
384                 }
385         }
386         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
387         if (ret < 0)
388                 dev_err(ri->dev, "Error in writing the Voltage register\n");
389         else
390                 ri->volt_reg_cache = vsel;
391         return ret;
392 }
393
394 static int tps80031dcdc_set_voltage(struct regulator_dev *rdev,
395                         int min_uV, int max_uV, unsigned *selector)
396 {
397         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
398         struct device *parent = to_tps80031_dev(rdev);
399         return __tps80031_dcdc_set_voltage(parent, ri, min_uV, max_uV,
400                                            selector);
401 }
402
403 static int tps80031dcdc_get_voltage(struct regulator_dev *rdev)
404 {
405         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
406         uint8_t vsel = 0;
407         int voltage = 0;
408
409         if (ri->force_reg) {
410                 vsel  = ri->force_reg_cache;
411                 if ((vsel & SMPS_CMD_MASK) == 0)
412                         goto decode;
413         }
414
415         vsel =  ri->volt_reg_cache;
416
417 decode:
418         vsel &= SMPS_VSEL_MASK;
419
420         switch (ri->flags) {
421         case 0:
422                 if (vsel == 0)
423                         voltage = 0;
424                 else if (vsel < 58)
425                         voltage = (600000 + (12500 * (vsel - 1)));
426                 else if (vsel == 58)
427                         voltage = 1350 * 1000;
428                 else if (vsel == 59)
429                         voltage = 1500 * 1000;
430                 else if (vsel == 60)
431                         voltage = 1800 * 1000;
432                 else if (vsel == 61)
433                         voltage = 1900 * 1000;
434                 else if (vsel == 62)
435                         voltage = 2100 * 1000;
436                 break;
437
438         case DCDC_OFFSET_EN:
439                 if (vsel == 0)
440                         voltage = 0;
441                 else if (vsel < 58)
442                         voltage = (700000 + (12500 * (vsel - 1)));
443                 else if (vsel == 58)
444                         voltage = 1350 * 1000;
445                 else if (vsel == 59)
446                         voltage = 1500 * 1000;
447                 else if (vsel == 60)
448                         voltage = 1800 * 1000;
449                 else if (vsel == 61)
450                         voltage = 1900 * 1000;
451                 else if (vsel == 62)
452                         voltage = 2100 * 1000;
453                 break;
454
455         case DCDC_EXTENDED_EN:
456                 if (vsel == 0)
457                         voltage = 0;
458                 else if (vsel < 58)
459                         voltage = (1852000 + (38600 * (vsel - 1)));
460                 else if (vsel == 58)
461                         voltage = 2084 * 1000;
462                 else if (vsel == 59)
463                         voltage = 2315 * 1000;
464                 else if (vsel == 60)
465                         voltage = 2778 * 1000;
466                 else if (vsel == 61)
467                         voltage = 2932 * 1000;
468                 else if (vsel == 62)
469                         voltage = 3241 * 1000;
470                 break;
471
472         case DCDC_EXTENDED_EN|DCDC_OFFSET_EN:
473                 if (vsel == 0)
474                         voltage = 0;
475                 else if (vsel < 58)
476                         voltage = (2161000 + (38600 * (vsel - 1)));
477                 else if (vsel == 58)
478                         voltage = 4167 * 1000;
479                 else if (vsel == 59)
480                         voltage = 2315 * 1000;
481                 else if (vsel == 60)
482                         voltage = 2778 * 1000;
483                 else if (vsel == 61)
484                         voltage = 2932 * 1000;
485                 else if (vsel == 62)
486                         voltage = 3241 * 1000;
487                 break;
488         }
489
490         return voltage;
491 }
492
493 static int tps80031ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
494 {
495         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
496
497         if (index == 0)
498                 return 0;
499
500         if ((ri->desc.id == TPS80031_ID_LDO2) &&
501                         (ri->flags &  TRACK_MODE_ENABLE))
502                 return (ri->min_mV + (((index - 1) * 125))/10) * 1000;
503
504         return (ri->min_mV + ((index - 1) * 100)) * 1000;
505 }
506
507 static int __tps80031_ldo2_set_voltage_track_mode(struct device *parent,
508                 struct tps80031_regulator *ri, int min_uV, int max_uV)
509 {
510         int vsel = 0;
511         int ret;
512         int nvsel;
513
514         if (min_uV < 600000) {
515                 vsel = 0;
516         } else if ((min_uV >= 600000) && (max_uV <= 1300000)) {
517                 vsel = (min_uV - 600000) / 125;
518                 if (vsel % 100)
519                         vsel += 100;
520                 vsel /= 100;
521                 vsel++;
522         } else {
523                 return -EINVAL;
524         }
525
526         /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */
527         if ((tps80031_get_chip_info(parent) == TPS80031) ||
528                 ((tps80031_get_chip_info(parent) == TPS80032) &&
529                 (tps80031_get_pmu_version(parent) == 0x0))) {
530                 nvsel = vsel & 0x1F;
531                 if ((nvsel == 0x0) || (nvsel >= 0x19 && nvsel <= 0x1F)) {
532                         dev_err(ri->dev, "Invalid value for track mode LDO2 "
533                                 "configuration for TPS8003x PMU\n");
534                         return -EINVAL;
535                 }
536         }
537
538         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
539         if (ret < 0)
540                 dev_err(ri->dev, "Error in writing the Voltage register\n");
541         else
542                 ri->volt_reg_cache = vsel;
543         return ret;
544 }
545
546
547 static int __tps80031_ldo_set_voltage(struct device *parent,
548                                       struct tps80031_regulator *ri,
549                                       int min_uV, int max_uV,
550                                       unsigned *selector)
551 {
552         int vsel;
553         int ret;
554
555         if ((min_uV/1000 < ri->min_mV) || (max_uV/1000 > ri->max_mV))
556                 return -EDOM;
557
558         if ((ri->desc.id == TPS80031_ID_LDO2) &&
559                         (ri->flags &  TRACK_MODE_ENABLE))
560                 return __tps80031_ldo2_set_voltage_track_mode(parent, ri,
561                                 min_uV, max_uV);
562
563         /*
564          * Use the below formula to calculate vsel
565          * mV = 1000mv + 100mv * (vsel - 1)
566          */
567         vsel = (min_uV/1000 - 1000)/100 + 1;
568         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
569         if (ret < 0)
570                 dev_err(ri->dev, "Error in writing the Voltage register\n");
571         else
572                 ri->volt_reg_cache = vsel;
573         return ret;
574 }
575
576 static int tps80031ldo_set_voltage(struct regulator_dev *rdev,
577                 int min_uV, int max_uV, unsigned *selector)
578 {
579         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
580         struct device *parent = to_tps80031_dev(rdev);
581
582         return __tps80031_ldo_set_voltage(parent, ri, min_uV, max_uV,
583                                           selector);
584 }
585
586 static int tps80031ldo_get_voltage(struct regulator_dev *rdev)
587 {
588         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
589         uint8_t vsel;
590
591
592         if ((ri->desc.id == TPS80031_ID_LDO2) &&
593                         (ri->flags &  TRACK_MODE_ENABLE)) {
594                 vsel = ri->volt_reg_cache & 0x3F;
595                 return (ri->min_mV + (((vsel - 1) * 125))/10) * 1000;
596         }
597
598         vsel = ri->volt_reg_cache & LDO_VSEL_MASK;
599         /*
600          * Use the below formula to calculate vsel
601          * mV = 1000mv + 100mv * (vsel - 1)
602          */
603         return (1000 + (100 * (vsel - 1))) * 1000;
604 }
605
606 /* VBUS */
607 static int tps80031_vbus_enable_time(struct regulator_dev *rdev)
608 {
609         /* Enable and settling time for vbus is 3ms */
610         return 3000;
611 }
612 static int tps80031_vbus_is_enabled(struct regulator_dev *rdev)
613 {
614         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
615         struct device *parent = to_tps80031_dev(rdev);
616         uint8_t ctrl1, ctrl3;
617         int ret;
618
619         if (ri->platform_flags & VBUS_SW_ONLY) {
620                 ret = tps80031_read(parent, SLAVE_ID2,
621                                 CHARGERUSB_CTRL1_ADD, &ctrl1);
622                 if (!ret)
623                         ret = tps80031_read(parent, SLAVE_ID2,
624                                         CHARGERUSB_CTRL3_ADD, &ctrl3);
625                 if (ret < 0) {
626                         dev_err(&rdev->dev, "Error in reading control reg\n");
627                         return ret;
628                 }
629                 if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN))
630                         return 1;
631                 return 0;
632         } else {
633                 return -EIO;
634         }
635 }
636
637 static int tps80031_vbus_enable(struct regulator_dev *rdev)
638 {
639         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
640         struct device *parent = to_tps80031_dev(rdev);
641         int ret;
642
643         if (ri->platform_flags & VBUS_SW_ONLY) {
644                 ret = tps80031_set_bits(parent, SLAVE_ID2,
645                                 CHARGERUSB_CTRL1_ADD,  OPA_MODE_EN);
646                 if (!ret)
647                         ret = tps80031_set_bits(parent, SLAVE_ID2,
648                                         CHARGERUSB_CTRL3_ADD, BOOST_HW_PWR_EN);
649                 if (ret < 0) {
650                         dev_err(&rdev->dev, "Error in reading control reg\n");
651                         return ret;
652                 }
653                 udelay(ri->delay);
654                 return ret;
655         }
656         dev_err(&rdev->dev, "%s() is not supported with flag 0x%08x\n",
657                  __func__, ri->platform_flags);
658         return -EIO;
659 }
660
661 static int tps80031_vbus_disable(struct regulator_dev *rdev)
662 {
663         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
664         struct device *parent = to_tps80031_dev(rdev);
665         int ret = 0;
666
667         if (ri->platform_flags & VBUS_SW_ONLY) {
668
669                 if (ri->platform_flags & VBUS_DISCHRG_EN_PDN)
670                         ret = tps80031_write(parent, SLAVE_ID2,
671                                 USB_VBUS_CTRL_SET, VBUS_DISCHRG);
672                 if (!ret)
673                         ret = tps80031_clr_bits(parent, SLAVE_ID2,
674                                 CHARGERUSB_CTRL1_ADD,  OPA_MODE_EN);
675                 if (!ret)
676                         ret = tps80031_clr_bits(parent, SLAVE_ID2,
677                                         CHARGERUSB_CTRL3_ADD, BOOST_HW_PWR_EN);
678                 if (!ret)
679                         mdelay((ri->delay + 999)/1000);
680
681                 if (ri->platform_flags & VBUS_DISCHRG_EN_PDN)
682                         tps80031_write(parent, SLAVE_ID2,
683                                 USB_VBUS_CTRL_CLR, VBUS_DISCHRG);
684
685                 if (ret < 0)
686                         dev_err(&rdev->dev, "Error in reading control reg\n");
687                 return ret;
688         }
689         dev_err(&rdev->dev, "%s() is not supported with flag 0x%08x\n",
690                  __func__, ri->platform_flags);
691         return -EIO;
692 }
693
694 static int tps80031vbus_get_voltage(struct regulator_dev *rdev)
695 {
696         int ret;
697         ret = tps80031_vbus_is_enabled(rdev);
698         if (ret > 0)
699                 return 5000000;
700         return ret;
701 }
702
703 static struct regulator_ops tps80031dcdc_ops = {
704         .list_voltage   = tps80031dcdc_list_voltage,
705         .set_voltage    = tps80031dcdc_set_voltage,
706         .get_voltage    = tps80031dcdc_get_voltage,
707         .enable         = tps80031_reg_enable,
708         .disable        = tps80031_reg_disable,
709         .is_enabled     = tps80031_reg_is_enabled,
710         .enable_time    = tps80031_regulator_enable_time,
711 };
712
713 static struct regulator_ops tps80031ldo_ops = {
714         .list_voltage   = tps80031ldo_list_voltage,
715         .set_voltage    = tps80031ldo_set_voltage,
716         .get_voltage    = tps80031ldo_get_voltage,
717         .enable         = tps80031_reg_enable,
718         .disable        = tps80031_reg_disable,
719         .is_enabled     = tps80031_reg_is_enabled,
720         .enable_time    = tps80031_regulator_enable_time,
721 };
722
723 static struct regulator_ops tps80031vbus_ops = {
724         .get_voltage    = tps80031vbus_get_voltage,
725         .enable         = tps80031_vbus_enable,
726         .disable        = tps80031_vbus_disable,
727         .is_enabled     = tps80031_vbus_is_enabled,
728         .enable_time    = tps80031_vbus_enable_time,
729 };
730
731 #define TPS80031_REG(_id, _trans_reg, _state_reg, _force_reg, _volt_reg, \
732                 _volt_id, min_mVolts, max_mVolts, _ops, _n_volt, _delay, \
733                 _preq_bit)                                               \
734 {                                                               \
735         .trans_reg = _trans_reg,                                \
736         .state_reg = _state_reg,                                \
737         .force_reg = _force_reg,                                \
738         .volt_reg = _volt_reg,                                  \
739         .volt_id = _volt_id,                                    \
740         .id = TPS80031_ID_##_id,                                \
741         .min_mV = min_mVolts,                                   \
742         .max_mV = max_mVolts,                                   \
743         .desc = {                                               \
744                 .name = tps80031_rails(_id),                    \
745                 .id = TPS80031_ID_##_id,                        \
746                 .n_voltages = _n_volt,                          \
747                 .ops = &_ops,                                   \
748                 .type = REGULATOR_VOLTAGE,                      \
749                 .owner = THIS_MODULE,                           \
750         },                                                      \
751         .delay = _delay,                                        \
752         .preq_bit = _preq_bit,                                  \
753 }
754
755 static struct tps80031_regulator tps80031_regulator[] = {
756         TPS80031_REG(VIO,   0x47, 0x48, 0x49, 0x4A, SLAVE_ID0, 600, 2100,
757                                 tps80031dcdc_ops, 63, 500, 4),
758         TPS80031_REG(SMPS1, 0x53, 0x54, 0x55, 0x56, SLAVE_ID0, 600, 2100,
759                                 tps80031dcdc_ops, 63, 500, 0),
760         TPS80031_REG(SMPS2, 0x59, 0x5A, 0x5B, 0x5C, SLAVE_ID0, 600, 2100,
761                                 tps80031dcdc_ops, 63, 500, 1),
762         TPS80031_REG(SMPS3, 0x65, 0x66, 0x00, 0x68, SLAVE_ID1, 600, 2100,
763                                 tps80031dcdc_ops, 63, 500, 2),
764         TPS80031_REG(SMPS4, 0x41, 0x42, 0x00, 0x44, SLAVE_ID1, 600, 2100,
765                                 tps80031dcdc_ops, 63, 500, 3),
766
767         TPS80031_REG(LDO1,   0x9D, 0x9E, 0x00, 0x9F, SLAVE_ID1, 1000, 3300,
768                                 tps80031ldo_ops, 25, 500, 8),
769         TPS80031_REG(LDO2,   0x85, 0x86, 0x00, 0x87, SLAVE_ID1, 1000, 3300,
770                                 tps80031ldo_ops, 25, 500, 9),
771         TPS80031_REG(LDO3,   0x8D, 0x8E, 0x00, 0x8F, SLAVE_ID1, 1000, 3300,
772                                 tps80031ldo_ops, 25, 500, 10),
773         TPS80031_REG(LDO4,   0x89, 0x8A, 0x00, 0x8B, SLAVE_ID1, 1000, 3300,
774                                 tps80031ldo_ops, 25, 500, 11),
775         TPS80031_REG(LDO5,   0x99, 0x9A, 0x00, 0x9B, SLAVE_ID1, 1000, 3300,
776                                 tps80031ldo_ops, 25, 500, 12),
777         TPS80031_REG(LDO6,   0x91, 0x92, 0x00, 0x93, SLAVE_ID1, 1000, 3300,
778                                 tps80031ldo_ops, 25, 500, 13),
779         TPS80031_REG(LDO7,   0xA5, 0xA6, 0x00, 0xA7, SLAVE_ID1, 1000, 3300,
780                                 tps80031ldo_ops, 25, 500, 14),
781         TPS80031_REG(LDOUSB, 0xA1, 0xA2, 0x00, 0xA3, SLAVE_ID1, 1000, 3300,
782                                 tps80031ldo_ops, 25, 500, 5),
783         TPS80031_REG(LDOLN,  0x95, 0x96, 0x00, 0x97, SLAVE_ID1, 1000, 3300,
784                                 tps80031ldo_ops, 25, 500, 15),
785         TPS80031_REG(VANA,   0x81, 0x82, 0x00, 0x83, SLAVE_ID1, 1000, 3300,
786                                 tps80031ldo_ops, 25, 500, -1),
787         TPS80031_REG(VBUS,   0x0,  0x0,  0x00, 0x0,  SLAVE_ID1, 0,    5000,
788                                 tps80031vbus_ops, 2, 200000, -1),
789 };
790
791 static int tps80031_power_req_config(struct device *parent,
792                 struct tps80031_regulator *ri,
793                 struct tps80031_regulator_platform_data *tps80031_pdata)
794 {
795         int ret;
796         if (ri->preq_bit < 0)
797                 return 0;
798
799         ret = tps80031_ext_power_req_config(parent, ri->ext_pwr_ctrl,
800                         ri->preq_bit, ri->state_reg, ri->trans_reg);
801         if (!ret)
802                 ret = tps80031_read(parent, SLAVE_ID1, ri->trans_reg,
803                         &ri->trans_reg_cache);
804
805         if (!ret && ri->state_reg)
806                 ret = tps80031_read(parent, SLAVE_ID1, ri->state_reg,
807                         &ri->state_reg_cache);
808         return ret;
809 }
810
811 static int tps80031_regulator_preinit(struct device *parent,
812                 struct tps80031_regulator *ri,
813                 struct tps80031_regulator_platform_data *tps80031_pdata)
814 {
815         int ret = 0;
816         uint8_t reg_val;
817
818         if (ri->desc.id == TPS80031_ID_LDOUSB) {
819                 if (ri->platform_flags & USBLDO_INPUT_VSYS)
820                         ret = tps80031_update(parent, SLAVE_ID1,
821                                 TPS80031_MISC2_ADD,
822                                 MISC2_LDOUSB_IN_VSYS, MISC2_LDOUSB_IN_MASK);
823                 if (ri->platform_flags & USBLDO_INPUT_PMID)
824                         ret = tps80031_update(parent, SLAVE_ID1,
825                                 TPS80031_MISC2_ADD,
826                                 MISC2_LDOUSB_IN_PMID, MISC2_LDOUSB_IN_MASK);
827                 if (ret < 0) {
828                         dev_err(ri->dev, "Not able to configure the rail "
829                                 "LDOUSB as per platform data error %d\n", ret);
830                         return ret;
831                 }
832         }
833
834         if (ri->desc.id == TPS80031_ID_LDO3) {
835                 if (ri->platform_flags & LDO3_OUTPUT_VIB)
836                         ret = tps80031_update(parent, SLAVE_ID1,
837                                 TPS80031_MISC2_ADD,
838                                 MISC2_LDO3_SEL_VIB_VAL,
839                                 MISC2_LDO3_SEL_VIB_MASK);
840                 if (ret < 0) {
841                         dev_err(ri->dev, "Not able to configure the rail "
842                                 "LDO3 as per platform data error %d\n", ret);
843                         return ret;
844                 }
845         }
846
847         if (!tps80031_pdata->init_apply)
848                 return 0;
849
850         if (tps80031_pdata->init_uV >= 0) {
851                 switch (ri->desc.id) {
852                 case TPS80031_ID_VIO:
853                 case TPS80031_ID_SMPS1:
854                 case TPS80031_ID_SMPS2:
855                 case TPS80031_ID_SMPS3:
856                 case TPS80031_ID_SMPS4:
857                         ret = __tps80031_dcdc_set_voltage(parent, ri,
858                                         tps80031_pdata->init_uV,
859                                         tps80031_pdata->init_uV, 0);
860                         break;
861
862                 case TPS80031_ID_LDO1:
863                 case TPS80031_ID_LDO2:
864                 case TPS80031_ID_LDO3:
865                 case TPS80031_ID_LDO4:
866                 case TPS80031_ID_LDO5:
867                 case TPS80031_ID_LDO6:
868                 case TPS80031_ID_LDO7:
869                 case TPS80031_ID_LDOUSB:
870                 case TPS80031_ID_LDOLN:
871                 case TPS80031_ID_VANA:
872                         ret = __tps80031_ldo_set_voltage(parent, ri,
873                                         tps80031_pdata->init_uV,
874                                         tps80031_pdata->init_uV, 0);
875                         break;
876                 default:
877                         ret = -EINVAL;
878                         break;
879                 }
880
881                 if (ret < 0) {
882                         dev_err(ri->dev, "Not able to initialize voltage %d "
883                                 "for rail %d err %d\n", tps80031_pdata->init_uV,
884                                 ri->desc.id, ret);
885                         return ret;
886                 }
887         }
888
889         if (tps80031_pdata->init_enable)
890                 reg_val = (ri->state_reg_cache & ~STATE_MASK) |
891                                 (STATE_ON & STATE_MASK);
892         else
893                 reg_val = (ri->state_reg_cache & ~STATE_MASK) |
894                                 (STATE_OFF & STATE_MASK);
895
896         ret = tps80031_write(parent, SLAVE_ID1, ri->state_reg, reg_val);
897         if (ret < 0)
898                 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
899                         (tps80031_pdata->init_enable) ? "enable" : "disable",
900                         ri->desc.id, ret);
901         else
902                 ri->state_reg_cache = reg_val;
903         return ret;
904 }
905
906 static inline struct tps80031_regulator *find_regulator_info(int id)
907 {
908         struct tps80031_regulator *ri;
909         int i;
910
911         for (i = 0; i < ARRAY_SIZE(tps80031_regulator); i++) {
912                 ri = &tps80031_regulator[i];
913                 if (ri->desc.id == id)
914                         return ri;
915         }
916         return NULL;
917 }
918 static void check_smps_mode_mult(struct device *parent,
919         struct tps80031_regulator *ri)
920 {
921         int mult_offset;
922         switch (ri->desc.id) {
923         case TPS80031_ID_VIO:
924                 mult_offset = SMPS_MULTOFFSET_VIO;
925                 break;
926         case TPS80031_ID_SMPS1:
927                 mult_offset = SMPS_MULTOFFSET_SMPS1;
928                 break;
929         case TPS80031_ID_SMPS2:
930                 mult_offset = SMPS_MULTOFFSET_SMPS2;
931                 break;
932         case TPS80031_ID_SMPS3:
933                 mult_offset = SMPS_MULTOFFSET_SMPS3;
934                 break;
935         case TPS80031_ID_SMPS4:
936                 mult_offset = SMPS_MULTOFFSET_SMPS4;
937                 break;
938         case TPS80031_ID_LDO2:
939                 ri->flags = (tps80031_get_smps_mult(parent) & (1 << 5)) ?
940                                                 TRACK_MODE_ENABLE : 0;
941                 /* TRACK mode the ldo2 varies from 600mV to 1300mV */
942                 if (ri->flags & TRACK_MODE_ENABLE) {
943                         ri->min_mV = 600;
944                         ri->max_mV = 1300;
945                         ri->desc.n_voltages = 57;
946                 }
947                 return;
948         default:
949                 return;
950         }
951
952         ri->flags = (tps80031_get_smps_offset(parent) & mult_offset) ?
953                                                 DCDC_OFFSET_EN : 0;
954         ri->flags |= (tps80031_get_smps_mult(parent) & mult_offset) ?
955                                                 DCDC_EXTENDED_EN : 0;
956         return;
957 }
958
959 static inline int tps80031_cache_regulator_register(struct device *parent,
960         struct tps80031_regulator *ri)
961 {
962         int ret;
963
964         ret = tps80031_read(parent, SLAVE_ID1, ri->trans_reg,
965                         &ri->trans_reg_cache);
966         if (!ret && ri->state_reg)
967                 ret = tps80031_read(parent, SLAVE_ID1, ri->state_reg,
968                         &ri->state_reg_cache);
969         if (!ret && ri->force_reg)
970                 ret = tps80031_read(parent, ri->volt_id, ri->force_reg,
971                         &ri->force_reg_cache);
972         if (!ret && ri->volt_reg)
973                 ret = tps80031_read(parent, ri->volt_id, ri->volt_reg,
974                         &ri->volt_reg_cache);
975         return ret;
976 }
977
978 static int __devinit tps80031_regulator_probe(struct platform_device *pdev)
979 {
980         struct tps80031_regulator *ri = NULL;
981         struct regulator_dev *rdev;
982         struct tps80031_regulator_platform_data *tps_pdata;
983         int id = pdev->id;
984         int err;
985
986         dev_dbg(&pdev->dev, "Probing reulator %d\n", id);
987
988         ri = find_regulator_info(id);
989         if (ri == NULL) {
990                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
991                 return -EINVAL;
992         }
993         tps_pdata = pdev->dev.platform_data;
994         ri->dev = &pdev->dev;
995         if (tps_pdata->delay_us > 0)
996                 ri->delay = tps_pdata->delay_us;
997
998         check_smps_mode_mult(pdev->dev.parent, ri);
999         ri->platform_flags = tps_pdata->flags;
1000         ri->ext_pwr_ctrl = tps_pdata->ext_pwr_ctrl;
1001
1002         err = tps80031_cache_regulator_register(pdev->dev.parent, ri);
1003         if (err) {
1004                 dev_err(&pdev->dev, "Register access for caching is failed\n");
1005                 return err;
1006         }
1007         err = tps80031_regulator_preinit(pdev->dev.parent, ri, tps_pdata);
1008         if (err)
1009                 return err;
1010
1011         err = tps80031_power_req_config(pdev->dev.parent, ri, tps_pdata);
1012         if (err)
1013                 return err;
1014
1015         rdev = regulator_register(&ri->desc, &pdev->dev,
1016                                 &tps_pdata->regulator, ri);
1017         if (IS_ERR_OR_NULL(rdev)) {
1018                 dev_err(&pdev->dev, "failed to register regulator %s\n",
1019                                 ri->desc.name);
1020                 return PTR_ERR(rdev);
1021         }
1022
1023         platform_set_drvdata(pdev, rdev);
1024
1025         return 0;
1026 }
1027
1028 static int __devexit tps80031_regulator_remove(struct platform_device *pdev)
1029 {
1030         struct regulator_dev *rdev = platform_get_drvdata(pdev);
1031
1032         regulator_unregister(rdev);
1033         return 0;
1034 }
1035
1036 static struct platform_driver tps80031_regulator_driver = {
1037         .driver = {
1038                 .name   = "tps80031-regulator",
1039                 .owner  = THIS_MODULE,
1040         },
1041         .probe          = tps80031_regulator_probe,
1042         .remove         = __devexit_p(tps80031_regulator_remove),
1043 };
1044
1045 static int __init tps80031_regulator_init(void)
1046 {
1047         return platform_driver_register(&tps80031_regulator_driver);
1048 }
1049 subsys_initcall(tps80031_regulator_init);
1050
1051 static void __exit tps80031_regulator_exit(void)
1052 {
1053         platform_driver_unregister(&tps80031_regulator_driver);
1054 }
1055 module_exit(tps80031_regulator_exit);
1056
1057 MODULE_LICENSE("GPL");
1058 MODULE_DESCRIPTION("Regulator Driver for TI TPS80031 PMIC");
1059 MODULE_ALIAS("platform:tps80031-regulator");