phy-omap-control.c 9.6 KB

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