phy-generic.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. if (otg->gadget)
  107. usb_gadget_vbus_connect(otg->gadget);
  108. /* drawing a "unit load" is *always* OK, except for OTG */
  109. nop_set_vbus_draw(nop, 100);
  110. atomic_notifier_call_chain(&nop->phy.notifier, status,
  111. otg->gadget);
  112. } else {
  113. nop_set_vbus_draw(nop, 0);
  114. if (otg->gadget)
  115. usb_gadget_vbus_disconnect(otg->gadget);
  116. status = USB_EVENT_NONE;
  117. otg->state = OTG_STATE_B_IDLE;
  118. nop->phy.last_event = status;
  119. atomic_notifier_call_chain(&nop->phy.notifier, status,
  120. otg->gadget);
  121. }
  122. return IRQ_HANDLED;
  123. }
  124. int usb_gen_phy_init(struct usb_phy *phy)
  125. {
  126. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  127. int ret;
  128. if (!IS_ERR(nop->vcc)) {
  129. if (regulator_enable(nop->vcc))
  130. dev_err(phy->dev, "Failed to enable power\n");
  131. }
  132. if (!IS_ERR(nop->clk)) {
  133. ret = clk_prepare_enable(nop->clk);
  134. if (ret)
  135. return ret;
  136. }
  137. nop_reset(nop);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL_GPL(usb_gen_phy_init);
  141. void usb_gen_phy_shutdown(struct usb_phy *phy)
  142. {
  143. struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
  144. gpiod_set_value(nop->gpiod_reset, 1);
  145. if (!IS_ERR(nop->clk))
  146. clk_disable_unprepare(nop->clk);
  147. if (!IS_ERR(nop->vcc)) {
  148. if (regulator_disable(nop->vcc))
  149. dev_err(phy->dev, "Failed to disable power\n");
  150. }
  151. }
  152. EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
  153. static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
  154. {
  155. if (!otg)
  156. return -ENODEV;
  157. if (!gadget) {
  158. otg->gadget = NULL;
  159. return -ENODEV;
  160. }
  161. otg->gadget = gadget;
  162. if (otg->state == OTG_STATE_B_PERIPHERAL)
  163. usb_gadget_vbus_connect(gadget);
  164. else
  165. otg->state = OTG_STATE_B_IDLE;
  166. return 0;
  167. }
  168. static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
  169. {
  170. if (!otg)
  171. return -ENODEV;
  172. if (!host) {
  173. otg->host = NULL;
  174. return -ENODEV;
  175. }
  176. otg->host = host;
  177. return 0;
  178. }
  179. int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop,
  180. struct usb_phy_generic_platform_data *pdata)
  181. {
  182. enum usb_phy_type type = USB_PHY_TYPE_USB2;
  183. int err = 0;
  184. u32 clk_rate = 0;
  185. bool needs_vcc = false;
  186. if (dev->of_node) {
  187. struct device_node *node = dev->of_node;
  188. if (of_property_read_u32(node, "clock-frequency", &clk_rate))
  189. clk_rate = 0;
  190. needs_vcc = of_property_read_bool(node, "vcc-supply");
  191. nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
  192. GPIOD_ASIS);
  193. err = PTR_ERR_OR_ZERO(nop->gpiod_reset);
  194. if (!err) {
  195. nop->gpiod_vbus = devm_gpiod_get_optional(dev,
  196. "vbus-detect",
  197. GPIOD_ASIS);
  198. err = PTR_ERR_OR_ZERO(nop->gpiod_vbus);
  199. }
  200. } else if (pdata) {
  201. type = pdata->type;
  202. clk_rate = pdata->clk_rate;
  203. needs_vcc = pdata->needs_vcc;
  204. if (gpio_is_valid(pdata->gpio_reset)) {
  205. err = devm_gpio_request_one(dev, pdata->gpio_reset,
  206. GPIOF_ACTIVE_LOW,
  207. dev_name(dev));
  208. if (!err)
  209. nop->gpiod_reset =
  210. gpio_to_desc(pdata->gpio_reset);
  211. }
  212. nop->gpiod_vbus = pdata->gpiod_vbus;
  213. }
  214. if (err == -EPROBE_DEFER)
  215. return -EPROBE_DEFER;
  216. if (err) {
  217. dev_err(dev, "Error requesting RESET or VBUS GPIO\n");
  218. return err;
  219. }
  220. if (nop->gpiod_reset)
  221. gpiod_direction_output(nop->gpiod_reset, 1);
  222. nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
  223. GFP_KERNEL);
  224. if (!nop->phy.otg)
  225. return -ENOMEM;
  226. nop->clk = devm_clk_get(dev, "main_clk");
  227. if (IS_ERR(nop->clk)) {
  228. dev_dbg(dev, "Can't get phy clock: %ld\n",
  229. PTR_ERR(nop->clk));
  230. }
  231. if (!IS_ERR(nop->clk) && clk_rate) {
  232. err = clk_set_rate(nop->clk, clk_rate);
  233. if (err) {
  234. dev_err(dev, "Error setting clock rate\n");
  235. return err;
  236. }
  237. }
  238. nop->vcc = devm_regulator_get(dev, "vcc");
  239. if (IS_ERR(nop->vcc)) {
  240. dev_dbg(dev, "Error getting vcc regulator: %ld\n",
  241. PTR_ERR(nop->vcc));
  242. if (needs_vcc)
  243. return -EPROBE_DEFER;
  244. }
  245. nop->dev = dev;
  246. nop->phy.dev = nop->dev;
  247. nop->phy.label = "nop-xceiv";
  248. nop->phy.set_suspend = nop_set_suspend;
  249. nop->phy.type = type;
  250. nop->phy.otg->state = OTG_STATE_UNDEFINED;
  251. nop->phy.otg->usb_phy = &nop->phy;
  252. nop->phy.otg->set_host = nop_set_host;
  253. nop->phy.otg->set_peripheral = nop_set_peripheral;
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
  257. static int usb_phy_generic_probe(struct platform_device *pdev)
  258. {
  259. struct device *dev = &pdev->dev;
  260. struct usb_phy_generic *nop;
  261. int err;
  262. nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
  263. if (!nop)
  264. return -ENOMEM;
  265. err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev));
  266. if (err)
  267. return err;
  268. if (nop->gpiod_vbus) {
  269. err = devm_request_threaded_irq(&pdev->dev,
  270. gpiod_to_irq(nop->gpiod_vbus),
  271. NULL, nop_gpio_vbus_thread,
  272. VBUS_IRQ_FLAGS, "vbus_detect",
  273. nop);
  274. if (err) {
  275. dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
  276. gpiod_to_irq(nop->gpiod_vbus), err);
  277. return err;
  278. }
  279. }
  280. nop->phy.init = usb_gen_phy_init;
  281. nop->phy.shutdown = usb_gen_phy_shutdown;
  282. err = usb_add_phy_dev(&nop->phy);
  283. if (err) {
  284. dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
  285. err);
  286. return err;
  287. }
  288. platform_set_drvdata(pdev, nop);
  289. return 0;
  290. }
  291. static int usb_phy_generic_remove(struct platform_device *pdev)
  292. {
  293. struct usb_phy_generic *nop = platform_get_drvdata(pdev);
  294. usb_remove_phy(&nop->phy);
  295. return 0;
  296. }
  297. static const struct of_device_id nop_xceiv_dt_ids[] = {
  298. { .compatible = "usb-nop-xceiv" },
  299. { }
  300. };
  301. MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
  302. static struct platform_driver usb_phy_generic_driver = {
  303. .probe = usb_phy_generic_probe,
  304. .remove = usb_phy_generic_remove,
  305. .driver = {
  306. .name = "usb_phy_generic",
  307. .of_match_table = nop_xceiv_dt_ids,
  308. },
  309. };
  310. static int __init usb_phy_generic_init(void)
  311. {
  312. return platform_driver_register(&usb_phy_generic_driver);
  313. }
  314. subsys_initcall(usb_phy_generic_init);
  315. static void __exit usb_phy_generic_exit(void)
  316. {
  317. platform_driver_unregister(&usb_phy_generic_driver);
  318. }
  319. module_exit(usb_phy_generic_exit);
  320. MODULE_ALIAS("platform:usb_phy_generic");
  321. MODULE_AUTHOR("Texas Instruments Inc");
  322. MODULE_DESCRIPTION("NOP USB Transceiver driver");
  323. MODULE_LICENSE("GPL");