KVM: x86 emulator: tighen up ->read_std() and ->write_std() error checks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / max8998.c
blob5c20756db60723b9a3cf1c712ee67a0fa724e52b
1 /*
2 * max8998.c - Voltage regulator driver for the Maxim 8998
4 * Copyright (C) 2009-2010 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 * Marek Szyprowski <m.szyprowski@samsung.com>
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.
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.
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
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>
37 struct max8998_data {
38 struct device *dev;
39 struct max8998_dev *iodev;
40 int num_regulators;
41 struct regulator_dev **rdev;
42 u8 buck1_vol[4]; /* voltages for selection */
43 u8 buck2_vol[2];
44 unsigned int buck1_idx; /* index to last changed voltage */
45 /* value in a set */
46 unsigned int buck2_idx;
49 struct voltage_map_desc {
50 int min;
51 int max;
52 int step;
55 /* Voltage maps */
56 static const struct voltage_map_desc ldo23_voltage_map_desc = {
57 .min = 800, .step = 50, .max = 1300,
59 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
60 .min = 1600, .step = 100, .max = 3600,
62 static const struct voltage_map_desc ldo8_voltage_map_desc = {
63 .min = 3000, .step = 100, .max = 3600,
65 static const struct voltage_map_desc ldo9_voltage_map_desc = {
66 .min = 2800, .step = 100, .max = 3100,
68 static const struct voltage_map_desc ldo10_voltage_map_desc = {
69 .min = 950, .step = 50, .max = 1300,
71 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
72 .min = 800, .step = 100, .max = 3300,
74 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
75 .min = 1200, .step = 100, .max = 3300,
77 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
78 .min = 1600, .step = 100, .max = 3600,
80 static const struct voltage_map_desc buck12_voltage_map_desc = {
81 .min = 750, .step = 25, .max = 1525,
83 static const struct voltage_map_desc buck3_voltage_map_desc = {
84 .min = 1600, .step = 100, .max = 3600,
86 static const struct voltage_map_desc buck4_voltage_map_desc = {
87 .min = 800, .step = 100, .max = 2300,
90 static const struct voltage_map_desc *ldo_voltage_map[] = {
91 NULL,
92 NULL,
93 &ldo23_voltage_map_desc, /* LDO2 */
94 &ldo23_voltage_map_desc, /* LDO3 */
95 &ldo456711_voltage_map_desc, /* LDO4 */
96 &ldo456711_voltage_map_desc, /* LDO5 */
97 &ldo456711_voltage_map_desc, /* LDO6 */
98 &ldo456711_voltage_map_desc, /* LDO7 */
99 &ldo8_voltage_map_desc, /* LDO8 */
100 &ldo9_voltage_map_desc, /* LDO9 */
101 &ldo10_voltage_map_desc, /* LDO10 */
102 &ldo456711_voltage_map_desc, /* LDO11 */
103 &ldo1213_voltage_map_desc, /* LDO12 */
104 &ldo1213_voltage_map_desc, /* LDO13 */
105 &ldo1415_voltage_map_desc, /* LDO14 */
106 &ldo1415_voltage_map_desc, /* LDO15 */
107 &ldo1617_voltage_map_desc, /* LDO16 */
108 &ldo1617_voltage_map_desc, /* LDO17 */
109 &buck12_voltage_map_desc, /* BUCK1 */
110 &buck12_voltage_map_desc, /* BUCK2 */
111 &buck3_voltage_map_desc, /* BUCK3 */
112 &buck4_voltage_map_desc, /* BUCK4 */
115 static inline int max8998_get_ldo(struct regulator_dev *rdev)
117 return rdev_get_id(rdev);
120 static int max8998_list_voltage(struct regulator_dev *rdev,
121 unsigned int selector)
123 const struct voltage_map_desc *desc;
124 int ldo = max8998_get_ldo(rdev);
125 int val;
127 if (ldo >= ARRAY_SIZE(ldo_voltage_map))
128 return -EINVAL;
130 desc = ldo_voltage_map[ldo];
131 if (desc == NULL)
132 return -EINVAL;
134 val = desc->min + desc->step * selector;
135 if (val > desc->max)
136 return -EINVAL;
138 return val * 1000;
141 static int max8998_get_enable_register(struct regulator_dev *rdev,
142 int *reg, int *shift)
144 int ldo = max8998_get_ldo(rdev);
146 switch (ldo) {
147 case MAX8998_LDO2 ... MAX8998_LDO5:
148 *reg = MAX8998_REG_ONOFF1;
149 *shift = 3 - (ldo - MAX8998_LDO2);
150 break;
151 case MAX8998_LDO6 ... MAX8998_LDO13:
152 *reg = MAX8998_REG_ONOFF2;
153 *shift = 7 - (ldo - MAX8998_LDO6);
154 break;
155 case MAX8998_LDO14 ... MAX8998_LDO17:
156 *reg = MAX8998_REG_ONOFF3;
157 *shift = 7 - (ldo - MAX8998_LDO14);
158 break;
159 case MAX8998_BUCK1 ... MAX8998_BUCK4:
160 *reg = MAX8998_REG_ONOFF1;
161 *shift = 7 - (ldo - MAX8998_BUCK1);
162 break;
163 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
164 *reg = MAX8998_REG_ONOFF4;
165 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
166 break;
167 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
168 *reg = MAX8998_REG_CHGR2;
169 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
170 break;
171 default:
172 return -EINVAL;
175 return 0;
178 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
180 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
181 struct i2c_client *i2c = max8998->iodev->i2c;
182 int ret, reg, shift = 8;
183 u8 val;
185 ret = max8998_get_enable_register(rdev, &reg, &shift);
186 if (ret)
187 return ret;
189 ret = max8998_read_reg(i2c, reg, &val);
190 if (ret)
191 return ret;
193 return val & (1 << shift);
196 static int max8998_ldo_enable(struct regulator_dev *rdev)
198 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
199 struct i2c_client *i2c = max8998->iodev->i2c;
200 int reg, shift = 8, ret;
202 ret = max8998_get_enable_register(rdev, &reg, &shift);
203 if (ret)
204 return ret;
206 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
209 static int max8998_ldo_disable(struct regulator_dev *rdev)
211 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
212 struct i2c_client *i2c = max8998->iodev->i2c;
213 int reg, shift = 8, ret;
215 ret = max8998_get_enable_register(rdev, &reg, &shift);
216 if (ret)
217 return ret;
219 return max8998_update_reg(i2c, reg, 0, 1<<shift);
222 static int max8998_get_voltage_register(struct regulator_dev *rdev,
223 int *_reg, int *_shift, int *_mask)
225 int ldo = max8998_get_ldo(rdev);
226 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
227 int reg, shift = 0, mask = 0xff;
229 switch (ldo) {
230 case MAX8998_LDO2 ... MAX8998_LDO3:
231 reg = MAX8998_REG_LDO2_LDO3;
232 mask = 0xf;
233 if (ldo == MAX8998_LDO2)
234 shift = 4;
235 else
236 shift = 0;
237 break;
238 case MAX8998_LDO4 ... MAX8998_LDO7:
239 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
240 break;
241 case MAX8998_LDO8 ... MAX8998_LDO9:
242 reg = MAX8998_REG_LDO8_LDO9;
243 mask = 0xf;
244 if (ldo == MAX8998_LDO8)
245 shift = 4;
246 else
247 shift = 0;
248 break;
249 case MAX8998_LDO10 ... MAX8998_LDO11:
250 reg = MAX8998_REG_LDO10_LDO11;
251 if (ldo == MAX8998_LDO10) {
252 shift = 5;
253 mask = 0x7;
254 } else {
255 shift = 0;
256 mask = 0x1f;
258 break;
259 case MAX8998_LDO12 ... MAX8998_LDO17:
260 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
261 break;
262 case MAX8998_BUCK1:
263 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
264 break;
265 case MAX8998_BUCK2:
266 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
267 break;
268 case MAX8998_BUCK3:
269 reg = MAX8998_REG_BUCK3;
270 break;
271 case MAX8998_BUCK4:
272 reg = MAX8998_REG_BUCK4;
273 break;
274 default:
275 return -EINVAL;
278 *_reg = reg;
279 *_shift = shift;
280 *_mask = mask;
282 return 0;
285 static int max8998_get_voltage(struct regulator_dev *rdev)
287 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
288 struct i2c_client *i2c = max8998->iodev->i2c;
289 int reg, shift = 0, mask, ret;
290 u8 val;
292 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
293 if (ret)
294 return ret;
296 ret = max8998_read_reg(i2c, reg, &val);
297 if (ret)
298 return ret;
300 val >>= shift;
301 val &= mask;
303 return max8998_list_voltage(rdev, val);
306 static int max8998_set_voltage_ldo(struct regulator_dev *rdev,
307 int min_uV, int max_uV)
309 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
310 struct i2c_client *i2c = max8998->iodev->i2c;
311 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
312 const struct voltage_map_desc *desc;
313 int ldo = max8998_get_ldo(rdev);
314 int reg, shift = 0, mask, ret;
315 int i = 0;
317 if (ldo >= ARRAY_SIZE(ldo_voltage_map))
318 return -EINVAL;
320 desc = ldo_voltage_map[ldo];
321 if (desc == NULL)
322 return -EINVAL;
324 if (max_vol < desc->min || min_vol > desc->max)
325 return -EINVAL;
327 while (desc->min + desc->step*i < min_vol &&
328 desc->min + desc->step*i < desc->max)
329 i++;
331 if (desc->min + desc->step*i > max_vol)
332 return -EINVAL;
334 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
335 if (ret)
336 return ret;
338 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
340 return ret;
343 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
345 gpio_set_value(gpio1, v & 0x1);
346 gpio_set_value(gpio2, (v >> 1) & 0x1);
349 static inline void buck2_gpio_set(int gpio, int v)
351 gpio_set_value(gpio, v & 0x1);
354 static int max8998_set_voltage_buck(struct regulator_dev *rdev,
355 int min_uV, int max_uV)
357 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
358 struct max8998_platform_data *pdata =
359 dev_get_platdata(max8998->iodev->dev);
360 struct i2c_client *i2c = max8998->iodev->i2c;
361 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
362 const struct voltage_map_desc *desc;
363 int buck = max8998_get_ldo(rdev);
364 int reg, shift = 0, mask, ret;
365 int difference = 0, i = 0, j = 0, previous_vol = 0;
366 u8 val = 0;
367 static u8 buck1_last_val;
369 if (buck >= ARRAY_SIZE(ldo_voltage_map))
370 return -EINVAL;
372 desc = ldo_voltage_map[buck];
374 if (desc == NULL)
375 return -EINVAL;
377 if (max_vol < desc->min || min_vol > desc->max)
378 return -EINVAL;
380 while (desc->min + desc->step*i < min_vol &&
381 desc->min + desc->step*i < desc->max)
382 i++;
384 if (desc->min + desc->step*i > max_vol)
385 return -EINVAL;
387 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
388 if (ret)
389 return ret;
391 previous_vol = max8998_get_voltage(rdev);
393 /* Check if voltage needs to be changed */
394 /* if previous_voltage equal new voltage, return */
395 if (previous_vol == max8998_list_voltage(rdev, i)) {
396 dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
397 previous_vol, max8998_list_voltage(rdev, i));
398 return ret;
401 switch (buck) {
402 case MAX8998_BUCK1:
403 dev_dbg(max8998->dev,
404 "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n\
405 buck1_vol3:%d, buck1_vol4:%d\n",
406 i, max8998->buck1_vol[0], max8998->buck1_vol[1],
407 max8998->buck1_vol[2], max8998->buck1_vol[3]);
409 if (gpio_is_valid(pdata->buck1_set1) &&
410 gpio_is_valid(pdata->buck1_set2)) {
412 /* check if requested voltage */
413 /* value is already defined */
414 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
415 if (max8998->buck1_vol[j] == i) {
416 max8998->buck1_idx = j;
417 buck1_gpio_set(pdata->buck1_set1,
418 pdata->buck1_set2, j);
419 goto buck1_exit;
423 /* no predefine regulator found */
424 max8998->buck1_idx = (buck1_last_val % 2) + 2;
425 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
426 max8998->buck1_idx);
427 max8998->buck1_vol[max8998->buck1_idx] = i;
428 ret = max8998_get_voltage_register(rdev, &reg,
429 &shift,
430 &mask);
431 ret = max8998_write_reg(i2c, reg, i);
432 buck1_gpio_set(pdata->buck1_set1,
433 pdata->buck1_set2, max8998->buck1_idx);
434 buck1_last_val++;
435 buck1_exit:
436 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
437 i2c->name, gpio_get_value(pdata->buck1_set1),
438 gpio_get_value(pdata->buck1_set2));
439 break;
440 } else {
441 ret = max8998_write_reg(i2c, reg, i);
443 break;
445 case MAX8998_BUCK2:
446 dev_dbg(max8998->dev,
447 "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n"
448 , i, max8998->buck2_vol[0], max8998->buck2_vol[1]);
449 if (gpio_is_valid(pdata->buck2_set3)) {
450 if (max8998->buck2_vol[0] == i) {
451 max8998->buck1_idx = 0;
452 buck2_gpio_set(pdata->buck2_set3, 0);
453 } else {
454 max8998->buck1_idx = 1;
455 ret = max8998_get_voltage_register(rdev, &reg,
456 &shift,
457 &mask);
458 ret = max8998_write_reg(i2c, reg, i);
459 max8998->buck2_vol[1] = i;
460 buck2_gpio_set(pdata->buck2_set3, 1);
462 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
463 gpio_get_value(pdata->buck2_set3));
464 } else {
465 ret = max8998_write_reg(i2c, reg, i);
467 break;
469 case MAX8998_BUCK3:
470 case MAX8998_BUCK4:
471 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
472 break;
475 /* Voltage stabilization */
476 max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
478 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
479 /* MAX8998 has ENRAMP bit implemented, so test it*/
480 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
481 return ret;
483 difference = desc->min + desc->step*i - previous_vol/1000;
484 if (difference > 0)
485 udelay(difference / ((val & 0x0f) + 1));
487 return ret;
490 static struct regulator_ops max8998_ldo_ops = {
491 .list_voltage = max8998_list_voltage,
492 .is_enabled = max8998_ldo_is_enabled,
493 .enable = max8998_ldo_enable,
494 .disable = max8998_ldo_disable,
495 .get_voltage = max8998_get_voltage,
496 .set_voltage = max8998_set_voltage_ldo,
497 .set_suspend_enable = max8998_ldo_enable,
498 .set_suspend_disable = max8998_ldo_disable,
501 static struct regulator_ops max8998_buck_ops = {
502 .list_voltage = max8998_list_voltage,
503 .is_enabled = max8998_ldo_is_enabled,
504 .enable = max8998_ldo_enable,
505 .disable = max8998_ldo_disable,
506 .get_voltage = max8998_get_voltage,
507 .set_voltage = max8998_set_voltage_buck,
508 .set_suspend_enable = max8998_ldo_enable,
509 .set_suspend_disable = max8998_ldo_disable,
512 static struct regulator_ops max8998_others_ops = {
513 .is_enabled = max8998_ldo_is_enabled,
514 .enable = max8998_ldo_enable,
515 .disable = max8998_ldo_disable,
516 .set_suspend_enable = max8998_ldo_enable,
517 .set_suspend_disable = max8998_ldo_disable,
520 static struct regulator_desc regulators[] = {
522 .name = "LDO2",
523 .id = MAX8998_LDO2,
524 .ops = &max8998_ldo_ops,
525 .type = REGULATOR_VOLTAGE,
526 .owner = THIS_MODULE,
527 }, {
528 .name = "LDO3",
529 .id = MAX8998_LDO3,
530 .ops = &max8998_ldo_ops,
531 .type = REGULATOR_VOLTAGE,
532 .owner = THIS_MODULE,
533 }, {
534 .name = "LDO4",
535 .id = MAX8998_LDO4,
536 .ops = &max8998_ldo_ops,
537 .type = REGULATOR_VOLTAGE,
538 .owner = THIS_MODULE,
539 }, {
540 .name = "LDO5",
541 .id = MAX8998_LDO5,
542 .ops = &max8998_ldo_ops,
543 .type = REGULATOR_VOLTAGE,
544 .owner = THIS_MODULE,
545 }, {
546 .name = "LDO6",
547 .id = MAX8998_LDO6,
548 .ops = &max8998_ldo_ops,
549 .type = REGULATOR_VOLTAGE,
550 .owner = THIS_MODULE,
551 }, {
552 .name = "LDO7",
553 .id = MAX8998_LDO7,
554 .ops = &max8998_ldo_ops,
555 .type = REGULATOR_VOLTAGE,
556 .owner = THIS_MODULE,
557 }, {
558 .name = "LDO8",
559 .id = MAX8998_LDO8,
560 .ops = &max8998_ldo_ops,
561 .type = REGULATOR_VOLTAGE,
562 .owner = THIS_MODULE,
563 }, {
564 .name = "LDO9",
565 .id = MAX8998_LDO9,
566 .ops = &max8998_ldo_ops,
567 .type = REGULATOR_VOLTAGE,
568 .owner = THIS_MODULE,
569 }, {
570 .name = "LDO10",
571 .id = MAX8998_LDO10,
572 .ops = &max8998_ldo_ops,
573 .type = REGULATOR_VOLTAGE,
574 .owner = THIS_MODULE,
575 }, {
576 .name = "LDO11",
577 .id = MAX8998_LDO11,
578 .ops = &max8998_ldo_ops,
579 .type = REGULATOR_VOLTAGE,
580 .owner = THIS_MODULE,
581 }, {
582 .name = "LDO12",
583 .id = MAX8998_LDO12,
584 .ops = &max8998_ldo_ops,
585 .type = REGULATOR_VOLTAGE,
586 .owner = THIS_MODULE,
587 }, {
588 .name = "LDO13",
589 .id = MAX8998_LDO13,
590 .ops = &max8998_ldo_ops,
591 .type = REGULATOR_VOLTAGE,
592 .owner = THIS_MODULE,
593 }, {
594 .name = "LDO14",
595 .id = MAX8998_LDO14,
596 .ops = &max8998_ldo_ops,
597 .type = REGULATOR_VOLTAGE,
598 .owner = THIS_MODULE,
599 }, {
600 .name = "LDO15",
601 .id = MAX8998_LDO15,
602 .ops = &max8998_ldo_ops,
603 .type = REGULATOR_VOLTAGE,
604 .owner = THIS_MODULE,
605 }, {
606 .name = "LDO16",
607 .id = MAX8998_LDO16,
608 .ops = &max8998_ldo_ops,
609 .type = REGULATOR_VOLTAGE,
610 .owner = THIS_MODULE,
611 }, {
612 .name = "LDO17",
613 .id = MAX8998_LDO17,
614 .ops = &max8998_ldo_ops,
615 .type = REGULATOR_VOLTAGE,
616 .owner = THIS_MODULE,
617 }, {
618 .name = "BUCK1",
619 .id = MAX8998_BUCK1,
620 .ops = &max8998_buck_ops,
621 .type = REGULATOR_VOLTAGE,
622 .owner = THIS_MODULE,
623 }, {
624 .name = "BUCK2",
625 .id = MAX8998_BUCK2,
626 .ops = &max8998_buck_ops,
627 .type = REGULATOR_VOLTAGE,
628 .owner = THIS_MODULE,
629 }, {
630 .name = "BUCK3",
631 .id = MAX8998_BUCK3,
632 .ops = &max8998_buck_ops,
633 .type = REGULATOR_VOLTAGE,
634 .owner = THIS_MODULE,
635 }, {
636 .name = "BUCK4",
637 .id = MAX8998_BUCK4,
638 .ops = &max8998_buck_ops,
639 .type = REGULATOR_VOLTAGE,
640 .owner = THIS_MODULE,
641 }, {
642 .name = "EN32KHz AP",
643 .id = MAX8998_EN32KHZ_AP,
644 .ops = &max8998_others_ops,
645 .type = REGULATOR_VOLTAGE,
646 .owner = THIS_MODULE,
647 }, {
648 .name = "EN32KHz CP",
649 .id = MAX8998_EN32KHZ_CP,
650 .ops = &max8998_others_ops,
651 .type = REGULATOR_VOLTAGE,
652 .owner = THIS_MODULE,
653 }, {
654 .name = "ENVICHG",
655 .id = MAX8998_ENVICHG,
656 .ops = &max8998_others_ops,
657 .type = REGULATOR_VOLTAGE,
658 .owner = THIS_MODULE,
659 }, {
660 .name = "ESAFEOUT1",
661 .id = MAX8998_ESAFEOUT1,
662 .ops = &max8998_others_ops,
663 .type = REGULATOR_VOLTAGE,
664 .owner = THIS_MODULE,
665 }, {
666 .name = "ESAFEOUT2",
667 .id = MAX8998_ESAFEOUT2,
668 .ops = &max8998_others_ops,
669 .type = REGULATOR_VOLTAGE,
670 .owner = THIS_MODULE,
674 static __devinit int max8998_pmic_probe(struct platform_device *pdev)
676 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
677 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
678 struct regulator_dev **rdev;
679 struct max8998_data *max8998;
680 struct i2c_client *i2c;
681 int i, ret, size;
683 if (!pdata) {
684 dev_err(pdev->dev.parent, "No platform init data supplied\n");
685 return -ENODEV;
688 max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
689 if (!max8998)
690 return -ENOMEM;
692 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
693 max8998->rdev = kzalloc(size, GFP_KERNEL);
694 if (!max8998->rdev) {
695 kfree(max8998);
696 return -ENOMEM;
699 rdev = max8998->rdev;
700 max8998->dev = &pdev->dev;
701 max8998->iodev = iodev;
702 max8998->num_regulators = pdata->num_regulators;
703 platform_set_drvdata(pdev, max8998);
704 i2c = max8998->iodev->i2c;
706 /* NOTE: */
707 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
708 /* will be displayed */
710 /* Check if MAX8998 voltage selection GPIOs are defined */
711 if (gpio_is_valid(pdata->buck1_set1) &&
712 gpio_is_valid(pdata->buck1_set2)) {
713 /* Check if SET1 is not equal to 0 */
714 if (!pdata->buck1_set1) {
715 printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n");
716 WARN_ON(!pdata->buck1_set1);
717 return -EIO;
719 /* Check if SET2 is not equal to 0 */
720 if (!pdata->buck1_set2) {
721 printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n");
722 WARN_ON(!pdata->buck1_set2);
723 return -EIO;
726 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
727 gpio_direction_output(pdata->buck1_set1,
728 max8998->buck1_idx & 0x1);
731 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
732 gpio_direction_output(pdata->buck1_set2,
733 (max8998->buck1_idx >> 1) & 0x1);
734 /* Set predefined value for BUCK1 register 1 */
735 i = 0;
736 while (buck12_voltage_map_desc.min +
737 buck12_voltage_map_desc.step*i
738 != (pdata->buck1_max_voltage1 / 1000))
739 i++;
740 printk(KERN_ERR "i:%d, buck1_idx:%d\n", i, max8998->buck1_idx);
741 max8998->buck1_vol[0] = i;
742 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
744 /* Set predefined value for BUCK1 register 2 */
745 i = 0;
746 while (buck12_voltage_map_desc.min +
747 buck12_voltage_map_desc.step*i
748 != (pdata->buck1_max_voltage2 / 1000))
749 i++;
751 max8998->buck1_vol[1] = i;
752 printk(KERN_ERR "i:%d, buck1_idx:%d\n", i, max8998->buck1_idx);
753 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i)
754 + ret;
755 if (ret)
756 return ret;
760 if (gpio_is_valid(pdata->buck2_set3)) {
761 /* Check if SET3 is not equal to 0 */
762 if (!pdata->buck2_set3) {
763 printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n");
764 WARN_ON(!pdata->buck2_set3);
765 return -EIO;
767 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
768 gpio_direction_output(pdata->buck2_set3,
769 max8998->buck2_idx & 0x1);
771 /* BUCK2 - set preset default voltage value to buck2_vol[0] */
772 i = 0;
773 while (buck12_voltage_map_desc.min +
774 buck12_voltage_map_desc.step*i
775 != (pdata->buck2_max_voltage / 1000))
776 i++;
777 printk(KERN_ERR "i:%d, buck2_idx:%d\n", i, max8998->buck2_idx);
778 max8998->buck2_vol[0] = i;
779 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
780 if (ret)
781 return ret;
785 for (i = 0; i < pdata->num_regulators; i++) {
786 const struct voltage_map_desc *desc;
787 int id = pdata->regulators[i].id;
788 int index = id - MAX8998_LDO2;
790 desc = ldo_voltage_map[id];
791 if (desc && regulators[index].ops != &max8998_others_ops) {
792 int count = (desc->max - desc->min) / desc->step + 1;
793 regulators[index].n_voltages = count;
795 rdev[i] = regulator_register(&regulators[index], max8998->dev,
796 pdata->regulators[i].initdata, max8998);
797 if (IS_ERR(rdev[i])) {
798 ret = PTR_ERR(rdev[i]);
799 dev_err(max8998->dev, "regulator init failed\n");
800 rdev[i] = NULL;
801 goto err;
806 return 0;
807 err:
808 for (i = 0; i < max8998->num_regulators; i++)
809 if (rdev[i])
810 regulator_unregister(rdev[i]);
812 kfree(max8998->rdev);
813 kfree(max8998);
815 return ret;
818 static int __devexit max8998_pmic_remove(struct platform_device *pdev)
820 struct max8998_data *max8998 = platform_get_drvdata(pdev);
821 struct regulator_dev **rdev = max8998->rdev;
822 int i;
824 for (i = 0; i < max8998->num_regulators; i++)
825 if (rdev[i])
826 regulator_unregister(rdev[i]);
828 kfree(max8998->rdev);
829 kfree(max8998);
831 return 0;
834 static struct platform_driver max8998_pmic_driver = {
835 .driver = {
836 .name = "max8998-pmic",
837 .owner = THIS_MODULE,
839 .probe = max8998_pmic_probe,
840 .remove = __devexit_p(max8998_pmic_remove),
843 static int __init max8998_pmic_init(void)
845 return platform_driver_register(&max8998_pmic_driver);
847 subsys_initcall(max8998_pmic_init);
849 static void __exit max8998_pmic_cleanup(void)
851 platform_driver_unregister(&max8998_pmic_driver);
853 module_exit(max8998_pmic_cleanup);
855 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
856 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
857 MODULE_LICENSE("GPL");