1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 ROHM Semiconductors
3 // ROHM BD9576MUF/BD9573MUF regulator driver
6 #include <linux/gpio/consumer.h>
7 #include <linux/interrupt.h>
8 #include <linux/jiffies.h>
9 #include <linux/kernel.h>
10 #include <linux/mfd/rohm-bd957x.h>
11 #include <linux/mfd/rohm-generic.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
22 #define BD957X_VOUTS1_VOLT 3300000
23 #define BD957X_VOUTS4_BASE_VOLT 1030000
24 #define BD957X_VOUTS34_NUM_VOLT 32
26 #define BD9576_THERM_IRQ_MASK_TW BIT(5)
27 #define BD9576_xVD_IRQ_MASK_VOUTL1 BIT(5)
28 #define BD9576_UVD_IRQ_MASK_VOUTS1_OCW BIT(6)
29 #define BD9576_xVD_IRQ_MASK_VOUT1TO4 0x0F
31 static const unsigned int vout1_volt_table[] = {
32 5000000, 4900000, 4800000, 4700000, 4600000,
33 4500000, 4500000, 4500000, 5000000, 5100000,
34 5200000, 5300000, 5400000, 5500000, 5500000,
38 static const unsigned int vout2_volt_table[] = {
39 1800000, 1780000, 1760000, 1740000, 1720000,
40 1700000, 1680000, 1660000, 1800000, 1820000,
41 1840000, 1860000, 1880000, 1900000, 1920000,
45 static const unsigned int voutl1_volt_table[] = {
46 2500000, 2540000, 2580000, 2620000, 2660000,
47 2700000, 2740000, 2780000, 2500000, 2460000,
48 2420000, 2380000, 2340000, 2300000, 2260000,
52 static const struct linear_range vout1_xvd_ranges[] = {
53 REGULATOR_LINEAR_RANGE(225000, 0x01, 0x2b, 0),
54 REGULATOR_LINEAR_RANGE(225000, 0x2c, 0x54, 5000),
55 REGULATOR_LINEAR_RANGE(425000, 0x55, 0x7f, 0),
58 static const struct linear_range vout234_xvd_ranges[] = {
59 REGULATOR_LINEAR_RANGE(17000, 0x01, 0x0f, 0),
60 REGULATOR_LINEAR_RANGE(17000, 0x10, 0x6d, 1000),
61 REGULATOR_LINEAR_RANGE(110000, 0x6e, 0x7f, 0),
64 static const struct linear_range voutL1_xvd_ranges[] = {
65 REGULATOR_LINEAR_RANGE(34000, 0x01, 0x0f, 0),
66 REGULATOR_LINEAR_RANGE(34000, 0x10, 0x6d, 2000),
67 REGULATOR_LINEAR_RANGE(220000, 0x6e, 0x7f, 0),
70 static struct linear_range voutS1_ocw_ranges_internal[] = {
71 REGULATOR_LINEAR_RANGE(200000, 0x01, 0x04, 0),
72 REGULATOR_LINEAR_RANGE(250000, 0x05, 0x18, 50000),
73 REGULATOR_LINEAR_RANGE(1200000, 0x19, 0x3f, 0),
76 static struct linear_range voutS1_ocw_ranges[] = {
77 REGULATOR_LINEAR_RANGE(50000, 0x01, 0x04, 0),
78 REGULATOR_LINEAR_RANGE(60000, 0x05, 0x18, 10000),
79 REGULATOR_LINEAR_RANGE(250000, 0x19, 0x3f, 0),
82 static struct linear_range voutS1_ocp_ranges_internal[] = {
83 REGULATOR_LINEAR_RANGE(300000, 0x01, 0x06, 0),
84 REGULATOR_LINEAR_RANGE(350000, 0x7, 0x1b, 50000),
85 REGULATOR_LINEAR_RANGE(1350000, 0x1c, 0x3f, 0),
88 static struct linear_range voutS1_ocp_ranges[] = {
89 REGULATOR_LINEAR_RANGE(70000, 0x01, 0x06, 0),
90 REGULATOR_LINEAR_RANGE(80000, 0x7, 0x1b, 10000),
91 REGULATOR_LINEAR_RANGE(280000, 0x1c, 0x3f, 0),
94 struct bd957x_regulator_data {
95 struct regulator_desc desc;
97 struct regulator_dev *rdev;
104 const struct linear_range *xvd_ranges;
107 unsigned int ovd_reg;
108 unsigned int uvd_reg;
109 unsigned int xvd_mask;
110 unsigned int ocp_reg;
111 unsigned int ocp_mask;
112 unsigned int ocw_reg;
113 unsigned int ocw_mask;
114 unsigned int ocw_rfet;
117 #define BD9576_NUM_REGULATORS 6
118 #define BD9576_NUM_OVD_REGULATORS 5
121 struct bd957x_regulator_data regulator_data[BD9576_NUM_REGULATORS];
122 struct regmap *regmap;
123 struct delayed_work therm_irq_suppress;
124 struct delayed_work ovd_irq_suppress;
125 struct delayed_work uvd_irq_suppress;
126 unsigned int therm_irq;
127 unsigned int ovd_irq;
128 unsigned int uvd_irq;
130 int regulator_global_err;
133 static int bd957x_vout34_list_voltage(struct regulator_dev *rdev,
134 unsigned int selector)
136 const struct regulator_desc *desc = rdev->desc;
137 int multiplier = selector & desc->vsel_mask & 0x7f;
140 /* VOUT3 and 4 has 10mV step */
141 tune = multiplier * 10000;
143 if (!(selector & 0x80))
144 return desc->fixed_uV - tune;
146 return desc->fixed_uV + tune;
149 static int bd957x_list_voltage(struct regulator_dev *rdev,
150 unsigned int selector)
152 const struct regulator_desc *desc = rdev->desc;
153 int index = selector & desc->vsel_mask & 0x7f;
155 if (!(selector & 0x80))
156 index += desc->n_voltages/2;
158 if (index >= desc->n_voltages)
161 return desc->volt_table[index];
164 static void bd9576_fill_ovd_flags(struct bd957x_regulator_data *data,
168 data->ovd_notif = REGULATOR_EVENT_OVER_VOLTAGE_WARN;
169 data->ovd_err = REGULATOR_ERROR_OVER_VOLTAGE_WARN;
171 data->ovd_notif = REGULATOR_EVENT_REGULATION_OUT;
172 data->ovd_err = REGULATOR_ERROR_REGULATION_OUT;
176 static void bd9576_fill_ocp_flags(struct bd957x_regulator_data *data,
180 data->uvd_notif = REGULATOR_EVENT_OVER_CURRENT_WARN;
181 data->uvd_err = REGULATOR_ERROR_OVER_CURRENT_WARN;
183 data->uvd_notif = REGULATOR_EVENT_OVER_CURRENT;
184 data->uvd_err = REGULATOR_ERROR_OVER_CURRENT;
188 static void bd9576_fill_uvd_flags(struct bd957x_regulator_data *data,
192 data->uvd_notif = REGULATOR_EVENT_UNDER_VOLTAGE_WARN;
193 data->uvd_err = REGULATOR_ERROR_UNDER_VOLTAGE_WARN;
195 data->uvd_notif = REGULATOR_EVENT_UNDER_VOLTAGE;
196 data->uvd_err = REGULATOR_ERROR_UNDER_VOLTAGE;
200 static void bd9576_fill_temp_flags(struct bd957x_regulator_data *data,
201 bool enable, bool warn)
204 data->temp_notif = 0;
207 data->temp_notif = REGULATOR_EVENT_OVER_TEMP_WARN;
208 data->temp_err = REGULATOR_ERROR_OVER_TEMP_WARN;
210 data->temp_notif = REGULATOR_EVENT_OVER_TEMP;
211 data->temp_err = REGULATOR_ERROR_OVER_TEMP;
215 static int bd9576_set_limit(const struct linear_range *r, int num_ranges,
216 struct regmap *regmap, int reg, int mask, int lim)
224 ret = linear_range_get_selector_low_array(r, num_ranges,
230 dev_warn(regmap_get_device(regmap),
231 "limit %d out of range. Setting lower\n",
235 return regmap_update_bits(regmap, reg, mask, sel);
238 static bool check_ocp_flag_mismatch(struct regulator_dev *rdev, int severity,
239 struct bd957x_regulator_data *r)
241 if ((severity == REGULATOR_SEVERITY_ERR &&
242 r->uvd_notif != REGULATOR_EVENT_OVER_CURRENT) ||
243 (severity == REGULATOR_SEVERITY_WARN &&
244 r->uvd_notif != REGULATOR_EVENT_OVER_CURRENT_WARN)) {
245 dev_warn(rdev_get_dev(rdev),
246 "Can't support both OCP WARN and ERR\n");
247 /* Do not overwrite ERR config with WARN */
248 if (severity == REGULATOR_SEVERITY_WARN)
251 bd9576_fill_ocp_flags(r, 0);
257 static bool check_uvd_flag_mismatch(struct regulator_dev *rdev, int severity,
258 struct bd957x_regulator_data *r)
260 if ((severity == REGULATOR_SEVERITY_ERR &&
261 r->uvd_notif != REGULATOR_EVENT_UNDER_VOLTAGE) ||
262 (severity == REGULATOR_SEVERITY_WARN &&
263 r->uvd_notif != REGULATOR_EVENT_UNDER_VOLTAGE_WARN)) {
264 dev_warn(rdev_get_dev(rdev),
265 "Can't support both UVD WARN and ERR\n");
266 if (severity == REGULATOR_SEVERITY_WARN)
269 bd9576_fill_uvd_flags(r, 0);
275 static bool check_ovd_flag_mismatch(struct regulator_dev *rdev, int severity,
276 struct bd957x_regulator_data *r)
278 if ((severity == REGULATOR_SEVERITY_ERR &&
279 r->ovd_notif != REGULATOR_EVENT_REGULATION_OUT) ||
280 (severity == REGULATOR_SEVERITY_WARN &&
281 r->ovd_notif != REGULATOR_EVENT_OVER_VOLTAGE_WARN)) {
282 dev_warn(rdev_get_dev(rdev),
283 "Can't support both OVD WARN and ERR\n");
284 if (severity == REGULATOR_SEVERITY_WARN)
287 bd9576_fill_ovd_flags(r, 0);
293 static bool check_temp_flag_mismatch(struct regulator_dev *rdev, int severity,
294 struct bd957x_regulator_data *r)
296 if ((severity == REGULATOR_SEVERITY_ERR &&
297 r->temp_notif != REGULATOR_EVENT_OVER_TEMP) ||
298 (severity == REGULATOR_SEVERITY_WARN &&
299 r->temp_notif != REGULATOR_EVENT_OVER_TEMP_WARN)) {
300 dev_warn(rdev_get_dev(rdev),
301 "Can't support both thermal WARN and ERR\n");
302 if (severity == REGULATOR_SEVERITY_WARN)
309 static int bd9576_set_ocp(struct regulator_dev *rdev, int lim_uA, int severity,
312 struct bd957x_data *d;
313 struct bd957x_regulator_data *r;
316 const struct linear_range *range;
319 if ((lim_uA && !enable) || (!lim_uA && enable))
322 r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
323 if (!r->oc_supported)
326 d = rdev_get_drvdata(rdev);
328 if (severity == REGULATOR_SEVERITY_PROT) {
332 range = voutS1_ocp_ranges;
333 num_ranges = ARRAY_SIZE(voutS1_ocp_ranges);
334 rfet = r->ocw_rfet / 1000;
336 range = voutS1_ocp_ranges_internal;
337 num_ranges = ARRAY_SIZE(voutS1_ocp_ranges_internal);
338 /* Internal values are already micro-amperes */
346 range = voutS1_ocw_ranges;
347 num_ranges = ARRAY_SIZE(voutS1_ocw_ranges);
348 rfet = r->ocw_rfet / 1000;
350 range = voutS1_ocw_ranges_internal;
351 num_ranges = ARRAY_SIZE(voutS1_ocw_ranges_internal);
352 /* Internal values are already micro-amperes */
356 /* We abuse uvd fields for OCW on VoutS1 */
359 * If both warning and error are requested, prioritize
360 * ERROR configuration
362 if (check_ocp_flag_mismatch(rdev, severity, r))
365 bool warn = severity == REGULATOR_SEVERITY_WARN;
367 bd9576_fill_ocp_flags(r, warn);
372 * limits are given in uA, rfet is mOhm
373 * Divide lim_uA by 1000 to get Vfet in uV.
374 * (We expect both Rfet and limit uA to be magnitude of hundreds of
375 * milli Amperes & milli Ohms => we should still have decent accuracy)
377 Vfet = lim_uA/1000 * rfet;
379 return bd9576_set_limit(range, num_ranges, d->regmap,
383 static int bd9576_set_uvp(struct regulator_dev *rdev, int lim_uV, int severity,
386 struct bd957x_data *d;
387 struct bd957x_regulator_data *r;
390 if (severity == REGULATOR_SEVERITY_PROT) {
391 if (!enable || lim_uV)
397 * BD9576 has enable control as a special value in limit reg. Can't
398 * set limit but keep feature disabled or enable W/O given limit.
400 if ((lim_uV && !enable) || (!lim_uV && enable))
403 r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
404 d = rdev_get_drvdata(rdev);
409 * Check that there is no mismatch for what the detection IRQs are to
413 if (check_uvd_flag_mismatch(rdev, severity, r))
416 bd9576_fill_uvd_flags(r, severity == REGULATOR_SEVERITY_WARN);
419 return bd9576_set_limit(r->xvd_ranges, r->num_xvd_ranges, d->regmap,
423 static int bd9576_set_ovp(struct regulator_dev *rdev, int lim_uV, int severity,
426 struct bd957x_data *d;
427 struct bd957x_regulator_data *r;
430 if (severity == REGULATOR_SEVERITY_PROT) {
431 if (!enable || lim_uV)
437 * BD9576 has enable control as a special value in limit reg. Can't
438 * set limit but keep feature disabled or enable W/O given limit.
440 if ((lim_uV && !enable) || (!lim_uV && enable))
443 r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
444 d = rdev_get_drvdata(rdev);
449 * Check that there is no mismatch for what the detection IRQs are to
453 if (check_ovd_flag_mismatch(rdev, severity, r))
456 bd9576_fill_ovd_flags(r, severity == REGULATOR_SEVERITY_WARN);
459 return bd9576_set_limit(r->xvd_ranges, r->num_xvd_ranges, d->regmap,
464 static int bd9576_set_tw(struct regulator_dev *rdev, int lim, int severity,
467 struct bd957x_data *d;
468 struct bd957x_regulator_data *r;
472 * BD9576MUF has fixed temperature limits
473 * The detection can only be enabled/disabled
478 /* Protection can't be disabled */
479 if (severity == REGULATOR_SEVERITY_PROT) {
486 r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
487 d = rdev_get_drvdata(rdev);
490 * Check that there is no mismatch for what the detection IRQs are to
494 if (check_temp_flag_mismatch(rdev, severity, r))
497 bd9576_fill_temp_flags(r, enable, severity == REGULATOR_SEVERITY_WARN);
500 return regmap_update_bits(d->regmap, BD957X_REG_INT_THERM_MASK,
501 BD9576_THERM_IRQ_MASK_TW, 0);
504 * If any of the regulators is interested in thermal warning we keep IRQ
507 for (i = 0; i < BD9576_NUM_REGULATORS; i++)
508 if (d->regulator_data[i].temp_notif)
511 return regmap_update_bits(d->regmap, BD957X_REG_INT_THERM_MASK,
512 BD9576_THERM_IRQ_MASK_TW,
513 BD9576_THERM_IRQ_MASK_TW);
516 static const struct regulator_ops bd9573_vout34_ops = {
517 .is_enabled = regulator_is_enabled_regmap,
518 .list_voltage = bd957x_vout34_list_voltage,
519 .get_voltage_sel = regulator_get_voltage_sel_regmap,
522 static const struct regulator_ops bd9576_vout34_ops = {
523 .is_enabled = regulator_is_enabled_regmap,
524 .list_voltage = bd957x_vout34_list_voltage,
525 .get_voltage_sel = regulator_get_voltage_sel_regmap,
526 .set_over_voltage_protection = bd9576_set_ovp,
527 .set_under_voltage_protection = bd9576_set_uvp,
528 .set_thermal_protection = bd9576_set_tw,
531 static const struct regulator_ops bd9573_vouts1_regulator_ops = {
532 .is_enabled = regulator_is_enabled_regmap,
535 static const struct regulator_ops bd9576_vouts1_regulator_ops = {
536 .is_enabled = regulator_is_enabled_regmap,
537 .set_over_current_protection = bd9576_set_ocp,
540 static const struct regulator_ops bd9573_ops = {
541 .is_enabled = regulator_is_enabled_regmap,
542 .list_voltage = bd957x_list_voltage,
543 .get_voltage_sel = regulator_get_voltage_sel_regmap,
546 static const struct regulator_ops bd9576_ops = {
547 .is_enabled = regulator_is_enabled_regmap,
548 .list_voltage = bd957x_list_voltage,
549 .get_voltage_sel = regulator_get_voltage_sel_regmap,
550 .set_over_voltage_protection = bd9576_set_ovp,
551 .set_under_voltage_protection = bd9576_set_uvp,
552 .set_thermal_protection = bd9576_set_tw,
555 static const struct regulator_ops *bd9573_ops_arr[] = {
556 [BD957X_VD50] = &bd9573_ops,
557 [BD957X_VD18] = &bd9573_ops,
558 [BD957X_VDDDR] = &bd9573_vout34_ops,
559 [BD957X_VD10] = &bd9573_vout34_ops,
560 [BD957X_VOUTL1] = &bd9573_ops,
561 [BD957X_VOUTS1] = &bd9573_vouts1_regulator_ops,
564 static const struct regulator_ops *bd9576_ops_arr[] = {
565 [BD957X_VD50] = &bd9576_ops,
566 [BD957X_VD18] = &bd9576_ops,
567 [BD957X_VDDDR] = &bd9576_vout34_ops,
568 [BD957X_VD10] = &bd9576_vout34_ops,
569 [BD957X_VOUTL1] = &bd9576_ops,
570 [BD957X_VOUTS1] = &bd9576_vouts1_regulator_ops,
573 static int vouts1_get_fet_res(struct device_node *np,
574 const struct regulator_desc *desc,
575 struct regulator_config *cfg)
577 struct bd957x_regulator_data *data;
581 data = container_of(desc, struct bd957x_regulator_data, desc);
583 ret = of_property_read_u32(np, "rohm,ocw-fet-ron-micro-ohms", &uohms);
590 data->ocw_rfet = uohms;
594 static struct bd957x_data bd957x_regulators = {
599 .of_match = of_match_ptr("regulator-vd50"),
600 .regulators_node = of_match_ptr("regulators"),
602 .type = REGULATOR_VOLTAGE,
603 .volt_table = &vout1_volt_table[0],
604 .n_voltages = ARRAY_SIZE(vout1_volt_table),
605 .vsel_reg = BD957X_REG_VOUT1_TUNE,
606 .vsel_mask = BD957X_MASK_VOUT1_TUNE,
607 .enable_reg = BD957X_REG_POW_TRIGGER1,
608 .enable_mask = BD957X_REGULATOR_EN_MASK,
609 .enable_val = BD957X_REGULATOR_DIS_VAL,
610 .enable_is_inverted = true,
611 .owner = THIS_MODULE,
613 .xvd_ranges = vout1_xvd_ranges,
614 .num_xvd_ranges = ARRAY_SIZE(vout1_xvd_ranges),
615 .ovd_reg = BD9576_REG_VOUT1_OVD,
616 .uvd_reg = BD9576_REG_VOUT1_UVD,
617 .xvd_mask = BD9576_MASK_XVD,
622 .of_match = of_match_ptr("regulator-vd18"),
623 .regulators_node = of_match_ptr("regulators"),
625 .type = REGULATOR_VOLTAGE,
626 .volt_table = &vout2_volt_table[0],
627 .n_voltages = ARRAY_SIZE(vout2_volt_table),
628 .vsel_reg = BD957X_REG_VOUT2_TUNE,
629 .vsel_mask = BD957X_MASK_VOUT2_TUNE,
630 .enable_reg = BD957X_REG_POW_TRIGGER2,
631 .enable_mask = BD957X_REGULATOR_EN_MASK,
632 .enable_val = BD957X_REGULATOR_DIS_VAL,
633 .enable_is_inverted = true,
634 .owner = THIS_MODULE,
636 .xvd_ranges = vout234_xvd_ranges,
637 .num_xvd_ranges = ARRAY_SIZE(vout234_xvd_ranges),
638 .ovd_reg = BD9576_REG_VOUT2_OVD,
639 .uvd_reg = BD9576_REG_VOUT2_UVD,
640 .xvd_mask = BD9576_MASK_XVD,
645 .of_match = of_match_ptr("regulator-vdddr"),
646 .regulators_node = of_match_ptr("regulators"),
648 .type = REGULATOR_VOLTAGE,
649 .n_voltages = BD957X_VOUTS34_NUM_VOLT,
650 .vsel_reg = BD957X_REG_VOUT3_TUNE,
651 .vsel_mask = BD957X_MASK_VOUT3_TUNE,
652 .enable_reg = BD957X_REG_POW_TRIGGER3,
653 .enable_mask = BD957X_REGULATOR_EN_MASK,
654 .enable_val = BD957X_REGULATOR_DIS_VAL,
655 .enable_is_inverted = true,
656 .owner = THIS_MODULE,
658 .ovd_reg = BD9576_REG_VOUT3_OVD,
659 .uvd_reg = BD9576_REG_VOUT3_UVD,
660 .xvd_mask = BD9576_MASK_XVD,
661 .xvd_ranges = vout234_xvd_ranges,
662 .num_xvd_ranges = ARRAY_SIZE(vout234_xvd_ranges),
667 .of_match = of_match_ptr("regulator-vd10"),
668 .regulators_node = of_match_ptr("regulators"),
670 .type = REGULATOR_VOLTAGE,
671 .fixed_uV = BD957X_VOUTS4_BASE_VOLT,
672 .n_voltages = BD957X_VOUTS34_NUM_VOLT,
673 .vsel_reg = BD957X_REG_VOUT4_TUNE,
674 .vsel_mask = BD957X_MASK_VOUT4_TUNE,
675 .enable_reg = BD957X_REG_POW_TRIGGER4,
676 .enable_mask = BD957X_REGULATOR_EN_MASK,
677 .enable_val = BD957X_REGULATOR_DIS_VAL,
678 .enable_is_inverted = true,
679 .owner = THIS_MODULE,
681 .xvd_ranges = vout234_xvd_ranges,
682 .num_xvd_ranges = ARRAY_SIZE(vout234_xvd_ranges),
683 .ovd_reg = BD9576_REG_VOUT4_OVD,
684 .uvd_reg = BD9576_REG_VOUT4_UVD,
685 .xvd_mask = BD9576_MASK_XVD,
690 .of_match = of_match_ptr("regulator-voutl1"),
691 .regulators_node = of_match_ptr("regulators"),
693 .type = REGULATOR_VOLTAGE,
694 .volt_table = &voutl1_volt_table[0],
695 .n_voltages = ARRAY_SIZE(voutl1_volt_table),
696 .vsel_reg = BD957X_REG_VOUTL1_TUNE,
697 .vsel_mask = BD957X_MASK_VOUTL1_TUNE,
698 .enable_reg = BD957X_REG_POW_TRIGGERL1,
699 .enable_mask = BD957X_REGULATOR_EN_MASK,
700 .enable_val = BD957X_REGULATOR_DIS_VAL,
701 .enable_is_inverted = true,
702 .owner = THIS_MODULE,
704 .xvd_ranges = voutL1_xvd_ranges,
705 .num_xvd_ranges = ARRAY_SIZE(voutL1_xvd_ranges),
706 .ovd_reg = BD9576_REG_VOUTL1_OVD,
707 .uvd_reg = BD9576_REG_VOUTL1_UVD,
708 .xvd_mask = BD9576_MASK_XVD,
713 .of_match = of_match_ptr("regulator-vouts1"),
714 .regulators_node = of_match_ptr("regulators"),
716 .type = REGULATOR_VOLTAGE,
718 .fixed_uV = BD957X_VOUTS1_VOLT,
719 .enable_reg = BD957X_REG_POW_TRIGGERS1,
720 .enable_mask = BD957X_REGULATOR_EN_MASK,
721 .enable_val = BD957X_REGULATOR_DIS_VAL,
722 .enable_is_inverted = true,
723 .owner = THIS_MODULE,
724 .of_parse_cb = vouts1_get_fet_res,
726 .oc_supported = true,
727 .ocw_reg = BD9576_REG_VOUT1S_OCW,
728 .ocw_mask = BD9576_MASK_VOUT1S_OCW,
729 .ocp_reg = BD9576_REG_VOUT1S_OCP,
730 .ocp_mask = BD9576_MASK_VOUT1S_OCP,
735 static int bd9576_renable(struct regulator_irq_data *rid, int reg, int mask)
738 struct bd957x_data *d = (struct bd957x_data *)rid->data;
740 ret = regmap_read(d->regmap, reg, &val);
742 return REGULATOR_FAILED_RETRY;
744 if (rid->opaque && rid->opaque == (val & mask)) {
746 * It seems we stil have same status. Ack and return
747 * information that we are still out of limits and core
748 * should not enable IRQ
750 regmap_write(d->regmap, reg, mask & val);
751 return REGULATOR_ERROR_ON;
755 * Status was changed. Either prolem was solved or we have new issues.
756 * Let's re-enable IRQs and be prepared to report problems again
758 return REGULATOR_ERROR_CLEARED;
761 static int bd9576_uvd_renable(struct regulator_irq_data *rid)
763 return bd9576_renable(rid, BD957X_REG_INT_UVD_STAT, UVD_IRQ_VALID_MASK);
766 static int bd9576_ovd_renable(struct regulator_irq_data *rid)
768 return bd9576_renable(rid, BD957X_REG_INT_OVD_STAT, OVD_IRQ_VALID_MASK);
771 static int bd9576_temp_renable(struct regulator_irq_data *rid)
773 return bd9576_renable(rid, BD957X_REG_INT_THERM_STAT,
774 BD9576_THERM_IRQ_MASK_TW);
777 static int bd9576_uvd_handler(int irq, struct regulator_irq_data *rid,
778 unsigned long *dev_mask)
781 struct bd957x_data *d = (struct bd957x_data *)rid->data;
783 ret = regmap_read(d->regmap, BD957X_REG_INT_UVD_STAT, &val);
785 return REGULATOR_FAILED_RETRY;
789 rid->opaque = val & UVD_IRQ_VALID_MASK;
792 * Go through the set status bits and report either error or warning
793 * to the notifier depending on what was flagged in DT
795 *dev_mask = val & BD9576_xVD_IRQ_MASK_VOUT1TO4;
796 /* There is 1 bit gap in register after Vout1 .. Vout4 statuses */
797 *dev_mask |= ((val & BD9576_xVD_IRQ_MASK_VOUTL1) >> 1);
799 * We (ab)use the uvd for OCW notification. DT parsing should
800 * have added correct OCW flag to uvd_notif and uvd_err for S1
802 *dev_mask |= ((val & BD9576_UVD_IRQ_MASK_VOUTS1_OCW) >> 1);
804 for_each_set_bit(i, dev_mask, 6) {
805 struct bd957x_regulator_data *rdata;
806 struct regulator_err_state *stat;
808 rdata = &d->regulator_data[i];
809 stat = &rid->states[i];
811 stat->notifs = rdata->uvd_notif;
812 stat->errors = rdata->uvd_err;
815 ret = regmap_write(d->regmap, BD957X_REG_INT_UVD_STAT,
816 UVD_IRQ_VALID_MASK & val);
821 static int bd9576_ovd_handler(int irq, struct regulator_irq_data *rid,
822 unsigned long *dev_mask)
825 struct bd957x_data *d = (struct bd957x_data *)rid->data;
827 ret = regmap_read(d->regmap, BD957X_REG_INT_OVD_STAT, &val);
829 return REGULATOR_FAILED_RETRY;
831 rid->opaque = val & OVD_IRQ_VALID_MASK;
834 if (!(val & OVD_IRQ_VALID_MASK))
837 *dev_mask = val & BD9576_xVD_IRQ_MASK_VOUT1TO4;
838 /* There is 1 bit gap in register after Vout1 .. Vout4 statuses */
839 *dev_mask |= ((val & BD9576_xVD_IRQ_MASK_VOUTL1) >> 1);
841 for_each_set_bit(i, dev_mask, 5) {
842 struct bd957x_regulator_data *rdata;
843 struct regulator_err_state *stat;
845 rdata = &d->regulator_data[i];
846 stat = &rid->states[i];
848 stat->notifs = rdata->ovd_notif;
849 stat->errors = rdata->ovd_err;
852 /* Clear the sub-IRQ status */
853 regmap_write(d->regmap, BD957X_REG_INT_OVD_STAT,
854 OVD_IRQ_VALID_MASK & val);
859 #define BD9576_DEV_MASK_ALL_REGULATORS 0x3F
861 static int bd9576_thermal_handler(int irq, struct regulator_irq_data *rid,
862 unsigned long *dev_mask)
865 struct bd957x_data *d = (struct bd957x_data *)rid->data;
867 ret = regmap_read(d->regmap, BD957X_REG_INT_THERM_STAT, &val);
869 return REGULATOR_FAILED_RETRY;
871 if (!(val & BD9576_THERM_IRQ_MASK_TW)) {
876 *dev_mask = BD9576_DEV_MASK_ALL_REGULATORS;
878 for (i = 0; i < BD9576_NUM_REGULATORS; i++) {
879 struct bd957x_regulator_data *rdata;
880 struct regulator_err_state *stat;
882 rdata = &d->regulator_data[i];
883 stat = &rid->states[i];
885 stat->notifs = rdata->temp_notif;
886 stat->errors = rdata->temp_err;
889 /* Clear the sub-IRQ status */
890 regmap_write(d->regmap, BD957X_REG_INT_THERM_STAT,
891 BD9576_THERM_IRQ_MASK_TW);
896 static int bd957x_probe(struct platform_device *pdev)
899 unsigned int num_reg_data;
900 bool vout_mode, ddr_sel, may_have_irqs = false;
901 struct regmap *regmap;
902 struct bd957x_data *ic_data;
903 struct regulator_config config = { 0 };
904 /* All regulators are related to UVD and thermal IRQs... */
905 struct regulator_dev *rdevs[BD9576_NUM_REGULATORS];
906 /* ...But VoutS1 is not flagged by OVD IRQ */
907 struct regulator_dev *ovd_devs[BD9576_NUM_OVD_REGULATORS];
908 static const struct regulator_irq_desc bd9576_notif_uvd = {
909 .name = "bd9576-uvd",
911 .map_event = bd9576_uvd_handler,
912 .renable = bd9576_uvd_renable,
913 .data = &bd957x_regulators,
915 static const struct regulator_irq_desc bd9576_notif_ovd = {
916 .name = "bd9576-ovd",
918 .map_event = bd9576_ovd_handler,
919 .renable = bd9576_ovd_renable,
920 .data = &bd957x_regulators,
922 static const struct regulator_irq_desc bd9576_notif_temp = {
923 .name = "bd9576-temp",
925 .map_event = bd9576_thermal_handler,
926 .renable = bd9576_temp_renable,
927 .data = &bd957x_regulators,
929 enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
931 num_reg_data = ARRAY_SIZE(bd957x_regulators.regulator_data);
933 ic_data = &bd957x_regulators;
935 regmap = dev_get_regmap(pdev->dev.parent, NULL);
937 dev_err(&pdev->dev, "No regmap\n");
941 ic_data->regmap = regmap;
942 vout_mode = of_property_read_bool(pdev->dev.parent->of_node,
943 "rohm,vout1-en-low");
945 struct gpio_desc *en;
947 dev_dbg(&pdev->dev, "GPIO controlled mode\n");
949 /* VOUT1 enable state judged by VOUT1_EN pin */
950 /* See if we have GPIO defined */
951 en = devm_gpiod_get_from_of_node(&pdev->dev,
952 pdev->dev.parent->of_node,
953 "rohm,vout1-en-gpios", 0,
954 GPIOD_OUT_LOW, "vout1-en");
956 /* VOUT1_OPS gpio ctrl */
958 * Regulator core prioritizes the ena_gpio over
959 * enable/disable/is_enabled callbacks so no need to
960 * clear them. We can still use same ops
962 config.ena_gpiod = en;
965 * In theory it is possible someone wants to set
966 * vout1-en LOW during OTP loading and set VOUT1 to be
967 * controlled by GPIO - but control the GPIO from some
968 * where else than this driver. For that to work we
969 * should unset the is_enabled callback here.
971 * I believe such case where rohm,vout1-en-low is set
972 * and vout1-en-gpios is not is likely to be a
973 * misconfiguration. So let's just err out for now.
976 "Failed to get VOUT1 control GPIO\n");
982 * If more than one PMIC needs to be controlled by same processor then
983 * allocate the regulator data array here and use bd9576_regulators as
984 * template. At the moment I see no such use-case so I spare some
985 * bytes and use bd9576_regulators directly for non-constant configs
986 * like DDR voltage selection.
988 platform_set_drvdata(pdev, ic_data);
989 ddr_sel = of_property_read_bool(pdev->dev.parent->of_node,
992 ic_data->regulator_data[2].desc.fixed_uV = 1350000;
994 ic_data->regulator_data[2].desc.fixed_uV = 1500000;
997 case ROHM_CHIP_TYPE_BD9576:
998 may_have_irqs = true;
999 dev_dbg(&pdev->dev, "Found BD9576MUF\n");
1001 case ROHM_CHIP_TYPE_BD9573:
1002 dev_dbg(&pdev->dev, "Found BD9573MUF\n");
1005 dev_err(&pdev->dev, "Unsupported chip type\n");
1009 for (i = 0; i < num_reg_data; i++) {
1010 struct regulator_desc *d;
1012 d = &ic_data->regulator_data[i].desc;
1015 if (may_have_irqs) {
1016 if (d->id >= ARRAY_SIZE(bd9576_ops_arr))
1019 d->ops = bd9576_ops_arr[d->id];
1021 if (d->id >= ARRAY_SIZE(bd9573_ops_arr))
1024 d->ops = bd9573_ops_arr[d->id];
1028 config.dev = pdev->dev.parent;
1029 config.regmap = regmap;
1030 config.driver_data = ic_data;
1032 for (i = 0; i < num_reg_data; i++) {
1034 struct bd957x_regulator_data *r = &ic_data->regulator_data[i];
1035 const struct regulator_desc *desc = &r->desc;
1037 r->rdev = devm_regulator_register(&pdev->dev, desc,
1039 if (IS_ERR(r->rdev)) {
1041 "failed to register %s regulator\n",
1043 return PTR_ERR(r->rdev);
1046 * Clear the VOUT1 GPIO setting - rest of the regulators do not
1047 * support GPIO control
1049 config.ena_gpiod = NULL;
1055 if (i < BD957X_VOUTS1)
1056 ovd_devs[i] = r->rdev;
1058 if (may_have_irqs) {
1061 * We can add both the possible error and warning flags here
1062 * because the core uses these only for status clearing and
1063 * if we use warnings - errors are always clear and the other
1064 * way around. We can also add CURRENT flag for all regulators
1065 * because it is never set if it is not supported. Same applies
1066 * to setting UVD for VoutS1 - it is not accidentally cleared
1067 * as it is never set.
1069 int uvd_errs = REGULATOR_ERROR_UNDER_VOLTAGE |
1070 REGULATOR_ERROR_UNDER_VOLTAGE_WARN |
1071 REGULATOR_ERROR_OVER_CURRENT |
1072 REGULATOR_ERROR_OVER_CURRENT_WARN;
1073 int ovd_errs = REGULATOR_ERROR_OVER_VOLTAGE_WARN |
1074 REGULATOR_ERROR_REGULATION_OUT;
1075 int temp_errs = REGULATOR_ERROR_OVER_TEMP |
1076 REGULATOR_ERROR_OVER_TEMP_WARN;
1079 irq = platform_get_irq_byname(pdev, "bd9576-uvd");
1081 /* Register notifiers - can fail if IRQ is not given */
1082 ret = devm_regulator_irq_helper(&pdev->dev, &bd9576_notif_uvd,
1083 irq, 0, uvd_errs, NULL,
1085 BD9576_NUM_REGULATORS);
1087 if (PTR_ERR(ret) == -EPROBE_DEFER)
1088 return -EPROBE_DEFER;
1090 dev_warn(&pdev->dev, "UVD disabled %pe\n", ret);
1093 irq = platform_get_irq_byname(pdev, "bd9576-ovd");
1095 ret = devm_regulator_irq_helper(&pdev->dev, &bd9576_notif_ovd,
1096 irq, 0, ovd_errs, NULL,
1098 BD9576_NUM_OVD_REGULATORS);
1100 if (PTR_ERR(ret) == -EPROBE_DEFER)
1101 return -EPROBE_DEFER;
1103 dev_warn(&pdev->dev, "OVD disabled %pe\n", ret);
1105 irq = platform_get_irq_byname(pdev, "bd9576-temp");
1107 ret = devm_regulator_irq_helper(&pdev->dev, &bd9576_notif_temp,
1108 irq, 0, temp_errs, NULL,
1110 BD9576_NUM_REGULATORS);
1112 if (PTR_ERR(ret) == -EPROBE_DEFER)
1113 return -EPROBE_DEFER;
1115 dev_warn(&pdev->dev, "Thermal warning disabled %pe\n",
1122 static const struct platform_device_id bd957x_pmic_id[] = {
1123 { "bd9573-regulator", ROHM_CHIP_TYPE_BD9573 },
1124 { "bd9576-regulator", ROHM_CHIP_TYPE_BD9576 },
1127 MODULE_DEVICE_TABLE(platform, bd957x_pmic_id);
1129 static struct platform_driver bd957x_regulator = {
1131 .name = "bd957x-pmic",
1133 .probe = bd957x_probe,
1134 .id_table = bd957x_pmic_id,
1137 module_platform_driver(bd957x_regulator);
1139 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1140 MODULE_DESCRIPTION("ROHM BD9576/BD9573 voltage regulator driver");
1141 MODULE_LICENSE("GPL");
1142 MODULE_ALIAS("platform:bd957x-pmic");