mfd: Use i2c_client as an argument on MAX8998 i2c routines
[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         struct i2c_client *i2c = max8998->iodev->i2c;
177         int ret, reg, shift = 8;
178         u8 val;
179
180         ret = max8998_get_enable_register(rdev, &reg, &shift);
181         if (ret)
182                 return ret;
183
184         ret = max8998_read_reg(i2c, reg, &val);
185         if (ret)
186                 return ret;
187
188         return val & (1 << shift);
189 }
190
191 static int max8998_ldo_enable(struct regulator_dev *rdev)
192 {
193         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
194         struct i2c_client *i2c = max8998->iodev->i2c;
195         int reg, shift = 8, ret;
196
197         ret = max8998_get_enable_register(rdev, &reg, &shift);
198         if (ret)
199                 return ret;
200
201         return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
202 }
203
204 static int max8998_ldo_disable(struct regulator_dev *rdev)
205 {
206         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
207         struct i2c_client *i2c = max8998->iodev->i2c;
208         int reg, shift = 8, ret;
209
210         ret = max8998_get_enable_register(rdev, &reg, &shift);
211         if (ret)
212                 return ret;
213
214         return max8998_update_reg(i2c, reg, 0, 1<<shift);
215 }
216
217 static int max8998_get_voltage_register(struct regulator_dev *rdev,
218                                 int *_reg, int *_shift, int *_mask)
219 {
220         int ldo = max8998_get_ldo(rdev);
221         int reg, shift = 0, mask = 0xff;
222
223         switch (ldo) {
224         case MAX8998_LDO2 ... MAX8998_LDO3:
225                 reg = MAX8998_REG_LDO2_LDO3;
226                 mask = 0xf;
227                 if (ldo == MAX8998_LDO2)
228                         shift = 4;
229                 else
230                         shift = 0;
231                 break;
232         case MAX8998_LDO4 ... MAX8998_LDO7:
233                 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
234                 break;
235         case MAX8998_LDO8 ... MAX8998_LDO9:
236                 reg = MAX8998_REG_LDO8_LDO9;
237                 mask = 0xf;
238                 if (ldo == MAX8998_LDO8)
239                         shift = 4;
240                 else
241                         shift = 0;
242                 break;
243         case MAX8998_LDO10 ... MAX8998_LDO11:
244                 reg = MAX8998_REG_LDO10_LDO11;
245                 if (ldo == MAX8998_LDO10) {
246                         shift = 5;
247                         mask = 0x7;
248                 } else {
249                         shift = 0;
250                         mask = 0x1f;
251                 }
252                 break;
253         case MAX8998_LDO12 ... MAX8998_LDO17:
254                 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
255                 break;
256         case MAX8998_BUCK1:
257                 reg = MAX8998_REG_BUCK1_DVSARM1;
258                 break;
259         case MAX8998_BUCK2:
260                 reg = MAX8998_REG_BUCK2_DVSINT1;
261                 break;
262         case MAX8998_BUCK3:
263                 reg = MAX8998_REG_BUCK3;
264                 break;
265         case MAX8998_BUCK4:
266                 reg = MAX8998_REG_BUCK4;
267                 break;
268         default:
269                 return -EINVAL;
270         }
271
272         *_reg = reg;
273         *_shift = shift;
274         *_mask = mask;
275
276         return 0;
277 }
278
279 static int max8998_get_voltage(struct regulator_dev *rdev)
280 {
281         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
282         struct i2c_client *i2c = max8998->iodev->i2c;
283         int reg, shift = 0, mask, ret;
284         u8 val;
285
286         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
287         if (ret)
288                 return ret;
289
290         ret = max8998_read_reg(i2c, reg, &val);
291         if (ret)
292                 return ret;
293
294         val >>= shift;
295         val &= mask;
296
297         return max8998_list_voltage(rdev, val);
298 }
299
300 static int max8998_set_voltage(struct regulator_dev *rdev,
301                                 int min_uV, int max_uV)
302 {
303         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
304         struct i2c_client *i2c = max8998->iodev->i2c;
305         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
306         int previous_vol = 0;
307         const struct voltage_map_desc *desc;
308         int ldo = max8998_get_ldo(rdev);
309         int reg, shift = 0, mask, ret;
310         int i = 0;
311         u8 val;
312         bool en_ramp = false;
313
314         if (ldo >= ARRAY_SIZE(ldo_voltage_map))
315                 return -EINVAL;
316
317         desc = ldo_voltage_map[ldo];
318         if (desc == NULL)
319                 return -EINVAL;
320
321         if (max_vol < desc->min || min_vol > desc->max)
322                 return -EINVAL;
323
324         while (desc->min + desc->step*i < min_vol &&
325                desc->min + desc->step*i < desc->max)
326                 i++;
327
328         if (desc->min + desc->step*i > max_vol)
329                 return -EINVAL;
330
331         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
332         if (ret)
333                 return ret;
334
335         /* wait for RAMP_UP_DELAY if rdev is BUCK1/2 and
336          * ENRAMP is ON */
337         if (ldo == MAX8998_BUCK1 || ldo == MAX8998_BUCK2) {
338                 max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
339                 if (val & (1 << 4)) {
340                         en_ramp = true;
341                         previous_vol = max8998_get_voltage(rdev);
342                 }
343         }
344
345         ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
346
347         if (en_ramp == true) {
348                 int difference = desc->min + desc->step*i - previous_vol/1000;
349                 if (difference > 0)
350                         udelay(difference / ((val & 0x0f) + 1));
351         }
352
353         return ret;
354 }
355
356 static struct regulator_ops max8998_ldo_ops = {
357         .list_voltage           = max8998_list_voltage,
358         .is_enabled             = max8998_ldo_is_enabled,
359         .enable                 = max8998_ldo_enable,
360         .disable                = max8998_ldo_disable,
361         .get_voltage            = max8998_get_voltage,
362         .set_voltage            = max8998_set_voltage,
363         .set_suspend_enable     = max8998_ldo_enable,
364         .set_suspend_disable    = max8998_ldo_disable,
365 };
366
367 static struct regulator_ops max8998_buck_ops = {
368         .list_voltage           = max8998_list_voltage,
369         .is_enabled             = max8998_ldo_is_enabled,
370         .enable                 = max8998_ldo_enable,
371         .disable                = max8998_ldo_disable,
372         .get_voltage            = max8998_get_voltage,
373         .set_voltage            = max8998_set_voltage,
374         .set_suspend_enable     = max8998_ldo_enable,
375         .set_suspend_disable    = max8998_ldo_disable,
376 };
377
378 static struct regulator_ops max8998_others_ops = {
379         .is_enabled             = max8998_ldo_is_enabled,
380         .enable                 = max8998_ldo_enable,
381         .disable                = max8998_ldo_disable,
382         .set_suspend_enable     = max8998_ldo_enable,
383         .set_suspend_disable    = max8998_ldo_disable,
384 };
385
386 static struct regulator_desc regulators[] = {
387         {
388                 .name           = "LDO2",
389                 .id             = MAX8998_LDO2,
390                 .ops            = &max8998_ldo_ops,
391                 .type           = REGULATOR_VOLTAGE,
392                 .owner          = THIS_MODULE,
393         }, {
394                 .name           = "LDO3",
395                 .id             = MAX8998_LDO3,
396                 .ops            = &max8998_ldo_ops,
397                 .type           = REGULATOR_VOLTAGE,
398                 .owner          = THIS_MODULE,
399         }, {
400                 .name           = "LDO4",
401                 .id             = MAX8998_LDO4,
402                 .ops            = &max8998_ldo_ops,
403                 .type           = REGULATOR_VOLTAGE,
404                 .owner          = THIS_MODULE,
405         }, {
406                 .name           = "LDO5",
407                 .id             = MAX8998_LDO5,
408                 .ops            = &max8998_ldo_ops,
409                 .type           = REGULATOR_VOLTAGE,
410                 .owner          = THIS_MODULE,
411         }, {
412                 .name           = "LDO6",
413                 .id             = MAX8998_LDO6,
414                 .ops            = &max8998_ldo_ops,
415                 .type           = REGULATOR_VOLTAGE,
416                 .owner          = THIS_MODULE,
417         }, {
418                 .name           = "LDO7",
419                 .id             = MAX8998_LDO7,
420                 .ops            = &max8998_ldo_ops,
421                 .type           = REGULATOR_VOLTAGE,
422                 .owner          = THIS_MODULE,
423         }, {
424                 .name           = "LDO8",
425                 .id             = MAX8998_LDO8,
426                 .ops            = &max8998_ldo_ops,
427                 .type           = REGULATOR_VOLTAGE,
428                 .owner          = THIS_MODULE,
429         }, {
430                 .name           = "LDO9",
431                 .id             = MAX8998_LDO9,
432                 .ops            = &max8998_ldo_ops,
433                 .type           = REGULATOR_VOLTAGE,
434                 .owner          = THIS_MODULE,
435         }, {
436                 .name           = "LDO10",
437                 .id             = MAX8998_LDO10,
438                 .ops            = &max8998_ldo_ops,
439                 .type           = REGULATOR_VOLTAGE,
440                 .owner          = THIS_MODULE,
441         }, {
442                 .name           = "LDO11",
443                 .id             = MAX8998_LDO11,
444                 .ops            = &max8998_ldo_ops,
445                 .type           = REGULATOR_VOLTAGE,
446                 .owner          = THIS_MODULE,
447         }, {
448                 .name           = "LDO12",
449                 .id             = MAX8998_LDO12,
450                 .ops            = &max8998_ldo_ops,
451                 .type           = REGULATOR_VOLTAGE,
452                 .owner          = THIS_MODULE,
453         }, {
454                 .name           = "LDO13",
455                 .id             = MAX8998_LDO13,
456                 .ops            = &max8998_ldo_ops,
457                 .type           = REGULATOR_VOLTAGE,
458                 .owner          = THIS_MODULE,
459         }, {
460                 .name           = "LDO14",
461                 .id             = MAX8998_LDO14,
462                 .ops            = &max8998_ldo_ops,
463                 .type           = REGULATOR_VOLTAGE,
464                 .owner          = THIS_MODULE,
465         }, {
466                 .name           = "LDO15",
467                 .id             = MAX8998_LDO15,
468                 .ops            = &max8998_ldo_ops,
469                 .type           = REGULATOR_VOLTAGE,
470                 .owner          = THIS_MODULE,
471         }, {
472                 .name           = "LDO16",
473                 .id             = MAX8998_LDO16,
474                 .ops            = &max8998_ldo_ops,
475                 .type           = REGULATOR_VOLTAGE,
476                 .owner          = THIS_MODULE,
477         }, {
478                 .name           = "LDO17",
479                 .id             = MAX8998_LDO17,
480                 .ops            = &max8998_ldo_ops,
481                 .type           = REGULATOR_VOLTAGE,
482                 .owner          = THIS_MODULE,
483         }, {
484                 .name           = "BUCK1",
485                 .id             = MAX8998_BUCK1,
486                 .ops            = &max8998_buck_ops,
487                 .type           = REGULATOR_VOLTAGE,
488                 .owner          = THIS_MODULE,
489         }, {
490                 .name           = "BUCK2",
491                 .id             = MAX8998_BUCK2,
492                 .ops            = &max8998_buck_ops,
493                 .type           = REGULATOR_VOLTAGE,
494                 .owner          = THIS_MODULE,
495         }, {
496                 .name           = "BUCK3",
497                 .id             = MAX8998_BUCK3,
498                 .ops            = &max8998_buck_ops,
499                 .type           = REGULATOR_VOLTAGE,
500                 .owner          = THIS_MODULE,
501         }, {
502                 .name           = "BUCK4",
503                 .id             = MAX8998_BUCK4,
504                 .ops            = &max8998_buck_ops,
505                 .type           = REGULATOR_VOLTAGE,
506                 .owner          = THIS_MODULE,
507         }, {
508                 .name           = "EN32KHz AP",
509                 .id             = MAX8998_EN32KHZ_AP,
510                 .ops            = &max8998_others_ops,
511                 .type           = REGULATOR_VOLTAGE,
512                 .owner          = THIS_MODULE,
513         }, {
514                 .name           = "EN32KHz CP",
515                 .id             = MAX8998_EN32KHZ_CP,
516                 .ops            = &max8998_others_ops,
517                 .type           = REGULATOR_VOLTAGE,
518                 .owner          = THIS_MODULE,
519         }, {
520                 .name           = "ENVICHG",
521                 .id             = MAX8998_ENVICHG,
522                 .ops            = &max8998_others_ops,
523                 .type           = REGULATOR_VOLTAGE,
524                 .owner          = THIS_MODULE,
525         }, {
526                 .name           = "ESAFEOUT1",
527                 .id             = MAX8998_ESAFEOUT1,
528                 .ops            = &max8998_others_ops,
529                 .type           = REGULATOR_VOLTAGE,
530                 .owner          = THIS_MODULE,
531         }, {
532                 .name           = "ESAFEOUT2",
533                 .id             = MAX8998_ESAFEOUT2,
534                 .ops            = &max8998_others_ops,
535                 .type           = REGULATOR_VOLTAGE,
536                 .owner          = THIS_MODULE,
537         }
538 };
539
540 static __devinit int max8998_pmic_probe(struct platform_device *pdev)
541 {
542         struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
543         struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
544         struct regulator_dev **rdev;
545         struct max8998_data *max8998;
546         int i, ret, size;
547
548         if (!pdata) {
549                 dev_err(pdev->dev.parent, "No platform init data supplied\n");
550                 return -ENODEV;
551         }
552
553         max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
554         if (!max8998)
555                 return -ENOMEM;
556
557         size = sizeof(struct regulator_dev *) * pdata->num_regulators;
558         max8998->rdev = kzalloc(size, GFP_KERNEL);
559         if (!max8998->rdev) {
560                 kfree(max8998);
561                 return -ENOMEM;
562         }
563
564         rdev = max8998->rdev;
565         max8998->dev = &pdev->dev;
566         max8998->iodev = iodev;
567         max8998->num_regulators = pdata->num_regulators;
568         platform_set_drvdata(pdev, max8998);
569
570         for (i = 0; i < pdata->num_regulators; i++) {
571                 const struct voltage_map_desc *desc;
572                 int id = pdata->regulators[i].id;
573                 int index = id - MAX8998_LDO2;
574
575                 desc = ldo_voltage_map[id];
576                 if (desc && regulators[index].ops != &max8998_others_ops) {
577                         int count = (desc->max - desc->min) / desc->step + 1;
578                         regulators[index].n_voltages = count;
579                 }
580                 rdev[i] = regulator_register(&regulators[index], max8998->dev,
581                                 pdata->regulators[i].initdata, max8998);
582                 if (IS_ERR(rdev[i])) {
583                         ret = PTR_ERR(rdev[i]);
584                         dev_err(max8998->dev, "regulator init failed\n");
585                         rdev[i] = NULL;
586                         goto err;
587                 }
588         }
589
590
591         return 0;
592 err:
593         for (i = 0; i < max8998->num_regulators; i++)
594                 if (rdev[i])
595                         regulator_unregister(rdev[i]);
596
597         kfree(max8998->rdev);
598         kfree(max8998);
599
600         return ret;
601 }
602
603 static int __devexit max8998_pmic_remove(struct platform_device *pdev)
604 {
605         struct max8998_data *max8998 = platform_get_drvdata(pdev);
606         struct regulator_dev **rdev = max8998->rdev;
607         int i;
608
609         for (i = 0; i < max8998->num_regulators; i++)
610                 if (rdev[i])
611                         regulator_unregister(rdev[i]);
612
613         kfree(max8998->rdev);
614         kfree(max8998);
615
616         return 0;
617 }
618
619 static struct platform_driver max8998_pmic_driver = {
620         .driver = {
621                 .name = "max8998-pmic",
622                 .owner = THIS_MODULE,
623         },
624         .probe = max8998_pmic_probe,
625         .remove = __devexit_p(max8998_pmic_remove),
626 };
627
628 static int __init max8998_pmic_init(void)
629 {
630         return platform_driver_register(&max8998_pmic_driver);
631 }
632 subsys_initcall(max8998_pmic_init);
633
634 static void __exit max8998_pmic_cleanup(void)
635 {
636         platform_driver_unregister(&max8998_pmic_driver);
637 }
638 module_exit(max8998_pmic_cleanup);
639
640 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
641 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
642 MODULE_LICENSE("GPL");