Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * tps65023-regulator.c
0004  *
0005  * Supports TPS65023 Regulator
0006  *
0007  * Copyright (C) 2009 Texas Instrument Incorporated - https://www.ti.com/
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/err.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regulator/driver.h>
0016 #include <linux/regulator/machine.h>
0017 #include <linux/i2c.h>
0018 #include <linux/slab.h>
0019 #include <linux/regmap.h>
0020 
0021 /* Register definitions */
0022 #define TPS65023_REG_VERSION        0
0023 #define TPS65023_REG_PGOODZ     1
0024 #define TPS65023_REG_MASK       2
0025 #define TPS65023_REG_REG_CTRL       3
0026 #define TPS65023_REG_CON_CTRL       4
0027 #define TPS65023_REG_CON_CTRL2      5
0028 #define TPS65023_REG_DEF_CORE       6
0029 #define TPS65023_REG_DEFSLEW        7
0030 #define TPS65023_REG_LDO_CTRL       8
0031 
0032 /* PGOODZ bitfields */
0033 #define TPS65023_PGOODZ_PWRFAILZ    BIT(7)
0034 #define TPS65023_PGOODZ_LOWBATTZ    BIT(6)
0035 #define TPS65023_PGOODZ_VDCDC1      BIT(5)
0036 #define TPS65023_PGOODZ_VDCDC2      BIT(4)
0037 #define TPS65023_PGOODZ_VDCDC3      BIT(3)
0038 #define TPS65023_PGOODZ_LDO2        BIT(2)
0039 #define TPS65023_PGOODZ_LDO1        BIT(1)
0040 
0041 /* MASK bitfields */
0042 #define TPS65023_MASK_PWRFAILZ      BIT(7)
0043 #define TPS65023_MASK_LOWBATTZ      BIT(6)
0044 #define TPS65023_MASK_VDCDC1        BIT(5)
0045 #define TPS65023_MASK_VDCDC2        BIT(4)
0046 #define TPS65023_MASK_VDCDC3        BIT(3)
0047 #define TPS65023_MASK_LDO2      BIT(2)
0048 #define TPS65023_MASK_LDO1      BIT(1)
0049 
0050 /* REG_CTRL bitfields */
0051 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
0052 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
0053 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
0054 #define TPS65023_REG_CTRL_LDO2_EN   BIT(2)
0055 #define TPS65023_REG_CTRL_LDO1_EN   BIT(1)
0056 
0057 /* REG_CTRL2 bitfields */
0058 #define TPS65023_REG_CTRL2_GO       BIT(7)
0059 #define TPS65023_REG_CTRL2_CORE_ADJ BIT(6)
0060 #define TPS65023_REG_CTRL2_DCDC2    BIT(2)
0061 #define TPS65023_REG_CTRL2_DCDC1    BIT(1)
0062 #define TPS65023_REG_CTRL2_DCDC3    BIT(0)
0063 
0064 /* Number of step-down converters available */
0065 #define TPS65023_NUM_DCDC       3
0066 /* Number of LDO voltage regulators  available */
0067 #define TPS65023_NUM_LDO        2
0068 /* Number of total regulators available */
0069 #define TPS65023_NUM_REGULATOR  (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
0070 
0071 /* DCDCs */
0072 #define TPS65023_DCDC_1         0
0073 #define TPS65023_DCDC_2         1
0074 #define TPS65023_DCDC_3         2
0075 /* LDOs */
0076 #define TPS65023_LDO_1          3
0077 #define TPS65023_LDO_2          4
0078 
0079 #define TPS65023_MAX_REG_ID     TPS65023_LDO_2
0080 
0081 #define TPS65023_REGULATOR_DCDC(_num, _t, _em)          \
0082     {                           \
0083         .name       = "VDCDC"#_num,         \
0084         .of_match   = of_match_ptr("VDCDC"#_num),   \
0085         .regulators_node = of_match_ptr("regulators"),  \
0086         .id     = TPS65023_DCDC_##_num,     \
0087         .n_voltages     = ARRAY_SIZE(_t),       \
0088         .ops        = &tps65023_dcdc_ops,       \
0089         .type       = REGULATOR_VOLTAGE,        \
0090         .owner      = THIS_MODULE,          \
0091         .volt_table = _t,               \
0092         .vsel_reg   = TPS65023_REG_DEF_CORE,    \
0093         .vsel_mask  = ARRAY_SIZE(_t) - 1,       \
0094         .enable_mask    = _em,              \
0095         .enable_reg = TPS65023_REG_REG_CTRL,    \
0096         .apply_reg  = TPS65023_REG_CON_CTRL2,   \
0097         .apply_bit  = TPS65023_REG_CTRL2_GO,    \
0098     }                           \
0099 
0100 #define TPS65023_REGULATOR_LDO(_num, _t, _vm)           \
0101     {                           \
0102         .name       = "LDO"#_num,           \
0103         .of_match   = of_match_ptr("LDO"#_num), \
0104         .regulators_node = of_match_ptr("regulators"),  \
0105         .id     = TPS65023_LDO_##_num,      \
0106         .n_voltages     = ARRAY_SIZE(_t),       \
0107         .ops        = &tps65023_ldo_ops,        \
0108         .type       = REGULATOR_VOLTAGE,        \
0109         .owner      = THIS_MODULE,          \
0110         .volt_table = _t,               \
0111         .vsel_reg   = TPS65023_REG_LDO_CTRL,    \
0112         .vsel_mask  = _vm,              \
0113         .enable_mask    = 1 << (_num),          \
0114         .enable_reg = TPS65023_REG_REG_CTRL,    \
0115     }                           \
0116 
0117 /* Supported voltage values for regulators */
0118 static const unsigned int VCORE_VSEL_table[] = {
0119     800000, 825000, 850000, 875000,
0120     900000, 925000, 950000, 975000,
0121     1000000, 1025000, 1050000, 1075000,
0122     1100000, 1125000, 1150000, 1175000,
0123     1200000, 1225000, 1250000, 1275000,
0124     1300000, 1325000, 1350000, 1375000,
0125     1400000, 1425000, 1450000, 1475000,
0126     1500000, 1525000, 1550000, 1600000,
0127 };
0128 
0129 static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = {
0130     3300000,
0131 };
0132 
0133 static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = {
0134     1800000,
0135 };
0136 
0137 /* Supported voltage values for LDO regulators for tps65020 */
0138 static const unsigned int TPS65020_LDO_VSEL_table[] = {
0139     1000000, 1050000, 1100000, 1300000,
0140     1800000, 2500000, 3000000, 3300000,
0141 };
0142 
0143 /* Supported voltage values for LDO regulators
0144  * for tps65021 and tps65023 */
0145 static const unsigned int TPS65023_LDO1_VSEL_table[] = {
0146     1000000, 1100000, 1300000, 1800000,
0147     2200000, 2600000, 2800000, 3150000,
0148 };
0149 
0150 static const unsigned int TPS65023_LDO2_VSEL_table[] = {
0151     1050000, 1200000, 1300000, 1800000,
0152     2500000, 2800000, 3000000, 3300000,
0153 };
0154 
0155 /* PMIC details */
0156 struct tps_pmic {
0157     struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
0158     const struct tps_driver_data *driver_data;
0159     struct regmap *regmap;
0160 };
0161 
0162 /* Struct passed as driver data */
0163 struct tps_driver_data {
0164     const struct regulator_desc *desc;
0165     u8 core_regulator;
0166 };
0167 
0168 static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev)
0169 {
0170     struct tps_pmic *tps = rdev_get_drvdata(dev);
0171     int dcdc = rdev_get_id(dev);
0172 
0173     if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
0174         return -EINVAL;
0175 
0176     if (dcdc != tps->driver_data->core_regulator)
0177         return 0;
0178 
0179     return regulator_get_voltage_sel_regmap(dev);
0180 }
0181 
0182 static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev,
0183                      unsigned selector)
0184 {
0185     struct tps_pmic *tps = rdev_get_drvdata(dev);
0186     int dcdc = rdev_get_id(dev);
0187 
0188     if (dcdc != tps->driver_data->core_regulator)
0189         return -EINVAL;
0190 
0191     return regulator_set_voltage_sel_regmap(dev, selector);
0192 }
0193 
0194 /* Operations permitted on VDCDCx */
0195 static const struct regulator_ops tps65023_dcdc_ops = {
0196     .is_enabled = regulator_is_enabled_regmap,
0197     .enable = regulator_enable_regmap,
0198     .disable = regulator_disable_regmap,
0199     .get_voltage_sel = tps65023_dcdc_get_voltage_sel,
0200     .set_voltage_sel = tps65023_dcdc_set_voltage_sel,
0201     .list_voltage = regulator_list_voltage_table,
0202     .map_voltage = regulator_map_voltage_ascend,
0203 };
0204 
0205 /* Operations permitted on LDOx */
0206 static const struct regulator_ops tps65023_ldo_ops = {
0207     .is_enabled = regulator_is_enabled_regmap,
0208     .enable = regulator_enable_regmap,
0209     .disable = regulator_disable_regmap,
0210     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0211     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0212     .list_voltage = regulator_list_voltage_table,
0213     .map_voltage = regulator_map_voltage_ascend,
0214 };
0215 
0216 static const struct regmap_config tps65023_regmap_config = {
0217     .reg_bits = 8,
0218     .val_bits = 8,
0219 };
0220 
0221 static const struct regulator_desc tps65020_regulators[] = {
0222     TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
0223     TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
0224     TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
0225     TPS65023_REGULATOR_LDO(1, TPS65020_LDO_VSEL_table, 0x07),
0226     TPS65023_REGULATOR_LDO(2, TPS65020_LDO_VSEL_table, 0x70),
0227 };
0228 
0229 static const struct regulator_desc tps65021_regulators[] = {
0230     TPS65023_REGULATOR_DCDC(1, DCDC_FIXED_3300000_VSEL_table, 0x20),
0231     TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_1800000_VSEL_table, 0x10),
0232     TPS65023_REGULATOR_DCDC(3, VCORE_VSEL_table, 0x08),
0233     TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
0234     TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
0235 };
0236 
0237 static const struct regulator_desc tps65023_regulators[] = {
0238     TPS65023_REGULATOR_DCDC(1, VCORE_VSEL_table, 0x20),
0239     TPS65023_REGULATOR_DCDC(2, DCDC_FIXED_3300000_VSEL_table, 0x10),
0240     TPS65023_REGULATOR_DCDC(3, DCDC_FIXED_1800000_VSEL_table, 0x08),
0241     TPS65023_REGULATOR_LDO(1, TPS65023_LDO1_VSEL_table, 0x07),
0242     TPS65023_REGULATOR_LDO(2, TPS65023_LDO2_VSEL_table, 0x70),
0243 };
0244 
0245 static struct tps_driver_data tps65020_drv_data = {
0246     .desc = tps65020_regulators,
0247     .core_regulator = TPS65023_DCDC_3,
0248 };
0249 
0250 static struct tps_driver_data tps65021_drv_data = {
0251     .desc = tps65021_regulators,
0252     .core_regulator = TPS65023_DCDC_3,
0253 };
0254 
0255 static struct tps_driver_data tps65023_drv_data = {
0256     .desc = tps65023_regulators,
0257     .core_regulator = TPS65023_DCDC_1,
0258 };
0259 
0260 static int tps_65023_probe(struct i2c_client *client,
0261                      const struct i2c_device_id *id)
0262 {
0263     struct regulator_init_data *init_data = dev_get_platdata(&client->dev);
0264     struct regulator_config config = { };
0265     struct tps_pmic *tps;
0266     int i;
0267     int error;
0268 
0269     tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
0270     if (!tps)
0271         return -ENOMEM;
0272 
0273     tps->driver_data = (struct tps_driver_data *)id->driver_data;
0274 
0275     tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config);
0276     if (IS_ERR(tps->regmap)) {
0277         error = PTR_ERR(tps->regmap);
0278         dev_err(&client->dev, "Failed to allocate register map: %d\n",
0279             error);
0280         return error;
0281     }
0282 
0283     /* common for all regulators */
0284     config.dev = &client->dev;
0285     config.driver_data = tps;
0286     config.regmap = tps->regmap;
0287 
0288     for (i = 0; i < TPS65023_NUM_REGULATOR; i++) {
0289         if (init_data)
0290             config.init_data = &init_data[i];
0291 
0292         /* Register the regulators */
0293         tps->rdev[i] = devm_regulator_register(&client->dev,
0294                     &tps->driver_data->desc[i], &config);
0295         if (IS_ERR(tps->rdev[i])) {
0296             dev_err(&client->dev, "failed to register %s\n",
0297                 id->name);
0298             return PTR_ERR(tps->rdev[i]);
0299         }
0300     }
0301 
0302     i2c_set_clientdata(client, tps);
0303 
0304     /* Enable setting output voltage by I2C */
0305     regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2,
0306                TPS65023_REG_CTRL2_CORE_ADJ, 0);
0307 
0308     return 0;
0309 }
0310 
0311 static const struct of_device_id __maybe_unused tps65023_of_match[] = {
0312     { .compatible = "ti,tps65020", .data = &tps65020_drv_data},
0313     { .compatible = "ti,tps65021", .data = &tps65021_drv_data},
0314     { .compatible = "ti,tps65023", .data = &tps65023_drv_data},
0315     {},
0316 };
0317 MODULE_DEVICE_TABLE(of, tps65023_of_match);
0318 
0319 static const struct i2c_device_id tps_65023_id[] = {
0320     {
0321         .name = "tps65023",
0322         .driver_data = (kernel_ulong_t)&tps65023_drv_data
0323     }, {
0324         .name = "tps65021",
0325         .driver_data = (kernel_ulong_t)&tps65021_drv_data
0326     }, {
0327         .name = "tps65020",
0328         .driver_data = (kernel_ulong_t)&tps65020_drv_data
0329     },
0330     { },
0331 };
0332 MODULE_DEVICE_TABLE(i2c, tps_65023_id);
0333 
0334 static struct i2c_driver tps_65023_i2c_driver = {
0335     .driver = {
0336         .name = "tps65023",
0337         .of_match_table = of_match_ptr(tps65023_of_match),
0338     },
0339     .probe = tps_65023_probe,
0340     .id_table = tps_65023_id,
0341 };
0342 
0343 static int __init tps_65023_init(void)
0344 {
0345     return i2c_add_driver(&tps_65023_i2c_driver);
0346 }
0347 subsys_initcall(tps_65023_init);
0348 
0349 static void __exit tps_65023_cleanup(void)
0350 {
0351     i2c_del_driver(&tps_65023_i2c_driver);
0352 }
0353 module_exit(tps_65023_cleanup);
0354 
0355 MODULE_AUTHOR("Texas Instruments");
0356 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
0357 MODULE_LICENSE("GPL v2");