3 * Regulator driver for DA9063 PMIC series
5 * Copyright 2012 Dialog Semiconductors Ltd.
6 * Copyright 2013 Philipp Zabel, Pengutronix
8 * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/regulator/of_regulator.h>
27 #include <linux/mfd/da9063/core.h>
28 #include <linux/mfd/da9063/pdata.h>
29 #include <linux/mfd/da9063/registers.h>
32 /* Definition for registering regmap bit fields using a mask */
33 #define BFIELD(_reg, _mask) \
34 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
35 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
37 /* Regulator capabilities and registers description */
38 struct da9063_regulator_info {
39 struct regulator_desc desc;
41 /* Current limiting */
42 unsigned n_current_limits;
43 const int *current_limits;
45 /* DA9063 main register fields */
46 struct reg_field mode; /* buck mode of operation */
47 struct reg_field suspend;
48 struct reg_field sleep;
49 struct reg_field suspend_sleep;
50 unsigned int suspend_vsel_reg;
51 struct reg_field ilimit;
53 /* DA9063 event detection bit */
54 struct reg_field oc_event;
58 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
59 .desc.id = chip##_ID_##regl_name, \
60 .desc.name = __stringify(chip##_##regl_name), \
61 .desc.ops = &da9063_ldo_ops, \
62 .desc.min_uV = (min_mV) * 1000, \
63 .desc.uV_step = (step_mV) * 1000, \
64 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
65 + (DA9063_V##regl_name##_BIAS)), \
66 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
67 .desc.enable_mask = DA9063_LDO_EN, \
68 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
69 .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
70 .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
71 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
72 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
73 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
75 /* Macros for voltage DC/DC converters (BUCKs) */
76 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array) \
77 .desc.id = chip##_ID_##regl_name, \
78 .desc.name = __stringify(chip##_##regl_name), \
79 .desc.ops = &da9063_buck_ops, \
80 .desc.min_uV = (min_mV) * 1000, \
81 .desc.uV_step = (step_mV) * 1000, \
82 .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
83 .current_limits = limits_array, \
84 .n_current_limits = ARRAY_SIZE(limits_array)
86 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
87 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
88 .desc.enable_mask = DA9063_BUCK_EN, \
89 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
90 .desc.vsel_mask = DA9063_VBUCK_MASK, \
91 .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
92 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
93 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
94 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
95 .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
97 /* Defines asignment of regulators info table to chip model */
98 struct da9063_dev_model {
99 const struct da9063_regulator_info *regulator_info;
100 unsigned n_regulators;
104 /* Single regulator settings */
105 struct da9063_regulator {
106 struct regulator_desc desc;
107 struct regulator_dev *rdev;
109 const struct da9063_regulator_info *info;
111 struct regmap_field *mode;
112 struct regmap_field *suspend;
113 struct regmap_field *sleep;
114 struct regmap_field *suspend_sleep;
115 struct regmap_field *ilimit;
118 /* Encapsulates all information for the regulators driver */
119 struct da9063_regulators {
123 unsigned n_regulators;
124 /* Array size to be defined during init. Keep at end. */
125 struct da9063_regulator regulator[0];
128 /* BUCK modes for DA9063 */
130 BUCK_MODE_MANUAL, /* 0 */
131 BUCK_MODE_SLEEP, /* 1 */
132 BUCK_MODE_SYNC, /* 2 */
133 BUCK_MODE_AUTO /* 3 */
136 /* Regulator operations */
138 /* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
139 Entry indexes corresponds to register values. */
140 static const int da9063_buck_a_limits[] = {
141 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
142 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
145 /* Current limits array (in uA) for BMEM, BIO, BPERI.
146 Entry indexes corresponds to register values. */
147 static const int da9063_buck_b_limits[] = {
148 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
149 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
152 /* Current limits array (in uA) for merged BCORE1 and BCORE2.
153 Entry indexes corresponds to register values. */
154 static const int da9063_bcores_merged_limits[] = {
155 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
156 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
159 /* Current limits array (in uA) for merged BMEM and BIO.
160 Entry indexes corresponds to register values. */
161 static const int da9063_bmem_bio_merged_limits[] = {
162 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
163 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
166 static int da9063_set_current_limit(struct regulator_dev *rdev,
167 int min_uA, int max_uA)
169 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
170 const struct da9063_regulator_info *rinfo = regl->info;
173 for (n = 0; n < rinfo->n_current_limits; n++) {
174 tval = rinfo->current_limits[n];
175 if (tval >= min_uA && tval <= max_uA)
176 return regmap_field_write(regl->ilimit, n);
182 static int da9063_get_current_limit(struct regulator_dev *rdev)
184 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
185 const struct da9063_regulator_info *rinfo = regl->info;
189 ret = regmap_field_read(regl->ilimit, &sel);
193 if (sel >= rinfo->n_current_limits)
194 sel = rinfo->n_current_limits - 1;
196 return rinfo->current_limits[sel];
199 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
201 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
205 case REGULATOR_MODE_FAST:
206 val = BUCK_MODE_SYNC;
208 case REGULATOR_MODE_NORMAL:
209 val = BUCK_MODE_AUTO;
211 case REGULATOR_MODE_STANDBY:
212 val = BUCK_MODE_SLEEP;
218 return regmap_field_write(regl->mode, val);
222 * Bucks use single mode register field for normal operation
224 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
227 static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
229 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
230 struct regmap_field *field;
231 unsigned int val, mode = 0;
234 ret = regmap_field_read(regl->mode, &val);
240 case BUCK_MODE_MANUAL:
241 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
242 /* Sleep flag bit decides the mode */
244 case BUCK_MODE_SLEEP:
245 return REGULATOR_MODE_STANDBY;
247 return REGULATOR_MODE_FAST;
249 return REGULATOR_MODE_NORMAL;
252 /* Detect current regulator state */
253 ret = regmap_field_read(regl->suspend, &val);
257 /* Read regulator mode from proper register, depending on state */
259 field = regl->suspend_sleep;
263 ret = regmap_field_read(field, &val);
268 mode &= REGULATOR_MODE_STANDBY;
270 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
276 * LDOs use sleep flags - one for normal and one for suspend state.
277 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
280 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
282 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
286 case REGULATOR_MODE_NORMAL:
289 case REGULATOR_MODE_STANDBY:
296 return regmap_field_write(regl->sleep, val);
299 static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
301 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
302 struct regmap_field *field;
305 /* Detect current regulator state */
306 ret = regmap_field_read(regl->suspend, &val);
310 /* Read regulator mode from proper register, depending on state */
312 field = regl->suspend_sleep;
316 ret = regmap_field_read(field, &val);
321 return REGULATOR_MODE_STANDBY;
323 return REGULATOR_MODE_NORMAL;
326 static int da9063_buck_get_status(struct regulator_dev *rdev)
328 int ret = regulator_is_enabled_regmap(rdev);
331 ret = REGULATOR_STATUS_OFF;
332 } else if (ret > 0) {
333 ret = da9063_buck_get_mode(rdev);
335 ret = regulator_mode_to_status(ret);
343 static int da9063_ldo_get_status(struct regulator_dev *rdev)
345 int ret = regulator_is_enabled_regmap(rdev);
348 ret = REGULATOR_STATUS_OFF;
349 } else if (ret > 0) {
350 ret = da9063_ldo_get_mode(rdev);
352 ret = regulator_mode_to_status(ret);
360 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
362 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
363 const struct da9063_regulator_info *rinfo = regl->info;
366 sel = regulator_map_voltage_linear(rdev, uV, uV);
370 sel <<= ffs(rdev->desc->vsel_mask) - 1;
372 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
373 rdev->desc->vsel_mask, sel);
378 static int da9063_suspend_enable(struct regulator_dev *rdev)
380 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
382 return regmap_field_write(regl->suspend, 1);
385 static int da9063_suspend_disable(struct regulator_dev *rdev)
387 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
389 return regmap_field_write(regl->suspend, 0);
392 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
394 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
398 case REGULATOR_MODE_FAST:
399 val = BUCK_MODE_SYNC;
401 case REGULATOR_MODE_NORMAL:
402 val = BUCK_MODE_AUTO;
404 case REGULATOR_MODE_STANDBY:
405 val = BUCK_MODE_SLEEP;
411 return regmap_field_write(regl->mode, val);
414 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
416 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
420 case REGULATOR_MODE_NORMAL:
423 case REGULATOR_MODE_STANDBY:
430 return regmap_field_write(regl->suspend_sleep, val);
433 static struct regulator_ops da9063_buck_ops = {
434 .enable = regulator_enable_regmap,
435 .disable = regulator_disable_regmap,
436 .is_enabled = regulator_is_enabled_regmap,
437 .get_voltage_sel = regulator_get_voltage_sel_regmap,
438 .set_voltage_sel = regulator_set_voltage_sel_regmap,
439 .list_voltage = regulator_list_voltage_linear,
440 .set_current_limit = da9063_set_current_limit,
441 .get_current_limit = da9063_get_current_limit,
442 .set_mode = da9063_buck_set_mode,
443 .get_mode = da9063_buck_get_mode,
444 .get_status = da9063_buck_get_status,
445 .set_suspend_voltage = da9063_set_suspend_voltage,
446 .set_suspend_enable = da9063_suspend_enable,
447 .set_suspend_disable = da9063_suspend_disable,
448 .set_suspend_mode = da9063_buck_set_suspend_mode,
451 static struct regulator_ops da9063_ldo_ops = {
452 .enable = regulator_enable_regmap,
453 .disable = regulator_disable_regmap,
454 .is_enabled = regulator_is_enabled_regmap,
455 .get_voltage_sel = regulator_get_voltage_sel_regmap,
456 .set_voltage_sel = regulator_set_voltage_sel_regmap,
457 .list_voltage = regulator_list_voltage_linear,
458 .set_mode = da9063_ldo_set_mode,
459 .get_mode = da9063_ldo_get_mode,
460 .get_status = da9063_ldo_get_status,
461 .set_suspend_voltage = da9063_set_suspend_voltage,
462 .set_suspend_enable = da9063_suspend_enable,
463 .set_suspend_disable = da9063_suspend_disable,
464 .set_suspend_mode = da9063_ldo_set_suspend_mode,
467 /* Info of regulators for DA9063 */
468 static const struct da9063_regulator_info da9063_regulator_info[] = {
470 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
471 da9063_buck_a_limits),
472 DA9063_BUCK_COMMON_FIELDS(BCORE1),
473 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
474 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
475 DA9063_BCORE1_ILIM_MASK),
478 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
479 da9063_buck_a_limits),
480 DA9063_BUCK_COMMON_FIELDS(BCORE2),
481 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
482 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
483 DA9063_BCORE2_ILIM_MASK),
486 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
487 da9063_buck_a_limits),
488 DA9063_BUCK_COMMON_FIELDS(BPRO),
489 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
490 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
491 DA9063_BPRO_ILIM_MASK),
494 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
495 da9063_buck_b_limits),
496 DA9063_BUCK_COMMON_FIELDS(BMEM),
497 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
498 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
499 DA9063_BMEM_ILIM_MASK),
502 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
503 da9063_buck_b_limits),
504 DA9063_BUCK_COMMON_FIELDS(BIO),
505 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
506 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
507 DA9063_BIO_ILIM_MASK),
510 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
511 da9063_buck_b_limits),
512 DA9063_BUCK_COMMON_FIELDS(BPERI),
513 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
514 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
515 DA9063_BPERI_ILIM_MASK),
518 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
519 da9063_bcores_merged_limits),
520 /* BCORES_MERGED uses the same register fields as BCORE1 */
521 DA9063_BUCK_COMMON_FIELDS(BCORE1),
522 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
523 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
524 DA9063_BCORE1_ILIM_MASK),
527 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
528 da9063_bmem_bio_merged_limits),
529 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
530 DA9063_BUCK_COMMON_FIELDS(BMEM),
531 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
532 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
533 DA9063_BMEM_ILIM_MASK),
536 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
537 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
540 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
541 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
544 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
545 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
546 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
549 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
550 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
551 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
554 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
555 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
558 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
559 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
562 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
563 .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
564 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
567 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
568 .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
569 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
572 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
573 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
576 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
577 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
580 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
581 .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
582 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
586 /* Link chip model with regulators info table */
587 static struct da9063_dev_model regulators_models[] = {
589 .regulator_info = da9063_regulator_info,
590 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
591 .dev_model = PMIC_DA9063,
596 /* Regulator interrupt handlers */
597 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
599 struct da9063_regulators *regulators = data;
600 struct da9063 *hw = regulators->regulator[0].hw;
601 struct da9063_regulator *regl;
604 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
608 for (i = regulators->n_regulators - 1; i >= 0; i--) {
609 regl = ®ulators->regulator[i];
610 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
613 if (BIT(regl->info->oc_event.lsb) & bits)
614 regulator_notifier_call_chain(regl->rdev,
615 REGULATOR_EVENT_OVER_CURRENT, NULL);
622 * Probing and Initialisation functions
624 static const struct regulator_init_data *da9063_get_regulator_initdata(
625 const struct da9063_regulators_pdata *regl_pdata, int id)
629 for (i = 0; i < regl_pdata->n_regulators; i++) {
630 if (id == regl_pdata->regulator_data[i].id)
631 return regl_pdata->regulator_data[i].initdata;
638 static struct of_regulator_match da9063_matches[] = {
639 [DA9063_ID_BCORE1] = { .name = "bcore1" },
640 [DA9063_ID_BCORE2] = { .name = "bcore2" },
641 [DA9063_ID_BPRO] = { .name = "bpro", },
642 [DA9063_ID_BMEM] = { .name = "bmem", },
643 [DA9063_ID_BIO] = { .name = "bio", },
644 [DA9063_ID_BPERI] = { .name = "bperi", },
645 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
646 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
647 [DA9063_ID_LDO1] = { .name = "ldo1", },
648 [DA9063_ID_LDO2] = { .name = "ldo2", },
649 [DA9063_ID_LDO3] = { .name = "ldo3", },
650 [DA9063_ID_LDO4] = { .name = "ldo4", },
651 [DA9063_ID_LDO5] = { .name = "ldo5", },
652 [DA9063_ID_LDO6] = { .name = "ldo6", },
653 [DA9063_ID_LDO7] = { .name = "ldo7", },
654 [DA9063_ID_LDO8] = { .name = "ldo8", },
655 [DA9063_ID_LDO9] = { .name = "ldo9", },
656 [DA9063_ID_LDO10] = { .name = "ldo10", },
657 [DA9063_ID_LDO11] = { .name = "ldo11", },
660 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
661 struct platform_device *pdev,
662 struct of_regulator_match **da9063_reg_matches)
664 struct da9063_regulators_pdata *pdata;
665 struct da9063_regulator_data *rdata;
666 struct device_node *node;
669 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
671 dev_err(&pdev->dev, "Regulators device node not found\n");
672 return ERR_PTR(-ENODEV);
675 num = of_regulator_match(&pdev->dev, node, da9063_matches,
676 ARRAY_SIZE(da9063_matches));
679 dev_err(&pdev->dev, "Failed to match regulators\n");
680 return ERR_PTR(-EINVAL);
683 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
685 return ERR_PTR(-ENOMEM);
687 pdata->regulator_data = devm_kzalloc(&pdev->dev,
688 num * sizeof(*pdata->regulator_data),
690 if (!pdata->regulator_data)
691 return ERR_PTR(-ENOMEM);
692 pdata->n_regulators = num;
695 for (i = 0; i < ARRAY_SIZE(da9063_matches); i++) {
696 if (!da9063_matches[i].init_data)
699 rdata = &pdata->regulator_data[n];
701 rdata->initdata = da9063_matches[i].init_data;
706 *da9063_reg_matches = da9063_matches;
710 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
711 struct platform_device *pdev,
712 struct of_regulator_match **da9063_reg_matches)
714 *da9063_reg_matches = NULL;
715 return ERR_PTR(-ENODEV);
719 static int da9063_regulator_probe(struct platform_device *pdev)
721 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
722 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
723 struct of_regulator_match *da9063_reg_matches = NULL;
724 struct da9063_regulators_pdata *regl_pdata;
725 const struct da9063_dev_model *model;
726 struct da9063_regulators *regulators;
727 struct da9063_regulator *regl;
728 struct regulator_config config;
729 bool bcores_merged, bmem_bio_merged;
730 int id, irq, n, n_regulators, ret, val;
733 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
736 regl_pdata = da9063_parse_regulators_dt(pdev,
737 &da9063_reg_matches);
739 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
741 "No regulators defined for the platform\n");
742 return PTR_ERR(regl_pdata);
745 /* Find regulators set for particular device model */
746 for (model = regulators_models; model->regulator_info; model++) {
747 if (model->dev_model == da9063->model)
750 if (!model->regulator_info) {
751 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
756 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
759 "Error while reading BUCKs configuration\n");
762 bcores_merged = val & DA9063_BCORE_MERGE;
763 bmem_bio_merged = val & DA9063_BUCK_MERGE;
765 n_regulators = model->n_regulators;
767 n_regulators -= 2; /* remove BCORE1, BCORE2 */
769 n_regulators--; /* remove BCORES_MERGED */
771 n_regulators -= 2; /* remove BMEM, BIO */
773 n_regulators--; /* remove BMEM_BIO_MERGED */
775 /* Allocate memory required by usable regulators */
776 size = sizeof(struct da9063_regulators) +
777 n_regulators * sizeof(struct da9063_regulator);
778 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
782 regulators->n_regulators = n_regulators;
783 platform_set_drvdata(pdev, regulators);
785 /* Register all regulators declared in platform information */
788 while (n < regulators->n_regulators) {
789 /* Skip regulator IDs depending on merge mode configuration */
791 case DA9063_ID_BCORE1:
792 case DA9063_ID_BCORE2:
800 if (bmem_bio_merged) {
805 case DA9063_ID_BCORES_MERGED:
806 if (!bcores_merged) {
811 case DA9063_ID_BMEM_BIO_MERGED:
812 if (!bmem_bio_merged) {
819 /* Initialise regulator structure */
820 regl = ®ulators->regulator[n];
822 regl->info = &model->regulator_info[id];
823 regl->desc = regl->info->desc;
824 regl->desc.type = REGULATOR_VOLTAGE;
825 regl->desc.owner = THIS_MODULE;
827 if (regl->info->mode.reg)
828 regl->mode = devm_regmap_field_alloc(&pdev->dev,
829 da9063->regmap, regl->info->mode);
830 if (regl->info->suspend.reg)
831 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
832 da9063->regmap, regl->info->suspend);
833 if (regl->info->sleep.reg)
834 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
835 da9063->regmap, regl->info->sleep);
836 if (regl->info->suspend_sleep.reg)
837 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
838 da9063->regmap, regl->info->suspend_sleep);
839 if (regl->info->ilimit.reg)
840 regl->ilimit = devm_regmap_field_alloc(&pdev->dev,
841 da9063->regmap, regl->info->ilimit);
843 /* Register regulator */
844 memset(&config, 0, sizeof(config));
845 config.dev = &pdev->dev;
846 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
847 config.driver_data = regl;
848 if (da9063_reg_matches)
849 config.of_node = da9063_reg_matches[id].of_node;
850 config.regmap = da9063->regmap;
851 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc,
853 if (IS_ERR(regl->rdev)) {
855 "Failed to register %s regulator\n",
857 return PTR_ERR(regl->rdev);
863 /* LDOs overcurrent event support */
864 irq = platform_get_irq_byname(pdev, "LDO_LIM");
866 dev_err(&pdev->dev, "Failed to get IRQ.\n");
870 ret = request_threaded_irq(irq,
871 NULL, da9063_ldo_lim_event,
872 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
873 "LDO_LIM", regulators);
876 "Failed to request LDO_LIM IRQ.\n");
877 regulators->irq_ldo_lim = -ENXIO;
883 static int da9063_regulator_remove(struct platform_device *pdev)
885 struct da9063_regulators *regulators = platform_get_drvdata(pdev);
887 free_irq(regulators->irq_ldo_lim, regulators);
888 free_irq(regulators->irq_uvov, regulators);
893 static struct platform_driver da9063_regulator_driver = {
895 .name = DA9063_DRVNAME_REGULATORS,
897 .probe = da9063_regulator_probe,
898 .remove = da9063_regulator_remove,
901 static int __init da9063_regulator_init(void)
903 return platform_driver_register(&da9063_regulator_driver);
905 subsys_initcall(da9063_regulator_init);
907 static void __exit da9063_regulator_cleanup(void)
909 platform_driver_unregister(&da9063_regulator_driver);
911 module_exit(da9063_regulator_cleanup);
914 /* Module information */
915 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
916 MODULE_DESCRIPTION("DA9063 regulators driver");
917 MODULE_LICENSE("GPL");
918 MODULE_ALIAS("paltform:" DA9063_DRVNAME_REGULATORS);