2 * tps62360.c -- TI tps62360
4 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
6 * Copyright (c) 2012, NVIDIA Corporation.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation version 2.
14 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
15 * whether express or implied; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
30 #include <linux/of_device.h>
31 #include <linux/regulator/of_regulator.h>
32 #include <linux/platform_device.h>
33 #include <linux/regulator/driver.h>
34 #include <linux/regulator/machine.h>
35 #include <linux/regulator/tps62360.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <linux/regmap.h>
41 /* Register definitions */
48 #define REG_RAMPCTRL 6
51 #define FORCE_PWM_ENABLE BIT(7)
53 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
55 #define TPS62360_BASE_VOLTAGE 770000
56 #define TPS62360_N_VOLTAGES 64
58 #define TPS62361_BASE_VOLTAGE 500000
59 #define TPS62361_N_VOLTAGES 128
61 /* tps 62360 chip information */
62 struct tps62360_chip {
64 struct regulator_desc desc;
65 struct regulator_dev *rdev;
66 struct regmap *regmap;
67 struct gpio_desc *vsel0_gpio;
68 struct gpio_desc *vsel1_gpio;
70 bool en_internal_pulldn;
74 int curr_vset_vsel[4];
79 * find_voltage_set_register: Find new voltage configuration register
81 * The finding of the new VSET register will be based on the LRU mechanism.
82 * Each VSET register will have different voltage configured . This
83 * Function will look if any of the VSET register have requested voltage set
85 * - If it is already there then it will make that register as most
86 * recently used and return as found so that caller need not to set
87 * the VSET register but need to set the proper gpios to select this
89 * - If requested voltage is not found then it will use the least
90 * recently mechanism to get new VSET register for new configuration
91 * and will return not_found so that caller need to set new VSET
92 * register and then gpios (both).
94 static bool find_voltage_set_register(struct tps62360_chip *tps,
95 int req_vsel, int *vset_reg_id)
99 int new_vset_reg = tps->lru_index[3];
102 for (i = 0; i < 4; ++i) {
103 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
104 new_vset_reg = tps->lru_index[i];
107 goto update_lru_index;
112 for (i = found_index; i > 0; i--)
113 tps->lru_index[i] = tps->lru_index[i - 1];
115 tps->lru_index[0] = new_vset_reg;
116 *vset_reg_id = new_vset_reg;
120 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
122 struct tps62360_chip *tps = rdev_get_drvdata(dev);
127 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
129 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
130 __func__, REG_VSET0 + tps->curr_vset_id, ret);
133 vsel = (int)data & tps->voltage_reg_mask;
137 static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
140 struct tps62360_chip *tps = rdev_get_drvdata(dev);
143 int new_vset_id = tps->curr_vset_id;
146 * If gpios are available to select the VSET register then least
147 * recently used register for new configuration.
149 if (tps->valid_gpios)
150 found = find_voltage_set_register(tps, selector, &new_vset_id);
153 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
154 tps->voltage_reg_mask, selector);
157 "%s(): register %d update failed with err %d\n",
158 __func__, REG_VSET0 + new_vset_id, ret);
161 tps->curr_vset_id = new_vset_id;
162 tps->curr_vset_vsel[new_vset_id] = selector;
165 /* Select proper VSET register vio gpios */
166 if (tps->valid_gpios) {
167 gpiod_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
168 gpiod_set_value_cansleep(tps->vsel1_gpio,
169 (new_vset_id >> 1) & 0x1);
174 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
176 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
181 /* Enable force PWM mode in FAST mode only. */
183 case REGULATOR_MODE_FAST:
184 val = FORCE_PWM_ENABLE;
187 case REGULATOR_MODE_NORMAL:
195 if (!tps->valid_gpios) {
196 ret = regmap_update_bits(tps->regmap,
197 REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
200 "%s(): register %d update failed with err %d\n",
201 __func__, REG_VSET0 + tps->curr_vset_id, ret);
205 /* If gpios are valid then all register set need to be control */
206 for (i = 0; i < 4; ++i) {
207 ret = regmap_update_bits(tps->regmap,
208 REG_VSET0 + i, FORCE_PWM_ENABLE, val);
211 "%s(): register %d update failed with err %d\n",
212 __func__, REG_VSET0 + i, ret);
219 static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
221 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
225 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
227 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
228 __func__, REG_VSET0 + tps->curr_vset_id, ret);
231 return (data & FORCE_PWM_ENABLE) ?
232 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
235 static const struct regulator_ops tps62360_dcdc_ops = {
236 .get_voltage_sel = tps62360_dcdc_get_voltage_sel,
237 .set_voltage_sel = tps62360_dcdc_set_voltage_sel,
238 .list_voltage = regulator_list_voltage_linear,
239 .map_voltage = regulator_map_voltage_linear,
240 .set_voltage_time_sel = regulator_set_voltage_time_sel,
241 .set_mode = tps62360_set_mode,
242 .get_mode = tps62360_get_mode,
245 static int tps62360_init_dcdc(struct tps62360_chip *tps,
246 struct tps62360_regulator_platform_data *pdata)
249 unsigned int ramp_ctrl;
251 /* Initialize internal pull up/down control */
252 if (tps->en_internal_pulldn)
253 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
255 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
258 "%s(): register %d write failed with err %d\n",
259 __func__, REG_CONTROL, ret);
263 /* Reset output discharge path to reduce power consumption */
264 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
267 "%s(): register %d update failed with err %d\n",
268 __func__, REG_RAMPCTRL, ret);
272 /* Get ramp value from ramp control register */
273 ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
276 "%s(): register %d read failed with err %d\n",
277 __func__, REG_RAMPCTRL, ret);
280 ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
282 /* ramp mV/us = 32/(2^ramp_ctrl) */
283 tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
287 static const struct regmap_config tps62360_regmap_config = {
290 .max_register = REG_CHIPID,
291 .cache_type = REGCACHE_RBTREE,
294 static struct tps62360_regulator_platform_data *
295 of_get_tps62360_platform_data(struct device *dev,
296 const struct regulator_desc *desc)
298 struct tps62360_regulator_platform_data *pdata;
299 struct device_node *np = dev->of_node;
301 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
305 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
307 if (!pdata->reg_init_data) {
308 dev_err(dev, "Not able to get OF regulator init data\n");
312 if (of_find_property(np, "ti,vsel0-state-high", NULL))
313 pdata->vsel0_def_state = 1;
315 if (of_find_property(np, "ti,vsel1-state-high", NULL))
316 pdata->vsel1_def_state = 1;
318 if (of_find_property(np, "ti,enable-pull-down", NULL))
319 pdata->en_internal_pulldn = true;
321 if (of_find_property(np, "ti,enable-vout-discharge", NULL))
322 pdata->en_discharge = true;
327 #if defined(CONFIG_OF)
328 static const struct of_device_id tps62360_of_match[] = {
329 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
330 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
331 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
332 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
335 MODULE_DEVICE_TABLE(of, tps62360_of_match);
338 static int tps62360_probe(struct i2c_client *client,
339 const struct i2c_device_id *id)
341 struct regulator_config config = { };
342 struct tps62360_regulator_platform_data *pdata;
343 struct regulator_dev *rdev;
344 struct tps62360_chip *tps;
350 pdata = dev_get_platdata(&client->dev);
352 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
356 tps->desc.name = client->name;
358 tps->desc.ops = &tps62360_dcdc_ops;
359 tps->desc.type = REGULATOR_VOLTAGE;
360 tps->desc.owner = THIS_MODULE;
361 tps->desc.uV_step = 10000;
363 if (client->dev.of_node) {
364 const struct of_device_id *match;
365 match = of_match_device(of_match_ptr(tps62360_of_match),
368 dev_err(&client->dev, "Error: No device match found\n");
371 chip_id = (int)(long)match->data;
373 pdata = of_get_tps62360_platform_data(&client->dev,
376 chip_id = id->driver_data;
378 dev_err(&client->dev, "No device tree match or id table match found\n");
383 dev_err(&client->dev, "%s(): Platform data not found\n",
388 tps->en_discharge = pdata->en_discharge;
389 tps->en_internal_pulldn = pdata->en_internal_pulldn;
390 tps->dev = &client->dev;
395 tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
396 tps->voltage_reg_mask = 0x3F;
397 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
401 tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
402 tps->voltage_reg_mask = 0x7F;
403 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
409 tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
410 if (IS_ERR(tps->regmap)) {
411 ret = PTR_ERR(tps->regmap);
412 dev_err(&client->dev,
413 "%s(): regmap allocation failed with err %d\n",
417 i2c_set_clientdata(client, tps);
419 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
420 (pdata->vsel0_def_state & 1);
421 tps->lru_index[0] = tps->curr_vset_id;
422 tps->valid_gpios = false;
424 gpio_flags = (pdata->vsel0_def_state) ?
425 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
426 tps->vsel0_gpio = devm_gpiod_get_optional(&client->dev, "vsel0", gpio_flags);
427 if (IS_ERR(tps->vsel0_gpio)) {
428 dev_err(&client->dev,
429 "%s(): Could not obtain vsel0 GPIO: %ld\n",
430 __func__, PTR_ERR(tps->vsel0_gpio));
431 return PTR_ERR(tps->vsel0_gpio);
434 gpio_flags = (pdata->vsel1_def_state) ?
435 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
436 tps->vsel1_gpio = devm_gpiod_get_optional(&client->dev, "vsel1", gpio_flags);
437 if (IS_ERR(tps->vsel1_gpio)) {
438 dev_err(&client->dev,
439 "%s(): Could not obtain vsel1 GPIO: %ld\n",
440 __func__, PTR_ERR(tps->vsel1_gpio));
441 return PTR_ERR(tps->vsel1_gpio);
444 if (tps->vsel0_gpio != NULL && tps->vsel1_gpio != NULL) {
445 tps->valid_gpios = true;
448 * Initialize the lru index with vset_reg id
449 * The index 0 will be most recently used and
450 * set with the tps->curr_vset_id */
451 for (i = 0; i < 4; ++i)
452 tps->lru_index[i] = i;
453 tps->lru_index[0] = tps->curr_vset_id;
454 tps->lru_index[tps->curr_vset_id] = 0;
457 ret = tps62360_init_dcdc(tps, pdata);
459 dev_err(tps->dev, "%s(): Init failed with err = %d\n",
464 config.dev = &client->dev;
465 config.init_data = pdata->reg_init_data;
466 config.driver_data = tps;
467 config.of_node = client->dev.of_node;
469 /* Register the regulators */
470 rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
473 "%s(): regulator register failed with err %s\n",
475 return PTR_ERR(rdev);
482 static void tps62360_shutdown(struct i2c_client *client)
484 struct tps62360_chip *tps = i2c_get_clientdata(client);
487 if (!tps->en_discharge)
490 /* Configure the output discharge path */
491 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
494 "%s(): register %d update failed with err %d\n",
495 __func__, REG_RAMPCTRL, st);
498 static const struct i2c_device_id tps62360_id[] = {
499 {.name = "tps62360", .driver_data = TPS62360},
500 {.name = "tps62361", .driver_data = TPS62361},
501 {.name = "tps62362", .driver_data = TPS62362},
502 {.name = "tps62363", .driver_data = TPS62363},
506 MODULE_DEVICE_TABLE(i2c, tps62360_id);
508 static struct i2c_driver tps62360_i2c_driver = {
511 .of_match_table = of_match_ptr(tps62360_of_match),
513 .probe = tps62360_probe,
514 .shutdown = tps62360_shutdown,
515 .id_table = tps62360_id,
518 static int __init tps62360_init(void)
520 return i2c_add_driver(&tps62360_i2c_driver);
522 subsys_initcall(tps62360_init);
524 static void __exit tps62360_cleanup(void)
526 i2c_del_driver(&tps62360_i2c_driver);
528 module_exit(tps62360_cleanup);
531 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
532 MODULE_LICENSE("GPL v2");