Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / phy / phy-omap-otg.c
blob56ee7603034b890fa49d7bf6574692d5c6383e41
1 /*
2 * OMAP OTG controller driver
4 * Based on code from tahvo-usb.c and isp1301_omap.c drivers.
6 * Copyright (C) 2005-2006 Nokia Corporation
7 * Copyright (C) 2004 Texas Instruments
8 * Copyright (C) 2004 David Brownell
10 * This file is subject to the terms and conditions of the GNU General
11 * Public License. See the file "COPYING" in the main directory of this
12 * archive for more details.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/io.h>
21 #include <linux/err.h>
22 #include <linux/extcon.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/platform_data/usb-omap1.h>
29 struct otg_device {
30 void __iomem *base;
31 bool id;
32 bool vbus;
33 struct extcon_specific_cable_nb vbus_dev;
34 struct extcon_specific_cable_nb id_dev;
35 struct notifier_block vbus_nb;
36 struct notifier_block id_nb;
39 #define OMAP_OTG_CTRL 0x0c
40 #define OMAP_OTG_ASESSVLD (1 << 20)
41 #define OMAP_OTG_BSESSEND (1 << 19)
42 #define OMAP_OTG_BSESSVLD (1 << 18)
43 #define OMAP_OTG_VBUSVLD (1 << 17)
44 #define OMAP_OTG_ID (1 << 16)
45 #define OMAP_OTG_XCEIV_OUTPUTS \
46 (OMAP_OTG_ASESSVLD | OMAP_OTG_BSESSEND | OMAP_OTG_BSESSVLD | \
47 OMAP_OTG_VBUSVLD | OMAP_OTG_ID)
49 static void omap_otg_ctrl(struct otg_device *otg_dev, u32 outputs)
51 u32 l;
53 l = readl(otg_dev->base + OMAP_OTG_CTRL);
54 l &= ~OMAP_OTG_XCEIV_OUTPUTS;
55 l |= outputs;
56 writel(l, otg_dev->base + OMAP_OTG_CTRL);
59 static void omap_otg_set_mode(struct otg_device *otg_dev)
61 if (!otg_dev->id && otg_dev->vbus)
62 /* Set B-session valid. */
63 omap_otg_ctrl(otg_dev, OMAP_OTG_ID | OMAP_OTG_BSESSVLD);
64 else if (otg_dev->vbus)
65 /* Set A-session valid. */
66 omap_otg_ctrl(otg_dev, OMAP_OTG_ASESSVLD);
67 else if (!otg_dev->id)
68 /* Set B-session end to indicate no VBUS. */
69 omap_otg_ctrl(otg_dev, OMAP_OTG_ID | OMAP_OTG_BSESSEND);
72 static int omap_otg_id_notifier(struct notifier_block *nb,
73 unsigned long event, void *ptr)
75 struct otg_device *otg_dev = container_of(nb, struct otg_device, id_nb);
77 otg_dev->id = event;
78 omap_otg_set_mode(otg_dev);
80 return NOTIFY_DONE;
83 static int omap_otg_vbus_notifier(struct notifier_block *nb,
84 unsigned long event, void *ptr)
86 struct otg_device *otg_dev = container_of(nb, struct otg_device,
87 vbus_nb);
89 otg_dev->vbus = event;
90 omap_otg_set_mode(otg_dev);
92 return NOTIFY_DONE;
95 static int omap_otg_probe(struct platform_device *pdev)
97 const struct omap_usb_config *config = pdev->dev.platform_data;
98 struct otg_device *otg_dev;
99 struct extcon_dev *extcon;
100 int ret;
101 u32 rev;
103 if (!config || !config->extcon)
104 return -ENODEV;
106 extcon = extcon_get_extcon_dev(config->extcon);
107 if (!extcon)
108 return -EPROBE_DEFER;
110 otg_dev = devm_kzalloc(&pdev->dev, sizeof(*otg_dev), GFP_KERNEL);
111 if (!otg_dev)
112 return -ENOMEM;
114 otg_dev->base = devm_ioremap_resource(&pdev->dev, &pdev->resource[0]);
115 if (IS_ERR(otg_dev->base))
116 return PTR_ERR(otg_dev->base);
118 otg_dev->id_nb.notifier_call = omap_otg_id_notifier;
119 otg_dev->vbus_nb.notifier_call = omap_otg_vbus_notifier;
121 ret = extcon_register_interest(&otg_dev->id_dev, config->extcon,
122 "USB-HOST", &otg_dev->id_nb);
123 if (ret)
124 return ret;
126 ret = extcon_register_interest(&otg_dev->vbus_dev, config->extcon,
127 "USB", &otg_dev->vbus_nb);
128 if (ret) {
129 extcon_unregister_interest(&otg_dev->id_dev);
130 return ret;
133 otg_dev->id = extcon_get_cable_state(extcon, "USB-HOST");
134 otg_dev->vbus = extcon_get_cable_state(extcon, "USB");
135 omap_otg_set_mode(otg_dev);
137 rev = readl(otg_dev->base);
139 dev_info(&pdev->dev,
140 "OMAP USB OTG controller rev %d.%d (%s, id=%d, vbus=%d)\n",
141 (rev >> 4) & 0xf, rev & 0xf, config->extcon, otg_dev->id,
142 otg_dev->vbus);
144 return 0;
147 static int omap_otg_remove(struct platform_device *pdev)
149 struct otg_device *otg_dev = platform_get_drvdata(pdev);
151 extcon_unregister_interest(&otg_dev->id_dev);
152 extcon_unregister_interest(&otg_dev->vbus_dev);
154 return 0;
157 static struct platform_driver omap_otg_driver = {
158 .probe = omap_otg_probe,
159 .remove = omap_otg_remove,
160 .driver = {
161 .name = "omap_otg",
164 module_platform_driver(omap_otg_driver);
166 MODULE_DESCRIPTION("OMAP USB OTG controller driver");
167 MODULE_LICENSE("GPL");
168 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");