Linux 4.1.16
[linux/fpc-iii.git] / drivers / extcon / extcon-usb-gpio.c
blobe45d1f13f4458e9130d8f052494b33b1a11a9c60
1 /**
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 {
31 struct device *dev;
32 struct extcon_dev *edev;
34 struct gpio_desc *id_gpiod;
35 int id_irq;
37 unsigned long debounce_jiffies;
38 struct delayed_work wq_detcable;
41 /* List of detectable cables */
42 enum {
43 EXTCON_CABLE_USB = 0,
44 EXTCON_CABLE_USB_HOST,
46 EXTCON_CABLE_END,
49 static const char *usb_extcon_cable[] = {
50 [EXTCON_CABLE_USB] = "USB",
51 [EXTCON_CABLE_USB_HOST] = "USB-HOST",
52 NULL,
55 static void usb_extcon_detect_cable(struct work_struct *work)
57 int id;
58 struct usb_extcon_info *info = container_of(to_delayed_work(work),
59 struct usb_extcon_info,
60 wq_detcable);
62 /* check ID and update cable state */
63 id = gpiod_get_value_cansleep(info->id_gpiod);
64 if (id) {
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],
72 false);
73 extcon_set_cable_state(info->edev,
74 usb_extcon_cable[EXTCON_CABLE_USB],
75 true);
76 } else {
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],
84 false);
85 extcon_set_cable_state(info->edev,
86 usb_extcon_cable[EXTCON_CABLE_USB_HOST],
87 true);
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);
98 return IRQ_HANDLED;
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;
106 int ret;
108 if (!np)
109 return -EINVAL;
111 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
112 if (!info)
113 return -ENOMEM;
115 info->dev = dev;
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");
125 return -ENOMEM;
128 ret = devm_extcon_dev_register(dev, info->edev);
129 if (ret < 0) {
130 dev_err(dev, "failed to register extcon device\n");
131 return ret;
134 ret = gpiod_set_debounce(info->id_gpiod,
135 USB_GPIO_DEBOUNCE_MS * 1000);
136 if (ret < 0)
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");
144 return info->id_irq;
147 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
148 usb_irq_handler,
149 IRQF_TRIGGER_RISING |
150 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
151 pdev->name, info);
152 if (ret < 0) {
153 dev_err(dev, "failed to request handler for ID IRQ\n");
154 return ret;
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);
163 return 0;
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);
172 return 0;
175 #ifdef CONFIG_PM_SLEEP
176 static int usb_extcon_suspend(struct device *dev)
178 struct usb_extcon_info *info = dev_get_drvdata(dev);
179 int ret = 0;
181 if (device_may_wakeup(dev)) {
182 ret = enable_irq_wake(info->id_irq);
183 if (ret)
184 return ret;
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);
194 return ret;
197 static int usb_extcon_resume(struct device *dev)
199 struct usb_extcon_info *info = dev_get_drvdata(dev);
200 int ret = 0;
202 if (device_may_wakeup(dev)) {
203 ret = disable_irq_wake(info->id_irq);
204 if (ret)
205 return ret;
208 enable_irq(info->id_irq);
210 return ret;
212 #endif
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", },
219 { /* sentinel */ }
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,
226 .driver = {
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");