blob: 2e093c3b41043b8463156539303bd9b699ffb854 [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>
Roger Quadrose52817f2015-02-02 12:21:59 +020030
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;
Roger Quadros1c936712016-09-20 17:53:55 +030038 struct gpio_desc *vbus_gpiod;
Roger Quadrose52817f2015-02-02 12:21:59 +020039 int id_irq;
Roger Quadros1c936712016-09-20 17:53:55 +030040 int vbus_irq;
Roger Quadrose52817f2015-02-02 12:21:59 +020041
42 unsigned long debounce_jiffies;
43 struct delayed_work wq_detcable;
44};
45
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090046static const unsigned int usb_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090047 EXTCON_USB,
48 EXTCON_USB_HOST,
49 EXTCON_NONE,
Roger Quadrose52817f2015-02-02 12:21:59 +020050};
51
Roger Quadros1c936712016-09-20 17:53:55 +030052/*
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 */
Roger Quadrose52817f2015-02-02 12:21:59 +020069static void usb_extcon_detect_cable(struct work_struct *work)
70{
Roger Quadros1c936712016-09-20 17:53:55 +030071 int id, vbus;
Roger Quadrose52817f2015-02-02 12:21:59 +020072 struct usb_extcon_info *info = container_of(to_delayed_work(work),
73 struct usb_extcon_info,
74 wq_detcable);
75
Roger Quadros1c936712016-09-20 17:53:55 +030076 /* check ID and VBUS and update cable state */
77 id = info->id_gpiod ?
78 gpiod_get_value_cansleep(info->id_gpiod) : 1;
79 vbus = info->vbus_gpiod ?
80 gpiod_get_value_cansleep(info->vbus_gpiod) : id;
81
82 /* at first we clean states which are no longer active */
83 if (id)
Chanwoo Choi8670b452016-08-16 15:55:34 +090084 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
Roger Quadros1c936712016-09-20 17:53:55 +030085 if (!vbus)
Chanwoo Choi8670b452016-08-16 15:55:34 +090086 extcon_set_state_sync(info->edev, EXTCON_USB, false);
Roger Quadros1c936712016-09-20 17:53:55 +030087
88 if (!id) {
Chanwoo Choi8670b452016-08-16 15:55:34 +090089 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
Roger Quadros1c936712016-09-20 17:53:55 +030090 } else {
91 if (vbus)
92 extcon_set_state_sync(info->edev, EXTCON_USB, true);
Roger Quadrose52817f2015-02-02 12:21:59 +020093 }
94}
95
96static irqreturn_t usb_irq_handler(int irq, void *dev_id)
97{
98 struct usb_extcon_info *info = dev_id;
99
100 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
101 info->debounce_jiffies);
102
103 return IRQ_HANDLED;
104}
105
106static int usb_extcon_probe(struct platform_device *pdev)
107{
108 struct device *dev = &pdev->dev;
109 struct device_node *np = dev->of_node;
110 struct usb_extcon_info *info;
111 int ret;
112
Lu Baolu942c7922016-04-26 08:35:54 +0900113 if (!np && !ACPI_HANDLE(dev))
Roger Quadrose52817f2015-02-02 12:21:59 +0200114 return -EINVAL;
115
116 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
117 if (!info)
118 return -ENOMEM;
119
120 info->dev = dev;
Roger Quadros1c936712016-09-20 17:53:55 +0300121 info->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", GPIOD_IN);
122 info->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
123 GPIOD_IN);
124
125 if (!info->id_gpiod && !info->vbus_gpiod) {
126 dev_err(dev, "failed to get gpios\n");
127 return -ENODEV;
Roger Quadrose52817f2015-02-02 12:21:59 +0200128 }
129
Roger Quadros1c936712016-09-20 17:53:55 +0300130 if (IS_ERR(info->id_gpiod))
131 return PTR_ERR(info->id_gpiod);
132
133 if (IS_ERR(info->vbus_gpiod))
134 return PTR_ERR(info->vbus_gpiod);
135
Robert Baldygabc1aaba2015-04-02 15:13:02 +0200136 info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
137 if (IS_ERR(info->edev)) {
138 dev_err(dev, "failed to allocate extcon device\n");
139 return -ENOMEM;
140 }
141
142 ret = devm_extcon_dev_register(dev, info->edev);
143 if (ret < 0) {
144 dev_err(dev, "failed to register extcon device\n");
145 return ret;
146 }
147
Roger Quadros1c936712016-09-20 17:53:55 +0300148 if (info->id_gpiod)
149 ret = gpiod_set_debounce(info->id_gpiod,
150 USB_GPIO_DEBOUNCE_MS * 1000);
151 if (!ret && info->vbus_gpiod)
152 ret = gpiod_set_debounce(info->vbus_gpiod,
153 USB_GPIO_DEBOUNCE_MS * 1000);
154
Roger Quadrose52817f2015-02-02 12:21:59 +0200155 if (ret < 0)
156 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
157
158 INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
159
Roger Quadros1c936712016-09-20 17:53:55 +0300160 if (info->id_gpiod) {
161 info->id_irq = gpiod_to_irq(info->id_gpiod);
162 if (info->id_irq < 0) {
163 dev_err(dev, "failed to get ID IRQ\n");
164 return info->id_irq;
165 }
166
167 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
168 usb_irq_handler,
169 IRQF_TRIGGER_RISING |
170 IRQF_TRIGGER_FALLING |
171 IRQF_ONESHOT,
172 pdev->name, info);
173 if (ret < 0) {
174 dev_err(dev, "failed to request handler for ID IRQ\n");
175 return ret;
176 }
Roger Quadrose52817f2015-02-02 12:21:59 +0200177 }
178
Roger Quadros1c936712016-09-20 17:53:55 +0300179 if (info->vbus_gpiod) {
180 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
181 if (info->vbus_irq < 0) {
182 dev_err(dev, "failed to get VBUS IRQ\n");
183 return info->vbus_irq;
184 }
185
186 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
187 usb_irq_handler,
188 IRQF_TRIGGER_RISING |
189 IRQF_TRIGGER_FALLING |
190 IRQF_ONESHOT,
191 pdev->name, info);
192 if (ret < 0) {
193 dev_err(dev, "failed to request handler for VBUS IRQ\n");
194 return ret;
195 }
Roger Quadrose52817f2015-02-02 12:21:59 +0200196 }
197
Roger Quadrose52817f2015-02-02 12:21:59 +0200198 platform_set_drvdata(pdev, info);
Grygorii Strashko12bd0f32016-04-06 20:32:37 +0300199 device_init_wakeup(dev, true);
Roger Quadrose52817f2015-02-02 12:21:59 +0200200
201 /* Perform initial detection */
202 usb_extcon_detect_cable(&info->wq_detcable.work);
203
204 return 0;
205}
206
207static int usb_extcon_remove(struct platform_device *pdev)
208{
209 struct usb_extcon_info *info = platform_get_drvdata(pdev);
210
211 cancel_delayed_work_sync(&info->wq_detcable);
Grygorii Strashko12bd0f32016-04-06 20:32:37 +0300212 device_init_wakeup(&pdev->dev, false);
213
Roger Quadrose52817f2015-02-02 12:21:59 +0200214 return 0;
215}
216
217#ifdef CONFIG_PM_SLEEP
218static int usb_extcon_suspend(struct device *dev)
219{
220 struct usb_extcon_info *info = dev_get_drvdata(dev);
221 int ret = 0;
222
Roger Quadros1c936712016-09-20 17:53:55 +0300223 if (device_may_wakeup(dev)) {
224 if (info->id_gpiod) {
225 ret = enable_irq_wake(info->id_irq);
226 if (ret)
227 return ret;
228 }
229 if (info->vbus_gpiod) {
230 ret = enable_irq_wake(info->vbus_irq);
231 if (ret) {
232 if (info->id_gpiod)
233 disable_irq_wake(info->id_irq);
234
235 return ret;
236 }
237 }
238 }
239
Roger Quadrose52817f2015-02-02 12:21:59 +0200240 /*
241 * We don't want to process any IRQs after this point
242 * as GPIOs used behind I2C subsystem might not be
243 * accessible until resume completes. So disable IRQ.
244 */
Roger Quadros1c936712016-09-20 17:53:55 +0300245 if (info->id_gpiod)
246 disable_irq(info->id_irq);
247 if (info->vbus_gpiod)
248 disable_irq(info->vbus_irq);
Roger Quadrose52817f2015-02-02 12:21:59 +0200249
250 return ret;
251}
252
253static int usb_extcon_resume(struct device *dev)
254{
255 struct usb_extcon_info *info = dev_get_drvdata(dev);
256 int ret = 0;
257
Roger Quadros1c936712016-09-20 17:53:55 +0300258 if (device_may_wakeup(dev)) {
259 if (info->id_gpiod) {
260 ret = disable_irq_wake(info->id_irq);
261 if (ret)
262 return ret;
263 }
264 if (info->vbus_gpiod) {
265 ret = disable_irq_wake(info->vbus_irq);
266 if (ret) {
267 if (info->id_gpiod)
268 enable_irq_wake(info->id_irq);
269
270 return ret;
271 }
272 }
273 }
274
275 if (info->id_gpiod)
276 enable_irq(info->id_irq);
277 if (info->vbus_gpiod)
278 enable_irq(info->vbus_irq);
279
Roger Quadros04c08002016-04-11 17:04:45 +0300280 if (!device_may_wakeup(dev))
281 queue_delayed_work(system_power_efficient_wq,
282 &info->wq_detcable, 0);
Roger Quadrose52817f2015-02-02 12:21:59 +0200283
284 return ret;
285}
286#endif
287
288static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
289 usb_extcon_suspend, usb_extcon_resume);
290
Chanwoo Choi34825e52015-03-07 01:41:36 +0900291static const struct of_device_id usb_extcon_dt_match[] = {
Roger Quadrose52817f2015-02-02 12:21:59 +0200292 { .compatible = "linux,extcon-usb-gpio", },
293 { /* sentinel */ }
294};
295MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
296
Lu Baolu058b6652016-04-25 16:04:47 +0800297static const struct platform_device_id usb_extcon_platform_ids[] = {
298 { .name = "extcon-usb-gpio", },
299 { /* sentinel */ }
300};
301MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
302
Roger Quadrose52817f2015-02-02 12:21:59 +0200303static struct platform_driver usb_extcon_driver = {
304 .probe = usb_extcon_probe,
305 .remove = usb_extcon_remove,
306 .driver = {
307 .name = "extcon-usb-gpio",
308 .pm = &usb_extcon_pm_ops,
309 .of_match_table = usb_extcon_dt_match,
310 },
Lu Baolu058b6652016-04-25 16:04:47 +0800311 .id_table = usb_extcon_platform_ids,
Roger Quadrose52817f2015-02-02 12:21:59 +0200312};
313
314module_platform_driver(usb_extcon_driver);
315
316MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
317MODULE_DESCRIPTION("USB GPIO extcon driver");
318MODULE_LICENSE("GPL v2");