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/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_gpio.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/workqueue.h>
28 #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
30 struct usb_extcon_info
{
32 struct extcon_dev
*edev
;
34 struct gpio_desc
*id_gpiod
;
37 unsigned long debounce_jiffies
;
38 struct delayed_work wq_detcable
;
41 /* List of detectable cables */
44 EXTCON_CABLE_USB_HOST
,
49 static const char *usb_extcon_cable
[] = {
50 [EXTCON_CABLE_USB
] = "USB",
51 [EXTCON_CABLE_USB_HOST
] = "USB-HOST",
55 static void usb_extcon_detect_cable(struct work_struct
*work
)
58 struct usb_extcon_info
*info
= container_of(to_delayed_work(work
),
59 struct usb_extcon_info
,
62 /* check ID and update cable state */
63 id
= gpiod_get_value_cansleep(info
->id_gpiod
);
66 * ID = 1 means USB HOST cable detached.
67 * As we don't have event for USB peripheral cable attached,
68 * we simulate USB peripheral attach here.
70 extcon_set_cable_state(info
->edev
,
71 usb_extcon_cable
[EXTCON_CABLE_USB_HOST
],
73 extcon_set_cable_state(info
->edev
,
74 usb_extcon_cable
[EXTCON_CABLE_USB
],
78 * ID = 0 means USB HOST cable attached.
79 * As we don't have event for USB peripheral cable detached,
80 * we simulate USB peripheral detach here.
82 extcon_set_cable_state(info
->edev
,
83 usb_extcon_cable
[EXTCON_CABLE_USB
],
85 extcon_set_cable_state(info
->edev
,
86 usb_extcon_cable
[EXTCON_CABLE_USB_HOST
],
91 static irqreturn_t
usb_irq_handler(int irq
, void *dev_id
)
93 struct usb_extcon_info
*info
= dev_id
;
95 queue_delayed_work(system_power_efficient_wq
, &info
->wq_detcable
,
96 info
->debounce_jiffies
);
101 static int usb_extcon_probe(struct platform_device
*pdev
)
103 struct device
*dev
= &pdev
->dev
;
104 struct device_node
*np
= dev
->of_node
;
105 struct usb_extcon_info
*info
;
111 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
116 info
->id_gpiod
= devm_gpiod_get(&pdev
->dev
, "id");
117 if (IS_ERR(info
->id_gpiod
)) {
118 dev_err(dev
, "failed to get ID GPIO\n");
119 return PTR_ERR(info
->id_gpiod
);
122 info
->edev
= devm_extcon_dev_allocate(dev
, usb_extcon_cable
);
123 if (IS_ERR(info
->edev
)) {
124 dev_err(dev
, "failed to allocate extcon device\n");
128 ret
= devm_extcon_dev_register(dev
, info
->edev
);
130 dev_err(dev
, "failed to register extcon device\n");
134 ret
= gpiod_set_debounce(info
->id_gpiod
,
135 USB_GPIO_DEBOUNCE_MS
* 1000);
137 info
->debounce_jiffies
= msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS
);
139 INIT_DELAYED_WORK(&info
->wq_detcable
, usb_extcon_detect_cable
);
141 info
->id_irq
= gpiod_to_irq(info
->id_gpiod
);
142 if (info
->id_irq
< 0) {
143 dev_err(dev
, "failed to get ID IRQ\n");
147 ret
= devm_request_threaded_irq(dev
, info
->id_irq
, NULL
,
149 IRQF_TRIGGER_RISING
|
150 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
153 dev_err(dev
, "failed to request handler for ID IRQ\n");
157 platform_set_drvdata(pdev
, info
);
158 device_init_wakeup(dev
, 1);
160 /* Perform initial detection */
161 usb_extcon_detect_cable(&info
->wq_detcable
.work
);
166 static int usb_extcon_remove(struct platform_device
*pdev
)
168 struct usb_extcon_info
*info
= platform_get_drvdata(pdev
);
170 cancel_delayed_work_sync(&info
->wq_detcable
);
175 #ifdef CONFIG_PM_SLEEP
176 static int usb_extcon_suspend(struct device
*dev
)
178 struct usb_extcon_info
*info
= dev_get_drvdata(dev
);
181 if (device_may_wakeup(dev
)) {
182 ret
= enable_irq_wake(info
->id_irq
);
188 * We don't want to process any IRQs after this point
189 * as GPIOs used behind I2C subsystem might not be
190 * accessible until resume completes. So disable IRQ.
192 disable_irq(info
->id_irq
);
197 static int usb_extcon_resume(struct device
*dev
)
199 struct usb_extcon_info
*info
= dev_get_drvdata(dev
);
202 if (device_may_wakeup(dev
)) {
203 ret
= disable_irq_wake(info
->id_irq
);
208 enable_irq(info
->id_irq
);
214 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops
,
215 usb_extcon_suspend
, usb_extcon_resume
);
217 static const struct of_device_id usb_extcon_dt_match
[] = {
218 { .compatible
= "linux,extcon-usb-gpio", },
221 MODULE_DEVICE_TABLE(of
, usb_extcon_dt_match
);
223 static struct platform_driver usb_extcon_driver
= {
224 .probe
= usb_extcon_probe
,
225 .remove
= usb_extcon_remove
,
227 .name
= "extcon-usb-gpio",
228 .pm
= &usb_extcon_pm_ops
,
229 .of_match_table
= usb_extcon_dt_match
,
233 module_platform_driver(usb_extcon_driver
);
235 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
236 MODULE_DESCRIPTION("USB GPIO extcon driver");
237 MODULE_LICENSE("GPL v2");