Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // mp5416.c  - regulator driver for mps mp5416
0004 //
0005 // Copyright 2020 Monolithic Power Systems, Inc
0006 //
0007 // Author: Saravanan Sekar <sravanhome@gmail.com>
0008 
0009 #include <linux/err.h>
0010 #include <linux/i2c.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 #include <linux/of_device.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regmap.h>
0016 #include <linux/regulator/driver.h>
0017 
0018 #define MP5416_REG_CTL0         0x00
0019 #define MP5416_REG_CTL1         0x01
0020 #define MP5416_REG_CTL2         0x02
0021 #define MP5416_REG_ILIM         0x03
0022 #define MP5416_REG_BUCK1        0x04
0023 #define MP5416_REG_BUCK2        0x05
0024 #define MP5416_REG_BUCK3        0x06
0025 #define MP5416_REG_BUCK4        0x07
0026 #define MP5416_REG_LDO1         0x08
0027 #define MP5416_REG_LDO2         0x09
0028 #define MP5416_REG_LDO3         0x0a
0029 #define MP5416_REG_LDO4         0x0b
0030 
0031 #define MP5416_REGULATOR_EN     BIT(7)
0032 #define MP5416_MASK_VSET        0x7f
0033 #define MP5416_MASK_BUCK1_ILIM      0xc0
0034 #define MP5416_MASK_BUCK2_ILIM      0x0c
0035 #define MP5416_MASK_BUCK3_ILIM      0x30
0036 #define MP5416_MASK_BUCK4_ILIM      0x03
0037 #define MP5416_MASK_DVS_SLEWRATE    0xc0
0038 
0039 /* values in uV */
0040 #define MP5416_VOLT1_MIN        600000
0041 #define MP5416_VOLT1_MAX        2187500
0042 #define MP5416_VOLT1_STEP       12500
0043 #define MP5416_VOLT2_MIN        800000
0044 #define MP5416_VOLT2_MAX        3975000
0045 #define MP5416_VOLT2_STEP       25000
0046 
0047 #define MP5416_VOLT1_RANGE \
0048     ((MP5416_VOLT1_MAX - MP5416_VOLT1_MIN)/MP5416_VOLT1_STEP + 1)
0049 #define MP5416_VOLT2_RANGE \
0050     ((MP5416_VOLT2_MAX - MP5416_VOLT2_MIN)/MP5416_VOLT2_STEP + 1)
0051 
0052 #define MP5416BUCK(_name, _id, _ilim, _dreg, _dval, _vsel)      \
0053     [MP5416_BUCK ## _id] = {                    \
0054         .id = MP5416_BUCK ## _id,               \
0055         .name = _name,                      \
0056         .of_match = _name,                  \
0057         .regulators_node = "regulators",            \
0058         .ops = &mp5416_buck_ops,                \
0059         .min_uV = MP5416_VOLT ##_vsel## _MIN,           \
0060         .uV_step = MP5416_VOLT ##_vsel## _STEP,         \
0061         .n_voltages = MP5416_VOLT ##_vsel## _RANGE,     \
0062         .curr_table = _ilim,                    \
0063         .n_current_limits = ARRAY_SIZE(_ilim),          \
0064         .csel_reg = MP5416_REG_ILIM,                \
0065         .csel_mask = MP5416_MASK_BUCK ## _id ##_ILIM,       \
0066         .vsel_reg = MP5416_REG_BUCK ## _id,         \
0067         .vsel_mask = MP5416_MASK_VSET,              \
0068         .enable_reg = MP5416_REG_BUCK ## _id,           \
0069         .enable_mask = MP5416_REGULATOR_EN,         \
0070         .ramp_reg = MP5416_REG_CTL2,                \
0071         .ramp_mask = MP5416_MASK_DVS_SLEWRATE,          \
0072         .ramp_delay_table = mp5416_buck_ramp_table,     \
0073         .n_ramp_values = ARRAY_SIZE(mp5416_buck_ramp_table),    \
0074         .active_discharge_on    = _dval,            \
0075         .active_discharge_reg   = _dreg,            \
0076         .active_discharge_mask  = _dval,            \
0077         .owner          = THIS_MODULE,          \
0078     }
0079 
0080 #define MP5416LDO(_name, _id, _dval)                    \
0081     [MP5416_LDO ## _id] = {                     \
0082         .id = MP5416_LDO ## _id,                \
0083         .name = _name,                      \
0084         .of_match = _name,                  \
0085         .regulators_node = "regulators",            \
0086         .ops = &mp5416_ldo_ops,                 \
0087         .min_uV = MP5416_VOLT2_MIN,             \
0088         .uV_step = MP5416_VOLT2_STEP,               \
0089         .n_voltages = MP5416_VOLT2_RANGE,           \
0090         .vsel_reg = MP5416_REG_LDO ##_id,           \
0091         .vsel_mask = MP5416_MASK_VSET,              \
0092         .enable_reg = MP5416_REG_LDO ##_id,         \
0093         .enable_mask = MP5416_REGULATOR_EN,         \
0094         .active_discharge_on    = _dval,            \
0095         .active_discharge_reg   = MP5416_REG_CTL2,      \
0096         .active_discharge_mask  = _dval,            \
0097         .owner          = THIS_MODULE,          \
0098     }
0099 
0100 enum mp5416_regulators {
0101     MP5416_BUCK1,
0102     MP5416_BUCK2,
0103     MP5416_BUCK3,
0104     MP5416_BUCK4,
0105     MP5416_LDO1,
0106     MP5416_LDO2,
0107     MP5416_LDO3,
0108     MP5416_LDO4,
0109     MP5416_MAX_REGULATORS,
0110 };
0111 
0112 static const struct regmap_config mp5416_regmap_config = {
0113     .reg_bits = 8,
0114     .val_bits = 8,
0115     .max_register = 0x0d,
0116 };
0117 
0118 /* Current limits array (in uA)
0119  * ILIM1 & ILIM3
0120  */
0121 static const unsigned int mp5416_I_limits1[] = {
0122     3800000, 4600000, 5600000, 6800000
0123 };
0124 
0125 /* ILIM2 & ILIM4 */
0126 static const unsigned int mp5416_I_limits2[] = {
0127     2200000, 3200000, 4200000, 5200000
0128 };
0129 
0130 /*
0131  * DVS ramp rate BUCK1 to BUCK4
0132  * 00: 32mV/us
0133  * 01: 16mV/us
0134  * 10: 8mV/us
0135  * 11: 4mV/us
0136  */
0137 static const unsigned int mp5416_buck_ramp_table[] = {
0138     32000, 16000, 8000, 4000
0139 };
0140 
0141 static const struct regulator_ops mp5416_ldo_ops = {
0142     .enable         = regulator_enable_regmap,
0143     .disable        = regulator_disable_regmap,
0144     .is_enabled     = regulator_is_enabled_regmap,
0145     .list_voltage       = regulator_list_voltage_linear,
0146     .map_voltage        = regulator_map_voltage_linear,
0147     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0148     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0149     .set_active_discharge   = regulator_set_active_discharge_regmap,
0150 };
0151 
0152 static const struct regulator_ops mp5416_buck_ops = {
0153     .enable         = regulator_enable_regmap,
0154     .disable        = regulator_disable_regmap,
0155     .is_enabled     = regulator_is_enabled_regmap,
0156     .list_voltage       = regulator_list_voltage_linear,
0157     .map_voltage        = regulator_map_voltage_linear,
0158     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0159     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0160     .set_active_discharge   = regulator_set_active_discharge_regmap,
0161     .get_current_limit  = regulator_get_current_limit_regmap,
0162     .set_current_limit  = regulator_set_current_limit_regmap,
0163     .set_ramp_delay     = regulator_set_ramp_delay_regmap,
0164 };
0165 
0166 static struct regulator_desc mp5416_regulators_desc[MP5416_MAX_REGULATORS] = {
0167     MP5416BUCK("buck1", 1, mp5416_I_limits1, MP5416_REG_CTL1, BIT(0), 1),
0168     MP5416BUCK("buck2", 2, mp5416_I_limits2, MP5416_REG_CTL1, BIT(1), 2),
0169     MP5416BUCK("buck3", 3, mp5416_I_limits1, MP5416_REG_CTL1, BIT(2), 1),
0170     MP5416BUCK("buck4", 4, mp5416_I_limits2, MP5416_REG_CTL2, BIT(5), 2),
0171     MP5416LDO("ldo1", 1, BIT(4)),
0172     MP5416LDO("ldo2", 2, BIT(3)),
0173     MP5416LDO("ldo3", 3, BIT(2)),
0174     MP5416LDO("ldo4", 4, BIT(1)),
0175 };
0176 
0177 static struct regulator_desc mp5496_regulators_desc[MP5416_MAX_REGULATORS] = {
0178     MP5416BUCK("buck1", 1, mp5416_I_limits1, MP5416_REG_CTL1, BIT(0), 1),
0179     MP5416BUCK("buck2", 2, mp5416_I_limits2, MP5416_REG_CTL1, BIT(1), 1),
0180     MP5416BUCK("buck3", 3, mp5416_I_limits1, MP5416_REG_CTL1, BIT(2), 1),
0181     MP5416BUCK("buck4", 4, mp5416_I_limits2, MP5416_REG_CTL2, BIT(5), 1),
0182     MP5416LDO("ldo1", 1, BIT(4)),
0183     MP5416LDO("ldo2", 2, BIT(3)),
0184     MP5416LDO("ldo3", 3, BIT(2)),
0185     MP5416LDO("ldo4", 4, BIT(1)),
0186 };
0187 
0188 static int mp5416_i2c_probe(struct i2c_client *client)
0189 {
0190     struct device *dev = &client->dev;
0191     struct regulator_config config = { NULL, };
0192     static const struct regulator_desc *desc;
0193     struct regulator_dev *rdev;
0194     struct regmap *regmap;
0195     int i;
0196 
0197     regmap = devm_regmap_init_i2c(client, &mp5416_regmap_config);
0198     if (IS_ERR(regmap)) {
0199         dev_err(dev, "Failed to allocate regmap!\n");
0200         return PTR_ERR(regmap);
0201     }
0202 
0203     desc = of_device_get_match_data(dev);
0204     if (!desc)
0205         return -ENODEV;
0206 
0207     config.dev = dev;
0208     config.regmap = regmap;
0209 
0210     for (i = 0; i < MP5416_MAX_REGULATORS; i++) {
0211         rdev = devm_regulator_register(dev,
0212                            &desc[i],
0213                            &config);
0214         if (IS_ERR(rdev)) {
0215             dev_err(dev, "Failed to register regulator!\n");
0216             return PTR_ERR(rdev);
0217         }
0218     }
0219 
0220     return 0;
0221 }
0222 
0223 static const struct of_device_id mp5416_of_match[] = {
0224     { .compatible = "mps,mp5416", .data = &mp5416_regulators_desc },
0225     { .compatible = "mps,mp5496", .data = &mp5496_regulators_desc },
0226     {},
0227 };
0228 MODULE_DEVICE_TABLE(of, mp5416_of_match);
0229 
0230 static const struct i2c_device_id mp5416_id[] = {
0231     { "mp5416", },
0232     { "mp5496", },
0233     { },
0234 };
0235 MODULE_DEVICE_TABLE(i2c, mp5416_id);
0236 
0237 static struct i2c_driver mp5416_regulator_driver = {
0238     .driver = {
0239         .name = "mp5416",
0240         .of_match_table = of_match_ptr(mp5416_of_match),
0241     },
0242     .probe_new = mp5416_i2c_probe,
0243     .id_table = mp5416_id,
0244 };
0245 module_i2c_driver(mp5416_regulator_driver);
0246 
0247 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
0248 MODULE_DESCRIPTION("MP5416 PMIC regulator driver");
0249 MODULE_LICENSE("GPL");