2 * Regulator driver for DA9055 PMIC
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.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.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
26 #include <linux/mfd/da9055/core.h>
27 #include <linux/mfd/da9055/reg.h>
28 #include <linux/mfd/da9055/pdata.h>
30 #define DA9055_MIN_UA 0
31 #define DA9055_MAX_UA 3
33 #define DA9055_LDO_MODE_SYNC 0
34 #define DA9055_LDO_MODE_SLEEP 1
36 #define DA9055_BUCK_MODE_SLEEP 1
37 #define DA9055_BUCK_MODE_SYNC 2
38 #define DA9055_BUCK_MODE_AUTO 3
40 /* DA9055 REGULATOR IDs */
41 #define DA9055_ID_BUCK1 0
42 #define DA9055_ID_BUCK2 1
43 #define DA9055_ID_LDO1 2
44 #define DA9055_ID_LDO2 3
45 #define DA9055_ID_LDO3 4
46 #define DA9055_ID_LDO4 5
47 #define DA9055_ID_LDO5 6
48 #define DA9055_ID_LDO6 7
50 /* DA9055 BUCK current limit */
51 static const unsigned int da9055_current_limits
[] = {
52 500000, 600000, 700000, 800000
55 struct da9055_conf_reg
{
61 struct da9055_volt_reg
{
68 struct da9055_mode_reg
{
74 struct da9055_regulator_info
{
75 struct regulator_desc reg_desc
;
76 struct da9055_conf_reg conf
;
77 struct da9055_volt_reg volt
;
78 struct da9055_mode_reg mode
;
81 struct da9055_regulator
{
82 struct da9055
*da9055
;
83 struct da9055_regulator_info
*info
;
84 struct regulator_dev
*rdev
;
85 enum gpio_select reg_rselect
;
88 static unsigned int da9055_buck_get_mode(struct regulator_dev
*rdev
)
90 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
91 struct da9055_regulator_info
*info
= regulator
->info
;
94 ret
= da9055_reg_read(regulator
->da9055
, info
->mode
.reg
);
98 switch ((ret
& info
->mode
.mask
) >> info
->mode
.shift
) {
99 case DA9055_BUCK_MODE_SYNC
:
100 mode
= REGULATOR_MODE_FAST
;
102 case DA9055_BUCK_MODE_AUTO
:
103 mode
= REGULATOR_MODE_NORMAL
;
105 case DA9055_BUCK_MODE_SLEEP
:
106 mode
= REGULATOR_MODE_STANDBY
;
113 static int da9055_buck_set_mode(struct regulator_dev
*rdev
,
116 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
117 struct da9055_regulator_info
*info
= regulator
->info
;
121 case REGULATOR_MODE_FAST
:
122 val
= DA9055_BUCK_MODE_SYNC
<< info
->mode
.shift
;
124 case REGULATOR_MODE_NORMAL
:
125 val
= DA9055_BUCK_MODE_AUTO
<< info
->mode
.shift
;
127 case REGULATOR_MODE_STANDBY
:
128 val
= DA9055_BUCK_MODE_SLEEP
<< info
->mode
.shift
;
132 return da9055_reg_update(regulator
->da9055
, info
->mode
.reg
,
133 info
->mode
.mask
, val
);
136 static unsigned int da9055_ldo_get_mode(struct regulator_dev
*rdev
)
138 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
139 struct da9055_regulator_info
*info
= regulator
->info
;
142 ret
= da9055_reg_read(regulator
->da9055
, info
->volt
.reg_b
);
146 if (ret
>> info
->volt
.sl_shift
)
147 return REGULATOR_MODE_STANDBY
;
149 return REGULATOR_MODE_NORMAL
;
152 static int da9055_ldo_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
154 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
155 struct da9055_regulator_info
*info
= regulator
->info
;
156 struct da9055_volt_reg volt
= info
->volt
;
160 case REGULATOR_MODE_NORMAL
:
161 case REGULATOR_MODE_FAST
:
162 val
= DA9055_LDO_MODE_SYNC
;
164 case REGULATOR_MODE_STANDBY
:
165 val
= DA9055_LDO_MODE_SLEEP
;
169 return da9055_reg_update(regulator
->da9055
, volt
.reg_b
,
171 val
<< volt
.sl_shift
);
174 static int da9055_regulator_get_voltage_sel(struct regulator_dev
*rdev
)
176 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
177 struct da9055_regulator_info
*info
= regulator
->info
;
178 struct da9055_volt_reg volt
= info
->volt
;
182 * There are two voltage register set A & B for voltage ramping but
183 * either one of then can be active therefore we first determine
184 * the active register set.
186 ret
= da9055_reg_read(regulator
->da9055
, info
->conf
.reg
);
190 ret
&= info
->conf
.sel_mask
;
192 /* Get the voltage for the active register set A/B */
193 if (ret
== DA9055_REGUALTOR_SET_A
)
194 ret
= da9055_reg_read(regulator
->da9055
, volt
.reg_a
);
196 ret
= da9055_reg_read(regulator
->da9055
, volt
.reg_b
);
201 sel
= (ret
& volt
.v_mask
);
205 static int da9055_regulator_set_voltage_sel(struct regulator_dev
*rdev
,
206 unsigned int selector
)
208 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
209 struct da9055_regulator_info
*info
= regulator
->info
;
213 * Regulator register set A/B is not selected through GPIO therefore
214 * we use default register set A for voltage ramping.
216 if (regulator
->reg_rselect
== NO_GPIO
) {
217 /* Select register set A */
218 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
219 info
->conf
.sel_mask
, DA9055_SEL_REG_A
);
223 /* Set the voltage */
224 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_a
,
225 info
->volt
.v_mask
, selector
);
229 * Here regulator register set A/B is selected through GPIO.
230 * Therefore we first determine the selected register set A/B and
231 * then set the desired voltage for that register set A/B.
233 ret
= da9055_reg_read(regulator
->da9055
, info
->conf
.reg
);
237 ret
&= info
->conf
.sel_mask
;
239 /* Set the voltage */
240 if (ret
== DA9055_REGUALTOR_SET_A
)
241 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_a
,
242 info
->volt
.v_mask
, selector
);
244 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_b
,
245 info
->volt
.v_mask
, selector
);
248 static int da9055_regulator_set_suspend_voltage(struct regulator_dev
*rdev
,
251 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
252 struct da9055_regulator_info
*info
= regulator
->info
;
255 /* Select register set B for suspend voltage ramping. */
256 if (regulator
->reg_rselect
== NO_GPIO
) {
257 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
258 info
->conf
.sel_mask
, DA9055_SEL_REG_B
);
263 ret
= regulator_map_voltage_linear(rdev
, uV
, uV
);
267 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_b
,
268 info
->volt
.v_mask
, ret
);
271 static int da9055_suspend_enable(struct regulator_dev
*rdev
)
273 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
274 struct da9055_regulator_info
*info
= regulator
->info
;
276 /* Select register set B for voltage ramping. */
277 if (regulator
->reg_rselect
== NO_GPIO
)
278 return da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
279 info
->conf
.sel_mask
, DA9055_SEL_REG_B
);
284 static int da9055_suspend_disable(struct regulator_dev
*rdev
)
286 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
287 struct da9055_regulator_info
*info
= regulator
->info
;
289 /* Diselect register set B. */
290 if (regulator
->reg_rselect
== NO_GPIO
)
291 return da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
292 info
->conf
.sel_mask
, DA9055_SEL_REG_A
);
297 static const struct regulator_ops da9055_buck_ops
= {
298 .get_mode
= da9055_buck_get_mode
,
299 .set_mode
= da9055_buck_set_mode
,
301 .get_current_limit
= regulator_get_current_limit_regmap
,
302 .set_current_limit
= regulator_set_current_limit_regmap
,
304 .get_voltage_sel
= da9055_regulator_get_voltage_sel
,
305 .set_voltage_sel
= da9055_regulator_set_voltage_sel
,
306 .list_voltage
= regulator_list_voltage_linear
,
307 .map_voltage
= regulator_map_voltage_linear
,
308 .is_enabled
= regulator_is_enabled_regmap
,
309 .enable
= regulator_enable_regmap
,
310 .disable
= regulator_disable_regmap
,
312 .set_suspend_voltage
= da9055_regulator_set_suspend_voltage
,
313 .set_suspend_enable
= da9055_suspend_enable
,
314 .set_suspend_disable
= da9055_suspend_disable
,
315 .set_suspend_mode
= da9055_buck_set_mode
,
318 static const struct regulator_ops da9055_ldo_ops
= {
319 .get_mode
= da9055_ldo_get_mode
,
320 .set_mode
= da9055_ldo_set_mode
,
322 .get_voltage_sel
= da9055_regulator_get_voltage_sel
,
323 .set_voltage_sel
= da9055_regulator_set_voltage_sel
,
324 .list_voltage
= regulator_list_voltage_linear
,
325 .map_voltage
= regulator_map_voltage_linear
,
326 .is_enabled
= regulator_is_enabled_regmap
,
327 .enable
= regulator_enable_regmap
,
328 .disable
= regulator_disable_regmap
,
330 .set_suspend_voltage
= da9055_regulator_set_suspend_voltage
,
331 .set_suspend_enable
= da9055_suspend_enable
,
332 .set_suspend_disable
= da9055_suspend_disable
,
333 .set_suspend_mode
= da9055_ldo_set_mode
,
337 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
341 .ops = &da9055_ldo_ops,\
342 .type = REGULATOR_VOLTAGE,\
343 .id = DA9055_ID_##_id,\
344 .n_voltages = (max - min) / step + 1 + (voffset), \
345 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
347 .min_uV = (min) * 1000,\
348 .uV_step = (step) * 1000,\
349 .linear_min_sel = (voffset),\
350 .owner = THIS_MODULE,\
353 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
354 .sel_mask = (1 << 4),\
358 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
359 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
361 .v_mask = (1 << (vbits)) - 1,\
365 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
369 .ops = &da9055_buck_ops,\
370 .type = REGULATOR_VOLTAGE,\
371 .id = DA9055_ID_##_id,\
372 .n_voltages = (max - min) / step + 1 + (voffset), \
373 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
375 .min_uV = (min) * 1000,\
376 .uV_step = (step) * 1000,\
377 .linear_min_sel = (voffset),\
378 .owner = THIS_MODULE,\
379 .curr_table = da9055_current_limits,\
380 .n_current_limits = ARRAY_SIZE(da9055_current_limits),\
381 .csel_reg = DA9055_REG_BUCK_LIM,\
382 .csel_mask = (mbits),\
385 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
386 .sel_mask = (1 << 4),\
390 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
391 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
393 .v_mask = (1 << (vbits)) - 1,\
396 .reg = DA9055_REG_BCORE_MODE,\
402 static struct da9055_regulator_info da9055_regulator_info
[] = {
403 DA9055_BUCK(BUCK1
, 25, 725, 2075, 6, 9, 0xc, 2),
404 DA9055_BUCK(BUCK2
, 25, 925, 2500, 6, 0, 3, 0),
405 DA9055_LDO(LDO1
, 50, 900, 3300, 6, 2),
406 DA9055_LDO(LDO2
, 50, 900, 3300, 6, 3),
407 DA9055_LDO(LDO3
, 50, 900, 3300, 6, 2),
408 DA9055_LDO(LDO4
, 50, 900, 3300, 6, 2),
409 DA9055_LDO(LDO5
, 50, 900, 2750, 6, 2),
410 DA9055_LDO(LDO6
, 20, 900, 3300, 7, 0),
414 * Configures regulator to be controlled either through GPIO 1 or 2.
415 * GPIO can control regulator state and/or select the regulator register
416 * set A/B for voltage ramping.
418 static int da9055_gpio_init(struct da9055_regulator
*regulator
,
419 struct regulator_config
*config
,
420 struct da9055_pdata
*pdata
, int id
)
422 struct da9055_regulator_info
*info
= regulator
->info
;
428 if (pdata
->gpio_ren
&& pdata
->gpio_ren
[id
]) {
430 int gpio_mux
= pdata
->gpio_ren
[id
];
432 config
->ena_gpiod
= pdata
->ena_gpiods
[id
];
435 * GPI pin is muxed with regulator to control the
438 sprintf(name
, "DA9055 GPI %d", gpio_mux
);
439 ret
= devm_gpio_request_one(config
->dev
, gpio_mux
, GPIOF_DIR_IN
,
445 * Let the regulator know that its state is controlled
448 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
451 << DA9055_E_GPI_SHIFT
);
456 if (pdata
->gpio_rsel
&& pdata
->gpio_rsel
[id
]) {
458 int gpio_mux
= pdata
->gpio_rsel
[id
];
460 regulator
->reg_rselect
= pdata
->reg_rsel
[id
];
463 * GPI pin is muxed with regulator to select the
464 * regulator register set A/B for voltage ramping.
466 sprintf(name
, "DA9055 GPI %d", gpio_mux
);
467 ret
= devm_gpio_request_one(config
->dev
, gpio_mux
, GPIOF_DIR_IN
,
473 * Let the regulator know that its register set A/B
474 * will be selected through GPI for voltage ramping.
476 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
479 << DA9055_V_GPI_SHIFT
);
486 static irqreturn_t
da9055_ldo5_6_oc_irq(int irq
, void *data
)
488 struct da9055_regulator
*regulator
= data
;
490 regulator_lock(regulator
->rdev
);
491 regulator_notifier_call_chain(regulator
->rdev
,
492 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
493 regulator_unlock(regulator
->rdev
);
498 static inline struct da9055_regulator_info
*find_regulator_info(int id
)
500 struct da9055_regulator_info
*info
;
503 for (i
= 0; i
< ARRAY_SIZE(da9055_regulator_info
); i
++) {
504 info
= &da9055_regulator_info
[i
];
505 if (info
->reg_desc
.id
== id
)
513 static struct of_regulator_match da9055_reg_matches
[] = {
514 { .name
= "BUCK1", },
515 { .name
= "BUCK2", },
524 static int da9055_regulator_dt_init(struct platform_device
*pdev
,
525 struct da9055_regulator
*regulator
,
526 struct regulator_config
*config
,
529 struct device_node
*nproot
, *np
;
532 nproot
= of_node_get(pdev
->dev
.parent
->of_node
);
536 np
= of_get_child_by_name(nproot
, "regulators");
540 ret
= of_regulator_match(&pdev
->dev
, np
, &da9055_reg_matches
[regid
], 1);
543 dev_err(&pdev
->dev
, "Error matching regulator: %d\n", ret
);
547 config
->init_data
= da9055_reg_matches
[regid
].init_data
;
548 config
->of_node
= da9055_reg_matches
[regid
].of_node
;
550 if (!config
->of_node
)
556 static inline int da9055_regulator_dt_init(struct platform_device
*pdev
,
557 struct da9055_regulator
*regulator
,
558 struct regulator_config
*config
,
563 #endif /* CONFIG_OF */
565 static int da9055_regulator_probe(struct platform_device
*pdev
)
567 struct regulator_config config
= { };
568 struct da9055_regulator
*regulator
;
569 struct da9055
*da9055
= dev_get_drvdata(pdev
->dev
.parent
);
570 struct da9055_pdata
*pdata
= dev_get_platdata(da9055
->dev
);
573 regulator
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9055_regulator
),
578 regulator
->info
= find_regulator_info(pdev
->id
);
579 if (regulator
->info
== NULL
) {
580 dev_err(&pdev
->dev
, "invalid regulator ID specified\n");
584 regulator
->da9055
= da9055
;
585 config
.dev
= &pdev
->dev
;
586 config
.driver_data
= regulator
;
587 config
.regmap
= da9055
->regmap
;
590 config
.init_data
= pdata
->regulators
[pdev
->id
];
592 ret
= da9055_regulator_dt_init(pdev
, regulator
, &config
,
598 ret
= da9055_gpio_init(regulator
, &config
, pdata
, pdev
->id
);
602 regulator
->rdev
= devm_regulator_register(&pdev
->dev
,
603 ®ulator
->info
->reg_desc
,
605 if (IS_ERR(regulator
->rdev
)) {
606 dev_err(&pdev
->dev
, "Failed to register regulator %s\n",
607 regulator
->info
->reg_desc
.name
);
608 return PTR_ERR(regulator
->rdev
);
611 /* Only LDO 5 and 6 has got the over current interrupt */
612 if (pdev
->id
== DA9055_ID_LDO5
|| pdev
->id
== DA9055_ID_LDO6
) {
613 irq
= platform_get_irq_byname(pdev
, "REGULATOR");
617 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
618 da9055_ldo5_6_oc_irq
,
622 pdev
->name
, regulator
);
626 "Failed to request Regulator IRQ %d: %d\n",
633 platform_set_drvdata(pdev
, regulator
);
638 static struct platform_driver da9055_regulator_driver
= {
639 .probe
= da9055_regulator_probe
,
641 .name
= "da9055-regulator",
645 static int __init
da9055_regulator_init(void)
647 return platform_driver_register(&da9055_regulator_driver
);
649 subsys_initcall(da9055_regulator_init
);
651 static void __exit
da9055_regulator_exit(void)
653 platform_driver_unregister(&da9055_regulator_driver
);
655 module_exit(da9055_regulator_exit
);
657 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
658 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
659 MODULE_LICENSE("GPL");
660 MODULE_ALIAS("platform:da9055-regulator");