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/registers.h>
25 /* Definition for registering regmap bit fields using a mask */
26 #define BFIELD(_reg, _mask) \
27 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
28 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
30 /* DA9063 and DA9063L regulator IDs */
40 /* BCORE1 and BCORE2 in merged mode */
41 DA9063_ID_BCORES_MERGED,
42 /* BMEM and BIO in merged mode */
43 DA9063_ID_BMEM_BIO_MERGED,
44 /* When two BUCKs are merged, they cannot be reused separately */
46 /* LDOs on both DA9063 and DA9063L */
53 /* DA9063-only LDOs */
62 /* Old regulator platform data */
63 struct da9063_regulator_data {
65 struct regulator_init_data *initdata;
68 struct da9063_regulators_pdata {
69 unsigned int n_regulators;
70 struct da9063_regulator_data *regulator_data;
73 /* Regulator capabilities and registers description */
74 struct da9063_regulator_info {
75 struct regulator_desc desc;
77 /* DA9063 main register fields */
78 struct reg_field mode; /* buck mode of operation */
79 struct reg_field suspend;
80 struct reg_field sleep;
81 struct reg_field suspend_sleep;
82 unsigned int suspend_vsel_reg;
84 /* DA9063 event detection bit */
85 struct reg_field oc_event;
87 /* DA9063 voltage monitor bit */
88 struct reg_field vmon;
92 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
93 .desc.id = chip##_ID_##regl_name, \
94 .desc.name = __stringify(chip##_##regl_name), \
95 .desc.ops = &da9063_ldo_ops, \
96 .desc.min_uV = (min_mV) * 1000, \
97 .desc.uV_step = (step_mV) * 1000, \
98 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
99 + (DA9063_V##regl_name##_BIAS)), \
100 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
101 .desc.enable_mask = DA9063_LDO_EN, \
102 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
103 .desc.vsel_mask = DA9063_V##regl_name##_MASK, \
104 .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
105 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
106 .suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_LDO_CONF), \
107 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
108 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B
110 /* Macros for voltage DC/DC converters (BUCKs) */
111 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
113 .desc.id = chip##_ID_##regl_name, \
114 .desc.name = __stringify(chip##_##regl_name), \
115 .desc.ops = &da9063_buck_ops, \
116 .desc.min_uV = (min_mV) * 1000, \
117 .desc.uV_step = (step_mV) * 1000, \
118 .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
119 .desc.csel_reg = (creg), \
120 .desc.csel_mask = (cmask), \
121 .desc.curr_table = limits_array, \
122 .desc.n_current_limits = ARRAY_SIZE(limits_array)
124 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
125 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
126 .desc.enable_mask = DA9063_BUCK_EN, \
127 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
128 .desc.vsel_mask = DA9063_VBUCK_MASK, \
129 .desc.linear_min_sel = DA9063_VBUCK_BIAS, \
130 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
131 .suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_BUCK_CONF), \
132 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
133 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
134 .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
136 /* Defines asignment of regulators info table to chip model */
137 struct da9063_dev_model {
138 const struct da9063_regulator_info *regulator_info;
139 unsigned int n_regulators;
140 enum da9063_type type;
143 /* Single regulator settings */
144 struct da9063_regulator {
145 struct regulator_desc desc;
146 struct regulator_dev *rdev;
148 const struct da9063_regulator_info *info;
150 struct regmap_field *mode;
151 struct regmap_field *suspend;
152 struct regmap_field *sleep;
153 struct regmap_field *suspend_sleep;
154 struct regmap_field *vmon;
157 /* Encapsulates all information for the regulators driver */
158 struct da9063_regulators {
159 unsigned int n_regulators;
160 /* Array size to be defined during init. Keep at end. */
161 struct da9063_regulator regulator[];
164 /* BUCK modes for DA9063 */
166 BUCK_MODE_MANUAL, /* 0 */
167 BUCK_MODE_SLEEP, /* 1 */
168 BUCK_MODE_SYNC, /* 2 */
169 BUCK_MODE_AUTO /* 3 */
172 /* Regulator operations */
175 * Current limits array (in uA) for BCORE1, BCORE2, BPRO.
176 * Entry indexes corresponds to register values.
178 static const unsigned int da9063_buck_a_limits[] = {
179 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
180 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
184 * Current limits array (in uA) for BMEM, BIO, BPERI.
185 * Entry indexes corresponds to register values.
187 static const unsigned int da9063_buck_b_limits[] = {
188 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
189 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
193 * Current limits array (in uA) for merged BCORE1 and BCORE2.
194 * Entry indexes corresponds to register values.
196 static const unsigned int da9063_bcores_merged_limits[] = {
197 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
198 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
202 * Current limits array (in uA) for merged BMEM and BIO.
203 * Entry indexes corresponds to register values.
205 static const unsigned int da9063_bmem_bio_merged_limits[] = {
206 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
207 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
210 static int da9063_set_xvp(struct regulator_dev *rdev, int lim_uV, int severity, bool enable)
212 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
213 struct device *dev = regl->hw->dev;
215 dev_dbg(dev, "%s: lim: %d, sev: %d, en: %d\n", regl->desc.name, lim_uV, severity, enable);
218 * only support enable and disable.
219 * the da9063 offers a GPIO (GP_FB2) which is unasserted if an XV happens.
220 * therefore ignore severity here, as there might be handlers in hardware.
225 return regmap_field_write(regl->vmon, enable ? 1 : 0);
228 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
230 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
234 case REGULATOR_MODE_FAST:
235 val = BUCK_MODE_SYNC;
237 case REGULATOR_MODE_NORMAL:
238 val = BUCK_MODE_AUTO;
240 case REGULATOR_MODE_STANDBY:
241 val = BUCK_MODE_SLEEP;
247 return regmap_field_write(regl->mode, val);
251 * Bucks use single mode register field for normal operation
253 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
256 static unsigned int da9063_buck_get_mode(struct regulator_dev *rdev)
258 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
262 ret = regmap_field_read(regl->mode, &val);
268 case BUCK_MODE_MANUAL:
269 /* Sleep flag bit decides the mode */
271 case BUCK_MODE_SLEEP:
272 return REGULATOR_MODE_STANDBY;
274 return REGULATOR_MODE_FAST;
276 return REGULATOR_MODE_NORMAL;
279 ret = regmap_field_read(regl->sleep, &val);
284 return REGULATOR_MODE_STANDBY;
286 return REGULATOR_MODE_FAST;
290 * LDOs use sleep flags - one for normal and one for suspend state.
291 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
294 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
296 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
300 case REGULATOR_MODE_NORMAL:
303 case REGULATOR_MODE_STANDBY:
310 return regmap_field_write(regl->sleep, val);
313 static unsigned int da9063_ldo_get_mode(struct regulator_dev *rdev)
315 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
318 ret = regmap_field_read(regl->sleep, &val);
323 return REGULATOR_MODE_STANDBY;
325 return REGULATOR_MODE_NORMAL;
328 static int da9063_buck_get_status(struct regulator_dev *rdev)
330 int ret = regulator_is_enabled_regmap(rdev);
333 ret = REGULATOR_STATUS_OFF;
334 } else if (ret > 0) {
335 ret = da9063_buck_get_mode(rdev);
337 ret = regulator_mode_to_status(ret);
345 static int da9063_ldo_get_status(struct regulator_dev *rdev)
347 int ret = regulator_is_enabled_regmap(rdev);
350 ret = REGULATOR_STATUS_OFF;
351 } else if (ret > 0) {
352 ret = da9063_ldo_get_mode(rdev);
354 ret = regulator_mode_to_status(ret);
362 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
364 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
365 const struct da9063_regulator_info *rinfo = regl->info;
368 sel = regulator_map_voltage_linear(rdev, uV, uV);
372 sel <<= ffs(rdev->desc->vsel_mask) - 1;
374 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
375 rdev->desc->vsel_mask, sel);
380 static int da9063_suspend_enable(struct regulator_dev *rdev)
382 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
384 return regmap_field_write(regl->suspend, 1);
387 static int da9063_suspend_disable(struct regulator_dev *rdev)
389 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
391 return regmap_field_write(regl->suspend, 0);
394 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev,
397 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
401 case REGULATOR_MODE_FAST:
402 val = BUCK_MODE_SYNC;
404 case REGULATOR_MODE_NORMAL:
405 val = BUCK_MODE_AUTO;
407 case REGULATOR_MODE_STANDBY:
408 val = BUCK_MODE_SLEEP;
414 return regmap_field_write(regl->mode, val);
417 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev,
420 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
424 case REGULATOR_MODE_NORMAL:
427 case REGULATOR_MODE_STANDBY:
434 return regmap_field_write(regl->suspend_sleep, val);
437 static unsigned int da9063_get_overdrive_mask(const struct regulator_desc *desc)
440 case DA9063_ID_BCORES_MERGED:
441 case DA9063_ID_BCORE1:
442 return DA9063_BCORE1_OD;
443 case DA9063_ID_BCORE2:
444 return DA9063_BCORE2_OD;
446 return DA9063_BPRO_OD;
452 static int da9063_buck_set_limit_set_overdrive(struct regulator_dev *rdev,
453 int min_uA, int max_uA,
454 unsigned int overdrive_mask)
457 * When enabling overdrive, do it before changing the current limit to
458 * ensure sufficient supply throughout the switch.
460 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
462 unsigned int orig_overdrive;
464 ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H,
468 orig_overdrive &= overdrive_mask;
470 if (orig_overdrive == 0) {
471 ret = regmap_set_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
477 ret = regulator_set_current_limit_regmap(rdev, min_uA / 2, max_uA / 2);
478 if (ret < 0 && orig_overdrive == 0)
480 * regulator_set_current_limit_regmap may have rejected the
481 * change because of unusable min_uA and/or max_uA inputs.
482 * Attempt to restore original overdrive state, ignore failure-
485 regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
491 static int da9063_buck_set_limit_clear_overdrive(struct regulator_dev *rdev,
492 int min_uA, int max_uA,
493 unsigned int overdrive_mask)
496 * When disabling overdrive, do it after changing the current limit to
497 * ensure sufficient supply throughout the switch.
499 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
502 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &orig_limit);
506 ret = regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
510 ret = regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
514 * Attempt to restore original current limit, ignore failure-
517 regmap_write(rdev->regmap, rdev->desc->csel_reg, orig_limit);
522 static int da9063_buck_set_current_limit(struct regulator_dev *rdev,
523 int min_uA, int max_uA)
525 unsigned int overdrive_mask, n_currents;
527 overdrive_mask = da9063_get_overdrive_mask(rdev->desc);
528 if (overdrive_mask) {
529 n_currents = rdev->desc->n_current_limits;
533 if (max_uA > rdev->desc->curr_table[n_currents - 1])
534 return da9063_buck_set_limit_set_overdrive(rdev, min_uA,
538 return da9063_buck_set_limit_clear_overdrive(rdev, min_uA,
542 return regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
545 static int da9063_buck_get_current_limit(struct regulator_dev *rdev)
547 struct da9063_regulator *regl = rdev_get_drvdata(rdev);
551 limit = regulator_get_current_limit_regmap(rdev);
554 mask = da9063_get_overdrive_mask(rdev->desc);
556 ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H, &val);
565 static const struct regulator_ops da9063_buck_ops = {
566 .enable = regulator_enable_regmap,
567 .disable = regulator_disable_regmap,
568 .is_enabled = regulator_is_enabled_regmap,
569 .get_voltage_sel = regulator_get_voltage_sel_regmap,
570 .set_voltage_sel = regulator_set_voltage_sel_regmap,
571 .list_voltage = regulator_list_voltage_linear,
572 .set_current_limit = da9063_buck_set_current_limit,
573 .get_current_limit = da9063_buck_get_current_limit,
574 .set_mode = da9063_buck_set_mode,
575 .get_mode = da9063_buck_get_mode,
576 .get_status = da9063_buck_get_status,
577 .set_suspend_voltage = da9063_set_suspend_voltage,
578 .set_suspend_enable = da9063_suspend_enable,
579 .set_suspend_disable = da9063_suspend_disable,
580 .set_suspend_mode = da9063_buck_set_suspend_mode,
581 .set_over_voltage_protection = da9063_set_xvp,
582 .set_under_voltage_protection = da9063_set_xvp,
585 static const struct regulator_ops da9063_ldo_ops = {
586 .enable = regulator_enable_regmap,
587 .disable = regulator_disable_regmap,
588 .is_enabled = regulator_is_enabled_regmap,
589 .get_voltage_sel = regulator_get_voltage_sel_regmap,
590 .set_voltage_sel = regulator_set_voltage_sel_regmap,
591 .list_voltage = regulator_list_voltage_linear,
592 .set_mode = da9063_ldo_set_mode,
593 .get_mode = da9063_ldo_get_mode,
594 .get_status = da9063_ldo_get_status,
595 .set_suspend_voltage = da9063_set_suspend_voltage,
596 .set_suspend_enable = da9063_suspend_enable,
597 .set_suspend_disable = da9063_suspend_disable,
598 .set_suspend_mode = da9063_ldo_set_suspend_mode,
599 .set_over_voltage_protection = da9063_set_xvp,
600 .set_under_voltage_protection = da9063_set_xvp,
603 /* Info of regulators for DA9063 */
604 static const struct da9063_regulator_info da9063_regulator_info[] = {
606 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
607 da9063_buck_a_limits,
608 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
609 DA9063_BUCK_COMMON_FIELDS(BCORE1),
610 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE1_MON_EN),
613 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
614 da9063_buck_a_limits,
615 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
616 DA9063_BUCK_COMMON_FIELDS(BCORE2),
617 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE2_MON_EN),
620 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
621 da9063_buck_a_limits,
622 DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
623 DA9063_BUCK_COMMON_FIELDS(BPRO),
624 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BPRO_MON_EN),
627 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
628 da9063_buck_b_limits,
629 DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
630 DA9063_BUCK_COMMON_FIELDS(BMEM),
631 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BMEM_MON_EN),
634 DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
635 da9063_buck_b_limits,
636 DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
637 DA9063_BUCK_COMMON_FIELDS(BIO),
638 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BIO_MON_EN),
641 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
642 da9063_buck_b_limits,
643 DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
644 DA9063_BUCK_COMMON_FIELDS(BPERI),
645 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BPERI_MON_EN),
648 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
649 da9063_bcores_merged_limits,
650 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
651 /* BCORES_MERGED uses the same register fields as BCORE1 */
652 DA9063_BUCK_COMMON_FIELDS(BCORE1),
653 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE1_MON_EN),
656 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
657 da9063_bmem_bio_merged_limits,
658 DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
659 /* BMEM_BIO_MERGED uses the same register fields as BMEM */
660 DA9063_BUCK_COMMON_FIELDS(BMEM),
661 .vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BMEM_MON_EN),
664 DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
665 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
666 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO3_MON_EN),
669 DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
670 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
671 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO7_MON_EN),
674 DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
675 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
676 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO8_MON_EN),
679 DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
680 .vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO9_MON_EN),
683 DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
684 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
685 .vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO11_MON_EN),
688 /* The following LDOs are present only on DA9063, not on DA9063L */
690 DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
691 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO1_MON_EN),
694 DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
695 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO2_MON_EN),
698 DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
699 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
700 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO4_MON_EN),
703 DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
704 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO5_MON_EN),
707 DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
708 .vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO6_MON_EN),
712 DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
713 .vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO10_MON_EN),
717 /* Link chip model with regulators info table */
718 static struct da9063_dev_model regulators_models[] = {
720 .regulator_info = da9063_regulator_info,
721 .n_regulators = ARRAY_SIZE(da9063_regulator_info),
722 .type = PMIC_TYPE_DA9063,
725 .regulator_info = da9063_regulator_info,
726 .n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
727 .type = PMIC_TYPE_DA9063L,
732 /* Regulator interrupt handlers */
733 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
735 struct da9063_regulators *regulators = data;
736 struct da9063 *hw = regulators->regulator[0].hw;
737 struct da9063_regulator *regl;
740 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
744 for (i = regulators->n_regulators - 1; i >= 0; i--) {
745 regl = ®ulators->regulator[i];
746 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
749 if (BIT(regl->info->oc_event.lsb) & bits) {
750 regulator_notifier_call_chain(regl->rdev,
751 REGULATOR_EVENT_OVER_CURRENT, NULL);
759 * Probing and Initialisation functions
761 static const struct regulator_init_data *da9063_get_regulator_initdata(
762 const struct da9063_regulators_pdata *regl_pdata, int id)
766 for (i = 0; i < regl_pdata->n_regulators; i++) {
767 if (id == regl_pdata->regulator_data[i].id)
768 return regl_pdata->regulator_data[i].initdata;
774 static int da9063_check_xvp_constraints(struct regulator_config *config)
776 struct da9063_regulator *regl = config->driver_data;
777 const struct regulation_constraints *constr = &config->init_data->constraints;
778 const struct notification_limit *uv_l = &constr->under_voltage_limits;
779 const struct notification_limit *ov_l = &constr->over_voltage_limits;
781 /* make sure that only one severity is used to clarify if unchanged, enabled or disabled */
782 if ((!!uv_l->prot + !!uv_l->err + !!uv_l->warn) > 1) {
783 dev_err(config->dev, "%s: at most one voltage monitoring severity allowed!\n",
788 /* make sure that UV and OV monitoring is set to the same severity and value */
789 if (uv_l->prot != ov_l->prot) {
791 "%s: protection-microvolt: value must be equal for uv and ov!\n",
795 if (uv_l->err != ov_l->err) {
796 dev_err(config->dev, "%s: error-microvolt: value must be equal for uv and ov!\n",
800 if (uv_l->warn != ov_l->warn) {
801 dev_err(config->dev, "%s: warn-microvolt: value must be equal for uv and ov!\n",
809 static struct of_regulator_match da9063_matches[] = {
810 [DA9063_ID_BCORE1] = { .name = "bcore1" },
811 [DA9063_ID_BCORE2] = { .name = "bcore2" },
812 [DA9063_ID_BPRO] = { .name = "bpro", },
813 [DA9063_ID_BMEM] = { .name = "bmem", },
814 [DA9063_ID_BIO] = { .name = "bio", },
815 [DA9063_ID_BPERI] = { .name = "bperi", },
816 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" },
817 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", },
818 [DA9063_ID_LDO3] = { .name = "ldo3", },
819 [DA9063_ID_LDO7] = { .name = "ldo7", },
820 [DA9063_ID_LDO8] = { .name = "ldo8", },
821 [DA9063_ID_LDO9] = { .name = "ldo9", },
822 [DA9063_ID_LDO11] = { .name = "ldo11", },
823 /* The following LDOs are present only on DA9063, not on DA9063L */
824 [DA9063_ID_LDO1] = { .name = "ldo1", },
825 [DA9063_ID_LDO2] = { .name = "ldo2", },
826 [DA9063_ID_LDO4] = { .name = "ldo4", },
827 [DA9063_ID_LDO5] = { .name = "ldo5", },
828 [DA9063_ID_LDO6] = { .name = "ldo6", },
829 [DA9063_ID_LDO10] = { .name = "ldo10", },
832 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
833 struct platform_device *pdev,
834 struct of_regulator_match **da9063_reg_matches)
836 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
837 struct da9063_regulators_pdata *pdata;
838 struct da9063_regulator_data *rdata;
839 struct device_node *node;
840 int da9063_matches_len = ARRAY_SIZE(da9063_matches);
843 if (da9063->type == PMIC_TYPE_DA9063L)
844 da9063_matches_len -= 6;
846 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
848 dev_err(&pdev->dev, "Regulators device node not found\n");
849 return ERR_PTR(-ENODEV);
852 num = of_regulator_match(&pdev->dev, node, da9063_matches,
856 dev_err(&pdev->dev, "Failed to match regulators\n");
857 return ERR_PTR(-EINVAL);
860 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
862 return ERR_PTR(-ENOMEM);
864 pdata->regulator_data = devm_kcalloc(&pdev->dev,
865 num, sizeof(*pdata->regulator_data),
867 if (!pdata->regulator_data)
868 return ERR_PTR(-ENOMEM);
869 pdata->n_regulators = num;
872 for (i = 0; i < da9063_matches_len; i++) {
873 if (!da9063_matches[i].init_data)
876 rdata = &pdata->regulator_data[n];
878 rdata->initdata = da9063_matches[i].init_data;
883 *da9063_reg_matches = da9063_matches;
887 static int da9063_regulator_probe(struct platform_device *pdev)
889 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
890 struct of_regulator_match *da9063_reg_matches = NULL;
891 struct da9063_regulators_pdata *regl_pdata;
892 const struct da9063_dev_model *model;
893 struct da9063_regulators *regulators;
894 struct da9063_regulator *regl;
895 struct regulator_config config;
896 bool bcores_merged, bmem_bio_merged;
897 int id, irq, n, n_regulators, ret, val;
899 regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches);
901 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
903 "No regulators defined for the platform\n");
907 /* Find regulators set for particular device model */
908 for (model = regulators_models; model->regulator_info; model++) {
909 if (model->type == da9063->type)
912 if (!model->regulator_info) {
913 dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
918 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
921 "Error while reading BUCKs configuration\n");
924 bcores_merged = val & DA9063_BCORE_MERGE;
925 bmem_bio_merged = val & DA9063_BUCK_MERGE;
927 n_regulators = model->n_regulators;
929 n_regulators -= 2; /* remove BCORE1, BCORE2 */
931 n_regulators--; /* remove BCORES_MERGED */
933 n_regulators -= 2; /* remove BMEM, BIO */
935 n_regulators--; /* remove BMEM_BIO_MERGED */
937 /* Allocate memory required by usable regulators */
938 regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
939 regulator, n_regulators), GFP_KERNEL);
943 regulators->n_regulators = n_regulators;
944 platform_set_drvdata(pdev, regulators);
946 /* Register all regulators declared in platform information */
949 while (n < regulators->n_regulators) {
950 /* Skip regulator IDs depending on merge mode configuration */
952 case DA9063_ID_BCORE1:
953 case DA9063_ID_BCORE2:
961 if (bmem_bio_merged) {
966 case DA9063_ID_BCORES_MERGED:
967 if (!bcores_merged) {
972 case DA9063_ID_BMEM_BIO_MERGED:
973 if (!bmem_bio_merged) {
980 /* Initialise regulator structure */
981 regl = ®ulators->regulator[n];
983 regl->info = &model->regulator_info[id];
984 regl->desc = regl->info->desc;
985 regl->desc.type = REGULATOR_VOLTAGE;
986 regl->desc.owner = THIS_MODULE;
988 if (regl->info->mode.reg) {
989 regl->mode = devm_regmap_field_alloc(&pdev->dev,
990 da9063->regmap, regl->info->mode);
991 if (IS_ERR(regl->mode))
992 return PTR_ERR(regl->mode);
995 if (regl->info->suspend.reg) {
996 regl->suspend = devm_regmap_field_alloc(&pdev->dev,
997 da9063->regmap, regl->info->suspend);
998 if (IS_ERR(regl->suspend))
999 return PTR_ERR(regl->suspend);
1002 if (regl->info->sleep.reg) {
1003 regl->sleep = devm_regmap_field_alloc(&pdev->dev,
1004 da9063->regmap, regl->info->sleep);
1005 if (IS_ERR(regl->sleep))
1006 return PTR_ERR(regl->sleep);
1009 if (regl->info->suspend_sleep.reg) {
1010 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
1011 da9063->regmap, regl->info->suspend_sleep);
1012 if (IS_ERR(regl->suspend_sleep))
1013 return PTR_ERR(regl->suspend_sleep);
1015 if (regl->info->vmon.reg) {
1016 regl->vmon = devm_regmap_field_alloc(&pdev->dev,
1017 da9063->regmap, regl->info->vmon);
1018 if (IS_ERR(regl->vmon))
1019 return PTR_ERR(regl->vmon);
1022 /* Register regulator */
1023 memset(&config, 0, sizeof(config));
1024 config.dev = &pdev->dev;
1025 config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
1026 config.driver_data = regl;
1027 if (da9063_reg_matches)
1028 config.of_node = da9063_reg_matches[id].of_node;
1029 config.regmap = da9063->regmap;
1031 ret = da9063_check_xvp_constraints(&config);
1035 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc,
1037 if (IS_ERR(regl->rdev)) {
1039 "Failed to register %s regulator\n",
1041 return PTR_ERR(regl->rdev);
1047 /* LDOs overcurrent event support */
1048 irq = platform_get_irq_byname(pdev, "LDO_LIM");
1052 ret = devm_request_threaded_irq(&pdev->dev, irq,
1053 NULL, da9063_ldo_lim_event,
1054 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1055 "LDO_LIM", regulators);
1057 dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
1062 static struct platform_driver da9063_regulator_driver = {
1064 .name = DA9063_DRVNAME_REGULATORS,
1065 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1067 .probe = da9063_regulator_probe,
1070 static int __init da9063_regulator_init(void)
1072 return platform_driver_register(&da9063_regulator_driver);
1074 subsys_initcall(da9063_regulator_init);
1076 static void __exit da9063_regulator_cleanup(void)
1078 platform_driver_unregister(&da9063_regulator_driver);
1080 module_exit(da9063_regulator_cleanup);
1083 /* Module information */
1084 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
1085 MODULE_DESCRIPTION("DA9063 regulators driver");
1086 MODULE_LICENSE("GPL");
1087 MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);