regulator: MAX8997/8966 support
[linux-2.6.git] / drivers / regulator / max8997.c
1 /*
2  * max8997.c - Regulator driver for the Maxim 8997/8966
3  *
4  * Copyright (C) 2011 Samsung Electronics
5  * MyungJoo Ham <myungjoo.ham@smasung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * This driver is based on max8998.c
22  */
23
24 #include <linux/bug.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/gpio.h>
28 #include <linux/slab.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/machine.h>
32 #include <linux/mfd/max8997.h>
33 #include <linux/mfd/max8997-private.h>
34
35 struct max8997_data {
36         struct device *dev;
37         struct max8997_dev *iodev;
38         int num_regulators;
39         struct regulator_dev **rdev;
40         int ramp_delay; /* in mV/us */
41
42         u8 buck1_vol[8];
43         u8 buck2_vol[8];
44         u8 buck5_vol[8];
45         int buck125_gpioindex;
46
47         u8 saved_states[MAX8997_REG_MAX];
48 };
49
50 static inline void max8997_set_gpio(struct max8997_data *max8997)
51 {
52         struct max8997_platform_data *pdata =
53                 dev_get_platdata(max8997->iodev->dev);
54         int set3 = (max8997->buck125_gpioindex) & 0x1;
55         int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1;
56         int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1;
57
58         gpio_set_value(pdata->buck125_gpios[0], set1);
59         gpio_set_value(pdata->buck125_gpios[1], set2);
60         gpio_set_value(pdata->buck125_gpios[2], set3);
61 }
62
63 struct voltage_map_desc {
64         int min;
65         int max;
66         int step;
67         unsigned int n_bits;
68 };
69
70 /* Voltage maps in mV */
71 static const struct voltage_map_desc ldo_voltage_map_desc = {
72         .min = 800,     .max = 3950,    .step = 50,     .n_bits = 6,
73 }; /* LDO1 ~ 18, 21 all */
74
75 static const struct voltage_map_desc buck1245_voltage_map_desc = {
76         .min = 650,     .max = 2225,    .step = 25,     .n_bits = 6,
77 }; /* Buck1, 2, 4, 5 */
78
79 static const struct voltage_map_desc buck37_voltage_map_desc = {
80         .min = 750,     .max = 3900,    .step = 50,     .n_bits = 6,
81 }; /* Buck3, 7 */
82
83 /* current map in mA */
84 static const struct voltage_map_desc charger_current_map_desc = {
85         .min = 200,     .max = 950,     .step = 50,     .n_bits = 4,
86 };
87
88 static const struct voltage_map_desc topoff_current_map_desc = {
89         .min = 50,      .max = 200,     .step = 10,     .n_bits = 4,
90 };
91
92 static const struct voltage_map_desc *reg_voltage_map[] = {
93         [MAX8997_LDO1] = &ldo_voltage_map_desc,
94         [MAX8997_LDO2] = &ldo_voltage_map_desc,
95         [MAX8997_LDO3] = &ldo_voltage_map_desc,
96         [MAX8997_LDO4] = &ldo_voltage_map_desc,
97         [MAX8997_LDO5] = &ldo_voltage_map_desc,
98         [MAX8997_LDO6] = &ldo_voltage_map_desc,
99         [MAX8997_LDO7] = &ldo_voltage_map_desc,
100         [MAX8997_LDO8] = &ldo_voltage_map_desc,
101         [MAX8997_LDO9] = &ldo_voltage_map_desc,
102         [MAX8997_LDO10] = &ldo_voltage_map_desc,
103         [MAX8997_LDO11] = &ldo_voltage_map_desc,
104         [MAX8997_LDO12] = &ldo_voltage_map_desc,
105         [MAX8997_LDO13] = &ldo_voltage_map_desc,
106         [MAX8997_LDO14] = &ldo_voltage_map_desc,
107         [MAX8997_LDO15] = &ldo_voltage_map_desc,
108         [MAX8997_LDO16] = &ldo_voltage_map_desc,
109         [MAX8997_LDO17] = &ldo_voltage_map_desc,
110         [MAX8997_LDO18] = &ldo_voltage_map_desc,
111         [MAX8997_LDO21] = &ldo_voltage_map_desc,
112         [MAX8997_BUCK1] = &buck1245_voltage_map_desc,
113         [MAX8997_BUCK2] = &buck1245_voltage_map_desc,
114         [MAX8997_BUCK3] = &buck37_voltage_map_desc,
115         [MAX8997_BUCK4] = &buck1245_voltage_map_desc,
116         [MAX8997_BUCK5] = &buck1245_voltage_map_desc,
117         [MAX8997_BUCK6] = NULL,
118         [MAX8997_BUCK7] = &buck37_voltage_map_desc,
119         [MAX8997_EN32KHZ_AP] = NULL,
120         [MAX8997_EN32KHZ_CP] = NULL,
121         [MAX8997_ENVICHG] = NULL,
122         [MAX8997_ESAFEOUT1] = NULL,
123         [MAX8997_ESAFEOUT2] = NULL,
124         [MAX8997_CHARGER_CV] = NULL,
125         [MAX8997_CHARGER] = &charger_current_map_desc,
126         [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc,
127 };
128
129 static inline int max8997_get_rid(struct regulator_dev *rdev)
130 {
131         return rdev_get_id(rdev);
132 }
133
134 static int max8997_list_voltage_safeout(struct regulator_dev *rdev,
135                 unsigned int selector)
136 {
137         int rid = max8997_get_rid(rdev);
138
139         if (rid == MAX8997_ESAFEOUT1 || rid == MAX8997_ESAFEOUT2) {
140                 switch (selector) {
141                 case 0:
142                         return 4850000;
143                 case 1:
144                         return 4900000;
145                 case 2:
146                         return 4950000;
147                 case 3:
148                         return 3300000;
149                 default:
150                         return -EINVAL;
151                 }
152         }
153
154         return -EINVAL;
155 }
156
157 static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev,
158                 unsigned int selector)
159 {
160         int rid = max8997_get_rid(rdev);
161
162         if (rid != MAX8997_CHARGER_CV)
163                 goto err;
164
165         switch (selector) {
166         case 0x00:
167                 return 4200000;
168         case 0x01 ... 0x0E:
169                 return 4000000 + 20000 * (selector - 0x01);
170         case 0x0F:
171                 return 4350000;
172         default:
173                 return -EINVAL;
174         }
175 err:
176         return -EINVAL;
177 }
178
179 static int max8997_list_voltage(struct regulator_dev *rdev,
180                 unsigned int selector)
181 {
182         const struct voltage_map_desc *desc;
183         int rid = max8997_get_rid(rdev);
184         int val;
185
186         if (rid >= ARRAY_SIZE(reg_voltage_map) ||
187                         rid < 0)
188                 return -EINVAL;
189
190         desc = reg_voltage_map[rid];
191         if (desc == NULL)
192                 return -EINVAL;
193
194         val = desc->min + desc->step * selector;
195         if (val > desc->max)
196                 return -EINVAL;
197
198         return val * 1000;
199 }
200
201 static int max8997_get_enable_register(struct regulator_dev *rdev,
202                 int *reg, int *mask, int *pattern)
203 {
204         int rid = max8997_get_rid(rdev);
205
206         switch (rid) {
207         case MAX8997_LDO1 ... MAX8997_LDO21:
208                 *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
209                 *mask = 0xC0;
210                 *pattern = 0xC0;
211                 break;
212         case MAX8997_BUCK1:
213                 *reg = MAX8997_REG_BUCK1CTRL;
214                 *mask = 0x01;
215                 *pattern = 0x01;
216                 break;
217         case MAX8997_BUCK2:
218                 *reg = MAX8997_REG_BUCK2CTRL;
219                 *mask = 0x01;
220                 *pattern = 0x01;
221                 break;
222         case MAX8997_BUCK3:
223                 *reg = MAX8997_REG_BUCK3CTRL;
224                 *mask = 0x01;
225                 *pattern = 0x01;
226                 break;
227         case MAX8997_BUCK4:
228                 *reg = MAX8997_REG_BUCK4CTRL;
229                 *mask = 0x01;
230                 *pattern = 0x01;
231                 break;
232         case MAX8997_BUCK5:
233                 *reg = MAX8997_REG_BUCK5CTRL;
234                 *mask = 0x01;
235                 *pattern = 0x01;
236                 break;
237         case MAX8997_BUCK6:
238                 *reg = MAX8997_REG_BUCK6CTRL;
239                 *mask = 0x01;
240                 *pattern = 0x01;
241                 break;
242         case MAX8997_BUCK7:
243                 *reg = MAX8997_REG_BUCK7CTRL;
244                 *mask = 0x01;
245                 *pattern = 0x01;
246                 break;
247         case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP:
248                 *reg = MAX8997_REG_MAINCON1;
249                 *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP);
250                 *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP);
251                 break;
252         case MAX8997_ENVICHG:
253                 *reg = MAX8997_REG_MBCCTRL1;
254                 *mask = 0x80;
255                 *pattern = 0x80;
256                 break;
257         case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2:
258                 *reg = MAX8997_REG_SAFEOUTCTRL;
259                 *mask = 0x40 << (rid - MAX8997_ESAFEOUT1);
260                 *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1);
261                 break;
262         case MAX8997_CHARGER:
263                 *reg = MAX8997_REG_MBCCTRL2;
264                 *mask = 0x40;
265                 *pattern = 0x40;
266                 break;
267         default:
268                 /* Not controllable or not exists */
269                 return -EINVAL;
270                 break;
271         }
272
273         return 0;
274 }
275
276 static int max8997_reg_is_enabled(struct regulator_dev *rdev)
277 {
278         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
279         struct i2c_client *i2c = max8997->iodev->i2c;
280         int ret, reg, mask, pattern;
281         u8 val;
282
283         ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
284         if (ret == -EINVAL)
285                 return 1; /* "not controllable" */
286         else if (ret)
287                 return ret;
288
289         ret = max8997_read_reg(i2c, reg, &val);
290         if (ret)
291                 return ret;
292
293         return (val & mask) == pattern;
294 }
295
296 static int max8997_reg_enable(struct regulator_dev *rdev)
297 {
298         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
299         struct i2c_client *i2c = max8997->iodev->i2c;
300         int ret, reg, mask, pattern;
301
302         ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
303         if (ret)
304                 return ret;
305
306         return max8997_update_reg(i2c, reg, pattern, mask);
307 }
308
309 static int max8997_reg_disable(struct regulator_dev *rdev)
310 {
311         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
312         struct i2c_client *i2c = max8997->iodev->i2c;
313         int ret, reg, mask, pattern;
314
315         ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
316         if (ret)
317                 return ret;
318
319         return max8997_update_reg(i2c, reg, ~pattern, mask);
320 }
321
322 static int max8997_get_voltage_register(struct regulator_dev *rdev,
323                 int *_reg, int *_shift, int *_mask)
324 {
325         int rid = max8997_get_rid(rdev);
326         int reg, shift = 0, mask = 0x3f;
327
328         switch (rid) {
329         case MAX8997_LDO1 ... MAX8997_LDO21:
330                 reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1);
331                 break;
332         case MAX8997_BUCK1:
333                 reg = MAX8997_REG_BUCK1DVS1;
334                 break;
335         case MAX8997_BUCK2:
336                 reg = MAX8997_REG_BUCK2DVS1;
337                 break;
338         case MAX8997_BUCK3:
339                 reg = MAX8997_REG_BUCK3DVS;
340                 break;
341         case MAX8997_BUCK4:
342                 reg = MAX8997_REG_BUCK4DVS;
343                 break;
344         case MAX8997_BUCK5:
345                 reg = MAX8997_REG_BUCK5DVS1;
346                 break;
347         case MAX8997_BUCK7:
348                 reg = MAX8997_REG_BUCK7DVS;
349                 break;
350         case MAX8997_ESAFEOUT1 ...  MAX8997_ESAFEOUT2:
351                 reg = MAX8997_REG_SAFEOUTCTRL;
352                 shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0;
353                 mask = 0x3;
354                 break;
355         case MAX8997_CHARGER_CV:
356                 reg = MAX8997_REG_MBCCTRL3;
357                 shift = 0;
358                 mask = 0xf;
359                 break;
360         case MAX8997_CHARGER:
361                 reg = MAX8997_REG_MBCCTRL4;
362                 shift = 0;
363                 mask = 0xf;
364                 break;
365         case MAX8997_CHARGER_TOPOFF:
366                 reg = MAX8997_REG_MBCCTRL5;
367                 shift = 0;
368                 mask = 0xf;
369                 break;
370         default:
371                 return -EINVAL;
372         }
373
374         *_reg = reg;
375         *_shift = shift;
376         *_mask = mask;
377
378         return 0;
379 }
380
381 static int max8997_get_voltage(struct regulator_dev *rdev)
382 {
383         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
384         struct max8997_platform_data *pdata =
385                 dev_get_platdata(max8997->iodev->dev);
386         struct i2c_client *i2c = max8997->iodev->i2c;
387         int reg, shift, mask, ret;
388         int rid = max8997_get_rid(rdev);
389         u8 val;
390
391         ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
392         if (ret)
393                 return ret;
394
395         if ((rid == MAX8997_BUCK1 && pdata->buck1_gpiodvs) ||
396                         (rid == MAX8997_BUCK2 && pdata->buck2_gpiodvs) ||
397                         (rid == MAX8997_BUCK5 && pdata->buck5_gpiodvs))
398                 reg += max8997->buck125_gpioindex;
399
400         ret = max8997_read_reg(i2c, reg, &val);
401         if (ret)
402                 return ret;
403
404         val >>= shift;
405         val &= mask;
406
407         if (rdev->desc && rdev->desc->ops && rdev->desc->ops->list_voltage)
408                 return rdev->desc->ops->list_voltage(rdev, val);
409
410         /*
411          * max8997_list_voltage returns value for any rdev with voltage_map,
412          * which works for "CHARGER" and "CHARGER TOPOFF" that do not have
413          * list_voltage ops (they are current regulators).
414          */
415         return max8997_list_voltage(rdev, val);
416 }
417
418 static inline int max8997_get_voltage_proper_val(
419                 const struct voltage_map_desc *desc,
420                 int min_vol, int max_vol)
421 {
422         int i = 0;
423
424         if (desc == NULL)
425                 return -EINVAL;
426
427         if (max_vol < desc->min || min_vol > desc->max)
428                 return -EINVAL;
429
430         while (desc->min + desc->step * i < min_vol &&
431                         desc->min + desc->step * i < desc->max)
432                 i++;
433
434         if (desc->min + desc->step * i > max_vol)
435                 return -EINVAL;
436
437         if (i >= (1 << desc->n_bits))
438                 return -EINVAL;
439
440         return i;
441 }
442
443 static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev,
444                 int min_uV, int max_uV, unsigned *selector)
445 {
446         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
447         struct i2c_client *i2c = max8997->iodev->i2c;
448         int rid = max8997_get_rid(rdev);
449         int lb, ub;
450         int reg, shift = 0, mask, ret = 0;
451         u8 val = 0x0;
452
453         if (rid != MAX8997_CHARGER_CV)
454                 return -EINVAL;
455
456         ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
457         if (ret)
458                 return ret;
459
460         if (max_uV < 4000000 || min_uV > 4350000)
461                 return -EINVAL;
462
463         if (min_uV <= 4000000) {
464                 if (max_uV >= 4000000)
465                         return -EINVAL;
466                 else
467                         val = 0x1;
468         } else if (min_uV <= 4200000 && max_uV >= 4200000)
469                 val = 0x0;
470         else {
471                 lb = (min_uV - 4000001) / 20000 + 2;
472                 ub = (max_uV - 4000000) / 20000 + 1;
473
474                 if (lb > ub)
475                         return -EINVAL;
476
477                 if (lb < 0xf)
478                         val = lb;
479                 else {
480                         if (ub >= 0xf)
481                                 val = 0xf;
482                         else
483                                 return -EINVAL;
484                 }
485         }
486
487         *selector = val;
488
489         ret = max8997_update_reg(i2c, reg, val << shift, mask);
490
491         return ret;
492 }
493
494 /*
495  * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF
496  * BUCK1, 2, and 5 are available if they are not controlled by gpio
497  */
498 static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev,
499                 int min_uV, int max_uV, unsigned *selector)
500 {
501         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
502         struct i2c_client *i2c = max8997->iodev->i2c;
503         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
504         const struct voltage_map_desc *desc;
505         int rid = max8997_get_rid(rdev);
506         int reg, shift = 0, mask, ret;
507         int i;
508         u8 org;
509
510         switch (rid) {
511         case MAX8997_LDO1 ... MAX8997_LDO21:
512                 break;
513         case MAX8997_BUCK1 ... MAX8997_BUCK5:
514                 break;
515         case MAX8997_BUCK6:
516                 return -EINVAL;
517         case MAX8997_BUCK7:
518                 break;
519         case MAX8997_CHARGER:
520                 break;
521         case MAX8997_CHARGER_TOPOFF:
522                 break;
523         default:
524                 return -EINVAL;
525         }
526
527         desc = reg_voltage_map[rid];
528
529         i = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
530         if (i < 0)
531                 return i;
532
533         ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
534         if (ret)
535                 return ret;
536
537         max8997_read_reg(i2c, reg, &org);
538         org = (org & mask) >> shift;
539
540         ret = max8997_update_reg(i2c, reg, i << shift, mask << shift);
541         *selector = i;
542
543         if (rid == MAX8997_BUCK1 || rid == MAX8997_BUCK2 ||
544                         rid == MAX8997_BUCK4 || rid == MAX8997_BUCK5) {
545                 /* If the voltage is increasing */
546                 if (org < i)
547                         udelay(desc->step * (i - org) / max8997->ramp_delay);
548         }
549
550         return ret;
551 }
552
553 /*
554  * Assess the damage on the voltage setting of BUCK1,2,5 by the change.
555  *
556  * When GPIO-DVS mode is used for multiple bucks, changing the voltage value
557  * of one of the bucks may affect that of another buck, which is the side
558  * effect of the change (set_voltage). This function examines the GPIO-DVS
559  * configurations and checks whether such side-effect exists.
560  */
561 static int max8997_assess_side_effect(struct regulator_dev *rdev,
562                 u8 new_val, int *best)
563 {
564         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
565         struct max8997_platform_data *pdata =
566                 dev_get_platdata(max8997->iodev->dev);
567         int rid = max8997_get_rid(rdev);
568         u8 *buckx_val[3];
569         bool buckx_gpiodvs[3];
570         int side_effect[8];
571         int min_side_effect = INT_MAX;
572         int i;
573
574         *best = -1;
575
576         switch (rid) {
577         case MAX8997_BUCK1:
578                 rid = 0;
579                 break;
580         case MAX8997_BUCK2:
581                 rid = 1;
582                 break;
583         case MAX8997_BUCK5:
584                 rid = 2;
585                 break;
586         default:
587                 return -EINVAL;
588         }
589
590         buckx_val[0] = max8997->buck1_vol;
591         buckx_val[1] = max8997->buck2_vol;
592         buckx_val[2] = max8997->buck5_vol;
593         buckx_gpiodvs[0] = pdata->buck1_gpiodvs;
594         buckx_gpiodvs[1] = pdata->buck2_gpiodvs;
595         buckx_gpiodvs[2] = pdata->buck5_gpiodvs;
596
597         for (i = 0; i < 8; i++) {
598                 int others;
599
600                 if (new_val != (buckx_val[rid])[i]) {
601                         side_effect[i] = -1;
602                         continue;
603                 }
604
605                 side_effect[i] = 0;
606                 for (others = 0; others < 3; others++) {
607                         int diff;
608
609                         if (others == rid)
610                                 continue;
611                         if (buckx_gpiodvs[others] == false)
612                                 continue; /* Not affected */
613                         diff = (buckx_val[others])[i] -
614                                 (buckx_val[others])[max8997->buck125_gpioindex];
615                         if (diff > 0)
616                                 side_effect[i] += diff;
617                         else if (diff < 0)
618                                 side_effect[i] -= diff;
619                 }
620                 if (side_effect[i] == 0) {
621                         *best = i;
622                         return 0; /* NO SIDE EFFECT! Use This! */
623                 }
624                 if (side_effect[i] < min_side_effect) {
625                         min_side_effect = side_effect[i];
626                         *best = i;
627                 }
628         }
629
630         if (*best == -1)
631                 return -EINVAL;
632
633         return side_effect[*best];
634 }
635
636 /*
637  * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls
638  * max8997_set_voltage_ldobuck to do the job.
639  */
640 static int max8997_set_voltage_buck(struct regulator_dev *rdev,
641                 int min_uV, int max_uV, unsigned *selector)
642 {
643         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
644         struct max8997_platform_data *pdata =
645                 dev_get_platdata(max8997->iodev->dev);
646         int rid = max8997_get_rid(rdev);
647         const struct voltage_map_desc *desc;
648         int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg;
649         bool gpio_dvs_mode = false;
650         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
651
652         if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7)
653                 return -EINVAL;
654
655         switch (rid) {
656         case MAX8997_BUCK1:
657                 if (pdata->buck1_gpiodvs)
658                         gpio_dvs_mode = true;
659                 break;
660         case MAX8997_BUCK2:
661                 if (pdata->buck2_gpiodvs)
662                         gpio_dvs_mode = true;
663                 break;
664         case MAX8997_BUCK5:
665                 if (pdata->buck5_gpiodvs)
666                         gpio_dvs_mode = true;
667                 break;
668         }
669
670         if (!gpio_dvs_mode)
671                 return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV,
672                                                 selector);
673
674         desc = reg_voltage_map[rid];
675         new_val = max8997_get_voltage_proper_val(desc, min_vol, max_vol);
676         if (new_val < 0)
677                 return new_val;
678
679         tmp_dmg = INT_MAX;
680         tmp_idx = -1;
681         tmp_val = -1;
682         do {
683                 damage = max8997_assess_side_effect(rdev, new_val, &new_idx);
684                 if (damage == 0)
685                         goto out;
686
687                 if (tmp_dmg > damage) {
688                         tmp_idx = new_idx;
689                         tmp_val = new_val;
690                         tmp_dmg = damage;
691                 }
692
693                 new_val++;
694         } while (desc->min + desc->step + new_val <= desc->max);
695
696         new_idx = tmp_idx;
697         new_val = tmp_val;
698
699         if (pdata->ignore_gpiodvs_side_effect == false)
700                 return -EINVAL;
701
702         dev_warn(&rdev->dev, "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET:"
703                         " %d -> %d\n", max8997->buck125_gpioindex, tmp_idx);
704
705 out:
706         if (new_idx < 0 || new_val < 0)
707                 return -EINVAL;
708
709         max8997->buck125_gpioindex = new_idx;
710         max8997_set_gpio(max8997);
711         *selector = new_val;
712
713         return 0;
714 }
715
716 static const int safeoutvolt[] = {
717         3300000,
718         4850000,
719         4900000,
720         4950000,
721 };
722
723 /* For SAFEOUT1 and SAFEOUT2 */
724 static int max8997_set_voltage_safeout(struct regulator_dev *rdev,
725                 int min_uV, int max_uV, unsigned *selector)
726 {
727         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
728         struct i2c_client *i2c = max8997->iodev->i2c;
729         int rid = max8997_get_rid(rdev);
730         int reg, shift = 0, mask, ret;
731         int i = 0;
732         u8 val;
733
734         if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2)
735                 return -EINVAL;
736
737         for (i = 0; i < ARRAY_SIZE(safeoutvolt); i++) {
738                 if (min_uV <= safeoutvolt[i] &&
739                                 max_uV >= safeoutvolt[i])
740                         break;
741         }
742
743         if (i >= ARRAY_SIZE(safeoutvolt))
744                 return -EINVAL;
745
746         if (i == 0)
747                 val = 0x3;
748         else
749                 val = i - 1;
750
751         ret = max8997_get_voltage_register(rdev, &reg, &shift, &mask);
752         if (ret)
753                 return ret;
754
755         ret = max8997_update_reg(i2c, reg, val << shift, mask << shift);
756         *selector = val;
757
758         return ret;
759 }
760
761 static int max8997_reg_enable_suspend(struct regulator_dev *rdev)
762 {
763         return 0;
764 }
765
766 static int max8997_reg_disable_suspend(struct regulator_dev *rdev)
767 {
768         struct max8997_data *max8997 = rdev_get_drvdata(rdev);
769         struct i2c_client *i2c = max8997->iodev->i2c;
770         int ret, reg, mask, pattern;
771         int rid = max8997_get_rid(rdev);
772
773         ret = max8997_get_enable_register(rdev, &reg, &mask, &pattern);
774         if (ret)
775                 return ret;
776
777         max8997_read_reg(i2c, reg, &max8997->saved_states[rid]);
778
779         if (rid == MAX8997_LDO1 ||
780                         rid == MAX8997_LDO10 ||
781                         rid == MAX8997_LDO21) {
782                 dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n",
783                                 rdev->desc->name);
784                 return max8997_update_reg(i2c, reg, 0x40, mask);
785         }
786
787         dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n",
788                         rdev->desc->name, max8997->saved_states[rid] & mask,
789                         (~pattern) & mask);
790         return max8997_update_reg(i2c, reg, ~pattern, mask);
791 }
792
793 static struct regulator_ops max8997_ldo_ops = {
794         .list_voltage           = max8997_list_voltage,
795         .is_enabled             = max8997_reg_is_enabled,
796         .enable                 = max8997_reg_enable,
797         .disable                = max8997_reg_disable,
798         .get_voltage            = max8997_get_voltage,
799         .set_voltage            = max8997_set_voltage_ldobuck,
800         .set_suspend_enable     = max8997_reg_enable_suspend,
801         .set_suspend_disable    = max8997_reg_disable_suspend,
802 };
803
804 static struct regulator_ops max8997_buck_ops = {
805         .list_voltage           = max8997_list_voltage,
806         .is_enabled             = max8997_reg_is_enabled,
807         .enable                 = max8997_reg_enable,
808         .disable                = max8997_reg_disable,
809         .get_voltage            = max8997_get_voltage,
810         .set_voltage            = max8997_set_voltage_buck,
811         .set_suspend_enable     = max8997_reg_enable_suspend,
812         .set_suspend_disable    = max8997_reg_disable_suspend,
813 };
814
815 static struct regulator_ops max8997_fixedvolt_ops = {
816         .list_voltage           = max8997_list_voltage,
817         .is_enabled             = max8997_reg_is_enabled,
818         .enable                 = max8997_reg_enable,
819         .disable                = max8997_reg_disable,
820         .set_suspend_enable     = max8997_reg_enable_suspend,
821         .set_suspend_disable    = max8997_reg_disable_suspend,
822 };
823
824 static struct regulator_ops max8997_safeout_ops = {
825         .list_voltage           = max8997_list_voltage_safeout,
826         .is_enabled             = max8997_reg_is_enabled,
827         .enable                 = max8997_reg_enable,
828         .disable                = max8997_reg_disable,
829         .get_voltage            = max8997_get_voltage,
830         .set_voltage            = max8997_set_voltage_safeout,
831         .set_suspend_enable     = max8997_reg_enable_suspend,
832         .set_suspend_disable    = max8997_reg_disable_suspend,
833 };
834
835 static struct regulator_ops max8997_fixedstate_ops = {
836         .list_voltage           = max8997_list_voltage_charger_cv,
837         .get_voltage            = max8997_get_voltage,
838         .set_voltage            = max8997_set_voltage_charger_cv,
839 };
840
841 static int max8997_set_voltage_ldobuck_wrap(struct regulator_dev *rdev,
842                 int min_uV, int max_uV)
843 {
844         unsigned dummy;
845
846         return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, &dummy);
847 }
848
849
850 static struct regulator_ops max8997_charger_ops = {
851         .is_enabled             = max8997_reg_is_enabled,
852         .enable                 = max8997_reg_enable,
853         .disable                = max8997_reg_disable,
854         .get_current_limit      = max8997_get_voltage,
855         .set_current_limit      = max8997_set_voltage_ldobuck_wrap,
856 };
857
858 static struct regulator_ops max8997_charger_fixedstate_ops = {
859         .is_enabled             = max8997_reg_is_enabled,
860         .get_current_limit      = max8997_get_voltage,
861         .set_current_limit      = max8997_set_voltage_ldobuck_wrap,
862 };
863
864 #define regulator_desc_ldo(num)         {       \
865         .name           = "LDO"#num,            \
866         .id             = MAX8997_LDO##num,     \
867         .ops            = &max8997_ldo_ops,     \
868         .type           = REGULATOR_VOLTAGE,    \
869         .owner          = THIS_MODULE,          \
870 }
871 #define regulator_desc_buck(num)                {       \
872         .name           = "BUCK"#num,           \
873         .id             = MAX8997_BUCK##num,    \
874         .ops            = &max8997_buck_ops,    \
875         .type           = REGULATOR_VOLTAGE,    \
876         .owner          = THIS_MODULE,          \
877 }
878
879 static struct regulator_desc regulators[] = {
880         regulator_desc_ldo(1),
881         regulator_desc_ldo(2),
882         regulator_desc_ldo(3),
883         regulator_desc_ldo(4),
884         regulator_desc_ldo(5),
885         regulator_desc_ldo(6),
886         regulator_desc_ldo(7),
887         regulator_desc_ldo(8),
888         regulator_desc_ldo(9),
889         regulator_desc_ldo(10),
890         regulator_desc_ldo(11),
891         regulator_desc_ldo(12),
892         regulator_desc_ldo(13),
893         regulator_desc_ldo(14),
894         regulator_desc_ldo(15),
895         regulator_desc_ldo(16),
896         regulator_desc_ldo(17),
897         regulator_desc_ldo(18),
898         regulator_desc_ldo(21),
899         regulator_desc_buck(1),
900         regulator_desc_buck(2),
901         regulator_desc_buck(3),
902         regulator_desc_buck(4),
903         regulator_desc_buck(5),
904         {
905                 .name   = "BUCK6",
906                 .id     = MAX8997_BUCK6,
907                 .ops    = &max8997_fixedvolt_ops,
908                 .type   = REGULATOR_VOLTAGE,
909                 .owner  = THIS_MODULE,
910         },
911         regulator_desc_buck(7),
912         {
913                 .name   = "EN32KHz AP",
914                 .id     = MAX8997_EN32KHZ_AP,
915                 .ops    = &max8997_fixedvolt_ops,
916                 .type   = REGULATOR_VOLTAGE,
917                 .owner  = THIS_MODULE,
918         }, {
919                 .name   = "EN32KHz CP",
920                 .id     = MAX8997_EN32KHZ_CP,
921                 .ops    = &max8997_fixedvolt_ops,
922                 .type   = REGULATOR_VOLTAGE,
923                 .owner  = THIS_MODULE,
924         }, {
925                 .name   = "ENVICHG",
926                 .id     = MAX8997_ENVICHG,
927                 .ops    = &max8997_fixedvolt_ops,
928                 .type   = REGULATOR_VOLTAGE,
929                 .owner  = THIS_MODULE,
930         }, {
931                 .name   = "ESAFEOUT1",
932                 .id     = MAX8997_ESAFEOUT1,
933                 .ops    = &max8997_safeout_ops,
934                 .type   = REGULATOR_VOLTAGE,
935                 .owner   = THIS_MODULE,
936         }, {
937                 .name   = "ESAFEOUT2",
938                 .id     = MAX8997_ESAFEOUT2,
939                 .ops    = &max8997_safeout_ops,
940                 .type   = REGULATOR_VOLTAGE,
941                 .owner   = THIS_MODULE,
942         }, {
943                 .name   = "CHARGER CV",
944                 .id     = MAX8997_CHARGER_CV,
945                 .ops    = &max8997_fixedstate_ops,
946                 .type   = REGULATOR_VOLTAGE,
947                 .owner   = THIS_MODULE,
948         }, {
949                 .name   = "CHARGER",
950                 .id     = MAX8997_CHARGER,
951                 .ops    = &max8997_charger_ops,
952                 .type   = REGULATOR_CURRENT,
953                 .owner   = THIS_MODULE,
954         }, {
955                 .name   = "CHARGER TOPOFF",
956                 .id     = MAX8997_CHARGER_TOPOFF,
957                 .ops    = &max8997_charger_fixedstate_ops,
958                 .type   = REGULATOR_CURRENT,
959                 .owner   = THIS_MODULE,
960         },
961 };
962
963 static __devinit int max8997_pmic_probe(struct platform_device *pdev)
964 {
965         struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
966         struct max8997_platform_data *pdata = dev_get_platdata(iodev->dev);
967         struct regulator_dev **rdev;
968         struct max8997_data *max8997;
969         struct i2c_client *i2c;
970         int i, ret, size;
971         u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0;
972
973         if (!pdata) {
974                 dev_err(pdev->dev.parent, "No platform init data supplied.\n");
975                 return -ENODEV;
976         }
977
978         max8997 = kzalloc(sizeof(struct max8997_data), GFP_KERNEL);
979         if (!max8997)
980                 return -ENOMEM;
981
982         size = sizeof(struct regulator_dev *) * pdata->num_regulators;
983         max8997->rdev = kzalloc(size, GFP_KERNEL);
984         if (!max8997->rdev) {
985                 kfree(max8997);
986                 return -ENOMEM;
987         }
988
989         rdev = max8997->rdev;
990         max8997->dev = &pdev->dev;
991         max8997->iodev = iodev;
992         max8997->num_regulators = pdata->num_regulators;
993         platform_set_drvdata(pdev, max8997);
994         i2c = max8997->iodev->i2c;
995
996         max8997->buck125_gpioindex = pdata->buck125_default_idx;
997
998         for (i = 0; i < 8; i++) {
999                 max8997->buck1_vol[i] = ret =
1000                         max8997_get_voltage_proper_val(
1001                                         &buck1245_voltage_map_desc,
1002                                         pdata->buck1_voltage[i] / 1000,
1003                                         pdata->buck1_voltage[i] / 1000 +
1004                                         buck1245_voltage_map_desc.step);
1005                 if (ret < 0)
1006                         goto err_alloc;
1007
1008                 max8997->buck2_vol[i] = ret =
1009                         max8997_get_voltage_proper_val(
1010                                         &buck1245_voltage_map_desc,
1011                                         pdata->buck2_voltage[i] / 1000,
1012                                         pdata->buck2_voltage[i] / 1000 +
1013                                         buck1245_voltage_map_desc.step);
1014                 if (ret < 0)
1015                         goto err_alloc;
1016
1017                 max8997->buck5_vol[i] = ret =
1018                         max8997_get_voltage_proper_val(
1019                                         &buck1245_voltage_map_desc,
1020                                         pdata->buck5_voltage[i] / 1000,
1021                                         pdata->buck5_voltage[i] / 1000 +
1022                                         buck1245_voltage_map_desc.step);
1023                 if (ret < 0)
1024                         goto err_alloc;
1025
1026                 if (max_buck1 < max8997->buck1_vol[i])
1027                         max_buck1 = max8997->buck1_vol[i];
1028                 if (max_buck2 < max8997->buck2_vol[i])
1029                         max_buck2 = max8997->buck2_vol[i];
1030                 if (max_buck5 < max8997->buck5_vol[i])
1031                         max_buck5 = max8997->buck5_vol[i];
1032         }
1033
1034         /* For the safety, set max voltage before setting up */
1035         for (i = 0; i < 8; i++) {
1036                 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS(i + 1),
1037                                 max_buck1, 0x3f);
1038                 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS(i + 1),
1039                                 max_buck2, 0x3f);
1040                 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS(i + 1),
1041                                 max_buck5, 0x3f);
1042         }
1043
1044         /*
1045          * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them.
1046          * If at least one of them cares, set gpios.
1047          */
1048         if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs ||
1049                         pdata->buck5_gpiodvs) {
1050                 bool gpio1set = false, gpio2set = false;
1051
1052                 if (!gpio_is_valid(pdata->buck125_gpios[0]) ||
1053                                 !gpio_is_valid(pdata->buck125_gpios[1]) ||
1054                                 !gpio_is_valid(pdata->buck125_gpios[2])) {
1055                         dev_err(&pdev->dev, "GPIO NOT VALID\n");
1056                         ret = -EINVAL;
1057                         goto err_alloc;
1058                 }
1059
1060                 ret = gpio_request(pdata->buck125_gpios[0],
1061                                 "MAX8997 SET1");
1062                 if (ret == -EBUSY)
1063                         dev_warn(&pdev->dev, "Duplicated gpio request"
1064                                         " on SET1\n");
1065                 else if (ret)
1066                         goto err_alloc;
1067                 else
1068                         gpio1set = true;
1069
1070                 ret = gpio_request(pdata->buck125_gpios[1],
1071                                 "MAX8997 SET2");
1072                 if (ret == -EBUSY)
1073                         dev_warn(&pdev->dev, "Duplicated gpio request"
1074                                         " on SET2\n");
1075                 else if (ret) {
1076                         if (gpio1set)
1077                                 gpio_free(pdata->buck125_gpios[0]);
1078                         goto err_alloc;
1079                 } else
1080                         gpio2set = true;
1081
1082                 ret = gpio_request(pdata->buck125_gpios[2],
1083                                 "MAX8997 SET3");
1084                 if (ret == -EBUSY)
1085                         dev_warn(&pdev->dev, "Duplicated gpio request"
1086                                         " on SET3\n");
1087                 else if (ret) {
1088                         if (gpio1set)
1089                                 gpio_free(pdata->buck125_gpios[0]);
1090                         if (gpio2set)
1091                                 gpio_free(pdata->buck125_gpios[1]);
1092                         goto err_alloc;
1093                 }
1094
1095                 gpio_direction_output(pdata->buck125_gpios[0],
1096                                 (max8997->buck125_gpioindex >> 2)
1097                                 & 0x1); /* SET1 */
1098                 gpio_direction_output(pdata->buck125_gpios[1],
1099                                 (max8997->buck125_gpioindex >> 1)
1100                                 & 0x1); /* SET2 */
1101                 gpio_direction_output(pdata->buck125_gpios[2],
1102                                 (max8997->buck125_gpioindex >> 0)
1103                                 & 0x1); /* SET3 */
1104                 ret = 0;
1105         }
1106
1107         /* DVS-GPIO disabled */
1108         max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ?
1109                         (1 << 1) : (0 << 1), 1 << 1);
1110         max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ?
1111                         (1 << 1) : (0 << 1), 1 << 1);
1112         max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ?
1113                         (1 << 1) : (0 << 1), 1 << 1);
1114
1115         /* Initialize all the DVS related BUCK registers */
1116         for (i = 0; i < 8; i++) {
1117                 max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS(i + 1),
1118                                 max8997->buck1_vol[i],
1119                                 0x3f);
1120                 max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS(i + 1),
1121                                 max8997->buck2_vol[i],
1122                                 0x3f);
1123                 max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS(i + 1),
1124                                 max8997->buck5_vol[i],
1125                                 0x3f);
1126         }
1127
1128         for (i = 0; i < pdata->num_regulators; i++) {
1129                 const struct voltage_map_desc *desc;
1130                 int id = pdata->regulators[i].id;
1131
1132                 desc = reg_voltage_map[id];
1133                 if (desc)
1134                         regulators[id].n_voltages =
1135                                 (desc->max - desc->min) / desc->step + 1;
1136                 else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2)
1137                         regulators[id].n_voltages = 4;
1138                 else if (id == MAX8997_CHARGER_CV)
1139                         regulators[id].n_voltages = 16;
1140
1141                 rdev[i] = regulator_register(&regulators[id], max8997->dev,
1142                                 pdata->regulators[i].initdata, max8997);
1143                 if (IS_ERR(rdev[i])) {
1144                         ret = PTR_ERR(rdev[i]);
1145                         dev_err(max8997->dev, "regulator init failed for %d\n",
1146                                         id);
1147                         rdev[i] = NULL;
1148                         goto err;
1149                 }
1150         }
1151
1152         /* Misc Settings */
1153         max8997->ramp_delay = 10; /* set 10mV/us, which is the default */
1154         max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9);
1155
1156         return 0;
1157 err:
1158         for (i = 0; i < max8997->num_regulators; i++)
1159                 if (rdev[i])
1160                         regulator_unregister(rdev[i]);
1161 err_alloc:
1162         kfree(max8997->rdev);
1163         kfree(max8997);
1164
1165         return ret;
1166 }
1167
1168 static int __devexit max8997_pmic_remove(struct platform_device *pdev)
1169 {
1170         struct max8997_data *max8997 = platform_get_drvdata(pdev);
1171         struct regulator_dev **rdev = max8997->rdev;
1172         int i;
1173
1174         for (i = 0; i < max8997->num_regulators; i++)
1175                 if (rdev[i])
1176                         regulator_unregister(rdev[i]);
1177
1178         kfree(max8997->rdev);
1179         kfree(max8997);
1180
1181         return 0;
1182 }
1183
1184 static const struct platform_device_id max8997_pmic_id[] = {
1185         { "max8997-pmic", 0},
1186         { },
1187 };
1188
1189 static struct platform_driver max8997_pmic_driver = {
1190         .driver = {
1191                 .name = "max8997-pmic",
1192                 .owner = THIS_MODULE,
1193         },
1194         .probe = max8997_pmic_probe,
1195         .remove = __devexit_p(max8997_pmic_remove),
1196         .id_table = max8997_pmic_id,
1197 };
1198
1199 static int __init max8997_pmic_init(void)
1200 {
1201         return platform_driver_register(&max8997_pmic_driver);
1202 }
1203 subsys_initcall(max8997_pmic_init);
1204
1205 static void __exit max8997_pmic_cleanup(void)
1206 {
1207         platform_driver_unregister(&max8997_pmic_driver);
1208 }
1209 module_exit(max8997_pmic_cleanup);
1210
1211 MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver");
1212 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1213 MODULE_LICENSE("GPL");