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