gpio-charger.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/power/gpio-charger.h>
  25. struct gpio_charger {
  26. const struct gpio_charger_platform_data *pdata;
  27. unsigned int irq;
  28. struct power_supply charger;
  29. };
  30. static irqreturn_t gpio_charger_irq(int irq, void *devid)
  31. {
  32. struct power_supply *charger = devid;
  33. power_supply_changed(charger);
  34. return IRQ_HANDLED;
  35. }
  36. static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
  37. {
  38. return container_of(psy, struct gpio_charger, charger);
  39. }
  40. static int gpio_charger_get_property(struct power_supply *psy,
  41. enum power_supply_property psp, union power_supply_propval *val)
  42. {
  43. struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
  44. const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
  45. switch (psp) {
  46. case POWER_SUPPLY_PROP_ONLINE:
  47. val->intval = gpio_get_value(pdata->gpio);
  48. val->intval ^= pdata->gpio_active_low;
  49. break;
  50. default:
  51. return -EINVAL;
  52. }
  53. return 0;
  54. }
  55. static enum power_supply_property gpio_charger_properties[] = {
  56. POWER_SUPPLY_PROP_ONLINE,
  57. };
  58. static int __devinit gpio_charger_probe(struct platform_device *pdev)
  59. {
  60. const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
  61. struct gpio_charger *gpio_charger;
  62. struct power_supply *charger;
  63. int ret;
  64. int irq;
  65. if (!pdata) {
  66. dev_err(&pdev->dev, "No platform data\n");
  67. return -EINVAL;
  68. }
  69. if (!gpio_is_valid(pdata->gpio)) {
  70. dev_err(&pdev->dev, "Invalid gpio pin\n");
  71. return -EINVAL;
  72. }
  73. gpio_charger = kzalloc(sizeof(*gpio_charger), GFP_KERNEL);
  74. if (!gpio_charger) {
  75. dev_err(&pdev->dev, "Failed to alloc driver structure\n");
  76. return -ENOMEM;
  77. }
  78. charger = &gpio_charger->charger;
  79. charger->name = pdata->name ? pdata->name : "gpio-charger";
  80. charger->type = pdata->type;
  81. charger->properties = gpio_charger_properties;
  82. charger->num_properties = ARRAY_SIZE(gpio_charger_properties);
  83. charger->get_property = gpio_charger_get_property;
  84. charger->supplied_to = pdata->supplied_to;
  85. charger->num_supplicants = pdata->num_supplicants;
  86. ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
  87. if (ret) {
  88. dev_err(&pdev->dev, "Failed to request gpio pin: %d\n", ret);
  89. goto err_free;
  90. }
  91. ret = gpio_direction_input(pdata->gpio);
  92. if (ret) {
  93. dev_err(&pdev->dev, "Failed to set gpio to input: %d\n", ret);
  94. goto err_gpio_free;
  95. }
  96. gpio_charger->pdata = pdata;
  97. ret = power_supply_register(&pdev->dev, charger);
  98. if (ret < 0) {
  99. dev_err(&pdev->dev, "Failed to register power supply: %d\n",
  100. ret);
  101. goto err_gpio_free;
  102. }
  103. irq = gpio_to_irq(pdata->gpio);
  104. if (irq > 0) {
  105. ret = request_any_context_irq(irq, gpio_charger_irq,
  106. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  107. dev_name(&pdev->dev), charger);
  108. if (ret < 0)
  109. dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret);
  110. else
  111. gpio_charger->irq = irq;
  112. }
  113. platform_set_drvdata(pdev, gpio_charger);
  114. return 0;
  115. err_gpio_free:
  116. gpio_free(pdata->gpio);
  117. err_free:
  118. kfree(gpio_charger);
  119. return ret;
  120. }
  121. static int __devexit gpio_charger_remove(struct platform_device *pdev)
  122. {
  123. struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
  124. if (gpio_charger->irq)
  125. free_irq(gpio_charger->irq, &gpio_charger->charger);
  126. power_supply_unregister(&gpio_charger->charger);
  127. gpio_free(gpio_charger->pdata->gpio);
  128. platform_set_drvdata(pdev, NULL);
  129. kfree(gpio_charger);
  130. return 0;
  131. }
  132. #ifdef CONFIG_PM_SLEEP
  133. static int gpio_charger_resume(struct device *dev)
  134. {
  135. struct platform_device *pdev = to_platform_device(dev);
  136. struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
  137. power_supply_changed(&gpio_charger->charger);
  138. return 0;
  139. }
  140. #endif
  141. static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops, NULL, gpio_charger_resume);
  142. static struct platform_driver gpio_charger_driver = {
  143. .probe = gpio_charger_probe,
  144. .remove = __devexit_p(gpio_charger_remove),
  145. .driver = {
  146. .name = "gpio-charger",
  147. .owner = THIS_MODULE,
  148. .pm = &gpio_charger_pm_ops,
  149. },
  150. };
  151. module_platform_driver(gpio_charger_driver);
  152. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  153. MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
  154. MODULE_LICENSE("GPL");
  155. MODULE_ALIAS("platform:gpio-charger");