2 * omap-control-usb.c - The USB 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.
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/err.h>
25 #include <linux/clk.h>
26 #include <linux/usb/omap_control_usb.h>
28 static struct omap_control_usb *control_usb;
31 * omap_get_control_dev - returns the device pointer for this control device
33 * This API should be called to get the device pointer for this control
34 * module device. This device pointer should be used for called other
35 * exported API's in this driver.
37 * To be used by PHY driver and glue driver.
39 struct device *omap_get_control_dev(void)
42 return ERR_PTR(-ENODEV);
44 return control_usb->dev;
46 EXPORT_SYMBOL_GPL(omap_get_control_dev);
49 * omap_control_usb3_phy_power - power on/off the serializer using control
51 * @dev: the control module device
52 * @on: 0 to off and 1 to on based on powering on or off the PHY
54 * usb3 PHY driver should call this API to power on or off the PHY.
56 void omap_control_usb3_phy_power(struct device *dev, bool on)
60 struct omap_control_usb *control_usb = dev_get_drvdata(dev);
62 if (control_usb->type != OMAP_CTRL_DEV_TYPE2)
65 rate = clk_get_rate(control_usb->sys_clk);
68 val = readl(control_usb->phy_power);
71 val &= ~(OMAP_CTRL_USB_PWRCTL_CLK_CMD_MASK |
72 OMAP_CTRL_USB_PWRCTL_CLK_FREQ_MASK);
73 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWERON <<
74 OMAP_CTRL_USB_PWRCTL_CLK_CMD_SHIFT;
75 val |= rate << OMAP_CTRL_USB_PWRCTL_CLK_FREQ_SHIFT;
77 val &= ~OMAP_CTRL_USB_PWRCTL_CLK_CMD_MASK;
78 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF <<
79 OMAP_CTRL_USB_PWRCTL_CLK_CMD_SHIFT;
82 writel(val, control_usb->phy_power);
84 EXPORT_SYMBOL_GPL(omap_control_usb3_phy_power);
87 * omap_control_usb_phy_power - power on/off the phy using control module reg
88 * @dev: the control module device
89 * @on: 0 or 1, based on powering on or off the PHY
91 void omap_control_usb_phy_power(struct device *dev, int on)
94 struct omap_control_usb *control_usb = dev_get_drvdata(dev);
96 val = readl(control_usb->dev_conf);
99 val &= ~OMAP_CTRL_DEV_PHY_PD;
101 val |= OMAP_CTRL_DEV_PHY_PD;
103 writel(val, control_usb->dev_conf);
105 EXPORT_SYMBOL_GPL(omap_control_usb_phy_power);
108 * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded
109 * @ctrl_usb: struct omap_control_usb *
111 * Writes to the mailbox register to notify the usb core that a usb
112 * device has been connected.
114 static void omap_control_usb_host_mode(struct omap_control_usb *ctrl_usb)
118 val = readl(ctrl_usb->otghs_control);
119 val &= ~(OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND);
120 val |= OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID;
121 writel(val, ctrl_usb->otghs_control);
125 * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high
127 * @ctrl_usb: struct omap_control_usb *
129 * Writes to the mailbox register to notify the usb core that it has been
130 * connected to a usb host.
132 static void omap_control_usb_device_mode(struct omap_control_usb *ctrl_usb)
136 val = readl(ctrl_usb->otghs_control);
137 val &= ~OMAP_CTRL_DEV_SESSEND;
138 val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_AVALID |
139 OMAP_CTRL_DEV_VBUSVALID;
140 writel(val, ctrl_usb->otghs_control);
144 * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high
146 * @ctrl_usb: struct omap_control_usb *
148 * Writes to the mailbox register to notify the usb core it's now in
149 * disconnected state.
151 static void omap_control_usb_set_sessionend(struct omap_control_usb *ctrl_usb)
155 val = readl(ctrl_usb->otghs_control);
156 val &= ~(OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID);
157 val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND;
158 writel(val, ctrl_usb->otghs_control);
162 * omap_control_usb_set_mode - Calls to functions to set USB in one of host mode
163 * or device mode or to denote disconnected state
164 * @dev: the control module device
165 * @mode: The mode to which usb should be configured
167 * This is an API to write to the mailbox register to notify the usb core that
168 * a usb device has been connected.
170 void omap_control_usb_set_mode(struct device *dev,
171 enum omap_control_usb_mode mode)
173 struct omap_control_usb *ctrl_usb;
175 if (IS_ERR(dev) || control_usb->type != OMAP_CTRL_DEV_TYPE1)
178 ctrl_usb = dev_get_drvdata(dev);
182 omap_control_usb_host_mode(ctrl_usb);
184 case USB_MODE_DEVICE:
185 omap_control_usb_device_mode(ctrl_usb);
187 case USB_MODE_DISCONNECT:
188 omap_control_usb_set_sessionend(ctrl_usb);
191 dev_vdbg(dev, "invalid omap control usb mode\n");
194 EXPORT_SYMBOL_GPL(omap_control_usb_set_mode);
196 static int omap_control_usb_probe(struct platform_device *pdev)
198 struct resource *res;
199 struct device_node *np = pdev->dev.of_node;
200 struct omap_control_usb_platform_data *pdata = pdev->dev.platform_data;
202 control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb),
205 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
210 of_property_read_u32(np, "ti,type", &control_usb->type);
212 control_usb->type = pdata->type;
214 dev_err(&pdev->dev, "no pdata present\n");
218 control_usb->dev = &pdev->dev;
220 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
222 control_usb->dev_conf = devm_request_and_ioremap(&pdev->dev, res);
223 if (!control_usb->dev_conf) {
224 dev_err(&pdev->dev, "Failed to obtain io memory\n");
225 return -EADDRNOTAVAIL;
228 if (control_usb->type == OMAP_CTRL_DEV_TYPE1) {
229 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
231 control_usb->otghs_control = devm_request_and_ioremap(
233 if (!control_usb->otghs_control) {
234 dev_err(&pdev->dev, "Failed to obtain io memory\n");
235 return -EADDRNOTAVAIL;
239 if (control_usb->type == OMAP_CTRL_DEV_TYPE2) {
240 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
242 control_usb->phy_power = devm_request_and_ioremap(
244 if (!control_usb->phy_power) {
245 dev_dbg(&pdev->dev, "Failed to obtain io memory\n");
246 return -EADDRNOTAVAIL;
249 control_usb->sys_clk = devm_clk_get(control_usb->dev,
251 if (IS_ERR(control_usb->sys_clk)) {
252 pr_err("%s: unable to get sys_clkin\n", __func__);
258 dev_set_drvdata(control_usb->dev, control_usb);
264 static const struct of_device_id omap_control_usb_id_table[] = {
265 { .compatible = "ti,omap-control-usb" },
268 MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
271 static struct platform_driver omap_control_usb_driver = {
272 .probe = omap_control_usb_probe,
274 .name = "omap-control-usb",
275 .owner = THIS_MODULE,
276 .of_match_table = of_match_ptr(omap_control_usb_id_table),
280 static int __init omap_control_usb_init(void)
282 return platform_driver_register(&omap_control_usb_driver);
284 subsys_initcall(omap_control_usb_init);
286 static void __exit omap_control_usb_exit(void)
288 platform_driver_unregister(&omap_control_usb_driver);
290 module_exit(omap_control_usb_exit);
292 MODULE_ALIAS("platform: omap_control_usb");
293 MODULE_AUTHOR("Texas Instruments Inc.");
294 MODULE_DESCRIPTION("OMAP Control Module USB Driver");
295 MODULE_LICENSE("GPL v2");