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 {
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_list_voltage(struct regulator_dev *rdev, unsigned selector)
206 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
207 struct da9055_regulator_info *info = regulator->info;
209 if (selector >= rdev->desc->n_voltages)
212 if (selector < info->volt.v_offset)
215 selector -= info->volt.v_offset;
216 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
219 static int da9055_map_voltage(struct regulator_dev *rdev, int min_uV,
222 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
223 struct da9055_regulator_info *info = regulator->info;
226 if (min_uV < rdev->desc->min_uV)
227 min_uV = rdev->desc->min_uV;
229 sel = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
230 sel += info->volt.v_offset;
232 voltage = da9055_list_voltage(rdev, sel);
233 if (voltage < min_uV || voltage > max_uV)
239 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
241 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
242 struct da9055_regulator_info *info = regulator->info;
243 struct da9055_volt_reg volt = info->volt;
247 * There are two voltage register set A & B for voltage ramping but
248 * either one of then can be active therefore we first determine
249 * the active register set.
251 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
255 ret &= info->conf.sel_mask;
257 /* Get the voltage for the active register set A/B */
258 if (ret == DA9055_REGUALTOR_SET_A)
259 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
261 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
266 sel = (ret & volt.v_mask);
267 if (sel <= volt.v_offset)
273 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
274 unsigned int selector)
276 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
277 struct da9055_regulator_info *info = regulator->info;
281 * Regulator register set A/B is not selected through GPIO therefore
282 * we use default register set A for voltage ramping.
284 if (regulator->reg_rselect == NO_GPIO) {
285 /* Select register set A */
286 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
287 info->conf.sel_mask, DA9055_SEL_REG_A);
291 /* Set the voltage */
292 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
293 info->volt.v_mask, selector);
297 * Here regulator register set A/B is selected through GPIO.
298 * Therefore we first determine the selected register set A/B and
299 * then set the desired voltage for that register set A/B.
301 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
305 ret &= info->conf.sel_mask;
307 /* Set the voltage */
308 if (ret == DA9055_REGUALTOR_SET_A)
309 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
310 info->volt.v_mask, selector);
312 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
313 info->volt.v_mask, selector);
316 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
319 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
320 struct da9055_regulator_info *info = regulator->info;
323 /* Select register set B for suspend voltage ramping. */
324 if (regulator->reg_rselect == NO_GPIO) {
325 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
326 info->conf.sel_mask, DA9055_SEL_REG_B);
331 ret = da9055_map_voltage(rdev, uV, uV);
335 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
336 info->volt.v_mask, ret);
339 static int da9055_suspend_enable(struct regulator_dev *rdev)
341 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
342 struct da9055_regulator_info *info = regulator->info;
344 /* Select register set B for voltage ramping. */
345 if (regulator->reg_rselect == NO_GPIO)
346 return da9055_reg_update(regulator->da9055, info->conf.reg,
347 info->conf.sel_mask, DA9055_SEL_REG_B);
352 static int da9055_suspend_disable(struct regulator_dev *rdev)
354 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
355 struct da9055_regulator_info *info = regulator->info;
357 /* Diselect register set B. */
358 if (regulator->reg_rselect == NO_GPIO)
359 return da9055_reg_update(regulator->da9055, info->conf.reg,
360 info->conf.sel_mask, DA9055_SEL_REG_A);
365 static struct regulator_ops da9055_buck_ops = {
366 .get_mode = da9055_buck_get_mode,
367 .set_mode = da9055_buck_set_mode,
369 .get_current_limit = da9055_buck_get_current_limit,
370 .set_current_limit = da9055_buck_set_current_limit,
372 .get_voltage_sel = da9055_regulator_get_voltage_sel,
373 .set_voltage_sel = da9055_regulator_set_voltage_sel,
374 .list_voltage = da9055_list_voltage,
375 .map_voltage = da9055_map_voltage,
376 .is_enabled = regulator_is_enabled_regmap,
377 .enable = regulator_enable_regmap,
378 .disable = regulator_disable_regmap,
380 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
381 .set_suspend_enable = da9055_suspend_enable,
382 .set_suspend_disable = da9055_suspend_disable,
383 .set_suspend_mode = da9055_buck_set_mode,
386 static struct regulator_ops da9055_ldo_ops = {
387 .get_mode = da9055_ldo_get_mode,
388 .set_mode = da9055_ldo_set_mode,
390 .get_voltage_sel = da9055_regulator_get_voltage_sel,
391 .set_voltage_sel = da9055_regulator_set_voltage_sel,
392 .list_voltage = da9055_list_voltage,
393 .map_voltage = da9055_map_voltage,
394 .is_enabled = regulator_is_enabled_regmap,
395 .enable = regulator_enable_regmap,
396 .disable = regulator_disable_regmap,
398 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
399 .set_suspend_enable = da9055_suspend_enable,
400 .set_suspend_disable = da9055_suspend_disable,
401 .set_suspend_mode = da9055_ldo_set_mode,
405 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
409 .ops = &da9055_ldo_ops,\
410 .type = REGULATOR_VOLTAGE,\
411 .id = DA9055_ID_##_id,\
412 .n_voltages = (max - min) / step + 1 + (voffset), \
413 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
415 .min_uV = (min) * 1000,\
416 .uV_step = (step) * 1000,\
417 .owner = THIS_MODULE,\
420 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
421 .sel_mask = (1 << 4),\
425 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
426 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
428 .v_offset = (voffset),\
429 .v_mask = (1 << (vbits)) - 1,\
434 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
438 .ops = &da9055_buck_ops,\
439 .type = REGULATOR_VOLTAGE,\
440 .id = DA9055_ID_##_id,\
441 .n_voltages = (max - min) / step + 1 + (voffset), \
442 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
444 .min_uV = (min) * 1000,\
445 .uV_step = (step) * 1000,\
446 .owner = THIS_MODULE,\
449 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
450 .sel_mask = (1 << 4),\
454 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
455 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
457 .v_offset = (voffset),\
458 .v_mask = (1 << (vbits)) - 1,\
462 .reg = DA9055_REG_BCORE_MODE,\
468 static struct da9055_regulator_info da9055_regulator_info[] = {
469 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
470 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
471 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
472 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
473 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
474 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
475 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
476 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
480 * Configures regulator to be controlled either through GPIO 1 or 2.
481 * GPIO can control regulator state and/or select the regulator register
482 * set A/B for voltage ramping.
484 static __devinit int da9055_gpio_init(struct da9055_regulator *regulator,
485 struct regulator_config *config,
486 struct da9055_pdata *pdata, int id)
488 struct da9055_regulator_info *info = regulator->info;
491 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
493 int gpio_mux = pdata->gpio_ren[id];
495 config->ena_gpio = pdata->ena_gpio[id];
496 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
497 config->ena_gpio_invert = 1;
500 * GPI pin is muxed with regulator to control the
503 sprintf(name, "DA9055 GPI %d", gpio_mux);
504 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
510 * Let the regulator know that its state is controlled
513 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
516 << DA9055_E_GPI_SHIFT);
521 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
523 int gpio_mux = pdata->gpio_rsel[id];
525 regulator->reg_rselect = pdata->reg_rsel[id];
528 * GPI pin is muxed with regulator to select the
529 * regulator register set A/B for voltage ramping.
531 sprintf(name, "DA9055 GPI %d", gpio_mux);
532 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
538 * Let the regulator know that its register set A/B
539 * will be selected through GPI for voltage ramping.
541 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
544 << DA9055_V_GPI_SHIFT);
551 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
553 struct da9055_regulator *regulator = data;
555 regulator_notifier_call_chain(regulator->rdev,
556 REGULATOR_EVENT_OVER_CURRENT, NULL);
561 static inline struct da9055_regulator_info *find_regulator_info(int id)
563 struct da9055_regulator_info *info;
566 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
567 info = &da9055_regulator_info[i];
568 if (info->reg_desc.id == id)
575 static int __devinit da9055_regulator_probe(struct platform_device *pdev)
577 struct regulator_config config = { };
578 struct da9055_regulator *regulator;
579 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
580 struct da9055_pdata *pdata = da9055->dev->platform_data;
583 if (pdata == NULL || pdata->regulators[pdev->id] == NULL)
586 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
591 regulator->info = find_regulator_info(pdev->id);
592 if (regulator->info == NULL) {
593 dev_err(&pdev->dev, "invalid regulator ID specified\n");
597 regulator->da9055 = da9055;
598 config.dev = &pdev->dev;
599 config.driver_data = regulator;
600 config.regmap = da9055->regmap;
602 if (pdata && pdata->regulators)
603 config.init_data = pdata->regulators[pdev->id];
605 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
609 regulator->rdev = regulator_register(®ulator->info->reg_desc,
611 if (IS_ERR(regulator->rdev)) {
612 dev_err(&pdev->dev, "Failed to register regulator %s\n",
613 regulator->info->reg_desc.name);
614 ret = PTR_ERR(regulator->rdev);
618 /* Only LDO 5 and 6 has got the over current interrupt */
619 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
620 irq = platform_get_irq_byname(pdev, "REGULATOR");
621 irq = regmap_irq_get_virq(da9055->irq_data, irq);
622 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
623 da9055_ldo5_6_oc_irq,
627 pdev->name, regulator);
631 "Failed to request Regulator IRQ %d: %d\n",
638 platform_set_drvdata(pdev, regulator);
643 regulator_unregister(regulator->rdev);
647 static int __devexit da9055_regulator_remove(struct platform_device *pdev)
649 struct da9055_regulator *regulator = platform_get_drvdata(pdev);
651 regulator_unregister(regulator->rdev);
656 static struct platform_driver da9055_regulator_driver = {
657 .probe = da9055_regulator_probe,
658 .remove = __devexit_p(da9055_regulator_remove),
660 .name = "da9055-regulator",
661 .owner = THIS_MODULE,
665 static int __init da9055_regulator_init(void)
667 return platform_driver_register(&da9055_regulator_driver);
669 subsys_initcall(da9055_regulator_init);
671 static void __exit da9055_regulator_exit(void)
673 platform_driver_unregister(&da9055_regulator_driver);
675 module_exit(da9055_regulator_exit);
677 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
678 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
679 MODULE_LICENSE("GPL");
680 MODULE_ALIAS("platform:da9055-regulator");