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 * @id: index of the pcie PHY (should be 1 or 2)
33 * @delay: 8 bit delay value
35 void omap_control_pcie_pcs(struct device
*dev
, u8 id
, u8 delay
)
38 struct omap_control_phy
*control_phy
;
40 if (IS_ERR(dev
) || !dev
) {
41 pr_err("%s: invalid device\n", __func__
);
45 control_phy
= dev_get_drvdata(dev
);
47 dev_err(dev
, "%s: invalid control phy device\n", __func__
);
51 if (control_phy
->type
!= OMAP_CTRL_TYPE_PCIE
) {
52 dev_err(dev
, "%s: unsupported operation\n", __func__
);
56 val
= readl(control_phy
->pcie_pcs
);
57 val
&= ~(OMAP_CTRL_PCIE_PCS_MASK
<<
58 (id
* OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT
));
59 val
|= delay
<< (id
* OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT
);
60 writel(val
, control_phy
->pcie_pcs
);
62 EXPORT_SYMBOL_GPL(omap_control_pcie_pcs
);
65 * omap_control_phy_power - power on/off the phy using control module reg
66 * @dev: the control module device
67 * @on: 0 or 1, based on powering on or off the PHY
69 void omap_control_phy_power(struct device
*dev
, int on
)
73 struct omap_control_phy
*control_phy
;
75 if (IS_ERR(dev
) || !dev
) {
76 pr_err("%s: invalid device\n", __func__
);
80 control_phy
= dev_get_drvdata(dev
);
82 dev_err(dev
, "%s: invalid control phy device\n", __func__
);
86 if (control_phy
->type
== OMAP_CTRL_TYPE_OTGHS
)
89 val
= readl(control_phy
->power
);
91 switch (control_phy
->type
) {
92 case OMAP_CTRL_TYPE_USB2
:
94 val
&= ~OMAP_CTRL_DEV_PHY_PD
;
96 val
|= OMAP_CTRL_DEV_PHY_PD
;
99 case OMAP_CTRL_TYPE_PCIE
:
100 case OMAP_CTRL_TYPE_PIPE3
:
101 rate
= clk_get_rate(control_phy
->sys_clk
);
105 val
&= ~(OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK
|
106 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK
);
107 val
|= OMAP_CTRL_PIPE3_PHY_TX_RX_POWERON
<<
108 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT
;
110 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT
;
112 val
&= ~OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK
;
113 val
|= OMAP_CTRL_PIPE3_PHY_TX_RX_POWEROFF
<<
114 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT
;
118 case OMAP_CTRL_TYPE_DRA7USB2
:
120 val
&= ~OMAP_CTRL_USB2_PHY_PD
;
122 val
|= OMAP_CTRL_USB2_PHY_PD
;
125 case OMAP_CTRL_TYPE_AM437USB2
:
127 val
&= ~(AM437X_CTRL_USB2_PHY_PD
|
128 AM437X_CTRL_USB2_OTG_PD
);
129 val
|= (AM437X_CTRL_USB2_OTGVDET_EN
|
130 AM437X_CTRL_USB2_OTGSESSEND_EN
);
132 val
&= ~(AM437X_CTRL_USB2_OTGVDET_EN
|
133 AM437X_CTRL_USB2_OTGSESSEND_EN
);
134 val
|= (AM437X_CTRL_USB2_PHY_PD
|
135 AM437X_CTRL_USB2_OTG_PD
);
139 dev_err(dev
, "%s: type %d not recognized\n",
140 __func__
, control_phy
->type
);
144 writel(val
, control_phy
->power
);
146 EXPORT_SYMBOL_GPL(omap_control_phy_power
);
149 * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded
150 * @ctrl_phy: struct omap_control_phy *
152 * Writes to the mailbox register to notify the usb core that a usb
153 * device has been connected.
155 static void omap_control_usb_host_mode(struct omap_control_phy
*ctrl_phy
)
159 val
= readl(ctrl_phy
->otghs_control
);
160 val
&= ~(OMAP_CTRL_DEV_IDDIG
| OMAP_CTRL_DEV_SESSEND
);
161 val
|= OMAP_CTRL_DEV_AVALID
| OMAP_CTRL_DEV_VBUSVALID
;
162 writel(val
, ctrl_phy
->otghs_control
);
166 * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high
168 * @ctrl_phy: struct omap_control_phy *
170 * Writes to the mailbox register to notify the usb core that it has been
171 * connected to a usb host.
173 static void omap_control_usb_device_mode(struct omap_control_phy
*ctrl_phy
)
177 val
= readl(ctrl_phy
->otghs_control
);
178 val
&= ~OMAP_CTRL_DEV_SESSEND
;
179 val
|= OMAP_CTRL_DEV_IDDIG
| OMAP_CTRL_DEV_AVALID
|
180 OMAP_CTRL_DEV_VBUSVALID
;
181 writel(val
, ctrl_phy
->otghs_control
);
185 * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high
187 * @ctrl_phy: struct omap_control_phy *
189 * Writes to the mailbox register to notify the usb core it's now in
190 * disconnected state.
192 static void omap_control_usb_set_sessionend(struct omap_control_phy
*ctrl_phy
)
196 val
= readl(ctrl_phy
->otghs_control
);
197 val
&= ~(OMAP_CTRL_DEV_AVALID
| OMAP_CTRL_DEV_VBUSVALID
);
198 val
|= OMAP_CTRL_DEV_IDDIG
| OMAP_CTRL_DEV_SESSEND
;
199 writel(val
, ctrl_phy
->otghs_control
);
203 * omap_control_usb_set_mode - Calls to functions to set USB in one of host mode
204 * or device mode or to denote disconnected state
205 * @dev: the control module device
206 * @mode: The mode to which usb should be configured
208 * This is an API to write to the mailbox register to notify the usb core that
209 * a usb device has been connected.
211 void omap_control_usb_set_mode(struct device
*dev
,
212 enum omap_control_usb_mode mode
)
214 struct omap_control_phy
*ctrl_phy
;
216 if (IS_ERR(dev
) || !dev
)
219 ctrl_phy
= dev_get_drvdata(dev
);
222 dev_err(dev
, "Invalid control phy device\n");
226 if (ctrl_phy
->type
!= OMAP_CTRL_TYPE_OTGHS
)
231 omap_control_usb_host_mode(ctrl_phy
);
233 case USB_MODE_DEVICE
:
234 omap_control_usb_device_mode(ctrl_phy
);
236 case USB_MODE_DISCONNECT
:
237 omap_control_usb_set_sessionend(ctrl_phy
);
240 dev_vdbg(dev
, "invalid omap control usb mode\n");
243 EXPORT_SYMBOL_GPL(omap_control_usb_set_mode
);
247 static const enum omap_control_phy_type otghs_data
= OMAP_CTRL_TYPE_OTGHS
;
248 static const enum omap_control_phy_type usb2_data
= OMAP_CTRL_TYPE_USB2
;
249 static const enum omap_control_phy_type pipe3_data
= OMAP_CTRL_TYPE_PIPE3
;
250 static const enum omap_control_phy_type pcie_data
= OMAP_CTRL_TYPE_PCIE
;
251 static const enum omap_control_phy_type dra7usb2_data
= OMAP_CTRL_TYPE_DRA7USB2
;
252 static const enum omap_control_phy_type am437usb2_data
= OMAP_CTRL_TYPE_AM437USB2
;
254 static const struct of_device_id omap_control_phy_id_table
[] = {
256 .compatible
= "ti,control-phy-otghs",
260 .compatible
= "ti,control-phy-usb2",
264 .compatible
= "ti,control-phy-pipe3",
268 .compatible
= "ti,control-phy-pcie",
272 .compatible
= "ti,control-phy-usb2-dra7",
273 .data
= &dra7usb2_data
,
276 .compatible
= "ti,control-phy-usb2-am437",
277 .data
= &am437usb2_data
,
281 MODULE_DEVICE_TABLE(of
, omap_control_phy_id_table
);
285 static int omap_control_phy_probe(struct platform_device
*pdev
)
287 struct resource
*res
;
288 const struct of_device_id
*of_id
;
289 struct omap_control_phy
*control_phy
;
291 of_id
= of_match_device(of_match_ptr(omap_control_phy_id_table
),
296 control_phy
= devm_kzalloc(&pdev
->dev
, sizeof(*control_phy
),
301 control_phy
->dev
= &pdev
->dev
;
302 control_phy
->type
= *(enum omap_control_phy_type
*)of_id
->data
;
304 if (control_phy
->type
== OMAP_CTRL_TYPE_OTGHS
) {
305 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
307 control_phy
->otghs_control
= devm_ioremap_resource(
309 if (IS_ERR(control_phy
->otghs_control
))
310 return PTR_ERR(control_phy
->otghs_control
);
312 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
314 control_phy
->power
= devm_ioremap_resource(&pdev
->dev
, res
);
315 if (IS_ERR(control_phy
->power
)) {
316 dev_err(&pdev
->dev
, "Couldn't get power register\n");
317 return PTR_ERR(control_phy
->power
);
321 if (control_phy
->type
== OMAP_CTRL_TYPE_PIPE3
||
322 control_phy
->type
== OMAP_CTRL_TYPE_PCIE
) {
323 control_phy
->sys_clk
= devm_clk_get(control_phy
->dev
,
325 if (IS_ERR(control_phy
->sys_clk
)) {
326 pr_err("%s: unable to get sys_clkin\n", __func__
);
331 if (control_phy
->type
== OMAP_CTRL_TYPE_PCIE
) {
332 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
334 control_phy
->pcie_pcs
= devm_ioremap_resource(&pdev
->dev
, res
);
335 if (IS_ERR(control_phy
->pcie_pcs
))
336 return PTR_ERR(control_phy
->pcie_pcs
);
339 dev_set_drvdata(control_phy
->dev
, control_phy
);
344 static struct platform_driver omap_control_phy_driver
= {
345 .probe
= omap_control_phy_probe
,
347 .name
= "omap-control-phy",
348 .of_match_table
= of_match_ptr(omap_control_phy_id_table
),
352 static int __init
omap_control_phy_init(void)
354 return platform_driver_register(&omap_control_phy_driver
);
356 subsys_initcall(omap_control_phy_init
);
358 static void __exit
omap_control_phy_exit(void)
360 platform_driver_unregister(&omap_control_phy_driver
);
362 module_exit(omap_control_phy_exit
);
364 MODULE_ALIAS("platform: omap_control_phy");
365 MODULE_AUTHOR("Texas Instruments Inc.");
366 MODULE_DESCRIPTION("OMAP Control Module PHY Driver");
367 MODULE_LICENSE("GPL v2");