]>
Commit | Line | Data |
---|---|---|
01658f0f KVA |
1 | /* |
2 | * omap-control-usb.c - The USB part of control module. | |
3 | * | |
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. | |
9 | * | |
10 | * Author: Kishon Vijay Abraham I <kishon@ti.com> | |
11 | * | |
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. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/of.h> | |
23 | #include <linux/err.h> | |
24 | #include <linux/io.h> | |
25 | #include <linux/clk.h> | |
26 | #include <linux/usb/omap_control_usb.h> | |
27 | ||
28 | static struct omap_control_usb *control_usb; | |
29 | ||
30 | /** | |
31 | * omap_get_control_dev - returns the device pointer for this control device | |
32 | * | |
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. | |
36 | * | |
37 | * To be used by PHY driver and glue driver. | |
38 | */ | |
39 | struct device *omap_get_control_dev(void) | |
40 | { | |
41 | if (!control_usb) | |
42 | return ERR_PTR(-ENODEV); | |
43 | ||
44 | return control_usb->dev; | |
45 | } | |
46 | EXPORT_SYMBOL_GPL(omap_get_control_dev); | |
47 | ||
48 | /** | |
49 | * omap_control_usb3_phy_power - power on/off the serializer using control | |
50 | * module | |
51 | * @dev: the control module device | |
52 | * @on: 0 to off and 1 to on based on powering on or off the PHY | |
53 | * | |
54 | * usb3 PHY driver should call this API to power on or off the PHY. | |
55 | */ | |
56 | void omap_control_usb3_phy_power(struct device *dev, bool on) | |
57 | { | |
58 | u32 val; | |
59 | unsigned long rate; | |
60 | struct omap_control_usb *control_usb = dev_get_drvdata(dev); | |
61 | ||
62 | if (control_usb->type != OMAP_CTRL_DEV_TYPE2) | |
63 | return; | |
64 | ||
65 | rate = clk_get_rate(control_usb->sys_clk); | |
66 | rate = rate/1000000; | |
67 | ||
68 | val = readl(control_usb->phy_power); | |
69 | ||
70 | if (on) { | |
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; | |
76 | } else { | |
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; | |
80 | } | |
81 | ||
82 | writel(val, control_usb->phy_power); | |
83 | } | |
84 | EXPORT_SYMBOL_GPL(omap_control_usb3_phy_power); | |
85 | ||
86 | /** | |
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 | |
90 | */ | |
91 | void omap_control_usb_phy_power(struct device *dev, int on) | |
92 | { | |
93 | u32 val; | |
94 | struct omap_control_usb *control_usb = dev_get_drvdata(dev); | |
95 | ||
96 | val = readl(control_usb->dev_conf); | |
97 | ||
98 | if (on) | |
99 | val &= ~OMAP_CTRL_DEV_PHY_PD; | |
100 | else | |
101 | val |= OMAP_CTRL_DEV_PHY_PD; | |
102 | ||
103 | writel(val, control_usb->dev_conf); | |
104 | } | |
105 | EXPORT_SYMBOL_GPL(omap_control_usb_phy_power); | |
106 | ||
107 | /** | |
108 | * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded | |
109 | * @ctrl_usb: struct omap_control_usb * | |
110 | * | |
111 | * Writes to the mailbox register to notify the usb core that a usb | |
112 | * device has been connected. | |
113 | */ | |
114 | static void omap_control_usb_host_mode(struct omap_control_usb *ctrl_usb) | |
115 | { | |
116 | u32 val; | |
117 | ||
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); | |
122 | } | |
123 | ||
124 | /** | |
125 | * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high | |
126 | * impedance | |
127 | * @ctrl_usb: struct omap_control_usb * | |
128 | * | |
129 | * Writes to the mailbox register to notify the usb core that it has been | |
130 | * connected to a usb host. | |
131 | */ | |
132 | static void omap_control_usb_device_mode(struct omap_control_usb *ctrl_usb) | |
133 | { | |
134 | u32 val; | |
135 | ||
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); | |
141 | } | |
142 | ||
143 | /** | |
144 | * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high | |
145 | * impedance | |
146 | * @ctrl_usb: struct omap_control_usb * | |
147 | * | |
148 | * Writes to the mailbox register to notify the usb core it's now in | |
149 | * disconnected state. | |
150 | */ | |
151 | static void omap_control_usb_set_sessionend(struct omap_control_usb *ctrl_usb) | |
152 | { | |
153 | u32 val; | |
154 | ||
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); | |
159 | } | |
160 | ||
161 | /** | |
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 | |
166 | * | |
167 | * This is an API to write to the mailbox register to notify the usb core that | |
168 | * a usb device has been connected. | |
169 | */ | |
170 | void omap_control_usb_set_mode(struct device *dev, | |
171 | enum omap_control_usb_mode mode) | |
172 | { | |
173 | struct omap_control_usb *ctrl_usb; | |
174 | ||
175 | if (IS_ERR(dev) || control_usb->type != OMAP_CTRL_DEV_TYPE1) | |
176 | return; | |
177 | ||
178 | ctrl_usb = dev_get_drvdata(dev); | |
179 | ||
180 | switch (mode) { | |
181 | case USB_MODE_HOST: | |
182 | omap_control_usb_host_mode(ctrl_usb); | |
183 | break; | |
184 | case USB_MODE_DEVICE: | |
185 | omap_control_usb_device_mode(ctrl_usb); | |
186 | break; | |
187 | case USB_MODE_DISCONNECT: | |
188 | omap_control_usb_set_sessionend(ctrl_usb); | |
189 | break; | |
190 | default: | |
191 | dev_vdbg(dev, "invalid omap control usb mode\n"); | |
192 | } | |
193 | } | |
194 | EXPORT_SYMBOL_GPL(omap_control_usb_set_mode); | |
195 | ||
196 | static int omap_control_usb_probe(struct platform_device *pdev) | |
197 | { | |
198 | struct resource *res; | |
199 | struct device_node *np = pdev->dev.of_node; | |
01658f0f KVA |
200 | |
201 | control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb), | |
202 | GFP_KERNEL); | |
203 | if (!control_usb) { | |
204 | dev_err(&pdev->dev, "unable to alloc memory for control usb\n"); | |
205 | return -ENOMEM; | |
206 | } | |
207 | ||
4fd06af9 | 208 | if (np) |
01658f0f | 209 | of_property_read_u32(np, "ti,type", &control_usb->type); |
4fd06af9 RQ |
210 | else |
211 | return -EINVAL; /* We only support DT boot */ | |
01658f0f KVA |
212 | |
213 | control_usb->dev = &pdev->dev; | |
214 | ||
215 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, | |
216 | "control_dev_conf"); | |
57ae575b SK |
217 | control_usb->dev_conf = devm_ioremap_resource(&pdev->dev, res); |
218 | if (IS_ERR(control_usb->dev_conf)) | |
219 | return PTR_ERR(control_usb->dev_conf); | |
01658f0f KVA |
220 | |
221 | if (control_usb->type == OMAP_CTRL_DEV_TYPE1) { | |
222 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, | |
223 | "otghs_control"); | |
57ae575b | 224 | control_usb->otghs_control = devm_ioremap_resource( |
01658f0f | 225 | &pdev->dev, res); |
57ae575b SK |
226 | if (IS_ERR(control_usb->otghs_control)) |
227 | return PTR_ERR(control_usb->otghs_control); | |
01658f0f KVA |
228 | } |
229 | ||
230 | if (control_usb->type == OMAP_CTRL_DEV_TYPE2) { | |
231 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, | |
232 | "phy_power_usb"); | |
57ae575b | 233 | control_usb->phy_power = devm_ioremap_resource( |
01658f0f | 234 | &pdev->dev, res); |
57ae575b SK |
235 | if (IS_ERR(control_usb->phy_power)) |
236 | return PTR_ERR(control_usb->phy_power); | |
01658f0f KVA |
237 | |
238 | control_usb->sys_clk = devm_clk_get(control_usb->dev, | |
239 | "sys_clkin"); | |
240 | if (IS_ERR(control_usb->sys_clk)) { | |
241 | pr_err("%s: unable to get sys_clkin\n", __func__); | |
242 | return -EINVAL; | |
243 | } | |
244 | } | |
245 | ||
246 | ||
247 | dev_set_drvdata(control_usb->dev, control_usb); | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | #ifdef CONFIG_OF | |
253 | static const struct of_device_id omap_control_usb_id_table[] = { | |
254 | { .compatible = "ti,omap-control-usb" }, | |
255 | {} | |
256 | }; | |
257 | MODULE_DEVICE_TABLE(of, omap_control_usb_id_table); | |
258 | #endif | |
259 | ||
260 | static struct platform_driver omap_control_usb_driver = { | |
261 | .probe = omap_control_usb_probe, | |
262 | .driver = { | |
263 | .name = "omap-control-usb", | |
264 | .owner = THIS_MODULE, | |
265 | .of_match_table = of_match_ptr(omap_control_usb_id_table), | |
266 | }, | |
267 | }; | |
268 | ||
269 | static int __init omap_control_usb_init(void) | |
270 | { | |
271 | return platform_driver_register(&omap_control_usb_driver); | |
272 | } | |
273 | subsys_initcall(omap_control_usb_init); | |
274 | ||
275 | static void __exit omap_control_usb_exit(void) | |
276 | { | |
277 | platform_driver_unregister(&omap_control_usb_driver); | |
278 | } | |
279 | module_exit(omap_control_usb_exit); | |
280 | ||
281 | MODULE_ALIAS("platform: omap_control_usb"); | |
282 | MODULE_AUTHOR("Texas Instruments Inc."); | |
283 | MODULE_DESCRIPTION("OMAP Control Module USB Driver"); | |
284 | MODULE_LICENSE("GPL v2"); |