extcon-intel-int3496.c 5.3 KB

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