extcon-gpio.c 4.1 KB

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