extcon: Redefine the unique id of supported external connectors without 'enum extcon...
[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 static const unsigned int usb_extcon_cable[] = {
43         EXTCON_USB,
44         EXTCON_USB_HOST,
45         EXTCON_NONE,
46 };
47
48 static 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                  */
63                 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, false);
64                 extcon_set_cable_state_(info->edev, EXTCON_USB, true);
65         } 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                  */
71                 extcon_set_cable_state_(info->edev, EXTCON_USB, false);
72                 extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, true);
73         }
74 }
75
76 static 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
86 static 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;
101         info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
102         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
107         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
119         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
142         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
151 static 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
161 static 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
182 static 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
199 static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
200                          usb_extcon_suspend, usb_extcon_resume);
201
202 static const struct of_device_id usb_extcon_dt_match[] = {
203         { .compatible = "linux,extcon-usb-gpio", },
204         { /* sentinel */ }
205 };
206 MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
207
208 static 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
218 module_platform_driver(usb_extcon_driver);
219
220 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
221 MODULE_DESCRIPTION("USB GPIO extcon driver");
222 MODULE_LICENSE("GPL v2");