phy-isp1301.c 4.0 KB

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