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