2 * Generic ULPI USB transceiver support
4 * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
6 * Based on sources from
8 * Sascha Hauer <s.hauer@pengutronix.de>
9 * Freescale Semiconductors
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.
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.
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.
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/usb/otg.h>
30 #include <linux/usb/ulpi.h>
32 #define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
34 /* ULPI hardcoded IDs, used for probing */
35 static unsigned int ulpi_ids[] = {
36 ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */
37 ULPI_ID(0x0424, 0x0006), /* SMSC USB3319 */
40 static int ulpi_set_otg_flags(struct otg_transceiver *otg)
42 unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
43 ULPI_OTG_CTRL_DM_PULLDOWN;
45 if (otg->flags & ULPI_OTG_ID_PULLUP)
46 flags |= ULPI_OTG_CTRL_ID_PULLUP;
49 * ULPI Specification rev.1.1 default
50 * for Dp/DmPulldown is enabled.
52 if (otg->flags & ULPI_OTG_DP_PULLDOWN_DIS)
53 flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
55 if (otg->flags & ULPI_OTG_DM_PULLDOWN_DIS)
56 flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
58 if (otg->flags & ULPI_OTG_EXTVBUSIND)
59 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
61 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
64 static int ulpi_set_fc_flags(struct otg_transceiver *otg)
66 unsigned int flags = 0;
69 * ULPI Specification rev.1.1 default
70 * for XcvrSelect is Full Speed.
72 if (otg->flags & ULPI_FC_HS)
73 flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
74 else if (otg->flags & ULPI_FC_LS)
75 flags |= ULPI_FUNC_CTRL_LOW_SPEED;
76 else if (otg->flags & ULPI_FC_FS4LS)
77 flags |= ULPI_FUNC_CTRL_FS4LS;
79 flags |= ULPI_FUNC_CTRL_FULL_SPEED;
81 if (otg->flags & ULPI_FC_TERMSEL)
82 flags |= ULPI_FUNC_CTRL_TERMSELECT;
85 * ULPI Specification rev.1.1 default
86 * for OpMode is Normal Operation.
88 if (otg->flags & ULPI_FC_OP_NODRV)
89 flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
90 else if (otg->flags & ULPI_FC_OP_DIS_NRZI)
91 flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
92 else if (otg->flags & ULPI_FC_OP_NSYNC_NEOP)
93 flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP;
95 flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
98 * ULPI Specification rev.1.1 default
99 * for SuspendM is Powered.
101 flags |= ULPI_FUNC_CTRL_SUSPENDM;
103 return otg_io_write(otg, flags, ULPI_FUNC_CTRL);
106 static int ulpi_set_ic_flags(struct otg_transceiver *otg)
108 unsigned int flags = 0;
110 if (otg->flags & ULPI_IC_AUTORESUME)
111 flags |= ULPI_IFC_CTRL_AUTORESUME;
113 if (otg->flags & ULPI_IC_EXTVBUS_INDINV)
114 flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
116 if (otg->flags & ULPI_IC_IND_PASSTHRU)
117 flags |= ULPI_IFC_CTRL_PASSTHRU;
119 if (otg->flags & ULPI_IC_PROTECT_DIS)
120 flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
122 return otg_io_write(otg, flags, ULPI_IFC_CTRL);
125 static int ulpi_set_flags(struct otg_transceiver *otg)
129 ret = ulpi_set_otg_flags(otg);
133 ret = ulpi_set_ic_flags(otg);
137 return ulpi_set_fc_flags(otg);
140 static int ulpi_init(struct otg_transceiver *otg)
142 int i, vid, pid, ret;
145 for (i = 0; i < 4; i++) {
146 ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i);
149 ulpi_id = (ulpi_id << 8) | ret;
151 vid = ulpi_id & 0xffff;
154 pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
156 for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++)
157 if (ulpi_ids[i] == ULPI_ID(vid, pid))
158 return ulpi_set_flags(otg);
160 pr_err("ULPI ID does not match any known transceiver.\n");
164 static int ulpi_set_host(struct otg_transceiver *otg, struct usb_bus *host)
166 unsigned int flags = otg_io_read(otg, ULPI_IFC_CTRL);
175 flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
176 ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
177 ULPI_IFC_CTRL_CARKITMODE);
179 if (otg->flags & ULPI_IC_6PIN_SERIAL)
180 flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
181 else if (otg->flags & ULPI_IC_3PIN_SERIAL)
182 flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
183 else if (otg->flags & ULPI_IC_CARKIT)
184 flags |= ULPI_IFC_CTRL_CARKITMODE;
186 return otg_io_write(otg, flags, ULPI_IFC_CTRL);
189 static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
191 unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL);
193 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
196 if (otg->flags & ULPI_OTG_DRVVBUS)
197 flags |= ULPI_OTG_CTRL_DRVVBUS;
199 if (otg->flags & ULPI_OTG_DRVVBUS_EXT)
200 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
203 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
206 struct otg_transceiver *
207 otg_ulpi_create(struct otg_io_access_ops *ops,
210 struct otg_transceiver *otg;
212 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
219 otg->init = ulpi_init;
220 otg->set_host = ulpi_set_host;
221 otg->set_vbus = ulpi_set_vbus;
225 EXPORT_SYMBOL_GPL(otg_ulpi_create);