phy-dm816x-usb.c 7.2 KB

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