usb3503.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. hub->port_off_mask = 0;
  159. clk = devm_clk_get(dev, "refclk");
  160. if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) {
  161. dev_err(dev, "unable to request refclk (%ld)\n",
  162. PTR_ERR(clk));
  163. return PTR_ERR(clk);
  164. }
  165. if (!IS_ERR(clk)) {
  166. u32 rate = 0;
  167. hub->clk = clk;
  168. if (!of_property_read_u32(np, "refclk-frequency",
  169. &rate)) {
  170. switch (rate) {
  171. case 38400000:
  172. case 26000000:
  173. case 19200000:
  174. case 12000000:
  175. hub->secondary_ref_clk = 0;
  176. break;
  177. case 24000000:
  178. case 27000000:
  179. case 25000000:
  180. case 50000000:
  181. hub->secondary_ref_clk = 1;
  182. break;
  183. default:
  184. dev_err(dev,
  185. "unsupported reference clock rate (%d)\n",
  186. (int) rate);
  187. return -EINVAL;
  188. }
  189. err = clk_set_rate(hub->clk, rate);
  190. if (err) {
  191. dev_err(dev,
  192. "unable to set reference clock rate to %d\n",
  193. (int) rate);
  194. return err;
  195. }
  196. }
  197. err = clk_prepare_enable(hub->clk);
  198. if (err) {
  199. dev_err(dev,
  200. "unable to enable reference clock\n");
  201. return err;
  202. }
  203. }
  204. property = of_get_property(np, "disabled-ports", &len);
  205. if (property && (len / sizeof(u32)) > 0) {
  206. int i;
  207. for (i = 0; i < len / sizeof(u32); i++) {
  208. u32 port = be32_to_cpu(property[i]);
  209. if ((1 <= port) && (port <= 3))
  210. hub->port_off_mask |= (1 << port);
  211. }
  212. }
  213. hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0);
  214. if (hub->gpio_intn == -EPROBE_DEFER)
  215. return -EPROBE_DEFER;
  216. hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0);
  217. if (hub->gpio_connect == -EPROBE_DEFER)
  218. return -EPROBE_DEFER;
  219. hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
  220. if (hub->gpio_reset == -EPROBE_DEFER)
  221. return -EPROBE_DEFER;
  222. of_property_read_u32(np, "initial-mode", &mode);
  223. hub->mode = mode;
  224. }
  225. if (hub->port_off_mask && !hub->regmap)
  226. dev_err(dev, "Ports disabled with no control interface\n");
  227. if (gpio_is_valid(hub->gpio_intn)) {
  228. int val = hub->secondary_ref_clk ? GPIOF_OUT_INIT_LOW :
  229. GPIOF_OUT_INIT_HIGH;
  230. err = devm_gpio_request_one(dev, hub->gpio_intn, val,
  231. "usb3503 intn");
  232. if (err) {
  233. dev_err(dev,
  234. "unable to request GPIO %d as interrupt pin (%d)\n",
  235. hub->gpio_intn, err);
  236. return err;
  237. }
  238. }
  239. if (gpio_is_valid(hub->gpio_connect)) {
  240. err = devm_gpio_request_one(dev, hub->gpio_connect,
  241. GPIOF_OUT_INIT_LOW, "usb3503 connect");
  242. if (err) {
  243. dev_err(dev,
  244. "unable to request GPIO %d as connect pin (%d)\n",
  245. hub->gpio_connect, err);
  246. return err;
  247. }
  248. }
  249. if (gpio_is_valid(hub->gpio_reset)) {
  250. err = devm_gpio_request_one(dev, hub->gpio_reset,
  251. GPIOF_OUT_INIT_LOW, "usb3503 reset");
  252. if (err) {
  253. dev_err(dev,
  254. "unable to request GPIO %d as reset pin (%d)\n",
  255. hub->gpio_reset, err);
  256. return err;
  257. }
  258. }
  259. usb3503_switch_mode(hub, hub->mode);
  260. dev_info(dev, "%s: probed in %s mode\n", __func__,
  261. (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
  262. return 0;
  263. }
  264. static int usb3503_i2c_probe(struct i2c_client *i2c,
  265. const struct i2c_device_id *id)
  266. {
  267. struct usb3503 *hub;
  268. int err;
  269. hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL);
  270. if (!hub)
  271. return -ENOMEM;
  272. i2c_set_clientdata(i2c, hub);
  273. hub->regmap = devm_regmap_init_i2c(i2c, &usb3503_regmap_config);
  274. if (IS_ERR(hub->regmap)) {
  275. err = PTR_ERR(hub->regmap);
  276. dev_err(&i2c->dev, "Failed to initialise regmap: %d\n", err);
  277. return err;
  278. }
  279. hub->dev = &i2c->dev;
  280. return usb3503_probe(hub);
  281. }
  282. static int usb3503_platform_probe(struct platform_device *pdev)
  283. {
  284. struct usb3503 *hub;
  285. hub = devm_kzalloc(&pdev->dev, sizeof(struct usb3503), GFP_KERNEL);
  286. if (!hub)
  287. return -ENOMEM;
  288. hub->dev = &pdev->dev;
  289. return usb3503_probe(hub);
  290. }
  291. #ifdef CONFIG_PM_SLEEP
  292. static int usb3503_i2c_suspend(struct device *dev)
  293. {
  294. struct i2c_client *client = to_i2c_client(dev);
  295. struct usb3503 *hub = i2c_get_clientdata(client);
  296. usb3503_switch_mode(hub, USB3503_MODE_STANDBY);
  297. if (hub->clk)
  298. clk_disable_unprepare(hub->clk);
  299. return 0;
  300. }
  301. static int usb3503_i2c_resume(struct device *dev)
  302. {
  303. struct i2c_client *client = to_i2c_client(dev);
  304. struct usb3503 *hub = i2c_get_clientdata(client);
  305. if (hub->clk)
  306. clk_prepare_enable(hub->clk);
  307. usb3503_switch_mode(hub, hub->mode);
  308. return 0;
  309. }
  310. #endif
  311. static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend,
  312. usb3503_i2c_resume);
  313. static const struct i2c_device_id usb3503_id[] = {
  314. { USB3503_I2C_NAME, 0 },
  315. { }
  316. };
  317. MODULE_DEVICE_TABLE(i2c, usb3503_id);
  318. #ifdef CONFIG_OF
  319. static const struct of_device_id usb3503_of_match[] = {
  320. { .compatible = "smsc,usb3503", },
  321. { .compatible = "smsc,usb3503a", },
  322. {},
  323. };
  324. MODULE_DEVICE_TABLE(of, usb3503_of_match);
  325. #endif
  326. static struct i2c_driver usb3503_i2c_driver = {
  327. .driver = {
  328. .name = USB3503_I2C_NAME,
  329. .pm = &usb3503_i2c_pm_ops,
  330. .of_match_table = of_match_ptr(usb3503_of_match),
  331. },
  332. .probe = usb3503_i2c_probe,
  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. },
  340. .probe = usb3503_platform_probe,
  341. };
  342. static int __init usb3503_init(void)
  343. {
  344. int err;
  345. err = i2c_register_driver(THIS_MODULE, &usb3503_i2c_driver);
  346. if (err != 0)
  347. pr_err("usb3503: Failed to register I2C driver: %d\n", err);
  348. err = platform_driver_register(&usb3503_platform_driver);
  349. if (err != 0)
  350. pr_err("usb3503: Failed to register platform driver: %d\n",
  351. err);
  352. return 0;
  353. }
  354. module_init(usb3503_init);
  355. static void __exit usb3503_exit(void)
  356. {
  357. platform_driver_unregister(&usb3503_platform_driver);
  358. i2c_del_driver(&usb3503_i2c_driver);
  359. }
  360. module_exit(usb3503_exit);
  361. MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
  362. MODULE_DESCRIPTION("USB3503 USB HUB driver");
  363. MODULE_LICENSE("GPL");