phy-omap-control.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * omap-control-phy.c - The PHY part of control module.
  3. *
  4. * Copyright (C) 2013 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/of_device.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/clk.h>
  26. #include <linux/phy/omap_control_phy.h>
  27. /**
  28. * omap_control_pcie_pcs - set the PCS delay count
  29. * @dev: the control module device
  30. * @delay: 8 bit delay value
  31. */
  32. void omap_control_pcie_pcs(struct device *dev, u8 delay)
  33. {
  34. u32 val;
  35. struct omap_control_phy *control_phy;
  36. if (IS_ERR(dev) || !dev) {
  37. pr_err("%s: invalid device\n", __func__);
  38. return;
  39. }
  40. control_phy = dev_get_drvdata(dev);
  41. if (!control_phy) {
  42. dev_err(dev, "%s: invalid control phy device\n", __func__);
  43. return;
  44. }
  45. if (control_phy->type != OMAP_CTRL_TYPE_PCIE) {
  46. dev_err(dev, "%s: unsupported operation\n", __func__);
  47. return;
  48. }
  49. val = readl(control_phy->pcie_pcs);
  50. val &= ~(OMAP_CTRL_PCIE_PCS_MASK <<
  51. OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT);
  52. val |= (delay << OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT);
  53. writel(val, control_phy->pcie_pcs);
  54. }
  55. EXPORT_SYMBOL_GPL(omap_control_pcie_pcs);
  56. /**
  57. * omap_control_phy_power - power on/off the phy using control module reg
  58. * @dev: the control module device
  59. * @on: 0 or 1, based on powering on or off the PHY
  60. */
  61. void omap_control_phy_power(struct device *dev, int on)
  62. {
  63. u32 val;
  64. unsigned long rate;
  65. struct omap_control_phy *control_phy;
  66. if (IS_ERR(dev) || !dev) {
  67. pr_err("%s: invalid device\n", __func__);
  68. return;
  69. }
  70. control_phy = dev_get_drvdata(dev);
  71. if (!control_phy) {
  72. dev_err(dev, "%s: invalid control phy device\n", __func__);
  73. return;
  74. }
  75. if (control_phy->type == OMAP_CTRL_TYPE_OTGHS)
  76. return;
  77. val = readl(control_phy->power);
  78. switch (control_phy->type) {
  79. case OMAP_CTRL_TYPE_USB2:
  80. if (on)
  81. val &= ~OMAP_CTRL_DEV_PHY_PD;
  82. else
  83. val |= OMAP_CTRL_DEV_PHY_PD;
  84. break;
  85. case OMAP_CTRL_TYPE_PCIE:
  86. case OMAP_CTRL_TYPE_PIPE3:
  87. rate = clk_get_rate(control_phy->sys_clk);
  88. rate = rate/1000000;
  89. if (on) {
  90. val &= ~(OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK |
  91. OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK);
  92. val |= OMAP_CTRL_PIPE3_PHY_TX_RX_POWERON <<
  93. OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT;
  94. val |= rate <<
  95. OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT;
  96. } else {
  97. val &= ~OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK;
  98. val |= OMAP_CTRL_PIPE3_PHY_TX_RX_POWEROFF <<
  99. OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT;
  100. }
  101. break;
  102. case OMAP_CTRL_TYPE_DRA7USB2:
  103. if (on)
  104. val &= ~OMAP_CTRL_USB2_PHY_PD;
  105. else
  106. val |= OMAP_CTRL_USB2_PHY_PD;
  107. break;
  108. case OMAP_CTRL_TYPE_AM437USB2:
  109. if (on) {
  110. val &= ~(AM437X_CTRL_USB2_PHY_PD |
  111. AM437X_CTRL_USB2_OTG_PD);
  112. val |= (AM437X_CTRL_USB2_OTGVDET_EN |
  113. AM437X_CTRL_USB2_OTGSESSEND_EN);
  114. } else {
  115. val &= ~(AM437X_CTRL_USB2_OTGVDET_EN |
  116. AM437X_CTRL_USB2_OTGSESSEND_EN);
  117. val |= (AM437X_CTRL_USB2_PHY_PD |
  118. AM437X_CTRL_USB2_OTG_PD);
  119. }
  120. break;
  121. default:
  122. dev_err(dev, "%s: type %d not recognized\n",
  123. __func__, control_phy->type);
  124. break;
  125. }
  126. writel(val, control_phy->power);
  127. }
  128. EXPORT_SYMBOL_GPL(omap_control_phy_power);
  129. /**
  130. * omap_control_usb_host_mode - set AVALID, VBUSVALID and ID pin in grounded
  131. * @ctrl_phy: struct omap_control_phy *
  132. *
  133. * Writes to the mailbox register to notify the usb core that a usb
  134. * device has been connected.
  135. */
  136. static void omap_control_usb_host_mode(struct omap_control_phy *ctrl_phy)
  137. {
  138. u32 val;
  139. val = readl(ctrl_phy->otghs_control);
  140. val &= ~(OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND);
  141. val |= OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID;
  142. writel(val, ctrl_phy->otghs_control);
  143. }
  144. /**
  145. * omap_control_usb_device_mode - set AVALID, VBUSVALID and ID pin in high
  146. * impedance
  147. * @ctrl_phy: struct omap_control_phy *
  148. *
  149. * Writes to the mailbox register to notify the usb core that it has been
  150. * connected to a usb host.
  151. */
  152. static void omap_control_usb_device_mode(struct omap_control_phy *ctrl_phy)
  153. {
  154. u32 val;
  155. val = readl(ctrl_phy->otghs_control);
  156. val &= ~OMAP_CTRL_DEV_SESSEND;
  157. val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_AVALID |
  158. OMAP_CTRL_DEV_VBUSVALID;
  159. writel(val, ctrl_phy->otghs_control);
  160. }
  161. /**
  162. * omap_control_usb_set_sessionend - Enable SESSIONEND and IDIG to high
  163. * impedance
  164. * @ctrl_phy: struct omap_control_phy *
  165. *
  166. * Writes to the mailbox register to notify the usb core it's now in
  167. * disconnected state.
  168. */
  169. static void omap_control_usb_set_sessionend(struct omap_control_phy *ctrl_phy)
  170. {
  171. u32 val;
  172. val = readl(ctrl_phy->otghs_control);
  173. val &= ~(OMAP_CTRL_DEV_AVALID | OMAP_CTRL_DEV_VBUSVALID);
  174. val |= OMAP_CTRL_DEV_IDDIG | OMAP_CTRL_DEV_SESSEND;
  175. writel(val, ctrl_phy->otghs_control);
  176. }
  177. /**
  178. * omap_control_usb_set_mode - Calls to functions to set USB in one of host mode
  179. * or device mode or to denote disconnected state
  180. * @dev: the control module device
  181. * @mode: The mode to which usb should be configured
  182. *
  183. * This is an API to write to the mailbox register to notify the usb core that
  184. * a usb device has been connected.
  185. */
  186. void omap_control_usb_set_mode(struct device *dev,
  187. enum omap_control_usb_mode mode)
  188. {
  189. struct omap_control_phy *ctrl_phy;
  190. if (IS_ERR(dev) || !dev)
  191. return;
  192. ctrl_phy = dev_get_drvdata(dev);
  193. if (!ctrl_phy) {
  194. dev_err(dev, "Invalid control phy device\n");
  195. return;
  196. }
  197. if (ctrl_phy->type != OMAP_CTRL_TYPE_OTGHS)
  198. return;
  199. switch (mode) {
  200. case USB_MODE_HOST:
  201. omap_control_usb_host_mode(ctrl_phy);
  202. break;
  203. case USB_MODE_DEVICE:
  204. omap_control_usb_device_mode(ctrl_phy);
  205. break;
  206. case USB_MODE_DISCONNECT:
  207. omap_control_usb_set_sessionend(ctrl_phy);
  208. break;
  209. default:
  210. dev_vdbg(dev, "invalid omap control usb mode\n");
  211. }
  212. }
  213. EXPORT_SYMBOL_GPL(omap_control_usb_set_mode);
  214. static const enum omap_control_phy_type otghs_data = OMAP_CTRL_TYPE_OTGHS;
  215. static const enum omap_control_phy_type usb2_data = OMAP_CTRL_TYPE_USB2;
  216. static const enum omap_control_phy_type pipe3_data = OMAP_CTRL_TYPE_PIPE3;
  217. static const enum omap_control_phy_type pcie_data = OMAP_CTRL_TYPE_PCIE;
  218. static const enum omap_control_phy_type dra7usb2_data = OMAP_CTRL_TYPE_DRA7USB2;
  219. static const enum omap_control_phy_type am437usb2_data = OMAP_CTRL_TYPE_AM437USB2;
  220. static const struct of_device_id omap_control_phy_id_table[] = {
  221. {
  222. .compatible = "ti,control-phy-otghs",
  223. .data = &otghs_data,
  224. },
  225. {
  226. .compatible = "ti,control-phy-usb2",
  227. .data = &usb2_data,
  228. },
  229. {
  230. .compatible = "ti,control-phy-pipe3",
  231. .data = &pipe3_data,
  232. },
  233. {
  234. .compatible = "ti,control-phy-pcie",
  235. .data = &pcie_data,
  236. },
  237. {
  238. .compatible = "ti,control-phy-usb2-dra7",
  239. .data = &dra7usb2_data,
  240. },
  241. {
  242. .compatible = "ti,control-phy-usb2-am437",
  243. .data = &am437usb2_data,
  244. },
  245. {},
  246. };
  247. MODULE_DEVICE_TABLE(of, omap_control_phy_id_table);
  248. static int omap_control_phy_probe(struct platform_device *pdev)
  249. {
  250. struct resource *res;
  251. const struct of_device_id *of_id;
  252. struct omap_control_phy *control_phy;
  253. of_id = of_match_device(omap_control_phy_id_table, &pdev->dev);
  254. if (!of_id)
  255. return -EINVAL;
  256. control_phy = devm_kzalloc(&pdev->dev, sizeof(*control_phy),
  257. GFP_KERNEL);
  258. if (!control_phy)
  259. return -ENOMEM;
  260. control_phy->dev = &pdev->dev;
  261. control_phy->type = *(enum omap_control_phy_type *)of_id->data;
  262. if (control_phy->type == OMAP_CTRL_TYPE_OTGHS) {
  263. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  264. "otghs_control");
  265. control_phy->otghs_control = devm_ioremap_resource(
  266. &pdev->dev, res);
  267. if (IS_ERR(control_phy->otghs_control))
  268. return PTR_ERR(control_phy->otghs_control);
  269. } else {
  270. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  271. "power");
  272. control_phy->power = devm_ioremap_resource(&pdev->dev, res);
  273. if (IS_ERR(control_phy->power)) {
  274. dev_err(&pdev->dev, "Couldn't get power register\n");
  275. return PTR_ERR(control_phy->power);
  276. }
  277. }
  278. if (control_phy->type == OMAP_CTRL_TYPE_PIPE3 ||
  279. control_phy->type == OMAP_CTRL_TYPE_PCIE) {
  280. control_phy->sys_clk = devm_clk_get(control_phy->dev,
  281. "sys_clkin");
  282. if (IS_ERR(control_phy->sys_clk)) {
  283. pr_err("%s: unable to get sys_clkin\n", __func__);
  284. return -EINVAL;
  285. }
  286. }
  287. if (control_phy->type == OMAP_CTRL_TYPE_PCIE) {
  288. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  289. "pcie_pcs");
  290. control_phy->pcie_pcs = devm_ioremap_resource(&pdev->dev, res);
  291. if (IS_ERR(control_phy->pcie_pcs))
  292. return PTR_ERR(control_phy->pcie_pcs);
  293. }
  294. dev_set_drvdata(control_phy->dev, control_phy);
  295. return 0;
  296. }
  297. static struct platform_driver omap_control_phy_driver = {
  298. .probe = omap_control_phy_probe,
  299. .driver = {
  300. .name = "omap-control-phy",
  301. .of_match_table = omap_control_phy_id_table,
  302. },
  303. };
  304. static int __init omap_control_phy_init(void)
  305. {
  306. return platform_driver_register(&omap_control_phy_driver);
  307. }
  308. subsys_initcall(omap_control_phy_init);
  309. static void __exit omap_control_phy_exit(void)
  310. {
  311. platform_driver_unregister(&omap_control_phy_driver);
  312. }
  313. module_exit(omap_control_phy_exit);
  314. MODULE_ALIAS("platform:omap_control_phy");
  315. MODULE_AUTHOR("Texas Instruments Inc.");
  316. MODULE_DESCRIPTION("OMAP Control Module PHY Driver");
  317. MODULE_LICENSE("GPL v2");