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