ci_hdrc_imx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 imx27_usb_data = {
  28. CI_HDRC_DISABLE_STREAMING,
  29. };
  30. static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
  31. .flags = CI_HDRC_IMX28_WRITE_FIX |
  32. CI_HDRC_TURN_VBUS_EARLY_ON |
  33. CI_HDRC_DISABLE_STREAMING,
  34. };
  35. static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = {
  36. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  37. CI_HDRC_TURN_VBUS_EARLY_ON |
  38. CI_HDRC_DISABLE_STREAMING,
  39. };
  40. static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = {
  41. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  42. CI_HDRC_TURN_VBUS_EARLY_ON |
  43. CI_HDRC_DISABLE_HOST_STREAMING,
  44. };
  45. static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = {
  46. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  47. CI_HDRC_TURN_VBUS_EARLY_ON |
  48. CI_HDRC_DISABLE_HOST_STREAMING,
  49. };
  50. static const struct ci_hdrc_imx_platform_flag imx6ul_usb_data = {
  51. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
  52. CI_HDRC_TURN_VBUS_EARLY_ON,
  53. };
  54. static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = {
  55. .flags = CI_HDRC_SUPPORTS_RUNTIME_PM,
  56. };
  57. static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
  58. { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
  59. { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
  60. { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
  61. { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
  62. { .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
  63. { .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data},
  64. { .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data},
  65. { /* sentinel */ }
  66. };
  67. MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
  68. struct ci_hdrc_imx_data {
  69. struct usb_phy *phy;
  70. struct platform_device *ci_pdev;
  71. struct clk *clk;
  72. struct imx_usbmisc_data *usbmisc_data;
  73. bool supports_runtime_pm;
  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, "external-vbus-divider", NULL))
  115. data->evdo = 1;
  116. return data;
  117. }
  118. /* End of common functions shared by usbmisc drivers*/
  119. static int imx_get_clks(struct device *dev)
  120. {
  121. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  122. int ret = 0;
  123. data->clk_ipg = devm_clk_get(dev, "ipg");
  124. if (IS_ERR(data->clk_ipg)) {
  125. /* If the platform only needs one clocks */
  126. data->clk = devm_clk_get(dev, NULL);
  127. if (IS_ERR(data->clk)) {
  128. ret = PTR_ERR(data->clk);
  129. dev_err(dev,
  130. "Failed to get clks, err=%ld,%ld\n",
  131. PTR_ERR(data->clk), PTR_ERR(data->clk_ipg));
  132. return ret;
  133. }
  134. return ret;
  135. }
  136. data->clk_ahb = devm_clk_get(dev, "ahb");
  137. if (IS_ERR(data->clk_ahb)) {
  138. ret = PTR_ERR(data->clk_ahb);
  139. dev_err(dev,
  140. "Failed to get ahb clock, err=%d\n", ret);
  141. return ret;
  142. }
  143. data->clk_per = devm_clk_get(dev, "per");
  144. if (IS_ERR(data->clk_per)) {
  145. ret = PTR_ERR(data->clk_per);
  146. dev_err(dev,
  147. "Failed to get per clock, err=%d\n", ret);
  148. return ret;
  149. }
  150. data->need_three_clks = true;
  151. return ret;
  152. }
  153. static int imx_prepare_enable_clks(struct device *dev)
  154. {
  155. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  156. int ret = 0;
  157. if (data->need_three_clks) {
  158. ret = clk_prepare_enable(data->clk_ipg);
  159. if (ret) {
  160. dev_err(dev,
  161. "Failed to prepare/enable ipg clk, err=%d\n",
  162. ret);
  163. return ret;
  164. }
  165. ret = clk_prepare_enable(data->clk_ahb);
  166. if (ret) {
  167. dev_err(dev,
  168. "Failed to prepare/enable ahb clk, err=%d\n",
  169. ret);
  170. clk_disable_unprepare(data->clk_ipg);
  171. return ret;
  172. }
  173. ret = clk_prepare_enable(data->clk_per);
  174. if (ret) {
  175. dev_err(dev,
  176. "Failed to prepare/enable per clk, err=%d\n",
  177. ret);
  178. clk_disable_unprepare(data->clk_ahb);
  179. clk_disable_unprepare(data->clk_ipg);
  180. return ret;
  181. }
  182. } else {
  183. ret = clk_prepare_enable(data->clk);
  184. if (ret) {
  185. dev_err(dev,
  186. "Failed to prepare/enable clk, err=%d\n",
  187. ret);
  188. return ret;
  189. }
  190. }
  191. return ret;
  192. }
  193. static void imx_disable_unprepare_clks(struct device *dev)
  194. {
  195. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  196. if (data->need_three_clks) {
  197. clk_disable_unprepare(data->clk_per);
  198. clk_disable_unprepare(data->clk_ahb);
  199. clk_disable_unprepare(data->clk_ipg);
  200. } else {
  201. clk_disable_unprepare(data->clk);
  202. }
  203. }
  204. static int ci_hdrc_imx_probe(struct platform_device *pdev)
  205. {
  206. struct ci_hdrc_imx_data *data;
  207. struct ci_hdrc_platform_data pdata = {
  208. .name = dev_name(&pdev->dev),
  209. .capoffset = DEF_CAPOFFSET,
  210. };
  211. int ret;
  212. const struct of_device_id *of_id;
  213. const struct ci_hdrc_imx_platform_flag *imx_platform_flag;
  214. of_id = of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev);
  215. if (!of_id)
  216. return -ENODEV;
  217. imx_platform_flag = of_id->data;
  218. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  219. if (!data)
  220. return -ENOMEM;
  221. platform_set_drvdata(pdev, data);
  222. data->usbmisc_data = usbmisc_get_init_data(&pdev->dev);
  223. if (IS_ERR(data->usbmisc_data))
  224. return PTR_ERR(data->usbmisc_data);
  225. ret = imx_get_clks(&pdev->dev);
  226. if (ret)
  227. return ret;
  228. ret = imx_prepare_enable_clks(&pdev->dev);
  229. if (ret)
  230. return ret;
  231. data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
  232. if (IS_ERR(data->phy)) {
  233. ret = PTR_ERR(data->phy);
  234. /* Return -EINVAL if no usbphy is available */
  235. if (ret == -ENODEV)
  236. ret = -EINVAL;
  237. goto err_clk;
  238. }
  239. pdata.usb_phy = data->phy;
  240. pdata.flags |= imx_platform_flag->flags;
  241. if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
  242. data->supports_runtime_pm = true;
  243. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  244. if (ret)
  245. goto err_clk;
  246. ret = imx_usbmisc_init(data->usbmisc_data);
  247. if (ret) {
  248. dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", ret);
  249. goto err_clk;
  250. }
  251. data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
  252. pdev->resource, pdev->num_resources,
  253. &pdata);
  254. if (IS_ERR(data->ci_pdev)) {
  255. ret = PTR_ERR(data->ci_pdev);
  256. dev_err(&pdev->dev,
  257. "Can't register ci_hdrc platform device, err=%d\n",
  258. ret);
  259. goto err_clk;
  260. }
  261. ret = imx_usbmisc_init_post(data->usbmisc_data);
  262. if (ret) {
  263. dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", ret);
  264. goto disable_device;
  265. }
  266. if (data->supports_runtime_pm) {
  267. pm_runtime_set_active(&pdev->dev);
  268. pm_runtime_enable(&pdev->dev);
  269. }
  270. device_set_wakeup_capable(&pdev->dev, true);
  271. return 0;
  272. disable_device:
  273. ci_hdrc_remove_device(data->ci_pdev);
  274. err_clk:
  275. imx_disable_unprepare_clks(&pdev->dev);
  276. return ret;
  277. }
  278. static int ci_hdrc_imx_remove(struct platform_device *pdev)
  279. {
  280. struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
  281. if (data->supports_runtime_pm) {
  282. pm_runtime_get_sync(&pdev->dev);
  283. pm_runtime_disable(&pdev->dev);
  284. pm_runtime_put_noidle(&pdev->dev);
  285. }
  286. ci_hdrc_remove_device(data->ci_pdev);
  287. imx_disable_unprepare_clks(&pdev->dev);
  288. return 0;
  289. }
  290. #ifdef CONFIG_PM
  291. static int imx_controller_suspend(struct device *dev)
  292. {
  293. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  294. dev_dbg(dev, "at %s\n", __func__);
  295. imx_disable_unprepare_clks(dev);
  296. data->in_lpm = true;
  297. return 0;
  298. }
  299. static int imx_controller_resume(struct device *dev)
  300. {
  301. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  302. int ret = 0;
  303. dev_dbg(dev, "at %s\n", __func__);
  304. if (!data->in_lpm) {
  305. WARN_ON(1);
  306. return 0;
  307. }
  308. ret = imx_prepare_enable_clks(dev);
  309. if (ret)
  310. return ret;
  311. data->in_lpm = false;
  312. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
  313. if (ret) {
  314. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
  315. goto clk_disable;
  316. }
  317. return 0;
  318. clk_disable:
  319. imx_disable_unprepare_clks(dev);
  320. return ret;
  321. }
  322. #ifdef CONFIG_PM_SLEEP
  323. static int ci_hdrc_imx_suspend(struct device *dev)
  324. {
  325. int ret;
  326. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  327. if (data->in_lpm)
  328. /* The core's suspend doesn't run */
  329. return 0;
  330. if (device_may_wakeup(dev)) {
  331. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
  332. if (ret) {
  333. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
  334. ret);
  335. return ret;
  336. }
  337. }
  338. return imx_controller_suspend(dev);
  339. }
  340. static int ci_hdrc_imx_resume(struct device *dev)
  341. {
  342. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  343. int ret;
  344. ret = imx_controller_resume(dev);
  345. if (!ret && data->supports_runtime_pm) {
  346. pm_runtime_disable(dev);
  347. pm_runtime_set_active(dev);
  348. pm_runtime_enable(dev);
  349. }
  350. return ret;
  351. }
  352. #endif /* CONFIG_PM_SLEEP */
  353. static int ci_hdrc_imx_runtime_suspend(struct device *dev)
  354. {
  355. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  356. int ret;
  357. if (data->in_lpm) {
  358. WARN_ON(1);
  359. return 0;
  360. }
  361. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
  362. if (ret) {
  363. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
  364. return ret;
  365. }
  366. return imx_controller_suspend(dev);
  367. }
  368. static int ci_hdrc_imx_runtime_resume(struct device *dev)
  369. {
  370. return imx_controller_resume(dev);
  371. }
  372. #endif /* CONFIG_PM */
  373. static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
  374. SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
  375. SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
  376. ci_hdrc_imx_runtime_resume, NULL)
  377. };
  378. static struct platform_driver ci_hdrc_imx_driver = {
  379. .probe = ci_hdrc_imx_probe,
  380. .remove = ci_hdrc_imx_remove,
  381. .driver = {
  382. .name = "imx_usb",
  383. .of_match_table = ci_hdrc_imx_dt_ids,
  384. .pm = &ci_hdrc_imx_pm_ops,
  385. },
  386. };
  387. module_platform_driver(ci_hdrc_imx_driver);
  388. MODULE_ALIAS("platform:imx-usb");
  389. MODULE_LICENSE("GPL v2");
  390. MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
  391. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  392. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");