phy-generic.c 8.6 KB

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