ci13xxx_imx.c 6.1 KB

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