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 {
120 unsigned n_regulators;
121 /* Array size to be defined during init. Keep at end. */
122 struct da9063_regulator regulator[0];
125 /* BUCK modes for DA9063 */
127 BUCK_MODE_MANUAL, /* 0 */
128 BUCK_MODE_SLEEP, /* 1 */
129 BUCK_MODE_SYNC, /* 2 */
130 BUCK_MODE_AUTO /* 3 */
133 /* Regulator operations */
135 /* Current limits array (in uA) for BCORE1, BCORE2, BPRO.
136 Entry indexes corresponds to register values. */
137 static const int da9063_buck_a_limits[] = {
138 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
139 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
142 /* Current limits array (in uA) for BMEM, BIO, BPERI.
143 Entry indexes corresponds to register values. */
144 static const int da9063_buck_b_limits[] = {
145 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
146 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
149 /* Current limits array (in uA) for merged BCORE1 and BCORE2.
150 Entry indexes corresponds to register values. */
151 static const int da9063_bcores_merged_limits[] = {
152 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
153 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
156 /* Current limits array (in uA) for merged BMEM and BIO.
157 Entry indexes corresponds to register values. */
158 static const int da9063_bmem_bio_merged_limits[] = {
159 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
160 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
163 static int da9063_set_current_limit(struct regulator_dev *rdev,
164 int min_uA, int max_uA)
166 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
167 const struct da9063_regulator_info *rinfo = regl->info;
170 for (n = 0; n < rinfo->n_current_limits; n++) {
171 tval = rinfo->current_limits[n];
172 if (tval >= min_uA && tval <= max_uA)
173 return regmap_field_write(regl->ilimit, n);
179 static int da9063_get_current_limit(struct regulator_dev *rdev)
181 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
182 const struct da9063_regulator_info *rinfo = regl->info;
186 ret = regmap_field_read(regl->ilimit, &sel);
190 if (sel >= rinfo->n_current_limits)
191 sel = rinfo->n_current_limits - 1;
193 return rinfo->current_limits[sel];
196 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
198 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
202 case REGULATOR_MODE_FAST:
203 val = BUCK_MODE_SYNC;
205 case REGULATOR_MODE_NORMAL:
206 val = BUCK_MODE_AUTO;
208 case REGULATOR_MODE_STANDBY:
209 val = BUCK_MODE_SLEEP;
215 return regmap_field_write(regl->mode, val);
219 * Bucks use single mode register field for normal operation
221 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
224 static unsigned da9063_buck_get_mode(struct regulator_dev *rdev)
226 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
227 struct regmap_field *field;
228 unsigned int val, mode = 0;
231 ret = regmap_field_read(regl->mode, &val);
237 case BUCK_MODE_MANUAL:
238 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
239 /* Sleep flag bit decides the mode */
241 case BUCK_MODE_SLEEP:
242 return REGULATOR_MODE_STANDBY;
244 return REGULATOR_MODE_FAST;
246 return REGULATOR_MODE_NORMAL;
249 /* Detect current regulator state */
250 ret = regmap_field_read(regl->suspend, &val);
254 /* Read regulator mode from proper register, depending on state */
256 field = regl->suspend_sleep;
260 ret = regmap_field_read(field, &val);
265 mode &= REGULATOR_MODE_STANDBY;
267 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
273 * LDOs use sleep flags - one for normal and one for suspend state.
274 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
277 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
279 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
283 case REGULATOR_MODE_NORMAL:
286 case REGULATOR_MODE_STANDBY:
293 return regmap_field_write(regl->sleep, val);
296 static unsigned da9063_ldo_get_mode(struct regulator_dev *rdev)
298 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
299 struct regmap_field *field;
302 /* Detect current regulator state */
303 ret = regmap_field_read(regl->suspend, &val);
307 /* Read regulator mode from proper register, depending on state */
309 field = regl->suspend_sleep;
313 ret = regmap_field_read(field, &val);
318 return REGULATOR_MODE_STANDBY;
320 return REGULATOR_MODE_NORMAL;
323 static int da9063_buck_get_status(struct regulator_dev *rdev)
325 int ret = regulator_is_enabled_regmap(rdev);
328 ret = REGULATOR_STATUS_OFF;
329 } else if (ret > 0) {
330 ret = da9063_buck_get_mode(rdev);
332 ret = regulator_mode_to_status(ret);
340 static int da9063_ldo_get_status(struct regulator_dev *rdev)
342 int ret = regulator_is_enabled_regmap(rdev);
345 ret = REGULATOR_STATUS_OFF;
346 } else if (ret > 0) {
347 ret = da9063_ldo_get_mode(rdev);
349 ret = regulator_mode_to_status(ret);
357 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
359 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
360 const struct da9063_regulator_info *rinfo = regl->info;
363 sel = regulator_map_voltage_linear(rdev, uV, uV);
367 sel <<= ffs(rdev->desc->vsel_mask) - 1;
369 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
370 rdev->desc->vsel_mask, sel);
375 static int da9063_suspend_enable(struct regulator_dev *rdev)
377 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
379 return regmap_field_write(regl->suspend, 1);
382 static int da9063_suspend_disable(struct regulator_dev *rdev)
384 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
386 return regmap_field_write(regl->suspend, 0);
389 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
391 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
395 case REGULATOR_MODE_FAST:
396 val = BUCK_MODE_SYNC;
398 case REGULATOR_MODE_NORMAL:
399 val = BUCK_MODE_AUTO;
401 case REGULATOR_MODE_STANDBY:
402 val = BUCK_MODE_SLEEP;
408 return regmap_field_write(regl->mode, val);
411 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, unsigned mode)
413 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
417 case REGULATOR_MODE_NORMAL:
420 case REGULATOR_MODE_STANDBY:
427 return regmap_field_write(regl->suspend_sleep, val);
430 static const struct regulator_ops da9063_buck_ops = {
431 .enable = regulator_enable_regmap,
432 .disable = regulator_disable_regmap,
433 .is_enabled = regulator_is_enabled_regmap,
434 .get_voltage_sel = regulator_get_voltage_sel_regmap,
435 .set_voltage_sel = regulator_set_voltage_sel_regmap,
436 .list_voltage = regulator_list_voltage_linear,
437 .set_current_limit = da9063_set_current_limit,
438 .get_current_limit = da9063_get_current_limit,
439 .set_mode = da9063_buck_set_mode,
440 .get_mode = da9063_buck_get_mode,
441 .get_status = da9063_buck_get_status,
442 .set_suspend_voltage = da9063_set_suspend_voltage,
443 .set_suspend_enable = da9063_suspend_enable,
444 .set_suspend_disable = da9063_suspend_disable,
445 .set_suspend_mode = da9063_buck_set_suspend_mode,
448 static const struct regulator_ops da9063_ldo_ops = {
449 .enable = regulator_enable_regmap,
450 .disable = regulator_disable_regmap,
451 .is_enabled = regulator_is_enabled_regmap,
452 .get_voltage_sel = regulator_get_voltage_sel_regmap,
453 .set_voltage_sel = regulator_set_voltage_sel_regmap,
454 .list_voltage = regulator_list_voltage_linear,
455 .set_mode = da9063_ldo_set_mode,
456 .get_mode = da9063_ldo_get_mode,
457 .get_status = da9063_ldo_get_status,
458 .set_suspend_voltage = da9063_set_suspend_voltage,
459 .set_suspend_enable = da9063_suspend_enable,
460 .set_suspend_disable = da9063_suspend_disable,
461 .set_suspend_mode = da9063_ldo_set_suspend_mode,
464 /* Info of regulators for DA9063 */
465 static const struct da9063_regulator_info da9063_regulator_info[] = {
467 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
468 da9063_buck_a_limits),
469 DA9063_BUCK_COMMON_FIELDS(BCORE1),
470 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
471 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
472 DA9063_BCORE1_ILIM_MASK),
475 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
476 da9063_buck_a_limits),
477 DA9063_BUCK_COMMON_FIELDS(BCORE2),
478 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE2_SEL),
479 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
480 DA9063_BCORE2_ILIM_MASK),
483 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
484 da9063_buck_a_limits),
485 DA9063_BUCK_COMMON_FIELDS(BPRO),
486 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPRO_SEL),
487 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
488 DA9063_BPRO_ILIM_MASK),
491 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
492 da9063_buck_b_limits),
493 DA9063_BUCK_COMMON_FIELDS(BMEM),
494 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
495 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
496 DA9063_BMEM_ILIM_MASK),
499 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
500 da9063_buck_b_limits),
501 DA9063_BUCK_COMMON_FIELDS(BIO),
502 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VBIO_SEL),
503 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
504 DA9063_BIO_ILIM_MASK),
507 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
508 da9063_buck_b_limits),
509 DA9063_BUCK_COMMON_FIELDS(BPERI),
510 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBPERI_SEL),
511 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_B,
512 DA9063_BPERI_ILIM_MASK),
515 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
516 da9063_bcores_merged_limits),
517 /* BCORES_MERGED uses the same register fields as BCORE1 */
518 DA9063_BUCK_COMMON_FIELDS(BCORE1),
519 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBCORE1_SEL),
520 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_C,
521 DA9063_BCORE1_ILIM_MASK),
524 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
525 da9063_bmem_bio_merged_limits),
526 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
527 DA9063_BUCK_COMMON_FIELDS(BMEM),
528 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VBMEM_SEL),
529 .ilimit = BFIELD(DA9063_REG_BUCK_ILIM_A,
530 DA9063_BMEM_ILIM_MASK),
533 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
534 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO1_SEL),
537 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
538 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO2_SEL),
541 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
542 .suspend = BFIELD(DA9063_REG_DVC_1, DA9063_VLDO3_SEL),
543 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
546 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
547 .suspend = BFIELD(DA9063_REG_DVC_2, DA9063_VLDO4_SEL),
548 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
551 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
552 .suspend = BFIELD(DA9063_REG_LDO5_CONT, DA9063_VLDO5_SEL),
555 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
556 .suspend = BFIELD(DA9063_REG_LDO6_CONT, DA9063_VLDO6_SEL),
559 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
560 .suspend = BFIELD(DA9063_REG_LDO7_CONT, DA9063_VLDO7_SEL),
561 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
564 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
565 .suspend = BFIELD(DA9063_REG_LDO8_CONT, DA9063_VLDO8_SEL),
566 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
569 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
570 .suspend = BFIELD(DA9063_REG_LDO9_CONT, DA9063_VLDO9_SEL),
573 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
574 .suspend = BFIELD(DA9063_REG_LDO10_CONT, DA9063_VLDO10_SEL),
577 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
578 .suspend = BFIELD(DA9063_REG_LDO11_CONT, DA9063_VLDO11_SEL),
579 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
583 /* Link chip model with regulators info table */
584 static struct da9063_dev_model regulators_models[] = {
586 .regulator_info = da9063_regulator_info,
587 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
588 .dev_model = PMIC_DA9063,
593 /* Regulator interrupt handlers */
594 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
596 struct da9063_regulators *regulators = data;
597 struct da9063 *hw = regulators->regulator[0].hw;
598 struct da9063_regulator *regl;
601 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
605 for (i = regulators->n_regulators - 1; i >= 0; i--) {
606 regl = ®ulators->regulator[i];
607 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
610 if (BIT(regl->info->oc_event.lsb) & bits)
611 regulator_notifier_call_chain(regl->rdev,
612 REGULATOR_EVENT_OVER_CURRENT, NULL);
619 * Probing and Initialisation functions
621 static const struct regulator_init_data *da9063_get_regulator_initdata(
622 const struct da9063_regulators_pdata *regl_pdata, int id)
626 for (i = 0; i < regl_pdata->n_regulators; i++) {
627 if (id == regl_pdata->regulator_data[i].id)
628 return regl_pdata->regulator_data[i].initdata;
635 static struct of_regulator_match da9063_matches[] = {
636 [DA9063_ID_BCORE1] = { .name = "bcore1" },
637 [DA9063_ID_BCORE2] = { .name = "bcore2" },
638 [DA9063_ID_BPRO] = { .name = "bpro", },
639 [DA9063_ID_BMEM] = { .name = "bmem", },
640 [DA9063_ID_BIO] = { .name = "bio", },
641 [DA9063_ID_BPERI] = { .name = "bperi", },
642 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
643 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
644 [DA9063_ID_LDO1] = { .name = "ldo1", },
645 [DA9063_ID_LDO2] = { .name = "ldo2", },
646 [DA9063_ID_LDO3] = { .name = "ldo3", },
647 [DA9063_ID_LDO4] = { .name = "ldo4", },
648 [DA9063_ID_LDO5] = { .name = "ldo5", },
649 [DA9063_ID_LDO6] = { .name = "ldo6", },
650 [DA9063_ID_LDO7] = { .name = "ldo7", },
651 [DA9063_ID_LDO8] = { .name = "ldo8", },
652 [DA9063_ID_LDO9] = { .name = "ldo9", },
653 [DA9063_ID_LDO10] = { .name = "ldo10", },
654 [DA9063_ID_LDO11] = { .name = "ldo11", },
657 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
658 struct platform_device *pdev,
659 struct of_regulator_match **da9063_reg_matches)
661 struct da9063_regulators_pdata *pdata;
662 struct da9063_regulator_data *rdata;
663 struct device_node *node;
666 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
668 dev_err(&pdev->dev, "Regulators device node not found\n");
669 return ERR_PTR(-ENODEV);
672 num = of_regulator_match(&pdev->dev, node, da9063_matches,
673 ARRAY_SIZE(da9063_matches));
676 dev_err(&pdev->dev, "Failed to match regulators\n");
677 return ERR_PTR(-EINVAL);
680 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
682 return ERR_PTR(-ENOMEM);
684 pdata->regulator_data = devm_kzalloc(&pdev->dev,
685 num * sizeof(*pdata->regulator_data),
687 if (!pdata->regulator_data)
688 return ERR_PTR(-ENOMEM);
689 pdata->n_regulators = num;
692 for (i = 0; i < ARRAY_SIZE(da9063_matches); i++) {
693 if (!da9063_matches[i].init_data)
696 rdata = &pdata->regulator_data[n];
698 rdata->initdata = da9063_matches[i].init_data;
703 *da9063_reg_matches = da9063_matches;
707 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
708 struct platform_device *pdev,
709 struct of_regulator_match **da9063_reg_matches)
711 *da9063_reg_matches = NULL;
712 return ERR_PTR(-ENODEV);
716 static int da9063_regulator_probe(struct platform_device *pdev)
718 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
719 struct da9063_pdata *da9063_pdata = dev_get_platdata(da9063->dev);
720 struct of_regulator_match *da9063_reg_matches = NULL;
721 struct da9063_regulators_pdata *regl_pdata;
722 const struct da9063_dev_model *model;
723 struct da9063_regulators *regulators;
724 struct da9063_regulator *regl;
725 struct regulator_config config;
726 bool bcores_merged, bmem_bio_merged;
727 int id, irq, n, n_regulators, ret, val;
730 regl_pdata = da9063_pdata ? da9063_pdata->regulators_pdata : NULL;
733 regl_pdata = da9063_parse_regulators_dt(pdev,
734 &da9063_reg_matches);
736 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
738 "No regulators defined for the platform\n");
739 return PTR_ERR(regl_pdata);
742 /* Find regulators set for particular device model */
743 for (model = regulators_models; model->regulator_info; model++) {
744 if (model->dev_model == da9063->model)
747 if (!model->regulator_info) {
748 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
753 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
756 "Error while reading BUCKs configuration\n");
759 bcores_merged = val & DA9063_BCORE_MERGE;
760 bmem_bio_merged = val & DA9063_BUCK_MERGE;
762 n_regulators = model->n_regulators;
764 n_regulators -= 2; /* remove BCORE1, BCORE2 */
766 n_regulators--; /* remove BCORES_MERGED */
768 n_regulators -= 2; /* remove BMEM, BIO */
770 n_regulators--; /* remove BMEM_BIO_MERGED */
772 /* Allocate memory required by usable regulators */
773 size = sizeof(struct da9063_regulators) +
774 n_regulators * sizeof(struct da9063_regulator);
775 regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
779 regulators->n_regulators = n_regulators;
780 platform_set_drvdata(pdev, regulators);
782 /* Register all regulators declared in platform information */
785 while (n < regulators->n_regulators) {
786 /* Skip regulator IDs depending on merge mode configuration */
788 case DA9063_ID_BCORE1:
789 case DA9063_ID_BCORE2:
797 if (bmem_bio_merged) {
802 case DA9063_ID_BCORES_MERGED:
803 if (!bcores_merged) {
808 case DA9063_ID_BMEM_BIO_MERGED:
809 if (!bmem_bio_merged) {
816 /* Initialise regulator structure */
817 regl = ®ulators->regulator[n];
819 regl->info = &model->regulator_info[id];
820 regl->desc = regl->info->desc;
821 regl->desc.type = REGULATOR_VOLTAGE;
822 regl->desc.owner = THIS_MODULE;
824 if (regl->info->mode.reg)
825 regl->mode = devm_regmap_field_alloc(&pdev->dev,
826 da9063->regmap, regl->info->mode);
827 if (regl->info->suspend.reg)
828 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
829 da9063->regmap, regl->info->suspend);
830 if (regl->info->sleep.reg)
831 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
832 da9063->regmap, regl->info->sleep);
833 if (regl->info->suspend_sleep.reg)
834 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
835 da9063->regmap, regl->info->suspend_sleep);
836 if (regl->info->ilimit.reg)
837 regl->ilimit = devm_regmap_field_alloc(&pdev->dev,
838 da9063->regmap, regl->info->ilimit);
840 /* Register regulator */
841 memset(&config, 0, sizeof(config));
842 config.dev = &pdev->dev;
843 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
844 config.driver_data = regl;
845 if (da9063_reg_matches)
846 config.of_node = da9063_reg_matches[id].of_node;
847 config.regmap = da9063->regmap;
848 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc,
850 if (IS_ERR(regl->rdev)) {
852 "Failed to register %s regulator\n",
854 return PTR_ERR(regl->rdev);
860 /* LDOs overcurrent event support */
861 irq = platform_get_irq_byname(pdev, "LDO_LIM");
863 dev_err(&pdev->dev, "Failed to get IRQ.\n");
867 ret = devm_request_threaded_irq(&pdev->dev, irq,
868 NULL, da9063_ldo_lim_event,
869 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
870 "LDO_LIM", regulators);
872 dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
879 static struct platform_driver da9063_regulator_driver = {
881 .name = DA9063_DRVNAME_REGULATORS,
883 .probe = da9063_regulator_probe,
886 static int __init da9063_regulator_init(void)
888 return platform_driver_register(&da9063_regulator_driver);
890 subsys_initcall(da9063_regulator_init);
892 static void __exit da9063_regulator_cleanup(void)
894 platform_driver_unregister(&da9063_regulator_driver);
896 module_exit(da9063_regulator_cleanup);
899 /* Module information */
900 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
901 MODULE_DESCRIPTION("DA9063 regulators driver");
902 MODULE_LICENSE("GPL");
903 MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);