Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 //
0003 // max77826-regulator.c  - regulator driver for Maxim MAX77826
0004 //
0005 // Author: Iskren Chernev <iskren.chernev@gmail.com>
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/err.h>
0011 #include <linux/of.h>
0012 #include <linux/of_device.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/regulator/driver.h>
0015 #include <linux/regulator/of_regulator.h>
0016 #include <linux/i2c.h>
0017 #include <linux/regmap.h>
0018 
0019 enum max77826_registers {
0020     MAX77826_REG_INT_SRC = 0x00,
0021     MAX77826_REG_SYS_INT,
0022     MAX77826_REG_INT1,
0023     MAX77826_REG_INT2,
0024     MAX77826_REG_BB_INT,
0025     MAX77826_REG_INT_SRC_M,
0026     MAX77826_REG_TOPSYS_INT_M,
0027     MAX77826_REG_INT1_M,
0028     MAX77826_REG_INT2_M,
0029     MAX77826_REG_BB_INT_M,
0030     MAX77826_REG_TOPSYS_STAT,
0031     MAX77826_REG_STAT1,
0032     MAX77826_REG_STAT2,
0033     MAX77826_REG_BB_STAT,
0034     /* 0x0E - 0x0F: Reserved */
0035     MAX77826_REG_LDO_OPMD1 = 0x10,
0036     MAX77826_REG_LDO_OPMD2,
0037     MAX77826_REG_LDO_OPMD3,
0038     MAX77826_REG_LDO_OPMD4,
0039     MAX77826_REG_B_BB_OPMD,
0040     /* 0x15 - 0x1F: Reserved */
0041     MAX77826_REG_LDO1_CFG = 0x20,
0042     MAX77826_REG_LDO2_CFG,
0043     MAX77826_REG_LDO3_CFG,
0044     MAX77826_REG_LDO4_CFG,
0045     MAX77826_REG_LDO5_CFG,
0046     MAX77826_REG_LDO6_CFG,
0047     MAX77826_REG_LDO7_CFG,
0048     MAX77826_REG_LDO8_CFG,
0049     MAX77826_REG_LDO9_CFG,
0050     MAX77826_REG_LDO10_CFG,
0051     MAX77826_REG_LDO11_CFG,
0052     MAX77826_REG_LDO12_CFG,
0053     MAX77826_REG_LDO13_CFG,
0054     MAX77826_REG_LDO14_CFG,
0055     MAX77826_REG_LDO15_CFG,
0056     /* 0x2F: Reserved */
0057     MAX77826_REG_BUCK_CFG = 0x30,
0058     MAX77826_REG_BUCK_VOUT,
0059     MAX77826_REG_BB_CFG,
0060     MAX77826_REG_BB_VOUT,
0061     /* 0x34 - 0x3F: Reserved */
0062     MAX77826_REG_BUCK_SS_FREQ = 0x40,
0063     MAX77826_REG_UVLO_FALL,
0064     /* 0x42 - 0xCE: Reserved */
0065     MAX77826_REG_DEVICE_ID = 0xCF,
0066 };
0067 
0068 enum max77826_regulators {
0069     MAX77826_LDO1 = 0,
0070     MAX77826_LDO2,
0071     MAX77826_LDO3,
0072     MAX77826_LDO4,
0073     MAX77826_LDO5,
0074     MAX77826_LDO6,
0075     MAX77826_LDO7,
0076     MAX77826_LDO8,
0077     MAX77826_LDO9,
0078     MAX77826_LDO10,
0079     MAX77826_LDO11,
0080     MAX77826_LDO12,
0081     MAX77826_LDO13,
0082     MAX77826_LDO14,
0083     MAX77826_LDO15,
0084     MAX77826_BUCK,
0085     MAX77826_BUCKBOOST,
0086     MAX77826_MAX_REGULATORS,
0087 };
0088 
0089 #define MAX77826_MASK_LDO       0x7f
0090 #define MAX77826_MASK_BUCK      0xff
0091 #define MAX77826_MASK_BUCKBOOST     0x7f
0092 #define MAX77826_BUCK_RAMP_DELAY    12500
0093 
0094 /* values in mV */
0095 /* for LDO1-3 */
0096 #define MAX77826_NMOS_LDO_VOLT_MIN  600000
0097 #define MAX77826_NMOS_LDO_VOLT_MAX  2187500
0098 #define MAX77826_NMOS_LDO_VOLT_STEP 12500
0099 
0100 /* for LDO4-15 */
0101 #define MAX77826_PMOS_LDO_VOLT_MIN  800000
0102 #define MAX77826_PMOS_LDO_VOLT_MAX  3975000
0103 #define MAX77826_PMOS_LDO_VOLT_STEP 25000
0104 
0105 /* for BUCK */
0106 #define MAX77826_BUCK_VOLT_MIN      500000
0107 #define MAX77826_BUCK_VOLT_MAX      1800000
0108 #define MAX77826_BUCK_VOLT_STEP     6250
0109 
0110 /* for BUCKBOOST */
0111 #define MAX77826_BUCKBOOST_VOLT_MIN 2600000
0112 #define MAX77826_BUCKBOOST_VOLT_MAX 4187500
0113 #define MAX77826_BUCKBOOST_VOLT_STEP    12500
0114 #define MAX77826_VOLT_RANGE(_type)                  \
0115     ((MAX77826_ ## _type ## _VOLT_MAX -             \
0116       MAX77826_ ## _type ## _VOLT_MIN) /                \
0117      MAX77826_ ## _type ## _VOLT_STEP + 1)
0118 
0119 #define MAX77826_LDO(_id, _type)                    \
0120     [MAX77826_LDO ## _id] = {                   \
0121         .id = MAX77826_LDO ## _id,              \
0122         .name = "LDO"#_id,                  \
0123         .of_match = of_match_ptr("LDO"#_id),            \
0124         .regulators_node = "regulators",            \
0125         .ops = &max77826_most_ops,              \
0126         .min_uV = MAX77826_ ## _type ## _LDO_VOLT_MIN,      \
0127         .uV_step = MAX77826_ ## _type ## _LDO_VOLT_STEP,    \
0128         .n_voltages = MAX77826_VOLT_RANGE(_type ## _LDO),   \
0129         .enable_reg = MAX77826_REG_LDO_OPMD1 + (_id - 1) / 4,   \
0130         .enable_mask = BIT(((_id - 1) % 4) * 2 + 1),        \
0131         .vsel_reg = MAX77826_REG_LDO1_CFG + (_id - 1),      \
0132         .vsel_mask = MAX77826_MASK_LDO,             \
0133         .owner = THIS_MODULE,                   \
0134     }
0135 
0136 #define MAX77826_BUCK(_idx, _id, _ops)                  \
0137     [MAX77826_ ## _id] = {                      \
0138         .id = MAX77826_ ## _id,                 \
0139         .name = #_id,                       \
0140         .of_match = of_match_ptr(#_id),             \
0141         .regulators_node = "regulators",            \
0142         .ops = &_ops,                       \
0143         .min_uV =  MAX77826_ ## _id ## _VOLT_MIN,       \
0144         .uV_step = MAX77826_ ## _id ## _VOLT_STEP,      \
0145         .n_voltages = MAX77826_VOLT_RANGE(_id),         \
0146         .enable_reg = MAX77826_REG_B_BB_OPMD,           \
0147         .enable_mask = BIT(_idx * 2 + 1),           \
0148         .vsel_reg = MAX77826_REG_BUCK_VOUT + _idx * 2,      \
0149         .vsel_mask = MAX77826_MASK_ ## _id,         \
0150         .owner = THIS_MODULE,                   \
0151     }
0152 
0153 
0154 
0155 struct max77826_regulator_info {
0156     struct regmap *regmap;
0157     struct regulator_desc *rdesc;
0158 };
0159 
0160 static const struct regmap_config max77826_regmap_config = {
0161     .reg_bits = 8,
0162     .val_bits = 8,
0163     .max_register = MAX77826_REG_DEVICE_ID,
0164 };
0165 
0166 static int max77826_set_voltage_time_sel(struct regulator_dev *,
0167                 unsigned int old_selector,
0168                 unsigned int new_selector);
0169 
0170 static const struct regulator_ops max77826_most_ops = {
0171     .enable         = regulator_enable_regmap,
0172     .disable        = regulator_disable_regmap,
0173     .is_enabled     = regulator_is_enabled_regmap,
0174     .list_voltage       = regulator_list_voltage_linear,
0175     .map_voltage        = regulator_map_voltage_linear,
0176     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0177     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0178 };
0179 
0180 static const struct regulator_ops max77826_buck_ops = {
0181     .enable         = regulator_enable_regmap,
0182     .disable        = regulator_disable_regmap,
0183     .is_enabled     = regulator_is_enabled_regmap,
0184     .list_voltage       = regulator_list_voltage_linear,
0185     .map_voltage        = regulator_map_voltage_linear,
0186     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0187     .set_voltage_sel    = regulator_set_voltage_sel_regmap,
0188     .set_voltage_time_sel   = max77826_set_voltage_time_sel,
0189 };
0190 
0191 static struct regulator_desc max77826_regulators_desc[] = {
0192     MAX77826_LDO(1, NMOS),
0193     MAX77826_LDO(2, NMOS),
0194     MAX77826_LDO(3, NMOS),
0195     MAX77826_LDO(4, PMOS),
0196     MAX77826_LDO(5, PMOS),
0197     MAX77826_LDO(6, PMOS),
0198     MAX77826_LDO(7, PMOS),
0199     MAX77826_LDO(8, PMOS),
0200     MAX77826_LDO(9, PMOS),
0201     MAX77826_LDO(10, PMOS),
0202     MAX77826_LDO(11, PMOS),
0203     MAX77826_LDO(12, PMOS),
0204     MAX77826_LDO(13, PMOS),
0205     MAX77826_LDO(14, PMOS),
0206     MAX77826_LDO(15, PMOS),
0207     MAX77826_BUCK(0, BUCK, max77826_buck_ops),
0208     MAX77826_BUCK(1, BUCKBOOST, max77826_most_ops),
0209 };
0210 
0211 static int max77826_set_voltage_time_sel(struct regulator_dev *rdev,
0212                 unsigned int old_selector,
0213                 unsigned int new_selector)
0214 {
0215     if (new_selector > old_selector) {
0216         return DIV_ROUND_UP(MAX77826_BUCK_VOLT_STEP *
0217                 (new_selector - old_selector),
0218                 MAX77826_BUCK_RAMP_DELAY);
0219     }
0220 
0221     return 0;
0222 }
0223 
0224 static int max77826_read_device_id(struct regmap *regmap, struct device *dev)
0225 {
0226     unsigned int device_id;
0227     int res;
0228 
0229     res = regmap_read(regmap, MAX77826_REG_DEVICE_ID, &device_id);
0230     if (!res)
0231         dev_dbg(dev, "DEVICE_ID: 0x%x\n", device_id);
0232 
0233     return res;
0234 }
0235 
0236 static int max77826_i2c_probe(struct i2c_client *client)
0237 {
0238     struct device *dev = &client->dev;
0239     struct max77826_regulator_info *info;
0240     struct regulator_config config = {};
0241     struct regulator_dev *rdev;
0242     struct regmap *regmap;
0243     int i;
0244 
0245     info = devm_kzalloc(dev, sizeof(struct max77826_regulator_info),
0246                 GFP_KERNEL);
0247     if (!info)
0248         return -ENOMEM;
0249 
0250     info->rdesc = max77826_regulators_desc;
0251     regmap = devm_regmap_init_i2c(client, &max77826_regmap_config);
0252     if (IS_ERR(regmap)) {
0253         dev_err(dev, "Failed to allocate regmap!\n");
0254         return PTR_ERR(regmap);
0255     }
0256 
0257     info->regmap = regmap;
0258     i2c_set_clientdata(client, info);
0259 
0260     config.dev = dev;
0261     config.regmap = regmap;
0262     config.driver_data = info;
0263 
0264     for (i = 0; i < MAX77826_MAX_REGULATORS; i++) {
0265         rdev = devm_regulator_register(dev,
0266                            &max77826_regulators_desc[i],
0267                            &config);
0268         if (IS_ERR(rdev)) {
0269             dev_err(dev, "Failed to register regulator!\n");
0270             return PTR_ERR(rdev);
0271         }
0272     }
0273 
0274     return max77826_read_device_id(regmap, dev);
0275 }
0276 
0277 static const struct of_device_id __maybe_unused max77826_of_match[] = {
0278     { .compatible = "maxim,max77826" },
0279     { /* sentinel */ }
0280 };
0281 MODULE_DEVICE_TABLE(of, max77826_of_match);
0282 
0283 static const struct i2c_device_id max77826_id[] = {
0284     { "max77826-regulator" },
0285     { /* sentinel */ }
0286 };
0287 MODULE_DEVICE_TABLE(i2c, max77826_id);
0288 
0289 static struct i2c_driver max77826_regulator_driver = {
0290     .driver = {
0291         .name = "max77826",
0292         .of_match_table = of_match_ptr(max77826_of_match),
0293     },
0294     .probe_new = max77826_i2c_probe,
0295     .id_table = max77826_id,
0296 };
0297 module_i2c_driver(max77826_regulator_driver);
0298 
0299 MODULE_AUTHOR("Iskren Chernev <iskren.chernev@gmail.com>");
0300 MODULE_DESCRIPTION("MAX77826 PMIC regulator driver");
0301 MODULE_LICENSE("GPL");