usb3503.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <linux/regmap.h>
  29. #define USB3503_VIDL 0x00
  30. #define USB3503_VIDM 0x01
  31. #define USB3503_PIDL 0x02
  32. #define USB3503_PIDM 0x03
  33. #define USB3503_DIDL 0x04
  34. #define USB3503_DIDM 0x05
  35. #define USB3503_CFG1 0x06
  36. #define USB3503_SELF_BUS_PWR (1 << 7)
  37. #define USB3503_CFG2 0x07
  38. #define USB3503_CFG3 0x08
  39. #define USB3503_NRD 0x09
  40. #define USB3503_PDS 0x0a
  41. #define USB3503_SP_ILOCK 0xe7
  42. #define USB3503_SPILOCK_CONNECT (1 << 1)
  43. #define USB3503_SPILOCK_CONFIG (1 << 0)
  44. #define USB3503_CFGP 0xee
  45. #define USB3503_CLKSUSP (1 << 7)
  46. #define USB3503_RESET 0xff
  47. struct usb3503 {
  48. enum usb3503_mode mode;
  49. struct regmap *regmap;
  50. struct device *dev;
  51. u8 port_off_mask;
  52. int gpio_intn;
  53. int gpio_reset;
  54. int gpio_connect;
  55. };
  56. static int usb3503_reset(struct usb3503 *hub, int state)
  57. {
  58. if (!state && gpio_is_valid(hub->gpio_connect))
  59. gpio_set_value_cansleep(hub->gpio_connect, 0);
  60. if (gpio_is_valid(hub->gpio_reset))
  61. gpio_set_value_cansleep(hub->gpio_reset, state);
  62. /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
  63. if (state)
  64. usleep_range(4000, 10000);
  65. return 0;
  66. }
  67. static int usb3503_connect(struct usb3503 *hub)
  68. {
  69. struct device *dev = hub->dev;
  70. int err;
  71. usb3503_reset(hub, 1);
  72. if (hub->regmap) {
  73. /* SP_ILOCK: set connect_n, config_n for config */
  74. err = regmap_write(hub->regmap, USB3503_SP_ILOCK,
  75. (USB3503_SPILOCK_CONNECT
  76. | USB3503_SPILOCK_CONFIG));
  77. if (err < 0) {
  78. dev_err(dev, "SP_ILOCK failed (%d)\n", err);
  79. return err;
  80. }
  81. /* PDS : Disable For Self Powered Operation */
  82. if (hub->port_off_mask) {
  83. err = regmap_update_bits(hub->regmap, USB3503_PDS,
  84. hub->port_off_mask,
  85. hub->port_off_mask);
  86. if (err < 0) {
  87. dev_err(dev, "PDS failed (%d)\n", err);
  88. return err;
  89. }
  90. }
  91. /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
  92. err = regmap_update_bits(hub->regmap, USB3503_CFG1,
  93. USB3503_SELF_BUS_PWR,
  94. USB3503_SELF_BUS_PWR);
  95. if (err < 0) {
  96. dev_err(dev, "CFG1 failed (%d)\n", err);
  97. return err;
  98. }
  99. /* SP_LOCK: clear connect_n, config_n for hub connect */
  100. err = regmap_update_bits(hub->regmap, USB3503_SP_ILOCK,
  101. (USB3503_SPILOCK_CONNECT
  102. | USB3503_SPILOCK_CONFIG), 0);
  103. if (err < 0) {
  104. dev_err(dev, "SP_ILOCK failed (%d)\n", err);
  105. return err;
  106. }
  107. }
  108. if (gpio_is_valid(hub->gpio_connect))
  109. gpio_set_value_cansleep(hub->gpio_connect, 1);
  110. hub->mode = USB3503_MODE_HUB;
  111. dev_info(dev, "switched to HUB mode\n");
  112. return 0;
  113. }
  114. static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
  115. {
  116. struct device *dev = hub->dev;
  117. int err = 0;
  118. switch (mode) {
  119. case USB3503_MODE_HUB:
  120. err = usb3503_connect(hub);
  121. break;
  122. case USB3503_MODE_STANDBY:
  123. usb3503_reset(hub, 0);
  124. hub->mode = mode;
  125. dev_info(dev, "switched to STANDBY mode\n");
  126. break;
  127. default:
  128. dev_err(dev, "unknown mode is requested\n");
  129. err = -EINVAL;
  130. break;
  131. }
  132. return err;
  133. }
  134. static const struct regmap_config usb3503_regmap_config = {
  135. .reg_bits = 8,
  136. .val_bits = 8,
  137. .max_register = USB3503_RESET,
  138. };
  139. static int usb3503_probe(struct usb3503 *hub)
  140. {
  141. struct device *dev = hub->dev;
  142. struct usb3503_platform_data *pdata = dev_get_platdata(dev);
  143. struct device_node *np = dev->of_node;
  144. int err;
  145. u32 mode = USB3503_MODE_HUB;
  146. const u32 *property;
  147. int len;
  148. if (pdata) {
  149. hub->port_off_mask = pdata->port_off_mask;
  150. hub->gpio_intn = pdata->gpio_intn;
  151. hub->gpio_connect = pdata->gpio_connect;
  152. hub->gpio_reset = pdata->gpio_reset;
  153. hub->mode = pdata->initial_mode;
  154. } else if (np) {
  155. hub->port_off_mask = 0;
  156. property = of_get_property(np, "disabled-ports", &len);
  157. if (property && (len / sizeof(u32)) > 0) {
  158. int i;
  159. for (i = 0; i < len / sizeof(u32); i++) {
  160. u32 port = be32_to_cpu(property[i]);
  161. if ((1 <= port) && (port <= 3))
  162. hub->port_off_mask |= (1 << port);
  163. }
  164. }
  165. hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0);
  166. if (hub->gpio_intn == -EPROBE_DEFER)
  167. return -EPROBE_DEFER;
  168. hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0);
  169. if (hub->gpio_connect == -EPROBE_DEFER)
  170. return -EPROBE_DEFER;
  171. hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
  172. if (hub->gpio_reset == -EPROBE_DEFER)
  173. return -EPROBE_DEFER;
  174. of_property_read_u32(np, "initial-mode", &mode);
  175. hub->mode = mode;
  176. }
  177. if (hub->port_off_mask && !hub->regmap)
  178. dev_err(dev, "Ports disabled with no control interface\n");
  179. if (gpio_is_valid(hub->gpio_intn)) {
  180. err = devm_gpio_request_one(dev, hub->gpio_intn,
  181. GPIOF_OUT_INIT_HIGH, "usb3503 intn");
  182. if (err) {
  183. dev_err(dev,
  184. "unable to request GPIO %d as connect pin (%d)\n",
  185. hub->gpio_intn, err);
  186. return err;
  187. }
  188. }
  189. if (gpio_is_valid(hub->gpio_connect)) {
  190. err = devm_gpio_request_one(dev, hub->gpio_connect,
  191. GPIOF_OUT_INIT_LOW, "usb3503 connect");
  192. if (err) {
  193. dev_err(dev,
  194. "unable to request GPIO %d as connect pin (%d)\n",
  195. hub->gpio_connect, err);
  196. return err;
  197. }
  198. }
  199. if (gpio_is_valid(hub->gpio_reset)) {
  200. err = devm_gpio_request_one(dev, hub->gpio_reset,
  201. GPIOF_OUT_INIT_LOW, "usb3503 reset");
  202. if (err) {
  203. dev_err(dev,
  204. "unable to request GPIO %d as reset pin (%d)\n",
  205. hub->gpio_reset, err);
  206. return err;
  207. }
  208. }
  209. usb3503_switch_mode(hub, hub->mode);
  210. dev_info(dev, "%s: probed in %s mode\n", __func__,
  211. (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
  212. return 0;
  213. }
  214. static int usb3503_i2c_probe(struct i2c_client *i2c,
  215. const struct i2c_device_id *id)
  216. {
  217. struct usb3503 *hub;
  218. int err;
  219. hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL);
  220. if (!hub) {
  221. dev_err(&i2c->dev, "private data alloc fail\n");
  222. return -ENOMEM;
  223. }
  224. i2c_set_clientdata(i2c, hub);
  225. hub->regmap = devm_regmap_init_i2c(i2c, &usb3503_regmap_config);
  226. if (IS_ERR(hub->regmap)) {
  227. err = PTR_ERR(hub->regmap);
  228. dev_err(&i2c->dev, "Failed to initialise regmap: %d\n", err);
  229. return err;
  230. }
  231. hub->dev = &i2c->dev;
  232. return usb3503_probe(hub);
  233. }
  234. static int usb3503_platform_probe(struct platform_device *pdev)
  235. {
  236. struct usb3503 *hub;
  237. hub = devm_kzalloc(&pdev->dev, sizeof(struct usb3503), GFP_KERNEL);
  238. if (!hub) {
  239. dev_err(&pdev->dev, "private data alloc fail\n");
  240. return -ENOMEM;
  241. }
  242. hub->dev = &pdev->dev;
  243. return usb3503_probe(hub);
  244. }
  245. static const struct i2c_device_id usb3503_id[] = {
  246. { USB3503_I2C_NAME, 0 },
  247. { }
  248. };
  249. MODULE_DEVICE_TABLE(i2c, usb3503_id);
  250. #ifdef CONFIG_OF
  251. static const struct of_device_id usb3503_of_match[] = {
  252. { .compatible = "smsc,usb3503", },
  253. { .compatible = "smsc,usb3503a", },
  254. {},
  255. };
  256. MODULE_DEVICE_TABLE(of, usb3503_of_match);
  257. #endif
  258. static struct i2c_driver usb3503_i2c_driver = {
  259. .driver = {
  260. .name = USB3503_I2C_NAME,
  261. .of_match_table = of_match_ptr(usb3503_of_match),
  262. },
  263. .probe = usb3503_i2c_probe,
  264. .id_table = usb3503_id,
  265. };
  266. static struct platform_driver usb3503_platform_driver = {
  267. .driver = {
  268. .name = USB3503_I2C_NAME,
  269. .of_match_table = of_match_ptr(usb3503_of_match),
  270. .owner = THIS_MODULE,
  271. },
  272. .probe = usb3503_platform_probe,
  273. };
  274. static int __init usb3503_init(void)
  275. {
  276. int err;
  277. err = i2c_register_driver(THIS_MODULE, &usb3503_i2c_driver);
  278. if (err != 0)
  279. pr_err("usb3503: Failed to register I2C driver: %d\n", err);
  280. err = platform_driver_register(&usb3503_platform_driver);
  281. if (err != 0)
  282. pr_err("usb3503: Failed to register platform driver: %d\n",
  283. err);
  284. return 0;
  285. }
  286. module_init(usb3503_init);
  287. static void __exit usb3503_exit(void)
  288. {
  289. platform_driver_unregister(&usb3503_platform_driver);
  290. i2c_del_driver(&usb3503_i2c_driver);
  291. }
  292. module_exit(usb3503_exit);
  293. MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
  294. MODULE_DESCRIPTION("USB3503 USB HUB driver");
  295. MODULE_LICENSE("GPL");