gpio-charger.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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>
  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/of_gpio.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. };
  34. static irqreturn_t gpio_charger_irq(int irq, void *devid)
  35. {
  36. struct power_supply *charger = devid;
  37. power_supply_changed(charger);
  38. return IRQ_HANDLED;
  39. }
  40. static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
  41. {
  42. return power_supply_get_drvdata(psy);
  43. }
  44. static int gpio_charger_get_property(struct power_supply *psy,
  45. enum power_supply_property psp, union power_supply_propval *val)
  46. {
  47. struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
  48. const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
  49. switch (psp) {
  50. case POWER_SUPPLY_PROP_ONLINE:
  51. val->intval = !!gpio_get_value_cansleep(pdata->gpio);
  52. val->intval ^= pdata->gpio_active_low;
  53. break;
  54. default:
  55. return -EINVAL;
  56. }
  57. return 0;
  58. }
  59. static enum power_supply_property gpio_charger_properties[] = {
  60. POWER_SUPPLY_PROP_ONLINE,
  61. };
  62. static
  63. struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
  64. {
  65. struct device_node *np = dev->of_node;
  66. struct gpio_charger_platform_data *pdata;
  67. const char *chargetype;
  68. enum of_gpio_flags flags;
  69. int ret;
  70. if (!np)
  71. return ERR_PTR(-ENOENT);
  72. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  73. if (!pdata)
  74. return ERR_PTR(-ENOMEM);
  75. pdata->name = np->name;
  76. pdata->gpio = of_get_gpio_flags(np, 0, &flags);
  77. if (pdata->gpio < 0) {
  78. if (pdata->gpio != -EPROBE_DEFER)
  79. dev_err(dev, "could not get charger gpio\n");
  80. return ERR_PTR(pdata->gpio);
  81. }
  82. pdata->gpio_active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
  83. pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
  84. ret = of_property_read_string(np, "charger-type", &chargetype);
  85. if (ret >= 0) {
  86. if (!strncmp("unknown", chargetype, 7))
  87. pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
  88. else if (!strncmp("battery", chargetype, 7))
  89. pdata->type = POWER_SUPPLY_TYPE_BATTERY;
  90. else if (!strncmp("ups", chargetype, 3))
  91. pdata->type = POWER_SUPPLY_TYPE_UPS;
  92. else if (!strncmp("mains", chargetype, 5))
  93. pdata->type = POWER_SUPPLY_TYPE_MAINS;
  94. else if (!strncmp("usb-sdp", chargetype, 7))
  95. pdata->type = POWER_SUPPLY_TYPE_USB;
  96. else if (!strncmp("usb-dcp", chargetype, 7))
  97. pdata->type = POWER_SUPPLY_TYPE_USB_DCP;
  98. else if (!strncmp("usb-cdp", chargetype, 7))
  99. pdata->type = POWER_SUPPLY_TYPE_USB_CDP;
  100. else if (!strncmp("usb-aca", chargetype, 7))
  101. pdata->type = POWER_SUPPLY_TYPE_USB_ACA;
  102. else
  103. dev_warn(dev, "unknown charger type %s\n", chargetype);
  104. }
  105. return pdata;
  106. }
  107. static int gpio_charger_probe(struct platform_device *pdev)
  108. {
  109. const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
  110. struct power_supply_config psy_cfg = {};
  111. struct gpio_charger *gpio_charger;
  112. struct power_supply_desc *charger_desc;
  113. int ret;
  114. int irq;
  115. if (!pdata) {
  116. pdata = gpio_charger_parse_dt(&pdev->dev);
  117. if (IS_ERR(pdata)) {
  118. ret = PTR_ERR(pdata);
  119. if (ret != -EPROBE_DEFER)
  120. dev_err(&pdev->dev, "No platform data\n");
  121. return ret;
  122. }
  123. }
  124. if (!gpio_is_valid(pdata->gpio)) {
  125. dev_err(&pdev->dev, "Invalid gpio pin\n");
  126. return -EINVAL;
  127. }
  128. gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger),
  129. GFP_KERNEL);
  130. if (!gpio_charger) {
  131. dev_err(&pdev->dev, "Failed to alloc driver structure\n");
  132. return -ENOMEM;
  133. }
  134. charger_desc = &gpio_charger->charger_desc;
  135. charger_desc->name = pdata->name ? pdata->name : "gpio-charger";
  136. charger_desc->type = pdata->type;
  137. charger_desc->properties = gpio_charger_properties;
  138. charger_desc->num_properties = ARRAY_SIZE(gpio_charger_properties);
  139. charger_desc->get_property = gpio_charger_get_property;
  140. psy_cfg.supplied_to = pdata->supplied_to;
  141. psy_cfg.num_supplicants = pdata->num_supplicants;
  142. psy_cfg.of_node = pdev->dev.of_node;
  143. psy_cfg.drv_data = gpio_charger;
  144. ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
  145. if (ret) {
  146. dev_err(&pdev->dev, "Failed to request gpio pin: %d\n", ret);
  147. goto err_free;
  148. }
  149. ret = gpio_direction_input(pdata->gpio);
  150. if (ret) {
  151. dev_err(&pdev->dev, "Failed to set gpio to input: %d\n", ret);
  152. goto err_gpio_free;
  153. }
  154. gpio_charger->pdata = pdata;
  155. gpio_charger->charger = power_supply_register(&pdev->dev,
  156. charger_desc, &psy_cfg);
  157. if (IS_ERR(gpio_charger->charger)) {
  158. ret = PTR_ERR(gpio_charger->charger);
  159. dev_err(&pdev->dev, "Failed to register power supply: %d\n",
  160. ret);
  161. goto err_gpio_free;
  162. }
  163. irq = gpio_to_irq(pdata->gpio);
  164. if (irq > 0) {
  165. ret = request_any_context_irq(irq, gpio_charger_irq,
  166. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  167. dev_name(&pdev->dev), gpio_charger->charger);
  168. if (ret < 0)
  169. dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret);
  170. else
  171. gpio_charger->irq = irq;
  172. }
  173. platform_set_drvdata(pdev, gpio_charger);
  174. device_init_wakeup(&pdev->dev, 1);
  175. return 0;
  176. err_gpio_free:
  177. gpio_free(pdata->gpio);
  178. err_free:
  179. return ret;
  180. }
  181. static int gpio_charger_remove(struct platform_device *pdev)
  182. {
  183. struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
  184. if (gpio_charger->irq)
  185. free_irq(gpio_charger->irq, gpio_charger->charger);
  186. power_supply_unregister(gpio_charger->charger);
  187. gpio_free(gpio_charger->pdata->gpio);
  188. return 0;
  189. }
  190. #ifdef CONFIG_PM_SLEEP
  191. static int gpio_charger_suspend(struct device *dev)
  192. {
  193. struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
  194. if (device_may_wakeup(dev))
  195. gpio_charger->wakeup_enabled =
  196. !enable_irq_wake(gpio_charger->irq);
  197. return 0;
  198. }
  199. static int gpio_charger_resume(struct device *dev)
  200. {
  201. struct platform_device *pdev = to_platform_device(dev);
  202. struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
  203. if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled)
  204. disable_irq_wake(gpio_charger->irq);
  205. power_supply_changed(gpio_charger->charger);
  206. return 0;
  207. }
  208. #endif
  209. static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
  210. gpio_charger_suspend, gpio_charger_resume);
  211. static const struct of_device_id gpio_charger_match[] = {
  212. { .compatible = "gpio-charger" },
  213. { }
  214. };
  215. MODULE_DEVICE_TABLE(of, gpio_charger_match);
  216. static struct platform_driver gpio_charger_driver = {
  217. .probe = gpio_charger_probe,
  218. .remove = gpio_charger_remove,
  219. .driver = {
  220. .name = "gpio-charger",
  221. .pm = &gpio_charger_pm_ops,
  222. .of_match_table = gpio_charger_match,
  223. },
  224. };
  225. module_platform_driver(gpio_charger_driver);
  226. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  227. MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
  228. MODULE_LICENSE("GPL");
  229. MODULE_ALIAS("platform:gpio-charger");