tps62360-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * tps62360.c -- TI tps62360
  3. *
  4. * Driver for processor core supply tps62360 and tps62361B
  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/platform_device.h>
  29. #include <linux/regulator/driver.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/regulator/tps62360.h>
  32. #include <linux/gpio.h>
  33. #include <linux/i2c.h>
  34. #include <linux/delay.h>
  35. #include <linux/slab.h>
  36. #include <linux/regmap.h>
  37. /* Register definitions */
  38. #define REG_VSET0 0
  39. #define REG_VSET1 1
  40. #define REG_VSET2 2
  41. #define REG_VSET3 3
  42. #define REG_CONTROL 4
  43. #define REG_TEMP 5
  44. #define REG_RAMPCTRL 6
  45. #define REG_CHIPID 8
  46. enum chips {TPS62360, TPS62361};
  47. #define TPS62360_BASE_VOLTAGE 770
  48. #define TPS62360_N_VOLTAGES 64
  49. #define TPS62361_BASE_VOLTAGE 500
  50. #define TPS62361_N_VOLTAGES 128
  51. /* tps 62360 chip information */
  52. struct tps62360_chip {
  53. const char *name;
  54. struct device *dev;
  55. struct regulator_desc desc;
  56. struct i2c_client *client;
  57. struct regulator_dev *rdev;
  58. struct regmap *regmap;
  59. int chip_id;
  60. int vsel0_gpio;
  61. int vsel1_gpio;
  62. int voltage_base;
  63. u8 voltage_reg_mask;
  64. bool en_internal_pulldn;
  65. bool en_force_pwm;
  66. bool en_discharge;
  67. bool valid_gpios;
  68. int lru_index[4];
  69. int curr_vset_vsel[4];
  70. int curr_vset_id;
  71. };
  72. /*
  73. * find_voltage_set_register: Find new voltage configuration register
  74. * (VSET) id.
  75. * The finding of the new VSET register will be based on the LRU mechanism.
  76. * Each VSET register will have different voltage configured . This
  77. * Function will look if any of the VSET register have requested voltage set
  78. * or not.
  79. * - If it is already there then it will make that register as most
  80. * recently used and return as found so that caller need not to set
  81. * the VSET register but need to set the proper gpios to select this
  82. * VSET register.
  83. * - If requested voltage is not found then it will use the least
  84. * recently mechanism to get new VSET register for new configuration
  85. * and will return not_found so that caller need to set new VSET
  86. * register and then gpios (both).
  87. */
  88. static bool find_voltage_set_register(struct tps62360_chip *tps,
  89. int req_vsel, int *vset_reg_id)
  90. {
  91. int i;
  92. bool found = false;
  93. int new_vset_reg = tps->lru_index[3];
  94. int found_index = 3;
  95. for (i = 0; i < 4; ++i) {
  96. if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
  97. new_vset_reg = tps->lru_index[i];
  98. found_index = i;
  99. found = true;
  100. goto update_lru_index;
  101. }
  102. }
  103. update_lru_index:
  104. for (i = found_index; i > 0; i--)
  105. tps->lru_index[i] = tps->lru_index[i - 1];
  106. tps->lru_index[0] = new_vset_reg;
  107. *vset_reg_id = new_vset_reg;
  108. return found;
  109. }
  110. static int tps62360_dcdc_get_voltage(struct regulator_dev *dev)
  111. {
  112. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  113. int vsel;
  114. unsigned int data;
  115. int ret;
  116. ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
  117. if (ret < 0) {
  118. dev_err(tps->dev, "%s: Error in reading register %d\n",
  119. __func__, REG_VSET0 + tps->curr_vset_id);
  120. return ret;
  121. }
  122. vsel = (int)data & tps->voltage_reg_mask;
  123. return (tps->voltage_base + vsel * 10) * 1000;
  124. }
  125. static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
  126. int min_uV, int max_uV, unsigned *selector)
  127. {
  128. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  129. int vsel;
  130. int ret;
  131. bool found = false;
  132. int new_vset_id = tps->curr_vset_id;
  133. if (max_uV < min_uV)
  134. return -EINVAL;
  135. if (min_uV >
  136. ((tps->voltage_base + (tps->desc.n_voltages - 1) * 10) * 1000))
  137. return -EINVAL;
  138. if (max_uV < tps->voltage_base * 1000)
  139. return -EINVAL;
  140. vsel = DIV_ROUND_UP(min_uV - (tps->voltage_base * 1000), 10000);
  141. if (selector)
  142. *selector = (vsel & tps->voltage_reg_mask);
  143. /*
  144. * If gpios are available to select the VSET register then least
  145. * recently used register for new configuration.
  146. */
  147. if (tps->valid_gpios)
  148. found = find_voltage_set_register(tps, vsel, &new_vset_id);
  149. if (!found) {
  150. ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
  151. tps->voltage_reg_mask, vsel);
  152. if (ret < 0) {
  153. dev_err(tps->dev, "%s: Error in updating register %d\n",
  154. __func__, REG_VSET0 + new_vset_id);
  155. return ret;
  156. }
  157. tps->curr_vset_id = new_vset_id;
  158. tps->curr_vset_vsel[new_vset_id] = vsel;
  159. }
  160. /* Select proper VSET register vio gpios */
  161. if (tps->valid_gpios) {
  162. gpio_set_value_cansleep(tps->vsel0_gpio,
  163. new_vset_id & 0x1);
  164. gpio_set_value_cansleep(tps->vsel1_gpio,
  165. (new_vset_id >> 1) & 0x1);
  166. }
  167. return 0;
  168. }
  169. static int tps62360_dcdc_list_voltage(struct regulator_dev *dev,
  170. unsigned selector)
  171. {
  172. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  173. if (selector >= tps->desc.n_voltages)
  174. return -EINVAL;
  175. return (tps->voltage_base + selector * 10) * 1000;
  176. }
  177. static struct regulator_ops tps62360_dcdc_ops = {
  178. .get_voltage = tps62360_dcdc_get_voltage,
  179. .set_voltage = tps62360_dcdc_set_voltage,
  180. .list_voltage = tps62360_dcdc_list_voltage,
  181. };
  182. static int tps62360_init_force_pwm(struct tps62360_chip *tps,
  183. struct tps62360_regulator_platform_data *pdata,
  184. int vset_id)
  185. {
  186. unsigned int data;
  187. int ret;
  188. ret = regmap_read(tps->regmap, REG_VSET0 + vset_id, &data);
  189. if (ret < 0) {
  190. dev_err(tps->dev, "%s() fails in writing reg %d\n",
  191. __func__, REG_VSET0 + vset_id);
  192. return ret;
  193. }
  194. tps->curr_vset_vsel[vset_id] = data & tps->voltage_reg_mask;
  195. if (pdata->en_force_pwm)
  196. data |= BIT(7);
  197. else
  198. data &= ~BIT(7);
  199. ret = regmap_write(tps->regmap, REG_VSET0 + vset_id, data);
  200. if (ret < 0)
  201. dev_err(tps->dev, "%s() fails in writing reg %d\n",
  202. __func__, REG_VSET0 + vset_id);
  203. return ret;
  204. }
  205. static int tps62360_init_dcdc(struct tps62360_chip *tps,
  206. struct tps62360_regulator_platform_data *pdata)
  207. {
  208. int ret;
  209. int i;
  210. /* Initailize internal pull up/down control */
  211. if (tps->en_internal_pulldn)
  212. ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
  213. else
  214. ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
  215. if (ret < 0) {
  216. dev_err(tps->dev, "%s() fails in writing reg %d\n",
  217. __func__, REG_CONTROL);
  218. return ret;
  219. }
  220. /* Initailize force PWM mode */
  221. if (tps->valid_gpios) {
  222. for (i = 0; i < 4; ++i) {
  223. ret = tps62360_init_force_pwm(tps, pdata, i);
  224. if (ret < 0)
  225. return ret;
  226. }
  227. } else {
  228. ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
  229. if (ret < 0)
  230. return ret;
  231. }
  232. /* Reset output discharge path to reduce power consumption */
  233. ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
  234. if (ret < 0)
  235. dev_err(tps->dev, "%s() fails in updating reg %d\n",
  236. __func__, REG_RAMPCTRL);
  237. return ret;
  238. }
  239. static const struct regmap_config tps62360_regmap_config = {
  240. .reg_bits = 8,
  241. .val_bits = 8,
  242. };
  243. static int __devinit tps62360_probe(struct i2c_client *client,
  244. const struct i2c_device_id *id)
  245. {
  246. struct tps62360_regulator_platform_data *pdata;
  247. struct regulator_dev *rdev;
  248. struct tps62360_chip *tps;
  249. int ret;
  250. int i;
  251. pdata = client->dev.platform_data;
  252. if (!pdata) {
  253. dev_err(&client->dev, "%s() Err: Platform data not found\n",
  254. __func__);
  255. return -EIO;
  256. }
  257. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  258. if (!tps) {
  259. dev_err(&client->dev, "%s() Err: Memory allocation fails\n",
  260. __func__);
  261. return -ENOMEM;
  262. }
  263. tps->en_force_pwm = pdata->en_force_pwm;
  264. tps->en_discharge = pdata->en_discharge;
  265. tps->en_internal_pulldn = pdata->en_internal_pulldn;
  266. tps->vsel0_gpio = pdata->vsel0_gpio;
  267. tps->vsel1_gpio = pdata->vsel1_gpio;
  268. tps->client = client;
  269. tps->dev = &client->dev;
  270. tps->name = id->name;
  271. tps->voltage_base = (id->driver_data == TPS62360) ?
  272. TPS62360_BASE_VOLTAGE : TPS62361_BASE_VOLTAGE;
  273. tps->voltage_reg_mask = (id->driver_data == TPS62360) ? 0x3F : 0x7F;
  274. tps->desc.name = id->name;
  275. tps->desc.id = 0;
  276. tps->desc.n_voltages = (id->driver_data == TPS62360) ?
  277. TPS62360_N_VOLTAGES : TPS62361_N_VOLTAGES;
  278. tps->desc.ops = &tps62360_dcdc_ops;
  279. tps->desc.type = REGULATOR_VOLTAGE;
  280. tps->desc.owner = THIS_MODULE;
  281. tps->regmap = regmap_init_i2c(client, &tps62360_regmap_config);
  282. if (IS_ERR(tps->regmap)) {
  283. ret = PTR_ERR(tps->regmap);
  284. dev_err(&client->dev, "%s() Err: Failed to allocate register"
  285. "map: %d\n", __func__, ret);
  286. return ret;
  287. }
  288. i2c_set_clientdata(client, tps);
  289. tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
  290. (pdata->vsel0_def_state & 1);
  291. tps->lru_index[0] = tps->curr_vset_id;
  292. tps->valid_gpios = false;
  293. if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
  294. ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
  295. if (ret) {
  296. dev_err(&client->dev,
  297. "Err: Could not obtain vsel0 GPIO %d: %d\n",
  298. tps->vsel0_gpio, ret);
  299. goto err_gpio0;
  300. }
  301. ret = gpio_direction_output(tps->vsel0_gpio,
  302. pdata->vsel0_def_state);
  303. if (ret) {
  304. dev_err(&client->dev, "Err: Could not set direction of"
  305. "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
  306. gpio_free(tps->vsel0_gpio);
  307. goto err_gpio0;
  308. }
  309. ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
  310. if (ret) {
  311. dev_err(&client->dev,
  312. "Err: Could not obtain vsel1 GPIO %d: %d\n",
  313. tps->vsel1_gpio, ret);
  314. goto err_gpio1;
  315. }
  316. ret = gpio_direction_output(tps->vsel1_gpio,
  317. pdata->vsel1_def_state);
  318. if (ret) {
  319. dev_err(&client->dev, "Err: Could not set direction of"
  320. "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
  321. gpio_free(tps->vsel1_gpio);
  322. goto err_gpio1;
  323. }
  324. tps->valid_gpios = true;
  325. /*
  326. * Initialize the lru index with vset_reg id
  327. * The index 0 will be most recently used and
  328. * set with the tps->curr_vset_id */
  329. for (i = 0; i < 4; ++i)
  330. tps->lru_index[i] = i;
  331. tps->lru_index[0] = tps->curr_vset_id;
  332. tps->lru_index[tps->curr_vset_id] = 0;
  333. }
  334. ret = tps62360_init_dcdc(tps, pdata);
  335. if (ret < 0) {
  336. dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
  337. __func__, ret);
  338. goto err_init;
  339. }
  340. /* Register the regulators */
  341. rdev = regulator_register(&tps->desc, &client->dev,
  342. &pdata->reg_init_data, tps, NULL);
  343. if (IS_ERR(rdev)) {
  344. dev_err(tps->dev, "%s() Err: Failed to register %s\n",
  345. __func__, id->name);
  346. ret = PTR_ERR(rdev);
  347. goto err_init;
  348. }
  349. tps->rdev = rdev;
  350. return 0;
  351. err_init:
  352. if (gpio_is_valid(tps->vsel1_gpio))
  353. gpio_free(tps->vsel1_gpio);
  354. err_gpio1:
  355. if (gpio_is_valid(tps->vsel0_gpio))
  356. gpio_free(tps->vsel0_gpio);
  357. err_gpio0:
  358. regmap_exit(tps->regmap);
  359. return ret;
  360. }
  361. /**
  362. * tps62360_remove - tps62360 driver i2c remove handler
  363. * @client: i2c driver client device structure
  364. *
  365. * Unregister TPS driver as an i2c client device driver
  366. */
  367. static int __devexit tps62360_remove(struct i2c_client *client)
  368. {
  369. struct tps62360_chip *tps = i2c_get_clientdata(client);
  370. if (gpio_is_valid(tps->vsel1_gpio))
  371. gpio_free(tps->vsel1_gpio);
  372. if (gpio_is_valid(tps->vsel0_gpio))
  373. gpio_free(tps->vsel0_gpio);
  374. regulator_unregister(tps->rdev);
  375. regmap_exit(tps->regmap);
  376. return 0;
  377. }
  378. static void tps62360_shutdown(struct i2c_client *client)
  379. {
  380. struct tps62360_chip *tps = i2c_get_clientdata(client);
  381. int st;
  382. if (!tps->en_discharge)
  383. return;
  384. /* Configure the output discharge path */
  385. st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
  386. if (st < 0)
  387. dev_err(tps->dev, "%s() fails in updating reg %d\n",
  388. __func__, REG_RAMPCTRL);
  389. }
  390. static const struct i2c_device_id tps62360_id[] = {
  391. {.name = "tps62360", .driver_data = TPS62360},
  392. {.name = "tps62361", .driver_data = TPS62361},
  393. {},
  394. };
  395. MODULE_DEVICE_TABLE(i2c, tps62360_id);
  396. static struct i2c_driver tps62360_i2c_driver = {
  397. .driver = {
  398. .name = "tps62360",
  399. .owner = THIS_MODULE,
  400. },
  401. .probe = tps62360_probe,
  402. .remove = __devexit_p(tps62360_remove),
  403. .shutdown = tps62360_shutdown,
  404. .id_table = tps62360_id,
  405. };
  406. static int __init tps62360_init(void)
  407. {
  408. return i2c_add_driver(&tps62360_i2c_driver);
  409. }
  410. subsys_initcall(tps62360_init);
  411. static void __exit tps62360_cleanup(void)
  412. {
  413. i2c_del_driver(&tps62360_i2c_driver);
  414. }
  415. module_exit(tps62360_cleanup);
  416. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  417. MODULE_DESCRIPTION("TPS62360 voltage regulator driver");
  418. MODULE_LICENSE("GPL v2");