ci_hdrc_imx.c 11 KB

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