omap-usb2.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * omap-usb2.c - USB PHY, talking to musb controller in OMAP.
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/io.h>
  23. #include <linux/usb/omap_usb.h>
  24. #include <linux/usb/phy_companion.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/delay.h>
  29. #include <linux/usb/omap_control_usb.h>
  30. /**
  31. * omap_usb2_set_comparator - links the comparator present in the sytem with
  32. * this phy
  33. * @comparator - the companion phy(comparator) for this phy
  34. *
  35. * The phy companion driver should call this API passing the phy_companion
  36. * filled with set_vbus and start_srp to be used by usb phy.
  37. *
  38. * For use by phy companion driver
  39. */
  40. int omap_usb2_set_comparator(struct phy_companion *comparator)
  41. {
  42. struct omap_usb *phy;
  43. struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
  44. if (IS_ERR(x))
  45. return -ENODEV;
  46. phy = phy_to_omapusb(x);
  47. phy->comparator = comparator;
  48. return 0;
  49. }
  50. EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
  51. static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
  52. {
  53. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  54. if (!phy->comparator)
  55. return -ENODEV;
  56. return phy->comparator->set_vbus(phy->comparator, enabled);
  57. }
  58. static int omap_usb_start_srp(struct usb_otg *otg)
  59. {
  60. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  61. if (!phy->comparator)
  62. return -ENODEV;
  63. return phy->comparator->start_srp(phy->comparator);
  64. }
  65. static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  66. {
  67. struct usb_phy *phy = otg->phy;
  68. otg->host = host;
  69. if (!host)
  70. phy->state = OTG_STATE_UNDEFINED;
  71. return 0;
  72. }
  73. static int omap_usb_set_peripheral(struct usb_otg *otg,
  74. struct usb_gadget *gadget)
  75. {
  76. struct usb_phy *phy = otg->phy;
  77. otg->gadget = gadget;
  78. if (!gadget)
  79. phy->state = OTG_STATE_UNDEFINED;
  80. return 0;
  81. }
  82. static int omap_usb2_suspend(struct usb_phy *x, int suspend)
  83. {
  84. u32 ret;
  85. struct omap_usb *phy = phy_to_omapusb(x);
  86. if (suspend && !phy->is_suspended) {
  87. omap_control_usb_phy_power(phy->control_dev, 0);
  88. pm_runtime_put_sync(phy->dev);
  89. phy->is_suspended = 1;
  90. } else if (!suspend && phy->is_suspended) {
  91. ret = pm_runtime_get_sync(phy->dev);
  92. if (ret < 0) {
  93. dev_err(phy->dev, "get_sync failed with err %d\n",
  94. ret);
  95. return ret;
  96. }
  97. omap_control_usb_phy_power(phy->control_dev, 1);
  98. phy->is_suspended = 0;
  99. }
  100. return 0;
  101. }
  102. static int omap_usb2_probe(struct platform_device *pdev)
  103. {
  104. struct omap_usb *phy;
  105. struct usb_otg *otg;
  106. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  107. if (!phy) {
  108. dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
  109. return -ENOMEM;
  110. }
  111. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  112. if (!otg) {
  113. dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
  114. return -ENOMEM;
  115. }
  116. phy->dev = &pdev->dev;
  117. phy->phy.dev = phy->dev;
  118. phy->phy.label = "omap-usb2";
  119. phy->phy.set_suspend = omap_usb2_suspend;
  120. phy->phy.otg = otg;
  121. phy->phy.type = USB_PHY_TYPE_USB2;
  122. phy->control_dev = omap_get_control_dev();
  123. if (IS_ERR(phy->control_dev)) {
  124. dev_dbg(&pdev->dev, "Failed to get control device\n");
  125. return -ENODEV;
  126. }
  127. phy->is_suspended = 1;
  128. omap_control_usb_phy_power(phy->control_dev, 0);
  129. otg->set_host = omap_usb_set_host;
  130. otg->set_peripheral = omap_usb_set_peripheral;
  131. otg->set_vbus = omap_usb_set_vbus;
  132. otg->start_srp = omap_usb_start_srp;
  133. otg->phy = &phy->phy;
  134. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  135. if (IS_ERR(phy->wkupclk)) {
  136. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  137. return PTR_ERR(phy->wkupclk);
  138. }
  139. clk_prepare(phy->wkupclk);
  140. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  141. if (IS_ERR(phy->optclk))
  142. dev_vdbg(&pdev->dev, "unable to get refclk960m\n");
  143. else
  144. clk_prepare(phy->optclk);
  145. usb_add_phy_dev(&phy->phy);
  146. platform_set_drvdata(pdev, phy);
  147. pm_runtime_enable(phy->dev);
  148. return 0;
  149. }
  150. static int omap_usb2_remove(struct platform_device *pdev)
  151. {
  152. struct omap_usb *phy = platform_get_drvdata(pdev);
  153. clk_unprepare(phy->wkupclk);
  154. if (!IS_ERR(phy->optclk))
  155. clk_unprepare(phy->optclk);
  156. usb_remove_phy(&phy->phy);
  157. return 0;
  158. }
  159. #ifdef CONFIG_PM_RUNTIME
  160. static int omap_usb2_runtime_suspend(struct device *dev)
  161. {
  162. struct platform_device *pdev = to_platform_device(dev);
  163. struct omap_usb *phy = platform_get_drvdata(pdev);
  164. clk_disable(phy->wkupclk);
  165. if (!IS_ERR(phy->optclk))
  166. clk_disable(phy->optclk);
  167. return 0;
  168. }
  169. static int omap_usb2_runtime_resume(struct device *dev)
  170. {
  171. u32 ret = 0;
  172. struct platform_device *pdev = to_platform_device(dev);
  173. struct omap_usb *phy = platform_get_drvdata(pdev);
  174. ret = clk_enable(phy->wkupclk);
  175. if (ret < 0) {
  176. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  177. goto err0;
  178. }
  179. if (!IS_ERR(phy->optclk)) {
  180. ret = clk_enable(phy->optclk);
  181. if (ret < 0) {
  182. dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
  183. goto err1;
  184. }
  185. }
  186. return 0;
  187. err1:
  188. clk_disable(phy->wkupclk);
  189. err0:
  190. return ret;
  191. }
  192. static const struct dev_pm_ops omap_usb2_pm_ops = {
  193. SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
  194. NULL)
  195. };
  196. #define DEV_PM_OPS (&omap_usb2_pm_ops)
  197. #else
  198. #define DEV_PM_OPS NULL
  199. #endif
  200. #ifdef CONFIG_OF
  201. static const struct of_device_id omap_usb2_id_table[] = {
  202. { .compatible = "ti,omap-usb2" },
  203. {}
  204. };
  205. MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
  206. #endif
  207. static struct platform_driver omap_usb2_driver = {
  208. .probe = omap_usb2_probe,
  209. .remove = omap_usb2_remove,
  210. .driver = {
  211. .name = "omap-usb2",
  212. .owner = THIS_MODULE,
  213. .pm = DEV_PM_OPS,
  214. .of_match_table = of_match_ptr(omap_usb2_id_table),
  215. },
  216. };
  217. module_platform_driver(omap_usb2_driver);
  218. MODULE_ALIAS("platform: omap_usb2");
  219. MODULE_AUTHOR("Texas Instruments Inc.");
  220. MODULE_DESCRIPTION("OMAP USB2 phy driver");
  221. MODULE_LICENSE("GPL v2");