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 struct regulator_dev
**rdev
;
44 u8 buck1_vol
[4]; /* voltages for selection */
46 unsigned int buck1_idx
; /* index to last changed voltage */
48 unsigned int buck2_idx
;
51 struct voltage_map_desc
{
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
[] = {
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
);
123 case MAX8998_LDO2
... MAX8998_LDO5
:
124 *reg
= MAX8998_REG_ONOFF1
;
125 *shift
= 3 - (ldo
- MAX8998_LDO2
);
127 case MAX8998_LDO6
... MAX8998_LDO13
:
128 *reg
= MAX8998_REG_ONOFF2
;
129 *shift
= 7 - (ldo
- MAX8998_LDO6
);
131 case MAX8998_LDO14
... MAX8998_LDO17
:
132 *reg
= MAX8998_REG_ONOFF3
;
133 *shift
= 7 - (ldo
- MAX8998_LDO14
);
135 case MAX8998_BUCK1
... MAX8998_BUCK4
:
136 *reg
= MAX8998_REG_ONOFF1
;
137 *shift
= 7 - (ldo
- MAX8998_BUCK1
);
139 case MAX8998_EN32KHZ_AP
... MAX8998_ENVICHG
:
140 *reg
= MAX8998_REG_ONOFF4
;
141 *shift
= 7 - (ldo
- MAX8998_EN32KHZ_AP
);
143 case MAX8998_ESAFEOUT1
... MAX8998_ESAFEOUT2
:
144 *reg
= MAX8998_REG_CHGR2
;
145 *shift
= 7 - (ldo
- MAX8998_ESAFEOUT1
);
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;
161 ret
= max8998_get_enable_register(rdev
, ®
, &shift
);
165 ret
= max8998_read_reg(i2c
, reg
, &val
);
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
, ®
, &shift
);
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
, ®
, &shift
);
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;
206 case MAX8998_LDO2
... MAX8998_LDO3
:
207 reg
= MAX8998_REG_LDO2_LDO3
;
209 if (ldo
== MAX8998_LDO2
)
214 case MAX8998_LDO4
... MAX8998_LDO7
:
215 reg
= MAX8998_REG_LDO4
+ (ldo
- MAX8998_LDO4
);
217 case MAX8998_LDO8
... MAX8998_LDO9
:
218 reg
= MAX8998_REG_LDO8_LDO9
;
220 if (ldo
== MAX8998_LDO8
)
225 case MAX8998_LDO10
... MAX8998_LDO11
:
226 reg
= MAX8998_REG_LDO10_LDO11
;
227 if (ldo
== MAX8998_LDO10
) {
235 case MAX8998_LDO12
... MAX8998_LDO17
:
236 reg
= MAX8998_REG_LDO12
+ (ldo
- MAX8998_LDO12
);
239 reg
= MAX8998_REG_BUCK1_VOLTAGE1
+ max8998
->buck1_idx
;
242 reg
= MAX8998_REG_BUCK2_VOLTAGE1
+ max8998
->buck2_idx
;
245 reg
= MAX8998_REG_BUCK3
;
248 reg
= MAX8998_REG_BUCK4
;
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
;
268 ret
= max8998_get_voltage_register(rdev
, ®
, &shift
, &mask
);
272 ret
= max8998_read_reg(i2c
, reg
, &val
);
282 static int max8998_set_voltage_ldo_sel(struct regulator_dev
*rdev
,
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
, ®
, &shift
, &mask
);
293 ret
= max8998_update_reg(i2c
, reg
, selector
<<shift
, mask
<<shift
);
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
,
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
, ®
, &shift
, &mask
);
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
);
346 if (pdata
->buck_voltage_lock
)
349 /* no predefine regulator found */
350 max8998
->buck1_idx
= (buck1_last_val
% 2) + 2;
351 dev_dbg(max8998
->dev
, "max8998->buck1_idx:%d\n",
353 max8998
->buck1_vol
[max8998
->buck1_idx
] = selector
;
354 ret
= max8998_get_voltage_register(rdev
, ®
,
357 ret
= max8998_write_reg(i2c
, reg
, selector
);
358 buck1_gpio_set(pdata
->buck1_set1
,
359 pdata
->buck1_set2
, max8998
->buck1_idx
);
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
));
367 ret
= max8998_write_reg(i2c
, reg
, selector
);
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
);
387 if (pdata
->buck_voltage_lock
)
390 max8998_get_voltage_register(rdev
,
391 ®
, &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
);
396 dev_dbg(max8998
->dev
, "%s: SET3:%d\n", i2c
->name
,
397 gpio_get_value(pdata
->buck2_set3
));
399 ret
= max8998_write_reg(i2c
, reg
, selector
);
405 ret
= max8998_update_reg(i2c
, reg
, selector
<<shift
,
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
);
424 if (buck
< MAX8998_BUCK1
|| buck
> MAX8998_BUCK4
)
427 desc
= ldo_voltage_map
[buck
];
429 /* Voltage stabilization */
430 ret
= max8998_read_reg(i2c
, MAX8998_REG_ONOFF4
, &val
);
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
))
439 difference
= (new_selector
- old_selector
) * desc
->step
/ 1000;
441 return DIV_ROUND_UP(difference
, (val
& 0x0f) + 1);
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
[] = {
477 .ops
= &max8998_ldo_ops
,
478 .type
= REGULATOR_VOLTAGE
,
479 .owner
= THIS_MODULE
,
483 .ops
= &max8998_ldo_ops
,
484 .type
= REGULATOR_VOLTAGE
,
485 .owner
= THIS_MODULE
,
489 .ops
= &max8998_ldo_ops
,
490 .type
= REGULATOR_VOLTAGE
,
491 .owner
= THIS_MODULE
,
495 .ops
= &max8998_ldo_ops
,
496 .type
= REGULATOR_VOLTAGE
,
497 .owner
= THIS_MODULE
,
501 .ops
= &max8998_ldo_ops
,
502 .type
= REGULATOR_VOLTAGE
,
503 .owner
= THIS_MODULE
,
507 .ops
= &max8998_ldo_ops
,
508 .type
= REGULATOR_VOLTAGE
,
509 .owner
= THIS_MODULE
,
513 .ops
= &max8998_ldo_ops
,
514 .type
= REGULATOR_VOLTAGE
,
515 .owner
= THIS_MODULE
,
519 .ops
= &max8998_ldo_ops
,
520 .type
= REGULATOR_VOLTAGE
,
521 .owner
= THIS_MODULE
,
525 .ops
= &max8998_ldo_ops
,
526 .type
= REGULATOR_VOLTAGE
,
527 .owner
= THIS_MODULE
,
531 .ops
= &max8998_ldo_ops
,
532 .type
= REGULATOR_VOLTAGE
,
533 .owner
= THIS_MODULE
,
537 .ops
= &max8998_ldo_ops
,
538 .type
= REGULATOR_VOLTAGE
,
539 .owner
= THIS_MODULE
,
543 .ops
= &max8998_ldo_ops
,
544 .type
= REGULATOR_VOLTAGE
,
545 .owner
= THIS_MODULE
,
549 .ops
= &max8998_ldo_ops
,
550 .type
= REGULATOR_VOLTAGE
,
551 .owner
= THIS_MODULE
,
555 .ops
= &max8998_ldo_ops
,
556 .type
= REGULATOR_VOLTAGE
,
557 .owner
= THIS_MODULE
,
561 .ops
= &max8998_ldo_ops
,
562 .type
= REGULATOR_VOLTAGE
,
563 .owner
= THIS_MODULE
,
567 .ops
= &max8998_ldo_ops
,
568 .type
= REGULATOR_VOLTAGE
,
569 .owner
= THIS_MODULE
,
573 .ops
= &max8998_buck_ops
,
574 .type
= REGULATOR_VOLTAGE
,
575 .owner
= THIS_MODULE
,
579 .ops
= &max8998_buck_ops
,
580 .type
= REGULATOR_VOLTAGE
,
581 .owner
= THIS_MODULE
,
585 .ops
= &max8998_buck_ops
,
586 .type
= REGULATOR_VOLTAGE
,
587 .owner
= THIS_MODULE
,
591 .ops
= &max8998_buck_ops
,
592 .type
= REGULATOR_VOLTAGE
,
593 .owner
= THIS_MODULE
,
595 .name
= "EN32KHz-AP",
596 .id
= MAX8998_EN32KHZ_AP
,
597 .ops
= &max8998_others_ops
,
598 .type
= REGULATOR_VOLTAGE
,
599 .owner
= THIS_MODULE
,
601 .name
= "EN32KHz-CP",
602 .id
= MAX8998_EN32KHZ_CP
,
603 .ops
= &max8998_others_ops
,
604 .type
= REGULATOR_VOLTAGE
,
605 .owner
= THIS_MODULE
,
608 .id
= MAX8998_ENVICHG
,
609 .ops
= &max8998_others_ops
,
610 .type
= REGULATOR_VOLTAGE
,
611 .owner
= THIS_MODULE
,
614 .id
= MAX8998_ESAFEOUT1
,
615 .ops
= &max8998_others_ops
,
616 .type
= REGULATOR_VOLTAGE
,
617 .owner
= THIS_MODULE
,
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
)
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
);
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
);
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
);
652 pdata
->buck2_set3
= gpio
;
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
;
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");
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
);
680 pdata
->regulators
= rdata
;
681 for (i
= 0; i
< ARRAY_SIZE(regulators
); ++i
) {
682 reg_np
= of_get_child_by_name(regulators_np
,
687 rdata
->id
= regulators
[i
].id
;
688 rdata
->initdata
= of_get_regulator_init_data(
690 rdata
->reg_node
= reg_np
;
693 pdata
->num_regulators
= rdata
- pdata
->regulators
;
695 ret
= max8998_pmic_dt_parse_dvs_gpio(iodev
, pdata
, pmic_np
);
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
));
723 dev_err(iodev
->dev
, "buck1 voltages not specified\n");
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
));
732 dev_err(iodev
->dev
, "buck2 voltages not specified\n");
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
;
751 dev_err(pdev
->dev
.parent
, "No platform init data supplied\n");
755 if (IS_ENABLED(CONFIG_OF
) && iodev
->dev
->of_node
) {
756 ret
= max8998_pmic_dt_parse_pdata(iodev
, pdata
);
761 max8998
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8998_data
),
766 size
= sizeof(struct regulator_dev
*) * pdata
->num_regulators
;
767 max8998
->rdev
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
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
;
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
) {
791 "MAX8998 SET1 GPIO defined as 0 !\n");
792 WARN_ON(!pdata
->buck1_set1
);
795 /* Check if SET2 is not equal to 0 */
796 if (!pdata
->buck1_set2
) {
798 "MAX8998 SET2 GPIO defined as 0 !\n");
799 WARN_ON(!pdata
->buck1_set2
);
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
) {
815 while (buck12_voltage_map_desc
.min
+
816 buck12_voltage_map_desc
.step
*i
817 < pdata
->buck1_voltage
[v
])
820 max8998
->buck1_vol
[v
] = i
;
821 ret
= max8998_write_reg(i2c
,
822 MAX8998_REG_BUCK1_VOLTAGE1
+ v
, i
);
828 if (gpio_is_valid(pdata
->buck2_set3
)) {
829 /* Check if SET3 is not equal to 0 */
830 if (!pdata
->buck2_set3
) {
832 "MAX8998 SET3 GPIO defined as 0 !\n");
833 WARN_ON(!pdata
->buck2_set3
);
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
) {
843 while (buck12_voltage_map_desc
.min
+
844 buck12_voltage_map_desc
.step
*i
845 < pdata
->buck2_voltage
[v
])
848 max8998
->buck2_vol
[v
] = i
;
849 ret
= max8998_write_reg(i2c
,
850 MAX8998_REG_BUCK2_VOLTAGE1
+ v
, i
);
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 ®ulators
[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
);
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
= {
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");