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