tps62360-regulator.c 15 KB

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