gpio-charger.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * Driver for chargers which report their online status through a GPIO pin
  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. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/device.h>
  16. #include <linux/gpio.h> /* For legacy platform data */
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/power/gpio-charger.h>
  27. struct gpio_charger {
  28. const struct gpio_charger_platform_data *pdata;
  29. unsigned int irq;
  30. bool wakeup_enabled;
  31. struct power_supply *charger;
  32. struct power_supply_desc charger_desc;
  33. struct gpio_desc *gpiod;
  34. bool legacy_gpio_requested;
  35. };
  36. static irqreturn_t gpio_charger_irq(int irq, void *devid)
  37. {
  38. struct power_supply *charger = devid;
  39. power_supply_changed(charger);
  40. return IRQ_HANDLED;
  41. }
  42. static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
  43. {
  44. return power_supply_get_drvdata(psy);
  45. }
  46. static int gpio_charger_get_property(struct power_supply *psy,
  47. enum power_supply_property psp, union power_supply_propval *val)
  48. {
  49. struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
  50. const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
  51. switch (psp) {
  52. case POWER_SUPPLY_PROP_ONLINE:
  53. val->intval = gpiod_get_value_cansleep(gpio_charger->gpiod);
  54. /* This xor is only ever used with legacy pdata GPIO */
  55. val->intval ^= pdata->gpio_active_low;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. return 0;
  61. }
  62. static enum power_supply_property gpio_charger_properties[] = {
  63. POWER_SUPPLY_PROP_ONLINE,
  64. };
  65. static
  66. struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
  67. {
  68. struct device_node *np = dev->of_node;
  69. struct gpio_charger_platform_data *pdata;
  70. const char *chargetype;
  71. int ret;
  72. if (!np)
  73. return ERR_PTR(-ENOENT);
  74. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  75. if (!pdata)
  76. return ERR_PTR(-ENOMEM);
  77. pdata->name = np->name;
  78. pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
  79. ret = of_property_read_string(np, "charger-type", &chargetype);
  80. if (ret >= 0) {
  81. if (!strncmp("unknown", chargetype, 7))
  82. pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
  83. else if (!strncmp("battery", chargetype, 7))
  84. pdata->type = POWER_SUPPLY_TYPE_BATTERY;
  85. else if (!strncmp("ups", chargetype, 3))
  86. pdata->type = POWER_SUPPLY_TYPE_UPS;
  87. else if (!strncmp("mains", chargetype, 5))
  88. pdata->type = POWER_SUPPLY_TYPE_MAINS;
  89. else if (!strncmp("usb-sdp", chargetype, 7))
  90. pdata->type = POWER_SUPPLY_TYPE_USB;
  91. else if (!strncmp("usb-dcp", chargetype, 7))
  92. pdata->type = POWER_SUPPLY_TYPE_USB_DCP;
  93. else if (!strncmp("usb-cdp", chargetype, 7))
  94. pdata->type = POWER_SUPPLY_TYPE_USB_CDP;
  95. else if (!strncmp("usb-aca", chargetype, 7))
  96. pdata->type = POWER_SUPPLY_TYPE_USB_ACA;
  97. else
  98. dev_warn(dev, "unknown charger type %s\n", chargetype);
  99. }
  100. return pdata;
  101. }
  102. static int gpio_charger_probe(struct platform_device *pdev)
  103. {
  104. const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
  105. struct power_supply_config psy_cfg = {};
  106. struct gpio_charger *gpio_charger;
  107. struct power_supply_desc *charger_desc;
  108. int ret;
  109. int irq;
  110. if (!pdata) {
  111. pdata = gpio_charger_parse_dt(&pdev->dev);
  112. if (IS_ERR(pdata)) {
  113. ret = PTR_ERR(pdata);
  114. if (ret != -EPROBE_DEFER)
  115. dev_err(&pdev->dev, "No platform data\n");
  116. return ret;
  117. }
  118. }
  119. gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger),
  120. GFP_KERNEL);
  121. if (!gpio_charger) {
  122. dev_err(&pdev->dev, "Failed to alloc driver structure\n");
  123. return -ENOMEM;
  124. }
  125. /*
  126. * This will fetch a GPIO descriptor from device tree, ACPI or
  127. * boardfile descriptor tables. It's good to try this first.
  128. */
  129. gpio_charger->gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
  130. /*
  131. * If this fails and we're not using device tree, try the
  132. * legacy platform data method.
  133. */
  134. if (IS_ERR(gpio_charger->gpiod) && !pdev->dev.of_node) {
  135. /* Non-DT: use legacy GPIO numbers */
  136. if (!gpio_is_valid(pdata->gpio)) {
  137. dev_err(&pdev->dev, "Invalid gpio pin in pdata\n");
  138. return -EINVAL;
  139. }
  140. ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
  141. if (ret) {
  142. dev_err(&pdev->dev, "Failed to request gpio pin: %d\n",
  143. ret);
  144. return ret;
  145. }
  146. gpio_charger->legacy_gpio_requested = true;
  147. ret = gpio_direction_input(pdata->gpio);
  148. if (ret) {
  149. dev_err(&pdev->dev, "Failed to set gpio to input: %d\n",
  150. ret);
  151. goto err_gpio_free;
  152. }
  153. /* Then convert this to gpiod for now */
  154. gpio_charger->gpiod = gpio_to_desc(pdata->gpio);
  155. } else if (IS_ERR(gpio_charger->gpiod)) {
  156. /* Just try again if this happens */
  157. if (PTR_ERR(gpio_charger->gpiod) == -EPROBE_DEFER)
  158. return -EPROBE_DEFER;
  159. dev_err(&pdev->dev, "error getting GPIO descriptor\n");
  160. return PTR_ERR(gpio_charger->gpiod);
  161. }
  162. charger_desc = &gpio_charger->charger_desc;
  163. charger_desc->name = pdata->name ? pdata->name : "gpio-charger";
  164. charger_desc->type = pdata->type;
  165. charger_desc->properties = gpio_charger_properties;
  166. charger_desc->num_properties = ARRAY_SIZE(gpio_charger_properties);
  167. charger_desc->get_property = gpio_charger_get_property;
  168. psy_cfg.supplied_to = pdata->supplied_to;
  169. psy_cfg.num_supplicants = pdata->num_supplicants;
  170. psy_cfg.of_node = pdev->dev.of_node;
  171. psy_cfg.drv_data = gpio_charger;
  172. gpio_charger->pdata = pdata;
  173. gpio_charger->charger = power_supply_register(&pdev->dev,
  174. charger_desc, &psy_cfg);
  175. if (IS_ERR(gpio_charger->charger)) {
  176. ret = PTR_ERR(gpio_charger->charger);
  177. dev_err(&pdev->dev, "Failed to register power supply: %d\n",
  178. ret);
  179. goto err_gpio_free;
  180. }
  181. irq = gpiod_to_irq(gpio_charger->gpiod);
  182. if (irq > 0) {
  183. ret = request_any_context_irq(irq, gpio_charger_irq,
  184. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  185. dev_name(&pdev->dev), gpio_charger->charger);
  186. if (ret < 0)
  187. dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret);
  188. else
  189. gpio_charger->irq = irq;
  190. }
  191. platform_set_drvdata(pdev, gpio_charger);
  192. device_init_wakeup(&pdev->dev, 1);
  193. return 0;
  194. err_gpio_free:
  195. if (gpio_charger->legacy_gpio_requested)
  196. gpio_free(pdata->gpio);
  197. return ret;
  198. }
  199. static int gpio_charger_remove(struct platform_device *pdev)
  200. {
  201. struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
  202. if (gpio_charger->irq)
  203. free_irq(gpio_charger->irq, gpio_charger->charger);
  204. power_supply_unregister(gpio_charger->charger);
  205. if (gpio_charger->legacy_gpio_requested)
  206. gpio_free(gpio_charger->pdata->gpio);
  207. return 0;
  208. }
  209. #ifdef CONFIG_PM_SLEEP
  210. static int gpio_charger_suspend(struct device *dev)
  211. {
  212. struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
  213. if (device_may_wakeup(dev))
  214. gpio_charger->wakeup_enabled =
  215. !enable_irq_wake(gpio_charger->irq);
  216. return 0;
  217. }
  218. static int gpio_charger_resume(struct device *dev)
  219. {
  220. struct platform_device *pdev = to_platform_device(dev);
  221. struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
  222. if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled)
  223. disable_irq_wake(gpio_charger->irq);
  224. power_supply_changed(gpio_charger->charger);
  225. return 0;
  226. }
  227. #endif
  228. static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
  229. gpio_charger_suspend, gpio_charger_resume);
  230. static const struct of_device_id gpio_charger_match[] = {
  231. { .compatible = "gpio-charger" },
  232. { }
  233. };
  234. MODULE_DEVICE_TABLE(of, gpio_charger_match);
  235. static struct platform_driver gpio_charger_driver = {
  236. .probe = gpio_charger_probe,
  237. .remove = gpio_charger_remove,
  238. .driver = {
  239. .name = "gpio-charger",
  240. .pm = &gpio_charger_pm_ops,
  241. .of_match_table = gpio_charger_match,
  242. },
  243. };
  244. module_platform_driver(gpio_charger_driver);
  245. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  246. MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
  247. MODULE_LICENSE("GPL");
  248. MODULE_ALIAS("platform:gpio-charger");