usb3503.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/platform_device.h>
  26. #include <linux/platform_data/usb3503.h>
  27. #define USB3503_VIDL 0x00
  28. #define USB3503_VIDM 0x01
  29. #define USB3503_PIDL 0x02
  30. #define USB3503_PIDM 0x03
  31. #define USB3503_DIDL 0x04
  32. #define USB3503_DIDM 0x05
  33. #define USB3503_CFG1 0x06
  34. #define USB3503_SELF_BUS_PWR (1 << 7)
  35. #define USB3503_CFG2 0x07
  36. #define USB3503_CFG3 0x08
  37. #define USB3503_NRD 0x09
  38. #define USB3503_PDS 0x0a
  39. #define USB3503_PORT1 (1 << 1)
  40. #define USB3503_PORT2 (1 << 2)
  41. #define USB3503_PORT3 (1 << 3)
  42. #define USB3503_SP_ILOCK 0xe7
  43. #define USB3503_SPILOCK_CONNECT (1 << 1)
  44. #define USB3503_SPILOCK_CONFIG (1 << 0)
  45. #define USB3503_CFGP 0xee
  46. #define USB3503_CLKSUSP (1 << 7)
  47. struct usb3503 {
  48. enum usb3503_mode mode;
  49. struct i2c_client *client;
  50. int gpio_intn;
  51. int gpio_reset;
  52. int gpio_connect;
  53. };
  54. static int usb3503_write_register(struct i2c_client *client,
  55. char reg, char data)
  56. {
  57. return i2c_smbus_write_byte_data(client, reg, data);
  58. }
  59. static int usb3503_read_register(struct i2c_client *client, char reg)
  60. {
  61. return i2c_smbus_read_byte_data(client, reg);
  62. }
  63. static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
  64. {
  65. int err;
  66. err = usb3503_read_register(client, reg);
  67. if (err < 0)
  68. return err;
  69. err = usb3503_write_register(client, reg, err | req);
  70. if (err < 0)
  71. return err;
  72. return 0;
  73. }
  74. static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
  75. {
  76. int err;
  77. err = usb3503_read_register(client, reg);
  78. if (err < 0)
  79. return err;
  80. err = usb3503_write_register(client, reg, err & ~req);
  81. if (err < 0)
  82. return err;
  83. return 0;
  84. }
  85. static int usb3503_reset(int gpio_reset, int state)
  86. {
  87. if (gpio_is_valid(gpio_reset))
  88. gpio_set_value(gpio_reset, state);
  89. /* Wait RefClk when RESET_N is released, otherwise Hub will
  90. * not transition to Hub Communication Stage.
  91. */
  92. if (state)
  93. msleep(100);
  94. return 0;
  95. }
  96. static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
  97. {
  98. struct i2c_client *i2c = hub->client;
  99. int err = 0;
  100. switch (mode) {
  101. case USB3503_MODE_HUB:
  102. usb3503_reset(hub->gpio_reset, 1);
  103. /* SP_ILOCK: set connect_n, config_n for config */
  104. err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
  105. (USB3503_SPILOCK_CONNECT
  106. | USB3503_SPILOCK_CONFIG));
  107. if (err < 0) {
  108. dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
  109. goto err_hubmode;
  110. }
  111. /* PDS : Port2,3 Disable For Self Powered Operation */
  112. err = usb3503_set_bits(i2c, USB3503_PDS,
  113. (USB3503_PORT2 | USB3503_PORT3));
  114. if (err < 0) {
  115. dev_err(&i2c->dev, "PDS failed (%d)\n", err);
  116. goto err_hubmode;
  117. }
  118. /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
  119. err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
  120. if (err < 0) {
  121. dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
  122. goto err_hubmode;
  123. }
  124. /* SP_LOCK: clear connect_n, config_n for hub connect */
  125. err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
  126. (USB3503_SPILOCK_CONNECT
  127. | USB3503_SPILOCK_CONFIG));
  128. if (err < 0) {
  129. dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
  130. goto err_hubmode;
  131. }
  132. hub->mode = mode;
  133. dev_info(&i2c->dev, "switched to HUB mode\n");
  134. break;
  135. case USB3503_MODE_STANDBY:
  136. usb3503_reset(hub->gpio_reset, 0);
  137. hub->mode = mode;
  138. dev_info(&i2c->dev, "switched to STANDBY mode\n");
  139. break;
  140. default:
  141. dev_err(&i2c->dev, "unknown mode is request\n");
  142. err = -EINVAL;
  143. break;
  144. }
  145. err_hubmode:
  146. return err;
  147. }
  148. int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  149. {
  150. struct usb3503_platform_data *pdata = i2c->dev.platform_data;
  151. struct usb3503 *hub;
  152. int err;
  153. hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL);
  154. if (!hub) {
  155. dev_err(&i2c->dev, "private data alloc fail\n");
  156. return -ENOMEM;
  157. }
  158. i2c_set_clientdata(i2c, hub);
  159. hub->client = i2c;
  160. if (!pdata) {
  161. dev_dbg(&i2c->dev, "missing platform data\n");
  162. } else {
  163. hub->gpio_intn = pdata->gpio_intn;
  164. hub->gpio_connect = pdata->gpio_connect;
  165. hub->gpio_reset = pdata->gpio_reset;
  166. hub->mode = pdata->initial_mode;
  167. }
  168. if (gpio_is_valid(hub->gpio_intn)) {
  169. err = gpio_request_one(hub->gpio_intn,
  170. GPIOF_OUT_INIT_HIGH, "usb3503 intn");
  171. if (err) {
  172. dev_err(&i2c->dev,
  173. "unable to request GPIO %d as connect pin (%d)\n",
  174. hub->gpio_intn, err);
  175. goto err_gpio_intn;
  176. }
  177. }
  178. if (gpio_is_valid(hub->gpio_connect)) {
  179. err = gpio_request_one(hub->gpio_connect,
  180. GPIOF_OUT_INIT_HIGH, "usb3503 connect");
  181. if (err) {
  182. dev_err(&i2c->dev,
  183. "unable to request GPIO %d as connect pin (%d)\n",
  184. hub->gpio_connect, err);
  185. goto err_gpio_connect;
  186. }
  187. }
  188. if (gpio_is_valid(hub->gpio_reset)) {
  189. err = gpio_request_one(hub->gpio_reset,
  190. GPIOF_OUT_INIT_LOW, "usb3503 reset");
  191. if (err) {
  192. dev_err(&i2c->dev,
  193. "unable to request GPIO %d as reset pin (%d)\n",
  194. hub->gpio_reset, err);
  195. goto err_gpio_reset;
  196. }
  197. }
  198. usb3503_switch_mode(hub, pdata->initial_mode);
  199. dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
  200. (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
  201. return 0;
  202. err_gpio_reset:
  203. if (gpio_is_valid(hub->gpio_connect))
  204. gpio_free(hub->gpio_connect);
  205. err_gpio_connect:
  206. if (gpio_is_valid(hub->gpio_intn))
  207. gpio_free(hub->gpio_intn);
  208. err_gpio_intn:
  209. kfree(hub);
  210. return err;
  211. }
  212. static int usb3503_remove(struct i2c_client *i2c)
  213. {
  214. struct usb3503 *hub = i2c_get_clientdata(i2c);
  215. if (gpio_is_valid(hub->gpio_intn))
  216. gpio_free(hub->gpio_intn);
  217. if (gpio_is_valid(hub->gpio_connect))
  218. gpio_free(hub->gpio_connect);
  219. if (gpio_is_valid(hub->gpio_reset))
  220. gpio_free(hub->gpio_reset);
  221. kfree(hub);
  222. return 0;
  223. }
  224. static const struct i2c_device_id usb3503_id[] = {
  225. { USB3503_I2C_NAME, 0 },
  226. { }
  227. };
  228. MODULE_DEVICE_TABLE(i2c, usb3503_id);
  229. static struct i2c_driver usb3503_driver = {
  230. .driver = {
  231. .name = USB3503_I2C_NAME,
  232. },
  233. .probe = usb3503_probe,
  234. .remove = usb3503_remove,
  235. .id_table = usb3503_id,
  236. };
  237. static int __init usb3503_init(void)
  238. {
  239. return i2c_add_driver(&usb3503_driver);
  240. }
  241. static void __exit usb3503_exit(void)
  242. {
  243. i2c_del_driver(&usb3503_driver);
  244. }
  245. module_init(usb3503_init);
  246. module_exit(usb3503_exit);
  247. MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
  248. MODULE_DESCRIPTION("USB3503 USB HUB driver");
  249. MODULE_LICENSE("GPL");