usb3503.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Driver for SMSC USB3503 USB 2.0 hub controller driver
  3. *
  4. * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/i2c.h>
  21. #include <linux/gpio.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/platform_data/usb3503.h>
  28. #define USB3503_VIDL 0x00
  29. #define USB3503_VIDM 0x01
  30. #define USB3503_PIDL 0x02
  31. #define USB3503_PIDM 0x03
  32. #define USB3503_DIDL 0x04
  33. #define USB3503_DIDM 0x05
  34. #define USB3503_CFG1 0x06
  35. #define USB3503_SELF_BUS_PWR (1 << 7)
  36. #define USB3503_CFG2 0x07
  37. #define USB3503_CFG3 0x08
  38. #define USB3503_NRD 0x09
  39. #define USB3503_PDS 0x0a
  40. #define USB3503_PORT1 (1 << 1)
  41. #define USB3503_PORT2 (1 << 2)
  42. #define USB3503_PORT3 (1 << 3)
  43. #define USB3503_SP_ILOCK 0xe7
  44. #define USB3503_SPILOCK_CONNECT (1 << 1)
  45. #define USB3503_SPILOCK_CONFIG (1 << 0)
  46. #define USB3503_CFGP 0xee
  47. #define USB3503_CLKSUSP (1 << 7)
  48. struct usb3503 {
  49. enum usb3503_mode mode;
  50. struct i2c_client *client;
  51. int gpio_intn;
  52. int gpio_reset;
  53. int gpio_connect;
  54. };
  55. static int usb3503_write_register(struct i2c_client *client,
  56. char reg, char data)
  57. {
  58. return i2c_smbus_write_byte_data(client, reg, data);
  59. }
  60. static int usb3503_read_register(struct i2c_client *client, char reg)
  61. {
  62. return i2c_smbus_read_byte_data(client, reg);
  63. }
  64. static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
  65. {
  66. int err;
  67. err = usb3503_read_register(client, reg);
  68. if (err < 0)
  69. return err;
  70. err = usb3503_write_register(client, reg, err | req);
  71. if (err < 0)
  72. return err;
  73. return 0;
  74. }
  75. static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
  76. {
  77. int err;
  78. err = usb3503_read_register(client, reg);
  79. if (err < 0)
  80. return err;
  81. err = usb3503_write_register(client, reg, err & ~req);
  82. if (err < 0)
  83. return err;
  84. return 0;
  85. }
  86. static int usb3503_reset(int gpio_reset, int state)
  87. {
  88. if (gpio_is_valid(gpio_reset))
  89. gpio_set_value(gpio_reset, state);
  90. /* Wait RefClk when RESET_N is released, otherwise Hub will
  91. * not transition to Hub Communication Stage.
  92. */
  93. if (state)
  94. msleep(100);
  95. return 0;
  96. }
  97. static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
  98. {
  99. struct i2c_client *i2c = hub->client;
  100. int err = 0;
  101. switch (mode) {
  102. case USB3503_MODE_HUB:
  103. usb3503_reset(hub->gpio_reset, 1);
  104. /* SP_ILOCK: set connect_n, config_n for config */
  105. err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
  106. (USB3503_SPILOCK_CONNECT
  107. | USB3503_SPILOCK_CONFIG));
  108. if (err < 0) {
  109. dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
  110. goto err_hubmode;
  111. }
  112. /* PDS : Port2,3 Disable For Self Powered Operation */
  113. err = usb3503_set_bits(i2c, USB3503_PDS,
  114. (USB3503_PORT2 | USB3503_PORT3));
  115. if (err < 0) {
  116. dev_err(&i2c->dev, "PDS failed (%d)\n", err);
  117. goto err_hubmode;
  118. }
  119. /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
  120. err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
  121. if (err < 0) {
  122. dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
  123. goto err_hubmode;
  124. }
  125. /* SP_LOCK: clear connect_n, config_n for hub connect */
  126. err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
  127. (USB3503_SPILOCK_CONNECT
  128. | USB3503_SPILOCK_CONFIG));
  129. if (err < 0) {
  130. dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
  131. goto err_hubmode;
  132. }
  133. hub->mode = mode;
  134. dev_info(&i2c->dev, "switched to HUB mode\n");
  135. break;
  136. case USB3503_MODE_STANDBY:
  137. usb3503_reset(hub->gpio_reset, 0);
  138. hub->mode = mode;
  139. dev_info(&i2c->dev, "switched to STANDBY mode\n");
  140. break;
  141. default:
  142. dev_err(&i2c->dev, "unknown mode is request\n");
  143. err = -EINVAL;
  144. break;
  145. }
  146. err_hubmode:
  147. return err;
  148. }
  149. static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  150. {
  151. struct usb3503_platform_data *pdata = i2c->dev.platform_data;
  152. struct device_node *np = i2c->dev.of_node;
  153. struct usb3503 *hub;
  154. int err = -ENOMEM;
  155. u32 mode = USB3503_MODE_UNKNOWN;
  156. hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL);
  157. if (!hub) {
  158. dev_err(&i2c->dev, "private data alloc fail\n");
  159. return err;
  160. }
  161. i2c_set_clientdata(i2c, hub);
  162. hub->client = i2c;
  163. if (pdata) {
  164. hub->gpio_intn = pdata->gpio_intn;
  165. hub->gpio_connect = pdata->gpio_connect;
  166. hub->gpio_reset = pdata->gpio_reset;
  167. hub->mode = pdata->initial_mode;
  168. } else if (np) {
  169. hub->gpio_intn = of_get_named_gpio(np, "connect-gpios", 0);
  170. if (hub->gpio_intn == -EPROBE_DEFER)
  171. return -EPROBE_DEFER;
  172. hub->gpio_connect = of_get_named_gpio(np, "intn-gpios", 0);
  173. if (hub->gpio_connect == -EPROBE_DEFER)
  174. return -EPROBE_DEFER;
  175. hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
  176. if (hub->gpio_reset == -EPROBE_DEFER)
  177. return -EPROBE_DEFER;
  178. of_property_read_u32(np, "initial-mode", &mode);
  179. hub->mode = mode;
  180. }
  181. if (gpio_is_valid(hub->gpio_intn)) {
  182. err = gpio_request_one(hub->gpio_intn,
  183. GPIOF_OUT_INIT_HIGH, "usb3503 intn");
  184. if (err) {
  185. dev_err(&i2c->dev,
  186. "unable to request GPIO %d as connect pin (%d)\n",
  187. hub->gpio_intn, err);
  188. goto err_out;
  189. }
  190. }
  191. if (gpio_is_valid(hub->gpio_connect)) {
  192. err = gpio_request_one(hub->gpio_connect,
  193. GPIOF_OUT_INIT_HIGH, "usb3503 connect");
  194. if (err) {
  195. dev_err(&i2c->dev,
  196. "unable to request GPIO %d as connect pin (%d)\n",
  197. hub->gpio_connect, err);
  198. goto err_gpio_connect;
  199. }
  200. }
  201. if (gpio_is_valid(hub->gpio_reset)) {
  202. err = gpio_request_one(hub->gpio_reset,
  203. GPIOF_OUT_INIT_LOW, "usb3503 reset");
  204. if (err) {
  205. dev_err(&i2c->dev,
  206. "unable to request GPIO %d as reset pin (%d)\n",
  207. hub->gpio_reset, err);
  208. goto err_gpio_reset;
  209. }
  210. }
  211. usb3503_switch_mode(hub, hub->mode);
  212. dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
  213. (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
  214. return 0;
  215. err_gpio_reset:
  216. if (gpio_is_valid(hub->gpio_connect))
  217. gpio_free(hub->gpio_connect);
  218. err_gpio_connect:
  219. if (gpio_is_valid(hub->gpio_intn))
  220. gpio_free(hub->gpio_intn);
  221. err_out:
  222. kfree(hub);
  223. return err;
  224. }
  225. static int usb3503_remove(struct i2c_client *i2c)
  226. {
  227. struct usb3503 *hub = i2c_get_clientdata(i2c);
  228. if (gpio_is_valid(hub->gpio_intn))
  229. gpio_free(hub->gpio_intn);
  230. if (gpio_is_valid(hub->gpio_connect))
  231. gpio_free(hub->gpio_connect);
  232. if (gpio_is_valid(hub->gpio_reset))
  233. gpio_free(hub->gpio_reset);
  234. kfree(hub);
  235. return 0;
  236. }
  237. static const struct i2c_device_id usb3503_id[] = {
  238. { USB3503_I2C_NAME, 0 },
  239. { }
  240. };
  241. MODULE_DEVICE_TABLE(i2c, usb3503_id);
  242. #ifdef CONFIG_OF
  243. static const struct of_device_id usb3503_of_match[] = {
  244. { .compatible = "smsc,usb3503", },
  245. {},
  246. };
  247. MODULE_DEVICE_TABLE(of, usb3503_of_match);
  248. #endif
  249. static struct i2c_driver usb3503_driver = {
  250. .driver = {
  251. .name = USB3503_I2C_NAME,
  252. .of_match_table = of_match_ptr(usb3503_of_match),
  253. },
  254. .probe = usb3503_probe,
  255. .remove = usb3503_remove,
  256. .id_table = usb3503_id,
  257. };
  258. static int __init usb3503_init(void)
  259. {
  260. return i2c_add_driver(&usb3503_driver);
  261. }
  262. static void __exit usb3503_exit(void)
  263. {
  264. i2c_del_driver(&usb3503_driver);
  265. }
  266. module_init(usb3503_init);
  267. module_exit(usb3503_exit);
  268. MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
  269. MODULE_DESCRIPTION("USB3503 USB HUB driver");
  270. MODULE_LICENSE("GPL");