ci13xxx_imx.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 device_node *phy_np;
  84. struct resource *res;
  85. int ret;
  86. if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
  87. && !usbmisc_ops)
  88. return -EPROBE_DEFER;
  89. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  90. if (!data) {
  91. dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
  92. return -ENOMEM;
  93. }
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. if (!res) {
  96. dev_err(&pdev->dev, "Can't get device resources!\n");
  97. return -ENOENT;
  98. }
  99. data->clk = devm_clk_get(&pdev->dev, NULL);
  100. if (IS_ERR(data->clk)) {
  101. dev_err(&pdev->dev,
  102. "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
  103. return PTR_ERR(data->clk);
  104. }
  105. ret = clk_prepare_enable(data->clk);
  106. if (ret) {
  107. dev_err(&pdev->dev,
  108. "Failed to prepare or enable clock, err=%d\n", ret);
  109. return ret;
  110. }
  111. phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
  112. if (phy_np) {
  113. data->phy_np = phy_np;
  114. phy_pdev = of_find_device_by_node(phy_np);
  115. if (phy_pdev) {
  116. struct usb_phy *phy;
  117. phy = pdev_to_phy(phy_pdev);
  118. if (phy &&
  119. try_module_get(phy_pdev->dev.driver->owner)) {
  120. usb_phy_init(phy);
  121. data->phy = phy;
  122. }
  123. }
  124. }
  125. /* we only support host now, so enable vbus here */
  126. data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
  127. if (!IS_ERR(data->reg_vbus)) {
  128. ret = regulator_enable(data->reg_vbus);
  129. if (ret) {
  130. dev_err(&pdev->dev,
  131. "Failed to enable vbus regulator, err=%d\n",
  132. ret);
  133. goto put_np;
  134. }
  135. } else {
  136. data->reg_vbus = NULL;
  137. }
  138. ci13xxx_imx_platdata.phy = data->phy;
  139. if (!pdev->dev.dma_mask)
  140. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  141. if (!pdev->dev.coherent_dma_mask)
  142. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  143. if (usbmisc_ops && usbmisc_ops->init) {
  144. ret = usbmisc_ops->init(&pdev->dev);
  145. if (ret) {
  146. dev_err(&pdev->dev,
  147. "usbmisc init failed, ret=%d\n", ret);
  148. goto err;
  149. }
  150. }
  151. data->ci_pdev = ci13xxx_add_device(&pdev->dev,
  152. pdev->resource, pdev->num_resources,
  153. &ci13xxx_imx_platdata);
  154. if (IS_ERR(data->ci_pdev)) {
  155. ret = PTR_ERR(data->ci_pdev);
  156. dev_err(&pdev->dev,
  157. "Can't register ci_hdrc platform device, err=%d\n",
  158. ret);
  159. goto err;
  160. }
  161. if (usbmisc_ops && usbmisc_ops->post) {
  162. ret = usbmisc_ops->post(&pdev->dev);
  163. if (ret) {
  164. dev_err(&pdev->dev,
  165. "usbmisc post failed, ret=%d\n", ret);
  166. goto disable_device;
  167. }
  168. }
  169. platform_set_drvdata(pdev, data);
  170. pm_runtime_no_callbacks(&pdev->dev);
  171. pm_runtime_enable(&pdev->dev);
  172. return 0;
  173. disable_device:
  174. ci13xxx_remove_device(data->ci_pdev);
  175. err:
  176. if (data->reg_vbus)
  177. regulator_disable(data->reg_vbus);
  178. put_np:
  179. if (phy_np)
  180. of_node_put(phy_np);
  181. clk_disable_unprepare(data->clk);
  182. return ret;
  183. }
  184. static int ci13xxx_imx_remove(struct platform_device *pdev)
  185. {
  186. struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
  187. pm_runtime_disable(&pdev->dev);
  188. ci13xxx_remove_device(data->ci_pdev);
  189. if (data->reg_vbus)
  190. regulator_disable(data->reg_vbus);
  191. if (data->phy) {
  192. usb_phy_shutdown(data->phy);
  193. module_put(data->phy->dev->driver->owner);
  194. }
  195. if (data->phy_np)
  196. of_node_put(data->phy_np);
  197. clk_disable_unprepare(data->clk);
  198. return 0;
  199. }
  200. static const struct of_device_id ci13xxx_imx_dt_ids[] = {
  201. { .compatible = "fsl,imx27-usb", },
  202. { /* sentinel */ }
  203. };
  204. MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
  205. static struct platform_driver ci13xxx_imx_driver = {
  206. .probe = ci13xxx_imx_probe,
  207. .remove = ci13xxx_imx_remove,
  208. .driver = {
  209. .name = "imx_usb",
  210. .owner = THIS_MODULE,
  211. .of_match_table = ci13xxx_imx_dt_ids,
  212. },
  213. };
  214. module_platform_driver(ci13xxx_imx_driver);
  215. MODULE_ALIAS("platform:imx-usb");
  216. MODULE_LICENSE("GPL v2");
  217. MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
  218. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  219. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");