regulator: palmas: Fix off-by-one for ramp_delay and register value mapping
[sfrench/cifs-2.6.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  *
6  * Author: Graeme Gregory <gg@slimlogic.co.uk>
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under  the terms of the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the License, or (at your
11  *  option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/slab.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/palmas.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/regulator/of_regulator.h>
28
29 struct regs_info {
30         char    *name;
31         char    *sname;
32         u8      vsel_addr;
33         u8      ctrl_addr;
34         u8      tstep_addr;
35 };
36
37 static const struct regs_info palmas_regs_info[] = {
38         {
39                 .name           = "SMPS12",
40                 .sname          = "smps1-in",
41                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
42                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
43                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
44         },
45         {
46                 .name           = "SMPS123",
47                 .sname          = "smps1-in",
48                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
49                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
50                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
51         },
52         {
53                 .name           = "SMPS3",
54                 .sname          = "smps3-in",
55                 .vsel_addr      = PALMAS_SMPS3_VOLTAGE,
56                 .ctrl_addr      = PALMAS_SMPS3_CTRL,
57         },
58         {
59                 .name           = "SMPS45",
60                 .sname          = "smps4-in",
61                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
62                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
63                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
64         },
65         {
66                 .name           = "SMPS457",
67                 .sname          = "smps4-in",
68                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
69                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
70                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
71         },
72         {
73                 .name           = "SMPS6",
74                 .sname          = "smps6-in",
75                 .vsel_addr      = PALMAS_SMPS6_VOLTAGE,
76                 .ctrl_addr      = PALMAS_SMPS6_CTRL,
77                 .tstep_addr     = PALMAS_SMPS6_TSTEP,
78         },
79         {
80                 .name           = "SMPS7",
81                 .sname          = "smps7-in",
82                 .vsel_addr      = PALMAS_SMPS7_VOLTAGE,
83                 .ctrl_addr      = PALMAS_SMPS7_CTRL,
84         },
85         {
86                 .name           = "SMPS8",
87                 .sname          = "smps8-in",
88                 .vsel_addr      = PALMAS_SMPS8_VOLTAGE,
89                 .ctrl_addr      = PALMAS_SMPS8_CTRL,
90                 .tstep_addr     = PALMAS_SMPS8_TSTEP,
91         },
92         {
93                 .name           = "SMPS9",
94                 .sname          = "smps9-in",
95                 .vsel_addr      = PALMAS_SMPS9_VOLTAGE,
96                 .ctrl_addr      = PALMAS_SMPS9_CTRL,
97         },
98         {
99                 .name           = "SMPS10",
100                 .sname          = "smps10-in",
101                 .ctrl_addr      = PALMAS_SMPS10_CTRL,
102         },
103         {
104                 .name           = "LDO1",
105                 .sname          = "ldo1-in",
106                 .vsel_addr      = PALMAS_LDO1_VOLTAGE,
107                 .ctrl_addr      = PALMAS_LDO1_CTRL,
108         },
109         {
110                 .name           = "LDO2",
111                 .sname          = "ldo2-in",
112                 .vsel_addr      = PALMAS_LDO2_VOLTAGE,
113                 .ctrl_addr      = PALMAS_LDO2_CTRL,
114         },
115         {
116                 .name           = "LDO3",
117                 .sname          = "ldo3-in",
118                 .vsel_addr      = PALMAS_LDO3_VOLTAGE,
119                 .ctrl_addr      = PALMAS_LDO3_CTRL,
120         },
121         {
122                 .name           = "LDO4",
123                 .sname          = "ldo4-in",
124                 .vsel_addr      = PALMAS_LDO4_VOLTAGE,
125                 .ctrl_addr      = PALMAS_LDO4_CTRL,
126         },
127         {
128                 .name           = "LDO5",
129                 .sname          = "ldo5-in",
130                 .vsel_addr      = PALMAS_LDO5_VOLTAGE,
131                 .ctrl_addr      = PALMAS_LDO5_CTRL,
132         },
133         {
134                 .name           = "LDO6",
135                 .sname          = "ldo6-in",
136                 .vsel_addr      = PALMAS_LDO6_VOLTAGE,
137                 .ctrl_addr      = PALMAS_LDO6_CTRL,
138         },
139         {
140                 .name           = "LDO7",
141                 .sname          = "ldo7-in",
142                 .vsel_addr      = PALMAS_LDO7_VOLTAGE,
143                 .ctrl_addr      = PALMAS_LDO7_CTRL,
144         },
145         {
146                 .name           = "LDO8",
147                 .sname          = "ldo8-in",
148                 .vsel_addr      = PALMAS_LDO8_VOLTAGE,
149                 .ctrl_addr      = PALMAS_LDO8_CTRL,
150         },
151         {
152                 .name           = "LDO9",
153                 .sname          = "ldo9-in",
154                 .vsel_addr      = PALMAS_LDO9_VOLTAGE,
155                 .ctrl_addr      = PALMAS_LDO9_CTRL,
156         },
157         {
158                 .name           = "LDOLN",
159                 .sname          = "ldoln-in",
160                 .vsel_addr      = PALMAS_LDOLN_VOLTAGE,
161                 .ctrl_addr      = PALMAS_LDOLN_CTRL,
162         },
163         {
164                 .name           = "LDOUSB",
165                 .sname          = "ldousb-in",
166                 .vsel_addr      = PALMAS_LDOUSB_VOLTAGE,
167                 .ctrl_addr      = PALMAS_LDOUSB_CTRL,
168         },
169         {
170                 .name           = "REGEN1",
171                 .ctrl_addr      = PALMAS_REGEN1_CTRL,
172         },
173         {
174                 .name           = "REGEN2",
175                 .ctrl_addr      = PALMAS_REGEN2_CTRL,
176         },
177         {
178                 .name           = "REGEN3",
179                 .ctrl_addr      = PALMAS_REGEN3_CTRL,
180         },
181         {
182                 .name           = "SYSEN1",
183                 .ctrl_addr      = PALMAS_SYSEN1_CTRL,
184         },
185         {
186                 .name           = "SYSEN2",
187                 .ctrl_addr      = PALMAS_SYSEN2_CTRL,
188         },
189 };
190
191 static unsigned int palmas_smps_ramp_delay[4] = {0, 10000, 5000, 2500};
192
193 #define SMPS_CTRL_MODE_OFF              0x00
194 #define SMPS_CTRL_MODE_ON               0x01
195 #define SMPS_CTRL_MODE_ECO              0x02
196 #define SMPS_CTRL_MODE_PWM              0x03
197
198 /* These values are derived from the data sheet. And are the number of steps
199  * where there is a voltage change, the ranges at beginning and end of register
200  * max/min values where there are no change are ommitted.
201  *
202  * So they are basically (maxV-minV)/stepV
203  */
204 #define PALMAS_SMPS_NUM_VOLTAGES        116
205 #define PALMAS_SMPS10_NUM_VOLTAGES      2
206 #define PALMAS_LDO_NUM_VOLTAGES         50
207
208 #define SMPS10_VSEL                     (1<<3)
209 #define SMPS10_BOOST_EN                 (1<<2)
210 #define SMPS10_BYPASS_EN                (1<<1)
211 #define SMPS10_SWITCH_EN                (1<<0)
212
213 #define REGULATOR_SLAVE                 0
214
215 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
216                 unsigned int *dest)
217 {
218         unsigned int addr;
219
220         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
221
222         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
223 }
224
225 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
226                 unsigned int value)
227 {
228         unsigned int addr;
229
230         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
231
232         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
233 }
234
235 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
236                 unsigned int *dest)
237 {
238         unsigned int addr;
239
240         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
241
242         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
243 }
244
245 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
246                 unsigned int value)
247 {
248         unsigned int addr;
249
250         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
251
252         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
253 }
254
255 static int palmas_is_enabled_smps(struct regulator_dev *dev)
256 {
257         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
258         int id = rdev_get_id(dev);
259         unsigned int reg;
260
261         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
262
263         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
264         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
265
266         return !!(reg);
267 }
268
269 static int palmas_enable_smps(struct regulator_dev *dev)
270 {
271         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
272         int id = rdev_get_id(dev);
273         unsigned int reg;
274
275         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
276
277         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
278         if (pmic->current_reg_mode[id])
279                 reg |= pmic->current_reg_mode[id];
280         else
281                 reg |= SMPS_CTRL_MODE_ON;
282
283         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
284
285         return 0;
286 }
287
288 static int palmas_disable_smps(struct regulator_dev *dev)
289 {
290         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
291         int id = rdev_get_id(dev);
292         unsigned int reg;
293
294         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
295
296         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
297
298         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
299
300         return 0;
301 }
302
303 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
304 {
305         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
306         int id = rdev_get_id(dev);
307         unsigned int reg;
308         bool rail_enable = true;
309
310         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
311         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
312
313         if (reg == SMPS_CTRL_MODE_OFF)
314                 rail_enable = false;
315
316         switch (mode) {
317         case REGULATOR_MODE_NORMAL:
318                 reg |= SMPS_CTRL_MODE_ON;
319                 break;
320         case REGULATOR_MODE_IDLE:
321                 reg |= SMPS_CTRL_MODE_ECO;
322                 break;
323         case REGULATOR_MODE_FAST:
324                 reg |= SMPS_CTRL_MODE_PWM;
325                 break;
326         default:
327                 return -EINVAL;
328         }
329
330         pmic->current_reg_mode[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
331         if (rail_enable)
332                 palmas_smps_write(pmic->palmas,
333                         palmas_regs_info[id].ctrl_addr, reg);
334         return 0;
335 }
336
337 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
338 {
339         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
340         int id = rdev_get_id(dev);
341         unsigned int reg;
342
343         reg = pmic->current_reg_mode[id] & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
344
345         switch (reg) {
346         case SMPS_CTRL_MODE_ON:
347                 return REGULATOR_MODE_NORMAL;
348         case SMPS_CTRL_MODE_ECO:
349                 return REGULATOR_MODE_IDLE;
350         case SMPS_CTRL_MODE_PWM:
351                 return REGULATOR_MODE_FAST;
352         }
353
354         return 0;
355 }
356
357 static int palmas_list_voltage_smps(struct regulator_dev *dev,
358                                         unsigned selector)
359 {
360         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
361         int id = rdev_get_id(dev);
362         int mult = 1;
363
364         /* Read the multiplier set in VSEL register to return
365          * the correct voltage.
366          */
367         if (pmic->range[id])
368                 mult = 2;
369
370         if (selector == 0)
371                 return 0;
372         else if (selector < 6)
373                 return 500000 * mult;
374         else
375                 /* Voltage is linear mapping starting from selector 6,
376                  * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
377                  * RANGE is either x1 or x2
378                  */
379                 return (490000 + ((selector - 5) * 10000)) * mult;
380 }
381
382 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
383                 int min_uV, int max_uV)
384 {
385         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
386         int id = rdev_get_id(rdev);
387         int ret, voltage;
388
389         if (min_uV == 0)
390                 return 0;
391
392         if (pmic->range[id]) { /* RANGE is x2 */
393                 if (min_uV < 1000000)
394                         min_uV = 1000000;
395                 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
396         } else {                /* RANGE is x1 */
397                 if (min_uV < 500000)
398                         min_uV = 500000;
399                 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
400         }
401
402         /* Map back into a voltage to verify we're still in bounds */
403         voltage = palmas_list_voltage_smps(rdev, ret);
404         if (voltage < min_uV || voltage > max_uV)
405                 return -EINVAL;
406
407         return ret;
408 }
409
410 static int palma_smps_set_voltage_smps_time_sel(struct regulator_dev *rdev,
411         unsigned int old_selector, unsigned int new_selector)
412 {
413         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
414         int id = rdev_get_id(rdev);
415         int old_uv, new_uv;
416         unsigned int ramp_delay = pmic->ramp_delay[id];
417
418         if (!ramp_delay)
419                 return 0;
420
421         old_uv = palmas_list_voltage_smps(rdev, old_selector);
422         if (old_uv < 0)
423                 return old_uv;
424
425         new_uv = palmas_list_voltage_smps(rdev, new_selector);
426         if (new_uv < 0)
427                 return new_uv;
428
429         return DIV_ROUND_UP(abs(old_uv - new_uv), ramp_delay);
430 }
431
432 static int palmas_smps_set_ramp_delay(struct regulator_dev *rdev,
433                  int ramp_delay)
434 {
435         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
436         int id = rdev_get_id(rdev);
437         unsigned int reg = 0;
438         unsigned int addr = palmas_regs_info[id].tstep_addr;
439         int ret;
440
441         /* SMPS3 and SMPS7 do not have tstep_addr setting */
442         switch (id) {
443         case PALMAS_REG_SMPS3:
444         case PALMAS_REG_SMPS7:
445                 return 0;
446         }
447
448         if (ramp_delay <= 0)
449                 reg = 0;
450         else if (ramp_delay <= 2500)
451                 reg = 3;
452         else if (ramp_delay <= 5000)
453                 reg = 2;
454         else
455                 reg = 1;
456
457         ret = palmas_smps_write(pmic->palmas, addr, reg);
458         if (ret < 0) {
459                 dev_err(pmic->palmas->dev, "TSTEP write failed: %d\n", ret);
460                 return ret;
461         }
462
463         pmic->ramp_delay[id] = palmas_smps_ramp_delay[reg];
464         return ret;
465 }
466
467 static struct regulator_ops palmas_ops_smps = {
468         .is_enabled             = palmas_is_enabled_smps,
469         .enable                 = palmas_enable_smps,
470         .disable                = palmas_disable_smps,
471         .set_mode               = palmas_set_mode_smps,
472         .get_mode               = palmas_get_mode_smps,
473         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
474         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
475         .list_voltage           = palmas_list_voltage_smps,
476         .map_voltage            = palmas_map_voltage_smps,
477         .set_voltage_time_sel   = palma_smps_set_voltage_smps_time_sel,
478         .set_ramp_delay         = palmas_smps_set_ramp_delay,
479 };
480
481 static struct regulator_ops palmas_ops_smps10 = {
482         .is_enabled             = regulator_is_enabled_regmap,
483         .enable                 = regulator_enable_regmap,
484         .disable                = regulator_disable_regmap,
485         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
486         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
487         .list_voltage           = regulator_list_voltage_linear,
488         .map_voltage            = regulator_map_voltage_linear,
489 };
490
491 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
492 {
493         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
494         int id = rdev_get_id(dev);
495         unsigned int reg;
496
497         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
498
499         reg &= PALMAS_LDO1_CTRL_STATUS;
500
501         return !!(reg);
502 }
503
504 static struct regulator_ops palmas_ops_ldo = {
505         .is_enabled             = palmas_is_enabled_ldo,
506         .enable                 = regulator_enable_regmap,
507         .disable                = regulator_disable_regmap,
508         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
509         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
510         .list_voltage           = regulator_list_voltage_linear,
511         .map_voltage            = regulator_map_voltage_linear,
512 };
513
514 static struct regulator_ops palmas_ops_extreg = {
515         .is_enabled             = regulator_is_enabled_regmap,
516         .enable                 = regulator_enable_regmap,
517         .disable                = regulator_disable_regmap,
518 };
519
520 /*
521  * setup the hardware based sleep configuration of the SMPS/LDO regulators
522  * from the platform data. This is different to the software based control
523  * supported by the regulator framework as it is controlled by toggling
524  * pins on the PMIC such as PREQ, SYSEN, ...
525  */
526 static int palmas_smps_init(struct palmas *palmas, int id,
527                 struct palmas_reg_init *reg_init)
528 {
529         unsigned int reg;
530         unsigned int addr;
531         int ret;
532
533         addr = palmas_regs_info[id].ctrl_addr;
534
535         ret = palmas_smps_read(palmas, addr, &reg);
536         if (ret)
537                 return ret;
538
539         switch (id) {
540         case PALMAS_REG_SMPS10:
541                 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
542                 if (reg_init->mode_sleep)
543                         reg |= reg_init->mode_sleep <<
544                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
545                 break;
546         default:
547                 if (reg_init->warm_reset)
548                         reg |= PALMAS_SMPS12_CTRL_WR_S;
549                 else
550                         reg &= ~PALMAS_SMPS12_CTRL_WR_S;
551
552                 if (reg_init->roof_floor)
553                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
554                 else
555                         reg &= ~PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
556
557                 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
558                 if (reg_init->mode_sleep)
559                         reg |= reg_init->mode_sleep <<
560                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
561         }
562
563         ret = palmas_smps_write(palmas, addr, reg);
564         if (ret)
565                 return ret;
566
567         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
568                 addr = palmas_regs_info[id].vsel_addr;
569
570                 reg = reg_init->vsel;
571
572                 ret = palmas_smps_write(palmas, addr, reg);
573                 if (ret)
574                         return ret;
575         }
576
577
578         return 0;
579 }
580
581 static int palmas_ldo_init(struct palmas *palmas, int id,
582                 struct palmas_reg_init *reg_init)
583 {
584         unsigned int reg;
585         unsigned int addr;
586         int ret;
587
588         addr = palmas_regs_info[id].ctrl_addr;
589
590         ret = palmas_ldo_read(palmas, addr, &reg);
591         if (ret)
592                 return ret;
593
594         if (reg_init->warm_reset)
595                 reg |= PALMAS_LDO1_CTRL_WR_S;
596         else
597                 reg &= ~PALMAS_LDO1_CTRL_WR_S;
598
599         if (reg_init->mode_sleep)
600                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
601         else
602                 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
603
604         ret = palmas_ldo_write(palmas, addr, reg);
605         if (ret)
606                 return ret;
607
608         return 0;
609 }
610
611 static int palmas_extreg_init(struct palmas *palmas, int id,
612                 struct palmas_reg_init *reg_init)
613 {
614         unsigned int addr;
615         int ret;
616         unsigned int val = 0;
617
618         addr = palmas_regs_info[id].ctrl_addr;
619
620         if (reg_init->mode_sleep)
621                 val = PALMAS_REGEN1_CTRL_MODE_SLEEP;
622
623         ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
624                         addr, PALMAS_REGEN1_CTRL_MODE_SLEEP, val);
625         if (ret < 0) {
626                 dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n",
627                         addr, ret);
628                 return ret;
629         }
630         return 0;
631 }
632
633 static void palmas_enable_ldo8_track(struct palmas *palmas)
634 {
635         unsigned int reg;
636         unsigned int addr;
637         int ret;
638
639         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
640
641         ret = palmas_ldo_read(palmas, addr, &reg);
642         if (ret) {
643                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
644                 return;
645         }
646
647         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
648         ret = palmas_ldo_write(palmas, addr, reg);
649         if (ret < 0) {
650                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
651                 return;
652         }
653         /*
654          * When SMPS45 is set to off and LDO8 tracking is enabled, the LDO8
655          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
656          * and can be set from 0.45 to 1.65 V.
657          */
658         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
659         ret = palmas_ldo_read(palmas, addr, &reg);
660         if (ret) {
661                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
662                 return;
663         }
664
665         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
666         ret = palmas_ldo_write(palmas, addr, reg);
667         if (ret < 0)
668                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
669
670         return;
671 }
672
673 static struct of_regulator_match palmas_matches[] = {
674         { .name = "smps12", },
675         { .name = "smps123", },
676         { .name = "smps3", },
677         { .name = "smps45", },
678         { .name = "smps457", },
679         { .name = "smps6", },
680         { .name = "smps7", },
681         { .name = "smps8", },
682         { .name = "smps9", },
683         { .name = "smps10", },
684         { .name = "ldo1", },
685         { .name = "ldo2", },
686         { .name = "ldo3", },
687         { .name = "ldo4", },
688         { .name = "ldo5", },
689         { .name = "ldo6", },
690         { .name = "ldo7", },
691         { .name = "ldo8", },
692         { .name = "ldo9", },
693         { .name = "ldoln", },
694         { .name = "ldousb", },
695         { .name = "regen1", },
696         { .name = "regen2", },
697         { .name = "regen3", },
698         { .name = "sysen1", },
699         { .name = "sysen2", },
700 };
701
702 static void palmas_dt_to_pdata(struct device *dev,
703                 struct device_node *node,
704                 struct palmas_pmic_platform_data *pdata)
705 {
706         struct device_node *regulators;
707         u32 prop;
708         int idx, ret;
709
710         node = of_node_get(node);
711         regulators = of_find_node_by_name(node, "regulators");
712         if (!regulators) {
713                 dev_info(dev, "regulator node not found\n");
714                 return;
715         }
716
717         ret = of_regulator_match(dev, regulators, palmas_matches,
718                         PALMAS_NUM_REGS);
719         of_node_put(regulators);
720         if (ret < 0) {
721                 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
722                 return;
723         }
724
725         for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
726                 if (!palmas_matches[idx].init_data ||
727                                 !palmas_matches[idx].of_node)
728                         continue;
729
730                 pdata->reg_data[idx] = palmas_matches[idx].init_data;
731
732                 pdata->reg_init[idx] = devm_kzalloc(dev,
733                                 sizeof(struct palmas_reg_init), GFP_KERNEL);
734
735                 pdata->reg_init[idx]->warm_reset =
736                         of_property_read_bool(palmas_matches[idx].of_node,
737                                              "ti,warm-reset");
738
739                 pdata->reg_init[idx]->roof_floor =
740                         of_property_read_bool(palmas_matches[idx].of_node,
741                                               "ti,roof-floor");
742
743                 ret = of_property_read_u32(palmas_matches[idx].of_node,
744                                 "ti,mode-sleep", &prop);
745                 if (!ret)
746                         pdata->reg_init[idx]->mode_sleep = prop;
747
748                 ret = of_property_read_bool(palmas_matches[idx].of_node,
749                                             "ti,smps-range");
750                 if (ret)
751                         pdata->reg_init[idx]->vsel =
752                                 PALMAS_SMPS12_VOLTAGE_RANGE;
753
754                 if (idx == PALMAS_REG_LDO8)
755                         pdata->enable_ldo8_tracking = of_property_read_bool(
756                                                 palmas_matches[idx].of_node,
757                                                 "ti,enable-ldo8-tracking");
758         }
759
760         pdata->ldo6_vibrator = of_property_read_bool(node, "ti,ldo6-vibrator");
761 }
762
763
764 static int palmas_regulators_probe(struct platform_device *pdev)
765 {
766         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
767         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
768         struct device_node *node = pdev->dev.of_node;
769         struct regulator_dev *rdev;
770         struct regulator_config config = { };
771         struct palmas_pmic *pmic;
772         struct palmas_reg_init *reg_init;
773         int id = 0, ret;
774         unsigned int addr, reg;
775
776         if (node && !pdata) {
777                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
778
779                 if (!pdata)
780                         return -ENOMEM;
781
782                 palmas_dt_to_pdata(&pdev->dev, node, pdata);
783         }
784
785         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
786         if (!pmic)
787                 return -ENOMEM;
788
789         pmic->dev = &pdev->dev;
790         pmic->palmas = palmas;
791         palmas->pmic = pmic;
792         platform_set_drvdata(pdev, pmic);
793
794         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
795         if (ret)
796                 return ret;
797
798         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
799                 pmic->smps123 = 1;
800
801         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
802                 pmic->smps457 = 1;
803
804         config.regmap = palmas->regmap[REGULATOR_SLAVE];
805         config.dev = &pdev->dev;
806         config.driver_data = pmic;
807
808         for (id = 0; id < PALMAS_REG_LDO1; id++) {
809                 bool ramp_delay_support = false;
810
811                 /*
812                  * Miss out regulators which are not available due
813                  * to slaving configurations.
814                  */
815                 switch (id) {
816                 case PALMAS_REG_SMPS12:
817                 case PALMAS_REG_SMPS3:
818                         if (pmic->smps123)
819                                 continue;
820                         if (id == PALMAS_REG_SMPS12)
821                                 ramp_delay_support = true;
822                         break;
823                 case PALMAS_REG_SMPS123:
824                         if (!pmic->smps123)
825                                 continue;
826                         ramp_delay_support = true;
827                         break;
828                 case PALMAS_REG_SMPS45:
829                 case PALMAS_REG_SMPS7:
830                         if (pmic->smps457)
831                                 continue;
832                         if (id == PALMAS_REG_SMPS45)
833                                 ramp_delay_support = true;
834                         break;
835                 case PALMAS_REG_SMPS457:
836                         if (!pmic->smps457)
837                                 continue;
838                         ramp_delay_support = true;
839                         break;
840                 }
841
842                 if ((id == PALMAS_REG_SMPS6) && (id == PALMAS_REG_SMPS8))
843                         ramp_delay_support = true;
844
845                 if (ramp_delay_support) {
846                         addr = palmas_regs_info[id].tstep_addr;
847                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
848                         if (ret < 0) {
849                                 dev_err(&pdev->dev,
850                                         "reading TSTEP reg failed: %d\n", ret);
851                                 goto err_unregister_regulator;
852                         }
853                         pmic->desc[id].ramp_delay =
854                                         palmas_smps_ramp_delay[reg & 0x3];
855                         pmic->ramp_delay[id] = pmic->desc[id].ramp_delay;
856                 }
857
858                 /* Initialise sleep/init values from platform data */
859                 if (pdata && pdata->reg_init[id]) {
860                         reg_init = pdata->reg_init[id];
861                         ret = palmas_smps_init(palmas, id, reg_init);
862                         if (ret)
863                                 goto err_unregister_regulator;
864                 }
865
866                 /* Register the regulators */
867                 pmic->desc[id].name = palmas_regs_info[id].name;
868                 pmic->desc[id].id = id;
869
870                 switch (id) {
871                 case PALMAS_REG_SMPS10:
872                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
873                         pmic->desc[id].ops = &palmas_ops_smps10;
874                         pmic->desc[id].vsel_reg =
875                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
876                                                         PALMAS_SMPS10_CTRL);
877                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
878                         pmic->desc[id].enable_reg =
879                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
880                                                         PALMAS_SMPS10_STATUS);
881                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
882                         pmic->desc[id].min_uV = 3750000;
883                         pmic->desc[id].uV_step = 1250000;
884                         break;
885                 default:
886                         /*
887                          * Read and store the RANGE bit for later use
888                          * This must be done before regulator is probed,
889                          * otherwise we error in probe with unsupportable
890                          * ranges. Read the current smps mode for later use.
891                          */
892                         addr = palmas_regs_info[id].vsel_addr;
893
894                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
895                         if (ret)
896                                 goto err_unregister_regulator;
897                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
898                                 pmic->range[id] = 1;
899
900                         pmic->desc[id].ops = &palmas_ops_smps;
901                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
902                         pmic->desc[id].vsel_reg =
903                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
904                                                 palmas_regs_info[id].vsel_addr);
905                         pmic->desc[id].vsel_mask =
906                                         PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
907
908                         /* Read the smps mode for later use. */
909                         addr = palmas_regs_info[id].ctrl_addr;
910                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
911                         if (ret)
912                                 goto err_unregister_regulator;
913                         pmic->current_reg_mode[id] = reg &
914                                         PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
915                 }
916
917                 pmic->desc[id].type = REGULATOR_VOLTAGE;
918                 pmic->desc[id].owner = THIS_MODULE;
919
920                 if (pdata)
921                         config.init_data = pdata->reg_data[id];
922                 else
923                         config.init_data = NULL;
924
925                 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
926                 config.of_node = palmas_matches[id].of_node;
927
928                 rdev = regulator_register(&pmic->desc[id], &config);
929                 if (IS_ERR(rdev)) {
930                         dev_err(&pdev->dev,
931                                 "failed to register %s regulator\n",
932                                 pdev->name);
933                         ret = PTR_ERR(rdev);
934                         goto err_unregister_regulator;
935                 }
936
937                 /* Save regulator for cleanup */
938                 pmic->rdev[id] = rdev;
939         }
940
941         /* Start this loop from the id left from previous loop */
942         for (; id < PALMAS_NUM_REGS; id++) {
943
944                 /* Miss out regulators which are not available due
945                  * to alternate functions.
946                  */
947
948                 /* Register the regulators */
949                 pmic->desc[id].name = palmas_regs_info[id].name;
950                 pmic->desc[id].id = id;
951                 pmic->desc[id].type = REGULATOR_VOLTAGE;
952                 pmic->desc[id].owner = THIS_MODULE;
953
954                 if (id < PALMAS_REG_REGEN1) {
955                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
956                         pmic->desc[id].ops = &palmas_ops_ldo;
957                         pmic->desc[id].min_uV = 900000;
958                         pmic->desc[id].uV_step = 50000;
959                         pmic->desc[id].linear_min_sel = 1;
960                         pmic->desc[id].vsel_reg =
961                                         PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
962                                                 palmas_regs_info[id].vsel_addr);
963                         pmic->desc[id].vsel_mask =
964                                         PALMAS_LDO1_VOLTAGE_VSEL_MASK;
965                         pmic->desc[id].enable_reg =
966                                         PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
967                                                 palmas_regs_info[id].ctrl_addr);
968                         pmic->desc[id].enable_mask =
969                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
970
971                         /* Check if LDO8 is in tracking mode or not */
972                         if (pdata && (id == PALMAS_REG_LDO8) &&
973                                         pdata->enable_ldo8_tracking) {
974                                 palmas_enable_ldo8_track(palmas);
975                                 pmic->desc[id].min_uV = 450000;
976                                 pmic->desc[id].uV_step = 25000;
977                         }
978                 } else {
979                         pmic->desc[id].n_voltages = 1;
980                         pmic->desc[id].ops = &palmas_ops_extreg;
981                         pmic->desc[id].enable_reg =
982                                         PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE,
983                                                 palmas_regs_info[id].ctrl_addr);
984                         pmic->desc[id].enable_mask =
985                                         PALMAS_REGEN1_CTRL_MODE_ACTIVE;
986                 }
987
988                 if (pdata)
989                         config.init_data = pdata->reg_data[id];
990                 else
991                         config.init_data = NULL;
992
993                 pmic->desc[id].supply_name = palmas_regs_info[id].sname;
994                 config.of_node = palmas_matches[id].of_node;
995
996                 rdev = regulator_register(&pmic->desc[id], &config);
997                 if (IS_ERR(rdev)) {
998                         dev_err(&pdev->dev,
999                                 "failed to register %s regulator\n",
1000                                 pdev->name);
1001                         ret = PTR_ERR(rdev);
1002                         goto err_unregister_regulator;
1003                 }
1004
1005                 /* Save regulator for cleanup */
1006                 pmic->rdev[id] = rdev;
1007
1008                 /* Initialise sleep/init values from platform data */
1009                 if (pdata) {
1010                         reg_init = pdata->reg_init[id];
1011                         if (reg_init) {
1012                                 if (id < PALMAS_REG_REGEN1)
1013                                         ret = palmas_ldo_init(palmas,
1014                                                         id, reg_init);
1015                                 else
1016                                         ret = palmas_extreg_init(palmas,
1017                                                         id, reg_init);
1018                                 if (ret) {
1019                                         regulator_unregister(pmic->rdev[id]);
1020                                         goto err_unregister_regulator;
1021                                 }
1022                         }
1023                 }
1024         }
1025
1026
1027         return 0;
1028
1029 err_unregister_regulator:
1030         while (--id >= 0)
1031                 regulator_unregister(pmic->rdev[id]);
1032         return ret;
1033 }
1034
1035 static int palmas_regulators_remove(struct platform_device *pdev)
1036 {
1037         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1038         int id;
1039
1040         for (id = 0; id < PALMAS_NUM_REGS; id++)
1041                 regulator_unregister(pmic->rdev[id]);
1042         return 0;
1043 }
1044
1045 static struct of_device_id of_palmas_match_tbl[] = {
1046         { .compatible = "ti,palmas-pmic", },
1047         { .compatible = "ti,twl6035-pmic", },
1048         { .compatible = "ti,twl6036-pmic", },
1049         { .compatible = "ti,twl6037-pmic", },
1050         { .compatible = "ti,tps65913-pmic", },
1051         { .compatible = "ti,tps65914-pmic", },
1052         { .compatible = "ti,tps80036-pmic", },
1053         { /* end */ }
1054 };
1055
1056 static struct platform_driver palmas_driver = {
1057         .driver = {
1058                 .name = "palmas-pmic",
1059                 .of_match_table = of_palmas_match_tbl,
1060                 .owner = THIS_MODULE,
1061         },
1062         .probe = palmas_regulators_probe,
1063         .remove = palmas_regulators_remove,
1064 };
1065
1066 static int __init palmas_init(void)
1067 {
1068         return platform_driver_register(&palmas_driver);
1069 }
1070 subsys_initcall(palmas_init);
1071
1072 static void __exit palmas_exit(void)
1073 {
1074         platform_driver_unregister(&palmas_driver);
1075 }
1076 module_exit(palmas_exit);
1077
1078 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1079 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1080 MODULE_LICENSE("GPL");
1081 MODULE_ALIAS("platform:palmas-pmic");
1082 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);