x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / extcon / extcon-usb-gpio.c
bloba5e1882b4ca66e370f4c52c7c024290a1685afd7
1 /**
2 * drivers/extcon/extcon-usb-gpio.c - USB GPIO extcon driver
4 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
5 * Author: Roger Quadros <rogerq@ti.com>
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.
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.
17 #include <linux/extcon.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/acpi.h>
30 #include <linux/pinctrl/consumer.h>
32 #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
34 struct usb_extcon_info {
35 struct device *dev;
36 struct extcon_dev *edev;
38 struct gpio_desc *id_gpiod;
39 struct gpio_desc *vbus_gpiod;
40 int id_irq;
41 int vbus_irq;
43 unsigned long debounce_jiffies;
44 struct delayed_work wq_detcable;
47 static const unsigned int usb_extcon_cable[] = {
48 EXTCON_USB,
49 EXTCON_USB_HOST,
50 EXTCON_NONE,
54 * "USB" = VBUS and "USB-HOST" = !ID, so we have:
55 * Both "USB" and "USB-HOST" can't be set as active at the
56 * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive
57 * even if VBUS is on.
59 * State | ID | VBUS
60 * ----------------------------------------
61 * [1] USB | H | H
62 * [2] none | H | L
63 * [3] USB-HOST | L | H
64 * [4] USB-HOST | L | L
66 * In case we have only one of these signals:
67 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1.
68 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID.
70 static void usb_extcon_detect_cable(struct work_struct *work)
72 int id, vbus;
73 struct usb_extcon_info *info = container_of(to_delayed_work(work),
74 struct usb_extcon_info,
75 wq_detcable);
77 /* check ID and VBUS and update cable state */
78 id = info->id_gpiod ?
79 gpiod_get_value_cansleep(info->id_gpiod) : 1;
80 vbus = info->vbus_gpiod ?
81 gpiod_get_value_cansleep(info->vbus_gpiod) : id;
83 /* at first we clean states which are no longer active */
84 if (id)
85 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
86 if (!vbus)
87 extcon_set_state_sync(info->edev, EXTCON_USB, false);
89 if (!id) {
90 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
91 } else {
92 if (vbus)
93 extcon_set_state_sync(info->edev, EXTCON_USB, true);
97 static irqreturn_t usb_irq_handler(int irq, void *dev_id)
99 struct usb_extcon_info *info = dev_id;
101 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
102 info->debounce_jiffies);
104 return IRQ_HANDLED;
107 static int usb_extcon_probe(struct platform_device *pdev)
109 struct device *dev = &pdev->dev;
110 struct device_node *np = dev->of_node;
111 struct usb_extcon_info *info;
112 int ret;
114 if (!np && !ACPI_HANDLE(dev))
115 return -EINVAL;
117 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
118 if (!info)
119 return -ENOMEM;
121 info->dev = dev;
122 info->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id", GPIOD_IN);
123 info->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
124 GPIOD_IN);
126 if (!info->id_gpiod && !info->vbus_gpiod) {
127 dev_err(dev, "failed to get gpios\n");
128 return -ENODEV;
131 if (IS_ERR(info->id_gpiod))
132 return PTR_ERR(info->id_gpiod);
134 if (IS_ERR(info->vbus_gpiod))
135 return PTR_ERR(info->vbus_gpiod);
137 info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
138 if (IS_ERR(info->edev)) {
139 dev_err(dev, "failed to allocate extcon device\n");
140 return -ENOMEM;
143 ret = devm_extcon_dev_register(dev, info->edev);
144 if (ret < 0) {
145 dev_err(dev, "failed to register extcon device\n");
146 return ret;
149 if (info->id_gpiod)
150 ret = gpiod_set_debounce(info->id_gpiod,
151 USB_GPIO_DEBOUNCE_MS * 1000);
152 if (!ret && info->vbus_gpiod)
153 ret = gpiod_set_debounce(info->vbus_gpiod,
154 USB_GPIO_DEBOUNCE_MS * 1000);
156 if (ret < 0)
157 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
159 INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
161 if (info->id_gpiod) {
162 info->id_irq = gpiod_to_irq(info->id_gpiod);
163 if (info->id_irq < 0) {
164 dev_err(dev, "failed to get ID IRQ\n");
165 return info->id_irq;
168 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
169 usb_irq_handler,
170 IRQF_TRIGGER_RISING |
171 IRQF_TRIGGER_FALLING | 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;
179 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;
186 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
187 usb_irq_handler,
188 IRQF_TRIGGER_RISING |
189 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
190 pdev->name, info);
191 if (ret < 0) {
192 dev_err(dev, "failed to request handler for VBUS IRQ\n");
193 return ret;
197 platform_set_drvdata(pdev, info);
198 device_init_wakeup(dev, true);
200 /* Perform initial detection */
201 usb_extcon_detect_cable(&info->wq_detcable.work);
203 return 0;
206 static int usb_extcon_remove(struct platform_device *pdev)
208 struct usb_extcon_info *info = platform_get_drvdata(pdev);
210 cancel_delayed_work_sync(&info->wq_detcable);
211 device_init_wakeup(&pdev->dev, false);
213 return 0;
216 #ifdef CONFIG_PM_SLEEP
217 static int usb_extcon_suspend(struct device *dev)
219 struct usb_extcon_info *info = dev_get_drvdata(dev);
220 int ret = 0;
222 if (device_may_wakeup(dev)) {
223 if (info->id_gpiod) {
224 ret = enable_irq_wake(info->id_irq);
225 if (ret)
226 return ret;
228 if (info->vbus_gpiod) {
229 ret = enable_irq_wake(info->vbus_irq);
230 if (ret) {
231 if (info->id_gpiod)
232 disable_irq_wake(info->id_irq);
234 return ret;
240 * We don't want to process any IRQs after this point
241 * as GPIOs used behind I2C subsystem might not be
242 * accessible until resume completes. So disable IRQ.
244 if (info->id_gpiod)
245 disable_irq(info->id_irq);
246 if (info->vbus_gpiod)
247 disable_irq(info->vbus_irq);
249 if (!device_may_wakeup(dev))
250 pinctrl_pm_select_sleep_state(dev);
252 return ret;
255 static int usb_extcon_resume(struct device *dev)
257 struct usb_extcon_info *info = dev_get_drvdata(dev);
258 int ret = 0;
260 if (!device_may_wakeup(dev))
261 pinctrl_pm_select_default_state(dev);
263 if (device_may_wakeup(dev)) {
264 if (info->id_gpiod) {
265 ret = disable_irq_wake(info->id_irq);
266 if (ret)
267 return ret;
269 if (info->vbus_gpiod) {
270 ret = disable_irq_wake(info->vbus_irq);
271 if (ret) {
272 if (info->id_gpiod)
273 enable_irq_wake(info->id_irq);
275 return ret;
280 if (info->id_gpiod)
281 enable_irq(info->id_irq);
282 if (info->vbus_gpiod)
283 enable_irq(info->vbus_irq);
285 if (!device_may_wakeup(dev))
286 queue_delayed_work(system_power_efficient_wq,
287 &info->wq_detcable, 0);
289 return ret;
291 #endif
293 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
294 usb_extcon_suspend, usb_extcon_resume);
296 static const struct of_device_id usb_extcon_dt_match[] = {
297 { .compatible = "linux,extcon-usb-gpio", },
298 { /* sentinel */ }
300 MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
302 static const struct platform_device_id usb_extcon_platform_ids[] = {
303 { .name = "extcon-usb-gpio", },
304 { /* sentinel */ }
306 MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
308 static struct platform_driver usb_extcon_driver = {
309 .probe = usb_extcon_probe,
310 .remove = usb_extcon_remove,
311 .driver = {
312 .name = "extcon-usb-gpio",
313 .pm = &usb_extcon_pm_ops,
314 .of_match_table = usb_extcon_dt_match,
316 .id_table = usb_extcon_platform_ids,
319 module_platform_driver(usb_extcon_driver);
321 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
322 MODULE_DESCRIPTION("USB GPIO extcon driver");
323 MODULE_LICENSE("GPL v2");