ci_hdrc_imx.c 8.4 KB

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