phy-generic.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/usb_phy_gen_xceiv.h>
  33. #include <linux/slab.h>
  34. #include <linux/clk.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/of.h>
  37. #include "phy-generic.h"
  38. static struct platform_device *pd;
  39. void usb_nop_xceiv_register(void)
  40. {
  41. if (pd)
  42. return;
  43. pd = platform_device_register_simple("usb_phy_gen_xceiv", -1, NULL, 0);
  44. if (!pd) {
  45. pr_err("Unable to register generic usb transceiver\n");
  46. return;
  47. }
  48. }
  49. EXPORT_SYMBOL(usb_nop_xceiv_register);
  50. void usb_nop_xceiv_unregister(void)
  51. {
  52. platform_device_unregister(pd);
  53. pd = NULL;
  54. }
  55. EXPORT_SYMBOL(usb_nop_xceiv_unregister);
  56. static int nop_set_suspend(struct usb_phy *x, int suspend)
  57. {
  58. return 0;
  59. }
  60. int usb_gen_phy_init(struct usb_phy *phy)
  61. {
  62. struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
  63. if (!IS_ERR(nop->vcc)) {
  64. if (regulator_enable(nop->vcc))
  65. dev_err(phy->dev, "Failed to enable power\n");
  66. }
  67. if (!IS_ERR(nop->clk))
  68. clk_enable(nop->clk);
  69. if (!IS_ERR(nop->reset)) {
  70. /* De-assert RESET */
  71. if (regulator_enable(nop->reset))
  72. dev_err(phy->dev, "Failed to de-assert reset\n");
  73. }
  74. return 0;
  75. }
  76. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  77. void usb_gen_phy_shutdown(struct usb_phy *phy)
  78. {
  79. struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
  80. if (!IS_ERR(nop->reset)) {
  81. /* Assert RESET */
  82. if (regulator_disable(nop->reset))
  83. dev_err(phy->dev, "Failed to assert reset\n");
  84. }
  85. if (!IS_ERR(nop->clk))
  86. clk_disable(nop->clk);
  87. if (!IS_ERR(nop->vcc)) {
  88. if (regulator_disable(nop->vcc))
  89. dev_err(phy->dev, "Failed to disable power\n");
  90. }
  91. }
  92. EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
  93. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  94. {
  95. if (!otg)
  96. return -ENODEV;
  97. if (!gadget) {
  98. otg->gadget = NULL;
  99. return -ENODEV;
  100. }
  101. otg->gadget = gadget;
  102. otg->phy->state = OTG_STATE_B_IDLE;
  103. return 0;
  104. }
  105. static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
  106. {
  107. if (!otg)
  108. return -ENODEV;
  109. if (!host) {
  110. otg->host = NULL;
  111. return -ENODEV;
  112. }
  113. otg->host = host;
  114. return 0;
  115. }
  116. int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_gen_xceiv *nop,
  117. enum usb_phy_type type, u32 clk_rate, bool needs_vcc,
  118. bool needs_reset)
  119. {
  120. int err;
  121. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  122. GFP_KERNEL);
  123. if (!nop->phy.otg)
  124. return -ENOMEM;
  125. nop->clk = devm_clk_get(dev, "main_clk");
  126. if (IS_ERR(nop->clk)) {
  127. dev_dbg(dev, "Can't get phy clock: %ld\n",
  128. PTR_ERR(nop->clk));
  129. }
  130. if (!IS_ERR(nop->clk) && clk_rate) {
  131. err = clk_set_rate(nop->clk, clk_rate);
  132. if (err) {
  133. dev_err(dev, "Error setting clock rate\n");
  134. return err;
  135. }
  136. }
  137. if (!IS_ERR(nop->clk)) {
  138. err = clk_prepare(nop->clk);
  139. if (err) {
  140. dev_err(dev, "Error preparing clock\n");
  141. return err;
  142. }
  143. }
  144. nop->vcc = devm_regulator_get(dev, "vcc");
  145. if (IS_ERR(nop->vcc)) {
  146. dev_dbg(dev, "Error getting vcc regulator: %ld\n",
  147. PTR_ERR(nop->vcc));
  148. if (needs_vcc)
  149. return -EPROBE_DEFER;
  150. }
  151. nop->reset = devm_regulator_get(dev, "reset");
  152. if (IS_ERR(nop->reset)) {
  153. dev_dbg(dev, "Error getting reset regulator: %ld\n",
  154. PTR_ERR(nop->reset));
  155. if (needs_reset)
  156. return -EPROBE_DEFER;
  157. }
  158. nop->dev = dev;
  159. nop->phy.dev = nop->dev;
  160. nop->phy.label = "nop-xceiv";
  161. nop->phy.set_suspend = nop_set_suspend;
  162. nop->phy.state = OTG_STATE_UNDEFINED;
  163. nop->phy.type = type;
  164. nop->phy.otg->phy = &nop->phy;
  165. nop->phy.otg->set_host = nop_set_host;
  166. nop->phy.otg->set_peripheral = nop_set_peripheral;
  167. ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  171. void usb_phy_gen_cleanup_phy(struct usb_phy_gen_xceiv *nop)
  172. {
  173. if (!IS_ERR(nop->clk))
  174. clk_unprepare(nop->clk);
  175. }
  176. EXPORT_SYMBOL_GPL(usb_phy_gen_cleanup_phy);
  177. static int usb_phy_gen_xceiv_probe(struct platform_device *pdev)
  178. {
  179. struct device *dev = &pdev->dev;
  180. struct usb_phy_gen_xceiv_platform_data *pdata =
  181. dev_get_platdata(&pdev->dev);
  182. struct usb_phy_gen_xceiv *nop;
  183. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  184. int err;
  185. u32 clk_rate = 0;
  186. bool needs_vcc = false;
  187. bool needs_reset = false;
  188. if (dev->of_node) {
  189. struct device_node *node = dev->of_node;
  190. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  191. clk_rate = 0;
  192. needs_vcc = of_property_read_bool(node, "vcc-supply");
  193. needs_reset = of_property_read_bool(node, "reset-supply");
  194. } else if (pdata) {
  195. type = pdata->type;
  196. clk_rate = pdata->clk_rate;
  197. needs_vcc = pdata->needs_vcc;
  198. needs_reset = pdata->needs_reset;
  199. }
  200. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  201. if (!nop)
  202. return -ENOMEM;
  203. err = usb_phy_gen_create_phy(dev, nop, type, clk_rate, needs_vcc,
  204. needs_reset);
  205. if (err)
  206. return err;
  207. nop->phy.init = usb_gen_phy_init;
  208. nop->phy.shutdown = usb_gen_phy_shutdown;
  209. err = usb_add_phy_dev(&nop->phy);
  210. if (err) {
  211. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  212. err);
  213. goto err_add;
  214. }
  215. platform_set_drvdata(pdev, nop);
  216. return 0;
  217. err_add:
  218. usb_phy_gen_cleanup_phy(nop);
  219. return err;
  220. }
  221. static int usb_phy_gen_xceiv_remove(struct platform_device *pdev)
  222. {
  223. struct usb_phy_gen_xceiv *nop = platform_get_drvdata(pdev);
  224. usb_phy_gen_cleanup_phy(nop);
  225. usb_remove_phy(&nop->phy);
  226. return 0;
  227. }
  228. static const struct of_device_id nop_xceiv_dt_ids[] = {
  229. { .compatible = "usb-nop-xceiv" },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  233. static struct platform_driver usb_phy_gen_xceiv_driver = {
  234. .probe = usb_phy_gen_xceiv_probe,
  235. .remove = usb_phy_gen_xceiv_remove,
  236. .driver = {
  237. .name = "usb_phy_gen_xceiv",
  238. .owner = THIS_MODULE,
  239. .of_match_table = nop_xceiv_dt_ids,
  240. },
  241. };
  242. static int __init usb_phy_gen_xceiv_init(void)
  243. {
  244. return platform_driver_register(&usb_phy_gen_xceiv_driver);
  245. }
  246. subsys_initcall(usb_phy_gen_xceiv_init);
  247. static void __exit usb_phy_gen_xceiv_exit(void)
  248. {
  249. platform_driver_unregister(&usb_phy_gen_xceiv_driver);
  250. }
  251. module_exit(usb_phy_gen_xceiv_exit);
  252. MODULE_ALIAS("platform:usb_phy_gen_xceiv");
  253. MODULE_AUTHOR("Texas Instruments Inc");
  254. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  255. MODULE_LICENSE("GPL");