phy-generic.c 7.2 KB

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