1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (c) 2011 Samsung Electronics Co., Ltd
4 // http://www.samsung.com
6 #include <linux/cleanup.h>
8 #include <linux/of_gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
14 #include <linux/mfd/samsung/core.h>
15 #include <linux/mfd/samsung/s5m8767.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/regmap.h>
19 #define S5M8767_OPMODE_NORMAL_MODE 0x1
23 struct sec_pmic_dev
*iodev
;
25 struct sec_opmode_data
*opmode
;
43 struct sec_voltage_desc
{
49 static const struct sec_voltage_desc buck_voltage_val1
= {
55 static const struct sec_voltage_desc buck_voltage_val2
= {
61 static const struct sec_voltage_desc buck_voltage_val3
= {
67 static const struct sec_voltage_desc ldo_voltage_val1
= {
73 static const struct sec_voltage_desc ldo_voltage_val2
= {
79 static const struct sec_voltage_desc
*reg_voltage_map
[] = {
80 [S5M8767_LDO1
] = &ldo_voltage_val2
,
81 [S5M8767_LDO2
] = &ldo_voltage_val2
,
82 [S5M8767_LDO3
] = &ldo_voltage_val1
,
83 [S5M8767_LDO4
] = &ldo_voltage_val1
,
84 [S5M8767_LDO5
] = &ldo_voltage_val1
,
85 [S5M8767_LDO6
] = &ldo_voltage_val2
,
86 [S5M8767_LDO7
] = &ldo_voltage_val2
,
87 [S5M8767_LDO8
] = &ldo_voltage_val2
,
88 [S5M8767_LDO9
] = &ldo_voltage_val1
,
89 [S5M8767_LDO10
] = &ldo_voltage_val1
,
90 [S5M8767_LDO11
] = &ldo_voltage_val1
,
91 [S5M8767_LDO12
] = &ldo_voltage_val1
,
92 [S5M8767_LDO13
] = &ldo_voltage_val1
,
93 [S5M8767_LDO14
] = &ldo_voltage_val1
,
94 [S5M8767_LDO15
] = &ldo_voltage_val2
,
95 [S5M8767_LDO16
] = &ldo_voltage_val1
,
96 [S5M8767_LDO17
] = &ldo_voltage_val1
,
97 [S5M8767_LDO18
] = &ldo_voltage_val1
,
98 [S5M8767_LDO19
] = &ldo_voltage_val1
,
99 [S5M8767_LDO20
] = &ldo_voltage_val1
,
100 [S5M8767_LDO21
] = &ldo_voltage_val1
,
101 [S5M8767_LDO22
] = &ldo_voltage_val1
,
102 [S5M8767_LDO23
] = &ldo_voltage_val1
,
103 [S5M8767_LDO24
] = &ldo_voltage_val1
,
104 [S5M8767_LDO25
] = &ldo_voltage_val1
,
105 [S5M8767_LDO26
] = &ldo_voltage_val1
,
106 [S5M8767_LDO27
] = &ldo_voltage_val1
,
107 [S5M8767_LDO28
] = &ldo_voltage_val1
,
108 [S5M8767_BUCK1
] = &buck_voltage_val1
,
109 [S5M8767_BUCK2
] = &buck_voltage_val2
,
110 [S5M8767_BUCK3
] = &buck_voltage_val2
,
111 [S5M8767_BUCK4
] = &buck_voltage_val2
,
112 [S5M8767_BUCK5
] = &buck_voltage_val1
,
113 [S5M8767_BUCK6
] = &buck_voltage_val1
,
114 [S5M8767_BUCK7
] = &buck_voltage_val3
,
115 [S5M8767_BUCK8
] = &buck_voltage_val3
,
116 [S5M8767_BUCK9
] = &buck_voltage_val3
,
119 static const unsigned int s5m8767_opmode_reg
[][4] = {
120 /* {OFF, ON, LOWPOWER, SUSPEND} */
122 {0x0, 0x3, 0x2, 0x1}, /* LDO1 */
123 {0x0, 0x3, 0x2, 0x1},
124 {0x0, 0x3, 0x2, 0x1},
125 {0x0, 0x0, 0x0, 0x0},
126 {0x0, 0x3, 0x2, 0x1}, /* LDO5 */
127 {0x0, 0x3, 0x2, 0x1},
128 {0x0, 0x3, 0x2, 0x1},
129 {0x0, 0x3, 0x2, 0x1},
130 {0x0, 0x3, 0x2, 0x1},
131 {0x0, 0x3, 0x2, 0x1}, /* LDO10 */
132 {0x0, 0x3, 0x2, 0x1},
133 {0x0, 0x3, 0x2, 0x1},
134 {0x0, 0x3, 0x2, 0x1},
135 {0x0, 0x3, 0x2, 0x1},
136 {0x0, 0x3, 0x2, 0x1}, /* LDO15 */
137 {0x0, 0x3, 0x2, 0x1},
138 {0x0, 0x3, 0x2, 0x1},
139 {0x0, 0x0, 0x0, 0x0},
140 {0x0, 0x3, 0x2, 0x1},
141 {0x0, 0x3, 0x2, 0x1}, /* LDO20 */
142 {0x0, 0x3, 0x2, 0x1},
143 {0x0, 0x3, 0x2, 0x1},
144 {0x0, 0x0, 0x0, 0x0},
145 {0x0, 0x3, 0x2, 0x1},
146 {0x0, 0x3, 0x2, 0x1}, /* LDO25 */
147 {0x0, 0x3, 0x2, 0x1},
148 {0x0, 0x3, 0x2, 0x1},
149 {0x0, 0x3, 0x2, 0x1}, /* LDO28 */
151 /* BUCK1 ... BUCK9 */
152 {0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
153 {0x0, 0x3, 0x1, 0x1},
154 {0x0, 0x3, 0x1, 0x1},
155 {0x0, 0x3, 0x1, 0x1},
156 {0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
157 {0x0, 0x3, 0x1, 0x1},
158 {0x0, 0x3, 0x1, 0x1},
159 {0x0, 0x3, 0x1, 0x1},
160 {0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
163 static int s5m8767_get_register(struct s5m8767_info
*s5m8767
, int reg_id
,
164 int *reg
, int *enable_ctrl
)
170 case S5M8767_LDO1
... S5M8767_LDO2
:
171 *reg
= S5M8767_REG_LDO1CTRL
+ (reg_id
- S5M8767_LDO1
);
173 case S5M8767_LDO3
... S5M8767_LDO28
:
174 *reg
= S5M8767_REG_LDO3CTRL
+ (reg_id
- S5M8767_LDO3
);
177 *reg
= S5M8767_REG_BUCK1CTRL1
;
179 case S5M8767_BUCK2
... S5M8767_BUCK4
:
180 *reg
= S5M8767_REG_BUCK2CTRL
+ (reg_id
- S5M8767_BUCK2
) * 9;
183 *reg
= S5M8767_REG_BUCK5CTRL1
;
185 case S5M8767_BUCK6
... S5M8767_BUCK9
:
186 *reg
= S5M8767_REG_BUCK6CTRL1
+ (reg_id
- S5M8767_BUCK6
) * 2;
192 for (i
= 0; i
< s5m8767
->num_regulators
; i
++) {
193 if (s5m8767
->opmode
[i
].id
== reg_id
) {
194 mode
= s5m8767
->opmode
[i
].mode
;
199 if (i
>= s5m8767
->num_regulators
)
202 *enable_ctrl
= s5m8767_opmode_reg
[reg_id
][mode
] << S5M8767_ENCTRL_SHIFT
;
207 static int s5m8767_get_vsel_reg(int reg_id
, struct s5m8767_info
*s5m8767
)
212 case S5M8767_LDO1
... S5M8767_LDO2
:
213 reg
= S5M8767_REG_LDO1CTRL
+ (reg_id
- S5M8767_LDO1
);
215 case S5M8767_LDO3
... S5M8767_LDO28
:
216 reg
= S5M8767_REG_LDO3CTRL
+ (reg_id
- S5M8767_LDO3
);
219 reg
= S5M8767_REG_BUCK1CTRL2
;
222 reg
= S5M8767_REG_BUCK2DVS1
;
223 if (s5m8767
->buck2_gpiodvs
)
224 reg
+= s5m8767
->buck_gpioindex
;
227 reg
= S5M8767_REG_BUCK3DVS1
;
228 if (s5m8767
->buck3_gpiodvs
)
229 reg
+= s5m8767
->buck_gpioindex
;
232 reg
= S5M8767_REG_BUCK4DVS1
;
233 if (s5m8767
->buck4_gpiodvs
)
234 reg
+= s5m8767
->buck_gpioindex
;
237 reg
= S5M8767_REG_BUCK5CTRL2
;
239 case S5M8767_BUCK6
... S5M8767_BUCK9
:
240 reg
= S5M8767_REG_BUCK6CTRL2
+ (reg_id
- S5M8767_BUCK6
) * 2;
249 static int s5m8767_convert_voltage_to_sel(const struct sec_voltage_desc
*desc
,
257 if (min_vol
> desc
->max
)
260 if (min_vol
< desc
->min
)
263 selector
= DIV_ROUND_UP(min_vol
- desc
->min
, desc
->step
);
265 if (desc
->min
+ desc
->step
* selector
> desc
->max
)
271 static inline int s5m8767_set_high(struct s5m8767_info
*s5m8767
)
273 int temp_index
= s5m8767
->buck_gpioindex
;
275 gpio_set_value(s5m8767
->buck_gpios
[0], (temp_index
>> 2) & 0x1);
276 gpio_set_value(s5m8767
->buck_gpios
[1], (temp_index
>> 1) & 0x1);
277 gpio_set_value(s5m8767
->buck_gpios
[2], temp_index
& 0x1);
282 static inline int s5m8767_set_low(struct s5m8767_info
*s5m8767
)
284 int temp_index
= s5m8767
->buck_gpioindex
;
286 gpio_set_value(s5m8767
->buck_gpios
[2], temp_index
& 0x1);
287 gpio_set_value(s5m8767
->buck_gpios
[1], (temp_index
>> 1) & 0x1);
288 gpio_set_value(s5m8767
->buck_gpios
[0], (temp_index
>> 2) & 0x1);
293 static int s5m8767_set_voltage_sel(struct regulator_dev
*rdev
,
296 struct s5m8767_info
*s5m8767
= rdev_get_drvdata(rdev
);
297 int reg_id
= rdev_get_id(rdev
);
298 int old_index
, index
= 0;
299 u8
*buck234_vol
= NULL
;
302 case S5M8767_LDO1
... S5M8767_LDO28
:
304 case S5M8767_BUCK1
... S5M8767_BUCK6
:
305 if (reg_id
== S5M8767_BUCK2
&& s5m8767
->buck2_gpiodvs
)
306 buck234_vol
= &s5m8767
->buck2_vol
[0];
307 else if (reg_id
== S5M8767_BUCK3
&& s5m8767
->buck3_gpiodvs
)
308 buck234_vol
= &s5m8767
->buck3_vol
[0];
309 else if (reg_id
== S5M8767_BUCK4
&& s5m8767
->buck4_gpiodvs
)
310 buck234_vol
= &s5m8767
->buck4_vol
[0];
312 case S5M8767_BUCK7
... S5M8767_BUCK8
:
320 /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
322 while (*buck234_vol
!= selector
) {
326 old_index
= s5m8767
->buck_gpioindex
;
327 s5m8767
->buck_gpioindex
= index
;
329 if (index
> old_index
)
330 return s5m8767_set_high(s5m8767
);
332 return s5m8767_set_low(s5m8767
);
334 return regulator_set_voltage_sel_regmap(rdev
, selector
);
338 static int s5m8767_set_voltage_time_sel(struct regulator_dev
*rdev
,
339 unsigned int old_sel
,
340 unsigned int new_sel
)
342 struct s5m8767_info
*s5m8767
= rdev_get_drvdata(rdev
);
344 if ((old_sel
< new_sel
) && s5m8767
->ramp_delay
)
345 return DIV_ROUND_UP(rdev
->desc
->uV_step
* (new_sel
- old_sel
),
346 s5m8767
->ramp_delay
* 1000);
350 static const struct regulator_ops s5m8767_ops
= {
351 .list_voltage
= regulator_list_voltage_linear
,
352 .is_enabled
= regulator_is_enabled_regmap
,
353 .enable
= regulator_enable_regmap
,
354 .disable
= regulator_disable_regmap
,
355 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
356 .set_voltage_sel
= s5m8767_set_voltage_sel
,
357 .set_voltage_time_sel
= s5m8767_set_voltage_time_sel
,
360 static const struct regulator_ops s5m8767_buck78_ops
= {
361 .list_voltage
= regulator_list_voltage_linear
,
362 .is_enabled
= regulator_is_enabled_regmap
,
363 .enable
= regulator_enable_regmap
,
364 .disable
= regulator_disable_regmap
,
365 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
366 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
369 #define s5m8767_regulator_desc(_name) { \
371 .id = S5M8767_##_name, \
372 .ops = &s5m8767_ops, \
373 .type = REGULATOR_VOLTAGE, \
374 .owner = THIS_MODULE, \
377 #define s5m8767_regulator_buck78_desc(_name) { \
379 .id = S5M8767_##_name, \
380 .ops = &s5m8767_buck78_ops, \
381 .type = REGULATOR_VOLTAGE, \
382 .owner = THIS_MODULE, \
385 static struct regulator_desc regulators
[] = {
386 s5m8767_regulator_desc(LDO1
),
387 s5m8767_regulator_desc(LDO2
),
388 s5m8767_regulator_desc(LDO3
),
389 s5m8767_regulator_desc(LDO4
),
390 s5m8767_regulator_desc(LDO5
),
391 s5m8767_regulator_desc(LDO6
),
392 s5m8767_regulator_desc(LDO7
),
393 s5m8767_regulator_desc(LDO8
),
394 s5m8767_regulator_desc(LDO9
),
395 s5m8767_regulator_desc(LDO10
),
396 s5m8767_regulator_desc(LDO11
),
397 s5m8767_regulator_desc(LDO12
),
398 s5m8767_regulator_desc(LDO13
),
399 s5m8767_regulator_desc(LDO14
),
400 s5m8767_regulator_desc(LDO15
),
401 s5m8767_regulator_desc(LDO16
),
402 s5m8767_regulator_desc(LDO17
),
403 s5m8767_regulator_desc(LDO18
),
404 s5m8767_regulator_desc(LDO19
),
405 s5m8767_regulator_desc(LDO20
),
406 s5m8767_regulator_desc(LDO21
),
407 s5m8767_regulator_desc(LDO22
),
408 s5m8767_regulator_desc(LDO23
),
409 s5m8767_regulator_desc(LDO24
),
410 s5m8767_regulator_desc(LDO25
),
411 s5m8767_regulator_desc(LDO26
),
412 s5m8767_regulator_desc(LDO27
),
413 s5m8767_regulator_desc(LDO28
),
414 s5m8767_regulator_desc(BUCK1
),
415 s5m8767_regulator_desc(BUCK2
),
416 s5m8767_regulator_desc(BUCK3
),
417 s5m8767_regulator_desc(BUCK4
),
418 s5m8767_regulator_desc(BUCK5
),
419 s5m8767_regulator_desc(BUCK6
),
420 s5m8767_regulator_buck78_desc(BUCK7
),
421 s5m8767_regulator_buck78_desc(BUCK8
),
422 s5m8767_regulator_desc(BUCK9
),
426 * Enable GPIO control over BUCK9 in regulator_config for that regulator.
428 static void s5m8767_regulator_config_ext_control(struct s5m8767_info
*s5m8767
,
429 struct sec_regulator_data
*rdata
,
430 struct regulator_config
*config
)
434 if (rdata
->id
!= S5M8767_BUCK9
)
437 /* Check if opmode for regulator matches S5M8767_ENCTRL_USE_GPIO */
438 for (i
= 0; i
< s5m8767
->num_regulators
; i
++) {
439 const struct sec_opmode_data
*opmode
= &s5m8767
->opmode
[i
];
440 if (opmode
->id
== rdata
->id
) {
441 mode
= s5m8767_opmode_reg
[rdata
->id
][opmode
->mode
];
445 if (mode
!= S5M8767_ENCTRL_USE_GPIO
) {
446 dev_warn(s5m8767
->dev
,
447 "ext-control for %pOFn: mismatched op_mode (%x), ignoring\n",
448 rdata
->reg_node
, mode
);
452 if (!rdata
->ext_control_gpiod
) {
453 dev_warn(s5m8767
->dev
,
454 "ext-control for %pOFn: GPIO not valid, ignoring\n",
459 config
->ena_gpiod
= rdata
->ext_control_gpiod
;
463 * Turn on GPIO control over BUCK9.
465 static int s5m8767_enable_ext_control(struct s5m8767_info
*s5m8767
,
466 struct regulator_dev
*rdev
)
468 int id
= rdev_get_id(rdev
);
469 int ret
, reg
, enable_ctrl
;
471 if (id
!= S5M8767_BUCK9
)
474 ret
= s5m8767_get_register(s5m8767
, id
, ®
, &enable_ctrl
);
478 return regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
479 reg
, S5M8767_ENCTRL_MASK
,
480 S5M8767_ENCTRL_USE_GPIO
<< S5M8767_ENCTRL_SHIFT
);
485 static int s5m8767_pmic_dt_parse_dvs_gpio(struct sec_pmic_dev
*iodev
,
486 struct sec_platform_data
*pdata
,
487 struct device_node
*pmic_np
)
491 for (i
= 0; i
< 3; i
++) {
492 gpio
= of_get_named_gpio(pmic_np
,
493 "s5m8767,pmic-buck-dvs-gpios", i
);
494 if (!gpio_is_valid(gpio
)) {
495 dev_err(iodev
->dev
, "invalid gpio[%d]: %d\n", i
, gpio
);
498 pdata
->buck_gpios
[i
] = gpio
;
503 static int s5m8767_pmic_dt_parse_ds_gpio(struct sec_pmic_dev
*iodev
,
504 struct sec_platform_data
*pdata
,
505 struct device_node
*pmic_np
)
509 for (i
= 0; i
< 3; i
++) {
510 gpio
= of_get_named_gpio(pmic_np
,
511 "s5m8767,pmic-buck-ds-gpios", i
);
512 if (!gpio_is_valid(gpio
)) {
513 dev_err(iodev
->dev
, "invalid gpio[%d]: %d\n", i
, gpio
);
516 pdata
->buck_ds
[i
] = gpio
;
521 static int s5m8767_pmic_dt_parse_pdata(struct platform_device
*pdev
,
522 struct sec_platform_data
*pdata
)
524 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
525 struct device_node
*pmic_np
, *reg_np
;
526 struct sec_regulator_data
*rdata
;
527 struct sec_opmode_data
*rmode
;
528 unsigned int i
, dvs_voltage_nr
= 8, ret
;
530 pmic_np
= iodev
->dev
->of_node
;
532 dev_err(iodev
->dev
, "could not find pmic sub-node\n");
536 struct device_node
*regulators_np
__free(device_node
) = of_get_child_by_name(pmic_np
,
538 if (!regulators_np
) {
539 dev_err(iodev
->dev
, "could not find regulators sub-node\n");
543 /* count the number of regulators to be supported in pmic */
544 pdata
->num_regulators
= of_get_child_count(regulators_np
);
546 rdata
= devm_kcalloc(&pdev
->dev
,
547 pdata
->num_regulators
, sizeof(*rdata
),
552 rmode
= devm_kcalloc(&pdev
->dev
,
553 pdata
->num_regulators
, sizeof(*rmode
),
558 pdata
->regulators
= rdata
;
559 pdata
->opmode
= rmode
;
560 for_each_child_of_node(regulators_np
, reg_np
) {
561 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++)
562 if (of_node_name_eq(reg_np
, regulators
[i
].name
))
565 if (i
== ARRAY_SIZE(regulators
)) {
567 "don't know how to configure regulator %pOFn\n",
572 rdata
->ext_control_gpiod
= devm_fwnode_gpiod_get(
574 of_fwnode_handle(reg_np
),
575 "s5m8767,pmic-ext-control",
576 GPIOD_OUT_HIGH
| GPIOD_FLAGS_BIT_NONEXCLUSIVE
,
578 if (PTR_ERR(rdata
->ext_control_gpiod
) == -ENOENT
) {
579 rdata
->ext_control_gpiod
= NULL
;
580 } else if (IS_ERR(rdata
->ext_control_gpiod
)) {
582 return PTR_ERR(rdata
->ext_control_gpiod
);
586 rdata
->initdata
= of_get_regulator_init_data(
589 rdata
->reg_node
= reg_np
;
592 if (of_property_read_u32(reg_np
, "op_mode",
595 "no op_mode property at %pOF\n",
598 rmode
->mode
= S5M8767_OPMODE_NORMAL_MODE
;
603 if (of_property_read_bool(pmic_np
, "s5m8767,pmic-buck2-uses-gpio-dvs")) {
604 pdata
->buck2_gpiodvs
= true;
606 if (of_property_read_u32_array(pmic_np
,
607 "s5m8767,pmic-buck2-dvs-voltage",
608 pdata
->buck2_voltage
, dvs_voltage_nr
)) {
609 dev_err(iodev
->dev
, "buck2 voltages not specified\n");
614 if (of_property_read_bool(pmic_np
, "s5m8767,pmic-buck3-uses-gpio-dvs")) {
615 pdata
->buck3_gpiodvs
= true;
617 if (of_property_read_u32_array(pmic_np
,
618 "s5m8767,pmic-buck3-dvs-voltage",
619 pdata
->buck3_voltage
, dvs_voltage_nr
)) {
620 dev_err(iodev
->dev
, "buck3 voltages not specified\n");
625 if (of_property_read_bool(pmic_np
, "s5m8767,pmic-buck4-uses-gpio-dvs")) {
626 pdata
->buck4_gpiodvs
= true;
628 if (of_property_read_u32_array(pmic_np
,
629 "s5m8767,pmic-buck4-dvs-voltage",
630 pdata
->buck4_voltage
, dvs_voltage_nr
)) {
631 dev_err(iodev
->dev
, "buck4 voltages not specified\n");
636 if (pdata
->buck2_gpiodvs
|| pdata
->buck3_gpiodvs
||
637 pdata
->buck4_gpiodvs
) {
638 ret
= s5m8767_pmic_dt_parse_dvs_gpio(iodev
, pdata
, pmic_np
);
642 if (of_property_read_u32(pmic_np
,
643 "s5m8767,pmic-buck-default-dvs-idx",
644 &pdata
->buck_default_idx
)) {
645 pdata
->buck_default_idx
= 0;
647 if (pdata
->buck_default_idx
>= 8) {
648 pdata
->buck_default_idx
= 0;
650 "invalid value for default dvs index, use 0\n");
655 ret
= s5m8767_pmic_dt_parse_ds_gpio(iodev
, pdata
, pmic_np
);
659 pdata
->buck2_ramp_enable
= of_property_read_bool(pmic_np
, "s5m8767,pmic-buck2-ramp-enable");
660 pdata
->buck3_ramp_enable
= of_property_read_bool(pmic_np
, "s5m8767,pmic-buck3-ramp-enable");
661 pdata
->buck4_ramp_enable
= of_property_read_bool(pmic_np
, "s5m8767,pmic-buck4-ramp-enable");
663 if (pdata
->buck2_ramp_enable
|| pdata
->buck3_ramp_enable
664 || pdata
->buck4_ramp_enable
) {
665 if (of_property_read_u32(pmic_np
, "s5m8767,pmic-buck-ramp-delay",
666 &pdata
->buck_ramp_delay
))
667 pdata
->buck_ramp_delay
= 0;
673 static int s5m8767_pmic_dt_parse_pdata(struct platform_device
*pdev
,
674 struct sec_platform_data
*pdata
)
678 #endif /* CONFIG_OF */
680 static int s5m8767_pmic_probe(struct platform_device
*pdev
)
682 struct sec_pmic_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
683 struct sec_platform_data
*pdata
= iodev
->pdata
;
684 struct regulator_config config
= { };
685 struct s5m8767_info
*s5m8767
;
686 int i
, ret
, buck_init
;
689 dev_err(pdev
->dev
.parent
, "Platform data not supplied\n");
693 if (iodev
->dev
->of_node
) {
694 ret
= s5m8767_pmic_dt_parse_pdata(pdev
, pdata
);
699 if (pdata
->buck2_gpiodvs
) {
700 if (pdata
->buck3_gpiodvs
|| pdata
->buck4_gpiodvs
) {
701 dev_err(&pdev
->dev
, "S5M8767 GPIO DVS NOT VALID\n");
706 if (pdata
->buck3_gpiodvs
) {
707 if (pdata
->buck2_gpiodvs
|| pdata
->buck4_gpiodvs
) {
708 dev_err(&pdev
->dev
, "S5M8767 GPIO DVS NOT VALID\n");
713 if (pdata
->buck4_gpiodvs
) {
714 if (pdata
->buck2_gpiodvs
|| pdata
->buck3_gpiodvs
) {
715 dev_err(&pdev
->dev
, "S5M8767 GPIO DVS NOT VALID\n");
720 s5m8767
= devm_kzalloc(&pdev
->dev
, sizeof(struct s5m8767_info
),
725 s5m8767
->dev
= &pdev
->dev
;
726 s5m8767
->iodev
= iodev
;
727 s5m8767
->num_regulators
= pdata
->num_regulators
;
728 platform_set_drvdata(pdev
, s5m8767
);
730 s5m8767
->buck_gpioindex
= pdata
->buck_default_idx
;
731 s5m8767
->buck2_gpiodvs
= pdata
->buck2_gpiodvs
;
732 s5m8767
->buck3_gpiodvs
= pdata
->buck3_gpiodvs
;
733 s5m8767
->buck4_gpiodvs
= pdata
->buck4_gpiodvs
;
734 s5m8767
->buck_gpios
[0] = pdata
->buck_gpios
[0];
735 s5m8767
->buck_gpios
[1] = pdata
->buck_gpios
[1];
736 s5m8767
->buck_gpios
[2] = pdata
->buck_gpios
[2];
737 s5m8767
->buck_ds
[0] = pdata
->buck_ds
[0];
738 s5m8767
->buck_ds
[1] = pdata
->buck_ds
[1];
739 s5m8767
->buck_ds
[2] = pdata
->buck_ds
[2];
741 s5m8767
->ramp_delay
= pdata
->buck_ramp_delay
;
742 s5m8767
->buck2_ramp
= pdata
->buck2_ramp_enable
;
743 s5m8767
->buck3_ramp
= pdata
->buck3_ramp_enable
;
744 s5m8767
->buck4_ramp
= pdata
->buck4_ramp_enable
;
745 s5m8767
->opmode
= pdata
->opmode
;
747 buck_init
= s5m8767_convert_voltage_to_sel(&buck_voltage_val2
,
750 regmap_write(s5m8767
->iodev
->regmap_pmic
, S5M8767_REG_BUCK2DVS2
,
753 buck_init
= s5m8767_convert_voltage_to_sel(&buck_voltage_val2
,
756 regmap_write(s5m8767
->iodev
->regmap_pmic
, S5M8767_REG_BUCK3DVS2
,
759 buck_init
= s5m8767_convert_voltage_to_sel(&buck_voltage_val2
,
762 regmap_write(s5m8767
->iodev
->regmap_pmic
, S5M8767_REG_BUCK4DVS2
,
765 for (i
= 0; i
< 8; i
++) {
766 if (s5m8767
->buck2_gpiodvs
) {
767 s5m8767
->buck2_vol
[i
] =
768 s5m8767_convert_voltage_to_sel(
770 pdata
->buck2_voltage
[i
]);
773 if (s5m8767
->buck3_gpiodvs
) {
774 s5m8767
->buck3_vol
[i
] =
775 s5m8767_convert_voltage_to_sel(
777 pdata
->buck3_voltage
[i
]);
780 if (s5m8767
->buck4_gpiodvs
) {
781 s5m8767
->buck4_vol
[i
] =
782 s5m8767_convert_voltage_to_sel(
784 pdata
->buck4_voltage
[i
]);
788 if (pdata
->buck2_gpiodvs
|| pdata
->buck3_gpiodvs
||
789 pdata
->buck4_gpiodvs
) {
791 if (!gpio_is_valid(pdata
->buck_gpios
[0]) ||
792 !gpio_is_valid(pdata
->buck_gpios
[1]) ||
793 !gpio_is_valid(pdata
->buck_gpios
[2])) {
794 dev_err(&pdev
->dev
, "GPIO NOT VALID\n");
798 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck_gpios
[0],
803 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck_gpios
[1],
808 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck_gpios
[2],
814 gpio_direction_output(pdata
->buck_gpios
[0],
815 (s5m8767
->buck_gpioindex
>> 2) & 0x1);
817 gpio_direction_output(pdata
->buck_gpios
[1],
818 (s5m8767
->buck_gpioindex
>> 1) & 0x1);
820 gpio_direction_output(pdata
->buck_gpios
[2],
821 (s5m8767
->buck_gpioindex
>> 0) & 0x1);
824 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck_ds
[0], "S5M8767 DS2");
828 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck_ds
[1], "S5M8767 DS3");
832 ret
= devm_gpio_request(&pdev
->dev
, pdata
->buck_ds
[2], "S5M8767 DS4");
837 gpio_direction_output(pdata
->buck_ds
[0], 0x0);
839 gpio_direction_output(pdata
->buck_ds
[1], 0x0);
841 gpio_direction_output(pdata
->buck_ds
[2], 0x0);
843 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
844 S5M8767_REG_BUCK2CTRL
, 1 << 1,
845 (pdata
->buck2_gpiodvs
) ? (1 << 1) : (0 << 1));
846 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
847 S5M8767_REG_BUCK3CTRL
, 1 << 1,
848 (pdata
->buck3_gpiodvs
) ? (1 << 1) : (0 << 1));
849 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
850 S5M8767_REG_BUCK4CTRL
, 1 << 1,
851 (pdata
->buck4_gpiodvs
) ? (1 << 1) : (0 << 1));
853 /* Initialize GPIO DVS registers */
854 for (i
= 0; i
< 8; i
++) {
855 if (s5m8767
->buck2_gpiodvs
) {
856 regmap_write(s5m8767
->iodev
->regmap_pmic
,
857 S5M8767_REG_BUCK2DVS1
+ i
,
858 s5m8767
->buck2_vol
[i
]);
861 if (s5m8767
->buck3_gpiodvs
) {
862 regmap_write(s5m8767
->iodev
->regmap_pmic
,
863 S5M8767_REG_BUCK3DVS1
+ i
,
864 s5m8767
->buck3_vol
[i
]);
867 if (s5m8767
->buck4_gpiodvs
) {
868 regmap_write(s5m8767
->iodev
->regmap_pmic
,
869 S5M8767_REG_BUCK4DVS1
+ i
,
870 s5m8767
->buck4_vol
[i
]);
874 if (s5m8767
->buck2_ramp
)
875 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
876 S5M8767_REG_DVSRAMP
, 0x08, 0x08);
878 if (s5m8767
->buck3_ramp
)
879 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
880 S5M8767_REG_DVSRAMP
, 0x04, 0x04);
882 if (s5m8767
->buck4_ramp
)
883 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
884 S5M8767_REG_DVSRAMP
, 0x02, 0x02);
886 if (s5m8767
->buck2_ramp
|| s5m8767
->buck3_ramp
887 || s5m8767
->buck4_ramp
) {
889 switch (s5m8767
->ramp_delay
) {
891 val
= S5M8767_DVS_BUCK_RAMP_5
;
894 val
= S5M8767_DVS_BUCK_RAMP_10
;
897 val
= S5M8767_DVS_BUCK_RAMP_25
;
900 val
= S5M8767_DVS_BUCK_RAMP_50
;
903 val
= S5M8767_DVS_BUCK_RAMP_100
;
906 val
= S5M8767_DVS_BUCK_RAMP_10
;
908 regmap_update_bits(s5m8767
->iodev
->regmap_pmic
,
910 S5M8767_DVS_BUCK_RAMP_MASK
,
911 val
<< S5M8767_DVS_BUCK_RAMP_SHIFT
);
914 for (i
= 0; i
< pdata
->num_regulators
; i
++) {
915 const struct sec_voltage_desc
*desc
;
916 unsigned int id
= pdata
->regulators
[i
].id
;
917 int enable_reg
, enable_val
;
918 struct regulator_dev
*rdev
;
920 BUILD_BUG_ON(ARRAY_SIZE(regulators
) != ARRAY_SIZE(reg_voltage_map
));
921 if (WARN_ON_ONCE(id
>= ARRAY_SIZE(regulators
)))
924 desc
= reg_voltage_map
[id
];
926 regulators
[id
].n_voltages
=
927 (desc
->max
- desc
->min
) / desc
->step
+ 1;
928 regulators
[id
].min_uV
= desc
->min
;
929 regulators
[id
].uV_step
= desc
->step
;
930 regulators
[id
].vsel_reg
=
931 s5m8767_get_vsel_reg(id
, s5m8767
);
932 if (id
< S5M8767_BUCK1
)
933 regulators
[id
].vsel_mask
= 0x3f;
935 regulators
[id
].vsel_mask
= 0xff;
937 ret
= s5m8767_get_register(s5m8767
, id
, &enable_reg
,
940 dev_err(s5m8767
->dev
, "error reading registers\n");
943 regulators
[id
].enable_reg
= enable_reg
;
944 regulators
[id
].enable_mask
= S5M8767_ENCTRL_MASK
;
945 regulators
[id
].enable_val
= enable_val
;
948 config
.dev
= s5m8767
->dev
;
949 config
.init_data
= pdata
->regulators
[i
].initdata
;
950 config
.driver_data
= s5m8767
;
951 config
.regmap
= iodev
->regmap_pmic
;
952 config
.of_node
= pdata
->regulators
[i
].reg_node
;
953 config
.ena_gpiod
= NULL
;
954 if (pdata
->regulators
[i
].ext_control_gpiod
) {
955 /* Assigns config.ena_gpiod */
956 s5m8767_regulator_config_ext_control(s5m8767
,
957 &pdata
->regulators
[i
], &config
);
960 * Hand the GPIO descriptor management over to the
961 * regulator core, remove it from devres management.
963 devm_gpiod_unhinge(s5m8767
->dev
, config
.ena_gpiod
);
965 rdev
= devm_regulator_register(&pdev
->dev
, ®ulators
[id
],
969 dev_err(s5m8767
->dev
, "regulator init failed for %d\n",
974 if (pdata
->regulators
[i
].ext_control_gpiod
) {
975 ret
= s5m8767_enable_ext_control(s5m8767
, rdev
);
977 dev_err(s5m8767
->dev
,
978 "failed to enable gpio control over %s: %d\n",
979 rdev
->desc
->name
, ret
);
988 static const struct platform_device_id s5m8767_pmic_id
[] = {
989 { "s5m8767-pmic", 0},
992 MODULE_DEVICE_TABLE(platform
, s5m8767_pmic_id
);
994 static struct platform_driver s5m8767_pmic_driver
= {
996 .name
= "s5m8767-pmic",
997 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
999 .probe
= s5m8767_pmic_probe
,
1000 .id_table
= s5m8767_pmic_id
,
1002 module_platform_driver(s5m8767_pmic_driver
);
1004 /* Module information */
1005 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1006 MODULE_DESCRIPTION("Samsung S5M8767 Regulator Driver");
1007 MODULE_LICENSE("GPL");