ARM: 7989/1: Delete asm/system.h
[linux/fpc-iii.git] / drivers / regulator / max8998.c
blobae3f0656feb00c406ca438642c051a6453ab9171
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/of.h>
32 #include <linux/of_gpio.h>
33 #include <linux/platform_device.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/of_regulator.h>
36 #include <linux/mfd/max8998.h>
37 #include <linux/mfd/max8998-private.h>
39 struct max8998_data {
40 struct device *dev;
41 struct max8998_dev *iodev;
42 int num_regulators;
43 struct regulator_dev **rdev;
44 u8 buck1_vol[4]; /* voltages for selection */
45 u8 buck2_vol[2];
46 unsigned int buck1_idx; /* index to last changed voltage */
47 /* value in a set */
48 unsigned int buck2_idx;
51 struct voltage_map_desc {
52 int min;
53 int max;
54 int step;
57 /* Voltage maps in uV*/
58 static const struct voltage_map_desc ldo23_voltage_map_desc = {
59 .min = 800000, .step = 50000, .max = 1300000,
61 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
62 .min = 1600000, .step = 100000, .max = 3600000,
64 static const struct voltage_map_desc ldo8_voltage_map_desc = {
65 .min = 3000000, .step = 100000, .max = 3600000,
67 static const struct voltage_map_desc ldo9_voltage_map_desc = {
68 .min = 2800000, .step = 100000, .max = 3100000,
70 static const struct voltage_map_desc ldo10_voltage_map_desc = {
71 .min = 950000, .step = 50000, .max = 1300000,
73 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
74 .min = 800000, .step = 100000, .max = 3300000,
76 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
77 .min = 1200000, .step = 100000, .max = 3300000,
79 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
80 .min = 1600000, .step = 100000, .max = 3600000,
82 static const struct voltage_map_desc buck12_voltage_map_desc = {
83 .min = 750000, .step = 25000, .max = 1525000,
85 static const struct voltage_map_desc buck3_voltage_map_desc = {
86 .min = 1600000, .step = 100000, .max = 3600000,
88 static const struct voltage_map_desc buck4_voltage_map_desc = {
89 .min = 800000, .step = 100000, .max = 2300000,
92 static const struct voltage_map_desc *ldo_voltage_map[] = {
93 NULL,
94 NULL,
95 &ldo23_voltage_map_desc, /* LDO2 */
96 &ldo23_voltage_map_desc, /* LDO3 */
97 &ldo456711_voltage_map_desc, /* LDO4 */
98 &ldo456711_voltage_map_desc, /* LDO5 */
99 &ldo456711_voltage_map_desc, /* LDO6 */
100 &ldo456711_voltage_map_desc, /* LDO7 */
101 &ldo8_voltage_map_desc, /* LDO8 */
102 &ldo9_voltage_map_desc, /* LDO9 */
103 &ldo10_voltage_map_desc, /* LDO10 */
104 &ldo456711_voltage_map_desc, /* LDO11 */
105 &ldo1213_voltage_map_desc, /* LDO12 */
106 &ldo1213_voltage_map_desc, /* LDO13 */
107 &ldo1415_voltage_map_desc, /* LDO14 */
108 &ldo1415_voltage_map_desc, /* LDO15 */
109 &ldo1617_voltage_map_desc, /* LDO16 */
110 &ldo1617_voltage_map_desc, /* LDO17 */
111 &buck12_voltage_map_desc, /* BUCK1 */
112 &buck12_voltage_map_desc, /* BUCK2 */
113 &buck3_voltage_map_desc, /* BUCK3 */
114 &buck4_voltage_map_desc, /* BUCK4 */
117 static int max8998_get_enable_register(struct regulator_dev *rdev,
118 int *reg, int *shift)
120 int ldo = rdev_get_id(rdev);
122 switch (ldo) {
123 case MAX8998_LDO2 ... MAX8998_LDO5:
124 *reg = MAX8998_REG_ONOFF1;
125 *shift = 3 - (ldo - MAX8998_LDO2);
126 break;
127 case MAX8998_LDO6 ... MAX8998_LDO13:
128 *reg = MAX8998_REG_ONOFF2;
129 *shift = 7 - (ldo - MAX8998_LDO6);
130 break;
131 case MAX8998_LDO14 ... MAX8998_LDO17:
132 *reg = MAX8998_REG_ONOFF3;
133 *shift = 7 - (ldo - MAX8998_LDO14);
134 break;
135 case MAX8998_BUCK1 ... MAX8998_BUCK4:
136 *reg = MAX8998_REG_ONOFF1;
137 *shift = 7 - (ldo - MAX8998_BUCK1);
138 break;
139 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
140 *reg = MAX8998_REG_ONOFF4;
141 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
142 break;
143 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
144 *reg = MAX8998_REG_CHGR2;
145 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
146 break;
147 default:
148 return -EINVAL;
151 return 0;
154 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
156 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
157 struct i2c_client *i2c = max8998->iodev->i2c;
158 int ret, reg, shift = 8;
159 u8 val;
161 ret = max8998_get_enable_register(rdev, &reg, &shift);
162 if (ret)
163 return ret;
165 ret = max8998_read_reg(i2c, reg, &val);
166 if (ret)
167 return ret;
169 return val & (1 << shift);
172 static int max8998_ldo_enable(struct regulator_dev *rdev)
174 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
175 struct i2c_client *i2c = max8998->iodev->i2c;
176 int reg, shift = 8, ret;
178 ret = max8998_get_enable_register(rdev, &reg, &shift);
179 if (ret)
180 return ret;
182 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
185 static int max8998_ldo_disable(struct regulator_dev *rdev)
187 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
188 struct i2c_client *i2c = max8998->iodev->i2c;
189 int reg, shift = 8, ret;
191 ret = max8998_get_enable_register(rdev, &reg, &shift);
192 if (ret)
193 return ret;
195 return max8998_update_reg(i2c, reg, 0, 1<<shift);
198 static int max8998_get_voltage_register(struct regulator_dev *rdev,
199 int *_reg, int *_shift, int *_mask)
201 int ldo = rdev_get_id(rdev);
202 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
203 int reg, shift = 0, mask = 0xff;
205 switch (ldo) {
206 case MAX8998_LDO2 ... MAX8998_LDO3:
207 reg = MAX8998_REG_LDO2_LDO3;
208 mask = 0xf;
209 if (ldo == MAX8998_LDO2)
210 shift = 4;
211 else
212 shift = 0;
213 break;
214 case MAX8998_LDO4 ... MAX8998_LDO7:
215 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
216 break;
217 case MAX8998_LDO8 ... MAX8998_LDO9:
218 reg = MAX8998_REG_LDO8_LDO9;
219 mask = 0xf;
220 if (ldo == MAX8998_LDO8)
221 shift = 4;
222 else
223 shift = 0;
224 break;
225 case MAX8998_LDO10 ... MAX8998_LDO11:
226 reg = MAX8998_REG_LDO10_LDO11;
227 if (ldo == MAX8998_LDO10) {
228 shift = 5;
229 mask = 0x7;
230 } else {
231 shift = 0;
232 mask = 0x1f;
234 break;
235 case MAX8998_LDO12 ... MAX8998_LDO17:
236 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
237 break;
238 case MAX8998_BUCK1:
239 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
240 break;
241 case MAX8998_BUCK2:
242 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
243 break;
244 case MAX8998_BUCK3:
245 reg = MAX8998_REG_BUCK3;
246 break;
247 case MAX8998_BUCK4:
248 reg = MAX8998_REG_BUCK4;
249 break;
250 default:
251 return -EINVAL;
254 *_reg = reg;
255 *_shift = shift;
256 *_mask = mask;
258 return 0;
261 static int max8998_get_voltage_sel(struct regulator_dev *rdev)
263 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
264 struct i2c_client *i2c = max8998->iodev->i2c;
265 int reg, shift = 0, mask, ret;
266 u8 val;
268 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
269 if (ret)
270 return ret;
272 ret = max8998_read_reg(i2c, reg, &val);
273 if (ret)
274 return ret;
276 val >>= shift;
277 val &= mask;
279 return val;
282 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
283 unsigned selector)
285 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
286 struct i2c_client *i2c = max8998->iodev->i2c;
287 int reg, shift = 0, mask, ret;
289 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
290 if (ret)
291 return ret;
293 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
295 return ret;
298 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
300 gpio_set_value(gpio1, v & 0x1);
301 gpio_set_value(gpio2, (v >> 1) & 0x1);
304 static inline void buck2_gpio_set(int gpio, int v)
306 gpio_set_value(gpio, v & 0x1);
309 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
310 unsigned selector)
312 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
313 struct max8998_platform_data *pdata =
314 dev_get_platdata(max8998->iodev->dev);
315 struct i2c_client *i2c = max8998->iodev->i2c;
316 int buck = rdev_get_id(rdev);
317 int reg, shift = 0, mask, ret, j;
318 static u8 buck1_last_val;
320 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
321 if (ret)
322 return ret;
324 switch (buck) {
325 case MAX8998_BUCK1:
326 dev_dbg(max8998->dev,
327 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
328 "buck1_vol3:%d, buck1_vol4:%d\n",
329 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
330 max8998->buck1_vol[2], max8998->buck1_vol[3]);
332 if (gpio_is_valid(pdata->buck1_set1) &&
333 gpio_is_valid(pdata->buck1_set2)) {
335 /* check if requested voltage */
336 /* value is already defined */
337 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
338 if (max8998->buck1_vol[j] == selector) {
339 max8998->buck1_idx = j;
340 buck1_gpio_set(pdata->buck1_set1,
341 pdata->buck1_set2, j);
342 goto buck1_exit;
346 if (pdata->buck_voltage_lock)
347 return -EINVAL;
349 /* no predefine regulator found */
350 max8998->buck1_idx = (buck1_last_val % 2) + 2;
351 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
352 max8998->buck1_idx);
353 max8998->buck1_vol[max8998->buck1_idx] = selector;
354 ret = max8998_get_voltage_register(rdev, &reg,
355 &shift,
356 &mask);
357 ret = max8998_write_reg(i2c, reg, selector);
358 buck1_gpio_set(pdata->buck1_set1,
359 pdata->buck1_set2, max8998->buck1_idx);
360 buck1_last_val++;
361 buck1_exit:
362 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
363 i2c->name, gpio_get_value(pdata->buck1_set1),
364 gpio_get_value(pdata->buck1_set2));
365 break;
366 } else {
367 ret = max8998_write_reg(i2c, reg, selector);
369 break;
371 case MAX8998_BUCK2:
372 dev_dbg(max8998->dev,
373 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
374 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
375 if (gpio_is_valid(pdata->buck2_set3)) {
377 /* check if requested voltage */
378 /* value is already defined */
379 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
380 if (max8998->buck2_vol[j] == selector) {
381 max8998->buck2_idx = j;
382 buck2_gpio_set(pdata->buck2_set3, j);
383 goto buck2_exit;
387 if (pdata->buck_voltage_lock)
388 return -EINVAL;
390 max8998_get_voltage_register(rdev,
391 &reg, &shift, &mask);
392 ret = max8998_write_reg(i2c, reg, selector);
393 max8998->buck2_vol[max8998->buck2_idx] = selector;
394 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
395 buck2_exit:
396 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
397 gpio_get_value(pdata->buck2_set3));
398 } else {
399 ret = max8998_write_reg(i2c, reg, selector);
401 break;
403 case MAX8998_BUCK3:
404 case MAX8998_BUCK4:
405 ret = max8998_update_reg(i2c, reg, selector<<shift,
406 mask<<shift);
407 break;
410 return ret;
413 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
414 unsigned int old_selector,
415 unsigned int new_selector)
417 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
418 struct i2c_client *i2c = max8998->iodev->i2c;
419 const struct voltage_map_desc *desc;
420 int buck = rdev_get_id(rdev);
421 u8 val = 0;
422 int difference, ret;
424 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
425 return -EINVAL;
427 desc = ldo_voltage_map[buck];
429 /* Voltage stabilization */
430 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
431 if (ret)
432 return ret;
434 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
435 /* MAX8998 has ENRAMP bit implemented, so test it*/
436 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
437 return 0;
439 difference = (new_selector - old_selector) * desc->step / 1000;
440 if (difference > 0)
441 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
443 return 0;
446 static struct regulator_ops max8998_ldo_ops = {
447 .list_voltage = regulator_list_voltage_linear,
448 .map_voltage = regulator_map_voltage_linear,
449 .is_enabled = max8998_ldo_is_enabled,
450 .enable = max8998_ldo_enable,
451 .disable = max8998_ldo_disable,
452 .get_voltage_sel = max8998_get_voltage_sel,
453 .set_voltage_sel = max8998_set_voltage_ldo_sel,
456 static struct regulator_ops max8998_buck_ops = {
457 .list_voltage = regulator_list_voltage_linear,
458 .map_voltage = regulator_map_voltage_linear,
459 .is_enabled = max8998_ldo_is_enabled,
460 .enable = max8998_ldo_enable,
461 .disable = max8998_ldo_disable,
462 .get_voltage_sel = max8998_get_voltage_sel,
463 .set_voltage_sel = max8998_set_voltage_buck_sel,
464 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
467 static struct regulator_ops max8998_others_ops = {
468 .is_enabled = max8998_ldo_is_enabled,
469 .enable = max8998_ldo_enable,
470 .disable = max8998_ldo_disable,
473 static struct regulator_desc regulators[] = {
475 .name = "LDO2",
476 .id = MAX8998_LDO2,
477 .ops = &max8998_ldo_ops,
478 .type = REGULATOR_VOLTAGE,
479 .owner = THIS_MODULE,
480 }, {
481 .name = "LDO3",
482 .id = MAX8998_LDO3,
483 .ops = &max8998_ldo_ops,
484 .type = REGULATOR_VOLTAGE,
485 .owner = THIS_MODULE,
486 }, {
487 .name = "LDO4",
488 .id = MAX8998_LDO4,
489 .ops = &max8998_ldo_ops,
490 .type = REGULATOR_VOLTAGE,
491 .owner = THIS_MODULE,
492 }, {
493 .name = "LDO5",
494 .id = MAX8998_LDO5,
495 .ops = &max8998_ldo_ops,
496 .type = REGULATOR_VOLTAGE,
497 .owner = THIS_MODULE,
498 }, {
499 .name = "LDO6",
500 .id = MAX8998_LDO6,
501 .ops = &max8998_ldo_ops,
502 .type = REGULATOR_VOLTAGE,
503 .owner = THIS_MODULE,
504 }, {
505 .name = "LDO7",
506 .id = MAX8998_LDO7,
507 .ops = &max8998_ldo_ops,
508 .type = REGULATOR_VOLTAGE,
509 .owner = THIS_MODULE,
510 }, {
511 .name = "LDO8",
512 .id = MAX8998_LDO8,
513 .ops = &max8998_ldo_ops,
514 .type = REGULATOR_VOLTAGE,
515 .owner = THIS_MODULE,
516 }, {
517 .name = "LDO9",
518 .id = MAX8998_LDO9,
519 .ops = &max8998_ldo_ops,
520 .type = REGULATOR_VOLTAGE,
521 .owner = THIS_MODULE,
522 }, {
523 .name = "LDO10",
524 .id = MAX8998_LDO10,
525 .ops = &max8998_ldo_ops,
526 .type = REGULATOR_VOLTAGE,
527 .owner = THIS_MODULE,
528 }, {
529 .name = "LDO11",
530 .id = MAX8998_LDO11,
531 .ops = &max8998_ldo_ops,
532 .type = REGULATOR_VOLTAGE,
533 .owner = THIS_MODULE,
534 }, {
535 .name = "LDO12",
536 .id = MAX8998_LDO12,
537 .ops = &max8998_ldo_ops,
538 .type = REGULATOR_VOLTAGE,
539 .owner = THIS_MODULE,
540 }, {
541 .name = "LDO13",
542 .id = MAX8998_LDO13,
543 .ops = &max8998_ldo_ops,
544 .type = REGULATOR_VOLTAGE,
545 .owner = THIS_MODULE,
546 }, {
547 .name = "LDO14",
548 .id = MAX8998_LDO14,
549 .ops = &max8998_ldo_ops,
550 .type = REGULATOR_VOLTAGE,
551 .owner = THIS_MODULE,
552 }, {
553 .name = "LDO15",
554 .id = MAX8998_LDO15,
555 .ops = &max8998_ldo_ops,
556 .type = REGULATOR_VOLTAGE,
557 .owner = THIS_MODULE,
558 }, {
559 .name = "LDO16",
560 .id = MAX8998_LDO16,
561 .ops = &max8998_ldo_ops,
562 .type = REGULATOR_VOLTAGE,
563 .owner = THIS_MODULE,
564 }, {
565 .name = "LDO17",
566 .id = MAX8998_LDO17,
567 .ops = &max8998_ldo_ops,
568 .type = REGULATOR_VOLTAGE,
569 .owner = THIS_MODULE,
570 }, {
571 .name = "BUCK1",
572 .id = MAX8998_BUCK1,
573 .ops = &max8998_buck_ops,
574 .type = REGULATOR_VOLTAGE,
575 .owner = THIS_MODULE,
576 }, {
577 .name = "BUCK2",
578 .id = MAX8998_BUCK2,
579 .ops = &max8998_buck_ops,
580 .type = REGULATOR_VOLTAGE,
581 .owner = THIS_MODULE,
582 }, {
583 .name = "BUCK3",
584 .id = MAX8998_BUCK3,
585 .ops = &max8998_buck_ops,
586 .type = REGULATOR_VOLTAGE,
587 .owner = THIS_MODULE,
588 }, {
589 .name = "BUCK4",
590 .id = MAX8998_BUCK4,
591 .ops = &max8998_buck_ops,
592 .type = REGULATOR_VOLTAGE,
593 .owner = THIS_MODULE,
594 }, {
595 .name = "EN32KHz-AP",
596 .id = MAX8998_EN32KHZ_AP,
597 .ops = &max8998_others_ops,
598 .type = REGULATOR_VOLTAGE,
599 .owner = THIS_MODULE,
600 }, {
601 .name = "EN32KHz-CP",
602 .id = MAX8998_EN32KHZ_CP,
603 .ops = &max8998_others_ops,
604 .type = REGULATOR_VOLTAGE,
605 .owner = THIS_MODULE,
606 }, {
607 .name = "ENVICHG",
608 .id = MAX8998_ENVICHG,
609 .ops = &max8998_others_ops,
610 .type = REGULATOR_VOLTAGE,
611 .owner = THIS_MODULE,
612 }, {
613 .name = "ESAFEOUT1",
614 .id = MAX8998_ESAFEOUT1,
615 .ops = &max8998_others_ops,
616 .type = REGULATOR_VOLTAGE,
617 .owner = THIS_MODULE,
618 }, {
619 .name = "ESAFEOUT2",
620 .id = MAX8998_ESAFEOUT2,
621 .ops = &max8998_others_ops,
622 .type = REGULATOR_VOLTAGE,
623 .owner = THIS_MODULE,
627 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
628 struct max8998_platform_data *pdata,
629 struct device_node *pmic_np)
631 int gpio;
633 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
634 if (!gpio_is_valid(gpio)) {
635 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
636 return -EINVAL;
638 pdata->buck1_set1 = gpio;
640 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
641 if (!gpio_is_valid(gpio)) {
642 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
643 return -EINVAL;
645 pdata->buck1_set2 = gpio;
647 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
648 if (!gpio_is_valid(gpio)) {
649 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
650 return -EINVAL;
652 pdata->buck2_set3 = gpio;
654 return 0;
657 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
658 struct max8998_platform_data *pdata)
660 struct device_node *pmic_np = iodev->dev->of_node;
661 struct device_node *regulators_np, *reg_np;
662 struct max8998_regulator_data *rdata;
663 unsigned int i;
664 int ret;
666 regulators_np = of_get_child_by_name(pmic_np, "regulators");
667 if (!regulators_np) {
668 dev_err(iodev->dev, "could not find regulators sub-node\n");
669 return -EINVAL;
672 /* count the number of regulators to be supported in pmic */
673 pdata->num_regulators = of_get_child_count(regulators_np);
675 rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
676 pdata->num_regulators, GFP_KERNEL);
677 if (!rdata)
678 return -ENOMEM;
680 pdata->regulators = rdata;
681 for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
682 reg_np = of_get_child_by_name(regulators_np,
683 regulators[i].name);
684 if (!reg_np)
685 continue;
687 rdata->id = regulators[i].id;
688 rdata->initdata = of_get_regulator_init_data(
689 iodev->dev, reg_np);
690 rdata->reg_node = reg_np;
691 ++rdata;
693 pdata->num_regulators = rdata - pdata->regulators;
695 ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
696 if (ret)
697 return -EINVAL;
699 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
700 pdata->buck_voltage_lock = true;
702 ret = of_property_read_u32(pmic_np,
703 "max8998,pmic-buck1-default-dvs-idx",
704 &pdata->buck1_default_idx);
705 if (!ret && pdata->buck1_default_idx >= 4) {
706 pdata->buck1_default_idx = 0;
707 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
710 ret = of_property_read_u32(pmic_np,
711 "max8998,pmic-buck2-default-dvs-idx",
712 &pdata->buck2_default_idx);
713 if (!ret && pdata->buck2_default_idx >= 2) {
714 pdata->buck2_default_idx = 0;
715 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
718 ret = of_property_read_u32_array(pmic_np,
719 "max8998,pmic-buck1-dvs-voltage",
720 pdata->buck1_voltage,
721 ARRAY_SIZE(pdata->buck1_voltage));
722 if (ret) {
723 dev_err(iodev->dev, "buck1 voltages not specified\n");
724 return -EINVAL;
727 ret = of_property_read_u32_array(pmic_np,
728 "max8998,pmic-buck2-dvs-voltage",
729 pdata->buck2_voltage,
730 ARRAY_SIZE(pdata->buck2_voltage));
731 if (ret) {
732 dev_err(iodev->dev, "buck2 voltages not specified\n");
733 return -EINVAL;
736 return 0;
739 static int max8998_pmic_probe(struct platform_device *pdev)
741 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
742 struct max8998_platform_data *pdata = iodev->pdata;
743 struct regulator_config config = { };
744 struct regulator_dev **rdev;
745 struct max8998_data *max8998;
746 struct i2c_client *i2c;
747 int i, ret, size;
748 unsigned int v;
750 if (!pdata) {
751 dev_err(pdev->dev.parent, "No platform init data supplied\n");
752 return -ENODEV;
755 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
756 ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
757 if (ret)
758 return ret;
761 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
762 GFP_KERNEL);
763 if (!max8998)
764 return -ENOMEM;
766 size = sizeof(struct regulator_dev *) * pdata->num_regulators;
767 max8998->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
768 if (!max8998->rdev)
769 return -ENOMEM;
771 rdev = max8998->rdev;
772 max8998->dev = &pdev->dev;
773 max8998->iodev = iodev;
774 max8998->num_regulators = pdata->num_regulators;
775 platform_set_drvdata(pdev, max8998);
776 i2c = max8998->iodev->i2c;
778 max8998->buck1_idx = pdata->buck1_default_idx;
779 max8998->buck2_idx = pdata->buck2_default_idx;
781 /* NOTE: */
782 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
783 /* will be displayed */
785 /* Check if MAX8998 voltage selection GPIOs are defined */
786 if (gpio_is_valid(pdata->buck1_set1) &&
787 gpio_is_valid(pdata->buck1_set2)) {
788 /* Check if SET1 is not equal to 0 */
789 if (!pdata->buck1_set1) {
790 dev_err(&pdev->dev,
791 "MAX8998 SET1 GPIO defined as 0 !\n");
792 WARN_ON(!pdata->buck1_set1);
793 return -EIO;
795 /* Check if SET2 is not equal to 0 */
796 if (!pdata->buck1_set2) {
797 dev_err(&pdev->dev,
798 "MAX8998 SET2 GPIO defined as 0 !\n");
799 WARN_ON(!pdata->buck1_set2);
800 return -EIO;
803 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
804 gpio_direction_output(pdata->buck1_set1,
805 max8998->buck1_idx & 0x1);
808 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
809 gpio_direction_output(pdata->buck1_set2,
810 (max8998->buck1_idx >> 1) & 0x1);
812 /* Set predefined values for BUCK1 registers */
813 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
814 i = 0;
815 while (buck12_voltage_map_desc.min +
816 buck12_voltage_map_desc.step*i
817 < pdata->buck1_voltage[v])
818 i++;
820 max8998->buck1_vol[v] = i;
821 ret = max8998_write_reg(i2c,
822 MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
823 if (ret)
824 return ret;
828 if (gpio_is_valid(pdata->buck2_set3)) {
829 /* Check if SET3 is not equal to 0 */
830 if (!pdata->buck2_set3) {
831 dev_err(&pdev->dev,
832 "MAX8998 SET3 GPIO defined as 0 !\n");
833 WARN_ON(!pdata->buck2_set3);
834 return -EIO;
836 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
837 gpio_direction_output(pdata->buck2_set3,
838 max8998->buck2_idx & 0x1);
840 /* Set predefined values for BUCK2 registers */
841 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
842 i = 0;
843 while (buck12_voltage_map_desc.min +
844 buck12_voltage_map_desc.step*i
845 < pdata->buck2_voltage[v])
846 i++;
848 max8998->buck2_vol[v] = i;
849 ret = max8998_write_reg(i2c,
850 MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
851 if (ret)
852 return ret;
856 for (i = 0; i < pdata->num_regulators; i++) {
857 const struct voltage_map_desc *desc;
858 int id = pdata->regulators[i].id;
859 int index = id - MAX8998_LDO2;
861 desc = ldo_voltage_map[id];
862 if (desc && regulators[index].ops != &max8998_others_ops) {
863 int count = (desc->max - desc->min) / desc->step + 1;
865 regulators[index].n_voltages = count;
866 regulators[index].min_uV = desc->min;
867 regulators[index].uV_step = desc->step;
870 config.dev = max8998->dev;
871 config.of_node = pdata->regulators[i].reg_node;
872 config.init_data = pdata->regulators[i].initdata;
873 config.driver_data = max8998;
875 rdev[i] = devm_regulator_register(&pdev->dev,
876 &regulators[index], &config);
877 if (IS_ERR(rdev[i])) {
878 ret = PTR_ERR(rdev[i]);
879 dev_err(max8998->dev, "regulator %s init failed (%d)\n",
880 regulators[index].name, ret);
881 rdev[i] = NULL;
882 return ret;
887 return 0;
890 static const struct platform_device_id max8998_pmic_id[] = {
891 { "max8998-pmic", TYPE_MAX8998 },
892 { "lp3974-pmic", TYPE_LP3974 },
895 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
897 static struct platform_driver max8998_pmic_driver = {
898 .driver = {
899 .name = "max8998-pmic",
900 .owner = THIS_MODULE,
902 .probe = max8998_pmic_probe,
903 .id_table = max8998_pmic_id,
906 static int __init max8998_pmic_init(void)
908 return platform_driver_register(&max8998_pmic_driver);
910 subsys_initcall(max8998_pmic_init);
912 static void __exit max8998_pmic_cleanup(void)
914 platform_driver_unregister(&max8998_pmic_driver);
916 module_exit(max8998_pmic_cleanup);
918 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
919 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
920 MODULE_LICENSE("GPL");