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