Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (C) 2020 ROHM Semiconductors
0003 // ROHM BD9576MUF/BD9573MUF regulator driver
0004 
0005 #include <linux/err.h>
0006 #include <linux/gpio/consumer.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/jiffies.h>
0009 #include <linux/kernel.h>
0010 #include <linux/mfd/rohm-bd957x.h>
0011 #include <linux/mfd/rohm-generic.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regulator/driver.h>
0016 #include <linux/regulator/machine.h>
0017 #include <linux/regulator/of_regulator.h>
0018 #include <linux/slab.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/workqueue.h>
0021 
0022 #define BD957X_VOUTS1_VOLT  3300000
0023 #define BD957X_VOUTS4_BASE_VOLT 1030000
0024 #define BD957X_VOUTS34_NUM_VOLT 32
0025 
0026 #define BD9576_THERM_IRQ_MASK_TW    BIT(5)
0027 #define BD9576_xVD_IRQ_MASK_VOUTL1  BIT(5)
0028 #define BD9576_UVD_IRQ_MASK_VOUTS1_OCW  BIT(6)
0029 #define BD9576_xVD_IRQ_MASK_VOUT1TO4    0x0F
0030 
0031 static const unsigned int vout1_volt_table[] = {
0032     5000000, 4900000, 4800000, 4700000, 4600000,
0033     4500000, 4500000, 4500000, 5000000, 5100000,
0034     5200000, 5300000, 5400000, 5500000, 5500000,
0035     5500000
0036 };
0037 
0038 static const unsigned int vout2_volt_table[] = {
0039     1800000, 1780000, 1760000, 1740000, 1720000,
0040     1700000, 1680000, 1660000, 1800000, 1820000,
0041     1840000, 1860000, 1880000, 1900000, 1920000,
0042     1940000
0043 };
0044 
0045 static const unsigned int voutl1_volt_table[] = {
0046     2500000, 2540000, 2580000, 2620000, 2660000,
0047     2700000, 2740000, 2780000, 2500000, 2460000,
0048     2420000, 2380000, 2340000, 2300000, 2260000,
0049     2220000
0050 };
0051 
0052 static const struct linear_range vout1_xvd_ranges[] = {
0053     REGULATOR_LINEAR_RANGE(225000, 0x01, 0x2b, 0),
0054     REGULATOR_LINEAR_RANGE(225000, 0x2c, 0x54, 5000),
0055     REGULATOR_LINEAR_RANGE(425000, 0x55, 0x7f, 0),
0056 };
0057 
0058 static const struct linear_range vout234_xvd_ranges[] = {
0059     REGULATOR_LINEAR_RANGE(17000, 0x01, 0x0f, 0),
0060     REGULATOR_LINEAR_RANGE(17000, 0x10, 0x6d, 1000),
0061     REGULATOR_LINEAR_RANGE(110000, 0x6e, 0x7f, 0),
0062 };
0063 
0064 static const struct linear_range voutL1_xvd_ranges[] = {
0065     REGULATOR_LINEAR_RANGE(34000, 0x01, 0x0f, 0),
0066     REGULATOR_LINEAR_RANGE(34000, 0x10, 0x6d, 2000),
0067     REGULATOR_LINEAR_RANGE(220000, 0x6e, 0x7f, 0),
0068 };
0069 
0070 static struct linear_range voutS1_ocw_ranges_internal[] = {
0071     REGULATOR_LINEAR_RANGE(200000, 0x01, 0x04, 0),
0072     REGULATOR_LINEAR_RANGE(250000, 0x05, 0x18, 50000),
0073     REGULATOR_LINEAR_RANGE(1200000, 0x19, 0x3f, 0),
0074 };
0075 
0076 static struct linear_range voutS1_ocw_ranges[] = {
0077     REGULATOR_LINEAR_RANGE(50000, 0x01, 0x04, 0),
0078     REGULATOR_LINEAR_RANGE(60000, 0x05, 0x18, 10000),
0079     REGULATOR_LINEAR_RANGE(250000, 0x19, 0x3f, 0),
0080 };
0081 
0082 static struct linear_range voutS1_ocp_ranges_internal[] = {
0083     REGULATOR_LINEAR_RANGE(300000, 0x01, 0x06, 0),
0084     REGULATOR_LINEAR_RANGE(350000, 0x7, 0x1b, 50000),
0085     REGULATOR_LINEAR_RANGE(1350000, 0x1c, 0x3f, 0),
0086 };
0087 
0088 static struct linear_range voutS1_ocp_ranges[] = {
0089     REGULATOR_LINEAR_RANGE(70000, 0x01, 0x06, 0),
0090     REGULATOR_LINEAR_RANGE(80000, 0x7, 0x1b, 10000),
0091     REGULATOR_LINEAR_RANGE(280000, 0x1c, 0x3f, 0),
0092 };
0093 
0094 struct bd957x_regulator_data {
0095     struct regulator_desc desc;
0096     int base_voltage;
0097     struct regulator_dev *rdev;
0098     int ovd_notif;
0099     int uvd_notif;
0100     int temp_notif;
0101     int ovd_err;
0102     int uvd_err;
0103     int temp_err;
0104     const struct linear_range *xvd_ranges;
0105     int num_xvd_ranges;
0106     bool oc_supported;
0107     unsigned int ovd_reg;
0108     unsigned int uvd_reg;
0109     unsigned int xvd_mask;
0110     unsigned int ocp_reg;
0111     unsigned int ocp_mask;
0112     unsigned int ocw_reg;
0113     unsigned int ocw_mask;
0114     unsigned int ocw_rfet;
0115 };
0116 
0117 #define BD9576_NUM_REGULATORS 6
0118 #define BD9576_NUM_OVD_REGULATORS 5
0119 
0120 struct bd957x_data {
0121     struct bd957x_regulator_data regulator_data[BD9576_NUM_REGULATORS];
0122     struct regmap *regmap;
0123     struct delayed_work therm_irq_suppress;
0124     struct delayed_work ovd_irq_suppress;
0125     struct delayed_work uvd_irq_suppress;
0126     unsigned int therm_irq;
0127     unsigned int ovd_irq;
0128     unsigned int uvd_irq;
0129     spinlock_t err_lock;
0130     int regulator_global_err;
0131 };
0132 
0133 static int bd957x_vout34_list_voltage(struct regulator_dev *rdev,
0134                       unsigned int selector)
0135 {
0136     const struct regulator_desc *desc = rdev->desc;
0137     int multiplier = selector & desc->vsel_mask & 0x7f;
0138     int tune;
0139 
0140     /* VOUT3 and 4 has 10mV step */
0141     tune = multiplier * 10000;
0142 
0143     if (!(selector & 0x80))
0144         return desc->fixed_uV - tune;
0145 
0146     return desc->fixed_uV + tune;
0147 }
0148 
0149 static int bd957x_list_voltage(struct regulator_dev *rdev,
0150                    unsigned int selector)
0151 {
0152     const struct regulator_desc *desc = rdev->desc;
0153     int index = selector & desc->vsel_mask & 0x7f;
0154 
0155     if (!(selector & 0x80))
0156         index += desc->n_voltages/2;
0157 
0158     if (index >= desc->n_voltages)
0159         return -EINVAL;
0160 
0161     return desc->volt_table[index];
0162 }
0163 
0164 static void bd9576_fill_ovd_flags(struct bd957x_regulator_data *data,
0165                   bool warn)
0166 {
0167     if (warn) {
0168         data->ovd_notif = REGULATOR_EVENT_OVER_VOLTAGE_WARN;
0169         data->ovd_err = REGULATOR_ERROR_OVER_VOLTAGE_WARN;
0170     } else {
0171         data->ovd_notif = REGULATOR_EVENT_REGULATION_OUT;
0172         data->ovd_err = REGULATOR_ERROR_REGULATION_OUT;
0173     }
0174 }
0175 
0176 static void bd9576_fill_ocp_flags(struct bd957x_regulator_data *data,
0177                   bool warn)
0178 {
0179     if (warn) {
0180         data->uvd_notif = REGULATOR_EVENT_OVER_CURRENT_WARN;
0181         data->uvd_err = REGULATOR_ERROR_OVER_CURRENT_WARN;
0182     } else {
0183         data->uvd_notif = REGULATOR_EVENT_OVER_CURRENT;
0184         data->uvd_err = REGULATOR_ERROR_OVER_CURRENT;
0185     }
0186 }
0187 
0188 static void bd9576_fill_uvd_flags(struct bd957x_regulator_data *data,
0189                   bool warn)
0190 {
0191     if (warn) {
0192         data->uvd_notif = REGULATOR_EVENT_UNDER_VOLTAGE_WARN;
0193         data->uvd_err = REGULATOR_ERROR_UNDER_VOLTAGE_WARN;
0194     } else {
0195         data->uvd_notif = REGULATOR_EVENT_UNDER_VOLTAGE;
0196         data->uvd_err = REGULATOR_ERROR_UNDER_VOLTAGE;
0197     }
0198 }
0199 
0200 static void bd9576_fill_temp_flags(struct bd957x_regulator_data *data,
0201                    bool enable, bool warn)
0202 {
0203     if (!enable) {
0204         data->temp_notif = 0;
0205         data->temp_err = 0;
0206     } else if (warn) {
0207         data->temp_notif = REGULATOR_EVENT_OVER_TEMP_WARN;
0208         data->temp_err = REGULATOR_ERROR_OVER_TEMP_WARN;
0209     } else {
0210         data->temp_notif = REGULATOR_EVENT_OVER_TEMP;
0211         data->temp_err = REGULATOR_ERROR_OVER_TEMP;
0212     }
0213 }
0214 
0215 static int bd9576_set_limit(const struct linear_range *r, int num_ranges,
0216                 struct regmap *regmap, int reg, int mask, int lim)
0217 {
0218     int ret;
0219     bool found;
0220     int sel = 0;
0221 
0222     if (lim) {
0223 
0224         ret = linear_range_get_selector_low_array(r, num_ranges,
0225                               lim, &sel, &found);
0226         if (ret)
0227             return ret;
0228 
0229         if (!found)
0230             dev_warn(regmap_get_device(regmap),
0231                  "limit %d out of range. Setting lower\n",
0232                  lim);
0233     }
0234 
0235     return regmap_update_bits(regmap, reg, mask, sel);
0236 }
0237 
0238 static bool check_ocp_flag_mismatch(struct regulator_dev *rdev, int severity,
0239                     struct bd957x_regulator_data *r)
0240 {
0241     if ((severity == REGULATOR_SEVERITY_ERR &&
0242         r->uvd_notif != REGULATOR_EVENT_OVER_CURRENT) ||
0243         (severity == REGULATOR_SEVERITY_WARN &&
0244         r->uvd_notif != REGULATOR_EVENT_OVER_CURRENT_WARN)) {
0245         dev_warn(rdev_get_dev(rdev),
0246              "Can't support both OCP WARN and ERR\n");
0247         /* Do not overwrite ERR config with WARN */
0248         if (severity == REGULATOR_SEVERITY_WARN)
0249             return true;
0250 
0251         bd9576_fill_ocp_flags(r, 0);
0252     }
0253 
0254     return false;
0255 }
0256 
0257 static bool check_uvd_flag_mismatch(struct regulator_dev *rdev, int severity,
0258                     struct bd957x_regulator_data *r)
0259 {
0260     if ((severity == REGULATOR_SEVERITY_ERR &&
0261          r->uvd_notif != REGULATOR_EVENT_UNDER_VOLTAGE) ||
0262          (severity == REGULATOR_SEVERITY_WARN &&
0263          r->uvd_notif != REGULATOR_EVENT_UNDER_VOLTAGE_WARN)) {
0264         dev_warn(rdev_get_dev(rdev),
0265              "Can't support both UVD WARN and ERR\n");
0266         if (severity == REGULATOR_SEVERITY_WARN)
0267             return true;
0268 
0269         bd9576_fill_uvd_flags(r, 0);
0270     }
0271 
0272     return false;
0273 }
0274 
0275 static bool check_ovd_flag_mismatch(struct regulator_dev *rdev, int severity,
0276                     struct bd957x_regulator_data *r)
0277 {
0278     if ((severity == REGULATOR_SEVERITY_ERR &&
0279          r->ovd_notif != REGULATOR_EVENT_REGULATION_OUT) ||
0280          (severity == REGULATOR_SEVERITY_WARN &&
0281          r->ovd_notif != REGULATOR_EVENT_OVER_VOLTAGE_WARN)) {
0282         dev_warn(rdev_get_dev(rdev),
0283              "Can't support both OVD WARN and ERR\n");
0284         if (severity == REGULATOR_SEVERITY_WARN)
0285             return true;
0286 
0287         bd9576_fill_ovd_flags(r, 0);
0288     }
0289 
0290     return false;
0291 }
0292 
0293 static bool check_temp_flag_mismatch(struct regulator_dev *rdev, int severity,
0294                     struct bd957x_regulator_data *r)
0295 {
0296     if ((severity == REGULATOR_SEVERITY_ERR &&
0297          r->temp_notif != REGULATOR_EVENT_OVER_TEMP) ||
0298          (severity == REGULATOR_SEVERITY_WARN &&
0299          r->temp_notif != REGULATOR_EVENT_OVER_TEMP_WARN)) {
0300         dev_warn(rdev_get_dev(rdev),
0301              "Can't support both thermal WARN and ERR\n");
0302         if (severity == REGULATOR_SEVERITY_WARN)
0303             return true;
0304     }
0305 
0306     return false;
0307 }
0308 
0309 static int bd9576_set_ocp(struct regulator_dev *rdev, int lim_uA, int severity,
0310               bool enable)
0311 {
0312     struct bd957x_data *d;
0313     struct bd957x_regulator_data *r;
0314     int reg, mask;
0315     int Vfet, rfet;
0316     const struct linear_range *range;
0317     int num_ranges;
0318 
0319     if ((lim_uA && !enable) || (!lim_uA && enable))
0320         return -EINVAL;
0321 
0322     r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
0323     if (!r->oc_supported)
0324         return -EINVAL;
0325 
0326     d = rdev_get_drvdata(rdev);
0327 
0328     if (severity == REGULATOR_SEVERITY_PROT) {
0329         reg = r->ocp_reg;
0330         mask = r->ocp_mask;
0331         if (r->ocw_rfet) {
0332             range = voutS1_ocp_ranges;
0333             num_ranges = ARRAY_SIZE(voutS1_ocp_ranges);
0334             rfet = r->ocw_rfet / 1000;
0335         } else {
0336             range = voutS1_ocp_ranges_internal;
0337             num_ranges = ARRAY_SIZE(voutS1_ocp_ranges_internal);
0338             /* Internal values are already micro-amperes */
0339             rfet = 1000;
0340         }
0341     } else {
0342         reg = r->ocw_reg;
0343         mask = r->ocw_mask;
0344 
0345         if (r->ocw_rfet) {
0346             range = voutS1_ocw_ranges;
0347             num_ranges = ARRAY_SIZE(voutS1_ocw_ranges);
0348             rfet = r->ocw_rfet / 1000;
0349         } else {
0350             range = voutS1_ocw_ranges_internal;
0351             num_ranges = ARRAY_SIZE(voutS1_ocw_ranges_internal);
0352             /* Internal values are already micro-amperes */
0353             rfet = 1000;
0354         }
0355 
0356         /* We abuse uvd fields for OCW on VoutS1 */
0357         if (r->uvd_notif) {
0358             /*
0359              * If both warning and error are requested, prioritize
0360              * ERROR configuration
0361              */
0362             if (check_ocp_flag_mismatch(rdev, severity, r))
0363                 return 0;
0364         } else {
0365             bool warn = severity == REGULATOR_SEVERITY_WARN;
0366 
0367             bd9576_fill_ocp_flags(r, warn);
0368         }
0369     }
0370 
0371     /*
0372      * limits are given in uA, rfet is mOhm
0373      * Divide lim_uA by 1000 to get Vfet in uV.
0374      * (We expect both Rfet and limit uA to be magnitude of hundreds of
0375      * milli Amperes & milli Ohms => we should still have decent accuracy)
0376      */
0377     Vfet = lim_uA/1000 * rfet;
0378 
0379     return bd9576_set_limit(range, num_ranges, d->regmap,
0380                 reg, mask, Vfet);
0381 }
0382 
0383 static int bd9576_set_uvp(struct regulator_dev *rdev, int lim_uV, int severity,
0384               bool enable)
0385 {
0386     struct bd957x_data *d;
0387     struct bd957x_regulator_data *r;
0388     int mask, reg;
0389 
0390     if (severity == REGULATOR_SEVERITY_PROT) {
0391         if (!enable || lim_uV)
0392             return -EINVAL;
0393         return 0;
0394     }
0395 
0396     /*
0397      * BD9576 has enable control as a special value in limit reg. Can't
0398      * set limit but keep feature disabled or enable W/O given limit.
0399      */
0400     if ((lim_uV && !enable) || (!lim_uV && enable))
0401         return -EINVAL;
0402 
0403     r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
0404     d = rdev_get_drvdata(rdev);
0405 
0406     mask = r->xvd_mask;
0407     reg = r->uvd_reg;
0408     /*
0409      * Check that there is no mismatch for what the detection IRQs are to
0410      * be used.
0411      */
0412     if (r->uvd_notif) {
0413         if (check_uvd_flag_mismatch(rdev, severity, r))
0414             return 0;
0415     } else {
0416         bd9576_fill_uvd_flags(r, severity == REGULATOR_SEVERITY_WARN);
0417     }
0418 
0419     return bd9576_set_limit(r->xvd_ranges, r->num_xvd_ranges, d->regmap,
0420                 reg, mask, lim_uV);
0421 }
0422 
0423 static int bd9576_set_ovp(struct regulator_dev *rdev, int lim_uV, int severity,
0424               bool enable)
0425 {
0426     struct bd957x_data *d;
0427     struct bd957x_regulator_data *r;
0428     int mask, reg;
0429 
0430     if (severity == REGULATOR_SEVERITY_PROT) {
0431         if (!enable || lim_uV)
0432             return -EINVAL;
0433         return 0;
0434     }
0435 
0436     /*
0437      * BD9576 has enable control as a special value in limit reg. Can't
0438      * set limit but keep feature disabled or enable W/O given limit.
0439      */
0440     if ((lim_uV && !enable) || (!lim_uV && enable))
0441         return -EINVAL;
0442 
0443     r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
0444     d = rdev_get_drvdata(rdev);
0445 
0446     mask = r->xvd_mask;
0447     reg = r->ovd_reg;
0448     /*
0449      * Check that there is no mismatch for what the detection IRQs are to
0450      * be used.
0451      */
0452     if (r->ovd_notif) {
0453         if (check_ovd_flag_mismatch(rdev, severity, r))
0454             return 0;
0455     } else {
0456         bd9576_fill_ovd_flags(r, severity == REGULATOR_SEVERITY_WARN);
0457     }
0458 
0459     return bd9576_set_limit(r->xvd_ranges, r->num_xvd_ranges, d->regmap,
0460                 reg, mask, lim_uV);
0461 }
0462 
0463 
0464 static int bd9576_set_tw(struct regulator_dev *rdev, int lim, int severity,
0465               bool enable)
0466 {
0467     struct bd957x_data *d;
0468     struct bd957x_regulator_data *r;
0469     int i;
0470 
0471     /*
0472      * BD9576MUF has fixed temperature limits
0473      * The detection can only be enabled/disabled
0474      */
0475     if (lim)
0476         return -EINVAL;
0477 
0478     /* Protection can't be disabled */
0479     if (severity == REGULATOR_SEVERITY_PROT) {
0480         if (!enable)
0481             return -EINVAL;
0482         else
0483             return 0;
0484     }
0485 
0486     r = container_of(rdev->desc, struct bd957x_regulator_data, desc);
0487     d = rdev_get_drvdata(rdev);
0488 
0489     /*
0490      * Check that there is no mismatch for what the detection IRQs are to
0491      * be used.
0492      */
0493     if (r->temp_notif)
0494         if (check_temp_flag_mismatch(rdev, severity, r))
0495             return 0;
0496 
0497     bd9576_fill_temp_flags(r, enable, severity == REGULATOR_SEVERITY_WARN);
0498 
0499     if (enable)
0500         return regmap_update_bits(d->regmap, BD957X_REG_INT_THERM_MASK,
0501                      BD9576_THERM_IRQ_MASK_TW, 0);
0502 
0503     /*
0504      * If any of the regulators is interested in thermal warning we keep IRQ
0505      * enabled.
0506      */
0507     for (i = 0; i < BD9576_NUM_REGULATORS; i++)
0508         if (d->regulator_data[i].temp_notif)
0509             return 0;
0510 
0511     return regmap_update_bits(d->regmap, BD957X_REG_INT_THERM_MASK,
0512                   BD9576_THERM_IRQ_MASK_TW,
0513                   BD9576_THERM_IRQ_MASK_TW);
0514 }
0515 
0516 static const struct regulator_ops bd9573_vout34_ops = {
0517     .is_enabled = regulator_is_enabled_regmap,
0518     .list_voltage = bd957x_vout34_list_voltage,
0519     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0520 };
0521 
0522 static const struct regulator_ops bd9576_vout34_ops = {
0523     .is_enabled = regulator_is_enabled_regmap,
0524     .list_voltage = bd957x_vout34_list_voltage,
0525     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0526     .set_over_voltage_protection = bd9576_set_ovp,
0527     .set_under_voltage_protection = bd9576_set_uvp,
0528     .set_thermal_protection = bd9576_set_tw,
0529 };
0530 
0531 static const struct regulator_ops bd9573_vouts1_regulator_ops = {
0532     .is_enabled = regulator_is_enabled_regmap,
0533 };
0534 
0535 static const struct regulator_ops bd9576_vouts1_regulator_ops = {
0536     .is_enabled = regulator_is_enabled_regmap,
0537     .set_over_current_protection = bd9576_set_ocp,
0538 };
0539 
0540 static const struct regulator_ops bd9573_ops = {
0541     .is_enabled = regulator_is_enabled_regmap,
0542     .list_voltage = bd957x_list_voltage,
0543     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0544 };
0545 
0546 static const struct regulator_ops bd9576_ops = {
0547     .is_enabled = regulator_is_enabled_regmap,
0548     .list_voltage = bd957x_list_voltage,
0549     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0550     .set_over_voltage_protection = bd9576_set_ovp,
0551     .set_under_voltage_protection = bd9576_set_uvp,
0552     .set_thermal_protection = bd9576_set_tw,
0553 };
0554 
0555 static const struct regulator_ops  *bd9573_ops_arr[] = {
0556     [BD957X_VD50]   = &bd9573_ops,
0557     [BD957X_VD18]   = &bd9573_ops,
0558     [BD957X_VDDDR]  = &bd9573_vout34_ops,
0559     [BD957X_VD10]   = &bd9573_vout34_ops,
0560     [BD957X_VOUTL1] = &bd9573_ops,
0561     [BD957X_VOUTS1] = &bd9573_vouts1_regulator_ops,
0562 };
0563 
0564 static const struct regulator_ops  *bd9576_ops_arr[] = {
0565     [BD957X_VD50]   = &bd9576_ops,
0566     [BD957X_VD18]   = &bd9576_ops,
0567     [BD957X_VDDDR]  = &bd9576_vout34_ops,
0568     [BD957X_VD10]   = &bd9576_vout34_ops,
0569     [BD957X_VOUTL1] = &bd9576_ops,
0570     [BD957X_VOUTS1] = &bd9576_vouts1_regulator_ops,
0571 };
0572 
0573 static int vouts1_get_fet_res(struct device_node *np,
0574                 const struct regulator_desc *desc,
0575                 struct regulator_config *cfg)
0576 {
0577     struct bd957x_regulator_data *data;
0578     int ret;
0579     u32 uohms;
0580 
0581     data = container_of(desc, struct bd957x_regulator_data, desc);
0582 
0583     ret = of_property_read_u32(np, "rohm,ocw-fet-ron-micro-ohms", &uohms);
0584     if (ret) {
0585         if (ret != -EINVAL)
0586             return ret;
0587 
0588         return 0;
0589     }
0590     data->ocw_rfet = uohms;
0591     return 0;
0592 }
0593 
0594 static struct bd957x_data bd957x_regulators = {
0595     .regulator_data = {
0596         {
0597             .desc = {
0598                 .name = "VD50",
0599                 .of_match = of_match_ptr("regulator-vd50"),
0600                 .regulators_node = of_match_ptr("regulators"),
0601                 .id = BD957X_VD50,
0602                 .type = REGULATOR_VOLTAGE,
0603                 .volt_table = &vout1_volt_table[0],
0604                 .n_voltages = ARRAY_SIZE(vout1_volt_table),
0605                 .vsel_reg = BD957X_REG_VOUT1_TUNE,
0606                 .vsel_mask = BD957X_MASK_VOUT1_TUNE,
0607                 .enable_reg = BD957X_REG_POW_TRIGGER1,
0608                 .enable_mask = BD957X_REGULATOR_EN_MASK,
0609                 .enable_val = BD957X_REGULATOR_DIS_VAL,
0610                 .enable_is_inverted = true,
0611                 .owner = THIS_MODULE,
0612             },
0613             .xvd_ranges = vout1_xvd_ranges,
0614             .num_xvd_ranges = ARRAY_SIZE(vout1_xvd_ranges),
0615             .ovd_reg = BD9576_REG_VOUT1_OVD,
0616             .uvd_reg = BD9576_REG_VOUT1_UVD,
0617             .xvd_mask = BD9576_MASK_XVD,
0618         },
0619         {
0620             .desc = {
0621                 .name = "VD18",
0622                 .of_match = of_match_ptr("regulator-vd18"),
0623                 .regulators_node = of_match_ptr("regulators"),
0624                 .id = BD957X_VD18,
0625                 .type = REGULATOR_VOLTAGE,
0626                 .volt_table = &vout2_volt_table[0],
0627                 .n_voltages = ARRAY_SIZE(vout2_volt_table),
0628                 .vsel_reg = BD957X_REG_VOUT2_TUNE,
0629                 .vsel_mask = BD957X_MASK_VOUT2_TUNE,
0630                 .enable_reg = BD957X_REG_POW_TRIGGER2,
0631                 .enable_mask = BD957X_REGULATOR_EN_MASK,
0632                 .enable_val = BD957X_REGULATOR_DIS_VAL,
0633                 .enable_is_inverted = true,
0634                 .owner = THIS_MODULE,
0635             },
0636             .xvd_ranges = vout234_xvd_ranges,
0637             .num_xvd_ranges = ARRAY_SIZE(vout234_xvd_ranges),
0638             .ovd_reg = BD9576_REG_VOUT2_OVD,
0639             .uvd_reg = BD9576_REG_VOUT2_UVD,
0640             .xvd_mask = BD9576_MASK_XVD,
0641         },
0642         {
0643             .desc = {
0644                 .name = "VDDDR",
0645                 .of_match = of_match_ptr("regulator-vdddr"),
0646                 .regulators_node = of_match_ptr("regulators"),
0647                 .id = BD957X_VDDDR,
0648                 .type = REGULATOR_VOLTAGE,
0649                 .n_voltages = BD957X_VOUTS34_NUM_VOLT,
0650                 .vsel_reg = BD957X_REG_VOUT3_TUNE,
0651                 .vsel_mask = BD957X_MASK_VOUT3_TUNE,
0652                 .enable_reg = BD957X_REG_POW_TRIGGER3,
0653                 .enable_mask = BD957X_REGULATOR_EN_MASK,
0654                 .enable_val = BD957X_REGULATOR_DIS_VAL,
0655                 .enable_is_inverted = true,
0656                 .owner = THIS_MODULE,
0657             },
0658             .ovd_reg = BD9576_REG_VOUT3_OVD,
0659             .uvd_reg = BD9576_REG_VOUT3_UVD,
0660             .xvd_mask = BD9576_MASK_XVD,
0661             .xvd_ranges = vout234_xvd_ranges,
0662             .num_xvd_ranges = ARRAY_SIZE(vout234_xvd_ranges),
0663         },
0664         {
0665             .desc = {
0666                 .name = "VD10",
0667                 .of_match = of_match_ptr("regulator-vd10"),
0668                 .regulators_node = of_match_ptr("regulators"),
0669                 .id = BD957X_VD10,
0670                 .type = REGULATOR_VOLTAGE,
0671                 .fixed_uV = BD957X_VOUTS4_BASE_VOLT,
0672                 .n_voltages = BD957X_VOUTS34_NUM_VOLT,
0673                 .vsel_reg = BD957X_REG_VOUT4_TUNE,
0674                 .vsel_mask = BD957X_MASK_VOUT4_TUNE,
0675                 .enable_reg = BD957X_REG_POW_TRIGGER4,
0676                 .enable_mask = BD957X_REGULATOR_EN_MASK,
0677                 .enable_val = BD957X_REGULATOR_DIS_VAL,
0678                 .enable_is_inverted = true,
0679                 .owner = THIS_MODULE,
0680             },
0681             .xvd_ranges = vout234_xvd_ranges,
0682             .num_xvd_ranges = ARRAY_SIZE(vout234_xvd_ranges),
0683             .ovd_reg = BD9576_REG_VOUT4_OVD,
0684             .uvd_reg = BD9576_REG_VOUT4_UVD,
0685             .xvd_mask = BD9576_MASK_XVD,
0686         },
0687         {
0688             .desc = {
0689                 .name = "VOUTL1",
0690                 .of_match = of_match_ptr("regulator-voutl1"),
0691                 .regulators_node = of_match_ptr("regulators"),
0692                 .id = BD957X_VOUTL1,
0693                 .type = REGULATOR_VOLTAGE,
0694                 .volt_table = &voutl1_volt_table[0],
0695                 .n_voltages = ARRAY_SIZE(voutl1_volt_table),
0696                 .vsel_reg = BD957X_REG_VOUTL1_TUNE,
0697                 .vsel_mask = BD957X_MASK_VOUTL1_TUNE,
0698                 .enable_reg = BD957X_REG_POW_TRIGGERL1,
0699                 .enable_mask = BD957X_REGULATOR_EN_MASK,
0700                 .enable_val = BD957X_REGULATOR_DIS_VAL,
0701                 .enable_is_inverted = true,
0702                 .owner = THIS_MODULE,
0703             },
0704             .xvd_ranges = voutL1_xvd_ranges,
0705             .num_xvd_ranges = ARRAY_SIZE(voutL1_xvd_ranges),
0706             .ovd_reg = BD9576_REG_VOUTL1_OVD,
0707             .uvd_reg = BD9576_REG_VOUTL1_UVD,
0708             .xvd_mask = BD9576_MASK_XVD,
0709         },
0710         {
0711             .desc = {
0712                 .name = "VOUTS1",
0713                 .of_match = of_match_ptr("regulator-vouts1"),
0714                 .regulators_node = of_match_ptr("regulators"),
0715                 .id = BD957X_VOUTS1,
0716                 .type = REGULATOR_VOLTAGE,
0717                 .n_voltages = 1,
0718                 .fixed_uV = BD957X_VOUTS1_VOLT,
0719                 .enable_reg = BD957X_REG_POW_TRIGGERS1,
0720                 .enable_mask = BD957X_REGULATOR_EN_MASK,
0721                 .enable_val = BD957X_REGULATOR_DIS_VAL,
0722                 .enable_is_inverted = true,
0723                 .owner = THIS_MODULE,
0724                 .of_parse_cb = vouts1_get_fet_res,
0725             },
0726             .oc_supported = true,
0727             .ocw_reg = BD9576_REG_VOUT1S_OCW,
0728             .ocw_mask = BD9576_MASK_VOUT1S_OCW,
0729             .ocp_reg = BD9576_REG_VOUT1S_OCP,
0730             .ocp_mask = BD9576_MASK_VOUT1S_OCP,
0731         },
0732     },
0733 };
0734 
0735 static int bd9576_renable(struct regulator_irq_data *rid, int reg, int mask)
0736 {
0737     int val, ret;
0738     struct bd957x_data *d = (struct bd957x_data *)rid->data;
0739 
0740     ret = regmap_read(d->regmap, reg, &val);
0741     if (ret)
0742         return REGULATOR_FAILED_RETRY;
0743 
0744     if (rid->opaque && rid->opaque == (val & mask)) {
0745         /*
0746          * It seems we stil have same status. Ack and return
0747          * information that we are still out of limits and core
0748          * should not enable IRQ
0749          */
0750         regmap_write(d->regmap, reg, mask & val);
0751         return REGULATOR_ERROR_ON;
0752     }
0753     rid->opaque = 0;
0754     /*
0755      * Status was changed. Either prolem was solved or we have new issues.
0756      * Let's re-enable IRQs and be prepared to report problems again
0757      */
0758     return REGULATOR_ERROR_CLEARED;
0759 }
0760 
0761 static int bd9576_uvd_renable(struct regulator_irq_data *rid)
0762 {
0763     return bd9576_renable(rid, BD957X_REG_INT_UVD_STAT, UVD_IRQ_VALID_MASK);
0764 }
0765 
0766 static int bd9576_ovd_renable(struct regulator_irq_data *rid)
0767 {
0768     return bd9576_renable(rid, BD957X_REG_INT_OVD_STAT, OVD_IRQ_VALID_MASK);
0769 }
0770 
0771 static int bd9576_temp_renable(struct regulator_irq_data *rid)
0772 {
0773     return bd9576_renable(rid, BD957X_REG_INT_THERM_STAT,
0774                   BD9576_THERM_IRQ_MASK_TW);
0775 }
0776 
0777 static int bd9576_uvd_handler(int irq, struct regulator_irq_data *rid,
0778                   unsigned long *dev_mask)
0779 {
0780     int val, ret, i;
0781     struct bd957x_data *d = (struct bd957x_data *)rid->data;
0782 
0783     ret = regmap_read(d->regmap, BD957X_REG_INT_UVD_STAT, &val);
0784     if (ret)
0785         return REGULATOR_FAILED_RETRY;
0786 
0787     *dev_mask = 0;
0788 
0789     rid->opaque = val & UVD_IRQ_VALID_MASK;
0790 
0791     /*
0792      * Go through the set status bits and report either error or warning
0793      * to the notifier depending on what was flagged in DT
0794      */
0795     *dev_mask = val & BD9576_xVD_IRQ_MASK_VOUT1TO4;
0796     /* There is 1 bit gap in register after Vout1 .. Vout4 statuses */
0797     *dev_mask |= ((val & BD9576_xVD_IRQ_MASK_VOUTL1) >> 1);
0798     /*
0799      * We (ab)use the uvd for OCW notification. DT parsing should
0800      * have added correct OCW flag to uvd_notif and uvd_err for S1
0801      */
0802     *dev_mask |= ((val & BD9576_UVD_IRQ_MASK_VOUTS1_OCW) >> 1);
0803 
0804     for_each_set_bit(i, dev_mask, 6) {
0805         struct bd957x_regulator_data *rdata;
0806         struct regulator_err_state *stat;
0807 
0808         rdata = &d->regulator_data[i];
0809         stat  = &rid->states[i];
0810 
0811         stat->notifs    = rdata->uvd_notif;
0812         stat->errors    = rdata->uvd_err;
0813     }
0814 
0815     ret = regmap_write(d->regmap, BD957X_REG_INT_UVD_STAT,
0816                UVD_IRQ_VALID_MASK & val);
0817 
0818     return 0;
0819 }
0820 
0821 static int bd9576_ovd_handler(int irq, struct regulator_irq_data *rid,
0822                   unsigned long *dev_mask)
0823 {
0824     int val, ret, i;
0825     struct bd957x_data *d = (struct bd957x_data *)rid->data;
0826 
0827     ret = regmap_read(d->regmap, BD957X_REG_INT_OVD_STAT, &val);
0828     if (ret)
0829         return REGULATOR_FAILED_RETRY;
0830 
0831     rid->opaque = val & OVD_IRQ_VALID_MASK;
0832     *dev_mask = 0;
0833 
0834     if (!(val & OVD_IRQ_VALID_MASK))
0835         return 0;
0836 
0837     *dev_mask = val & BD9576_xVD_IRQ_MASK_VOUT1TO4;
0838     /* There is 1 bit gap in register after Vout1 .. Vout4 statuses */
0839     *dev_mask |= ((val & BD9576_xVD_IRQ_MASK_VOUTL1) >> 1);
0840 
0841     for_each_set_bit(i, dev_mask, 5) {
0842         struct bd957x_regulator_data *rdata;
0843         struct regulator_err_state *stat;
0844 
0845         rdata = &d->regulator_data[i];
0846         stat  = &rid->states[i];
0847 
0848         stat->notifs    = rdata->ovd_notif;
0849         stat->errors    = rdata->ovd_err;
0850     }
0851 
0852     /* Clear the sub-IRQ status */
0853     regmap_write(d->regmap, BD957X_REG_INT_OVD_STAT,
0854              OVD_IRQ_VALID_MASK & val);
0855 
0856     return 0;
0857 }
0858 
0859 #define BD9576_DEV_MASK_ALL_REGULATORS 0x3F
0860 
0861 static int bd9576_thermal_handler(int irq, struct regulator_irq_data *rid,
0862                   unsigned long *dev_mask)
0863 {
0864     int val, ret, i;
0865     struct bd957x_data *d = (struct bd957x_data *)rid->data;
0866 
0867     ret = regmap_read(d->regmap, BD957X_REG_INT_THERM_STAT, &val);
0868     if (ret)
0869         return REGULATOR_FAILED_RETRY;
0870 
0871     if (!(val & BD9576_THERM_IRQ_MASK_TW)) {
0872         *dev_mask = 0;
0873         return 0;
0874     }
0875 
0876     *dev_mask = BD9576_DEV_MASK_ALL_REGULATORS;
0877 
0878     for (i = 0; i < BD9576_NUM_REGULATORS; i++) {
0879         struct bd957x_regulator_data *rdata;
0880         struct regulator_err_state *stat;
0881 
0882         rdata = &d->regulator_data[i];
0883         stat  = &rid->states[i];
0884 
0885         stat->notifs    = rdata->temp_notif;
0886         stat->errors    = rdata->temp_err;
0887     }
0888 
0889     /* Clear the sub-IRQ status */
0890     regmap_write(d->regmap, BD957X_REG_INT_THERM_STAT,
0891              BD9576_THERM_IRQ_MASK_TW);
0892 
0893     return 0;
0894 }
0895 
0896 static int bd957x_probe(struct platform_device *pdev)
0897 {
0898     int i;
0899     unsigned int num_reg_data;
0900     bool vout_mode, ddr_sel, may_have_irqs = false;
0901     struct regmap *regmap;
0902     struct bd957x_data *ic_data;
0903     struct regulator_config config = { 0 };
0904     /* All regulators are related to UVD and thermal IRQs... */
0905     struct regulator_dev *rdevs[BD9576_NUM_REGULATORS];
0906     /* ...But VoutS1 is not flagged by OVD IRQ */
0907     struct regulator_dev *ovd_devs[BD9576_NUM_OVD_REGULATORS];
0908     static const struct regulator_irq_desc bd9576_notif_uvd = {
0909         .name = "bd9576-uvd",
0910         .irq_off_ms = 1000,
0911         .map_event = bd9576_uvd_handler,
0912         .renable = bd9576_uvd_renable,
0913         .data = &bd957x_regulators,
0914     };
0915     static const struct regulator_irq_desc bd9576_notif_ovd = {
0916         .name = "bd9576-ovd",
0917         .irq_off_ms = 1000,
0918         .map_event = bd9576_ovd_handler,
0919         .renable = bd9576_ovd_renable,
0920         .data = &bd957x_regulators,
0921     };
0922     static const struct regulator_irq_desc bd9576_notif_temp = {
0923         .name = "bd9576-temp",
0924         .irq_off_ms = 1000,
0925         .map_event = bd9576_thermal_handler,
0926         .renable = bd9576_temp_renable,
0927         .data = &bd957x_regulators,
0928     };
0929     enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
0930 
0931     num_reg_data = ARRAY_SIZE(bd957x_regulators.regulator_data);
0932 
0933     ic_data = &bd957x_regulators;
0934 
0935     regmap = dev_get_regmap(pdev->dev.parent, NULL);
0936     if (!regmap) {
0937         dev_err(&pdev->dev, "No regmap\n");
0938         return -EINVAL;
0939     }
0940 
0941     ic_data->regmap = regmap;
0942     vout_mode = of_property_read_bool(pdev->dev.parent->of_node,
0943                      "rohm,vout1-en-low");
0944     if (vout_mode) {
0945         struct gpio_desc *en;
0946 
0947         dev_dbg(&pdev->dev, "GPIO controlled mode\n");
0948 
0949         /* VOUT1 enable state judged by VOUT1_EN pin */
0950         /* See if we have GPIO defined */
0951         en = devm_gpiod_get_from_of_node(&pdev->dev,
0952                          pdev->dev.parent->of_node,
0953                          "rohm,vout1-en-gpios", 0,
0954                          GPIOD_OUT_LOW, "vout1-en");
0955         if (!IS_ERR(en)) {
0956             /* VOUT1_OPS gpio ctrl */
0957             /*
0958              * Regulator core prioritizes the ena_gpio over
0959              * enable/disable/is_enabled callbacks so no need to
0960              * clear them. We can still use same ops
0961              */
0962             config.ena_gpiod = en;
0963         } else {
0964             /*
0965              * In theory it is possible someone wants to set
0966              * vout1-en LOW during OTP loading and set VOUT1 to be
0967              * controlled by GPIO - but control the GPIO from some
0968              * where else than this driver. For that to work we
0969              * should unset the is_enabled callback here.
0970              *
0971              * I believe such case where rohm,vout1-en-low is set
0972              * and vout1-en-gpios is not is likely to be a
0973              * misconfiguration. So let's just err out for now.
0974              */
0975             dev_err(&pdev->dev,
0976                 "Failed to get VOUT1 control GPIO\n");
0977             return PTR_ERR(en);
0978         }
0979     }
0980 
0981     /*
0982      * If more than one PMIC needs to be controlled by same processor then
0983      * allocate the regulator data array here and use bd9576_regulators as
0984      * template. At the moment I see no such use-case so I spare some
0985      * bytes and use bd9576_regulators directly for non-constant configs
0986      * like DDR voltage selection.
0987      */
0988     platform_set_drvdata(pdev, ic_data);
0989     ddr_sel =  of_property_read_bool(pdev->dev.parent->of_node,
0990                      "rohm,ddr-sel-low");
0991     if (ddr_sel)
0992         ic_data->regulator_data[2].desc.fixed_uV = 1350000;
0993     else
0994         ic_data->regulator_data[2].desc.fixed_uV = 1500000;
0995 
0996     switch (chip) {
0997     case ROHM_CHIP_TYPE_BD9576:
0998         may_have_irqs = true;
0999         dev_dbg(&pdev->dev, "Found BD9576MUF\n");
1000         break;
1001     case ROHM_CHIP_TYPE_BD9573:
1002         dev_dbg(&pdev->dev, "Found BD9573MUF\n");
1003         break;
1004     default:
1005         dev_err(&pdev->dev, "Unsupported chip type\n");
1006         return -EINVAL;
1007     }
1008 
1009     for (i = 0; i < num_reg_data; i++) {
1010         struct regulator_desc *d;
1011 
1012         d = &ic_data->regulator_data[i].desc;
1013 
1014 
1015         if (may_have_irqs) {
1016             if (d->id >= ARRAY_SIZE(bd9576_ops_arr))
1017                 return -EINVAL;
1018 
1019             d->ops = bd9576_ops_arr[d->id];
1020         } else {
1021             if (d->id >= ARRAY_SIZE(bd9573_ops_arr))
1022                 return -EINVAL;
1023 
1024             d->ops = bd9573_ops_arr[d->id];
1025         }
1026     }
1027 
1028     config.dev = pdev->dev.parent;
1029     config.regmap = regmap;
1030     config.driver_data = ic_data;
1031 
1032     for (i = 0; i < num_reg_data; i++) {
1033 
1034         struct bd957x_regulator_data *r = &ic_data->regulator_data[i];
1035         const struct regulator_desc *desc = &r->desc;
1036 
1037         r->rdev = devm_regulator_register(&pdev->dev, desc,
1038                                &config);
1039         if (IS_ERR(r->rdev)) {
1040             dev_err(&pdev->dev,
1041                 "failed to register %s regulator\n",
1042                 desc->name);
1043             return PTR_ERR(r->rdev);
1044         }
1045         /*
1046          * Clear the VOUT1 GPIO setting - rest of the regulators do not
1047          * support GPIO control
1048          */
1049         config.ena_gpiod = NULL;
1050 
1051         if (!may_have_irqs)
1052             continue;
1053 
1054         rdevs[i] = r->rdev;
1055         if (i < BD957X_VOUTS1)
1056             ovd_devs[i] = r->rdev;
1057     }
1058     if (may_have_irqs) {
1059         void *ret;
1060         /*
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.
1068          */
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;
1077         int irq;
1078 
1079         irq = platform_get_irq_byname(pdev, "bd9576-uvd");
1080 
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,
1084                         &rdevs[0],
1085                         BD9576_NUM_REGULATORS);
1086         if (IS_ERR(ret)) {
1087             if (PTR_ERR(ret) == -EPROBE_DEFER)
1088                 return -EPROBE_DEFER;
1089 
1090             dev_warn(&pdev->dev, "UVD disabled %pe\n", ret);
1091         }
1092 
1093         irq = platform_get_irq_byname(pdev, "bd9576-ovd");
1094 
1095         ret = devm_regulator_irq_helper(&pdev->dev, &bd9576_notif_ovd,
1096                         irq, 0, ovd_errs, NULL,
1097                         &ovd_devs[0],
1098                         BD9576_NUM_OVD_REGULATORS);
1099         if (IS_ERR(ret)) {
1100             if (PTR_ERR(ret) == -EPROBE_DEFER)
1101                 return -EPROBE_DEFER;
1102 
1103             dev_warn(&pdev->dev, "OVD disabled %pe\n", ret);
1104         }
1105         irq = platform_get_irq_byname(pdev, "bd9576-temp");
1106 
1107         ret = devm_regulator_irq_helper(&pdev->dev, &bd9576_notif_temp,
1108                         irq, 0, temp_errs, NULL,
1109                         &rdevs[0],
1110                         BD9576_NUM_REGULATORS);
1111         if (IS_ERR(ret)) {
1112             if (PTR_ERR(ret) == -EPROBE_DEFER)
1113                 return -EPROBE_DEFER;
1114 
1115             dev_warn(&pdev->dev, "Thermal warning disabled %pe\n",
1116                  ret);
1117         }
1118     }
1119     return 0;
1120 }
1121 
1122 static const struct platform_device_id bd957x_pmic_id[] = {
1123     { "bd9573-regulator", ROHM_CHIP_TYPE_BD9573 },
1124     { "bd9576-regulator", ROHM_CHIP_TYPE_BD9576 },
1125     { },
1126 };
1127 MODULE_DEVICE_TABLE(platform, bd957x_pmic_id);
1128 
1129 static struct platform_driver bd957x_regulator = {
1130     .driver = {
1131         .name = "bd957x-pmic",
1132     },
1133     .probe = bd957x_probe,
1134     .id_table = bd957x_pmic_id,
1135 };
1136 
1137 module_platform_driver(bd957x_regulator);
1138 
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");