phy-generic.c 9.3 KB

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