extcon-intel-int3496.c 4.9 KB

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