2 * drivers/extcon/extcon-usb-gpio.c - USB GPIO extcon driver
4 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
5 * Author: Roger Quadros <rogerq@ti.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/extcon.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of_gpio.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/workqueue.h>
29 #include <linux/acpi.h>
30 #include <linux/pinctrl/consumer.h>
32 #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
34 struct usb_extcon_info
{
36 struct extcon_dev
*edev
;
38 struct gpio_desc
*id_gpiod
;
39 struct gpio_desc
*vbus_gpiod
;
43 unsigned long debounce_jiffies
;
44 struct delayed_work wq_detcable
;
47 static const unsigned int usb_extcon_cable
[] = {
54 * "USB" = VBUS and "USB-HOST" = !ID, so we have:
55 * Both "USB" and "USB-HOST" can't be set as active at the
56 * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive
60 * ----------------------------------------
63 * [3] USB-HOST | L | H
64 * [4] USB-HOST | L | L
66 * In case we have only one of these signals:
67 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1.
68 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID.
70 static void usb_extcon_detect_cable(struct work_struct
*work
)
73 struct usb_extcon_info
*info
= container_of(to_delayed_work(work
),
74 struct usb_extcon_info
,
77 /* check ID and VBUS and update cable state */
79 gpiod_get_value_cansleep(info
->id_gpiod
) : 1;
80 vbus
= info
->vbus_gpiod
?
81 gpiod_get_value_cansleep(info
->vbus_gpiod
) : id
;
83 /* at first we clean states which are no longer active */
85 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, false);
87 extcon_set_state_sync(info
->edev
, EXTCON_USB
, false);
90 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, true);
93 extcon_set_state_sync(info
->edev
, EXTCON_USB
, true);
97 static irqreturn_t
usb_irq_handler(int irq
, void *dev_id
)
99 struct usb_extcon_info
*info
= dev_id
;
101 queue_delayed_work(system_power_efficient_wq
, &info
->wq_detcable
,
102 info
->debounce_jiffies
);
107 static int usb_extcon_probe(struct platform_device
*pdev
)
109 struct device
*dev
= &pdev
->dev
;
110 struct device_node
*np
= dev
->of_node
;
111 struct usb_extcon_info
*info
;
114 if (!np
&& !ACPI_HANDLE(dev
))
117 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
122 info
->id_gpiod
= devm_gpiod_get_optional(&pdev
->dev
, "id", GPIOD_IN
);
123 info
->vbus_gpiod
= devm_gpiod_get_optional(&pdev
->dev
, "vbus",
126 if (!info
->id_gpiod
&& !info
->vbus_gpiod
) {
127 dev_err(dev
, "failed to get gpios\n");
131 if (IS_ERR(info
->id_gpiod
))
132 return PTR_ERR(info
->id_gpiod
);
134 if (IS_ERR(info
->vbus_gpiod
))
135 return PTR_ERR(info
->vbus_gpiod
);
137 info
->edev
= devm_extcon_dev_allocate(dev
, usb_extcon_cable
);
138 if (IS_ERR(info
->edev
)) {
139 dev_err(dev
, "failed to allocate extcon device\n");
143 ret
= devm_extcon_dev_register(dev
, info
->edev
);
145 dev_err(dev
, "failed to register extcon device\n");
150 ret
= gpiod_set_debounce(info
->id_gpiod
,
151 USB_GPIO_DEBOUNCE_MS
* 1000);
152 if (!ret
&& info
->vbus_gpiod
)
153 ret
= gpiod_set_debounce(info
->vbus_gpiod
,
154 USB_GPIO_DEBOUNCE_MS
* 1000);
157 info
->debounce_jiffies
= msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS
);
159 INIT_DELAYED_WORK(&info
->wq_detcable
, usb_extcon_detect_cable
);
161 if (info
->id_gpiod
) {
162 info
->id_irq
= gpiod_to_irq(info
->id_gpiod
);
163 if (info
->id_irq
< 0) {
164 dev_err(dev
, "failed to get ID IRQ\n");
168 ret
= devm_request_threaded_irq(dev
, info
->id_irq
, NULL
,
170 IRQF_TRIGGER_RISING
|
171 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
174 dev_err(dev
, "failed to request handler for ID IRQ\n");
179 if (info
->vbus_gpiod
) {
180 info
->vbus_irq
= gpiod_to_irq(info
->vbus_gpiod
);
181 if (info
->vbus_irq
< 0) {
182 dev_err(dev
, "failed to get VBUS IRQ\n");
183 return info
->vbus_irq
;
186 ret
= devm_request_threaded_irq(dev
, info
->vbus_irq
, NULL
,
188 IRQF_TRIGGER_RISING
|
189 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
192 dev_err(dev
, "failed to request handler for VBUS IRQ\n");
197 platform_set_drvdata(pdev
, info
);
198 device_init_wakeup(dev
, true);
200 /* Perform initial detection */
201 usb_extcon_detect_cable(&info
->wq_detcable
.work
);
206 static int usb_extcon_remove(struct platform_device
*pdev
)
208 struct usb_extcon_info
*info
= platform_get_drvdata(pdev
);
210 cancel_delayed_work_sync(&info
->wq_detcable
);
211 device_init_wakeup(&pdev
->dev
, false);
216 #ifdef CONFIG_PM_SLEEP
217 static int usb_extcon_suspend(struct device
*dev
)
219 struct usb_extcon_info
*info
= dev_get_drvdata(dev
);
222 if (device_may_wakeup(dev
)) {
223 if (info
->id_gpiod
) {
224 ret
= enable_irq_wake(info
->id_irq
);
228 if (info
->vbus_gpiod
) {
229 ret
= enable_irq_wake(info
->vbus_irq
);
232 disable_irq_wake(info
->id_irq
);
240 * We don't want to process any IRQs after this point
241 * as GPIOs used behind I2C subsystem might not be
242 * accessible until resume completes. So disable IRQ.
245 disable_irq(info
->id_irq
);
246 if (info
->vbus_gpiod
)
247 disable_irq(info
->vbus_irq
);
249 if (!device_may_wakeup(dev
))
250 pinctrl_pm_select_sleep_state(dev
);
255 static int usb_extcon_resume(struct device
*dev
)
257 struct usb_extcon_info
*info
= dev_get_drvdata(dev
);
260 if (!device_may_wakeup(dev
))
261 pinctrl_pm_select_default_state(dev
);
263 if (device_may_wakeup(dev
)) {
264 if (info
->id_gpiod
) {
265 ret
= disable_irq_wake(info
->id_irq
);
269 if (info
->vbus_gpiod
) {
270 ret
= disable_irq_wake(info
->vbus_irq
);
273 enable_irq_wake(info
->id_irq
);
281 enable_irq(info
->id_irq
);
282 if (info
->vbus_gpiod
)
283 enable_irq(info
->vbus_irq
);
285 if (!device_may_wakeup(dev
))
286 queue_delayed_work(system_power_efficient_wq
,
287 &info
->wq_detcable
, 0);
293 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops
,
294 usb_extcon_suspend
, usb_extcon_resume
);
296 static const struct of_device_id usb_extcon_dt_match
[] = {
297 { .compatible
= "linux,extcon-usb-gpio", },
300 MODULE_DEVICE_TABLE(of
, usb_extcon_dt_match
);
302 static const struct platform_device_id usb_extcon_platform_ids
[] = {
303 { .name
= "extcon-usb-gpio", },
306 MODULE_DEVICE_TABLE(platform
, usb_extcon_platform_ids
);
308 static struct platform_driver usb_extcon_driver
= {
309 .probe
= usb_extcon_probe
,
310 .remove
= usb_extcon_remove
,
312 .name
= "extcon-usb-gpio",
313 .pm
= &usb_extcon_pm_ops
,
314 .of_match_table
= usb_extcon_dt_match
,
316 .id_table
= usb_extcon_platform_ids
,
319 module_platform_driver(usb_extcon_driver
);
321 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
322 MODULE_DESCRIPTION("USB GPIO extcon driver");
323 MODULE_LICENSE("GPL v2");