rt2800: initialize BBP_R104 on proper subroutines
[linux/fpc-iii.git] / drivers / regulator / max8998.c
bloba57a1b15cdbaf1eb2d4f85c8d39bbc72ece4c812
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/platform_device.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/mfd/max8998.h>
34 #include <linux/mfd/max8998-private.h>
36 struct max8998_data {
37 struct device *dev;
38 struct max8998_dev *iodev;
39 int num_regulators;
40 struct regulator_dev **rdev;
41 u8 buck1_vol[4]; /* voltages for selection */
42 u8 buck2_vol[2];
43 unsigned int buck1_idx; /* index to last changed voltage */
44 /* value in a set */
45 unsigned int buck2_idx;
48 struct voltage_map_desc {
49 int min;
50 int max;
51 int step;
54 /* Voltage maps in uV*/
55 static const struct voltage_map_desc ldo23_voltage_map_desc = {
56 .min = 800000, .step = 50000, .max = 1300000,
58 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
59 .min = 1600000, .step = 100000, .max = 3600000,
61 static const struct voltage_map_desc ldo8_voltage_map_desc = {
62 .min = 3000000, .step = 100000, .max = 3600000,
64 static const struct voltage_map_desc ldo9_voltage_map_desc = {
65 .min = 2800000, .step = 100000, .max = 3100000,
67 static const struct voltage_map_desc ldo10_voltage_map_desc = {
68 .min = 950000, .step = 50000, .max = 1300000,
70 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
71 .min = 800000, .step = 100000, .max = 3300000,
73 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
74 .min = 1200000, .step = 100000, .max = 3300000,
76 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
77 .min = 1600000, .step = 100000, .max = 3600000,
79 static const struct voltage_map_desc buck12_voltage_map_desc = {
80 .min = 750000, .step = 25000, .max = 1525000,
82 static const struct voltage_map_desc buck3_voltage_map_desc = {
83 .min = 1600000, .step = 100000, .max = 3600000,
85 static const struct voltage_map_desc buck4_voltage_map_desc = {
86 .min = 800000, .step = 100000, .max = 2300000,
89 static const struct voltage_map_desc *ldo_voltage_map[] = {
90 NULL,
91 NULL,
92 &ldo23_voltage_map_desc, /* LDO2 */
93 &ldo23_voltage_map_desc, /* LDO3 */
94 &ldo456711_voltage_map_desc, /* LDO4 */
95 &ldo456711_voltage_map_desc, /* LDO5 */
96 &ldo456711_voltage_map_desc, /* LDO6 */
97 &ldo456711_voltage_map_desc, /* LDO7 */
98 &ldo8_voltage_map_desc, /* LDO8 */
99 &ldo9_voltage_map_desc, /* LDO9 */
100 &ldo10_voltage_map_desc, /* LDO10 */
101 &ldo456711_voltage_map_desc, /* LDO11 */
102 &ldo1213_voltage_map_desc, /* LDO12 */
103 &ldo1213_voltage_map_desc, /* LDO13 */
104 &ldo1415_voltage_map_desc, /* LDO14 */
105 &ldo1415_voltage_map_desc, /* LDO15 */
106 &ldo1617_voltage_map_desc, /* LDO16 */
107 &ldo1617_voltage_map_desc, /* LDO17 */
108 &buck12_voltage_map_desc, /* BUCK1 */
109 &buck12_voltage_map_desc, /* BUCK2 */
110 &buck3_voltage_map_desc, /* BUCK3 */
111 &buck4_voltage_map_desc, /* BUCK4 */
114 static int max8998_get_enable_register(struct regulator_dev *rdev,
115 int *reg, int *shift)
117 int ldo = rdev_get_id(rdev);
119 switch (ldo) {
120 case MAX8998_LDO2 ... MAX8998_LDO5:
121 *reg = MAX8998_REG_ONOFF1;
122 *shift = 3 - (ldo - MAX8998_LDO2);
123 break;
124 case MAX8998_LDO6 ... MAX8998_LDO13:
125 *reg = MAX8998_REG_ONOFF2;
126 *shift = 7 - (ldo - MAX8998_LDO6);
127 break;
128 case MAX8998_LDO14 ... MAX8998_LDO17:
129 *reg = MAX8998_REG_ONOFF3;
130 *shift = 7 - (ldo - MAX8998_LDO14);
131 break;
132 case MAX8998_BUCK1 ... MAX8998_BUCK4:
133 *reg = MAX8998_REG_ONOFF1;
134 *shift = 7 - (ldo - MAX8998_BUCK1);
135 break;
136 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
137 *reg = MAX8998_REG_ONOFF4;
138 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
139 break;
140 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
141 *reg = MAX8998_REG_CHGR2;
142 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
143 break;
144 default:
145 return -EINVAL;
148 return 0;
151 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
153 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
154 struct i2c_client *i2c = max8998->iodev->i2c;
155 int ret, reg, shift = 8;
156 u8 val;
158 ret = max8998_get_enable_register(rdev, &reg, &shift);
159 if (ret)
160 return ret;
162 ret = max8998_read_reg(i2c, reg, &val);
163 if (ret)
164 return ret;
166 return val & (1 << shift);
169 static int max8998_ldo_enable(struct regulator_dev *rdev)
171 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
172 struct i2c_client *i2c = max8998->iodev->i2c;
173 int reg, shift = 8, ret;
175 ret = max8998_get_enable_register(rdev, &reg, &shift);
176 if (ret)
177 return ret;
179 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
182 static int max8998_ldo_disable(struct regulator_dev *rdev)
184 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
185 struct i2c_client *i2c = max8998->iodev->i2c;
186 int reg, shift = 8, ret;
188 ret = max8998_get_enable_register(rdev, &reg, &shift);
189 if (ret)
190 return ret;
192 return max8998_update_reg(i2c, reg, 0, 1<<shift);
195 static int max8998_get_voltage_register(struct regulator_dev *rdev,
196 int *_reg, int *_shift, int *_mask)
198 int ldo = rdev_get_id(rdev);
199 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
200 int reg, shift = 0, mask = 0xff;
202 switch (ldo) {
203 case MAX8998_LDO2 ... MAX8998_LDO3:
204 reg = MAX8998_REG_LDO2_LDO3;
205 mask = 0xf;
206 if (ldo == MAX8998_LDO2)
207 shift = 4;
208 else
209 shift = 0;
210 break;
211 case MAX8998_LDO4 ... MAX8998_LDO7:
212 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
213 break;
214 case MAX8998_LDO8 ... MAX8998_LDO9:
215 reg = MAX8998_REG_LDO8_LDO9;
216 mask = 0xf;
217 if (ldo == MAX8998_LDO8)
218 shift = 4;
219 else
220 shift = 0;
221 break;
222 case MAX8998_LDO10 ... MAX8998_LDO11:
223 reg = MAX8998_REG_LDO10_LDO11;
224 if (ldo == MAX8998_LDO10) {
225 shift = 5;
226 mask = 0x7;
227 } else {
228 shift = 0;
229 mask = 0x1f;
231 break;
232 case MAX8998_LDO12 ... MAX8998_LDO17:
233 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
234 break;
235 case MAX8998_BUCK1:
236 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
237 break;
238 case MAX8998_BUCK2:
239 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
240 break;
241 case MAX8998_BUCK3:
242 reg = MAX8998_REG_BUCK3;
243 break;
244 case MAX8998_BUCK4:
245 reg = MAX8998_REG_BUCK4;
246 break;
247 default:
248 return -EINVAL;
251 *_reg = reg;
252 *_shift = shift;
253 *_mask = mask;
255 return 0;
258 static int max8998_get_voltage_sel(struct regulator_dev *rdev)
260 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
261 struct i2c_client *i2c = max8998->iodev->i2c;
262 int reg, shift = 0, mask, ret;
263 u8 val;
265 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
266 if (ret)
267 return ret;
269 ret = max8998_read_reg(i2c, reg, &val);
270 if (ret)
271 return ret;
273 val >>= shift;
274 val &= mask;
276 return val;
279 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
280 unsigned selector)
282 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
283 struct i2c_client *i2c = max8998->iodev->i2c;
284 int reg, shift = 0, mask, ret;
286 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
287 if (ret)
288 return ret;
290 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
292 return ret;
295 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
297 gpio_set_value(gpio1, v & 0x1);
298 gpio_set_value(gpio2, (v >> 1) & 0x1);
301 static inline void buck2_gpio_set(int gpio, int v)
303 gpio_set_value(gpio, v & 0x1);
306 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
307 unsigned selector)
309 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
310 struct max8998_platform_data *pdata =
311 dev_get_platdata(max8998->iodev->dev);
312 struct i2c_client *i2c = max8998->iodev->i2c;
313 int buck = rdev_get_id(rdev);
314 int reg, shift = 0, mask, ret, j;
315 static u8 buck1_last_val;
317 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
318 if (ret)
319 return ret;
321 switch (buck) {
322 case MAX8998_BUCK1:
323 dev_dbg(max8998->dev,
324 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
325 "buck1_vol3:%d, buck1_vol4:%d\n",
326 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
327 max8998->buck1_vol[2], max8998->buck1_vol[3]);
329 if (gpio_is_valid(pdata->buck1_set1) &&
330 gpio_is_valid(pdata->buck1_set2)) {
332 /* check if requested voltage */
333 /* value is already defined */
334 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
335 if (max8998->buck1_vol[j] == selector) {
336 max8998->buck1_idx = j;
337 buck1_gpio_set(pdata->buck1_set1,
338 pdata->buck1_set2, j);
339 goto buck1_exit;
343 if (pdata->buck_voltage_lock)
344 return -EINVAL;
346 /* no predefine regulator found */
347 max8998->buck1_idx = (buck1_last_val % 2) + 2;
348 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
349 max8998->buck1_idx);
350 max8998->buck1_vol[max8998->buck1_idx] = selector;
351 ret = max8998_get_voltage_register(rdev, &reg,
352 &shift,
353 &mask);
354 ret = max8998_write_reg(i2c, reg, selector);
355 buck1_gpio_set(pdata->buck1_set1,
356 pdata->buck1_set2, max8998->buck1_idx);
357 buck1_last_val++;
358 buck1_exit:
359 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
360 i2c->name, gpio_get_value(pdata->buck1_set1),
361 gpio_get_value(pdata->buck1_set2));
362 break;
363 } else {
364 ret = max8998_write_reg(i2c, reg, selector);
366 break;
368 case MAX8998_BUCK2:
369 dev_dbg(max8998->dev,
370 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
371 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
372 if (gpio_is_valid(pdata->buck2_set3)) {
374 /* check if requested voltage */
375 /* value is already defined */
376 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
377 if (max8998->buck2_vol[j] == selector) {
378 max8998->buck2_idx = j;
379 buck2_gpio_set(pdata->buck2_set3, j);
380 goto buck2_exit;
384 if (pdata->buck_voltage_lock)
385 return -EINVAL;
387 max8998_get_voltage_register(rdev,
388 &reg, &shift, &mask);
389 ret = max8998_write_reg(i2c, reg, selector);
390 max8998->buck2_vol[max8998->buck2_idx] = selector;
391 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
392 buck2_exit:
393 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
394 gpio_get_value(pdata->buck2_set3));
395 } else {
396 ret = max8998_write_reg(i2c, reg, selector);
398 break;
400 case MAX8998_BUCK3:
401 case MAX8998_BUCK4:
402 ret = max8998_update_reg(i2c, reg, selector<<shift,
403 mask<<shift);
404 break;
407 return ret;
410 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
411 unsigned int old_selector,
412 unsigned int new_selector)
414 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
415 struct i2c_client *i2c = max8998->iodev->i2c;
416 const struct voltage_map_desc *desc;
417 int buck = rdev_get_id(rdev);
418 u8 val = 0;
419 int difference, ret;
421 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
422 return -EINVAL;
424 desc = ldo_voltage_map[buck];
426 /* Voltage stabilization */
427 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
428 if (ret)
429 return ret;
431 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
432 /* MAX8998 has ENRAMP bit implemented, so test it*/
433 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
434 return 0;
436 difference = (new_selector - old_selector) * desc->step / 1000;
437 if (difference > 0)
438 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
440 return 0;
443 static struct regulator_ops max8998_ldo_ops = {
444 .list_voltage = regulator_list_voltage_linear,
445 .map_voltage = regulator_map_voltage_linear,
446 .is_enabled = max8998_ldo_is_enabled,
447 .enable = max8998_ldo_enable,
448 .disable = max8998_ldo_disable,
449 .get_voltage_sel = max8998_get_voltage_sel,
450 .set_voltage_sel = max8998_set_voltage_ldo_sel,
453 static struct regulator_ops max8998_buck_ops = {
454 .list_voltage = regulator_list_voltage_linear,
455 .map_voltage = regulator_map_voltage_linear,
456 .is_enabled = max8998_ldo_is_enabled,
457 .enable = max8998_ldo_enable,
458 .disable = max8998_ldo_disable,
459 .get_voltage_sel = max8998_get_voltage_sel,
460 .set_voltage_sel = max8998_set_voltage_buck_sel,
461 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
464 static struct regulator_ops max8998_others_ops = {
465 .is_enabled = max8998_ldo_is_enabled,
466 .enable = max8998_ldo_enable,
467 .disable = max8998_ldo_disable,
470 static struct regulator_desc regulators[] = {
472 .name = "LDO2",
473 .id = MAX8998_LDO2,
474 .ops = &max8998_ldo_ops,
475 .type = REGULATOR_VOLTAGE,
476 .owner = THIS_MODULE,
477 }, {
478 .name = "LDO3",
479 .id = MAX8998_LDO3,
480 .ops = &max8998_ldo_ops,
481 .type = REGULATOR_VOLTAGE,
482 .owner = THIS_MODULE,
483 }, {
484 .name = "LDO4",
485 .id = MAX8998_LDO4,
486 .ops = &max8998_ldo_ops,
487 .type = REGULATOR_VOLTAGE,
488 .owner = THIS_MODULE,
489 }, {
490 .name = "LDO5",
491 .id = MAX8998_LDO5,
492 .ops = &max8998_ldo_ops,
493 .type = REGULATOR_VOLTAGE,
494 .owner = THIS_MODULE,
495 }, {
496 .name = "LDO6",
497 .id = MAX8998_LDO6,
498 .ops = &max8998_ldo_ops,
499 .type = REGULATOR_VOLTAGE,
500 .owner = THIS_MODULE,
501 }, {
502 .name = "LDO7",
503 .id = MAX8998_LDO7,
504 .ops = &max8998_ldo_ops,
505 .type = REGULATOR_VOLTAGE,
506 .owner = THIS_MODULE,
507 }, {
508 .name = "LDO8",
509 .id = MAX8998_LDO8,
510 .ops = &max8998_ldo_ops,
511 .type = REGULATOR_VOLTAGE,
512 .owner = THIS_MODULE,
513 }, {
514 .name = "LDO9",
515 .id = MAX8998_LDO9,
516 .ops = &max8998_ldo_ops,
517 .type = REGULATOR_VOLTAGE,
518 .owner = THIS_MODULE,
519 }, {
520 .name = "LDO10",
521 .id = MAX8998_LDO10,
522 .ops = &max8998_ldo_ops,
523 .type = REGULATOR_VOLTAGE,
524 .owner = THIS_MODULE,
525 }, {
526 .name = "LDO11",
527 .id = MAX8998_LDO11,
528 .ops = &max8998_ldo_ops,
529 .type = REGULATOR_VOLTAGE,
530 .owner = THIS_MODULE,
531 }, {
532 .name = "LDO12",
533 .id = MAX8998_LDO12,
534 .ops = &max8998_ldo_ops,
535 .type = REGULATOR_VOLTAGE,
536 .owner = THIS_MODULE,
537 }, {
538 .name = "LDO13",
539 .id = MAX8998_LDO13,
540 .ops = &max8998_ldo_ops,
541 .type = REGULATOR_VOLTAGE,
542 .owner = THIS_MODULE,
543 }, {
544 .name = "LDO14",
545 .id = MAX8998_LDO14,
546 .ops = &max8998_ldo_ops,
547 .type = REGULATOR_VOLTAGE,
548 .owner = THIS_MODULE,
549 }, {
550 .name = "LDO15",
551 .id = MAX8998_LDO15,
552 .ops = &max8998_ldo_ops,
553 .type = REGULATOR_VOLTAGE,
554 .owner = THIS_MODULE,
555 }, {
556 .name = "LDO16",
557 .id = MAX8998_LDO16,
558 .ops = &max8998_ldo_ops,
559 .type = REGULATOR_VOLTAGE,
560 .owner = THIS_MODULE,
561 }, {
562 .name = "LDO17",
563 .id = MAX8998_LDO17,
564 .ops = &max8998_ldo_ops,
565 .type = REGULATOR_VOLTAGE,
566 .owner = THIS_MODULE,
567 }, {
568 .name = "BUCK1",
569 .id = MAX8998_BUCK1,
570 .ops = &max8998_buck_ops,
571 .type = REGULATOR_VOLTAGE,
572 .owner = THIS_MODULE,
573 }, {
574 .name = "BUCK2",
575 .id = MAX8998_BUCK2,
576 .ops = &max8998_buck_ops,
577 .type = REGULATOR_VOLTAGE,
578 .owner = THIS_MODULE,
579 }, {
580 .name = "BUCK3",
581 .id = MAX8998_BUCK3,
582 .ops = &max8998_buck_ops,
583 .type = REGULATOR_VOLTAGE,
584 .owner = THIS_MODULE,
585 }, {
586 .name = "BUCK4",
587 .id = MAX8998_BUCK4,
588 .ops = &max8998_buck_ops,
589 .type = REGULATOR_VOLTAGE,
590 .owner = THIS_MODULE,
591 }, {
592 .name = "EN32KHz AP",
593 .id = MAX8998_EN32KHZ_AP,
594 .ops = &max8998_others_ops,
595 .type = REGULATOR_VOLTAGE,
596 .owner = THIS_MODULE,
597 }, {
598 .name = "EN32KHz CP",
599 .id = MAX8998_EN32KHZ_CP,
600 .ops = &max8998_others_ops,
601 .type = REGULATOR_VOLTAGE,
602 .owner = THIS_MODULE,
603 }, {
604 .name = "ENVICHG",
605 .id = MAX8998_ENVICHG,
606 .ops = &max8998_others_ops,
607 .type = REGULATOR_VOLTAGE,
608 .owner = THIS_MODULE,
609 }, {
610 .name = "ESAFEOUT1",
611 .id = MAX8998_ESAFEOUT1,
612 .ops = &max8998_others_ops,
613 .type = REGULATOR_VOLTAGE,
614 .owner = THIS_MODULE,
615 }, {
616 .name = "ESAFEOUT2",
617 .id = MAX8998_ESAFEOUT2,
618 .ops = &max8998_others_ops,
619 .type = REGULATOR_VOLTAGE,
620 .owner = THIS_MODULE,
624 static int max8998_pmic_probe(struct platform_device *pdev)
626 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
627 struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
628 struct regulator_config config = { };
629 struct regulator_dev **rdev;
630 struct max8998_data *max8998;
631 struct i2c_client *i2c;
632 int i, ret, size;
634 if (!pdata) {
635 dev_err(pdev->dev.parent, "No platform init data supplied\n");
636 return -ENODEV;
639 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
640 GFP_KERNEL);
641 if (!max8998)
642 return -ENOMEM;
644 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
645 max8998->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
646 if (!max8998->rdev)
647 return -ENOMEM;
649 rdev = max8998->rdev;
650 max8998->dev = &pdev->dev;
651 max8998->iodev = iodev;
652 max8998->num_regulators = pdata->num_regulators;
653 platform_set_drvdata(pdev, max8998);
654 i2c = max8998->iodev->i2c;
656 max8998->buck1_idx = pdata->buck1_default_idx;
657 max8998->buck2_idx = pdata->buck2_default_idx;
659 /* NOTE: */
660 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
661 /* will be displayed */
663 /* Check if MAX8998 voltage selection GPIOs are defined */
664 if (gpio_is_valid(pdata->buck1_set1) &&
665 gpio_is_valid(pdata->buck1_set2)) {
666 /* Check if SET1 is not equal to 0 */
667 if (!pdata->buck1_set1) {
668 dev_err(&pdev->dev,
669 "MAX8998 SET1 GPIO defined as 0 !\n");
670 WARN_ON(!pdata->buck1_set1);
671 ret = -EIO;
672 goto err_out;
674 /* Check if SET2 is not equal to 0 */
675 if (!pdata->buck1_set2) {
676 dev_err(&pdev->dev,
677 "MAX8998 SET2 GPIO defined as 0 !\n");
678 WARN_ON(!pdata->buck1_set2);
679 ret = -EIO;
680 goto err_out;
683 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
684 gpio_direction_output(pdata->buck1_set1,
685 max8998->buck1_idx & 0x1);
688 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
689 gpio_direction_output(pdata->buck1_set2,
690 (max8998->buck1_idx >> 1) & 0x1);
691 /* Set predefined value for BUCK1 register 1 */
692 i = 0;
693 while (buck12_voltage_map_desc.min +
694 buck12_voltage_map_desc.step*i
695 < pdata->buck1_voltage1)
696 i++;
697 max8998->buck1_vol[0] = i;
698 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
699 if (ret)
700 goto err_out;
702 /* Set predefined value for BUCK1 register 2 */
703 i = 0;
704 while (buck12_voltage_map_desc.min +
705 buck12_voltage_map_desc.step*i
706 < pdata->buck1_voltage2)
707 i++;
709 max8998->buck1_vol[1] = i;
710 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i);
711 if (ret)
712 goto err_out;
714 /* Set predefined value for BUCK1 register 3 */
715 i = 0;
716 while (buck12_voltage_map_desc.min +
717 buck12_voltage_map_desc.step*i
718 < pdata->buck1_voltage3)
719 i++;
721 max8998->buck1_vol[2] = i;
722 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i);
723 if (ret)
724 goto err_out;
726 /* Set predefined value for BUCK1 register 4 */
727 i = 0;
728 while (buck12_voltage_map_desc.min +
729 buck12_voltage_map_desc.step*i
730 < pdata->buck1_voltage4)
731 i++;
733 max8998->buck1_vol[3] = i;
734 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i);
735 if (ret)
736 goto err_out;
740 if (gpio_is_valid(pdata->buck2_set3)) {
741 /* Check if SET3 is not equal to 0 */
742 if (!pdata->buck2_set3) {
743 dev_err(&pdev->dev,
744 "MAX8998 SET3 GPIO defined as 0 !\n");
745 WARN_ON(!pdata->buck2_set3);
746 ret = -EIO;
747 goto err_out;
749 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
750 gpio_direction_output(pdata->buck2_set3,
751 max8998->buck2_idx & 0x1);
753 /* BUCK2 register 1 */
754 i = 0;
755 while (buck12_voltage_map_desc.min +
756 buck12_voltage_map_desc.step*i
757 < pdata->buck2_voltage1)
758 i++;
759 max8998->buck2_vol[0] = i;
760 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
761 if (ret)
762 goto err_out;
764 /* BUCK2 register 2 */
765 i = 0;
766 while (buck12_voltage_map_desc.min +
767 buck12_voltage_map_desc.step*i
768 < pdata->buck2_voltage2)
769 i++;
770 max8998->buck2_vol[1] = i;
771 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i);
772 if (ret)
773 goto err_out;
776 for (i = 0; i < pdata->num_regulators; i++) {
777 const struct voltage_map_desc *desc;
778 int id = pdata->regulators[i].id;
779 int index = id - MAX8998_LDO2;
781 desc = ldo_voltage_map[id];
782 if (desc && regulators[index].ops != &max8998_others_ops) {
783 int count = (desc->max - desc->min) / desc->step + 1;
785 regulators[index].n_voltages = count;
786 regulators[index].min_uV = desc->min;
787 regulators[index].uV_step = desc->step;
790 config.dev = max8998->dev;
791 config.init_data = pdata->regulators[i].initdata;
792 config.driver_data = max8998;
794 rdev[i] = regulator_register(&regulators[index], &config);
795 if (IS_ERR(rdev[i])) {
796 ret = PTR_ERR(rdev[i]);
797 dev_err(max8998->dev, "regulator init failed\n");
798 rdev[i] = NULL;
799 goto err;
804 return 0;
805 err:
806 while (--i >= 0)
807 regulator_unregister(rdev[i]);
808 err_out:
809 return ret;
812 static int max8998_pmic_remove(struct platform_device *pdev)
814 struct max8998_data *max8998 = platform_get_drvdata(pdev);
815 struct regulator_dev **rdev = max8998->rdev;
816 int i;
818 for (i = 0; i < max8998->num_regulators; i++)
819 regulator_unregister(rdev[i]);
820 return 0;
823 static const struct platform_device_id max8998_pmic_id[] = {
824 { "max8998-pmic", TYPE_MAX8998 },
825 { "lp3974-pmic", TYPE_LP3974 },
828 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
830 static struct platform_driver max8998_pmic_driver = {
831 .driver = {
832 .name = "max8998-pmic",
833 .owner = THIS_MODULE,
835 .probe = max8998_pmic_probe,
836 .remove = max8998_pmic_remove,
837 .id_table = max8998_pmic_id,
840 static int __init max8998_pmic_init(void)
842 return platform_driver_register(&max8998_pmic_driver);
844 subsys_initcall(max8998_pmic_init);
846 static void __exit max8998_pmic_cleanup(void)
848 platform_driver_unregister(&max8998_pmic_driver);
850 module_exit(max8998_pmic_cleanup);
852 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
853 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
854 MODULE_LICENSE("GPL");