tps65090-charger.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Battery charger driver for TI's tps65090
  3. *
  4. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/freezer.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/kthread.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/power_supply.h>
  26. #include <linux/slab.h>
  27. #include <linux/mfd/tps65090.h>
  28. #define TPS65090_CHARGER_ENABLE BIT(0)
  29. #define TPS65090_VACG BIT(1)
  30. #define TPS65090_NOITERM BIT(5)
  31. #define POLL_INTERVAL (HZ * 2) /* Used when no irq */
  32. struct tps65090_charger {
  33. struct device *dev;
  34. int ac_online;
  35. int prev_ac_online;
  36. int irq;
  37. struct task_struct *poll_task;
  38. bool passive_mode;
  39. struct power_supply *ac;
  40. struct tps65090_platform_data *pdata;
  41. };
  42. static enum power_supply_property tps65090_ac_props[] = {
  43. POWER_SUPPLY_PROP_ONLINE,
  44. };
  45. static int tps65090_low_chrg_current(struct tps65090_charger *charger)
  46. {
  47. int ret;
  48. if (charger->passive_mode)
  49. return 0;
  50. ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL5,
  51. TPS65090_NOITERM);
  52. if (ret < 0) {
  53. dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
  54. __func__, TPS65090_REG_CG_CTRL5);
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. static int tps65090_enable_charging(struct tps65090_charger *charger)
  60. {
  61. int ret;
  62. uint8_t ctrl0 = 0;
  63. if (charger->passive_mode)
  64. return 0;
  65. ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_CTRL0,
  66. &ctrl0);
  67. if (ret < 0) {
  68. dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
  69. __func__, TPS65090_REG_CG_CTRL0);
  70. return ret;
  71. }
  72. ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL0,
  73. (ctrl0 | TPS65090_CHARGER_ENABLE));
  74. if (ret < 0) {
  75. dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
  76. __func__, TPS65090_REG_CG_CTRL0);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int tps65090_config_charger(struct tps65090_charger *charger)
  82. {
  83. uint8_t intrmask = 0;
  84. int ret;
  85. if (charger->passive_mode)
  86. return 0;
  87. if (charger->pdata->enable_low_current_chrg) {
  88. ret = tps65090_low_chrg_current(charger);
  89. if (ret < 0) {
  90. dev_err(charger->dev,
  91. "error configuring low charge current\n");
  92. return ret;
  93. }
  94. }
  95. /* Enable the VACG interrupt for AC power detect */
  96. ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_MASK,
  97. &intrmask);
  98. if (ret < 0) {
  99. dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
  100. __func__, TPS65090_REG_INTR_MASK);
  101. return ret;
  102. }
  103. ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_MASK,
  104. (intrmask | TPS65090_VACG));
  105. if (ret < 0) {
  106. dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
  107. __func__, TPS65090_REG_CG_CTRL0);
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. static int tps65090_ac_get_property(struct power_supply *psy,
  113. enum power_supply_property psp,
  114. union power_supply_propval *val)
  115. {
  116. struct tps65090_charger *charger = power_supply_get_drvdata(psy);
  117. if (psp == POWER_SUPPLY_PROP_ONLINE) {
  118. val->intval = charger->ac_online;
  119. charger->prev_ac_online = charger->ac_online;
  120. return 0;
  121. }
  122. return -EINVAL;
  123. }
  124. static irqreturn_t tps65090_charger_isr(int irq, void *dev_id)
  125. {
  126. struct tps65090_charger *charger = dev_id;
  127. int ret;
  128. uint8_t status1 = 0;
  129. uint8_t intrsts = 0;
  130. ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_STATUS1,
  131. &status1);
  132. if (ret < 0) {
  133. dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
  134. __func__, TPS65090_REG_CG_STATUS1);
  135. return IRQ_HANDLED;
  136. }
  137. msleep(75);
  138. ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_STS,
  139. &intrsts);
  140. if (ret < 0) {
  141. dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
  142. __func__, TPS65090_REG_INTR_STS);
  143. return IRQ_HANDLED;
  144. }
  145. if (intrsts & TPS65090_VACG) {
  146. ret = tps65090_enable_charging(charger);
  147. if (ret < 0)
  148. return IRQ_HANDLED;
  149. charger->ac_online = 1;
  150. } else {
  151. charger->ac_online = 0;
  152. }
  153. /* Clear interrupts. */
  154. if (!charger->passive_mode) {
  155. ret = tps65090_write(charger->dev->parent,
  156. TPS65090_REG_INTR_STS, 0x00);
  157. if (ret < 0) {
  158. dev_err(charger->dev,
  159. "%s(): Error in writing reg 0x%x\n",
  160. __func__, TPS65090_REG_INTR_STS);
  161. }
  162. }
  163. if (charger->prev_ac_online != charger->ac_online)
  164. power_supply_changed(charger->ac);
  165. return IRQ_HANDLED;
  166. }
  167. static struct tps65090_platform_data *
  168. tps65090_parse_dt_charger_data(struct platform_device *pdev)
  169. {
  170. struct tps65090_platform_data *pdata;
  171. struct device_node *np = pdev->dev.of_node;
  172. unsigned int prop;
  173. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  174. if (!pdata) {
  175. dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
  176. return NULL;
  177. }
  178. prop = of_property_read_bool(np, "ti,enable-low-current-chrg");
  179. pdata->enable_low_current_chrg = prop;
  180. pdata->irq_base = -1;
  181. return pdata;
  182. }
  183. static int tps65090_charger_poll_task(void *data)
  184. {
  185. set_freezable();
  186. while (!kthread_should_stop()) {
  187. schedule_timeout_interruptible(POLL_INTERVAL);
  188. try_to_freeze();
  189. tps65090_charger_isr(-1, data);
  190. }
  191. return 0;
  192. }
  193. static const struct power_supply_desc tps65090_charger_desc = {
  194. .name = "tps65090-ac",
  195. .type = POWER_SUPPLY_TYPE_MAINS,
  196. .get_property = tps65090_ac_get_property,
  197. .properties = tps65090_ac_props,
  198. .num_properties = ARRAY_SIZE(tps65090_ac_props),
  199. };
  200. static int tps65090_charger_probe(struct platform_device *pdev)
  201. {
  202. struct tps65090_charger *cdata;
  203. struct tps65090_platform_data *pdata;
  204. struct power_supply_config psy_cfg = {};
  205. uint8_t status1 = 0;
  206. int ret;
  207. int irq;
  208. pdata = dev_get_platdata(pdev->dev.parent);
  209. if (IS_ENABLED(CONFIG_OF) && !pdata && pdev->dev.of_node)
  210. pdata = tps65090_parse_dt_charger_data(pdev);
  211. if (!pdata) {
  212. dev_err(&pdev->dev, "%s():no platform data available\n",
  213. __func__);
  214. return -ENODEV;
  215. }
  216. cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL);
  217. if (!cdata) {
  218. dev_err(&pdev->dev, "failed to allocate memory status\n");
  219. return -ENOMEM;
  220. }
  221. platform_set_drvdata(pdev, cdata);
  222. cdata->dev = &pdev->dev;
  223. cdata->pdata = pdata;
  224. psy_cfg.supplied_to = pdata->supplied_to;
  225. psy_cfg.num_supplicants = pdata->num_supplicants;
  226. psy_cfg.of_node = pdev->dev.of_node;
  227. psy_cfg.drv_data = cdata;
  228. cdata->ac = power_supply_register(&pdev->dev, &tps65090_charger_desc,
  229. &psy_cfg);
  230. if (IS_ERR(cdata->ac)) {
  231. dev_err(&pdev->dev, "failed: power supply register\n");
  232. return PTR_ERR(cdata->ac);
  233. }
  234. irq = platform_get_irq(pdev, 0);
  235. if (irq < 0)
  236. irq = -ENXIO;
  237. cdata->irq = irq;
  238. ret = tps65090_config_charger(cdata);
  239. if (ret < 0) {
  240. dev_err(&pdev->dev, "charger config failed, err %d\n", ret);
  241. goto fail_unregister_supply;
  242. }
  243. /* Check for charger presence */
  244. ret = tps65090_read(cdata->dev->parent, TPS65090_REG_CG_STATUS1,
  245. &status1);
  246. if (ret < 0) {
  247. dev_err(cdata->dev, "%s(): Error in reading reg 0x%x", __func__,
  248. TPS65090_REG_CG_STATUS1);
  249. goto fail_unregister_supply;
  250. }
  251. if (status1 != 0) {
  252. ret = tps65090_enable_charging(cdata);
  253. if (ret < 0) {
  254. dev_err(cdata->dev, "error enabling charger\n");
  255. goto fail_unregister_supply;
  256. }
  257. cdata->ac_online = 1;
  258. power_supply_changed(cdata->ac);
  259. }
  260. if (irq != -ENXIO) {
  261. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  262. tps65090_charger_isr, 0, "tps65090-charger", cdata);
  263. if (ret) {
  264. dev_err(cdata->dev,
  265. "Unable to register irq %d err %d\n", irq,
  266. ret);
  267. goto fail_unregister_supply;
  268. }
  269. } else {
  270. cdata->poll_task = kthread_run(tps65090_charger_poll_task,
  271. cdata, "ktps65090charger");
  272. cdata->passive_mode = true;
  273. if (IS_ERR(cdata->poll_task)) {
  274. ret = PTR_ERR(cdata->poll_task);
  275. dev_err(cdata->dev,
  276. "Unable to run kthread err %d\n", ret);
  277. goto fail_unregister_supply;
  278. }
  279. }
  280. return 0;
  281. fail_unregister_supply:
  282. power_supply_unregister(cdata->ac);
  283. return ret;
  284. }
  285. static int tps65090_charger_remove(struct platform_device *pdev)
  286. {
  287. struct tps65090_charger *cdata = platform_get_drvdata(pdev);
  288. if (cdata->irq == -ENXIO)
  289. kthread_stop(cdata->poll_task);
  290. power_supply_unregister(cdata->ac);
  291. return 0;
  292. }
  293. static const struct of_device_id of_tps65090_charger_match[] = {
  294. { .compatible = "ti,tps65090-charger", },
  295. { /* end */ }
  296. };
  297. MODULE_DEVICE_TABLE(of, of_tps65090_charger_match);
  298. static struct platform_driver tps65090_charger_driver = {
  299. .driver = {
  300. .name = "tps65090-charger",
  301. .of_match_table = of_tps65090_charger_match,
  302. },
  303. .probe = tps65090_charger_probe,
  304. .remove = tps65090_charger_remove,
  305. };
  306. module_platform_driver(tps65090_charger_driver);
  307. MODULE_LICENSE("GPL v2");
  308. MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
  309. MODULE_DESCRIPTION("tps65090 battery charger driver");