411e9a7ad54e15943bfa10134eb63f55a86469ba
[linux-3.10.git] / drivers / regulator / palmas-regulator.c
1 /*
2  * Driver for Regulator part of Palmas PMIC Chips
3  *
4  * Copyright 2011-2013 Texas Instruments Inc.
5  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
6  *
7  * Author: Graeme Gregory <gg@slimlogic.co.uk>
8  * Author: Ian Lartey <ian@slimlogic.co.uk>
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under  the terms of the GNU General  Public License as published by the
12  *  Free Software Foundation;  either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/err.h>
21 #include <linux/gpio.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/slab.h>
26 #include <linux/regmap.h>
27 #include <linux/mfd/palmas.h>
28 #include <linux/of.h>
29 #include <linux/of_platform.h>
30 #include <linux/regulator/of_regulator.h>
31
32 #define EXT_PWR_REQ (PALMAS_EXT_CONTROL_ENABLE1 |       \
33                      PALMAS_EXT_CONTROL_ENABLE2 |       \
34                      PALMAS_EXT_CONTROL_NSLEEP)
35
36 struct regs_info {
37         char    *name;
38         char    *sname;
39         u8      vsel_addr;
40         u8      ctrl_addr;
41         u8      tstep_addr;
42         u8      fvsel_addr;
43         int     sleep_id;
44 };
45
46 static const struct regs_info palmas_regs_info[] = {
47         {
48                 .name           = "SMPS12",
49                 .sname          = "smps1-in",
50                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
51                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
52                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
53                 .fvsel_addr     = PALMAS_SMPS12_FORCE,
54                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS12,
55         },
56         {
57                 .name           = "SMPS123",
58                 .sname          = "smps1-in",
59                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
60                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
61                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
62                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS12,
63         },
64         {
65                 .name           = "SMPS3",
66                 .sname          = "smps3-in",
67                 .vsel_addr      = PALMAS_SMPS3_VOLTAGE,
68                 .ctrl_addr      = PALMAS_SMPS3_CTRL,
69                 .tstep_addr     = PALMAS_SMPS3_TSTEP,
70                 .fvsel_addr     = PALMAS_SMPS3_FORCE,
71                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS3,
72         },
73         {
74                 .name           = "SMPS45",
75                 .sname          = "smps4-in",
76                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
77                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
78                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
79                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS45,
80         },
81         {
82                 .name           = "SMPS457",
83                 .sname          = "smps4-in",
84                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
85                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
86                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
87                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS45,
88         },
89         {
90                 .name           = "SMPS6",
91                 .sname          = "smps6-in",
92                 .vsel_addr      = PALMAS_SMPS6_VOLTAGE,
93                 .ctrl_addr      = PALMAS_SMPS6_CTRL,
94                 .tstep_addr     = PALMAS_SMPS6_TSTEP,
95                 .fvsel_addr     = PALMAS_SMPS6_FORCE,
96                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS6,
97         },
98         {
99                 .name           = "SMPS7",
100                 .sname          = "smps7-in",
101                 .vsel_addr      = PALMAS_SMPS7_VOLTAGE,
102                 .ctrl_addr      = PALMAS_SMPS7_CTRL,
103                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS7,
104         },
105         {
106                 .name           = "SMPS8",
107                 .sname          = "smps8-in",
108                 .vsel_addr      = PALMAS_SMPS8_VOLTAGE,
109                 .ctrl_addr      = PALMAS_SMPS8_CTRL,
110                 .tstep_addr     = PALMAS_SMPS8_TSTEP,
111                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS8,
112         },
113         {
114                 .name           = "SMPS9",
115                 .sname          = "smps9-in",
116                 .vsel_addr      = PALMAS_SMPS9_VOLTAGE,
117                 .ctrl_addr      = PALMAS_SMPS9_CTRL,
118                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS9,
119         },
120         {
121                 .name           = "SMPS10_OUT2",
122                 .sname          = "smps10-in",
123                 .ctrl_addr      = PALMAS_SMPS10_CTRL,
124                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS10,
125         },
126         {
127                 .name           = "SMPS10_OUT1",
128                 .sname          = "smps10-in",
129                 .ctrl_addr      = PALMAS_SMPS10_CTRL,
130                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS10,
131         },
132         {
133                 .name           = "LDO1",
134                 .sname          = "ldo1-in",
135                 .vsel_addr      = PALMAS_LDO1_VOLTAGE,
136                 .ctrl_addr      = PALMAS_LDO1_CTRL,
137                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO1,
138         },
139         {
140                 .name           = "LDO2",
141                 .sname          = "ldo2-in",
142                 .vsel_addr      = PALMAS_LDO2_VOLTAGE,
143                 .ctrl_addr      = PALMAS_LDO2_CTRL,
144                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO2,
145         },
146         {
147                 .name           = "LDO3",
148                 .sname          = "ldo3-in",
149                 .vsel_addr      = PALMAS_LDO3_VOLTAGE,
150                 .ctrl_addr      = PALMAS_LDO3_CTRL,
151                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO3,
152         },
153         {
154                 .name           = "LDO4",
155                 .sname          = "ldo4-in",
156                 .vsel_addr      = PALMAS_LDO4_VOLTAGE,
157                 .ctrl_addr      = PALMAS_LDO4_CTRL,
158                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO4,
159         },
160         {
161                 .name           = "LDO5",
162                 .sname          = "ldo5-in",
163                 .vsel_addr      = PALMAS_LDO5_VOLTAGE,
164                 .ctrl_addr      = PALMAS_LDO5_CTRL,
165                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO5,
166         },
167         {
168                 .name           = "LDO6",
169                 .sname          = "ldo6-in",
170                 .vsel_addr      = PALMAS_LDO6_VOLTAGE,
171                 .ctrl_addr      = PALMAS_LDO6_CTRL,
172                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO6,
173         },
174         {
175                 .name           = "LDO7",
176                 .sname          = "ldo7-in",
177                 .vsel_addr      = PALMAS_LDO7_VOLTAGE,
178                 .ctrl_addr      = PALMAS_LDO7_CTRL,
179                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO7,
180         },
181         {
182                 .name           = "LDO8",
183                 .sname          = "ldo8-in",
184                 .vsel_addr      = PALMAS_LDO8_VOLTAGE,
185                 .ctrl_addr      = PALMAS_LDO8_CTRL,
186                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO8,
187         },
188         {
189                 .name           = "LDO9",
190                 .sname          = "ldo9-in",
191                 .vsel_addr      = PALMAS_LDO9_VOLTAGE,
192                 .ctrl_addr      = PALMAS_LDO9_CTRL,
193                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO9,
194         },
195         {
196                 .name           = "LDO10",
197                 .vsel_addr      = PALMAS_LDO10_VOLTAGE,
198                 .ctrl_addr      = PALMAS_LDO10_CTRL,
199                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO10,
200         },
201         {
202                 .name           = "LDO11",
203                 .vsel_addr      = PALMAS_LDO11_VOLTAGE,
204                 .ctrl_addr      = PALMAS_LDO11_CTRL,
205                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO11,
206         },
207         {
208                 .name           = "LDO12",
209                 .vsel_addr      = PALMAS_LDO12_VOLTAGE,
210                 .ctrl_addr      = PALMAS_LDO12_CTRL,
211                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO12,
212         },
213         {
214                 .name           = "LDO13",
215                 .vsel_addr      = PALMAS_LDO13_VOLTAGE,
216                 .ctrl_addr      = PALMAS_LDO13_CTRL,
217                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO13,
218         },
219         {
220                 .name           = "LDO14",
221                 .vsel_addr      = PALMAS_LDO14_VOLTAGE,
222                 .ctrl_addr      = PALMAS_LDO14_CTRL,
223                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO14,
224         },
225         {
226                 .name           = "LDOLN",
227                 .sname          = "ldoln-in",
228                 .vsel_addr      = PALMAS_LDOLN_VOLTAGE,
229                 .ctrl_addr      = PALMAS_LDOLN_CTRL,
230                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDOLN,
231         },
232         {
233                 .name           = "LDOUSB",
234                 .sname          = "ldousb-in",
235                 .vsel_addr      = PALMAS_LDOUSB_VOLTAGE,
236                 .ctrl_addr      = PALMAS_LDOUSB_CTRL,
237                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDOUSB,
238         },
239         {
240                 .name           = "REGEN1",
241                 .ctrl_addr      = PALMAS_REGEN1_CTRL,
242                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN1,
243         },
244         {
245                 .name           = "REGEN2",
246                 .ctrl_addr      = PALMAS_REGEN2_CTRL,
247                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN2,
248         },
249         {
250                 .name           = "REGEN3",
251                 .ctrl_addr      = PALMAS_REGEN3_CTRL,
252                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN3,
253         },
254         {
255                 .name           = "REGEN4",
256                 .ctrl_addr      = PALMAS_REGEN4_CTRL,
257                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN4,
258         },
259         {
260                 .name           = "REGEN5",
261                 .ctrl_addr      = PALMAS_REGEN5_CTRL,
262                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN5,
263         },
264         {
265                 .name           = "REGEN7",
266                 .ctrl_addr      = PALMAS_REGEN7_CTRL,
267                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN7,
268         },
269         {
270                 .name           = "SYSEN1",
271                 .ctrl_addr      = PALMAS_SYSEN1_CTRL,
272                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SYSEN1,
273         },
274         {
275                 .name           = "SYSEN2",
276                 .ctrl_addr      = PALMAS_SYSEN2_CTRL,
277                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SYSEN2,
278         },
279         {
280                 .name           = "CHARGERPUMP",
281                 .ctrl_addr      = PALMAS_CHARGE_PUMP_CTRL,
282         },
283 };
284
285 static unsigned int palmas_smps_ramp_delay[4] = {0, 10000, 5000, 2500};
286
287 #define SMPS_CTRL_MODE_OFF              0x00
288 #define SMPS_CTRL_MODE_ON               0x01
289 #define SMPS_CTRL_MODE_ECO              0x02
290 #define SMPS_CTRL_MODE_PWM              0x03
291
292 #define SMPS_CTRL_SLEEP_MODE_OFF        0x00
293 #define SMPS_CTRL_SLEEP_MODE_ON         0x04
294 #define SMPS_CTRL_SLEEP_MODE_ECO        0x08
295 #define SMPS_CTRL_SLEEP_MODE_PWM        0x0C
296
297 /* These values are derived from the data sheet. And are the number of steps
298  * where there is a voltage change, the ranges at beginning and end of register
299  * max/min values where there are no change are ommitted.
300  *
301  * So they are basically (maxV-minV)/stepV
302  */
303 #define PALMAS_SMPS_NUM_VOLTAGES        122
304 #define PALMAS_SMPS10_NUM_VOLTAGES      2
305 #define PALMAS_LDO_NUM_VOLTAGES         50
306
307 #define SMPS10_VSEL                     (1<<3)
308 #define SMPS10_BOOST_EN                 (1<<2)
309 #define SMPS10_BYPASS_EN                (1<<1)
310 #define SMPS10_SWITCH_EN                (1<<0)
311
312 #define REGULATOR_SLAVE                 0
313
314 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
315                 unsigned int *dest)
316 {
317         unsigned int addr;
318
319         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
320
321         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
322 }
323
324 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
325                 unsigned int value)
326 {
327         unsigned int addr;
328
329         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
330
331         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
332 }
333
334 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
335                 unsigned int *dest)
336 {
337         unsigned int addr;
338
339         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
340
341         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
342 }
343
344 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
345                 unsigned int value)
346 {
347         unsigned int addr;
348
349         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
350
351         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
352 }
353
354 static int palmas_is_enabled_smps(struct regulator_dev *dev)
355 {
356         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
357         int id = rdev_get_id(dev);
358         unsigned int reg;
359
360         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
361
362         reg &= PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
363         reg >>= PALMAS_SMPS12_CTRL_MODE_ACTIVE_SHIFT;
364
365         return !!(reg);
366 }
367
368 static int palmas_enable_smps(struct regulator_dev *dev)
369 {
370         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
371         int id = rdev_get_id(dev);
372         unsigned int reg;
373
374         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
375
376         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
377         if (pmic->current_reg_mode[id])
378                 reg |= pmic->current_reg_mode[id];
379         else
380                 reg |= SMPS_CTRL_MODE_ON;
381
382         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
383
384         return 0;
385 }
386
387 static int palmas_disable_smps(struct regulator_dev *dev)
388 {
389         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
390         int id = rdev_get_id(dev);
391         unsigned int reg;
392
393         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
394
395         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
396
397         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
398
399         return 0;
400 }
401
402 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
403 {
404         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
405         int id = rdev_get_id(dev);
406         unsigned int reg;
407         bool rail_enable = true;
408
409         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
410         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
411
412         if (reg == SMPS_CTRL_MODE_OFF)
413                 rail_enable = false;
414
415         switch (mode) {
416         case REGULATOR_MODE_NORMAL:
417                 reg |= SMPS_CTRL_MODE_ON;
418                 break;
419         case REGULATOR_MODE_IDLE:
420                 reg |= SMPS_CTRL_MODE_ECO;
421                 break;
422         case REGULATOR_MODE_FAST:
423                 reg |= SMPS_CTRL_MODE_PWM;
424                 break;
425         default:
426                 return -EINVAL;
427         }
428
429         pmic->current_reg_mode[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
430         if (rail_enable)
431                 palmas_smps_write(pmic->palmas,
432                         palmas_regs_info[id].ctrl_addr, reg);
433         return 0;
434 }
435
436 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
437 {
438         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
439         int id = rdev_get_id(dev);
440         unsigned int reg;
441
442         reg = pmic->current_reg_mode[id] & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
443
444         switch (reg) {
445         case SMPS_CTRL_MODE_ON:
446                 return REGULATOR_MODE_NORMAL;
447         case SMPS_CTRL_MODE_ECO:
448                 return REGULATOR_MODE_IDLE;
449         case SMPS_CTRL_MODE_PWM:
450                 return REGULATOR_MODE_FAST;
451         }
452
453         return 0;
454 }
455
456 static int palmas_set_sleep_mode_smps(struct regulator_dev *dev,
457         unsigned int mode)
458 {
459         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
460         int id = rdev_get_id(dev);
461         unsigned int reg;
462
463         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
464         reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
465
466         switch (mode) {
467         case REGULATOR_MODE_NORMAL:
468                 reg |= SMPS_CTRL_SLEEP_MODE_ON;
469                 break;
470         case REGULATOR_MODE_IDLE:
471                 reg |= SMPS_CTRL_SLEEP_MODE_ECO;
472                 break;
473         case REGULATOR_MODE_FAST:
474                 reg |= SMPS_CTRL_SLEEP_MODE_PWM;
475                 break;
476         case REGULATOR_MODE_STANDBY:
477         case REGULATOR_MODE_OFF:
478                 reg |= SMPS_CTRL_SLEEP_MODE_OFF;
479                 break;
480         default:
481                 return -EINVAL;
482         }
483         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
484         return 0;
485 }
486
487 static int palmas_list_voltage_smps(struct regulator_dev *dev,
488                                         unsigned selector)
489 {
490         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
491         int id = rdev_get_id(dev);
492         int mult = 1;
493
494         /* Read the multiplier set in VSEL register to return
495          * the correct voltage.
496          */
497         if (pmic->range[id])
498                 mult = 2;
499
500         if (selector == 0)
501                 return 0;
502         else if (selector < 6)
503                 return 500000 * mult;
504         else
505                 /* Voltage is linear mapping starting from selector 6,
506                  * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
507                  * RANGE is either x1 or x2
508                  */
509                 return (490000 + ((selector - 5) * 10000)) * mult;
510 }
511
512 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
513                 int min_uV, int max_uV)
514 {
515         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
516         int id = rdev_get_id(rdev);
517         int ret, voltage;
518
519         if (min_uV == 0)
520                 return 0;
521
522         if (pmic->range[id]) { /* RANGE is x2 */
523                 if (min_uV < 1000000)
524                         min_uV = 1000000;
525                 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
526         } else {                /* RANGE is x1 */
527                 if (min_uV < 500000)
528                         min_uV = 500000;
529                 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
530         }
531
532         /* Map back into a voltage to verify we're still in bounds */
533         voltage = palmas_list_voltage_smps(rdev, ret);
534         if (voltage < min_uV || voltage > max_uV)
535                 return -EINVAL;
536
537         return ret;
538 }
539
540 static int palma_smps_set_voltage_smps_time_sel(struct regulator_dev *rdev,
541         unsigned int old_selector, unsigned int new_selector)
542 {
543         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
544         int id = rdev_get_id(rdev);
545         int old_uv, new_uv;
546         unsigned int ramp_delay = pmic->ramp_delay[id];
547
548         if (!pmic->ramp_delay_support[id])
549                 return 0;
550
551         /* ES2.1, have the 1.5X slower slew rate than configured */
552         if (palmas_is_es_version_or_less(pmic->palmas, 2, 1))
553                 ramp_delay = (ramp_delay * 10)/15;
554
555         if (!ramp_delay)
556                 return 0;
557
558         old_uv = palmas_list_voltage_smps(rdev, old_selector);
559         if (old_uv < 0)
560                 return old_uv;
561
562         new_uv = palmas_list_voltage_smps(rdev, new_selector);
563         if (new_uv < 0)
564                 return new_uv;
565
566         return DIV_ROUND_UP(abs(old_uv - new_uv), ramp_delay);
567 }
568
569 static int palmas_smps_set_ramp_delay(struct regulator_dev *rdev,
570                  int ramp_delay)
571 {
572         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
573         int id = rdev_get_id(rdev);
574         unsigned int reg = 0;
575         unsigned int addr = palmas_regs_info[id].tstep_addr;
576         int ret;
577
578         if (!pmic->ramp_delay_support[id])
579                 return 0;
580
581         if (ramp_delay <= 0)
582                 reg = 0;
583         else if (ramp_delay <= 2500)
584                 reg = 3;
585         else if (ramp_delay <= 5000)
586                 reg = 2;
587         else
588                 reg = 1;
589
590         ret = palmas_smps_write(pmic->palmas, addr, reg);
591         if (ret < 0) {
592                 dev_err(pmic->palmas->dev, "TSTEP write failed: %d\n", ret);
593                 return ret;
594         }
595
596         pmic->ramp_delay[id] = palmas_smps_ramp_delay[reg];
597         return ret;
598 }
599
600 static struct regulator_ops palmas_ops_smps = {
601         .is_enabled             = palmas_is_enabled_smps,
602         .enable                 = palmas_enable_smps,
603         .disable                = palmas_disable_smps,
604         .set_mode               = palmas_set_mode_smps,
605         .get_mode               = palmas_get_mode_smps,
606         .set_sleep_mode         = palmas_set_sleep_mode_smps,
607         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
608         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
609         .list_voltage           = palmas_list_voltage_smps,
610         .map_voltage            = palmas_map_voltage_smps,
611         .set_voltage_time_sel   = palma_smps_set_voltage_smps_time_sel,
612         .set_ramp_delay         = palmas_smps_set_ramp_delay,
613 };
614
615 static struct regulator_ops palmas_ops_smps_extctrl = {
616         .set_mode               = palmas_set_mode_smps,
617         .get_mode               = palmas_get_mode_smps,
618         .set_sleep_mode         = palmas_set_sleep_mode_smps,
619         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
620         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
621         .list_voltage           = palmas_list_voltage_smps,
622         .map_voltage            = palmas_map_voltage_smps,
623         .set_voltage_time_sel   = palma_smps_set_voltage_smps_time_sel,
624         .set_ramp_delay         = palmas_smps_set_ramp_delay,
625 };
626
627 static struct regulator_ops palmas_ops_smps10 = {
628         .is_enabled             = regulator_is_enabled_regmap,
629         .enable                 = regulator_enable_regmap,
630         .disable                = regulator_disable_regmap,
631         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
632         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
633         .list_voltage           = regulator_list_voltage_linear,
634         .map_voltage            = regulator_map_voltage_linear,
635         .set_bypass             = regulator_set_bypass_regmap,
636         .get_bypass             = regulator_get_bypass_regmap,
637 };
638
639 static int palmas_ldo_enable_time(struct regulator_dev *dev)
640 {
641         return 500;
642 }
643
644 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
645                                         unsigned selector)
646 {
647         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
648         int id = rdev_get_id(dev);
649
650         if (!selector)
651                 return 0;
652
653         if (pmic->palmas->id == TPS80036) {
654                 if (id == PALMAS_REG_LDO4 || id == PALMAS_REG_LDO5 ||
655                                 id == PALMAS_REG_LDO9)
656                         if (pmic->ldo_vref0p425)
657                                 return  (850000 + (selector * 50000)) / 2;
658
659         }
660
661         /* voltage is 0.85V + (selector * 0.05v) */
662         return  850000 + (selector * 50000);
663 }
664
665 static int palmas_map_voltage_ldo(struct regulator_dev *rdev,
666                 int min_uV, int max_uV)
667 {
668         int ret, voltage;
669
670         if (min_uV == 0)
671                 return 0;
672
673         if (min_uV < 900000)
674                 min_uV = 900000;
675         ret = DIV_ROUND_UP(min_uV - 900000, 50000) + 1;
676
677         /* Map back into a voltage to verify we're still in bounds */
678         voltage = palmas_list_voltage_ldo(rdev, ret);
679         if (voltage < min_uV || voltage > max_uV)
680                 return -EINVAL;
681
682         return ret;
683 }
684
685 static struct regulator_ops palmas_ops_ldo = {
686         .enable_time            = palmas_ldo_enable_time,
687         .is_enabled             = regulator_is_enabled_regmap,
688         .enable                 = regulator_enable_regmap,
689         .disable                = regulator_disable_regmap,
690         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
691         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
692         .list_voltage           = palmas_list_voltage_ldo,
693         .map_voltage            = palmas_map_voltage_ldo,
694 };
695
696 static struct regulator_ops palmas_ops_ldo_extctrl = {
697         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
698         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
699         .list_voltage           = palmas_list_voltage_ldo,
700         .map_voltage            = palmas_map_voltage_ldo,
701 };
702
703 static int palmas_getvoltage_chargepump(struct regulator_dev *rdev)
704 {
705         return 5000;
706 }
707
708 static int palmas_getvoltage_extreg(struct regulator_dev *rdev)
709 {
710         return 4300 * 1000;
711 }
712
713 static struct regulator_ops palmas_ops_extreg = {
714         .is_enabled             = regulator_is_enabled_regmap,
715         .enable                 = regulator_enable_regmap,
716         .disable                = regulator_disable_regmap,
717 };
718
719 static struct regulator_ops palmas_ops_extreg_extctrl = {
720         .get_voltage            = palmas_getvoltage_extreg,
721 };
722
723 static struct regulator_ops palmas_ops_chargepump = {
724         .is_enabled             = regulator_is_enabled_regmap,
725         .enable                 = regulator_enable_regmap,
726         .disable                = regulator_disable_regmap,
727         .get_voltage            = palmas_getvoltage_chargepump,
728 };
729
730 static int palmas_ldo5_tracking_init(struct palmas *palmas,
731                 struct palmas_reg_init *reg_init,
732                 struct regulator_dev *rdev)
733 {
734         unsigned int reg;
735         int ret;
736
737         ret = palmas_ldo_read(palmas, PALMAS_LDO_CTRL, &reg);
738         if (ret < 0)
739                 return ret;
740         reg = reg & ~PALMAS_LDO_CTRL_LDO5_BYPASS_SRC_SEL_MASK;
741         if (reg_init->config_flags & PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE) {
742                 if (reg_init->tracking_regulator == PALMAS_REG_SMPS12)
743                         reg |= PALMAS_LDO_CTRL_LDO5_BYPASS_SRC_SEL_SMPS12;
744                 else if (reg_init->tracking_regulator == PALMAS_REG_SMPS3)
745                         reg |= PALMAS_LDO_CTRL_LDO5_BYPASS_SRC_SEL_SMPS3;
746                 else if (reg_init->tracking_regulator == PALMAS_REG_SMPS6)
747                         reg |= PALMAS_LDO_CTRL_LDO5_BYPASS_SRC_SEL_SMPS6;
748         }
749         ret = palmas_ldo_write(palmas, PALMAS_LDO_CTRL, reg);
750         if (ret < 0)
751                 return ret;
752
753         if (reg_init->config_flags & PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE) {
754                 ret = regulator_disable_regmap(rdev);
755                 if (ret < 0)
756                 return ret;
757         }
758         return 0;
759 }
760
761 static void palmas_enable_ldo8_track(struct palmas *palmas)
762 {
763         unsigned int reg;
764         unsigned int addr;
765         int ret;
766
767         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
768
769         ret = palmas_ldo_read(palmas, addr, &reg);
770         if (ret) {
771                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
772                 return;
773         }
774
775         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
776         ret = palmas_ldo_write(palmas, addr, reg);
777         if (ret < 0) {
778                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
779                 return;
780         }
781         /*
782          * When SMPS45 is set to off and LDO8 tracking is enabled, the LDO8
783          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
784          * and can be set from 0.45 to 1.65 V.
785          */
786         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
787         ret = palmas_ldo_read(palmas, addr, &reg);
788         if (ret) {
789                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
790                 return;
791         }
792
793         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
794         ret = palmas_ldo_write(palmas, addr, reg);
795         if (ret < 0) {
796                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
797                 return;
798         }
799
800         /*
801          * Errata ES1.0, 2,0 and 2.1
802          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
803          * when tracking is disabled, SW has to enabe Pull-Down.
804          */
805         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
806                 addr = PALMAS_LDO_PD_CTRL1;
807                 ret = palmas_ldo_read(palmas, addr, &reg);
808                 if (ret < 0) {
809                         dev_err(palmas->dev,
810                                 "Error in reading pulldown control reg\n");
811                         return;
812                 }
813                 reg &= ~PALMAS_LDO_PD_CTRL1_LDO8;
814                 ret = palmas_ldo_write(palmas, addr, reg);
815                 if (ret < 0) {
816                         dev_err(palmas->dev,
817                                 "Error in setting pulldown control reg\n");
818                         return;
819                 }
820         }
821
822         return;
823 }
824
825 static void palmas_disable_ldo8_track(struct palmas *palmas)
826 {
827         unsigned int reg;
828         unsigned int addr;
829         int ret;
830
831         /*
832          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
833          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
834          * and can be set from 0.45 to 1.65 V.
835          */
836         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
837         ret = palmas_ldo_read(palmas, addr, &reg);
838         if (ret) {
839                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
840                 return;
841         }
842
843         reg = (reg >> 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
844         ret = palmas_ldo_write(palmas, addr, reg);
845         if (ret < 0) {
846                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
847                 return;
848         }
849
850         /* Disable the tracking mode */
851         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
852         ret = palmas_ldo_read(palmas, addr, &reg);
853         if (ret) {
854                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
855                 return;
856         }
857         reg &= ~PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
858         ret = palmas_ldo_write(palmas, addr, reg);
859         if (ret < 0) {
860                 dev_err(palmas->dev, "Error in disabling tracking mode\n");
861                 return;
862         }
863
864         /*
865          * Errata ES1.0, 2,0 and 2.1
866          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
867          * when tracking is disabled, SW has to enabe Pull-Down.
868          */
869         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
870                 addr = PALMAS_LDO_PD_CTRL1;
871                 ret = palmas_ldo_read(palmas, addr, &reg);
872                 if (ret < 0) {
873                         dev_err(palmas->dev,
874                                 "Error in reading pulldown control reg\n");
875                         return;
876                 }
877                 reg |= PALMAS_LDO_PD_CTRL1_LDO8;
878                 ret = palmas_ldo_write(palmas, addr, reg);
879                 if (ret < 0) {
880                         dev_err(palmas->dev,
881                                 "Error in setting pulldown control reg\n");
882                         return;
883                 }
884         }
885
886         return;
887 }
888
889 /*
890  * setup the hardware based sleep configuration of the SMPS/LDO regulators
891  * from the platform data. This is different to the software based control
892  * supported by the regulator framework as it is controlled by toggling
893  * pins on the PMIC such as PREQ, SYSEN, ...
894  */
895 static int palmas_smps_init(struct regulator_dev *rdev,
896                 struct palmas *palmas, int id,
897                 struct palmas_reg_init *reg_init)
898 {
899         unsigned int reg;
900         unsigned int addr;
901         int ret;
902
903         addr = palmas_regs_info[id].ctrl_addr;
904
905         ret = palmas_smps_read(palmas, addr, &reg);
906         if (ret)
907                 return ret;
908
909         switch (id) {
910         case PALMAS_REG_SMPS10_OUT1:
911         case PALMAS_REG_SMPS10_OUT2:
912                 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
913                 if (reg_init->mode_sleep)
914                         reg |= reg_init->mode_sleep <<
915                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
916                 break;
917         default:
918                 if (reg_init->warm_reset)
919                         reg |= PALMAS_SMPS12_CTRL_WR_S;
920                 else
921                         reg &= ~PALMAS_SMPS12_CTRL_WR_S;
922
923                 if (reg_init->roof_floor)
924                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
925                 else
926                         reg &= ~PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
927
928                 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
929                 if (reg_init->mode_sleep)
930                         reg |= reg_init->mode_sleep <<
931                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
932                 break;
933         }
934
935         ret = palmas_smps_write(palmas, addr, reg);
936         if (ret)
937                 return ret;
938
939         if ((id != PALMAS_REG_SMPS10_OUT1) &&
940                         (id != PALMAS_REG_SMPS10_OUT2) &&
941                         reg_init->roof_floor) {
942                 int sleep_id = palmas_regs_info[id].sleep_id;
943
944                 ret = palmas_enable_smps(rdev);
945                 if (ret < 0) {
946                         dev_err(palmas->dev, "SMPS enable failed %d\n", ret);
947                         return ret;
948                 }
949
950                 ret = palmas_ext_power_req_config(palmas, sleep_id,
951                                         reg_init->roof_floor, true);
952                 if (ret < 0) {
953                         dev_err(palmas->dev,
954                                 "Error in configuring external control\n");
955                         return ret;
956                 }
957
958                 if (id == PALMAS_REG_SMPS123) {
959                         ret = palmas_ext_power_req_config(palmas,
960                                         PALMAS_SLEEP_REQSTR_ID_SMPS3,
961                                         reg_init->roof_floor, true);
962                         if (ret < 0) {
963                                 dev_err(palmas->dev,
964                                         "Error in configuring ext control\n");
965                                 return ret;
966                         }
967                 }
968         }
969
970         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
971                 addr = palmas_regs_info[id].vsel_addr;
972
973                 reg = reg_init->vsel;
974
975                 ret = palmas_smps_write(palmas, addr, reg);
976                 if (ret)
977                         return ret;
978         }
979
980
981         return 0;
982 }
983
984 static int palmas_ldo_init(struct regulator_dev *rdev,
985                 struct palmas *palmas, int id,
986                 struct palmas_reg_init *reg_init)
987 {
988         unsigned int reg;
989         unsigned int addr;
990         int ret;
991
992         addr = palmas_regs_info[id].ctrl_addr;
993
994         ret = palmas_ldo_read(palmas, addr, &reg);
995         if (ret)
996                 return ret;
997
998         if (reg_init->warm_reset)
999                 reg |= PALMAS_LDO1_CTRL_WR_S;
1000         else
1001                 reg &= ~PALMAS_LDO1_CTRL_WR_S;
1002
1003         if (reg_init->mode_sleep)
1004                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
1005         else
1006                 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
1007
1008         ret = palmas_ldo_write(palmas, addr, reg);
1009         if (ret)
1010                 return ret;
1011
1012         if (reg_init->roof_floor) {
1013                 int sleep_id = palmas_regs_info[id].sleep_id;
1014
1015                 /* Always enable if externally controlled */
1016                 ret = regulator_enable_regmap(rdev);
1017                 if (ret < 0) {
1018                         dev_err(palmas->dev,
1019                                 "Error in enabling LOD regulator %d\n", id);
1020                         return ret;
1021                 }
1022
1023                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1024                         reg_init->roof_floor, true);
1025                 if (ret < 0) {
1026                         dev_err(palmas->dev,
1027                                 "Error in configuring external control\n");
1028                         return ret;
1029                 }
1030         }
1031
1032         /* Rail specific configuration */
1033         switch (id) {
1034         case PALMAS_REG_LDO5:
1035                 ret = palmas_ldo5_tracking_init(palmas, reg_init,
1036                                         palmas->pmic->rdev[id]);
1037                 if (ret < 0) {
1038                         dev_err(palmas->dev,
1039                                 "tracking mode init for rail %d failed: %d\n",
1040                                 id, ret);
1041                         return ret;
1042                 }
1043                 break;
1044
1045         case PALMAS_REG_LDO8:
1046                 if (reg_init->config_flags &
1047                         PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE)
1048                         palmas_enable_ldo8_track(palmas);
1049                 break;
1050
1051         default:
1052                 break;
1053         }
1054         return 0;
1055 }
1056
1057 static int palmas_extreg_init(struct regulator_dev *rdev,
1058                 struct palmas *palmas, int id,
1059                 struct palmas_reg_init *reg_init)
1060 {
1061         unsigned int addr;
1062         int ret;
1063         unsigned int val = 0;
1064
1065         addr = palmas_regs_info[id].ctrl_addr;
1066
1067         if (reg_init->mode_sleep)
1068                 val = PALMAS_REGEN1_CTRL_MODE_SLEEP;
1069
1070         ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
1071                         addr, PALMAS_REGEN1_CTRL_MODE_SLEEP, val);
1072         if (ret < 0) {
1073                 dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n",
1074                         addr, ret);
1075                 return ret;
1076         }
1077
1078         if (reg_init->roof_floor) {
1079                 int sleep_id = palmas_regs_info[id].sleep_id;
1080
1081                 /* Always enable if externally controlled */
1082                 ret = regulator_enable_regmap(rdev);
1083                 if (ret < 0) {
1084                         dev_err(palmas->dev,
1085                                 "Error in enabling regulator %d\n", id);
1086                         return ret;
1087                 }
1088
1089                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1090                         reg_init->roof_floor, true);
1091                 if (ret < 0) {
1092                         dev_err(palmas->dev,
1093                                 "Error in configuring external control\n");
1094                         return ret;
1095                 }
1096         }
1097         return 0;
1098 }
1099
1100 static ssize_t palmas_show_dvfs_data(struct device *dev,
1101                         struct device_attribute *attr, char *buf)
1102 {
1103         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1104         struct palmas_dvfs_init_data *dvfs_idata = pdata->dvfs_init_data;
1105         int data_size = pdata->dvfs_init_data_size;
1106         int i;
1107         int count = 0;
1108
1109         if (!dvfs_idata || !data_size)
1110                 return 0;
1111
1112         for (i = 0; i < data_size; i++) {
1113                 struct palmas_dvfs_init_data *dvfs_pd =  &dvfs_idata[i];
1114                 if (!dvfs_pd->en_pwm)
1115                         continue;
1116
1117                 count += sprintf(buf+count, "base_voltage:%d\n",
1118                                 dvfs_pd->base_voltage_uV);
1119                 count += sprintf(buf+count, "step_size:%d\n",
1120                                 dvfs_pd->step_20mV ? 20000 : 10000);
1121                 count += sprintf(buf+count, "max_voltage:%d\n",
1122                                 dvfs_pd->max_voltage_uV);
1123         }
1124
1125         return count;
1126 }
1127 static DEVICE_ATTR(dvfs_data, 0444, palmas_show_dvfs_data, NULL);
1128
1129 static void palmas_dvfs_init(struct palmas *palmas,
1130                         struct palmas_pmic_platform_data *pdata)
1131 {
1132         int slave;
1133         struct palmas_dvfs_init_data *dvfs_idata = pdata->dvfs_init_data;
1134         int data_size = pdata->dvfs_init_data_size;
1135         unsigned int reg, addr;
1136         int ret;
1137         int sleep_id;
1138         int i;
1139
1140         if (!dvfs_idata || !data_size)
1141                 return;
1142
1143         slave = PALMAS_BASE_TO_SLAVE(PALMAS_DVFS_BASE);
1144         for (i = 0; i < data_size; i++) {
1145                 struct palmas_dvfs_init_data *dvfs_pd =  &dvfs_idata[i];
1146
1147                 sleep_id = palmas_regs_info[dvfs_pd->reg_id].sleep_id;
1148                 if (!dvfs_pd->en_pwm)
1149                         continue;
1150
1151                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1152                                 dvfs_pd->ext_ctrl, true);
1153                 if (ret < 0) {
1154                         dev_err(palmas->dev,
1155                                         "Error in configuring external control\n");
1156                         goto err;
1157                 }
1158
1159                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1160                                 (PALMAS_SMPS_DVFS1_CTRL) + i*3);
1161                 reg =  (1 << PALMAS_SMPS_DVFS1_ENABLE_SHIFT);
1162                 if (dvfs_pd->step_20mV)
1163                         reg |= (1 << PALMAS_SMPS_DVFS1_OFFSET_STEP_SHIFT);
1164                 /* only DVFS1_CTRL register contains smps select bit */
1165                 if (dvfs_pd->smps3_ctrl && (i == 0))
1166                         reg |= (1 << PALMAS_SMPS_DVFS1_SMPS_SELECT_SHIFT);
1167
1168                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1169                 if (ret)
1170                         goto err;
1171
1172                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1173                                 (PALMAS_SMPS_DVFS1_VOLTAGE_MAX) + i*3);
1174                 if (!(dvfs_pd->max_voltage_uV >= DVFS_BASE_VOLTAGE_UV &&
1175                         dvfs_pd->max_voltage_uV <= DVFS_MAX_VOLTAGE_UV))
1176                         goto err;
1177
1178                 reg = DIV_ROUND_UP((dvfs_pd->max_voltage_uV -
1179                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1180                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1181                 if (ret)
1182                         goto err;
1183
1184                 addr = palmas_regs_info[dvfs_pd->reg_id].ctrl_addr;
1185                 ret = palmas_update_bits(palmas, PALMAS_SMPS_BASE, addr,
1186                         PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN, 0);
1187                 if (ret)
1188                         goto err;
1189
1190                 addr = palmas_regs_info[dvfs_pd->reg_id].fvsel_addr;
1191                 reg = (1 << PALMAS_SMPS12_FORCE_CMD_SHIFT);
1192                 reg |= DIV_ROUND_UP((dvfs_pd->base_voltage_uV -
1193                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1194                 ret = palmas_smps_write(palmas, addr, reg);
1195                 if (ret)
1196                         goto  err;
1197
1198         }
1199
1200         ret = device_create_file(palmas->pmic->dev, &dev_attr_dvfs_data);
1201         if (ret)
1202                 dev_warn(palmas->pmic->dev,
1203                                 "Can't register dvfs sysfs attribute\n");
1204
1205         ret = sysfs_create_link(kernel_kobj, &(palmas->pmic->dev->kobj),
1206                                 "pmic");
1207         if (ret)
1208                 dev_warn(palmas->pmic->dev, "Can't create sysfs link\n");
1209
1210         return;
1211 err:
1212         dev_err(palmas->dev, "Failed to initilize cl dvfs(%d)", i);
1213         return;
1214 }
1215
1216 static struct of_regulator_match palmas_matches[] = {
1217         { .name = "smps12", },
1218         { .name = "smps123", },
1219         { .name = "smps3", },
1220         { .name = "smps45", },
1221         { .name = "smps457", },
1222         { .name = "smps6", },
1223         { .name = "smps7", },
1224         { .name = "smps8", },
1225         { .name = "smps9", },
1226         { .name = "smps10", },
1227         { .name = "ldo1", },
1228         { .name = "ldo2", },
1229         { .name = "ldo3", },
1230         { .name = "ldo4", },
1231         { .name = "ldo5", },
1232         { .name = "ldo6", },
1233         { .name = "ldo7", },
1234         { .name = "ldo8", },
1235         { .name = "ldo9", },
1236         { .name = "ldoln", },
1237         { .name = "ldousb", },
1238         { .name = "regen1", },
1239         { .name = "regen2", },
1240         { .name = "regen3", },
1241         { .name = "sysen1", },
1242         { .name = "sysen2", },
1243 };
1244
1245 static void palmas_dt_to_pdata(struct device *dev,
1246                 struct device_node *node,
1247                 struct palmas_pmic_platform_data *pdata)
1248 {
1249         struct device_node *regulators;
1250         u32 prop;
1251         int idx, ret;
1252
1253         node = of_node_get(node);
1254         regulators = of_find_node_by_name(node, "regulators");
1255         if (!regulators) {
1256                 dev_info(dev, "regulator node not found\n");
1257                 return;
1258         }
1259
1260         ret = of_regulator_match(dev, regulators, palmas_matches,
1261                         PALMAS_NUM_REGS);
1262         of_node_put(regulators);
1263         if (ret < 0) {
1264                 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
1265                 return;
1266         }
1267
1268         for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
1269                 if (!palmas_matches[idx].init_data ||
1270                                 !palmas_matches[idx].of_node)
1271                         continue;
1272
1273                 pdata->reg_data[idx] = palmas_matches[idx].init_data;
1274
1275                 pdata->reg_init[idx] = devm_kzalloc(dev,
1276                                 sizeof(struct palmas_reg_init), GFP_KERNEL);
1277
1278                 pdata->reg_init[idx]->warm_reset =
1279                         of_property_read_bool(palmas_matches[idx].of_node,
1280                                              "ti,warm-reset");
1281
1282                 pdata->reg_init[idx]->roof_floor =
1283                         of_property_read_bool(palmas_matches[idx].of_node,
1284                                               "ti,roof-floor");
1285
1286                 ret = of_property_read_u32(palmas_matches[idx].of_node,
1287                                 "ti,mode-sleep", &prop);
1288                 if (!ret)
1289                         pdata->reg_init[idx]->mode_sleep = prop;
1290
1291                 ret = of_property_read_bool(palmas_matches[idx].of_node,
1292                                             "ti,smps-range");
1293                 if (ret)
1294                         pdata->reg_init[idx]->vsel =
1295                                 PALMAS_SMPS12_VOLTAGE_RANGE;
1296
1297                 if (idx == PALMAS_REG_LDO8) {
1298                         if (of_property_read_bool(palmas_matches[idx].of_node,
1299                                                 "ti,enable-ldo8-tracking")) {
1300                                 pdata->reg_init[idx]->config_flags =
1301                                         PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE;
1302                         }
1303                 }
1304         }
1305
1306         pdata->ldo6_vibrator = of_property_read_bool(node, "ti,ldo6-vibrator");
1307 }
1308
1309
1310 static int palmas_regulators_probe(struct platform_device *pdev)
1311 {
1312         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
1313         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
1314         struct device_node *node = pdev->dev.of_node;
1315         struct regulator_dev *rdev;
1316         struct regulator_config config = { };
1317         struct palmas_pmic *pmic;
1318         struct palmas_reg_init *reg_init;
1319         int id = 0, ret;
1320         unsigned int addr, reg;
1321         int roof_floor;
1322
1323         if (node && !pdata) {
1324                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1325
1326                 if (!pdata)
1327                         return -ENOMEM;
1328
1329                 palmas_dt_to_pdata(&pdev->dev, node, pdata);
1330         }
1331
1332         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1333         if (!pmic)
1334                 return -ENOMEM;
1335
1336         pmic->dev = &pdev->dev;
1337         pmic->palmas = palmas;
1338         palmas->pmic = pmic;
1339         platform_set_drvdata(pdev, pmic);
1340
1341         /* Read VREF0P425 of LDO_CTRL register for TPS80036 */
1342         if (palmas->id == TPS80036) {
1343                 ret = palmas_read(palmas, PALMAS_LDO_BASE,
1344                         PALMAS_LDO_CTRL, &reg);
1345                 if (ret)
1346                         goto err_unregister_regulator;
1347
1348                 palmas->pmic->ldo_vref0p425 = (reg & PALMAS_LDO_CTRL_VREF_425)
1349                                                 ? 1 : 0;
1350         }
1351
1352         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
1353         if (ret)
1354                 return ret;
1355
1356         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
1357                 pmic->smps123 = 1;
1358
1359         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
1360                 pmic->smps457 = 1;
1361
1362         config.regmap = palmas->regmap[REGULATOR_SLAVE];
1363         config.dev = &pdev->dev;
1364         config.driver_data = pmic;
1365
1366         for (id = 0; id < PALMAS_REG_LDO1; id++) {
1367                 bool ramp_delay_support = false;
1368
1369                 reg_init = pdata->reg_init[id];
1370                 roof_floor = 0;
1371                 if (reg_init)
1372                         roof_floor = reg_init->roof_floor;
1373
1374                 /*
1375                  * Miss out regulators which are not available due
1376                  * to slaving configurations.
1377                  */
1378                 switch (id) {
1379                 case PALMAS_REG_SMPS12:
1380                 case PALMAS_REG_SMPS3:
1381                         if (pmic->smps123)
1382                                 continue;
1383                         if (id == PALMAS_REG_SMPS12)
1384                                 ramp_delay_support = true;
1385
1386                         /* TPS80036 suports ramp delay on SMPS3 also */
1387                         if (palmas->id == TPS80036)
1388                                 ramp_delay_support = true;
1389
1390                         break;
1391                 case PALMAS_REG_SMPS123:
1392                         if (!pmic->smps123)
1393                                 continue;
1394                         ramp_delay_support = true;
1395                         break;
1396                 case PALMAS_REG_SMPS45:
1397                 case PALMAS_REG_SMPS7:
1398                         if (pmic->smps457)
1399                                 continue;
1400                         if (id == PALMAS_REG_SMPS45)
1401                                 ramp_delay_support = true;
1402                         break;
1403                 case PALMAS_REG_SMPS457:
1404                         if (!pmic->smps457)
1405                                 continue;
1406                         ramp_delay_support = true;
1407                         break;
1408                 }
1409
1410                 if ((id == PALMAS_REG_SMPS6) || (id == PALMAS_REG_SMPS8))
1411                         ramp_delay_support = true;
1412
1413                 pmic->ramp_delay_support[id] = ramp_delay_support;
1414                 if (ramp_delay_support) {
1415                         addr = palmas_regs_info[id].tstep_addr;
1416                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1417                         if (ret < 0) {
1418                                 dev_err(&pdev->dev,
1419                                         "reading TSTEP reg failed: %d\n", ret);
1420                                 goto err_unregister_regulator;
1421                         }
1422                         pmic->desc[id].ramp_delay =
1423                                         palmas_smps_ramp_delay[reg & 0x3];
1424                         pmic->ramp_delay[id] = pmic->desc[id].ramp_delay;
1425                 }
1426
1427                 /* Register the regulators */
1428                 pmic->desc[id].name = palmas_regs_info[id].name;
1429                 pmic->desc[id].id = id;
1430
1431                 switch (id) {
1432                 case PALMAS_REG_SMPS10_OUT1:
1433                 case PALMAS_REG_SMPS10_OUT2:
1434                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
1435                         pmic->desc[id].ops = &palmas_ops_smps10;
1436                         pmic->desc[id].vsel_reg =
1437                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
1438                                                         PALMAS_SMPS10_CTRL);
1439                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
1440                         pmic->desc[id].enable_reg =
1441                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
1442                                                         PALMAS_SMPS10_CTRL);
1443                         if (id == PALMAS_REG_SMPS10_OUT1)
1444                                 pmic->desc[id].enable_mask = SMPS10_SWITCH_EN;
1445                         else
1446                                 pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
1447                         pmic->desc[id].bypass_reg =
1448                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
1449                                                         PALMAS_SMPS10_CTRL);
1450                         pmic->desc[id].bypass_mask = SMPS10_BYPASS_EN;
1451                         pmic->desc[id].min_uV = 3750000;
1452                         pmic->desc[id].uV_step = 1250000;
1453                         break;
1454                 default:
1455                         /*
1456                          * Read and store the RANGE bit for later use
1457                          * This must be done before regulator is probed,
1458                          * otherwise we error in probe with unsupportable
1459                          * ranges. Read the current smps mode for later use.
1460                          */
1461                         addr = palmas_regs_info[id].vsel_addr;
1462
1463                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1464                         if (ret)
1465                                 goto err_unregister_regulator;
1466                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
1467                                 pmic->range[id] = 1;
1468
1469                         if (roof_floor)
1470                                 pmic->desc[id].ops = &palmas_ops_smps_extctrl;
1471                         else
1472                                 pmic->desc[id].ops = &palmas_ops_smps;
1473                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
1474                         pmic->desc[id].vsel_reg =
1475                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
1476                                                 palmas_regs_info[id].vsel_addr);
1477                         pmic->desc[id].vsel_mask =
1478                                         PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
1479
1480                         /* Read the smps mode for later use. */
1481                         addr = palmas_regs_info[id].ctrl_addr;
1482                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1483                         if (ret)
1484                                 goto err_unregister_regulator;
1485                         pmic->current_reg_mode[id] = reg &
1486                                         PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
1487                         break;
1488                 }
1489
1490                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1491                 pmic->desc[id].owner = THIS_MODULE;
1492
1493                 if (pdata)
1494                         config.init_data = pdata->reg_data[id];
1495                 else
1496                         config.init_data = NULL;
1497
1498                 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
1499                 config.of_node = NULL;//palmas_matches[id].of_node;
1500                 if (roof_floor & EXT_PWR_REQ) {
1501                         config.ena_gpio = reg_init->enable_gpio;
1502                         config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
1503                 } else {
1504                         config.ena_gpio = -EINVAL;
1505                         config.ena_gpio_flags = 0;
1506                 }
1507
1508                 rdev = regulator_register(&pmic->desc[id], &config);
1509                 if (IS_ERR(rdev)) {
1510                         dev_err(&pdev->dev,
1511                                 "failed to register %s regulator\n",
1512                                 pdev->name);
1513                         ret = PTR_ERR(rdev);
1514                         goto err_unregister_regulator;
1515                 }
1516
1517                 /* Save regulator for cleanup */
1518                 pmic->rdev[id] = rdev;
1519
1520                 /* Initialise sleep/init values from platform data */
1521                 if (reg_init) {
1522                         pmic->config_flags[id] = reg_init->config_flags;
1523                         ret = palmas_smps_init(rdev, palmas, id, reg_init);
1524                         if (ret)
1525                                 goto err_unregister_regulator;
1526                 }
1527         }
1528
1529         /* Start this loop from the id left from previous loop */
1530         for (; id < PALMAS_NUM_REGS; id++) {
1531
1532                 if (palmas->id != TPS80036) {
1533                         if (id > PALMAS_REG_LDO9 && id < PALMAS_REG_LDOLN)
1534                                 continue;
1535                         if (id > PALMAS_REG_REGEN3 && id < PALMAS_REG_SYSEN1)
1536                                 continue;
1537                 } else {
1538                         if (id == PALMAS_REG_REGEN3)
1539                                 continue;
1540                 }
1541
1542                 reg_init = pdata->reg_init[id];
1543                 roof_floor = 0;
1544                 if (reg_init)
1545                         roof_floor = reg_init->roof_floor;
1546
1547                 /* Miss out regulators which are not available due
1548                  * to alternate functions.
1549                  */
1550
1551                 /* Register the regulators */
1552                 pmic->desc[id].name = palmas_regs_info[id].name;
1553                 pmic->desc[id].id = id;
1554                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1555                 pmic->desc[id].owner = THIS_MODULE;
1556
1557                 if (id < PALMAS_REG_REGEN1) {
1558                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
1559                         if (roof_floor)
1560                                 pmic->desc[id].ops = &palmas_ops_ldo_extctrl;
1561                         else
1562                                 pmic->desc[id].ops = &palmas_ops_ldo;
1563                         pmic->desc[id].vsel_reg =
1564                                         PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
1565                                                 palmas_regs_info[id].vsel_addr);
1566                         pmic->desc[id].vsel_mask =
1567                                         PALMAS_LDO1_VOLTAGE_VSEL_MASK;
1568                         pmic->desc[id].enable_reg =
1569                                         PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
1570                                                 palmas_regs_info[id].ctrl_addr);
1571                         pmic->desc[id].enable_mask =
1572                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
1573
1574                         /* Check if LDO8 is in tracking mode or not */
1575                         if (pdata && (id == PALMAS_REG_LDO8) &&
1576                                         (pdata->reg_init[id]->config_flags &
1577                                         PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE)) {
1578                                 pmic->desc[id].min_uV = 450000;
1579                                 pmic->desc[id].uV_step = 25000;
1580                         }
1581                 } else if (id == PALMAS_REG_CHARGER_PUMP) {
1582                         pmic->desc[id].enable_reg =
1583                                         palmas_regs_info[id].ctrl_addr;
1584                         pmic->desc[id].enable_mask =
1585                                         PALMAS_CHARGE_PUMP_CTRL_MODE_ACTIVE;
1586                         pmic->desc[id].ops = &palmas_ops_chargepump;
1587                 } else {
1588                         pmic->desc[id].n_voltages = 1;
1589                         if (roof_floor)
1590                                 pmic->desc[id].ops = &palmas_ops_extreg_extctrl;
1591                         else
1592                                 pmic->desc[id].ops = &palmas_ops_extreg;
1593                         pmic->desc[id].enable_reg =
1594                                         PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE,
1595                                                 palmas_regs_info[id].ctrl_addr);
1596                         pmic->desc[id].enable_mask =
1597                                         PALMAS_REGEN1_CTRL_MODE_ACTIVE;
1598                 }
1599
1600                 if (pdata)
1601                         config.init_data = pdata->reg_data[id];
1602                 else
1603                         config.init_data = NULL;
1604
1605                 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
1606                 config.of_node = NULL; //palmas_matches[id].of_node;
1607                 if (roof_floor) {
1608                         config.ena_gpio = reg_init->enable_gpio;
1609                         config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
1610                 }
1611
1612                 rdev = regulator_register(&pmic->desc[id], &config);
1613                 if (IS_ERR(rdev)) {
1614                         dev_err(&pdev->dev,
1615                                 "failed to register %s regulator\n",
1616                                 pdev->name);
1617                         ret = PTR_ERR(rdev);
1618                         goto err_unregister_regulator;
1619                 }
1620
1621                 /* Save regulator for cleanup */
1622                 pmic->rdev[id] = rdev;
1623
1624                 /* Initialise sleep/init values from platform data */
1625                 if (pdata && reg_init && (id != PALMAS_REG_CHARGER_PUMP)) {
1626                         pmic->config_flags[id] = reg_init->config_flags;
1627                         if (id < PALMAS_REG_REGEN1)
1628                                 ret = palmas_ldo_init(rdev, palmas, id,
1629                                                         reg_init);
1630                         else
1631                                 ret = palmas_extreg_init(rdev, palmas,
1632                                                 id, reg_init);
1633                         if (ret) {
1634                                 regulator_unregister(pmic->rdev[id]);
1635                                 goto err_unregister_regulator;
1636                         }
1637                 }
1638         }
1639
1640         palmas_dvfs_init(palmas, pdata);
1641         return 0;
1642
1643 err_unregister_regulator:
1644         while (--id >= 0)
1645                 regulator_unregister(pmic->rdev[id]);
1646         return ret;
1647 }
1648
1649 static int palmas_regulators_remove(struct platform_device *pdev)
1650 {
1651         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1652         int id;
1653
1654         for (id = 0; id < PALMAS_NUM_REGS; id++)
1655                 regulator_unregister(pmic->rdev[id]);
1656         return 0;
1657 }
1658
1659 static struct of_device_id of_palmas_match_tbl[] = {
1660         { .compatible = "ti,palmas-pmic", },
1661         { .compatible = "ti,twl6035-pmic", },
1662         { .compatible = "ti,twl6036-pmic", },
1663         { .compatible = "ti,twl6037-pmic", },
1664         { .compatible = "ti,tps65913-pmic", },
1665         { .compatible = "ti,tps65914-pmic", },
1666         { .compatible = "ti,tps80036-pmic", },
1667         { /* end */ }
1668 };
1669
1670 #ifdef CONFIG_PM_SLEEP
1671 static int palmas_suspend(struct device *dev)
1672 {
1673         struct palmas *palmas = dev_get_drvdata(dev->parent);
1674         struct palmas_pmic *pmic = dev_get_drvdata(dev);
1675         int id;
1676
1677         for (id = 0; id < PALMAS_NUM_REGS; id++) {
1678                 unsigned int cf = pmic->config_flags[id];
1679
1680                 if ((cf & PALMAS_REGULATOR_CONFIG_SUSPEND_TRACKING_DISABLE) &&
1681                         (cf & PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE)) {
1682                         if (id == PALMAS_REG_LDO8)
1683                                 palmas_disable_ldo8_track(palmas);
1684                 }
1685
1686                 if (pmic->config_flags[id] &
1687                         PALMAS_REGULATOR_CONFIG_SUSPEND_FORCE_OFF) {
1688                         if (pmic->desc[id].ops->disable)
1689                                 pmic->desc[id].ops->disable(pmic->rdev[id]);
1690                 }
1691         }
1692         return 0;
1693 }
1694
1695 static int palmas_resume(struct device *dev)
1696 {
1697         struct palmas *palmas = dev_get_drvdata(dev->parent);
1698         struct palmas_pmic *pmic = dev_get_drvdata(dev);
1699         int id;
1700
1701         for (id = 0; id < PALMAS_NUM_REGS; id++) {
1702                 unsigned int cf = pmic->config_flags[id];
1703
1704                 if ((cf & PALMAS_REGULATOR_CONFIG_SUSPEND_TRACKING_DISABLE) &&
1705                         (cf & PALMAS_REGULATOR_CONFIG_TRACKING_ENABLE)) {
1706                         if (id == PALMAS_REG_LDO8)
1707                                 palmas_enable_ldo8_track(palmas);
1708                 }
1709
1710                 if (pmic->config_flags[id] &
1711                         PALMAS_REGULATOR_CONFIG_SUSPEND_FORCE_OFF) {
1712                         if (pmic->desc[id].ops->enable)
1713                                 pmic->desc[id].ops->enable(pmic->rdev[id]);
1714                 }
1715         }
1716         return 0;
1717 }
1718 #endif
1719 static const struct dev_pm_ops palmas_pm_ops = {
1720         SET_SYSTEM_SLEEP_PM_OPS(palmas_suspend, palmas_resume)
1721 };
1722
1723
1724 static struct platform_driver palmas_driver = {
1725         .driver = {
1726                 .name = "palmas-pmic",
1727                 .of_match_table = of_palmas_match_tbl,
1728                 .owner = THIS_MODULE,
1729                 .pm     = &palmas_pm_ops,
1730         },
1731         .probe = palmas_regulators_probe,
1732         .remove = palmas_regulators_remove,
1733 };
1734
1735 static int __init palmas_init(void)
1736 {
1737         return platform_driver_register(&palmas_driver);
1738 }
1739 subsys_initcall(palmas_init);
1740
1741 static void __exit palmas_exit(void)
1742 {
1743         platform_driver_unregister(&palmas_driver);
1744 }
1745 module_exit(palmas_exit);
1746
1747 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1748 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1749 MODULE_LICENSE("GPL");
1750 MODULE_ALIAS("platform:palmas-pmic");
1751 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);