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