usb: otg: Remove the force_irq hack
[firefly-linux-kernel-4.4.55.git] / drivers / usb / otg / cpcap-otg.c
1 /*
2  * Copyright (C) 2010 Google, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <linux/usb.h>
20 #include <linux/usb/otg.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/platform_device.h>
24 #include <linux/clk.h>
25 #include <linux/io.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28
29 #define TEGRA_USB_PHY_WAKEUP_REG_OFFSET         0x408
30 #define   TEGRA_VBUS_WAKEUP_SW_VALUE            (1 << 12)
31 #define   TEGRA_VBUS_WAKEUP_SW_ENABLE           (1 << 11)
32 #define   TEGRA_ID_SW_VALUE                     (1 << 4)
33 #define   TEGRA_ID_SW_ENABLE                    (1 << 3)
34
35 struct cpcap_otg_data {
36         struct otg_transceiver otg;
37         struct notifier_block nb;
38         void __iomem *regs;
39         struct clk *clk;
40         struct platform_device *host;
41         struct platform_device *pdev;
42 };
43
44 static const char *cpcap_state_name(enum usb_otg_state state)
45 {
46         if (state == OTG_STATE_A_HOST)
47                 return "HOST";
48         if (state == OTG_STATE_B_PERIPHERAL)
49                 return "PERIPHERAL";
50         if (state == OTG_STATE_A_SUSPEND)
51                 return "SUSPEND";
52         return "INVALID";
53 }
54
55 void cpcap_start_host(struct cpcap_otg_data *cpcap)
56 {
57         int retval;
58
59         cpcap->pdev = platform_device_alloc(cpcap->host->name, cpcap->host->id);
60         if (!cpcap->pdev)
61                 return;
62
63         if (cpcap->host->resource) {
64                 retval = platform_device_add_resources(cpcap->pdev,
65                                                 cpcap->host->resource,
66                                                 cpcap->host->num_resources);
67                 if (retval)
68                         goto error;
69         }
70
71         cpcap->pdev->dev.dma_mask = cpcap->host->dev.dma_mask;
72         cpcap->pdev->dev.coherent_dma_mask = cpcap->host->dev.coherent_dma_mask;
73
74         retval = platform_device_add(cpcap->pdev);
75         if (retval)
76                 goto error;
77
78         return;
79 error:
80         pr_err("%s: failed to add the host contoller device\n", __func__);
81         platform_device_put(cpcap->pdev);
82 }
83
84 void cpcap_stop_host(struct cpcap_otg_data *cpcap)
85 {
86         if (cpcap->pdev) {
87                 platform_device_unregister(cpcap->pdev);
88                 cpcap->pdev = NULL;
89         }
90 }
91
92 static int cpcap_otg_notify(struct notifier_block *nb, unsigned long event,
93                             void *ignore)
94 {
95         struct cpcap_otg_data *cpcap;
96         struct otg_transceiver *otg;
97         enum usb_otg_state from;
98         enum usb_otg_state to;
99         unsigned long val;
100         struct usb_hcd *hcd;
101
102         cpcap = container_of(nb, struct cpcap_otg_data, nb);
103         otg = &cpcap->otg;
104
105         from = otg->state;
106         if (event == USB_EVENT_VBUS)
107                 to = OTG_STATE_B_PERIPHERAL;
108         else if (event == USB_EVENT_ID)
109                 to = OTG_STATE_A_HOST;
110         else
111                 to = OTG_STATE_A_SUSPEND;
112
113         if (from == to)
114                 return 0;
115         otg->state = to;
116
117         dev_info(cpcap->otg.dev, "%s --> %s", cpcap_state_name(from),
118                                               cpcap_state_name(to));
119
120         clk_enable(cpcap->clk);
121
122         if ((to == OTG_STATE_A_HOST) && (from == OTG_STATE_A_SUSPEND)
123                         && cpcap->host) {
124                 hcd = (struct usb_hcd *)otg->host;
125
126                 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
127                 val &= ~TEGRA_ID_SW_VALUE;
128                 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
129
130                 cpcap_start_host(cpcap);
131
132         } else if ((to == OTG_STATE_A_SUSPEND) && (from == OTG_STATE_A_HOST)
133                         && cpcap->host) {
134                 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
135                 val |= TEGRA_ID_SW_VALUE;
136                 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
137
138                 cpcap_stop_host(cpcap);
139
140         } else if ((to == OTG_STATE_B_PERIPHERAL)
141                         && (from == OTG_STATE_A_SUSPEND)
142                         && otg->gadget) {
143                 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
144                 val |= TEGRA_VBUS_WAKEUP_SW_VALUE;
145                 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
146
147                 usb_gadget_vbus_connect(otg->gadget);
148
149         } else if ((to == OTG_STATE_A_SUSPEND)
150                         && (from == OTG_STATE_B_PERIPHERAL)
151                         && otg->gadget) {
152                 val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
153                 val &= ~TEGRA_VBUS_WAKEUP_SW_VALUE;
154                 writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
155
156                 usb_gadget_vbus_disconnect(otg->gadget);
157         }
158
159         clk_disable(cpcap->clk);
160
161         return 0;
162 }
163
164 static int cpcap_otg_set_peripheral(struct otg_transceiver *otg,
165                                 struct usb_gadget *gadget)
166 {
167         otg->gadget = gadget;
168         return 0;
169 }
170
171 static int cpcap_otg_set_host(struct otg_transceiver *otg,
172                                 struct usb_bus *host)
173 {
174         otg->host = host;
175         return 0;
176 }
177
178 static int cpcap_otg_set_power(struct otg_transceiver *otg, unsigned mA)
179 {
180         return 0;
181 }
182
183 static int cpcap_otg_set_suspend(struct otg_transceiver *otg, int suspend)
184 {
185         return 0;
186 }
187
188 static int cpcap_otg_probe(struct platform_device *pdev)
189 {
190         struct cpcap_otg_data *cpcap;
191         struct resource *res;
192         unsigned long val;
193         int err;
194
195         cpcap = kzalloc(sizeof(struct cpcap_otg_data), GFP_KERNEL);
196         if (!cpcap)
197                 return -ENOMEM;
198
199         cpcap->otg.dev = &pdev->dev;
200         cpcap->otg.label = "cpcap-otg";
201         cpcap->otg.state = OTG_STATE_UNDEFINED;
202         cpcap->otg.set_host = cpcap_otg_set_host;
203         cpcap->otg.set_peripheral = cpcap_otg_set_peripheral;
204         cpcap->otg.set_suspend = cpcap_otg_set_suspend;
205         cpcap->otg.set_power = cpcap_otg_set_power;
206         cpcap->host = pdev->dev.platform_data;
207
208         platform_set_drvdata(pdev, cpcap);
209
210         cpcap->clk = clk_get(&pdev->dev, NULL);
211         if (IS_ERR(cpcap->clk)) {
212                 dev_err(&pdev->dev, "Can't get otg clock\n");
213                 err = PTR_ERR(cpcap->clk);
214                 goto err_clk;
215         }
216
217         err = clk_enable(cpcap->clk);
218         if (err)
219                 goto err_clken;
220
221         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
222         if (!res) {
223                 dev_err(&pdev->dev, "Failed to get I/O memory\n");
224                 err = -ENXIO;
225                 goto err_io;
226         }
227         cpcap->regs = ioremap(res->start, resource_size(res));
228         if (!cpcap->regs) {
229                 err = -ENOMEM;
230                 goto err_io;
231         }
232
233         val = readl(cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
234         val |= TEGRA_VBUS_WAKEUP_SW_ENABLE | TEGRA_ID_SW_ENABLE;
235         val |= TEGRA_ID_SW_VALUE;
236         val &= ~(TEGRA_VBUS_WAKEUP_SW_VALUE);
237         writel(val, cpcap->regs + TEGRA_USB_PHY_WAKEUP_REG_OFFSET);
238
239         clk_disable(cpcap->clk);
240         cpcap->otg.state = OTG_STATE_A_SUSPEND;
241
242         BLOCKING_INIT_NOTIFIER_HEAD(&cpcap->otg.notifier);
243         cpcap->nb.notifier_call = cpcap_otg_notify;
244         otg_register_notifier(&cpcap->otg, &cpcap->nb);
245
246         err = otg_set_transceiver(&cpcap->otg);
247         if (err) {
248                 dev_err(&pdev->dev, "can't register transceiver (%d)\n", err);
249                 goto err_otg;
250         }
251
252         return 0;
253
254 err_otg:
255         iounmap(cpcap->regs);
256 err_io:
257         clk_disable(cpcap->clk);
258 err_clken:
259         clk_put(cpcap->clk);
260 err_clk:
261         platform_set_drvdata(pdev, NULL);
262         kfree(cpcap);
263         return err;
264 }
265
266 static int __exit cpcap_otg_remove(struct platform_device *pdev)
267 {
268         struct cpcap_otg_data *cpcap = platform_get_drvdata(pdev);
269
270         otg_set_transceiver(NULL);
271         iounmap(cpcap->regs);
272         clk_disable(cpcap->clk);
273         clk_put(cpcap->clk);
274         platform_set_drvdata(pdev, NULL);
275         kfree(cpcap);
276
277         return 0;
278 }
279
280 static struct platform_driver cpcap_otg_driver = {
281         .driver = {
282                 .name  = "cpcap-otg",
283         },
284         .remove  = __exit_p(cpcap_otg_remove),
285         .probe   = cpcap_otg_probe,
286 };
287
288 static int __init cpcap_otg_init(void)
289 {
290         return platform_driver_register(&cpcap_otg_driver);
291 }
292 module_init(cpcap_otg_init);
293
294 static void __exit cpcap_otg_exit(void)
295 {
296         platform_driver_unregister(&cpcap_otg_driver);
297 }
298 module_exit(cpcap_otg_exit);