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/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
23 #include <linux/mfd/da9055/core.h>
24 #include <linux/mfd/da9055/reg.h>
25 #include <linux/mfd/da9055/pdata.h>
27 #define DA9055_MIN_UA 0
28 #define DA9055_MAX_UA 3
30 #define DA9055_LDO_MODE_SYNC 0
31 #define DA9055_LDO_MODE_SLEEP 1
33 #define DA9055_BUCK_MODE_SLEEP 1
34 #define DA9055_BUCK_MODE_SYNC 2
35 #define DA9055_BUCK_MODE_AUTO 3
37 /* DA9055 REGULATOR IDs */
38 #define DA9055_ID_BUCK1 0
39 #define DA9055_ID_BUCK2 1
40 #define DA9055_ID_LDO1 2
41 #define DA9055_ID_LDO2 3
42 #define DA9055_ID_LDO3 4
43 #define DA9055_ID_LDO4 5
44 #define DA9055_ID_LDO5 6
45 #define DA9055_ID_LDO6 7
47 /* DA9055 BUCK current limit */
48 static const int da9055_current_limits
[] = { 500000, 600000, 700000, 800000 };
50 struct da9055_conf_reg
{
56 struct da9055_volt_reg
{
63 struct da9055_mode_reg
{
69 struct da9055_regulator_info
{
70 struct regulator_desc reg_desc
;
71 struct da9055_conf_reg conf
;
72 struct da9055_volt_reg volt
;
73 struct da9055_mode_reg mode
;
76 struct da9055_regulator
{
77 struct da9055
*da9055
;
78 struct da9055_regulator_info
*info
;
79 struct regulator_dev
*rdev
;
80 enum gpio_select reg_rselect
;
83 static unsigned int da9055_buck_get_mode(struct regulator_dev
*rdev
)
85 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
86 struct da9055_regulator_info
*info
= regulator
->info
;
89 ret
= da9055_reg_read(regulator
->da9055
, info
->mode
.reg
);
93 switch ((ret
& info
->mode
.mask
) >> info
->mode
.shift
) {
94 case DA9055_BUCK_MODE_SYNC
:
95 mode
= REGULATOR_MODE_FAST
;
97 case DA9055_BUCK_MODE_AUTO
:
98 mode
= REGULATOR_MODE_NORMAL
;
100 case DA9055_BUCK_MODE_SLEEP
:
101 mode
= REGULATOR_MODE_STANDBY
;
108 static int da9055_buck_set_mode(struct regulator_dev
*rdev
,
111 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
112 struct da9055_regulator_info
*info
= regulator
->info
;
116 case REGULATOR_MODE_FAST
:
117 val
= DA9055_BUCK_MODE_SYNC
<< info
->mode
.shift
;
119 case REGULATOR_MODE_NORMAL
:
120 val
= DA9055_BUCK_MODE_AUTO
<< info
->mode
.shift
;
122 case REGULATOR_MODE_STANDBY
:
123 val
= DA9055_BUCK_MODE_SLEEP
<< info
->mode
.shift
;
127 return da9055_reg_update(regulator
->da9055
, info
->mode
.reg
,
128 info
->mode
.mask
, val
);
131 static unsigned int da9055_ldo_get_mode(struct regulator_dev
*rdev
)
133 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
134 struct da9055_regulator_info
*info
= regulator
->info
;
137 ret
= da9055_reg_read(regulator
->da9055
, info
->volt
.reg_b
);
141 if (ret
>> info
->volt
.sl_shift
)
142 return REGULATOR_MODE_STANDBY
;
144 return REGULATOR_MODE_NORMAL
;
147 static int da9055_ldo_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
149 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
150 struct da9055_regulator_info
*info
= regulator
->info
;
151 struct da9055_volt_reg volt
= info
->volt
;
155 case REGULATOR_MODE_NORMAL
:
156 case REGULATOR_MODE_FAST
:
157 val
= DA9055_LDO_MODE_SYNC
;
159 case REGULATOR_MODE_STANDBY
:
160 val
= DA9055_LDO_MODE_SLEEP
;
164 return da9055_reg_update(regulator
->da9055
, volt
.reg_b
,
166 val
<< volt
.sl_shift
);
169 static int da9055_buck_get_current_limit(struct regulator_dev
*rdev
)
171 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
172 struct da9055_regulator_info
*info
= regulator
->info
;
175 ret
= da9055_reg_read(regulator
->da9055
, DA9055_REG_BUCK_LIM
);
179 ret
&= info
->mode
.mask
;
180 return da9055_current_limits
[ret
>> info
->mode
.shift
];
183 static int da9055_buck_set_current_limit(struct regulator_dev
*rdev
, int min_uA
,
186 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
187 struct da9055_regulator_info
*info
= regulator
->info
;
190 for (i
= ARRAY_SIZE(da9055_current_limits
) - 1; i
>= 0; i
--) {
191 if ((min_uA
<= da9055_current_limits
[i
]) &&
192 (da9055_current_limits
[i
] <= max_uA
))
193 return da9055_reg_update(regulator
->da9055
,
196 i
<< info
->mode
.shift
);
202 static int da9055_regulator_get_voltage_sel(struct regulator_dev
*rdev
)
204 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
205 struct da9055_regulator_info
*info
= regulator
->info
;
206 struct da9055_volt_reg volt
= info
->volt
;
210 * There are two voltage register set A & B for voltage ramping but
211 * either one of then can be active therefore we first determine
212 * the active register set.
214 ret
= da9055_reg_read(regulator
->da9055
, info
->conf
.reg
);
218 ret
&= info
->conf
.sel_mask
;
220 /* Get the voltage for the active register set A/B */
221 if (ret
== DA9055_REGUALTOR_SET_A
)
222 ret
= da9055_reg_read(regulator
->da9055
, volt
.reg_a
);
224 ret
= da9055_reg_read(regulator
->da9055
, volt
.reg_b
);
229 sel
= (ret
& volt
.v_mask
);
233 static int da9055_regulator_set_voltage_sel(struct regulator_dev
*rdev
,
234 unsigned int selector
)
236 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
237 struct da9055_regulator_info
*info
= regulator
->info
;
241 * Regulator register set A/B is not selected through GPIO therefore
242 * we use default register set A for voltage ramping.
244 if (regulator
->reg_rselect
== NO_GPIO
) {
245 /* Select register set A */
246 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
247 info
->conf
.sel_mask
, DA9055_SEL_REG_A
);
251 /* Set the voltage */
252 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_a
,
253 info
->volt
.v_mask
, selector
);
257 * Here regulator register set A/B is selected through GPIO.
258 * Therefore we first determine the selected register set A/B and
259 * then set the desired voltage for that register set A/B.
261 ret
= da9055_reg_read(regulator
->da9055
, info
->conf
.reg
);
265 ret
&= info
->conf
.sel_mask
;
267 /* Set the voltage */
268 if (ret
== DA9055_REGUALTOR_SET_A
)
269 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_a
,
270 info
->volt
.v_mask
, selector
);
272 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_b
,
273 info
->volt
.v_mask
, selector
);
276 static int da9055_regulator_set_suspend_voltage(struct regulator_dev
*rdev
,
279 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
280 struct da9055_regulator_info
*info
= regulator
->info
;
283 /* Select register set B for suspend voltage ramping. */
284 if (regulator
->reg_rselect
== NO_GPIO
) {
285 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
286 info
->conf
.sel_mask
, DA9055_SEL_REG_B
);
291 ret
= regulator_map_voltage_linear(rdev
, uV
, uV
);
295 return da9055_reg_update(regulator
->da9055
, info
->volt
.reg_b
,
296 info
->volt
.v_mask
, ret
);
299 static int da9055_suspend_enable(struct regulator_dev
*rdev
)
301 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
302 struct da9055_regulator_info
*info
= regulator
->info
;
304 /* Select register set B for voltage ramping. */
305 if (regulator
->reg_rselect
== NO_GPIO
)
306 return da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
307 info
->conf
.sel_mask
, DA9055_SEL_REG_B
);
312 static int da9055_suspend_disable(struct regulator_dev
*rdev
)
314 struct da9055_regulator
*regulator
= rdev_get_drvdata(rdev
);
315 struct da9055_regulator_info
*info
= regulator
->info
;
317 /* Diselect register set B. */
318 if (regulator
->reg_rselect
== NO_GPIO
)
319 return da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
320 info
->conf
.sel_mask
, DA9055_SEL_REG_A
);
325 static struct regulator_ops da9055_buck_ops
= {
326 .get_mode
= da9055_buck_get_mode
,
327 .set_mode
= da9055_buck_set_mode
,
329 .get_current_limit
= da9055_buck_get_current_limit
,
330 .set_current_limit
= da9055_buck_set_current_limit
,
332 .get_voltage_sel
= da9055_regulator_get_voltage_sel
,
333 .set_voltage_sel
= da9055_regulator_set_voltage_sel
,
334 .list_voltage
= regulator_list_voltage_linear
,
335 .map_voltage
= regulator_map_voltage_linear
,
336 .is_enabled
= regulator_is_enabled_regmap
,
337 .enable
= regulator_enable_regmap
,
338 .disable
= regulator_disable_regmap
,
340 .set_suspend_voltage
= da9055_regulator_set_suspend_voltage
,
341 .set_suspend_enable
= da9055_suspend_enable
,
342 .set_suspend_disable
= da9055_suspend_disable
,
343 .set_suspend_mode
= da9055_buck_set_mode
,
346 static struct regulator_ops da9055_ldo_ops
= {
347 .get_mode
= da9055_ldo_get_mode
,
348 .set_mode
= da9055_ldo_set_mode
,
350 .get_voltage_sel
= da9055_regulator_get_voltage_sel
,
351 .set_voltage_sel
= da9055_regulator_set_voltage_sel
,
352 .list_voltage
= regulator_list_voltage_linear
,
353 .map_voltage
= regulator_map_voltage_linear
,
354 .is_enabled
= regulator_is_enabled_regmap
,
355 .enable
= regulator_enable_regmap
,
356 .disable
= regulator_disable_regmap
,
358 .set_suspend_voltage
= da9055_regulator_set_suspend_voltage
,
359 .set_suspend_enable
= da9055_suspend_enable
,
360 .set_suspend_disable
= da9055_suspend_disable
,
361 .set_suspend_mode
= da9055_ldo_set_mode
,
365 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
369 .ops = &da9055_ldo_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,\
381 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
382 .sel_mask = (1 << 4),\
386 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
387 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
389 .v_mask = (1 << (vbits)) - 1,\
393 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
397 .ops = &da9055_buck_ops,\
398 .type = REGULATOR_VOLTAGE,\
399 .id = DA9055_ID_##_id,\
400 .n_voltages = (max - min) / step + 1 + (voffset), \
401 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
403 .min_uV = (min) * 1000,\
404 .uV_step = (step) * 1000,\
405 .linear_min_sel = (voffset),\
406 .owner = THIS_MODULE,\
409 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
410 .sel_mask = (1 << 4),\
414 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
415 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
417 .v_mask = (1 << (vbits)) - 1,\
420 .reg = DA9055_REG_BCORE_MODE,\
426 static struct da9055_regulator_info da9055_regulator_info
[] = {
427 DA9055_BUCK(BUCK1
, 25, 725, 2075, 6, 9, 0xc, 2),
428 DA9055_BUCK(BUCK2
, 25, 925, 2500, 6, 0, 3, 0),
429 DA9055_LDO(LDO1
, 50, 900, 3300, 6, 2),
430 DA9055_LDO(LDO2
, 50, 900, 3300, 6, 3),
431 DA9055_LDO(LDO3
, 50, 900, 3300, 6, 2),
432 DA9055_LDO(LDO4
, 50, 900, 3300, 6, 2),
433 DA9055_LDO(LDO5
, 50, 900, 2750, 6, 2),
434 DA9055_LDO(LDO6
, 20, 900, 3300, 7, 0),
438 * Configures regulator to be controlled either through GPIO 1 or 2.
439 * GPIO can control regulator state and/or select the regulator register
440 * set A/B for voltage ramping.
442 static int da9055_gpio_init(struct da9055_regulator
*regulator
,
443 struct regulator_config
*config
,
444 struct da9055_pdata
*pdata
, int id
)
446 struct da9055_regulator_info
*info
= regulator
->info
;
449 if (pdata
->gpio_ren
&& pdata
->gpio_ren
[id
]) {
451 int gpio_mux
= pdata
->gpio_ren
[id
];
453 config
->ena_gpio
= pdata
->ena_gpio
[id
];
454 config
->ena_gpio_flags
= GPIOF_OUT_INIT_HIGH
;
455 config
->ena_gpio_invert
= 1;
458 * GPI pin is muxed with regulator to control the
461 sprintf(name
, "DA9055 GPI %d", gpio_mux
);
462 ret
= devm_gpio_request_one(config
->dev
, gpio_mux
, GPIOF_DIR_IN
,
468 * Let the regulator know that its state is controlled
471 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
474 << DA9055_E_GPI_SHIFT
);
479 if (pdata
->gpio_rsel
&& pdata
->gpio_rsel
[id
]) {
481 int gpio_mux
= pdata
->gpio_rsel
[id
];
483 regulator
->reg_rselect
= pdata
->reg_rsel
[id
];
486 * GPI pin is muxed with regulator to select the
487 * regulator register set A/B for voltage ramping.
489 sprintf(name
, "DA9055 GPI %d", gpio_mux
);
490 ret
= devm_gpio_request_one(config
->dev
, gpio_mux
, GPIOF_DIR_IN
,
496 * Let the regulator know that its register set A/B
497 * will be selected through GPI for voltage ramping.
499 ret
= da9055_reg_update(regulator
->da9055
, info
->conf
.reg
,
502 << DA9055_V_GPI_SHIFT
);
509 static irqreturn_t
da9055_ldo5_6_oc_irq(int irq
, void *data
)
511 struct da9055_regulator
*regulator
= data
;
513 regulator_notifier_call_chain(regulator
->rdev
,
514 REGULATOR_EVENT_OVER_CURRENT
, NULL
);
519 static inline struct da9055_regulator_info
*find_regulator_info(int id
)
521 struct da9055_regulator_info
*info
;
524 for (i
= 0; i
< ARRAY_SIZE(da9055_regulator_info
); i
++) {
525 info
= &da9055_regulator_info
[i
];
526 if (info
->reg_desc
.id
== id
)
533 static int da9055_regulator_probe(struct platform_device
*pdev
)
535 struct regulator_config config
= { };
536 struct da9055_regulator
*regulator
;
537 struct da9055
*da9055
= dev_get_drvdata(pdev
->dev
.parent
);
538 struct da9055_pdata
*pdata
= dev_get_platdata(da9055
->dev
);
541 if (pdata
== NULL
|| pdata
->regulators
[pdev
->id
] == NULL
)
544 regulator
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9055_regulator
),
549 regulator
->info
= find_regulator_info(pdev
->id
);
550 if (regulator
->info
== NULL
) {
551 dev_err(&pdev
->dev
, "invalid regulator ID specified\n");
555 regulator
->da9055
= da9055
;
556 config
.dev
= &pdev
->dev
;
557 config
.driver_data
= regulator
;
558 config
.regmap
= da9055
->regmap
;
560 if (pdata
&& pdata
->regulators
)
561 config
.init_data
= pdata
->regulators
[pdev
->id
];
563 ret
= da9055_gpio_init(regulator
, &config
, pdata
, pdev
->id
);
567 regulator
->rdev
= devm_regulator_register(&pdev
->dev
,
568 ®ulator
->info
->reg_desc
,
570 if (IS_ERR(regulator
->rdev
)) {
571 dev_err(&pdev
->dev
, "Failed to register regulator %s\n",
572 regulator
->info
->reg_desc
.name
);
573 return PTR_ERR(regulator
->rdev
);
576 /* Only LDO 5 and 6 has got the over current interrupt */
577 if (pdev
->id
== DA9055_ID_LDO5
|| pdev
->id
== DA9055_ID_LDO6
) {
578 irq
= platform_get_irq_byname(pdev
, "REGULATOR");
579 irq
= regmap_irq_get_virq(da9055
->irq_data
, irq
);
580 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
581 da9055_ldo5_6_oc_irq
,
585 pdev
->name
, regulator
);
589 "Failed to request Regulator IRQ %d: %d\n",
596 platform_set_drvdata(pdev
, regulator
);
601 static struct platform_driver da9055_regulator_driver
= {
602 .probe
= da9055_regulator_probe
,
604 .name
= "da9055-regulator",
605 .owner
= THIS_MODULE
,
609 static int __init
da9055_regulator_init(void)
611 return platform_driver_register(&da9055_regulator_driver
);
613 subsys_initcall(da9055_regulator_init
);
615 static void __exit
da9055_regulator_exit(void)
617 platform_driver_unregister(&da9055_regulator_driver
);
619 module_exit(da9055_regulator_exit
);
621 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
622 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
623 MODULE_LICENSE("GPL");
624 MODULE_ALIAS("platform:da9055-regulator");