Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / usb / phy / phy-omap-control.c
blob09c5ace1edd8a987f015508640f7f461508dd0a9
1 /*
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.
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>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/err.h>
25 #include <linux/io.h>
26 #include <linux/clk.h>
27 #include <linux/usb/omap_control_usb.h>
29 /**
30 * omap_control_usb_phy_power - power on/off the phy using control module reg
31 * @dev: the control module device
32 * @on: 0 or 1, based on powering on or off the PHY
34 void omap_control_usb_phy_power(struct device *dev, int on)
36 u32 val;
37 unsigned long rate;
38 struct omap_control_usb *control_usb;
40 if (IS_ERR(dev) || !dev) {
41 pr_err("%s: invalid device\n", __func__);
42 return;
45 control_usb = dev_get_drvdata(dev);
46 if (!control_usb) {
47 dev_err(dev, "%s: invalid control usb device\n", __func__);
48 return;
51 if (control_usb->type == OMAP_CTRL_TYPE_OTGHS)
52 return;
54 val = readl(control_usb->power);
56 switch (control_usb->type) {
57 case OMAP_CTRL_TYPE_USB2:
58 if (on)
59 val &= ~OMAP_CTRL_DEV_PHY_PD;
60 else
61 val |= OMAP_CTRL_DEV_PHY_PD;
62 break;
64 case OMAP_CTRL_TYPE_PIPE3:
65 rate = clk_get_rate(control_usb->sys_clk);
66 rate = rate/1000000;
68 if (on) {
69 val &= ~(OMAP_CTRL_USB_PWRCTL_CLK_CMD_MASK |
70 OMAP_CTRL_USB_PWRCTL_CLK_FREQ_MASK);
71 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWERON <<
72 OMAP_CTRL_USB_PWRCTL_CLK_CMD_SHIFT;
73 val |= rate << OMAP_CTRL_USB_PWRCTL_CLK_FREQ_SHIFT;
74 } else {
75 val &= ~OMAP_CTRL_USB_PWRCTL_CLK_CMD_MASK;
76 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF <<
77 OMAP_CTRL_USB_PWRCTL_CLK_CMD_SHIFT;
79 break;
81 case OMAP_CTRL_TYPE_DRA7USB2:
82 if (on)
83 val &= ~OMAP_CTRL_USB2_PHY_PD;
84 else
85 val |= OMAP_CTRL_USB2_PHY_PD;
86 break;
87 default:
88 dev_err(dev, "%s: type %d not recognized\n",
89 __func__, control_usb->type);
90 break;
93 writel(val, control_usb->power);
95 EXPORT_SYMBOL_GPL(omap_control_usb_phy_power);
97 /**
98 * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded
99 * @ctrl_usb: struct omap_control_usb *
101 * Writes to the mailbox register to notify the usb core that a usb
102 * device has been connected.
104 static void omap_control_usb_host_mode(struct omap_control_usb *ctrl_usb)
106 u32 val;
108 val = readl(ctrl_usb->otghs_control);
109 val &= ~(OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND);
110 val |= OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID;
111 writel(val, ctrl_usb->otghs_control);
115 * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high
116 * impedance
117 * @ctrl_usb: struct omap_control_usb *
119 * Writes to the mailbox register to notify the usb core that it has been
120 * connected to a usb host.
122 static void omap_control_usb_device_mode(struct omap_control_usb *ctrl_usb)
124 u32 val;
126 val = readl(ctrl_usb->otghs_control);
127 val &= ~OMAP_CTRL_DEV_SESSEND;
128 val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_AVALID |
129 OMAP_CTRL_DEV_VBUSVALID;
130 writel(val, ctrl_usb->otghs_control);
134 * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high
135 * impedance
136 * @ctrl_usb: struct omap_control_usb *
138 * Writes to the mailbox register to notify the usb core it's now in
139 * disconnected state.
141 static void omap_control_usb_set_sessionend(struct omap_control_usb *ctrl_usb)
143 u32 val;
145 val = readl(ctrl_usb->otghs_control);
146 val &= ~(OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID);
147 val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND;
148 writel(val, ctrl_usb->otghs_control);
152 * omap_control_usb_set_mode - Calls to functions to set USB in one of host mode
153 * or device mode or to denote disconnected state
154 * @dev: the control module device
155 * @mode: The mode to which usb should be configured
157 * This is an API to write to the mailbox register to notify the usb core that
158 * a usb device has been connected.
160 void omap_control_usb_set_mode(struct device *dev,
161 enum omap_control_usb_mode mode)
163 struct omap_control_usb *ctrl_usb;
165 if (IS_ERR(dev) || !dev)
166 return;
168 ctrl_usb = dev_get_drvdata(dev);
170 if (!ctrl_usb) {
171 dev_err(dev, "Invalid control usb device\n");
172 return;
175 if (ctrl_usb->type != OMAP_CTRL_TYPE_OTGHS)
176 return;
178 switch (mode) {
179 case USB_MODE_HOST:
180 omap_control_usb_host_mode(ctrl_usb);
181 break;
182 case USB_MODE_DEVICE:
183 omap_control_usb_device_mode(ctrl_usb);
184 break;
185 case USB_MODE_DISCONNECT:
186 omap_control_usb_set_sessionend(ctrl_usb);
187 break;
188 default:
189 dev_vdbg(dev, "invalid omap control usb mode\n");
192 EXPORT_SYMBOL_GPL(omap_control_usb_set_mode);
194 #ifdef CONFIG_OF
196 static const enum omap_control_usb_type otghs_data = OMAP_CTRL_TYPE_OTGHS;
197 static const enum omap_control_usb_type usb2_data = OMAP_CTRL_TYPE_USB2;
198 static const enum omap_control_usb_type pipe3_data = OMAP_CTRL_TYPE_PIPE3;
199 static const enum omap_control_usb_type dra7usb2_data = OMAP_CTRL_TYPE_DRA7USB2;
201 static const struct of_device_id omap_control_usb_id_table[] = {
203 .compatible = "ti,control-phy-otghs",
204 .data = &otghs_data,
207 .compatible = "ti,control-phy-usb2",
208 .data = &usb2_data,
211 .compatible = "ti,control-phy-pipe3",
212 .data = &pipe3_data,
215 .compatible = "ti,control-phy-dra7usb2",
216 .data = &dra7usb2_data,
220 MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
221 #endif
224 static int omap_control_usb_probe(struct platform_device *pdev)
226 struct resource *res;
227 const struct of_device_id *of_id;
228 struct omap_control_usb *control_usb;
230 of_id = of_match_device(of_match_ptr(omap_control_usb_id_table),
231 &pdev->dev);
232 if (!of_id)
233 return -EINVAL;
235 control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb),
236 GFP_KERNEL);
237 if (!control_usb) {
238 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
239 return -ENOMEM;
242 control_usb->dev = &pdev->dev;
243 control_usb->type = *(enum omap_control_usb_type *)of_id->data;
245 if (control_usb->type == OMAP_CTRL_TYPE_OTGHS) {
246 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
247 "otghs_control");
248 control_usb->otghs_control = devm_ioremap_resource(
249 &pdev->dev, res);
250 if (IS_ERR(control_usb->otghs_control))
251 return PTR_ERR(control_usb->otghs_control);
252 } else {
253 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
254 "power");
255 control_usb->power = devm_ioremap_resource(&pdev->dev, res);
256 if (IS_ERR(control_usb->power)) {
257 dev_err(&pdev->dev, "Couldn't get power register\n");
258 return PTR_ERR(control_usb->power);
262 if (control_usb->type == OMAP_CTRL_TYPE_PIPE3) {
263 control_usb->sys_clk = devm_clk_get(control_usb->dev,
264 "sys_clkin");
265 if (IS_ERR(control_usb->sys_clk)) {
266 pr_err("%s: unable to get sys_clkin\n", __func__);
267 return -EINVAL;
271 dev_set_drvdata(control_usb->dev, control_usb);
273 return 0;
276 static struct platform_driver omap_control_usb_driver = {
277 .probe = omap_control_usb_probe,
278 .driver = {
279 .name = "omap-control-usb",
280 .owner = THIS_MODULE,
281 .of_match_table = of_match_ptr(omap_control_usb_id_table),
285 static int __init omap_control_usb_init(void)
287 return platform_driver_register(&omap_control_usb_driver);
289 subsys_initcall(omap_control_usb_init);
291 static void __exit omap_control_usb_exit(void)
293 platform_driver_unregister(&omap_control_usb_driver);
295 module_exit(omap_control_usb_exit);
297 MODULE_ALIAS("platform: omap_control_usb");
298 MODULE_AUTHOR("Texas Instruments Inc.");
299 MODULE_DESCRIPTION("OMAP Control Module USB Driver");
300 MODULE_LICENSE("GPL v2");