extcon-gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * drivers/extcon/extcon_gpio.c
  3. *
  4. * Single-state GPIO extcon driver based on extcon class
  5. *
  6. * Copyright (C) 2008 Google, Inc.
  7. * Author: Mike Lockwood <lockwood@android.com>
  8. *
  9. * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
  10. * (originally switch class is supported)
  11. *
  12. * This software is licensed under the terms of the GNU General Public
  13. * License version 2, as published by the Free Software Foundation, and
  14. * may be copied, distributed, and modified under those terms.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/gpio.h>
  30. #include <linux/extcon.h>
  31. #include <linux/extcon/extcon-gpio.h>
  32. struct gpio_extcon_data {
  33. struct extcon_dev edev;
  34. unsigned gpio;
  35. bool gpio_active_low;
  36. const char *state_on;
  37. const char *state_off;
  38. int irq;
  39. struct delayed_work work;
  40. unsigned long debounce_jiffies;
  41. bool check_on_resume;
  42. };
  43. static void gpio_extcon_work(struct work_struct *work)
  44. {
  45. int state;
  46. struct gpio_extcon_data *data =
  47. container_of(to_delayed_work(work), struct gpio_extcon_data,
  48. work);
  49. state = gpio_get_value(data->gpio);
  50. if (data->gpio_active_low)
  51. state = !state;
  52. extcon_set_state(&data->edev, state);
  53. }
  54. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  55. {
  56. struct gpio_extcon_data *extcon_data = dev_id;
  57. queue_delayed_work(system_power_efficient_wq, &extcon_data->work,
  58. extcon_data->debounce_jiffies);
  59. return IRQ_HANDLED;
  60. }
  61. static ssize_t extcon_gpio_print_state(struct extcon_dev *edev, char *buf)
  62. {
  63. struct gpio_extcon_data *extcon_data =
  64. container_of(edev, struct gpio_extcon_data, edev);
  65. const char *state;
  66. if (extcon_get_state(edev))
  67. state = extcon_data->state_on;
  68. else
  69. state = extcon_data->state_off;
  70. if (state)
  71. return sprintf(buf, "%s\n", state);
  72. return -EINVAL;
  73. }
  74. static int gpio_extcon_probe(struct platform_device *pdev)
  75. {
  76. struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
  77. struct gpio_extcon_data *extcon_data;
  78. int ret;
  79. if (!pdata)
  80. return -EBUSY;
  81. if (!pdata->irq_flags) {
  82. dev_err(&pdev->dev, "IRQ flag is not specified.\n");
  83. return -EINVAL;
  84. }
  85. extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
  86. GFP_KERNEL);
  87. if (!extcon_data)
  88. return -ENOMEM;
  89. extcon_data->edev.name = pdata->name;
  90. extcon_data->edev.dev.parent = &pdev->dev;
  91. extcon_data->gpio = pdata->gpio;
  92. extcon_data->gpio_active_low = pdata->gpio_active_low;
  93. extcon_data->state_on = pdata->state_on;
  94. extcon_data->state_off = pdata->state_off;
  95. extcon_data->check_on_resume = pdata->check_on_resume;
  96. if (pdata->state_on && pdata->state_off)
  97. extcon_data->edev.print_state = extcon_gpio_print_state;
  98. ret = devm_gpio_request_one(&pdev->dev, extcon_data->gpio, GPIOF_DIR_IN,
  99. pdev->name);
  100. if (ret < 0)
  101. return ret;
  102. if (pdata->debounce) {
  103. ret = gpio_set_debounce(extcon_data->gpio,
  104. pdata->debounce * 1000);
  105. if (ret < 0)
  106. extcon_data->debounce_jiffies =
  107. msecs_to_jiffies(pdata->debounce);
  108. }
  109. ret = extcon_dev_register(&extcon_data->edev);
  110. if (ret < 0)
  111. return ret;
  112. INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
  113. extcon_data->irq = gpio_to_irq(extcon_data->gpio);
  114. if (extcon_data->irq < 0) {
  115. ret = extcon_data->irq;
  116. goto err;
  117. }
  118. ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
  119. pdata->irq_flags, pdev->name,
  120. extcon_data);
  121. if (ret < 0)
  122. goto err;
  123. platform_set_drvdata(pdev, extcon_data);
  124. /* Perform initial detection */
  125. gpio_extcon_work(&extcon_data->work.work);
  126. return 0;
  127. err:
  128. extcon_dev_unregister(&extcon_data->edev);
  129. return ret;
  130. }
  131. static int gpio_extcon_remove(struct platform_device *pdev)
  132. {
  133. struct gpio_extcon_data *extcon_data = platform_get_drvdata(pdev);
  134. cancel_delayed_work_sync(&extcon_data->work);
  135. free_irq(extcon_data->irq, extcon_data);
  136. extcon_dev_unregister(&extcon_data->edev);
  137. return 0;
  138. }
  139. #ifdef CONFIG_PM_SLEEP
  140. static int gpio_extcon_resume(struct device *dev)
  141. {
  142. struct gpio_extcon_data *extcon_data;
  143. extcon_data = dev_get_drvdata(dev);
  144. if (extcon_data->check_on_resume)
  145. queue_delayed_work(system_power_efficient_wq,
  146. &extcon_data->work, extcon_data->debounce_jiffies);
  147. return 0;
  148. }
  149. #endif
  150. static const struct dev_pm_ops gpio_extcon_pm_ops = {
  151. SET_SYSTEM_SLEEP_PM_OPS(NULL, gpio_extcon_resume)
  152. };
  153. static struct platform_driver gpio_extcon_driver = {
  154. .probe = gpio_extcon_probe,
  155. .remove = gpio_extcon_remove,
  156. .driver = {
  157. .name = "extcon-gpio",
  158. .owner = THIS_MODULE,
  159. .pm = &gpio_extcon_pm_ops,
  160. },
  161. };
  162. module_platform_driver(gpio_extcon_driver);
  163. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  164. MODULE_DESCRIPTION("GPIO extcon driver");
  165. MODULE_LICENSE("GPL");