ci13xxx_imx.c 6.1 KB

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