phy-generic.c 7.3 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 (!pd) {
  48. pr_err("Unable to register generic usb transceiver\n");
  49. return;
  50. }
  51. }
  52. EXPORT_SYMBOL(usb_nop_xceiv_register);
  53. void usb_nop_xceiv_unregister(void)
  54. {
  55. platform_device_unregister(pd);
  56. pd = NULL;
  57. }
  58. EXPORT_SYMBOL(usb_nop_xceiv_unregister);
  59. static int nop_set_suspend(struct usb_phy *x, int suspend)
  60. {
  61. return 0;
  62. }
  63. static void nop_reset_set(struct usb_phy_gen_xceiv *nop, int asserted)
  64. {
  65. int value;
  66. if (!gpio_is_valid(nop->gpio_reset))
  67. return;
  68. value = asserted;
  69. if (nop->reset_active_low)
  70. value = !value;
  71. gpio_set_value_cansleep(nop->gpio_reset, value);
  72. if (!asserted)
  73. usleep_range(10000, 20000);
  74. }
  75. int usb_gen_phy_init(struct usb_phy *phy)
  76. {
  77. struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
  78. if (!IS_ERR(nop->vcc)) {
  79. if (regulator_enable(nop->vcc))
  80. dev_err(phy->dev, "Failed to enable power\n");
  81. }
  82. if (!IS_ERR(nop->clk))
  83. clk_prepare_enable(nop->clk);
  84. /* De-assert RESET */
  85. nop_reset_set(nop, 0);
  86. return 0;
  87. }
  88. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  89. void usb_gen_phy_shutdown(struct usb_phy *phy)
  90. {
  91. struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
  92. /* Assert RESET */
  93. nop_reset_set(nop, 1);
  94. if (!IS_ERR(nop->clk))
  95. clk_disable_unprepare(nop->clk);
  96. if (!IS_ERR(nop->vcc)) {
  97. if (regulator_disable(nop->vcc))
  98. dev_err(phy->dev, "Failed to disable power\n");
  99. }
  100. }
  101. EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
  102. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  103. {
  104. if (!otg)
  105. return -ENODEV;
  106. if (!gadget) {
  107. otg->gadget = NULL;
  108. return -ENODEV;
  109. }
  110. otg->gadget = gadget;
  111. otg->phy->state = OTG_STATE_B_IDLE;
  112. return 0;
  113. }
  114. static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
  115. {
  116. if (!otg)
  117. return -ENODEV;
  118. if (!host) {
  119. otg->host = NULL;
  120. return -ENODEV;
  121. }
  122. otg->host = host;
  123. return 0;
  124. }
  125. int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_gen_xceiv *nop,
  126. enum usb_phy_type type, u32 clk_rate, bool needs_vcc)
  127. {
  128. int err;
  129. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  130. GFP_KERNEL);
  131. if (!nop->phy.otg)
  132. return -ENOMEM;
  133. nop->clk = devm_clk_get(dev, "main_clk");
  134. if (IS_ERR(nop->clk)) {
  135. dev_dbg(dev, "Can't get phy clock: %ld\n",
  136. PTR_ERR(nop->clk));
  137. }
  138. if (!IS_ERR(nop->clk) && clk_rate) {
  139. err = clk_set_rate(nop->clk, clk_rate);
  140. if (err) {
  141. dev_err(dev, "Error setting clock rate\n");
  142. return err;
  143. }
  144. }
  145. nop->vcc = devm_regulator_get(dev, "vcc");
  146. if (IS_ERR(nop->vcc)) {
  147. dev_dbg(dev, "Error getting vcc regulator: %ld\n",
  148. PTR_ERR(nop->vcc));
  149. if (needs_vcc)
  150. return -EPROBE_DEFER;
  151. }
  152. if (gpio_is_valid(nop->gpio_reset)) {
  153. unsigned long gpio_flags;
  154. /* Assert RESET */
  155. if (nop->reset_active_low)
  156. gpio_flags = GPIOF_OUT_INIT_LOW;
  157. else
  158. gpio_flags = GPIOF_OUT_INIT_HIGH;
  159. err = devm_gpio_request_one(dev, nop->gpio_reset,
  160. gpio_flags, dev_name(dev));
  161. if (err) {
  162. dev_err(dev, "Error requesting RESET GPIO %d\n",
  163. nop->gpio_reset);
  164. return err;
  165. }
  166. }
  167. nop->dev = dev;
  168. nop->phy.dev = nop->dev;
  169. nop->phy.label = "nop-xceiv";
  170. nop->phy.set_suspend = nop_set_suspend;
  171. nop->phy.state = OTG_STATE_UNDEFINED;
  172. nop->phy.type = type;
  173. nop->phy.otg->phy = &nop->phy;
  174. nop->phy.otg->set_host = nop_set_host;
  175. nop->phy.otg->set_peripheral = nop_set_peripheral;
  176. ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  180. static int usb_phy_gen_xceiv_probe(struct platform_device *pdev)
  181. {
  182. struct device *dev = &pdev->dev;
  183. struct usb_phy_gen_xceiv_platform_data *pdata =
  184. dev_get_platdata(&pdev->dev);
  185. struct usb_phy_gen_xceiv *nop;
  186. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  187. int err;
  188. u32 clk_rate = 0;
  189. bool needs_vcc = false;
  190. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  191. if (!nop)
  192. return -ENOMEM;
  193. nop->reset_active_low = true; /* default behaviour */
  194. if (dev->of_node) {
  195. struct device_node *node = dev->of_node;
  196. enum of_gpio_flags flags;
  197. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  198. clk_rate = 0;
  199. needs_vcc = of_property_read_bool(node, "vcc-supply");
  200. nop->gpio_reset = of_get_named_gpio_flags(node, "reset-gpios",
  201. 0, &flags);
  202. if (nop->gpio_reset == -EPROBE_DEFER)
  203. return -EPROBE_DEFER;
  204. nop->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
  205. } else if (pdata) {
  206. type = pdata->type;
  207. clk_rate = pdata->clk_rate;
  208. needs_vcc = pdata->needs_vcc;
  209. nop->gpio_reset = pdata->gpio_reset;
  210. }
  211. err = usb_phy_gen_create_phy(dev, nop, type, clk_rate, needs_vcc);
  212. if (err)
  213. return err;
  214. nop->phy.init = usb_gen_phy_init;
  215. nop->phy.shutdown = usb_gen_phy_shutdown;
  216. err = usb_add_phy_dev(&nop->phy);
  217. if (err) {
  218. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  219. err);
  220. return err;
  221. }
  222. platform_set_drvdata(pdev, nop);
  223. return 0;
  224. return err;
  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");