usb3503.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_SP_ILOCK 0xe7
  41. #define USB3503_SPILOCK_CONNECT (1 << 1)
  42. #define USB3503_SPILOCK_CONFIG (1 << 0)
  43. #define USB3503_CFGP 0xee
  44. #define USB3503_CLKSUSP (1 << 7)
  45. struct usb3503 {
  46. enum usb3503_mode mode;
  47. struct i2c_client *client;
  48. u8 port_off_mask;
  49. int gpio_intn;
  50. int gpio_reset;
  51. int gpio_connect;
  52. };
  53. static int usb3503_write_register(struct i2c_client *client,
  54. char reg, char data)
  55. {
  56. return i2c_smbus_write_byte_data(client, reg, data);
  57. }
  58. static int usb3503_read_register(struct i2c_client *client, char reg)
  59. {
  60. return i2c_smbus_read_byte_data(client, reg);
  61. }
  62. static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
  63. {
  64. int err;
  65. err = usb3503_read_register(client, reg);
  66. if (err < 0)
  67. return err;
  68. err = usb3503_write_register(client, reg, err | req);
  69. if (err < 0)
  70. return err;
  71. return 0;
  72. }
  73. static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
  74. {
  75. int err;
  76. err = usb3503_read_register(client, reg);
  77. if (err < 0)
  78. return err;
  79. err = usb3503_write_register(client, reg, err & ~req);
  80. if (err < 0)
  81. return err;
  82. return 0;
  83. }
  84. static int usb3503_reset(int gpio_reset, int state)
  85. {
  86. if (gpio_is_valid(gpio_reset))
  87. gpio_set_value(gpio_reset, state);
  88. /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
  89. if (state)
  90. usleep_range(4000, 10000);
  91. return 0;
  92. }
  93. static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
  94. {
  95. struct i2c_client *i2c = hub->client;
  96. int err = 0;
  97. switch (mode) {
  98. case USB3503_MODE_HUB:
  99. usb3503_reset(hub->gpio_reset, 1);
  100. /* SP_ILOCK: set connect_n, config_n for config */
  101. err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
  102. (USB3503_SPILOCK_CONNECT
  103. | USB3503_SPILOCK_CONFIG));
  104. if (err < 0) {
  105. dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
  106. goto err_hubmode;
  107. }
  108. /* PDS : Disable For Self Powered Operation */
  109. if (hub->port_off_mask) {
  110. err = usb3503_set_bits(i2c, USB3503_PDS,
  111. hub->port_off_mask);
  112. if (err < 0) {
  113. dev_err(&i2c->dev, "PDS failed (%d)\n", err);
  114. goto err_hubmode;
  115. }
  116. }
  117. /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
  118. err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
  119. if (err < 0) {
  120. dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
  121. goto err_hubmode;
  122. }
  123. /* SP_LOCK: clear connect_n, config_n for hub connect */
  124. err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
  125. (USB3503_SPILOCK_CONNECT
  126. | USB3503_SPILOCK_CONFIG));
  127. if (err < 0) {
  128. dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
  129. goto err_hubmode;
  130. }
  131. hub->mode = mode;
  132. dev_info(&i2c->dev, "switched to HUB mode\n");
  133. break;
  134. case USB3503_MODE_STANDBY:
  135. usb3503_reset(hub->gpio_reset, 0);
  136. hub->mode = mode;
  137. dev_info(&i2c->dev, "switched to STANDBY mode\n");
  138. break;
  139. default:
  140. dev_err(&i2c->dev, "unknown mode is request\n");
  141. err = -EINVAL;
  142. break;
  143. }
  144. err_hubmode:
  145. return err;
  146. }
  147. static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  148. {
  149. struct usb3503_platform_data *pdata = i2c->dev.platform_data;
  150. struct device_node *np = i2c->dev.of_node;
  151. struct usb3503 *hub;
  152. int err = -ENOMEM;
  153. u32 mode = USB3503_MODE_UNKNOWN;
  154. const u32 *property;
  155. int len;
  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->port_off_mask = pdata->port_off_mask;
  165. hub->gpio_intn = pdata->gpio_intn;
  166. hub->gpio_connect = pdata->gpio_connect;
  167. hub->gpio_reset = pdata->gpio_reset;
  168. hub->mode = pdata->initial_mode;
  169. } else if (np) {
  170. hub->port_off_mask = 0;
  171. property = of_get_property(np, "disabled-ports", &len);
  172. if (property && (len / sizeof(u32)) > 0) {
  173. int i;
  174. for (i = 0; i < len / sizeof(u32); i++) {
  175. u32 port = be32_to_cpu(property[i]);
  176. if ((1 <= port) && (port <= 3))
  177. hub->port_off_mask |= (1 << port);
  178. }
  179. }
  180. hub->gpio_intn = of_get_named_gpio(np, "connect-gpios", 0);
  181. if (hub->gpio_intn == -EPROBE_DEFER)
  182. return -EPROBE_DEFER;
  183. hub->gpio_connect = of_get_named_gpio(np, "intn-gpios", 0);
  184. if (hub->gpio_connect == -EPROBE_DEFER)
  185. return -EPROBE_DEFER;
  186. hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
  187. if (hub->gpio_reset == -EPROBE_DEFER)
  188. return -EPROBE_DEFER;
  189. of_property_read_u32(np, "initial-mode", &mode);
  190. hub->mode = mode;
  191. }
  192. if (gpio_is_valid(hub->gpio_intn)) {
  193. err = gpio_request_one(hub->gpio_intn,
  194. GPIOF_OUT_INIT_HIGH, "usb3503 intn");
  195. if (err) {
  196. dev_err(&i2c->dev,
  197. "unable to request GPIO %d as connect pin (%d)\n",
  198. hub->gpio_intn, err);
  199. goto err_out;
  200. }
  201. }
  202. if (gpio_is_valid(hub->gpio_connect)) {
  203. err = gpio_request_one(hub->gpio_connect,
  204. GPIOF_OUT_INIT_HIGH, "usb3503 connect");
  205. if (err) {
  206. dev_err(&i2c->dev,
  207. "unable to request GPIO %d as connect pin (%d)\n",
  208. hub->gpio_connect, err);
  209. goto err_gpio_connect;
  210. }
  211. }
  212. if (gpio_is_valid(hub->gpio_reset)) {
  213. err = gpio_request_one(hub->gpio_reset,
  214. GPIOF_OUT_INIT_LOW, "usb3503 reset");
  215. if (err) {
  216. dev_err(&i2c->dev,
  217. "unable to request GPIO %d as reset pin (%d)\n",
  218. hub->gpio_reset, err);
  219. goto err_gpio_reset;
  220. }
  221. }
  222. usb3503_switch_mode(hub, hub->mode);
  223. dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
  224. (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
  225. return 0;
  226. err_gpio_reset:
  227. if (gpio_is_valid(hub->gpio_connect))
  228. gpio_free(hub->gpio_connect);
  229. err_gpio_connect:
  230. if (gpio_is_valid(hub->gpio_intn))
  231. gpio_free(hub->gpio_intn);
  232. err_out:
  233. kfree(hub);
  234. return err;
  235. }
  236. static int usb3503_remove(struct i2c_client *i2c)
  237. {
  238. struct usb3503 *hub = i2c_get_clientdata(i2c);
  239. if (gpio_is_valid(hub->gpio_intn))
  240. gpio_free(hub->gpio_intn);
  241. if (gpio_is_valid(hub->gpio_connect))
  242. gpio_free(hub->gpio_connect);
  243. if (gpio_is_valid(hub->gpio_reset))
  244. gpio_free(hub->gpio_reset);
  245. kfree(hub);
  246. return 0;
  247. }
  248. static const struct i2c_device_id usb3503_id[] = {
  249. { USB3503_I2C_NAME, 0 },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(i2c, usb3503_id);
  253. #ifdef CONFIG_OF
  254. static const struct of_device_id usb3503_of_match[] = {
  255. { .compatible = "smsc,usb3503", },
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(of, usb3503_of_match);
  259. #endif
  260. static struct i2c_driver usb3503_driver = {
  261. .driver = {
  262. .name = USB3503_I2C_NAME,
  263. .of_match_table = of_match_ptr(usb3503_of_match),
  264. },
  265. .probe = usb3503_probe,
  266. .remove = usb3503_remove,
  267. .id_table = usb3503_id,
  268. };
  269. module_i2c_driver(usb3503_driver);
  270. MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
  271. MODULE_DESCRIPTION("USB3503 USB HUB driver");
  272. MODULE_LICENSE("GPL");