phy-isp1301.c 4.2 KB

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