extcon-gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * extcon_gpio.c - Single-state GPIO extcon driver based on extcon class
  4. *
  5. * Copyright (C) 2008 Google, Inc.
  6. * Author: Mike Lockwood <lockwood@android.com>
  7. *
  8. * Modified by MyungJoo Ham <myungjoo.ham@samsung.com> to support extcon
  9. * (originally switch class is supported)
  10. */
  11. #include <linux/extcon-provider.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/workqueue.h>
  20. /**
  21. * struct gpio_extcon_data - A simple GPIO-controlled extcon device state container.
  22. * @edev: Extcon device.
  23. * @work: Work fired by the interrupt.
  24. * @debounce_jiffies: Number of jiffies to wait for the GPIO to stabilize, from the debounce
  25. * value.
  26. * @gpiod: GPIO descriptor for this external connector.
  27. * @extcon_id: The unique id of specific external connector.
  28. * @debounce: Debounce time for GPIO IRQ in ms.
  29. * @check_on_resume: Boolean describing whether to check the state of gpio
  30. * while resuming from sleep.
  31. */
  32. struct gpio_extcon_data {
  33. struct extcon_dev *edev;
  34. struct delayed_work work;
  35. unsigned long debounce_jiffies;
  36. struct gpio_desc *gpiod;
  37. unsigned int extcon_id;
  38. unsigned long debounce;
  39. bool check_on_resume;
  40. };
  41. static void gpio_extcon_work(struct work_struct *work)
  42. {
  43. int state;
  44. struct gpio_extcon_data *data =
  45. container_of(to_delayed_work(work), struct gpio_extcon_data,
  46. work);
  47. state = gpiod_get_value_cansleep(data->gpiod);
  48. extcon_set_state_sync(data->edev, data->extcon_id, state);
  49. }
  50. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  51. {
  52. struct gpio_extcon_data *data = dev_id;
  53. queue_delayed_work(system_power_efficient_wq, &data->work,
  54. data->debounce_jiffies);
  55. return IRQ_HANDLED;
  56. }
  57. static int gpio_extcon_probe(struct platform_device *pdev)
  58. {
  59. struct gpio_extcon_data *data;
  60. struct device *dev = &pdev->dev;
  61. unsigned long irq_flags;
  62. int irq;
  63. int ret;
  64. data = devm_kzalloc(dev, sizeof(struct gpio_extcon_data), GFP_KERNEL);
  65. if (!data)
  66. return -ENOMEM;
  67. /*
  68. * FIXME: extcon_id represents the unique identifier of external
  69. * connectors such as EXTCON_USB, EXTCON_DISP_HDMI and so on. extcon_id
  70. * is necessary to register the extcon device. But, it's not yet
  71. * developed to get the extcon id from device-tree or others.
  72. * On later, it have to be solved.
  73. */
  74. if (data->extcon_id > EXTCON_NONE)
  75. return -EINVAL;
  76. data->gpiod = devm_gpiod_get(dev, "extcon", GPIOD_IN);
  77. if (IS_ERR(data->gpiod))
  78. return PTR_ERR(data->gpiod);
  79. irq = gpiod_to_irq(data->gpiod);
  80. if (irq <= 0)
  81. return irq;
  82. /*
  83. * It is unlikely that this is an acknowledged interrupt that goes
  84. * away after handling, what we are looking for are falling edges
  85. * if the signal is active low, and rising edges if the signal is
  86. * active high.
  87. */
  88. if (gpiod_is_active_low(data->gpiod))
  89. irq_flags = IRQF_TRIGGER_FALLING;
  90. else
  91. irq_flags = IRQF_TRIGGER_RISING;
  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, irq,
  107. gpio_irq_handler, 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");