usb: musb: omap2430: Don't use omap_get_control_dev()
[deliverable/linux.git] / drivers / usb / phy / phy-omap-control.c
CommitLineData
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>
6cb9310a 23#include <linux/of_device.h>
01658f0f
KVA
24#include <linux/err.h>
25#include <linux/io.h>
26#include <linux/clk.h>
27#include <linux/usb/omap_control_usb.h>
28
29static struct omap_control_usb *control_usb;
30
31/**
32 * omap_get_control_dev - returns the device pointer for this control device
33 *
34 * This API should be called to get the device pointer for this control
35 * module device. This device pointer should be used for called other
36 * exported API's in this driver.
37 *
38 * To be used by PHY driver and glue driver.
39 */
40struct device *omap_get_control_dev(void)
41{
42 if (!control_usb)
43 return ERR_PTR(-ENODEV);
44
45 return control_usb->dev;
46}
47EXPORT_SYMBOL_GPL(omap_get_control_dev);
48
49/**
6cb9310a 50 * omap_control_usb_phy_power - power on/off the phy using control module reg
01658f0f 51 * @dev: the control module device
6cb9310a 52 * @on: 0 or 1, based on powering on or off the PHY
01658f0f 53 */
6cb9310a 54void omap_control_usb_phy_power(struct device *dev, int on)
01658f0f
KVA
55{
56 u32 val;
57 unsigned long rate;
6cb9310a 58 struct omap_control_usb *control_usb;
01658f0f 59
6cb9310a
RQ
60 if (IS_ERR(dev) || !dev) {
61 pr_err("%s: invalid device\n", __func__);
01658f0f 62 return;
6cb9310a 63 }
01658f0f 64
6cb9310a
RQ
65 control_usb = dev_get_drvdata(dev);
66 if (!control_usb) {
67 dev_err(dev, "%s: invalid control usb device\n", __func__);
68 return;
01658f0f
KVA
69 }
70
6cb9310a
RQ
71 if (control_usb->type == OMAP_CTRL_TYPE_OTGHS)
72 return;
01658f0f 73
6cb9310a
RQ
74 val = readl(control_usb->power);
75
76 switch (control_usb->type) {
77 case OMAP_CTRL_TYPE_USB2:
78 if (on)
79 val &= ~OMAP_CTRL_DEV_PHY_PD;
80 else
81 val |= OMAP_CTRL_DEV_PHY_PD;
82 break;
01658f0f 83
6cb9310a
RQ
84 case OMAP_CTRL_TYPE_PIPE3:
85 rate = clk_get_rate(control_usb->sys_clk);
86 rate = rate/1000000;
87
88 if (on) {
89 val &= ~(OMAP_CTRL_USB_PWRCTL_CLK_CMD_MASK |
90 OMAP_CTRL_USB_PWRCTL_CLK_FREQ_MASK);
91 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWERON <<
92 OMAP_CTRL_USB_PWRCTL_CLK_CMD_SHIFT;
93 val |= rate << OMAP_CTRL_USB_PWRCTL_CLK_FREQ_SHIFT;
94 } else {
95 val &= ~OMAP_CTRL_USB_PWRCTL_CLK_CMD_MASK;
96 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF <<
97 OMAP_CTRL_USB_PWRCTL_CLK_CMD_SHIFT;
98 }
99 break;
01658f0f 100
6cb9310a
RQ
101 case OMAP_CTRL_TYPE_DRA7USB2:
102 if (on)
103 val &= ~OMAP_CTRL_USB2_PHY_PD;
104 else
105 val |= OMAP_CTRL_USB2_PHY_PD;
106 break;
107 default:
108 dev_err(dev, "%s: type %d not recognized\n",
109 __func__, control_usb->type);
110 break;
111 }
01658f0f 112
6cb9310a 113 writel(val, control_usb->power);
01658f0f
KVA
114}
115EXPORT_SYMBOL_GPL(omap_control_usb_phy_power);
116
117/**
118 * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded
119 * @ctrl_usb: struct omap_control_usb *
120 *
121 * Writes to the mailbox register to notify the usb core that a usb
122 * device has been connected.
123 */
124static void omap_control_usb_host_mode(struct omap_control_usb *ctrl_usb)
125{
126 u32 val;
127
128 val = readl(ctrl_usb->otghs_control);
129 val &= ~(OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND);
130 val |= OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID;
131 writel(val, ctrl_usb->otghs_control);
132}
133
134/**
135 * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high
136 * impedance
137 * @ctrl_usb: struct omap_control_usb *
138 *
139 * Writes to the mailbox register to notify the usb core that it has been
140 * connected to a usb host.
141 */
142static void omap_control_usb_device_mode(struct omap_control_usb *ctrl_usb)
143{
144 u32 val;
145
146 val = readl(ctrl_usb->otghs_control);
147 val &= ~OMAP_CTRL_DEV_SESSEND;
148 val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_AVALID |
149 OMAP_CTRL_DEV_VBUSVALID;
150 writel(val, ctrl_usb->otghs_control);
151}
152
153/**
154 * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high
155 * impedance
156 * @ctrl_usb: struct omap_control_usb *
157 *
158 * Writes to the mailbox register to notify the usb core it's now in
159 * disconnected state.
160 */
161static void omap_control_usb_set_sessionend(struct omap_control_usb *ctrl_usb)
162{
163 u32 val;
164
165 val = readl(ctrl_usb->otghs_control);
166 val &= ~(OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID);
167 val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND;
168 writel(val, ctrl_usb->otghs_control);
169}
170
171/**
172 * omap_control_usb_set_mode - Calls to functions to set USB in one of host mode
173 * or device mode or to denote disconnected state
174 * @dev: the control module device
175 * @mode: The mode to which usb should be configured
176 *
177 * This is an API to write to the mailbox register to notify the usb core that
178 * a usb device has been connected.
179 */
180void omap_control_usb_set_mode(struct device *dev,
181 enum omap_control_usb_mode mode)
182{
183 struct omap_control_usb *ctrl_usb;
184
6cb9310a 185 if (IS_ERR(dev) || control_usb->type != OMAP_CTRL_TYPE_OTGHS)
01658f0f
KVA
186 return;
187
188 ctrl_usb = dev_get_drvdata(dev);
189
190 switch (mode) {
191 case USB_MODE_HOST:
192 omap_control_usb_host_mode(ctrl_usb);
193 break;
194 case USB_MODE_DEVICE:
195 omap_control_usb_device_mode(ctrl_usb);
196 break;
197 case USB_MODE_DISCONNECT:
198 omap_control_usb_set_sessionend(ctrl_usb);
199 break;
200 default:
201 dev_vdbg(dev, "invalid omap control usb mode\n");
202 }
203}
204EXPORT_SYMBOL_GPL(omap_control_usb_set_mode);
205
6cb9310a
RQ
206#ifdef CONFIG_OF
207
208static const enum omap_control_usb_type otghs_data = OMAP_CTRL_TYPE_OTGHS;
209static const enum omap_control_usb_type usb2_data = OMAP_CTRL_TYPE_USB2;
210static const enum omap_control_usb_type pipe3_data = OMAP_CTRL_TYPE_PIPE3;
211static const enum omap_control_usb_type dra7usb2_data = OMAP_CTRL_TYPE_DRA7USB2;
212
213static const struct of_device_id omap_control_usb_id_table[] = {
214 {
215 .compatible = "ti,control-phy-otghs",
216 .data = &otghs_data,
217 },
218 {
219 .compatible = "ti,control-phy-usb2",
220 .data = &usb2_data,
221 },
222 {
223 .compatible = "ti,control-phy-pipe3",
224 .data = &pipe3_data,
225 },
226 {
227 .compatible = "ti,control-phy-dra7usb2",
228 .data = &dra7usb2_data,
229 },
230 {},
231};
232MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
233#endif
234
235
01658f0f
KVA
236static int omap_control_usb_probe(struct platform_device *pdev)
237{
238 struct resource *res;
6cb9310a
RQ
239 const struct of_device_id *of_id;
240
241 of_id = of_match_device(of_match_ptr(omap_control_usb_id_table),
242 &pdev->dev);
243 if (!of_id)
244 return -EINVAL;
01658f0f
KVA
245
246 control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb),
247 GFP_KERNEL);
248 if (!control_usb) {
249 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
250 return -ENOMEM;
251 }
252
6cb9310a
RQ
253 control_usb->dev = &pdev->dev;
254 control_usb->type = *(enum omap_control_usb_type *)of_id->data;
01658f0f 255
6cb9310a 256 if (control_usb->type == OMAP_CTRL_TYPE_OTGHS) {
01658f0f
KVA
257 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
258 "otghs_control");
57ae575b 259 control_usb->otghs_control = devm_ioremap_resource(
01658f0f 260 &pdev->dev, res);
57ae575b
SK
261 if (IS_ERR(control_usb->otghs_control))
262 return PTR_ERR(control_usb->otghs_control);
6cb9310a 263 } else {
01658f0f 264 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
6cb9310a
RQ
265 "power");
266 control_usb->power = devm_ioremap_resource(&pdev->dev, res);
267 if (IS_ERR(control_usb->power)) {
268 dev_err(&pdev->dev, "Couldn't get power register\n");
269 return PTR_ERR(control_usb->power);
270 }
271 }
01658f0f 272
6cb9310a 273 if (control_usb->type == OMAP_CTRL_TYPE_PIPE3) {
01658f0f
KVA
274 control_usb->sys_clk = devm_clk_get(control_usb->dev,
275 "sys_clkin");
276 if (IS_ERR(control_usb->sys_clk)) {
277 pr_err("%s: unable to get sys_clkin\n", __func__);
278 return -EINVAL;
279 }
280 }
281
01658f0f
KVA
282 dev_set_drvdata(control_usb->dev, control_usb);
283
284 return 0;
285}
286
01658f0f
KVA
287static struct platform_driver omap_control_usb_driver = {
288 .probe = omap_control_usb_probe,
289 .driver = {
290 .name = "omap-control-usb",
291 .owner = THIS_MODULE,
292 .of_match_table = of_match_ptr(omap_control_usb_id_table),
293 },
294};
295
296static int __init omap_control_usb_init(void)
297{
298 return platform_driver_register(&omap_control_usb_driver);
299}
300subsys_initcall(omap_control_usb_init);
301
302static void __exit omap_control_usb_exit(void)
303{
304 platform_driver_unregister(&omap_control_usb_driver);
305}
306module_exit(omap_control_usb_exit);
307
308MODULE_ALIAS("platform: omap_control_usb");
309MODULE_AUTHOR("Texas Instruments Inc.");
310MODULE_DESCRIPTION("OMAP Control Module USB Driver");
311MODULE_LICENSE("GPL v2");
This page took 0.084029 seconds and 5 git commands to generate.