ci_hdrc_imx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2012 Freescale Semiconductor, Inc.
  4. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  5. * on behalf of DENX Software Engineering GmbH
  6. */
  7. #include <linux/module.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/usb/chipidea.h>
  14. #include <linux/usb/of.h>
  15. #include <linux/clk.h>
  16. #include "ci.h"
  17. #include "ci_hdrc_imx.h"
  18. struct ci_hdrc_imx_platform_flag {
  19. unsigned int flags;
  20. };
  21. static const struct ci_hdrc_imx_platform_flag imx23_usb_data = {
  22. .flags = CI_HDRC_TURN_VBUS_EARLY_ON |
  23. CI_HDRC_DISABLE_STREAMING,
  24. };
  25. static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
  26. .flags = CI_HDRC_DISABLE_STREAMING,
  27. };
  28. static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
  29. .flags = CI_HDRC_IMX28_WRITE_FIX |
  30. CI_HDRC_TURN_VBUS_EARLY_ON |
  31. CI_HDRC_DISABLE_STREAMING,
  32. };
  33. static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = {
  34. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  35. CI_HDRC_TURN_VBUS_EARLY_ON |
  36. CI_HDRC_DISABLE_STREAMING,
  37. };
  38. static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = {
  39. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  40. CI_HDRC_TURN_VBUS_EARLY_ON |
  41. CI_HDRC_DISABLE_HOST_STREAMING,
  42. };
  43. static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = {
  44. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  45. CI_HDRC_TURN_VBUS_EARLY_ON |
  46. CI_HDRC_DISABLE_HOST_STREAMING,
  47. };
  48. static const struct ci_hdrc_imx_platform_flag imx6ul_usb_data = {
  49. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  50. CI_HDRC_TURN_VBUS_EARLY_ON,
  51. };
  52. static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = {
  53. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM,
  54. };
  55. static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
  56. { .compatible = "fsl,imx23-usb", .data = &imx23_usb_data},
  57. { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
  58. { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
  59. { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
  60. { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
  61. { .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
  62. { .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data},
  63. { .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data},
  64. { /* sentinel */ }
  65. };
  66. MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
  67. struct ci_hdrc_imx_data {
  68. struct usb_phy *phy;
  69. struct platform_device *ci_pdev;
  70. struct clk *clk;
  71. struct imx_usbmisc_data *usbmisc_data;
  72. bool supports_runtime_pm;
  73. bool override_phy_control;
  74. bool in_lpm;
  75. /* SoC before i.mx6 (except imx23/imx28) needs three clks */
  76. bool need_three_clks;
  77. struct clk *clk_ipg;
  78. struct clk *clk_ahb;
  79. struct clk *clk_per;
  80. /* --------------------------------- */
  81. };
  82. /* Common functions shared by usbmisc drivers */
  83. static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
  84. {
  85. struct platform_device *misc_pdev;
  86. struct device_node *np = dev->of_node;
  87. struct of_phandle_args args;
  88. struct imx_usbmisc_data *data;
  89. int ret;
  90. /*
  91. * In case the fsl,usbmisc property is not present this device doesn't
  92. * need usbmisc. Return NULL (which is no error here)
  93. */
  94. if (!of_get_property(np, "fsl,usbmisc", NULL))
  95. return NULL;
  96. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  97. if (!data)
  98. return ERR_PTR(-ENOMEM);
  99. ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
  100. 0, &args);
  101. if (ret) {
  102. dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
  103. ret);
  104. return ERR_PTR(ret);
  105. }
  106. data->index = args.args[0];
  107. misc_pdev = of_find_device_by_node(args.np);
  108. of_node_put(args.np);
  109. if (!misc_pdev || !platform_get_drvdata(misc_pdev))
  110. return ERR_PTR(-EPROBE_DEFER);
  111. data->dev = &misc_pdev->dev;
  112. if (of_find_property(np, "disable-over-current", NULL))
  113. data->disable_oc = 1;
  114. if (of_find_property(np, "over-current-active-high", NULL))
  115. data->oc_polarity = 1;
  116. if (of_find_property(np, "external-vbus-divider", NULL))
  117. data->evdo = 1;
  118. if (of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI)
  119. data->ulpi = 1;
  120. return data;
  121. }
  122. /* End of common functions shared by usbmisc drivers*/
  123. static int imx_get_clks(struct device *dev)
  124. {
  125. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  126. int ret = 0;
  127. data->clk_ipg = devm_clk_get(dev, "ipg");
  128. if (IS_ERR(data->clk_ipg)) {
  129. /* If the platform only needs one clocks */
  130. data->clk = devm_clk_get(dev, NULL);
  131. if (IS_ERR(data->clk)) {
  132. ret = PTR_ERR(data->clk);
  133. dev_err(dev,
  134. "Failed to get clks, err=%ld,%ld\n",
  135. PTR_ERR(data->clk), PTR_ERR(data->clk_ipg));
  136. return ret;
  137. }
  138. return ret;
  139. }
  140. data->clk_ahb = devm_clk_get(dev, "ahb");
  141. if (IS_ERR(data->clk_ahb)) {
  142. ret = PTR_ERR(data->clk_ahb);
  143. dev_err(dev,
  144. "Failed to get ahb clock, err=%d\n", ret);
  145. return ret;
  146. }
  147. data->clk_per = devm_clk_get(dev, "per");
  148. if (IS_ERR(data->clk_per)) {
  149. ret = PTR_ERR(data->clk_per);
  150. dev_err(dev,
  151. "Failed to get per clock, err=%d\n", ret);
  152. return ret;
  153. }
  154. data->need_three_clks = true;
  155. return ret;
  156. }
  157. static int imx_prepare_enable_clks(struct device *dev)
  158. {
  159. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  160. int ret = 0;
  161. if (data->need_three_clks) {
  162. ret = clk_prepare_enable(data->clk_ipg);
  163. if (ret) {
  164. dev_err(dev,
  165. "Failed to prepare/enable ipg clk, err=%d\n",
  166. ret);
  167. return ret;
  168. }
  169. ret = clk_prepare_enable(data->clk_ahb);
  170. if (ret) {
  171. dev_err(dev,
  172. "Failed to prepare/enable ahb clk, err=%d\n",
  173. ret);
  174. clk_disable_unprepare(data->clk_ipg);
  175. return ret;
  176. }
  177. ret = clk_prepare_enable(data->clk_per);
  178. if (ret) {
  179. dev_err(dev,
  180. "Failed to prepare/enable per clk, err=%d\n",
  181. ret);
  182. clk_disable_unprepare(data->clk_ahb);
  183. clk_disable_unprepare(data->clk_ipg);
  184. return ret;
  185. }
  186. } else {
  187. ret = clk_prepare_enable(data->clk);
  188. if (ret) {
  189. dev_err(dev,
  190. "Failed to prepare/enable clk, err=%d\n",
  191. ret);
  192. return ret;
  193. }
  194. }
  195. return ret;
  196. }
  197. static void imx_disable_unprepare_clks(struct device *dev)
  198. {
  199. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  200. if (data->need_three_clks) {
  201. clk_disable_unprepare(data->clk_per);
  202. clk_disable_unprepare(data->clk_ahb);
  203. clk_disable_unprepare(data->clk_ipg);
  204. } else {
  205. clk_disable_unprepare(data->clk);
  206. }
  207. }
  208. static int ci_hdrc_imx_probe(struct platform_device *pdev)
  209. {
  210. struct ci_hdrc_imx_data *data;
  211. struct ci_hdrc_platform_data pdata = {
  212. .name = dev_name(&pdev->dev),
  213. .capoffset = DEF_CAPOFFSET,
  214. };
  215. int ret;
  216. const struct of_device_id *of_id;
  217. const struct ci_hdrc_imx_platform_flag *imx_platform_flag;
  218. struct device_node *np = pdev->dev.of_node;
  219. of_id = of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev);
  220. if (!of_id)
  221. return -ENODEV;
  222. imx_platform_flag = of_id->data;
  223. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  224. if (!data)
  225. return -ENOMEM;
  226. platform_set_drvdata(pdev, data);
  227. data->usbmisc_data = usbmisc_get_init_data(&pdev->dev);
  228. if (IS_ERR(data->usbmisc_data))
  229. return PTR_ERR(data->usbmisc_data);
  230. ret = imx_get_clks(&pdev->dev);
  231. if (ret)
  232. return ret;
  233. ret = imx_prepare_enable_clks(&pdev->dev);
  234. if (ret)
  235. return ret;
  236. data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
  237. if (IS_ERR(data->phy)) {
  238. ret = PTR_ERR(data->phy);
  239. /* Return -EINVAL if no usbphy is available */
  240. if (ret == -ENODEV)
  241. ret = -EINVAL;
  242. goto err_clk;
  243. }
  244. pdata.usb_phy = data->phy;
  245. if ((of_device_is_compatible(np, "fsl,imx53-usb") ||
  246. of_device_is_compatible(np, "fsl,imx51-usb")) && pdata.usb_phy &&
  247. of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI) {
  248. pdata.flags |= CI_HDRC_OVERRIDE_PHY_CONTROL;
  249. data->override_phy_control = true;
  250. usb_phy_init(pdata.usb_phy);
  251. }
  252. pdata.flags |= imx_platform_flag->flags;
  253. if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
  254. data->supports_runtime_pm = true;
  255. ret = imx_usbmisc_init(data->usbmisc_data);
  256. if (ret) {
  257. dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", ret);
  258. goto err_clk;
  259. }
  260. data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
  261. pdev->resource, pdev->num_resources,
  262. &pdata);
  263. if (IS_ERR(data->ci_pdev)) {
  264. ret = PTR_ERR(data->ci_pdev);
  265. if (ret != -EPROBE_DEFER)
  266. dev_err(&pdev->dev,
  267. "ci_hdrc_add_device failed, err=%d\n", ret);
  268. goto err_clk;
  269. }
  270. ret = imx_usbmisc_init_post(data->usbmisc_data);
  271. if (ret) {
  272. dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", ret);
  273. goto disable_device;
  274. }
  275. if (data->supports_runtime_pm) {
  276. pm_runtime_set_active(&pdev->dev);
  277. pm_runtime_enable(&pdev->dev);
  278. }
  279. device_set_wakeup_capable(&pdev->dev, true);
  280. return 0;
  281. disable_device:
  282. ci_hdrc_remove_device(data->ci_pdev);
  283. err_clk:
  284. imx_disable_unprepare_clks(&pdev->dev);
  285. return ret;
  286. }
  287. static int ci_hdrc_imx_remove(struct platform_device *pdev)
  288. {
  289. struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
  290. if (data->supports_runtime_pm) {
  291. pm_runtime_get_sync(&pdev->dev);
  292. pm_runtime_disable(&pdev->dev);
  293. pm_runtime_put_noidle(&pdev->dev);
  294. }
  295. ci_hdrc_remove_device(data->ci_pdev);
  296. if (data->override_phy_control)
  297. usb_phy_shutdown(data->phy);
  298. imx_disable_unprepare_clks(&pdev->dev);
  299. return 0;
  300. }
  301. static void ci_hdrc_imx_shutdown(struct platform_device *pdev)
  302. {
  303. ci_hdrc_imx_remove(pdev);
  304. }
  305. #ifdef CONFIG_PM
  306. static int imx_controller_suspend(struct device *dev)
  307. {
  308. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  309. dev_dbg(dev, "at %s\n", __func__);
  310. imx_disable_unprepare_clks(dev);
  311. data->in_lpm = true;
  312. return 0;
  313. }
  314. static int imx_controller_resume(struct device *dev)
  315. {
  316. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  317. int ret = 0;
  318. dev_dbg(dev, "at %s\n", __func__);
  319. if (!data->in_lpm) {
  320. WARN_ON(1);
  321. return 0;
  322. }
  323. ret = imx_prepare_enable_clks(dev);
  324. if (ret)
  325. return ret;
  326. data->in_lpm = false;
  327. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
  328. if (ret) {
  329. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
  330. goto clk_disable;
  331. }
  332. return 0;
  333. clk_disable:
  334. imx_disable_unprepare_clks(dev);
  335. return ret;
  336. }
  337. #ifdef CONFIG_PM_SLEEP
  338. static int ci_hdrc_imx_suspend(struct device *dev)
  339. {
  340. int ret;
  341. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  342. if (data->in_lpm)
  343. /* The core's suspend doesn't run */
  344. return 0;
  345. if (device_may_wakeup(dev)) {
  346. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
  347. if (ret) {
  348. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
  349. ret);
  350. return ret;
  351. }
  352. }
  353. return imx_controller_suspend(dev);
  354. }
  355. static int ci_hdrc_imx_resume(struct device *dev)
  356. {
  357. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  358. int ret;
  359. ret = imx_controller_resume(dev);
  360. if (!ret && data->supports_runtime_pm) {
  361. pm_runtime_disable(dev);
  362. pm_runtime_set_active(dev);
  363. pm_runtime_enable(dev);
  364. }
  365. return ret;
  366. }
  367. #endif /* CONFIG_PM_SLEEP */
  368. static int ci_hdrc_imx_runtime_suspend(struct device *dev)
  369. {
  370. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  371. int ret;
  372. if (data->in_lpm) {
  373. WARN_ON(1);
  374. return 0;
  375. }
  376. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
  377. if (ret) {
  378. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
  379. return ret;
  380. }
  381. return imx_controller_suspend(dev);
  382. }
  383. static int ci_hdrc_imx_runtime_resume(struct device *dev)
  384. {
  385. return imx_controller_resume(dev);
  386. }
  387. #endif /* CONFIG_PM */
  388. static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
  389. SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
  390. SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
  391. ci_hdrc_imx_runtime_resume, NULL)
  392. };
  393. static struct platform_driver ci_hdrc_imx_driver = {
  394. .probe = ci_hdrc_imx_probe,
  395. .remove = ci_hdrc_imx_remove,
  396. .shutdown = ci_hdrc_imx_shutdown,
  397. .driver = {
  398. .name = "imx_usb",
  399. .of_match_table = ci_hdrc_imx_dt_ids,
  400. .pm = &ci_hdrc_imx_pm_ops,
  401. },
  402. };
  403. module_platform_driver(ci_hdrc_imx_driver);
  404. MODULE_ALIAS("platform:imx-usb");
  405. MODULE_LICENSE("GPL v2");
  406. MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
  407. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  408. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");