tps62360-regulator.c 13 KB

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