omap-control-usb.c 8.0 KB

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