extcon-gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Author: Mike Lockwood <lockwood@android.com>
  6. *
  7. * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
  8. * (originally switch class is supported)
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/extcon.h>
  20. #include <linux/extcon/extcon-gpio.h>
  21. #include <linux/gpio.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/workqueue.h>
  30. struct gpio_extcon_data {
  31. struct extcon_dev *edev;
  32. int irq;
  33. struct delayed_work work;
  34. unsigned long debounce_jiffies;
  35. struct gpio_desc *id_gpiod;
  36. struct gpio_extcon_pdata *pdata;
  37. };
  38. static void gpio_extcon_work(struct work_struct *work)
  39. {
  40. int state;
  41. struct gpio_extcon_data *data =
  42. container_of(to_delayed_work(work), struct gpio_extcon_data,
  43. work);
  44. state = gpiod_get_value_cansleep(data->id_gpiod);
  45. if (data->pdata->gpio_active_low)
  46. state = !state;
  47. extcon_set_state(data->edev, state);
  48. }
  49. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  50. {
  51. struct gpio_extcon_data *data = dev_id;
  52. queue_delayed_work(system_power_efficient_wq, &data->work,
  53. data->debounce_jiffies);
  54. return IRQ_HANDLED;
  55. }
  56. static int gpio_extcon_init(struct device *dev, struct gpio_extcon_data *data)
  57. {
  58. struct gpio_extcon_pdata *pdata = data->pdata;
  59. int ret;
  60. ret = devm_gpio_request_one(dev, pdata->gpio, GPIOF_DIR_IN,
  61. dev_name(dev));
  62. if (ret < 0)
  63. return ret;
  64. data->id_gpiod = gpio_to_desc(pdata->gpio);
  65. if (!data->id_gpiod)
  66. return -EINVAL;
  67. if (pdata->debounce) {
  68. ret = gpiod_set_debounce(data->id_gpiod,
  69. pdata->debounce * 1000);
  70. if (ret < 0)
  71. data->debounce_jiffies =
  72. msecs_to_jiffies(pdata->debounce);
  73. }
  74. data->irq = gpiod_to_irq(data->id_gpiod);
  75. if (data->irq < 0)
  76. return data->irq;
  77. return 0;
  78. }
  79. static int gpio_extcon_probe(struct platform_device *pdev)
  80. {
  81. struct gpio_extcon_pdata *pdata = dev_get_platdata(&pdev->dev);
  82. struct gpio_extcon_data *data;
  83. int ret;
  84. if (!pdata)
  85. return -EBUSY;
  86. if (!pdata->irq_flags || pdata->extcon_id > EXTCON_NONE)
  87. return -EINVAL;
  88. data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_extcon_data),
  89. GFP_KERNEL);
  90. if (!data)
  91. return -ENOMEM;
  92. data->pdata = pdata;
  93. /* Initialize the gpio */
  94. ret = gpio_extcon_init(&pdev->dev, data);
  95. if (ret < 0)
  96. return ret;
  97. /* Allocate the memory of extcon devie and register extcon device */
  98. data->edev = devm_extcon_dev_allocate(&pdev->dev, &pdata->extcon_id);
  99. if (IS_ERR(data->edev)) {
  100. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  101. return -ENOMEM;
  102. }
  103. ret = devm_extcon_dev_register(&pdev->dev, data->edev);
  104. if (ret < 0)
  105. return ret;
  106. INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
  107. /*
  108. * Request the interrput of gpio to detect whether external connector
  109. * is attached or detached.
  110. */
  111. ret = devm_request_any_context_irq(&pdev->dev, data->irq,
  112. gpio_irq_handler, pdata->irq_flags,
  113. pdev->name, data);
  114. if (ret < 0)
  115. return ret;
  116. platform_set_drvdata(pdev, data);
  117. /* Perform initial detection */
  118. gpio_extcon_work(&data->work.work);
  119. return 0;
  120. }
  121. static int gpio_extcon_remove(struct platform_device *pdev)
  122. {
  123. struct gpio_extcon_data *data = platform_get_drvdata(pdev);
  124. cancel_delayed_work_sync(&data->work);
  125. return 0;
  126. }
  127. #ifdef CONFIG_PM_SLEEP
  128. static int gpio_extcon_resume(struct device *dev)
  129. {
  130. struct gpio_extcon_data *data;
  131. data = dev_get_drvdata(dev);
  132. if (data->pdata->check_on_resume)
  133. queue_delayed_work(system_power_efficient_wq,
  134. &data->work, data->debounce_jiffies);
  135. return 0;
  136. }
  137. #endif
  138. static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
  139. static struct platform_driver gpio_extcon_driver = {
  140. .probe = gpio_extcon_probe,
  141. .remove = gpio_extcon_remove,
  142. .driver = {
  143. .name = "extcon-gpio",
  144. .pm = &gpio_extcon_pm_ops,
  145. },
  146. };
  147. module_platform_driver(gpio_extcon_driver);
  148. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  149. MODULE_DESCRIPTION("GPIO extcon driver");
  150. MODULE_LICENSE("GPL");