tps65217_charger.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Battery charger driver for TI's tps65217
  3. *
  4. * Copyright (c) 2015, Collabora Ltd.
  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. /*
  16. * Battery charger driver for TI's tps65217
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/kthread.h>
  20. #include <linux/device.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/power_supply.h>
  30. #include <linux/mfd/core.h>
  31. #include <linux/mfd/tps65217.h>
  32. #define POLL_INTERVAL (HZ * 2)
  33. struct tps65217_charger {
  34. struct tps65217 *tps;
  35. struct device *dev;
  36. struct power_supply *ac;
  37. int ac_online;
  38. int prev_ac_online;
  39. struct task_struct *poll_task;
  40. int irq;
  41. };
  42. static enum power_supply_property tps65217_ac_props[] = {
  43. POWER_SUPPLY_PROP_ONLINE,
  44. };
  45. static int tps65217_config_charger(struct tps65217_charger *charger)
  46. {
  47. int ret;
  48. dev_dbg(charger->dev, "%s\n", __func__);
  49. /*
  50. * tps65217 rev. G, p. 31 (see p. 32 for NTC schematic)
  51. *
  52. * The device can be configured to support a 100k NTC (B = 3960) by
  53. * setting the the NTC_TYPE bit in register CHGCONFIG1 to 1. However it
  54. * is not recommended to do so. In sleep mode, the charger continues
  55. * charging the battery, but all register values are reset to default
  56. * values. Therefore, the charger would get the wrong temperature
  57. * information. If 100k NTC setting is required, please contact the
  58. * factory.
  59. *
  60. * ATTENTION, conflicting information, from p. 46
  61. *
  62. * NTC TYPE (for battery temperature measurement)
  63. * 0 – 100k (curve 1, B = 3960)
  64. * 1 – 10k (curve 2, B = 3480) (default on reset)
  65. *
  66. */
  67. ret = tps65217_clear_bits(charger->tps, TPS65217_REG_CHGCONFIG1,
  68. TPS65217_CHGCONFIG1_NTC_TYPE,
  69. TPS65217_PROTECT_NONE);
  70. if (ret) {
  71. dev_err(charger->dev,
  72. "failed to set 100k NTC setting: %d\n", ret);
  73. return ret;
  74. }
  75. return 0;
  76. }
  77. static int tps65217_enable_charging(struct tps65217_charger *charger)
  78. {
  79. int ret;
  80. /* charger already enabled */
  81. if (charger->ac_online)
  82. return 0;
  83. dev_dbg(charger->dev, "%s: enable charging\n", __func__);
  84. ret = tps65217_set_bits(charger->tps, TPS65217_REG_CHGCONFIG1,
  85. TPS65217_CHGCONFIG1_CHG_EN,
  86. TPS65217_CHGCONFIG1_CHG_EN,
  87. TPS65217_PROTECT_NONE);
  88. if (ret) {
  89. dev_err(charger->dev,
  90. "%s: Error in writing CHG_EN in reg 0x%x: %d\n",
  91. __func__, TPS65217_REG_CHGCONFIG1, ret);
  92. return ret;
  93. }
  94. charger->ac_online = 1;
  95. return 0;
  96. }
  97. static int tps65217_ac_get_property(struct power_supply *psy,
  98. enum power_supply_property psp,
  99. union power_supply_propval *val)
  100. {
  101. struct tps65217_charger *charger = power_supply_get_drvdata(psy);
  102. if (psp == POWER_SUPPLY_PROP_ONLINE) {
  103. val->intval = charger->ac_online;
  104. return 0;
  105. }
  106. return -EINVAL;
  107. }
  108. static irqreturn_t tps65217_charger_irq(int irq, void *dev)
  109. {
  110. int ret, val;
  111. struct tps65217_charger *charger = dev;
  112. charger->prev_ac_online = charger->ac_online;
  113. ret = tps65217_reg_read(charger->tps, TPS65217_REG_STATUS, &val);
  114. if (ret < 0) {
  115. dev_err(charger->dev, "%s: Error in reading reg 0x%x\n",
  116. __func__, TPS65217_REG_STATUS);
  117. return IRQ_HANDLED;
  118. }
  119. dev_dbg(charger->dev, "%s: 0x%x\n", __func__, val);
  120. /* check for AC status bit */
  121. if (val & TPS65217_STATUS_ACPWR) {
  122. ret = tps65217_enable_charging(charger);
  123. if (ret) {
  124. dev_err(charger->dev,
  125. "failed to enable charger: %d\n", ret);
  126. return IRQ_HANDLED;
  127. }
  128. } else {
  129. charger->ac_online = 0;
  130. }
  131. if (charger->prev_ac_online != charger->ac_online)
  132. power_supply_changed(charger->ac);
  133. ret = tps65217_reg_read(charger->tps, TPS65217_REG_CHGCONFIG0, &val);
  134. if (ret < 0) {
  135. dev_err(charger->dev, "%s: Error in reading reg 0x%x\n",
  136. __func__, TPS65217_REG_CHGCONFIG0);
  137. return IRQ_HANDLED;
  138. }
  139. if (val & TPS65217_CHGCONFIG0_ACTIVE)
  140. dev_dbg(charger->dev, "%s: charger is charging\n", __func__);
  141. else
  142. dev_dbg(charger->dev,
  143. "%s: charger is NOT charging\n", __func__);
  144. return IRQ_HANDLED;
  145. }
  146. static int tps65217_charger_poll_task(void *data)
  147. {
  148. set_freezable();
  149. while (!kthread_should_stop()) {
  150. schedule_timeout_interruptible(POLL_INTERVAL);
  151. try_to_freeze();
  152. tps65217_charger_irq(-1, data);
  153. }
  154. return 0;
  155. }
  156. static const struct power_supply_desc tps65217_charger_desc = {
  157. .name = "tps65217-ac",
  158. .type = POWER_SUPPLY_TYPE_MAINS,
  159. .get_property = tps65217_ac_get_property,
  160. .properties = tps65217_ac_props,
  161. .num_properties = ARRAY_SIZE(tps65217_ac_props),
  162. };
  163. static int tps65217_charger_probe(struct platform_device *pdev)
  164. {
  165. struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
  166. struct tps65217_charger *charger;
  167. struct power_supply_config cfg = {};
  168. int irq;
  169. int ret;
  170. dev_dbg(&pdev->dev, "%s\n", __func__);
  171. charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
  172. if (!charger)
  173. return -ENOMEM;
  174. platform_set_drvdata(pdev, charger);
  175. charger->tps = tps;
  176. charger->dev = &pdev->dev;
  177. cfg.of_node = pdev->dev.of_node;
  178. cfg.drv_data = charger;
  179. charger->ac = devm_power_supply_register(&pdev->dev,
  180. &tps65217_charger_desc,
  181. &cfg);
  182. if (IS_ERR(charger->ac)) {
  183. dev_err(&pdev->dev, "failed: power supply register\n");
  184. return PTR_ERR(charger->ac);
  185. }
  186. irq = platform_get_irq_byname(pdev, "AC");
  187. if (irq < 0)
  188. irq = -ENXIO;
  189. charger->irq = irq;
  190. ret = tps65217_config_charger(charger);
  191. if (ret < 0) {
  192. dev_err(charger->dev, "charger config failed, err %d\n", ret);
  193. return ret;
  194. }
  195. if (irq != -ENXIO) {
  196. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  197. tps65217_charger_irq,
  198. 0, "tps65217-charger",
  199. charger);
  200. if (ret) {
  201. dev_err(charger->dev,
  202. "Unable to register irq %d err %d\n", irq,
  203. ret);
  204. return ret;
  205. }
  206. /* Check current state */
  207. tps65217_charger_irq(irq, charger);
  208. } else {
  209. charger->poll_task = kthread_run(tps65217_charger_poll_task,
  210. charger, "ktps65217charger");
  211. if (IS_ERR(charger->poll_task)) {
  212. ret = PTR_ERR(charger->poll_task);
  213. dev_err(charger->dev,
  214. "Unable to run kthread err %d\n", ret);
  215. return ret;
  216. }
  217. }
  218. return 0;
  219. }
  220. static int tps65217_charger_remove(struct platform_device *pdev)
  221. {
  222. struct tps65217_charger *charger = platform_get_drvdata(pdev);
  223. if (charger->irq == -ENXIO)
  224. kthread_stop(charger->poll_task);
  225. return 0;
  226. }
  227. static const struct of_device_id tps65217_charger_match_table[] = {
  228. { .compatible = "ti,tps65217-charger", },
  229. { /* sentinel */ }
  230. };
  231. MODULE_DEVICE_TABLE(of, tps65217_charger_match_table);
  232. static struct platform_driver tps65217_charger_driver = {
  233. .probe = tps65217_charger_probe,
  234. .remove = tps65217_charger_remove,
  235. .driver = {
  236. .name = "tps65217-charger",
  237. .of_match_table = of_match_ptr(tps65217_charger_match_table),
  238. },
  239. };
  240. module_platform_driver(tps65217_charger_driver);
  241. MODULE_LICENSE("GPL v2");
  242. MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
  243. MODULE_DESCRIPTION("TPS65217 battery charger driver");