1 // SPDX-License-Identifier: GPL-2.0-only
3 * tps62360.c -- TI tps62360
5 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
7 * Copyright (c) 2012, NVIDIA Corporation.
9 * Author: Laxman Dewangan <ldewangan@nvidia.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
17 #include <linux/of_device.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/tps62360.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
26 #include <linux/regmap.h>
28 /* Register definitions */
35 #define REG_RAMPCTRL 6
38 #define FORCE_PWM_ENABLE BIT(7)
40 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
42 #define TPS62360_BASE_VOLTAGE 770000
43 #define TPS62360_N_VOLTAGES 64
45 #define TPS62361_BASE_VOLTAGE 500000
46 #define TPS62361_N_VOLTAGES 128
48 /* tps 62360 chip information */
49 struct tps62360_chip {
51 struct regulator_desc desc;
52 struct regulator_dev *rdev;
53 struct regmap *regmap;
54 struct gpio_desc *vsel0_gpio;
55 struct gpio_desc *vsel1_gpio;
57 bool en_internal_pulldn;
61 int curr_vset_vsel[4];
66 * find_voltage_set_register: Find new voltage configuration register
68 * The finding of the new VSET register will be based on the LRU mechanism.
69 * Each VSET register will have different voltage configured . This
70 * Function will look if any of the VSET register have requested voltage set
72 * - If it is already there then it will make that register as most
73 * recently used and return as found so that caller need not to set
74 * the VSET register but need to set the proper gpios to select this
76 * - If requested voltage is not found then it will use the least
77 * recently mechanism to get new VSET register for new configuration
78 * and will return not_found so that caller need to set new VSET
79 * register and then gpios (both).
81 static bool find_voltage_set_register(struct tps62360_chip *tps,
82 int req_vsel, int *vset_reg_id)
86 int new_vset_reg = tps->lru_index[3];
89 for (i = 0; i < 4; ++i) {
90 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
91 new_vset_reg = tps->lru_index[i];
94 goto update_lru_index;
99 for (i = found_index; i > 0; i--)
100 tps->lru_index[i] = tps->lru_index[i - 1];
102 tps->lru_index[0] = new_vset_reg;
103 *vset_reg_id = new_vset_reg;
107 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
109 struct tps62360_chip *tps = rdev_get_drvdata(dev);
114 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
116 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
117 __func__, REG_VSET0 + tps->curr_vset_id, ret);
120 vsel = (int)data & tps->voltage_reg_mask;
124 static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
127 struct tps62360_chip *tps = rdev_get_drvdata(dev);
130 int new_vset_id = tps->curr_vset_id;
133 * If gpios are available to select the VSET register then least
134 * recently used register for new configuration.
136 if (tps->valid_gpios)
137 found = find_voltage_set_register(tps, selector, &new_vset_id);
140 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
141 tps->voltage_reg_mask, selector);
144 "%s(): register %d update failed with err %d\n",
145 __func__, REG_VSET0 + new_vset_id, ret);
148 tps->curr_vset_id = new_vset_id;
149 tps->curr_vset_vsel[new_vset_id] = selector;
152 /* Select proper VSET register vio gpios */
153 if (tps->valid_gpios) {
154 gpiod_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
155 gpiod_set_value_cansleep(tps->vsel1_gpio,
156 (new_vset_id >> 1) & 0x1);
161 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
163 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
168 /* Enable force PWM mode in FAST mode only. */
170 case REGULATOR_MODE_FAST:
171 val = FORCE_PWM_ENABLE;
174 case REGULATOR_MODE_NORMAL:
182 if (!tps->valid_gpios) {
183 ret = regmap_update_bits(tps->regmap,
184 REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
187 "%s(): register %d update failed with err %d\n",
188 __func__, REG_VSET0 + tps->curr_vset_id, ret);
192 /* If gpios are valid then all register set need to be control */
193 for (i = 0; i < 4; ++i) {
194 ret = regmap_update_bits(tps->regmap,
195 REG_VSET0 + i, FORCE_PWM_ENABLE, val);
198 "%s(): register %d update failed with err %d\n",
199 __func__, REG_VSET0 + i, ret);
206 static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
208 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
212 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
214 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
215 __func__, REG_VSET0 + tps->curr_vset_id, ret);
218 return (data & FORCE_PWM_ENABLE) ?
219 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
222 static const struct regulator_ops tps62360_dcdc_ops = {
223 .get_voltage_sel = tps62360_dcdc_get_voltage_sel,
224 .set_voltage_sel = tps62360_dcdc_set_voltage_sel,
225 .list_voltage = regulator_list_voltage_linear,
226 .map_voltage = regulator_map_voltage_linear,
227 .set_voltage_time_sel = regulator_set_voltage_time_sel,
228 .set_mode = tps62360_set_mode,
229 .get_mode = tps62360_get_mode,
232 static int tps62360_init_dcdc(struct tps62360_chip *tps,
233 struct tps62360_regulator_platform_data *pdata)
236 unsigned int ramp_ctrl;
238 /* Initialize internal pull up/down control */
239 if (tps->en_internal_pulldn)
240 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
242 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
245 "%s(): register %d write failed with err %d\n",
246 __func__, REG_CONTROL, ret);
250 /* Reset output discharge path to reduce power consumption */
251 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
254 "%s(): register %d update failed with err %d\n",
255 __func__, REG_RAMPCTRL, ret);
259 /* Get ramp value from ramp control register */
260 ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
263 "%s(): register %d read failed with err %d\n",
264 __func__, REG_RAMPCTRL, ret);
267 ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
269 /* ramp mV/us = 32/(2^ramp_ctrl) */
270 tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
274 static const struct regmap_config tps62360_regmap_config = {
277 .max_register = REG_CHIPID,
278 .cache_type = REGCACHE_RBTREE,
281 static struct tps62360_regulator_platform_data *
282 of_get_tps62360_platform_data(struct device *dev,
283 const struct regulator_desc *desc)
285 struct tps62360_regulator_platform_data *pdata;
286 struct device_node *np = dev->of_node;
288 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
292 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
294 if (!pdata->reg_init_data) {
295 dev_err(dev, "Not able to get OF regulator init data\n");
299 if (of_find_property(np, "ti,vsel0-state-high", NULL))
300 pdata->vsel0_def_state = 1;
302 if (of_find_property(np, "ti,vsel1-state-high", NULL))
303 pdata->vsel1_def_state = 1;
305 if (of_find_property(np, "ti,enable-pull-down", NULL))
306 pdata->en_internal_pulldn = true;
308 if (of_find_property(np, "ti,enable-vout-discharge", NULL))
309 pdata->en_discharge = true;
314 #if defined(CONFIG_OF)
315 static const struct of_device_id tps62360_of_match[] = {
316 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
317 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
318 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
319 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
322 MODULE_DEVICE_TABLE(of, tps62360_of_match);
325 static int tps62360_probe(struct i2c_client *client,
326 const struct i2c_device_id *id)
328 struct regulator_config config = { };
329 struct tps62360_regulator_platform_data *pdata;
330 struct regulator_dev *rdev;
331 struct tps62360_chip *tps;
337 pdata = dev_get_platdata(&client->dev);
339 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
343 tps->desc.name = client->name;
345 tps->desc.ops = &tps62360_dcdc_ops;
346 tps->desc.type = REGULATOR_VOLTAGE;
347 tps->desc.owner = THIS_MODULE;
348 tps->desc.uV_step = 10000;
350 if (client->dev.of_node) {
351 const struct of_device_id *match;
352 match = of_match_device(of_match_ptr(tps62360_of_match),
355 dev_err(&client->dev, "Error: No device match found\n");
358 chip_id = (int)(long)match->data;
360 pdata = of_get_tps62360_platform_data(&client->dev,
363 chip_id = id->driver_data;
365 dev_err(&client->dev, "No device tree match or id table match found\n");
370 dev_err(&client->dev, "%s(): Platform data not found\n",
375 tps->en_discharge = pdata->en_discharge;
376 tps->en_internal_pulldn = pdata->en_internal_pulldn;
377 tps->dev = &client->dev;
382 tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
383 tps->voltage_reg_mask = 0x3F;
384 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
388 tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
389 tps->voltage_reg_mask = 0x7F;
390 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
396 tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
397 if (IS_ERR(tps->regmap)) {
398 ret = PTR_ERR(tps->regmap);
399 dev_err(&client->dev,
400 "%s(): regmap allocation failed with err %d\n",
404 i2c_set_clientdata(client, tps);
406 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
407 (pdata->vsel0_def_state & 1);
408 tps->lru_index[0] = tps->curr_vset_id;
409 tps->valid_gpios = false;
411 gpio_flags = (pdata->vsel0_def_state) ?
412 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
413 tps->vsel0_gpio = devm_gpiod_get_optional(&client->dev, "vsel0", gpio_flags);
414 if (IS_ERR(tps->vsel0_gpio)) {
415 dev_err(&client->dev,
416 "%s(): Could not obtain vsel0 GPIO: %ld\n",
417 __func__, PTR_ERR(tps->vsel0_gpio));
418 return PTR_ERR(tps->vsel0_gpio);
421 gpio_flags = (pdata->vsel1_def_state) ?
422 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
423 tps->vsel1_gpio = devm_gpiod_get_optional(&client->dev, "vsel1", gpio_flags);
424 if (IS_ERR(tps->vsel1_gpio)) {
425 dev_err(&client->dev,
426 "%s(): Could not obtain vsel1 GPIO: %ld\n",
427 __func__, PTR_ERR(tps->vsel1_gpio));
428 return PTR_ERR(tps->vsel1_gpio);
431 if (tps->vsel0_gpio != NULL && tps->vsel1_gpio != NULL) {
432 tps->valid_gpios = true;
435 * Initialize the lru index with vset_reg id
436 * The index 0 will be most recently used and
437 * set with the tps->curr_vset_id */
438 for (i = 0; i < 4; ++i)
439 tps->lru_index[i] = i;
440 tps->lru_index[0] = tps->curr_vset_id;
441 tps->lru_index[tps->curr_vset_id] = 0;
444 ret = tps62360_init_dcdc(tps, pdata);
446 dev_err(tps->dev, "%s(): Init failed with err = %d\n",
451 config.dev = &client->dev;
452 config.init_data = pdata->reg_init_data;
453 config.driver_data = tps;
454 config.of_node = client->dev.of_node;
456 /* Register the regulators */
457 rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
460 "%s(): regulator register failed with err %s\n",
462 return PTR_ERR(rdev);
469 static void tps62360_shutdown(struct i2c_client *client)
471 struct tps62360_chip *tps = i2c_get_clientdata(client);
474 if (!tps->en_discharge)
477 /* Configure the output discharge path */
478 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
481 "%s(): register %d update failed with err %d\n",
482 __func__, REG_RAMPCTRL, st);
485 static const struct i2c_device_id tps62360_id[] = {
486 {.name = "tps62360", .driver_data = TPS62360},
487 {.name = "tps62361", .driver_data = TPS62361},
488 {.name = "tps62362", .driver_data = TPS62362},
489 {.name = "tps62363", .driver_data = TPS62363},
493 MODULE_DEVICE_TABLE(i2c, tps62360_id);
495 static struct i2c_driver tps62360_i2c_driver = {
498 .of_match_table = of_match_ptr(tps62360_of_match),
500 .probe = tps62360_probe,
501 .shutdown = tps62360_shutdown,
502 .id_table = tps62360_id,
505 static int __init tps62360_init(void)
507 return i2c_add_driver(&tps62360_i2c_driver);
509 subsys_initcall(tps62360_init);
511 static void __exit tps62360_cleanup(void)
513 i2c_del_driver(&tps62360_i2c_driver);
515 module_exit(tps62360_cleanup);
517 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
518 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
519 MODULE_LICENSE("GPL v2");