Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (C) 2018 ROHM Semiconductors
0003 // bd71837-regulator.c ROHM BD71837MWV/BD71847MWV regulator driver
0004 
0005 #include <linux/delay.h>
0006 #include <linux/err.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/kernel.h>
0009 #include <linux/mfd/rohm-bd718x7.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regulator/driver.h>
0014 #include <linux/regulator/machine.h>
0015 #include <linux/regulator/of_regulator.h>
0016 #include <linux/slab.h>
0017 
0018 /* Typical regulator startup times as per data sheet in uS */
0019 #define BD71847_BUCK1_STARTUP_TIME 144
0020 #define BD71847_BUCK2_STARTUP_TIME 162
0021 #define BD71847_BUCK3_STARTUP_TIME 162
0022 #define BD71847_BUCK4_STARTUP_TIME 240
0023 #define BD71847_BUCK5_STARTUP_TIME 270
0024 #define BD71847_BUCK6_STARTUP_TIME 200
0025 #define BD71847_LDO1_STARTUP_TIME  440
0026 #define BD71847_LDO2_STARTUP_TIME  370
0027 #define BD71847_LDO3_STARTUP_TIME  310
0028 #define BD71847_LDO4_STARTUP_TIME  400
0029 #define BD71847_LDO5_STARTUP_TIME  530
0030 #define BD71847_LDO6_STARTUP_TIME  400
0031 
0032 #define BD71837_BUCK1_STARTUP_TIME 160
0033 #define BD71837_BUCK2_STARTUP_TIME 180
0034 #define BD71837_BUCK3_STARTUP_TIME 180
0035 #define BD71837_BUCK4_STARTUP_TIME 180
0036 #define BD71837_BUCK5_STARTUP_TIME 160
0037 #define BD71837_BUCK6_STARTUP_TIME 240
0038 #define BD71837_BUCK7_STARTUP_TIME 220
0039 #define BD71837_BUCK8_STARTUP_TIME 200
0040 #define BD71837_LDO1_STARTUP_TIME  440
0041 #define BD71837_LDO2_STARTUP_TIME  370
0042 #define BD71837_LDO3_STARTUP_TIME  310
0043 #define BD71837_LDO4_STARTUP_TIME  400
0044 #define BD71837_LDO5_STARTUP_TIME  310
0045 #define BD71837_LDO6_STARTUP_TIME  400
0046 #define BD71837_LDO7_STARTUP_TIME  530
0047 
0048 /*
0049  * BD718(37/47/50) have two "enable control modes". ON/OFF can either be
0050  * controlled by software - or by PMIC internal HW state machine. Whether
0051  * regulator should be under SW or HW control can be defined from device-tree.
0052  * Let's provide separate ops for regulators to use depending on the "enable
0053  * control mode".
0054  */
0055 #define BD718XX_HWOPNAME(swopname) swopname##_hwcontrol
0056 
0057 #define BD718XX_OPS(name, _list_voltage, _map_voltage, _set_voltage_sel, \
0058            _get_voltage_sel, _set_voltage_time_sel, _set_ramp_delay, \
0059            _set_uvp, _set_ovp)              \
0060 static const struct regulator_ops name = {          \
0061     .enable = regulator_enable_regmap,          \
0062     .disable = regulator_disable_regmap,            \
0063     .is_enabled = regulator_is_enabled_regmap,      \
0064     .list_voltage = (_list_voltage),            \
0065     .map_voltage = (_map_voltage),              \
0066     .set_voltage_sel = (_set_voltage_sel),          \
0067     .get_voltage_sel = (_get_voltage_sel),          \
0068     .set_voltage_time_sel = (_set_voltage_time_sel),    \
0069     .set_ramp_delay = (_set_ramp_delay),            \
0070     .set_under_voltage_protection = (_set_uvp),     \
0071     .set_over_voltage_protection = (_set_ovp),      \
0072 };                              \
0073                                 \
0074 static const struct regulator_ops BD718XX_HWOPNAME(name) = {    \
0075     .is_enabled = always_enabled_by_hwstate,        \
0076     .list_voltage = (_list_voltage),            \
0077     .map_voltage = (_map_voltage),              \
0078     .set_voltage_sel = (_set_voltage_sel),          \
0079     .get_voltage_sel = (_get_voltage_sel),          \
0080     .set_voltage_time_sel = (_set_voltage_time_sel),    \
0081     .set_ramp_delay = (_set_ramp_delay),            \
0082     .set_under_voltage_protection = (_set_uvp),     \
0083     .set_over_voltage_protection = (_set_ovp),      \
0084 }                               \
0085 
0086 /*
0087  * BUCK1/2/3/4
0088  * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
0089  * 00: 10.00mV/usec 10mV 1uS
0090  * 01: 5.00mV/usec  10mV 2uS
0091  * 10: 2.50mV/usec  10mV 4uS
0092  * 11: 1.25mV/usec  10mV 8uS
0093  */
0094 static const unsigned int bd718xx_ramp_delay[] = { 10000, 5000, 2500, 1250 };
0095 
0096 /* These functions are used when regulators are under HW state machine control.
0097  * We assume PMIC is in RUN state because SW running and able to query the
0098  * status. Most of the regulators have fixed ON or OFF state at RUN/IDLE so for
0099  * them we just return a constant. BD71837 BUCK3 and BUCK4 are exceptions as
0100  * they support configuring the ON/OFF state for RUN.
0101  *
0102  * Note for next hacker - these PMICs have a register where the HW state can be
0103  * read. If assuming RUN appears to be false in your use-case - you can
0104  * implement state reading (although that is not going to be atomic) before
0105  * returning the enable state.
0106  */
0107 static int always_enabled_by_hwstate(struct regulator_dev *rdev)
0108 {
0109     return 1;
0110 }
0111 
0112 static int never_enabled_by_hwstate(struct regulator_dev *rdev)
0113 {
0114     return 0;
0115 }
0116 
0117 static int bd71837_get_buck34_enable_hwctrl(struct regulator_dev *rdev)
0118 {
0119     int ret;
0120     unsigned int val;
0121 
0122     ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
0123     if (ret)
0124         return ret;
0125 
0126     return !!(BD718XX_BUCK_RUN_ON & val);
0127 }
0128 
0129 static void voltage_change_done(struct regulator_dev *rdev, unsigned int sel,
0130                 unsigned int *mask)
0131 {
0132     int ret;
0133 
0134     if (*mask) {
0135         /*
0136          * Let's allow scheduling as we use I2C anyways. We just need to
0137          * guarantee minimum of 1ms sleep - it shouldn't matter if we
0138          * exceed it due to the scheduling.
0139          */
0140         msleep(1);
0141 
0142         ret = regmap_clear_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
0143                      *mask);
0144         if (ret)
0145             dev_err(&rdev->dev,
0146                 "Failed to re-enable voltage monitoring (%d)\n",
0147                 ret);
0148     }
0149 }
0150 
0151 static int voltage_change_prepare(struct regulator_dev *rdev, unsigned int sel,
0152                   unsigned int *mask)
0153 {
0154     int ret;
0155 
0156     *mask = 0;
0157     if (rdev->desc->ops->is_enabled(rdev)) {
0158         int now, new;
0159 
0160         now = rdev->desc->ops->get_voltage_sel(rdev);
0161         if (now < 0)
0162             return now;
0163 
0164         now = rdev->desc->ops->list_voltage(rdev, now);
0165         if (now < 0)
0166             return now;
0167 
0168         new = rdev->desc->ops->list_voltage(rdev, sel);
0169         if (new < 0)
0170             return new;
0171 
0172         /*
0173          * If we increase LDO voltage when LDO is enabled we need to
0174          * disable the power-good detection until voltage has reached
0175          * the new level. According to HW colleagues the maximum time
0176          * it takes is 1000us. I assume that on systems with light load
0177          * this might be less - and we could probably use DT to give
0178          * system specific delay value if performance matters.
0179          *
0180          * Well, knowing we use I2C here and can add scheduling delays
0181          * I don't think it is worth the hassle and I just add fixed
0182          * 1ms sleep here (and allow scheduling). If this turns out to
0183          * be a problem we can change it to delay and make the delay
0184          * time configurable.
0185          */
0186         if (new > now) {
0187             int tmp;
0188             int prot_bit;
0189             int ldo_offset = rdev->desc->id - BD718XX_LDO1;
0190 
0191             prot_bit = BD718XX_LDO1_VRMON80 << ldo_offset;
0192             ret = regmap_read(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
0193                       &tmp);
0194             if (ret) {
0195                 dev_err(&rdev->dev,
0196                     "Failed to read voltage monitoring state\n");
0197                 return ret;
0198             }
0199 
0200             if (!(tmp & prot_bit)) {
0201                 /* We disable protection if it was enabled... */
0202                 ret = regmap_set_bits(rdev->regmap,
0203                               BD718XX_REG_MVRFLTMASK2,
0204                               prot_bit);
0205                 /* ...and we also want to re-enable it */
0206                 *mask = prot_bit;
0207             }
0208             if (ret) {
0209                 dev_err(&rdev->dev,
0210                     "Failed to stop voltage monitoring\n");
0211                 return ret;
0212             }
0213         }
0214     }
0215 
0216     return 0;
0217 }
0218 
0219 static int bd718xx_set_voltage_sel_restricted(struct regulator_dev *rdev,
0220                             unsigned int sel)
0221 {
0222     int ret;
0223     int mask;
0224 
0225     ret = voltage_change_prepare(rdev, sel, &mask);
0226     if (ret)
0227         return ret;
0228 
0229     ret = regulator_set_voltage_sel_regmap(rdev, sel);
0230     voltage_change_done(rdev, sel, &mask);
0231 
0232     return ret;
0233 }
0234 
0235 static int bd718xx_set_voltage_sel_pickable_restricted(
0236         struct regulator_dev *rdev, unsigned int sel)
0237 {
0238     int ret;
0239     int mask;
0240 
0241     ret = voltage_change_prepare(rdev, sel, &mask);
0242     if (ret)
0243         return ret;
0244 
0245     ret = regulator_set_voltage_sel_pickable_regmap(rdev, sel);
0246     voltage_change_done(rdev, sel, &mask);
0247 
0248     return ret;
0249 }
0250 
0251 static int bd71837_set_voltage_sel_pickable_restricted(
0252         struct regulator_dev *rdev, unsigned int sel)
0253 {
0254     if (rdev->desc->ops->is_enabled(rdev))
0255         return -EBUSY;
0256 
0257     return regulator_set_voltage_sel_pickable_regmap(rdev, sel);
0258 }
0259 
0260 /*
0261  * BD71837 BUCK1/2/3/4
0262  * BD71847 BUCK1/2
0263  * 0.70 to 1.30V (10mV step)
0264  */
0265 static const struct linear_range bd718xx_dvs_buck_volts[] = {
0266     REGULATOR_LINEAR_RANGE(700000, 0x00, 0x3C, 10000),
0267     REGULATOR_LINEAR_RANGE(1300000, 0x3D, 0x3F, 0),
0268 };
0269 
0270 /*
0271  * BD71837 BUCK5
0272  * 0.7V to 1.35V  (range 0)
0273  * and
0274  * 0.675 to 1.325 (range 1)
0275  */
0276 static const struct linear_range bd71837_buck5_volts[] = {
0277     /* Ranges when VOLT_SEL bit is 0 */
0278     REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
0279     REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
0280     REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
0281     /* Ranges when VOLT_SEL bit is 1  */
0282     REGULATOR_LINEAR_RANGE(675000, 0x0, 0x3, 100000),
0283     REGULATOR_LINEAR_RANGE(1025000, 0x4, 0x5, 50000),
0284     REGULATOR_LINEAR_RANGE(1175000, 0x6, 0x7, 150000),
0285 };
0286 
0287 /*
0288  * Range selector for first 3 linear ranges is 0x0
0289  * and 0x1 for last 3 ranges.
0290  */
0291 static const unsigned int bd71837_buck5_volt_range_sel[] = {
0292     0x0, 0x0, 0x0, 0x80, 0x80, 0x80
0293 };
0294 
0295 /*
0296  * BD71847 BUCK3
0297  */
0298 static const struct linear_range bd71847_buck3_volts[] = {
0299     /* Ranges when VOLT_SEL bits are 00 */
0300     REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
0301     REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
0302     REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
0303     /* Ranges when VOLT_SEL bits are 01 */
0304     REGULATOR_LINEAR_RANGE(550000, 0x0, 0x7, 50000),
0305     /* Ranges when VOLT_SEL bits are 11 */
0306     REGULATOR_LINEAR_RANGE(675000, 0x0, 0x3, 100000),
0307     REGULATOR_LINEAR_RANGE(1025000, 0x4, 0x5, 50000),
0308     REGULATOR_LINEAR_RANGE(1175000, 0x6, 0x7, 150000),
0309 };
0310 
0311 static const unsigned int bd71847_buck3_volt_range_sel[] = {
0312     0x0, 0x0, 0x0, 0x40, 0x80, 0x80, 0x80
0313 };
0314 
0315 static const struct linear_range bd71847_buck4_volts[] = {
0316     REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
0317     REGULATOR_LINEAR_RANGE(2600000, 0x00, 0x03, 100000),
0318 };
0319 
0320 static const unsigned int bd71847_buck4_volt_range_sel[] = { 0x0, 0x40 };
0321 
0322 /*
0323  * BUCK6
0324  * 3.0V to 3.3V (step 100mV)
0325  */
0326 static const struct linear_range bd71837_buck6_volts[] = {
0327     REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
0328 };
0329 
0330 /*
0331  * BD71837 BUCK7
0332  * BD71847 BUCK5
0333  * 000 = 1.605V
0334  * 001 = 1.695V
0335  * 010 = 1.755V
0336  * 011 = 1.8V (Initial)
0337  * 100 = 1.845V
0338  * 101 = 1.905V
0339  * 110 = 1.95V
0340  * 111 = 1.995V
0341  */
0342 static const unsigned int bd718xx_3rd_nodvs_buck_volts[] = {
0343     1605000, 1695000, 1755000, 1800000, 1845000, 1905000, 1950000, 1995000
0344 };
0345 
0346 /*
0347  * BUCK8
0348  * 0.8V to 1.40V (step 10mV)
0349  */
0350 static const struct linear_range bd718xx_4th_nodvs_buck_volts[] = {
0351     REGULATOR_LINEAR_RANGE(800000, 0x00, 0x3C, 10000),
0352 };
0353 
0354 /*
0355  * LDO1
0356  * 3.0 to 3.3V (100mV step)
0357  */
0358 static const struct linear_range bd718xx_ldo1_volts[] = {
0359     REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
0360     REGULATOR_LINEAR_RANGE(1600000, 0x00, 0x03, 100000),
0361 };
0362 
0363 static const unsigned int bd718xx_ldo1_volt_range_sel[] = { 0x0, 0x20 };
0364 
0365 /*
0366  * LDO2
0367  * 0.8 or 0.9V
0368  */
0369 static const unsigned int ldo_2_volts[] = {
0370     900000, 800000
0371 };
0372 
0373 /*
0374  * LDO3
0375  * 1.8 to 3.3V (100mV step)
0376  */
0377 static const struct linear_range bd718xx_ldo3_volts[] = {
0378     REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
0379 };
0380 
0381 /*
0382  * LDO4
0383  * 0.9 to 1.8V (100mV step)
0384  */
0385 static const struct linear_range bd718xx_ldo4_volts[] = {
0386     REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
0387 };
0388 
0389 /*
0390  * LDO5 for BD71837
0391  * 1.8 to 3.3V (100mV step)
0392  */
0393 static const struct linear_range bd71837_ldo5_volts[] = {
0394     REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
0395 };
0396 
0397 /*
0398  * LDO5 for BD71837
0399  * 1.8 to 3.3V (100mV step)
0400  */
0401 static const struct linear_range bd71847_ldo5_volts[] = {
0402     REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
0403     REGULATOR_LINEAR_RANGE(800000, 0x00, 0x0F, 100000),
0404 };
0405 
0406 static const unsigned int bd71847_ldo5_volt_range_sel[] = { 0x0, 0x20 };
0407 
0408 /*
0409  * LDO6
0410  * 0.9 to 1.8V (100mV step)
0411  */
0412 static const struct linear_range bd718xx_ldo6_volts[] = {
0413     REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
0414 };
0415 
0416 /*
0417  * LDO7
0418  * 1.8 to 3.3V (100mV step)
0419  */
0420 static const struct linear_range bd71837_ldo7_volts[] = {
0421     REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
0422 };
0423 
0424 struct reg_init {
0425     unsigned int reg;
0426     unsigned int mask;
0427     unsigned int val;
0428 };
0429 struct bd718xx_regulator_data {
0430     struct regulator_desc desc;
0431     const struct rohm_dvs_config dvs;
0432     const struct reg_init init;
0433     const struct reg_init *additional_inits;
0434     int additional_init_amnt;
0435 };
0436 
0437 static int bd718x7_xvp_sanity_check(struct regulator_dev *rdev, int lim_uV,
0438                     int severity)
0439 {
0440     /*
0441      * BD71837/47/50 ... (ICs supported by this driver) do not provide
0442      * warnings, only protection
0443      */
0444     if (severity != REGULATOR_SEVERITY_PROT) {
0445         dev_err(&rdev->dev,
0446             "Unsupported Under Voltage protection level\n");
0447         return -EINVAL;
0448     }
0449 
0450     /*
0451      * And protection limit is not changeable. It can only be enabled
0452      * or disabled
0453      */
0454     if (lim_uV)
0455         return -EINVAL;
0456 
0457     return 0;
0458 }
0459 
0460 static int bd718x7_set_ldo_uvp(struct regulator_dev *rdev, int lim_uV,
0461                    int severity, bool enable)
0462 {
0463     int ldo_offset = rdev->desc->id - BD718XX_LDO1;
0464     int prot_bit, ret;
0465 
0466     ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
0467     if (ret)
0468         return ret;
0469 
0470     prot_bit = BD718XX_LDO1_VRMON80 << ldo_offset;
0471 
0472     if (enable)
0473         return regmap_clear_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
0474                      prot_bit);
0475 
0476     return regmap_set_bits(rdev->regmap, BD718XX_REG_MVRFLTMASK2,
0477                    prot_bit);
0478 }
0479 
0480 static int bd718x7_get_buck_prot_reg(int id, int *reg)
0481 {
0482 
0483     if (id > BD718XX_BUCK8) {
0484         WARN_ON(id > BD718XX_BUCK8);
0485         return -EINVAL;
0486     }
0487 
0488     if (id > BD718XX_BUCK4)
0489         *reg = BD718XX_REG_MVRFLTMASK0;
0490     else
0491         *reg = BD718XX_REG_MVRFLTMASK1;
0492 
0493     return 0;
0494 }
0495 
0496 static int bd718x7_get_buck_ovp_info(int id, int *reg, int *bit)
0497 {
0498     int ret;
0499 
0500     ret = bd718x7_get_buck_prot_reg(id, reg);
0501     if (ret)
0502         return ret;
0503 
0504     *bit = BIT((id % 4) * 2 + 1);
0505 
0506     return 0;
0507 }
0508 
0509 static int bd718x7_get_buck_uvp_info(int id, int *reg, int *bit)
0510 {
0511     int ret;
0512 
0513     ret = bd718x7_get_buck_prot_reg(id, reg);
0514     if (ret)
0515         return ret;
0516 
0517     *bit = BIT((id % 4) * 2);
0518 
0519     return 0;
0520 }
0521 
0522 static int bd718x7_set_buck_uvp(struct regulator_dev *rdev, int lim_uV,
0523                 int severity, bool enable)
0524 {
0525     int bit, reg, ret;
0526 
0527     ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
0528     if (ret)
0529         return ret;
0530 
0531     ret = bd718x7_get_buck_uvp_info(rdev->desc->id, &reg, &bit);
0532     if (ret)
0533         return ret;
0534 
0535     if (enable)
0536         return regmap_clear_bits(rdev->regmap, reg, bit);
0537 
0538     return regmap_set_bits(rdev->regmap, reg, bit);
0539 
0540 }
0541 
0542 static int bd718x7_set_buck_ovp(struct regulator_dev *rdev, int lim_uV,
0543                 int severity,
0544                 bool enable)
0545 {
0546     int bit, reg, ret;
0547 
0548     ret = bd718x7_xvp_sanity_check(rdev, lim_uV, severity);
0549     if (ret)
0550         return ret;
0551 
0552     ret = bd718x7_get_buck_ovp_info(rdev->desc->id, &reg, &bit);
0553     if (ret)
0554         return ret;
0555 
0556     if (enable)
0557         return regmap_clear_bits(rdev->regmap, reg, bit);
0558 
0559     return regmap_set_bits(rdev->regmap, reg, bit);
0560 }
0561 
0562 /*
0563  * OPS common for BD71847 and BD71850
0564  */
0565 BD718XX_OPS(bd718xx_pickable_range_ldo_ops,
0566         regulator_list_voltage_pickable_linear_range, NULL,
0567         bd718xx_set_voltage_sel_pickable_restricted,
0568         regulator_get_voltage_sel_pickable_regmap, NULL, NULL,
0569         bd718x7_set_ldo_uvp, NULL);
0570 
0571 /* BD71847 and BD71850 LDO 5 is by default OFF at RUN state */
0572 static const struct regulator_ops bd718xx_ldo5_ops_hwstate = {
0573     .is_enabled = never_enabled_by_hwstate,
0574     .list_voltage = regulator_list_voltage_pickable_linear_range,
0575     .set_voltage_sel = bd718xx_set_voltage_sel_pickable_restricted,
0576     .get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
0577     .set_under_voltage_protection = bd718x7_set_ldo_uvp,
0578 };
0579 
0580 BD718XX_OPS(bd718xx_pickable_range_buck_ops,
0581         regulator_list_voltage_pickable_linear_range, NULL,
0582         regulator_set_voltage_sel_pickable_regmap,
0583         regulator_get_voltage_sel_pickable_regmap,
0584         regulator_set_voltage_time_sel, NULL, bd718x7_set_buck_uvp,
0585         bd718x7_set_buck_ovp);
0586 
0587 BD718XX_OPS(bd718xx_ldo_regulator_ops, regulator_list_voltage_linear_range,
0588         NULL, bd718xx_set_voltage_sel_restricted,
0589         regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
0590         NULL);
0591 
0592 BD718XX_OPS(bd718xx_ldo_regulator_nolinear_ops, regulator_list_voltage_table,
0593         NULL, bd718xx_set_voltage_sel_restricted,
0594         regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
0595         NULL);
0596 
0597 BD718XX_OPS(bd718xx_buck_regulator_ops, regulator_list_voltage_linear_range,
0598         NULL, regulator_set_voltage_sel_regmap,
0599         regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
0600         NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
0601 
0602 BD718XX_OPS(bd718xx_buck_regulator_nolinear_ops, regulator_list_voltage_table,
0603         regulator_map_voltage_ascend, regulator_set_voltage_sel_regmap,
0604         regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
0605         NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
0606 
0607 /*
0608  * OPS for BD71837
0609  */
0610 BD718XX_OPS(bd71837_pickable_range_ldo_ops,
0611         regulator_list_voltage_pickable_linear_range, NULL,
0612         bd71837_set_voltage_sel_pickable_restricted,
0613         regulator_get_voltage_sel_pickable_regmap, NULL, NULL,
0614         bd718x7_set_ldo_uvp, NULL);
0615 
0616 BD718XX_OPS(bd71837_pickable_range_buck_ops,
0617         regulator_list_voltage_pickable_linear_range, NULL,
0618         bd71837_set_voltage_sel_pickable_restricted,
0619         regulator_get_voltage_sel_pickable_regmap,
0620         regulator_set_voltage_time_sel, NULL, bd718x7_set_buck_uvp,
0621         bd718x7_set_buck_ovp);
0622 
0623 BD718XX_OPS(bd71837_ldo_regulator_ops, regulator_list_voltage_linear_range,
0624         NULL, rohm_regulator_set_voltage_sel_restricted,
0625         regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
0626         NULL);
0627 
0628 BD718XX_OPS(bd71837_ldo_regulator_nolinear_ops, regulator_list_voltage_table,
0629         NULL, rohm_regulator_set_voltage_sel_restricted,
0630         regulator_get_voltage_sel_regmap, NULL, NULL, bd718x7_set_ldo_uvp,
0631         NULL);
0632 
0633 BD718XX_OPS(bd71837_buck_regulator_ops, regulator_list_voltage_linear_range,
0634         NULL, rohm_regulator_set_voltage_sel_restricted,
0635         regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
0636         NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
0637 
0638 BD718XX_OPS(bd71837_buck_regulator_nolinear_ops, regulator_list_voltage_table,
0639         regulator_map_voltage_ascend, rohm_regulator_set_voltage_sel_restricted,
0640         regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
0641         NULL, bd718x7_set_buck_uvp, bd718x7_set_buck_ovp);
0642 /*
0643  * BD71837 bucks 3 and 4 support defining their enable/disable state also
0644  * when buck enable state is under HW state machine control. In that case the
0645  * bit [2] in CTRL register is used to indicate if regulator should be ON.
0646  */
0647 static const struct regulator_ops bd71837_buck34_ops_hwctrl = {
0648     .is_enabled = bd71837_get_buck34_enable_hwctrl,
0649     .list_voltage = regulator_list_voltage_linear_range,
0650     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0651     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0652     .set_voltage_time_sel = regulator_set_voltage_time_sel,
0653     .set_ramp_delay = regulator_set_ramp_delay_regmap,
0654     .set_under_voltage_protection = bd718x7_set_buck_uvp,
0655     .set_over_voltage_protection = bd718x7_set_buck_ovp,
0656 };
0657 
0658 /*
0659  * OPS for all of the ICs - BD718(37/47/50)
0660  */
0661 BD718XX_OPS(bd718xx_dvs_buck_regulator_ops, regulator_list_voltage_linear_range,
0662         NULL, regulator_set_voltage_sel_regmap,
0663         regulator_get_voltage_sel_regmap, regulator_set_voltage_time_sel,
0664         regulator_set_ramp_delay_regmap, bd718x7_set_buck_uvp,
0665         bd718x7_set_buck_ovp);
0666 
0667 
0668 
0669 /*
0670  * There is a HW quirk in BD71837. The shutdown sequence timings for
0671  * bucks/LDOs which are controlled via register interface are changed.
0672  * At PMIC poweroff the voltage for BUCK6/7 is cut immediately at the
0673  * beginning of shut-down sequence. As bucks 6 and 7 are parent
0674  * supplies for LDO5 and LDO6 - this causes LDO5/6 voltage
0675  * monitoring to errorneously detect under voltage and force PMIC to
0676  * emergency state instead of poweroff. In order to avoid this we
0677  * disable voltage monitoring for LDO5 and LDO6
0678  */
0679 static const struct reg_init bd71837_ldo5_inits[] = {
0680     {
0681         .reg = BD718XX_REG_MVRFLTMASK2,
0682         .mask = BD718XX_LDO5_VRMON80,
0683         .val = BD718XX_LDO5_VRMON80,
0684     },
0685 };
0686 
0687 static const struct reg_init bd71837_ldo6_inits[] = {
0688     {
0689         .reg = BD718XX_REG_MVRFLTMASK2,
0690         .mask = BD718XX_LDO6_VRMON80,
0691         .val = BD718XX_LDO6_VRMON80,
0692     },
0693 };
0694 
0695 static int buck_set_hw_dvs_levels(struct device_node *np,
0696                 const struct regulator_desc *desc,
0697                 struct regulator_config *cfg)
0698 {
0699     struct bd718xx_regulator_data *data;
0700 
0701     data = container_of(desc, struct bd718xx_regulator_data, desc);
0702 
0703     return rohm_regulator_set_dvs_levels(&data->dvs, np, desc, cfg->regmap);
0704 }
0705 
0706 static const struct regulator_ops *bd71847_swcontrol_ops[] = {
0707     &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
0708     &bd718xx_pickable_range_buck_ops, &bd718xx_pickable_range_buck_ops,
0709     &bd718xx_buck_regulator_nolinear_ops, &bd718xx_buck_regulator_ops,
0710     &bd718xx_pickable_range_ldo_ops, &bd718xx_ldo_regulator_nolinear_ops,
0711     &bd718xx_ldo_regulator_ops, &bd718xx_ldo_regulator_ops,
0712     &bd718xx_pickable_range_ldo_ops, &bd718xx_ldo_regulator_ops,
0713 };
0714 
0715 static const struct regulator_ops *bd71847_hwcontrol_ops[] = {
0716     &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
0717     &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
0718     &BD718XX_HWOPNAME(bd718xx_pickable_range_buck_ops),
0719     &BD718XX_HWOPNAME(bd718xx_pickable_range_buck_ops),
0720     &BD718XX_HWOPNAME(bd718xx_buck_regulator_nolinear_ops),
0721     &BD718XX_HWOPNAME(bd718xx_buck_regulator_ops),
0722     &BD718XX_HWOPNAME(bd718xx_pickable_range_ldo_ops),
0723     &BD718XX_HWOPNAME(bd718xx_ldo_regulator_nolinear_ops),
0724     &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
0725     &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
0726     &bd718xx_ldo5_ops_hwstate,
0727     &BD718XX_HWOPNAME(bd718xx_ldo_regulator_ops),
0728 };
0729 
0730 static struct bd718xx_regulator_data bd71847_regulators[] = {
0731     {
0732         .desc = {
0733             .name = "buck1",
0734             .of_match = of_match_ptr("BUCK1"),
0735             .regulators_node = of_match_ptr("regulators"),
0736             .id = BD718XX_BUCK1,
0737             .type = REGULATOR_VOLTAGE,
0738             .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
0739             .linear_ranges = bd718xx_dvs_buck_volts,
0740             .n_linear_ranges =
0741                 ARRAY_SIZE(bd718xx_dvs_buck_volts),
0742             .vsel_reg = BD718XX_REG_BUCK1_VOLT_RUN,
0743             .vsel_mask = DVS_BUCK_RUN_MASK,
0744             .enable_reg = BD718XX_REG_BUCK1_CTRL,
0745             .enable_mask = BD718XX_BUCK_EN,
0746             .enable_time = BD71847_BUCK1_STARTUP_TIME,
0747             .owner = THIS_MODULE,
0748             .ramp_delay_table = bd718xx_ramp_delay,
0749             .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
0750             .ramp_reg = BD718XX_REG_BUCK1_CTRL,
0751             .ramp_mask = BUCK_RAMPRATE_MASK,
0752             .of_parse_cb = buck_set_hw_dvs_levels,
0753         },
0754         .dvs = {
0755             .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE |
0756                      ROHM_DVS_LEVEL_SUSPEND,
0757             .run_reg = BD718XX_REG_BUCK1_VOLT_RUN,
0758             .run_mask = DVS_BUCK_RUN_MASK,
0759             .idle_reg = BD718XX_REG_BUCK1_VOLT_IDLE,
0760             .idle_mask = DVS_BUCK_RUN_MASK,
0761             .suspend_reg = BD718XX_REG_BUCK1_VOLT_SUSP,
0762             .suspend_mask = DVS_BUCK_RUN_MASK,
0763         },
0764         .init = {
0765             .reg = BD718XX_REG_BUCK1_CTRL,
0766             .mask = BD718XX_BUCK_SEL,
0767             .val = BD718XX_BUCK_SEL,
0768         },
0769     },
0770     {
0771         .desc = {
0772             .name = "buck2",
0773             .of_match = of_match_ptr("BUCK2"),
0774             .regulators_node = of_match_ptr("regulators"),
0775             .id = BD718XX_BUCK2,
0776             .type = REGULATOR_VOLTAGE,
0777             .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
0778             .linear_ranges = bd718xx_dvs_buck_volts,
0779             .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
0780             .vsel_reg = BD718XX_REG_BUCK2_VOLT_RUN,
0781             .vsel_mask = DVS_BUCK_RUN_MASK,
0782             .enable_reg = BD718XX_REG_BUCK2_CTRL,
0783             .enable_mask = BD718XX_BUCK_EN,
0784             .enable_time = BD71847_BUCK2_STARTUP_TIME,
0785             .ramp_delay_table = bd718xx_ramp_delay,
0786             .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
0787             .ramp_reg = BD718XX_REG_BUCK2_CTRL,
0788             .ramp_mask = BUCK_RAMPRATE_MASK,
0789             .owner = THIS_MODULE,
0790             .of_parse_cb = buck_set_hw_dvs_levels,
0791         },
0792         .dvs = {
0793             .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE,
0794             .run_reg = BD718XX_REG_BUCK2_VOLT_RUN,
0795             .run_mask = DVS_BUCK_RUN_MASK,
0796             .idle_reg = BD718XX_REG_BUCK2_VOLT_IDLE,
0797             .idle_mask = DVS_BUCK_RUN_MASK,
0798         },
0799         .init = {
0800             .reg = BD718XX_REG_BUCK2_CTRL,
0801             .mask = BD718XX_BUCK_SEL,
0802             .val = BD718XX_BUCK_SEL,
0803         },
0804     },
0805     {
0806         .desc = {
0807             .name = "buck3",
0808             .of_match = of_match_ptr("BUCK3"),
0809             .regulators_node = of_match_ptr("regulators"),
0810             .id = BD718XX_BUCK3,
0811             .type = REGULATOR_VOLTAGE,
0812             .n_voltages = BD71847_BUCK3_VOLTAGE_NUM,
0813             .linear_ranges = bd71847_buck3_volts,
0814             .n_linear_ranges =
0815                 ARRAY_SIZE(bd71847_buck3_volts),
0816             .vsel_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
0817             .vsel_mask = BD718XX_1ST_NODVS_BUCK_MASK,
0818             .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
0819             .vsel_range_mask = BD71847_BUCK3_RANGE_MASK,
0820             .linear_range_selectors = bd71847_buck3_volt_range_sel,
0821             .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
0822             .enable_mask = BD718XX_BUCK_EN,
0823             .enable_time = BD71847_BUCK3_STARTUP_TIME,
0824             .owner = THIS_MODULE,
0825         },
0826         .init = {
0827             .reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
0828             .mask = BD718XX_BUCK_SEL,
0829             .val = BD718XX_BUCK_SEL,
0830         },
0831     },
0832     {
0833         .desc = {
0834             .name = "buck4",
0835             .of_match = of_match_ptr("BUCK4"),
0836             .regulators_node = of_match_ptr("regulators"),
0837             .id = BD718XX_BUCK4,
0838             .type = REGULATOR_VOLTAGE,
0839             .n_voltages = BD71847_BUCK4_VOLTAGE_NUM,
0840             .linear_ranges = bd71847_buck4_volts,
0841             .n_linear_ranges =
0842                 ARRAY_SIZE(bd71847_buck4_volts),
0843             .enable_reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
0844             .vsel_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
0845             .vsel_mask = BD71847_BUCK4_MASK,
0846             .vsel_range_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
0847             .vsel_range_mask = BD71847_BUCK4_RANGE_MASK,
0848             .linear_range_selectors = bd71847_buck4_volt_range_sel,
0849             .enable_mask = BD718XX_BUCK_EN,
0850             .enable_time = BD71847_BUCK4_STARTUP_TIME,
0851             .owner = THIS_MODULE,
0852         },
0853         .init = {
0854             .reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
0855             .mask = BD718XX_BUCK_SEL,
0856             .val = BD718XX_BUCK_SEL,
0857         },
0858     },
0859     {
0860         .desc = {
0861             .name = "buck5",
0862             .of_match = of_match_ptr("BUCK5"),
0863             .regulators_node = of_match_ptr("regulators"),
0864             .id = BD718XX_BUCK5,
0865             .type = REGULATOR_VOLTAGE,
0866             .volt_table = &bd718xx_3rd_nodvs_buck_volts[0],
0867             .n_voltages = ARRAY_SIZE(bd718xx_3rd_nodvs_buck_volts),
0868             .vsel_reg = BD718XX_REG_3RD_NODVS_BUCK_VOLT,
0869             .vsel_mask = BD718XX_3RD_NODVS_BUCK_MASK,
0870             .enable_reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
0871             .enable_mask = BD718XX_BUCK_EN,
0872             .enable_time = BD71847_BUCK5_STARTUP_TIME,
0873             .owner = THIS_MODULE,
0874         },
0875         .init = {
0876             .reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
0877             .mask = BD718XX_BUCK_SEL,
0878             .val = BD718XX_BUCK_SEL,
0879         },
0880     },
0881     {
0882         .desc = {
0883             .name = "buck6",
0884             .of_match = of_match_ptr("BUCK6"),
0885             .regulators_node = of_match_ptr("regulators"),
0886             .id = BD718XX_BUCK6,
0887             .type = REGULATOR_VOLTAGE,
0888             .n_voltages = BD718XX_4TH_NODVS_BUCK_VOLTAGE_NUM,
0889             .linear_ranges = bd718xx_4th_nodvs_buck_volts,
0890             .n_linear_ranges =
0891                 ARRAY_SIZE(bd718xx_4th_nodvs_buck_volts),
0892             .vsel_reg = BD718XX_REG_4TH_NODVS_BUCK_VOLT,
0893             .vsel_mask = BD718XX_4TH_NODVS_BUCK_MASK,
0894             .enable_reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
0895             .enable_mask = BD718XX_BUCK_EN,
0896             .enable_time = BD71847_BUCK6_STARTUP_TIME,
0897             .owner = THIS_MODULE,
0898         },
0899         .init = {
0900             .reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
0901             .mask = BD718XX_BUCK_SEL,
0902             .val = BD718XX_BUCK_SEL,
0903         },
0904     },
0905     {
0906         .desc = {
0907             .name = "ldo1",
0908             .of_match = of_match_ptr("LDO1"),
0909             .regulators_node = of_match_ptr("regulators"),
0910             .id = BD718XX_LDO1,
0911             .type = REGULATOR_VOLTAGE,
0912             .n_voltages = BD718XX_LDO1_VOLTAGE_NUM,
0913             .linear_ranges = bd718xx_ldo1_volts,
0914             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo1_volts),
0915             .vsel_reg = BD718XX_REG_LDO1_VOLT,
0916             .vsel_mask = BD718XX_LDO1_MASK,
0917             .vsel_range_reg = BD718XX_REG_LDO1_VOLT,
0918             .vsel_range_mask = BD718XX_LDO1_RANGE_MASK,
0919             .linear_range_selectors = bd718xx_ldo1_volt_range_sel,
0920             .enable_reg = BD718XX_REG_LDO1_VOLT,
0921             .enable_mask = BD718XX_LDO_EN,
0922             .enable_time = BD71847_LDO1_STARTUP_TIME,
0923             .owner = THIS_MODULE,
0924         },
0925         .init = {
0926             .reg = BD718XX_REG_LDO1_VOLT,
0927             .mask = BD718XX_LDO_SEL,
0928             .val = BD718XX_LDO_SEL,
0929         },
0930     },
0931     {
0932         .desc = {
0933             .name = "ldo2",
0934             .of_match = of_match_ptr("LDO2"),
0935             .regulators_node = of_match_ptr("regulators"),
0936             .id = BD718XX_LDO2,
0937             .type = REGULATOR_VOLTAGE,
0938             .volt_table = &ldo_2_volts[0],
0939             .vsel_reg = BD718XX_REG_LDO2_VOLT,
0940             .vsel_mask = BD718XX_LDO2_MASK,
0941             .n_voltages = ARRAY_SIZE(ldo_2_volts),
0942             .enable_reg = BD718XX_REG_LDO2_VOLT,
0943             .enable_mask = BD718XX_LDO_EN,
0944             .enable_time = BD71847_LDO2_STARTUP_TIME,
0945             .owner = THIS_MODULE,
0946         },
0947         .init = {
0948             .reg = BD718XX_REG_LDO2_VOLT,
0949             .mask = BD718XX_LDO_SEL,
0950             .val = BD718XX_LDO_SEL,
0951         },
0952     },
0953     {
0954         .desc = {
0955             .name = "ldo3",
0956             .of_match = of_match_ptr("LDO3"),
0957             .regulators_node = of_match_ptr("regulators"),
0958             .id = BD718XX_LDO3,
0959             .type = REGULATOR_VOLTAGE,
0960             .n_voltages = BD718XX_LDO3_VOLTAGE_NUM,
0961             .linear_ranges = bd718xx_ldo3_volts,
0962             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo3_volts),
0963             .vsel_reg = BD718XX_REG_LDO3_VOLT,
0964             .vsel_mask = BD718XX_LDO3_MASK,
0965             .enable_reg = BD718XX_REG_LDO3_VOLT,
0966             .enable_mask = BD718XX_LDO_EN,
0967             .enable_time = BD71847_LDO3_STARTUP_TIME,
0968             .owner = THIS_MODULE,
0969         },
0970         .init = {
0971             .reg = BD718XX_REG_LDO3_VOLT,
0972             .mask = BD718XX_LDO_SEL,
0973             .val = BD718XX_LDO_SEL,
0974         },
0975     },
0976     {
0977         .desc = {
0978             .name = "ldo4",
0979             .of_match = of_match_ptr("LDO4"),
0980             .regulators_node = of_match_ptr("regulators"),
0981             .id = BD718XX_LDO4,
0982             .type = REGULATOR_VOLTAGE,
0983             .n_voltages = BD718XX_LDO4_VOLTAGE_NUM,
0984             .linear_ranges = bd718xx_ldo4_volts,
0985             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo4_volts),
0986             .vsel_reg = BD718XX_REG_LDO4_VOLT,
0987             .vsel_mask = BD718XX_LDO4_MASK,
0988             .enable_reg = BD718XX_REG_LDO4_VOLT,
0989             .enable_mask = BD718XX_LDO_EN,
0990             .enable_time = BD71847_LDO4_STARTUP_TIME,
0991             .owner = THIS_MODULE,
0992         },
0993         .init = {
0994             .reg = BD718XX_REG_LDO4_VOLT,
0995             .mask = BD718XX_LDO_SEL,
0996             .val = BD718XX_LDO_SEL,
0997         },
0998     },
0999     {
1000         .desc = {
1001             .name = "ldo5",
1002             .of_match = of_match_ptr("LDO5"),
1003             .regulators_node = of_match_ptr("regulators"),
1004             .id = BD718XX_LDO5,
1005             .type = REGULATOR_VOLTAGE,
1006             .n_voltages = BD71847_LDO5_VOLTAGE_NUM,
1007             .linear_ranges = bd71847_ldo5_volts,
1008             .n_linear_ranges = ARRAY_SIZE(bd71847_ldo5_volts),
1009             .vsel_reg = BD718XX_REG_LDO5_VOLT,
1010             .vsel_mask = BD71847_LDO5_MASK,
1011             .vsel_range_reg = BD718XX_REG_LDO5_VOLT,
1012             .vsel_range_mask = BD71847_LDO5_RANGE_MASK,
1013             .linear_range_selectors = bd71847_ldo5_volt_range_sel,
1014             .enable_reg = BD718XX_REG_LDO5_VOLT,
1015             .enable_mask = BD718XX_LDO_EN,
1016             .enable_time = BD71847_LDO5_STARTUP_TIME,
1017             .owner = THIS_MODULE,
1018         },
1019         .init = {
1020             .reg = BD718XX_REG_LDO5_VOLT,
1021             .mask = BD718XX_LDO_SEL,
1022             .val = BD718XX_LDO_SEL,
1023         },
1024     },
1025     {
1026         .desc = {
1027             .name = "ldo6",
1028             .of_match = of_match_ptr("LDO6"),
1029             .regulators_node = of_match_ptr("regulators"),
1030             .id = BD718XX_LDO6,
1031             .type = REGULATOR_VOLTAGE,
1032             .n_voltages = BD718XX_LDO6_VOLTAGE_NUM,
1033             .linear_ranges = bd718xx_ldo6_volts,
1034             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo6_volts),
1035             /* LDO6 is supplied by buck5 */
1036             .supply_name = "buck5",
1037             .vsel_reg = BD718XX_REG_LDO6_VOLT,
1038             .vsel_mask = BD718XX_LDO6_MASK,
1039             .enable_reg = BD718XX_REG_LDO6_VOLT,
1040             .enable_mask = BD718XX_LDO_EN,
1041             .enable_time = BD71847_LDO6_STARTUP_TIME,
1042             .owner = THIS_MODULE,
1043         },
1044         .init = {
1045             .reg = BD718XX_REG_LDO6_VOLT,
1046             .mask = BD718XX_LDO_SEL,
1047             .val = BD718XX_LDO_SEL,
1048         },
1049     },
1050 };
1051 
1052 static const struct regulator_ops *bd71837_swcontrol_ops[] = {
1053     &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
1054     &bd718xx_dvs_buck_regulator_ops, &bd718xx_dvs_buck_regulator_ops,
1055     &bd71837_pickable_range_buck_ops, &bd71837_buck_regulator_ops,
1056     &bd71837_buck_regulator_nolinear_ops, &bd71837_buck_regulator_ops,
1057     &bd71837_pickable_range_ldo_ops, &bd71837_ldo_regulator_nolinear_ops,
1058     &bd71837_ldo_regulator_ops, &bd71837_ldo_regulator_ops,
1059     &bd71837_ldo_regulator_ops, &bd71837_ldo_regulator_ops,
1060     &bd71837_ldo_regulator_ops,
1061 };
1062 
1063 static const struct regulator_ops *bd71837_hwcontrol_ops[] = {
1064     &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
1065     &BD718XX_HWOPNAME(bd718xx_dvs_buck_regulator_ops),
1066     &bd71837_buck34_ops_hwctrl, &bd71837_buck34_ops_hwctrl,
1067     &BD718XX_HWOPNAME(bd71837_pickable_range_buck_ops),
1068     &BD718XX_HWOPNAME(bd71837_buck_regulator_ops),
1069     &BD718XX_HWOPNAME(bd71837_buck_regulator_nolinear_ops),
1070     &BD718XX_HWOPNAME(bd71837_buck_regulator_ops),
1071     &BD718XX_HWOPNAME(bd71837_pickable_range_ldo_ops),
1072     &BD718XX_HWOPNAME(bd71837_ldo_regulator_nolinear_ops),
1073     &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1074     &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1075     &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1076     &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1077     &BD718XX_HWOPNAME(bd71837_ldo_regulator_ops),
1078 };
1079 
1080 static struct bd718xx_regulator_data bd71837_regulators[] = {
1081     {
1082         .desc = {
1083             .name = "buck1",
1084             .of_match = of_match_ptr("BUCK1"),
1085             .regulators_node = of_match_ptr("regulators"),
1086             .id = BD718XX_BUCK1,
1087             .type = REGULATOR_VOLTAGE,
1088             .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1089             .linear_ranges = bd718xx_dvs_buck_volts,
1090             .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1091             .vsel_reg = BD718XX_REG_BUCK1_VOLT_RUN,
1092             .vsel_mask = DVS_BUCK_RUN_MASK,
1093             .enable_reg = BD718XX_REG_BUCK1_CTRL,
1094             .enable_mask = BD718XX_BUCK_EN,
1095             .enable_time = BD71837_BUCK1_STARTUP_TIME,
1096             .ramp_delay_table = bd718xx_ramp_delay,
1097             .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1098             .ramp_reg = BD718XX_REG_BUCK1_CTRL,
1099             .ramp_mask = BUCK_RAMPRATE_MASK,
1100             .owner = THIS_MODULE,
1101             .of_parse_cb = buck_set_hw_dvs_levels,
1102         },
1103         .dvs = {
1104             .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE |
1105                      ROHM_DVS_LEVEL_SUSPEND,
1106             .run_reg = BD718XX_REG_BUCK1_VOLT_RUN,
1107             .run_mask = DVS_BUCK_RUN_MASK,
1108             .idle_reg = BD718XX_REG_BUCK1_VOLT_IDLE,
1109             .idle_mask = DVS_BUCK_RUN_MASK,
1110             .suspend_reg = BD718XX_REG_BUCK1_VOLT_SUSP,
1111             .suspend_mask = DVS_BUCK_RUN_MASK,
1112         },
1113         .init = {
1114             .reg = BD718XX_REG_BUCK1_CTRL,
1115             .mask = BD718XX_BUCK_SEL,
1116             .val = BD718XX_BUCK_SEL,
1117         },
1118     },
1119     {
1120         .desc = {
1121             .name = "buck2",
1122             .of_match = of_match_ptr("BUCK2"),
1123             .regulators_node = of_match_ptr("regulators"),
1124             .id = BD718XX_BUCK2,
1125             .type = REGULATOR_VOLTAGE,
1126             .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1127             .linear_ranges = bd718xx_dvs_buck_volts,
1128             .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1129             .vsel_reg = BD718XX_REG_BUCK2_VOLT_RUN,
1130             .vsel_mask = DVS_BUCK_RUN_MASK,
1131             .enable_reg = BD718XX_REG_BUCK2_CTRL,
1132             .enable_mask = BD718XX_BUCK_EN,
1133             .enable_time = BD71837_BUCK2_STARTUP_TIME,
1134             .ramp_delay_table = bd718xx_ramp_delay,
1135             .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1136             .ramp_reg = BD718XX_REG_BUCK2_CTRL,
1137             .ramp_mask = BUCK_RAMPRATE_MASK,
1138             .owner = THIS_MODULE,
1139             .of_parse_cb = buck_set_hw_dvs_levels,
1140         },
1141         .dvs = {
1142             .level_map = ROHM_DVS_LEVEL_RUN | ROHM_DVS_LEVEL_IDLE,
1143             .run_reg = BD718XX_REG_BUCK2_VOLT_RUN,
1144             .run_mask = DVS_BUCK_RUN_MASK,
1145             .idle_reg = BD718XX_REG_BUCK2_VOLT_IDLE,
1146             .idle_mask = DVS_BUCK_RUN_MASK,
1147         },
1148         .init = {
1149             .reg = BD718XX_REG_BUCK2_CTRL,
1150             .mask = BD718XX_BUCK_SEL,
1151             .val = BD718XX_BUCK_SEL,
1152         },
1153     },
1154     {
1155         .desc = {
1156             .name = "buck3",
1157             .of_match = of_match_ptr("BUCK3"),
1158             .regulators_node = of_match_ptr("regulators"),
1159             .id = BD718XX_BUCK3,
1160             .type = REGULATOR_VOLTAGE,
1161             .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1162             .linear_ranges = bd718xx_dvs_buck_volts,
1163             .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1164             .vsel_reg = BD71837_REG_BUCK3_VOLT_RUN,
1165             .vsel_mask = DVS_BUCK_RUN_MASK,
1166             .enable_reg = BD71837_REG_BUCK3_CTRL,
1167             .enable_mask = BD718XX_BUCK_EN,
1168             .enable_time = BD71837_BUCK3_STARTUP_TIME,
1169             .ramp_delay_table = bd718xx_ramp_delay,
1170             .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1171             .ramp_reg = BD71837_REG_BUCK3_CTRL,
1172             .ramp_mask = BUCK_RAMPRATE_MASK,
1173             .owner = THIS_MODULE,
1174             .of_parse_cb = buck_set_hw_dvs_levels,
1175         },
1176         .dvs = {
1177             .level_map = ROHM_DVS_LEVEL_RUN,
1178             .run_reg = BD71837_REG_BUCK3_VOLT_RUN,
1179             .run_mask = DVS_BUCK_RUN_MASK,
1180         },
1181         .init = {
1182             .reg = BD71837_REG_BUCK3_CTRL,
1183             .mask = BD718XX_BUCK_SEL,
1184             .val = BD718XX_BUCK_SEL,
1185         },
1186     },
1187     {
1188         .desc = {
1189             .name = "buck4",
1190             .of_match = of_match_ptr("BUCK4"),
1191             .regulators_node = of_match_ptr("regulators"),
1192             .id = BD718XX_BUCK4,
1193             .type = REGULATOR_VOLTAGE,
1194             .n_voltages = BD718XX_DVS_BUCK_VOLTAGE_NUM,
1195             .linear_ranges = bd718xx_dvs_buck_volts,
1196             .n_linear_ranges = ARRAY_SIZE(bd718xx_dvs_buck_volts),
1197             .vsel_reg = BD71837_REG_BUCK4_VOLT_RUN,
1198             .vsel_mask = DVS_BUCK_RUN_MASK,
1199             .enable_reg = BD71837_REG_BUCK4_CTRL,
1200             .enable_mask = BD718XX_BUCK_EN,
1201             .enable_time = BD71837_BUCK4_STARTUP_TIME,
1202             .ramp_delay_table = bd718xx_ramp_delay,
1203             .n_ramp_values = ARRAY_SIZE(bd718xx_ramp_delay),
1204             .ramp_reg = BD71837_REG_BUCK4_CTRL,
1205             .ramp_mask = BUCK_RAMPRATE_MASK,
1206             .owner = THIS_MODULE,
1207             .of_parse_cb = buck_set_hw_dvs_levels,
1208         },
1209         .dvs = {
1210             .level_map = ROHM_DVS_LEVEL_RUN,
1211             .run_reg = BD71837_REG_BUCK4_VOLT_RUN,
1212             .run_mask = DVS_BUCK_RUN_MASK,
1213         },
1214         .init = {
1215             .reg = BD71837_REG_BUCK4_CTRL,
1216             .mask = BD718XX_BUCK_SEL,
1217             .val = BD718XX_BUCK_SEL,
1218         },
1219     },
1220     {
1221         .desc = {
1222             .name = "buck5",
1223             .of_match = of_match_ptr("BUCK5"),
1224             .regulators_node = of_match_ptr("regulators"),
1225             .id = BD718XX_BUCK5,
1226             .type = REGULATOR_VOLTAGE,
1227             .n_voltages = BD71837_BUCK5_VOLTAGE_NUM,
1228             .linear_ranges = bd71837_buck5_volts,
1229             .n_linear_ranges =
1230                 ARRAY_SIZE(bd71837_buck5_volts),
1231             .vsel_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
1232             .vsel_mask = BD71837_BUCK5_MASK,
1233             .vsel_range_reg = BD718XX_REG_1ST_NODVS_BUCK_VOLT,
1234             .vsel_range_mask = BD71837_BUCK5_RANGE_MASK,
1235             .linear_range_selectors = bd71837_buck5_volt_range_sel,
1236             .enable_reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
1237             .enable_mask = BD718XX_BUCK_EN,
1238             .enable_time = BD71837_BUCK5_STARTUP_TIME,
1239             .owner = THIS_MODULE,
1240         },
1241         .init = {
1242             .reg = BD718XX_REG_1ST_NODVS_BUCK_CTRL,
1243             .mask = BD718XX_BUCK_SEL,
1244             .val = BD718XX_BUCK_SEL,
1245         },
1246     },
1247     {
1248         .desc = {
1249             .name = "buck6",
1250             .of_match = of_match_ptr("BUCK6"),
1251             .regulators_node = of_match_ptr("regulators"),
1252             .id = BD718XX_BUCK6,
1253             .type = REGULATOR_VOLTAGE,
1254             .n_voltages = BD71837_BUCK6_VOLTAGE_NUM,
1255             .linear_ranges = bd71837_buck6_volts,
1256             .n_linear_ranges =
1257                 ARRAY_SIZE(bd71837_buck6_volts),
1258             .vsel_reg = BD718XX_REG_2ND_NODVS_BUCK_VOLT,
1259             .vsel_mask = BD71837_BUCK6_MASK,
1260             .enable_reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
1261             .enable_mask = BD718XX_BUCK_EN,
1262             .enable_time = BD71837_BUCK6_STARTUP_TIME,
1263             .owner = THIS_MODULE,
1264         },
1265         .init = {
1266             .reg = BD718XX_REG_2ND_NODVS_BUCK_CTRL,
1267             .mask = BD718XX_BUCK_SEL,
1268             .val = BD718XX_BUCK_SEL,
1269         },
1270     },
1271     {
1272         .desc = {
1273             .name = "buck7",
1274             .of_match = of_match_ptr("BUCK7"),
1275             .regulators_node = of_match_ptr("regulators"),
1276             .id = BD718XX_BUCK7,
1277             .type = REGULATOR_VOLTAGE,
1278             .volt_table = &bd718xx_3rd_nodvs_buck_volts[0],
1279             .n_voltages = ARRAY_SIZE(bd718xx_3rd_nodvs_buck_volts),
1280             .vsel_reg = BD718XX_REG_3RD_NODVS_BUCK_VOLT,
1281             .vsel_mask = BD718XX_3RD_NODVS_BUCK_MASK,
1282             .enable_reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
1283             .enable_mask = BD718XX_BUCK_EN,
1284             .enable_time = BD71837_BUCK7_STARTUP_TIME,
1285             .owner = THIS_MODULE,
1286         },
1287         .init = {
1288             .reg = BD718XX_REG_3RD_NODVS_BUCK_CTRL,
1289             .mask = BD718XX_BUCK_SEL,
1290             .val = BD718XX_BUCK_SEL,
1291         },
1292     },
1293     {
1294         .desc = {
1295             .name = "buck8",
1296             .of_match = of_match_ptr("BUCK8"),
1297             .regulators_node = of_match_ptr("regulators"),
1298             .id = BD718XX_BUCK8,
1299             .type = REGULATOR_VOLTAGE,
1300             .n_voltages = BD718XX_4TH_NODVS_BUCK_VOLTAGE_NUM,
1301             .linear_ranges = bd718xx_4th_nodvs_buck_volts,
1302             .n_linear_ranges =
1303                 ARRAY_SIZE(bd718xx_4th_nodvs_buck_volts),
1304             .vsel_reg = BD718XX_REG_4TH_NODVS_BUCK_VOLT,
1305             .vsel_mask = BD718XX_4TH_NODVS_BUCK_MASK,
1306             .enable_reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
1307             .enable_mask = BD718XX_BUCK_EN,
1308             .enable_time = BD71837_BUCK8_STARTUP_TIME,
1309             .owner = THIS_MODULE,
1310         },
1311         .init = {
1312             .reg = BD718XX_REG_4TH_NODVS_BUCK_CTRL,
1313             .mask = BD718XX_BUCK_SEL,
1314             .val = BD718XX_BUCK_SEL,
1315         },
1316     },
1317     {
1318         .desc = {
1319             .name = "ldo1",
1320             .of_match = of_match_ptr("LDO1"),
1321             .regulators_node = of_match_ptr("regulators"),
1322             .id = BD718XX_LDO1,
1323             .type = REGULATOR_VOLTAGE,
1324             .n_voltages = BD718XX_LDO1_VOLTAGE_NUM,
1325             .linear_ranges = bd718xx_ldo1_volts,
1326             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo1_volts),
1327             .vsel_reg = BD718XX_REG_LDO1_VOLT,
1328             .vsel_mask = BD718XX_LDO1_MASK,
1329             .vsel_range_reg = BD718XX_REG_LDO1_VOLT,
1330             .vsel_range_mask = BD718XX_LDO1_RANGE_MASK,
1331             .linear_range_selectors = bd718xx_ldo1_volt_range_sel,
1332             .enable_reg = BD718XX_REG_LDO1_VOLT,
1333             .enable_mask = BD718XX_LDO_EN,
1334             .enable_time = BD71837_LDO1_STARTUP_TIME,
1335             .owner = THIS_MODULE,
1336         },
1337         .init = {
1338             .reg = BD718XX_REG_LDO1_VOLT,
1339             .mask = BD718XX_LDO_SEL,
1340             .val = BD718XX_LDO_SEL,
1341         },
1342     },
1343     {
1344         .desc = {
1345             .name = "ldo2",
1346             .of_match = of_match_ptr("LDO2"),
1347             .regulators_node = of_match_ptr("regulators"),
1348             .id = BD718XX_LDO2,
1349             .type = REGULATOR_VOLTAGE,
1350             .volt_table = &ldo_2_volts[0],
1351             .vsel_reg = BD718XX_REG_LDO2_VOLT,
1352             .vsel_mask = BD718XX_LDO2_MASK,
1353             .n_voltages = ARRAY_SIZE(ldo_2_volts),
1354             .enable_reg = BD718XX_REG_LDO2_VOLT,
1355             .enable_mask = BD718XX_LDO_EN,
1356             .enable_time = BD71837_LDO2_STARTUP_TIME,
1357             .owner = THIS_MODULE,
1358         },
1359         .init = {
1360             .reg = BD718XX_REG_LDO2_VOLT,
1361             .mask = BD718XX_LDO_SEL,
1362             .val = BD718XX_LDO_SEL,
1363         },
1364     },
1365     {
1366         .desc = {
1367             .name = "ldo3",
1368             .of_match = of_match_ptr("LDO3"),
1369             .regulators_node = of_match_ptr("regulators"),
1370             .id = BD718XX_LDO3,
1371             .type = REGULATOR_VOLTAGE,
1372             .n_voltages = BD718XX_LDO3_VOLTAGE_NUM,
1373             .linear_ranges = bd718xx_ldo3_volts,
1374             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo3_volts),
1375             .vsel_reg = BD718XX_REG_LDO3_VOLT,
1376             .vsel_mask = BD718XX_LDO3_MASK,
1377             .enable_reg = BD718XX_REG_LDO3_VOLT,
1378             .enable_mask = BD718XX_LDO_EN,
1379             .enable_time = BD71837_LDO3_STARTUP_TIME,
1380             .owner = THIS_MODULE,
1381         },
1382         .init = {
1383             .reg = BD718XX_REG_LDO3_VOLT,
1384             .mask = BD718XX_LDO_SEL,
1385             .val = BD718XX_LDO_SEL,
1386         },
1387     },
1388     {
1389         .desc = {
1390             .name = "ldo4",
1391             .of_match = of_match_ptr("LDO4"),
1392             .regulators_node = of_match_ptr("regulators"),
1393             .id = BD718XX_LDO4,
1394             .type = REGULATOR_VOLTAGE,
1395             .n_voltages = BD718XX_LDO4_VOLTAGE_NUM,
1396             .linear_ranges = bd718xx_ldo4_volts,
1397             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo4_volts),
1398             .vsel_reg = BD718XX_REG_LDO4_VOLT,
1399             .vsel_mask = BD718XX_LDO4_MASK,
1400             .enable_reg = BD718XX_REG_LDO4_VOLT,
1401             .enable_mask = BD718XX_LDO_EN,
1402             .enable_time = BD71837_LDO4_STARTUP_TIME,
1403             .owner = THIS_MODULE,
1404         },
1405         .init = {
1406             .reg = BD718XX_REG_LDO4_VOLT,
1407             .mask = BD718XX_LDO_SEL,
1408             .val = BD718XX_LDO_SEL,
1409         },
1410     },
1411     {
1412         .desc = {
1413             .name = "ldo5",
1414             .of_match = of_match_ptr("LDO5"),
1415             .regulators_node = of_match_ptr("regulators"),
1416             .id = BD718XX_LDO5,
1417             .type = REGULATOR_VOLTAGE,
1418             .n_voltages = BD71837_LDO5_VOLTAGE_NUM,
1419             .linear_ranges = bd71837_ldo5_volts,
1420             .n_linear_ranges = ARRAY_SIZE(bd71837_ldo5_volts),
1421             /* LDO5 is supplied by buck6 */
1422             .supply_name = "buck6",
1423             .vsel_reg = BD718XX_REG_LDO5_VOLT,
1424             .vsel_mask = BD71837_LDO5_MASK,
1425             .enable_reg = BD718XX_REG_LDO5_VOLT,
1426             .enable_mask = BD718XX_LDO_EN,
1427             .enable_time = BD71837_LDO5_STARTUP_TIME,
1428             .owner = THIS_MODULE,
1429         },
1430         .init = {
1431             .reg = BD718XX_REG_LDO5_VOLT,
1432             .mask = BD718XX_LDO_SEL,
1433             .val = BD718XX_LDO_SEL,
1434         },
1435         .additional_inits = bd71837_ldo5_inits,
1436         .additional_init_amnt = ARRAY_SIZE(bd71837_ldo5_inits),
1437     },
1438     {
1439         .desc = {
1440             .name = "ldo6",
1441             .of_match = of_match_ptr("LDO6"),
1442             .regulators_node = of_match_ptr("regulators"),
1443             .id = BD718XX_LDO6,
1444             .type = REGULATOR_VOLTAGE,
1445             .n_voltages = BD718XX_LDO6_VOLTAGE_NUM,
1446             .linear_ranges = bd718xx_ldo6_volts,
1447             .n_linear_ranges = ARRAY_SIZE(bd718xx_ldo6_volts),
1448             /* LDO6 is supplied by buck7 */
1449             .supply_name = "buck7",
1450             .vsel_reg = BD718XX_REG_LDO6_VOLT,
1451             .vsel_mask = BD718XX_LDO6_MASK,
1452             .enable_reg = BD718XX_REG_LDO6_VOLT,
1453             .enable_mask = BD718XX_LDO_EN,
1454             .enable_time = BD71837_LDO6_STARTUP_TIME,
1455             .owner = THIS_MODULE,
1456         },
1457         .init = {
1458             .reg = BD718XX_REG_LDO6_VOLT,
1459             .mask = BD718XX_LDO_SEL,
1460             .val = BD718XX_LDO_SEL,
1461         },
1462         .additional_inits = bd71837_ldo6_inits,
1463         .additional_init_amnt = ARRAY_SIZE(bd71837_ldo6_inits),
1464     },
1465     {
1466         .desc = {
1467             .name = "ldo7",
1468             .of_match = of_match_ptr("LDO7"),
1469             .regulators_node = of_match_ptr("regulators"),
1470             .id = BD718XX_LDO7,
1471             .type = REGULATOR_VOLTAGE,
1472             .n_voltages = BD71837_LDO7_VOLTAGE_NUM,
1473             .linear_ranges = bd71837_ldo7_volts,
1474             .n_linear_ranges = ARRAY_SIZE(bd71837_ldo7_volts),
1475             .vsel_reg = BD71837_REG_LDO7_VOLT,
1476             .vsel_mask = BD71837_LDO7_MASK,
1477             .enable_reg = BD71837_REG_LDO7_VOLT,
1478             .enable_mask = BD718XX_LDO_EN,
1479             .enable_time = BD71837_LDO7_STARTUP_TIME,
1480             .owner = THIS_MODULE,
1481         },
1482         .init = {
1483             .reg = BD71837_REG_LDO7_VOLT,
1484             .mask = BD718XX_LDO_SEL,
1485             .val = BD718XX_LDO_SEL,
1486         },
1487     },
1488 };
1489 
1490 static void mark_hw_controlled(struct device *dev, struct device_node *np,
1491                    struct bd718xx_regulator_data *reg_data,
1492                    unsigned int num_reg_data, int *info)
1493 {
1494     int i;
1495 
1496     for (i = 1; i <= num_reg_data; i++) {
1497         if (!of_node_name_eq(np, reg_data[i-1].desc.of_match))
1498             continue;
1499 
1500         *info |= 1 << (i - 1);
1501         dev_dbg(dev, "regulator %d runlevel controlled\n", i);
1502         return;
1503     }
1504     dev_warn(dev, "Bad regulator node\n");
1505 }
1506 
1507 /*
1508  * Setups where regulator (especially the buck8) output voltage is scaled
1509  * by adding external connection where some other regulator output is connected
1510  * to feedback-pin (over suitable resistors) is getting popular amongst users
1511  * of BD71837. (This allows for example scaling down the buck8 voltages to suit
1512  * lover GPU voltages for projects where buck8 is (ab)used to supply power
1513  * for GPU. Additionally some setups do allow DVS for buck8 but as this do
1514  * produce voltage spikes the HW must be evaluated to be able to survive this
1515  * - hence I keep the DVS disabled for non DVS bucks by default. I don't want
1516  * to help you burn your proto board)
1517  *
1518  * So we allow describing this external connection from DT and scale the
1519  * voltages accordingly. This is what the connection should look like:
1520  *
1521  * |------------|
1522  * |    buck 8  |-------+----->Vout
1523  * |        |   |
1524  * |------------|   |
1525  *  | FB pin    |
1526  *  |       |
1527  *  +-------+--R2---+
1528  *      |
1529  *      R1
1530  *      |
1531  *  V FB-pull-up
1532  *
1533  *  Here the buck output is sifted according to formula:
1534  *
1535  * Vout_o = Vo - (Vpu - Vo)*R2/R1
1536  * Linear_step = step_orig*(R1+R2)/R1
1537  *
1538  * where:
1539  * Vout_o is adjusted voltage output at vsel reg value 0
1540  * Vo is original voltage output at vsel reg value 0
1541  * Vpu is the pull-up voltage V FB-pull-up in the picture
1542  * R1 and R2 are resistor values.
1543  *
1544  * As a real world example for buck8 and a specific GPU:
1545  * VLDO = 1.6V (used as FB-pull-up)
1546  * R1 = 1000ohms
1547  * R2 = 150ohms
1548  * VSEL 0x0 => 0.8V – (VLDO – 0.8) * R2 / R1 = 0.68V
1549  * Linear Step = 10mV * (R1 + R2) / R1 = 11.5mV
1550  */
1551 static int setup_feedback_loop(struct device *dev, struct device_node *np,
1552                    struct bd718xx_regulator_data *reg_data,
1553                    unsigned int num_reg_data, int fb_uv)
1554 {
1555     int i, r1, r2, ret;
1556 
1557     /*
1558      * We do adjust the values in the global desc based on DT settings.
1559      * This may not be best approach as it can cause problems if more than
1560      * one PMIC is controlled from same processor. I don't see such use-case
1561      * for BD718x7 now - so we spare some bits.
1562      *
1563      * If this will point out to be a problem - then we can allocate new
1564      * bd718xx_regulator_data array at probe and just use the global
1565      * array as a template where we copy initial values. Then we can
1566      * use allocated descs for regultor registration and do IC specific
1567      * modifications to this copy while leaving other PMICs untouched. But
1568      * that means allocating new array for each PMIC - and currently I see
1569      * no need for that.
1570      */
1571 
1572     for (i = 0; i < num_reg_data; i++) {
1573         struct regulator_desc *desc = &reg_data[i].desc;
1574         int j;
1575 
1576         if (!of_node_name_eq(np, desc->of_match))
1577             continue;
1578 
1579         pr_info("Looking at node '%s'\n", desc->of_match);
1580 
1581         /* The feedback loop connection does not make sense for LDOs */
1582         if (desc->id >= BD718XX_LDO1)
1583             return -EINVAL;
1584 
1585         ret = of_property_read_u32(np, "rohm,feedback-pull-up-r1-ohms",
1586                        &r1);
1587         if (ret)
1588             return ret;
1589 
1590         if (!r1)
1591             return -EINVAL;
1592 
1593         ret = of_property_read_u32(np, "rohm,feedback-pull-up-r2-ohms",
1594                        &r2);
1595         if (ret)
1596             return ret;
1597 
1598         if (desc->n_linear_ranges && desc->linear_ranges) {
1599             struct linear_range *new;
1600 
1601             new = devm_kzalloc(dev, desc->n_linear_ranges *
1602                        sizeof(struct linear_range),
1603                        GFP_KERNEL);
1604             if (!new)
1605                 return -ENOMEM;
1606 
1607             for (j = 0; j < desc->n_linear_ranges; j++) {
1608                 int min = desc->linear_ranges[j].min;
1609                 int step = desc->linear_ranges[j].step;
1610 
1611                 min -= (fb_uv - min)*r2/r1;
1612                 step = step * (r1 + r2);
1613                 step /= r1;
1614 
1615                 new[j].min = min;
1616                 new[j].step = step;
1617 
1618                 dev_dbg(dev, "%s: old range min %d, step %d\n",
1619                     desc->name, desc->linear_ranges[j].min,
1620                     desc->linear_ranges[j].step);
1621                 dev_dbg(dev, "new range min %d, step %d\n", min,
1622                     step);
1623             }
1624             desc->linear_ranges = new;
1625         }
1626         dev_dbg(dev, "regulator '%s' has FB pull-up configured\n",
1627             desc->name);
1628 
1629         return 0;
1630     }
1631 
1632     return -ENODEV;
1633 }
1634 
1635 static int get_special_regulators(struct device *dev,
1636                   struct bd718xx_regulator_data *reg_data,
1637                   unsigned int num_reg_data, int *info)
1638 {
1639     int ret;
1640     struct device_node *np;
1641     struct device_node *nproot = dev->of_node;
1642     int uv;
1643 
1644     *info = 0;
1645 
1646     nproot = of_get_child_by_name(nproot, "regulators");
1647     if (!nproot) {
1648         dev_err(dev, "failed to find regulators node\n");
1649         return -ENODEV;
1650     }
1651     for_each_child_of_node(nproot, np) {
1652         if (of_property_read_bool(np, "rohm,no-regulator-enable-control"))
1653             mark_hw_controlled(dev, np, reg_data, num_reg_data,
1654                        info);
1655         ret = of_property_read_u32(np, "rohm,fb-pull-up-microvolt",
1656                        &uv);
1657         if (ret) {
1658             if (ret == -EINVAL)
1659                 continue;
1660             else
1661                 goto err_out;
1662         }
1663 
1664         ret = setup_feedback_loop(dev, np, reg_data, num_reg_data, uv);
1665         if (ret)
1666             goto err_out;
1667     }
1668 
1669     of_node_put(nproot);
1670     return 0;
1671 
1672 err_out:
1673     of_node_put(np);
1674     of_node_put(nproot);
1675 
1676     return ret;
1677 }
1678 
1679 static int bd718xx_probe(struct platform_device *pdev)
1680 {
1681     struct regmap *regmap;
1682     struct regulator_config config = { 0 };
1683     int i, j, err, omit_enable;
1684     bool use_snvs;
1685     struct bd718xx_regulator_data *reg_data;
1686     unsigned int num_reg_data;
1687     enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
1688     const struct regulator_ops **swops, **hwops;
1689 
1690     regmap = dev_get_regmap(pdev->dev.parent, NULL);
1691     if (!regmap) {
1692         dev_err(&pdev->dev, "No MFD driver data\n");
1693         return -EINVAL;
1694     }
1695 
1696     switch (chip) {
1697     case ROHM_CHIP_TYPE_BD71837:
1698         reg_data = bd71837_regulators;
1699         num_reg_data = ARRAY_SIZE(bd71837_regulators);
1700         swops = &bd71837_swcontrol_ops[0];
1701         hwops = &bd71837_hwcontrol_ops[0];
1702         break;
1703     case ROHM_CHIP_TYPE_BD71847:
1704         reg_data = bd71847_regulators;
1705         num_reg_data = ARRAY_SIZE(bd71847_regulators);
1706         swops = &bd71847_swcontrol_ops[0];
1707         hwops = &bd71847_hwcontrol_ops[0];
1708         break;
1709     default:
1710         dev_err(&pdev->dev, "Unsupported chip type\n");
1711         err = -EINVAL;
1712         goto err;
1713     }
1714 
1715     /* Register LOCK release */
1716     err = regmap_update_bits(regmap, BD718XX_REG_REGLOCK,
1717                  (REGLOCK_PWRSEQ | REGLOCK_VREG), 0);
1718     if (err) {
1719         dev_err(&pdev->dev, "Failed to unlock PMIC (%d)\n", err);
1720         goto err;
1721     } else {
1722         dev_dbg(&pdev->dev, "Unlocked lock register 0x%x\n",
1723             BD718XX_REG_REGLOCK);
1724     }
1725 
1726     use_snvs = of_property_read_bool(pdev->dev.parent->of_node,
1727                      "rohm,reset-snvs-powered");
1728 
1729     /*
1730      * Change the next stage from poweroff to be READY instead of SNVS
1731      * for all reset types because OTP loading at READY will clear SEL
1732      * bit allowing HW defaults for power rails to be used
1733      */
1734     if (!use_snvs) {
1735         err = regmap_update_bits(regmap, BD718XX_REG_TRANS_COND1,
1736                      BD718XX_ON_REQ_POWEROFF_MASK |
1737                      BD718XX_SWRESET_POWEROFF_MASK |
1738                      BD718XX_WDOG_POWEROFF_MASK |
1739                      BD718XX_KEY_L_POWEROFF_MASK,
1740                      BD718XX_POWOFF_TO_RDY);
1741         if (err) {
1742             dev_err(&pdev->dev, "Failed to change reset target\n");
1743             goto err;
1744         } else {
1745             dev_dbg(&pdev->dev,
1746                 "Changed all resets from SVNS to READY\n");
1747         }
1748     }
1749 
1750     config.dev = pdev->dev.parent;
1751     config.regmap = regmap;
1752     /*
1753      * There are cases when we want to leave the enable-control for
1754      * the HW state machine and use this driver only for voltage control.
1755      * One special case is when we use PMIC_STBY_REQ line from SoC to PMIC
1756      * in order to set the system to SUSPEND state.
1757      *
1758      * If regulator is taken under SW control the regulator state will not
1759      * be affected by PMIC state machine - Eg. regulator is likely to stay
1760      * on even in SUSPEND
1761      */
1762     err = get_special_regulators(pdev->dev.parent, reg_data, num_reg_data,
1763                      &omit_enable);
1764     if (err)
1765         return err;
1766 
1767     for (i = 0; i < num_reg_data; i++) {
1768 
1769         struct regulator_desc *desc;
1770         struct regulator_dev *rdev;
1771         struct bd718xx_regulator_data *r;
1772         int no_enable_control = omit_enable & (1 << i);
1773 
1774         r = &reg_data[i];
1775         desc = &r->desc;
1776 
1777         if (no_enable_control)
1778             desc->ops = hwops[i];
1779         else
1780             desc->ops = swops[i];
1781 
1782         rdev = devm_regulator_register(&pdev->dev, desc, &config);
1783         if (IS_ERR(rdev)) {
1784             dev_err(&pdev->dev,
1785                 "failed to register %s regulator\n",
1786                 desc->name);
1787             err = PTR_ERR(rdev);
1788             goto err;
1789         }
1790 
1791         /*
1792          * Regulator register gets the regulator constraints and
1793          * applies them (set_machine_constraints). This should have
1794          * turned the control register(s) to correct values and we
1795          * can now switch the control from PMIC state machine to the
1796          * register interface
1797          *
1798          * At poweroff transition PMIC HW disables EN bit for
1799          * regulators but leaves SEL bit untouched. So if state
1800          * transition from POWEROFF is done to SNVS - then all power
1801          * rails controlled by SW (having SEL bit set) stay disabled
1802          * as EN is cleared. This will result boot failure if any
1803          * crucial systems are powered by these rails. We don't
1804          * enable SW control for crucial regulators if snvs state is
1805          * used
1806          */
1807         if (!no_enable_control && (!use_snvs ||
1808             !rdev->constraints->always_on ||
1809             !rdev->constraints->boot_on)) {
1810             err = regmap_update_bits(regmap, r->init.reg,
1811                          r->init.mask, r->init.val);
1812             if (err) {
1813                 dev_err(&pdev->dev,
1814                     "Failed to take control for (%s)\n",
1815                     desc->name);
1816                 goto err;
1817             }
1818         }
1819         for (j = 0; j < r->additional_init_amnt; j++) {
1820             err = regmap_update_bits(regmap,
1821                          r->additional_inits[j].reg,
1822                          r->additional_inits[j].mask,
1823                          r->additional_inits[j].val);
1824             if (err) {
1825                 dev_err(&pdev->dev,
1826                     "Buck (%s) initialization failed\n",
1827                     desc->name);
1828                 goto err;
1829             }
1830         }
1831     }
1832 
1833 err:
1834     return err;
1835 }
1836 
1837 static const struct platform_device_id bd718x7_pmic_id[] = {
1838     { "bd71837-pmic", ROHM_CHIP_TYPE_BD71837 },
1839     { "bd71847-pmic", ROHM_CHIP_TYPE_BD71847 },
1840     { },
1841 };
1842 MODULE_DEVICE_TABLE(platform, bd718x7_pmic_id);
1843 
1844 static struct platform_driver bd718xx_regulator = {
1845     .driver = {
1846         .name = "bd718xx-pmic",
1847     },
1848     .probe = bd718xx_probe,
1849     .id_table = bd718x7_pmic_id,
1850 };
1851 
1852 module_platform_driver(bd718xx_regulator);
1853 
1854 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1855 MODULE_DESCRIPTION("BD71837/BD71847 voltage regulator driver");
1856 MODULE_LICENSE("GPL");
1857 MODULE_ALIAS("platform:bd718xx-pmic");