ci_hdrc_imx.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 of_device_id ci_hdrc_imx_dt_ids[] = {
  51. { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
  52. { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
  53. { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
  54. { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
  55. { .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
  56. { /* sentinel */ }
  57. };
  58. MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
  59. struct ci_hdrc_imx_data {
  60. struct usb_phy *phy;
  61. struct platform_device *ci_pdev;
  62. struct clk *clk;
  63. struct imx_usbmisc_data *usbmisc_data;
  64. bool supports_runtime_pm;
  65. bool in_lpm;
  66. };
  67. /* Common functions shared by usbmisc drivers */
  68. static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
  69. {
  70. struct platform_device *misc_pdev;
  71. struct device_node *np = dev->of_node;
  72. struct of_phandle_args args;
  73. struct imx_usbmisc_data *data;
  74. int ret;
  75. /*
  76. * In case the fsl,usbmisc property is not present this device doesn't
  77. * need usbmisc. Return NULL (which is no error here)
  78. */
  79. if (!of_get_property(np, "fsl,usbmisc", NULL))
  80. return NULL;
  81. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  82. if (!data)
  83. return ERR_PTR(-ENOMEM);
  84. ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
  85. 0, &args);
  86. if (ret) {
  87. dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
  88. ret);
  89. return ERR_PTR(ret);
  90. }
  91. data->index = args.args[0];
  92. misc_pdev = of_find_device_by_node(args.np);
  93. of_node_put(args.np);
  94. if (!misc_pdev || !platform_get_drvdata(misc_pdev))
  95. return ERR_PTR(-EPROBE_DEFER);
  96. data->dev = &misc_pdev->dev;
  97. if (of_find_property(np, "disable-over-current", NULL))
  98. data->disable_oc = 1;
  99. if (of_find_property(np, "external-vbus-divider", NULL))
  100. data->evdo = 1;
  101. return data;
  102. }
  103. /* End of common functions shared by usbmisc drivers*/
  104. static int ci_hdrc_imx_probe(struct platform_device *pdev)
  105. {
  106. struct ci_hdrc_imx_data *data;
  107. struct ci_hdrc_platform_data pdata = {
  108. .name = dev_name(&pdev->dev),
  109. .capoffset = DEF_CAPOFFSET,
  110. .flags = CI_HDRC_SET_NON_ZERO_TTHA,
  111. };
  112. int ret;
  113. const struct of_device_id *of_id =
  114. of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev);
  115. const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data;
  116. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  117. if (!data)
  118. return -ENOMEM;
  119. data->usbmisc_data = usbmisc_get_init_data(&pdev->dev);
  120. if (IS_ERR(data->usbmisc_data))
  121. return PTR_ERR(data->usbmisc_data);
  122. data->clk = devm_clk_get(&pdev->dev, NULL);
  123. if (IS_ERR(data->clk)) {
  124. dev_err(&pdev->dev,
  125. "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
  126. return PTR_ERR(data->clk);
  127. }
  128. ret = clk_prepare_enable(data->clk);
  129. if (ret) {
  130. dev_err(&pdev->dev,
  131. "Failed to prepare or enable clock, err=%d\n", ret);
  132. return ret;
  133. }
  134. data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
  135. if (IS_ERR(data->phy)) {
  136. ret = PTR_ERR(data->phy);
  137. /* Return -EINVAL if no usbphy is available */
  138. if (ret == -ENODEV)
  139. ret = -EINVAL;
  140. goto err_clk;
  141. }
  142. pdata.usb_phy = data->phy;
  143. pdata.flags |= imx_platform_flag->flags;
  144. if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
  145. data->supports_runtime_pm = true;
  146. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  147. if (ret)
  148. goto err_clk;
  149. ret = imx_usbmisc_init(data->usbmisc_data);
  150. if (ret) {
  151. dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", ret);
  152. goto err_clk;
  153. }
  154. data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
  155. pdev->resource, pdev->num_resources,
  156. &pdata);
  157. if (IS_ERR(data->ci_pdev)) {
  158. ret = PTR_ERR(data->ci_pdev);
  159. dev_err(&pdev->dev,
  160. "Can't register ci_hdrc platform device, err=%d\n",
  161. ret);
  162. goto err_clk;
  163. }
  164. ret = imx_usbmisc_init_post(data->usbmisc_data);
  165. if (ret) {
  166. dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", ret);
  167. goto disable_device;
  168. }
  169. platform_set_drvdata(pdev, data);
  170. if (data->supports_runtime_pm) {
  171. pm_runtime_set_active(&pdev->dev);
  172. pm_runtime_enable(&pdev->dev);
  173. }
  174. device_set_wakeup_capable(&pdev->dev, true);
  175. return 0;
  176. disable_device:
  177. ci_hdrc_remove_device(data->ci_pdev);
  178. err_clk:
  179. clk_disable_unprepare(data->clk);
  180. return ret;
  181. }
  182. static int ci_hdrc_imx_remove(struct platform_device *pdev)
  183. {
  184. struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
  185. if (data->supports_runtime_pm) {
  186. pm_runtime_get_sync(&pdev->dev);
  187. pm_runtime_disable(&pdev->dev);
  188. pm_runtime_put_noidle(&pdev->dev);
  189. }
  190. ci_hdrc_remove_device(data->ci_pdev);
  191. clk_disable_unprepare(data->clk);
  192. return 0;
  193. }
  194. #ifdef CONFIG_PM
  195. static int imx_controller_suspend(struct device *dev)
  196. {
  197. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  198. dev_dbg(dev, "at %s\n", __func__);
  199. clk_disable_unprepare(data->clk);
  200. data->in_lpm = true;
  201. return 0;
  202. }
  203. static int imx_controller_resume(struct device *dev)
  204. {
  205. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  206. int ret = 0;
  207. dev_dbg(dev, "at %s\n", __func__);
  208. if (!data->in_lpm) {
  209. WARN_ON(1);
  210. return 0;
  211. }
  212. ret = clk_prepare_enable(data->clk);
  213. if (ret)
  214. return ret;
  215. data->in_lpm = false;
  216. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
  217. if (ret) {
  218. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
  219. goto clk_disable;
  220. }
  221. return 0;
  222. clk_disable:
  223. clk_disable_unprepare(data->clk);
  224. return ret;
  225. }
  226. #ifdef CONFIG_PM_SLEEP
  227. static int ci_hdrc_imx_suspend(struct device *dev)
  228. {
  229. int ret;
  230. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  231. if (data->in_lpm)
  232. /* The core's suspend doesn't run */
  233. return 0;
  234. if (device_may_wakeup(dev)) {
  235. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
  236. if (ret) {
  237. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
  238. ret);
  239. return ret;
  240. }
  241. }
  242. return imx_controller_suspend(dev);
  243. }
  244. static int ci_hdrc_imx_resume(struct device *dev)
  245. {
  246. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  247. int ret;
  248. ret = imx_controller_resume(dev);
  249. if (!ret && data->supports_runtime_pm) {
  250. pm_runtime_disable(dev);
  251. pm_runtime_set_active(dev);
  252. pm_runtime_enable(dev);
  253. }
  254. return ret;
  255. }
  256. #endif /* CONFIG_PM_SLEEP */
  257. static int ci_hdrc_imx_runtime_suspend(struct device *dev)
  258. {
  259. struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
  260. int ret;
  261. if (data->in_lpm) {
  262. WARN_ON(1);
  263. return 0;
  264. }
  265. ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
  266. if (ret) {
  267. dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
  268. return ret;
  269. }
  270. return imx_controller_suspend(dev);
  271. }
  272. static int ci_hdrc_imx_runtime_resume(struct device *dev)
  273. {
  274. return imx_controller_resume(dev);
  275. }
  276. #endif /* CONFIG_PM */
  277. static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
  278. SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
  279. SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
  280. ci_hdrc_imx_runtime_resume, NULL)
  281. };
  282. static struct platform_driver ci_hdrc_imx_driver = {
  283. .probe = ci_hdrc_imx_probe,
  284. .remove = ci_hdrc_imx_remove,
  285. .driver = {
  286. .name = "imx_usb",
  287. .of_match_table = ci_hdrc_imx_dt_ids,
  288. .pm = &ci_hdrc_imx_pm_ops,
  289. },
  290. };
  291. module_platform_driver(ci_hdrc_imx_driver);
  292. MODULE_ALIAS("platform:imx-usb");
  293. MODULE_LICENSE("GPL v2");
  294. MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
  295. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  296. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");