regulator: tps80031: Set selector in __tps80031_ldo_set_voltage
[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         unsigned int            ext_ctrl_flag;
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_ctrl_flag & 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_ctrl_flag & 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_ctrl_flag & 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         if (selector)
569                 *selector = vsel;
570         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
571         if (ret < 0)
572                 dev_err(ri->dev, "Error in writing the Voltage register\n");
573         else
574                 ri->volt_reg_cache = vsel;
575         return ret;
576 }
577
578 static int tps80031ldo_set_voltage(struct regulator_dev *rdev,
579                 int min_uV, int max_uV, unsigned *selector)
580 {
581         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
582         struct device *parent = to_tps80031_dev(rdev);
583
584         return __tps80031_ldo_set_voltage(parent, ri, min_uV, max_uV,
585                                           selector);
586 }
587
588 static int tps80031ldo_get_voltage(struct regulator_dev *rdev)
589 {
590         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
591         uint8_t vsel;
592
593
594         if ((ri->desc.id == TPS80031_ID_LDO2) &&
595                         (ri->flags &  TRACK_MODE_ENABLE)) {
596                 vsel = ri->volt_reg_cache & 0x3F;
597                 return (ri->min_mV + (((vsel - 1) * 125))/10) * 1000;
598         }
599
600         vsel = ri->volt_reg_cache & LDO_VSEL_MASK;
601         /*
602          * Use the below formula to calculate vsel
603          * mV = 1000mv + 100mv * (vsel - 1)
604          */
605         return (1000 + (100 * (vsel - 1))) * 1000;
606 }
607
608 /* VBUS */
609 static int tps80031_vbus_enable_time(struct regulator_dev *rdev)
610 {
611         /* Enable and settling time for vbus is 3ms */
612         return 3000;
613 }
614 static int tps80031_vbus_is_enabled(struct regulator_dev *rdev)
615 {
616         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
617         struct device *parent = to_tps80031_dev(rdev);
618         uint8_t ctrl1, ctrl3;
619         int ret;
620
621         if (ri->platform_flags & VBUS_SW_ONLY) {
622                 ret = tps80031_read(parent, SLAVE_ID2,
623                                 CHARGERUSB_CTRL1_ADD, &ctrl1);
624                 if (!ret)
625                         ret = tps80031_read(parent, SLAVE_ID2,
626                                         CHARGERUSB_CTRL3_ADD, &ctrl3);
627                 if (ret < 0) {
628                         dev_err(&rdev->dev, "Error in reading control reg\n");
629                         return ret;
630                 }
631                 if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN))
632                         return 1;
633                 return 0;
634         } else {
635                 return -EIO;
636         }
637 }
638
639 static int tps80031_vbus_enable(struct regulator_dev *rdev)
640 {
641         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
642         struct device *parent = to_tps80031_dev(rdev);
643         int ret;
644
645         if (ri->platform_flags & VBUS_SW_ONLY) {
646                 ret = tps80031_set_bits(parent, SLAVE_ID2,
647                                 CHARGERUSB_CTRL1_ADD,  OPA_MODE_EN);
648                 if (!ret)
649                         ret = tps80031_set_bits(parent, SLAVE_ID2,
650                                         CHARGERUSB_CTRL3_ADD, BOOST_HW_PWR_EN);
651                 if (ret < 0) {
652                         dev_err(&rdev->dev, "Error in reading control reg\n");
653                         return ret;
654                 }
655                 udelay(ri->delay);
656                 return ret;
657         }
658         dev_err(&rdev->dev, "%s() is not supported with flag 0x%08x\n",
659                  __func__, ri->platform_flags);
660         return -EIO;
661 }
662
663 static int tps80031_vbus_disable(struct regulator_dev *rdev)
664 {
665         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
666         struct device *parent = to_tps80031_dev(rdev);
667         int ret = 0;
668
669         if (ri->platform_flags & VBUS_SW_ONLY) {
670
671                 if (ri->platform_flags & VBUS_DISCHRG_EN_PDN)
672                         ret = tps80031_write(parent, SLAVE_ID2,
673                                 USB_VBUS_CTRL_SET, VBUS_DISCHRG);
674                 if (!ret)
675                         ret = tps80031_clr_bits(parent, SLAVE_ID2,
676                                 CHARGERUSB_CTRL1_ADD,  OPA_MODE_EN);
677                 if (!ret)
678                         ret = tps80031_clr_bits(parent, SLAVE_ID2,
679                                         CHARGERUSB_CTRL3_ADD, BOOST_HW_PWR_EN);
680                 if (!ret)
681                         mdelay((ri->delay + 999)/1000);
682
683                 if (ri->platform_flags & VBUS_DISCHRG_EN_PDN)
684                         tps80031_write(parent, SLAVE_ID2,
685                                 USB_VBUS_CTRL_CLR, VBUS_DISCHRG);
686
687                 if (ret < 0)
688                         dev_err(&rdev->dev, "Error in reading control reg\n");
689                 return ret;
690         }
691         dev_err(&rdev->dev, "%s() is not supported with flag 0x%08x\n",
692                  __func__, ri->platform_flags);
693         return -EIO;
694 }
695
696 static int tps80031vbus_get_voltage(struct regulator_dev *rdev)
697 {
698         int ret;
699         ret = tps80031_vbus_is_enabled(rdev);
700         if (ret > 0)
701                 return 5000000;
702         return ret;
703 }
704
705 static struct regulator_ops tps80031dcdc_ops = {
706         .list_voltage   = tps80031dcdc_list_voltage,
707         .set_voltage    = tps80031dcdc_set_voltage,
708         .get_voltage    = tps80031dcdc_get_voltage,
709         .enable         = tps80031_reg_enable,
710         .disable        = tps80031_reg_disable,
711         .is_enabled     = tps80031_reg_is_enabled,
712         .enable_time    = tps80031_regulator_enable_time,
713 };
714
715 static struct regulator_ops tps80031ldo_ops = {
716         .list_voltage   = tps80031ldo_list_voltage,
717         .set_voltage    = tps80031ldo_set_voltage,
718         .get_voltage    = tps80031ldo_get_voltage,
719         .enable         = tps80031_reg_enable,
720         .disable        = tps80031_reg_disable,
721         .is_enabled     = tps80031_reg_is_enabled,
722         .enable_time    = tps80031_regulator_enable_time,
723 };
724
725 static struct regulator_ops tps80031vbus_ops = {
726         .get_voltage    = tps80031vbus_get_voltage,
727         .enable         = tps80031_vbus_enable,
728         .disable        = tps80031_vbus_disable,
729         .is_enabled     = tps80031_vbus_is_enabled,
730         .enable_time    = tps80031_vbus_enable_time,
731 };
732
733 #define TPS80031_REG(_id, _trans_reg, _state_reg, _force_reg, _volt_reg, \
734                 _volt_id, min_mVolts, max_mVolts, _ops, _n_volt, _delay, \
735                 _preq_bit)                                               \
736 {                                                               \
737         .trans_reg = _trans_reg,                                \
738         .state_reg = _state_reg,                                \
739         .force_reg = _force_reg,                                \
740         .volt_reg = _volt_reg,                                  \
741         .volt_id = _volt_id,                                    \
742         .id = TPS80031_ID_##_id,                                \
743         .min_mV = min_mVolts,                                   \
744         .max_mV = max_mVolts,                                   \
745         .desc = {                                               \
746                 .name = tps80031_rails(_id),                    \
747                 .id = TPS80031_ID_##_id,                        \
748                 .n_voltages = _n_volt,                          \
749                 .ops = &_ops,                                   \
750                 .type = REGULATOR_VOLTAGE,                      \
751                 .owner = THIS_MODULE,                           \
752         },                                                      \
753         .delay = _delay,                                        \
754         .preq_bit = _preq_bit,                                  \
755 }
756
757 static struct tps80031_regulator tps80031_regulator[] = {
758         TPS80031_REG(VIO,   0x47, 0x48, 0x49, 0x4A, SLAVE_ID0, 600, 2100,
759                                 tps80031dcdc_ops, 63, 500, 4),
760         TPS80031_REG(SMPS1, 0x53, 0x54, 0x55, 0x56, SLAVE_ID0, 600, 2100,
761                                 tps80031dcdc_ops, 63, 500, 0),
762         TPS80031_REG(SMPS2, 0x59, 0x5A, 0x5B, 0x5C, SLAVE_ID0, 600, 2100,
763                                 tps80031dcdc_ops, 63, 500, 1),
764         TPS80031_REG(SMPS3, 0x65, 0x66, 0x00, 0x68, SLAVE_ID1, 600, 2100,
765                                 tps80031dcdc_ops, 63, 500, 2),
766         TPS80031_REG(SMPS4, 0x41, 0x42, 0x00, 0x44, SLAVE_ID1, 600, 2100,
767                                 tps80031dcdc_ops, 63, 500, 3),
768
769         TPS80031_REG(LDO1,   0x9D, 0x9E, 0x00, 0x9F, SLAVE_ID1, 1000, 3300,
770                                 tps80031ldo_ops, 25, 500, 8),
771         TPS80031_REG(LDO2,   0x85, 0x86, 0x00, 0x87, SLAVE_ID1, 1000, 3300,
772                                 tps80031ldo_ops, 25, 500, 9),
773         TPS80031_REG(LDO3,   0x8D, 0x8E, 0x00, 0x8F, SLAVE_ID1, 1000, 3300,
774                                 tps80031ldo_ops, 25, 500, 10),
775         TPS80031_REG(LDO4,   0x89, 0x8A, 0x00, 0x8B, SLAVE_ID1, 1000, 3300,
776                                 tps80031ldo_ops, 25, 500, 11),
777         TPS80031_REG(LDO5,   0x99, 0x9A, 0x00, 0x9B, SLAVE_ID1, 1000, 3300,
778                                 tps80031ldo_ops, 25, 500, 12),
779         TPS80031_REG(LDO6,   0x91, 0x92, 0x00, 0x93, SLAVE_ID1, 1000, 3300,
780                                 tps80031ldo_ops, 25, 500, 13),
781         TPS80031_REG(LDO7,   0xA5, 0xA6, 0x00, 0xA7, SLAVE_ID1, 1000, 3300,
782                                 tps80031ldo_ops, 25, 500, 14),
783         TPS80031_REG(LDOUSB, 0xA1, 0xA2, 0x00, 0xA3, SLAVE_ID1, 1000, 3300,
784                                 tps80031ldo_ops, 25, 500, 5),
785         TPS80031_REG(LDOLN,  0x95, 0x96, 0x00, 0x97, SLAVE_ID1, 1000, 3300,
786                                 tps80031ldo_ops, 25, 500, 15),
787         TPS80031_REG(VANA,   0x81, 0x82, 0x00, 0x83, SLAVE_ID1, 1000, 3300,
788                                 tps80031ldo_ops, 25, 500, -1),
789         TPS80031_REG(VBUS,   0x0,  0x0,  0x00, 0x0,  SLAVE_ID1, 0,    5000,
790                                 tps80031vbus_ops, 2, 200000, -1),
791 };
792
793 static int tps80031_power_req_config(struct device *parent,
794                 struct tps80031_regulator *ri,
795                 struct tps80031_regulator_platform_data *tps80031_pdata)
796 {
797         int ret;
798         uint8_t reg_val;
799
800         if (ri->preq_bit < 0)
801                 return 0;
802
803         ret = tps80031_ext_power_req_config(parent, ri->ext_ctrl_flag,
804                         ri->preq_bit, ri->state_reg, ri->trans_reg);
805         if (!ret)
806                 ret = tps80031_read(parent, SLAVE_ID1, ri->trans_reg,
807                         &ri->trans_reg_cache);
808
809         if (!ret && ri->state_reg)
810                 ret = tps80031_read(parent, SLAVE_ID1, ri->state_reg,
811                         &ri->state_reg_cache);
812         if (ret < 0) {
813                 dev_err(ri->dev, "%s() fails\n", __func__);
814                 return ret;
815         }
816
817         if (tps80031_pdata->ext_ctrl_flag &
818                         (PWR_OFF_ON_SLEEP | PWR_ON_ON_SLEEP)) {
819                 reg_val = (ri->trans_reg_cache & ~0xC);
820                 if (tps80031_pdata->ext_ctrl_flag & PWR_ON_ON_SLEEP)
821                         reg_val |= 0x4;
822
823                 ret = tps80031_write(parent, SLAVE_ID1, ri->trans_reg,
824                         reg_val);
825                 if (ret < 0)
826                         dev_err(ri->dev, "Not able to write reg 0x%02x\n",
827                                 ri->trans_reg);
828                 else
829                         ri->trans_reg_cache = reg_val;
830         }
831         return ret;
832 }
833
834 static int tps80031_regulator_preinit(struct device *parent,
835                 struct tps80031_regulator *ri,
836                 struct tps80031_regulator_platform_data *tps80031_pdata)
837 {
838         int ret = 0;
839         uint8_t reg_val;
840
841         if (ri->desc.id == TPS80031_ID_LDOUSB) {
842                 if (ri->platform_flags & USBLDO_INPUT_VSYS)
843                         ret = tps80031_update(parent, SLAVE_ID1,
844                                 TPS80031_MISC2_ADD,
845                                 MISC2_LDOUSB_IN_VSYS, MISC2_LDOUSB_IN_MASK);
846                 if (ri->platform_flags & USBLDO_INPUT_PMID)
847                         ret = tps80031_update(parent, SLAVE_ID1,
848                                 TPS80031_MISC2_ADD,
849                                 MISC2_LDOUSB_IN_PMID, MISC2_LDOUSB_IN_MASK);
850                 if (ret < 0) {
851                         dev_err(ri->dev, "Not able to configure the rail "
852                                 "LDOUSB as per platform data error %d\n", ret);
853                         return ret;
854                 }
855         }
856
857         if (ri->desc.id == TPS80031_ID_LDO3) {
858                 if (ri->platform_flags & LDO3_OUTPUT_VIB)
859                         ret = tps80031_update(parent, SLAVE_ID1,
860                                 TPS80031_MISC2_ADD,
861                                 MISC2_LDO3_SEL_VIB_VAL,
862                                 MISC2_LDO3_SEL_VIB_MASK);
863                 if (ret < 0) {
864                         dev_err(ri->dev, "Not able to configure the rail "
865                                 "LDO3 as per platform data error %d\n", ret);
866                         return ret;
867                 }
868         }
869
870         if (!tps80031_pdata->init_apply)
871                 return 0;
872
873         if (tps80031_pdata->init_uV >= 0) {
874                 switch (ri->desc.id) {
875                 case TPS80031_ID_VIO:
876                 case TPS80031_ID_SMPS1:
877                 case TPS80031_ID_SMPS2:
878                 case TPS80031_ID_SMPS3:
879                 case TPS80031_ID_SMPS4:
880                         ret = __tps80031_dcdc_set_voltage(parent, ri,
881                                         tps80031_pdata->init_uV,
882                                         tps80031_pdata->init_uV, 0);
883                         break;
884
885                 case TPS80031_ID_LDO1:
886                 case TPS80031_ID_LDO2:
887                 case TPS80031_ID_LDO3:
888                 case TPS80031_ID_LDO4:
889                 case TPS80031_ID_LDO5:
890                 case TPS80031_ID_LDO6:
891                 case TPS80031_ID_LDO7:
892                 case TPS80031_ID_LDOUSB:
893                 case TPS80031_ID_LDOLN:
894                 case TPS80031_ID_VANA:
895                         ret = __tps80031_ldo_set_voltage(parent, ri,
896                                         tps80031_pdata->init_uV,
897                                         tps80031_pdata->init_uV, 0);
898                         break;
899                 default:
900                         ret = -EINVAL;
901                         break;
902                 }
903
904                 if (ret < 0) {
905                         dev_err(ri->dev, "Not able to initialize voltage %d "
906                                 "for rail %d err %d\n", tps80031_pdata->init_uV,
907                                 ri->desc.id, ret);
908                         return ret;
909                 }
910         }
911
912         if (tps80031_pdata->init_enable)
913                 reg_val = (ri->state_reg_cache & ~STATE_MASK) |
914                                 (STATE_ON & STATE_MASK);
915         else
916                 reg_val = (ri->state_reg_cache & ~STATE_MASK) |
917                                 (STATE_OFF & STATE_MASK);
918
919         ret = tps80031_write(parent, SLAVE_ID1, ri->state_reg, reg_val);
920         if (ret < 0)
921                 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
922                         (tps80031_pdata->init_enable) ? "enable" : "disable",
923                         ri->desc.id, ret);
924         else
925                 ri->state_reg_cache = reg_val;
926         return ret;
927 }
928
929 static inline struct tps80031_regulator *find_regulator_info(int id)
930 {
931         struct tps80031_regulator *ri;
932         int i;
933
934         for (i = 0; i < ARRAY_SIZE(tps80031_regulator); i++) {
935                 ri = &tps80031_regulator[i];
936                 if (ri->desc.id == id)
937                         return ri;
938         }
939         return NULL;
940 }
941 static void check_smps_mode_mult(struct device *parent,
942         struct tps80031_regulator *ri)
943 {
944         int mult_offset;
945         switch (ri->desc.id) {
946         case TPS80031_ID_VIO:
947                 mult_offset = SMPS_MULTOFFSET_VIO;
948                 break;
949         case TPS80031_ID_SMPS1:
950                 mult_offset = SMPS_MULTOFFSET_SMPS1;
951                 break;
952         case TPS80031_ID_SMPS2:
953                 mult_offset = SMPS_MULTOFFSET_SMPS2;
954                 break;
955         case TPS80031_ID_SMPS3:
956                 mult_offset = SMPS_MULTOFFSET_SMPS3;
957                 break;
958         case TPS80031_ID_SMPS4:
959                 mult_offset = SMPS_MULTOFFSET_SMPS4;
960                 break;
961         case TPS80031_ID_LDO2:
962                 ri->flags = (tps80031_get_smps_mult(parent) & (1 << 5)) ?
963                                                 TRACK_MODE_ENABLE : 0;
964                 /* TRACK mode the ldo2 varies from 600mV to 1300mV */
965                 if (ri->flags & TRACK_MODE_ENABLE) {
966                         ri->min_mV = 600;
967                         ri->max_mV = 1300;
968                         ri->desc.n_voltages = 57;
969                 }
970                 return;
971         default:
972                 return;
973         }
974
975         ri->flags = (tps80031_get_smps_offset(parent) & mult_offset) ?
976                                                 DCDC_OFFSET_EN : 0;
977         ri->flags |= (tps80031_get_smps_mult(parent) & mult_offset) ?
978                                                 DCDC_EXTENDED_EN : 0;
979         return;
980 }
981
982 static inline int tps80031_cache_regulator_register(struct device *parent,
983         struct tps80031_regulator *ri)
984 {
985         int ret;
986
987         ret = tps80031_read(parent, SLAVE_ID1, ri->trans_reg,
988                         &ri->trans_reg_cache);
989         if (!ret && ri->state_reg)
990                 ret = tps80031_read(parent, SLAVE_ID1, ri->state_reg,
991                         &ri->state_reg_cache);
992         if (!ret && ri->force_reg)
993                 ret = tps80031_read(parent, ri->volt_id, ri->force_reg,
994                         &ri->force_reg_cache);
995         if (!ret && ri->volt_reg)
996                 ret = tps80031_read(parent, ri->volt_id, ri->volt_reg,
997                         &ri->volt_reg_cache);
998         return ret;
999 }
1000
1001 static int __devinit tps80031_regulator_probe(struct platform_device *pdev)
1002 {
1003         struct tps80031_regulator *ri = NULL;
1004         struct regulator_dev *rdev;
1005         struct tps80031_regulator_platform_data *tps_pdata;
1006         int id = pdev->id;
1007         int err;
1008
1009         dev_dbg(&pdev->dev, "Probing reulator %d\n", id);
1010
1011         ri = find_regulator_info(id);
1012         if (ri == NULL) {
1013                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
1014                 return -EINVAL;
1015         }
1016         tps_pdata = pdev->dev.platform_data;
1017         ri->dev = &pdev->dev;
1018         if (tps_pdata->delay_us > 0)
1019                 ri->delay = tps_pdata->delay_us;
1020
1021         check_smps_mode_mult(pdev->dev.parent, ri);
1022         ri->platform_flags = tps_pdata->flags;
1023         ri->ext_ctrl_flag = tps_pdata->ext_ctrl_flag;
1024
1025         err = tps80031_cache_regulator_register(pdev->dev.parent, ri);
1026         if (err) {
1027                 dev_err(&pdev->dev, "Register access for caching is failed\n");
1028                 return err;
1029         }
1030         err = tps80031_regulator_preinit(pdev->dev.parent, ri, tps_pdata);
1031         if (err)
1032                 return err;
1033
1034         err = tps80031_power_req_config(pdev->dev.parent, ri, tps_pdata);
1035         if (err)
1036                 return err;
1037
1038         rdev = regulator_register(&ri->desc, &pdev->dev,
1039                                 &tps_pdata->regulator, ri);
1040         if (IS_ERR_OR_NULL(rdev)) {
1041                 dev_err(&pdev->dev, "failed to register regulator %s\n",
1042                                 ri->desc.name);
1043                 return PTR_ERR(rdev);
1044         }
1045
1046         platform_set_drvdata(pdev, rdev);
1047
1048         return 0;
1049 }
1050
1051 static int __devexit tps80031_regulator_remove(struct platform_device *pdev)
1052 {
1053         struct regulator_dev *rdev = platform_get_drvdata(pdev);
1054
1055         regulator_unregister(rdev);
1056         return 0;
1057 }
1058
1059 static struct platform_driver tps80031_regulator_driver = {
1060         .driver = {
1061                 .name   = "tps80031-regulator",
1062                 .owner  = THIS_MODULE,
1063         },
1064         .probe          = tps80031_regulator_probe,
1065         .remove         = __devexit_p(tps80031_regulator_remove),
1066 };
1067
1068 static int __init tps80031_regulator_init(void)
1069 {
1070         return platform_driver_register(&tps80031_regulator_driver);
1071 }
1072 subsys_initcall(tps80031_regulator_init);
1073
1074 static void __exit tps80031_regulator_exit(void)
1075 {
1076         platform_driver_unregister(&tps80031_regulator_driver);
1077 }
1078 module_exit(tps80031_regulator_exit);
1079
1080 MODULE_LICENSE("GPL");
1081 MODULE_DESCRIPTION("Regulator Driver for TI TPS80031 PMIC");
1082 MODULE_ALIAS("platform:tps80031-regulator");