Merge tag 'metag-v3.9-rc1-v4' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan...
[pandora-kernel.git] / drivers / usb / otg / mxs-phy.c
1 /*
2  * Copyright 2012 Freescale Semiconductor, Inc.
3  * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4  * on behalf of DENX Software Engineering GmbH
5  *
6  * The code contained herein is licensed under the GNU General Public
7  * License. You may obtain a copy of the GNU General Public License
8  * Version 2 or later at the following locations:
9  *
10  * http://www.opensource.org/licenses/gpl-license.html
11  * http://www.gnu.org/copyleft/gpl.html
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/clk.h>
18 #include <linux/usb/otg.h>
19 #include <linux/stmp_device.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/io.h>
23
24 #define DRIVER_NAME "mxs_phy"
25
26 #define HW_USBPHY_PWD                           0x00
27 #define HW_USBPHY_CTRL                          0x30
28 #define HW_USBPHY_CTRL_SET                      0x34
29 #define HW_USBPHY_CTRL_CLR                      0x38
30
31 #define BM_USBPHY_CTRL_SFTRST                   BIT(31)
32 #define BM_USBPHY_CTRL_CLKGATE                  BIT(30)
33 #define BM_USBPHY_CTRL_ENUTMILEVEL3             BIT(15)
34 #define BM_USBPHY_CTRL_ENUTMILEVEL2             BIT(14)
35 #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT       BIT(1)
36
37 struct mxs_phy {
38         struct usb_phy phy;
39         struct clk *clk;
40 };
41
42 #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
43
44 static void mxs_phy_hw_init(struct mxs_phy *mxs_phy)
45 {
46         void __iomem *base = mxs_phy->phy.io_priv;
47
48         stmp_reset_block(base + HW_USBPHY_CTRL);
49
50         /* Power up the PHY */
51         writel_relaxed(0, base + HW_USBPHY_PWD);
52
53         /* enable FS/LS device */
54         writel_relaxed(BM_USBPHY_CTRL_ENUTMILEVEL2 |
55                         BM_USBPHY_CTRL_ENUTMILEVEL3,
56                         base + HW_USBPHY_CTRL_SET);
57 }
58
59 static int mxs_phy_init(struct usb_phy *phy)
60 {
61         struct mxs_phy *mxs_phy = to_mxs_phy(phy);
62
63         clk_prepare_enable(mxs_phy->clk);
64         mxs_phy_hw_init(mxs_phy);
65
66         return 0;
67 }
68
69 static void mxs_phy_shutdown(struct usb_phy *phy)
70 {
71         struct mxs_phy *mxs_phy = to_mxs_phy(phy);
72
73         writel_relaxed(BM_USBPHY_CTRL_CLKGATE,
74                         phy->io_priv + HW_USBPHY_CTRL_SET);
75
76         clk_disable_unprepare(mxs_phy->clk);
77 }
78
79 static int mxs_phy_suspend(struct usb_phy *x, int suspend)
80 {
81         struct mxs_phy *mxs_phy = to_mxs_phy(x);
82
83         if (suspend) {
84                 writel_relaxed(0xffffffff, x->io_priv + HW_USBPHY_PWD);
85                 writel_relaxed(BM_USBPHY_CTRL_CLKGATE,
86                         x->io_priv + HW_USBPHY_CTRL_SET);
87                 clk_disable_unprepare(mxs_phy->clk);
88         } else {
89                 clk_prepare_enable(mxs_phy->clk);
90                 writel_relaxed(BM_USBPHY_CTRL_CLKGATE,
91                         x->io_priv + HW_USBPHY_CTRL_CLR);
92                 writel_relaxed(0, x->io_priv + HW_USBPHY_PWD);
93         }
94
95         return 0;
96 }
97
98 static int mxs_phy_on_connect(struct usb_phy *phy,
99                 enum usb_device_speed speed)
100 {
101         dev_dbg(phy->dev, "%s speed device has connected\n",
102                 (speed == USB_SPEED_HIGH) ? "high" : "non-high");
103
104         if (speed == USB_SPEED_HIGH)
105                 writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
106                                 phy->io_priv + HW_USBPHY_CTRL_SET);
107
108         return 0;
109 }
110
111 static int mxs_phy_on_disconnect(struct usb_phy *phy,
112                 enum usb_device_speed speed)
113 {
114         dev_dbg(phy->dev, "%s speed device has disconnected\n",
115                 (speed == USB_SPEED_HIGH) ? "high" : "non-high");
116
117         if (speed == USB_SPEED_HIGH)
118                 writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
119                                 phy->io_priv + HW_USBPHY_CTRL_CLR);
120
121         return 0;
122 }
123
124 static int mxs_phy_probe(struct platform_device *pdev)
125 {
126         struct resource *res;
127         void __iomem *base;
128         struct clk *clk;
129         struct mxs_phy *mxs_phy;
130
131         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132         if (!res) {
133                 dev_err(&pdev->dev, "can't get device resources\n");
134                 return -ENOENT;
135         }
136
137         base = devm_ioremap_resource(&pdev->dev, res);
138         if (IS_ERR(base))
139                 return PTR_ERR(base);
140
141         clk = devm_clk_get(&pdev->dev, NULL);
142         if (IS_ERR(clk)) {
143                 dev_err(&pdev->dev,
144                         "can't get the clock, err=%ld", PTR_ERR(clk));
145                 return PTR_ERR(clk);
146         }
147
148         mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
149         if (!mxs_phy) {
150                 dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
151                 return -ENOMEM;
152         }
153
154         mxs_phy->phy.io_priv            = base;
155         mxs_phy->phy.dev                = &pdev->dev;
156         mxs_phy->phy.label              = DRIVER_NAME;
157         mxs_phy->phy.init               = mxs_phy_init;
158         mxs_phy->phy.shutdown           = mxs_phy_shutdown;
159         mxs_phy->phy.set_suspend        = mxs_phy_suspend;
160         mxs_phy->phy.notify_connect     = mxs_phy_on_connect;
161         mxs_phy->phy.notify_disconnect  = mxs_phy_on_disconnect;
162
163         ATOMIC_INIT_NOTIFIER_HEAD(&mxs_phy->phy.notifier);
164
165         mxs_phy->clk = clk;
166
167         platform_set_drvdata(pdev, &mxs_phy->phy);
168
169         return 0;
170 }
171
172 static int mxs_phy_remove(struct platform_device *pdev)
173 {
174         platform_set_drvdata(pdev, NULL);
175
176         return 0;
177 }
178
179 static const struct of_device_id mxs_phy_dt_ids[] = {
180         { .compatible = "fsl,imx23-usbphy", },
181         { /* sentinel */ }
182 };
183 MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
184
185 static struct platform_driver mxs_phy_driver = {
186         .probe = mxs_phy_probe,
187         .remove = mxs_phy_remove,
188         .driver = {
189                 .name = DRIVER_NAME,
190                 .owner  = THIS_MODULE,
191                 .of_match_table = mxs_phy_dt_ids,
192          },
193 };
194
195 static int __init mxs_phy_module_init(void)
196 {
197         return platform_driver_register(&mxs_phy_driver);
198 }
199 postcore_initcall(mxs_phy_module_init);
200
201 static void __exit mxs_phy_module_exit(void)
202 {
203         platform_driver_unregister(&mxs_phy_driver);
204 }
205 module_exit(mxs_phy_module_exit);
206
207 MODULE_ALIAS("platform:mxs-usb-phy");
208 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
209 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
210 MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
211 MODULE_LICENSE("GPL");