Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
0004 //              http://www.samsung.com
0005 
0006 #include <linux/bug.h>
0007 #include <linux/err.h>
0008 #include <linux/gpio/consumer.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/regmap.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/regulator/driver.h>
0015 #include <linux/regulator/machine.h>
0016 #include <linux/regulator/of_regulator.h>
0017 #include <linux/mfd/samsung/core.h>
0018 #include <linux/mfd/samsung/s2mps11.h>
0019 #include <linux/mfd/samsung/s2mps13.h>
0020 #include <linux/mfd/samsung/s2mps14.h>
0021 #include <linux/mfd/samsung/s2mps15.h>
0022 #include <linux/mfd/samsung/s2mpu02.h>
0023 
0024 /* The highest number of possible regulators for supported devices. */
0025 #define S2MPS_REGULATOR_MAX     S2MPS13_REGULATOR_MAX
0026 struct s2mps11_info {
0027     int ramp_delay2;
0028     int ramp_delay34;
0029     int ramp_delay5;
0030     int ramp_delay16;
0031     int ramp_delay7810;
0032     int ramp_delay9;
0033 
0034     enum sec_device_type dev_type;
0035 
0036     /*
0037      * One bit for each S2MPS11/S2MPS13/S2MPS14/S2MPU02 regulator whether
0038      * the suspend mode was enabled.
0039      */
0040     DECLARE_BITMAP(suspend_state, S2MPS_REGULATOR_MAX);
0041 
0042     /*
0043      * Array (size: number of regulators) with GPIO-s for external
0044      * sleep control.
0045      */
0046     struct gpio_desc **ext_control_gpiod;
0047 };
0048 
0049 static int get_ramp_delay(int ramp_delay)
0050 {
0051     unsigned char cnt = 0;
0052 
0053     ramp_delay /= 6250;
0054 
0055     while (true) {
0056         ramp_delay = ramp_delay >> 1;
0057         if (ramp_delay == 0)
0058             break;
0059         cnt++;
0060     }
0061 
0062     if (cnt > 3)
0063         cnt = 3;
0064 
0065     return cnt;
0066 }
0067 
0068 static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
0069                    unsigned int old_selector,
0070                    unsigned int new_selector)
0071 {
0072     struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
0073     int rdev_id = rdev_get_id(rdev);
0074     unsigned int ramp_delay = 0;
0075     int old_volt, new_volt;
0076 
0077     switch (rdev_id) {
0078     case S2MPS11_BUCK2:
0079         ramp_delay = s2mps11->ramp_delay2;
0080         break;
0081     case S2MPS11_BUCK3:
0082     case S2MPS11_BUCK4:
0083         ramp_delay = s2mps11->ramp_delay34;
0084         break;
0085     case S2MPS11_BUCK5:
0086         ramp_delay = s2mps11->ramp_delay5;
0087         break;
0088     case S2MPS11_BUCK6:
0089     case S2MPS11_BUCK1:
0090         ramp_delay = s2mps11->ramp_delay16;
0091         break;
0092     case S2MPS11_BUCK7:
0093     case S2MPS11_BUCK8:
0094     case S2MPS11_BUCK10:
0095         ramp_delay = s2mps11->ramp_delay7810;
0096         break;
0097     case S2MPS11_BUCK9:
0098         ramp_delay = s2mps11->ramp_delay9;
0099     }
0100 
0101     if (ramp_delay == 0)
0102         ramp_delay = rdev->desc->ramp_delay;
0103 
0104     old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
0105     new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
0106 
0107     return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
0108 }
0109 
0110 static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
0111 {
0112     struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
0113     unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
0114     unsigned int ramp_enable = 1, enable_shift = 0;
0115     int rdev_id = rdev_get_id(rdev);
0116     int ret;
0117 
0118     switch (rdev_id) {
0119     case S2MPS11_BUCK1:
0120         if (ramp_delay > s2mps11->ramp_delay16)
0121             s2mps11->ramp_delay16 = ramp_delay;
0122         else
0123             ramp_delay = s2mps11->ramp_delay16;
0124 
0125         ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
0126         break;
0127     case S2MPS11_BUCK2:
0128         enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
0129         if (!ramp_delay) {
0130             ramp_enable = 0;
0131             break;
0132         }
0133 
0134         s2mps11->ramp_delay2 = ramp_delay;
0135         ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
0136         ramp_reg = S2MPS11_REG_RAMP;
0137         break;
0138     case S2MPS11_BUCK3:
0139         enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
0140         if (!ramp_delay) {
0141             ramp_enable = 0;
0142             break;
0143         }
0144 
0145         if (ramp_delay > s2mps11->ramp_delay34)
0146             s2mps11->ramp_delay34 = ramp_delay;
0147         else
0148             ramp_delay = s2mps11->ramp_delay34;
0149 
0150         ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
0151         ramp_reg = S2MPS11_REG_RAMP;
0152         break;
0153     case S2MPS11_BUCK4:
0154         enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
0155         if (!ramp_delay) {
0156             ramp_enable = 0;
0157             break;
0158         }
0159 
0160         if (ramp_delay > s2mps11->ramp_delay34)
0161             s2mps11->ramp_delay34 = ramp_delay;
0162         else
0163             ramp_delay = s2mps11->ramp_delay34;
0164 
0165         ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
0166         ramp_reg = S2MPS11_REG_RAMP;
0167         break;
0168     case S2MPS11_BUCK5:
0169         s2mps11->ramp_delay5 = ramp_delay;
0170         ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
0171         break;
0172     case S2MPS11_BUCK6:
0173         enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
0174         if (!ramp_delay) {
0175             ramp_enable = 0;
0176             break;
0177         }
0178 
0179         if (ramp_delay > s2mps11->ramp_delay16)
0180             s2mps11->ramp_delay16 = ramp_delay;
0181         else
0182             ramp_delay = s2mps11->ramp_delay16;
0183 
0184         ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
0185         break;
0186     case S2MPS11_BUCK7:
0187     case S2MPS11_BUCK8:
0188     case S2MPS11_BUCK10:
0189         if (ramp_delay > s2mps11->ramp_delay7810)
0190             s2mps11->ramp_delay7810 = ramp_delay;
0191         else
0192             ramp_delay = s2mps11->ramp_delay7810;
0193 
0194         ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
0195         break;
0196     case S2MPS11_BUCK9:
0197         s2mps11->ramp_delay9 = ramp_delay;
0198         ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
0199         break;
0200     default:
0201         return 0;
0202     }
0203 
0204     if (!ramp_enable)
0205         goto ramp_disable;
0206 
0207     /* Ramp delay can be enabled/disabled only for buck[2346] */
0208     if ((rdev_id >= S2MPS11_BUCK2 && rdev_id <= S2MPS11_BUCK4) ||
0209         rdev_id == S2MPS11_BUCK6)  {
0210         ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
0211                      1 << enable_shift, 1 << enable_shift);
0212         if (ret) {
0213             dev_err(&rdev->dev, "failed to enable ramp rate\n");
0214             return ret;
0215         }
0216     }
0217 
0218     ramp_val = get_ramp_delay(ramp_delay);
0219 
0220     return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
0221                   ramp_val << ramp_shift);
0222 
0223 ramp_disable:
0224     return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
0225                   1 << enable_shift, 0);
0226 }
0227 
0228 static int s2mps11_regulator_enable(struct regulator_dev *rdev)
0229 {
0230     struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
0231     int rdev_id = rdev_get_id(rdev);
0232     unsigned int val;
0233 
0234     switch (s2mps11->dev_type) {
0235     case S2MPS11X:
0236         if (test_bit(rdev_id, s2mps11->suspend_state))
0237             val = S2MPS14_ENABLE_SUSPEND;
0238         else
0239             val = rdev->desc->enable_mask;
0240         break;
0241     case S2MPS13X:
0242     case S2MPS14X:
0243         if (test_bit(rdev_id, s2mps11->suspend_state))
0244             val = S2MPS14_ENABLE_SUSPEND;
0245         else if (s2mps11->ext_control_gpiod[rdev_id])
0246             val = S2MPS14_ENABLE_EXT_CONTROL;
0247         else
0248             val = rdev->desc->enable_mask;
0249         break;
0250     case S2MPU02:
0251         if (test_bit(rdev_id, s2mps11->suspend_state))
0252             val = S2MPU02_ENABLE_SUSPEND;
0253         else
0254             val = rdev->desc->enable_mask;
0255         break;
0256     default:
0257         return -EINVAL;
0258     }
0259 
0260     return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
0261             rdev->desc->enable_mask, val);
0262 }
0263 
0264 static int s2mps11_regulator_set_suspend_disable(struct regulator_dev *rdev)
0265 {
0266     int ret;
0267     unsigned int val, state;
0268     struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
0269     int rdev_id = rdev_get_id(rdev);
0270 
0271     /* Below LDO should be always on or does not support suspend mode. */
0272     switch (s2mps11->dev_type) {
0273     case S2MPS11X:
0274         switch (rdev_id) {
0275         case S2MPS11_LDO2:
0276         case S2MPS11_LDO36:
0277         case S2MPS11_LDO37:
0278         case S2MPS11_LDO38:
0279             return 0;
0280         default:
0281             state = S2MPS14_ENABLE_SUSPEND;
0282             break;
0283         }
0284         break;
0285     case S2MPS13X:
0286     case S2MPS14X:
0287         switch (rdev_id) {
0288         case S2MPS14_LDO3:
0289             return 0;
0290         default:
0291             state = S2MPS14_ENABLE_SUSPEND;
0292             break;
0293         }
0294         break;
0295     case S2MPU02:
0296         switch (rdev_id) {
0297         case S2MPU02_LDO13:
0298         case S2MPU02_LDO14:
0299         case S2MPU02_LDO15:
0300         case S2MPU02_LDO17:
0301         case S2MPU02_BUCK7:
0302             state = S2MPU02_DISABLE_SUSPEND;
0303             break;
0304         default:
0305             state = S2MPU02_ENABLE_SUSPEND;
0306             break;
0307         }
0308         break;
0309     default:
0310         return -EINVAL;
0311     }
0312 
0313     ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
0314     if (ret < 0)
0315         return ret;
0316 
0317     set_bit(rdev_id, s2mps11->suspend_state);
0318     /*
0319      * Don't enable suspend mode if regulator is already disabled because
0320      * this would effectively for a short time turn on the regulator after
0321      * resuming.
0322      * However we still want to toggle the suspend_state bit for regulator
0323      * in case if it got enabled before suspending the system.
0324      */
0325     if (!(val & rdev->desc->enable_mask))
0326         return 0;
0327 
0328     return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
0329                   rdev->desc->enable_mask, state);
0330 }
0331 
0332 static const struct regulator_ops s2mps11_ldo_ops = {
0333     .list_voltage       = regulator_list_voltage_linear,
0334     .map_voltage        = regulator_map_voltage_linear,
0335     .is_enabled     = regulator_is_enabled_regmap,
0336     .enable         = s2mps11_regulator_enable,
0337     .disable        = regulator_disable_regmap,
0338     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0339     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0340     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0341     .set_suspend_disable    = s2mps11_regulator_set_suspend_disable,
0342 };
0343 
0344 static const struct regulator_ops s2mps11_buck_ops = {
0345     .list_voltage       = regulator_list_voltage_linear,
0346     .map_voltage        = regulator_map_voltage_linear,
0347     .is_enabled     = regulator_is_enabled_regmap,
0348     .enable         = s2mps11_regulator_enable,
0349     .disable        = regulator_disable_regmap,
0350     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0351     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0352     .set_voltage_time_sel   = s2mps11_regulator_set_voltage_time_sel,
0353     .set_ramp_delay     = s2mps11_set_ramp_delay,
0354     .set_suspend_disable    = s2mps11_regulator_set_suspend_disable,
0355 };
0356 
0357 #define regulator_desc_s2mps11_ldo(num, step) {     \
0358     .name       = "LDO"#num,            \
0359     .id     = S2MPS11_LDO##num,     \
0360     .ops        = &s2mps11_ldo_ops,     \
0361     .type       = REGULATOR_VOLTAGE,        \
0362     .owner      = THIS_MODULE,          \
0363     .ramp_delay = RAMP_DELAY_12_MVUS,       \
0364     .min_uV     = MIN_800_MV,           \
0365     .uV_step    = step,             \
0366     .n_voltages = S2MPS11_LDO_N_VOLTAGES,   \
0367     .vsel_reg   = S2MPS11_REG_L1CTRL + num - 1, \
0368     .vsel_mask  = S2MPS11_LDO_VSEL_MASK,    \
0369     .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
0370     .enable_mask    = S2MPS11_ENABLE_MASK       \
0371 }
0372 
0373 #define regulator_desc_s2mps11_buck1_4(num) {           \
0374     .name       = "BUCK"#num,               \
0375     .id     = S2MPS11_BUCK##num,            \
0376     .ops        = &s2mps11_buck_ops,            \
0377     .type       = REGULATOR_VOLTAGE,            \
0378     .owner      = THIS_MODULE,              \
0379     .min_uV     = MIN_650_MV,               \
0380     .uV_step    = STEP_6_25_MV,             \
0381     .linear_min_sel = 8,                    \
0382     .n_voltages = S2MPS11_BUCK12346_N_VOLTAGES,     \
0383     .ramp_delay = S2MPS11_RAMP_DELAY,           \
0384     .vsel_reg   = S2MPS11_REG_B1CTRL2 + (num - 1) * 2,  \
0385     .vsel_mask  = S2MPS11_BUCK_VSEL_MASK,       \
0386     .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2,  \
0387     .enable_mask    = S2MPS11_ENABLE_MASK           \
0388 }
0389 
0390 #define regulator_desc_s2mps11_buck5 {              \
0391     .name       = "BUCK5",              \
0392     .id     = S2MPS11_BUCK5,            \
0393     .ops        = &s2mps11_buck_ops,            \
0394     .type       = REGULATOR_VOLTAGE,            \
0395     .owner      = THIS_MODULE,              \
0396     .min_uV     = MIN_650_MV,               \
0397     .uV_step    = STEP_6_25_MV,             \
0398     .linear_min_sel = 8,                    \
0399     .n_voltages = S2MPS11_BUCK5_N_VOLTAGES,     \
0400     .ramp_delay = S2MPS11_RAMP_DELAY,           \
0401     .vsel_reg   = S2MPS11_REG_B5CTRL2,          \
0402     .vsel_mask  = S2MPS11_BUCK_VSEL_MASK,       \
0403     .enable_reg = S2MPS11_REG_B5CTRL1,          \
0404     .enable_mask    = S2MPS11_ENABLE_MASK           \
0405 }
0406 
0407 #define regulator_desc_s2mps11_buck67810(num, min, step, min_sel, voltages) {   \
0408     .name       = "BUCK"#num,               \
0409     .id     = S2MPS11_BUCK##num,            \
0410     .ops        = &s2mps11_buck_ops,            \
0411     .type       = REGULATOR_VOLTAGE,            \
0412     .owner      = THIS_MODULE,              \
0413     .min_uV     = min,                  \
0414     .uV_step    = step,                 \
0415     .linear_min_sel = min_sel,              \
0416     .n_voltages = voltages,             \
0417     .ramp_delay = S2MPS11_RAMP_DELAY,           \
0418     .vsel_reg   = S2MPS11_REG_B6CTRL2 + (num - 6) * 2,  \
0419     .vsel_mask  = S2MPS11_BUCK_VSEL_MASK,       \
0420     .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2,  \
0421     .enable_mask    = S2MPS11_ENABLE_MASK           \
0422 }
0423 
0424 #define regulator_desc_s2mps11_buck9 {              \
0425     .name       = "BUCK9",              \
0426     .id     = S2MPS11_BUCK9,            \
0427     .ops        = &s2mps11_buck_ops,            \
0428     .type       = REGULATOR_VOLTAGE,            \
0429     .owner      = THIS_MODULE,              \
0430     .min_uV     = MIN_3000_MV,              \
0431     .uV_step    = STEP_25_MV,               \
0432     .n_voltages = S2MPS11_BUCK9_N_VOLTAGES,     \
0433     .ramp_delay = S2MPS11_RAMP_DELAY,           \
0434     .vsel_reg   = S2MPS11_REG_B9CTRL2,          \
0435     .vsel_mask  = S2MPS11_BUCK9_VSEL_MASK,      \
0436     .enable_reg = S2MPS11_REG_B9CTRL1,          \
0437     .enable_mask    = S2MPS11_ENABLE_MASK           \
0438 }
0439 
0440 static const struct regulator_desc s2mps11_regulators[] = {
0441     regulator_desc_s2mps11_ldo(1, STEP_25_MV),
0442     regulator_desc_s2mps11_ldo(2, STEP_50_MV),
0443     regulator_desc_s2mps11_ldo(3, STEP_50_MV),
0444     regulator_desc_s2mps11_ldo(4, STEP_50_MV),
0445     regulator_desc_s2mps11_ldo(5, STEP_50_MV),
0446     regulator_desc_s2mps11_ldo(6, STEP_25_MV),
0447     regulator_desc_s2mps11_ldo(7, STEP_50_MV),
0448     regulator_desc_s2mps11_ldo(8, STEP_50_MV),
0449     regulator_desc_s2mps11_ldo(9, STEP_50_MV),
0450     regulator_desc_s2mps11_ldo(10, STEP_50_MV),
0451     regulator_desc_s2mps11_ldo(11, STEP_25_MV),
0452     regulator_desc_s2mps11_ldo(12, STEP_50_MV),
0453     regulator_desc_s2mps11_ldo(13, STEP_50_MV),
0454     regulator_desc_s2mps11_ldo(14, STEP_50_MV),
0455     regulator_desc_s2mps11_ldo(15, STEP_50_MV),
0456     regulator_desc_s2mps11_ldo(16, STEP_50_MV),
0457     regulator_desc_s2mps11_ldo(17, STEP_50_MV),
0458     regulator_desc_s2mps11_ldo(18, STEP_50_MV),
0459     regulator_desc_s2mps11_ldo(19, STEP_50_MV),
0460     regulator_desc_s2mps11_ldo(20, STEP_50_MV),
0461     regulator_desc_s2mps11_ldo(21, STEP_50_MV),
0462     regulator_desc_s2mps11_ldo(22, STEP_25_MV),
0463     regulator_desc_s2mps11_ldo(23, STEP_25_MV),
0464     regulator_desc_s2mps11_ldo(24, STEP_50_MV),
0465     regulator_desc_s2mps11_ldo(25, STEP_50_MV),
0466     regulator_desc_s2mps11_ldo(26, STEP_50_MV),
0467     regulator_desc_s2mps11_ldo(27, STEP_25_MV),
0468     regulator_desc_s2mps11_ldo(28, STEP_50_MV),
0469     regulator_desc_s2mps11_ldo(29, STEP_50_MV),
0470     regulator_desc_s2mps11_ldo(30, STEP_50_MV),
0471     regulator_desc_s2mps11_ldo(31, STEP_50_MV),
0472     regulator_desc_s2mps11_ldo(32, STEP_50_MV),
0473     regulator_desc_s2mps11_ldo(33, STEP_50_MV),
0474     regulator_desc_s2mps11_ldo(34, STEP_50_MV),
0475     regulator_desc_s2mps11_ldo(35, STEP_25_MV),
0476     regulator_desc_s2mps11_ldo(36, STEP_50_MV),
0477     regulator_desc_s2mps11_ldo(37, STEP_50_MV),
0478     regulator_desc_s2mps11_ldo(38, STEP_50_MV),
0479     regulator_desc_s2mps11_buck1_4(1),
0480     regulator_desc_s2mps11_buck1_4(2),
0481     regulator_desc_s2mps11_buck1_4(3),
0482     regulator_desc_s2mps11_buck1_4(4),
0483     regulator_desc_s2mps11_buck5,
0484     regulator_desc_s2mps11_buck67810(6, MIN_650_MV, STEP_6_25_MV, 8,
0485                      S2MPS11_BUCK12346_N_VOLTAGES),
0486     regulator_desc_s2mps11_buck67810(7, MIN_750_MV, STEP_12_5_MV, 0,
0487                      S2MPS11_BUCK7810_N_VOLTAGES),
0488     regulator_desc_s2mps11_buck67810(8, MIN_750_MV, STEP_12_5_MV, 0,
0489                      S2MPS11_BUCK7810_N_VOLTAGES),
0490     regulator_desc_s2mps11_buck9,
0491     regulator_desc_s2mps11_buck67810(10, MIN_750_MV, STEP_12_5_MV, 0,
0492                      S2MPS11_BUCK7810_N_VOLTAGES),
0493 };
0494 
0495 static const struct regulator_ops s2mps14_reg_ops;
0496 
0497 #define regulator_desc_s2mps13_ldo(num, min, step, min_sel) {   \
0498     .name       = "LDO"#num,                \
0499     .id     = S2MPS13_LDO##num,         \
0500     .ops        = &s2mps14_reg_ops,         \
0501     .type       = REGULATOR_VOLTAGE,            \
0502     .owner      = THIS_MODULE,              \
0503     .min_uV     = min,                  \
0504     .uV_step    = step,                 \
0505     .linear_min_sel = min_sel,              \
0506     .n_voltages = S2MPS14_LDO_N_VOLTAGES,       \
0507     .vsel_reg   = S2MPS13_REG_L1CTRL + num - 1,     \
0508     .vsel_mask  = S2MPS14_LDO_VSEL_MASK,        \
0509     .enable_reg = S2MPS13_REG_L1CTRL + num - 1,     \
0510     .enable_mask    = S2MPS14_ENABLE_MASK           \
0511 }
0512 
0513 #define regulator_desc_s2mps13_buck(num, min, step, min_sel) {  \
0514     .name       = "BUCK"#num,               \
0515     .id     = S2MPS13_BUCK##num,            \
0516     .ops        = &s2mps14_reg_ops,         \
0517     .type       = REGULATOR_VOLTAGE,            \
0518     .owner      = THIS_MODULE,              \
0519     .min_uV     = min,                  \
0520     .uV_step    = step,                 \
0521     .linear_min_sel = min_sel,              \
0522     .n_voltages = S2MPS14_BUCK_N_VOLTAGES,      \
0523     .ramp_delay = S2MPS13_BUCK_RAMP_DELAY,      \
0524     .vsel_reg   = S2MPS13_REG_B1OUT + (num - 1) * 2,    \
0525     .vsel_mask  = S2MPS14_BUCK_VSEL_MASK,       \
0526     .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2,   \
0527     .enable_mask    = S2MPS14_ENABLE_MASK           \
0528 }
0529 
0530 #define regulator_desc_s2mps13_buck7(num, min, step, min_sel) { \
0531     .name       = "BUCK"#num,               \
0532     .id     = S2MPS13_BUCK##num,            \
0533     .ops        = &s2mps14_reg_ops,         \
0534     .type       = REGULATOR_VOLTAGE,            \
0535     .owner      = THIS_MODULE,              \
0536     .min_uV     = min,                  \
0537     .uV_step    = step,                 \
0538     .linear_min_sel = min_sel,              \
0539     .n_voltages = S2MPS14_BUCK_N_VOLTAGES,      \
0540     .ramp_delay = S2MPS13_BUCK_RAMP_DELAY,      \
0541     .vsel_reg   = S2MPS13_REG_B1OUT + (num) * 2 - 1,    \
0542     .vsel_mask  = S2MPS14_BUCK_VSEL_MASK,       \
0543     .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2,   \
0544     .enable_mask    = S2MPS14_ENABLE_MASK           \
0545 }
0546 
0547 #define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) {  \
0548     .name       = "BUCK"#num,               \
0549     .id     = S2MPS13_BUCK##num,            \
0550     .ops        = &s2mps14_reg_ops,         \
0551     .type       = REGULATOR_VOLTAGE,            \
0552     .owner      = THIS_MODULE,              \
0553     .min_uV     = min,                  \
0554     .uV_step    = step,                 \
0555     .linear_min_sel = min_sel,              \
0556     .n_voltages = S2MPS14_BUCK_N_VOLTAGES,      \
0557     .ramp_delay = S2MPS13_BUCK_RAMP_DELAY,      \
0558     .vsel_reg   = S2MPS13_REG_B1OUT + (num) * 2 - 1,    \
0559     .vsel_mask  = S2MPS14_BUCK_VSEL_MASK,       \
0560     .enable_reg = S2MPS13_REG_B1CTRL + (num) * 2 - 1,   \
0561     .enable_mask    = S2MPS14_ENABLE_MASK           \
0562 }
0563 
0564 static const struct regulator_desc s2mps13_regulators[] = {
0565     regulator_desc_s2mps13_ldo(1,  MIN_800_MV,  STEP_12_5_MV, 0x00),
0566     regulator_desc_s2mps13_ldo(2,  MIN_1400_MV, STEP_50_MV,   0x0C),
0567     regulator_desc_s2mps13_ldo(3,  MIN_1000_MV, STEP_25_MV,   0x08),
0568     regulator_desc_s2mps13_ldo(4,  MIN_800_MV,  STEP_12_5_MV, 0x00),
0569     regulator_desc_s2mps13_ldo(5,  MIN_800_MV,  STEP_12_5_MV, 0x00),
0570     regulator_desc_s2mps13_ldo(6,  MIN_800_MV,  STEP_12_5_MV, 0x00),
0571     regulator_desc_s2mps13_ldo(7,  MIN_1000_MV, STEP_25_MV,   0x08),
0572     regulator_desc_s2mps13_ldo(8,  MIN_1000_MV, STEP_25_MV,   0x08),
0573     regulator_desc_s2mps13_ldo(9,  MIN_1000_MV, STEP_25_MV,   0x08),
0574     regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV,   0x0C),
0575     regulator_desc_s2mps13_ldo(11, MIN_800_MV,  STEP_25_MV,   0x10),
0576     regulator_desc_s2mps13_ldo(12, MIN_800_MV,  STEP_25_MV,   0x10),
0577     regulator_desc_s2mps13_ldo(13, MIN_800_MV,  STEP_25_MV,   0x10),
0578     regulator_desc_s2mps13_ldo(14, MIN_800_MV,  STEP_12_5_MV, 0x00),
0579     regulator_desc_s2mps13_ldo(15, MIN_800_MV,  STEP_12_5_MV, 0x00),
0580     regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV,   0x0C),
0581     regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV,   0x0C),
0582     regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV,   0x08),
0583     regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV,   0x08),
0584     regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV,   0x0C),
0585     regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV,   0x08),
0586     regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV,   0x08),
0587     regulator_desc_s2mps13_ldo(23, MIN_800_MV,  STEP_12_5_MV, 0x00),
0588     regulator_desc_s2mps13_ldo(24, MIN_800_MV,  STEP_12_5_MV, 0x00),
0589     regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV,   0x0C),
0590     regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV,   0x0C),
0591     regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV,   0x0C),
0592     regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV,   0x08),
0593     regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV,   0x0C),
0594     regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV,   0x0C),
0595     regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV,   0x08),
0596     regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV,   0x08),
0597     regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV,   0x0C),
0598     regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV,   0x08),
0599     regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV,   0x0C),
0600     regulator_desc_s2mps13_ldo(36, MIN_800_MV,  STEP_12_5_MV, 0x00),
0601     regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV,   0x08),
0602     regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV,   0x0C),
0603     regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV,   0x08),
0604     regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV,   0x0C),
0605     regulator_desc_s2mps13_buck(1,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0606     regulator_desc_s2mps13_buck(2,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0607     regulator_desc_s2mps13_buck(3,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0608     regulator_desc_s2mps13_buck(4,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0609     regulator_desc_s2mps13_buck(5,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0610     regulator_desc_s2mps13_buck(6,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0611     regulator_desc_s2mps13_buck7(7,  MIN_500_MV,  STEP_6_25_MV, 0x10),
0612     regulator_desc_s2mps13_buck8_10(8,  MIN_1000_MV, STEP_12_5_MV, 0x20),
0613     regulator_desc_s2mps13_buck8_10(9,  MIN_1000_MV, STEP_12_5_MV, 0x20),
0614     regulator_desc_s2mps13_buck8_10(10, MIN_500_MV,  STEP_6_25_MV, 0x10),
0615 };
0616 
0617 static const struct regulator_ops s2mps14_reg_ops = {
0618     .list_voltage       = regulator_list_voltage_linear,
0619     .map_voltage        = regulator_map_voltage_linear,
0620     .is_enabled     = regulator_is_enabled_regmap,
0621     .enable         = s2mps11_regulator_enable,
0622     .disable        = regulator_disable_regmap,
0623     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0624     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0625     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0626     .set_suspend_disable    = s2mps11_regulator_set_suspend_disable,
0627 };
0628 
0629 #define regulator_desc_s2mps14_ldo(num, min, step) {    \
0630     .name       = "LDO"#num,            \
0631     .id     = S2MPS14_LDO##num,     \
0632     .ops        = &s2mps14_reg_ops,     \
0633     .type       = REGULATOR_VOLTAGE,        \
0634     .owner      = THIS_MODULE,          \
0635     .min_uV     = min,              \
0636     .uV_step    = step,             \
0637     .n_voltages = S2MPS14_LDO_N_VOLTAGES,   \
0638     .vsel_reg   = S2MPS14_REG_L1CTRL + num - 1, \
0639     .vsel_mask  = S2MPS14_LDO_VSEL_MASK,    \
0640     .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
0641     .enable_mask    = S2MPS14_ENABLE_MASK       \
0642 }
0643 
0644 #define regulator_desc_s2mps14_buck(num, min, step, min_sel) {  \
0645     .name       = "BUCK"#num,               \
0646     .id     = S2MPS14_BUCK##num,            \
0647     .ops        = &s2mps14_reg_ops,         \
0648     .type       = REGULATOR_VOLTAGE,            \
0649     .owner      = THIS_MODULE,              \
0650     .min_uV     = min,                  \
0651     .uV_step    = step,                 \
0652     .n_voltages = S2MPS14_BUCK_N_VOLTAGES,      \
0653     .linear_min_sel = min_sel,              \
0654     .ramp_delay = S2MPS14_BUCK_RAMP_DELAY,      \
0655     .vsel_reg   = S2MPS14_REG_B1CTRL2 + (num - 1) * 2,  \
0656     .vsel_mask  = S2MPS14_BUCK_VSEL_MASK,       \
0657     .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2,  \
0658     .enable_mask    = S2MPS14_ENABLE_MASK           \
0659 }
0660 
0661 static const struct regulator_desc s2mps14_regulators[] = {
0662     regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
0663     regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
0664     regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
0665     regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
0666     regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
0667     regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
0668     regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
0669     regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
0670     regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
0671     regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
0672     regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
0673     regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
0674     regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
0675     regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
0676     regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
0677     regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
0678     regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
0679     regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
0680     regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
0681     regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
0682     regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
0683     regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
0684     regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
0685     regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
0686     regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
0687     regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
0688                     S2MPS14_BUCK1235_START_SEL),
0689     regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
0690                     S2MPS14_BUCK1235_START_SEL),
0691     regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
0692                     S2MPS14_BUCK1235_START_SEL),
0693     regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
0694                     S2MPS14_BUCK4_START_SEL),
0695     regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
0696                     S2MPS14_BUCK1235_START_SEL),
0697 };
0698 
0699 static const struct regulator_ops s2mps15_reg_ldo_ops = {
0700     .list_voltage       = regulator_list_voltage_linear_range,
0701     .map_voltage        = regulator_map_voltage_linear_range,
0702     .is_enabled     = regulator_is_enabled_regmap,
0703     .enable         = regulator_enable_regmap,
0704     .disable        = regulator_disable_regmap,
0705     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0706     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0707 };
0708 
0709 static const struct regulator_ops s2mps15_reg_buck_ops = {
0710     .list_voltage       = regulator_list_voltage_linear_range,
0711     .map_voltage        = regulator_map_voltage_linear_range,
0712     .is_enabled     = regulator_is_enabled_regmap,
0713     .enable         = regulator_enable_regmap,
0714     .disable        = regulator_disable_regmap,
0715     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0716     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0717     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0718 };
0719 
0720 #define regulator_desc_s2mps15_ldo(num, range) {    \
0721     .name       = "LDO"#num,            \
0722     .id     = S2MPS15_LDO##num,     \
0723     .ops        = &s2mps15_reg_ldo_ops,     \
0724     .type       = REGULATOR_VOLTAGE,        \
0725     .owner      = THIS_MODULE,          \
0726     .linear_ranges  = range,            \
0727     .n_linear_ranges = ARRAY_SIZE(range),       \
0728     .n_voltages = S2MPS15_LDO_N_VOLTAGES,   \
0729     .vsel_reg   = S2MPS15_REG_L1CTRL + num - 1, \
0730     .vsel_mask  = S2MPS15_LDO_VSEL_MASK,    \
0731     .enable_reg = S2MPS15_REG_L1CTRL + num - 1, \
0732     .enable_mask    = S2MPS15_ENABLE_MASK       \
0733 }
0734 
0735 #define regulator_desc_s2mps15_buck(num, range) {           \
0736     .name       = "BUCK"#num,                   \
0737     .id     = S2MPS15_BUCK##num,                \
0738     .ops        = &s2mps15_reg_buck_ops,            \
0739     .type       = REGULATOR_VOLTAGE,                \
0740     .owner      = THIS_MODULE,                  \
0741     .linear_ranges  = range,                    \
0742     .n_linear_ranges = ARRAY_SIZE(range),               \
0743     .ramp_delay = 12500,                    \
0744     .n_voltages = S2MPS15_BUCK_N_VOLTAGES,          \
0745     .vsel_reg   = S2MPS15_REG_B1CTRL2 + ((num - 1) * 2),    \
0746     .vsel_mask  = S2MPS15_BUCK_VSEL_MASK,           \
0747     .enable_reg = S2MPS15_REG_B1CTRL1 + ((num - 1) * 2),    \
0748     .enable_mask    = S2MPS15_ENABLE_MASK               \
0749 }
0750 
0751 /* voltage range for s2mps15 LDO 3, 5, 15, 16, 18, 20, 23 and 27 */
0752 static const struct linear_range s2mps15_ldo_voltage_ranges1[] = {
0753     REGULATOR_LINEAR_RANGE(1000000, 0xc, 0x38, 25000),
0754 };
0755 
0756 /* voltage range for s2mps15 LDO 2, 6, 14, 17, 19, 21, 24 and 25 */
0757 static const struct linear_range s2mps15_ldo_voltage_ranges2[] = {
0758     REGULATOR_LINEAR_RANGE(1800000, 0x0, 0x3f, 25000),
0759 };
0760 
0761 /* voltage range for s2mps15 LDO 4, 11, 12, 13, 22 and 26 */
0762 static const struct linear_range s2mps15_ldo_voltage_ranges3[] = {
0763     REGULATOR_LINEAR_RANGE(700000, 0x0, 0x34, 12500),
0764 };
0765 
0766 /* voltage range for s2mps15 LDO 7, 8, 9 and 10 */
0767 static const struct linear_range s2mps15_ldo_voltage_ranges4[] = {
0768     REGULATOR_LINEAR_RANGE(700000, 0x10, 0x20, 25000),
0769 };
0770 
0771 /* voltage range for s2mps15 LDO 1 */
0772 static const struct linear_range s2mps15_ldo_voltage_ranges5[] = {
0773     REGULATOR_LINEAR_RANGE(500000, 0x0, 0x20, 12500),
0774 };
0775 
0776 /* voltage range for s2mps15 BUCK 1, 2, 3, 4, 5, 6 and 7 */
0777 static const struct linear_range s2mps15_buck_voltage_ranges1[] = {
0778     REGULATOR_LINEAR_RANGE(500000, 0x20, 0xc0, 6250),
0779 };
0780 
0781 /* voltage range for s2mps15 BUCK 8, 9 and 10 */
0782 static const struct linear_range s2mps15_buck_voltage_ranges2[] = {
0783     REGULATOR_LINEAR_RANGE(1000000, 0x20, 0x78, 12500),
0784 };
0785 
0786 static const struct regulator_desc s2mps15_regulators[] = {
0787     regulator_desc_s2mps15_ldo(1, s2mps15_ldo_voltage_ranges5),
0788     regulator_desc_s2mps15_ldo(2, s2mps15_ldo_voltage_ranges2),
0789     regulator_desc_s2mps15_ldo(3, s2mps15_ldo_voltage_ranges1),
0790     regulator_desc_s2mps15_ldo(4, s2mps15_ldo_voltage_ranges3),
0791     regulator_desc_s2mps15_ldo(5, s2mps15_ldo_voltage_ranges1),
0792     regulator_desc_s2mps15_ldo(6, s2mps15_ldo_voltage_ranges2),
0793     regulator_desc_s2mps15_ldo(7, s2mps15_ldo_voltage_ranges4),
0794     regulator_desc_s2mps15_ldo(8, s2mps15_ldo_voltage_ranges4),
0795     regulator_desc_s2mps15_ldo(9, s2mps15_ldo_voltage_ranges4),
0796     regulator_desc_s2mps15_ldo(10, s2mps15_ldo_voltage_ranges4),
0797     regulator_desc_s2mps15_ldo(11, s2mps15_ldo_voltage_ranges3),
0798     regulator_desc_s2mps15_ldo(12, s2mps15_ldo_voltage_ranges3),
0799     regulator_desc_s2mps15_ldo(13, s2mps15_ldo_voltage_ranges3),
0800     regulator_desc_s2mps15_ldo(14, s2mps15_ldo_voltage_ranges2),
0801     regulator_desc_s2mps15_ldo(15, s2mps15_ldo_voltage_ranges1),
0802     regulator_desc_s2mps15_ldo(16, s2mps15_ldo_voltage_ranges1),
0803     regulator_desc_s2mps15_ldo(17, s2mps15_ldo_voltage_ranges2),
0804     regulator_desc_s2mps15_ldo(18, s2mps15_ldo_voltage_ranges1),
0805     regulator_desc_s2mps15_ldo(19, s2mps15_ldo_voltage_ranges2),
0806     regulator_desc_s2mps15_ldo(20, s2mps15_ldo_voltage_ranges1),
0807     regulator_desc_s2mps15_ldo(21, s2mps15_ldo_voltage_ranges2),
0808     regulator_desc_s2mps15_ldo(22, s2mps15_ldo_voltage_ranges3),
0809     regulator_desc_s2mps15_ldo(23, s2mps15_ldo_voltage_ranges1),
0810     regulator_desc_s2mps15_ldo(24, s2mps15_ldo_voltage_ranges2),
0811     regulator_desc_s2mps15_ldo(25, s2mps15_ldo_voltage_ranges2),
0812     regulator_desc_s2mps15_ldo(26, s2mps15_ldo_voltage_ranges3),
0813     regulator_desc_s2mps15_ldo(27, s2mps15_ldo_voltage_ranges1),
0814     regulator_desc_s2mps15_buck(1, s2mps15_buck_voltage_ranges1),
0815     regulator_desc_s2mps15_buck(2, s2mps15_buck_voltage_ranges1),
0816     regulator_desc_s2mps15_buck(3, s2mps15_buck_voltage_ranges1),
0817     regulator_desc_s2mps15_buck(4, s2mps15_buck_voltage_ranges1),
0818     regulator_desc_s2mps15_buck(5, s2mps15_buck_voltage_ranges1),
0819     regulator_desc_s2mps15_buck(6, s2mps15_buck_voltage_ranges1),
0820     regulator_desc_s2mps15_buck(7, s2mps15_buck_voltage_ranges1),
0821     regulator_desc_s2mps15_buck(8, s2mps15_buck_voltage_ranges2),
0822     regulator_desc_s2mps15_buck(9, s2mps15_buck_voltage_ranges2),
0823     regulator_desc_s2mps15_buck(10, s2mps15_buck_voltage_ranges2),
0824 };
0825 
0826 static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
0827         struct regulator_dev *rdev)
0828 {
0829     return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
0830             rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
0831 }
0832 
0833 static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
0834         struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
0835 {
0836     struct gpio_desc **gpio = s2mps11->ext_control_gpiod;
0837     unsigned int i;
0838     unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
0839         S2MPS14_LDO12 };
0840 
0841     for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
0842         unsigned int reg = valid_regulators[i];
0843 
0844         if (!rdata[reg].init_data || !rdata[reg].of_node)
0845             continue;
0846 
0847         gpio[reg] = devm_fwnode_gpiod_get(&pdev->dev,
0848                 of_fwnode_handle(rdata[reg].of_node),
0849                 "samsung,ext-control",
0850                 GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
0851                 "s2mps11-regulator");
0852         if (PTR_ERR(gpio[reg]) == -ENOENT)
0853             gpio[reg] = NULL;
0854         else if (IS_ERR(gpio[reg])) {
0855             dev_err(&pdev->dev, "Failed to get control GPIO for %d/%s\n",
0856                 reg, rdata[reg].name);
0857             gpio[reg] = NULL;
0858             continue;
0859         }
0860         if (gpio[reg])
0861             dev_dbg(&pdev->dev, "Using GPIO for ext-control over %d/%s\n",
0862                 reg, rdata[reg].name);
0863     }
0864 }
0865 
0866 static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
0867         struct of_regulator_match *rdata, struct s2mps11_info *s2mps11,
0868         unsigned int rdev_num)
0869 {
0870     struct device_node *reg_np;
0871 
0872     reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
0873     if (!reg_np) {
0874         dev_err(&pdev->dev, "could not find regulators sub-node\n");
0875         return -EINVAL;
0876     }
0877 
0878     of_regulator_match(&pdev->dev, reg_np, rdata, rdev_num);
0879     if (s2mps11->dev_type == S2MPS14X)
0880         s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
0881 
0882     of_node_put(reg_np);
0883 
0884     return 0;
0885 }
0886 
0887 static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
0888 {
0889     unsigned int ramp_val, ramp_shift, ramp_reg;
0890     int rdev_id = rdev_get_id(rdev);
0891 
0892     switch (rdev_id) {
0893     case S2MPU02_BUCK1:
0894         ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
0895         break;
0896     case S2MPU02_BUCK2:
0897         ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
0898         break;
0899     case S2MPU02_BUCK3:
0900         ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
0901         break;
0902     case S2MPU02_BUCK4:
0903         ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
0904         break;
0905     default:
0906         return 0;
0907     }
0908     ramp_reg = S2MPU02_REG_RAMP1;
0909     ramp_val = get_ramp_delay(ramp_delay);
0910 
0911     return regmap_update_bits(rdev->regmap, ramp_reg,
0912                   S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
0913                   ramp_val << ramp_shift);
0914 }
0915 
0916 static const struct regulator_ops s2mpu02_ldo_ops = {
0917     .list_voltage       = regulator_list_voltage_linear,
0918     .map_voltage        = regulator_map_voltage_linear,
0919     .is_enabled     = regulator_is_enabled_regmap,
0920     .enable         = s2mps11_regulator_enable,
0921     .disable        = regulator_disable_regmap,
0922     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0923     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0924     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0925     .set_suspend_disable    = s2mps11_regulator_set_suspend_disable,
0926 };
0927 
0928 static const struct regulator_ops s2mpu02_buck_ops = {
0929     .list_voltage       = regulator_list_voltage_linear,
0930     .map_voltage        = regulator_map_voltage_linear,
0931     .is_enabled     = regulator_is_enabled_regmap,
0932     .enable         = s2mps11_regulator_enable,
0933     .disable        = regulator_disable_regmap,
0934     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0935     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0936     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0937     .set_suspend_disable    = s2mps11_regulator_set_suspend_disable,
0938     .set_ramp_delay     = s2mpu02_set_ramp_delay,
0939 };
0940 
0941 #define regulator_desc_s2mpu02_ldo1(num) {      \
0942     .name       = "LDO"#num,            \
0943     .id     = S2MPU02_LDO##num,     \
0944     .ops        = &s2mpu02_ldo_ops,     \
0945     .type       = REGULATOR_VOLTAGE,        \
0946     .owner      = THIS_MODULE,          \
0947     .min_uV     = S2MPU02_LDO_MIN_900MV,    \
0948     .uV_step    = S2MPU02_LDO_STEP_12_5MV,  \
0949     .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
0950     .n_voltages = S2MPU02_LDO_N_VOLTAGES,   \
0951     .vsel_reg   = S2MPU02_REG_L1CTRL,       \
0952     .vsel_mask  = S2MPU02_LDO_VSEL_MASK,    \
0953     .enable_reg = S2MPU02_REG_L1CTRL,       \
0954     .enable_mask    = S2MPU02_ENABLE_MASK       \
0955 }
0956 #define regulator_desc_s2mpu02_ldo2(num) {      \
0957     .name       = "LDO"#num,            \
0958     .id     = S2MPU02_LDO##num,     \
0959     .ops        = &s2mpu02_ldo_ops,     \
0960     .type       = REGULATOR_VOLTAGE,        \
0961     .owner      = THIS_MODULE,          \
0962     .min_uV     = S2MPU02_LDO_MIN_1050MV,   \
0963     .uV_step    = S2MPU02_LDO_STEP_25MV,    \
0964     .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
0965     .n_voltages = S2MPU02_LDO_N_VOLTAGES,   \
0966     .vsel_reg   = S2MPU02_REG_L2CTRL1,      \
0967     .vsel_mask  = S2MPU02_LDO_VSEL_MASK,    \
0968     .enable_reg = S2MPU02_REG_L2CTRL1,      \
0969     .enable_mask    = S2MPU02_ENABLE_MASK       \
0970 }
0971 #define regulator_desc_s2mpu02_ldo3(num) {      \
0972     .name       = "LDO"#num,            \
0973     .id     = S2MPU02_LDO##num,     \
0974     .ops        = &s2mpu02_ldo_ops,     \
0975     .type       = REGULATOR_VOLTAGE,        \
0976     .owner      = THIS_MODULE,          \
0977     .min_uV     = S2MPU02_LDO_MIN_900MV,    \
0978     .uV_step    = S2MPU02_LDO_STEP_12_5MV,  \
0979     .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
0980     .n_voltages = S2MPU02_LDO_N_VOLTAGES,   \
0981     .vsel_reg   = S2MPU02_REG_L3CTRL + num - 3, \
0982     .vsel_mask  = S2MPU02_LDO_VSEL_MASK,    \
0983     .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
0984     .enable_mask    = S2MPU02_ENABLE_MASK       \
0985 }
0986 #define regulator_desc_s2mpu02_ldo4(num) {      \
0987     .name       = "LDO"#num,            \
0988     .id     = S2MPU02_LDO##num,     \
0989     .ops        = &s2mpu02_ldo_ops,     \
0990     .type       = REGULATOR_VOLTAGE,        \
0991     .owner      = THIS_MODULE,          \
0992     .min_uV     = S2MPU02_LDO_MIN_1050MV,   \
0993     .uV_step    = S2MPU02_LDO_STEP_25MV,    \
0994     .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
0995     .n_voltages = S2MPU02_LDO_N_VOLTAGES,   \
0996     .vsel_reg   = S2MPU02_REG_L3CTRL + num - 3, \
0997     .vsel_mask  = S2MPU02_LDO_VSEL_MASK,    \
0998     .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
0999     .enable_mask    = S2MPU02_ENABLE_MASK       \
1000 }
1001 #define regulator_desc_s2mpu02_ldo5(num) {      \
1002     .name       = "LDO"#num,            \
1003     .id     = S2MPU02_LDO##num,     \
1004     .ops        = &s2mpu02_ldo_ops,     \
1005     .type       = REGULATOR_VOLTAGE,        \
1006     .owner      = THIS_MODULE,          \
1007     .min_uV     = S2MPU02_LDO_MIN_1600MV,   \
1008     .uV_step    = S2MPU02_LDO_STEP_50MV,    \
1009     .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
1010     .n_voltages = S2MPU02_LDO_N_VOLTAGES,   \
1011     .vsel_reg   = S2MPU02_REG_L3CTRL + num - 3, \
1012     .vsel_mask  = S2MPU02_LDO_VSEL_MASK,    \
1013     .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
1014     .enable_mask    = S2MPU02_ENABLE_MASK       \
1015 }
1016 
1017 #define regulator_desc_s2mpu02_buck1234(num) {          \
1018     .name       = "BUCK"#num,               \
1019     .id     = S2MPU02_BUCK##num,            \
1020     .ops        = &s2mpu02_buck_ops,            \
1021     .type       = REGULATOR_VOLTAGE,            \
1022     .owner      = THIS_MODULE,              \
1023     .min_uV     = S2MPU02_BUCK1234_MIN_600MV,       \
1024     .uV_step    = S2MPU02_BUCK1234_STEP_6_25MV,     \
1025     .n_voltages = S2MPU02_BUCK_N_VOLTAGES,      \
1026     .linear_min_sel = S2MPU02_BUCK1234_START_SEL,       \
1027     .ramp_delay = S2MPU02_BUCK_RAMP_DELAY,      \
1028     .vsel_reg   = S2MPU02_REG_B1CTRL2 + (num - 1) * 2,  \
1029     .vsel_mask  = S2MPU02_BUCK_VSEL_MASK,       \
1030     .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2,  \
1031     .enable_mask    = S2MPU02_ENABLE_MASK           \
1032 }
1033 #define regulator_desc_s2mpu02_buck5(num) {         \
1034     .name       = "BUCK"#num,               \
1035     .id     = S2MPU02_BUCK##num,            \
1036     .ops        = &s2mpu02_ldo_ops,         \
1037     .type       = REGULATOR_VOLTAGE,            \
1038     .owner      = THIS_MODULE,              \
1039     .min_uV     = S2MPU02_BUCK5_MIN_1081_25MV,      \
1040     .uV_step    = S2MPU02_BUCK5_STEP_6_25MV,        \
1041     .n_voltages = S2MPU02_BUCK_N_VOLTAGES,      \
1042     .linear_min_sel = S2MPU02_BUCK5_START_SEL,      \
1043     .ramp_delay = S2MPU02_BUCK_RAMP_DELAY,      \
1044     .vsel_reg   = S2MPU02_REG_B5CTRL2,          \
1045     .vsel_mask  = S2MPU02_BUCK_VSEL_MASK,       \
1046     .enable_reg = S2MPU02_REG_B5CTRL1,          \
1047     .enable_mask    = S2MPU02_ENABLE_MASK           \
1048 }
1049 #define regulator_desc_s2mpu02_buck6(num) {         \
1050     .name       = "BUCK"#num,               \
1051     .id     = S2MPU02_BUCK##num,            \
1052     .ops        = &s2mpu02_ldo_ops,         \
1053     .type       = REGULATOR_VOLTAGE,            \
1054     .owner      = THIS_MODULE,              \
1055     .min_uV     = S2MPU02_BUCK6_MIN_1700MV,     \
1056     .uV_step    = S2MPU02_BUCK6_STEP_2_50MV,        \
1057     .n_voltages = S2MPU02_BUCK_N_VOLTAGES,      \
1058     .linear_min_sel = S2MPU02_BUCK6_START_SEL,      \
1059     .ramp_delay = S2MPU02_BUCK_RAMP_DELAY,      \
1060     .vsel_reg   = S2MPU02_REG_B6CTRL2,          \
1061     .vsel_mask  = S2MPU02_BUCK_VSEL_MASK,       \
1062     .enable_reg = S2MPU02_REG_B6CTRL1,          \
1063     .enable_mask    = S2MPU02_ENABLE_MASK           \
1064 }
1065 #define regulator_desc_s2mpu02_buck7(num) {         \
1066     .name       = "BUCK"#num,               \
1067     .id     = S2MPU02_BUCK##num,            \
1068     .ops        = &s2mpu02_ldo_ops,         \
1069     .type       = REGULATOR_VOLTAGE,            \
1070     .owner      = THIS_MODULE,              \
1071     .min_uV     = S2MPU02_BUCK7_MIN_900MV,      \
1072     .uV_step    = S2MPU02_BUCK7_STEP_6_25MV,        \
1073     .n_voltages = S2MPU02_BUCK_N_VOLTAGES,      \
1074     .linear_min_sel = S2MPU02_BUCK7_START_SEL,      \
1075     .ramp_delay = S2MPU02_BUCK_RAMP_DELAY,      \
1076     .vsel_reg   = S2MPU02_REG_B7CTRL2,          \
1077     .vsel_mask  = S2MPU02_BUCK_VSEL_MASK,       \
1078     .enable_reg = S2MPU02_REG_B7CTRL1,          \
1079     .enable_mask    = S2MPU02_ENABLE_MASK           \
1080 }
1081 
1082 static const struct regulator_desc s2mpu02_regulators[] = {
1083     regulator_desc_s2mpu02_ldo1(1),
1084     regulator_desc_s2mpu02_ldo2(2),
1085     regulator_desc_s2mpu02_ldo4(3),
1086     regulator_desc_s2mpu02_ldo5(4),
1087     regulator_desc_s2mpu02_ldo4(5),
1088     regulator_desc_s2mpu02_ldo3(6),
1089     regulator_desc_s2mpu02_ldo3(7),
1090     regulator_desc_s2mpu02_ldo4(8),
1091     regulator_desc_s2mpu02_ldo5(9),
1092     regulator_desc_s2mpu02_ldo3(10),
1093     regulator_desc_s2mpu02_ldo4(11),
1094     regulator_desc_s2mpu02_ldo5(12),
1095     regulator_desc_s2mpu02_ldo5(13),
1096     regulator_desc_s2mpu02_ldo5(14),
1097     regulator_desc_s2mpu02_ldo5(15),
1098     regulator_desc_s2mpu02_ldo5(16),
1099     regulator_desc_s2mpu02_ldo4(17),
1100     regulator_desc_s2mpu02_ldo5(18),
1101     regulator_desc_s2mpu02_ldo3(19),
1102     regulator_desc_s2mpu02_ldo4(20),
1103     regulator_desc_s2mpu02_ldo5(21),
1104     regulator_desc_s2mpu02_ldo5(22),
1105     regulator_desc_s2mpu02_ldo5(23),
1106     regulator_desc_s2mpu02_ldo4(24),
1107     regulator_desc_s2mpu02_ldo5(25),
1108     regulator_desc_s2mpu02_ldo4(26),
1109     regulator_desc_s2mpu02_ldo5(27),
1110     regulator_desc_s2mpu02_ldo5(28),
1111     regulator_desc_s2mpu02_buck1234(1),
1112     regulator_desc_s2mpu02_buck1234(2),
1113     regulator_desc_s2mpu02_buck1234(3),
1114     regulator_desc_s2mpu02_buck1234(4),
1115     regulator_desc_s2mpu02_buck5(5),
1116     regulator_desc_s2mpu02_buck6(6),
1117     regulator_desc_s2mpu02_buck7(7),
1118 };
1119 
1120 static int s2mps11_pmic_probe(struct platform_device *pdev)
1121 {
1122     struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
1123     struct of_regulator_match *rdata = NULL;
1124     struct regulator_config config = { };
1125     struct s2mps11_info *s2mps11;
1126     unsigned int rdev_num = 0;
1127     int i, ret = 0;
1128     const struct regulator_desc *regulators;
1129 
1130     s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
1131                 GFP_KERNEL);
1132     if (!s2mps11)
1133         return -ENOMEM;
1134 
1135     s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
1136     switch (s2mps11->dev_type) {
1137     case S2MPS11X:
1138         rdev_num = ARRAY_SIZE(s2mps11_regulators);
1139         regulators = s2mps11_regulators;
1140         BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators));
1141         break;
1142     case S2MPS13X:
1143         rdev_num = ARRAY_SIZE(s2mps13_regulators);
1144         regulators = s2mps13_regulators;
1145         BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps13_regulators));
1146         break;
1147     case S2MPS14X:
1148         rdev_num = ARRAY_SIZE(s2mps14_regulators);
1149         regulators = s2mps14_regulators;
1150         BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps14_regulators));
1151         break;
1152     case S2MPS15X:
1153         rdev_num = ARRAY_SIZE(s2mps15_regulators);
1154         regulators = s2mps15_regulators;
1155         BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators));
1156         break;
1157     case S2MPU02:
1158         rdev_num = ARRAY_SIZE(s2mpu02_regulators);
1159         regulators = s2mpu02_regulators;
1160         BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mpu02_regulators));
1161         break;
1162     default:
1163         dev_err(&pdev->dev, "Invalid device type: %u\n",
1164                     s2mps11->dev_type);
1165         return -EINVAL;
1166     }
1167 
1168     s2mps11->ext_control_gpiod = devm_kcalloc(&pdev->dev, rdev_num,
1169                    sizeof(*s2mps11->ext_control_gpiod), GFP_KERNEL);
1170     if (!s2mps11->ext_control_gpiod)
1171         return -ENOMEM;
1172 
1173     rdata = kcalloc(rdev_num, sizeof(*rdata), GFP_KERNEL);
1174     if (!rdata)
1175         return -ENOMEM;
1176 
1177     for (i = 0; i < rdev_num; i++)
1178         rdata[i].name = regulators[i].name;
1179 
1180     ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, rdev_num);
1181     if (ret)
1182         goto out;
1183 
1184     platform_set_drvdata(pdev, s2mps11);
1185 
1186     config.dev = &pdev->dev;
1187     config.regmap = iodev->regmap_pmic;
1188     config.driver_data = s2mps11;
1189     for (i = 0; i < rdev_num; i++) {
1190         struct regulator_dev *regulator;
1191 
1192         config.init_data = rdata[i].init_data;
1193         config.of_node = rdata[i].of_node;
1194         config.ena_gpiod = s2mps11->ext_control_gpiod[i];
1195         /*
1196          * Hand the GPIO descriptor management over to the regulator
1197          * core, remove it from devres management.
1198          */
1199         if (config.ena_gpiod)
1200             devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
1201         regulator = devm_regulator_register(&pdev->dev,
1202                         &regulators[i], &config);
1203         if (IS_ERR(regulator)) {
1204             ret = PTR_ERR(regulator);
1205             dev_err(&pdev->dev, "regulator init failed for %d\n",
1206                 i);
1207             goto out;
1208         }
1209 
1210         if (config.ena_gpiod) {
1211             ret = s2mps14_pmic_enable_ext_control(s2mps11,
1212                     regulator);
1213             if (ret < 0) {
1214                 dev_err(&pdev->dev,
1215                         "failed to enable GPIO control over %s: %d\n",
1216                         regulator->desc->name, ret);
1217                 goto out;
1218             }
1219         }
1220     }
1221 
1222 out:
1223     kfree(rdata);
1224 
1225     return ret;
1226 }
1227 
1228 static const struct platform_device_id s2mps11_pmic_id[] = {
1229     { "s2mps11-regulator", S2MPS11X},
1230     { "s2mps13-regulator", S2MPS13X},
1231     { "s2mps14-regulator", S2MPS14X},
1232     { "s2mps15-regulator", S2MPS15X},
1233     { "s2mpu02-regulator", S2MPU02},
1234     { },
1235 };
1236 MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1237 
1238 static struct platform_driver s2mps11_pmic_driver = {
1239     .driver = {
1240         .name = "s2mps11-pmic",
1241     },
1242     .probe = s2mps11_pmic_probe,
1243     .id_table = s2mps11_pmic_id,
1244 };
1245 
1246 module_platform_driver(s2mps11_pmic_driver);
1247 
1248 /* Module information */
1249 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
1250 MODULE_DESCRIPTION("Samsung S2MPS11/S2MPS14/S2MPS15/S2MPU02 Regulator Driver");
1251 MODULE_LICENSE("GPL");