extcon-intel-int3496.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel INT3496 ACPI device extcon driver
  4. *
  5. * Copyright (c) 2016 Hans de Goede <[email protected]>
  6. *
  7. * Based on android x86 kernel code which is:
  8. *
  9. * Copyright (c) 2014, Intel Corporation.
  10. * Author: David Cohen <[email protected]>
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/devm-helpers.h>
  14. #include <linux/extcon-provider.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #define INT3496_GPIO_USB_ID 0
  21. #define INT3496_GPIO_VBUS_EN 1
  22. #define INT3496_GPIO_USB_MUX 2
  23. #define DEBOUNCE_TIME msecs_to_jiffies(50)
  24. struct int3496_data {
  25. struct device *dev;
  26. struct extcon_dev *edev;
  27. struct delayed_work work;
  28. struct gpio_desc *gpio_usb_id;
  29. struct gpio_desc *gpio_vbus_en;
  30. struct gpio_desc *gpio_usb_mux;
  31. struct regulator *vbus_boost;
  32. int usb_id_irq;
  33. bool vbus_boost_enabled;
  34. };
  35. static const unsigned int int3496_cable[] = {
  36. EXTCON_USB_HOST,
  37. EXTCON_NONE,
  38. };
  39. static const struct acpi_gpio_params id_gpios = { INT3496_GPIO_USB_ID, 0, false };
  40. static const struct acpi_gpio_params vbus_gpios = { INT3496_GPIO_VBUS_EN, 0, false };
  41. static const struct acpi_gpio_params mux_gpios = { INT3496_GPIO_USB_MUX, 0, false };
  42. static const struct acpi_gpio_mapping acpi_int3496_default_gpios[] = {
  43. /*
  44. * Some platforms have a bug in ACPI GPIO description making IRQ
  45. * GPIO to be output only. Ask the GPIO core to ignore this limit.
  46. */
  47. { "id-gpios", &id_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
  48. { "vbus-gpios", &vbus_gpios, 1 },
  49. { "mux-gpios", &mux_gpios, 1 },
  50. { },
  51. };
  52. static void int3496_set_vbus_boost(struct int3496_data *data, bool enable)
  53. {
  54. int ret;
  55. if (IS_ERR_OR_NULL(data->vbus_boost))
  56. return;
  57. if (data->vbus_boost_enabled == enable)
  58. return;
  59. if (enable)
  60. ret = regulator_enable(data->vbus_boost);
  61. else
  62. ret = regulator_disable(data->vbus_boost);
  63. if (ret == 0)
  64. data->vbus_boost_enabled = enable;
  65. else
  66. dev_err(data->dev, "Error updating Vbus boost regulator: %d\n", ret);
  67. }
  68. static void int3496_do_usb_id(struct work_struct *work)
  69. {
  70. struct int3496_data *data =
  71. container_of(work, struct int3496_data, work.work);
  72. int id = gpiod_get_value_cansleep(data->gpio_usb_id);
  73. /* id == 1: PERIPHERAL, id == 0: HOST */
  74. dev_dbg(data->dev, "Connected %s cable\n", id ? "PERIPHERAL" : "HOST");
  75. /*
  76. * Peripheral: set USB mux to peripheral and disable VBUS
  77. * Host: set USB mux to host and enable VBUS
  78. */
  79. if (!IS_ERR(data->gpio_usb_mux))
  80. gpiod_direction_output(data->gpio_usb_mux, id);
  81. if (!IS_ERR(data->gpio_vbus_en))
  82. gpiod_direction_output(data->gpio_vbus_en, !id);
  83. else
  84. int3496_set_vbus_boost(data, !id);
  85. extcon_set_state_sync(data->edev, EXTCON_USB_HOST, !id);
  86. }
  87. static irqreturn_t int3496_thread_isr(int irq, void *priv)
  88. {
  89. struct int3496_data *data = priv;
  90. /* Let the pin settle before processing it */
  91. mod_delayed_work(system_wq, &data->work, DEBOUNCE_TIME);
  92. return IRQ_HANDLED;
  93. }
  94. static int int3496_probe(struct platform_device *pdev)
  95. {
  96. struct device *dev = &pdev->dev;
  97. struct int3496_data *data;
  98. int ret;
  99. if (has_acpi_companion(dev)) {
  100. ret = devm_acpi_dev_add_driver_gpios(dev, acpi_int3496_default_gpios);
  101. if (ret) {
  102. dev_err(dev, "can't add GPIO ACPI mapping\n");
  103. return ret;
  104. }
  105. }
  106. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  107. if (!data)
  108. return -ENOMEM;
  109. data->dev = dev;
  110. ret = devm_delayed_work_autocancel(dev, &data->work, int3496_do_usb_id);
  111. if (ret)
  112. return ret;
  113. data->gpio_usb_id =
  114. devm_gpiod_get(dev, "id", GPIOD_IN | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  115. if (IS_ERR(data->gpio_usb_id)) {
  116. ret = PTR_ERR(data->gpio_usb_id);
  117. dev_err(dev, "can't request USB ID GPIO: %d\n", ret);
  118. return ret;
  119. }
  120. data->usb_id_irq = gpiod_to_irq(data->gpio_usb_id);
  121. if (data->usb_id_irq < 0) {
  122. dev_err(dev, "can't get USB ID IRQ: %d\n", data->usb_id_irq);
  123. return data->usb_id_irq;
  124. }
  125. data->gpio_vbus_en = devm_gpiod_get(dev, "vbus", GPIOD_ASIS);
  126. if (IS_ERR(data->gpio_vbus_en)) {
  127. dev_dbg(dev, "can't request VBUS EN GPIO\n");
  128. data->vbus_boost = devm_regulator_get_optional(dev, "vbus");
  129. }
  130. data->gpio_usb_mux = devm_gpiod_get(dev, "mux", GPIOD_ASIS);
  131. if (IS_ERR(data->gpio_usb_mux))
  132. dev_dbg(dev, "can't request USB MUX GPIO\n");
  133. /* register extcon device */
  134. data->edev = devm_extcon_dev_allocate(dev, int3496_cable);
  135. if (IS_ERR(data->edev))
  136. return -ENOMEM;
  137. ret = devm_extcon_dev_register(dev, data->edev);
  138. if (ret < 0) {
  139. dev_err(dev, "can't register extcon device: %d\n", ret);
  140. return ret;
  141. }
  142. ret = devm_request_threaded_irq(dev, data->usb_id_irq,
  143. NULL, int3496_thread_isr,
  144. IRQF_SHARED | IRQF_ONESHOT |
  145. IRQF_TRIGGER_RISING |
  146. IRQF_TRIGGER_FALLING,
  147. dev_name(dev), data);
  148. if (ret < 0) {
  149. dev_err(dev, "can't request IRQ for USB ID GPIO: %d\n", ret);
  150. return ret;
  151. }
  152. /* process id-pin so that we start with the right status */
  153. queue_delayed_work(system_wq, &data->work, 0);
  154. flush_delayed_work(&data->work);
  155. platform_set_drvdata(pdev, data);
  156. return 0;
  157. }
  158. static const struct acpi_device_id int3496_acpi_match[] = {
  159. { "INT3496" },
  160. { }
  161. };
  162. MODULE_DEVICE_TABLE(acpi, int3496_acpi_match);
  163. static const struct platform_device_id int3496_ids[] = {
  164. { .name = "intel-int3496" },
  165. {},
  166. };
  167. MODULE_DEVICE_TABLE(platform, int3496_ids);
  168. static struct platform_driver int3496_driver = {
  169. .driver = {
  170. .name = "intel-int3496",
  171. .acpi_match_table = int3496_acpi_match,
  172. },
  173. .probe = int3496_probe,
  174. .id_table = int3496_ids,
  175. };
  176. module_platform_driver(int3496_driver);
  177. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  178. MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver");
  179. MODULE_LICENSE("GPL v2");