extcon_gpio.c 4.2 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/extcon.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/gpio.h>
  31. #include <linux/extcon.h>
  32. #include <linux/extcon/extcon_gpio.h>
  33. struct gpio_extcon_data {
  34. struct extcon_dev edev;
  35. unsigned gpio;
  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. extcon_set_state(&data->edev, state);
  50. }
  51. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  52. {
  53. struct gpio_extcon_data *extcon_data = dev_id;
  54. schedule_delayed_work(&extcon_data->work,
  55. extcon_data->debounce_jiffies);
  56. return IRQ_HANDLED;
  57. }
  58. static ssize_t extcon_gpio_print_state(struct extcon_dev *edev, char *buf)
  59. {
  60. struct gpio_extcon_data *extcon_data =
  61. container_of(edev, struct gpio_extcon_data, edev);
  62. const char *state;
  63. if (extcon_get_state(edev))
  64. state = extcon_data->state_on;
  65. else
  66. state = extcon_data->state_off;
  67. if (state)
  68. return sprintf(buf, "%s\n", state);
  69. return -EINVAL;
  70. }
  71. static int __devinit gpio_extcon_probe(struct platform_device *pdev)
  72. {
  73. struct gpio_extcon_platform_data *pdata = pdev->dev.platform_data;
  74. struct gpio_extcon_data *extcon_data;
  75. int ret = 0;
  76. if (!pdata)
  77. return -EBUSY;
  78. if (!pdata->irq_flags) {
  79. dev_err(&pdev->dev, "IRQ flag is not specified.\n");
  80. return -EINVAL;
  81. }
  82. extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
  83. GFP_KERNEL);
  84. if (!extcon_data)
  85. return -ENOMEM;
  86. extcon_data->edev.name = pdata->name;
  87. extcon_data->gpio = pdata->gpio;
  88. extcon_data->state_on = pdata->state_on;
  89. extcon_data->state_off = pdata->state_off;
  90. if (pdata->state_on && pdata->state_off)
  91. extcon_data->edev.print_state = extcon_gpio_print_state;
  92. extcon_data->debounce_jiffies = msecs_to_jiffies(pdata->debounce);
  93. ret = extcon_dev_register(&extcon_data->edev, &pdev->dev);
  94. if (ret < 0)
  95. return ret;
  96. ret = gpio_request_one(extcon_data->gpio, GPIOF_DIR_IN, 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 __devexit 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 = __devexit_p(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");