Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 28 | #include <linux/usb.h> |
| 29 | #include <linux/usb/otg.h> |
| 30 | #include <linux/usb/ulpi.h> |
| 31 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 32 | #define ULPI_ID(vendor, product) (((vendor) << 16) | (product)) |
| 33 | |
| 34 | #define TR_FLAG(flags, a, b) (((flags) & a) ? b : 0) |
| 35 | |
| 36 | /* ULPI hardcoded IDs, used for probing */ |
| 37 | static unsigned int ulpi_ids[] = { |
| 38 | ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */ |
| 39 | }; |
| 40 | |
| 41 | static int ulpi_set_flags(struct otg_transceiver *otg) |
| 42 | { |
| 43 | unsigned int flags = 0; |
| 44 | |
| 45 | if (otg->flags & USB_OTG_PULLUP_ID) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 46 | flags |= ULPI_OTG_CTRL_ID_PULLUP; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 47 | |
| 48 | if (otg->flags & USB_OTG_PULLDOWN_DM) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 49 | flags |= ULPI_OTG_CTRL_DM_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 50 | |
| 51 | if (otg->flags & USB_OTG_PULLDOWN_DP) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 52 | flags |= ULPI_OTG_CTRL_DP_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 53 | |
| 54 | if (otg->flags & USB_OTG_EXT_VBUS_INDICATOR) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 55 | flags |= ULPI_OTG_CTRL_EXTVBUSIND; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 56 | |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 57 | return otg_io_write(otg, flags, ULPI_SET(ULPI_OTG_CTRL)); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int ulpi_init(struct otg_transceiver *otg) |
| 61 | { |
| 62 | int i, vid, pid; |
| 63 | |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 64 | vid = (otg_io_read(otg, ULPI_VENDOR_ID_HIGH) << 8) | |
| 65 | otg_io_read(otg, ULPI_VENDOR_ID_LOW); |
| 66 | pid = (otg_io_read(otg, ULPI_PRODUCT_ID_HIGH) << 8) | |
| 67 | otg_io_read(otg, ULPI_PRODUCT_ID_LOW); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 68 | |
| 69 | pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid); |
| 70 | |
| 71 | for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) |
| 72 | if (ulpi_ids[i] == ULPI_ID(vid, pid)) |
| 73 | return ulpi_set_flags(otg); |
| 74 | |
| 75 | pr_err("ULPI ID does not match any known transceiver.\n"); |
| 76 | return -ENODEV; |
| 77 | } |
| 78 | |
| 79 | static int ulpi_set_vbus(struct otg_transceiver *otg, bool on) |
| 80 | { |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 81 | unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 82 | |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 83 | flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 84 | |
| 85 | if (on) { |
| 86 | if (otg->flags & USB_OTG_DRV_VBUS) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 87 | flags |= ULPI_OTG_CTRL_DRVVBUS; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 88 | |
| 89 | if (otg->flags & USB_OTG_DRV_VBUS_EXT) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 90 | flags |= ULPI_OTG_CTRL_DRVVBUS_EXT; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 91 | } |
| 92 | |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame^] | 93 | return otg_io_write(otg, flags, ULPI_SET(ULPI_OTG_CTRL)); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | struct otg_transceiver * |
| 97 | otg_ulpi_create(struct otg_io_access_ops *ops, |
| 98 | unsigned int flags) |
| 99 | { |
| 100 | struct otg_transceiver *otg; |
| 101 | |
| 102 | otg = kzalloc(sizeof(*otg), GFP_KERNEL); |
| 103 | if (!otg) |
| 104 | return NULL; |
| 105 | |
| 106 | otg->label = "ULPI"; |
| 107 | otg->flags = flags; |
| 108 | otg->io_ops = ops; |
| 109 | otg->init = ulpi_init; |
| 110 | otg->set_vbus = ulpi_set_vbus; |
| 111 | |
| 112 | return otg; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(otg_ulpi_create); |
| 115 | |