phy-dm816x-usb.c 6.9 KB

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