ci13xxx_imx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <linux/regulator/consumer.h>
  22. #include "ci.h"
  23. #include "ci13xxx_imx.h"
  24. #define pdev_to_phy(pdev) \
  25. ((struct usb_phy *)platform_get_drvdata(pdev))
  26. struct ci13xxx_imx_data {
  27. struct usb_phy *phy;
  28. struct platform_device *ci_pdev;
  29. struct clk *clk;
  30. struct regulator *reg_vbus;
  31. };
  32. static const struct usbmisc_ops *usbmisc_ops;
  33. /* Common functions shared by usbmisc drivers */
  34. int usbmisc_set_ops(const struct usbmisc_ops *ops)
  35. {
  36. if (usbmisc_ops)
  37. return -EBUSY;
  38. usbmisc_ops = ops;
  39. return 0;
  40. }
  41. EXPORT_SYMBOL_GPL(usbmisc_set_ops);
  42. void usbmisc_unset_ops(const struct usbmisc_ops *ops)
  43. {
  44. usbmisc_ops = NULL;
  45. }
  46. EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
  47. int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
  48. {
  49. struct device_node *np = dev->of_node;
  50. struct of_phandle_args args;
  51. int ret;
  52. usbdev->dev = dev;
  53. ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
  54. 0, &args);
  55. if (ret) {
  56. dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
  57. ret);
  58. memset(usbdev, 0, sizeof(*usbdev));
  59. return ret;
  60. }
  61. usbdev->index = args.args[0];
  62. of_node_put(args.np);
  63. if (of_find_property(np, "disable-over-current", NULL))
  64. usbdev->disable_oc = 1;
  65. if (of_find_property(np, "external-vbus-divider", NULL))
  66. usbdev->evdo = 1;
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
  70. /* End of common functions shared by usbmisc drivers*/
  71. static int ci13xxx_imx_probe(struct platform_device *pdev)
  72. {
  73. struct ci13xxx_imx_data *data;
  74. struct ci13xxx_platform_data pdata = {
  75. .name = "ci13xxx_imx",
  76. .capoffset = DEF_CAPOFFSET,
  77. .flags = CI13XXX_REQUIRE_TRANSCEIVER |
  78. CI13XXX_PULLUP_ON_VBUS |
  79. CI13XXX_DISABLE_STREAMING,
  80. };
  81. struct resource *res;
  82. int ret;
  83. struct usb_phy *phy;
  84. if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
  85. && !usbmisc_ops)
  86. return -EPROBE_DEFER;
  87. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  88. if (!data) {
  89. dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
  90. return -ENOMEM;
  91. }
  92. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  93. if (!res) {
  94. dev_err(&pdev->dev, "Can't get device resources!\n");
  95. return -ENOENT;
  96. }
  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. phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
  110. if (!IS_ERR(phy)) {
  111. ret = usb_phy_init(phy);
  112. if (ret) {
  113. dev_err(&pdev->dev, "unable to init phy: %d\n", ret);
  114. goto err_clk;
  115. }
  116. } else if (PTR_ERR(phy) == -EPROBE_DEFER) {
  117. ret = -EPROBE_DEFER;
  118. goto err_clk;
  119. }
  120. /* we only support host now, so enable vbus here */
  121. data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
  122. if (!IS_ERR(data->reg_vbus)) {
  123. ret = regulator_enable(data->reg_vbus);
  124. if (ret) {
  125. dev_err(&pdev->dev,
  126. "Failed to enable vbus regulator, err=%d\n",
  127. ret);
  128. goto err_clk;
  129. }
  130. } else {
  131. data->reg_vbus = NULL;
  132. }
  133. pdata.phy = data->phy;
  134. if (!pdev->dev.dma_mask)
  135. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  136. if (!pdev->dev.coherent_dma_mask)
  137. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  138. if (usbmisc_ops && usbmisc_ops->init) {
  139. ret = usbmisc_ops->init(&pdev->dev);
  140. if (ret) {
  141. dev_err(&pdev->dev,
  142. "usbmisc init failed, ret=%d\n", ret);
  143. goto err;
  144. }
  145. }
  146. data->ci_pdev = ci13xxx_add_device(&pdev->dev,
  147. pdev->resource, pdev->num_resources,
  148. &pdata);
  149. if (IS_ERR(data->ci_pdev)) {
  150. ret = PTR_ERR(data->ci_pdev);
  151. dev_err(&pdev->dev,
  152. "Can't register ci_hdrc platform device, err=%d\n",
  153. ret);
  154. goto err;
  155. }
  156. if (usbmisc_ops && usbmisc_ops->post) {
  157. ret = usbmisc_ops->post(&pdev->dev);
  158. if (ret) {
  159. dev_err(&pdev->dev,
  160. "usbmisc post failed, ret=%d\n", ret);
  161. goto disable_device;
  162. }
  163. }
  164. platform_set_drvdata(pdev, data);
  165. pm_runtime_no_callbacks(&pdev->dev);
  166. pm_runtime_enable(&pdev->dev);
  167. return 0;
  168. disable_device:
  169. ci13xxx_remove_device(data->ci_pdev);
  170. err:
  171. if (data->reg_vbus)
  172. regulator_disable(data->reg_vbus);
  173. err_clk:
  174. clk_disable_unprepare(data->clk);
  175. return ret;
  176. }
  177. static int ci13xxx_imx_remove(struct platform_device *pdev)
  178. {
  179. struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
  180. pm_runtime_disable(&pdev->dev);
  181. ci13xxx_remove_device(data->ci_pdev);
  182. if (data->reg_vbus)
  183. regulator_disable(data->reg_vbus);
  184. if (data->phy) {
  185. usb_phy_shutdown(data->phy);
  186. module_put(data->phy->dev->driver->owner);
  187. }
  188. clk_disable_unprepare(data->clk);
  189. return 0;
  190. }
  191. static const struct of_device_id ci13xxx_imx_dt_ids[] = {
  192. { .compatible = "fsl,imx27-usb", },
  193. { /* sentinel */ }
  194. };
  195. MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
  196. static struct platform_driver ci13xxx_imx_driver = {
  197. .probe = ci13xxx_imx_probe,
  198. .remove = ci13xxx_imx_remove,
  199. .driver = {
  200. .name = "imx_usb",
  201. .owner = THIS_MODULE,
  202. .of_match_table = ci13xxx_imx_dt_ids,
  203. },
  204. };
  205. module_platform_driver(ci13xxx_imx_driver);
  206. MODULE_ALIAS("platform:imx-usb");
  207. MODULE_LICENSE("GPL v2");
  208. MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
  209. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  210. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");