blob: ef7dbe40f1114fa991f40ed9f1a7c27b2d9b29b6 [file] [log] [blame]
Daniel Mack2d57a952009-10-15 17:09:35 +03001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Daniel Mack2d57a952009-10-15 17:09:35 +030028#include <linux/usb.h>
29#include <linux/usb/otg.h>
30#include <linux/usb/ulpi.h>
31
Daniel Mack2d57a952009-10-15 17:09:35 +030032#define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
33
Daniel Mack2d57a952009-10-15 17:09:35 +030034/* ULPI hardcoded IDs, used for probing */
35static unsigned int ulpi_ids[] = {
36 ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */
Igor Grinberg51a91a52010-07-15 16:00:15 +030037 ULPI_ID(0x0424, 0x0006), /* SMSC USB3319 */
Daniel Mack2d57a952009-10-15 17:09:35 +030038};
39
40static int ulpi_set_flags(struct otg_transceiver *otg)
41{
42 unsigned int flags = 0;
43
44 if (otg->flags & USB_OTG_PULLUP_ID)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030045 flags |= ULPI_OTG_CTRL_ID_PULLUP;
Daniel Mack2d57a952009-10-15 17:09:35 +030046
47 if (otg->flags & USB_OTG_PULLDOWN_DM)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030048 flags |= ULPI_OTG_CTRL_DM_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030049
50 if (otg->flags & USB_OTG_PULLDOWN_DP)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030051 flags |= ULPI_OTG_CTRL_DP_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030052
53 if (otg->flags & USB_OTG_EXT_VBUS_INDICATOR)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030054 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
Daniel Mack2d57a952009-10-15 17:09:35 +030055
Eric Bénardfa345d02010-07-15 09:20:19 +020056 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +030057}
58
59static int ulpi_init(struct otg_transceiver *otg)
60{
Wolfram Sang7b4a0362010-06-15 12:34:22 +020061 int i, vid, pid, ret;
62 u32 ulpi_id = 0;
Daniel Mack2d57a952009-10-15 17:09:35 +030063
Wolfram Sang7b4a0362010-06-15 12:34:22 +020064 for (i = 0; i < 4; i++) {
65 ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i);
66 if (ret < 0)
67 return ret;
68 ulpi_id = (ulpi_id << 8) | ret;
69 }
70 vid = ulpi_id & 0xffff;
71 pid = ulpi_id >> 16;
Daniel Mack2d57a952009-10-15 17:09:35 +030072
73 pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
74
75 for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++)
76 if (ulpi_ids[i] == ULPI_ID(vid, pid))
77 return ulpi_set_flags(otg);
78
79 pr_err("ULPI ID does not match any known transceiver.\n");
80 return -ENODEV;
81}
82
83static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
84{
Heikki Krogerusfc567f02010-05-03 09:13:02 +030085 unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +030086
Heikki Krogerusfc567f02010-05-03 09:13:02 +030087 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
Daniel Mack2d57a952009-10-15 17:09:35 +030088
89 if (on) {
90 if (otg->flags & USB_OTG_DRV_VBUS)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030091 flags |= ULPI_OTG_CTRL_DRVVBUS;
Daniel Mack2d57a952009-10-15 17:09:35 +030092
93 if (otg->flags & USB_OTG_DRV_VBUS_EXT)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030094 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
Daniel Mack2d57a952009-10-15 17:09:35 +030095 }
96
Eric Bénardfa345d02010-07-15 09:20:19 +020097 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +030098}
99
100struct otg_transceiver *
101otg_ulpi_create(struct otg_io_access_ops *ops,
102 unsigned int flags)
103{
104 struct otg_transceiver *otg;
105
106 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
107 if (!otg)
108 return NULL;
109
110 otg->label = "ULPI";
111 otg->flags = flags;
112 otg->io_ops = ops;
113 otg->init = ulpi_init;
114 otg->set_vbus = ulpi_set_vbus;
115
116 return otg;
117}
118EXPORT_SYMBOL_GPL(otg_ulpi_create);
119