phy-generic.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <[email protected]>
  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. if (!IS_ERR(nop->clk)) {
  44. if (suspend)
  45. clk_disable_unprepare(nop->clk);
  46. else
  47. clk_prepare_enable(nop->clk);
  48. }
  49. return 0;
  50. }
  51. static void nop_reset(struct usb_phy_generic *nop)
  52. {
  53. if (!nop->gpiod_reset)
  54. return;
  55. gpiod_set_value_cansleep(nop->gpiod_reset, 1);
  56. usleep_range(10000, 20000);
  57. gpiod_set_value_cansleep(nop->gpiod_reset, 0);
  58. }
  59. /* interface to regulator framework */
  60. static void nop_set_vbus_draw(struct usb_phy_generic *nop, unsigned mA)
  61. {
  62. struct regulator *vbus_draw = nop->vbus_draw;
  63. int enabled;
  64. int ret;
  65. if (!vbus_draw)
  66. return;
  67. enabled = nop->vbus_draw_enabled;
  68. if (mA) {
  69. regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
  70. if (!enabled) {
  71. ret = regulator_enable(vbus_draw);
  72. if (ret < 0)
  73. return;
  74. nop->vbus_draw_enabled = 1;
  75. }
  76. } else {
  77. if (enabled) {
  78. ret = regulator_disable(vbus_draw);
  79. if (ret < 0)
  80. return;
  81. nop->vbus_draw_enabled = 0;
  82. }
  83. }
  84. nop->mA = mA;
  85. }
  86. static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
  87. {
  88. struct usb_phy_generic *nop = data;
  89. struct usb_otg *otg = nop->phy.otg;
  90. int vbus, status;
  91. vbus = gpiod_get_value(nop->gpiod_vbus);
  92. if ((vbus ^ nop->vbus) == 0)
  93. return IRQ_HANDLED;
  94. nop->vbus = vbus;
  95. if (vbus) {
  96. status = USB_EVENT_VBUS;
  97. otg->state = OTG_STATE_B_PERIPHERAL;
  98. nop->phy.last_event = status;
  99. /* drawing a "unit load" is *always* OK, except for OTG */
  100. nop_set_vbus_draw(nop, 100);
  101. atomic_notifier_call_chain(&nop->phy.notifier, status,
  102. otg->gadget);
  103. } else {
  104. nop_set_vbus_draw(nop, 0);
  105. status = USB_EVENT_NONE;
  106. otg->state = OTG_STATE_B_IDLE;
  107. nop->phy.last_event = status;
  108. atomic_notifier_call_chain(&nop->phy.notifier, status,
  109. otg->gadget);
  110. }
  111. return IRQ_HANDLED;
  112. }
  113. int usb_gen_phy_init(struct usb_phy *phy)
  114. {
  115. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  116. int ret;
  117. if (!IS_ERR(nop->vcc)) {
  118. if (regulator_enable(nop->vcc))
  119. dev_err(phy->dev, "Failed to enable power\n");
  120. }
  121. if (!IS_ERR(nop->clk)) {
  122. ret = clk_prepare_enable(nop->clk);
  123. if (ret)
  124. return ret;
  125. }
  126. nop_reset(nop);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  130. void usb_gen_phy_shutdown(struct usb_phy *phy)
  131. {
  132. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  133. gpiod_set_value_cansleep(nop->gpiod_reset, 1);
  134. if (!IS_ERR(nop->clk))
  135. clk_disable_unprepare(nop->clk);
  136. if (!IS_ERR(nop->vcc)) {
  137. if (regulator_disable(nop->vcc))
  138. dev_err(phy->dev, "Failed to disable power\n");
  139. }
  140. }
  141. EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
  142. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  143. {
  144. if (!otg)
  145. return -ENODEV;
  146. if (!gadget) {
  147. otg->gadget = NULL;
  148. return -ENODEV;
  149. }
  150. otg->gadget = gadget;
  151. if (otg->state == OTG_STATE_B_PERIPHERAL)
  152. atomic_notifier_call_chain(&otg->usb_phy->notifier,
  153. USB_EVENT_VBUS, otg->gadget);
  154. else
  155. otg->state = OTG_STATE_B_IDLE;
  156. return 0;
  157. }
  158. static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
  159. {
  160. if (!otg)
  161. return -ENODEV;
  162. if (!host) {
  163. otg->host = NULL;
  164. return -ENODEV;
  165. }
  166. otg->host = host;
  167. return 0;
  168. }
  169. int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop)
  170. {
  171. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  172. int err = 0;
  173. u32 clk_rate = 0;
  174. bool needs_vcc = false, needs_clk = false;
  175. if (dev->of_node) {
  176. struct device_node *node = dev->of_node;
  177. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  178. clk_rate = 0;
  179. needs_vcc = of_property_read_bool(node, "vcc-supply");
  180. needs_clk = of_property_read_bool(node, "clocks");
  181. }
  182. nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
  183. GPIOD_ASIS);
  184. err = PTR_ERR_OR_ZERO(nop->gpiod_reset);
  185. if (!err) {
  186. nop->gpiod_vbus = devm_gpiod_get_optional(dev,
  187. "vbus-detect",
  188. GPIOD_ASIS);
  189. err = PTR_ERR_OR_ZERO(nop->gpiod_vbus);
  190. }
  191. if (err)
  192. return dev_err_probe(dev, err,
  193. "Error requesting RESET or VBUS GPIO\n");
  194. if (nop->gpiod_reset)
  195. gpiod_direction_output(nop->gpiod_reset, 1);
  196. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  197. GFP_KERNEL);
  198. if (!nop->phy.otg)
  199. return -ENOMEM;
  200. nop->clk = devm_clk_get(dev, "main_clk");
  201. if (IS_ERR(nop->clk)) {
  202. dev_dbg(dev, "Can't get phy clock: %ld\n",
  203. PTR_ERR(nop->clk));
  204. if (needs_clk)
  205. return PTR_ERR(nop->clk);
  206. }
  207. if (!IS_ERR(nop->clk) && clk_rate) {
  208. err = clk_set_rate(nop->clk, clk_rate);
  209. if (err) {
  210. dev_err(dev, "Error setting clock rate\n");
  211. return err;
  212. }
  213. }
  214. nop->vcc = devm_regulator_get(dev, "vcc");
  215. if (IS_ERR(nop->vcc)) {
  216. dev_dbg(dev, "Error getting vcc regulator: %ld\n",
  217. PTR_ERR(nop->vcc));
  218. if (needs_vcc)
  219. return -EPROBE_DEFER;
  220. }
  221. nop->vbus_draw = devm_regulator_get_exclusive(dev, "vbus");
  222. if (PTR_ERR(nop->vbus_draw) == -ENODEV)
  223. nop->vbus_draw = NULL;
  224. if (IS_ERR(nop->vbus_draw))
  225. return dev_err_probe(dev, PTR_ERR(nop->vbus_draw),
  226. "could not get vbus regulator\n");
  227. nop->dev = dev;
  228. nop->phy.dev = nop->dev;
  229. nop->phy.label = "nop-xceiv";
  230. nop->phy.set_suspend = nop_set_suspend;
  231. nop->phy.type = type;
  232. nop->phy.otg->state = OTG_STATE_UNDEFINED;
  233. nop->phy.otg->usb_phy = &nop->phy;
  234. nop->phy.otg->set_host = nop_set_host;
  235. nop->phy.otg->set_peripheral = nop_set_peripheral;
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  239. static int usb_phy_generic_probe(struct platform_device *pdev)
  240. {
  241. struct device *dev = &pdev->dev;
  242. struct usb_phy_generic *nop;
  243. int err;
  244. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  245. if (!nop)
  246. return -ENOMEM;
  247. err = usb_phy_gen_create_phy(dev, nop);
  248. if (err)
  249. return err;
  250. if (nop->gpiod_vbus) {
  251. err = devm_request_threaded_irq(&pdev->dev,
  252. gpiod_to_irq(nop->gpiod_vbus),
  253. NULL, nop_gpio_vbus_thread,
  254. VBUS_IRQ_FLAGS, "vbus_detect",
  255. nop);
  256. if (err) {
  257. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  258. gpiod_to_irq(nop->gpiod_vbus), err);
  259. return err;
  260. }
  261. nop->phy.otg->state = gpiod_get_value(nop->gpiod_vbus) ?
  262. OTG_STATE_B_PERIPHERAL : OTG_STATE_B_IDLE;
  263. }
  264. nop->phy.init = usb_gen_phy_init;
  265. nop->phy.shutdown = usb_gen_phy_shutdown;
  266. err = usb_add_phy_dev(&nop->phy);
  267. if (err) {
  268. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  269. err);
  270. return err;
  271. }
  272. platform_set_drvdata(pdev, nop);
  273. return 0;
  274. }
  275. static int usb_phy_generic_remove(struct platform_device *pdev)
  276. {
  277. struct usb_phy_generic *nop = platform_get_drvdata(pdev);
  278. usb_remove_phy(&nop->phy);
  279. return 0;
  280. }
  281. static const struct of_device_id nop_xceiv_dt_ids[] = {
  282. { .compatible = "usb-nop-xceiv" },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  286. static struct platform_driver usb_phy_generic_driver = {
  287. .probe = usb_phy_generic_probe,
  288. .remove = usb_phy_generic_remove,
  289. .driver = {
  290. .name = "usb_phy_generic",
  291. .of_match_table = nop_xceiv_dt_ids,
  292. },
  293. };
  294. static int __init usb_phy_generic_init(void)
  295. {
  296. return platform_driver_register(&usb_phy_generic_driver);
  297. }
  298. subsys_initcall(usb_phy_generic_init);
  299. static void __exit usb_phy_generic_exit(void)
  300. {
  301. platform_driver_unregister(&usb_phy_generic_driver);
  302. }
  303. module_exit(usb_phy_generic_exit);
  304. MODULE_ALIAS("platform:usb_phy_generic");
  305. MODULE_AUTHOR("Texas Instruments Inc");
  306. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  307. MODULE_LICENSE("GPL");