900744b978fc795f9d5217204db8406ce9555dec
[cascardo/linux.git] / drivers / extcon / extcon-usb-gpio.c
1 /**
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>
18 #include <linux/gpio/consumer.h>
19 #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
31 struct 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
42 /* List of detectable cables */
43 enum {
44         EXTCON_CABLE_USB = 0,
45         EXTCON_CABLE_USB_HOST,
46
47         EXTCON_CABLE_END,
48 };
49
50 static const char *usb_extcon_cable[] = {
51         [EXTCON_CABLE_USB] = "USB",
52         [EXTCON_CABLE_USB_HOST] = "USB-HOST",
53         NULL,
54 };
55
56 static void usb_extcon_detect_cable(struct work_struct *work)
57 {
58         int id;
59         struct usb_extcon_info *info = container_of(to_delayed_work(work),
60                                                     struct usb_extcon_info,
61                                                     wq_detcable);
62
63         /* check ID and update cable state */
64         id = gpiod_get_value_cansleep(info->id_gpiod);
65         if (id) {
66                 /*
67                  * ID = 1 means USB HOST cable detached.
68                  * As we don't have event for USB peripheral cable attached,
69                  * we simulate USB peripheral attach here.
70                  */
71                 extcon_set_cable_state(info->edev,
72                                        usb_extcon_cable[EXTCON_CABLE_USB_HOST],
73                                        false);
74                 extcon_set_cable_state(info->edev,
75                                        usb_extcon_cable[EXTCON_CABLE_USB],
76                                        true);
77         } else {
78                 /*
79                  * ID = 0 means USB HOST cable attached.
80                  * As we don't have event for USB peripheral cable detached,
81                  * we simulate USB peripheral detach here.
82                  */
83                 extcon_set_cable_state(info->edev,
84                                        usb_extcon_cable[EXTCON_CABLE_USB],
85                                        false);
86                 extcon_set_cable_state(info->edev,
87                                        usb_extcon_cable[EXTCON_CABLE_USB_HOST],
88                                        true);
89         }
90 }
91
92 static irqreturn_t usb_irq_handler(int irq, void *dev_id)
93 {
94         struct usb_extcon_info *info = dev_id;
95
96         queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
97                            info->debounce_jiffies);
98
99         return IRQ_HANDLED;
100 }
101
102 static int usb_extcon_probe(struct platform_device *pdev)
103 {
104         struct device *dev = &pdev->dev;
105         struct device_node *np = dev->of_node;
106         struct usb_extcon_info *info;
107         int ret;
108
109         if (!np)
110                 return -EINVAL;
111
112         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
113         if (!info)
114                 return -ENOMEM;
115
116         info->dev = dev;
117         info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
118         if (IS_ERR(info->id_gpiod)) {
119                 dev_err(dev, "failed to get ID GPIO\n");
120                 return PTR_ERR(info->id_gpiod);
121         }
122
123         info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
124         if (IS_ERR(info->edev)) {
125                 dev_err(dev, "failed to allocate extcon device\n");
126                 return -ENOMEM;
127         }
128
129         ret = devm_extcon_dev_register(dev, info->edev);
130         if (ret < 0) {
131                 dev_err(dev, "failed to register extcon device\n");
132                 return ret;
133         }
134
135         ret = gpiod_set_debounce(info->id_gpiod,
136                                  USB_GPIO_DEBOUNCE_MS * 1000);
137         if (ret < 0)
138                 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
139
140         INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
141
142         info->id_irq = gpiod_to_irq(info->id_gpiod);
143         if (info->id_irq < 0) {
144                 dev_err(dev, "failed to get ID IRQ\n");
145                 return info->id_irq;
146         }
147
148         ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
149                                         usb_irq_handler,
150                                         IRQF_TRIGGER_RISING |
151                                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
152                                         pdev->name, info);
153         if (ret < 0) {
154                 dev_err(dev, "failed to request handler for ID IRQ\n");
155                 return ret;
156         }
157
158         platform_set_drvdata(pdev, info);
159         device_init_wakeup(dev, 1);
160
161         /* Perform initial detection */
162         usb_extcon_detect_cable(&info->wq_detcable.work);
163
164         return 0;
165 }
166
167 static int usb_extcon_remove(struct platform_device *pdev)
168 {
169         struct usb_extcon_info *info = platform_get_drvdata(pdev);
170
171         cancel_delayed_work_sync(&info->wq_detcable);
172
173         return 0;
174 }
175
176 #ifdef CONFIG_PM_SLEEP
177 static int usb_extcon_suspend(struct device *dev)
178 {
179         struct usb_extcon_info *info = dev_get_drvdata(dev);
180         int ret = 0;
181
182         if (device_may_wakeup(dev)) {
183                 ret = enable_irq_wake(info->id_irq);
184                 if (ret)
185                         return ret;
186         }
187
188         /*
189          * We don't want to process any IRQs after this point
190          * as GPIOs used behind I2C subsystem might not be
191          * accessible until resume completes. So disable IRQ.
192          */
193         disable_irq(info->id_irq);
194
195         return ret;
196 }
197
198 static int usb_extcon_resume(struct device *dev)
199 {
200         struct usb_extcon_info *info = dev_get_drvdata(dev);
201         int ret = 0;
202
203         if (device_may_wakeup(dev)) {
204                 ret = disable_irq_wake(info->id_irq);
205                 if (ret)
206                         return ret;
207         }
208
209         enable_irq(info->id_irq);
210
211         return ret;
212 }
213 #endif
214
215 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
216                          usb_extcon_suspend, usb_extcon_resume);
217
218 static const struct of_device_id usb_extcon_dt_match[] = {
219         { .compatible = "linux,extcon-usb-gpio", },
220         { /* sentinel */ }
221 };
222 MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
223
224 static struct platform_driver usb_extcon_driver = {
225         .probe          = usb_extcon_probe,
226         .remove         = usb_extcon_remove,
227         .driver         = {
228                 .name   = "extcon-usb-gpio",
229                 .pm     = &usb_extcon_pm_ops,
230                 .of_match_table = usb_extcon_dt_match,
231         },
232 };
233
234 module_platform_driver(usb_extcon_driver);
235
236 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
237 MODULE_DESCRIPTION("USB GPIO extcon driver");
238 MODULE_LICENSE("GPL v2");