phy-omap-usb2.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * omap-usb2.c - USB PHY, talking to musb controller in OMAP.
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/io.h>
  23. #include <linux/phy/omap_usb.h>
  24. #include <linux/usb/phy_companion.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/delay.h>
  29. #include <linux/phy/omap_control_phy.h>
  30. #include <linux/phy/phy.h>
  31. #include <linux/of_platform.h>
  32. #define USB2PHY_DISCON_BYP_LATCH (1 << 31)
  33. #define USB2PHY_ANA_CONFIG1 0x4c
  34. /**
  35. * omap_usb2_set_comparator - links the comparator present in the sytem with
  36. * this phy
  37. * @comparator - the companion phy(comparator) for this phy
  38. *
  39. * The phy companion driver should call this API passing the phy_companion
  40. * filled with set_vbus and start_srp to be used by usb phy.
  41. *
  42. * For use by phy companion driver
  43. */
  44. int omap_usb2_set_comparator(struct phy_companion *comparator)
  45. {
  46. struct omap_usb *phy;
  47. struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
  48. if (IS_ERR(x))
  49. return -ENODEV;
  50. phy = phy_to_omapusb(x);
  51. phy->comparator = comparator;
  52. return 0;
  53. }
  54. EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
  55. static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
  56. {
  57. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  58. if (!phy->comparator)
  59. return -ENODEV;
  60. return phy->comparator->set_vbus(phy->comparator, enabled);
  61. }
  62. static int omap_usb_start_srp(struct usb_otg *otg)
  63. {
  64. struct omap_usb *phy = phy_to_omapusb(otg->phy);
  65. if (!phy->comparator)
  66. return -ENODEV;
  67. return phy->comparator->start_srp(phy->comparator);
  68. }
  69. static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  70. {
  71. struct usb_phy *phy = otg->phy;
  72. otg->host = host;
  73. if (!host)
  74. phy->state = OTG_STATE_UNDEFINED;
  75. return 0;
  76. }
  77. static int omap_usb_set_peripheral(struct usb_otg *otg,
  78. struct usb_gadget *gadget)
  79. {
  80. struct usb_phy *phy = otg->phy;
  81. otg->gadget = gadget;
  82. if (!gadget)
  83. phy->state = OTG_STATE_UNDEFINED;
  84. return 0;
  85. }
  86. static int omap_usb_power_off(struct phy *x)
  87. {
  88. struct omap_usb *phy = phy_get_drvdata(x);
  89. omap_control_phy_power(phy->control_dev, 0);
  90. return 0;
  91. }
  92. static int omap_usb_power_on(struct phy *x)
  93. {
  94. struct omap_usb *phy = phy_get_drvdata(x);
  95. omap_control_phy_power(phy->control_dev, 1);
  96. return 0;
  97. }
  98. static int omap_usb_init(struct phy *x)
  99. {
  100. struct omap_usb *phy = phy_get_drvdata(x);
  101. u32 val;
  102. if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
  103. /*
  104. *
  105. * Reduce the sensitivity of internal PHY by enabling the
  106. * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
  107. * resolves issues with certain devices which can otherwise
  108. * be prone to false disconnects.
  109. *
  110. */
  111. val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
  112. val |= USB2PHY_DISCON_BYP_LATCH;
  113. omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
  114. }
  115. return 0;
  116. }
  117. static struct phy_ops ops = {
  118. .init = omap_usb_init,
  119. .power_on = omap_usb_power_on,
  120. .power_off = omap_usb_power_off,
  121. .owner = THIS_MODULE,
  122. };
  123. #ifdef CONFIG_OF
  124. static const struct usb_phy_data omap_usb2_data = {
  125. .label = "omap_usb2",
  126. .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
  127. };
  128. static const struct usb_phy_data omap5_usb2_data = {
  129. .label = "omap5_usb2",
  130. .flags = 0,
  131. };
  132. static const struct usb_phy_data dra7x_usb2_data = {
  133. .label = "dra7x_usb2",
  134. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  135. };
  136. static const struct usb_phy_data am437x_usb2_data = {
  137. .label = "am437x_usb2",
  138. .flags = 0,
  139. };
  140. static const struct of_device_id omap_usb2_id_table[] = {
  141. {
  142. .compatible = "ti,omap-usb2",
  143. .data = &omap_usb2_data,
  144. },
  145. {
  146. .compatible = "ti,omap5-usb2",
  147. .data = &omap5_usb2_data,
  148. },
  149. {
  150. .compatible = "ti,dra7x-usb2",
  151. .data = &dra7x_usb2_data,
  152. },
  153. {
  154. .compatible = "ti,am437x-usb2",
  155. .data = &am437x_usb2_data,
  156. },
  157. {},
  158. };
  159. MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
  160. #endif
  161. static int omap_usb2_probe(struct platform_device *pdev)
  162. {
  163. struct omap_usb *phy;
  164. struct phy *generic_phy;
  165. struct resource *res;
  166. struct phy_provider *phy_provider;
  167. struct usb_otg *otg;
  168. struct device_node *node = pdev->dev.of_node;
  169. struct device_node *control_node;
  170. struct platform_device *control_pdev;
  171. const struct of_device_id *of_id;
  172. struct usb_phy_data *phy_data;
  173. of_id = of_match_device(of_match_ptr(omap_usb2_id_table), &pdev->dev);
  174. if (!of_id)
  175. return -EINVAL;
  176. phy_data = (struct usb_phy_data *)of_id->data;
  177. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  178. if (!phy) {
  179. dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
  180. return -ENOMEM;
  181. }
  182. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  183. if (!otg) {
  184. dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
  185. return -ENOMEM;
  186. }
  187. phy->dev = &pdev->dev;
  188. phy->phy.dev = phy->dev;
  189. phy->phy.label = phy_data->label;
  190. phy->phy.otg = otg;
  191. phy->phy.type = USB_PHY_TYPE_USB2;
  192. if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
  193. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  194. phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
  195. if (IS_ERR(phy->phy_base))
  196. return PTR_ERR(phy->phy_base);
  197. phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
  198. }
  199. control_node = of_parse_phandle(node, "ctrl-module", 0);
  200. if (!control_node) {
  201. dev_err(&pdev->dev, "Failed to get control device phandle\n");
  202. return -EINVAL;
  203. }
  204. control_pdev = of_find_device_by_node(control_node);
  205. if (!control_pdev) {
  206. dev_err(&pdev->dev, "Failed to get control device\n");
  207. return -EINVAL;
  208. }
  209. phy->control_dev = &control_pdev->dev;
  210. omap_control_phy_power(phy->control_dev, 0);
  211. otg->set_host = omap_usb_set_host;
  212. otg->set_peripheral = omap_usb_set_peripheral;
  213. if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
  214. otg->set_vbus = omap_usb_set_vbus;
  215. if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
  216. otg->start_srp = omap_usb_start_srp;
  217. otg->phy = &phy->phy;
  218. platform_set_drvdata(pdev, phy);
  219. generic_phy = devm_phy_create(phy->dev, &ops, NULL);
  220. if (IS_ERR(generic_phy))
  221. return PTR_ERR(generic_phy);
  222. phy_set_drvdata(generic_phy, phy);
  223. pm_runtime_enable(phy->dev);
  224. phy_provider = devm_of_phy_provider_register(phy->dev,
  225. of_phy_simple_xlate);
  226. if (IS_ERR(phy_provider)) {
  227. pm_runtime_disable(phy->dev);
  228. return PTR_ERR(phy_provider);
  229. }
  230. phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
  231. if (IS_ERR(phy->wkupclk)) {
  232. dev_warn(&pdev->dev, "unable to get wkupclk, trying old name\n");
  233. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  234. if (IS_ERR(phy->wkupclk)) {
  235. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  236. return PTR_ERR(phy->wkupclk);
  237. } else {
  238. dev_warn(&pdev->dev,
  239. "found usb_phy_cm_clk32k, please fix DTS\n");
  240. }
  241. }
  242. clk_prepare(phy->wkupclk);
  243. phy->optclk = devm_clk_get(phy->dev, "refclk");
  244. if (IS_ERR(phy->optclk)) {
  245. dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
  246. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  247. if (IS_ERR(phy->optclk)) {
  248. dev_dbg(&pdev->dev,
  249. "unable to get usb_otg_ss_refclk960m\n");
  250. } else {
  251. dev_warn(&pdev->dev,
  252. "found usb_otg_ss_refclk960m, please fix DTS\n");
  253. }
  254. } else {
  255. clk_prepare(phy->optclk);
  256. }
  257. usb_add_phy_dev(&phy->phy);
  258. return 0;
  259. }
  260. static int omap_usb2_remove(struct platform_device *pdev)
  261. {
  262. struct omap_usb *phy = platform_get_drvdata(pdev);
  263. clk_unprepare(phy->wkupclk);
  264. if (!IS_ERR(phy->optclk))
  265. clk_unprepare(phy->optclk);
  266. usb_remove_phy(&phy->phy);
  267. pm_runtime_disable(phy->dev);
  268. return 0;
  269. }
  270. #ifdef CONFIG_PM_RUNTIME
  271. static int omap_usb2_runtime_suspend(struct device *dev)
  272. {
  273. struct platform_device *pdev = to_platform_device(dev);
  274. struct omap_usb *phy = platform_get_drvdata(pdev);
  275. clk_disable(phy->wkupclk);
  276. if (!IS_ERR(phy->optclk))
  277. clk_disable(phy->optclk);
  278. return 0;
  279. }
  280. static int omap_usb2_runtime_resume(struct device *dev)
  281. {
  282. struct platform_device *pdev = to_platform_device(dev);
  283. struct omap_usb *phy = platform_get_drvdata(pdev);
  284. int ret;
  285. ret = clk_enable(phy->wkupclk);
  286. if (ret < 0) {
  287. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  288. goto err0;
  289. }
  290. if (!IS_ERR(phy->optclk)) {
  291. ret = clk_enable(phy->optclk);
  292. if (ret < 0) {
  293. dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
  294. goto err1;
  295. }
  296. }
  297. return 0;
  298. err1:
  299. clk_disable(phy->wkupclk);
  300. err0:
  301. return ret;
  302. }
  303. static const struct dev_pm_ops omap_usb2_pm_ops = {
  304. SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
  305. NULL)
  306. };
  307. #define DEV_PM_OPS (&omap_usb2_pm_ops)
  308. #else
  309. #define DEV_PM_OPS NULL
  310. #endif
  311. static struct platform_driver omap_usb2_driver = {
  312. .probe = omap_usb2_probe,
  313. .remove = omap_usb2_remove,
  314. .driver = {
  315. .name = "omap-usb2",
  316. .owner = THIS_MODULE,
  317. .pm = DEV_PM_OPS,
  318. .of_match_table = of_match_ptr(omap_usb2_id_table),
  319. },
  320. };
  321. module_platform_driver(omap_usb2_driver);
  322. MODULE_ALIAS("platform: omap_usb2");
  323. MODULE_AUTHOR("Texas Instruments Inc.");
  324. MODULE_DESCRIPTION("OMAP USB2 phy driver");
  325. MODULE_LICENSE("GPL v2");