phy-isp1301.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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->dev = &client->dev;
  84. phy->label = DRV_NAME;
  85. phy->init = isp1301_phy_init;
  86. phy->set_vbus = isp1301_phy_set_vbus;
  87. phy->type = USB_PHY_TYPE_USB2;
  88. i2c_set_clientdata(client, isp);
  89. usb_add_phy_dev(phy);
  90. isp1301_i2c_client = client;
  91. return 0;
  92. }
  93. static int isp1301_remove(struct i2c_client *client)
  94. {
  95. struct isp1301 *isp = i2c_get_clientdata(client);
  96. usb_remove_phy(&isp->phy);
  97. isp1301_i2c_client = NULL;
  98. return 0;
  99. }
  100. static struct i2c_driver isp1301_driver = {
  101. .driver = {
  102. .name = DRV_NAME,
  103. },
  104. .probe = isp1301_probe,
  105. .remove = isp1301_remove,
  106. .id_table = isp1301_id,
  107. };
  108. module_i2c_driver(isp1301_driver);
  109. static int match(struct device *dev, void *data)
  110. {
  111. struct device_node *node = (struct device_node *)data;
  112. return (dev->of_node == node) &&
  113. (dev->driver == &isp1301_driver.driver);
  114. }
  115. struct i2c_client *isp1301_get_client(struct device_node *node)
  116. {
  117. if (node) { /* reference of ISP1301 I2C node via DT */
  118. struct device *dev = bus_find_device(&i2c_bus_type, NULL,
  119. node, match);
  120. if (!dev)
  121. return NULL;
  122. return to_i2c_client(dev);
  123. } else { /* non-DT: only one ISP1301 chip supported */
  124. return isp1301_i2c_client;
  125. }
  126. }
  127. EXPORT_SYMBOL_GPL(isp1301_get_client);
  128. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  129. MODULE_DESCRIPTION("NXP ISP1301 USB transceiver driver");
  130. MODULE_LICENSE("GPL");