extcon-gpio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/extcon.h>
  23. #include <linux/extcon/extcon-gpio.h>
  24. #include <linux/gpio.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/workqueue.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 int gpio_extcon_probe(struct platform_device *pdev)
  62. {
  63. struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
  64. struct gpio_extcon_data *extcon_data;
  65. int ret;
  66. if (!pdata)
  67. return -EBUSY;
  68. if (!pdata->irq_flags) {
  69. dev_err(&pdev->dev, "IRQ flag is not specified.\n");
  70. return -EINVAL;
  71. }
  72. extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
  73. GFP_KERNEL);
  74. if (!extcon_data)
  75. return -ENOMEM;
  76. extcon_data->edev = devm_extcon_dev_allocate(&pdev->dev, NULL);
  77. if (IS_ERR(extcon_data->edev)) {
  78. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  79. return -ENOMEM;
  80. }
  81. extcon_data->gpio = pdata->gpio;
  82. extcon_data->gpio_active_low = pdata->gpio_active_low;
  83. extcon_data->state_on = pdata->state_on;
  84. extcon_data->state_off = pdata->state_off;
  85. extcon_data->check_on_resume = pdata->check_on_resume;
  86. ret = devm_gpio_request_one(&pdev->dev, extcon_data->gpio, GPIOF_DIR_IN,
  87. pdev->name);
  88. if (ret < 0)
  89. return ret;
  90. if (pdata->debounce) {
  91. ret = gpio_set_debounce(extcon_data->gpio,
  92. pdata->debounce * 1000);
  93. if (ret < 0)
  94. extcon_data->debounce_jiffies =
  95. msecs_to_jiffies(pdata->debounce);
  96. }
  97. ret = devm_extcon_dev_register(&pdev->dev, extcon_data->edev);
  98. if (ret < 0)
  99. return ret;
  100. INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
  101. extcon_data->irq = gpio_to_irq(extcon_data->gpio);
  102. if (extcon_data->irq < 0)
  103. return extcon_data->irq;
  104. ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
  105. pdata->irq_flags, pdev->name,
  106. extcon_data);
  107. if (ret < 0)
  108. return ret;
  109. platform_set_drvdata(pdev, extcon_data);
  110. /* Perform initial detection */
  111. gpio_extcon_work(&extcon_data->work.work);
  112. return 0;
  113. }
  114. static int gpio_extcon_remove(struct platform_device *pdev)
  115. {
  116. struct gpio_extcon_data *extcon_data = platform_get_drvdata(pdev);
  117. cancel_delayed_work_sync(&extcon_data->work);
  118. free_irq(extcon_data->irq, extcon_data);
  119. return 0;
  120. }
  121. #ifdef CONFIG_PM_SLEEP
  122. static int gpio_extcon_resume(struct device *dev)
  123. {
  124. struct gpio_extcon_data *extcon_data;
  125. extcon_data = dev_get_drvdata(dev);
  126. if (extcon_data->check_on_resume)
  127. queue_delayed_work(system_power_efficient_wq,
  128. &extcon_data->work, extcon_data->debounce_jiffies);
  129. return 0;
  130. }
  131. #endif
  132. static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
  133. static struct platform_driver gpio_extcon_driver = {
  134. .probe = gpio_extcon_probe,
  135. .remove = gpio_extcon_remove,
  136. .driver = {
  137. .name = "extcon-gpio",
  138. .pm = &gpio_extcon_pm_ops,
  139. },
  140. };
  141. module_platform_driver(gpio_extcon_driver);
  142. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  143. MODULE_DESCRIPTION("GPIO extcon driver");
  144. MODULE_LICENSE("GPL");