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