OSDN Git Service

448d643e72723b85fcdacf6ed6802738ee810e38
[android-x86/kernel.git] / drivers / usb / otg / ulpi.c
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>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/usb/otg.h>
30 #include <linux/usb/ulpi.h>
31
32 #define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
33
34 /* ULPI hardcoded IDs, used for probing */
35 static unsigned int ulpi_ids[] = {
36         ULPI_ID(0x04cc, 0x1504),        /* NXP ISP1504 */
37 };
38
39 static int ulpi_set_flags(struct otg_transceiver *otg)
40 {
41         unsigned int flags = 0;
42
43         if (otg->flags & USB_OTG_PULLUP_ID)
44                 flags |= ULPI_OTG_CTRL_ID_PULLUP;
45
46         if (otg->flags & USB_OTG_PULLDOWN_DM)
47                 flags |= ULPI_OTG_CTRL_DM_PULLDOWN;
48
49         if (otg->flags & USB_OTG_PULLDOWN_DP)
50                 flags |= ULPI_OTG_CTRL_DP_PULLDOWN;
51
52         if (otg->flags & USB_OTG_EXT_VBUS_INDICATOR)
53                 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
54
55         return otg_io_write(otg, flags, ULPI_OTG_CTRL);
56 }
57
58 static int ulpi_init(struct otg_transceiver *otg)
59 {
60         int i, vid, pid, ret;
61         u32 ulpi_id = 0;
62
63         for (i = 0; i < 4; i++) {
64                 ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i);
65                 if (ret < 0)
66                         return ret;
67                 ulpi_id = (ulpi_id << 8) | ret;
68         }
69         vid = ulpi_id & 0xffff;
70         pid = ulpi_id >> 16;
71
72         pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
73
74         for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++)
75                 if (ulpi_ids[i] == ULPI_ID(vid, pid))
76                         return ulpi_set_flags(otg);
77
78         pr_err("ULPI ID does not match any known transceiver.\n");
79         return -ENODEV;
80 }
81
82 static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
83 {
84         unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL);
85
86         flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
87
88         if (on) {
89                 if (otg->flags & USB_OTG_DRV_VBUS)
90                         flags |= ULPI_OTG_CTRL_DRVVBUS;
91
92                 if (otg->flags & USB_OTG_DRV_VBUS_EXT)
93                         flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
94         }
95
96         return otg_io_write(otg, flags, ULPI_OTG_CTRL);
97 }
98
99 struct otg_transceiver *
100 otg_ulpi_create(struct otg_io_access_ops *ops,
101                 unsigned int flags)
102 {
103         struct otg_transceiver *otg;
104
105         otg = kzalloc(sizeof(*otg), GFP_KERNEL);
106         if (!otg)
107                 return NULL;
108
109         otg->label      = "ULPI";
110         otg->flags      = flags;
111         otg->io_ops     = ops;
112         otg->init       = ulpi_init;
113         otg->set_vbus   = ulpi_set_vbus;
114
115         return otg;
116 }
117 EXPORT_SYMBOL_GPL(otg_ulpi_create);
118