2 * Intel INT3496 ACPI device extcon driver
4 * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
6 * Based on android x86 kernel code which is:
8 * Copyright (c) 2014, Intel Corporation.
9 * Author: David Cohen <david.a.cohen@linux.intel.com>
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.
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.
21 #include <linux/acpi.h>
22 #include <linux/extcon-provider.h>
23 #include <linux/gpio.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
28 #define INT3496_GPIO_USB_ID 0
29 #define INT3496_GPIO_VBUS_EN 1
30 #define INT3496_GPIO_USB_MUX 2
31 #define DEBOUNCE_TIME msecs_to_jiffies(50)
35 struct extcon_dev
*edev
;
36 struct delayed_work work
;
37 struct gpio_desc
*gpio_usb_id
;
38 struct gpio_desc
*gpio_vbus_en
;
39 struct gpio_desc
*gpio_usb_mux
;
43 static const unsigned int int3496_cable
[] = {
48 static const struct acpi_gpio_params id_gpios
= { INT3496_GPIO_USB_ID
, 0, false };
49 static const struct acpi_gpio_params vbus_gpios
= { INT3496_GPIO_VBUS_EN
, 0, false };
50 static const struct acpi_gpio_params mux_gpios
= { INT3496_GPIO_USB_MUX
, 0, false };
52 static const struct acpi_gpio_mapping acpi_int3496_default_gpios
[] = {
54 * Some platforms have a bug in ACPI GPIO description making IRQ
55 * GPIO to be output only. Ask the GPIO core to ignore this limit.
57 { "id-gpios", &id_gpios
, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION
},
58 { "vbus-gpios", &vbus_gpios
, 1 },
59 { "mux-gpios", &mux_gpios
, 1 },
63 static void int3496_do_usb_id(struct work_struct
*work
)
65 struct int3496_data
*data
=
66 container_of(work
, struct int3496_data
, work
.work
);
67 int id
= gpiod_get_value_cansleep(data
->gpio_usb_id
);
69 /* id == 1: PERIPHERAL, id == 0: HOST */
70 dev_dbg(data
->dev
, "Connected %s cable\n", id
? "PERIPHERAL" : "HOST");
73 * Peripheral: set USB mux to peripheral and disable VBUS
74 * Host: set USB mux to host and enable VBUS
76 if (!IS_ERR(data
->gpio_usb_mux
))
77 gpiod_direction_output(data
->gpio_usb_mux
, id
);
79 if (!IS_ERR(data
->gpio_vbus_en
))
80 gpiod_direction_output(data
->gpio_vbus_en
, !id
);
82 extcon_set_state_sync(data
->edev
, EXTCON_USB_HOST
, !id
);
85 static irqreturn_t
int3496_thread_isr(int irq
, void *priv
)
87 struct int3496_data
*data
= priv
;
89 /* Let the pin settle before processing it */
90 mod_delayed_work(system_wq
, &data
->work
, DEBOUNCE_TIME
);
95 static int int3496_probe(struct platform_device
*pdev
)
97 struct device
*dev
= &pdev
->dev
;
98 struct int3496_data
*data
;
101 ret
= devm_acpi_dev_add_driver_gpios(dev
, acpi_int3496_default_gpios
);
103 dev_err(dev
, "can't add GPIO ACPI mapping\n");
107 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
112 INIT_DELAYED_WORK(&data
->work
, int3496_do_usb_id
);
114 data
->gpio_usb_id
= devm_gpiod_get(dev
, "id", GPIOD_IN
);
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
);
121 data
->usb_id_irq
= gpiod_to_irq(data
->gpio_usb_id
);
122 if (data
->usb_id_irq
< 0) {
123 dev_err(dev
, "can't get USB ID IRQ: %d\n", data
->usb_id_irq
);
124 return data
->usb_id_irq
;
127 data
->gpio_vbus_en
= devm_gpiod_get(dev
, "vbus", GPIOD_ASIS
);
128 if (IS_ERR(data
->gpio_vbus_en
))
129 dev_info(dev
, "can't request VBUS EN GPIO\n");
131 data
->gpio_usb_mux
= devm_gpiod_get(dev
, "mux", GPIOD_ASIS
);
132 if (IS_ERR(data
->gpio_usb_mux
))
133 dev_info(dev
, "can't request USB MUX GPIO\n");
135 /* register extcon device */
136 data
->edev
= devm_extcon_dev_allocate(dev
, int3496_cable
);
137 if (IS_ERR(data
->edev
))
140 ret
= devm_extcon_dev_register(dev
, data
->edev
);
142 dev_err(dev
, "can't register extcon device: %d\n", ret
);
146 ret
= devm_request_threaded_irq(dev
, data
->usb_id_irq
,
147 NULL
, int3496_thread_isr
,
148 IRQF_SHARED
| IRQF_ONESHOT
|
149 IRQF_TRIGGER_RISING
|
150 IRQF_TRIGGER_FALLING
,
151 dev_name(dev
), data
);
153 dev_err(dev
, "can't request IRQ for USB ID GPIO: %d\n", ret
);
157 /* process id-pin so that we start with the right status */
158 queue_delayed_work(system_wq
, &data
->work
, 0);
159 flush_delayed_work(&data
->work
);
161 platform_set_drvdata(pdev
, data
);
166 static int int3496_remove(struct platform_device
*pdev
)
168 struct int3496_data
*data
= platform_get_drvdata(pdev
);
170 devm_free_irq(&pdev
->dev
, data
->usb_id_irq
, data
);
171 cancel_delayed_work_sync(&data
->work
);
176 static const struct acpi_device_id int3496_acpi_match
[] = {
180 MODULE_DEVICE_TABLE(acpi
, int3496_acpi_match
);
182 static struct platform_driver int3496_driver
= {
184 .name
= "intel-int3496",
185 .acpi_match_table
= int3496_acpi_match
,
187 .probe
= int3496_probe
,
188 .remove
= int3496_remove
,
191 module_platform_driver(int3496_driver
);
193 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
194 MODULE_DESCRIPTION("Intel INT3496 ACPI device extcon driver");
195 MODULE_LICENSE("GPL");