extcon-gpio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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-provider.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/workqueue.h>
  28. /**
  29. * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container.
  30. * @edev: Extcon device.
  31. * @irq: Interrupt line for the external connector.
  32. * @work: Work fired by the interrupt.
  33. * @debounce_jiffies: Number of jiffies to wait for the GPIO to stabilize, from the debounce
  34. * value.
  35. * @gpiod: GPIO descriptor for this external connector.
  36. * @extcon_id: The unique id of specific external connector.
  37. * @debounce: Debounce time for GPIO IRQ in ms.
  38. * @irq_flags: IRQ Flags (e.g., IRQF_TRIGGER_LOW).
  39. * @check_on_resume: Boolean describing whether to check the state of gpio
  40. * while resuming from sleep.
  41. */
  42. struct gpio_extcon_data {
  43. struct extcon_dev *edev;
  44. int irq;
  45. struct delayed_work work;
  46. unsigned long debounce_jiffies;
  47. struct gpio_desc *gpiod;
  48. unsigned int extcon_id;
  49. unsigned long debounce;
  50. unsigned long irq_flags;
  51. bool check_on_resume;
  52. };
  53. static void gpio_extcon_work(struct work_struct *work)
  54. {
  55. int state;
  56. struct gpio_extcon_data *data =
  57. container_of(to_delayed_work(work), struct gpio_extcon_data,
  58. work);
  59. state = gpiod_get_value_cansleep(data->gpiod);
  60. extcon_set_state_sync(data->edev, data->extcon_id, state);
  61. }
  62. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  63. {
  64. struct gpio_extcon_data *data = dev_id;
  65. queue_delayed_work(system_power_efficient_wq, &data->work,
  66. data->debounce_jiffies);
  67. return IRQ_HANDLED;
  68. }
  69. static int gpio_extcon_probe(struct platform_device *pdev)
  70. {
  71. struct gpio_extcon_data *data;
  72. struct device *dev = &pdev->dev;
  73. int ret;
  74. data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL);
  75. if (!data)
  76. return -ENOMEM;
  77. /*
  78. * FIXME: extcon_id represents the unique identifier of external
  79. * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id
  80. * is necessary to register the extcon device. But, it's not yet
  81. * developed to get the extcon id from device-tree or others.
  82. * On later, it have to be solved.
  83. */
  84. if (!data->irq_flags || data->extcon_id > EXTCON_NONE)
  85. return -EINVAL;
  86. data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN);
  87. if (IS_ERR(data->gpiod))
  88. return PTR_ERR(data->gpiod);
  89. data->irq = gpiod_to_irq(data->gpiod);
  90. if (data->irq <= 0)
  91. return data->irq;
  92. /* Allocate the memory of extcon devie and register extcon device */
  93. data->edev = devm_extcon_dev_allocate(dev, &data->extcon_id);
  94. if (IS_ERR(data->edev)) {
  95. dev_err(dev, "failed to allocate extcon device\n");
  96. return -ENOMEM;
  97. }
  98. ret = devm_extcon_dev_register(dev, data->edev);
  99. if (ret < 0)
  100. return ret;
  101. INIT_DELAYED_WORK(&data->work, gpio_extcon_work);
  102. /*
  103. * Request the interrupt of gpio to detect whether external connector
  104. * is attached or detached.
  105. */
  106. ret = devm_request_any_context_irq(dev, data->irq,
  107. gpio_irq_handler, data->irq_flags,
  108. pdev->name, data);
  109. if (ret < 0)
  110. return ret;
  111. platform_set_drvdata(pdev, data);
  112. /* Perform initial detection */
  113. gpio_extcon_work(&data->work.work);
  114. return 0;
  115. }
  116. static int gpio_extcon_remove(struct platform_device *pdev)
  117. {
  118. struct gpio_extcon_data *data = platform_get_drvdata(pdev);
  119. cancel_delayed_work_sync(&data->work);
  120. return 0;
  121. }
  122. #ifdef CONFIG_PM_SLEEP
  123. static int gpio_extcon_resume(struct device *dev)
  124. {
  125. struct gpio_extcon_data *data;
  126. data = dev_get_drvdata(dev);
  127. if (data->check_on_resume)
  128. queue_delayed_work(system_power_efficient_wq,
  129. &data->work, data->debounce_jiffies);
  130. return 0;
  131. }
  132. #endif
  133. static SIMPLE_DEV_PM_OPS(gpio_extcon_pm_ops, NULL, gpio_extcon_resume);
  134. static struct platform_driver gpio_extcon_driver = {
  135. .probe = gpio_extcon_probe,
  136. .remove = gpio_extcon_remove,
  137. .driver = {
  138. .name = "extcon-gpio",
  139. .pm = &gpio_extcon_pm_ops,
  140. },
  141. };
  142. module_platform_driver(gpio_extcon_driver);
  143. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  144. MODULE_DESCRIPTION("GPIO extcon driver");
  145. MODULE_LICENSE("GPL");