Linux 5.1.15
[linux/fpc-iii.git] / drivers / regulator / da9055-regulator.c
blob3ade4b8d204ebfa4f0d04e31c4f91f810f348e45
1 /*
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>
23 #include <linux/of.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 {
56 int reg;
57 int sel_mask;
58 int en_mask;
61 struct da9055_volt_reg {
62 int reg_a;
63 int reg_b;
64 int sl_shift;
65 int v_mask;
68 struct da9055_mode_reg {
69 int reg;
70 int mask;
71 int shift;
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;
92 int ret, mode = 0;
94 ret = da9055_reg_read(regulator->da9055, info->mode.reg);
95 if (ret < 0)
96 return ret;
98 switch ((ret & info->mode.mask) >> info->mode.shift) {
99 case DA9055_BUCK_MODE_SYNC:
100 mode = REGULATOR_MODE_FAST;
101 break;
102 case DA9055_BUCK_MODE_AUTO:
103 mode = REGULATOR_MODE_NORMAL;
104 break;
105 case DA9055_BUCK_MODE_SLEEP:
106 mode = REGULATOR_MODE_STANDBY;
107 break;
110 return mode;
113 static int da9055_buck_set_mode(struct regulator_dev *rdev,
114 unsigned int mode)
116 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
117 struct da9055_regulator_info *info = regulator->info;
118 int val = 0;
120 switch (mode) {
121 case REGULATOR_MODE_FAST:
122 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
123 break;
124 case REGULATOR_MODE_NORMAL:
125 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
126 break;
127 case REGULATOR_MODE_STANDBY:
128 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
129 break;
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;
140 int ret;
142 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
143 if (ret < 0)
144 return ret;
146 if (ret >> info->volt.sl_shift)
147 return REGULATOR_MODE_STANDBY;
148 else
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;
157 int val = 0;
159 switch (mode) {
160 case REGULATOR_MODE_NORMAL:
161 case REGULATOR_MODE_FAST:
162 val = DA9055_LDO_MODE_SYNC;
163 break;
164 case REGULATOR_MODE_STANDBY:
165 val = DA9055_LDO_MODE_SLEEP;
166 break;
169 return da9055_reg_update(regulator->da9055, volt.reg_b,
170 1 << volt.sl_shift,
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;
179 int ret, sel;
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);
187 if (ret < 0)
188 return ret;
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);
195 else
196 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
198 if (ret < 0)
199 return ret;
201 sel = (ret & volt.v_mask);
202 return sel;
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;
210 int ret;
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);
220 if (ret < 0)
221 return ret;
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);
234 if (ret < 0)
235 return ret;
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);
243 else
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,
249 int uV)
251 struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
252 struct da9055_regulator_info *info = regulator->info;
253 int ret;
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);
259 if (ret < 0)
260 return ret;
263 ret = regulator_map_voltage_linear(rdev, uV, uV);
264 if (ret < 0)
265 return ret;
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);
280 else
281 return 0;
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);
293 else
294 return 0;
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) \
339 .reg_desc = {\
340 .name = #_id,\
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, \
346 .enable_mask = 1, \
347 .min_uV = (min) * 1000,\
348 .uV_step = (step) * 1000,\
349 .linear_min_sel = (voffset),\
350 .owner = THIS_MODULE,\
352 .conf = {\
353 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
354 .sel_mask = (1 << 4),\
355 .en_mask = 1,\
357 .volt = {\
358 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
359 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
360 .sl_shift = 7,\
361 .v_mask = (1 << (vbits)) - 1,\
365 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
367 .reg_desc = {\
368 .name = #_id,\
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, \
374 .enable_mask = 1,\
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),\
384 .conf = {\
385 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
386 .sel_mask = (1 << 4),\
387 .en_mask = 1,\
389 .volt = {\
390 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
391 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
392 .sl_shift = 7,\
393 .v_mask = (1 << (vbits)) - 1,\
395 .mode = {\
396 .reg = DA9055_REG_BCORE_MODE,\
397 .mask = (mbits),\
398 .shift = (sbits),\
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;
423 int ret = 0;
425 if (!pdata)
426 return 0;
428 if (pdata->gpio_ren && pdata->gpio_ren[id]) {
429 char name[18];
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
436 * regulator state.
438 sprintf(name, "DA9055 GPI %d", gpio_mux);
439 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
440 name);
441 if (ret < 0)
442 goto err;
445 * Let the regulator know that its state is controlled
446 * through GPI.
448 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
449 DA9055_E_GPI_MASK,
450 pdata->reg_ren[id]
451 << DA9055_E_GPI_SHIFT);
452 if (ret < 0)
453 goto err;
456 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
457 char name[18];
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,
468 name);
469 if (ret < 0)
470 goto err;
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,
477 DA9055_V_GPI_MASK,
478 pdata->reg_rsel[id]
479 << DA9055_V_GPI_SHIFT);
482 err:
483 return ret;
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);
495 return IRQ_HANDLED;
498 static inline struct da9055_regulator_info *find_regulator_info(int id)
500 struct da9055_regulator_info *info;
501 int i;
503 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
504 info = &da9055_regulator_info[i];
505 if (info->reg_desc.id == id)
506 return info;
509 return NULL;
512 #ifdef CONFIG_OF
513 static struct of_regulator_match da9055_reg_matches[] = {
514 { .name = "BUCK1", },
515 { .name = "BUCK2", },
516 { .name = "LDO1", },
517 { .name = "LDO2", },
518 { .name = "LDO3", },
519 { .name = "LDO4", },
520 { .name = "LDO5", },
521 { .name = "LDO6", },
524 static int da9055_regulator_dt_init(struct platform_device *pdev,
525 struct da9055_regulator *regulator,
526 struct regulator_config *config,
527 int regid)
529 struct device_node *nproot, *np;
530 int ret;
532 nproot = of_node_get(pdev->dev.parent->of_node);
533 if (!nproot)
534 return -ENODEV;
536 np = of_get_child_by_name(nproot, "regulators");
537 if (!np)
538 return -ENODEV;
540 ret = of_regulator_match(&pdev->dev, np, &da9055_reg_matches[regid], 1);
541 of_node_put(nproot);
542 if (ret < 0) {
543 dev_err(&pdev->dev, "Error matching regulator: %d\n", ret);
544 return 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)
551 return -ENODEV;
553 return 0;
555 #else
556 static inline int da9055_regulator_dt_init(struct platform_device *pdev,
557 struct da9055_regulator *regulator,
558 struct regulator_config *config,
559 int regid)
561 return -ENODEV;
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);
571 int ret, irq;
573 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
574 GFP_KERNEL);
575 if (!regulator)
576 return -ENOMEM;
578 regulator->info = find_regulator_info(pdev->id);
579 if (regulator->info == NULL) {
580 dev_err(&pdev->dev, "invalid regulator ID specified\n");
581 return -EINVAL;
584 regulator->da9055 = da9055;
585 config.dev = &pdev->dev;
586 config.driver_data = regulator;
587 config.regmap = da9055->regmap;
589 if (pdata) {
590 config.init_data = pdata->regulators[pdev->id];
591 } else {
592 ret = da9055_regulator_dt_init(pdev, regulator, &config,
593 pdev->id);
594 if (ret < 0)
595 return ret;
598 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
599 if (ret < 0)
600 return ret;
602 regulator->rdev = devm_regulator_register(&pdev->dev,
603 &regulator->info->reg_desc,
604 &config);
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");
614 if (irq < 0)
615 return irq;
617 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
618 da9055_ldo5_6_oc_irq,
619 IRQF_TRIGGER_HIGH |
620 IRQF_ONESHOT |
621 IRQF_PROBE_SHARED,
622 pdev->name, regulator);
623 if (ret != 0) {
624 if (ret != -EBUSY) {
625 dev_err(&pdev->dev,
626 "Failed to request Regulator IRQ %d: %d\n",
627 irq, ret);
628 return ret;
633 platform_set_drvdata(pdev, regulator);
635 return 0;
638 static struct platform_driver da9055_regulator_driver = {
639 .probe = da9055_regulator_probe,
640 .driver = {
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");