phy-generic.c 8.7 KB

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