phy-generic.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * NOP USB transceiver for all USB transceiver which are either built-in
  3. * into USB IP or which are mostly autonomous.
  4. *
  5. * Copyright (C) 2009 Texas Instruments Inc
  6. * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Current status:
  23. * This provides a "nop" transceiver for PHYs which are
  24. * autonomous such as isp1504, isp1707, etc.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/usb/otg.h>
  30. #include <linux/usb/usb_phy_generic.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/of.h>
  35. #include <linux/of_gpio.h>
  36. #include <linux/gpio.h>
  37. #include <linux/delay.h>
  38. #include "phy-generic.h"
  39. struct platform_device *usb_phy_generic_register(void)
  40. {
  41. return platform_device_register_simple("usb_phy_generic",
  42. PLATFORM_DEVID_AUTO, NULL, 0);
  43. }
  44. EXPORT_SYMBOL_GPL(usb_phy_generic_register);
  45. void usb_phy_generic_unregister(struct platform_device *pdev)
  46. {
  47. platform_device_unregister(pdev);
  48. }
  49. EXPORT_SYMBOL_GPL(usb_phy_generic_unregister);
  50. static int nop_set_suspend(struct usb_phy *x, int suspend)
  51. {
  52. return 0;
  53. }
  54. static void nop_reset_set(struct usb_phy_generic *nop, int asserted)
  55. {
  56. int value;
  57. if (!gpio_is_valid(nop->gpio_reset))
  58. return;
  59. value = asserted;
  60. if (nop->reset_active_low)
  61. value = !value;
  62. gpio_set_value_cansleep(nop->gpio_reset, value);
  63. if (!asserted)
  64. usleep_range(10000, 20000);
  65. }
  66. int usb_gen_phy_init(struct usb_phy *phy)
  67. {
  68. struct usb_phy_generic *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_prepare_enable(nop->clk);
  75. /* De-assert RESET */
  76. nop_reset_set(nop, 0);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  80. void usb_gen_phy_shutdown(struct usb_phy *phy)
  81. {
  82. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  83. /* Assert RESET */
  84. nop_reset_set(nop, 1);
  85. if (!IS_ERR(nop->clk))
  86. clk_disable_unprepare(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->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_generic *nop,
  117. struct usb_phy_generic_platform_data *pdata)
  118. {
  119. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  120. int err;
  121. u32 clk_rate = 0;
  122. bool needs_vcc = false;
  123. nop->reset_active_low = true; /* default behaviour */
  124. if (dev->of_node) {
  125. struct device_node *node = dev->of_node;
  126. enum of_gpio_flags flags = 0;
  127. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  128. clk_rate = 0;
  129. needs_vcc = of_property_read_bool(node, "vcc-supply");
  130. nop->gpio_reset = of_get_named_gpio_flags(node, "reset-gpios",
  131. 0, &flags);
  132. if (nop->gpio_reset == -EPROBE_DEFER)
  133. return -EPROBE_DEFER;
  134. nop->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
  135. } else if (pdata) {
  136. type = pdata->type;
  137. clk_rate = pdata->clk_rate;
  138. needs_vcc = pdata->needs_vcc;
  139. nop->gpio_reset = pdata->gpio_reset;
  140. } else {
  141. nop->gpio_reset = -1;
  142. }
  143. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  144. GFP_KERNEL);
  145. if (!nop->phy.otg)
  146. return -ENOMEM;
  147. nop->clk = devm_clk_get(dev, "main_clk");
  148. if (IS_ERR(nop->clk)) {
  149. dev_dbg(dev, "Can't get phy clock: %ld\n",
  150. PTR_ERR(nop->clk));
  151. }
  152. if (!IS_ERR(nop->clk) && clk_rate) {
  153. err = clk_set_rate(nop->clk, clk_rate);
  154. if (err) {
  155. dev_err(dev, "Error setting clock rate\n");
  156. return err;
  157. }
  158. }
  159. nop->vcc = devm_regulator_get(dev, "vcc");
  160. if (IS_ERR(nop->vcc)) {
  161. dev_dbg(dev, "Error getting vcc regulator: %ld\n",
  162. PTR_ERR(nop->vcc));
  163. if (needs_vcc)
  164. return -EPROBE_DEFER;
  165. }
  166. if (gpio_is_valid(nop->gpio_reset)) {
  167. unsigned long gpio_flags;
  168. /* Assert RESET */
  169. if (nop->reset_active_low)
  170. gpio_flags = GPIOF_OUT_INIT_LOW;
  171. else
  172. gpio_flags = GPIOF_OUT_INIT_HIGH;
  173. err = devm_gpio_request_one(dev, nop->gpio_reset,
  174. gpio_flags, dev_name(dev));
  175. if (err) {
  176. dev_err(dev, "Error requesting RESET GPIO %d\n",
  177. nop->gpio_reset);
  178. return err;
  179. }
  180. }
  181. nop->dev = dev;
  182. nop->phy.dev = nop->dev;
  183. nop->phy.label = "nop-xceiv";
  184. nop->phy.set_suspend = nop_set_suspend;
  185. nop->phy.type = type;
  186. nop->phy.otg->state = OTG_STATE_UNDEFINED;
  187. nop->phy.otg->usb_phy = &nop->phy;
  188. nop->phy.otg->set_host = nop_set_host;
  189. nop->phy.otg->set_peripheral = nop_set_peripheral;
  190. return 0;
  191. }
  192. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  193. static int usb_phy_generic_probe(struct platform_device *pdev)
  194. {
  195. struct device *dev = &pdev->dev;
  196. struct usb_phy_generic *nop;
  197. int err;
  198. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  199. if (!nop)
  200. return -ENOMEM;
  201. err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev));
  202. if (err)
  203. return err;
  204. nop->phy.init = usb_gen_phy_init;
  205. nop->phy.shutdown = usb_gen_phy_shutdown;
  206. err = usb_add_phy_dev(&nop->phy);
  207. if (err) {
  208. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  209. err);
  210. return err;
  211. }
  212. platform_set_drvdata(pdev, nop);
  213. return 0;
  214. }
  215. static int usb_phy_generic_remove(struct platform_device *pdev)
  216. {
  217. struct usb_phy_generic *nop = platform_get_drvdata(pdev);
  218. usb_remove_phy(&nop->phy);
  219. return 0;
  220. }
  221. static const struct of_device_id nop_xceiv_dt_ids[] = {
  222. { .compatible = "usb-nop-xceiv" },
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  226. static struct platform_driver usb_phy_generic_driver = {
  227. .probe = usb_phy_generic_probe,
  228. .remove = usb_phy_generic_remove,
  229. .driver = {
  230. .name = "usb_phy_generic",
  231. .of_match_table = nop_xceiv_dt_ids,
  232. },
  233. };
  234. static int __init usb_phy_generic_init(void)
  235. {
  236. return platform_driver_register(&usb_phy_generic_driver);
  237. }
  238. subsys_initcall(usb_phy_generic_init);
  239. static void __exit usb_phy_generic_exit(void)
  240. {
  241. platform_driver_unregister(&usb_phy_generic_driver);
  242. }
  243. module_exit(usb_phy_generic_exit);
  244. MODULE_ALIAS("platform:usb_phy_generic");
  245. MODULE_AUTHOR("Texas Instruments Inc");
  246. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  247. MODULE_LICENSE("GPL");