blob: b1ef5c499c98c534c366ecfc18acabc066c70e60 [file] [log] [blame]
Roger Quadrose52817f2015-02-02 12:21:59 +02001/**
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>
Roger Quadros92b7cb52015-08-03 17:40:51 +030018#include <linux/gpio.h>
Geert Uytterhoeven638f9582015-05-05 18:32:20 +020019#include <linux/gpio/consumer.h>
Roger Quadrose52817f2015-02-02 12:21:59 +020020#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>
Lu Baolu942c7922016-04-26 08:35:54 +090029#include <linux/acpi.h>
Bo Zhang3905e7e2018-10-15 13:05:50 +080030#include <linux/delay.h>
Roger Quadrose52817f2015-02-02 12:21:59 +020031
32#define USB_GPIO_DEBOUNCE_MS 20 /* ms */
33
34struct usb_extcon_info {
35 struct device *dev;
36 struct extcon_dev *edev;
37
38 struct gpio_desc *id_gpiod;
Roger Quadros1c936712016-09-20 17:53:55 +030039 struct gpio_desc *vbus_gpiod;
Bo Zhang3905e7e2018-10-15 13:05:50 +080040 struct gpio_desc *trig_gpiod;
Roger Quadrose52817f2015-02-02 12:21:59 +020041 int id_irq;
Roger Quadros1c936712016-09-20 17:53:55 +030042 int vbus_irq;
Roger Quadrose52817f2015-02-02 12:21:59 +020043
44 unsigned long debounce_jiffies;
45 struct delayed_work wq_detcable;
46};
47
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090048static const unsigned int usb_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090049 EXTCON_USB,
50 EXTCON_USB_HOST,
51 EXTCON_NONE,
Roger Quadrose52817f2015-02-02 12:21:59 +020052};
53
Roger Quadros1c936712016-09-20 17:53:55 +030054/*
55 * "USB" = VBUS and "USB-HOST" = !ID, so we have:
56 * Both "USB" and "USB-HOST" can't be set as active at the
57 * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive
58 * even if VBUS is on.
59 *
60 * State | ID | VBUS
61 * ----------------------------------------
62 * [1] USB | H | H
63 * [2] none | H | L
64 * [3] USB-HOST | L | H
65 * [4] USB-HOST | L | L
66 *
67 * In case we have only one of these signals:
68 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1.
69 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID.
70 */
Roger Quadrose52817f2015-02-02 12:21:59 +020071static void usb_extcon_detect_cable(struct work_struct *work)
72{
Roger Quadros1c936712016-09-20 17:53:55 +030073 int id, vbus;
Roger Quadrose52817f2015-02-02 12:21:59 +020074 struct usb_extcon_info *info = container_of(to_delayed_work(work),
75 struct usb_extcon_info,
76 wq_detcable);
77
Roger Quadros1c936712016-09-20 17:53:55 +030078 /* check ID and VBUS and update cable state */
79 id = info->id_gpiod ?
80 gpiod_get_value_cansleep(info->id_gpiod) : 1;
81 vbus = info->vbus_gpiod ?
82 gpiod_get_value_cansleep(info->vbus_gpiod) : id;
83
84 /* at first we clean states which are no longer active */
85 if (id)
Chanwoo Choi8670b452016-08-16 15:55:34 +090086 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
Roger Quadros1c936712016-09-20 17:53:55 +030087 if (!vbus)
Chanwoo Choi8670b452016-08-16 15:55:34 +090088 extcon_set_state_sync(info->edev, EXTCON_USB, false);
Roger Quadros1c936712016-09-20 17:53:55 +030089
90 if (!id) {
Chanwoo Choi8670b452016-08-16 15:55:34 +090091 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
Bo Zhang3905e7e2018-10-15 13:05:50 +080092 if (info->trig_gpiod) {
93 gpiod_set_value(info->trig_gpiod, 1);
94 msleep(20);
95 gpiod_set_value(info->trig_gpiod, 0);
96 msleep(20);
97 }
Roger Quadros1c936712016-09-20 17:53:55 +030098 } else {
99 if (vbus)
100 extcon_set_state_sync(info->edev, EXTCON_USB, true);
Roger Quadrose52817f2015-02-02 12:21:59 +0200101 }
102}
103
104static irqreturn_t usb_irq_handler(int irq, void *dev_id)
105{
106 struct usb_extcon_info *info = dev_id;
107
108 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
109 info->debounce_jiffies);
110
111 return IRQ_HANDLED;
112}
113
114static int usb_extcon_probe(struct platform_device *pdev)
115{
116 struct device *dev = &pdev->dev;
117 struct device_node *np = dev->of_node;
118 struct usb_extcon_info *info;
119 int ret;
120
Lu Baolu942c7922016-04-26 08:35:54 +0900121 if (!np && !ACPI_HANDLE(dev))
Roger Quadrose52817f2015-02-02 12:21:59 +0200122 return -EINVAL;
123
124 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
125 if (!info)
126 return -ENOMEM;
127
128 info->dev = dev;
Roger Quadros1c936712016-09-20 17:53:55 +0300129 info->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", GPIOD_IN);
130 info->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
131 GPIOD_IN);
Bo Zhang3905e7e2018-10-15 13:05:50 +0800132 info->trig_gpiod = devm_gpiod_get_optional(&pdev->dev, "trig",
133 GPIOD_OUT_LOW);
Roger Quadros1c936712016-09-20 17:53:55 +0300134
135 if (!info->id_gpiod && !info->vbus_gpiod) {
136 dev_err(dev, "failed to get gpios\n");
137 return -ENODEV;
Roger Quadrose52817f2015-02-02 12:21:59 +0200138 }
139
Roger Quadros1c936712016-09-20 17:53:55 +0300140 if (IS_ERR(info->id_gpiod))
141 return PTR_ERR(info->id_gpiod);
142
143 if (IS_ERR(info->vbus_gpiod))
144 return PTR_ERR(info->vbus_gpiod);
145
Bo Zhang3905e7e2018-10-15 13:05:50 +0800146 if (IS_ERR(info->trig_gpiod))
147 return PTR_ERR(info->trig_gpiod);
148
Robert Baldygabc1aaba2015-04-02 15:13:02 +0200149 info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
150 if (IS_ERR(info->edev)) {
151 dev_err(dev, "failed to allocate extcon device\n");
152 return -ENOMEM;
153 }
154
155 ret = devm_extcon_dev_register(dev, info->edev);
156 if (ret < 0) {
157 dev_err(dev, "failed to register extcon device\n");
158 return ret;
159 }
160
Roger Quadros1c936712016-09-20 17:53:55 +0300161 if (info->id_gpiod)
162 ret = gpiod_set_debounce(info->id_gpiod,
163 USB_GPIO_DEBOUNCE_MS * 1000);
164 if (!ret && info->vbus_gpiod)
165 ret = gpiod_set_debounce(info->vbus_gpiod,
166 USB_GPIO_DEBOUNCE_MS * 1000);
167
Roger Quadrose52817f2015-02-02 12:21:59 +0200168 if (ret < 0)
169 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
170
171 INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
172
Roger Quadros1c936712016-09-20 17:53:55 +0300173 if (info->id_gpiod) {
174 info->id_irq = gpiod_to_irq(info->id_gpiod);
175 if (info->id_irq < 0) {
176 dev_err(dev, "failed to get ID IRQ\n");
177 return info->id_irq;
178 }
179
180 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
181 usb_irq_handler,
182 IRQF_TRIGGER_RISING |
183 IRQF_TRIGGER_FALLING |
184 IRQF_ONESHOT,
185 pdev->name, info);
186 if (ret < 0) {
187 dev_err(dev, "failed to request handler for ID IRQ\n");
188 return ret;
189 }
Roger Quadrose52817f2015-02-02 12:21:59 +0200190 }
191
Roger Quadros1c936712016-09-20 17:53:55 +0300192 if (info->vbus_gpiod) {
193 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
194 if (info->vbus_irq < 0) {
195 dev_err(dev, "failed to get VBUS IRQ\n");
196 return info->vbus_irq;
197 }
198
199 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
200 usb_irq_handler,
201 IRQF_TRIGGER_RISING |
202 IRQF_TRIGGER_FALLING |
203 IRQF_ONESHOT,
204 pdev->name, info);
205 if (ret < 0) {
206 dev_err(dev, "failed to request handler for VBUS IRQ\n");
207 return ret;
208 }
Roger Quadrose52817f2015-02-02 12:21:59 +0200209 }
210
Roger Quadrose52817f2015-02-02 12:21:59 +0200211 platform_set_drvdata(pdev, info);
Grygorii Strashko12bd0f32016-04-06 20:32:37 +0300212 device_init_wakeup(dev, true);
Roger Quadrose52817f2015-02-02 12:21:59 +0200213
Vijayavardhan Vennapusa71275502018-11-02 13:41:31 +0530214 if (info->trig_gpiod)
215 /* Schedule with delay to reset ethernet bridge */
216 queue_delayed_work(system_power_efficient_wq,
217 &info->wq_detcable, msecs_to_jiffies(1500));
218 else
219 /* Perform initial detection */
220 usb_extcon_detect_cable(&info->wq_detcable.work);
Roger Quadrose52817f2015-02-02 12:21:59 +0200221
222 return 0;
223}
224
225static int usb_extcon_remove(struct platform_device *pdev)
226{
227 struct usb_extcon_info *info = platform_get_drvdata(pdev);
228
229 cancel_delayed_work_sync(&info->wq_detcable);
Grygorii Strashko12bd0f32016-04-06 20:32:37 +0300230 device_init_wakeup(&pdev->dev, false);
231
Roger Quadrose52817f2015-02-02 12:21:59 +0200232 return 0;
233}
234
235#ifdef CONFIG_PM_SLEEP
236static int usb_extcon_suspend(struct device *dev)
237{
238 struct usb_extcon_info *info = dev_get_drvdata(dev);
239 int ret = 0;
240
Roger Quadros1c936712016-09-20 17:53:55 +0300241 if (device_may_wakeup(dev)) {
242 if (info->id_gpiod) {
243 ret = enable_irq_wake(info->id_irq);
244 if (ret)
245 return ret;
246 }
247 if (info->vbus_gpiod) {
248 ret = enable_irq_wake(info->vbus_irq);
249 if (ret) {
250 if (info->id_gpiod)
251 disable_irq_wake(info->id_irq);
252
253 return ret;
254 }
255 }
256 }
257
Roger Quadrose52817f2015-02-02 12:21:59 +0200258 /*
259 * We don't want to process any IRQs after this point
260 * as GPIOs used behind I2C subsystem might not be
261 * accessible until resume completes. So disable IRQ.
262 */
Roger Quadros1c936712016-09-20 17:53:55 +0300263 if (info->id_gpiod)
264 disable_irq(info->id_irq);
265 if (info->vbus_gpiod)
266 disable_irq(info->vbus_irq);
Roger Quadrose52817f2015-02-02 12:21:59 +0200267
268 return ret;
269}
270
271static int usb_extcon_resume(struct device *dev)
272{
273 struct usb_extcon_info *info = dev_get_drvdata(dev);
274 int ret = 0;
275
Roger Quadros1c936712016-09-20 17:53:55 +0300276 if (device_may_wakeup(dev)) {
277 if (info->id_gpiod) {
278 ret = disable_irq_wake(info->id_irq);
279 if (ret)
280 return ret;
281 }
282 if (info->vbus_gpiod) {
283 ret = disable_irq_wake(info->vbus_irq);
284 if (ret) {
285 if (info->id_gpiod)
286 enable_irq_wake(info->id_irq);
287
288 return ret;
289 }
290 }
291 }
292
293 if (info->id_gpiod)
294 enable_irq(info->id_irq);
295 if (info->vbus_gpiod)
296 enable_irq(info->vbus_irq);
297
Sriharsha Allenki9a477732018-06-01 16:00:01 +0530298 queue_delayed_work(system_power_efficient_wq,
299 &info->wq_detcable, 0);
Roger Quadrose52817f2015-02-02 12:21:59 +0200300
301 return ret;
302}
303#endif
304
305static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
306 usb_extcon_suspend, usb_extcon_resume);
307
Chanwoo Choi34825e52015-03-07 01:41:36 +0900308static const struct of_device_id usb_extcon_dt_match[] = {
Roger Quadrose52817f2015-02-02 12:21:59 +0200309 { .compatible = "linux,extcon-usb-gpio", },
310 { /* sentinel */ }
311};
312MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
313
Lu Baolu058b6652016-04-25 16:04:47 +0800314static const struct platform_device_id usb_extcon_platform_ids[] = {
315 { .name = "extcon-usb-gpio", },
316 { /* sentinel */ }
317};
318MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
319
Roger Quadrose52817f2015-02-02 12:21:59 +0200320static struct platform_driver usb_extcon_driver = {
321 .probe = usb_extcon_probe,
322 .remove = usb_extcon_remove,
323 .driver = {
324 .name = "extcon-usb-gpio",
325 .pm = &usb_extcon_pm_ops,
326 .of_match_table = usb_extcon_dt_match,
327 },
Lu Baolu058b6652016-04-25 16:04:47 +0800328 .id_table = usb_extcon_platform_ids,
Roger Quadrose52817f2015-02-02 12:21:59 +0200329};
330
331module_platform_driver(usb_extcon_driver);
332
333MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
334MODULE_DESCRIPTION("USB GPIO extcon driver");
335MODULE_LICENSE("GPL v2");