phy-generic.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NOP USB transceiver for all USB transceiver which are either built-in
  4. * into USB IP or which are mostly autonomous.
  5. *
  6. * Copyright (C) 2009 Texas Instruments Inc
  7. * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
  8. *
  9. * Current status:
  10. * This provides a "nop" transceiver for PHYs which are
  11. * autonomous such as isp1504, isp1707, etc.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/usb/gadget.h>
  17. #include <linux/usb/otg.h>
  18. #include <linux/usb/usb_phy_generic.h>
  19. #include <linux/slab.h>
  20. #include <linux/clk.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/of.h>
  23. #include <linux/gpio/consumer.h>
  24. #include <linux/delay.h>
  25. #include "phy-generic.h"
  26. #define VBUS_IRQ_FLAGS \
  27. (IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | \
  28. IRQF_ONESHOT)
  29. struct platform_device *usb_phy_generic_register(void)
  30. {
  31. return platform_device_register_simple("usb_phy_generic",
  32. PLATFORM_DEVID_AUTO, NULL, 0);
  33. }
  34. EXPORT_SYMBOL_GPL(usb_phy_generic_register);
  35. void usb_phy_generic_unregister(struct platform_device *pdev)
  36. {
  37. platform_device_unregister(pdev);
  38. }
  39. EXPORT_SYMBOL_GPL(usb_phy_generic_unregister);
  40. static int nop_set_suspend(struct usb_phy *x, int suspend)
  41. {
  42. struct usb_phy_generic *nop = dev_get_drvdata(x->dev);
  43. int ret = 0;
  44. if (suspend) {
  45. if (!IS_ERR(nop->clk))
  46. clk_disable_unprepare(nop->clk);
  47. if (!IS_ERR(nop->vcc) && !device_may_wakeup(x->dev))
  48. ret = regulator_disable(nop->vcc);
  49. } else {
  50. if (!IS_ERR(nop->vcc) && !device_may_wakeup(x->dev))
  51. ret = regulator_enable(nop->vcc);
  52. if (!IS_ERR(nop->clk))
  53. clk_prepare_enable(nop->clk);
  54. }
  55. return ret;
  56. }
  57. static void nop_reset(struct usb_phy_generic *nop)
  58. {
  59. if (!nop->gpiod_reset)
  60. return;
  61. gpiod_set_value_cansleep(nop->gpiod_reset, 1);
  62. usleep_range(10000, 20000);
  63. gpiod_set_value_cansleep(nop->gpiod_reset, 0);
  64. usleep_range(10000, 30000);
  65. }
  66. /* interface to regulator framework */
  67. static int nop_set_vbus(struct usb_otg *otg, bool enable)
  68. {
  69. int ret = 0;
  70. struct usb_phy_generic *nop = dev_get_drvdata(otg->usb_phy->dev);
  71. if (!nop->vbus_draw)
  72. return 0;
  73. if (enable && !nop->vbus_draw_enabled) {
  74. ret = regulator_enable(nop->vbus_draw);
  75. if (ret)
  76. nop->vbus_draw_enabled = false;
  77. else
  78. nop->vbus_draw_enabled = true;
  79. } else if (!enable && nop->vbus_draw_enabled) {
  80. ret = regulator_disable(nop->vbus_draw);
  81. nop->vbus_draw_enabled = false;
  82. }
  83. return ret;
  84. }
  85. static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
  86. {
  87. struct usb_phy_generic *nop = data;
  88. struct usb_otg *otg = nop->phy.otg;
  89. int vbus, status;
  90. vbus = gpiod_get_value(nop->gpiod_vbus);
  91. if ((vbus ^ nop->vbus) == 0)
  92. return IRQ_HANDLED;
  93. nop->vbus = vbus;
  94. if (vbus) {
  95. status = USB_EVENT_VBUS;
  96. otg->state = OTG_STATE_B_PERIPHERAL;
  97. nop->phy.last_event = status;
  98. atomic_notifier_call_chain(&nop->phy.notifier, status,
  99. otg->gadget);
  100. } else {
  101. status = USB_EVENT_NONE;
  102. otg->state = OTG_STATE_B_IDLE;
  103. nop->phy.last_event = status;
  104. atomic_notifier_call_chain(&nop->phy.notifier, status,
  105. otg->gadget);
  106. }
  107. return IRQ_HANDLED;
  108. }
  109. int usb_gen_phy_init(struct usb_phy *phy)
  110. {
  111. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  112. int ret;
  113. if (!IS_ERR(nop->vcc)) {
  114. if (regulator_enable(nop->vcc))
  115. dev_err(phy->dev, "Failed to enable power\n");
  116. }
  117. if (!IS_ERR(nop->clk)) {
  118. ret = clk_prepare_enable(nop->clk);
  119. if (ret)
  120. return ret;
  121. }
  122. nop_reset(nop);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  126. void usb_gen_phy_shutdown(struct usb_phy *phy)
  127. {
  128. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  129. gpiod_set_value_cansleep(nop->gpiod_reset, 1);
  130. if (!IS_ERR(nop->clk))
  131. clk_disable_unprepare(nop->clk);
  132. if (!IS_ERR(nop->vcc)) {
  133. if (regulator_disable(nop->vcc))
  134. dev_err(phy->dev, "Failed to disable power\n");
  135. }
  136. }
  137. EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
  138. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  139. {
  140. if (!otg)
  141. return -ENODEV;
  142. if (!gadget) {
  143. otg->gadget = NULL;
  144. return -ENODEV;
  145. }
  146. otg->gadget = gadget;
  147. if (otg->state == OTG_STATE_B_PERIPHERAL)
  148. atomic_notifier_call_chain(&otg->usb_phy->notifier,
  149. USB_EVENT_VBUS, otg->gadget);
  150. else
  151. otg->state = OTG_STATE_B_IDLE;
  152. return 0;
  153. }
  154. static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
  155. {
  156. if (!otg)
  157. return -ENODEV;
  158. if (!host) {
  159. otg->host = NULL;
  160. return -ENODEV;
  161. }
  162. otg->host = host;
  163. return 0;
  164. }
  165. int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop)
  166. {
  167. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  168. int err = 0;
  169. u32 clk_rate = 0;
  170. bool needs_clk = false;
  171. if (dev->of_node) {
  172. struct device_node *node = dev->of_node;
  173. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  174. clk_rate = 0;
  175. needs_clk = of_property_present(node, "clocks");
  176. }
  177. nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
  178. GPIOD_ASIS);
  179. err = PTR_ERR_OR_ZERO(nop->gpiod_reset);
  180. if (!err) {
  181. nop->gpiod_vbus = devm_gpiod_get_optional(dev,
  182. "vbus-detect",
  183. GPIOD_ASIS);
  184. err = PTR_ERR_OR_ZERO(nop->gpiod_vbus);
  185. }
  186. if (err)
  187. return dev_err_probe(dev, err,
  188. "Error requesting RESET or VBUS GPIO\n");
  189. if (nop->gpiod_reset)
  190. gpiod_direction_output(nop->gpiod_reset, 1);
  191. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  192. GFP_KERNEL);
  193. if (!nop->phy.otg)
  194. return -ENOMEM;
  195. nop->clk = devm_clk_get(dev, "main_clk");
  196. if (IS_ERR(nop->clk)) {
  197. dev_dbg(dev, "Can't get phy clock: %ld\n",
  198. PTR_ERR(nop->clk));
  199. if (needs_clk)
  200. return PTR_ERR(nop->clk);
  201. }
  202. if (!IS_ERR(nop->clk) && clk_rate) {
  203. err = clk_set_rate(nop->clk, clk_rate);
  204. if (err) {
  205. dev_err(dev, "Error setting clock rate\n");
  206. return err;
  207. }
  208. }
  209. nop->vcc = devm_regulator_get_optional(dev, "vcc");
  210. if (IS_ERR(nop->vcc) && PTR_ERR(nop->vcc) != -ENODEV)
  211. return dev_err_probe(dev, PTR_ERR(nop->vcc),
  212. "could not get vcc regulator\n");
  213. nop->vbus_draw = devm_regulator_get_exclusive(dev, "vbus");
  214. if (PTR_ERR(nop->vbus_draw) == -ENODEV)
  215. nop->vbus_draw = NULL;
  216. if (IS_ERR(nop->vbus_draw))
  217. return dev_err_probe(dev, PTR_ERR(nop->vbus_draw),
  218. "could not get vbus regulator\n");
  219. nop->dev = dev;
  220. nop->phy.dev = nop->dev;
  221. nop->phy.label = "nop-xceiv";
  222. nop->phy.set_suspend = nop_set_suspend;
  223. nop->phy.type = type;
  224. nop->phy.otg->state = OTG_STATE_UNDEFINED;
  225. nop->phy.otg->usb_phy = &nop->phy;
  226. nop->phy.otg->set_host = nop_set_host;
  227. nop->phy.otg->set_peripheral = nop_set_peripheral;
  228. nop->phy.otg->set_vbus = nop_set_vbus;
  229. return 0;
  230. }
  231. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  232. static int usb_phy_generic_probe(struct platform_device *pdev)
  233. {
  234. struct device *dev = &pdev->dev;
  235. struct device_node *dn = dev->of_node;
  236. struct usb_phy_generic *nop;
  237. int err;
  238. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  239. if (!nop)
  240. return -ENOMEM;
  241. err = usb_phy_gen_create_phy(dev, nop);
  242. if (err)
  243. return err;
  244. if (nop->gpiod_vbus) {
  245. err = devm_request_threaded_irq(&pdev->dev,
  246. gpiod_to_irq(nop->gpiod_vbus),
  247. NULL, nop_gpio_vbus_thread,
  248. VBUS_IRQ_FLAGS, "vbus_detect",
  249. nop);
  250. if (err) {
  251. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  252. gpiod_to_irq(nop->gpiod_vbus), err);
  253. return err;
  254. }
  255. nop->phy.otg->state = gpiod_get_value(nop->gpiod_vbus) ?
  256. OTG_STATE_B_PERIPHERAL : OTG_STATE_B_IDLE;
  257. }
  258. nop->phy.init = usb_gen_phy_init;
  259. nop->phy.shutdown = usb_gen_phy_shutdown;
  260. err = usb_add_phy_dev(&nop->phy);
  261. if (err) {
  262. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  263. err);
  264. return err;
  265. }
  266. platform_set_drvdata(pdev, nop);
  267. device_set_wakeup_capable(&pdev->dev,
  268. of_property_read_bool(dn, "wakeup-source"));
  269. return 0;
  270. }
  271. static void usb_phy_generic_remove(struct platform_device *pdev)
  272. {
  273. struct usb_phy_generic *nop = platform_get_drvdata(pdev);
  274. usb_remove_phy(&nop->phy);
  275. if (nop->vbus_draw && nop->vbus_draw_enabled)
  276. regulator_disable(nop->vbus_draw);
  277. }
  278. static const struct of_device_id nop_xceiv_dt_ids[] = {
  279. { .compatible = "usb-nop-xceiv" },
  280. { }
  281. };
  282. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  283. static struct platform_driver usb_phy_generic_driver = {
  284. .probe = usb_phy_generic_probe,
  285. .remove_new = usb_phy_generic_remove,
  286. .driver = {
  287. .name = "usb_phy_generic",
  288. .of_match_table = nop_xceiv_dt_ids,
  289. },
  290. };
  291. static int __init usb_phy_generic_init(void)
  292. {
  293. return platform_driver_register(&usb_phy_generic_driver);
  294. }
  295. subsys_initcall(usb_phy_generic_init);
  296. static void __exit usb_phy_generic_exit(void)
  297. {
  298. platform_driver_unregister(&usb_phy_generic_driver);
  299. }
  300. module_exit(usb_phy_generic_exit);
  301. MODULE_ALIAS("platform:usb_phy_generic");
  302. MODULE_AUTHOR("Texas Instruments Inc");
  303. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  304. MODULE_LICENSE("GPL");