tps62360-regulator.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. int old_uV, new_uV;
  165. old_uV = regulator_list_voltage_linear(rdev, old_selector);
  166. if (old_uV < 0)
  167. return old_uV;
  168. new_uV = regulator_list_voltage_linear(rdev, new_selector);
  169. if (new_uV < 0)
  170. return new_uV;
  171. return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
  172. }
  173. static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
  174. {
  175. struct tps62360_chip *tps = rdev_get_drvdata(rdev);
  176. int i;
  177. int val;
  178. int ret;
  179. /* Enable force PWM mode in FAST mode only. */
  180. switch (mode) {
  181. case REGULATOR_MODE_FAST:
  182. val = FORCE_PWM_ENABLE;
  183. break;
  184. case REGULATOR_MODE_NORMAL:
  185. val = 0;
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. if (!tps->valid_gpios) {
  191. ret = regmap_update_bits(tps->regmap,
  192. REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
  193. if (ret < 0)
  194. dev_err(tps->dev,
  195. "%s(): register %d update failed with err %d\n",
  196. __func__, REG_VSET0 + tps->curr_vset_id, ret);
  197. return ret;
  198. }
  199. /* If gpios are valid then all register set need to be control */
  200. for (i = 0; i < 4; ++i) {
  201. ret = regmap_update_bits(tps->regmap,
  202. REG_VSET0 + i, FORCE_PWM_ENABLE, val);
  203. if (ret < 0) {
  204. dev_err(tps->dev,
  205. "%s(): register %d update failed with err %d\n",
  206. __func__, REG_VSET0 + i, ret);
  207. return ret;
  208. }
  209. }
  210. return ret;
  211. }
  212. static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
  213. {
  214. struct tps62360_chip *tps = rdev_get_drvdata(rdev);
  215. unsigned int data;
  216. int ret;
  217. ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
  218. if (ret < 0) {
  219. dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
  220. __func__, REG_VSET0 + tps->curr_vset_id, ret);
  221. return ret;
  222. }
  223. return (data & FORCE_PWM_ENABLE) ?
  224. REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
  225. }
  226. static struct regulator_ops tps62360_dcdc_ops = {
  227. .get_voltage_sel = tps62360_dcdc_get_voltage_sel,
  228. .set_voltage_sel = tps62360_dcdc_set_voltage_sel,
  229. .list_voltage = regulator_list_voltage_linear,
  230. .map_voltage = regulator_map_voltage_linear,
  231. .set_voltage_time_sel = tps62360_set_voltage_time_sel,
  232. .set_mode = tps62360_set_mode,
  233. .get_mode = tps62360_get_mode,
  234. };
  235. static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps,
  236. struct tps62360_regulator_platform_data *pdata)
  237. {
  238. int ret;
  239. unsigned int ramp_ctrl;
  240. /* Initialize internal pull up/down control */
  241. if (tps->en_internal_pulldn)
  242. ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
  243. else
  244. ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
  245. if (ret < 0) {
  246. dev_err(tps->dev,
  247. "%s(): register %d write failed with err %d\n",
  248. __func__, REG_CONTROL, ret);
  249. return ret;
  250. }
  251. /* Reset output discharge path to reduce power consumption */
  252. ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
  253. if (ret < 0) {
  254. dev_err(tps->dev,
  255. "%s(): register %d update failed with err %d\n",
  256. __func__, REG_RAMPCTRL, ret);
  257. return ret;
  258. }
  259. /* Get ramp value from ramp control register */
  260. ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
  261. if (ret < 0) {
  262. dev_err(tps->dev,
  263. "%s(): register %d read failed with err %d\n",
  264. __func__, REG_RAMPCTRL, ret);
  265. return ret;
  266. }
  267. ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
  268. /* ramp mV/us = 32/(2^ramp_ctrl) */
  269. tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
  270. return ret;
  271. }
  272. static const struct regmap_config tps62360_regmap_config = {
  273. .reg_bits = 8,
  274. .val_bits = 8,
  275. .max_register = REG_CHIPID,
  276. .cache_type = REGCACHE_RBTREE,
  277. };
  278. static struct tps62360_regulator_platform_data *
  279. of_get_tps62360_platform_data(struct device *dev)
  280. {
  281. struct tps62360_regulator_platform_data *pdata;
  282. struct device_node *np = dev->of_node;
  283. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  284. if (!pdata) {
  285. dev_err(dev, "Memory alloc failed for platform data\n");
  286. return NULL;
  287. }
  288. pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node);
  289. if (!pdata->reg_init_data) {
  290. dev_err(dev, "Not able to get OF regulator init data\n");
  291. return NULL;
  292. }
  293. pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0);
  294. pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0);
  295. if (of_find_property(np, "ti,vsel0-state-high", NULL))
  296. pdata->vsel0_def_state = 1;
  297. if (of_find_property(np, "ti,vsel1-state-high", NULL))
  298. pdata->vsel1_def_state = 1;
  299. if (of_find_property(np, "ti,enable-pull-down", NULL))
  300. pdata->en_internal_pulldn = true;
  301. if (of_find_property(np, "ti,enable-vout-discharge", NULL))
  302. pdata->en_discharge = true;
  303. return pdata;
  304. }
  305. #if defined(CONFIG_OF)
  306. static const struct of_device_id tps62360_of_match[] = {
  307. { .compatible = "ti,tps62360", .data = (void *)TPS62360},
  308. { .compatible = "ti,tps62361", .data = (void *)TPS62361},
  309. { .compatible = "ti,tps62362", .data = (void *)TPS62362},
  310. { .compatible = "ti,tps62363", .data = (void *)TPS62363},
  311. {},
  312. };
  313. MODULE_DEVICE_TABLE(of, tps62360_of_match);
  314. #endif
  315. static int __devinit tps62360_probe(struct i2c_client *client,
  316. const struct i2c_device_id *id)
  317. {
  318. struct regulator_config config = { };
  319. struct tps62360_regulator_platform_data *pdata;
  320. struct regulator_dev *rdev;
  321. struct tps62360_chip *tps;
  322. int ret;
  323. int i;
  324. int chip_id;
  325. pdata = client->dev.platform_data;
  326. chip_id = id->driver_data;
  327. if (client->dev.of_node) {
  328. const struct of_device_id *match;
  329. match = of_match_device(of_match_ptr(tps62360_of_match),
  330. &client->dev);
  331. if (!match) {
  332. dev_err(&client->dev, "Error: No device match found\n");
  333. return -ENODEV;
  334. }
  335. chip_id = (int)match->data;
  336. if (!pdata)
  337. pdata = of_get_tps62360_platform_data(&client->dev);
  338. }
  339. if (!pdata) {
  340. dev_err(&client->dev, "%s(): Platform data not found\n",
  341. __func__);
  342. return -EIO;
  343. }
  344. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  345. if (!tps) {
  346. dev_err(&client->dev, "%s(): Memory allocation failed\n",
  347. __func__);
  348. return -ENOMEM;
  349. }
  350. tps->en_discharge = pdata->en_discharge;
  351. tps->en_internal_pulldn = pdata->en_internal_pulldn;
  352. tps->vsel0_gpio = pdata->vsel0_gpio;
  353. tps->vsel1_gpio = pdata->vsel1_gpio;
  354. tps->dev = &client->dev;
  355. switch (chip_id) {
  356. case TPS62360:
  357. case TPS62362:
  358. tps->voltage_base = TPS62360_BASE_VOLTAGE;
  359. tps->voltage_reg_mask = 0x3F;
  360. tps->desc.n_voltages = TPS62360_N_VOLTAGES;
  361. break;
  362. case TPS62361:
  363. case TPS62363:
  364. tps->voltage_base = TPS62361_BASE_VOLTAGE;
  365. tps->voltage_reg_mask = 0x7F;
  366. tps->desc.n_voltages = TPS62361_N_VOLTAGES;
  367. break;
  368. default:
  369. return -ENODEV;
  370. }
  371. tps->desc.name = id->name;
  372. tps->desc.id = 0;
  373. tps->desc.ops = &tps62360_dcdc_ops;
  374. tps->desc.type = REGULATOR_VOLTAGE;
  375. tps->desc.owner = THIS_MODULE;
  376. tps->desc.min_uV = tps->voltage_base;
  377. tps->desc.uV_step = 10000;
  378. tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
  379. if (IS_ERR(tps->regmap)) {
  380. ret = PTR_ERR(tps->regmap);
  381. dev_err(&client->dev,
  382. "%s(): regmap allocation failed with err %d\n",
  383. __func__, ret);
  384. return ret;
  385. }
  386. i2c_set_clientdata(client, tps);
  387. tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
  388. (pdata->vsel0_def_state & 1);
  389. tps->lru_index[0] = tps->curr_vset_id;
  390. tps->valid_gpios = false;
  391. if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
  392. int gpio_flags;
  393. gpio_flags = (pdata->vsel0_def_state) ?
  394. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  395. ret = gpio_request_one(tps->vsel0_gpio,
  396. gpio_flags, "tps62360-vsel0");
  397. if (ret) {
  398. dev_err(&client->dev,
  399. "%s(): Could not obtain vsel0 GPIO %d: %d\n",
  400. __func__, tps->vsel0_gpio, ret);
  401. goto err_gpio0;
  402. }
  403. gpio_flags = (pdata->vsel1_def_state) ?
  404. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  405. ret = gpio_request_one(tps->vsel1_gpio,
  406. gpio_flags, "tps62360-vsel1");
  407. if (ret) {
  408. dev_err(&client->dev,
  409. "%s(): Could not obtain vsel1 GPIO %d: %d\n",
  410. __func__, tps->vsel1_gpio, ret);
  411. goto err_gpio1;
  412. }
  413. tps->valid_gpios = true;
  414. /*
  415. * Initialize the lru index with vset_reg id
  416. * The index 0 will be most recently used and
  417. * set with the tps->curr_vset_id */
  418. for (i = 0; i < 4; ++i)
  419. tps->lru_index[i] = i;
  420. tps->lru_index[0] = tps->curr_vset_id;
  421. tps->lru_index[tps->curr_vset_id] = 0;
  422. }
  423. ret = tps62360_init_dcdc(tps, pdata);
  424. if (ret < 0) {
  425. dev_err(tps->dev, "%s(): Init failed with err = %d\n",
  426. __func__, ret);
  427. goto err_init;
  428. }
  429. config.dev = &client->dev;
  430. config.init_data = pdata->reg_init_data;
  431. config.driver_data = tps;
  432. config.of_node = client->dev.of_node;
  433. /* Register the regulators */
  434. rdev = regulator_register(&tps->desc, &config);
  435. if (IS_ERR(rdev)) {
  436. dev_err(tps->dev,
  437. "%s(): regulator register failed with err %s\n",
  438. __func__, id->name);
  439. ret = PTR_ERR(rdev);
  440. goto err_init;
  441. }
  442. tps->rdev = rdev;
  443. return 0;
  444. err_init:
  445. if (gpio_is_valid(tps->vsel1_gpio))
  446. gpio_free(tps->vsel1_gpio);
  447. err_gpio1:
  448. if (gpio_is_valid(tps->vsel0_gpio))
  449. gpio_free(tps->vsel0_gpio);
  450. err_gpio0:
  451. return ret;
  452. }
  453. /**
  454. * tps62360_remove - tps62360 driver i2c remove handler
  455. * @client: i2c driver client device structure
  456. *
  457. * Unregister TPS driver as an i2c client device driver
  458. */
  459. static int __devexit tps62360_remove(struct i2c_client *client)
  460. {
  461. struct tps62360_chip *tps = i2c_get_clientdata(client);
  462. if (gpio_is_valid(tps->vsel1_gpio))
  463. gpio_free(tps->vsel1_gpio);
  464. if (gpio_is_valid(tps->vsel0_gpio))
  465. gpio_free(tps->vsel0_gpio);
  466. regulator_unregister(tps->rdev);
  467. return 0;
  468. }
  469. static void tps62360_shutdown(struct i2c_client *client)
  470. {
  471. struct tps62360_chip *tps = i2c_get_clientdata(client);
  472. int st;
  473. if (!tps->en_discharge)
  474. return;
  475. /* Configure the output discharge path */
  476. st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
  477. if (st < 0)
  478. dev_err(tps->dev,
  479. "%s(): register %d update failed with err %d\n",
  480. __func__, REG_RAMPCTRL, st);
  481. }
  482. static const struct i2c_device_id tps62360_id[] = {
  483. {.name = "tps62360", .driver_data = TPS62360},
  484. {.name = "tps62361", .driver_data = TPS62361},
  485. {.name = "tps62362", .driver_data = TPS62362},
  486. {.name = "tps62363", .driver_data = TPS62363},
  487. {},
  488. };
  489. MODULE_DEVICE_TABLE(i2c, tps62360_id);
  490. static struct i2c_driver tps62360_i2c_driver = {
  491. .driver = {
  492. .name = "tps62360",
  493. .owner = THIS_MODULE,
  494. .of_match_table = of_match_ptr(tps62360_of_match),
  495. },
  496. .probe = tps62360_probe,
  497. .remove = __devexit_p(tps62360_remove),
  498. .shutdown = tps62360_shutdown,
  499. .id_table = tps62360_id,
  500. };
  501. static int __init tps62360_init(void)
  502. {
  503. return i2c_add_driver(&tps62360_i2c_driver);
  504. }
  505. subsys_initcall(tps62360_init);
  506. static void __exit tps62360_cleanup(void)
  507. {
  508. i2c_del_driver(&tps62360_i2c_driver);
  509. }
  510. module_exit(tps62360_cleanup);
  511. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  512. MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
  513. MODULE_LICENSE("GPL v2");