phy-generic.c 7.0 KB

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