tps62360-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. .max_register = REG_CHIPID,
  240. .cache_type = REGCACHE_RBTREE,
  241. };
  242. static int __devinit tps62360_probe(struct i2c_client *client,
  243. const struct i2c_device_id *id)
  244. {
  245. struct regulator_config config = { };
  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->dev = &client->dev;
  269. switch (id->driver_data) {
  270. case TPS62360:
  271. case TPS62362:
  272. tps->voltage_base = TPS62360_BASE_VOLTAGE;
  273. tps->voltage_reg_mask = 0x3F;
  274. tps->desc.n_voltages = TPS62360_N_VOLTAGES;
  275. break;
  276. case TPS62361:
  277. case TPS62363:
  278. tps->voltage_base = TPS62361_BASE_VOLTAGE;
  279. tps->voltage_reg_mask = 0x7F;
  280. tps->desc.n_voltages = TPS62361_N_VOLTAGES;
  281. break;
  282. default:
  283. return -ENODEV;
  284. }
  285. tps->desc.name = id->name;
  286. tps->desc.id = 0;
  287. tps->desc.ops = &tps62360_dcdc_ops;
  288. tps->desc.type = REGULATOR_VOLTAGE;
  289. tps->desc.owner = THIS_MODULE;
  290. tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
  291. if (IS_ERR(tps->regmap)) {
  292. ret = PTR_ERR(tps->regmap);
  293. dev_err(&client->dev, "%s() Err: Failed to allocate register"
  294. "map: %d\n", __func__, ret);
  295. return ret;
  296. }
  297. i2c_set_clientdata(client, tps);
  298. tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
  299. (pdata->vsel0_def_state & 1);
  300. tps->lru_index[0] = tps->curr_vset_id;
  301. tps->valid_gpios = false;
  302. if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
  303. ret = gpio_request(tps->vsel0_gpio, "tps62360-vsel0");
  304. if (ret) {
  305. dev_err(&client->dev,
  306. "Err: Could not obtain vsel0 GPIO %d: %d\n",
  307. tps->vsel0_gpio, ret);
  308. goto err_gpio0;
  309. }
  310. ret = gpio_direction_output(tps->vsel0_gpio,
  311. pdata->vsel0_def_state);
  312. if (ret) {
  313. dev_err(&client->dev, "Err: Could not set direction of"
  314. "vsel0 GPIO %d: %d\n", tps->vsel0_gpio, ret);
  315. gpio_free(tps->vsel0_gpio);
  316. goto err_gpio0;
  317. }
  318. ret = gpio_request(tps->vsel1_gpio, "tps62360-vsel1");
  319. if (ret) {
  320. dev_err(&client->dev,
  321. "Err: Could not obtain vsel1 GPIO %d: %d\n",
  322. tps->vsel1_gpio, ret);
  323. goto err_gpio1;
  324. }
  325. ret = gpio_direction_output(tps->vsel1_gpio,
  326. pdata->vsel1_def_state);
  327. if (ret) {
  328. dev_err(&client->dev, "Err: Could not set direction of"
  329. "vsel1 GPIO %d: %d\n", tps->vsel1_gpio, ret);
  330. gpio_free(tps->vsel1_gpio);
  331. goto err_gpio1;
  332. }
  333. tps->valid_gpios = true;
  334. /*
  335. * Initialize the lru index with vset_reg id
  336. * The index 0 will be most recently used and
  337. * set with the tps->curr_vset_id */
  338. for (i = 0; i < 4; ++i)
  339. tps->lru_index[i] = i;
  340. tps->lru_index[0] = tps->curr_vset_id;
  341. tps->lru_index[tps->curr_vset_id] = 0;
  342. }
  343. ret = tps62360_init_dcdc(tps, pdata);
  344. if (ret < 0) {
  345. dev_err(tps->dev, "%s() Err: Init fails with = %d\n",
  346. __func__, ret);
  347. goto err_init;
  348. }
  349. config.dev = &client->dev;
  350. config.init_data = &pdata->reg_init_data;
  351. config.driver_data = tps;
  352. /* Register the regulators */
  353. rdev = regulator_register(&tps->desc, &config);
  354. if (IS_ERR(rdev)) {
  355. dev_err(tps->dev, "%s() Err: Failed to register %s\n",
  356. __func__, id->name);
  357. ret = PTR_ERR(rdev);
  358. goto err_init;
  359. }
  360. tps->rdev = rdev;
  361. return 0;
  362. err_init:
  363. if (gpio_is_valid(tps->vsel1_gpio))
  364. gpio_free(tps->vsel1_gpio);
  365. err_gpio1:
  366. if (gpio_is_valid(tps->vsel0_gpio))
  367. gpio_free(tps->vsel0_gpio);
  368. err_gpio0:
  369. return ret;
  370. }
  371. /**
  372. * tps62360_remove - tps62360 driver i2c remove handler
  373. * @client: i2c driver client device structure
  374. *
  375. * Unregister TPS driver as an i2c client device driver
  376. */
  377. static int __devexit tps62360_remove(struct i2c_client *client)
  378. {
  379. struct tps62360_chip *tps = i2c_get_clientdata(client);
  380. if (gpio_is_valid(tps->vsel1_gpio))
  381. gpio_free(tps->vsel1_gpio);
  382. if (gpio_is_valid(tps->vsel0_gpio))
  383. gpio_free(tps->vsel0_gpio);
  384. regulator_unregister(tps->rdev);
  385. return 0;
  386. }
  387. static void tps62360_shutdown(struct i2c_client *client)
  388. {
  389. struct tps62360_chip *tps = i2c_get_clientdata(client);
  390. int st;
  391. if (!tps->en_discharge)
  392. return;
  393. /* Configure the output discharge path */
  394. st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
  395. if (st < 0)
  396. dev_err(tps->dev, "%s() fails in updating reg %d\n",
  397. __func__, REG_RAMPCTRL);
  398. }
  399. static const struct i2c_device_id tps62360_id[] = {
  400. {.name = "tps62360", .driver_data = TPS62360},
  401. {.name = "tps62361", .driver_data = TPS62361},
  402. {.name = "tps62362", .driver_data = TPS62362},
  403. {.name = "tps62363", .driver_data = TPS62363},
  404. {},
  405. };
  406. MODULE_DEVICE_TABLE(i2c, tps62360_id);
  407. static struct i2c_driver tps62360_i2c_driver = {
  408. .driver = {
  409. .name = "tps62360",
  410. .owner = THIS_MODULE,
  411. },
  412. .probe = tps62360_probe,
  413. .remove = __devexit_p(tps62360_remove),
  414. .shutdown = tps62360_shutdown,
  415. .id_table = tps62360_id,
  416. };
  417. static int __init tps62360_init(void)
  418. {
  419. return i2c_add_driver(&tps62360_i2c_driver);
  420. }
  421. subsys_initcall(tps62360_init);
  422. static void __exit tps62360_cleanup(void)
  423. {
  424. i2c_del_driver(&tps62360_i2c_driver);
  425. }
  426. module_exit(tps62360_cleanup);
  427. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  428. MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
  429. MODULE_LICENSE("GPL v2");