Merge remote-tracking branch 'asoc/topic/mc13783' into asoc-next
[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>
23#include <linux/err.h>
24#include <linux/io.h>
25#include <linux/clk.h>
26#include <linux/usb/omap_control_usb.h>
27
28static 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 */
39struct device *omap_get_control_dev(void)
40{
41 if (!control_usb)
42 return ERR_PTR(-ENODEV);
43
44 return control_usb->dev;
45}
46EXPORT_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 */
56void 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}
84EXPORT_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 */
91void 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}
105EXPORT_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 */
114static 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 */
132static 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 */
151static 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 */
170void 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}
194EXPORT_SYMBOL_GPL(omap_control_usb_set_mode);
195
196static int omap_control_usb_probe(struct platform_device *pdev)
197{
198 struct resource *res;
199 struct device_node *np = pdev->dev.of_node;
19f9e188
JH
200 struct omap_control_usb_platform_data *pdata =
201 dev_get_platdata(&pdev->dev);
01658f0f
KVA
202
203 control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb),
204 GFP_KERNEL);
205 if (!control_usb) {
206 dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
207 return -ENOMEM;
208 }
209
210 if (np) {
211 of_property_read_u32(np, "ti,type", &control_usb->type);
212 } else if (pdata) {
213 control_usb->type = pdata->type;
214 } else {
215 dev_err(&pdev->dev, "no pdata present\n");
216 return -EINVAL;
217 }
218
219 control_usb->dev = &pdev->dev;
220
221 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
222 "control_dev_conf");
57ae575b
SK
223 control_usb->dev_conf = devm_ioremap_resource(&pdev->dev, res);
224 if (IS_ERR(control_usb->dev_conf))
225 return PTR_ERR(control_usb->dev_conf);
01658f0f
KVA
226
227 if (control_usb->type == OMAP_CTRL_DEV_TYPE1) {
228 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
229 "otghs_control");
57ae575b 230 control_usb->otghs_control = devm_ioremap_resource(
01658f0f 231 &pdev->dev, res);
57ae575b
SK
232 if (IS_ERR(control_usb->otghs_control))
233 return PTR_ERR(control_usb->otghs_control);
01658f0f
KVA
234 }
235
236 if (control_usb->type == OMAP_CTRL_DEV_TYPE2) {
237 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
238 "phy_power_usb");
57ae575b 239 control_usb->phy_power = devm_ioremap_resource(
01658f0f 240 &pdev->dev, res);
57ae575b
SK
241 if (IS_ERR(control_usb->phy_power))
242 return PTR_ERR(control_usb->phy_power);
01658f0f
KVA
243
244 control_usb->sys_clk = devm_clk_get(control_usb->dev,
245 "sys_clkin");
246 if (IS_ERR(control_usb->sys_clk)) {
247 pr_err("%s: unable to get sys_clkin\n", __func__);
248 return -EINVAL;
249 }
250 }
251
252
253 dev_set_drvdata(control_usb->dev, control_usb);
254
255 return 0;
256}
257
258#ifdef CONFIG_OF
259static const struct of_device_id omap_control_usb_id_table[] = {
260 { .compatible = "ti,omap-control-usb" },
261 {}
262};
263MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
264#endif
265
266static struct platform_driver omap_control_usb_driver = {
267 .probe = omap_control_usb_probe,
268 .driver = {
269 .name = "omap-control-usb",
270 .owner = THIS_MODULE,
271 .of_match_table = of_match_ptr(omap_control_usb_id_table),
272 },
273};
274
275static int __init omap_control_usb_init(void)
276{
277 return platform_driver_register(&omap_control_usb_driver);
278}
279subsys_initcall(omap_control_usb_init);
280
281static void __exit omap_control_usb_exit(void)
282{
283 platform_driver_unregister(&omap_control_usb_driver);
284}
285module_exit(omap_control_usb_exit);
286
287MODULE_ALIAS("platform: omap_control_usb");
288MODULE_AUTHOR("Texas Instruments Inc.");
289MODULE_DESCRIPTION("OMAP Control Module USB Driver");
290MODULE_LICENSE("GPL v2");
This page took 0.091716 seconds and 5 git commands to generate.