1 // SPDX-License-Identifier: GPL-2.0-only
3 * tps51632-regulator.c -- TI TPS51632
5 * Regulator driver for TPS51632 3-2-1 Phase D-Cap Step Down Driverless
6 * Controller with serial VID control and DVFS.
8 * Copyright (c) 2012, NVIDIA Corporation.
10 * Author: Laxman Dewangan <ldewangan@nvidia.com>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regulator/tps51632-regulator.h>
26 #include <linux/slab.h>
28 /* Register definitions */
29 #define TPS51632_VOLTAGE_SELECT_REG 0x0
30 #define TPS51632_VOLTAGE_BASE_REG 0x1
31 #define TPS51632_OFFSET_REG 0x2
32 #define TPS51632_IMON_REG 0x3
33 #define TPS51632_VMAX_REG 0x4
34 #define TPS51632_DVFS_CONTROL_REG 0x5
35 #define TPS51632_POWER_STATE_REG 0x6
36 #define TPS51632_SLEW_REGS 0x7
37 #define TPS51632_FAULT_REG 0x14
39 #define TPS51632_MAX_REG 0x15
41 #define TPS51632_VOUT_MASK 0x7F
42 #define TPS51632_VOUT_OFFSET_MASK 0x1F
43 #define TPS51632_VMAX_MASK 0x7F
44 #define TPS51632_VMAX_LOCK 0x80
46 /* TPS51632_DVFS_CONTROL_REG */
47 #define TPS51632_DVFS_PWMEN 0x1
48 #define TPS51632_DVFS_STEP_20 0x2
49 #define TPS51632_DVFS_VMAX_PG 0x4
50 #define TPS51632_DVFS_PWMRST 0x8
51 #define TPS51632_DVFS_OCA_EN 0x10
52 #define TPS51632_DVFS_FCCM 0x20
54 /* TPS51632_POWER_STATE_REG */
55 #define TPS51632_POWER_STATE_MASK 0x03
56 #define TPS51632_POWER_STATE_MULTI_PHASE_CCM 0x0
57 #define TPS51632_POWER_STATE_SINGLE_PHASE_CCM 0x1
58 #define TPS51632_POWER_STATE_SINGLE_PHASE_DCM 0x2
60 #define TPS51632_MIN_VOLTAGE 500000
61 #define TPS51632_MAX_VOLTAGE 1520000
62 #define TPS51632_VOLTAGE_STEP_10mV 10000
63 #define TPS51632_VOLTAGE_STEP_20mV 20000
64 #define TPS51632_MAX_VSEL 0x7F
65 #define TPS51632_MIN_VSEL 0x19
66 #define TPS51632_DEFAULT_RAMP_DELAY 6000
67 #define TPS51632_VOLT_VSEL(uV) \
68 (DIV_ROUND_UP(uV - TPS51632_MIN_VOLTAGE, \
69 TPS51632_VOLTAGE_STEP_10mV) + \
72 /* TPS51632 chip information */
73 struct tps51632_chip {
75 struct regulator_desc desc;
76 struct regulator_dev *rdev;
77 struct regmap *regmap;
80 static int tps51632_dcdc_set_ramp_delay(struct regulator_dev *rdev,
83 struct tps51632_chip *tps = rdev_get_drvdata(rdev);
90 bit = DIV_ROUND_UP(ramp_delay, 6000) - 1;
92 ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, BIT(bit));
94 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
98 static const struct regulator_ops tps51632_dcdc_ops = {
99 .get_voltage_sel = regulator_get_voltage_sel_regmap,
100 .set_voltage_sel = regulator_set_voltage_sel_regmap,
101 .list_voltage = regulator_list_voltage_linear,
102 .set_voltage_time_sel = regulator_set_voltage_time_sel,
103 .set_ramp_delay = tps51632_dcdc_set_ramp_delay,
106 static int tps51632_init_dcdc(struct tps51632_chip *tps,
107 struct tps51632_regulator_platform_data *pdata)
113 if (!pdata->enable_pwm_dvfs)
114 goto skip_pwm_config;
116 control |= TPS51632_DVFS_PWMEN;
117 vsel = TPS51632_VOLT_VSEL(pdata->base_voltage_uV);
118 ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
120 dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
124 if (pdata->dvfs_step_20mV)
125 control |= TPS51632_DVFS_STEP_20;
127 if (pdata->max_voltage_uV) {
130 * TPS51632 hw behavior: VMAX register can be write only
131 * once as it get locked after first write. The lock get
132 * reset only when device is power-reset.
133 * Write register only when lock bit is not enabled.
135 ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
137 dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
140 if (!(vmax & TPS51632_VMAX_LOCK)) {
141 vsel = TPS51632_VOLT_VSEL(pdata->max_voltage_uV);
142 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG,
146 "VMAX write failed, err %d\n", ret);
153 ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
155 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
159 static bool is_volatile_reg(struct device *dev, unsigned int reg)
162 case TPS51632_OFFSET_REG:
163 case TPS51632_FAULT_REG:
164 case TPS51632_IMON_REG:
171 static bool is_read_reg(struct device *dev, unsigned int reg)
181 static bool is_write_reg(struct device *dev, unsigned int reg)
184 case TPS51632_VOLTAGE_SELECT_REG:
185 case TPS51632_VOLTAGE_BASE_REG:
186 case TPS51632_VMAX_REG:
187 case TPS51632_DVFS_CONTROL_REG:
188 case TPS51632_POWER_STATE_REG:
189 case TPS51632_SLEW_REGS:
196 static const struct regmap_config tps51632_regmap_config = {
199 .writeable_reg = is_write_reg,
200 .readable_reg = is_read_reg,
201 .volatile_reg = is_volatile_reg,
202 .max_register = TPS51632_MAX_REG - 1,
203 .cache_type = REGCACHE_RBTREE,
206 #if defined(CONFIG_OF)
207 static const struct of_device_id tps51632_of_match[] = {
208 { .compatible = "ti,tps51632",},
211 MODULE_DEVICE_TABLE(of, tps51632_of_match);
213 static struct tps51632_regulator_platform_data *
214 of_get_tps51632_platform_data(struct device *dev,
215 const struct regulator_desc *desc)
217 struct tps51632_regulator_platform_data *pdata;
218 struct device_node *np = dev->of_node;
220 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
224 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
226 if (!pdata->reg_init_data) {
227 dev_err(dev, "Not able to get OF regulator init data\n");
231 pdata->enable_pwm_dvfs =
232 of_property_read_bool(np, "ti,enable-pwm-dvfs");
233 pdata->dvfs_step_20mV = of_property_read_bool(np, "ti,dvfs-step-20mV");
235 pdata->base_voltage_uV = pdata->reg_init_data->constraints.min_uV ? :
236 TPS51632_MIN_VOLTAGE;
237 pdata->max_voltage_uV = pdata->reg_init_data->constraints.max_uV ? :
238 TPS51632_MAX_VOLTAGE;
242 static struct tps51632_regulator_platform_data *
243 of_get_tps51632_platform_data(struct device *dev,
244 const struct regulator_desc *desc)
250 static int tps51632_probe(struct i2c_client *client,
251 const struct i2c_device_id *id)
253 struct tps51632_regulator_platform_data *pdata;
254 struct regulator_dev *rdev;
255 struct tps51632_chip *tps;
257 struct regulator_config config = { };
259 if (client->dev.of_node) {
260 const struct of_device_id *match;
261 match = of_match_device(of_match_ptr(tps51632_of_match),
264 dev_err(&client->dev, "Error: No device match found\n");
269 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
273 tps->dev = &client->dev;
274 tps->desc.name = client->name;
276 tps->desc.ramp_delay = TPS51632_DEFAULT_RAMP_DELAY;
277 tps->desc.min_uV = TPS51632_MIN_VOLTAGE;
278 tps->desc.uV_step = TPS51632_VOLTAGE_STEP_10mV;
279 tps->desc.linear_min_sel = TPS51632_MIN_VSEL;
280 tps->desc.n_voltages = TPS51632_MAX_VSEL + 1;
281 tps->desc.ops = &tps51632_dcdc_ops;
282 tps->desc.type = REGULATOR_VOLTAGE;
283 tps->desc.owner = THIS_MODULE;
285 pdata = dev_get_platdata(&client->dev);
286 if (!pdata && client->dev.of_node)
287 pdata = of_get_tps51632_platform_data(&client->dev, &tps->desc);
289 dev_err(&client->dev, "No Platform data\n");
293 if (pdata->enable_pwm_dvfs) {
294 if ((pdata->base_voltage_uV < TPS51632_MIN_VOLTAGE) ||
295 (pdata->base_voltage_uV > TPS51632_MAX_VOLTAGE)) {
296 dev_err(&client->dev, "Invalid base_voltage_uV setting\n");
300 if ((pdata->max_voltage_uV) &&
301 ((pdata->max_voltage_uV < TPS51632_MIN_VOLTAGE) ||
302 (pdata->max_voltage_uV > TPS51632_MAX_VOLTAGE))) {
303 dev_err(&client->dev, "Invalid max_voltage_uV setting\n");
308 if (pdata->enable_pwm_dvfs)
309 tps->desc.vsel_reg = TPS51632_VOLTAGE_BASE_REG;
311 tps->desc.vsel_reg = TPS51632_VOLTAGE_SELECT_REG;
312 tps->desc.vsel_mask = TPS51632_VOUT_MASK;
314 tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
315 if (IS_ERR(tps->regmap)) {
316 ret = PTR_ERR(tps->regmap);
317 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
320 i2c_set_clientdata(client, tps);
322 ret = tps51632_init_dcdc(tps, pdata);
324 dev_err(tps->dev, "Init failed, err = %d\n", ret);
328 /* Register the regulators */
329 config.dev = &client->dev;
330 config.init_data = pdata->reg_init_data;
331 config.driver_data = tps;
332 config.regmap = tps->regmap;
333 config.of_node = client->dev.of_node;
335 rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
337 dev_err(tps->dev, "regulator register failed\n");
338 return PTR_ERR(rdev);
345 static const struct i2c_device_id tps51632_id[] = {
346 {.name = "tps51632",},
350 MODULE_DEVICE_TABLE(i2c, tps51632_id);
352 static struct i2c_driver tps51632_i2c_driver = {
355 .of_match_table = of_match_ptr(tps51632_of_match),
357 .probe = tps51632_probe,
358 .id_table = tps51632_id,
361 static int __init tps51632_init(void)
363 return i2c_add_driver(&tps51632_i2c_driver);
365 subsys_initcall(tps51632_init);
367 static void __exit tps51632_cleanup(void)
369 i2c_del_driver(&tps51632_i2c_driver);
371 module_exit(tps51632_cleanup);
373 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
374 MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
375 MODULE_LICENSE("GPL v2");