extcon-gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 ssize_t extcon_gpio_print_state(struct extcon_dev *edev, char *buf)
  62. {
  63. struct device *dev = edev->dev.parent;
  64. struct gpio_extcon_data *extcon_data = dev_get_drvdata(dev);
  65. const char *state;
  66. if (extcon_get_state(edev))
  67. state = extcon_data->state_on;
  68. else
  69. state = extcon_data->state_off;
  70. if (state)
  71. return sprintf(buf, "%s\n", state);
  72. return -EINVAL;
  73. }
  74. static int gpio_extcon_probe(struct platform_device *pdev)
  75. {
  76. struct gpio_extcon_platform_data *pdata = dev_get_platdata(&pdev->dev);
  77. struct gpio_extcon_data *extcon_data;
  78. int ret;
  79. if (!pdata)
  80. return -EBUSY;
  81. if (!pdata->irq_flags) {
  82. dev_err(&pdev->dev, "IRQ flag is not specified.\n");
  83. return -EINVAL;
  84. }
  85. extcon_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
  86. GFP_KERNEL);
  87. if (!extcon_data)
  88. return -ENOMEM;
  89. extcon_data->edev = devm_extcon_dev_allocate(&pdev->dev, NULL);
  90. if (IS_ERR(extcon_data->edev)) {
  91. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  92. return -ENOMEM;
  93. }
  94. extcon_data->gpio = pdata->gpio;
  95. extcon_data->gpio_active_low = pdata->gpio_active_low;
  96. extcon_data->state_on = pdata->state_on;
  97. extcon_data->state_off = pdata->state_off;
  98. extcon_data->check_on_resume = pdata->check_on_resume;
  99. if (pdata->state_on && pdata->state_off)
  100. extcon_data->edev->print_state = extcon_gpio_print_state;
  101. ret = devm_gpio_request_one(&pdev->dev, extcon_data->gpio, GPIOF_DIR_IN,
  102. pdev->name);
  103. if (ret < 0)
  104. return ret;
  105. if (pdata->debounce) {
  106. ret = gpio_set_debounce(extcon_data->gpio,
  107. pdata->debounce * 1000);
  108. if (ret < 0)
  109. extcon_data->debounce_jiffies =
  110. msecs_to_jiffies(pdata->debounce);
  111. }
  112. ret = devm_extcon_dev_register(&pdev->dev, extcon_data->edev);
  113. if (ret < 0)
  114. return ret;
  115. INIT_DELAYED_WORK(&extcon_data->work, gpio_extcon_work);
  116. extcon_data->irq = gpio_to_irq(extcon_data->gpio);
  117. if (extcon_data->irq < 0)
  118. return extcon_data->irq;
  119. ret = request_any_context_irq(extcon_data->irq, gpio_irq_handler,
  120. pdata->irq_flags, pdev->name,
  121. extcon_data);
  122. if (ret < 0)
  123. return ret;
  124. platform_set_drvdata(pdev, extcon_data);
  125. /* Perform initial detection */
  126. gpio_extcon_work(&extcon_data->work.work);
  127. return 0;
  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. return 0;
  135. }
  136. #ifdef CONFIG_PM_SLEEP
  137. static int gpio_extcon_resume(struct device *dev)
  138. {
  139. struct gpio_extcon_data *extcon_data;
  140. extcon_data = dev_get_drvdata(dev);
  141. if (extcon_data->check_on_resume)
  142. queue_delayed_work(system_power_efficient_wq,
  143. &extcon_data->work, extcon_data->debounce_jiffies);
  144. return 0;
  145. }
  146. #endif
  147. static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
  148. static struct platform_driver gpio_extcon_driver = {
  149. .probe = gpio_extcon_probe,
  150. .remove = gpio_extcon_remove,
  151. .driver = {
  152. .name = "extcon-gpio",
  153. .pm = &gpio_extcon_pm_ops,
  154. },
  155. };
  156. module_platform_driver(gpio_extcon_driver);
  157. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  158. MODULE_DESCRIPTION("GPIO extcon driver");
  159. MODULE_LICENSE("GPL");