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 /* List of detectable cables */
45 EXTCON_CABLE_USB_HOST,
50 static const char *usb_extcon_cable[] = {
51 [EXTCON_CABLE_USB] = "USB",
52 [EXTCON_CABLE_USB_HOST] = "USB-HOST",
56 static void usb_extcon_detect_cable(struct work_struct *work)
59 struct usb_extcon_info *info = container_of(to_delayed_work(work),
60 struct usb_extcon_info,
63 /* check ID and update cable state */
64 id = gpiod_get_value_cansleep(info->id_gpiod);
67 * ID = 1 means USB HOST cable detached.
68 * As we don't have event for USB peripheral cable attached,
69 * we simulate USB peripheral attach here.
71 extcon_set_cable_state(info->edev,
72 usb_extcon_cable[EXTCON_CABLE_USB_HOST],
74 extcon_set_cable_state(info->edev,
75 usb_extcon_cable[EXTCON_CABLE_USB],
79 * ID = 0 means USB HOST cable attached.
80 * As we don't have event for USB peripheral cable detached,
81 * we simulate USB peripheral detach here.
83 extcon_set_cable_state(info->edev,
84 usb_extcon_cable[EXTCON_CABLE_USB],
86 extcon_set_cable_state(info->edev,
87 usb_extcon_cable[EXTCON_CABLE_USB_HOST],
92 static irqreturn_t usb_irq_handler(int irq, void *dev_id)
94 struct usb_extcon_info *info = dev_id;
96 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
97 info->debounce_jiffies);
102 static int usb_extcon_probe(struct platform_device *pdev)
104 struct device *dev = &pdev->dev;
105 struct device_node *np = dev->of_node;
106 struct usb_extcon_info *info;
112 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
117 info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
118 if (IS_ERR(info->id_gpiod)) {
119 dev_err(dev, "failed to get ID GPIO\n");
120 return PTR_ERR(info->id_gpiod);
123 info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
124 if (IS_ERR(info->edev)) {
125 dev_err(dev, "failed to allocate extcon device\n");
129 ret = devm_extcon_dev_register(dev, info->edev);
131 dev_err(dev, "failed to register extcon device\n");
135 ret = gpiod_set_debounce(info->id_gpiod,
136 USB_GPIO_DEBOUNCE_MS * 1000);
138 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
140 INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
142 info->id_irq = gpiod_to_irq(info->id_gpiod);
143 if (info->id_irq < 0) {
144 dev_err(dev, "failed to get ID IRQ\n");
148 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
150 IRQF_TRIGGER_RISING |
151 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
154 dev_err(dev, "failed to request handler for ID IRQ\n");
158 platform_set_drvdata(pdev, info);
159 device_init_wakeup(dev, 1);
161 /* Perform initial detection */
162 usb_extcon_detect_cable(&info->wq_detcable.work);
167 static int usb_extcon_remove(struct platform_device *pdev)
169 struct usb_extcon_info *info = platform_get_drvdata(pdev);
171 cancel_delayed_work_sync(&info->wq_detcable);
176 #ifdef CONFIG_PM_SLEEP
177 static int usb_extcon_suspend(struct device *dev)
179 struct usb_extcon_info *info = dev_get_drvdata(dev);
182 if (device_may_wakeup(dev)) {
183 ret = enable_irq_wake(info->id_irq);
189 * We don't want to process any IRQs after this point
190 * as GPIOs used behind I2C subsystem might not be
191 * accessible until resume completes. So disable IRQ.
193 disable_irq(info->id_irq);
198 static int usb_extcon_resume(struct device *dev)
200 struct usb_extcon_info *info = dev_get_drvdata(dev);
203 if (device_may_wakeup(dev)) {
204 ret = disable_irq_wake(info->id_irq);
209 enable_irq(info->id_irq);
215 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
216 usb_extcon_suspend, usb_extcon_resume);
218 static const struct of_device_id usb_extcon_dt_match[] = {
219 { .compatible = "linux,extcon-usb-gpio", },
222 MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
224 static struct platform_driver usb_extcon_driver = {
225 .probe = usb_extcon_probe,
226 .remove = usb_extcon_remove,
228 .name = "extcon-usb-gpio",
229 .pm = &usb_extcon_pm_ops,
230 .of_match_table = usb_extcon_dt_match,
234 module_platform_driver(usb_extcon_driver);
236 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
237 MODULE_DESCRIPTION("USB GPIO extcon driver");
238 MODULE_LICENSE("GPL v2");