2 * omap-control-phy.c - The PHY part of control module.
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Author: Kishon Vijay Abraham I <kishon@ti.com>
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
23 #include <linux/of_device.h>
24 #include <linux/err.h>
26 #include <linux/clk.h>
27 #include <linux/phy/omap_control_phy.h>
30 * omap_control_pcie_pcs - set the PCS delay count
31 * @dev: the control module device
32 * @delay: 8 bit delay value
34 void omap_control_pcie_pcs(struct device
*dev
, u8 delay
)
37 struct omap_control_phy
*control_phy
;
39 if (IS_ERR(dev
) || !dev
) {
40 pr_err("%s: invalid device\n", __func__
);
44 control_phy
= dev_get_drvdata(dev
);
46 dev_err(dev
, "%s: invalid control phy device\n", __func__
);
50 if (control_phy
->type
!= OMAP_CTRL_TYPE_PCIE
) {
51 dev_err(dev
, "%s: unsupported operation\n", __func__
);
55 val
= readl(control_phy
->pcie_pcs
);
56 val
&= ~(OMAP_CTRL_PCIE_PCS_MASK
<<
57 OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT
);
58 val
|= (delay
<< OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT
);
59 writel(val
, control_phy
->pcie_pcs
);
61 EXPORT_SYMBOL_GPL(omap_control_pcie_pcs
);
64 * omap_control_phy_power - power on/off the phy using control module reg
65 * @dev: the control module device
66 * @on: 0 or 1, based on powering on or off the PHY
68 void omap_control_phy_power(struct device
*dev
, int on
)
72 struct omap_control_phy
*control_phy
;
74 if (IS_ERR(dev
) || !dev
) {
75 pr_err("%s: invalid device\n", __func__
);
79 control_phy
= dev_get_drvdata(dev
);
81 dev_err(dev
, "%s: invalid control phy device\n", __func__
);
85 if (control_phy
->type
== OMAP_CTRL_TYPE_OTGHS
)
88 val
= readl(control_phy
->power
);
90 switch (control_phy
->type
) {
91 case OMAP_CTRL_TYPE_USB2
:
93 val
&= ~OMAP_CTRL_DEV_PHY_PD
;
95 val
|= OMAP_CTRL_DEV_PHY_PD
;
98 case OMAP_CTRL_TYPE_PCIE
:
99 case OMAP_CTRL_TYPE_PIPE3
:
100 rate
= clk_get_rate(control_phy
->sys_clk
);
104 val
&= ~(OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK
|
105 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK
);
106 val
|= OMAP_CTRL_PIPE3_PHY_TX_RX_POWERON
<<
107 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT
;
109 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT
;
111 val
&= ~OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK
;
112 val
|= OMAP_CTRL_PIPE3_PHY_TX_RX_POWEROFF
<<
113 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT
;
117 case OMAP_CTRL_TYPE_DRA7USB2
:
119 val
&= ~OMAP_CTRL_USB2_PHY_PD
;
121 val
|= OMAP_CTRL_USB2_PHY_PD
;
124 case OMAP_CTRL_TYPE_AM437USB2
:
126 val
&= ~(AM437X_CTRL_USB2_PHY_PD
|
127 AM437X_CTRL_USB2_OTG_PD
);
128 val
|= (AM437X_CTRL_USB2_OTGVDET_EN
|
129 AM437X_CTRL_USB2_OTGSESSEND_EN
);
131 val
&= ~(AM437X_CTRL_USB2_OTGVDET_EN
|
132 AM437X_CTRL_USB2_OTGSESSEND_EN
);
133 val
|= (AM437X_CTRL_USB2_PHY_PD
|
134 AM437X_CTRL_USB2_OTG_PD
);
138 dev_err(dev
, "%s: type %d not recognized\n",
139 __func__
, control_phy
->type
);
143 writel(val
, control_phy
->power
);
145 EXPORT_SYMBOL_GPL(omap_control_phy_power
);
148 * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded
149 * @ctrl_phy: struct omap_control_phy *
151 * Writes to the mailbox register to notify the usb core that a usb
152 * device has been connected.
154 static void omap_control_usb_host_mode(struct omap_control_phy
*ctrl_phy
)
158 val
= readl(ctrl_phy
->otghs_control
);
159 val
&= ~(OMAP_CTRL_DEV_IDDIG
| OMAP_CTRL_DEV_SESSEND
);
160 val
|= OMAP_CTRL_DEV_AVALID
| OMAP_CTRL_DEV_VBUSVALID
;
161 writel(val
, ctrl_phy
->otghs_control
);
165 * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high
167 * @ctrl_phy: struct omap_control_phy *
169 * Writes to the mailbox register to notify the usb core that it has been
170 * connected to a usb host.
172 static void omap_control_usb_device_mode(struct omap_control_phy
*ctrl_phy
)
176 val
= readl(ctrl_phy
->otghs_control
);
177 val
&= ~OMAP_CTRL_DEV_SESSEND
;
178 val
|= OMAP_CTRL_DEV_IDDIG
| OMAP_CTRL_DEV_AVALID
|
179 OMAP_CTRL_DEV_VBUSVALID
;
180 writel(val
, ctrl_phy
->otghs_control
);
184 * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high
186 * @ctrl_phy: struct omap_control_phy *
188 * Writes to the mailbox register to notify the usb core it's now in
189 * disconnected state.
191 static void omap_control_usb_set_sessionend(struct omap_control_phy
*ctrl_phy
)
195 val
= readl(ctrl_phy
->otghs_control
);
196 val
&= ~(OMAP_CTRL_DEV_AVALID
| OMAP_CTRL_DEV_VBUSVALID
);
197 val
|= OMAP_CTRL_DEV_IDDIG
| OMAP_CTRL_DEV_SESSEND
;
198 writel(val
, ctrl_phy
->otghs_control
);
202 * omap_control_usb_set_mode - Calls to functions to set USB in one of host mode
203 * or device mode or to denote disconnected state
204 * @dev: the control module device
205 * @mode: The mode to which usb should be configured
207 * This is an API to write to the mailbox register to notify the usb core that
208 * a usb device has been connected.
210 void omap_control_usb_set_mode(struct device
*dev
,
211 enum omap_control_usb_mode mode
)
213 struct omap_control_phy
*ctrl_phy
;
215 if (IS_ERR(dev
) || !dev
)
218 ctrl_phy
= dev_get_drvdata(dev
);
220 dev_err(dev
, "Invalid control phy device\n");
224 if (ctrl_phy
->type
!= OMAP_CTRL_TYPE_OTGHS
)
229 omap_control_usb_host_mode(ctrl_phy
);
231 case USB_MODE_DEVICE
:
232 omap_control_usb_device_mode(ctrl_phy
);
234 case USB_MODE_DISCONNECT
:
235 omap_control_usb_set_sessionend(ctrl_phy
);
238 dev_vdbg(dev
, "invalid omap control usb mode\n");
241 EXPORT_SYMBOL_GPL(omap_control_usb_set_mode
);
243 static const enum omap_control_phy_type otghs_data
= OMAP_CTRL_TYPE_OTGHS
;
244 static const enum omap_control_phy_type usb2_data
= OMAP_CTRL_TYPE_USB2
;
245 static const enum omap_control_phy_type pipe3_data
= OMAP_CTRL_TYPE_PIPE3
;
246 static const enum omap_control_phy_type pcie_data
= OMAP_CTRL_TYPE_PCIE
;
247 static const enum omap_control_phy_type dra7usb2_data
= OMAP_CTRL_TYPE_DRA7USB2
;
248 static const enum omap_control_phy_type am437usb2_data
= OMAP_CTRL_TYPE_AM437USB2
;
250 static const struct of_device_id omap_control_phy_id_table
[] = {
252 .compatible
= "ti,control-phy-otghs",
256 .compatible
= "ti,control-phy-usb2",
260 .compatible
= "ti,control-phy-pipe3",
264 .compatible
= "ti,control-phy-pcie",
268 .compatible
= "ti,control-phy-usb2-dra7",
269 .data
= &dra7usb2_data
,
272 .compatible
= "ti,control-phy-usb2-am437",
273 .data
= &am437usb2_data
,
277 MODULE_DEVICE_TABLE(of
, omap_control_phy_id_table
);
279 static int omap_control_phy_probe(struct platform_device
*pdev
)
281 struct resource
*res
;
282 const struct of_device_id
*of_id
;
283 struct omap_control_phy
*control_phy
;
285 of_id
= of_match_device(omap_control_phy_id_table
, &pdev
->dev
);
289 control_phy
= devm_kzalloc(&pdev
->dev
, sizeof(*control_phy
),
294 control_phy
->dev
= &pdev
->dev
;
295 control_phy
->type
= *(enum omap_control_phy_type
*)of_id
->data
;
297 if (control_phy
->type
== OMAP_CTRL_TYPE_OTGHS
) {
298 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
300 control_phy
->otghs_control
= devm_ioremap_resource(
302 if (IS_ERR(control_phy
->otghs_control
))
303 return PTR_ERR(control_phy
->otghs_control
);
305 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
307 control_phy
->power
= devm_ioremap_resource(&pdev
->dev
, res
);
308 if (IS_ERR(control_phy
->power
)) {
309 dev_err(&pdev
->dev
, "Couldn't get power register\n");
310 return PTR_ERR(control_phy
->power
);
314 if (control_phy
->type
== OMAP_CTRL_TYPE_PIPE3
||
315 control_phy
->type
== OMAP_CTRL_TYPE_PCIE
) {
316 control_phy
->sys_clk
= devm_clk_get(control_phy
->dev
,
318 if (IS_ERR(control_phy
->sys_clk
)) {
319 pr_err("%s: unable to get sys_clkin\n", __func__
);
324 if (control_phy
->type
== OMAP_CTRL_TYPE_PCIE
) {
325 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
327 control_phy
->pcie_pcs
= devm_ioremap_resource(&pdev
->dev
, res
);
328 if (IS_ERR(control_phy
->pcie_pcs
))
329 return PTR_ERR(control_phy
->pcie_pcs
);
332 dev_set_drvdata(control_phy
->dev
, control_phy
);
337 static struct platform_driver omap_control_phy_driver
= {
338 .probe
= omap_control_phy_probe
,
340 .name
= "omap-control-phy",
341 .of_match_table
= omap_control_phy_id_table
,
345 static int __init
omap_control_phy_init(void)
347 return platform_driver_register(&omap_control_phy_driver
);
349 subsys_initcall(omap_control_phy_init
);
351 static void __exit
omap_control_phy_exit(void)
353 platform_driver_unregister(&omap_control_phy_driver
);
355 module_exit(omap_control_phy_exit
);
357 MODULE_ALIAS("platform:omap_control_phy");
358 MODULE_AUTHOR("Texas Instruments Inc.");
359 MODULE_DESCRIPTION("OMAP Control Module PHY Driver");
360 MODULE_LICENSE("GPL v2");