phy-tusb1210.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * tusb1210.c - TUSB1210 USB ULPI PHY driver
  3. *
  4. * Copyright (C) 2015 Intel Corporation
  5. *
  6. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/ulpi/driver.h>
  14. #include <linux/ulpi/regs.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/phy/ulpi_phy.h>
  17. #define TUSB1210_VENDOR_SPECIFIC2 0x80
  18. #define TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT 0
  19. #define TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT 4
  20. #define TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT 6
  21. struct tusb1210 {
  22. struct ulpi *ulpi;
  23. struct phy *phy;
  24. struct gpio_desc *gpio_reset;
  25. struct gpio_desc *gpio_cs;
  26. u8 vendor_specific2;
  27. };
  28. static int tusb1210_power_on(struct phy *phy)
  29. {
  30. struct tusb1210 *tusb = phy_get_drvdata(phy);
  31. gpiod_set_value_cansleep(tusb->gpio_reset, 1);
  32. gpiod_set_value_cansleep(tusb->gpio_cs, 1);
  33. /* Restore the optional eye diagram optimization value */
  34. if (tusb->vendor_specific2)
  35. ulpi_write(tusb->ulpi, TUSB1210_VENDOR_SPECIFIC2,
  36. tusb->vendor_specific2);
  37. return 0;
  38. }
  39. static int tusb1210_power_off(struct phy *phy)
  40. {
  41. struct tusb1210 *tusb = phy_get_drvdata(phy);
  42. gpiod_set_value_cansleep(tusb->gpio_reset, 0);
  43. gpiod_set_value_cansleep(tusb->gpio_cs, 0);
  44. return 0;
  45. }
  46. static int tusb1210_set_mode(struct phy *phy, enum phy_mode mode)
  47. {
  48. struct tusb1210 *tusb = phy_get_drvdata(phy);
  49. int ret;
  50. ret = ulpi_read(tusb->ulpi, ULPI_OTG_CTRL);
  51. if (ret < 0)
  52. return ret;
  53. switch (mode) {
  54. case PHY_MODE_USB_HOST:
  55. ret |= (ULPI_OTG_CTRL_DRVVBUS_EXT
  56. | ULPI_OTG_CTRL_ID_PULLUP
  57. | ULPI_OTG_CTRL_DP_PULLDOWN
  58. | ULPI_OTG_CTRL_DM_PULLDOWN);
  59. ulpi_write(tusb->ulpi, ULPI_OTG_CTRL, ret);
  60. ret |= ULPI_OTG_CTRL_DRVVBUS;
  61. break;
  62. case PHY_MODE_USB_DEVICE:
  63. ret &= ~(ULPI_OTG_CTRL_DRVVBUS
  64. | ULPI_OTG_CTRL_DP_PULLDOWN
  65. | ULPI_OTG_CTRL_DM_PULLDOWN);
  66. ulpi_write(tusb->ulpi, ULPI_OTG_CTRL, ret);
  67. ret &= ~ULPI_OTG_CTRL_DRVVBUS_EXT;
  68. break;
  69. default:
  70. /* nothing */
  71. return 0;
  72. }
  73. return ulpi_write(tusb->ulpi, ULPI_OTG_CTRL, ret);
  74. }
  75. static const struct phy_ops phy_ops = {
  76. .power_on = tusb1210_power_on,
  77. .power_off = tusb1210_power_off,
  78. .set_mode = tusb1210_set_mode,
  79. .owner = THIS_MODULE,
  80. };
  81. static int tusb1210_probe(struct ulpi *ulpi)
  82. {
  83. struct tusb1210 *tusb;
  84. u8 val, reg;
  85. tusb = devm_kzalloc(&ulpi->dev, sizeof(*tusb), GFP_KERNEL);
  86. if (!tusb)
  87. return -ENOMEM;
  88. tusb->gpio_reset = devm_gpiod_get_optional(&ulpi->dev, "reset",
  89. GPIOD_OUT_LOW);
  90. if (IS_ERR(tusb->gpio_reset))
  91. return PTR_ERR(tusb->gpio_reset);
  92. gpiod_set_value_cansleep(tusb->gpio_reset, 1);
  93. tusb->gpio_cs = devm_gpiod_get_optional(&ulpi->dev, "cs",
  94. GPIOD_OUT_LOW);
  95. if (IS_ERR(tusb->gpio_cs))
  96. return PTR_ERR(tusb->gpio_cs);
  97. gpiod_set_value_cansleep(tusb->gpio_cs, 1);
  98. /*
  99. * VENDOR_SPECIFIC2 register in TUSB1210 can be used for configuring eye
  100. * diagram optimization and DP/DM swap.
  101. */
  102. /* High speed output drive strength configuration */
  103. device_property_read_u8(&ulpi->dev, "ihstx", &val);
  104. reg = val << TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT;
  105. /* High speed output impedance configuration */
  106. device_property_read_u8(&ulpi->dev, "zhsdrv", &val);
  107. reg |= val << TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT;
  108. /* DP/DM swap control */
  109. device_property_read_u8(&ulpi->dev, "datapolarity", &val);
  110. reg |= val << TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT;
  111. if (reg) {
  112. ulpi_write(ulpi, TUSB1210_VENDOR_SPECIFIC2, reg);
  113. tusb->vendor_specific2 = reg;
  114. }
  115. tusb->phy = ulpi_phy_create(ulpi, &phy_ops);
  116. if (IS_ERR(tusb->phy))
  117. return PTR_ERR(tusb->phy);
  118. tusb->ulpi = ulpi;
  119. phy_set_drvdata(tusb->phy, tusb);
  120. ulpi_set_drvdata(ulpi, tusb);
  121. return 0;
  122. }
  123. static void tusb1210_remove(struct ulpi *ulpi)
  124. {
  125. struct tusb1210 *tusb = ulpi_get_drvdata(ulpi);
  126. ulpi_phy_destroy(ulpi, tusb->phy);
  127. }
  128. #define TI_VENDOR_ID 0x0451
  129. static const struct ulpi_device_id tusb1210_ulpi_id[] = {
  130. { TI_VENDOR_ID, 0x1507, }, /* TUSB1210 */
  131. { TI_VENDOR_ID, 0x1508, }, /* TUSB1211 */
  132. { },
  133. };
  134. MODULE_DEVICE_TABLE(ulpi, tusb1210_ulpi_id);
  135. static struct ulpi_driver tusb1210_driver = {
  136. .id_table = tusb1210_ulpi_id,
  137. .probe = tusb1210_probe,
  138. .remove = tusb1210_remove,
  139. .driver = {
  140. .name = "tusb1210",
  141. .owner = THIS_MODULE,
  142. },
  143. };
  144. module_ulpi_driver(tusb1210_driver);
  145. MODULE_AUTHOR("Intel Corporation");
  146. MODULE_LICENSE("GPL v2");
  147. MODULE_DESCRIPTION("TUSB1210 ULPI PHY driver");