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