phy-generic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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(struct usb_phy_generic *nop)
  59. {
  60. if (!nop->gpiod_reset)
  61. return;
  62. gpiod_set_value(nop->gpiod_reset, 1);
  63. usleep_range(10000, 20000);
  64. gpiod_set_value(nop->gpiod_reset, 0);
  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. nop_reset(nop);
  132. return 0;
  133. }
  134. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  135. void usb_gen_phy_shutdown(struct usb_phy *phy)
  136. {
  137. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  138. gpiod_set_value(nop->gpiod_reset, 1);
  139. if (!IS_ERR(nop->clk))
  140. clk_disable_unprepare(nop->clk);
  141. if (!IS_ERR(nop->vcc)) {
  142. if (regulator_disable(nop->vcc))
  143. dev_err(phy->dev, "Failed to disable power\n");
  144. }
  145. }
  146. EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
  147. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  148. {
  149. if (!otg)
  150. return -ENODEV;
  151. if (!gadget) {
  152. otg->gadget = NULL;
  153. return -ENODEV;
  154. }
  155. otg->gadget = gadget;
  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;
  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. 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. } 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,
  197. GPIOF_ACTIVE_LOW,
  198. dev_name(dev));
  199. if (!err)
  200. nop->gpiod_reset =
  201. gpio_to_desc(pdata->gpio_reset);
  202. }
  203. nop->gpiod_vbus = pdata->gpiod_vbus;
  204. }
  205. if (err == -EPROBE_DEFER)
  206. return -EPROBE_DEFER;
  207. if (err) {
  208. dev_err(dev, "Error requesting RESET or VBUS GPIO\n");
  209. return err;
  210. }
  211. if (nop->gpiod_reset)
  212. gpiod_direction_output(nop->gpiod_reset, 1);
  213. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  214. GFP_KERNEL);
  215. if (!nop->phy.otg)
  216. return -ENOMEM;
  217. nop->clk = devm_clk_get(dev, "main_clk");
  218. if (IS_ERR(nop->clk)) {
  219. dev_dbg(dev, "Can't get phy clock: %ld\n",
  220. PTR_ERR(nop->clk));
  221. }
  222. if (!IS_ERR(nop->clk) && clk_rate) {
  223. err = clk_set_rate(nop->clk, clk_rate);
  224. if (err) {
  225. dev_err(dev, "Error setting clock rate\n");
  226. return err;
  227. }
  228. }
  229. nop->vcc = devm_regulator_get(dev, "vcc");
  230. if (IS_ERR(nop->vcc)) {
  231. dev_dbg(dev, "Error getting vcc regulator: %ld\n",
  232. PTR_ERR(nop->vcc));
  233. if (needs_vcc)
  234. return -EPROBE_DEFER;
  235. }
  236. nop->dev = dev;
  237. nop->phy.dev = nop->dev;
  238. nop->phy.label = "nop-xceiv";
  239. nop->phy.set_suspend = nop_set_suspend;
  240. nop->phy.type = type;
  241. nop->phy.otg->state = OTG_STATE_UNDEFINED;
  242. nop->phy.otg->usb_phy = &nop->phy;
  243. nop->phy.otg->set_host = nop_set_host;
  244. nop->phy.otg->set_peripheral = nop_set_peripheral;
  245. return 0;
  246. }
  247. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  248. static int usb_phy_generic_probe(struct platform_device *pdev)
  249. {
  250. struct device *dev = &pdev->dev;
  251. struct usb_phy_generic *nop;
  252. int err;
  253. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  254. if (!nop)
  255. return -ENOMEM;
  256. err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev));
  257. if (err)
  258. return err;
  259. if (nop->gpiod_vbus) {
  260. err = devm_request_threaded_irq(&pdev->dev,
  261. gpiod_to_irq(nop->gpiod_vbus),
  262. NULL, nop_gpio_vbus_thread,
  263. VBUS_IRQ_FLAGS, "vbus_detect",
  264. nop);
  265. if (err) {
  266. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  267. gpiod_to_irq(nop->gpiod_vbus), err);
  268. return err;
  269. }
  270. }
  271. nop->phy.init = usb_gen_phy_init;
  272. nop->phy.shutdown = usb_gen_phy_shutdown;
  273. err = usb_add_phy_dev(&nop->phy);
  274. if (err) {
  275. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  276. err);
  277. return err;
  278. }
  279. platform_set_drvdata(pdev, nop);
  280. return 0;
  281. }
  282. static int usb_phy_generic_remove(struct platform_device *pdev)
  283. {
  284. struct usb_phy_generic *nop = platform_get_drvdata(pdev);
  285. usb_remove_phy(&nop->phy);
  286. return 0;
  287. }
  288. static const struct of_device_id nop_xceiv_dt_ids[] = {
  289. { .compatible = "usb-nop-xceiv" },
  290. { }
  291. };
  292. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  293. static struct platform_driver usb_phy_generic_driver = {
  294. .probe = usb_phy_generic_probe,
  295. .remove = usb_phy_generic_remove,
  296. .driver = {
  297. .name = "usb_phy_generic",
  298. .of_match_table = nop_xceiv_dt_ids,
  299. },
  300. };
  301. static int __init usb_phy_generic_init(void)
  302. {
  303. return platform_driver_register(&usb_phy_generic_driver);
  304. }
  305. subsys_initcall(usb_phy_generic_init);
  306. static void __exit usb_phy_generic_exit(void)
  307. {
  308. platform_driver_unregister(&usb_phy_generic_driver);
  309. }
  310. module_exit(usb_phy_generic_exit);
  311. MODULE_ALIAS("platform:usb_phy_generic");
  312. MODULE_AUTHOR("Texas Instruments Inc");
  313. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  314. MODULE_LICENSE("GPL");