phy-nop.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * drivers/usb/otg/nop-usb-xceiv.c
  3. *
  4. * NOP USB transceiver for all USB transceiver which are either built-in
  5. * into USB IP or which are mostly autonomous.
  6. *
  7. * Copyright (C) 2009 Texas Instruments Inc
  8. * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
  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 as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Current status:
  25. * This provides a "nop" transceiver for PHYs which are
  26. * autonomous such as isp1504, isp1707, etc.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/usb/otg.h>
  32. #include <linux/usb/nop-usb-xceiv.h>
  33. #include <linux/slab.h>
  34. #include <linux/clk.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/of.h>
  37. struct nop_usb_xceiv {
  38. struct usb_phy phy;
  39. struct device *dev;
  40. struct clk *clk;
  41. struct regulator *vcc;
  42. struct regulator *reset;
  43. };
  44. static struct platform_device *pd;
  45. void usb_nop_xceiv_register(void)
  46. {
  47. if (pd)
  48. return;
  49. pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0);
  50. if (!pd) {
  51. printk(KERN_ERR "Unable to register usb nop transceiver\n");
  52. return;
  53. }
  54. }
  55. EXPORT_SYMBOL(usb_nop_xceiv_register);
  56. void usb_nop_xceiv_unregister(void)
  57. {
  58. platform_device_unregister(pd);
  59. pd = NULL;
  60. }
  61. EXPORT_SYMBOL(usb_nop_xceiv_unregister);
  62. static int nop_set_suspend(struct usb_phy *x, int suspend)
  63. {
  64. return 0;
  65. }
  66. static int nop_init(struct usb_phy *phy)
  67. {
  68. struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev);
  69. if (!IS_ERR(nop->vcc)) {
  70. if (regulator_enable(nop->vcc))
  71. dev_err(phy->dev, "Failed to enable power\n");
  72. }
  73. if (!IS_ERR(nop->clk))
  74. clk_enable(nop->clk);
  75. if (!IS_ERR(nop->reset)) {
  76. /* De-assert RESET */
  77. if (regulator_enable(nop->reset))
  78. dev_err(phy->dev, "Failed to de-assert reset\n");
  79. }
  80. return 0;
  81. }
  82. static void nop_shutdown(struct usb_phy *phy)
  83. {
  84. struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev);
  85. if (!IS_ERR(nop->reset)) {
  86. /* Assert RESET */
  87. if (regulator_disable(nop->reset))
  88. dev_err(phy->dev, "Failed to assert reset\n");
  89. }
  90. if (!IS_ERR(nop->clk))
  91. clk_disable(nop->clk);
  92. if (!IS_ERR(nop->vcc)) {
  93. if (regulator_disable(nop->vcc))
  94. dev_err(phy->dev, "Failed to disable power\n");
  95. }
  96. }
  97. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  98. {
  99. if (!otg)
  100. return -ENODEV;
  101. if (!gadget) {
  102. otg->gadget = NULL;
  103. return -ENODEV;
  104. }
  105. otg->gadget = gadget;
  106. otg->phy->state = OTG_STATE_B_IDLE;
  107. return 0;
  108. }
  109. static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
  110. {
  111. if (!otg)
  112. return -ENODEV;
  113. if (!host) {
  114. otg->host = NULL;
  115. return -ENODEV;
  116. }
  117. otg->host = host;
  118. return 0;
  119. }
  120. static int nop_usb_xceiv_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. struct nop_usb_xceiv_platform_data *pdata = pdev->dev.platform_data;
  124. struct nop_usb_xceiv *nop;
  125. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  126. int err;
  127. u32 clk_rate = 0;
  128. bool needs_vcc = false;
  129. bool needs_reset = false;
  130. nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL);
  131. if (!nop)
  132. return -ENOMEM;
  133. nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg),
  134. GFP_KERNEL);
  135. if (!nop->phy.otg)
  136. return -ENOMEM;
  137. if (dev->of_node) {
  138. struct device_node *node = dev->of_node;
  139. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  140. clk_rate = 0;
  141. needs_vcc = of_property_read_bool(node, "vcc-supply");
  142. needs_reset = of_property_read_bool(node, "reset-supply");
  143. } else if (pdata) {
  144. type = pdata->type;
  145. clk_rate = pdata->clk_rate;
  146. needs_vcc = pdata->needs_vcc;
  147. needs_reset = pdata->needs_reset;
  148. }
  149. nop->clk = devm_clk_get(&pdev->dev, "main_clk");
  150. if (IS_ERR(nop->clk)) {
  151. dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n",
  152. PTR_ERR(nop->clk));
  153. }
  154. if (!IS_ERR(nop->clk) && clk_rate) {
  155. err = clk_set_rate(nop->clk, clk_rate);
  156. if (err) {
  157. dev_err(&pdev->dev, "Error setting clock rate\n");
  158. return err;
  159. }
  160. }
  161. if (!IS_ERR(nop->clk)) {
  162. err = clk_prepare(nop->clk);
  163. if (err) {
  164. dev_err(&pdev->dev, "Error preparing clock\n");
  165. return err;
  166. }
  167. }
  168. nop->vcc = devm_regulator_get(&pdev->dev, "vcc");
  169. if (IS_ERR(nop->vcc)) {
  170. dev_dbg(&pdev->dev, "Error getting vcc regulator: %ld\n",
  171. PTR_ERR(nop->vcc));
  172. if (needs_vcc)
  173. return -EPROBE_DEFER;
  174. }
  175. nop->reset = devm_regulator_get(&pdev->dev, "reset");
  176. if (IS_ERR(nop->reset)) {
  177. dev_dbg(&pdev->dev, "Error getting reset regulator: %ld\n",
  178. PTR_ERR(nop->reset));
  179. if (needs_reset)
  180. return -EPROBE_DEFER;
  181. }
  182. nop->dev = &pdev->dev;
  183. nop->phy.dev = nop->dev;
  184. nop->phy.label = "nop-xceiv";
  185. nop->phy.set_suspend = nop_set_suspend;
  186. nop->phy.init = nop_init;
  187. nop->phy.shutdown = nop_shutdown;
  188. nop->phy.state = OTG_STATE_UNDEFINED;
  189. nop->phy.type = type;
  190. nop->phy.otg->phy = &nop->phy;
  191. nop->phy.otg->set_host = nop_set_host;
  192. nop->phy.otg->set_peripheral = nop_set_peripheral;
  193. err = usb_add_phy_dev(&nop->phy);
  194. if (err) {
  195. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  196. err);
  197. goto err_add;
  198. }
  199. platform_set_drvdata(pdev, nop);
  200. ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
  201. return 0;
  202. err_add:
  203. if (!IS_ERR(nop->clk))
  204. clk_unprepare(nop->clk);
  205. return err;
  206. }
  207. static int nop_usb_xceiv_remove(struct platform_device *pdev)
  208. {
  209. struct nop_usb_xceiv *nop = platform_get_drvdata(pdev);
  210. if (!IS_ERR(nop->clk))
  211. clk_unprepare(nop->clk);
  212. usb_remove_phy(&nop->phy);
  213. return 0;
  214. }
  215. static const struct of_device_id nop_xceiv_dt_ids[] = {
  216. { .compatible = "usb-nop-xceiv" },
  217. { }
  218. };
  219. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  220. static struct platform_driver nop_usb_xceiv_driver = {
  221. .probe = nop_usb_xceiv_probe,
  222. .remove = nop_usb_xceiv_remove,
  223. .driver = {
  224. .name = "nop_usb_xceiv",
  225. .owner = THIS_MODULE,
  226. .of_match_table = nop_xceiv_dt_ids,
  227. },
  228. };
  229. static int __init nop_usb_xceiv_init(void)
  230. {
  231. return platform_driver_register(&nop_usb_xceiv_driver);
  232. }
  233. subsys_initcall(nop_usb_xceiv_init);
  234. static void __exit nop_usb_xceiv_exit(void)
  235. {
  236. platform_driver_unregister(&nop_usb_xceiv_driver);
  237. }
  238. module_exit(nop_usb_xceiv_exit);
  239. MODULE_ALIAS("platform:nop_usb_xceiv");
  240. MODULE_AUTHOR("Texas Instruments Inc");
  241. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  242. MODULE_LICENSE("GPL");