ci_hdrc_imx.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #define CI_HDRC_IMX_IMX28_WRITE_FIX BIT(0)
  24. struct ci_hdrc_imx_platform_flag {
  25. unsigned int flags;
  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_IMX_IMX28_WRITE_FIX,
  31. };
  32. static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
  33. { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
  34. { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
  35. { /* sentinel */ }
  36. };
  37. MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
  38. struct ci_hdrc_imx_data {
  39. struct usb_phy *phy;
  40. struct platform_device *ci_pdev;
  41. struct clk *clk;
  42. struct imx_usbmisc_data *usbmisc_data;
  43. };
  44. /* Common functions shared by usbmisc drivers */
  45. static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
  46. {
  47. struct device_node *np = dev->of_node;
  48. struct of_phandle_args args;
  49. struct imx_usbmisc_data *data;
  50. int ret;
  51. /*
  52. * In case the fsl,usbmisc property is not present this device doesn't
  53. * need usbmisc. Return NULL (which is no error here)
  54. */
  55. if (!of_get_property(np, "fsl,usbmisc", NULL))
  56. return NULL;
  57. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  58. if (!data)
  59. return ERR_PTR(-ENOMEM);
  60. ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
  61. 0, &args);
  62. if (ret) {
  63. dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
  64. ret);
  65. return ERR_PTR(ret);
  66. }
  67. data->index = args.args[0];
  68. of_node_put(args.np);
  69. if (of_find_property(np, "disable-over-current", NULL))
  70. data->disable_oc = 1;
  71. if (of_find_property(np, "external-vbus-divider", NULL))
  72. data->evdo = 1;
  73. return data;
  74. }
  75. /* End of common functions shared by usbmisc drivers*/
  76. static int ci_hdrc_imx_probe(struct platform_device *pdev)
  77. {
  78. struct ci_hdrc_imx_data *data;
  79. struct ci_hdrc_platform_data pdata = {
  80. .name = dev_name(&pdev->dev),
  81. .capoffset = DEF_CAPOFFSET,
  82. .flags = CI_HDRC_REQUIRE_TRANSCEIVER |
  83. CI_HDRC_DISABLE_STREAMING,
  84. };
  85. int ret;
  86. const struct of_device_id *of_id =
  87. of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev);
  88. const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data;
  89. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  90. if (!data) {
  91. dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n");
  92. return -ENOMEM;
  93. }
  94. data->usbmisc_data = usbmisc_get_init_data(&pdev->dev);
  95. if (IS_ERR(data->usbmisc_data))
  96. return PTR_ERR(data->usbmisc_data);
  97. data->clk = devm_clk_get(&pdev->dev, NULL);
  98. if (IS_ERR(data->clk)) {
  99. dev_err(&pdev->dev,
  100. "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
  101. return PTR_ERR(data->clk);
  102. }
  103. ret = clk_prepare_enable(data->clk);
  104. if (ret) {
  105. dev_err(&pdev->dev,
  106. "Failed to prepare or enable clock, err=%d\n", ret);
  107. return ret;
  108. }
  109. data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
  110. if (IS_ERR(data->phy)) {
  111. ret = PTR_ERR(data->phy);
  112. goto err_clk;
  113. }
  114. pdata.phy = data->phy;
  115. if (imx_platform_flag->flags & CI_HDRC_IMX_IMX28_WRITE_FIX)
  116. pdata.flags |= CI_HDRC_IMX28_WRITE_FIX;
  117. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  118. if (ret)
  119. goto err_clk;
  120. if (data->usbmisc_data) {
  121. ret = imx_usbmisc_init(data->usbmisc_data);
  122. if (ret) {
  123. dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n",
  124. ret);
  125. goto err_clk;
  126. }
  127. }
  128. data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
  129. pdev->resource, pdev->num_resources,
  130. &pdata);
  131. if (IS_ERR(data->ci_pdev)) {
  132. ret = PTR_ERR(data->ci_pdev);
  133. dev_err(&pdev->dev,
  134. "Can't register ci_hdrc platform device, err=%d\n",
  135. ret);
  136. goto err_clk;
  137. }
  138. if (data->usbmisc_data) {
  139. ret = imx_usbmisc_init_post(data->usbmisc_data);
  140. if (ret) {
  141. dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n",
  142. ret);
  143. goto disable_device;
  144. }
  145. }
  146. platform_set_drvdata(pdev, data);
  147. pm_runtime_no_callbacks(&pdev->dev);
  148. pm_runtime_enable(&pdev->dev);
  149. return 0;
  150. disable_device:
  151. ci_hdrc_remove_device(data->ci_pdev);
  152. err_clk:
  153. clk_disable_unprepare(data->clk);
  154. return ret;
  155. }
  156. static int ci_hdrc_imx_remove(struct platform_device *pdev)
  157. {
  158. struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
  159. pm_runtime_disable(&pdev->dev);
  160. ci_hdrc_remove_device(data->ci_pdev);
  161. clk_disable_unprepare(data->clk);
  162. return 0;
  163. }
  164. static struct platform_driver ci_hdrc_imx_driver = {
  165. .probe = ci_hdrc_imx_probe,
  166. .remove = ci_hdrc_imx_remove,
  167. .driver = {
  168. .name = "imx_usb",
  169. .owner = THIS_MODULE,
  170. .of_match_table = ci_hdrc_imx_dt_ids,
  171. },
  172. };
  173. module_platform_driver(ci_hdrc_imx_driver);
  174. MODULE_ALIAS("platform:imx-usb");
  175. MODULE_LICENSE("GPL v2");
  176. MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
  177. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  178. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");