x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / usb / phy / phy-omap-control.c
bloba4dda8e1256257104cdfdd3d54707af57361ed8f
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/err.h>
24 #include <linux/io.h>
25 #include <linux/clk.h>
26 #include <linux/usb/omap_control_usb.h>
28 static struct omap_control_usb *control_usb;
30 /**
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)
41 if (!control_usb)
42 return ERR_PTR(-ENODEV);
44 return control_usb->dev;
46 EXPORT_SYMBOL_GPL(omap_get_control_dev);
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
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)
58 u32 val;
59 unsigned long rate;
60 struct omap_control_usb *control_usb = dev_get_drvdata(dev);
62 if (control_usb->type != OMAP_CTRL_DEV_TYPE2)
63 return;
65 rate = clk_get_rate(control_usb->sys_clk);
66 rate = rate/1000000;
68 val = readl(control_usb->phy_power);
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;
82 writel(val, control_usb->phy_power);
84 EXPORT_SYMBOL_GPL(omap_control_usb3_phy_power);
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
91 void omap_control_usb_phy_power(struct device *dev, int on)
93 u32 val;
94 struct omap_control_usb *control_usb = dev_get_drvdata(dev);
96 val = readl(control_usb->dev_conf);
98 if (on)
99 val &= ~OMAP_CTRL_DEV_PHY_PD;
100 else
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)
116 u32 val;
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
126 * impedance
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)
134 u32 val;
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
145 * impedance
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)
153 u32 val;
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)
176 return;
178 ctrl_usb = dev_get_drvdata(dev);
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");
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 =
201 dev_get_platdata(&pdev->dev);
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;
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;
219 control_usb->dev = &pdev->dev;
221 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
222 "control_dev_conf");
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);
227 if (control_usb->type == OMAP_CTRL_DEV_TYPE1) {
228 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
229 "otghs_control");
230 control_usb->otghs_control = devm_ioremap_resource(
231 &pdev->dev, res);
232 if (IS_ERR(control_usb->otghs_control))
233 return PTR_ERR(control_usb->otghs_control);
236 if (control_usb->type == OMAP_CTRL_DEV_TYPE2) {
237 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
238 "phy_power_usb");
239 control_usb->phy_power = devm_ioremap_resource(
240 &pdev->dev, res);
241 if (IS_ERR(control_usb->phy_power))
242 return PTR_ERR(control_usb->phy_power);
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;
253 dev_set_drvdata(control_usb->dev, control_usb);
255 return 0;
258 #ifdef CONFIG_OF
259 static const struct of_device_id omap_control_usb_id_table[] = {
260 { .compatible = "ti,omap-control-usb" },
263 MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
264 #endif
266 static 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),
275 static int __init omap_control_usb_init(void)
277 return platform_driver_register(&omap_control_usb_driver);
279 subsys_initcall(omap_control_usb_init);
281 static void __exit omap_control_usb_exit(void)
283 platform_driver_unregister(&omap_control_usb_driver);
285 module_exit(omap_control_usb_exit);
287 MODULE_ALIAS("platform: omap_control_usb");
288 MODULE_AUTHOR("Texas Instruments Inc.");
289 MODULE_DESCRIPTION("OMAP Control Module USB Driver");
290 MODULE_LICENSE("GPL v2");