phy-omap-usb2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/mfd/syscon.h>
  32. #include <linux/regmap.h>
  33. #include <linux/of_platform.h>
  34. #define USB2PHY_DISCON_BYP_LATCH (1 << 31)
  35. #define USB2PHY_ANA_CONFIG1 0x4c
  36. /**
  37. * omap_usb2_set_comparator - links the comparator present in the sytem with
  38. * this phy
  39. * @comparator - the companion phy(comparator) for this phy
  40. *
  41. * The phy companion driver should call this API passing the phy_companion
  42. * filled with set_vbus and start_srp to be used by usb phy.
  43. *
  44. * For use by phy companion driver
  45. */
  46. int omap_usb2_set_comparator(struct phy_companion *comparator)
  47. {
  48. struct omap_usb *phy;
  49. struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
  50. if (IS_ERR(x))
  51. return -ENODEV;
  52. phy = phy_to_omapusb(x);
  53. phy->comparator = comparator;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
  57. static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
  58. {
  59. struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
  60. if (!phy->comparator)
  61. return -ENODEV;
  62. return phy->comparator->set_vbus(phy->comparator, enabled);
  63. }
  64. static int omap_usb_start_srp(struct usb_otg *otg)
  65. {
  66. struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
  67. if (!phy->comparator)
  68. return -ENODEV;
  69. return phy->comparator->start_srp(phy->comparator);
  70. }
  71. static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  72. {
  73. otg->host = host;
  74. if (!host)
  75. otg->state = OTG_STATE_UNDEFINED;
  76. return 0;
  77. }
  78. static int omap_usb_set_peripheral(struct usb_otg *otg,
  79. struct usb_gadget *gadget)
  80. {
  81. otg->gadget = gadget;
  82. if (!gadget)
  83. otg->state = OTG_STATE_UNDEFINED;
  84. return 0;
  85. }
  86. static int omap_usb_phy_power(struct omap_usb *phy, int on)
  87. {
  88. u32 val;
  89. int ret;
  90. if (!phy->syscon_phy_power) {
  91. omap_control_phy_power(phy->control_dev, on);
  92. return 0;
  93. }
  94. if (on)
  95. val = phy->power_on;
  96. else
  97. val = phy->power_off;
  98. ret = regmap_update_bits(phy->syscon_phy_power, phy->power_reg,
  99. phy->mask, val);
  100. return ret;
  101. }
  102. static int omap_usb_power_off(struct phy *x)
  103. {
  104. struct omap_usb *phy = phy_get_drvdata(x);
  105. return omap_usb_phy_power(phy, false);
  106. }
  107. static int omap_usb_power_on(struct phy *x)
  108. {
  109. struct omap_usb *phy = phy_get_drvdata(x);
  110. return omap_usb_phy_power(phy, true);
  111. }
  112. static int omap_usb2_disable_clocks(struct omap_usb *phy)
  113. {
  114. clk_disable(phy->wkupclk);
  115. if (!IS_ERR(phy->optclk))
  116. clk_disable(phy->optclk);
  117. return 0;
  118. }
  119. static int omap_usb2_enable_clocks(struct omap_usb *phy)
  120. {
  121. int ret;
  122. ret = clk_enable(phy->wkupclk);
  123. if (ret < 0) {
  124. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  125. goto err0;
  126. }
  127. if (!IS_ERR(phy->optclk)) {
  128. ret = clk_enable(phy->optclk);
  129. if (ret < 0) {
  130. dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
  131. goto err1;
  132. }
  133. }
  134. return 0;
  135. err1:
  136. clk_disable(phy->wkupclk);
  137. err0:
  138. return ret;
  139. }
  140. static int omap_usb_init(struct phy *x)
  141. {
  142. struct omap_usb *phy = phy_get_drvdata(x);
  143. u32 val;
  144. omap_usb2_enable_clocks(phy);
  145. if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
  146. /*
  147. *
  148. * Reduce the sensitivity of internal PHY by enabling the
  149. * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
  150. * resolves issues with certain devices which can otherwise
  151. * be prone to false disconnects.
  152. *
  153. */
  154. val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
  155. val |= USB2PHY_DISCON_BYP_LATCH;
  156. omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
  157. }
  158. return 0;
  159. }
  160. static int omap_usb_exit(struct phy *x)
  161. {
  162. struct omap_usb *phy = phy_get_drvdata(x);
  163. return omap_usb2_disable_clocks(phy);
  164. }
  165. static const struct phy_ops ops = {
  166. .init = omap_usb_init,
  167. .exit = omap_usb_exit,
  168. .power_on = omap_usb_power_on,
  169. .power_off = omap_usb_power_off,
  170. .owner = THIS_MODULE,
  171. };
  172. static const struct usb_phy_data omap_usb2_data = {
  173. .label = "omap_usb2",
  174. .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
  175. .mask = OMAP_DEV_PHY_PD,
  176. .power_off = OMAP_DEV_PHY_PD,
  177. };
  178. static const struct usb_phy_data omap5_usb2_data = {
  179. .label = "omap5_usb2",
  180. .flags = 0,
  181. .mask = OMAP_DEV_PHY_PD,
  182. .power_off = OMAP_DEV_PHY_PD,
  183. };
  184. static const struct usb_phy_data dra7x_usb2_data = {
  185. .label = "dra7x_usb2",
  186. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  187. .mask = OMAP_DEV_PHY_PD,
  188. .power_off = OMAP_DEV_PHY_PD,
  189. };
  190. static const struct usb_phy_data dra7x_usb2_phy2_data = {
  191. .label = "dra7x_usb2_phy2",
  192. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  193. .mask = OMAP_USB2_PHY_PD,
  194. .power_off = OMAP_USB2_PHY_PD,
  195. };
  196. static const struct usb_phy_data am437x_usb2_data = {
  197. .label = "am437x_usb2",
  198. .flags = 0,
  199. .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
  200. AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
  201. .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
  202. .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
  203. };
  204. static const struct of_device_id omap_usb2_id_table[] = {
  205. {
  206. .compatible = "ti,omap-usb2",
  207. .data = &omap_usb2_data,
  208. },
  209. {
  210. .compatible = "ti,omap5-usb2",
  211. .data = &omap5_usb2_data,
  212. },
  213. {
  214. .compatible = "ti,dra7x-usb2",
  215. .data = &dra7x_usb2_data,
  216. },
  217. {
  218. .compatible = "ti,dra7x-usb2-phy2",
  219. .data = &dra7x_usb2_phy2_data,
  220. },
  221. {
  222. .compatible = "ti,am437x-usb2",
  223. .data = &am437x_usb2_data,
  224. },
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
  228. static int omap_usb2_probe(struct platform_device *pdev)
  229. {
  230. struct omap_usb *phy;
  231. struct phy *generic_phy;
  232. struct resource *res;
  233. struct phy_provider *phy_provider;
  234. struct usb_otg *otg;
  235. struct device_node *node = pdev->dev.of_node;
  236. struct device_node *control_node;
  237. struct platform_device *control_pdev;
  238. const struct of_device_id *of_id;
  239. struct usb_phy_data *phy_data;
  240. of_id = of_match_device(omap_usb2_id_table, &pdev->dev);
  241. if (!of_id)
  242. return -EINVAL;
  243. phy_data = (struct usb_phy_data *)of_id->data;
  244. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  245. if (!phy)
  246. return -ENOMEM;
  247. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  248. if (!otg)
  249. return -ENOMEM;
  250. phy->dev = &pdev->dev;
  251. phy->phy.dev = phy->dev;
  252. phy->phy.label = phy_data->label;
  253. phy->phy.otg = otg;
  254. phy->phy.type = USB_PHY_TYPE_USB2;
  255. phy->mask = phy_data->mask;
  256. phy->power_on = phy_data->power_on;
  257. phy->power_off = phy_data->power_off;
  258. if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
  259. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
  261. if (IS_ERR(phy->phy_base))
  262. return PTR_ERR(phy->phy_base);
  263. phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
  264. }
  265. phy->syscon_phy_power = syscon_regmap_lookup_by_phandle(node,
  266. "syscon-phy-power");
  267. if (IS_ERR(phy->syscon_phy_power)) {
  268. dev_dbg(&pdev->dev,
  269. "can't get syscon-phy-power, using control device\n");
  270. phy->syscon_phy_power = NULL;
  271. control_node = of_parse_phandle(node, "ctrl-module", 0);
  272. if (!control_node) {
  273. dev_err(&pdev->dev,
  274. "Failed to get control device phandle\n");
  275. return -EINVAL;
  276. }
  277. control_pdev = of_find_device_by_node(control_node);
  278. if (!control_pdev) {
  279. dev_err(&pdev->dev, "Failed to get control device\n");
  280. return -EINVAL;
  281. }
  282. phy->control_dev = &control_pdev->dev;
  283. } else {
  284. if (of_property_read_u32_index(node,
  285. "syscon-phy-power", 1,
  286. &phy->power_reg)) {
  287. dev_err(&pdev->dev,
  288. "couldn't get power reg. offset\n");
  289. return -EINVAL;
  290. }
  291. }
  292. otg->set_host = omap_usb_set_host;
  293. otg->set_peripheral = omap_usb_set_peripheral;
  294. if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
  295. otg->set_vbus = omap_usb_set_vbus;
  296. if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
  297. otg->start_srp = omap_usb_start_srp;
  298. otg->usb_phy = &phy->phy;
  299. platform_set_drvdata(pdev, phy);
  300. pm_runtime_enable(phy->dev);
  301. generic_phy = devm_phy_create(phy->dev, NULL, &ops);
  302. if (IS_ERR(generic_phy)) {
  303. pm_runtime_disable(phy->dev);
  304. return PTR_ERR(generic_phy);
  305. }
  306. phy_set_drvdata(generic_phy, phy);
  307. omap_usb_power_off(generic_phy);
  308. phy_provider = devm_of_phy_provider_register(phy->dev,
  309. of_phy_simple_xlate);
  310. if (IS_ERR(phy_provider)) {
  311. pm_runtime_disable(phy->dev);
  312. return PTR_ERR(phy_provider);
  313. }
  314. phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
  315. if (IS_ERR(phy->wkupclk)) {
  316. dev_warn(&pdev->dev, "unable to get wkupclk, trying old name\n");
  317. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  318. if (IS_ERR(phy->wkupclk)) {
  319. dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
  320. pm_runtime_disable(phy->dev);
  321. return PTR_ERR(phy->wkupclk);
  322. } else {
  323. dev_warn(&pdev->dev,
  324. "found usb_phy_cm_clk32k, please fix DTS\n");
  325. }
  326. }
  327. clk_prepare(phy->wkupclk);
  328. phy->optclk = devm_clk_get(phy->dev, "refclk");
  329. if (IS_ERR(phy->optclk)) {
  330. dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
  331. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  332. if (IS_ERR(phy->optclk)) {
  333. dev_dbg(&pdev->dev,
  334. "unable to get usb_otg_ss_refclk960m\n");
  335. } else {
  336. dev_warn(&pdev->dev,
  337. "found usb_otg_ss_refclk960m, please fix DTS\n");
  338. }
  339. }
  340. if (!IS_ERR(phy->optclk))
  341. clk_prepare(phy->optclk);
  342. usb_add_phy_dev(&phy->phy);
  343. return 0;
  344. }
  345. static int omap_usb2_remove(struct platform_device *pdev)
  346. {
  347. struct omap_usb *phy = platform_get_drvdata(pdev);
  348. clk_unprepare(phy->wkupclk);
  349. if (!IS_ERR(phy->optclk))
  350. clk_unprepare(phy->optclk);
  351. usb_remove_phy(&phy->phy);
  352. pm_runtime_disable(phy->dev);
  353. return 0;
  354. }
  355. static struct platform_driver omap_usb2_driver = {
  356. .probe = omap_usb2_probe,
  357. .remove = omap_usb2_remove,
  358. .driver = {
  359. .name = "omap-usb2",
  360. .of_match_table = omap_usb2_id_table,
  361. },
  362. };
  363. module_platform_driver(omap_usb2_driver);
  364. MODULE_ALIAS("platform:omap_usb2");
  365. MODULE_AUTHOR("Texas Instruments Inc.");
  366. MODULE_DESCRIPTION("OMAP USB2 phy driver");
  367. MODULE_LICENSE("GPL v2");