blob: fb527c23639e80f689b0f7d2120042d6b62906bb [file] [log] [blame]
Andy Shevchenko2e464ff2018-08-27 18:35:57 +03001// SPDX-License-Identifier: GPL-2.0
David Cohen2f556bd2016-12-21 12:20:25 +01002/*
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>
David Cohen2f556bd2016-12-21 12:20:25 +010011 */
12
13#include <linux/acpi.h>
Matti Vaittinenf94a5be2021-03-23 15:57:08 +020014#include <linux/devm-helpers.h>
Chanwoo Choi176aa362017-09-21 12:11:24 +090015#include <linux/extcon-provider.h>
Wolfram Sangc355bb12018-04-10 14:43:02 +020016#include <linux/gpio/consumer.h>
David Cohen2f556bd2016-12-21 12:20:25 +010017#include <linux/interrupt.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20
21#define INT3496_GPIO_USB_ID 0
22#define INT3496_GPIO_VBUS_EN 1
23#define INT3496_GPIO_USB_MUX 2
24#define DEBOUNCE_TIME msecs_to_jiffies(50)
25
26struct int3496_data {
27 struct device *dev;
28 struct extcon_dev *edev;
29 struct delayed_work work;
30 struct gpio_desc *gpio_usb_id;
31 struct gpio_desc *gpio_vbus_en;
32 struct gpio_desc *gpio_usb_mux;
33 int usb_id_irq;
34};
35
36static const unsigned int int3496_cable[] = {
37 EXTCON_USB_HOST,
38 EXTCON_NONE,
39};
40
Andy Shevchenko8cb2cba2017-02-24 14:35:55 +020041static const struct acpi_gpio_params id_gpios = { INT3496_GPIO_USB_ID, 0, false };
42static const struct acpi_gpio_params vbus_gpios = { INT3496_GPIO_VBUS_EN, 0, false };
43static const struct acpi_gpio_params mux_gpios = { INT3496_GPIO_USB_MUX, 0, false };
44
45static const struct acpi_gpio_mapping acpi_int3496_default_gpios[] = {
Andy Shevchenkoeca0f132018-02-28 18:22:08 +020046 /*
47 * Some platforms have a bug in ACPI GPIO description making IRQ
48 * GPIO to be output only. Ask the GPIO core to ignore this limit.
49 */
50 { "id-gpios", &id_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
Andy Shevchenko8cb2cba2017-02-24 14:35:55 +020051 { "vbus-gpios", &vbus_gpios, 1 },
52 { "mux-gpios", &mux_gpios, 1 },
53 { },
54};
55
David Cohen2f556bd2016-12-21 12:20:25 +010056static void int3496_do_usb_id(struct work_struct *work)
57{
58 struct int3496_data *data =
59 container_of(work, struct int3496_data, work.work);
60 int id = gpiod_get_value_cansleep(data->gpio_usb_id);
61
62 /* id == 1: PERIPHERAL, id == 0: HOST */
63 dev_dbg(data->dev, "Connected %s cable\n", id ? "PERIPHERAL" : "HOST");
64
65 /*
66 * Peripheral: set USB mux to peripheral and disable VBUS
67 * Host: set USB mux to host and enable VBUS
68 */
69 if (!IS_ERR(data->gpio_usb_mux))
70 gpiod_direction_output(data->gpio_usb_mux, id);
71
72 if (!IS_ERR(data->gpio_vbus_en))
73 gpiod_direction_output(data->gpio_vbus_en, !id);
74
75 extcon_set_state_sync(data->edev, EXTCON_USB_HOST, !id);
76}
77
78static irqreturn_t int3496_thread_isr(int irq, void *priv)
79{
80 struct int3496_data *data = priv;
81
82 /* Let the pin settle before processing it */
83 mod_delayed_work(system_wq, &data->work, DEBOUNCE_TIME);
84
85 return IRQ_HANDLED;
86}
87
88static int int3496_probe(struct platform_device *pdev)
89{
90 struct device *dev = &pdev->dev;
91 struct int3496_data *data;
92 int ret;
93
Andy Shevchenko1f4be242017-06-10 22:09:21 +030094 ret = devm_acpi_dev_add_driver_gpios(dev, acpi_int3496_default_gpios);
Andy Shevchenko8cb2cba2017-02-24 14:35:55 +020095 if (ret) {
96 dev_err(dev, "can't add GPIO ACPI mapping\n");
97 return ret;
98 }
99
David Cohen2f556bd2016-12-21 12:20:25 +0100100 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
101 if (!data)
102 return -ENOMEM;
103
104 data->dev = dev;
Matti Vaittinenf94a5be2021-03-23 15:57:08 +0200105 ret = devm_delayed_work_autocancel(dev, &data->work, int3496_do_usb_id);
106 if (ret)
107 return ret;
David Cohen2f556bd2016-12-21 12:20:25 +0100108
Hans de Goede408c5b42017-03-10 21:52:08 +0100109 data->gpio_usb_id = devm_gpiod_get(dev, "id", GPIOD_IN);
David Cohen2f556bd2016-12-21 12:20:25 +0100110 if (IS_ERR(data->gpio_usb_id)) {
111 ret = PTR_ERR(data->gpio_usb_id);
112 dev_err(dev, "can't request USB ID GPIO: %d\n", ret);
113 return ret;
114 }
115
116 data->usb_id_irq = gpiod_to_irq(data->gpio_usb_id);
Andy Shevchenkobafa6872017-02-23 12:31:54 +0200117 if (data->usb_id_irq < 0) {
David Cohen2f556bd2016-12-21 12:20:25 +0100118 dev_err(dev, "can't get USB ID IRQ: %d\n", data->usb_id_irq);
Andy Shevchenkobafa6872017-02-23 12:31:54 +0200119 return data->usb_id_irq;
David Cohen2f556bd2016-12-21 12:20:25 +0100120 }
121
Hans de Goede408c5b42017-03-10 21:52:08 +0100122 data->gpio_vbus_en = devm_gpiod_get(dev, "vbus", GPIOD_ASIS);
David Cohen2f556bd2016-12-21 12:20:25 +0100123 if (IS_ERR(data->gpio_vbus_en))
124 dev_info(dev, "can't request VBUS EN GPIO\n");
125
Hans de Goede408c5b42017-03-10 21:52:08 +0100126 data->gpio_usb_mux = devm_gpiod_get(dev, "mux", GPIOD_ASIS);
David Cohen2f556bd2016-12-21 12:20:25 +0100127 if (IS_ERR(data->gpio_usb_mux))
128 dev_info(dev, "can't request USB MUX GPIO\n");
129
130 /* register extcon device */
131 data->edev = devm_extcon_dev_allocate(dev, int3496_cable);
132 if (IS_ERR(data->edev))
133 return -ENOMEM;
134
135 ret = devm_extcon_dev_register(dev, data->edev);
136 if (ret < 0) {
137 dev_err(dev, "can't register extcon device: %d\n", ret);
138 return ret;
139 }
140
141 ret = devm_request_threaded_irq(dev, data->usb_id_irq,
142 NULL, int3496_thread_isr,
143 IRQF_SHARED | IRQF_ONESHOT |
144 IRQF_TRIGGER_RISING |
145 IRQF_TRIGGER_FALLING,
146 dev_name(dev), data);
147 if (ret < 0) {
148 dev_err(dev, "can't request IRQ for USB ID GPIO: %d\n", ret);
149 return ret;
150 }
151
Hans de Goede04343522018-02-13 20:25:50 +0100152 /* process id-pin so that we start with the right status */
David Cohen2f556bd2016-12-21 12:20:25 +0100153 queue_delayed_work(system_wq, &data->work, 0);
Hans de Goede04343522018-02-13 20:25:50 +0100154 flush_delayed_work(&data->work);
David Cohen2f556bd2016-12-21 12:20:25 +0100155
156 platform_set_drvdata(pdev, data);
157
158 return 0;
159}
160
Arvind Yadavff890bc2017-07-06 22:25:56 +0530161static const struct acpi_device_id int3496_acpi_match[] = {
David Cohen2f556bd2016-12-21 12:20:25 +0100162 { "INT3496" },
163 { }
164};
165MODULE_DEVICE_TABLE(acpi, int3496_acpi_match);
166
167static struct platform_driver int3496_driver = {
168 .driver = {
169 .name = "intel-int3496",
170 .acpi_match_table = int3496_acpi_match,
171 },
172 .probe = int3496_probe,
David Cohen2f556bd2016-12-21 12:20:25 +0100173};
174
175module_platform_driver(int3496_driver);
176
177MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
178MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver");
Andy Shevchenko2e464ff2018-08-27 18:35:57 +0300179MODULE_LICENSE("GPL v2");