1 // SPDX-License-Identifier: GPL-2.0+
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 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/of_regulator.h>
21 #include <linux/mfd/da9063/core.h>
22 #include <linux/mfd/da9063/pdata.h>
23 #include <linux/mfd/da9063/registers.h>
26 /* Definition for registering regmap bit fields using a mask */
27 #define BFIELD(_reg, _mask) \
28 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
29 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
31 /* Regulator capabilities and registers description */
32 struct da9063_regulator_info {
33 struct regulator_desc desc;
35 /* DA9063 main register fields */
36 struct reg_field mode; /* buck mode of operation */
37 struct reg_field suspend;
38 struct reg_field sleep;
39 struct reg_field suspend_sleep;
40 unsigned int suspend_vsel_reg;
42 /* DA9063 event detection bit */
43 struct reg_field oc_event;
47 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
48 .desc.id = chip##_ID_##regl_name, \
49 .desc.name = __stringify(chip##_##regl_name), \
50 .desc.ops = &da9063_ldo_ops, \
51 .desc.min_uV = (min_mV) * 1000, \
52 .desc.uV_step = (step_mV) * 1000, \
53 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
54 + (DA9063_V##regl_name##_BIAS)), \
55 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
56 .desc.enable_mask = DA9063_LDO_EN, \
57 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
58 .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
59 .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
60 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
61 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
62 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
64 /* Macros for voltage DC/DC converters (BUCKs) */
65 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
67 .desc.id = chip##_ID_##regl_name, \
68 .desc.name = __stringify(chip##_##regl_name), \
69 .desc.ops = &da9063_buck_ops, \
70 .desc.min_uV = (min_mV) * 1000, \
71 .desc.uV_step = (step_mV) * 1000, \
72 .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
73 .desc.csel_reg = (creg), \
74 .desc.csel_mask = (cmask), \
75 .desc.curr_table = limits_array, \
76 .desc.n_current_limits = ARRAY_SIZE(limits_array)
78 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
79 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
80 .desc.enable_mask = DA9063_BUCK_EN, \
81 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
82 .desc.vsel_mask = DA9063_VBUCK_MASK, \
83 .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
84 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
85 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
86 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
87 .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
89 /* Defines asignment of regulators info table to chip model */
90 struct da9063_dev_model {
91 const struct da9063_regulator_info *regulator_info;
92 unsigned n_regulators;
93 enum da9063_type type;
96 /* Single regulator settings */
97 struct da9063_regulator {
98 struct regulator_desc desc;
99 struct regulator_dev *rdev;
101 const struct da9063_regulator_info *info;
103 struct regmap_field *mode;
104 struct regmap_field *suspend;
105 struct regmap_field *sleep;
106 struct regmap_field *suspend_sleep;
109 /* Encapsulates all information for the regulators driver */
110 struct da9063_regulators {
111 unsigned n_regulators;
112 /* Array size to be defined during init. Keep at end. */
113 struct da9063_regulator regulator[0];
116 /* BUCK modes for DA9063 */
118 BUCK_MODE_MANUAL, /* 0 */
119 BUCK_MODE_SLEEP, /* 1 */
120 BUCK_MODE_SYNC, /* 2 */
121 BUCK_MODE_AUTO /* 3 */
124 /* Regulator operations */
126 /* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
127 Entry indexes corresponds to register values. */
128 static const unsigned int da9063_buck_a_limits[] = {
129 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
130 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
133 /* Current limits array (in uA) for BMEM, BIO, BPERI.
134 Entry indexes corresponds to register values. */
135 static const unsigned int da9063_buck_b_limits[] = {
136 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
137 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
140 /* Current limits array (in uA) for merged BCORE1 and BCORE2.
141 Entry indexes corresponds to register values. */
142 static const unsigned int da9063_bcores_merged_limits[] = {
143 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
144 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
147 /* Current limits array (in uA) for merged BMEM and BIO.
148 Entry indexes corresponds to register values. */
149 static const unsigned int da9063_bmem_bio_merged_limits[] = {
150 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
151 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
154 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
156 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
160 case REGULATOR_MODE_FAST:
161 val = BUCK_MODE_SYNC;
163 case REGULATOR_MODE_NORMAL:
164 val = BUCK_MODE_AUTO;
166 case REGULATOR_MODE_STANDBY:
167 val = BUCK_MODE_SLEEP;
173 return regmap_field_write(regl->mode, val);
177 * Bucks use single mode register field for normal operation
179 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
182 static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
184 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
185 struct regmap_field *field;
186 unsigned int val, mode = 0;
189 ret = regmap_field_read(regl->mode, &val);
195 case BUCK_MODE_MANUAL:
196 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
197 /* Sleep flag bit decides the mode */
199 case BUCK_MODE_SLEEP:
200 return REGULATOR_MODE_STANDBY;
202 return REGULATOR_MODE_FAST;
204 return REGULATOR_MODE_NORMAL;
207 /* Detect current regulator state */
208 ret = regmap_field_read(regl->suspend, &val);
212 /* Read regulator mode from proper register, depending on state */
214 field = regl->suspend_sleep;
218 ret = regmap_field_read(field, &val);
223 mode &= REGULATOR_MODE_STANDBY;
225 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
231 * LDOs use sleep flags - one for normal and one for suspend state.
232 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
235 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
237 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
241 case REGULATOR_MODE_NORMAL:
244 case REGULATOR_MODE_STANDBY:
251 return regmap_field_write(regl->sleep, val);
254 static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
256 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
257 struct regmap_field *field;
260 /* Detect current regulator state */
261 ret = regmap_field_read(regl->suspend, &val);
265 /* Read regulator mode from proper register, depending on state */
267 field = regl->suspend_sleep;
271 ret = regmap_field_read(field, &val);
276 return REGULATOR_MODE_STANDBY;
278 return REGULATOR_MODE_NORMAL;
281 static int da9063_buck_get_status(struct regulator_dev *rdev)
283 int ret = regulator_is_enabled_regmap(rdev);
286 ret = REGULATOR_STATUS_OFF;
287 } else if (ret > 0) {
288 ret = da9063_buck_get_mode(rdev);
290 ret = regulator_mode_to_status(ret);
298 static int da9063_ldo_get_status(struct regulator_dev *rdev)
300 int ret = regulator_is_enabled_regmap(rdev);
303 ret = REGULATOR_STATUS_OFF;
304 } else if (ret > 0) {
305 ret = da9063_ldo_get_mode(rdev);
307 ret = regulator_mode_to_status(ret);
315 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
317 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
318 const struct da9063_regulator_info *rinfo = regl->info;
321 sel = regulator_map_voltage_linear(rdev, uV, uV);
325 sel <<= ffs(rdev->desc->vsel_mask) - 1;
327 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
328 rdev->desc->vsel_mask, sel);
333 static int da9063_suspend_enable(struct regulator_dev *rdev)
335 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
337 return regmap_field_write(regl->suspend, 1);
340 static int da9063_suspend_disable(struct regulator_dev *rdev)
342 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
344 return regmap_field_write(regl->suspend, 0);
347 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
349 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
353 case REGULATOR_MODE_FAST:
354 val = BUCK_MODE_SYNC;
356 case REGULATOR_MODE_NORMAL:
357 val = BUCK_MODE_AUTO;
359 case REGULATOR_MODE_STANDBY:
360 val = BUCK_MODE_SLEEP;
366 return regmap_field_write(regl->mode, val);
369 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
371 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
375 case REGULATOR_MODE_NORMAL:
378 case REGULATOR_MODE_STANDBY:
385 return regmap_field_write(regl->suspend_sleep, val);
388 static const struct regulator_ops da9063_buck_ops = {
389 .enable = regulator_enable_regmap,
390 .disable = regulator_disable_regmap,
391 .is_enabled = regulator_is_enabled_regmap,
392 .get_voltage_sel = regulator_get_voltage_sel_regmap,
393 .set_voltage_sel = regulator_set_voltage_sel_regmap,
394 .list_voltage = regulator_list_voltage_linear,
395 .set_current_limit = regulator_set_current_limit_regmap,
396 .get_current_limit = regulator_get_current_limit_regmap,
397 .set_mode = da9063_buck_set_mode,
398 .get_mode = da9063_buck_get_mode,
399 .get_status = da9063_buck_get_status,
400 .set_suspend_voltage = da9063_set_suspend_voltage,
401 .set_suspend_enable = da9063_suspend_enable,
402 .set_suspend_disable = da9063_suspend_disable,
403 .set_suspend_mode = da9063_buck_set_suspend_mode,
406 static const struct regulator_ops da9063_ldo_ops = {
407 .enable = regulator_enable_regmap,
408 .disable = regulator_disable_regmap,
409 .is_enabled = regulator_is_enabled_regmap,
410 .get_voltage_sel = regulator_get_voltage_sel_regmap,
411 .set_voltage_sel = regulator_set_voltage_sel_regmap,
412 .list_voltage = regulator_list_voltage_linear,
413 .set_mode = da9063_ldo_set_mode,
414 .get_mode = da9063_ldo_get_mode,
415 .get_status = da9063_ldo_get_status,
416 .set_suspend_voltage = da9063_set_suspend_voltage,
417 .set_suspend_enable = da9063_suspend_enable,
418 .set_suspend_disable = da9063_suspend_disable,
419 .set_suspend_mode = da9063_ldo_set_suspend_mode,
422 /* Info of regulators for DA9063 */
423 static const struct da9063_regulator_info da9063_regulator_info[] = {
425 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
426 da9063_buck_a_limits,
427 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
428 DA9063_BUCK_COMMON_FIELDS(BCORE1),
429 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
432 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
433 da9063_buck_a_limits,
434 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
435 DA9063_BUCK_COMMON_FIELDS(BCORE2),
436 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
439 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
440 da9063_buck_a_limits,
441 DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
442 DA9063_BUCK_COMMON_FIELDS(BPRO),
443 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
446 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
447 da9063_buck_b_limits,
448 DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
449 DA9063_BUCK_COMMON_FIELDS(BMEM),
450 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
453 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
454 da9063_buck_b_limits,
455 DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
456 DA9063_BUCK_COMMON_FIELDS(BIO),
457 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
460 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
461 da9063_buck_b_limits,
462 DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
463 DA9063_BUCK_COMMON_FIELDS(BPERI),
464 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
467 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
468 da9063_bcores_merged_limits,
469 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
470 /* BCORES_MERGED uses the same register fields as BCORE1 */
471 DA9063_BUCK_COMMON_FIELDS(BCORE1),
472 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
475 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
476 da9063_bmem_bio_merged_limits,
477 DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
478 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
479 DA9063_BUCK_COMMON_FIELDS(BMEM),
480 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
483 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
484 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
485 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
488 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
489 .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
490 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
493 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
494 .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
495 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
498 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
499 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
502 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
503 .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
504 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
507 /* The following LDOs are present only on DA9063, not on DA9063L */
509 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
510 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
513 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
514 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
517 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
518 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
519 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
522 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
523 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
526 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
527 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
531 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
532 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
536 /* Link chip model with regulators info table */
537 static struct da9063_dev_model regulators_models[] = {
539 .regulator_info = da9063_regulator_info,
540 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
541 .type = PMIC_TYPE_DA9063,
544 .regulator_info = da9063_regulator_info,
545 .n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
546 .type = PMIC_TYPE_DA9063L,
551 /* Regulator interrupt handlers */
552 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
554 struct da9063_regulators *regulators = data;
555 struct da9063 *hw = regulators->regulator[0].hw;
556 struct da9063_regulator *regl;
559 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
563 for (i = regulators->n_regulators - 1; i >= 0; i--) {
564 regl = ®ulators->regulator[i];
565 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
568 if (BIT(regl->info->oc_event.lsb) & bits) {
569 regulator_lock(regl->rdev);
570 regulator_notifier_call_chain(regl->rdev,
571 REGULATOR_EVENT_OVER_CURRENT, NULL);
572 regulator_unlock(regl->rdev);
580 * Probing and Initialisation functions
582 static const struct regulator_init_data *da9063_get_regulator_initdata(
583 const struct da9063_regulators_pdata *regl_pdata, int id)
587 for (i = 0; i < regl_pdata->n_regulators; i++) {
588 if (id == regl_pdata->regulator_data[i].id)
589 return regl_pdata->regulator_data[i].initdata;
596 static struct of_regulator_match da9063_matches[] = {
597 [DA9063_ID_BCORE1] = { .name = "bcore1" },
598 [DA9063_ID_BCORE2] = { .name = "bcore2" },
599 [DA9063_ID_BPRO] = { .name = "bpro", },
600 [DA9063_ID_BMEM] = { .name = "bmem", },
601 [DA9063_ID_BIO] = { .name = "bio", },
602 [DA9063_ID_BPERI] = { .name = "bperi", },
603 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
604 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
605 [DA9063_ID_LDO3] = { .name = "ldo3", },
606 [DA9063_ID_LDO7] = { .name = "ldo7", },
607 [DA9063_ID_LDO8] = { .name = "ldo8", },
608 [DA9063_ID_LDO9] = { .name = "ldo9", },
609 [DA9063_ID_LDO11] = { .name = "ldo11", },
610 /* The following LDOs are present only on DA9063, not on DA9063L */
611 [DA9063_ID_LDO1] = { .name = "ldo1", },
612 [DA9063_ID_LDO2] = { .name = "ldo2", },
613 [DA9063_ID_LDO4] = { .name = "ldo4", },
614 [DA9063_ID_LDO5] = { .name = "ldo5", },
615 [DA9063_ID_LDO6] = { .name = "ldo6", },
616 [DA9063_ID_LDO10] = { .name = "ldo10", },
619 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
620 struct platform_device *pdev,
621 struct of_regulator_match **da9063_reg_matches)
623 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
624 struct da9063_regulators_pdata *pdata;
625 struct da9063_regulator_data *rdata;
626 struct device_node *node;
627 int da9063_matches_len = ARRAY_SIZE(da9063_matches);
630 if (da9063->type == PMIC_TYPE_DA9063L)
631 da9063_matches_len -= 6;
633 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
635 dev_err(&pdev->dev, "Regulators device node not found\n");
636 return ERR_PTR(-ENODEV);
639 num = of_regulator_match(&pdev->dev, node, da9063_matches,
643 dev_err(&pdev->dev, "Failed to match regulators\n");
644 return ERR_PTR(-EINVAL);
647 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
649 return ERR_PTR(-ENOMEM);
651 pdata->regulator_data = devm_kcalloc(&pdev->dev,
652 num, sizeof(*pdata->regulator_data),
654 if (!pdata->regulator_data)
655 return ERR_PTR(-ENOMEM);
656 pdata->n_regulators = num;
659 for (i = 0; i < da9063_matches_len; i++) {
660 if (!da9063_matches[i].init_data)
663 rdata = &pdata->regulator_data[n];
665 rdata->initdata = da9063_matches[i].init_data;
670 *da9063_reg_matches = da9063_matches;
674 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
675 struct platform_device *pdev,
676 struct of_regulator_match **da9063_reg_matches)
678 *da9063_reg_matches = NULL;
679 return ERR_PTR(-ENODEV);
683 static int da9063_regulator_probe(struct platform_device *pdev)
685 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
686 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
687 struct of_regulator_match *da9063_reg_matches = NULL;
688 struct da9063_regulators_pdata *regl_pdata;
689 const struct da9063_dev_model *model;
690 struct da9063_regulators *regulators;
691 struct da9063_regulator *regl;
692 struct regulator_config config;
693 bool bcores_merged, bmem_bio_merged;
694 int id, irq, n, n_regulators, ret, val;
696 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
699 regl_pdata = da9063_parse_regulators_dt(pdev,
700 &da9063_reg_matches);
702 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
704 "No regulators defined for the platform\n");
708 /* Find regulators set for particular device model */
709 for (model = regulators_models; model->regulator_info; model++) {
710 if (model->type == da9063->type)
713 if (!model->regulator_info) {
714 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
719 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
722 "Error while reading BUCKs configuration\n");
725 bcores_merged = val & DA9063_BCORE_MERGE;
726 bmem_bio_merged = val & DA9063_BUCK_MERGE;
728 n_regulators = model->n_regulators;
730 n_regulators -= 2; /* remove BCORE1, BCORE2 */
732 n_regulators--; /* remove BCORES_MERGED */
734 n_regulators -= 2; /* remove BMEM, BIO */
736 n_regulators--; /* remove BMEM_BIO_MERGED */
738 /* Allocate memory required by usable regulators */
739 regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
740 regulator, n_regulators), GFP_KERNEL);
744 regulators->n_regulators = n_regulators;
745 platform_set_drvdata(pdev, regulators);
747 /* Register all regulators declared in platform information */
750 while (n < regulators->n_regulators) {
751 /* Skip regulator IDs depending on merge mode configuration */
753 case DA9063_ID_BCORE1:
754 case DA9063_ID_BCORE2:
762 if (bmem_bio_merged) {
767 case DA9063_ID_BCORES_MERGED:
768 if (!bcores_merged) {
773 case DA9063_ID_BMEM_BIO_MERGED:
774 if (!bmem_bio_merged) {
781 /* Initialise regulator structure */
782 regl = ®ulators->regulator[n];
784 regl->info = &model->regulator_info[id];
785 regl->desc = regl->info->desc;
786 regl->desc.type = REGULATOR_VOLTAGE;
787 regl->desc.owner = THIS_MODULE;
789 if (regl->info->mode.reg) {
790 regl->mode = devm_regmap_field_alloc(&pdev->dev,
791 da9063->regmap, regl->info->mode);
792 if (IS_ERR(regl->mode))
793 return PTR_ERR(regl->mode);
796 if (regl->info->suspend.reg) {
797 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
798 da9063->regmap, regl->info->suspend);
799 if (IS_ERR(regl->suspend))
800 return PTR_ERR(regl->suspend);
803 if (regl->info->sleep.reg) {
804 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
805 da9063->regmap, regl->info->sleep);
806 if (IS_ERR(regl->sleep))
807 return PTR_ERR(regl->sleep);
810 if (regl->info->suspend_sleep.reg) {
811 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
812 da9063->regmap, regl->info->suspend_sleep);
813 if (IS_ERR(regl->suspend_sleep))
814 return PTR_ERR(regl->suspend_sleep);
817 /* Register regulator */
818 memset(&config, 0, sizeof(config));
819 config.dev = &pdev->dev;
820 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
821 config.driver_data = regl;
822 if (da9063_reg_matches)
823 config.of_node = da9063_reg_matches[id].of_node;
824 config.regmap = da9063->regmap;
825 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc,
827 if (IS_ERR(regl->rdev)) {
829 "Failed to register %s regulator\n",
831 return PTR_ERR(regl->rdev);
837 /* LDOs overcurrent event support */
838 irq = platform_get_irq_byname(pdev, "LDO_LIM");
840 dev_err(&pdev->dev, "Failed to get IRQ.\n");
844 ret = devm_request_threaded_irq(&pdev->dev, irq,
845 NULL, da9063_ldo_lim_event,
846 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
847 "LDO_LIM", regulators);
849 dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
856 static struct platform_driver da9063_regulator_driver = {
858 .name = DA9063_DRVNAME_REGULATORS,
860 .probe = da9063_regulator_probe,
863 static int __init da9063_regulator_init(void)
865 return platform_driver_register(&da9063_regulator_driver);
867 subsys_initcall(da9063_regulator_init);
869 static void __exit da9063_regulator_cleanup(void)
871 platform_driver_unregister(&da9063_regulator_driver);
873 module_exit(da9063_regulator_cleanup);
876 /* Module information */
877 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
878 MODULE_DESCRIPTION("DA9063 regulators driver");
879 MODULE_LICENSE("GPL");
880 MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);