ulpi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <linux/kernel.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/otg.h>
  28. #include <linux/usb/ulpi.h>
  29. /* ULPI register addresses */
  30. #define ULPI_VID_LOW 0x00 /* Vendor ID low */
  31. #define ULPI_VID_HIGH 0x01 /* Vendor ID high */
  32. #define ULPI_PID_LOW 0x02 /* Product ID low */
  33. #define ULPI_PID_HIGH 0x03 /* Product ID high */
  34. #define ULPI_ITFCTL 0x07 /* Interface Control */
  35. #define ULPI_OTGCTL 0x0A /* OTG Control */
  36. /* add to above register address to access Set/Clear functions */
  37. #define ULPI_REG_SET 0x01
  38. #define ULPI_REG_CLEAR 0x02
  39. /* ULPI OTG Control Register bits */
  40. #define ID_PULL_UP (1 << 0) /* enable ID Pull Up */
  41. #define DP_PULL_DOWN (1 << 1) /* enable DP Pull Down */
  42. #define DM_PULL_DOWN (1 << 2) /* enable DM Pull Down */
  43. #define DISCHRG_VBUS (1 << 3) /* Discharge Vbus */
  44. #define CHRG_VBUS (1 << 4) /* Charge Vbus */
  45. #define DRV_VBUS (1 << 5) /* Drive Vbus */
  46. #define DRV_VBUS_EXT (1 << 6) /* Drive Vbus external */
  47. #define USE_EXT_VBUS_IND (1 << 7) /* Use ext. Vbus indicator */
  48. #define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
  49. #define TR_FLAG(flags, a, b) (((flags) & a) ? b : 0)
  50. /* ULPI hardcoded IDs, used for probing */
  51. static unsigned int ulpi_ids[] = {
  52. ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */
  53. };
  54. static int ulpi_set_flags(struct otg_transceiver *otg)
  55. {
  56. unsigned int flags = 0;
  57. if (otg->flags & USB_OTG_PULLUP_ID)
  58. flags |= ID_PULL_UP;
  59. if (otg->flags & USB_OTG_PULLDOWN_DM)
  60. flags |= DM_PULL_DOWN;
  61. if (otg->flags & USB_OTG_PULLDOWN_DP)
  62. flags |= DP_PULL_DOWN;
  63. if (otg->flags & USB_OTG_EXT_VBUS_INDICATOR)
  64. flags |= USE_EXT_VBUS_IND;
  65. return otg_io_write(otg, flags, ULPI_OTGCTL + ULPI_REG_SET);
  66. }
  67. static int ulpi_init(struct otg_transceiver *otg)
  68. {
  69. int i, vid, pid;
  70. vid = (otg_io_read(otg, ULPI_VID_HIGH) << 8) |
  71. otg_io_read(otg, ULPI_VID_LOW);
  72. pid = (otg_io_read(otg, ULPI_PID_HIGH) << 8) |
  73. otg_io_read(otg, ULPI_PID_LOW);
  74. pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
  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. pr_err("ULPI ID does not match any known transceiver.\n");
  79. return -ENODEV;
  80. }
  81. static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
  82. {
  83. unsigned int flags = otg_io_read(otg, ULPI_OTGCTL);
  84. flags &= ~(DRV_VBUS | DRV_VBUS_EXT);
  85. if (on) {
  86. if (otg->flags & USB_OTG_DRV_VBUS)
  87. flags |= DRV_VBUS;
  88. if (otg->flags & USB_OTG_DRV_VBUS_EXT)
  89. flags |= DRV_VBUS_EXT;
  90. }
  91. return otg_io_write(otg, flags, ULPI_OTGCTL + ULPI_REG_SET);
  92. }
  93. struct otg_transceiver *
  94. otg_ulpi_create(struct otg_io_access_ops *ops,
  95. unsigned int flags)
  96. {
  97. struct otg_transceiver *otg;
  98. otg = kzalloc(sizeof(*otg), GFP_KERNEL);
  99. if (!otg)
  100. return NULL;
  101. otg->label = "ULPI";
  102. otg->flags = flags;
  103. otg->io_ops = ops;
  104. otg->init = ulpi_init;
  105. otg->set_vbus = ulpi_set_vbus;
  106. return otg;
  107. }
  108. EXPORT_SYMBOL_GPL(otg_ulpi_create);