usb3503.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for SMSC USB3503 USB 2.0 hub controller driver
  4. *
  5. * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/i2c.h>
  9. #include <linux/gpio.h>
  10. #include <linux/delay.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/platform_data/usb3503.h>
  16. #include <linux/regmap.h>
  17. #define USB3503_VIDL 0x00
  18. #define USB3503_VIDM 0x01
  19. #define USB3503_PIDL 0x02
  20. #define USB3503_PIDM 0x03
  21. #define USB3503_DIDL 0x04
  22. #define USB3503_DIDM 0x05
  23. #define USB3503_CFG1 0x06
  24. #define USB3503_SELF_BUS_PWR (1 << 7)
  25. #define USB3503_CFG2 0x07
  26. #define USB3503_CFG3 0x08
  27. #define USB3503_NRD 0x09
  28. #define USB3503_PDS 0x0a
  29. #define USB3503_SP_ILOCK 0xe7
  30. #define USB3503_SPILOCK_CONNECT (1 << 1)
  31. #define USB3503_SPILOCK_CONFIG (1 << 0)
  32. #define USB3503_CFGP 0xee
  33. #define USB3503_CLKSUSP (1 << 7)
  34. #define USB3503_RESET 0xff
  35. struct usb3503 {
  36. enum usb3503_mode mode;
  37. struct regmap *regmap;
  38. struct device *dev;
  39. struct clk *clk;
  40. u8 port_off_mask;
  41. int gpio_intn;
  42. int gpio_reset;
  43. int gpio_connect;
  44. bool secondary_ref_clk;
  45. };
  46. static int usb3503_reset(struct usb3503 *hub, int state)
  47. {
  48. if (!state && gpio_is_valid(hub->gpio_connect))
  49. gpio_set_value_cansleep(hub->gpio_connect, 0);
  50. if (gpio_is_valid(hub->gpio_reset))
  51. gpio_set_value_cansleep(hub->gpio_reset, state);
  52. /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
  53. if (state)
  54. usleep_range(4000, 10000);
  55. return 0;
  56. }
  57. static int usb3503_connect(struct usb3503 *hub)
  58. {
  59. struct device *dev = hub->dev;
  60. int err;
  61. usb3503_reset(hub, 1);
  62. if (hub->regmap) {
  63. /* SP_ILOCK: set connect_n, config_n for config */
  64. err = regmap_write(hub->regmap, USB3503_SP_ILOCK,
  65. (USB3503_SPILOCK_CONNECT
  66. | USB3503_SPILOCK_CONFIG));
  67. if (err < 0) {
  68. dev_err(dev, "SP_ILOCK failed (%d)\n", err);
  69. return err;
  70. }
  71. /* PDS : Set the ports which are disabled in self-powered mode. */
  72. if (hub->port_off_mask) {
  73. err = regmap_update_bits(hub->regmap, USB3503_PDS,
  74. hub->port_off_mask,
  75. hub->port_off_mask);
  76. if (err < 0) {
  77. dev_err(dev, "PDS failed (%d)\n", err);
  78. return err;
  79. }
  80. }
  81. /* CFG1 : Set SELF_BUS_PWR, this enables self-powered operation. */
  82. err = regmap_update_bits(hub->regmap, USB3503_CFG1,
  83. USB3503_SELF_BUS_PWR,
  84. USB3503_SELF_BUS_PWR);
  85. if (err < 0) {
  86. dev_err(dev, "CFG1 failed (%d)\n", err);
  87. return err;
  88. }
  89. /* SP_LOCK: clear connect_n, config_n for hub connect */
  90. err = regmap_update_bits(hub->regmap, USB3503_SP_ILOCK,
  91. (USB3503_SPILOCK_CONNECT
  92. | USB3503_SPILOCK_CONFIG), 0);
  93. if (err < 0) {
  94. dev_err(dev, "SP_ILOCK failed (%d)\n", err);
  95. return err;
  96. }
  97. }
  98. if (gpio_is_valid(hub->gpio_connect))
  99. gpio_set_value_cansleep(hub->gpio_connect, 1);
  100. hub->mode = USB3503_MODE_HUB;
  101. dev_info(dev, "switched to HUB mode\n");
  102. return 0;
  103. }
  104. static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
  105. {
  106. struct device *dev = hub->dev;
  107. int err = 0;
  108. switch (mode) {
  109. case USB3503_MODE_HUB:
  110. err = usb3503_connect(hub);
  111. break;
  112. case USB3503_MODE_STANDBY:
  113. usb3503_reset(hub, 0);
  114. dev_info(dev, "switched to STANDBY mode\n");
  115. break;
  116. default:
  117. dev_err(dev, "unknown mode is requested\n");
  118. err = -EINVAL;
  119. break;
  120. }
  121. return err;
  122. }
  123. static const struct regmap_config usb3503_regmap_config = {
  124. .reg_bits = 8,
  125. .val_bits = 8,
  126. .max_register = USB3503_RESET,
  127. };
  128. static int usb3503_probe(struct usb3503 *hub)
  129. {
  130. struct device *dev = hub->dev;
  131. struct usb3503_platform_data *pdata = dev_get_platdata(dev);
  132. struct device_node *np = dev->of_node;
  133. int err;
  134. u32 mode = USB3503_MODE_HUB;
  135. const u32 *property;
  136. int len;
  137. if (pdata) {
  138. hub->port_off_mask = pdata->port_off_mask;
  139. hub->gpio_intn = pdata->gpio_intn;
  140. hub->gpio_connect = pdata->gpio_connect;
  141. hub->gpio_reset = pdata->gpio_reset;
  142. hub->mode = pdata->initial_mode;
  143. } else if (np) {
  144. struct clk *clk;
  145. u32 rate = 0;
  146. hub->port_off_mask = 0;
  147. if (!of_property_read_u32(np, "refclk-frequency", &rate)) {
  148. switch (rate) {
  149. case 38400000:
  150. case 26000000:
  151. case 19200000:
  152. case 12000000:
  153. hub->secondary_ref_clk = 0;
  154. break;
  155. case 24000000:
  156. case 27000000:
  157. case 25000000:
  158. case 50000000:
  159. hub->secondary_ref_clk = 1;
  160. break;
  161. default:
  162. dev_err(dev,
  163. "unsupported reference clock rate (%d)\n",
  164. (int) rate);
  165. return -EINVAL;
  166. }
  167. }
  168. clk = devm_clk_get(dev, "refclk");
  169. if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) {
  170. dev_err(dev, "unable to request refclk (%ld)\n",
  171. PTR_ERR(clk));
  172. return PTR_ERR(clk);
  173. }
  174. if (!IS_ERR(clk)) {
  175. hub->clk = clk;
  176. if (rate != 0) {
  177. err = clk_set_rate(hub->clk, rate);
  178. if (err) {
  179. dev_err(dev,
  180. "unable to set reference clock rate to %d\n",
  181. (int) rate);
  182. return err;
  183. }
  184. }
  185. err = clk_prepare_enable(hub->clk);
  186. if (err) {
  187. dev_err(dev,
  188. "unable to enable reference clock\n");
  189. return err;
  190. }
  191. }
  192. property = of_get_property(np, "disabled-ports", &len);
  193. if (property && (len / sizeof(u32)) > 0) {
  194. int i;
  195. for (i = 0; i < len / sizeof(u32); i++) {
  196. u32 port = be32_to_cpu(property[i]);
  197. if ((1 <= port) && (port <= 3))
  198. hub->port_off_mask |= (1 << port);
  199. }
  200. }
  201. hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0);
  202. if (hub->gpio_intn == -EPROBE_DEFER)
  203. return -EPROBE_DEFER;
  204. hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0);
  205. if (hub->gpio_connect == -EPROBE_DEFER)
  206. return -EPROBE_DEFER;
  207. hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
  208. if (hub->gpio_reset == -EPROBE_DEFER)
  209. return -EPROBE_DEFER;
  210. of_property_read_u32(np, "initial-mode", &mode);
  211. hub->mode = mode;
  212. }
  213. if (hub->port_off_mask && !hub->regmap)
  214. dev_err(dev, "Ports disabled with no control interface\n");
  215. if (gpio_is_valid(hub->gpio_intn)) {
  216. int val = hub->secondary_ref_clk ? GPIOF_OUT_INIT_LOW :
  217. GPIOF_OUT_INIT_HIGH;
  218. err = devm_gpio_request_one(dev, hub->gpio_intn, val,
  219. "usb3503 intn");
  220. if (err) {
  221. dev_err(dev,
  222. "unable to request GPIO %d as interrupt pin (%d)\n",
  223. hub->gpio_intn, err);
  224. return err;
  225. }
  226. }
  227. if (gpio_is_valid(hub->gpio_connect)) {
  228. err = devm_gpio_request_one(dev, hub->gpio_connect,
  229. GPIOF_OUT_INIT_LOW, "usb3503 connect");
  230. if (err) {
  231. dev_err(dev,
  232. "unable to request GPIO %d as connect pin (%d)\n",
  233. hub->gpio_connect, err);
  234. return err;
  235. }
  236. }
  237. if (gpio_is_valid(hub->gpio_reset)) {
  238. err = devm_gpio_request_one(dev, hub->gpio_reset,
  239. GPIOF_OUT_INIT_LOW, "usb3503 reset");
  240. /* Datasheet defines a hardware reset to be at least 100us */
  241. usleep_range(100, 10000);
  242. if (err) {
  243. dev_err(dev,
  244. "unable to request GPIO %d as reset pin (%d)\n",
  245. hub->gpio_reset, err);
  246. return err;
  247. }
  248. }
  249. usb3503_switch_mode(hub, hub->mode);
  250. dev_info(dev, "%s: probed in %s mode\n", __func__,
  251. (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
  252. return 0;
  253. }
  254. static int usb3503_i2c_probe(struct i2c_client *i2c,
  255. const struct i2c_device_id *id)
  256. {
  257. struct usb3503 *hub;
  258. int err;
  259. hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL);
  260. if (!hub)
  261. return -ENOMEM;
  262. i2c_set_clientdata(i2c, hub);
  263. hub->regmap = devm_regmap_init_i2c(i2c, &usb3503_regmap_config);
  264. if (IS_ERR(hub->regmap)) {
  265. err = PTR_ERR(hub->regmap);
  266. dev_err(&i2c->dev, "Failed to initialise regmap: %d\n", err);
  267. return err;
  268. }
  269. hub->dev = &i2c->dev;
  270. return usb3503_probe(hub);
  271. }
  272. static int usb3503_i2c_remove(struct i2c_client *i2c)
  273. {
  274. struct usb3503 *hub;
  275. hub = i2c_get_clientdata(i2c);
  276. if (hub->clk)
  277. clk_disable_unprepare(hub->clk);
  278. return 0;
  279. }
  280. static int usb3503_platform_probe(struct platform_device *pdev)
  281. {
  282. struct usb3503 *hub;
  283. hub = devm_kzalloc(&pdev->dev, sizeof(struct usb3503), GFP_KERNEL);
  284. if (!hub)
  285. return -ENOMEM;
  286. hub->dev = &pdev->dev;
  287. platform_set_drvdata(pdev, hub);
  288. return usb3503_probe(hub);
  289. }
  290. static int usb3503_platform_remove(struct platform_device *pdev)
  291. {
  292. struct usb3503 *hub;
  293. hub = platform_get_drvdata(pdev);
  294. if (hub->clk)
  295. clk_disable_unprepare(hub->clk);
  296. return 0;
  297. }
  298. #ifdef CONFIG_PM_SLEEP
  299. static int usb3503_i2c_suspend(struct device *dev)
  300. {
  301. struct i2c_client *client = to_i2c_client(dev);
  302. struct usb3503 *hub = i2c_get_clientdata(client);
  303. usb3503_switch_mode(hub, USB3503_MODE_STANDBY);
  304. if (hub->clk)
  305. clk_disable_unprepare(hub->clk);
  306. return 0;
  307. }
  308. static int usb3503_i2c_resume(struct device *dev)
  309. {
  310. struct i2c_client *client = to_i2c_client(dev);
  311. struct usb3503 *hub = i2c_get_clientdata(client);
  312. if (hub->clk)
  313. clk_prepare_enable(hub->clk);
  314. usb3503_switch_mode(hub, hub->mode);
  315. return 0;
  316. }
  317. #endif
  318. static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend,
  319. usb3503_i2c_resume);
  320. static const struct i2c_device_id usb3503_id[] = {
  321. { USB3503_I2C_NAME, 0 },
  322. { }
  323. };
  324. MODULE_DEVICE_TABLE(i2c, usb3503_id);
  325. #ifdef CONFIG_OF
  326. static const struct of_device_id usb3503_of_match[] = {
  327. { .compatible = "smsc,usb3503", },
  328. { .compatible = "smsc,usb3503a", },
  329. {},
  330. };
  331. MODULE_DEVICE_TABLE(of, usb3503_of_match);
  332. #endif
  333. static struct i2c_driver usb3503_i2c_driver = {
  334. .driver = {
  335. .name = USB3503_I2C_NAME,
  336. .pm = &usb3503_i2c_pm_ops,
  337. .of_match_table = of_match_ptr(usb3503_of_match),
  338. },
  339. .probe = usb3503_i2c_probe,
  340. .remove = usb3503_i2c_remove,
  341. .id_table = usb3503_id,
  342. };
  343. static struct platform_driver usb3503_platform_driver = {
  344. .driver = {
  345. .name = USB3503_I2C_NAME,
  346. .of_match_table = of_match_ptr(usb3503_of_match),
  347. },
  348. .probe = usb3503_platform_probe,
  349. .remove = usb3503_platform_remove,
  350. };
  351. static int __init usb3503_init(void)
  352. {
  353. int err;
  354. err = i2c_add_driver(&usb3503_i2c_driver);
  355. if (err != 0)
  356. pr_err("usb3503: Failed to register I2C driver: %d\n", err);
  357. err = platform_driver_register(&usb3503_platform_driver);
  358. if (err != 0)
  359. pr_err("usb3503: Failed to register platform driver: %d\n",
  360. err);
  361. return 0;
  362. }
  363. module_init(usb3503_init);
  364. static void __exit usb3503_exit(void)
  365. {
  366. platform_driver_unregister(&usb3503_platform_driver);
  367. i2c_del_driver(&usb3503_i2c_driver);
  368. }
  369. module_exit(usb3503_exit);
  370. MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
  371. MODULE_DESCRIPTION("USB3503 USB HUB driver");
  372. MODULE_LICENSE("GPL");