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>
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>
41 struct max8998_dev
*iodev
;
43 u8 buck1_vol
[4]; /* voltages for selection */
45 unsigned int buck1_idx
; /* index to last changed voltage */
47 unsigned int buck2_idx
;
50 struct voltage_map_desc
{
56 /* Voltage maps in uV*/
57 static const struct voltage_map_desc ldo23_voltage_map_desc
= {
58 .min
= 800000, .step
= 50000, .max
= 1300000,
60 static const struct voltage_map_desc ldo456711_voltage_map_desc
= {
61 .min
= 1600000, .step
= 100000, .max
= 3600000,
63 static const struct voltage_map_desc ldo8_voltage_map_desc
= {
64 .min
= 3000000, .step
= 100000, .max
= 3600000,
66 static const struct voltage_map_desc ldo9_voltage_map_desc
= {
67 .min
= 2800000, .step
= 100000, .max
= 3100000,
69 static const struct voltage_map_desc ldo10_voltage_map_desc
= {
70 .min
= 950000, .step
= 50000, .max
= 1300000,
72 static const struct voltage_map_desc ldo1213_voltage_map_desc
= {
73 .min
= 800000, .step
= 100000, .max
= 3300000,
75 static const struct voltage_map_desc ldo1415_voltage_map_desc
= {
76 .min
= 1200000, .step
= 100000, .max
= 3300000,
78 static const struct voltage_map_desc ldo1617_voltage_map_desc
= {
79 .min
= 1600000, .step
= 100000, .max
= 3600000,
81 static const struct voltage_map_desc buck12_voltage_map_desc
= {
82 .min
= 750000, .step
= 25000, .max
= 1525000,
84 static const struct voltage_map_desc buck3_voltage_map_desc
= {
85 .min
= 1600000, .step
= 100000, .max
= 3600000,
87 static const struct voltage_map_desc buck4_voltage_map_desc
= {
88 .min
= 800000, .step
= 100000, .max
= 2300000,
91 static const struct voltage_map_desc
*ldo_voltage_map
[] = {
94 &ldo23_voltage_map_desc
, /* LDO2 */
95 &ldo23_voltage_map_desc
, /* LDO3 */
96 &ldo456711_voltage_map_desc
, /* LDO4 */
97 &ldo456711_voltage_map_desc
, /* LDO5 */
98 &ldo456711_voltage_map_desc
, /* LDO6 */
99 &ldo456711_voltage_map_desc
, /* LDO7 */
100 &ldo8_voltage_map_desc
, /* LDO8 */
101 &ldo9_voltage_map_desc
, /* LDO9 */
102 &ldo10_voltage_map_desc
, /* LDO10 */
103 &ldo456711_voltage_map_desc
, /* LDO11 */
104 &ldo1213_voltage_map_desc
, /* LDO12 */
105 &ldo1213_voltage_map_desc
, /* LDO13 */
106 &ldo1415_voltage_map_desc
, /* LDO14 */
107 &ldo1415_voltage_map_desc
, /* LDO15 */
108 &ldo1617_voltage_map_desc
, /* LDO16 */
109 &ldo1617_voltage_map_desc
, /* LDO17 */
110 &buck12_voltage_map_desc
, /* BUCK1 */
111 &buck12_voltage_map_desc
, /* BUCK2 */
112 &buck3_voltage_map_desc
, /* BUCK3 */
113 &buck4_voltage_map_desc
, /* BUCK4 */
116 static int max8998_get_enable_register(struct regulator_dev
*rdev
,
117 int *reg
, int *shift
)
119 int ldo
= rdev_get_id(rdev
);
122 case MAX8998_LDO2
... MAX8998_LDO5
:
123 *reg
= MAX8998_REG_ONOFF1
;
124 *shift
= 3 - (ldo
- MAX8998_LDO2
);
126 case MAX8998_LDO6
... MAX8998_LDO13
:
127 *reg
= MAX8998_REG_ONOFF2
;
128 *shift
= 7 - (ldo
- MAX8998_LDO6
);
130 case MAX8998_LDO14
... MAX8998_LDO17
:
131 *reg
= MAX8998_REG_ONOFF3
;
132 *shift
= 7 - (ldo
- MAX8998_LDO14
);
134 case MAX8998_BUCK1
... MAX8998_BUCK4
:
135 *reg
= MAX8998_REG_ONOFF1
;
136 *shift
= 7 - (ldo
- MAX8998_BUCK1
);
138 case MAX8998_EN32KHZ_AP
... MAX8998_ENVICHG
:
139 *reg
= MAX8998_REG_ONOFF4
;
140 *shift
= 7 - (ldo
- MAX8998_EN32KHZ_AP
);
142 case MAX8998_ESAFEOUT1
... MAX8998_ESAFEOUT2
:
143 *reg
= MAX8998_REG_CHGR2
;
144 *shift
= 7 - (ldo
- MAX8998_ESAFEOUT1
);
153 static int max8998_ldo_is_enabled(struct regulator_dev
*rdev
)
155 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
156 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
157 int ret
, reg
, shift
= 8;
160 ret
= max8998_get_enable_register(rdev
, ®
, &shift
);
164 ret
= max8998_read_reg(i2c
, reg
, &val
);
168 return val
& (1 << shift
);
171 static int max8998_ldo_enable(struct regulator_dev
*rdev
)
173 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
174 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
175 int reg
, shift
= 8, ret
;
177 ret
= max8998_get_enable_register(rdev
, ®
, &shift
);
181 return max8998_update_reg(i2c
, reg
, 1<<shift
, 1<<shift
);
184 static int max8998_ldo_disable(struct regulator_dev
*rdev
)
186 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
187 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
188 int reg
, shift
= 8, ret
;
190 ret
= max8998_get_enable_register(rdev
, ®
, &shift
);
194 return max8998_update_reg(i2c
, reg
, 0, 1<<shift
);
197 static int max8998_get_voltage_register(struct regulator_dev
*rdev
,
198 int *_reg
, int *_shift
, int *_mask
)
200 int ldo
= rdev_get_id(rdev
);
201 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
202 int reg
, shift
= 0, mask
= 0xff;
205 case MAX8998_LDO2
... MAX8998_LDO3
:
206 reg
= MAX8998_REG_LDO2_LDO3
;
208 if (ldo
== MAX8998_LDO2
)
213 case MAX8998_LDO4
... MAX8998_LDO7
:
214 reg
= MAX8998_REG_LDO4
+ (ldo
- MAX8998_LDO4
);
216 case MAX8998_LDO8
... MAX8998_LDO9
:
217 reg
= MAX8998_REG_LDO8_LDO9
;
219 if (ldo
== MAX8998_LDO8
)
224 case MAX8998_LDO10
... MAX8998_LDO11
:
225 reg
= MAX8998_REG_LDO10_LDO11
;
226 if (ldo
== MAX8998_LDO10
) {
234 case MAX8998_LDO12
... MAX8998_LDO17
:
235 reg
= MAX8998_REG_LDO12
+ (ldo
- MAX8998_LDO12
);
238 reg
= MAX8998_REG_BUCK1_VOLTAGE1
+ max8998
->buck1_idx
;
241 reg
= MAX8998_REG_BUCK2_VOLTAGE1
+ max8998
->buck2_idx
;
244 reg
= MAX8998_REG_BUCK3
;
247 reg
= MAX8998_REG_BUCK4
;
260 static int max8998_get_voltage_sel(struct regulator_dev
*rdev
)
262 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
263 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
264 int reg
, shift
= 0, mask
, ret
;
267 ret
= max8998_get_voltage_register(rdev
, ®
, &shift
, &mask
);
271 ret
= max8998_read_reg(i2c
, reg
, &val
);
281 static int max8998_set_voltage_ldo_sel(struct regulator_dev
*rdev
,
284 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
285 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
286 int reg
, shift
= 0, mask
, ret
;
288 ret
= max8998_get_voltage_register(rdev
, ®
, &shift
, &mask
);
292 ret
= max8998_update_reg(i2c
, reg
, selector
<<shift
, mask
<<shift
);
297 static inline void buck1_gpio_set(int gpio1
, int gpio2
, int v
)
299 gpio_set_value(gpio1
, v
& 0x1);
300 gpio_set_value(gpio2
, (v
>> 1) & 0x1);
303 static inline void buck2_gpio_set(int gpio
, int v
)
305 gpio_set_value(gpio
, v
& 0x1);
308 static int max8998_set_voltage_buck_sel(struct regulator_dev
*rdev
,
311 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
312 struct max8998_platform_data
*pdata
=
313 dev_get_platdata(max8998
->iodev
->dev
);
314 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
315 int buck
= rdev_get_id(rdev
);
316 int reg
, shift
= 0, mask
, ret
, j
;
317 static u8 buck1_last_val
;
319 ret
= max8998_get_voltage_register(rdev
, ®
, &shift
, &mask
);
325 dev_dbg(max8998
->dev
,
326 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
327 "buck1_vol3:%d, buck1_vol4:%d\n",
328 selector
, max8998
->buck1_vol
[0], max8998
->buck1_vol
[1],
329 max8998
->buck1_vol
[2], max8998
->buck1_vol
[3]);
331 if (gpio_is_valid(pdata
->buck1_set1
) &&
332 gpio_is_valid(pdata
->buck1_set2
)) {
334 /* check if requested voltage */
335 /* value is already defined */
336 for (j
= 0; j
< ARRAY_SIZE(max8998
->buck1_vol
); j
++) {
337 if (max8998
->buck1_vol
[j
] == selector
) {
338 max8998
->buck1_idx
= j
;
339 buck1_gpio_set(pdata
->buck1_set1
,
340 pdata
->buck1_set2
, j
);
345 if (pdata
->buck_voltage_lock
)
348 /* no predefine regulator found */
349 max8998
->buck1_idx
= (buck1_last_val
% 2) + 2;
350 dev_dbg(max8998
->dev
, "max8998->buck1_idx:%d\n",
352 max8998
->buck1_vol
[max8998
->buck1_idx
] = selector
;
353 ret
= max8998_get_voltage_register(rdev
, ®
,
356 ret
= max8998_write_reg(i2c
, reg
, selector
);
357 buck1_gpio_set(pdata
->buck1_set1
,
358 pdata
->buck1_set2
, max8998
->buck1_idx
);
361 dev_dbg(max8998
->dev
, "%s: SET1:%d, SET2:%d\n",
362 i2c
->name
, gpio_get_value(pdata
->buck1_set1
),
363 gpio_get_value(pdata
->buck1_set2
));
366 ret
= max8998_write_reg(i2c
, reg
, selector
);
371 dev_dbg(max8998
->dev
,
372 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
373 selector
, max8998
->buck2_vol
[0], max8998
->buck2_vol
[1]);
374 if (gpio_is_valid(pdata
->buck2_set3
)) {
376 /* check if requested voltage */
377 /* value is already defined */
378 for (j
= 0; j
< ARRAY_SIZE(max8998
->buck2_vol
); j
++) {
379 if (max8998
->buck2_vol
[j
] == selector
) {
380 max8998
->buck2_idx
= j
;
381 buck2_gpio_set(pdata
->buck2_set3
, j
);
386 if (pdata
->buck_voltage_lock
)
389 max8998_get_voltage_register(rdev
,
390 ®
, &shift
, &mask
);
391 ret
= max8998_write_reg(i2c
, reg
, selector
);
392 max8998
->buck2_vol
[max8998
->buck2_idx
] = selector
;
393 buck2_gpio_set(pdata
->buck2_set3
, max8998
->buck2_idx
);
395 dev_dbg(max8998
->dev
, "%s: SET3:%d\n", i2c
->name
,
396 gpio_get_value(pdata
->buck2_set3
));
398 ret
= max8998_write_reg(i2c
, reg
, selector
);
404 ret
= max8998_update_reg(i2c
, reg
, selector
<<shift
,
412 static int max8998_set_voltage_buck_time_sel(struct regulator_dev
*rdev
,
413 unsigned int old_selector
,
414 unsigned int new_selector
)
416 struct max8998_data
*max8998
= rdev_get_drvdata(rdev
);
417 struct i2c_client
*i2c
= max8998
->iodev
->i2c
;
418 const struct voltage_map_desc
*desc
;
419 int buck
= rdev_get_id(rdev
);
423 if (buck
< MAX8998_BUCK1
|| buck
> MAX8998_BUCK4
)
426 desc
= ldo_voltage_map
[buck
];
428 /* Voltage stabilization */
429 ret
= max8998_read_reg(i2c
, MAX8998_REG_ONOFF4
, &val
);
433 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
434 /* MAX8998 has ENRAMP bit implemented, so test it*/
435 if (max8998
->iodev
->type
== TYPE_MAX8998
&& !(val
& MAX8998_ENRAMP
))
438 difference
= (new_selector
- old_selector
) * desc
->step
/ 1000;
440 return DIV_ROUND_UP(difference
, (val
& 0x0f) + 1);
445 static struct regulator_ops max8998_ldo_ops
= {
446 .list_voltage
= regulator_list_voltage_linear
,
447 .map_voltage
= regulator_map_voltage_linear
,
448 .is_enabled
= max8998_ldo_is_enabled
,
449 .enable
= max8998_ldo_enable
,
450 .disable
= max8998_ldo_disable
,
451 .get_voltage_sel
= max8998_get_voltage_sel
,
452 .set_voltage_sel
= max8998_set_voltage_ldo_sel
,
455 static struct regulator_ops max8998_buck_ops
= {
456 .list_voltage
= regulator_list_voltage_linear
,
457 .map_voltage
= regulator_map_voltage_linear
,
458 .is_enabled
= max8998_ldo_is_enabled
,
459 .enable
= max8998_ldo_enable
,
460 .disable
= max8998_ldo_disable
,
461 .get_voltage_sel
= max8998_get_voltage_sel
,
462 .set_voltage_sel
= max8998_set_voltage_buck_sel
,
463 .set_voltage_time_sel
= max8998_set_voltage_buck_time_sel
,
466 static struct regulator_ops max8998_others_ops
= {
467 .is_enabled
= max8998_ldo_is_enabled
,
468 .enable
= max8998_ldo_enable
,
469 .disable
= max8998_ldo_disable
,
472 static struct regulator_desc regulators
[] = {
476 .ops
= &max8998_ldo_ops
,
477 .type
= REGULATOR_VOLTAGE
,
478 .owner
= THIS_MODULE
,
482 .ops
= &max8998_ldo_ops
,
483 .type
= REGULATOR_VOLTAGE
,
484 .owner
= THIS_MODULE
,
488 .ops
= &max8998_ldo_ops
,
489 .type
= REGULATOR_VOLTAGE
,
490 .owner
= THIS_MODULE
,
494 .ops
= &max8998_ldo_ops
,
495 .type
= REGULATOR_VOLTAGE
,
496 .owner
= THIS_MODULE
,
500 .ops
= &max8998_ldo_ops
,
501 .type
= REGULATOR_VOLTAGE
,
502 .owner
= THIS_MODULE
,
506 .ops
= &max8998_ldo_ops
,
507 .type
= REGULATOR_VOLTAGE
,
508 .owner
= THIS_MODULE
,
512 .ops
= &max8998_ldo_ops
,
513 .type
= REGULATOR_VOLTAGE
,
514 .owner
= THIS_MODULE
,
518 .ops
= &max8998_ldo_ops
,
519 .type
= REGULATOR_VOLTAGE
,
520 .owner
= THIS_MODULE
,
524 .ops
= &max8998_ldo_ops
,
525 .type
= REGULATOR_VOLTAGE
,
526 .owner
= THIS_MODULE
,
530 .ops
= &max8998_ldo_ops
,
531 .type
= REGULATOR_VOLTAGE
,
532 .owner
= THIS_MODULE
,
536 .ops
= &max8998_ldo_ops
,
537 .type
= REGULATOR_VOLTAGE
,
538 .owner
= THIS_MODULE
,
542 .ops
= &max8998_ldo_ops
,
543 .type
= REGULATOR_VOLTAGE
,
544 .owner
= THIS_MODULE
,
548 .ops
= &max8998_ldo_ops
,
549 .type
= REGULATOR_VOLTAGE
,
550 .owner
= THIS_MODULE
,
554 .ops
= &max8998_ldo_ops
,
555 .type
= REGULATOR_VOLTAGE
,
556 .owner
= THIS_MODULE
,
560 .ops
= &max8998_ldo_ops
,
561 .type
= REGULATOR_VOLTAGE
,
562 .owner
= THIS_MODULE
,
566 .ops
= &max8998_ldo_ops
,
567 .type
= REGULATOR_VOLTAGE
,
568 .owner
= THIS_MODULE
,
572 .ops
= &max8998_buck_ops
,
573 .type
= REGULATOR_VOLTAGE
,
574 .owner
= THIS_MODULE
,
578 .ops
= &max8998_buck_ops
,
579 .type
= REGULATOR_VOLTAGE
,
580 .owner
= THIS_MODULE
,
584 .ops
= &max8998_buck_ops
,
585 .type
= REGULATOR_VOLTAGE
,
586 .owner
= THIS_MODULE
,
590 .ops
= &max8998_buck_ops
,
591 .type
= REGULATOR_VOLTAGE
,
592 .owner
= THIS_MODULE
,
594 .name
= "EN32KHz-AP",
595 .id
= MAX8998_EN32KHZ_AP
,
596 .ops
= &max8998_others_ops
,
597 .type
= REGULATOR_VOLTAGE
,
598 .owner
= THIS_MODULE
,
600 .name
= "EN32KHz-CP",
601 .id
= MAX8998_EN32KHZ_CP
,
602 .ops
= &max8998_others_ops
,
603 .type
= REGULATOR_VOLTAGE
,
604 .owner
= THIS_MODULE
,
607 .id
= MAX8998_ENVICHG
,
608 .ops
= &max8998_others_ops
,
609 .type
= REGULATOR_VOLTAGE
,
610 .owner
= THIS_MODULE
,
613 .id
= MAX8998_ESAFEOUT1
,
614 .ops
= &max8998_others_ops
,
615 .type
= REGULATOR_VOLTAGE
,
616 .owner
= THIS_MODULE
,
619 .id
= MAX8998_ESAFEOUT2
,
620 .ops
= &max8998_others_ops
,
621 .type
= REGULATOR_VOLTAGE
,
622 .owner
= THIS_MODULE
,
626 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev
*iodev
,
627 struct max8998_platform_data
*pdata
,
628 struct device_node
*pmic_np
)
632 gpio
= of_get_named_gpio(pmic_np
, "max8998,pmic-buck1-dvs-gpios", 0);
633 if (!gpio_is_valid(gpio
)) {
634 dev_err(iodev
->dev
, "invalid buck1 gpio[0]: %d\n", gpio
);
637 pdata
->buck1_set1
= gpio
;
639 gpio
= of_get_named_gpio(pmic_np
, "max8998,pmic-buck1-dvs-gpios", 1);
640 if (!gpio_is_valid(gpio
)) {
641 dev_err(iodev
->dev
, "invalid buck1 gpio[1]: %d\n", gpio
);
644 pdata
->buck1_set2
= gpio
;
646 gpio
= of_get_named_gpio(pmic_np
, "max8998,pmic-buck2-dvs-gpio", 0);
647 if (!gpio_is_valid(gpio
)) {
648 dev_err(iodev
->dev
, "invalid buck 2 gpio: %d\n", gpio
);
651 pdata
->buck2_set3
= gpio
;
656 static int max8998_pmic_dt_parse_pdata(struct max8998_dev
*iodev
,
657 struct max8998_platform_data
*pdata
)
659 struct device_node
*pmic_np
= iodev
->dev
->of_node
;
660 struct device_node
*regulators_np
, *reg_np
;
661 struct max8998_regulator_data
*rdata
;
665 regulators_np
= of_get_child_by_name(pmic_np
, "regulators");
666 if (!regulators_np
) {
667 dev_err(iodev
->dev
, "could not find regulators sub-node\n");
671 /* count the number of regulators to be supported in pmic */
672 pdata
->num_regulators
= of_get_child_count(regulators_np
);
674 rdata
= devm_kzalloc(iodev
->dev
, sizeof(*rdata
) *
675 pdata
->num_regulators
, GFP_KERNEL
);
677 of_node_put(regulators_np
);
681 pdata
->regulators
= rdata
;
682 for (i
= 0; i
< ARRAY_SIZE(regulators
); ++i
) {
683 reg_np
= of_get_child_by_name(regulators_np
,
688 rdata
->id
= regulators
[i
].id
;
689 rdata
->initdata
= of_get_regulator_init_data(iodev
->dev
,
692 rdata
->reg_node
= reg_np
;
695 pdata
->num_regulators
= rdata
- pdata
->regulators
;
698 of_node_put(regulators_np
);
700 ret
= max8998_pmic_dt_parse_dvs_gpio(iodev
, pdata
, pmic_np
);
704 if (of_find_property(pmic_np
, "max8998,pmic-buck-voltage-lock", NULL
))
705 pdata
->buck_voltage_lock
= true;
707 ret
= of_property_read_u32(pmic_np
,
708 "max8998,pmic-buck1-default-dvs-idx",
709 &pdata
->buck1_default_idx
);
710 if (!ret
&& pdata
->buck1_default_idx
>= 4) {
711 pdata
->buck1_default_idx
= 0;
712 dev_warn(iodev
->dev
, "invalid value for default dvs index, using 0 instead\n");
715 ret
= of_property_read_u32(pmic_np
,
716 "max8998,pmic-buck2-default-dvs-idx",
717 &pdata
->buck2_default_idx
);
718 if (!ret
&& pdata
->buck2_default_idx
>= 2) {
719 pdata
->buck2_default_idx
= 0;
720 dev_warn(iodev
->dev
, "invalid value for default dvs index, using 0 instead\n");
723 ret
= of_property_read_u32_array(pmic_np
,
724 "max8998,pmic-buck1-dvs-voltage",
725 pdata
->buck1_voltage
,
726 ARRAY_SIZE(pdata
->buck1_voltage
));
728 dev_err(iodev
->dev
, "buck1 voltages not specified\n");
732 ret
= of_property_read_u32_array(pmic_np
,
733 "max8998,pmic-buck2-dvs-voltage",
734 pdata
->buck2_voltage
,
735 ARRAY_SIZE(pdata
->buck2_voltage
));
737 dev_err(iodev
->dev
, "buck2 voltages not specified\n");
744 static int max8998_pmic_probe(struct platform_device
*pdev
)
746 struct max8998_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
747 struct max8998_platform_data
*pdata
= iodev
->pdata
;
748 struct regulator_config config
= { };
749 struct regulator_dev
*rdev
;
750 struct max8998_data
*max8998
;
751 struct i2c_client
*i2c
;
756 dev_err(pdev
->dev
.parent
, "No platform init data supplied\n");
760 if (IS_ENABLED(CONFIG_OF
) && iodev
->dev
->of_node
) {
761 ret
= max8998_pmic_dt_parse_pdata(iodev
, pdata
);
766 max8998
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8998_data
),
771 max8998
->dev
= &pdev
->dev
;
772 max8998
->iodev
= iodev
;
773 max8998
->num_regulators
= pdata
->num_regulators
;
774 platform_set_drvdata(pdev
, max8998
);
775 i2c
= max8998
->iodev
->i2c
;
777 max8998
->buck1_idx
= pdata
->buck1_default_idx
;
778 max8998
->buck2_idx
= pdata
->buck2_default_idx
;
781 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
782 /* will be displayed */
784 /* Check if MAX8998 voltage selection GPIOs are defined */
785 if (gpio_is_valid(pdata
->buck1_set1
) &&
786 gpio_is_valid(pdata
->buck1_set2
)) {
787 /* Check if SET1 is not equal to 0 */
788 if (!pdata
->buck1_set1
) {
790 "MAX8998 SET1 GPIO defined as 0 !\n");
791 WARN_ON(!pdata
->buck1_set1
);
794 /* Check if SET2 is not equal to 0 */
795 if (!pdata
->buck1_set2
) {
797 "MAX8998 SET2 GPIO defined as 0 !\n");
798 WARN_ON(!pdata
->buck1_set2
);
802 gpio_request(pdata
->buck1_set1
, "MAX8998 BUCK1_SET1");
803 gpio_direction_output(pdata
->buck1_set1
,
804 max8998
->buck1_idx
& 0x1);
807 gpio_request(pdata
->buck1_set2
, "MAX8998 BUCK1_SET2");
808 gpio_direction_output(pdata
->buck1_set2
,
809 (max8998
->buck1_idx
>> 1) & 0x1);
811 /* Set predefined values for BUCK1 registers */
812 for (v
= 0; v
< ARRAY_SIZE(pdata
->buck1_voltage
); ++v
) {
814 while (buck12_voltage_map_desc
.min
+
815 buck12_voltage_map_desc
.step
*i
816 < pdata
->buck1_voltage
[v
])
819 max8998
->buck1_vol
[v
] = i
;
820 ret
= max8998_write_reg(i2c
,
821 MAX8998_REG_BUCK1_VOLTAGE1
+ v
, i
);
827 if (gpio_is_valid(pdata
->buck2_set3
)) {
828 /* Check if SET3 is not equal to 0 */
829 if (!pdata
->buck2_set3
) {
831 "MAX8998 SET3 GPIO defined as 0 !\n");
832 WARN_ON(!pdata
->buck2_set3
);
835 gpio_request(pdata
->buck2_set3
, "MAX8998 BUCK2_SET3");
836 gpio_direction_output(pdata
->buck2_set3
,
837 max8998
->buck2_idx
& 0x1);
839 /* Set predefined values for BUCK2 registers */
840 for (v
= 0; v
< ARRAY_SIZE(pdata
->buck2_voltage
); ++v
) {
842 while (buck12_voltage_map_desc
.min
+
843 buck12_voltage_map_desc
.step
*i
844 < pdata
->buck2_voltage
[v
])
847 max8998
->buck2_vol
[v
] = i
;
848 ret
= max8998_write_reg(i2c
,
849 MAX8998_REG_BUCK2_VOLTAGE1
+ v
, i
);
855 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
856 const struct voltage_map_desc
*desc
;
857 int id
= pdata
->regulators
[i
].id
;
858 int index
= id
- MAX8998_LDO2
;
860 desc
= ldo_voltage_map
[id
];
861 if (desc
&& regulators
[index
].ops
!= &max8998_others_ops
) {
862 int count
= (desc
->max
- desc
->min
) / desc
->step
+ 1;
864 regulators
[index
].n_voltages
= count
;
865 regulators
[index
].min_uV
= desc
->min
;
866 regulators
[index
].uV_step
= desc
->step
;
869 config
.dev
= max8998
->dev
;
870 config
.of_node
= pdata
->regulators
[i
].reg_node
;
871 config
.init_data
= pdata
->regulators
[i
].initdata
;
872 config
.driver_data
= max8998
;
874 rdev
= devm_regulator_register(&pdev
->dev
, ®ulators
[index
],
878 dev_err(max8998
->dev
, "regulator %s init failed (%d)\n",
879 regulators
[index
].name
, ret
);
888 static const struct platform_device_id max8998_pmic_id
[] = {
889 { "max8998-pmic", TYPE_MAX8998
},
890 { "lp3974-pmic", TYPE_LP3974
},
893 MODULE_DEVICE_TABLE(platform
, max8998_pmic_id
);
895 static struct platform_driver max8998_pmic_driver
= {
897 .name
= "max8998-pmic",
899 .probe
= max8998_pmic_probe
,
900 .id_table
= max8998_pmic_id
,
903 static int __init
max8998_pmic_init(void)
905 return platform_driver_register(&max8998_pmic_driver
);
907 subsys_initcall(max8998_pmic_init
);
909 static void __exit
max8998_pmic_cleanup(void)
911 platform_driver_unregister(&max8998_pmic_driver
);
913 module_exit(max8998_pmic_cleanup
);
915 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
916 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
917 MODULE_LICENSE("GPL");