ulpi.c 3.0 KB

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