extcon-gpio.c 4.4 KB

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