ltc3651-charger.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2017, Topic Embedded Products
  3. * Driver for LTC3651 charger IC.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/slab.h>
  19. #include <linux/of.h>
  20. struct ltc3651_charger {
  21. struct power_supply *charger;
  22. struct power_supply_desc charger_desc;
  23. struct gpio_desc *acpr_gpio;
  24. struct gpio_desc *fault_gpio;
  25. struct gpio_desc *chrg_gpio;
  26. };
  27. static irqreturn_t ltc3651_charger_irq(int irq, void *devid)
  28. {
  29. struct power_supply *charger = devid;
  30. power_supply_changed(charger);
  31. return IRQ_HANDLED;
  32. }
  33. static inline struct ltc3651_charger *psy_to_ltc3651_charger(
  34. struct power_supply *psy)
  35. {
  36. return power_supply_get_drvdata(psy);
  37. }
  38. static int ltc3651_charger_get_property(struct power_supply *psy,
  39. enum power_supply_property psp, union power_supply_propval *val)
  40. {
  41. struct ltc3651_charger *ltc3651_charger = psy_to_ltc3651_charger(psy);
  42. switch (psp) {
  43. case POWER_SUPPLY_PROP_STATUS:
  44. if (!ltc3651_charger->chrg_gpio) {
  45. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  46. break;
  47. }
  48. if (gpiod_get_value(ltc3651_charger->chrg_gpio))
  49. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  50. else
  51. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  52. break;
  53. case POWER_SUPPLY_PROP_ONLINE:
  54. val->intval = gpiod_get_value(ltc3651_charger->acpr_gpio);
  55. break;
  56. case POWER_SUPPLY_PROP_HEALTH:
  57. if (!ltc3651_charger->fault_gpio) {
  58. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  59. break;
  60. }
  61. if (!gpiod_get_value(ltc3651_charger->fault_gpio)) {
  62. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  63. break;
  64. }
  65. /*
  66. * If the fault pin is active, the chrg pin explains the type
  67. * of failure.
  68. */
  69. if (!ltc3651_charger->chrg_gpio) {
  70. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  71. break;
  72. }
  73. val->intval = gpiod_get_value(ltc3651_charger->chrg_gpio) ?
  74. POWER_SUPPLY_HEALTH_OVERHEAT :
  75. POWER_SUPPLY_HEALTH_DEAD;
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static enum power_supply_property ltc3651_charger_properties[] = {
  83. POWER_SUPPLY_PROP_STATUS,
  84. POWER_SUPPLY_PROP_ONLINE,
  85. POWER_SUPPLY_PROP_HEALTH,
  86. };
  87. static int ltc3651_charger_probe(struct platform_device *pdev)
  88. {
  89. struct power_supply_config psy_cfg = {};
  90. struct ltc3651_charger *ltc3651_charger;
  91. struct power_supply_desc *charger_desc;
  92. int ret;
  93. ltc3651_charger = devm_kzalloc(&pdev->dev, sizeof(*ltc3651_charger),
  94. GFP_KERNEL);
  95. if (!ltc3651_charger)
  96. return -ENOMEM;
  97. ltc3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
  98. "lltc,acpr", GPIOD_IN);
  99. if (IS_ERR(ltc3651_charger->acpr_gpio)) {
  100. ret = PTR_ERR(ltc3651_charger->acpr_gpio);
  101. dev_err(&pdev->dev, "Failed to acquire acpr GPIO: %d\n", ret);
  102. return ret;
  103. }
  104. ltc3651_charger->fault_gpio = devm_gpiod_get_optional(&pdev->dev,
  105. "lltc,fault", GPIOD_IN);
  106. if (IS_ERR(ltc3651_charger->fault_gpio)) {
  107. ret = PTR_ERR(ltc3651_charger->fault_gpio);
  108. dev_err(&pdev->dev, "Failed to acquire fault GPIO: %d\n", ret);
  109. return ret;
  110. }
  111. ltc3651_charger->chrg_gpio = devm_gpiod_get_optional(&pdev->dev,
  112. "lltc,chrg", GPIOD_IN);
  113. if (IS_ERR(ltc3651_charger->chrg_gpio)) {
  114. ret = PTR_ERR(ltc3651_charger->chrg_gpio);
  115. dev_err(&pdev->dev, "Failed to acquire chrg GPIO: %d\n", ret);
  116. return ret;
  117. }
  118. charger_desc = &ltc3651_charger->charger_desc;
  119. charger_desc->name = pdev->dev.of_node->name;
  120. charger_desc->type = POWER_SUPPLY_TYPE_MAINS;
  121. charger_desc->properties = ltc3651_charger_properties;
  122. charger_desc->num_properties = ARRAY_SIZE(ltc3651_charger_properties);
  123. charger_desc->get_property = ltc3651_charger_get_property;
  124. psy_cfg.of_node = pdev->dev.of_node;
  125. psy_cfg.drv_data = ltc3651_charger;
  126. ltc3651_charger->charger = devm_power_supply_register(&pdev->dev,
  127. charger_desc, &psy_cfg);
  128. if (IS_ERR(ltc3651_charger->charger)) {
  129. ret = PTR_ERR(ltc3651_charger->charger);
  130. dev_err(&pdev->dev, "Failed to register power supply: %d\n",
  131. ret);
  132. return ret;
  133. }
  134. /*
  135. * Acquire IRQs for the GPIO pins if possible. If the system does not
  136. * support IRQs on these pins, userspace will have to poll the sysfs
  137. * files manually.
  138. */
  139. if (ltc3651_charger->acpr_gpio) {
  140. ret = gpiod_to_irq(ltc3651_charger->acpr_gpio);
  141. if (ret >= 0)
  142. ret = devm_request_any_context_irq(&pdev->dev, ret,
  143. ltc3651_charger_irq,
  144. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  145. dev_name(&pdev->dev), ltc3651_charger->charger);
  146. if (ret < 0)
  147. dev_warn(&pdev->dev, "Failed to request acpr irq\n");
  148. }
  149. if (ltc3651_charger->fault_gpio) {
  150. ret = gpiod_to_irq(ltc3651_charger->fault_gpio);
  151. if (ret >= 0)
  152. ret = devm_request_any_context_irq(&pdev->dev, ret,
  153. ltc3651_charger_irq,
  154. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  155. dev_name(&pdev->dev), ltc3651_charger->charger);
  156. if (ret < 0)
  157. dev_warn(&pdev->dev, "Failed to request fault irq\n");
  158. }
  159. if (ltc3651_charger->chrg_gpio) {
  160. ret = gpiod_to_irq(ltc3651_charger->chrg_gpio);
  161. if (ret >= 0)
  162. ret = devm_request_any_context_irq(&pdev->dev, ret,
  163. ltc3651_charger_irq,
  164. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  165. dev_name(&pdev->dev), ltc3651_charger->charger);
  166. if (ret < 0)
  167. dev_warn(&pdev->dev, "Failed to request chrg irq\n");
  168. }
  169. platform_set_drvdata(pdev, ltc3651_charger);
  170. return 0;
  171. }
  172. static const struct of_device_id ltc3651_charger_match[] = {
  173. { .compatible = "lltc,ltc3651-charger" },
  174. { }
  175. };
  176. MODULE_DEVICE_TABLE(of, ltc3651_charger_match);
  177. static struct platform_driver ltc3651_charger_driver = {
  178. .probe = ltc3651_charger_probe,
  179. .driver = {
  180. .name = "ltc3651-charger",
  181. .of_match_table = ltc3651_charger_match,
  182. },
  183. };
  184. module_platform_driver(ltc3651_charger_driver);
  185. MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
  186. MODULE_DESCRIPTION("Driver for LTC3651 charger");
  187. MODULE_LICENSE("GPL");
  188. MODULE_ALIAS("platform:ltc3651-charger");