phy-dm816x-usb.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation version 2.
  5. *
  6. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  7. * kind, whether express or implied; without even the implied warranty
  8. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/slab.h>
  15. #include <linux/of.h>
  16. #include <linux/io.h>
  17. #include <linux/usb/phy_companion.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/delay.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/mfd/syscon.h>
  25. /*
  26. * TRM has two sets of USB_CTRL registers.. The correct register bits
  27. * are in TRM section 24.9.8.2 USB_CTRL Register. The TRM documents the
  28. * phy as being SR70LX Synopsys USB 2.0 OTG nanoPHY. It also seems at
  29. * least dm816x rev c ignores writes to USB_CTRL register, but the TI
  30. * kernel is writing to those so it's possible that later revisions
  31. * have worknig USB_CTRL register.
  32. *
  33. * Also note that At least USB_CTRL register seems to be dm816x specific
  34. * according to the TRM. It's possible that USBPHY_CTRL is more generic,
  35. * but that would have to be checked against the SR70LX documentation
  36. * which does not seem to be publicly available.
  37. *
  38. * Finally, the phy on dm814x and am335x is different from dm816x.
  39. */
  40. #define DM816X_USB_CTRL_PHYCLKSRC BIT(8) /* 1 = PLL ref clock */
  41. #define DM816X_USB_CTRL_PHYSLEEP1 BIT(1) /* Enable the first phy */
  42. #define DM816X_USB_CTRL_PHYSLEEP0 BIT(0) /* Enable the second phy */
  43. #define DM816X_USBPHY_CTRL_TXRISETUNE 1
  44. #define DM816X_USBPHY_CTRL_TXVREFTUNE 0xc
  45. #define DM816X_USBPHY_CTRL_TXPREEMTUNE 0x2
  46. struct dm816x_usb_phy {
  47. struct regmap *syscon;
  48. struct device *dev;
  49. unsigned int instance;
  50. struct clk *refclk;
  51. struct usb_phy phy;
  52. unsigned int usb_ctrl; /* Shared between phy0 and phy1 */
  53. unsigned int usbphy_ctrl;
  54. };
  55. static int dm816x_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
  56. {
  57. otg->host = host;
  58. if (!host)
  59. otg->state = OTG_STATE_UNDEFINED;
  60. return 0;
  61. }
  62. static int dm816x_usb_phy_set_peripheral(struct usb_otg *otg,
  63. struct usb_gadget *gadget)
  64. {
  65. otg->gadget = gadget;
  66. if (!gadget)
  67. otg->state = OTG_STATE_UNDEFINED;
  68. return 0;
  69. }
  70. static int dm816x_usb_phy_init(struct phy *x)
  71. {
  72. struct dm816x_usb_phy *phy = phy_get_drvdata(x);
  73. unsigned int val;
  74. if (clk_get_rate(phy->refclk) != 24000000)
  75. dev_warn(phy->dev, "nonstandard phy refclk\n");
  76. /* Set PLL ref clock and put phys to sleep */
  77. regmap_update_bits(phy->syscon, phy->usb_ctrl,
  78. DM816X_USB_CTRL_PHYCLKSRC |
  79. DM816X_USB_CTRL_PHYSLEEP1 |
  80. DM816X_USB_CTRL_PHYSLEEP0,
  81. 0);
  82. regmap_read(phy->syscon, phy->usb_ctrl, &val);
  83. if ((val & 3) != 0)
  84. dev_info(phy->dev,
  85. "Working dm816x USB_CTRL! (0x%08x)\n",
  86. val);
  87. /*
  88. * TI kernel sets these values for "symmetrical eye diagram and
  89. * better signal quality" so let's assume somebody checked the
  90. * values with a scope and set them here too.
  91. */
  92. regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
  93. val |= DM816X_USBPHY_CTRL_TXRISETUNE |
  94. DM816X_USBPHY_CTRL_TXVREFTUNE |
  95. DM816X_USBPHY_CTRL_TXPREEMTUNE;
  96. regmap_write(phy->syscon, phy->usbphy_ctrl, val);
  97. return 0;
  98. }
  99. static const struct phy_ops ops = {
  100. .init = dm816x_usb_phy_init,
  101. .owner = THIS_MODULE,
  102. };
  103. static int __maybe_unused dm816x_usb_phy_runtime_suspend(struct device *dev)
  104. {
  105. struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
  106. unsigned int mask, val;
  107. int error = 0;
  108. mask = BIT(phy->instance);
  109. val = ~BIT(phy->instance);
  110. error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
  111. mask, val);
  112. if (error)
  113. dev_err(phy->dev, "phy%i failed to power off\n",
  114. phy->instance);
  115. clk_disable(phy->refclk);
  116. return 0;
  117. }
  118. static int __maybe_unused dm816x_usb_phy_runtime_resume(struct device *dev)
  119. {
  120. struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
  121. unsigned int mask, val;
  122. int error;
  123. error = clk_enable(phy->refclk);
  124. if (error)
  125. return error;
  126. /*
  127. * Note that at least dm816x rev c does not seem to do
  128. * anything with the USB_CTRL register. But let's follow
  129. * what the TI tree is doing in case later revisions use
  130. * USB_CTRL.
  131. */
  132. mask = BIT(phy->instance);
  133. val = BIT(phy->instance);
  134. error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
  135. mask, val);
  136. if (error) {
  137. dev_err(phy->dev, "phy%i failed to power on\n",
  138. phy->instance);
  139. clk_disable(phy->refclk);
  140. return error;
  141. }
  142. return 0;
  143. }
  144. static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
  145. dm816x_usb_phy_runtime_suspend,
  146. dm816x_usb_phy_runtime_resume,
  147. NULL);
  148. #ifdef CONFIG_OF
  149. static const struct of_device_id dm816x_usb_phy_id_table[] = {
  150. {
  151. .compatible = "ti,dm8168-usb-phy",
  152. },
  153. {},
  154. };
  155. MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
  156. #endif
  157. static int dm816x_usb_phy_probe(struct platform_device *pdev)
  158. {
  159. struct dm816x_usb_phy *phy;
  160. struct resource *res;
  161. struct phy *generic_phy;
  162. struct phy_provider *phy_provider;
  163. struct usb_otg *otg;
  164. const struct of_device_id *of_id;
  165. int error;
  166. of_id = of_match_device(of_match_ptr(dm816x_usb_phy_id_table),
  167. &pdev->dev);
  168. if (!of_id)
  169. return -EINVAL;
  170. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  171. if (!phy)
  172. return -ENOMEM;
  173. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  174. if (!res)
  175. return -ENOENT;
  176. phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  177. "syscon");
  178. if (IS_ERR(phy->syscon))
  179. return PTR_ERR(phy->syscon);
  180. /*
  181. * According to sprs614e.pdf, the first usb_ctrl is shared and
  182. * the second instance for usb_ctrl is reserved.. Also the
  183. * register bits are different from earlier TRMs.
  184. */
  185. phy->usb_ctrl = 0x20;
  186. phy->usbphy_ctrl = (res->start & 0xff) + 4;
  187. if (phy->usbphy_ctrl == 0x2c)
  188. phy->instance = 1;
  189. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  190. if (!otg)
  191. return -ENOMEM;
  192. phy->dev = &pdev->dev;
  193. phy->phy.dev = phy->dev;
  194. phy->phy.label = "dm8168_usb_phy";
  195. phy->phy.otg = otg;
  196. phy->phy.type = USB_PHY_TYPE_USB2;
  197. otg->set_host = dm816x_usb_phy_set_host;
  198. otg->set_peripheral = dm816x_usb_phy_set_peripheral;
  199. otg->usb_phy = &phy->phy;
  200. platform_set_drvdata(pdev, phy);
  201. phy->refclk = devm_clk_get(phy->dev, "refclk");
  202. if (IS_ERR(phy->refclk))
  203. return PTR_ERR(phy->refclk);
  204. error = clk_prepare(phy->refclk);
  205. if (error)
  206. return error;
  207. pm_runtime_enable(phy->dev);
  208. generic_phy = devm_phy_create(phy->dev, NULL, &ops);
  209. if (IS_ERR(generic_phy)) {
  210. error = PTR_ERR(generic_phy);
  211. goto clk_unprepare;
  212. }
  213. phy_set_drvdata(generic_phy, phy);
  214. phy_provider = devm_of_phy_provider_register(phy->dev,
  215. of_phy_simple_xlate);
  216. if (IS_ERR(phy_provider)) {
  217. error = PTR_ERR(phy_provider);
  218. goto clk_unprepare;
  219. }
  220. usb_add_phy_dev(&phy->phy);
  221. return 0;
  222. clk_unprepare:
  223. pm_runtime_disable(phy->dev);
  224. clk_unprepare(phy->refclk);
  225. return error;
  226. }
  227. static int dm816x_usb_phy_remove(struct platform_device *pdev)
  228. {
  229. struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
  230. usb_remove_phy(&phy->phy);
  231. pm_runtime_disable(phy->dev);
  232. clk_unprepare(phy->refclk);
  233. return 0;
  234. }
  235. static struct platform_driver dm816x_usb_phy_driver = {
  236. .probe = dm816x_usb_phy_probe,
  237. .remove = dm816x_usb_phy_remove,
  238. .driver = {
  239. .name = "dm816x-usb-phy",
  240. .pm = &dm816x_usb_phy_pm_ops,
  241. .of_match_table = of_match_ptr(dm816x_usb_phy_id_table),
  242. },
  243. };
  244. module_platform_driver(dm816x_usb_phy_driver);
  245. MODULE_ALIAS("platform:dm816x_usb");
  246. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  247. MODULE_DESCRIPTION("dm816x usb phy driver");
  248. MODULE_LICENSE("GPL v2");