phy-omap-usb2.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include <linux/phy/phy.h>
  31. #include <linux/of_platform.h>
  32. /**
  33. * omap_usb2_set_comparator - links the comparator present in the sytem with
  34. * this phy
  35. * @comparator - the companion phy(comparator) for this phy
  36. *
  37. * The phy companion driver should call this API passing the phy_companion
  38. * filled with set_vbus and start_srp to be used by usb phy.
  39. *
  40. * For use by phy companion driver
  41. */
  42. int omap_usb2_set_comparator(struct phy_companion *comparator)
  43. {
  44. struct omap_usb *phy;
  45. struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
  46. if (IS_ERR(x))
  47. return -ENODEV;
  48. phy = phy_to_omapusb(x);
  49. phy->comparator = comparator;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
  53. static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
  54. {
  55. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  56. if (!phy->comparator)
  57. return -ENODEV;
  58. return phy->comparator->set_vbus(phy->comparator, enabled);
  59. }
  60. static int omap_usb_start_srp(struct usb_otg *otg)
  61. {
  62. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  63. if (!phy->comparator)
  64. return -ENODEV;
  65. return phy->comparator->start_srp(phy->comparator);
  66. }
  67. static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  68. {
  69. struct usb_phy *phy = otg->phy;
  70. otg->host = host;
  71. if (!host)
  72. phy->state = OTG_STATE_UNDEFINED;
  73. return 0;
  74. }
  75. static int omap_usb_set_peripheral(struct usb_otg *otg,
  76. struct usb_gadget *gadget)
  77. {
  78. struct usb_phy *phy = otg->phy;
  79. otg->gadget = gadget;
  80. if (!gadget)
  81. phy->state = OTG_STATE_UNDEFINED;
  82. return 0;
  83. }
  84. static int omap_usb2_suspend(struct usb_phy *x, int suspend)
  85. {
  86. struct omap_usb *phy = phy_to_omapusb(x);
  87. int ret;
  88. if (suspend && !phy->is_suspended) {
  89. omap_control_usb_phy_power(phy->control_dev, 0);
  90. pm_runtime_put_sync(phy->dev);
  91. phy->is_suspended = 1;
  92. } else if (!suspend && phy->is_suspended) {
  93. ret = pm_runtime_get_sync(phy->dev);
  94. if (ret < 0) {
  95. dev_err(phy->dev, "get_sync failed with err %d\n", ret);
  96. return ret;
  97. }
  98. omap_control_usb_phy_power(phy->control_dev, 1);
  99. phy->is_suspended = 0;
  100. }
  101. return 0;
  102. }
  103. static int omap_usb_power_off(struct phy *x)
  104. {
  105. struct omap_usb *phy = phy_get_drvdata(x);
  106. omap_control_usb_phy_power(phy->control_dev, 0);
  107. return 0;
  108. }
  109. static int omap_usb_power_on(struct phy *x)
  110. {
  111. struct omap_usb *phy = phy_get_drvdata(x);
  112. omap_control_usb_phy_power(phy->control_dev, 1);
  113. return 0;
  114. }
  115. static struct phy_ops ops = {
  116. .power_on = omap_usb_power_on,
  117. .power_off = omap_usb_power_off,
  118. .owner = THIS_MODULE,
  119. };
  120. static int omap_usb2_probe(struct platform_device *pdev)
  121. {
  122. struct omap_usb *phy;
  123. struct phy *generic_phy;
  124. struct phy_provider *phy_provider;
  125. struct usb_otg *otg;
  126. struct device_node *node = pdev->dev.of_node;
  127. struct device_node *control_node;
  128. struct platform_device *control_pdev;
  129. if (!node)
  130. return -EINVAL;
  131. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  132. if (!phy) {
  133. dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
  134. return -ENOMEM;
  135. }
  136. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  137. if (!otg) {
  138. dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
  139. return -ENOMEM;
  140. }
  141. phy->dev = &pdev->dev;
  142. phy->phy.dev = phy->dev;
  143. phy->phy.label = "omap-usb2";
  144. phy->phy.set_suspend = omap_usb2_suspend;
  145. phy->phy.otg = otg;
  146. phy->phy.type = USB_PHY_TYPE_USB2;
  147. phy_provider = devm_of_phy_provider_register(phy->dev,
  148. of_phy_simple_xlate);
  149. if (IS_ERR(phy_provider))
  150. return PTR_ERR(phy_provider);
  151. control_node = of_parse_phandle(node, "ctrl-module", 0);
  152. if (!control_node) {
  153. dev_err(&pdev->dev, "Failed to get control device phandle\n");
  154. return -EINVAL;
  155. }
  156. control_pdev = of_find_device_by_node(control_node);
  157. if (!control_pdev) {
  158. dev_err(&pdev->dev, "Failed to get control device\n");
  159. return -EINVAL;
  160. }
  161. phy->control_dev = &control_pdev->dev;
  162. phy->is_suspended = 1;
  163. omap_control_usb_phy_power(phy->control_dev, 0);
  164. otg->set_host = omap_usb_set_host;
  165. otg->set_peripheral = omap_usb_set_peripheral;
  166. otg->set_vbus = omap_usb_set_vbus;
  167. otg->start_srp = omap_usb_start_srp;
  168. otg->phy = &phy->phy;
  169. platform_set_drvdata(pdev, phy);
  170. pm_runtime_enable(phy->dev);
  171. generic_phy = devm_phy_create(phy->dev, &ops, NULL);
  172. if (IS_ERR(generic_phy))
  173. return PTR_ERR(generic_phy);
  174. phy_set_drvdata(generic_phy, phy);
  175. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  176. if (IS_ERR(phy->wkupclk)) {
  177. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  178. return PTR_ERR(phy->wkupclk);
  179. }
  180. clk_prepare(phy->wkupclk);
  181. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  182. if (IS_ERR(phy->optclk))
  183. dev_vdbg(&pdev->dev, "unable to get refclk960m\n");
  184. else
  185. clk_prepare(phy->optclk);
  186. usb_add_phy_dev(&phy->phy);
  187. return 0;
  188. }
  189. static int omap_usb2_remove(struct platform_device *pdev)
  190. {
  191. struct omap_usb *phy = platform_get_drvdata(pdev);
  192. clk_unprepare(phy->wkupclk);
  193. if (!IS_ERR(phy->optclk))
  194. clk_unprepare(phy->optclk);
  195. usb_remove_phy(&phy->phy);
  196. return 0;
  197. }
  198. #ifdef CONFIG_PM_RUNTIME
  199. static int omap_usb2_runtime_suspend(struct device *dev)
  200. {
  201. struct platform_device *pdev = to_platform_device(dev);
  202. struct omap_usb *phy = platform_get_drvdata(pdev);
  203. clk_disable(phy->wkupclk);
  204. if (!IS_ERR(phy->optclk))
  205. clk_disable(phy->optclk);
  206. return 0;
  207. }
  208. static int omap_usb2_runtime_resume(struct device *dev)
  209. {
  210. struct platform_device *pdev = to_platform_device(dev);
  211. struct omap_usb *phy = platform_get_drvdata(pdev);
  212. int ret;
  213. ret = clk_enable(phy->wkupclk);
  214. if (ret < 0) {
  215. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  216. goto err0;
  217. }
  218. if (!IS_ERR(phy->optclk)) {
  219. ret = clk_enable(phy->optclk);
  220. if (ret < 0) {
  221. dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
  222. goto err1;
  223. }
  224. }
  225. return 0;
  226. err1:
  227. clk_disable(phy->wkupclk);
  228. err0:
  229. return ret;
  230. }
  231. static const struct dev_pm_ops omap_usb2_pm_ops = {
  232. SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
  233. NULL)
  234. };
  235. #define DEV_PM_OPS (&omap_usb2_pm_ops)
  236. #else
  237. #define DEV_PM_OPS NULL
  238. #endif
  239. #ifdef CONFIG_OF
  240. static const struct of_device_id omap_usb2_id_table[] = {
  241. { .compatible = "ti,omap-usb2" },
  242. {}
  243. };
  244. MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
  245. #endif
  246. static struct platform_driver omap_usb2_driver = {
  247. .probe = omap_usb2_probe,
  248. .remove = omap_usb2_remove,
  249. .driver = {
  250. .name = "omap-usb2",
  251. .owner = THIS_MODULE,
  252. .pm = DEV_PM_OPS,
  253. .of_match_table = of_match_ptr(omap_usb2_id_table),
  254. },
  255. };
  256. module_platform_driver(omap_usb2_driver);
  257. MODULE_ALIAS("platform: omap_usb2");
  258. MODULE_AUTHOR("Texas Instruments Inc.");
  259. MODULE_DESCRIPTION("OMAP USB2 phy driver");
  260. MODULE_LICENSE("GPL v2");