ci13xxx_imx.c 6.5 KB

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