omap-usb2.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /**
  30. * omap_usb2_set_comparator - links the comparator present in the sytem with
  31. * this phy
  32. * @comparator - the companion phy(comparator) for this phy
  33. *
  34. * The phy companion driver should call this API passing the phy_companion
  35. * filled with set_vbus and start_srp to be used by usb phy.
  36. *
  37. * For use by phy companion driver
  38. */
  39. int omap_usb2_set_comparator(struct phy_companion *comparator)
  40. {
  41. struct omap_usb *phy;
  42. struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
  43. if (IS_ERR(x))
  44. return -ENODEV;
  45. phy = phy_to_omapusb(x);
  46. phy->comparator = comparator;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
  50. /**
  51. * omap_usb_phy_power - power on/off the phy using control module reg
  52. * @phy: struct omap_usb *
  53. * @on: 0 or 1, based on powering on or off the PHY
  54. *
  55. * XXX: Remove this function once control module driver gets merged
  56. */
  57. static void omap_usb_phy_power(struct omap_usb *phy, int on)
  58. {
  59. u32 val;
  60. if (on) {
  61. val = readl(phy->control_dev);
  62. if (val & PHY_PD) {
  63. writel(~PHY_PD, phy->control_dev);
  64. /* XXX: add proper documentation for this delay */
  65. mdelay(200);
  66. }
  67. } else {
  68. writel(PHY_PD, phy->control_dev);
  69. }
  70. }
  71. static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
  72. {
  73. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  74. if (!phy->comparator)
  75. return -ENODEV;
  76. return phy->comparator->set_vbus(phy->comparator, enabled);
  77. }
  78. static int omap_usb_start_srp(struct usb_otg *otg)
  79. {
  80. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  81. if (!phy->comparator)
  82. return -ENODEV;
  83. return phy->comparator->start_srp(phy->comparator);
  84. }
  85. static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  86. {
  87. struct usb_phy *phy = otg->phy;
  88. otg->host = host;
  89. if (!host)
  90. phy->state = OTG_STATE_UNDEFINED;
  91. return 0;
  92. }
  93. static int omap_usb_set_peripheral(struct usb_otg *otg,
  94. struct usb_gadget *gadget)
  95. {
  96. struct usb_phy *phy = otg->phy;
  97. otg->gadget = gadget;
  98. if (!gadget)
  99. phy->state = OTG_STATE_UNDEFINED;
  100. return 0;
  101. }
  102. static int omap_usb2_suspend(struct usb_phy *x, int suspend)
  103. {
  104. u32 ret;
  105. struct omap_usb *phy = phy_to_omapusb(x);
  106. if (suspend && !phy->is_suspended) {
  107. omap_usb_phy_power(phy, 0);
  108. pm_runtime_put_sync(phy->dev);
  109. phy->is_suspended = 1;
  110. } else if (!suspend && phy->is_suspended) {
  111. ret = pm_runtime_get_sync(phy->dev);
  112. if (ret < 0) {
  113. dev_err(phy->dev, "get_sync failed with err %d\n",
  114. ret);
  115. return ret;
  116. }
  117. omap_usb_phy_power(phy, 1);
  118. phy->is_suspended = 0;
  119. }
  120. return 0;
  121. }
  122. static int omap_usb2_probe(struct platform_device *pdev)
  123. {
  124. struct omap_usb *phy;
  125. struct usb_otg *otg;
  126. struct resource *res;
  127. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  128. if (!phy) {
  129. dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
  130. return -ENOMEM;
  131. }
  132. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  133. if (!otg) {
  134. dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
  135. return -ENOMEM;
  136. }
  137. phy->dev = &pdev->dev;
  138. phy->phy.dev = phy->dev;
  139. phy->phy.label = "omap-usb2";
  140. phy->phy.set_suspend = omap_usb2_suspend;
  141. phy->phy.otg = otg;
  142. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  143. phy->control_dev = devm_request_and_ioremap(&pdev->dev, res);
  144. if (phy->control_dev == NULL) {
  145. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  146. return -ENXIO;
  147. }
  148. phy->is_suspended = 1;
  149. omap_usb_phy_power(phy, 0);
  150. otg->set_host = omap_usb_set_host;
  151. otg->set_peripheral = omap_usb_set_peripheral;
  152. otg->set_vbus = omap_usb_set_vbus;
  153. otg->start_srp = omap_usb_start_srp;
  154. otg->phy = &phy->phy;
  155. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  156. if (IS_ERR(phy->wkupclk)) {
  157. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  158. return PTR_ERR(phy->wkupclk);
  159. }
  160. clk_prepare(phy->wkupclk);
  161. usb_add_phy(&phy->phy, USB_PHY_TYPE_USB2);
  162. platform_set_drvdata(pdev, phy);
  163. pm_runtime_enable(phy->dev);
  164. return 0;
  165. }
  166. static int omap_usb2_remove(struct platform_device *pdev)
  167. {
  168. struct omap_usb *phy = platform_get_drvdata(pdev);
  169. clk_unprepare(phy->wkupclk);
  170. usb_remove_phy(&phy->phy);
  171. return 0;
  172. }
  173. #ifdef CONFIG_PM_RUNTIME
  174. static int omap_usb2_runtime_suspend(struct device *dev)
  175. {
  176. struct platform_device *pdev = to_platform_device(dev);
  177. struct omap_usb *phy = platform_get_drvdata(pdev);
  178. clk_disable(phy->wkupclk);
  179. return 0;
  180. }
  181. static int omap_usb2_runtime_resume(struct device *dev)
  182. {
  183. u32 ret = 0;
  184. struct platform_device *pdev = to_platform_device(dev);
  185. struct omap_usb *phy = platform_get_drvdata(pdev);
  186. ret = clk_enable(phy->wkupclk);
  187. if (ret < 0)
  188. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  189. return ret;
  190. }
  191. static const struct dev_pm_ops omap_usb2_pm_ops = {
  192. SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
  193. NULL)
  194. };
  195. #define DEV_PM_OPS (&omap_usb2_pm_ops)
  196. #else
  197. #define DEV_PM_OPS NULL
  198. #endif
  199. #ifdef CONFIG_OF
  200. static const struct of_device_id omap_usb2_id_table[] = {
  201. { .compatible = "ti,omap-usb2" },
  202. {}
  203. };
  204. MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
  205. #endif
  206. static struct platform_driver omap_usb2_driver = {
  207. .probe = omap_usb2_probe,
  208. .remove = omap_usb2_remove,
  209. .driver = {
  210. .name = "omap-usb2",
  211. .owner = THIS_MODULE,
  212. .pm = DEV_PM_OPS,
  213. .of_match_table = of_match_ptr(omap_usb2_id_table),
  214. },
  215. };
  216. module_platform_driver(omap_usb2_driver);
  217. MODULE_ALIAS("platform: omap_usb2");
  218. MODULE_AUTHOR("Texas Instruments Inc.");
  219. MODULE_DESCRIPTION("OMAP USB2 phy driver");
  220. MODULE_LICENSE("GPL v2");