phy-dm816x-usb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. int error;
  75. if (clk_get_rate(phy->refclk) != 24000000)
  76. dev_warn(phy->dev, "nonstandard phy refclk\n");
  77. /* Set PLL ref clock and put phys to sleep */
  78. error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
  79. DM816X_USB_CTRL_PHYCLKSRC |
  80. DM816X_USB_CTRL_PHYSLEEP1 |
  81. DM816X_USB_CTRL_PHYSLEEP0,
  82. 0);
  83. regmap_read(phy->syscon, phy->usb_ctrl, &val);
  84. if ((val & 3) != 0)
  85. dev_info(phy->dev,
  86. "Working dm816x USB_CTRL! (0x%08x)\n",
  87. val);
  88. /*
  89. * TI kernel sets these values for "symmetrical eye diagram and
  90. * better signal quality" so let's assume somebody checked the
  91. * values with a scope and set them here too.
  92. */
  93. regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
  94. val |= DM816X_USBPHY_CTRL_TXRISETUNE |
  95. DM816X_USBPHY_CTRL_TXVREFTUNE |
  96. DM816X_USBPHY_CTRL_TXPREEMTUNE;
  97. regmap_write(phy->syscon, phy->usbphy_ctrl, val);
  98. return 0;
  99. }
  100. static const struct phy_ops ops = {
  101. .init = dm816x_usb_phy_init,
  102. .owner = THIS_MODULE,
  103. };
  104. static int dm816x_usb_phy_runtime_suspend(struct device *dev)
  105. {
  106. struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
  107. unsigned int mask, val;
  108. int error = 0;
  109. mask = BIT(phy->instance);
  110. val = ~BIT(phy->instance);
  111. error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
  112. mask, val);
  113. if (error)
  114. dev_err(phy->dev, "phy%i failed to power off\n",
  115. phy->instance);
  116. clk_disable(phy->refclk);
  117. return 0;
  118. }
  119. static int dm816x_usb_phy_runtime_resume(struct device *dev)
  120. {
  121. struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
  122. unsigned int mask, val;
  123. int error;
  124. error = clk_enable(phy->refclk);
  125. if (error)
  126. return error;
  127. /*
  128. * Note that at least dm816x rev c does not seem to do
  129. * anything with the USB_CTRL register. But let's follow
  130. * what the TI tree is doing in case later revisions use
  131. * USB_CTRL.
  132. */
  133. mask = BIT(phy->instance);
  134. val = BIT(phy->instance);
  135. error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
  136. mask, val);
  137. if (error) {
  138. dev_err(phy->dev, "phy%i failed to power on\n",
  139. phy->instance);
  140. clk_disable(phy->refclk);
  141. return error;
  142. }
  143. return 0;
  144. }
  145. static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
  146. dm816x_usb_phy_runtime_suspend,
  147. dm816x_usb_phy_runtime_resume,
  148. NULL);
  149. #ifdef CONFIG_OF
  150. static const struct of_device_id dm816x_usb_phy_id_table[] = {
  151. {
  152. .compatible = "ti,dm8168-usb-phy",
  153. },
  154. {},
  155. };
  156. MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
  157. #endif
  158. static int dm816x_usb_phy_probe(struct platform_device *pdev)
  159. {
  160. struct dm816x_usb_phy *phy;
  161. struct resource *res;
  162. struct phy *generic_phy;
  163. struct phy_provider *phy_provider;
  164. struct usb_otg *otg;
  165. const struct of_device_id *of_id;
  166. const struct usb_phy_data *phy_data;
  167. int error;
  168. of_id = of_match_device(of_match_ptr(dm816x_usb_phy_id_table),
  169. &pdev->dev);
  170. if (!of_id)
  171. return -EINVAL;
  172. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  173. if (!phy)
  174. return -ENOMEM;
  175. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  176. if (!res)
  177. return -ENOENT;
  178. phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  179. "syscon");
  180. if (IS_ERR(phy->syscon))
  181. return PTR_ERR(phy->syscon);
  182. /*
  183. * According to sprs614e.pdf, the first usb_ctrl is shared and
  184. * the second instance for usb_ctrl is reserved.. Also the
  185. * register bits are different from earlier TRMs.
  186. */
  187. phy->usb_ctrl = 0x20;
  188. phy->usbphy_ctrl = (res->start & 0xff) + 4;
  189. if (phy->usbphy_ctrl == 0x2c)
  190. phy->instance = 1;
  191. phy_data = of_id->data;
  192. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  193. if (!otg)
  194. return -ENOMEM;
  195. phy->dev = &pdev->dev;
  196. phy->phy.dev = phy->dev;
  197. phy->phy.label = "dm8168_usb_phy";
  198. phy->phy.otg = otg;
  199. phy->phy.type = USB_PHY_TYPE_USB2;
  200. otg->set_host = dm816x_usb_phy_set_host;
  201. otg->set_peripheral = dm816x_usb_phy_set_peripheral;
  202. otg->usb_phy = &phy->phy;
  203. platform_set_drvdata(pdev, phy);
  204. phy->refclk = devm_clk_get(phy->dev, "refclk");
  205. if (IS_ERR(phy->refclk))
  206. return PTR_ERR(phy->refclk);
  207. error = clk_prepare(phy->refclk);
  208. if (error)
  209. return error;
  210. pm_runtime_enable(phy->dev);
  211. generic_phy = devm_phy_create(phy->dev, NULL, &ops);
  212. if (IS_ERR(generic_phy))
  213. return PTR_ERR(generic_phy);
  214. phy_set_drvdata(generic_phy, phy);
  215. phy_provider = devm_of_phy_provider_register(phy->dev,
  216. of_phy_simple_xlate);
  217. if (IS_ERR(phy_provider))
  218. return PTR_ERR(phy_provider);
  219. usb_add_phy_dev(&phy->phy);
  220. return 0;
  221. }
  222. static int dm816x_usb_phy_remove(struct platform_device *pdev)
  223. {
  224. struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
  225. usb_remove_phy(&phy->phy);
  226. pm_runtime_disable(phy->dev);
  227. clk_unprepare(phy->refclk);
  228. return 0;
  229. }
  230. static struct platform_driver dm816x_usb_phy_driver = {
  231. .probe = dm816x_usb_phy_probe,
  232. .remove = dm816x_usb_phy_remove,
  233. .driver = {
  234. .name = "dm816x-usb-phy",
  235. .pm = &dm816x_usb_phy_pm_ops,
  236. .of_match_table = of_match_ptr(dm816x_usb_phy_id_table),
  237. },
  238. };
  239. module_platform_driver(dm816x_usb_phy_driver);
  240. MODULE_ALIAS("platform:dm816x_usb");
  241. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  242. MODULE_DESCRIPTION("dm816x usb phy driver");
  243. MODULE_LICENSE("GPL v2");