ci13xxx_imx.c 6.2 KB

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