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