0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/err.h>
0014 #include <linux/i2c.h>
0015 #include <linux/init.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 #include <linux/of_device.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/regmap.h>
0022 #include <linux/regulator/driver.h>
0023 #include <linux/regulator/machine.h>
0024 #include <linux/regulator/of_regulator.h>
0025 #include <linux/regulator/tps51632-regulator.h>
0026 #include <linux/slab.h>
0027
0028
0029 #define TPS51632_VOLTAGE_SELECT_REG 0x0
0030 #define TPS51632_VOLTAGE_BASE_REG 0x1
0031 #define TPS51632_OFFSET_REG 0x2
0032 #define TPS51632_IMON_REG 0x3
0033 #define TPS51632_VMAX_REG 0x4
0034 #define TPS51632_DVFS_CONTROL_REG 0x5
0035 #define TPS51632_POWER_STATE_REG 0x6
0036 #define TPS51632_SLEW_REGS 0x7
0037 #define TPS51632_FAULT_REG 0x14
0038
0039 #define TPS51632_MAX_REG 0x15
0040
0041 #define TPS51632_VOUT_MASK 0x7F
0042 #define TPS51632_VOUT_OFFSET_MASK 0x1F
0043 #define TPS51632_VMAX_MASK 0x7F
0044 #define TPS51632_VMAX_LOCK 0x80
0045
0046
0047 #define TPS51632_DVFS_PWMEN 0x1
0048 #define TPS51632_DVFS_STEP_20 0x2
0049 #define TPS51632_DVFS_VMAX_PG 0x4
0050 #define TPS51632_DVFS_PWMRST 0x8
0051 #define TPS51632_DVFS_OCA_EN 0x10
0052 #define TPS51632_DVFS_FCCM 0x20
0053
0054
0055 #define TPS51632_POWER_STATE_MASK 0x03
0056 #define TPS51632_POWER_STATE_MULTI_PHASE_CCM 0x0
0057 #define TPS51632_POWER_STATE_SINGLE_PHASE_CCM 0x1
0058 #define TPS51632_POWER_STATE_SINGLE_PHASE_DCM 0x2
0059
0060 #define TPS51632_MIN_VOLTAGE 500000
0061 #define TPS51632_MAX_VOLTAGE 1520000
0062 #define TPS51632_VOLTAGE_STEP_10mV 10000
0063 #define TPS51632_VOLTAGE_STEP_20mV 20000
0064 #define TPS51632_MAX_VSEL 0x7F
0065 #define TPS51632_MIN_VSEL 0x19
0066 #define TPS51632_DEFAULT_RAMP_DELAY 6000
0067 #define TPS51632_VOLT_VSEL(uV) \
0068 (DIV_ROUND_UP(uV - TPS51632_MIN_VOLTAGE, \
0069 TPS51632_VOLTAGE_STEP_10mV) + \
0070 TPS51632_MIN_VSEL)
0071
0072
0073 struct tps51632_chip {
0074 struct device *dev;
0075 struct regulator_desc desc;
0076 struct regulator_dev *rdev;
0077 struct regmap *regmap;
0078 };
0079
0080 static int tps51632_dcdc_set_ramp_delay(struct regulator_dev *rdev,
0081 int ramp_delay)
0082 {
0083 struct tps51632_chip *tps = rdev_get_drvdata(rdev);
0084 int bit;
0085 int ret;
0086
0087 if (ramp_delay == 0)
0088 bit = 0;
0089 else
0090 bit = DIV_ROUND_UP(ramp_delay, 6000) - 1;
0091
0092 ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, BIT(bit));
0093 if (ret < 0)
0094 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
0095 return ret;
0096 }
0097
0098 static const struct regulator_ops tps51632_dcdc_ops = {
0099 .get_voltage_sel = regulator_get_voltage_sel_regmap,
0100 .set_voltage_sel = regulator_set_voltage_sel_regmap,
0101 .list_voltage = regulator_list_voltage_linear,
0102 .set_voltage_time_sel = regulator_set_voltage_time_sel,
0103 .set_ramp_delay = tps51632_dcdc_set_ramp_delay,
0104 };
0105
0106 static int tps51632_init_dcdc(struct tps51632_chip *tps,
0107 struct tps51632_regulator_platform_data *pdata)
0108 {
0109 int ret;
0110 uint8_t control = 0;
0111 int vsel;
0112
0113 if (!pdata->enable_pwm_dvfs)
0114 goto skip_pwm_config;
0115
0116 control |= TPS51632_DVFS_PWMEN;
0117 vsel = TPS51632_VOLT_VSEL(pdata->base_voltage_uV);
0118 ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
0119 if (ret < 0) {
0120 dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
0121 return ret;
0122 }
0123
0124 if (pdata->dvfs_step_20mV)
0125 control |= TPS51632_DVFS_STEP_20;
0126
0127 if (pdata->max_voltage_uV) {
0128 unsigned int vmax;
0129
0130
0131
0132
0133
0134
0135 ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
0136 if (ret < 0) {
0137 dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
0138 return ret;
0139 }
0140 if (!(vmax & TPS51632_VMAX_LOCK)) {
0141 vsel = TPS51632_VOLT_VSEL(pdata->max_voltage_uV);
0142 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG,
0143 vsel);
0144 if (ret < 0) {
0145 dev_err(tps->dev,
0146 "VMAX write failed, err %d\n", ret);
0147 return ret;
0148 }
0149 }
0150 }
0151
0152 skip_pwm_config:
0153 ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
0154 if (ret < 0)
0155 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
0156 return ret;
0157 }
0158
0159 static bool is_volatile_reg(struct device *dev, unsigned int reg)
0160 {
0161 switch (reg) {
0162 case TPS51632_OFFSET_REG:
0163 case TPS51632_FAULT_REG:
0164 case TPS51632_IMON_REG:
0165 return true;
0166 default:
0167 return false;
0168 }
0169 }
0170
0171 static bool is_read_reg(struct device *dev, unsigned int reg)
0172 {
0173 switch (reg) {
0174 case 0x08 ... 0x0F:
0175 return false;
0176 default:
0177 return true;
0178 }
0179 }
0180
0181 static bool is_write_reg(struct device *dev, unsigned int reg)
0182 {
0183 switch (reg) {
0184 case TPS51632_VOLTAGE_SELECT_REG:
0185 case TPS51632_VOLTAGE_BASE_REG:
0186 case TPS51632_VMAX_REG:
0187 case TPS51632_DVFS_CONTROL_REG:
0188 case TPS51632_POWER_STATE_REG:
0189 case TPS51632_SLEW_REGS:
0190 return true;
0191 default:
0192 return false;
0193 }
0194 }
0195
0196 static const struct regmap_config tps51632_regmap_config = {
0197 .reg_bits = 8,
0198 .val_bits = 8,
0199 .writeable_reg = is_write_reg,
0200 .readable_reg = is_read_reg,
0201 .volatile_reg = is_volatile_reg,
0202 .max_register = TPS51632_MAX_REG - 1,
0203 .cache_type = REGCACHE_RBTREE,
0204 };
0205
0206 #if defined(CONFIG_OF)
0207 static const struct of_device_id tps51632_of_match[] = {
0208 { .compatible = "ti,tps51632",},
0209 {},
0210 };
0211 MODULE_DEVICE_TABLE(of, tps51632_of_match);
0212
0213 static struct tps51632_regulator_platform_data *
0214 of_get_tps51632_platform_data(struct device *dev,
0215 const struct regulator_desc *desc)
0216 {
0217 struct tps51632_regulator_platform_data *pdata;
0218 struct device_node *np = dev->of_node;
0219
0220 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0221 if (!pdata)
0222 return NULL;
0223
0224 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
0225 desc);
0226 if (!pdata->reg_init_data) {
0227 dev_err(dev, "Not able to get OF regulator init data\n");
0228 return NULL;
0229 }
0230
0231 pdata->enable_pwm_dvfs =
0232 of_property_read_bool(np, "ti,enable-pwm-dvfs");
0233 pdata->dvfs_step_20mV = of_property_read_bool(np, "ti,dvfs-step-20mV");
0234
0235 pdata->base_voltage_uV = pdata->reg_init_data->constraints.min_uV ? :
0236 TPS51632_MIN_VOLTAGE;
0237 pdata->max_voltage_uV = pdata->reg_init_data->constraints.max_uV ? :
0238 TPS51632_MAX_VOLTAGE;
0239 return pdata;
0240 }
0241 #else
0242 static struct tps51632_regulator_platform_data *
0243 of_get_tps51632_platform_data(struct device *dev,
0244 const struct regulator_desc *desc)
0245 {
0246 return NULL;
0247 }
0248 #endif
0249
0250 static int tps51632_probe(struct i2c_client *client,
0251 const struct i2c_device_id *id)
0252 {
0253 struct tps51632_regulator_platform_data *pdata;
0254 struct regulator_dev *rdev;
0255 struct tps51632_chip *tps;
0256 int ret;
0257 struct regulator_config config = { };
0258
0259 if (client->dev.of_node) {
0260 const struct of_device_id *match;
0261 match = of_match_device(of_match_ptr(tps51632_of_match),
0262 &client->dev);
0263 if (!match) {
0264 dev_err(&client->dev, "Error: No device match found\n");
0265 return -ENODEV;
0266 }
0267 }
0268
0269 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
0270 if (!tps)
0271 return -ENOMEM;
0272
0273 tps->dev = &client->dev;
0274 tps->desc.name = client->name;
0275 tps->desc.id = 0;
0276 tps->desc.ramp_delay = TPS51632_DEFAULT_RAMP_DELAY;
0277 tps->desc.min_uV = TPS51632_MIN_VOLTAGE;
0278 tps->desc.uV_step = TPS51632_VOLTAGE_STEP_10mV;
0279 tps->desc.linear_min_sel = TPS51632_MIN_VSEL;
0280 tps->desc.n_voltages = TPS51632_MAX_VSEL + 1;
0281 tps->desc.ops = &tps51632_dcdc_ops;
0282 tps->desc.type = REGULATOR_VOLTAGE;
0283 tps->desc.owner = THIS_MODULE;
0284
0285 pdata = dev_get_platdata(&client->dev);
0286 if (!pdata && client->dev.of_node)
0287 pdata = of_get_tps51632_platform_data(&client->dev, &tps->desc);
0288 if (!pdata) {
0289 dev_err(&client->dev, "No Platform data\n");
0290 return -EINVAL;
0291 }
0292
0293 if (pdata->enable_pwm_dvfs) {
0294 if ((pdata->base_voltage_uV < TPS51632_MIN_VOLTAGE) ||
0295 (pdata->base_voltage_uV > TPS51632_MAX_VOLTAGE)) {
0296 dev_err(&client->dev, "Invalid base_voltage_uV setting\n");
0297 return -EINVAL;
0298 }
0299
0300 if ((pdata->max_voltage_uV) &&
0301 ((pdata->max_voltage_uV < TPS51632_MIN_VOLTAGE) ||
0302 (pdata->max_voltage_uV > TPS51632_MAX_VOLTAGE))) {
0303 dev_err(&client->dev, "Invalid max_voltage_uV setting\n");
0304 return -EINVAL;
0305 }
0306 }
0307
0308 if (pdata->enable_pwm_dvfs)
0309 tps->desc.vsel_reg = TPS51632_VOLTAGE_BASE_REG;
0310 else
0311 tps->desc.vsel_reg = TPS51632_VOLTAGE_SELECT_REG;
0312 tps->desc.vsel_mask = TPS51632_VOUT_MASK;
0313
0314 tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
0315 if (IS_ERR(tps->regmap)) {
0316 ret = PTR_ERR(tps->regmap);
0317 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
0318 return ret;
0319 }
0320 i2c_set_clientdata(client, tps);
0321
0322 ret = tps51632_init_dcdc(tps, pdata);
0323 if (ret < 0) {
0324 dev_err(tps->dev, "Init failed, err = %d\n", ret);
0325 return ret;
0326 }
0327
0328
0329 config.dev = &client->dev;
0330 config.init_data = pdata->reg_init_data;
0331 config.driver_data = tps;
0332 config.regmap = tps->regmap;
0333 config.of_node = client->dev.of_node;
0334
0335 rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
0336 if (IS_ERR(rdev)) {
0337 dev_err(tps->dev, "regulator register failed\n");
0338 return PTR_ERR(rdev);
0339 }
0340
0341 tps->rdev = rdev;
0342 return 0;
0343 }
0344
0345 static const struct i2c_device_id tps51632_id[] = {
0346 {.name = "tps51632",},
0347 {},
0348 };
0349
0350 MODULE_DEVICE_TABLE(i2c, tps51632_id);
0351
0352 static struct i2c_driver tps51632_i2c_driver = {
0353 .driver = {
0354 .name = "tps51632",
0355 .of_match_table = of_match_ptr(tps51632_of_match),
0356 },
0357 .probe = tps51632_probe,
0358 .id_table = tps51632_id,
0359 };
0360
0361 static int __init tps51632_init(void)
0362 {
0363 return i2c_add_driver(&tps51632_i2c_driver);
0364 }
0365 subsys_initcall(tps51632_init);
0366
0367 static void __exit tps51632_cleanup(void)
0368 {
0369 i2c_del_driver(&tps51632_i2c_driver);
0370 }
0371 module_exit(tps51632_cleanup);
0372
0373 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
0374 MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
0375 MODULE_LICENSE("GPL v2");