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/consumer.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/of_gpio.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/workqueue.h>
29 #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
31 struct usb_extcon_info
{
33 struct extcon_dev
*edev
;
35 struct gpio_desc
*id_gpiod
;
38 unsigned long debounce_jiffies
;
39 struct delayed_work wq_detcable
;
42 static const unsigned int usb_extcon_cable
[] = {
48 static void usb_extcon_detect_cable(struct work_struct
*work
)
51 struct usb_extcon_info
*info
= container_of(to_delayed_work(work
),
52 struct usb_extcon_info
,
55 /* check ID and update cable state */
56 id
= gpiod_get_value_cansleep(info
->id_gpiod
);
59 * ID = 1 means USB HOST cable detached.
60 * As we don't have event for USB peripheral cable attached,
61 * we simulate USB peripheral attach here.
63 extcon_set_cable_state_(info
->edev
, EXTCON_USB_HOST
, false);
64 extcon_set_cable_state_(info
->edev
, EXTCON_USB
, true);
67 * ID = 0 means USB HOST cable attached.
68 * As we don't have event for USB peripheral cable detached,
69 * we simulate USB peripheral detach here.
71 extcon_set_cable_state_(info
->edev
, EXTCON_USB
, false);
72 extcon_set_cable_state_(info
->edev
, EXTCON_USB_HOST
, true);
76 static irqreturn_t
usb_irq_handler(int irq
, void *dev_id
)
78 struct usb_extcon_info
*info
= dev_id
;
80 queue_delayed_work(system_power_efficient_wq
, &info
->wq_detcable
,
81 info
->debounce_jiffies
);
86 static int usb_extcon_probe(struct platform_device
*pdev
)
88 struct device
*dev
= &pdev
->dev
;
89 struct device_node
*np
= dev
->of_node
;
90 struct usb_extcon_info
*info
;
96 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
101 info
->id_gpiod
= devm_gpiod_get(&pdev
->dev
, "id", GPIOD_IN
);
102 if (IS_ERR(info
->id_gpiod
)) {
103 dev_err(dev
, "failed to get ID GPIO\n");
104 return PTR_ERR(info
->id_gpiod
);
107 info
->edev
= devm_extcon_dev_allocate(dev
, usb_extcon_cable
);
108 if (IS_ERR(info
->edev
)) {
109 dev_err(dev
, "failed to allocate extcon device\n");
113 ret
= devm_extcon_dev_register(dev
, info
->edev
);
115 dev_err(dev
, "failed to register extcon device\n");
119 ret
= gpiod_set_debounce(info
->id_gpiod
,
120 USB_GPIO_DEBOUNCE_MS
* 1000);
122 info
->debounce_jiffies
= msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS
);
124 INIT_DELAYED_WORK(&info
->wq_detcable
, usb_extcon_detect_cable
);
126 info
->id_irq
= gpiod_to_irq(info
->id_gpiod
);
127 if (info
->id_irq
< 0) {
128 dev_err(dev
, "failed to get ID IRQ\n");
132 ret
= devm_request_threaded_irq(dev
, info
->id_irq
, NULL
,
134 IRQF_TRIGGER_RISING
|
135 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
138 dev_err(dev
, "failed to request handler for ID IRQ\n");
142 platform_set_drvdata(pdev
, info
);
143 device_init_wakeup(dev
, 1);
145 /* Perform initial detection */
146 usb_extcon_detect_cable(&info
->wq_detcable
.work
);
151 static int usb_extcon_remove(struct platform_device
*pdev
)
153 struct usb_extcon_info
*info
= platform_get_drvdata(pdev
);
155 cancel_delayed_work_sync(&info
->wq_detcable
);
160 #ifdef CONFIG_PM_SLEEP
161 static int usb_extcon_suspend(struct device
*dev
)
163 struct usb_extcon_info
*info
= dev_get_drvdata(dev
);
166 if (device_may_wakeup(dev
)) {
167 ret
= enable_irq_wake(info
->id_irq
);
173 * We don't want to process any IRQs after this point
174 * as GPIOs used behind I2C subsystem might not be
175 * accessible until resume completes. So disable IRQ.
177 disable_irq(info
->id_irq
);
182 static int usb_extcon_resume(struct device
*dev
)
184 struct usb_extcon_info
*info
= dev_get_drvdata(dev
);
187 if (device_may_wakeup(dev
)) {
188 ret
= disable_irq_wake(info
->id_irq
);
193 enable_irq(info
->id_irq
);
199 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops
,
200 usb_extcon_suspend
, usb_extcon_resume
);
202 static const struct of_device_id usb_extcon_dt_match
[] = {
203 { .compatible
= "linux,extcon-usb-gpio", },
206 MODULE_DEVICE_TABLE(of
, usb_extcon_dt_match
);
208 static struct platform_driver usb_extcon_driver
= {
209 .probe
= usb_extcon_probe
,
210 .remove
= usb_extcon_remove
,
212 .name
= "extcon-usb-gpio",
213 .pm
= &usb_extcon_pm_ops
,
214 .of_match_table
= usb_extcon_dt_match
,
218 module_platform_driver(usb_extcon_driver
);
220 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
221 MODULE_DESCRIPTION("USB GPIO extcon driver");
222 MODULE_LICENSE("GPL v2");