usb: gadget: f_uac2: don't queue new requests when shutting down
[linux-2.6-block.git] / drivers / usb / phy / phy-ulpi.c
CommitLineData
2d57a95f
DM
1/*
2 * Generic ULPI USB transceiver support
3 *
4 * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
5 *
6 * Based on sources from
7 *
8 * Sascha Hauer <s.hauer@pengutronix.de>
9 * Freescale Semiconductors
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/kernel.h>
5a0e3ad6 27#include <linux/slab.h>
f940fcd8 28#include <linux/export.h>
2d57a95f
DM
29#include <linux/usb.h>
30#include <linux/usb/otg.h>
31#include <linux/usb/ulpi.h>
32
96b9e832
IG
33
34struct ulpi_info {
35 unsigned int id;
36 char *name;
37};
38
2d57a95f 39#define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
96b9e832
IG
40#define ULPI_INFO(_id, _name) \
41 { \
42 .id = (_id), \
43 .name = (_name), \
44 }
2d57a95f 45
2d57a95f 46/* ULPI hardcoded IDs, used for probing */
96b9e832
IG
47static struct ulpi_info ulpi_ids[] = {
48 ULPI_INFO(ULPI_ID(0x04cc, 0x1504), "NXP ISP1504"),
1e4cba8b 49 ULPI_INFO(ULPI_ID(0x0424, 0x0006), "SMSC USB331x"),
ead5178b
MS
50 ULPI_INFO(ULPI_ID(0x0424, 0x0007), "SMSC USB3320"),
51 ULPI_INFO(ULPI_ID(0x0451, 0x1507), "TI TUSB1210"),
2d57a95f
DM
52};
53
298b083c 54static int ulpi_set_otg_flags(struct usb_phy *phy)
2d57a95f 55{
13dd0c97
IG
56 unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
57 ULPI_OTG_CTRL_DM_PULLDOWN;
2d57a95f 58
298b083c 59 if (phy->flags & ULPI_OTG_ID_PULLUP)
fc567f06 60 flags |= ULPI_OTG_CTRL_ID_PULLUP;
2d57a95f 61
13dd0c97
IG
62 /*
63 * ULPI Specification rev.1.1 default
64 * for Dp/DmPulldown is enabled.
65 */
298b083c 66 if (phy->flags & ULPI_OTG_DP_PULLDOWN_DIS)
13dd0c97 67 flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
2d57a95f 68
298b083c 69 if (phy->flags & ULPI_OTG_DM_PULLDOWN_DIS)
13dd0c97 70 flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
2d57a95f 71
298b083c 72 if (phy->flags & ULPI_OTG_EXTVBUSIND)
fc567f06 73 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
2d57a95f 74
298b083c 75 return usb_phy_io_write(phy, flags, ULPI_OTG_CTRL);
2d57a95f
DM
76}
77
298b083c 78static int ulpi_set_fc_flags(struct usb_phy *phy)
13dd0c97
IG
79{
80 unsigned int flags = 0;
81
82 /*
83 * ULPI Specification rev.1.1 default
84 * for XcvrSelect is Full Speed.
85 */
298b083c 86 if (phy->flags & ULPI_FC_HS)
13dd0c97 87 flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
298b083c 88 else if (phy->flags & ULPI_FC_LS)
13dd0c97 89 flags |= ULPI_FUNC_CTRL_LOW_SPEED;
298b083c 90 else if (phy->flags & ULPI_FC_FS4LS)
13dd0c97
IG
91 flags |= ULPI_FUNC_CTRL_FS4LS;
92 else
93 flags |= ULPI_FUNC_CTRL_FULL_SPEED;
94
298b083c 95 if (phy->flags & ULPI_FC_TERMSEL)
13dd0c97
IG
96 flags |= ULPI_FUNC_CTRL_TERMSELECT;
97
98 /*
99 * ULPI Specification rev.1.1 default
100 * for OpMode is Normal Operation.
101 */
298b083c 102 if (phy->flags & ULPI_FC_OP_NODRV)
13dd0c97 103 flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
298b083c 104 else if (phy->flags & ULPI_FC_OP_DIS_NRZI)
13dd0c97 105 flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
298b083c 106 else if (phy->flags & ULPI_FC_OP_NSYNC_NEOP)
13dd0c97
IG
107 flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP;
108 else
109 flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
110
111 /*
112 * ULPI Specification rev.1.1 default
113 * for SuspendM is Powered.
114 */
115 flags |= ULPI_FUNC_CTRL_SUSPENDM;
116
298b083c 117 return usb_phy_io_write(phy, flags, ULPI_FUNC_CTRL);
13dd0c97
IG
118}
119
298b083c 120static int ulpi_set_ic_flags(struct usb_phy *phy)
13dd0c97
IG
121{
122 unsigned int flags = 0;
123
298b083c 124 if (phy->flags & ULPI_IC_AUTORESUME)
13dd0c97
IG
125 flags |= ULPI_IFC_CTRL_AUTORESUME;
126
298b083c 127 if (phy->flags & ULPI_IC_EXTVBUS_INDINV)
13dd0c97
IG
128 flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
129
298b083c 130 if (phy->flags & ULPI_IC_IND_PASSTHRU)
13dd0c97
IG
131 flags |= ULPI_IFC_CTRL_PASSTHRU;
132
298b083c 133 if (phy->flags & ULPI_IC_PROTECT_DIS)
13dd0c97
IG
134 flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
135
298b083c 136 return usb_phy_io_write(phy, flags, ULPI_IFC_CTRL);
13dd0c97
IG
137}
138
298b083c 139static int ulpi_set_flags(struct usb_phy *phy)
13dd0c97
IG
140{
141 int ret;
142
298b083c 143 ret = ulpi_set_otg_flags(phy);
13dd0c97
IG
144 if (ret)
145 return ret;
146
298b083c 147 ret = ulpi_set_ic_flags(phy);
13dd0c97
IG
148 if (ret)
149 return ret;
150
298b083c 151 return ulpi_set_fc_flags(phy);
13dd0c97
IG
152}
153
298b083c 154static int ulpi_check_integrity(struct usb_phy *phy)
a9138192
IG
155{
156 int ret, i;
157 unsigned int val = 0x55;
158
159 for (i = 0; i < 2; i++) {
298b083c 160 ret = usb_phy_io_write(phy, val, ULPI_SCRATCH);
a9138192
IG
161 if (ret < 0)
162 return ret;
163
298b083c 164 ret = usb_phy_io_read(phy, ULPI_SCRATCH);
a9138192
IG
165 if (ret < 0)
166 return ret;
167
168 if (ret != val) {
169 pr_err("ULPI integrity check: failed!");
170 return -ENODEV;
171 }
172 val = val << 1;
173 }
174
175 pr_info("ULPI integrity check: passed.\n");
176
177 return 0;
178}
179
298b083c 180static int ulpi_init(struct usb_phy *phy)
2d57a95f 181{
7b4a0367
WS
182 int i, vid, pid, ret;
183 u32 ulpi_id = 0;
2d57a95f 184
7b4a0367 185 for (i = 0; i < 4; i++) {
298b083c 186 ret = usb_phy_io_read(phy, ULPI_PRODUCT_ID_HIGH - i);
7b4a0367
WS
187 if (ret < 0)
188 return ret;
189 ulpi_id = (ulpi_id << 8) | ret;
190 }
191 vid = ulpi_id & 0xffff;
192 pid = ulpi_id >> 16;
2d57a95f
DM
193
194 pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
195
96b9e832
IG
196 for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) {
197 if (ulpi_ids[i].id == ULPI_ID(vid, pid)) {
198 pr_info("Found %s ULPI transceiver.\n",
199 ulpi_ids[i].name);
a9138192 200 break;
96b9e832
IG
201 }
202 }
a9138192 203
298b083c 204 ret = ulpi_check_integrity(phy);
a9138192
IG
205 if (ret)
206 return ret;
2d57a95f 207
298b083c 208 return ulpi_set_flags(phy);
2d57a95f
DM
209}
210
298b083c 211static int ulpi_set_host(struct usb_otg *otg, struct usb_bus *host)
13dd0c97 212{
298b083c
HK
213 struct usb_phy *phy = otg->phy;
214 unsigned int flags = usb_phy_io_read(phy, ULPI_IFC_CTRL);
13dd0c97
IG
215
216 if (!host) {
217 otg->host = NULL;
218 return 0;
219 }
220
221 otg->host = host;
222
223 flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
224 ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
225 ULPI_IFC_CTRL_CARKITMODE);
226
298b083c 227 if (phy->flags & ULPI_IC_6PIN_SERIAL)
13dd0c97 228 flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
298b083c 229 else if (phy->flags & ULPI_IC_3PIN_SERIAL)
13dd0c97 230 flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
298b083c 231 else if (phy->flags & ULPI_IC_CARKIT)
13dd0c97
IG
232 flags |= ULPI_IFC_CTRL_CARKITMODE;
233
298b083c 234 return usb_phy_io_write(phy, flags, ULPI_IFC_CTRL);
13dd0c97
IG
235}
236
298b083c 237static int ulpi_set_vbus(struct usb_otg *otg, bool on)
2d57a95f 238{
298b083c
HK
239 struct usb_phy *phy = otg->phy;
240 unsigned int flags = usb_phy_io_read(phy, ULPI_OTG_CTRL);
2d57a95f 241
fc567f06 242 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
2d57a95f
DM
243
244 if (on) {
298b083c 245 if (phy->flags & ULPI_OTG_DRVVBUS)
fc567f06 246 flags |= ULPI_OTG_CTRL_DRVVBUS;
2d57a95f 247
298b083c 248 if (phy->flags & ULPI_OTG_DRVVBUS_EXT)
fc567f06 249 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
2d57a95f
DM
250 }
251
298b083c 252 return usb_phy_io_write(phy, flags, ULPI_OTG_CTRL);
2d57a95f
DM
253}
254
86753811 255struct usb_phy *
298b083c 256otg_ulpi_create(struct usb_phy_io_ops *ops,
2d57a95f
DM
257 unsigned int flags)
258{
298b083c
HK
259 struct usb_phy *phy;
260 struct usb_otg *otg;
261
262 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
263 if (!phy)
264 return NULL;
2d57a95f
DM
265
266 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
298b083c
HK
267 if (!otg) {
268 kfree(phy);
2d57a95f 269 return NULL;
298b083c
HK
270 }
271
272 phy->label = "ULPI";
273 phy->flags = flags;
274 phy->io_ops = ops;
275 phy->otg = otg;
276 phy->init = ulpi_init;
2d57a95f 277
298b083c 278 otg->phy = phy;
13dd0c97 279 otg->set_host = ulpi_set_host;
2d57a95f
DM
280 otg->set_vbus = ulpi_set_vbus;
281
298b083c 282 return phy;
2d57a95f
DM
283}
284EXPORT_SYMBOL_GPL(otg_ulpi_create);
285