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/regulator/of_regulator.h>
25 #include <linux/mfd/da9055/core.h>
26 #include <linux/mfd/da9055/reg.h>
27 #include <linux/mfd/da9055/pdata.h>
29 #define DA9055_MIN_UA 0
30 #define DA9055_MAX_UA 3
32 #define DA9055_LDO_MODE_SYNC 0
33 #define DA9055_LDO_MODE_SLEEP 1
35 #define DA9055_BUCK_MODE_SLEEP 1
36 #define DA9055_BUCK_MODE_SYNC 2
37 #define DA9055_BUCK_MODE_AUTO 3
39 /* DA9055 REGULATOR IDs */
40 #define DA9055_ID_BUCK1 0
41 #define DA9055_ID_BUCK2 1
42 #define DA9055_ID_LDO1 2
43 #define DA9055_ID_LDO2 3
44 #define DA9055_ID_LDO3 4
45 #define DA9055_ID_LDO4 5
46 #define DA9055_ID_LDO5 6
47 #define DA9055_ID_LDO6 7
49 /* DA9055 BUCK current limit */
50 static const int da9055_current_limits[] = { 500000, 600000, 700000, 800000 };
52 struct da9055_conf_reg {
58 struct da9055_volt_reg {
65 struct da9055_mode_reg {
71 struct da9055_regulator_info {
72 struct regulator_desc reg_desc;
73 struct da9055_conf_reg conf;
74 struct da9055_volt_reg volt;
75 struct da9055_mode_reg mode;
78 struct da9055_regulator {
79 struct da9055 *da9055;
80 struct da9055_regulator_info *info;
81 struct regulator_dev *rdev;
82 enum gpio_select reg_rselect;
85 static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
87 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
88 struct da9055_regulator_info *info = regulator->info;
91 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
95 switch ((ret & info->mode.mask) >> info->mode.shift) {
96 case DA9055_BUCK_MODE_SYNC:
97 mode = REGULATOR_MODE_FAST;
99 case DA9055_BUCK_MODE_AUTO:
100 mode = REGULATOR_MODE_NORMAL;
102 case DA9055_BUCK_MODE_SLEEP:
103 mode = REGULATOR_MODE_STANDBY;
110 static int da9055_buck_set_mode(struct regulator_dev *rdev,
113 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
114 struct da9055_regulator_info *info = regulator->info;
118 case REGULATOR_MODE_FAST:
119 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
121 case REGULATOR_MODE_NORMAL:
122 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
124 case REGULATOR_MODE_STANDBY:
125 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
129 return da9055_reg_update(regulator->da9055, info->mode.reg,
130 info->mode.mask, val);
133 static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
135 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
136 struct da9055_regulator_info *info = regulator->info;
139 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
143 if (ret >> info->volt.sl_shift)
144 return REGULATOR_MODE_STANDBY;
146 return REGULATOR_MODE_NORMAL;
149 static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
151 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
152 struct da9055_regulator_info *info = regulator->info;
153 struct da9055_volt_reg volt = info->volt;
157 case REGULATOR_MODE_NORMAL:
158 case REGULATOR_MODE_FAST:
159 val = DA9055_LDO_MODE_SYNC;
161 case REGULATOR_MODE_STANDBY:
162 val = DA9055_LDO_MODE_SLEEP;
166 return da9055_reg_update(regulator->da9055, volt.reg_b,
168 val << volt.sl_shift);
171 static int da9055_buck_get_current_limit(struct regulator_dev *rdev)
173 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
174 struct da9055_regulator_info *info = regulator->info;
177 ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM);
181 ret &= info->mode.mask;
182 return da9055_current_limits[ret >> info->mode.shift];
185 static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA,
188 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
189 struct da9055_regulator_info *info = regulator->info;
192 for (i = ARRAY_SIZE(da9055_current_limits) - 1; i >= 0; i--) {
193 if ((min_uA <= da9055_current_limits[i]) &&
194 (da9055_current_limits[i] <= max_uA))
195 return da9055_reg_update(regulator->da9055,
198 i << info->mode.shift);
204 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
206 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
207 struct da9055_regulator_info *info = regulator->info;
208 struct da9055_volt_reg volt = info->volt;
212 * There are two voltage register set A & B for voltage ramping but
213 * either one of then can be active therefore we first determine
214 * the active register set.
216 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
220 ret &= info->conf.sel_mask;
222 /* Get the voltage for the active register set A/B */
223 if (ret == DA9055_REGUALTOR_SET_A)
224 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
226 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
231 sel = (ret & volt.v_mask);
235 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
236 unsigned int selector)
238 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
239 struct da9055_regulator_info *info = regulator->info;
243 * Regulator register set A/B is not selected through GPIO therefore
244 * we use default register set A for voltage ramping.
246 if (regulator->reg_rselect == NO_GPIO) {
247 /* Select register set A */
248 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
249 info->conf.sel_mask, DA9055_SEL_REG_A);
253 /* Set the voltage */
254 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
255 info->volt.v_mask, selector);
259 * Here regulator register set A/B is selected through GPIO.
260 * Therefore we first determine the selected register set A/B and
261 * then set the desired voltage for that register set A/B.
263 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
267 ret &= info->conf.sel_mask;
269 /* Set the voltage */
270 if (ret == DA9055_REGUALTOR_SET_A)
271 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
272 info->volt.v_mask, selector);
274 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
275 info->volt.v_mask, selector);
278 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
281 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
282 struct da9055_regulator_info *info = regulator->info;
285 /* Select register set B for suspend voltage ramping. */
286 if (regulator->reg_rselect == NO_GPIO) {
287 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
288 info->conf.sel_mask, DA9055_SEL_REG_B);
293 ret = regulator_map_voltage_linear(rdev, uV, uV);
297 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
298 info->volt.v_mask, ret);
301 static int da9055_suspend_enable(struct regulator_dev *rdev)
303 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
304 struct da9055_regulator_info *info = regulator->info;
306 /* Select register set B for voltage ramping. */
307 if (regulator->reg_rselect == NO_GPIO)
308 return da9055_reg_update(regulator->da9055, info->conf.reg,
309 info->conf.sel_mask, DA9055_SEL_REG_B);
314 static int da9055_suspend_disable(struct regulator_dev *rdev)
316 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
317 struct da9055_regulator_info *info = regulator->info;
319 /* Diselect register set B. */
320 if (regulator->reg_rselect == NO_GPIO)
321 return da9055_reg_update(regulator->da9055, info->conf.reg,
322 info->conf.sel_mask, DA9055_SEL_REG_A);
327 static struct regulator_ops da9055_buck_ops = {
328 .get_mode = da9055_buck_get_mode,
329 .set_mode = da9055_buck_set_mode,
331 .get_current_limit = da9055_buck_get_current_limit,
332 .set_current_limit = da9055_buck_set_current_limit,
334 .get_voltage_sel = da9055_regulator_get_voltage_sel,
335 .set_voltage_sel = da9055_regulator_set_voltage_sel,
336 .list_voltage = regulator_list_voltage_linear,
337 .map_voltage = regulator_map_voltage_linear,
338 .is_enabled = regulator_is_enabled_regmap,
339 .enable = regulator_enable_regmap,
340 .disable = regulator_disable_regmap,
342 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
343 .set_suspend_enable = da9055_suspend_enable,
344 .set_suspend_disable = da9055_suspend_disable,
345 .set_suspend_mode = da9055_buck_set_mode,
348 static struct regulator_ops da9055_ldo_ops = {
349 .get_mode = da9055_ldo_get_mode,
350 .set_mode = da9055_ldo_set_mode,
352 .get_voltage_sel = da9055_regulator_get_voltage_sel,
353 .set_voltage_sel = da9055_regulator_set_voltage_sel,
354 .list_voltage = regulator_list_voltage_linear,
355 .map_voltage = regulator_map_voltage_linear,
356 .is_enabled = regulator_is_enabled_regmap,
357 .enable = regulator_enable_regmap,
358 .disable = regulator_disable_regmap,
360 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
361 .set_suspend_enable = da9055_suspend_enable,
362 .set_suspend_disable = da9055_suspend_disable,
363 .set_suspend_mode = da9055_ldo_set_mode,
367 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
371 .ops = &da9055_ldo_ops,\
372 .type = REGULATOR_VOLTAGE,\
373 .id = DA9055_ID_##_id,\
374 .n_voltages = (max - min) / step + 1 + (voffset), \
375 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
377 .min_uV = (min) * 1000,\
378 .uV_step = (step) * 1000,\
379 .linear_min_sel = (voffset),\
380 .owner = THIS_MODULE,\
383 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
384 .sel_mask = (1 << 4),\
388 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
389 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
391 .v_mask = (1 << (vbits)) - 1,\
395 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
399 .ops = &da9055_buck_ops,\
400 .type = REGULATOR_VOLTAGE,\
401 .id = DA9055_ID_##_id,\
402 .n_voltages = (max - min) / step + 1 + (voffset), \
403 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
405 .min_uV = (min) * 1000,\
406 .uV_step = (step) * 1000,\
407 .linear_min_sel = (voffset),\
408 .owner = THIS_MODULE,\
411 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
412 .sel_mask = (1 << 4),\
416 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
417 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
419 .v_mask = (1 << (vbits)) - 1,\
422 .reg = DA9055_REG_BCORE_MODE,\
428 static struct da9055_regulator_info da9055_regulator_info[] = {
429 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
430 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
431 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
432 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
433 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
434 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
435 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
436 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
440 * Configures regulator to be controlled either through GPIO 1 or 2.
441 * GPIO can control regulator state and/or select the regulator register
442 * set A/B for voltage ramping.
444 static int da9055_gpio_init(struct da9055_regulator *regulator,
445 struct regulator_config *config,
446 struct da9055_pdata *pdata, int id)
448 struct da9055_regulator_info *info = regulator->info;
454 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
456 int gpio_mux = pdata->gpio_ren[id];
458 config->ena_gpio = pdata->ena_gpio[id];
459 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
460 config->ena_gpio_invert = 1;
463 * GPI pin is muxed with regulator to control the
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 state is controlled
476 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
479 << DA9055_E_GPI_SHIFT);
484 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
486 int gpio_mux = pdata->gpio_rsel[id];
488 regulator->reg_rselect = pdata->reg_rsel[id];
491 * GPI pin is muxed with regulator to select the
492 * regulator register set A/B for voltage ramping.
494 sprintf(name, "DA9055 GPI %d", gpio_mux);
495 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
501 * Let the regulator know that its register set A/B
502 * will be selected through GPI for voltage ramping.
504 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
507 << DA9055_V_GPI_SHIFT);
514 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
516 struct da9055_regulator *regulator = data;
518 regulator_notifier_call_chain(regulator->rdev,
519 REGULATOR_EVENT_OVER_CURRENT, NULL);
524 static inline struct da9055_regulator_info *find_regulator_info(int id)
526 struct da9055_regulator_info *info;
529 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
530 info = &da9055_regulator_info[i];
531 if (info->reg_desc.id == id)
539 static struct of_regulator_match da9055_reg_matches[] = {
540 { .name = "BUCK1", },
541 { .name = "BUCK2", },
550 static int da9055_regulator_dt_init(struct platform_device *pdev,
551 struct da9055_regulator *regulator,
552 struct regulator_config *config,
555 struct device_node *nproot, *np;
558 nproot = of_node_get(pdev->dev.parent->of_node);
562 np = of_get_child_by_name(nproot, "regulators");
566 ret = of_regulator_match(&pdev->dev, np, &da9055_reg_matches[regid], 1);
569 dev_err(&pdev->dev, "Error matching regulator: %d\n", ret);
573 config->init_data = da9055_reg_matches[regid].init_data;
574 config->of_node = da9055_reg_matches[regid].of_node;
576 if (!config->of_node)
582 static inline int da9055_regulator_dt_init(struct platform_device *pdev,
583 struct da9055_regulator *regulator,
584 struct regulator_config *config,
589 #endif /* CONFIG_OF */
591 static int da9055_regulator_probe(struct platform_device *pdev)
593 struct regulator_config config = { };
594 struct da9055_regulator *regulator;
595 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
596 struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
599 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
604 regulator->info = find_regulator_info(pdev->id);
605 if (regulator->info == NULL) {
606 dev_err(&pdev->dev, "invalid regulator ID specified\n");
610 regulator->da9055 = da9055;
611 config.dev = &pdev->dev;
612 config.driver_data = regulator;
613 config.regmap = da9055->regmap;
615 if (pdata && pdata->regulators) {
616 config.init_data = pdata->regulators[pdev->id];
618 ret = da9055_regulator_dt_init(pdev, regulator, &config,
624 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
628 regulator->rdev = devm_regulator_register(&pdev->dev,
629 ®ulator->info->reg_desc,
631 if (IS_ERR(regulator->rdev)) {
632 dev_err(&pdev->dev, "Failed to register regulator %s\n",
633 regulator->info->reg_desc.name);
634 return PTR_ERR(regulator->rdev);
637 /* Only LDO 5 and 6 has got the over current interrupt */
638 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
639 irq = platform_get_irq_byname(pdev, "REGULATOR");
643 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
644 da9055_ldo5_6_oc_irq,
648 pdev->name, regulator);
652 "Failed to request Regulator IRQ %d: %d\n",
659 platform_set_drvdata(pdev, regulator);
664 static struct platform_driver da9055_regulator_driver = {
665 .probe = da9055_regulator_probe,
667 .name = "da9055-regulator",
671 static int __init da9055_regulator_init(void)
673 return platform_driver_register(&da9055_regulator_driver);
675 subsys_initcall(da9055_regulator_init);
677 static void __exit da9055_regulator_exit(void)
679 platform_driver_unregister(&da9055_regulator_driver);
681 module_exit(da9055_regulator_exit);
683 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
684 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
685 MODULE_LICENSE("GPL");
686 MODULE_ALIAS("platform:da9055-regulator");