1 /* 2 * tps62360.c -- TI tps62360 3 * 4 * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363. 5 * 6 * Copyright (c) 2012, NVIDIA Corporation. 7 * 8 * Author: Laxman Dewangan <ldewangan@nvidia.com> 9 * 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. 13 * 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. 18 * 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 22 * 02111-1307, USA 23 */ 24 25 #include <linux/kernel.h> 26 #include <linux/module.h> 27 #include <linux/init.h> 28 #include <linux/err.h> 29 #include <linux/of.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> 41 42 /* Register definitions */ 43 #define REG_VSET0 0 44 #define REG_VSET1 1 45 #define REG_VSET2 2 46 #define REG_VSET3 3 47 #define REG_CONTROL 4 48 #define REG_TEMP 5 49 #define REG_RAMPCTRL 6 50 #define REG_CHIPID 8 51 52 #define FORCE_PWM_ENABLE BIT(7) 53 54 enum chips {TPS62360, TPS62361, TPS62362, TPS62363}; 55 56 #define TPS62360_BASE_VOLTAGE 770000 57 #define TPS62360_N_VOLTAGES 64 58 59 #define TPS62361_BASE_VOLTAGE 500000 60 #define TPS62361_N_VOLTAGES 128 61 62 /* tps 62360 chip information */ 63 struct tps62360_chip { 64 struct device *dev; 65 struct regulator_desc desc; 66 struct regulator_dev *rdev; 67 struct regmap *regmap; 68 int chip_id; 69 int vsel0_gpio; 70 int vsel1_gpio; 71 int voltage_base; 72 u8 voltage_reg_mask; 73 bool en_internal_pulldn; 74 bool en_discharge; 75 bool valid_gpios; 76 int lru_index[4]; 77 int curr_vset_vsel[4]; 78 int curr_vset_id; 79 int change_uv_per_us; 80 }; 81 82 /* 83 * find_voltage_set_register: Find new voltage configuration register 84 * (VSET) id. 85 * The finding of the new VSET register will be based on the LRU mechanism. 86 * Each VSET register will have different voltage configured . This 87 * Function will look if any of the VSET register have requested voltage set 88 * or not. 89 * - If it is already there then it will make that register as most 90 * recently used and return as found so that caller need not to set 91 * the VSET register but need to set the proper gpios to select this 92 * VSET register. 93 * - If requested voltage is not found then it will use the least 94 * recently mechanism to get new VSET register for new configuration 95 * and will return not_found so that caller need to set new VSET 96 * register and then gpios (both). 97 */ 98 static bool find_voltage_set_register(struct tps62360_chip *tps, 99 int req_vsel, int *vset_reg_id) 100 { 101 int i; 102 bool found = false; 103 int new_vset_reg = tps->lru_index[3]; 104 int found_index = 3; 105 106 for (i = 0; i < 4; ++i) { 107 if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) { 108 new_vset_reg = tps->lru_index[i]; 109 found_index = i; 110 found = true; 111 goto update_lru_index; 112 } 113 } 114 115 update_lru_index: 116 for (i = found_index; i > 0; i--) 117 tps->lru_index[i] = tps->lru_index[i - 1]; 118 119 tps->lru_index[0] = new_vset_reg; 120 *vset_reg_id = new_vset_reg; 121 return found; 122 } 123 124 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev) 125 { 126 struct tps62360_chip *tps = rdev_get_drvdata(dev); 127 int vsel; 128 unsigned int data; 129 int ret; 130 131 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data); 132 if (ret < 0) { 133 dev_err(tps->dev, "%s(): register %d read failed with err %d\n", 134 __func__, REG_VSET0 + tps->curr_vset_id, ret); 135 return ret; 136 } 137 vsel = (int)data & tps->voltage_reg_mask; 138 return vsel; 139 } 140 141 static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev, 142 unsigned selector) 143 { 144 struct tps62360_chip *tps = rdev_get_drvdata(dev); 145 int ret; 146 bool found = false; 147 int new_vset_id = tps->curr_vset_id; 148 149 /* 150 * If gpios are available to select the VSET register then least 151 * recently used register for new configuration. 152 */ 153 if (tps->valid_gpios) 154 found = find_voltage_set_register(tps, selector, &new_vset_id); 155 156 if (!found) { 157 ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id, 158 tps->voltage_reg_mask, selector); 159 if (ret < 0) { 160 dev_err(tps->dev, 161 "%s(): register %d update failed with err %d\n", 162 __func__, REG_VSET0 + new_vset_id, ret); 163 return ret; 164 } 165 tps->curr_vset_id = new_vset_id; 166 tps->curr_vset_vsel[new_vset_id] = selector; 167 } 168 169 /* Select proper VSET register vio gpios */ 170 if (tps->valid_gpios) { 171 gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1); 172 gpio_set_value_cansleep(tps->vsel1_gpio, 173 (new_vset_id >> 1) & 0x1); 174 } 175 return 0; 176 } 177 178 static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev, 179 unsigned int old_selector, unsigned int new_selector) 180 { 181 struct tps62360_chip *tps = rdev_get_drvdata(rdev); 182 int old_uV, new_uV; 183 184 old_uV = regulator_list_voltage_linear(rdev, old_selector); 185 if (old_uV < 0) 186 return old_uV; 187 188 new_uV = regulator_list_voltage_linear(rdev, new_selector); 189 if (new_uV < 0) 190 return new_uV; 191 192 return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us); 193 } 194 195 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode) 196 { 197 struct tps62360_chip *tps = rdev_get_drvdata(rdev); 198 int i; 199 int val; 200 int ret; 201 202 /* Enable force PWM mode in FAST mode only. */ 203 switch (mode) { 204 case REGULATOR_MODE_FAST: 205 val = FORCE_PWM_ENABLE; 206 break; 207 208 case REGULATOR_MODE_NORMAL: 209 val = 0; 210 break; 211 212 default: 213 return -EINVAL; 214 } 215 216 if (!tps->valid_gpios) { 217 ret = regmap_update_bits(tps->regmap, 218 REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val); 219 if (ret < 0) 220 dev_err(tps->dev, 221 "%s(): register %d update failed with err %d\n", 222 __func__, REG_VSET0 + tps->curr_vset_id, ret); 223 return ret; 224 } 225 226 /* If gpios are valid then all register set need to be control */ 227 for (i = 0; i < 4; ++i) { 228 ret = regmap_update_bits(tps->regmap, 229 REG_VSET0 + i, FORCE_PWM_ENABLE, val); 230 if (ret < 0) { 231 dev_err(tps->dev, 232 "%s(): register %d update failed with err %d\n", 233 __func__, REG_VSET0 + i, ret); 234 return ret; 235 } 236 } 237 return ret; 238 } 239 240 static unsigned int tps62360_get_mode(struct regulator_dev *rdev) 241 { 242 struct tps62360_chip *tps = rdev_get_drvdata(rdev); 243 unsigned int data; 244 int ret; 245 246 ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data); 247 if (ret < 0) { 248 dev_err(tps->dev, "%s(): register %d read failed with err %d\n", 249 __func__, REG_VSET0 + tps->curr_vset_id, ret); 250 return ret; 251 } 252 return (data & FORCE_PWM_ENABLE) ? 253 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL; 254 } 255 256 static struct regulator_ops tps62360_dcdc_ops = { 257 .get_voltage_sel = tps62360_dcdc_get_voltage_sel, 258 .set_voltage_sel = tps62360_dcdc_set_voltage_sel, 259 .list_voltage = regulator_list_voltage_linear, 260 .map_voltage = regulator_map_voltage_linear, 261 .set_voltage_time_sel = tps62360_set_voltage_time_sel, 262 .set_mode = tps62360_set_mode, 263 .get_mode = tps62360_get_mode, 264 }; 265 266 static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps, 267 struct tps62360_regulator_platform_data *pdata) 268 { 269 int ret; 270 unsigned int ramp_ctrl; 271 272 /* Initialize internal pull up/down control */ 273 if (tps->en_internal_pulldn) 274 ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0); 275 else 276 ret = regmap_write(tps->regmap, REG_CONTROL, 0x0); 277 if (ret < 0) { 278 dev_err(tps->dev, 279 "%s(): register %d write failed with err %d\n", 280 __func__, REG_CONTROL, ret); 281 return ret; 282 } 283 284 /* Reset output discharge path to reduce power consumption */ 285 ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0); 286 if (ret < 0) { 287 dev_err(tps->dev, 288 "%s(): register %d update failed with err %d\n", 289 __func__, REG_RAMPCTRL, ret); 290 return ret; 291 } 292 293 /* Get ramp value from ramp control register */ 294 ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl); 295 if (ret < 0) { 296 dev_err(tps->dev, 297 "%s(): register %d read failed with err %d\n", 298 __func__, REG_RAMPCTRL, ret); 299 return ret; 300 } 301 ramp_ctrl = (ramp_ctrl >> 4) & 0x7; 302 303 /* ramp mV/us = 32/(2^ramp_ctrl) */ 304 tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl)); 305 return ret; 306 } 307 308 static const struct regmap_config tps62360_regmap_config = { 309 .reg_bits = 8, 310 .val_bits = 8, 311 .max_register = REG_CHIPID, 312 .cache_type = REGCACHE_RBTREE, 313 }; 314 315 static struct tps62360_regulator_platform_data * 316 of_get_tps62360_platform_data(struct device *dev) 317 { 318 struct tps62360_regulator_platform_data *pdata; 319 struct device_node *np = dev->of_node; 320 321 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 322 if (!pdata) { 323 dev_err(dev, "Memory alloc failed for platform data\n"); 324 return NULL; 325 } 326 327 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node); 328 if (!pdata->reg_init_data) { 329 dev_err(dev, "Not able to get OF regulator init data\n"); 330 return NULL; 331 } 332 333 pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0); 334 pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0); 335 336 if (of_find_property(np, "ti,vsel0-state-high", NULL)) 337 pdata->vsel0_def_state = 1; 338 339 if (of_find_property(np, "ti,vsel1-state-high", NULL)) 340 pdata->vsel1_def_state = 1; 341 342 if (of_find_property(np, "ti,enable-pull-down", NULL)) 343 pdata->en_internal_pulldn = true; 344 345 if (of_find_property(np, "ti,enable-vout-discharge", NULL)) 346 pdata->en_discharge = true; 347 348 return pdata; 349 } 350 351 #if defined(CONFIG_OF) 352 static const struct of_device_id tps62360_of_match[] = { 353 { .compatible = "ti,tps62360", .data = (void *)TPS62360}, 354 { .compatible = "ti,tps62361", .data = (void *)TPS62361}, 355 { .compatible = "ti,tps62362", .data = (void *)TPS62362}, 356 { .compatible = "ti,tps62363", .data = (void *)TPS62363}, 357 {}, 358 }; 359 MODULE_DEVICE_TABLE(of, tps62360_of_match); 360 #endif 361 362 static int __devinit tps62360_probe(struct i2c_client *client, 363 const struct i2c_device_id *id) 364 { 365 struct regulator_config config = { }; 366 struct tps62360_regulator_platform_data *pdata; 367 struct regulator_dev *rdev; 368 struct tps62360_chip *tps; 369 int ret; 370 int i; 371 int chip_id; 372 373 pdata = client->dev.platform_data; 374 chip_id = id->driver_data; 375 376 if (client->dev.of_node) { 377 const struct of_device_id *match; 378 match = of_match_device(of_match_ptr(tps62360_of_match), 379 &client->dev); 380 if (!match) { 381 dev_err(&client->dev, "Error: No device match found\n"); 382 return -ENODEV; 383 } 384 chip_id = (int)match->data; 385 if (!pdata) 386 pdata = of_get_tps62360_platform_data(&client->dev); 387 } 388 389 if (!pdata) { 390 dev_err(&client->dev, "%s(): Platform data not found\n", 391 __func__); 392 return -EIO; 393 } 394 395 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); 396 if (!tps) { 397 dev_err(&client->dev, "%s(): Memory allocation failed\n", 398 __func__); 399 return -ENOMEM; 400 } 401 402 tps->en_discharge = pdata->en_discharge; 403 tps->en_internal_pulldn = pdata->en_internal_pulldn; 404 tps->vsel0_gpio = pdata->vsel0_gpio; 405 tps->vsel1_gpio = pdata->vsel1_gpio; 406 tps->dev = &client->dev; 407 408 switch (chip_id) { 409 case TPS62360: 410 case TPS62362: 411 tps->voltage_base = TPS62360_BASE_VOLTAGE; 412 tps->voltage_reg_mask = 0x3F; 413 tps->desc.n_voltages = TPS62360_N_VOLTAGES; 414 break; 415 case TPS62361: 416 case TPS62363: 417 tps->voltage_base = TPS62361_BASE_VOLTAGE; 418 tps->voltage_reg_mask = 0x7F; 419 tps->desc.n_voltages = TPS62361_N_VOLTAGES; 420 break; 421 default: 422 return -ENODEV; 423 } 424 425 tps->desc.name = id->name; 426 tps->desc.id = 0; 427 tps->desc.ops = &tps62360_dcdc_ops; 428 tps->desc.type = REGULATOR_VOLTAGE; 429 tps->desc.owner = THIS_MODULE; 430 tps->desc.min_uV = tps->voltage_base; 431 tps->desc.uV_step = 10000; 432 433 tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config); 434 if (IS_ERR(tps->regmap)) { 435 ret = PTR_ERR(tps->regmap); 436 dev_err(&client->dev, 437 "%s(): regmap allocation failed with err %d\n", 438 __func__, ret); 439 return ret; 440 } 441 i2c_set_clientdata(client, tps); 442 443 tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 + 444 (pdata->vsel0_def_state & 1); 445 tps->lru_index[0] = tps->curr_vset_id; 446 tps->valid_gpios = false; 447 448 if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) { 449 int gpio_flags; 450 gpio_flags = (pdata->vsel0_def_state) ? 451 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW; 452 ret = gpio_request_one(tps->vsel0_gpio, 453 gpio_flags, "tps62360-vsel0"); 454 if (ret) { 455 dev_err(&client->dev, 456 "%s(): Could not obtain vsel0 GPIO %d: %d\n", 457 __func__, tps->vsel0_gpio, ret); 458 goto err_gpio0; 459 } 460 461 gpio_flags = (pdata->vsel1_def_state) ? 462 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW; 463 ret = gpio_request_one(tps->vsel1_gpio, 464 gpio_flags, "tps62360-vsel1"); 465 if (ret) { 466 dev_err(&client->dev, 467 "%s(): Could not obtain vsel1 GPIO %d: %d\n", 468 __func__, tps->vsel1_gpio, ret); 469 goto err_gpio1; 470 } 471 tps->valid_gpios = true; 472 473 /* 474 * Initialize the lru index with vset_reg id 475 * The index 0 will be most recently used and 476 * set with the tps->curr_vset_id */ 477 for (i = 0; i < 4; ++i) 478 tps->lru_index[i] = i; 479 tps->lru_index[0] = tps->curr_vset_id; 480 tps->lru_index[tps->curr_vset_id] = 0; 481 } 482 483 ret = tps62360_init_dcdc(tps, pdata); 484 if (ret < 0) { 485 dev_err(tps->dev, "%s(): Init failed with err = %d\n", 486 __func__, ret); 487 goto err_init; 488 } 489 490 config.dev = &client->dev; 491 config.init_data = pdata->reg_init_data; 492 config.driver_data = tps; 493 config.of_node = client->dev.of_node; 494 495 /* Register the regulators */ 496 rdev = regulator_register(&tps->desc, &config); 497 if (IS_ERR(rdev)) { 498 dev_err(tps->dev, 499 "%s(): regulator register failed with err %s\n", 500 __func__, id->name); 501 ret = PTR_ERR(rdev); 502 goto err_init; 503 } 504 505 tps->rdev = rdev; 506 return 0; 507 508 err_init: 509 if (gpio_is_valid(tps->vsel1_gpio)) 510 gpio_free(tps->vsel1_gpio); 511 err_gpio1: 512 if (gpio_is_valid(tps->vsel0_gpio)) 513 gpio_free(tps->vsel0_gpio); 514 err_gpio0: 515 return ret; 516 } 517 518 /** 519 * tps62360_remove - tps62360 driver i2c remove handler 520 * @client: i2c driver client device structure 521 * 522 * Unregister TPS driver as an i2c client device driver 523 */ 524 static int __devexit tps62360_remove(struct i2c_client *client) 525 { 526 struct tps62360_chip *tps = i2c_get_clientdata(client); 527 528 if (gpio_is_valid(tps->vsel1_gpio)) 529 gpio_free(tps->vsel1_gpio); 530 531 if (gpio_is_valid(tps->vsel0_gpio)) 532 gpio_free(tps->vsel0_gpio); 533 534 regulator_unregister(tps->rdev); 535 return 0; 536 } 537 538 static void tps62360_shutdown(struct i2c_client *client) 539 { 540 struct tps62360_chip *tps = i2c_get_clientdata(client); 541 int st; 542 543 if (!tps->en_discharge) 544 return; 545 546 /* Configure the output discharge path */ 547 st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2)); 548 if (st < 0) 549 dev_err(tps->dev, 550 "%s(): register %d update failed with err %d\n", 551 __func__, REG_RAMPCTRL, st); 552 } 553 554 static const struct i2c_device_id tps62360_id[] = { 555 {.name = "tps62360", .driver_data = TPS62360}, 556 {.name = "tps62361", .driver_data = TPS62361}, 557 {.name = "tps62362", .driver_data = TPS62362}, 558 {.name = "tps62363", .driver_data = TPS62363}, 559 {}, 560 }; 561 562 MODULE_DEVICE_TABLE(i2c, tps62360_id); 563 564 static struct i2c_driver tps62360_i2c_driver = { 565 .driver = { 566 .name = "tps62360", 567 .owner = THIS_MODULE, 568 .of_match_table = of_match_ptr(tps62360_of_match), 569 }, 570 .probe = tps62360_probe, 571 .remove = __devexit_p(tps62360_remove), 572 .shutdown = tps62360_shutdown, 573 .id_table = tps62360_id, 574 }; 575 576 static int __init tps62360_init(void) 577 { 578 return i2c_add_driver(&tps62360_i2c_driver); 579 } 580 subsys_initcall(tps62360_init); 581 582 static void __exit tps62360_cleanup(void) 583 { 584 i2c_del_driver(&tps62360_i2c_driver); 585 } 586 module_exit(tps62360_cleanup); 587 588 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 589 MODULE_DESCRIPTION("TPS6236x voltage regulator driver"); 590 MODULE_LICENSE("GPL v2"); 591