regulator: tps80031: Allow to configure in OFF mode in sleep state
[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 = (607700 + (12660 * (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 >= 607700) && (min_uV <= 1300000)) {
304                         int cal_volt;
305                         vsel = (10 * (min_uV - 607700)) / 1266;
306                         if (vsel % 100)
307                                 vsel += 100;
308                         vsel /= 100;
309                         vsel++;
310                         cal_volt = (607700 + (12660 * (vsel - 1)));
311                         if (cal_volt > max_uV)
312                                 return -EINVAL;
313                 } else if ((min_uV > 1900000) && (max_uV >= 2100000))
314                         vsel = 62;
315                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
316                         vsel = 61;
317                 else if ((min_uV > 1500000) && (max_uV >= 1800000))
318                         vsel = 60;
319                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
320                         vsel = 59;
321                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
322                         vsel = 58;
323                 else
324                         return -EINVAL;
325                 break;
326
327         case DCDC_OFFSET_EN:
328                 if (min_uV == 0)
329                         vsel = 0;
330                 else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
331                         int cal_volt;
332                         vsel = (min_uV - 700000) / 125;
333                         if (vsel % 100)
334                                 vsel += 100;
335                         vsel /= 100;
336                         vsel++;
337                         cal_volt = (700000 + (12500 * (vsel - 1)));
338                         if (cal_volt > max_uV)
339                                 return -EINVAL;
340                 } else if ((min_uV > 1900000) && (max_uV >= 2100000))
341                         vsel = 62;
342                 else if ((min_uV > 1800000) && (max_uV >= 1900000))
343                         vsel = 61;
344                 else if ((min_uV > 1500000) && (max_uV >= 1800000))
345                         vsel = 60;
346                 else if ((min_uV > 1350000) && (max_uV >= 1500000))
347                         vsel = 59;
348                 else if ((min_uV > 1300000) && (max_uV >= 1350000))
349                         vsel = 58;
350                 else
351                         return -EINVAL;
352                 break;
353
354         case DCDC_EXTENDED_EN:
355                 if (min_uV == 0)
356                         vsel = 0;
357                 else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
358                         vsel = (min_uV - 1852000) / 386;
359                         if (vsel % 100)
360                                 vsel += 100;
361                         vsel++;
362                 }
363                 break;
364
365         case DCDC_OFFSET_EN|DCDC_EXTENDED_EN:
366                 if (min_uV == 0)
367                         vsel = 0;
368                 else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
369                         vsel = (min_uV - 2161000) / 386;
370                         if (vsel % 100)
371                                 vsel += 100;
372                         vsel /= 100;
373                         vsel++;
374                 }
375                 break;
376         }
377
378         if (selector)
379                 *selector = vsel;
380
381         if (ri->force_reg) {
382                 if (((ri->force_reg_cache >> 6) & 0x3) == 0) {
383                         ret = tps80031_write(parent, ri->volt_id,
384                                                 ri->force_reg, vsel);
385                         if (ret < 0)
386                                 dev_err(ri->dev, "Error in writing the "
387                                                 "force register\n");
388                         else
389                                 ri->force_reg_cache = vsel;
390                         return ret;
391                 }
392         }
393         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
394         if (ret < 0)
395                 dev_err(ri->dev, "Error in writing the Voltage register\n");
396         else
397                 ri->volt_reg_cache = vsel;
398         return ret;
399 }
400
401 static int tps80031dcdc_set_voltage(struct regulator_dev *rdev,
402                         int min_uV, int max_uV, unsigned *selector)
403 {
404         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
405         struct device *parent = to_tps80031_dev(rdev);
406         return __tps80031_dcdc_set_voltage(parent, ri, min_uV, max_uV,
407                                            selector);
408 }
409
410 static int tps80031dcdc_get_voltage(struct regulator_dev *rdev)
411 {
412         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
413         uint8_t vsel = 0;
414         int voltage = 0;
415
416         if (ri->force_reg) {
417                 vsel  = ri->force_reg_cache;
418                 if ((vsel & SMPS_CMD_MASK) == 0)
419                         goto decode;
420         }
421
422         vsel =  ri->volt_reg_cache;
423
424 decode:
425         vsel &= SMPS_VSEL_MASK;
426
427         switch (ri->flags) {
428         case 0:
429                 if (vsel == 0)
430                         voltage = 0;
431                 else if (vsel < 58)
432                         voltage = (607700 + (12660 * (vsel - 1)));
433                 else if (vsel == 58)
434                         voltage = 1350 * 1000;
435                 else if (vsel == 59)
436                         voltage = 1500 * 1000;
437                 else if (vsel == 60)
438                         voltage = 1800 * 1000;
439                 else if (vsel == 61)
440                         voltage = 1900 * 1000;
441                 else if (vsel == 62)
442                         voltage = 2100 * 1000;
443                 break;
444
445         case DCDC_OFFSET_EN:
446                 if (vsel == 0)
447                         voltage = 0;
448                 else if (vsel < 58)
449                         voltage = (700000 + (12500 * (vsel - 1)));
450                 else if (vsel == 58)
451                         voltage = 1350 * 1000;
452                 else if (vsel == 59)
453                         voltage = 1500 * 1000;
454                 else if (vsel == 60)
455                         voltage = 1800 * 1000;
456                 else if (vsel == 61)
457                         voltage = 1900 * 1000;
458                 else if (vsel == 62)
459                         voltage = 2100 * 1000;
460                 break;
461
462         case DCDC_EXTENDED_EN:
463                 if (vsel == 0)
464                         voltage = 0;
465                 else if (vsel < 58)
466                         voltage = (1852000 + (38600 * (vsel - 1)));
467                 else if (vsel == 58)
468                         voltage = 2084 * 1000;
469                 else if (vsel == 59)
470                         voltage = 2315 * 1000;
471                 else if (vsel == 60)
472                         voltage = 2778 * 1000;
473                 else if (vsel == 61)
474                         voltage = 2932 * 1000;
475                 else if (vsel == 62)
476                         voltage = 3241 * 1000;
477                 break;
478
479         case DCDC_EXTENDED_EN|DCDC_OFFSET_EN:
480                 if (vsel == 0)
481                         voltage = 0;
482                 else if (vsel < 58)
483                         voltage = (2161000 + (38600 * (vsel - 1)));
484                 else if (vsel == 58)
485                         voltage = 4167 * 1000;
486                 else if (vsel == 59)
487                         voltage = 2315 * 1000;
488                 else if (vsel == 60)
489                         voltage = 2778 * 1000;
490                 else if (vsel == 61)
491                         voltage = 2932 * 1000;
492                 else if (vsel == 62)
493                         voltage = 3241 * 1000;
494                 break;
495         }
496
497         return voltage;
498 }
499
500 static int tps80031ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
501 {
502         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
503
504         if (index == 0)
505                 return 0;
506
507         if ((ri->desc.id == TPS80031_ID_LDO2) &&
508                         (ri->flags &  TRACK_MODE_ENABLE))
509                 return (ri->min_mV + (((index - 1) * 125))/10) * 1000;
510
511         return (ri->min_mV + ((index - 1) * 100)) * 1000;
512 }
513
514 static int __tps80031_ldo2_set_voltage_track_mode(struct device *parent,
515                 struct tps80031_regulator *ri, int min_uV, int max_uV)
516 {
517         int vsel = 0;
518         int ret;
519         int nvsel;
520
521         if (min_uV < 600000) {
522                 vsel = 0;
523         } else if ((min_uV >= 600000) && (max_uV <= 1300000)) {
524                 vsel = (min_uV - 600000) / 125;
525                 if (vsel % 100)
526                         vsel += 100;
527                 vsel /= 100;
528                 vsel++;
529         } else {
530                 return -EINVAL;
531         }
532
533         /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */
534         if ((tps80031_get_chip_info(parent) == TPS80031) ||
535                 ((tps80031_get_chip_info(parent) == TPS80032) &&
536                 (tps80031_get_pmu_version(parent) == 0x0))) {
537                 nvsel = vsel & 0x1F;
538                 if ((nvsel == 0x0) || (nvsel >= 0x19 && nvsel <= 0x1F)) {
539                         dev_err(ri->dev, "Invalid value for track mode LDO2 "
540                                 "configuration for TPS8003x PMU\n");
541                         return -EINVAL;
542                 }
543         }
544
545         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
546         if (ret < 0)
547                 dev_err(ri->dev, "Error in writing the Voltage register\n");
548         else
549                 ri->volt_reg_cache = vsel;
550         return ret;
551 }
552
553
554 static int __tps80031_ldo_set_voltage(struct device *parent,
555                                       struct tps80031_regulator *ri,
556                                       int min_uV, int max_uV,
557                                       unsigned *selector)
558 {
559         int vsel;
560         int ret;
561
562         if ((min_uV/1000 < ri->min_mV) || (max_uV/1000 > ri->max_mV))
563                 return -EDOM;
564
565         if ((ri->desc.id == TPS80031_ID_LDO2) &&
566                         (ri->flags &  TRACK_MODE_ENABLE))
567                 return __tps80031_ldo2_set_voltage_track_mode(parent, ri,
568                                 min_uV, max_uV);
569
570         /*
571          * Use the below formula to calculate vsel
572          * mV = 1000mv + 100mv * (vsel - 1)
573          */
574         vsel = (min_uV/1000 - 1000)/100 + 1;
575         if (selector)
576                 *selector = vsel;
577         ret = tps80031_write(parent, ri->volt_id, ri->volt_reg, vsel);
578         if (ret < 0)
579                 dev_err(ri->dev, "Error in writing the Voltage register\n");
580         else
581                 ri->volt_reg_cache = vsel;
582         return ret;
583 }
584
585 static int tps80031ldo_set_voltage(struct regulator_dev *rdev,
586                 int min_uV, int max_uV, unsigned *selector)
587 {
588         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
589         struct device *parent = to_tps80031_dev(rdev);
590
591         return __tps80031_ldo_set_voltage(parent, ri, min_uV, max_uV,
592                                           selector);
593 }
594
595 static int tps80031ldo_get_voltage(struct regulator_dev *rdev)
596 {
597         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
598         uint8_t vsel;
599
600
601         if ((ri->desc.id == TPS80031_ID_LDO2) &&
602                         (ri->flags &  TRACK_MODE_ENABLE)) {
603                 vsel = ri->volt_reg_cache & 0x3F;
604                 return (ri->min_mV + (((vsel - 1) * 125))/10) * 1000;
605         }
606
607         vsel = ri->volt_reg_cache & LDO_VSEL_MASK;
608         /*
609          * Use the below formula to calculate vsel
610          * mV = 1000mv + 100mv * (vsel - 1)
611          */
612         return (1000 + (100 * (vsel - 1))) * 1000;
613 }
614
615 /* VBUS */
616 static int tps80031_vbus_enable_time(struct regulator_dev *rdev)
617 {
618         /* Enable and settling time for vbus is 3ms */
619         return 3000;
620 }
621 static int tps80031_vbus_is_enabled(struct regulator_dev *rdev)
622 {
623         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
624         struct device *parent = to_tps80031_dev(rdev);
625         uint8_t ctrl1, ctrl3;
626         int ret;
627
628         if (ri->platform_flags & VBUS_SW_ONLY) {
629                 ret = tps80031_read(parent, SLAVE_ID2,
630                                 CHARGERUSB_CTRL1_ADD, &ctrl1);
631                 if (!ret)
632                         ret = tps80031_read(parent, SLAVE_ID2,
633                                         CHARGERUSB_CTRL3_ADD, &ctrl3);
634                 if (ret < 0) {
635                         dev_err(&rdev->dev, "Error in reading control reg\n");
636                         return ret;
637                 }
638                 if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN))
639                         return 1;
640                 return 0;
641         } else {
642                 return -EIO;
643         }
644 }
645
646 static int tps80031_vbus_enable(struct regulator_dev *rdev)
647 {
648         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
649         struct device *parent = to_tps80031_dev(rdev);
650         int ret;
651
652         if (ri->platform_flags & VBUS_SW_ONLY) {
653                 ret = tps80031_set_bits(parent, SLAVE_ID2,
654                                 CHARGERUSB_CTRL1_ADD,  OPA_MODE_EN);
655                 if (!ret)
656                         ret = tps80031_set_bits(parent, SLAVE_ID2,
657                                         CHARGERUSB_CTRL3_ADD, BOOST_HW_PWR_EN);
658                 if (ret < 0) {
659                         dev_err(&rdev->dev, "Error in reading control reg\n");
660                         return ret;
661                 }
662                 udelay(ri->delay);
663                 return ret;
664         }
665         dev_err(&rdev->dev, "%s() is not supported with flag 0x%08x\n",
666                  __func__, ri->platform_flags);
667         return -EIO;
668 }
669
670 static int tps80031_vbus_disable(struct regulator_dev *rdev)
671 {
672         struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
673         struct device *parent = to_tps80031_dev(rdev);
674         int ret = 0;
675
676         if (ri->platform_flags & VBUS_SW_ONLY) {
677
678                 if (ri->platform_flags & VBUS_DISCHRG_EN_PDN)
679                         ret = tps80031_write(parent, SLAVE_ID2,
680                                 USB_VBUS_CTRL_SET, VBUS_DISCHRG);
681                 if (!ret)
682                         ret = tps80031_clr_bits(parent, SLAVE_ID2,
683                                 CHARGERUSB_CTRL1_ADD,  OPA_MODE_EN);
684                 if (!ret)
685                         ret = tps80031_clr_bits(parent, SLAVE_ID2,
686                                         CHARGERUSB_CTRL3_ADD, BOOST_HW_PWR_EN);
687                 if (!ret)
688                         mdelay((ri->delay + 999)/1000);
689
690                 if (ri->platform_flags & VBUS_DISCHRG_EN_PDN)
691                         tps80031_write(parent, SLAVE_ID2,
692                                 USB_VBUS_CTRL_CLR, VBUS_DISCHRG);
693
694                 if (ret < 0)
695                         dev_err(&rdev->dev, "Error in reading control reg\n");
696                 return ret;
697         }
698         dev_err(&rdev->dev, "%s() is not supported with flag 0x%08x\n",
699                  __func__, ri->platform_flags);
700         return -EIO;
701 }
702
703 static int tps80031vbus_get_voltage(struct regulator_dev *rdev)
704 {
705         int ret;
706         ret = tps80031_vbus_is_enabled(rdev);
707         if (ret > 0)
708                 return 5000000;
709         return ret;
710 }
711
712 static struct regulator_ops tps80031dcdc_ops = {
713         .list_voltage   = tps80031dcdc_list_voltage,
714         .set_voltage    = tps80031dcdc_set_voltage,
715         .get_voltage    = tps80031dcdc_get_voltage,
716         .enable         = tps80031_reg_enable,
717         .disable        = tps80031_reg_disable,
718         .is_enabled     = tps80031_reg_is_enabled,
719         .enable_time    = tps80031_regulator_enable_time,
720 };
721
722 static struct regulator_ops tps80031ldo_ops = {
723         .list_voltage   = tps80031ldo_list_voltage,
724         .set_voltage    = tps80031ldo_set_voltage,
725         .get_voltage    = tps80031ldo_get_voltage,
726         .enable         = tps80031_reg_enable,
727         .disable        = tps80031_reg_disable,
728         .is_enabled     = tps80031_reg_is_enabled,
729         .enable_time    = tps80031_regulator_enable_time,
730 };
731
732 static struct regulator_ops tps80031vbus_ops = {
733         .get_voltage    = tps80031vbus_get_voltage,
734         .enable         = tps80031_vbus_enable,
735         .disable        = tps80031_vbus_disable,
736         .is_enabled     = tps80031_vbus_is_enabled,
737         .enable_time    = tps80031_vbus_enable_time,
738 };
739
740 #define TPS80031_REG(_id, _trans_reg, _state_reg, _force_reg, _volt_reg, \
741                 _volt_id, min_mVolts, max_mVolts, _ops, _n_volt, _delay, \
742                 _preq_bit)                                               \
743 {                                                               \
744         .trans_reg = _trans_reg,                                \
745         .state_reg = _state_reg,                                \
746         .force_reg = _force_reg,                                \
747         .volt_reg = _volt_reg,                                  \
748         .volt_id = _volt_id,                                    \
749         .id = TPS80031_ID_##_id,                                \
750         .min_mV = min_mVolts,                                   \
751         .max_mV = max_mVolts,                                   \
752         .desc = {                                               \
753                 .name = tps80031_rails(_id),                    \
754                 .id = TPS80031_ID_##_id,                        \
755                 .n_voltages = _n_volt,                          \
756                 .ops = &_ops,                                   \
757                 .type = REGULATOR_VOLTAGE,                      \
758                 .owner = THIS_MODULE,                           \
759         },                                                      \
760         .delay = _delay,                                        \
761         .preq_bit = _preq_bit,                                  \
762 }
763
764 static struct tps80031_regulator tps80031_regulator[] = {
765         TPS80031_REG(VIO,   0x47, 0x48, 0x49, 0x4A, SLAVE_ID0, 600, 2100,
766                                 tps80031dcdc_ops, 63, 500, 4),
767         TPS80031_REG(SMPS1, 0x53, 0x54, 0x55, 0x56, SLAVE_ID0, 600, 2100,
768                                 tps80031dcdc_ops, 63, 500, 0),
769         TPS80031_REG(SMPS2, 0x59, 0x5A, 0x5B, 0x5C, SLAVE_ID0, 600, 2100,
770                                 tps80031dcdc_ops, 63, 500, 1),
771         TPS80031_REG(SMPS3, 0x65, 0x66, 0x00, 0x68, SLAVE_ID1, 600, 2100,
772                                 tps80031dcdc_ops, 63, 500, 2),
773         TPS80031_REG(SMPS4, 0x41, 0x42, 0x00, 0x44, SLAVE_ID1, 600, 2100,
774                                 tps80031dcdc_ops, 63, 500, 3),
775
776         TPS80031_REG(LDO1,   0x9D, 0x9E, 0x00, 0x9F, SLAVE_ID1, 1000, 3300,
777                                 tps80031ldo_ops, 25, 500, 8),
778         TPS80031_REG(LDO2,   0x85, 0x86, 0x00, 0x87, SLAVE_ID1, 1000, 3300,
779                                 tps80031ldo_ops, 25, 500, 9),
780         TPS80031_REG(LDO3,   0x8D, 0x8E, 0x00, 0x8F, SLAVE_ID1, 1000, 3300,
781                                 tps80031ldo_ops, 25, 500, 10),
782         TPS80031_REG(LDO4,   0x89, 0x8A, 0x00, 0x8B, SLAVE_ID1, 1000, 3300,
783                                 tps80031ldo_ops, 25, 500, 11),
784         TPS80031_REG(LDO5,   0x99, 0x9A, 0x00, 0x9B, SLAVE_ID1, 1000, 3300,
785                                 tps80031ldo_ops, 25, 500, 12),
786         TPS80031_REG(LDO6,   0x91, 0x92, 0x00, 0x93, SLAVE_ID1, 1000, 3300,
787                                 tps80031ldo_ops, 25, 500, 13),
788         TPS80031_REG(LDO7,   0xA5, 0xA6, 0x00, 0xA7, SLAVE_ID1, 1000, 3300,
789                                 tps80031ldo_ops, 25, 500, 14),
790         TPS80031_REG(LDOUSB, 0xA1, 0xA2, 0x00, 0xA3, SLAVE_ID1, 1000, 3300,
791                                 tps80031ldo_ops, 25, 500, 5),
792         TPS80031_REG(LDOLN,  0x95, 0x96, 0x00, 0x97, SLAVE_ID1, 1000, 3300,
793                                 tps80031ldo_ops, 25, 500, 15),
794         TPS80031_REG(VANA,   0x81, 0x82, 0x00, 0x83, SLAVE_ID1, 1000, 3300,
795                                 tps80031ldo_ops, 25, 500, -1),
796         TPS80031_REG(VBUS,   0x0,  0x0,  0x00, 0x0,  SLAVE_ID1, 0,    5000,
797                                 tps80031vbus_ops, 2, 200000, -1),
798 };
799
800 static int tps80031_power_req_config(struct device *parent,
801                 struct tps80031_regulator *ri,
802                 struct tps80031_regulator_platform_data *tps80031_pdata)
803 {
804         int ret = 0;
805         uint8_t reg_val;
806
807         if (ri->preq_bit < 0)
808                 goto skip_pwr_req_config;
809
810         ret = tps80031_ext_power_req_config(parent, ri->ext_ctrl_flag,
811                         ri->preq_bit, ri->state_reg, ri->trans_reg);
812         if (!ret)
813                 ret = tps80031_read(parent, SLAVE_ID1, ri->trans_reg,
814                         &ri->trans_reg_cache);
815
816         if (!ret && ri->state_reg)
817                 ret = tps80031_read(parent, SLAVE_ID1, ri->state_reg,
818                         &ri->state_reg_cache);
819         if (ret < 0) {
820                 dev_err(ri->dev, "%s() fails\n", __func__);
821                 return ret;
822         }
823
824 skip_pwr_req_config:
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");