2 * tps62360.c -- TI tps62360
4 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
6 * Copyright (c) 2012, NVIDIA Corporation.
8 * Author: Laxman Dewangan <ldewangan@nvidia.com>
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/of_gpio.h>
32 #include <linux/regulator/of_regulator.h>
33 #include <linux/platform_device.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/machine.h>
36 #include <linux/regulator/tps62360.h>
37 #include <linux/gpio.h>
38 #include <linux/i2c.h>
39 #include <linux/slab.h>
40 #include <linux/regmap.h>
42 /* Register definitions */
49 #define REG_RAMPCTRL 6
52 #define FORCE_PWM_ENABLE BIT(7)
54 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
56 #define TPS62360_BASE_VOLTAGE 770000
57 #define TPS62360_N_VOLTAGES 64
59 #define TPS62361_BASE_VOLTAGE 500000
60 #define TPS62361_N_VOLTAGES 128
62 /* tps 62360 chip information */
63 struct tps62360_chip {
65 struct regulator_desc desc;
66 struct regulator_dev *rdev;
67 struct regmap *regmap;
71 bool en_internal_pulldn;
75 int curr_vset_vsel[4];
80 * find_voltage_set_register: Find new voltage configuration register
82 * The finding of the new VSET register will be based on the LRU mechanism.
83 * Each VSET register will have different voltage configured . This
84 * Function will look if any of the VSET register have requested voltage set
86 * - If it is already there then it will make that register as most
87 * recently used and return as found so that caller need not to set
88 * the VSET register but need to set the proper gpios to select this
90 * - If requested voltage is not found then it will use the least
91 * recently mechanism to get new VSET register for new configuration
92 * and will return not_found so that caller need to set new VSET
93 * register and then gpios (both).
95 static bool find_voltage_set_register(struct tps62360_chip *tps,
96 int req_vsel, int *vset_reg_id)
100 int new_vset_reg = tps->lru_index[3];
103 for (i = 0; i < 4; ++i) {
104 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
105 new_vset_reg = tps->lru_index[i];
108 goto update_lru_index;
113 for (i = found_index; i > 0; i--)
114 tps->lru_index[i] = tps->lru_index[i - 1];
116 tps->lru_index[0] = new_vset_reg;
117 *vset_reg_id = new_vset_reg;
121 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
123 struct tps62360_chip *tps = rdev_get_drvdata(dev);
128 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
130 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
131 __func__, REG_VSET0 + tps->curr_vset_id, ret);
134 vsel = (int)data & tps->voltage_reg_mask;
138 static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
141 struct tps62360_chip *tps = rdev_get_drvdata(dev);
144 int new_vset_id = tps->curr_vset_id;
147 * If gpios are available to select the VSET register then least
148 * recently used register for new configuration.
150 if (tps->valid_gpios)
151 found = find_voltage_set_register(tps, selector, &new_vset_id);
154 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
155 tps->voltage_reg_mask, selector);
158 "%s(): register %d update failed with err %d\n",
159 __func__, REG_VSET0 + new_vset_id, ret);
162 tps->curr_vset_id = new_vset_id;
163 tps->curr_vset_vsel[new_vset_id] = selector;
166 /* Select proper VSET register vio gpios */
167 if (tps->valid_gpios) {
168 gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
169 gpio_set_value_cansleep(tps->vsel1_gpio,
170 (new_vset_id >> 1) & 0x1);
175 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
177 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
182 /* Enable force PWM mode in FAST mode only. */
184 case REGULATOR_MODE_FAST:
185 val = FORCE_PWM_ENABLE;
188 case REGULATOR_MODE_NORMAL:
196 if (!tps->valid_gpios) {
197 ret = regmap_update_bits(tps->regmap,
198 REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
201 "%s(): register %d update failed with err %d\n",
202 __func__, REG_VSET0 + tps->curr_vset_id, ret);
206 /* If gpios are valid then all register set need to be control */
207 for (i = 0; i < 4; ++i) {
208 ret = regmap_update_bits(tps->regmap,
209 REG_VSET0 + i, FORCE_PWM_ENABLE, val);
212 "%s(): register %d update failed with err %d\n",
213 __func__, REG_VSET0 + i, ret);
220 static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
222 struct tps62360_chip *tps = rdev_get_drvdata(rdev);
226 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
228 dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
229 __func__, REG_VSET0 + tps->curr_vset_id, ret);
232 return (data & FORCE_PWM_ENABLE) ?
233 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
236 static struct regulator_ops tps62360_dcdc_ops = {
237 .get_voltage_sel = tps62360_dcdc_get_voltage_sel,
238 .set_voltage_sel = tps62360_dcdc_set_voltage_sel,
239 .list_voltage = regulator_list_voltage_linear,
240 .map_voltage = regulator_map_voltage_linear,
241 .set_voltage_time_sel = regulator_set_voltage_time_sel,
242 .set_mode = tps62360_set_mode,
243 .get_mode = tps62360_get_mode,
246 static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps,
247 struct tps62360_regulator_platform_data *pdata)
250 unsigned int ramp_ctrl;
252 /* Initialize internal pull up/down control */
253 if (tps->en_internal_pulldn)
254 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
256 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
259 "%s(): register %d write failed with err %d\n",
260 __func__, REG_CONTROL, ret);
264 /* Reset output discharge path to reduce power consumption */
265 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
268 "%s(): register %d update failed with err %d\n",
269 __func__, REG_RAMPCTRL, ret);
273 /* Get ramp value from ramp control register */
274 ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
277 "%s(): register %d read failed with err %d\n",
278 __func__, REG_RAMPCTRL, ret);
281 ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
283 /* ramp mV/us = 32/(2^ramp_ctrl) */
284 tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
288 static const struct regmap_config tps62360_regmap_config = {
291 .max_register = REG_CHIPID,
292 .cache_type = REGCACHE_RBTREE,
295 static struct tps62360_regulator_platform_data *
296 of_get_tps62360_platform_data(struct device *dev)
298 struct tps62360_regulator_platform_data *pdata;
299 struct device_node *np = dev->of_node;
301 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
303 dev_err(dev, "Memory alloc failed for platform data\n");
307 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node);
308 if (!pdata->reg_init_data) {
309 dev_err(dev, "Not able to get OF regulator init data\n");
313 pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0);
314 pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0);
316 if (of_find_property(np, "ti,vsel0-state-high", NULL))
317 pdata->vsel0_def_state = 1;
319 if (of_find_property(np, "ti,vsel1-state-high", NULL))
320 pdata->vsel1_def_state = 1;
322 if (of_find_property(np, "ti,enable-pull-down", NULL))
323 pdata->en_internal_pulldn = true;
325 if (of_find_property(np, "ti,enable-vout-discharge", NULL))
326 pdata->en_discharge = true;
331 #if defined(CONFIG_OF)
332 static const struct of_device_id tps62360_of_match[] = {
333 { .compatible = "ti,tps62360", .data = (void *)TPS62360},
334 { .compatible = "ti,tps62361", .data = (void *)TPS62361},
335 { .compatible = "ti,tps62362", .data = (void *)TPS62362},
336 { .compatible = "ti,tps62363", .data = (void *)TPS62363},
339 MODULE_DEVICE_TABLE(of, tps62360_of_match);
342 static int __devinit tps62360_probe(struct i2c_client *client,
343 const struct i2c_device_id *id)
345 struct regulator_config config = { };
346 struct tps62360_regulator_platform_data *pdata;
347 struct regulator_dev *rdev;
348 struct tps62360_chip *tps;
353 pdata = client->dev.platform_data;
354 chip_id = id->driver_data;
356 if (client->dev.of_node) {
357 const struct of_device_id *match;
358 match = of_match_device(of_match_ptr(tps62360_of_match),
361 dev_err(&client->dev, "Error: No device match found\n");
364 chip_id = (int)match->data;
366 pdata = of_get_tps62360_platform_data(&client->dev);
370 dev_err(&client->dev, "%s(): Platform data not found\n",
375 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
377 dev_err(&client->dev, "%s(): Memory allocation failed\n",
382 tps->en_discharge = pdata->en_discharge;
383 tps->en_internal_pulldn = pdata->en_internal_pulldn;
384 tps->vsel0_gpio = pdata->vsel0_gpio;
385 tps->vsel1_gpio = pdata->vsel1_gpio;
386 tps->dev = &client->dev;
391 tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
392 tps->voltage_reg_mask = 0x3F;
393 tps->desc.n_voltages = TPS62360_N_VOLTAGES;
397 tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
398 tps->voltage_reg_mask = 0x7F;
399 tps->desc.n_voltages = TPS62361_N_VOLTAGES;
405 tps->desc.name = id->name;
407 tps->desc.ops = &tps62360_dcdc_ops;
408 tps->desc.type = REGULATOR_VOLTAGE;
409 tps->desc.owner = THIS_MODULE;
410 tps->desc.uV_step = 10000;
412 tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
413 if (IS_ERR(tps->regmap)) {
414 ret = PTR_ERR(tps->regmap);
415 dev_err(&client->dev,
416 "%s(): regmap allocation failed with err %d\n",
420 i2c_set_clientdata(client, tps);
422 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
423 (pdata->vsel0_def_state & 1);
424 tps->lru_index[0] = tps->curr_vset_id;
425 tps->valid_gpios = false;
427 if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
429 gpio_flags = (pdata->vsel0_def_state) ?
430 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
431 ret = devm_gpio_request_one(&client->dev, tps->vsel0_gpio,
432 gpio_flags, "tps62360-vsel0");
434 dev_err(&client->dev,
435 "%s(): Could not obtain vsel0 GPIO %d: %d\n",
436 __func__, tps->vsel0_gpio, ret);
440 gpio_flags = (pdata->vsel1_def_state) ?
441 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
442 ret = devm_gpio_request_one(&client->dev, tps->vsel1_gpio,
443 gpio_flags, "tps62360-vsel1");
445 dev_err(&client->dev,
446 "%s(): Could not obtain vsel1 GPIO %d: %d\n",
447 __func__, tps->vsel1_gpio, ret);
450 tps->valid_gpios = true;
453 * Initialize the lru index with vset_reg id
454 * The index 0 will be most recently used and
455 * set with the tps->curr_vset_id */
456 for (i = 0; i < 4; ++i)
457 tps->lru_index[i] = i;
458 tps->lru_index[0] = tps->curr_vset_id;
459 tps->lru_index[tps->curr_vset_id] = 0;
462 ret = tps62360_init_dcdc(tps, pdata);
464 dev_err(tps->dev, "%s(): Init failed with err = %d\n",
469 config.dev = &client->dev;
470 config.init_data = pdata->reg_init_data;
471 config.driver_data = tps;
472 config.of_node = client->dev.of_node;
474 /* Register the regulators */
475 rdev = regulator_register(&tps->desc, &config);
478 "%s(): regulator register failed with err %s\n",
480 return PTR_ERR(rdev);
488 * tps62360_remove - tps62360 driver i2c remove handler
489 * @client: i2c driver client device structure
491 * Unregister TPS driver as an i2c client device driver
493 static int __devexit tps62360_remove(struct i2c_client *client)
495 struct tps62360_chip *tps = i2c_get_clientdata(client);
497 regulator_unregister(tps->rdev);
501 static void tps62360_shutdown(struct i2c_client *client)
503 struct tps62360_chip *tps = i2c_get_clientdata(client);
506 if (!tps->en_discharge)
509 /* Configure the output discharge path */
510 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
513 "%s(): register %d update failed with err %d\n",
514 __func__, REG_RAMPCTRL, st);
517 static const struct i2c_device_id tps62360_id[] = {
518 {.name = "tps62360", .driver_data = TPS62360},
519 {.name = "tps62361", .driver_data = TPS62361},
520 {.name = "tps62362", .driver_data = TPS62362},
521 {.name = "tps62363", .driver_data = TPS62363},
525 MODULE_DEVICE_TABLE(i2c, tps62360_id);
527 static struct i2c_driver tps62360_i2c_driver = {
530 .owner = THIS_MODULE,
531 .of_match_table = of_match_ptr(tps62360_of_match),
533 .probe = tps62360_probe,
534 .remove = __devexit_p(tps62360_remove),
535 .shutdown = tps62360_shutdown,
536 .id_table = tps62360_id,
539 static int __init tps62360_init(void)
541 return i2c_add_driver(&tps62360_i2c_driver);
543 subsys_initcall(tps62360_init);
545 static void __exit tps62360_cleanup(void)
547 i2c_del_driver(&tps62360_i2c_driver);
549 module_exit(tps62360_cleanup);
551 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
552 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
553 MODULE_LICENSE("GPL v2");