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