blob: eb746051eda4001fffeaad3e4cec940548cea397 [file] [log] [blame]
Richard Zhaob3d99682012-07-07 22:56:47 +08001/*
Peter Chen43f36342014-08-26 10:55:17 +08002 * Copyright 2012-2014 Freescale Semiconductor, Inc.
Richard Zhaob3d99682012-07-07 22:56:47 +08003 * 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>
Peter Chen24007802014-02-24 10:20:54 +080023#include <linux/of_device.h>
Peter Chen0d896532014-02-24 10:20:57 +080024#include <linux/regmap.h>
25#include <linux/mfd/syscon.h>
Richard Zhaob3d99682012-07-07 22:56:47 +080026
27#define DRIVER_NAME "mxs_phy"
28
29#define HW_USBPHY_PWD 0x00
30#define HW_USBPHY_CTRL 0x30
31#define HW_USBPHY_CTRL_SET 0x34
32#define HW_USBPHY_CTRL_CLR 0x38
33
Peter Chen3f126502014-02-24 10:21:02 +080034#define HW_USBPHY_DEBUG_SET 0x54
35#define HW_USBPHY_DEBUG_CLR 0x58
36
Peter Chen22db05e2014-02-24 10:20:59 +080037#define HW_USBPHY_IP 0x90
38#define HW_USBPHY_IP_SET 0x94
39#define HW_USBPHY_IP_CLR 0x98
40
Richard Zhaob3d99682012-07-07 22:56:47 +080041#define BM_USBPHY_CTRL_SFTRST BIT(31)
42#define BM_USBPHY_CTRL_CLKGATE BIT(30)
Li Jun7b09e672015-01-16 18:28:59 +080043#define BM_USBPHY_CTRL_OTG_ID_VALUE BIT(27)
Peter Chen13644142014-02-24 10:20:55 +080044#define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
45#define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
Peter Chen3f126502014-02-24 10:21:02 +080046#define BM_USBPHY_CTRL_ENVBUSCHG_WKUP BIT(23)
47#define BM_USBPHY_CTRL_ENIDCHG_WKUP BIT(22)
48#define BM_USBPHY_CTRL_ENDPDMCHG_WKUP BIT(21)
Peter Chen13644142014-02-24 10:20:55 +080049#define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
50#define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
51#define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
Richard Zhaob3d99682012-07-07 22:56:47 +080052#define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
53#define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
54#define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
55
Peter Chen22db05e2014-02-24 10:20:59 +080056#define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
57
Peter Chen3f126502014-02-24 10:21:02 +080058#define BM_USBPHY_DEBUG_CLKGATE BIT(30)
59
60/* Anatop Registers */
Peter Chenbf783432014-02-24 10:21:03 +080061#define ANADIG_ANA_MISC0 0x150
62#define ANADIG_ANA_MISC0_SET 0x154
63#define ANADIG_ANA_MISC0_CLR 0x158
64
Peter Chen3f126502014-02-24 10:21:02 +080065#define ANADIG_USB1_VBUS_DET_STAT 0x1c0
66#define ANADIG_USB2_VBUS_DET_STAT 0x220
67
68#define ANADIG_USB1_LOOPBACK_SET 0x1e4
69#define ANADIG_USB1_LOOPBACK_CLR 0x1e8
70#define ANADIG_USB2_LOOPBACK_SET 0x244
71#define ANADIG_USB2_LOOPBACK_CLR 0x248
72
Peter Chenbf783432014-02-24 10:21:03 +080073#define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG BIT(12)
74#define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL BIT(11)
75
Peter Chen3f126502014-02-24 10:21:02 +080076#define BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
77#define BM_ANADIG_USB2_VBUS_DET_STAT_VBUS_VALID BIT(3)
78
79#define BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 BIT(2)
80#define BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN BIT(5)
81#define BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 BIT(2)
82#define BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN BIT(5)
83
Peter Chen24007802014-02-24 10:20:54 +080084#define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
85
86/* Do disconnection between PHY and controller without vbus */
87#define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
88
89/*
90 * The PHY will be in messy if there is a wakeup after putting
91 * bus to suspend (set portsc.suspendM) but before setting PHY to low
92 * power mode (set portsc.phcd).
93 */
94#define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
95
96/*
97 * The SOF sends too fast after resuming, it will cause disconnection
98 * between host and high speed device.
99 */
100#define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
101
Peter Chen22db05e2014-02-24 10:20:59 +0800102/*
103 * IC has bug fixes logic, they include
104 * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
105 * which are described at above flags, the RTL will handle it
106 * according to different versions.
107 */
108#define MXS_PHY_NEED_IP_FIX BIT(3)
109
Peter Chen24007802014-02-24 10:20:54 +0800110struct mxs_phy_data {
111 unsigned int flags;
112};
113
114static const struct mxs_phy_data imx23_phy_data = {
115 .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
116};
117
118static const struct mxs_phy_data imx6q_phy_data = {
119 .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
Peter Chen22db05e2014-02-24 10:20:59 +0800120 MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
121 MXS_PHY_NEED_IP_FIX,
Peter Chen24007802014-02-24 10:20:54 +0800122};
123
124static const struct mxs_phy_data imx6sl_phy_data = {
Peter Chen22db05e2014-02-24 10:20:59 +0800125 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
126 MXS_PHY_NEED_IP_FIX,
Peter Chen24007802014-02-24 10:20:54 +0800127};
128
Stefan Agnerd0ee68b2014-07-28 16:57:29 +0200129static const struct mxs_phy_data vf610_phy_data = {
130 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
131 MXS_PHY_NEED_IP_FIX,
132};
133
Peter Chen43f36342014-08-26 10:55:17 +0800134static const struct mxs_phy_data imx6sx_phy_data = {
Peter Chendd811ba2015-01-16 18:28:58 +0800135 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
Peter Chen43f36342014-08-26 10:55:17 +0800136};
137
Peter Chen24007802014-02-24 10:20:54 +0800138static const struct of_device_id mxs_phy_dt_ids[] = {
Peter Chen43f36342014-08-26 10:55:17 +0800139 { .compatible = "fsl,imx6sx-usbphy", .data = &imx6sx_phy_data, },
Peter Chen24007802014-02-24 10:20:54 +0800140 { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
141 { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
142 { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
Stefan Agnerd0ee68b2014-07-28 16:57:29 +0200143 { .compatible = "fsl,vf610-usbphy", .data = &vf610_phy_data, },
Peter Chen24007802014-02-24 10:20:54 +0800144 { /* sentinel */ }
145};
146MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
147
Richard Zhaob3d99682012-07-07 22:56:47 +0800148struct mxs_phy {
149 struct usb_phy phy;
150 struct clk *clk;
Peter Chen24007802014-02-24 10:20:54 +0800151 const struct mxs_phy_data *data;
Peter Chen0d896532014-02-24 10:20:57 +0800152 struct regmap *regmap_anatop;
Peter Chen83be1812014-02-24 10:21:00 +0800153 int port_id;
Richard Zhaob3d99682012-07-07 22:56:47 +0800154};
155
Peter Chenbf783432014-02-24 10:21:03 +0800156static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy)
157{
158 return mxs_phy->data == &imx6q_phy_data;
159}
160
161static inline bool is_imx6sl_phy(struct mxs_phy *mxs_phy)
162{
163 return mxs_phy->data == &imx6sl_phy_data;
164}
165
Peter Chen47d18452014-02-24 10:21:04 +0800166/*
167 * PHY needs some 32K cycles to switch from 32K clock to
168 * bus (such as AHB/AXI, etc) clock.
169 */
170static void mxs_phy_clock_switch_delay(void)
171{
172 usleep_range(300, 400);
173}
174
Fabio Estevam51e563e2013-07-03 16:34:13 -0300175static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
Richard Zhaob3d99682012-07-07 22:56:47 +0800176{
Fabio Estevam51e563e2013-07-03 16:34:13 -0300177 int ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800178 void __iomem *base = mxs_phy->phy.io_priv;
179
Fabio Estevam51e563e2013-07-03 16:34:13 -0300180 ret = stmp_reset_block(base + HW_USBPHY_CTRL);
181 if (ret)
182 return ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800183
184 /* Power up the PHY */
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100185 writel(0, base + HW_USBPHY_PWD);
Richard Zhaob3d99682012-07-07 22:56:47 +0800186
Peter Chen13644142014-02-24 10:20:55 +0800187 /*
188 * USB PHY Ctrl Setting
189 * - Auto clock/power on
190 * - Enable full/low speed support
191 */
192 writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
193 BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
194 BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
195 BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
196 BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
197 BM_USBPHY_CTRL_ENUTMILEVEL2 |
198 BM_USBPHY_CTRL_ENUTMILEVEL3,
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100199 base + HW_USBPHY_CTRL_SET);
Fabio Estevam51e563e2013-07-03 16:34:13 -0300200
Peter Chen22db05e2014-02-24 10:20:59 +0800201 if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
202 writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
203
Fabio Estevam51e563e2013-07-03 16:34:13 -0300204 return 0;
Richard Zhaob3d99682012-07-07 22:56:47 +0800205}
206
Peter Chen3f126502014-02-24 10:21:02 +0800207/* Return true if the vbus is there */
208static bool mxs_phy_get_vbus_status(struct mxs_phy *mxs_phy)
209{
210 unsigned int vbus_value;
211
212 if (mxs_phy->port_id == 0)
213 regmap_read(mxs_phy->regmap_anatop,
214 ANADIG_USB1_VBUS_DET_STAT,
215 &vbus_value);
216 else if (mxs_phy->port_id == 1)
217 regmap_read(mxs_phy->regmap_anatop,
218 ANADIG_USB2_VBUS_DET_STAT,
219 &vbus_value);
220
221 if (vbus_value & BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)
222 return true;
223 else
224 return false;
225}
226
227static void __mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool disconnect)
228{
229 void __iomem *base = mxs_phy->phy.io_priv;
230 u32 reg;
231
232 if (disconnect)
233 writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
234 base + HW_USBPHY_DEBUG_CLR);
235
236 if (mxs_phy->port_id == 0) {
237 reg = disconnect ? ANADIG_USB1_LOOPBACK_SET
238 : ANADIG_USB1_LOOPBACK_CLR;
239 regmap_write(mxs_phy->regmap_anatop, reg,
240 BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 |
241 BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN);
242 } else if (mxs_phy->port_id == 1) {
243 reg = disconnect ? ANADIG_USB2_LOOPBACK_SET
244 : ANADIG_USB2_LOOPBACK_CLR;
245 regmap_write(mxs_phy->regmap_anatop, reg,
246 BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 |
247 BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN);
248 }
249
250 if (!disconnect)
251 writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
252 base + HW_USBPHY_DEBUG_SET);
253
254 /* Delay some time, and let Linestate be SE0 for controller */
255 if (disconnect)
256 usleep_range(500, 1000);
257}
258
Li Jun7b09e672015-01-16 18:28:59 +0800259static bool mxs_phy_is_otg_host(struct mxs_phy *mxs_phy)
260{
261 void __iomem *base = mxs_phy->phy.io_priv;
262 u32 phyctrl = readl(base + HW_USBPHY_CTRL);
263
264 if (IS_ENABLED(CONFIG_USB_OTG) &&
265 !(phyctrl & BM_USBPHY_CTRL_OTG_ID_VALUE))
266 return true;
267
268 return false;
269}
270
Peter Chen3f126502014-02-24 10:21:02 +0800271static void mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool on)
272{
273 bool vbus_is_on = false;
274
275 /* If the SoCs don't need to disconnect line without vbus, quit */
276 if (!(mxs_phy->data->flags & MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS))
277 return;
278
279 /* If the SoCs don't have anatop, quit */
280 if (!mxs_phy->regmap_anatop)
281 return;
282
283 vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
284
Li Jun7b09e672015-01-16 18:28:59 +0800285 if (on && !vbus_is_on && !mxs_phy_is_otg_host(mxs_phy))
Peter Chen3f126502014-02-24 10:21:02 +0800286 __mxs_phy_disconnect_line(mxs_phy, true);
287 else
288 __mxs_phy_disconnect_line(mxs_phy, false);
289
290}
291
Richard Zhaob3d99682012-07-07 22:56:47 +0800292static int mxs_phy_init(struct usb_phy *phy)
293{
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200294 int ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800295 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
296
Peter Chen47d18452014-02-24 10:21:04 +0800297 mxs_phy_clock_switch_delay();
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200298 ret = clk_prepare_enable(mxs_phy->clk);
299 if (ret)
300 return ret;
301
Fabio Estevam51e563e2013-07-03 16:34:13 -0300302 return mxs_phy_hw_init(mxs_phy);
Richard Zhaob3d99682012-07-07 22:56:47 +0800303}
304
305static void mxs_phy_shutdown(struct usb_phy *phy)
306{
307 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
Peter Chenfdf80e72014-12-24 13:48:02 +0800308 u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
309 BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
310 BM_USBPHY_CTRL_ENIDCHG_WKUP |
311 BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
312 BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
313 BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
314 BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
315 BM_USBPHY_CTRL_ENAUTO_PWRON_PLL;
316
317 writel(value, phy->io_priv + HW_USBPHY_CTRL_CLR);
318 writel(0xffffffff, phy->io_priv + HW_USBPHY_PWD);
Richard Zhaob3d99682012-07-07 22:56:47 +0800319
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100320 writel(BM_USBPHY_CTRL_CLKGATE,
321 phy->io_priv + HW_USBPHY_CTRL_SET);
Richard Zhaob3d99682012-07-07 22:56:47 +0800322
323 clk_disable_unprepare(mxs_phy->clk);
324}
325
Peter Chen04a62212013-01-10 16:35:53 +0800326static int mxs_phy_suspend(struct usb_phy *x, int suspend)
327{
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200328 int ret;
Peter Chen04a62212013-01-10 16:35:53 +0800329 struct mxs_phy *mxs_phy = to_mxs_phy(x);
330
331 if (suspend) {
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100332 writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
333 writel(BM_USBPHY_CTRL_CLKGATE,
334 x->io_priv + HW_USBPHY_CTRL_SET);
Peter Chen04a62212013-01-10 16:35:53 +0800335 clk_disable_unprepare(mxs_phy->clk);
336 } else {
Peter Chen47d18452014-02-24 10:21:04 +0800337 mxs_phy_clock_switch_delay();
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200338 ret = clk_prepare_enable(mxs_phy->clk);
339 if (ret)
340 return ret;
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100341 writel(BM_USBPHY_CTRL_CLKGATE,
342 x->io_priv + HW_USBPHY_CTRL_CLR);
343 writel(0, x->io_priv + HW_USBPHY_PWD);
Peter Chen04a62212013-01-10 16:35:53 +0800344 }
345
346 return 0;
347}
348
Peter Chen3f126502014-02-24 10:21:02 +0800349static int mxs_phy_set_wakeup(struct usb_phy *x, bool enabled)
350{
351 struct mxs_phy *mxs_phy = to_mxs_phy(x);
352 u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
353 BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
354 BM_USBPHY_CTRL_ENIDCHG_WKUP;
355 if (enabled) {
356 mxs_phy_disconnect_line(mxs_phy, true);
357 writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_SET);
358 } else {
359 writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_CLR);
360 mxs_phy_disconnect_line(mxs_phy, false);
361 }
362
363 return 0;
364}
365
Peter Chenac965112012-11-09 09:44:44 +0800366static int mxs_phy_on_connect(struct usb_phy *phy,
367 enum usb_device_speed speed)
Richard Zhaob3d99682012-07-07 22:56:47 +0800368{
Peter Chenf6a15822014-02-24 10:20:58 +0800369 dev_dbg(phy->dev, "%s device has connected\n",
370 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
Richard Zhaob3d99682012-07-07 22:56:47 +0800371
Peter Chenac965112012-11-09 09:44:44 +0800372 if (speed == USB_SPEED_HIGH)
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100373 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
374 phy->io_priv + HW_USBPHY_CTRL_SET);
Richard Zhaob3d99682012-07-07 22:56:47 +0800375
376 return 0;
377}
378
Peter Chenac965112012-11-09 09:44:44 +0800379static int mxs_phy_on_disconnect(struct usb_phy *phy,
380 enum usb_device_speed speed)
Richard Zhaob3d99682012-07-07 22:56:47 +0800381{
Peter Chenf6a15822014-02-24 10:20:58 +0800382 dev_dbg(phy->dev, "%s device has disconnected\n",
383 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
Richard Zhaob3d99682012-07-07 22:56:47 +0800384
Peter Chenf78c0952014-12-24 13:48:03 +0800385 /* Sometimes, the speed is not high speed when the error occurs */
386 if (readl(phy->io_priv + HW_USBPHY_CTRL) &
387 BM_USBPHY_CTRL_ENHOSTDISCONDETECT)
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100388 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
389 phy->io_priv + HW_USBPHY_CTRL_CLR);
Richard Zhaob3d99682012-07-07 22:56:47 +0800390
391 return 0;
392}
393
394static int mxs_phy_probe(struct platform_device *pdev)
395{
396 struct resource *res;
397 void __iomem *base;
398 struct clk *clk;
399 struct mxs_phy *mxs_phy;
Sascha Hauer25df6392013-02-27 15:16:30 +0100400 int ret;
Peter Chen24007802014-02-24 10:20:54 +0800401 const struct of_device_id *of_id =
402 of_match_device(mxs_phy_dt_ids, &pdev->dev);
Peter Chen0d896532014-02-24 10:20:57 +0800403 struct device_node *np = pdev->dev.of_node;
Richard Zhaob3d99682012-07-07 22:56:47 +0800404
405 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100406 base = devm_ioremap_resource(&pdev->dev, res);
407 if (IS_ERR(base))
408 return PTR_ERR(base);
Richard Zhaob3d99682012-07-07 22:56:47 +0800409
410 clk = devm_clk_get(&pdev->dev, NULL);
411 if (IS_ERR(clk)) {
412 dev_err(&pdev->dev,
413 "can't get the clock, err=%ld", PTR_ERR(clk));
414 return PTR_ERR(clk);
415 }
416
417 mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
Peter Chenc62fe552014-10-14 15:56:16 +0800418 if (!mxs_phy)
Richard Zhaob3d99682012-07-07 22:56:47 +0800419 return -ENOMEM;
Richard Zhaob3d99682012-07-07 22:56:47 +0800420
Peter Chen0d896532014-02-24 10:20:57 +0800421 /* Some SoCs don't have anatop registers */
422 if (of_get_property(np, "fsl,anatop", NULL)) {
423 mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
424 (np, "fsl,anatop");
425 if (IS_ERR(mxs_phy->regmap_anatop)) {
426 dev_dbg(&pdev->dev,
427 "failed to find regmap for anatop\n");
428 return PTR_ERR(mxs_phy->regmap_anatop);
429 }
430 }
431
Peter Chen83be1812014-02-24 10:21:00 +0800432 ret = of_alias_get_id(np, "usbphy");
433 if (ret < 0)
434 dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
435 mxs_phy->port_id = ret;
436
Richard Zhaob3d99682012-07-07 22:56:47 +0800437 mxs_phy->phy.io_priv = base;
438 mxs_phy->phy.dev = &pdev->dev;
439 mxs_phy->phy.label = DRIVER_NAME;
440 mxs_phy->phy.init = mxs_phy_init;
441 mxs_phy->phy.shutdown = mxs_phy_shutdown;
Peter Chen04a62212013-01-10 16:35:53 +0800442 mxs_phy->phy.set_suspend = mxs_phy_suspend;
Richard Zhaob3d99682012-07-07 22:56:47 +0800443 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
444 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
Michael Grzeschik4e0aa632013-05-15 15:03:14 +0200445 mxs_phy->phy.type = USB_PHY_TYPE_USB2;
Peter Chen3f126502014-02-24 10:21:02 +0800446 mxs_phy->phy.set_wakeup = mxs_phy_set_wakeup;
Richard Zhaob3d99682012-07-07 22:56:47 +0800447
Richard Zhaob3d99682012-07-07 22:56:47 +0800448 mxs_phy->clk = clk;
Peter Chen24007802014-02-24 10:20:54 +0800449 mxs_phy->data = of_id->data;
Richard Zhaob3d99682012-07-07 22:56:47 +0800450
Jisheng Zhang97a27f72013-11-07 10:55:49 +0800451 platform_set_drvdata(pdev, mxs_phy);
Richard Zhaob3d99682012-07-07 22:56:47 +0800452
Peter Chenbf783432014-02-24 10:21:03 +0800453 device_set_wakeup_capable(&pdev->dev, true);
454
Sascha Hauer25df6392013-02-27 15:16:30 +0100455 ret = usb_add_phy_dev(&mxs_phy->phy);
456 if (ret)
457 return ret;
458
Richard Zhaob3d99682012-07-07 22:56:47 +0800459 return 0;
460}
461
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500462static int mxs_phy_remove(struct platform_device *pdev)
Richard Zhaob3d99682012-07-07 22:56:47 +0800463{
Sascha Hauer25df6392013-02-27 15:16:30 +0100464 struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
465
466 usb_remove_phy(&mxs_phy->phy);
467
Richard Zhaob3d99682012-07-07 22:56:47 +0800468 return 0;
469}
470
Peter Chenbf783432014-02-24 10:21:03 +0800471#ifdef CONFIG_PM_SLEEP
472static void mxs_phy_enable_ldo_in_suspend(struct mxs_phy *mxs_phy, bool on)
473{
474 unsigned int reg = on ? ANADIG_ANA_MISC0_SET : ANADIG_ANA_MISC0_CLR;
475
476 /* If the SoCs don't have anatop, quit */
477 if (!mxs_phy->regmap_anatop)
478 return;
479
480 if (is_imx6q_phy(mxs_phy))
481 regmap_write(mxs_phy->regmap_anatop, reg,
482 BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG);
483 else if (is_imx6sl_phy(mxs_phy))
484 regmap_write(mxs_phy->regmap_anatop,
485 reg, BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL);
486}
487
488static int mxs_phy_system_suspend(struct device *dev)
489{
490 struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
491
492 if (device_may_wakeup(dev))
493 mxs_phy_enable_ldo_in_suspend(mxs_phy, true);
494
495 return 0;
496}
497
498static int mxs_phy_system_resume(struct device *dev)
499{
500 struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
501
502 if (device_may_wakeup(dev))
503 mxs_phy_enable_ldo_in_suspend(mxs_phy, false);
504
505 return 0;
506}
507#endif /* CONFIG_PM_SLEEP */
508
509static SIMPLE_DEV_PM_OPS(mxs_phy_pm, mxs_phy_system_suspend,
510 mxs_phy_system_resume);
511
Richard Zhaob3d99682012-07-07 22:56:47 +0800512static struct platform_driver mxs_phy_driver = {
513 .probe = mxs_phy_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500514 .remove = mxs_phy_remove,
Richard Zhaob3d99682012-07-07 22:56:47 +0800515 .driver = {
516 .name = DRIVER_NAME,
Richard Zhaob3d99682012-07-07 22:56:47 +0800517 .of_match_table = mxs_phy_dt_ids,
Peter Chenbf783432014-02-24 10:21:03 +0800518 .pm = &mxs_phy_pm,
Richard Zhaob3d99682012-07-07 22:56:47 +0800519 },
520};
521
522static int __init mxs_phy_module_init(void)
523{
524 return platform_driver_register(&mxs_phy_driver);
525}
526postcore_initcall(mxs_phy_module_init);
527
528static void __exit mxs_phy_module_exit(void)
529{
530 platform_driver_unregister(&mxs_phy_driver);
531}
532module_exit(mxs_phy_module_exit);
533
534MODULE_ALIAS("platform:mxs-usb-phy");
535MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
536MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
537MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
538MODULE_LICENSE("GPL");