extcon-gpio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. };
  42. static void gpio_extcon_work(struct work_struct *work)
  43. {
  44. int state;
  45. struct gpio_extcon_data *data =
  46. container_of(to_delayed_work(work), struct gpio_extcon_data,
  47. work);
  48. state = gpio_get_value(data->gpio);
  49. if (data->gpio_active_low)
  50. state = !state;
  51. extcon_set_state(&data->edev, state);
  52. }
  53. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  54. {
  55. struct gpio_extcon_data *extcon_data = dev_id;
  56. queue_delayed_work(system_power_efficient_wq, &extcon_data->work,
  57. extcon_data->debounce_jiffies);
  58. return IRQ_HANDLED;
  59. }
  60. static ssize_t extcon_gpio_print_state(struct extcon_dev *edev, char *buf)
  61. {
  62. struct gpio_extcon_data *extcon_data =
  63. container_of(edev, struct gpio_extcon_data, edev);
  64. const char *state;
  65. if (extcon_get_state(edev))
  66. state = extcon_data->state_on;
  67. else
  68. state = extcon_data->state_off;
  69. if (state)
  70. return sprintf(buf, "%s\n", state);
  71. return -EINVAL;
  72. }
  73. static int gpio_extcon_probe(struct platform_device *pdev)
  74. {
  75. struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
  76. struct gpio_extcon_data *extcon_data;
  77. int ret;
  78. if (!pdata)
  79. return -EBUSY;
  80. if (!pdata->irq_flags) {
  81. dev_err(&pdev->dev, "IRQ flag is not specified.\n");
  82. return -EINVAL;
  83. }
  84. extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
  85. GFP_KERNEL);
  86. if (!extcon_data)
  87. return -ENOMEM;
  88. extcon_data->edev.name = pdata->name;
  89. extcon_data->gpio = pdata->gpio;
  90. extcon_data->gpio_active_low = pdata->gpio_active_low;
  91. extcon_data->state_on = pdata->state_on;
  92. extcon_data->state_off = pdata->state_off;
  93. if (pdata->state_on && pdata->state_off)
  94. extcon_data->edev.print_state = extcon_gpio_print_state;
  95. if (pdata->debounce) {
  96. ret = gpio_set_debounce(extcon_data->gpio,
  97. pdata->debounce * 1000);
  98. if (ret < 0)
  99. extcon_data->debounce_jiffies =
  100. msecs_to_jiffies(pdata->debounce);
  101. }
  102. ret = extcon_dev_register(&extcon_data->edev, &pdev->dev);
  103. if (ret < 0)
  104. return ret;
  105. ret = devm_gpio_request_one(&pdev->dev, extcon_data->gpio, GPIOF_DIR_IN,
  106. pdev->name);
  107. if (ret < 0)
  108. goto err;
  109. INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
  110. extcon_data->irq = gpio_to_irq(extcon_data->gpio);
  111. if (extcon_data->irq < 0) {
  112. ret = extcon_data->irq;
  113. goto err;
  114. }
  115. ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
  116. pdata->irq_flags, pdev->name,
  117. extcon_data);
  118. if (ret < 0)
  119. goto err;
  120. platform_set_drvdata(pdev, extcon_data);
  121. /* Perform initial detection */
  122. gpio_extcon_work(&extcon_data->work.work);
  123. return 0;
  124. err:
  125. extcon_dev_unregister(&extcon_data->edev);
  126. return ret;
  127. }
  128. static int gpio_extcon_remove(struct platform_device *pdev)
  129. {
  130. struct gpio_extcon_data *extcon_data = platform_get_drvdata(pdev);
  131. cancel_delayed_work_sync(&extcon_data->work);
  132. free_irq(extcon_data->irq, extcon_data);
  133. extcon_dev_unregister(&extcon_data->edev);
  134. return 0;
  135. }
  136. static struct platform_driver gpio_extcon_driver = {
  137. .probe = gpio_extcon_probe,
  138. .remove = gpio_extcon_remove,
  139. .driver = {
  140. .name = "extcon-gpio",
  141. .owner = THIS_MODULE,
  142. },
  143. };
  144. module_platform_driver(gpio_extcon_driver);
  145. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  146. MODULE_DESCRIPTION("GPIO extcon driver");
  147. MODULE_LICENSE("GPL");