phy-omap-control.c 8.3 KB

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