phy-isp1301.c 3.9 KB

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