ohci-st.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * ST OHCI driver
  3. *
  4. * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
  5. *
  6. * Author: Peter Griffin <peter.griffin@linaro.org>
  7. *
  8. * Derived from ohci-platform.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/err.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reset.h>
  24. #include <linux/usb/ohci_pdriver.h>
  25. #include <linux/usb.h>
  26. #include <linux/usb/hcd.h>
  27. #include "ohci.h"
  28. #define USB_MAX_CLKS 3
  29. struct st_ohci_platform_priv {
  30. struct clk *clks[USB_MAX_CLKS];
  31. struct clk *clk48;
  32. struct reset_control *rst;
  33. struct reset_control *pwr;
  34. struct phy *phy;
  35. };
  36. #define DRIVER_DESC "OHCI STMicroelectronics driver"
  37. #define hcd_to_ohci_priv(h) \
  38. ((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
  39. static const char hcd_name[] = "ohci-st";
  40. static int st_ohci_platform_power_on(struct platform_device *dev)
  41. {
  42. struct usb_hcd *hcd = platform_get_drvdata(dev);
  43. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  44. int clk, ret;
  45. ret = reset_control_deassert(priv->pwr);
  46. if (ret)
  47. return ret;
  48. ret = reset_control_deassert(priv->rst);
  49. if (ret)
  50. goto err_assert_power;
  51. /* some SoCs don't have a dedicated 48Mhz clock, but those that do
  52. need the rate to be explicitly set */
  53. if (priv->clk48) {
  54. ret = clk_set_rate(priv->clk48, 48000000);
  55. if (ret)
  56. goto err_assert_reset;
  57. }
  58. for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
  59. ret = clk_prepare_enable(priv->clks[clk]);
  60. if (ret)
  61. goto err_disable_clks;
  62. }
  63. ret = phy_init(priv->phy);
  64. if (ret)
  65. goto err_disable_clks;
  66. ret = phy_power_on(priv->phy);
  67. if (ret)
  68. goto err_exit_phy;
  69. return 0;
  70. err_exit_phy:
  71. phy_exit(priv->phy);
  72. err_disable_clks:
  73. while (--clk >= 0)
  74. clk_disable_unprepare(priv->clks[clk]);
  75. err_assert_reset:
  76. reset_control_assert(priv->rst);
  77. err_assert_power:
  78. reset_control_assert(priv->pwr);
  79. return ret;
  80. }
  81. static void st_ohci_platform_power_off(struct platform_device *dev)
  82. {
  83. struct usb_hcd *hcd = platform_get_drvdata(dev);
  84. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  85. int clk;
  86. reset_control_assert(priv->pwr);
  87. reset_control_assert(priv->rst);
  88. phy_power_off(priv->phy);
  89. phy_exit(priv->phy);
  90. for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
  91. if (priv->clks[clk])
  92. clk_disable_unprepare(priv->clks[clk]);
  93. }
  94. static struct hc_driver __read_mostly ohci_platform_hc_driver;
  95. static const struct ohci_driver_overrides platform_overrides __initconst = {
  96. .product_desc = "ST OHCI controller",
  97. .extra_priv_size = sizeof(struct st_ohci_platform_priv),
  98. };
  99. static struct usb_ohci_pdata ohci_platform_defaults = {
  100. .power_on = st_ohci_platform_power_on,
  101. .power_suspend = st_ohci_platform_power_off,
  102. .power_off = st_ohci_platform_power_off,
  103. };
  104. static int st_ohci_platform_probe(struct platform_device *dev)
  105. {
  106. struct usb_hcd *hcd;
  107. struct resource *res_mem;
  108. struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
  109. struct st_ohci_platform_priv *priv;
  110. struct ohci_hcd *ohci;
  111. int err, irq, clk = 0;
  112. if (usb_disabled())
  113. return -ENODEV;
  114. irq = platform_get_irq(dev, 0);
  115. if (irq < 0) {
  116. dev_err(&dev->dev, "no irq provided");
  117. return irq;
  118. }
  119. res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  120. if (!res_mem) {
  121. dev_err(&dev->dev, "no memory resource provided");
  122. return -ENXIO;
  123. }
  124. hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
  125. dev_name(&dev->dev));
  126. if (!hcd)
  127. return -ENOMEM;
  128. platform_set_drvdata(dev, hcd);
  129. dev->dev.platform_data = pdata;
  130. priv = hcd_to_ohci_priv(hcd);
  131. ohci = hcd_to_ohci(hcd);
  132. priv->phy = devm_phy_get(&dev->dev, "usb");
  133. if (IS_ERR(priv->phy)) {
  134. err = PTR_ERR(priv->phy);
  135. goto err_put_hcd;
  136. }
  137. for (clk = 0; clk < USB_MAX_CLKS; clk++) {
  138. priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
  139. if (IS_ERR(priv->clks[clk])) {
  140. err = PTR_ERR(priv->clks[clk]);
  141. if (err == -EPROBE_DEFER)
  142. goto err_put_clks;
  143. priv->clks[clk] = NULL;
  144. break;
  145. }
  146. }
  147. /* some SoCs don't have a dedicated 48Mhz clock, but those that
  148. do need the rate to be explicitly set */
  149. priv->clk48 = devm_clk_get(&dev->dev, "clk48");
  150. if (IS_ERR(priv->clk48)) {
  151. dev_info(&dev->dev, "48MHz clk not found\n");
  152. priv->clk48 = NULL;
  153. }
  154. priv->pwr = devm_reset_control_get_optional(&dev->dev, "power");
  155. if (IS_ERR(priv->pwr)) {
  156. err = PTR_ERR(priv->pwr);
  157. goto err_put_clks;
  158. }
  159. priv->rst = devm_reset_control_get_optional(&dev->dev, "softreset");
  160. if (IS_ERR(priv->rst)) {
  161. err = PTR_ERR(priv->rst);
  162. goto err_put_clks;
  163. }
  164. if (pdata->power_on) {
  165. err = pdata->power_on(dev);
  166. if (err < 0)
  167. goto err_power;
  168. }
  169. hcd->rsrc_start = res_mem->start;
  170. hcd->rsrc_len = resource_size(res_mem);
  171. hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
  172. if (IS_ERR(hcd->regs)) {
  173. err = PTR_ERR(hcd->regs);
  174. goto err_power;
  175. }
  176. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  177. if (err)
  178. goto err_power;
  179. device_wakeup_enable(hcd->self.controller);
  180. platform_set_drvdata(dev, hcd);
  181. return err;
  182. err_power:
  183. if (pdata->power_off)
  184. pdata->power_off(dev);
  185. err_put_clks:
  186. while (--clk >= 0)
  187. clk_put(priv->clks[clk]);
  188. err_put_hcd:
  189. if (pdata == &ohci_platform_defaults)
  190. dev->dev.platform_data = NULL;
  191. usb_put_hcd(hcd);
  192. return err;
  193. }
  194. static int st_ohci_platform_remove(struct platform_device *dev)
  195. {
  196. struct usb_hcd *hcd = platform_get_drvdata(dev);
  197. struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
  198. struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  199. int clk;
  200. usb_remove_hcd(hcd);
  201. if (pdata->power_off)
  202. pdata->power_off(dev);
  203. for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
  204. clk_put(priv->clks[clk]);
  205. usb_put_hcd(hcd);
  206. if (pdata == &ohci_platform_defaults)
  207. dev->dev.platform_data = NULL;
  208. return 0;
  209. }
  210. #ifdef CONFIG_PM_SLEEP
  211. static int st_ohci_suspend(struct device *dev)
  212. {
  213. struct usb_hcd *hcd = dev_get_drvdata(dev);
  214. struct usb_ohci_pdata *pdata = dev->platform_data;
  215. struct platform_device *pdev =
  216. container_of(dev, struct platform_device, dev);
  217. bool do_wakeup = device_may_wakeup(dev);
  218. int ret;
  219. ret = ohci_suspend(hcd, do_wakeup);
  220. if (ret)
  221. return ret;
  222. if (pdata->power_suspend)
  223. pdata->power_suspend(pdev);
  224. return ret;
  225. }
  226. static int st_ohci_resume(struct device *dev)
  227. {
  228. struct usb_hcd *hcd = dev_get_drvdata(dev);
  229. struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
  230. struct platform_device *pdev =
  231. container_of(dev, struct platform_device, dev);
  232. int err;
  233. if (pdata->power_on) {
  234. err = pdata->power_on(pdev);
  235. if (err < 0)
  236. return err;
  237. }
  238. ohci_resume(hcd, false);
  239. return 0;
  240. }
  241. static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
  242. #endif /* CONFIG_PM_SLEEP */
  243. static const struct of_device_id st_ohci_platform_ids[] = {
  244. { .compatible = "st,st-ohci-300x", },
  245. { /* sentinel */ }
  246. };
  247. MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
  248. static struct platform_driver ohci_platform_driver = {
  249. .probe = st_ohci_platform_probe,
  250. .remove = st_ohci_platform_remove,
  251. .shutdown = usb_hcd_platform_shutdown,
  252. .driver = {
  253. .name = "st-ohci",
  254. #ifdef CONFIG_PM_SLEEP
  255. .pm = &st_ohci_pm_ops,
  256. #endif
  257. .of_match_table = st_ohci_platform_ids,
  258. }
  259. };
  260. static int __init ohci_platform_init(void)
  261. {
  262. if (usb_disabled())
  263. return -ENODEV;
  264. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  265. ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
  266. return platform_driver_register(&ohci_platform_driver);
  267. }
  268. module_init(ohci_platform_init);
  269. static void __exit ohci_platform_cleanup(void)
  270. {
  271. platform_driver_unregister(&ohci_platform_driver);
  272. }
  273. module_exit(ohci_platform_cleanup);
  274. MODULE_DESCRIPTION(DRIVER_DESC);
  275. MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
  276. MODULE_LICENSE("GPL");