phy-generic.c 9.1 KB

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