1 /**
2  * drivers/extcon/extcon-usb-gpio.c - USB GPIO extcon driver
3  *
4  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
5  * Author: Roger Quadros <rogerq@ti.com>
6  *
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.
10  *
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.
15  */
16 
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>
27 
28 #define USB_GPIO_DEBOUNCE_MS	20	/* ms */
29 
30 struct usb_extcon_info {
31 	struct device *dev;
32 	struct extcon_dev *edev;
33 
34 	struct gpio_desc *id_gpiod;
35 	int id_irq;
36 
37 	unsigned long debounce_jiffies;
38 	struct delayed_work wq_detcable;
39 };
40 
41 /* List of detectable cables */
42 enum {
43 	EXTCON_CABLE_USB = 0,
44 	EXTCON_CABLE_USB_HOST,
45 
46 	EXTCON_CABLE_END,
47 };
48 
49 static const char *usb_extcon_cable[] = {
50 	[EXTCON_CABLE_USB] = "USB",
51 	[EXTCON_CABLE_USB_HOST] = "USB-HOST",
52 	NULL,
53 };
54 
55 static void usb_extcon_detect_cable(struct work_struct *work)
56 {
57 	int id;
58 	struct usb_extcon_info *info = container_of(to_delayed_work(work),
59 						    struct usb_extcon_info,
60 						    wq_detcable);
61 
62 	/* check ID and update cable state */
63 	id = gpiod_get_value_cansleep(info->id_gpiod);
64 	if (id) {
65 		/*
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.
69 		 */
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 {
77 		/*
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.
81 		 */
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);
88 	}
89 }
90 
91 static irqreturn_t usb_irq_handler(int irq, void *dev_id)
92 {
93 	struct usb_extcon_info *info = dev_id;
94 
95 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
96 			   info->debounce_jiffies);
97 
98 	return IRQ_HANDLED;
99 }
100 
101 static int usb_extcon_probe(struct platform_device *pdev)
102 {
103 	struct device *dev = &pdev->dev;
104 	struct device_node *np = dev->of_node;
105 	struct usb_extcon_info *info;
106 	int ret;
107 
108 	if (!np)
109 		return -EINVAL;
110 
111 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
112 	if (!info)
113 		return -ENOMEM;
114 
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);
120 	}
121 
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;
126 	}
127 
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;
132 	}
133 
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);
138 
139 	INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
140 
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;
145 	}
146 
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;
155 	}
156 
157 	platform_set_drvdata(pdev, info);
158 	device_init_wakeup(dev, 1);
159 
160 	/* Perform initial detection */
161 	usb_extcon_detect_cable(&info->wq_detcable.work);
162 
163 	return 0;
164 }
165 
166 static int usb_extcon_remove(struct platform_device *pdev)
167 {
168 	struct usb_extcon_info *info = platform_get_drvdata(pdev);
169 
170 	cancel_delayed_work_sync(&info->wq_detcable);
171 
172 	return 0;
173 }
174 
175 #ifdef CONFIG_PM_SLEEP
176 static int usb_extcon_suspend(struct device *dev)
177 {
178 	struct usb_extcon_info *info = dev_get_drvdata(dev);
179 	int ret = 0;
180 
181 	if (device_may_wakeup(dev)) {
182 		ret = enable_irq_wake(info->id_irq);
183 		if (ret)
184 			return ret;
185 	}
186 
187 	/*
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.
191 	 */
192 	disable_irq(info->id_irq);
193 
194 	return ret;
195 }
196 
197 static int usb_extcon_resume(struct device *dev)
198 {
199 	struct usb_extcon_info *info = dev_get_drvdata(dev);
200 	int ret = 0;
201 
202 	if (device_may_wakeup(dev)) {
203 		ret = disable_irq_wake(info->id_irq);
204 		if (ret)
205 			return ret;
206 	}
207 
208 	enable_irq(info->id_irq);
209 
210 	return ret;
211 }
212 #endif
213 
214 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
215 			 usb_extcon_suspend, usb_extcon_resume);
216 
217 static const struct of_device_id usb_extcon_dt_match[] = {
218 	{ .compatible = "linux,extcon-usb-gpio", },
219 	{ /* sentinel */ }
220 };
221 MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
222 
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,
230 	},
231 };
232 
233 module_platform_driver(usb_extcon_driver);
234 
235 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
236 MODULE_DESCRIPTION("USB GPIO extcon driver");
237 MODULE_LICENSE("GPL v2");
238