ci13xxx_imx.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #define pdev_to_phy(pdev) \
  24. ((struct usb_phy *)platform_get_drvdata(pdev))
  25. struct ci13xxx_imx_data {
  26. struct device_node *phy_np;
  27. struct usb_phy *phy;
  28. struct platform_device *ci_pdev;
  29. struct clk *clk;
  30. struct regulator *reg_vbus;
  31. };
  32. static struct ci13xxx_platform_data ci13xxx_imx_platdata __devinitdata = {
  33. .name = "ci13xxx_imx",
  34. .flags = CI13XXX_REQUIRE_TRANSCEIVER |
  35. CI13XXX_PULLUP_ON_VBUS |
  36. CI13XXX_DISABLE_STREAMING,
  37. .capoffset = DEF_CAPOFFSET,
  38. };
  39. static int __devinit ci13xxx_imx_probe(struct platform_device *pdev)
  40. {
  41. struct ci13xxx_imx_data *data;
  42. struct platform_device *plat_ci, *phy_pdev;
  43. struct device_node *phy_np;
  44. struct resource *res;
  45. struct regulator *reg_vbus;
  46. int ret;
  47. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  48. if (!data) {
  49. dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
  50. return -ENOMEM;
  51. }
  52. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  53. if (!res) {
  54. dev_err(&pdev->dev, "Can't get device resources!\n");
  55. return -ENOENT;
  56. }
  57. data->clk = devm_clk_get(&pdev->dev, NULL);
  58. if (IS_ERR(data->clk)) {
  59. dev_err(&pdev->dev,
  60. "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
  61. return PTR_ERR(data->clk);
  62. }
  63. ret = clk_prepare_enable(data->clk);
  64. if (ret) {
  65. dev_err(&pdev->dev,
  66. "Failed to prepare or enable clock, err=%d\n", ret);
  67. return ret;
  68. }
  69. phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
  70. if (phy_np) {
  71. data->phy_np = phy_np;
  72. phy_pdev = of_find_device_by_node(phy_np);
  73. if (phy_pdev) {
  74. struct usb_phy *phy;
  75. phy = pdev_to_phy(phy_pdev);
  76. if (phy &&
  77. try_module_get(phy_pdev->dev.driver->owner)) {
  78. usb_phy_init(phy);
  79. data->phy = phy;
  80. }
  81. }
  82. }
  83. /* we only support host now, so enable vbus here */
  84. reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
  85. if (!IS_ERR(reg_vbus)) {
  86. ret = regulator_enable(reg_vbus);
  87. if (ret) {
  88. dev_err(&pdev->dev,
  89. "Failed to enable vbus regulator, err=%d\n",
  90. ret);
  91. goto put_np;
  92. }
  93. data->reg_vbus = reg_vbus;
  94. } else {
  95. reg_vbus = NULL;
  96. }
  97. ci13xxx_imx_platdata.phy = data->phy;
  98. if (!pdev->dev.dma_mask) {
  99. pdev->dev.dma_mask = devm_kzalloc(&pdev->dev,
  100. sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
  101. if (!pdev->dev.dma_mask) {
  102. ret = -ENOMEM;
  103. dev_err(&pdev->dev, "Failed to alloc dma_mask!\n");
  104. goto err;
  105. }
  106. *pdev->dev.dma_mask = DMA_BIT_MASK(32);
  107. dma_set_coherent_mask(&pdev->dev, *pdev->dev.dma_mask);
  108. }
  109. plat_ci = ci13xxx_add_device(&pdev->dev,
  110. pdev->resource, pdev->num_resources,
  111. &ci13xxx_imx_platdata);
  112. if (IS_ERR(plat_ci)) {
  113. ret = PTR_ERR(plat_ci);
  114. dev_err(&pdev->dev,
  115. "Can't register ci_hdrc platform device, err=%d\n",
  116. ret);
  117. goto err;
  118. }
  119. data->ci_pdev = plat_ci;
  120. platform_set_drvdata(pdev, data);
  121. pm_runtime_no_callbacks(&pdev->dev);
  122. pm_runtime_enable(&pdev->dev);
  123. return 0;
  124. err:
  125. if (reg_vbus)
  126. regulator_disable(reg_vbus);
  127. put_np:
  128. if (phy_np)
  129. of_node_put(phy_np);
  130. clk_disable_unprepare(data->clk);
  131. return ret;
  132. }
  133. static int __devexit ci13xxx_imx_remove(struct platform_device *pdev)
  134. {
  135. struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
  136. pm_runtime_disable(&pdev->dev);
  137. ci13xxx_remove_device(data->ci_pdev);
  138. if (data->reg_vbus)
  139. regulator_disable(data->reg_vbus);
  140. if (data->phy) {
  141. usb_phy_shutdown(data->phy);
  142. module_put(data->phy->dev->driver->owner);
  143. }
  144. of_node_put(data->phy_np);
  145. clk_disable_unprepare(data->clk);
  146. platform_set_drvdata(pdev, NULL);
  147. return 0;
  148. }
  149. static const struct of_device_id ci13xxx_imx_dt_ids[] = {
  150. { .compatible = "fsl,imx27-usb", },
  151. { /* sentinel */ }
  152. };
  153. MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
  154. static struct platform_driver ci13xxx_imx_driver = {
  155. .probe = ci13xxx_imx_probe,
  156. .remove = __devexit_p(ci13xxx_imx_remove),
  157. .driver = {
  158. .name = "imx_usb",
  159. .owner = THIS_MODULE,
  160. .of_match_table = ci13xxx_imx_dt_ids,
  161. },
  162. };
  163. module_platform_driver(ci13xxx_imx_driver);
  164. MODULE_ALIAS("platform:imx-usb");
  165. MODULE_LICENSE("GPL v2");
  166. MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
  167. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  168. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");