1 // SPDX-License-Identifier: GPL-2.0+
3 // Regulator driver for DA9055 PMIC
5 // Copyright(c) 2012 Dialog Semiconductor Ltd.
7 // Author: David Dajun Chen <dchen@diasemi.com>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
19 #include <linux/mfd/da9055/core.h>
20 #include <linux/mfd/da9055/reg.h>
21 #include <linux/mfd/da9055/pdata.h>
23 #define DA9055_MIN_UA 0
24 #define DA9055_MAX_UA 3
26 #define DA9055_LDO_MODE_SYNC 0
27 #define DA9055_LDO_MODE_SLEEP 1
29 #define DA9055_BUCK_MODE_SLEEP 1
30 #define DA9055_BUCK_MODE_SYNC 2
31 #define DA9055_BUCK_MODE_AUTO 3
33 /* DA9055 REGULATOR IDs */
34 #define DA9055_ID_BUCK1 0
35 #define DA9055_ID_BUCK2 1
36 #define DA9055_ID_LDO1 2
37 #define DA9055_ID_LDO2 3
38 #define DA9055_ID_LDO3 4
39 #define DA9055_ID_LDO4 5
40 #define DA9055_ID_LDO5 6
41 #define DA9055_ID_LDO6 7
43 /* DA9055 BUCK current limit */
44 static const unsigned int da9055_current_limits
[] = {
45 500000, 600000, 700000, 800000
48 struct da9055_conf_reg
{
54 struct da9055_volt_reg
{
61 struct da9055_mode_reg
{
67 struct da9055_regulator_info
{
68 struct regulator_desc reg_desc
;
69 struct da9055_conf_reg conf
;
70 struct da9055_volt_reg volt
;
71 struct da9055_mode_reg mode
;
74 struct da9055_regulator
{
75 struct da9055
*da9055
;
76 const struct da9055_regulator_info
*info
;
77 struct regulator_dev
*rdev
;
78 enum gpio_select reg_rselect
;
81 static unsigned int da9055_buck_get_mode(struct regulator_dev
*rdev
)
83 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
84 const struct da9055_regulator_info
*info
= regulator
->info
;
87 ret
= da9055_reg_read(regulator
->da9055
, info
->mode
.reg
);
91 switch ((ret
& info
->mode
.mask
) >> info
->mode
.shift
) {
92 case DA9055_BUCK_MODE_SYNC
:
93 mode
= REGULATOR_MODE_FAST
;
95 case DA9055_BUCK_MODE_AUTO
:
96 mode
= REGULATOR_MODE_NORMAL
;
98 case DA9055_BUCK_MODE_SLEEP
:
99 mode
= REGULATOR_MODE_STANDBY
;
106 static int da9055_buck_set_mode(struct regulator_dev
*rdev
,
109 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
110 const struct da9055_regulator_info
*info
= regulator
->info
;
114 case REGULATOR_MODE_FAST
:
115 val
= DA9055_BUCK_MODE_SYNC
<< info
->mode
.shift
;
117 case REGULATOR_MODE_NORMAL
:
118 val
= DA9055_BUCK_MODE_AUTO
<< info
->mode
.shift
;
120 case REGULATOR_MODE_STANDBY
:
121 val
= DA9055_BUCK_MODE_SLEEP
<< info
->mode
.shift
;
125 return da9055_reg_update(regulator
->da9055
, info
->mode
.reg
,
126 info
->mode
.mask
, val
);
129 static unsigned int da9055_ldo_get_mode(struct regulator_dev
*rdev
)
131 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
132 const struct da9055_regulator_info
*info
= regulator
->info
;
135 ret
= da9055_reg_read(regulator
->da9055
, info
->volt
.reg_b
);
139 if (ret
>> info
->volt
.sl_shift
)
140 return REGULATOR_MODE_STANDBY
;
142 return REGULATOR_MODE_NORMAL
;
145 static int da9055_ldo_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
147 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
148 const struct da9055_regulator_info
*info
= regulator
->info
;
149 struct da9055_volt_reg volt
= info
->volt
;
153 case REGULATOR_MODE_NORMAL
:
154 case REGULATOR_MODE_FAST
:
155 val
= DA9055_LDO_MODE_SYNC
;
157 case REGULATOR_MODE_STANDBY
:
158 val
= DA9055_LDO_MODE_SLEEP
;
162 return da9055_reg_update(regulator
->da9055
, volt
.reg_b
,
164 val
<< volt
.sl_shift
);
167 static int da9055_regulator_get_voltage_sel(struct regulator_dev
*rdev
)
169 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
170 const struct da9055_regulator_info
*info
= regulator
->info
;
171 struct da9055_volt_reg volt
= info
->volt
;
175 * There are two voltage register set A & B for voltage ramping but
176 * either one of then can be active therefore we first determine
177 * the active register set.
179 ret
= da9055_reg_read(regulator
->da9055
, info
->conf
.reg
);
183 ret
&= info
->conf
.sel_mask
;
185 /* Get the voltage for the active register set A/B */
186 if (ret
== DA9055_REGUALTOR_SET_A
)
187 ret
= da9055_reg_read(regulator
->da9055
, volt
.reg_a
);
189 ret
= da9055_reg_read(regulator
->da9055
, volt
.reg_b
);
194 sel
= (ret
& volt
.v_mask
);
198 static int da9055_regulator_set_voltage_sel(struct regulator_dev
*rdev
,
199 unsigned int selector
)
201 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
202 const struct da9055_regulator_info
*info
= regulator
->info
;
206 * Regulator register set A/B is not selected through GPIO therefore
207 * we use default register set A for voltage ramping.
209 if (regulator
->reg_rselect
== NO_GPIO
) {
210 /* Select register set A */
211 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
212 info
->conf
.sel_mask
, DA9055_SEL_REG_A
);
216 /* Set the voltage */
217 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_a
,
218 info
->volt
.v_mask
, selector
);
222 * Here regulator register set A/B is selected through GPIO.
223 * Therefore we first determine the selected register set A/B and
224 * then set the desired voltage for that register set A/B.
226 ret
= da9055_reg_read(regulator
->da9055
, info
->conf
.reg
);
230 ret
&= info
->conf
.sel_mask
;
232 /* Set the voltage */
233 if (ret
== DA9055_REGUALTOR_SET_A
)
234 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_a
,
235 info
->volt
.v_mask
, selector
);
237 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_b
,
238 info
->volt
.v_mask
, selector
);
241 static int da9055_regulator_set_suspend_voltage(struct regulator_dev
*rdev
,
244 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
245 const struct da9055_regulator_info
*info
= regulator
->info
;
248 /* Select register set B for suspend voltage ramping. */
249 if (regulator
->reg_rselect
== NO_GPIO
) {
250 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
251 info
->conf
.sel_mask
, DA9055_SEL_REG_B
);
256 ret
= regulator_map_voltage_linear(rdev
, uV
, uV
);
260 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_b
,
261 info
->volt
.v_mask
, ret
);
264 static int da9055_suspend_enable(struct regulator_dev
*rdev
)
266 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
267 const struct da9055_regulator_info
*info
= regulator
->info
;
269 /* Select register set B for voltage ramping. */
270 if (regulator
->reg_rselect
== NO_GPIO
)
271 return da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
272 info
->conf
.sel_mask
, DA9055_SEL_REG_B
);
277 static int da9055_suspend_disable(struct regulator_dev
*rdev
)
279 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
280 const struct da9055_regulator_info
*info
= regulator
->info
;
282 /* Diselect register set B. */
283 if (regulator
->reg_rselect
== NO_GPIO
)
284 return da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
285 info
->conf
.sel_mask
, DA9055_SEL_REG_A
);
290 static const struct regulator_ops da9055_buck_ops
= {
291 .get_mode
= da9055_buck_get_mode
,
292 .set_mode
= da9055_buck_set_mode
,
294 .get_current_limit
= regulator_get_current_limit_regmap
,
295 .set_current_limit
= regulator_set_current_limit_regmap
,
297 .get_voltage_sel
= da9055_regulator_get_voltage_sel
,
298 .set_voltage_sel
= da9055_regulator_set_voltage_sel
,
299 .list_voltage
= regulator_list_voltage_linear
,
300 .map_voltage
= regulator_map_voltage_linear
,
301 .is_enabled
= regulator_is_enabled_regmap
,
302 .enable
= regulator_enable_regmap
,
303 .disable
= regulator_disable_regmap
,
305 .set_suspend_voltage
= da9055_regulator_set_suspend_voltage
,
306 .set_suspend_enable
= da9055_suspend_enable
,
307 .set_suspend_disable
= da9055_suspend_disable
,
308 .set_suspend_mode
= da9055_buck_set_mode
,
311 static const struct regulator_ops da9055_ldo_ops
= {
312 .get_mode
= da9055_ldo_get_mode
,
313 .set_mode
= da9055_ldo_set_mode
,
315 .get_voltage_sel
= da9055_regulator_get_voltage_sel
,
316 .set_voltage_sel
= da9055_regulator_set_voltage_sel
,
317 .list_voltage
= regulator_list_voltage_linear
,
318 .map_voltage
= regulator_map_voltage_linear
,
319 .is_enabled
= regulator_is_enabled_regmap
,
320 .enable
= regulator_enable_regmap
,
321 .disable
= regulator_disable_regmap
,
323 .set_suspend_voltage
= da9055_regulator_set_suspend_voltage
,
324 .set_suspend_enable
= da9055_suspend_enable
,
325 .set_suspend_disable
= da9055_suspend_disable
,
326 .set_suspend_mode
= da9055_ldo_set_mode
,
330 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
334 .of_match = of_match_ptr(#_id),\
335 .regulators_node = of_match_ptr("regulators"),\
336 .ops = &da9055_ldo_ops,\
337 .type = REGULATOR_VOLTAGE,\
338 .id = DA9055_ID_##_id,\
339 .n_voltages = (max - min) / step + 1 + (voffset), \
340 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
342 .min_uV = (min) * 1000,\
343 .uV_step = (step) * 1000,\
344 .linear_min_sel = (voffset),\
345 .owner = THIS_MODULE,\
348 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
349 .sel_mask = (1 << 4),\
353 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
354 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
356 .v_mask = (1 << (vbits)) - 1,\
360 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
364 .of_match = of_match_ptr(#_id),\
365 .regulators_node = of_match_ptr("regulators"),\
366 .ops = &da9055_buck_ops,\
367 .type = REGULATOR_VOLTAGE,\
368 .id = DA9055_ID_##_id,\
369 .n_voltages = (max - min) / step + 1 + (voffset), \
370 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
372 .min_uV = (min) * 1000,\
373 .uV_step = (step) * 1000,\
374 .linear_min_sel = (voffset),\
375 .owner = THIS_MODULE,\
376 .curr_table = da9055_current_limits,\
377 .n_current_limits = ARRAY_SIZE(da9055_current_limits),\
378 .csel_reg = DA9055_REG_BUCK_LIM,\
379 .csel_mask = (mbits),\
382 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
383 .sel_mask = (1 << 4),\
387 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
388 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
390 .v_mask = (1 << (vbits)) - 1,\
393 .reg = DA9055_REG_BCORE_MODE,\
399 static const struct da9055_regulator_info da9055_regulator_info
[] = {
400 DA9055_BUCK(BUCK1
, 25, 725, 2075, 6, 9, 0xc, 2),
401 DA9055_BUCK(BUCK2
, 25, 925, 2500, 6, 0, 3, 0),
402 DA9055_LDO(LDO1
, 50, 900, 3300, 6, 2),
403 DA9055_LDO(LDO2
, 50, 900, 3300, 6, 3),
404 DA9055_LDO(LDO3
, 50, 900, 3300, 6, 2),
405 DA9055_LDO(LDO4
, 50, 900, 3300, 6, 2),
406 DA9055_LDO(LDO5
, 50, 900, 2750, 6, 2),
407 DA9055_LDO(LDO6
, 20, 900, 3300, 7, 0),
411 * Configures regulator to be controlled either through GPIO 1 or 2.
412 * GPIO can control regulator state and/or select the regulator register
413 * set A/B for voltage ramping.
415 static int da9055_gpio_init(struct device
*dev
,
416 struct da9055_regulator
*regulator
,
417 struct regulator_config
*config
,
418 struct da9055_pdata
*pdata
, int id
)
420 const struct da9055_regulator_info
*info
= regulator
->info
;
421 struct gpio_desc
*ren
;
422 struct gpio_desc
*ena
;
423 struct gpio_desc
*rsel
;
426 /* Look for "regulator-enable-gpios" GPIOs in the regulator node */
427 ren
= devm_gpiod_get_optional(dev
, "regulator-enable", GPIOD_IN
);
432 /* This GPIO is not optional at this point */
433 ena
= devm_gpiod_get(dev
, "enable", GPIOD_OUT_HIGH
);
437 config
->ena_gpiod
= ena
;
440 * GPI pin is muxed with regulator to control the
443 gpiod_set_consumer_name(ren
, "DA9055 ren GPI");
446 * Let the regulator know that its state is controlled
449 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
452 << DA9055_E_GPI_SHIFT
);
457 /* Look for "regulator-select-gpios" GPIOs in the regulator node */
458 rsel
= devm_gpiod_get_optional(dev
, "regulator-select", GPIOD_IN
);
460 return PTR_ERR(rsel
);
463 regulator
->reg_rselect
= pdata
->reg_rsel
[id
];
466 * GPI pin is muxed with regulator to select the
467 * regulator register set A/B for voltage ramping.
469 gpiod_set_consumer_name(rsel
, "DA9055 rsel GPI");
472 * Let the regulator know that its register set A/B
473 * will be selected through GPI for voltage ramping.
475 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
478 << DA9055_V_GPI_SHIFT
);
484 static irqreturn_t
da9055_ldo5_6_oc_irq(int irq
, void *data
)
486 struct da9055_regulator
*regulator
= data
;
488 regulator_notifier_call_chain(regulator
->rdev
,
489 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
494 static inline const struct da9055_regulator_info
*find_regulator_info(int id
)
496 const struct da9055_regulator_info
*info
;
499 for (i
= 0; i
< ARRAY_SIZE(da9055_regulator_info
); i
++) {
500 info
= &da9055_regulator_info
[i
];
501 if (info
->reg_desc
.id
== id
)
508 static int da9055_regulator_probe(struct platform_device
*pdev
)
510 struct regulator_config config
= { };
511 struct da9055_regulator
*regulator
;
512 struct da9055
*da9055
= dev_get_drvdata(pdev
->dev
.parent
);
513 struct da9055_pdata
*pdata
= dev_get_platdata(da9055
->dev
);
516 regulator
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9055_regulator
),
521 regulator
->info
= find_regulator_info(pdev
->id
);
522 if (regulator
->info
== NULL
) {
523 dev_err(&pdev
->dev
, "invalid regulator ID specified\n");
527 regulator
->da9055
= da9055
;
528 config
.dev
= da9055
->dev
;
529 config
.driver_data
= regulator
;
530 config
.regmap
= da9055
->regmap
;
533 config
.init_data
= pdata
->regulators
[pdev
->id
];
535 ret
= da9055_gpio_init(&pdev
->dev
, regulator
, &config
, pdata
, pdev
->id
);
539 regulator
->rdev
= devm_regulator_register(&pdev
->dev
,
540 ®ulator
->info
->reg_desc
,
542 if (IS_ERR(regulator
->rdev
)) {
543 dev_err(&pdev
->dev
, "Failed to register regulator %s\n",
544 regulator
->info
->reg_desc
.name
);
545 return PTR_ERR(regulator
->rdev
);
548 /* Only LDO 5 and 6 has got the over current interrupt */
549 if (pdev
->id
== DA9055_ID_LDO5
|| pdev
->id
== DA9055_ID_LDO6
) {
550 irq
= platform_get_irq_byname(pdev
, "REGULATOR");
554 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
555 da9055_ldo5_6_oc_irq
,
559 pdev
->name
, regulator
);
563 "Failed to request Regulator IRQ %d: %d\n",
570 platform_set_drvdata(pdev
, regulator
);
575 static struct platform_driver da9055_regulator_driver
= {
576 .probe
= da9055_regulator_probe
,
578 .name
= "da9055-regulator",
579 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
583 static int __init
da9055_regulator_init(void)
585 return platform_driver_register(&da9055_regulator_driver
);
587 subsys_initcall(da9055_regulator_init
);
589 static void __exit
da9055_regulator_exit(void)
591 platform_driver_unregister(&da9055_regulator_driver
);
593 module_exit(da9055_regulator_exit
);
595 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
596 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
597 MODULE_LICENSE("GPL");
598 MODULE_ALIAS("platform:da9055-regulator");