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