blob: aad76377a5a3fde25fbf1129a6157760f04335fd [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/**
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-provider.h>
18#include <linux/gpio.h>
19#include <linux/gpio/consumer.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/irq.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/of_gpio.h>
26#include <linux/platform_device.h>
27#include <linux/slab.h>
28#include <linux/workqueue.h>
29#include <linux/pinctrl/consumer.h>
30
31#define USB_GPIO_DEBOUNCE_MS 20 /* ms */
32
33struct usb_extcon_info {
34 struct device *dev;
35 struct extcon_dev *edev;
36
37 struct gpio_desc *id_gpiod;
38 struct gpio_desc *vbus_gpiod;
39 int id_irq;
40 int vbus_irq;
41
42 unsigned long debounce_jiffies;
43 struct delayed_work wq_detcable;
44};
45
46static const unsigned int usb_extcon_cable[] = {
47 EXTCON_USB,
48 EXTCON_USB_HOST,
49 EXTCON_NONE,
50};
51
52/*
53 * "USB" = VBUS and "USB-HOST" = !ID, so we have:
54 * Both "USB" and "USB-HOST" can't be set as active at the
55 * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive
56 * even if VBUS is on.
57 *
58 * State | ID | VBUS
59 * ----------------------------------------
60 * [1] USB | H | H
61 * [2] none | H | L
62 * [3] USB-HOST | L | H
63 * [4] USB-HOST | L | L
64 *
65 * In case we have only one of these signals:
66 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1.
67 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID.
68*/
69static void usb_extcon_detect_cable(struct work_struct *work)
70{
71 int id, vbus;
72 struct usb_extcon_info *info = container_of(to_delayed_work(work),
73 struct usb_extcon_info,
74 wq_detcable);
75
76 /* check ID and VBUS and update cable state */
77 id = info->id_gpiod ?
78 gpiod_get_value_cansleep(info->id_gpiod) : 1;
rjw2e8229f2022-02-15 21:08:12 +080079 //vbus = info->vbus_gpiod ?
80 //gpiod_get_value_cansleep(info->vbus_gpiod) : id;//tianyan@2021.11.29 modify for usb otg
81
xjb04a4022021-11-25 15:01:52 +080082 vbus = info->vbus_gpiod ?
rjw2e8229f2022-02-15 21:08:12 +080083 gpiod_get_value_cansleep(info->vbus_gpiod) : 0;//tianyan@2021.11.29 modify for usb otg
xjb04a4022021-11-25 15:01:52 +080084
85 /* at first we clean states which are no longer active */
86 if (id)
87 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
88 if (!vbus)
89 extcon_set_state_sync(info->edev, EXTCON_USB, false);
90
91 if (!id) {
92 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
93 } else {
94 if (vbus)
95 extcon_set_state_sync(info->edev, EXTCON_USB, true);
96 }
97}
98
99static irqreturn_t usb_irq_handler(int irq, void *dev_id)
100{
101 struct usb_extcon_info *info = dev_id;
102
103 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
104 info->debounce_jiffies);
105
106 return IRQ_HANDLED;
107}
108
109static int usb_extcon_probe(struct platform_device *pdev)
110{
111 struct device *dev = &pdev->dev;
112 struct device_node *np = dev->of_node;
113 struct usb_extcon_info *info;
114 int ret;
115
116 if (!np)
117 return -EINVAL;
118
119 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
120 if (!info)
121 return -ENOMEM;
122
123 info->dev = dev;
124 info->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", GPIOD_IN);
125 info->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
126 GPIOD_IN);
127
128 if (!info->id_gpiod && !info->vbus_gpiod) {
129 dev_err(dev, "failed to get gpios\n");
130 return -ENODEV;
131 }
132
133 if (IS_ERR(info->id_gpiod))
134 return PTR_ERR(info->id_gpiod);
135
136 if (IS_ERR(info->vbus_gpiod))
137 return PTR_ERR(info->vbus_gpiod);
138
139 info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
140 if (IS_ERR(info->edev)) {
141 dev_err(dev, "failed to allocate extcon device\n");
142 return -ENOMEM;
143 }
144
145 ret = devm_extcon_dev_register(dev, info->edev);
146 if (ret < 0) {
147 dev_err(dev, "failed to register extcon device\n");
148 return ret;
149 }
150
151 if (info->id_gpiod)
152 ret = gpiod_set_debounce(info->id_gpiod,
153 USB_GPIO_DEBOUNCE_MS * 1000);
154 if (!ret && info->vbus_gpiod)
155 ret = gpiod_set_debounce(info->vbus_gpiod,
156 USB_GPIO_DEBOUNCE_MS * 1000);
157
158 if (ret < 0)
159 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
160
161 INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
162
163 if (info->id_gpiod) {
164 info->id_irq = gpiod_to_irq(info->id_gpiod);
165 if (info->id_irq < 0) {
166 dev_err(dev, "failed to get ID IRQ\n");
167 return info->id_irq;
168 }
169
170 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
171 usb_irq_handler,
172 IRQF_TRIGGER_RISING |
173 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
174 pdev->name, info);
175 if (ret < 0) {
176 dev_err(dev, "failed to request handler for ID IRQ\n");
177 return ret;
178 }
179 }
180
181 if (info->vbus_gpiod) {
182 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
183 if (info->vbus_irq < 0) {
184 dev_err(dev, "failed to get VBUS IRQ\n");
185 return info->vbus_irq;
186 }
187
188 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
189 usb_irq_handler,
190 IRQF_TRIGGER_RISING |
191 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
192 pdev->name, info);
193 if (ret < 0) {
194 dev_err(dev, "failed to request handler for VBUS IRQ\n");
195 return ret;
196 }
197 }
198
199 platform_set_drvdata(pdev, info);
200 device_set_wakeup_capable(&pdev->dev, true);
201
202 /* Perform initial detection */
203 usb_extcon_detect_cable(&info->wq_detcable.work);
204
205 return 0;
206}
207
208static int usb_extcon_remove(struct platform_device *pdev)
209{
210 struct usb_extcon_info *info = platform_get_drvdata(pdev);
211
212 cancel_delayed_work_sync(&info->wq_detcable);
213 device_init_wakeup(&pdev->dev, false);
214
215 return 0;
216}
217
218#ifdef CONFIG_PM_SLEEP
219static int usb_extcon_suspend(struct device *dev)
220{
221 struct usb_extcon_info *info = dev_get_drvdata(dev);
222 int ret = 0;
rjw2e8229f2022-02-15 21:08:12 +0800223
224 if (info->vbus_gpiod) {//tianyan@2021.11.29 modify for usb otg
225 ret = enable_irq_wake(info->vbus_irq);
226 if (ret)
227 return ret;
228 }
xjb04a4022021-11-25 15:01:52 +0800229
230 if (device_may_wakeup(dev)) {
231 if (info->id_gpiod) {
232 ret = enable_irq_wake(info->id_irq);
233 if (ret)
234 return ret;
235 }
236 if (info->vbus_gpiod) {
237 ret = enable_irq_wake(info->vbus_irq);
238 if (ret) {
239 if (info->id_gpiod)
240 disable_irq_wake(info->id_irq);
241
242 return ret;
243 }
244 }
245 }
246
247 /*
248 * We don't want to process any IRQs after this point
249 * as GPIOs used behind I2C subsystem might not be
250 * accessible until resume completes. So disable IRQ.
251 */
252 if (info->id_gpiod)
253 disable_irq(info->id_irq);
254 if (info->vbus_gpiod)
255 disable_irq(info->vbus_irq);
256
257 if (!device_may_wakeup(dev))
258 pinctrl_pm_select_sleep_state(dev);
259
260 return ret;
261}
262
263static int usb_extcon_resume(struct device *dev)
264{
265 struct usb_extcon_info *info = dev_get_drvdata(dev);
266 int ret = 0;
267
rjw2e8229f2022-02-15 21:08:12 +0800268 if (info->vbus_gpiod) {//tianyan@2021.11.29 modify for usb otg
269 ret = disable_irq_wake(info->vbus_irq);
270 if (ret)
271 return ret;
272 }
273
xjb04a4022021-11-25 15:01:52 +0800274 if (!device_may_wakeup(dev))
275 pinctrl_pm_select_default_state(dev);
276
277 if (device_may_wakeup(dev)) {
278 if (info->id_gpiod) {
279 ret = disable_irq_wake(info->id_irq);
280 if (ret)
281 return ret;
282 }
283 if (info->vbus_gpiod) {
284 ret = disable_irq_wake(info->vbus_irq);
285 if (ret) {
286 if (info->id_gpiod)
287 enable_irq_wake(info->id_irq);
288
289 return ret;
290 }
291 }
292 }
293
294 if (info->id_gpiod)
295 enable_irq(info->id_irq);
296 if (info->vbus_gpiod)
297 enable_irq(info->vbus_irq);
298
299 queue_delayed_work(system_power_efficient_wq,
300 &info->wq_detcable, 0);
301
302 return ret;
303}
304#endif
305
306static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
307 usb_extcon_suspend, usb_extcon_resume);
308
309static const struct of_device_id usb_extcon_dt_match[] = {
310 { .compatible = "linux,extcon-usb-gpio", },
311 { /* sentinel */ }
312};
313MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
314
315static const struct platform_device_id usb_extcon_platform_ids[] = {
316 { .name = "extcon-usb-gpio", },
317 { /* sentinel */ }
318};
319MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
320
321static struct platform_driver usb_extcon_driver = {
322 .probe = usb_extcon_probe,
323 .remove = usb_extcon_remove,
324 .driver = {
325 .name = "extcon-usb-gpio",
326 .pm = &usb_extcon_pm_ops,
327 .of_match_table = usb_extcon_dt_match,
328 },
329 .id_table = usb_extcon_platform_ids,
330};
331
332module_platform_driver(usb_extcon_driver);
333
334MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
335MODULE_DESCRIPTION("USB GPIO extcon driver");
336MODULE_LICENSE("GPL v2");