regulator: set max8998->dev to &pdev->dev.
[linux-2.6.git] / drivers / regulator / max8998.c
1 /*
2  * max8998.c - Voltage regulator driver for the Maxim 8998
3  *
4  *  Copyright (C) 2009-2010 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *  Marek Szyprowski <m.szyprowski@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
27 #include <linux/gpio.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/mutex.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/regulator/driver.h>
34 #include <linux/mfd/max8998.h>
35 #include <linux/mfd/max8998-private.h>
36
37 struct max8998_data {
38         struct device           *dev;
39         struct max8998_dev      *iodev;
40         int                     num_regulators;
41         struct regulator_dev    **rdev;
42 };
43
44 struct voltage_map_desc {
45         int min;
46         int max;
47         int step;
48 };
49
50 /* Voltage maps */
51 static const struct voltage_map_desc ldo23_voltage_map_desc = {
52         .min = 800,     .step = 50,     .max = 1300,
53 };
54 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
55         .min = 1600,    .step = 100,    .max = 3600,
56 };
57 static const struct voltage_map_desc ldo8_voltage_map_desc = {
58         .min = 3000,    .step = 100,    .max = 3600,
59 };
60 static const struct voltage_map_desc ldo9_voltage_map_desc = {
61         .min = 2800,    .step = 100,    .max = 3100,
62 };
63 static const struct voltage_map_desc ldo10_voltage_map_desc = {
64         .min = 950,     .step = 50,     .max = 1300,
65 };
66 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
67         .min = 800,     .step = 100,    .max = 3300,
68 };
69 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
70         .min = 1200,    .step = 100,    .max = 3300,
71 };
72 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
73         .min = 1600,    .step = 100,    .max = 3600,
74 };
75 static const struct voltage_map_desc buck12_voltage_map_desc = {
76         .min = 750,     .step = 25,     .max = 1525,
77 };
78 static const struct voltage_map_desc buck3_voltage_map_desc = {
79         .min = 1600,    .step = 100,    .max = 3600,
80 };
81 static const struct voltage_map_desc buck4_voltage_map_desc = {
82         .min = 800,     .step = 100,    .max = 2300,
83 };
84
85 static const struct voltage_map_desc *ldo_voltage_map[] = {
86         NULL,
87         NULL,
88         &ldo23_voltage_map_desc,        /* LDO2 */
89         &ldo23_voltage_map_desc,        /* LDO3 */
90         &ldo456711_voltage_map_desc,    /* LDO4 */
91         &ldo456711_voltage_map_desc,    /* LDO5 */
92         &ldo456711_voltage_map_desc,    /* LDO6 */
93         &ldo456711_voltage_map_desc,    /* LDO7 */
94         &ldo8_voltage_map_desc,         /* LDO8 */
95         &ldo9_voltage_map_desc,         /* LDO9 */
96         &ldo10_voltage_map_desc,        /* LDO10 */
97         &ldo456711_voltage_map_desc,    /* LDO11 */
98         &ldo1213_voltage_map_desc,      /* LDO12 */
99         &ldo1213_voltage_map_desc,      /* LDO13 */
100         &ldo1415_voltage_map_desc,      /* LDO14 */
101         &ldo1415_voltage_map_desc,      /* LDO15 */
102         &ldo1617_voltage_map_desc,      /* LDO16 */
103         &ldo1617_voltage_map_desc,      /* LDO17 */
104         &buck12_voltage_map_desc,       /* BUCK1 */
105         &buck12_voltage_map_desc,       /* BUCK2 */
106         &buck3_voltage_map_desc,        /* BUCK3 */
107         &buck4_voltage_map_desc,        /* BUCK4 */
108 };
109
110 static inline int max8998_get_ldo(struct regulator_dev *rdev)
111 {
112         return rdev_get_id(rdev);
113 }
114
115 static int max8998_list_voltage(struct regulator_dev *rdev,
116                                 unsigned int selector)
117 {
118         const struct voltage_map_desc *desc;
119         int ldo = max8998_get_ldo(rdev);
120         int val;
121
122         if (ldo >= ARRAY_SIZE(ldo_voltage_map))
123                 return -EINVAL;
124
125         desc = ldo_voltage_map[ldo];
126         if (desc == NULL)
127                 return -EINVAL;
128
129         val = desc->min + desc->step * selector;
130         if (val > desc->max)
131                 return -EINVAL;
132
133         return val * 1000;
134 }
135
136 static int max8998_get_enable_register(struct regulator_dev *rdev,
137                                         int *reg, int *shift)
138 {
139         int ldo = max8998_get_ldo(rdev);
140
141         switch (ldo) {
142         case MAX8998_LDO2 ... MAX8998_LDO5:
143                 *reg = MAX8998_REG_ONOFF1;
144                 *shift = 3 - (ldo - MAX8998_LDO2);
145                 break;
146         case MAX8998_LDO6 ... MAX8998_LDO13:
147                 *reg = MAX8998_REG_ONOFF2;
148                 *shift = 7 - (ldo - MAX8998_LDO6);
149                 break;
150         case MAX8998_LDO14 ... MAX8998_LDO17:
151                 *reg = MAX8998_REG_ONOFF3;
152                 *shift = 7 - (ldo - MAX8998_LDO14);
153                 break;
154         case MAX8998_BUCK1 ... MAX8998_BUCK4:
155                 *reg = MAX8998_REG_ONOFF1;
156                 *shift = 7 - (ldo - MAX8998_BUCK1);
157                 break;
158         case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
159                 *reg = MAX8998_REG_ONOFF4;
160                 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
161                 break;
162         case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
163                 *reg = MAX8998_REG_CHGR2;
164                 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
165                 break;
166         default:
167                 return -EINVAL;
168         }
169
170         return 0;
171 }
172
173 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
174 {
175         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
176         int ret, reg, shift = 8;
177         u8 val;
178
179         ret = max8998_get_enable_register(rdev, &reg, &shift);
180         if (ret)
181                 return ret;
182
183         ret = max8998_read_reg(max8998->iodev, reg, &val);
184         if (ret)
185                 return ret;
186
187         return val & (1 << shift);
188 }
189
190 static int max8998_ldo_enable(struct regulator_dev *rdev)
191 {
192         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
193         int reg, shift = 8, ret;
194
195         ret = max8998_get_enable_register(rdev, &reg, &shift);
196         if (ret)
197                 return ret;
198
199         return max8998_update_reg(max8998->iodev, reg, 1<<shift, 1<<shift);
200 }
201
202 static int max8998_ldo_disable(struct regulator_dev *rdev)
203 {
204         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
205         int reg, shift = 8, ret;
206
207         ret = max8998_get_enable_register(rdev, &reg, &shift);
208         if (ret)
209                 return ret;
210
211         return max8998_update_reg(max8998->iodev, reg, 0, 1<<shift);
212 }
213
214 static int max8998_get_voltage_register(struct regulator_dev *rdev,
215                                 int *_reg, int *_shift, int *_mask)
216 {
217         int ldo = max8998_get_ldo(rdev);
218         int reg, shift = 0, mask = 0xff;
219
220         switch (ldo) {
221         case MAX8998_LDO2 ... MAX8998_LDO3:
222                 reg = MAX8998_REG_LDO2_LDO3;
223                 mask = 0xf;
224                 if (ldo == MAX8998_LDO2)
225                         shift = 4;
226                 else
227                         shift = 0;
228                 break;
229         case MAX8998_LDO4 ... MAX8998_LDO7:
230                 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
231                 break;
232         case MAX8998_LDO8 ... MAX8998_LDO9:
233                 reg = MAX8998_REG_LDO8_LDO9;
234                 mask = 0xf;
235                 if (ldo == MAX8998_LDO8)
236                         shift = 4;
237                 else
238                         shift = 0;
239                 break;
240         case MAX8998_LDO10 ... MAX8998_LDO11:
241                 reg = MAX8998_REG_LDO10_LDO11;
242                 if (ldo == MAX8998_LDO10) {
243                         shift = 5;
244                         mask = 0x7;
245                 } else {
246                         shift = 0;
247                         mask = 0x1f;
248                 }
249                 break;
250         case MAX8998_LDO12 ... MAX8998_LDO17:
251                 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
252                 break;
253         case MAX8998_BUCK1:
254                 reg = MAX8998_REG_BUCK1_DVSARM1;
255                 break;
256         case MAX8998_BUCK2:
257                 reg = MAX8998_REG_BUCK2_DVSINT1;
258                 break;
259         case MAX8998_BUCK3:
260                 reg = MAX8998_REG_BUCK3;
261                 break;
262         case MAX8998_BUCK4:
263                 reg = MAX8998_REG_BUCK4;
264                 break;
265         default:
266                 return -EINVAL;
267         }
268
269         *_reg = reg;
270         *_shift = shift;
271         *_mask = mask;
272
273         return 0;
274 }
275
276 static int max8998_get_voltage(struct regulator_dev *rdev)
277 {
278         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
279         int reg, shift = 0, mask, ret;
280         u8 val;
281
282         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
283         if (ret)
284                 return ret;
285
286         ret = max8998_read_reg(max8998->iodev, reg, &val);
287         if (ret)
288                 return ret;
289
290         val >>= shift;
291         val &= mask;
292
293         return max8998_list_voltage(rdev, val);
294 }
295
296 static int max8998_set_voltage(struct regulator_dev *rdev,
297                                 int min_uV, int max_uV)
298 {
299         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
300         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
301         int previous_vol = 0;
302         const struct voltage_map_desc *desc;
303         int ldo = max8998_get_ldo(rdev);
304         int reg, shift = 0, mask, ret;
305         int i = 0;
306         u8 val;
307         bool en_ramp = false;
308
309         if (ldo >= ARRAY_SIZE(ldo_voltage_map))
310                 return -EINVAL;
311
312         desc = ldo_voltage_map[ldo];
313         if (desc == NULL)
314                 return -EINVAL;
315
316         if (max_vol < desc->min || min_vol > desc->max)
317                 return -EINVAL;
318
319         while (desc->min + desc->step*i < min_vol &&
320                desc->min + desc->step*i < desc->max)
321                 i++;
322
323         if (desc->min + desc->step*i > max_vol)
324                 return -EINVAL;
325
326         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
327         if (ret)
328                 return ret;
329
330         /* wait for RAMP_UP_DELAY if rdev is BUCK1/2 and
331          * ENRAMP is ON */
332         if (ldo == MAX8998_BUCK1 || ldo == MAX8998_BUCK2) {
333                 max8998_read_reg(max8998->iodev, MAX8998_REG_ONOFF4, &val);
334                 if (val & (1 << 4)) {
335                         en_ramp = true;
336                         previous_vol = max8998_get_voltage(rdev);
337                 }
338         }
339
340         ret = max8998_update_reg(max8998->iodev, reg, i<<shift, mask<<shift);
341
342         if (en_ramp == true) {
343                 int difference = desc->min + desc->step*i - previous_vol/1000;
344                 if (difference > 0)
345                         udelay(difference / ((val & 0x0f) + 1));
346         }
347
348         return ret;
349 }
350
351 static struct regulator_ops max8998_ldo_ops = {
352         .list_voltage           = max8998_list_voltage,
353         .is_enabled             = max8998_ldo_is_enabled,
354         .enable                 = max8998_ldo_enable,
355         .disable                = max8998_ldo_disable,
356         .get_voltage            = max8998_get_voltage,
357         .set_voltage            = max8998_set_voltage,
358         .set_suspend_enable     = max8998_ldo_enable,
359         .set_suspend_disable    = max8998_ldo_disable,
360 };
361
362 static struct regulator_ops max8998_buck_ops = {
363         .list_voltage           = max8998_list_voltage,
364         .is_enabled             = max8998_ldo_is_enabled,
365         .enable                 = max8998_ldo_enable,
366         .disable                = max8998_ldo_disable,
367         .get_voltage            = max8998_get_voltage,
368         .set_voltage            = max8998_set_voltage,
369         .set_suspend_enable     = max8998_ldo_enable,
370         .set_suspend_disable    = max8998_ldo_disable,
371 };
372
373 static struct regulator_ops max8998_others_ops = {
374         .is_enabled             = max8998_ldo_is_enabled,
375         .enable                 = max8998_ldo_enable,
376         .disable                = max8998_ldo_disable,
377         .set_suspend_enable     = max8998_ldo_enable,
378         .set_suspend_disable    = max8998_ldo_disable,
379 };
380
381 static struct regulator_desc regulators[] = {
382         {
383                 .name           = "LDO2",
384                 .id             = MAX8998_LDO2,
385                 .ops            = &max8998_ldo_ops,
386                 .type           = REGULATOR_VOLTAGE,
387                 .owner          = THIS_MODULE,
388         }, {
389                 .name           = "LDO3",
390                 .id             = MAX8998_LDO3,
391                 .ops            = &max8998_ldo_ops,
392                 .type           = REGULATOR_VOLTAGE,
393                 .owner          = THIS_MODULE,
394         }, {
395                 .name           = "LDO4",
396                 .id             = MAX8998_LDO4,
397                 .ops            = &max8998_ldo_ops,
398                 .type           = REGULATOR_VOLTAGE,
399                 .owner          = THIS_MODULE,
400         }, {
401                 .name           = "LDO5",
402                 .id             = MAX8998_LDO5,
403                 .ops            = &max8998_ldo_ops,
404                 .type           = REGULATOR_VOLTAGE,
405                 .owner          = THIS_MODULE,
406         }, {
407                 .name           = "LDO6",
408                 .id             = MAX8998_LDO6,
409                 .ops            = &max8998_ldo_ops,
410                 .type           = REGULATOR_VOLTAGE,
411                 .owner          = THIS_MODULE,
412         }, {
413                 .name           = "LDO7",
414                 .id             = MAX8998_LDO7,
415                 .ops            = &max8998_ldo_ops,
416                 .type           = REGULATOR_VOLTAGE,
417                 .owner          = THIS_MODULE,
418         }, {
419                 .name           = "LDO8",
420                 .id             = MAX8998_LDO8,
421                 .ops            = &max8998_ldo_ops,
422                 .type           = REGULATOR_VOLTAGE,
423                 .owner          = THIS_MODULE,
424         }, {
425                 .name           = "LDO9",
426                 .id             = MAX8998_LDO9,
427                 .ops            = &max8998_ldo_ops,
428                 .type           = REGULATOR_VOLTAGE,
429                 .owner          = THIS_MODULE,
430         }, {
431                 .name           = "LDO10",
432                 .id             = MAX8998_LDO10,
433                 .ops            = &max8998_ldo_ops,
434                 .type           = REGULATOR_VOLTAGE,
435                 .owner          = THIS_MODULE,
436         }, {
437                 .name           = "LDO11",
438                 .id             = MAX8998_LDO11,
439                 .ops            = &max8998_ldo_ops,
440                 .type           = REGULATOR_VOLTAGE,
441                 .owner          = THIS_MODULE,
442         }, {
443                 .name           = "LDO12",
444                 .id             = MAX8998_LDO12,
445                 .ops            = &max8998_ldo_ops,
446                 .type           = REGULATOR_VOLTAGE,
447                 .owner          = THIS_MODULE,
448         }, {
449                 .name           = "LDO13",
450                 .id             = MAX8998_LDO13,
451                 .ops            = &max8998_ldo_ops,
452                 .type           = REGULATOR_VOLTAGE,
453                 .owner          = THIS_MODULE,
454         }, {
455                 .name           = "LDO14",
456                 .id             = MAX8998_LDO14,
457                 .ops            = &max8998_ldo_ops,
458                 .type           = REGULATOR_VOLTAGE,
459                 .owner          = THIS_MODULE,
460         }, {
461                 .name           = "LDO15",
462                 .id             = MAX8998_LDO15,
463                 .ops            = &max8998_ldo_ops,
464                 .type           = REGULATOR_VOLTAGE,
465                 .owner          = THIS_MODULE,
466         }, {
467                 .name           = "LDO16",
468                 .id             = MAX8998_LDO16,
469                 .ops            = &max8998_ldo_ops,
470                 .type           = REGULATOR_VOLTAGE,
471                 .owner          = THIS_MODULE,
472         }, {
473                 .name           = "LDO17",
474                 .id             = MAX8998_LDO17,
475                 .ops            = &max8998_ldo_ops,
476                 .type           = REGULATOR_VOLTAGE,
477                 .owner          = THIS_MODULE,
478         }, {
479                 .name           = "BUCK1",
480                 .id             = MAX8998_BUCK1,
481                 .ops            = &max8998_buck_ops,
482                 .type           = REGULATOR_VOLTAGE,
483                 .owner          = THIS_MODULE,
484         }, {
485                 .name           = "BUCK2",
486                 .id             = MAX8998_BUCK2,
487                 .ops            = &max8998_buck_ops,
488                 .type           = REGULATOR_VOLTAGE,
489                 .owner          = THIS_MODULE,
490         }, {
491                 .name           = "BUCK3",
492                 .id             = MAX8998_BUCK3,
493                 .ops            = &max8998_buck_ops,
494                 .type           = REGULATOR_VOLTAGE,
495                 .owner          = THIS_MODULE,
496         }, {
497                 .name           = "BUCK4",
498                 .id             = MAX8998_BUCK4,
499                 .ops            = &max8998_buck_ops,
500                 .type           = REGULATOR_VOLTAGE,
501                 .owner          = THIS_MODULE,
502         }, {
503                 .name           = "EN32KHz AP",
504                 .id             = MAX8998_EN32KHZ_AP,
505                 .ops            = &max8998_others_ops,
506                 .type           = REGULATOR_VOLTAGE,
507                 .owner          = THIS_MODULE,
508         }, {
509                 .name           = "EN32KHz CP",
510                 .id             = MAX8998_EN32KHZ_CP,
511                 .ops            = &max8998_others_ops,
512                 .type           = REGULATOR_VOLTAGE,
513                 .owner          = THIS_MODULE,
514         }, {
515                 .name           = "ENVICHG",
516                 .id             = MAX8998_ENVICHG,
517                 .ops            = &max8998_others_ops,
518                 .type           = REGULATOR_VOLTAGE,
519                 .owner          = THIS_MODULE,
520         }, {
521                 .name           = "ESAFEOUT1",
522                 .id             = MAX8998_ESAFEOUT1,
523                 .ops            = &max8998_others_ops,
524                 .type           = REGULATOR_VOLTAGE,
525                 .owner          = THIS_MODULE,
526         }, {
527                 .name           = "ESAFEOUT2",
528                 .id             = MAX8998_ESAFEOUT2,
529                 .ops            = &max8998_others_ops,
530                 .type           = REGULATOR_VOLTAGE,
531                 .owner          = THIS_MODULE,
532         }
533 };
534
535 static __devinit int max8998_pmic_probe(struct platform_device *pdev)
536 {
537         struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
538         struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
539         struct regulator_dev **rdev;
540         struct max8998_data *max8998;
541         int i, ret, size;
542
543         if (!pdata) {
544                 dev_err(pdev->dev.parent, "No platform init data supplied\n");
545                 return -ENODEV;
546         }
547
548         max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
549         if (!max8998)
550                 return -ENOMEM;
551
552         size = sizeof(struct regulator_dev *) * pdata->num_regulators;
553         max8998->rdev = kzalloc(size, GFP_KERNEL);
554         if (!max8998->rdev) {
555                 kfree(max8998);
556                 return -ENOMEM;
557         }
558
559         rdev = max8998->rdev;
560         max8998->dev = &pdev->dev;
561         max8998->iodev = iodev;
562         max8998->num_regulators = pdata->num_regulators;
563         platform_set_drvdata(pdev, max8998);
564
565         for (i = 0; i < pdata->num_regulators; i++) {
566                 const struct voltage_map_desc *desc;
567                 int id = pdata->regulators[i].id;
568                 int index = id - MAX8998_LDO2;
569
570                 desc = ldo_voltage_map[id];
571                 if (desc && regulators[index].ops != &max8998_others_ops) {
572                         int count = (desc->max - desc->min) / desc->step + 1;
573                         regulators[index].n_voltages = count;
574                 }
575                 rdev[i] = regulator_register(&regulators[index], max8998->dev,
576                                 pdata->regulators[i].initdata, max8998);
577                 if (IS_ERR(rdev[i])) {
578                         ret = PTR_ERR(rdev[i]);
579                         dev_err(max8998->dev, "regulator init failed\n");
580                         rdev[i] = NULL;
581                         goto err;
582                 }
583         }
584
585
586         return 0;
587 err:
588         for (i = 0; i < max8998->num_regulators; i++)
589                 if (rdev[i])
590                         regulator_unregister(rdev[i]);
591
592         kfree(max8998->rdev);
593         kfree(max8998);
594
595         return ret;
596 }
597
598 static int __devexit max8998_pmic_remove(struct platform_device *pdev)
599 {
600         struct max8998_data *max8998 = platform_get_drvdata(pdev);
601         struct regulator_dev **rdev = max8998->rdev;
602         int i;
603
604         for (i = 0; i < max8998->num_regulators; i++)
605                 if (rdev[i])
606                         regulator_unregister(rdev[i]);
607
608         kfree(max8998->rdev);
609         kfree(max8998);
610
611         return 0;
612 }
613
614 static struct platform_driver max8998_pmic_driver = {
615         .driver = {
616                 .name = "max8998-pmic",
617                 .owner = THIS_MODULE,
618         },
619         .probe = max8998_pmic_probe,
620         .remove = __devexit_p(max8998_pmic_remove),
621 };
622
623 static int __init max8998_pmic_init(void)
624 {
625         return platform_driver_register(&max8998_pmic_driver);
626 }
627 subsys_initcall(max8998_pmic_init);
628
629 static void __exit max8998_pmic_cleanup(void)
630 {
631         platform_driver_unregister(&max8998_pmic_driver);
632 }
633 module_exit(max8998_pmic_cleanup);
634
635 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
636 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
637 MODULE_LICENSE("GPL");