usb3503.c 10 KB

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