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 {
64 struct da9055_mode_reg {
70 struct da9055_regulator_info {
71 struct regulator_desc reg_desc;
72 struct da9055_conf_reg conf;
73 struct da9055_volt_reg volt;
74 struct da9055_mode_reg mode;
77 struct da9055_regulator {
78 struct da9055 *da9055;
79 struct da9055_regulator_info *info;
80 struct regulator_dev *rdev;
81 enum gpio_select reg_rselect;
84 static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
86 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
87 struct da9055_regulator_info *info = regulator->info;
90 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
94 switch ((ret & info->mode.mask) >> info->mode.shift) {
95 case DA9055_BUCK_MODE_SYNC:
96 mode = REGULATOR_MODE_FAST;
98 case DA9055_BUCK_MODE_AUTO:
99 mode = REGULATOR_MODE_NORMAL;
101 case DA9055_BUCK_MODE_SLEEP:
102 mode = REGULATOR_MODE_STANDBY;
109 static int da9055_buck_set_mode(struct regulator_dev *rdev,
112 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
113 struct da9055_regulator_info *info = regulator->info;
117 case REGULATOR_MODE_FAST:
118 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
120 case REGULATOR_MODE_NORMAL:
121 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
123 case REGULATOR_MODE_STANDBY:
124 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
128 return da9055_reg_update(regulator->da9055, info->mode.reg,
129 info->mode.mask, val);
132 static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
134 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
135 struct da9055_regulator_info *info = regulator->info;
138 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
142 if (ret >> info->volt.sl_shift)
143 return REGULATOR_MODE_STANDBY;
145 return REGULATOR_MODE_NORMAL;
148 static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
150 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
151 struct da9055_regulator_info *info = regulator->info;
152 struct da9055_volt_reg volt = info->volt;
156 case REGULATOR_MODE_NORMAL:
157 case REGULATOR_MODE_FAST:
158 val = DA9055_LDO_MODE_SYNC;
160 case REGULATOR_MODE_STANDBY:
161 val = DA9055_LDO_MODE_SLEEP;
165 return da9055_reg_update(regulator->da9055, volt.reg_b,
167 val << volt.sl_shift);
170 static int da9055_buck_get_current_limit(struct regulator_dev *rdev)
172 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
173 struct da9055_regulator_info *info = regulator->info;
176 ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM);
180 ret &= info->mode.mask;
181 return da9055_current_limits[ret >> info->mode.shift];
184 static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA,
187 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
188 struct da9055_regulator_info *info = regulator->info;
191 for (i = ARRAY_SIZE(da9055_current_limits) - 1; i >= 0; i--) {
192 if ((min_uA <= da9055_current_limits[i]) &&
193 (da9055_current_limits[i] <= max_uA))
194 return da9055_reg_update(regulator->da9055,
197 i << info->mode.shift);
203 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
205 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
206 struct da9055_regulator_info *info = regulator->info;
207 struct da9055_volt_reg volt = info->volt;
211 * There are two voltage register set A & B for voltage ramping but
212 * either one of then can be active therefore we first determine
213 * the active register set.
215 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
219 ret &= info->conf.sel_mask;
221 /* Get the voltage for the active register set A/B */
222 if (ret == DA9055_REGUALTOR_SET_A)
223 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
225 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
230 sel = (ret & volt.v_mask);
234 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
235 unsigned int selector)
237 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
238 struct da9055_regulator_info *info = regulator->info;
242 * Regulator register set A/B is not selected through GPIO therefore
243 * we use default register set A for voltage ramping.
245 if (regulator->reg_rselect == NO_GPIO) {
246 /* Select register set A */
247 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
248 info->conf.sel_mask, DA9055_SEL_REG_A);
252 /* Set the voltage */
253 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
254 info->volt.v_mask, selector);
258 * Here regulator register set A/B is selected through GPIO.
259 * Therefore we first determine the selected register set A/B and
260 * then set the desired voltage for that register set A/B.
262 ret = da9055_reg_read(regulator->da9055, info->conf.reg);
266 ret &= info->conf.sel_mask;
268 /* Set the voltage */
269 if (ret == DA9055_REGUALTOR_SET_A)
270 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
271 info->volt.v_mask, selector);
273 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
274 info->volt.v_mask, selector);
277 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
280 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
281 struct da9055_regulator_info *info = regulator->info;
284 /* Select register set B for suspend voltage ramping. */
285 if (regulator->reg_rselect == NO_GPIO) {
286 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
287 info->conf.sel_mask, DA9055_SEL_REG_B);
292 ret = regulator_map_voltage_linear(rdev, uV, uV);
296 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
297 info->volt.v_mask, ret);
300 static int da9055_suspend_enable(struct regulator_dev *rdev)
302 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
303 struct da9055_regulator_info *info = regulator->info;
305 /* Select register set B for voltage ramping. */
306 if (regulator->reg_rselect == NO_GPIO)
307 return da9055_reg_update(regulator->da9055, info->conf.reg,
308 info->conf.sel_mask, DA9055_SEL_REG_B);
313 static int da9055_suspend_disable(struct regulator_dev *rdev)
315 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
316 struct da9055_regulator_info *info = regulator->info;
318 /* Diselect register set B. */
319 if (regulator->reg_rselect == NO_GPIO)
320 return da9055_reg_update(regulator->da9055, info->conf.reg,
321 info->conf.sel_mask, DA9055_SEL_REG_A);
326 static struct regulator_ops da9055_buck_ops = {
327 .get_mode = da9055_buck_get_mode,
328 .set_mode = da9055_buck_set_mode,
330 .get_current_limit = da9055_buck_get_current_limit,
331 .set_current_limit = da9055_buck_set_current_limit,
333 .get_voltage_sel = da9055_regulator_get_voltage_sel,
334 .set_voltage_sel = da9055_regulator_set_voltage_sel,
335 .list_voltage = regulator_list_voltage_linear,
336 .map_voltage = regulator_map_voltage_linear,
337 .is_enabled = regulator_is_enabled_regmap,
338 .enable = regulator_enable_regmap,
339 .disable = regulator_disable_regmap,
341 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
342 .set_suspend_enable = da9055_suspend_enable,
343 .set_suspend_disable = da9055_suspend_disable,
344 .set_suspend_mode = da9055_buck_set_mode,
347 static struct regulator_ops da9055_ldo_ops = {
348 .get_mode = da9055_ldo_get_mode,
349 .set_mode = da9055_ldo_set_mode,
351 .get_voltage_sel = da9055_regulator_get_voltage_sel,
352 .set_voltage_sel = da9055_regulator_set_voltage_sel,
353 .list_voltage = regulator_list_voltage_linear,
354 .map_voltage = regulator_map_voltage_linear,
355 .is_enabled = regulator_is_enabled_regmap,
356 .enable = regulator_enable_regmap,
357 .disable = regulator_disable_regmap,
359 .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
360 .set_suspend_enable = da9055_suspend_enable,
361 .set_suspend_disable = da9055_suspend_disable,
362 .set_suspend_mode = da9055_ldo_set_mode,
366 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
370 .ops = &da9055_ldo_ops,\
371 .type = REGULATOR_VOLTAGE,\
372 .id = DA9055_ID_##_id,\
373 .n_voltages = (max - min) / step + 1 + (voffset), \
374 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
376 .min_uV = (min) * 1000,\
377 .uV_step = (step) * 1000,\
378 .linear_min_sel = (voffset),\
379 .owner = THIS_MODULE,\
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,\
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,\
423 .reg = DA9055_REG_BCORE_MODE,\
429 static struct da9055_regulator_info da9055_regulator_info[] = {
430 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
431 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
432 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
433 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
434 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
435 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
436 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
437 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
441 * Configures regulator to be controlled either through GPIO 1 or 2.
442 * GPIO can control regulator state and/or select the regulator register
443 * set A/B for voltage ramping.
445 static __devinit int da9055_gpio_init(struct da9055_regulator *regulator,
446 struct regulator_config *config,
447 struct da9055_pdata *pdata, int id)
449 struct da9055_regulator_info *info = regulator->info;
452 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
454 int gpio_mux = pdata->gpio_ren[id];
456 config->ena_gpio = pdata->ena_gpio[id];
457 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
458 config->ena_gpio_invert = 1;
461 * GPI pin is muxed with regulator to control the
464 sprintf(name, "DA9055 GPI %d", gpio_mux);
465 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
471 * Let the regulator know that its state is controlled
474 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
477 << DA9055_E_GPI_SHIFT);
482 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
484 int gpio_mux = pdata->gpio_rsel[id];
486 regulator->reg_rselect = pdata->reg_rsel[id];
489 * GPI pin is muxed with regulator to select the
490 * regulator register set A/B for voltage ramping.
492 sprintf(name, "DA9055 GPI %d", gpio_mux);
493 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
499 * Let the regulator know that its register set A/B
500 * will be selected through GPI for voltage ramping.
502 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
505 << DA9055_V_GPI_SHIFT);
512 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
514 struct da9055_regulator *regulator = data;
516 regulator_notifier_call_chain(regulator->rdev,
517 REGULATOR_EVENT_OVER_CURRENT, NULL);
522 static inline struct da9055_regulator_info *find_regulator_info(int id)
524 struct da9055_regulator_info *info;
527 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
528 info = &da9055_regulator_info[i];
529 if (info->reg_desc.id == id)
536 static int __devinit da9055_regulator_probe(struct platform_device *pdev)
538 struct regulator_config config = { };
539 struct da9055_regulator *regulator;
540 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
541 struct da9055_pdata *pdata = da9055->dev->platform_data;
544 if (pdata == NULL || pdata->regulators[pdev->id] == NULL)
547 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
552 regulator->info = find_regulator_info(pdev->id);
553 if (regulator->info == NULL) {
554 dev_err(&pdev->dev, "invalid regulator ID specified\n");
558 regulator->da9055 = da9055;
559 config.dev = &pdev->dev;
560 config.driver_data = regulator;
561 config.regmap = da9055->regmap;
563 if (pdata && pdata->regulators)
564 config.init_data = pdata->regulators[pdev->id];
566 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
570 regulator->rdev = regulator_register(®ulator->info->reg_desc,
572 if (IS_ERR(regulator->rdev)) {
573 dev_err(&pdev->dev, "Failed to register regulator %s\n",
574 regulator->info->reg_desc.name);
575 ret = PTR_ERR(regulator->rdev);
579 /* Only LDO 5 and 6 has got the over current interrupt */
580 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) {
581 irq = platform_get_irq_byname(pdev, "REGULATOR");
582 irq = regmap_irq_get_virq(da9055->irq_data, irq);
583 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
584 da9055_ldo5_6_oc_irq,
588 pdev->name, regulator);
592 "Failed to request Regulator IRQ %d: %d\n",
599 platform_set_drvdata(pdev, regulator);
604 regulator_unregister(regulator->rdev);
608 static int __devexit da9055_regulator_remove(struct platform_device *pdev)
610 struct da9055_regulator *regulator = platform_get_drvdata(pdev);
612 regulator_unregister(regulator->rdev);
617 static struct platform_driver da9055_regulator_driver = {
618 .probe = da9055_regulator_probe,
619 .remove = __devexit_p(da9055_regulator_remove),
621 .name = "da9055-regulator",
622 .owner = THIS_MODULE,
626 static int __init da9055_regulator_init(void)
628 return platform_driver_register(&da9055_regulator_driver);
630 subsys_initcall(da9055_regulator_init);
632 static void __exit da9055_regulator_exit(void)
634 platform_driver_unregister(&da9055_regulator_driver);
636 module_exit(da9055_regulator_exit);
638 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
639 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
640 MODULE_LICENSE("GPL");
641 MODULE_ALIAS("platform:da9055-regulator");