usb4604.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for SMSC USB4604 USB HSIC 4-port 2.0 hub controller driver
  4. * Based on usb3503 driver
  5. *
  6. * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
  7. * Copyright (c) 2016 Linaro Ltd.
  8. */
  9. #include <linux/i2c.h>
  10. #include <linux/delay.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/gpio/consumer.h>
  14. enum usb4604_mode {
  15. USB4604_MODE_UNKNOWN,
  16. USB4604_MODE_HUB,
  17. USB4604_MODE_STANDBY,
  18. };
  19. struct usb4604 {
  20. enum usb4604_mode mode;
  21. struct device *dev;
  22. struct gpio_desc *gpio_reset;
  23. };
  24. static void usb4604_reset(struct usb4604 *hub, int state)
  25. {
  26. gpiod_set_value_cansleep(hub->gpio_reset, state);
  27. /* Wait for i2c logic to come up */
  28. if (state)
  29. msleep(250);
  30. }
  31. static int usb4604_connect(struct usb4604 *hub)
  32. {
  33. struct device *dev = hub->dev;
  34. struct i2c_client *client = to_i2c_client(dev);
  35. int err;
  36. u8 connect_cmd[] = { 0xaa, 0x55, 0x00 };
  37. usb4604_reset(hub, 1);
  38. err = i2c_master_send(client, connect_cmd, ARRAY_SIZE(connect_cmd));
  39. if (err < 0) {
  40. usb4604_reset(hub, 0);
  41. return err;
  42. }
  43. hub->mode = USB4604_MODE_HUB;
  44. dev_dbg(dev, "switched to HUB mode\n");
  45. return 0;
  46. }
  47. static int usb4604_switch_mode(struct usb4604 *hub, enum usb4604_mode mode)
  48. {
  49. struct device *dev = hub->dev;
  50. int err = 0;
  51. switch (mode) {
  52. case USB4604_MODE_HUB:
  53. err = usb4604_connect(hub);
  54. break;
  55. case USB4604_MODE_STANDBY:
  56. usb4604_reset(hub, 0);
  57. dev_dbg(dev, "switched to STANDBY mode\n");
  58. break;
  59. default:
  60. dev_err(dev, "unknown mode is requested\n");
  61. err = -EINVAL;
  62. break;
  63. }
  64. return err;
  65. }
  66. static int usb4604_probe(struct usb4604 *hub)
  67. {
  68. struct device *dev = hub->dev;
  69. struct device_node *np = dev->of_node;
  70. struct gpio_desc *gpio;
  71. u32 mode = USB4604_MODE_HUB;
  72. gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  73. if (IS_ERR(gpio))
  74. return PTR_ERR(gpio);
  75. hub->gpio_reset = gpio;
  76. if (of_property_read_u32(np, "initial-mode", &hub->mode))
  77. hub->mode = mode;
  78. return usb4604_switch_mode(hub, hub->mode);
  79. }
  80. static int usb4604_i2c_probe(struct i2c_client *i2c,
  81. const struct i2c_device_id *id)
  82. {
  83. struct usb4604 *hub;
  84. hub = devm_kzalloc(&i2c->dev, sizeof(*hub), GFP_KERNEL);
  85. if (!hub)
  86. return -ENOMEM;
  87. i2c_set_clientdata(i2c, hub);
  88. hub->dev = &i2c->dev;
  89. return usb4604_probe(hub);
  90. }
  91. #ifdef CONFIG_PM_SLEEP
  92. static int usb4604_i2c_suspend(struct device *dev)
  93. {
  94. struct i2c_client *client = to_i2c_client(dev);
  95. struct usb4604 *hub = i2c_get_clientdata(client);
  96. usb4604_switch_mode(hub, USB4604_MODE_STANDBY);
  97. return 0;
  98. }
  99. static int usb4604_i2c_resume(struct device *dev)
  100. {
  101. struct i2c_client *client = to_i2c_client(dev);
  102. struct usb4604 *hub = i2c_get_clientdata(client);
  103. usb4604_switch_mode(hub, hub->mode);
  104. return 0;
  105. }
  106. #endif
  107. static SIMPLE_DEV_PM_OPS(usb4604_i2c_pm_ops, usb4604_i2c_suspend,
  108. usb4604_i2c_resume);
  109. static const struct i2c_device_id usb4604_id[] = {
  110. { "usb4604", 0 },
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(i2c, usb4604_id);
  114. #ifdef CONFIG_OF
  115. static const struct of_device_id usb4604_of_match[] = {
  116. { .compatible = "smsc,usb4604" },
  117. {}
  118. };
  119. MODULE_DEVICE_TABLE(of, usb4604_of_match);
  120. #endif
  121. static struct i2c_driver usb4604_i2c_driver = {
  122. .driver = {
  123. .name = "usb4604",
  124. .pm = &usb4604_i2c_pm_ops,
  125. .of_match_table = of_match_ptr(usb4604_of_match),
  126. },
  127. .probe = usb4604_i2c_probe,
  128. .id_table = usb4604_id,
  129. };
  130. module_i2c_driver(usb4604_i2c_driver);
  131. MODULE_DESCRIPTION("USB4604 USB HUB driver");
  132. MODULE_LICENSE("GPL v2");