blob: a2a44536a608c2c1e300c8c02b40cd1e96bc5f0e [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>
Geert Uytterhoeven638f9582015-05-05 18:32:20 +020018#include <linux/gpio/consumer.h>
Roger Quadrose52817f2015-02-02 12:21:59 +020019#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/irq.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/of_gpio.h>
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27#include <linux/workqueue.h>
28
29#define USB_GPIO_DEBOUNCE_MS 20 /* ms */
30
31struct usb_extcon_info {
32 struct device *dev;
33 struct extcon_dev *edev;
34
35 struct gpio_desc *id_gpiod;
36 int id_irq;
37
38 unsigned long debounce_jiffies;
39 struct delayed_work wq_detcable;
40};
41
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090042static const unsigned int usb_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090043 EXTCON_USB,
44 EXTCON_USB_HOST,
45 EXTCON_NONE,
Roger Quadrose52817f2015-02-02 12:21:59 +020046};
47
48static void usb_extcon_detect_cable(struct work_struct *work)
49{
50 int id;
51 struct usb_extcon_info *info = container_of(to_delayed_work(work),
52 struct usb_extcon_info,
53 wq_detcable);
54
55 /* check ID and update cable state */
56 id = gpiod_get_value_cansleep(info->id_gpiod);
57 if (id) {
58 /*
59 * ID = 1 means USB HOST cable detached.
60 * As we don't have event for USB peripheral cable attached,
61 * we simulate USB peripheral attach here.
62 */
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090063 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, false);
64 extcon_set_cable_state_(info->edev, EXTCON_USB, true);
Roger Quadrose52817f2015-02-02 12:21:59 +020065 } else {
66 /*
67 * ID = 0 means USB HOST cable attached.
68 * As we don't have event for USB peripheral cable detached,
69 * we simulate USB peripheral detach here.
70 */
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090071 extcon_set_cable_state_(info->edev, EXTCON_USB, false);
72 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, true);
Roger Quadrose52817f2015-02-02 12:21:59 +020073 }
74}
75
76static irqreturn_t usb_irq_handler(int irq, void *dev_id)
77{
78 struct usb_extcon_info *info = dev_id;
79
80 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
81 info->debounce_jiffies);
82
83 return IRQ_HANDLED;
84}
85
86static int usb_extcon_probe(struct platform_device *pdev)
87{
88 struct device *dev = &pdev->dev;
89 struct device_node *np = dev->of_node;
90 struct usb_extcon_info *info;
91 int ret;
92
93 if (!np)
94 return -EINVAL;
95
96 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
97 if (!info)
98 return -ENOMEM;
99
100 info->dev = dev;
Uwe Kleine-König35eed7a2015-05-19 14:18:30 +0200101 info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
Roger Quadrose52817f2015-02-02 12:21:59 +0200102 if (IS_ERR(info->id_gpiod)) {
103 dev_err(dev, "failed to get ID GPIO\n");
104 return PTR_ERR(info->id_gpiod);
105 }
106
Robert Baldygabc1aaba2015-04-02 15:13:02 +0200107 info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
108 if (IS_ERR(info->edev)) {
109 dev_err(dev, "failed to allocate extcon device\n");
110 return -ENOMEM;
111 }
112
113 ret = devm_extcon_dev_register(dev, info->edev);
114 if (ret < 0) {
115 dev_err(dev, "failed to register extcon device\n");
116 return ret;
117 }
118
Roger Quadrose52817f2015-02-02 12:21:59 +0200119 ret = gpiod_set_debounce(info->id_gpiod,
120 USB_GPIO_DEBOUNCE_MS * 1000);
121 if (ret < 0)
122 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
123
124 INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
125
126 info->id_irq = gpiod_to_irq(info->id_gpiod);
127 if (info->id_irq < 0) {
128 dev_err(dev, "failed to get ID IRQ\n");
129 return info->id_irq;
130 }
131
132 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
133 usb_irq_handler,
134 IRQF_TRIGGER_RISING |
135 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
136 pdev->name, info);
137 if (ret < 0) {
138 dev_err(dev, "failed to request handler for ID IRQ\n");
139 return ret;
140 }
141
Roger Quadrose52817f2015-02-02 12:21:59 +0200142 platform_set_drvdata(pdev, info);
143 device_init_wakeup(dev, 1);
144
145 /* Perform initial detection */
146 usb_extcon_detect_cable(&info->wq_detcable.work);
147
148 return 0;
149}
150
151static int usb_extcon_remove(struct platform_device *pdev)
152{
153 struct usb_extcon_info *info = platform_get_drvdata(pdev);
154
155 cancel_delayed_work_sync(&info->wq_detcable);
156
157 return 0;
158}
159
160#ifdef CONFIG_PM_SLEEP
161static int usb_extcon_suspend(struct device *dev)
162{
163 struct usb_extcon_info *info = dev_get_drvdata(dev);
164 int ret = 0;
165
166 if (device_may_wakeup(dev)) {
167 ret = enable_irq_wake(info->id_irq);
168 if (ret)
169 return ret;
170 }
171
172 /*
173 * We don't want to process any IRQs after this point
174 * as GPIOs used behind I2C subsystem might not be
175 * accessible until resume completes. So disable IRQ.
176 */
177 disable_irq(info->id_irq);
178
179 return ret;
180}
181
182static int usb_extcon_resume(struct device *dev)
183{
184 struct usb_extcon_info *info = dev_get_drvdata(dev);
185 int ret = 0;
186
187 if (device_may_wakeup(dev)) {
188 ret = disable_irq_wake(info->id_irq);
189 if (ret)
190 return ret;
191 }
192
193 enable_irq(info->id_irq);
194
195 return ret;
196}
197#endif
198
199static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
200 usb_extcon_suspend, usb_extcon_resume);
201
Chanwoo Choi34825e52015-03-07 01:41:36 +0900202static const struct of_device_id usb_extcon_dt_match[] = {
Roger Quadrose52817f2015-02-02 12:21:59 +0200203 { .compatible = "linux,extcon-usb-gpio", },
204 { /* sentinel */ }
205};
206MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
207
208static struct platform_driver usb_extcon_driver = {
209 .probe = usb_extcon_probe,
210 .remove = usb_extcon_remove,
211 .driver = {
212 .name = "extcon-usb-gpio",
213 .pm = &usb_extcon_pm_ops,
214 .of_match_table = usb_extcon_dt_match,
215 },
216};
217
218module_platform_driver(usb_extcon_driver);
219
220MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
221MODULE_DESCRIPTION("USB GPIO extcon driver");
222MODULE_LICENSE("GPL v2");