phy-isp1301.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NXP ISP1301 USB transceiver driver
  4. *
  5. * Copyright (C) 2012 Roland Stigge
  6. *
  7. * Author: Roland Stigge <stigge@antcom.de>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/i2c.h>
  12. #include <linux/usb/phy.h>
  13. #include <linux/usb/isp1301.h>
  14. #define DRV_NAME "isp1301"
  15. struct isp1301 {
  16. struct usb_phy phy;
  17. struct mutex mutex;
  18. struct i2c_client *client;
  19. };
  20. #define phy_to_isp(p) (container_of((p), struct isp1301, phy))
  21. static const struct i2c_device_id isp1301_id[] = {
  22. { "isp1301", 0 },
  23. { }
  24. };
  25. MODULE_DEVICE_TABLE(i2c, isp1301_id);
  26. static const struct of_device_id isp1301_of_match[] = {
  27. {.compatible = "nxp,isp1301" },
  28. { },
  29. };
  30. MODULE_DEVICE_TABLE(of, isp1301_of_match);
  31. static struct i2c_client *isp1301_i2c_client;
  32. static int __isp1301_write(struct isp1301 *isp, u8 reg, u8 value, u8 clear)
  33. {
  34. return i2c_smbus_write_byte_data(isp->client, reg | clear, value);
  35. }
  36. static int isp1301_write(struct isp1301 *isp, u8 reg, u8 value)
  37. {
  38. return __isp1301_write(isp, reg, value, 0);
  39. }
  40. static int isp1301_clear(struct isp1301 *isp, u8 reg, u8 value)
  41. {
  42. return __isp1301_write(isp, reg, value, ISP1301_I2C_REG_CLEAR_ADDR);
  43. }
  44. static int isp1301_phy_init(struct usb_phy *phy)
  45. {
  46. struct isp1301 *isp = phy_to_isp(phy);
  47. /* Disable transparent UART mode first */
  48. isp1301_clear(isp, ISP1301_I2C_MODE_CONTROL_1, MC1_UART_EN);
  49. isp1301_clear(isp, ISP1301_I2C_MODE_CONTROL_1, ~MC1_SPEED_REG);
  50. isp1301_write(isp, ISP1301_I2C_MODE_CONTROL_1, MC1_SPEED_REG);
  51. isp1301_clear(isp, ISP1301_I2C_MODE_CONTROL_2, ~0);
  52. isp1301_write(isp, ISP1301_I2C_MODE_CONTROL_2, (MC2_BI_DI | MC2_PSW_EN
  53. | MC2_SPD_SUSP_CTRL));
  54. isp1301_clear(isp, ISP1301_I2C_OTG_CONTROL_1, ~0);
  55. isp1301_write(isp, ISP1301_I2C_MODE_CONTROL_1, MC1_DAT_SE0);
  56. isp1301_write(isp, ISP1301_I2C_OTG_CONTROL_1, (OTG1_DM_PULLDOWN
  57. | OTG1_DP_PULLDOWN));
  58. isp1301_clear(isp, ISP1301_I2C_OTG_CONTROL_1, (OTG1_DM_PULLUP
  59. | OTG1_DP_PULLUP));
  60. /* mask all interrupts */
  61. isp1301_clear(isp, ISP1301_I2C_INTERRUPT_LATCH, ~0);
  62. isp1301_clear(isp, ISP1301_I2C_INTERRUPT_FALLING, ~0);
  63. isp1301_clear(isp, ISP1301_I2C_INTERRUPT_RISING, ~0);
  64. return 0;
  65. }
  66. static int isp1301_phy_set_vbus(struct usb_phy *phy, int on)
  67. {
  68. struct isp1301 *isp = phy_to_isp(phy);
  69. if (on)
  70. isp1301_write(isp, ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
  71. else
  72. isp1301_clear(isp, ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
  73. return 0;
  74. }
  75. static int isp1301_probe(struct i2c_client *client,
  76. const struct i2c_device_id *i2c_id)
  77. {
  78. struct isp1301 *isp;
  79. struct usb_phy *phy;
  80. isp = devm_kzalloc(&client->dev, sizeof(*isp), GFP_KERNEL);
  81. if (!isp)
  82. return -ENOMEM;
  83. isp->client = client;
  84. mutex_init(&isp->mutex);
  85. phy = &isp->phy;
  86. phy->dev = &client->dev;
  87. phy->label = DRV_NAME;
  88. phy->init = isp1301_phy_init;
  89. phy->set_vbus = isp1301_phy_set_vbus;
  90. phy->type = USB_PHY_TYPE_USB2;
  91. i2c_set_clientdata(client, isp);
  92. usb_add_phy_dev(phy);
  93. isp1301_i2c_client = client;
  94. return 0;
  95. }
  96. static int isp1301_remove(struct i2c_client *client)
  97. {
  98. struct isp1301 *isp = i2c_get_clientdata(client);
  99. usb_remove_phy(&isp->phy);
  100. isp1301_i2c_client = NULL;
  101. return 0;
  102. }
  103. static struct i2c_driver isp1301_driver = {
  104. .driver = {
  105. .name = DRV_NAME,
  106. .of_match_table = isp1301_of_match,
  107. },
  108. .probe = isp1301_probe,
  109. .remove = isp1301_remove,
  110. .id_table = isp1301_id,
  111. };
  112. module_i2c_driver(isp1301_driver);
  113. static int match(struct device *dev, void *data)
  114. {
  115. struct device_node *node = (struct device_node *)data;
  116. return (dev->of_node == node) &&
  117. (dev->driver == &isp1301_driver.driver);
  118. }
  119. struct i2c_client *isp1301_get_client(struct device_node *node)
  120. {
  121. if (node) { /* reference of ISP1301 I2C node via DT */
  122. struct device *dev = bus_find_device(&i2c_bus_type, NULL,
  123. node, match);
  124. if (!dev)
  125. return NULL;
  126. return to_i2c_client(dev);
  127. } else { /* non-DT: only one ISP1301 chip supported */
  128. return isp1301_i2c_client;
  129. }
  130. }
  131. EXPORT_SYMBOL_GPL(isp1301_get_client);
  132. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  133. MODULE_DESCRIPTION("NXP ISP1301 USB transceiver driver");
  134. MODULE_LICENSE("GPL");