extcon-gpio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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->edev.dev.parent = &pdev->dev;
  90. extcon_data->gpio = pdata->gpio;
  91. extcon_data->gpio_active_low = pdata->gpio_active_low;
  92. extcon_data->state_on = pdata->state_on;
  93. extcon_data->state_off = pdata->state_off;
  94. if (pdata->state_on && pdata->state_off)
  95. extcon_data->edev.print_state = extcon_gpio_print_state;
  96. if (pdata->debounce) {
  97. ret = gpio_set_debounce(extcon_data->gpio,
  98. pdata->debounce * 1000);
  99. if (ret < 0)
  100. extcon_data->debounce_jiffies =
  101. msecs_to_jiffies(pdata->debounce);
  102. }
  103. ret = extcon_dev_register(&extcon_data->edev);
  104. if (ret < 0)
  105. return ret;
  106. ret = devm_gpio_request_one(&pdev->dev, extcon_data->gpio, GPIOF_DIR_IN,
  107. pdev->name);
  108. if (ret < 0)
  109. goto err;
  110. INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
  111. extcon_data->irq = gpio_to_irq(extcon_data->gpio);
  112. if (extcon_data->irq < 0) {
  113. ret = extcon_data->irq;
  114. goto err;
  115. }
  116. ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
  117. pdata->irq_flags, pdev->name,
  118. extcon_data);
  119. if (ret < 0)
  120. goto err;
  121. platform_set_drvdata(pdev, extcon_data);
  122. /* Perform initial detection */
  123. gpio_extcon_work(&extcon_data->work.work);
  124. return 0;
  125. err:
  126. extcon_dev_unregister(&extcon_data->edev);
  127. return ret;
  128. }
  129. static int gpio_extcon_remove(struct platform_device *pdev)
  130. {
  131. struct gpio_extcon_data *extcon_data = platform_get_drvdata(pdev);
  132. cancel_delayed_work_sync(&extcon_data->work);
  133. free_irq(extcon_data->irq, extcon_data);
  134. extcon_dev_unregister(&extcon_data->edev);
  135. return 0;
  136. }
  137. static struct platform_driver gpio_extcon_driver = {
  138. .probe = gpio_extcon_probe,
  139. .remove = gpio_extcon_remove,
  140. .driver = {
  141. .name = "extcon-gpio",
  142. .owner = THIS_MODULE,
  143. },
  144. };
  145. module_platform_driver(gpio_extcon_driver);
  146. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  147. MODULE_DESCRIPTION("GPIO extcon driver");
  148. MODULE_LICENSE("GPL");