USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / host / ohci-spear.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * OHCI HCD (Host Controller Driver) for USB.
4 *
5 * Copyright (C) 2010 ST Microelectronics.
6 * Deepak Sikri<deepak.sikri@st.com>
7 *
8 * Based on various ohci-*.c drivers
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/io.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/platform_device.h>
22 #include <linux/signal.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25
26 #include "ohci.h"
27
28 #define DRIVER_DESC "OHCI SPEAr driver"
29
30 static const char hcd_name[] = "SPEAr-ohci";
31 struct spear_ohci {
32         struct clk *clk;
33 };
34
35 #define to_spear_ohci(hcd)     (struct spear_ohci *)(hcd_to_ohci(hcd)->priv)
36
37 static struct hc_driver __read_mostly ohci_spear_hc_driver;
38
39 static int spear_ohci_hcd_drv_probe(struct platform_device *pdev)
40 {
41         const struct hc_driver *driver = &ohci_spear_hc_driver;
42         struct ohci_hcd *ohci;
43         struct usb_hcd *hcd = NULL;
44         struct clk *usbh_clk;
45         struct spear_ohci *sohci_p;
46         struct resource *res;
47         int retval, irq;
48
49         irq = platform_get_irq(pdev, 0);
50         if (irq < 0) {
51                 retval = irq;
52                 goto fail;
53         }
54
55         /*
56          * Right now device-tree probed devices don't get dma_mask set.
57          * Since shared usb code relies on it, set it here for now.
58          * Once we have dma capability bindings this can go away.
59          */
60         retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
61         if (retval)
62                 goto fail;
63
64         usbh_clk = devm_clk_get(&pdev->dev, NULL);
65         if (IS_ERR(usbh_clk)) {
66                 dev_err(&pdev->dev, "Error getting interface clock\n");
67                 retval = PTR_ERR(usbh_clk);
68                 goto fail;
69         }
70
71         hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
72         if (!hcd) {
73                 retval = -ENOMEM;
74                 goto fail;
75         }
76
77         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
78         hcd->regs = devm_ioremap_resource(&pdev->dev, res);
79         if (IS_ERR(hcd->regs)) {
80                 retval = PTR_ERR(hcd->regs);
81                 goto err_put_hcd;
82         }
83
84         hcd->rsrc_start = pdev->resource[0].start;
85         hcd->rsrc_len = resource_size(res);
86
87         sohci_p = to_spear_ohci(hcd);
88         sohci_p->clk = usbh_clk;
89
90         clk_prepare_enable(sohci_p->clk);
91
92         ohci = hcd_to_ohci(hcd);
93
94         retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0);
95         if (retval == 0) {
96                 device_wakeup_enable(hcd->self.controller);
97                 return retval;
98         }
99
100         clk_disable_unprepare(sohci_p->clk);
101 err_put_hcd:
102         usb_put_hcd(hcd);
103 fail:
104         dev_err(&pdev->dev, "init fail, %d\n", retval);
105
106         return retval;
107 }
108
109 static int spear_ohci_hcd_drv_remove(struct platform_device *pdev)
110 {
111         struct usb_hcd *hcd = platform_get_drvdata(pdev);
112         struct spear_ohci *sohci_p = to_spear_ohci(hcd);
113
114         usb_remove_hcd(hcd);
115         if (sohci_p->clk)
116                 clk_disable_unprepare(sohci_p->clk);
117
118         usb_put_hcd(hcd);
119         return 0;
120 }
121
122 #if defined(CONFIG_PM)
123 static int spear_ohci_hcd_drv_suspend(struct platform_device *pdev,
124                 pm_message_t message)
125 {
126         struct usb_hcd *hcd = platform_get_drvdata(pdev);
127         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
128         struct spear_ohci *sohci_p = to_spear_ohci(hcd);
129         bool do_wakeup = device_may_wakeup(&pdev->dev);
130         int ret;
131
132         if (time_before(jiffies, ohci->next_statechange))
133                 msleep(5);
134         ohci->next_statechange = jiffies;
135
136         ret = ohci_suspend(hcd, do_wakeup);
137         if (ret)
138                 return ret;
139
140         clk_disable_unprepare(sohci_p->clk);
141
142         return ret;
143 }
144
145 static int spear_ohci_hcd_drv_resume(struct platform_device *dev)
146 {
147         struct usb_hcd *hcd = platform_get_drvdata(dev);
148         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
149         struct spear_ohci *sohci_p = to_spear_ohci(hcd);
150
151         if (time_before(jiffies, ohci->next_statechange))
152                 msleep(5);
153         ohci->next_statechange = jiffies;
154
155         clk_prepare_enable(sohci_p->clk);
156         ohci_resume(hcd, false);
157         return 0;
158 }
159 #endif
160
161 static const struct of_device_id spear_ohci_id_table[] = {
162         { .compatible = "st,spear600-ohci", },
163         { },
164 };
165 MODULE_DEVICE_TABLE(of, spear_ohci_id_table);
166
167 /* Driver definition to register with the platform bus */
168 static struct platform_driver spear_ohci_hcd_driver = {
169         .probe =        spear_ohci_hcd_drv_probe,
170         .remove =       spear_ohci_hcd_drv_remove,
171 #ifdef CONFIG_PM
172         .suspend =      spear_ohci_hcd_drv_suspend,
173         .resume =       spear_ohci_hcd_drv_resume,
174 #endif
175         .driver = {
176                 .name = "spear-ohci",
177                 .of_match_table = spear_ohci_id_table,
178         },
179 };
180
181 static const struct ohci_driver_overrides spear_overrides __initconst = {
182         .extra_priv_size = sizeof(struct spear_ohci),
183 };
184 static int __init ohci_spear_init(void)
185 {
186         if (usb_disabled())
187                 return -ENODEV;
188
189         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
190
191         ohci_init_driver(&ohci_spear_hc_driver, &spear_overrides);
192         return platform_driver_register(&spear_ohci_hcd_driver);
193 }
194 module_init(ohci_spear_init);
195
196 static void __exit ohci_spear_cleanup(void)
197 {
198         platform_driver_unregister(&spear_ohci_hcd_driver);
199 }
200 module_exit(ohci_spear_cleanup);
201
202 MODULE_DESCRIPTION(DRIVER_DESC);
203 MODULE_AUTHOR("Deepak Sikri");
204 MODULE_LICENSE("GPL v2");
205 MODULE_ALIAS("platform:spear-ohci");