mtu3_plat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2016 MediaTek Inc.
  3. *
  4. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include <linux/platform_device.h>
  25. #include "mtu3.h"
  26. #include "mtu3_dr.h"
  27. /* u2-port0 should be powered on and enabled; */
  28. int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
  29. {
  30. void __iomem *ibase = ssusb->ippc_base;
  31. u32 value, check_val;
  32. int ret;
  33. check_val = ex_clks | SSUSB_SYS125_RST_B_STS | SSUSB_SYSPLL_STABLE |
  34. SSUSB_REF_RST_B_STS;
  35. ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
  36. (check_val == (value & check_val)), 100, 20000);
  37. if (ret) {
  38. dev_err(ssusb->dev, "clks of sts1 are not stable!\n");
  39. return ret;
  40. }
  41. ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS2, value,
  42. (value & SSUSB_U2_MAC_SYS_RST_B_STS), 100, 10000);
  43. if (ret) {
  44. dev_err(ssusb->dev, "mac2 clock is not stable\n");
  45. return ret;
  46. }
  47. return 0;
  48. }
  49. static int ssusb_phy_init(struct ssusb_mtk *ssusb)
  50. {
  51. int i;
  52. int ret;
  53. for (i = 0; i < ssusb->num_phys; i++) {
  54. ret = phy_init(ssusb->phys[i]);
  55. if (ret)
  56. goto exit_phy;
  57. }
  58. return 0;
  59. exit_phy:
  60. for (; i > 0; i--)
  61. phy_exit(ssusb->phys[i - 1]);
  62. return ret;
  63. }
  64. static int ssusb_phy_exit(struct ssusb_mtk *ssusb)
  65. {
  66. int i;
  67. for (i = 0; i < ssusb->num_phys; i++)
  68. phy_exit(ssusb->phys[i]);
  69. return 0;
  70. }
  71. static int ssusb_phy_power_on(struct ssusb_mtk *ssusb)
  72. {
  73. int i;
  74. int ret;
  75. for (i = 0; i < ssusb->num_phys; i++) {
  76. ret = phy_power_on(ssusb->phys[i]);
  77. if (ret)
  78. goto power_off_phy;
  79. }
  80. return 0;
  81. power_off_phy:
  82. for (; i > 0; i--)
  83. phy_power_off(ssusb->phys[i - 1]);
  84. return ret;
  85. }
  86. static void ssusb_phy_power_off(struct ssusb_mtk *ssusb)
  87. {
  88. unsigned int i;
  89. for (i = 0; i < ssusb->num_phys; i++)
  90. phy_power_off(ssusb->phys[i]);
  91. }
  92. static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
  93. {
  94. int ret = 0;
  95. ret = regulator_enable(ssusb->vusb33);
  96. if (ret) {
  97. dev_err(ssusb->dev, "failed to enable vusb33\n");
  98. goto vusb33_err;
  99. }
  100. ret = clk_prepare_enable(ssusb->sys_clk);
  101. if (ret) {
  102. dev_err(ssusb->dev, "failed to enable sys_clk\n");
  103. goto sys_clk_err;
  104. }
  105. ret = clk_prepare_enable(ssusb->ref_clk);
  106. if (ret) {
  107. dev_err(ssusb->dev, "failed to enable ref_clk\n");
  108. goto ref_clk_err;
  109. }
  110. ret = ssusb_phy_init(ssusb);
  111. if (ret) {
  112. dev_err(ssusb->dev, "failed to init phy\n");
  113. goto phy_init_err;
  114. }
  115. ret = ssusb_phy_power_on(ssusb);
  116. if (ret) {
  117. dev_err(ssusb->dev, "failed to power on phy\n");
  118. goto phy_err;
  119. }
  120. return 0;
  121. phy_err:
  122. ssusb_phy_exit(ssusb);
  123. phy_init_err:
  124. clk_disable_unprepare(ssusb->ref_clk);
  125. ref_clk_err:
  126. clk_disable_unprepare(ssusb->sys_clk);
  127. sys_clk_err:
  128. regulator_disable(ssusb->vusb33);
  129. vusb33_err:
  130. return ret;
  131. }
  132. static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
  133. {
  134. clk_disable_unprepare(ssusb->sys_clk);
  135. clk_disable_unprepare(ssusb->ref_clk);
  136. regulator_disable(ssusb->vusb33);
  137. ssusb_phy_power_off(ssusb);
  138. ssusb_phy_exit(ssusb);
  139. }
  140. static void ssusb_ip_sw_reset(struct ssusb_mtk *ssusb)
  141. {
  142. /* reset whole ip (xhci & u3d) */
  143. mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  144. udelay(1);
  145. mtu3_clrbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  146. }
  147. static int get_iddig_pinctrl(struct ssusb_mtk *ssusb)
  148. {
  149. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  150. otg_sx->id_pinctrl = devm_pinctrl_get(ssusb->dev);
  151. if (IS_ERR(otg_sx->id_pinctrl)) {
  152. dev_err(ssusb->dev, "Cannot find id pinctrl!\n");
  153. return PTR_ERR(otg_sx->id_pinctrl);
  154. }
  155. otg_sx->id_float =
  156. pinctrl_lookup_state(otg_sx->id_pinctrl, "id_float");
  157. if (IS_ERR(otg_sx->id_float)) {
  158. dev_err(ssusb->dev, "Cannot find pinctrl id_float!\n");
  159. return PTR_ERR(otg_sx->id_float);
  160. }
  161. otg_sx->id_ground =
  162. pinctrl_lookup_state(otg_sx->id_pinctrl, "id_ground");
  163. if (IS_ERR(otg_sx->id_ground)) {
  164. dev_err(ssusb->dev, "Cannot find pinctrl id_ground!\n");
  165. return PTR_ERR(otg_sx->id_ground);
  166. }
  167. return 0;
  168. }
  169. static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
  170. {
  171. struct device_node *node = pdev->dev.of_node;
  172. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  173. struct device *dev = &pdev->dev;
  174. struct regulator *vbus;
  175. struct resource *res;
  176. int i;
  177. int ret;
  178. ssusb->vusb33 = devm_regulator_get(&pdev->dev, "vusb33");
  179. if (IS_ERR(ssusb->vusb33)) {
  180. dev_err(dev, "failed to get vusb33\n");
  181. return PTR_ERR(ssusb->vusb33);
  182. }
  183. ssusb->sys_clk = devm_clk_get(dev, "sys_ck");
  184. if (IS_ERR(ssusb->sys_clk)) {
  185. dev_err(dev, "failed to get sys clock\n");
  186. return PTR_ERR(ssusb->sys_clk);
  187. }
  188. /*
  189. * reference clock is usually a "fixed-clock", make it optional
  190. * for backward compatibility and ignore the error if it does
  191. * not exist.
  192. */
  193. ssusb->ref_clk = devm_clk_get(dev, "ref_ck");
  194. if (IS_ERR(ssusb->ref_clk)) {
  195. if (PTR_ERR(ssusb->ref_clk) == -EPROBE_DEFER)
  196. return -EPROBE_DEFER;
  197. ssusb->ref_clk = NULL;
  198. }
  199. ssusb->num_phys = of_count_phandle_with_args(node,
  200. "phys", "#phy-cells");
  201. if (ssusb->num_phys > 0) {
  202. ssusb->phys = devm_kcalloc(dev, ssusb->num_phys,
  203. sizeof(*ssusb->phys), GFP_KERNEL);
  204. if (!ssusb->phys)
  205. return -ENOMEM;
  206. } else {
  207. ssusb->num_phys = 0;
  208. }
  209. for (i = 0; i < ssusb->num_phys; i++) {
  210. ssusb->phys[i] = devm_of_phy_get_by_index(dev, node, i);
  211. if (IS_ERR(ssusb->phys[i])) {
  212. dev_err(dev, "failed to get phy-%d\n", i);
  213. return PTR_ERR(ssusb->phys[i]);
  214. }
  215. }
  216. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
  217. ssusb->ippc_base = devm_ioremap_resource(dev, res);
  218. if (IS_ERR(ssusb->ippc_base)) {
  219. dev_err(dev, "failed to map memory for ippc\n");
  220. return PTR_ERR(ssusb->ippc_base);
  221. }
  222. ssusb->dr_mode = usb_get_dr_mode(dev);
  223. if (ssusb->dr_mode == USB_DR_MODE_UNKNOWN) {
  224. dev_err(dev, "dr_mode is error\n");
  225. return -EINVAL;
  226. }
  227. if (ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
  228. return 0;
  229. /* if host role is supported */
  230. ret = ssusb_wakeup_of_property_parse(ssusb, node);
  231. if (ret)
  232. return ret;
  233. if (ssusb->dr_mode != USB_DR_MODE_OTG)
  234. return 0;
  235. /* if dual-role mode is supported */
  236. vbus = devm_regulator_get(&pdev->dev, "vbus");
  237. if (IS_ERR(vbus)) {
  238. dev_err(dev, "failed to get vbus\n");
  239. return PTR_ERR(vbus);
  240. }
  241. otg_sx->vbus = vbus;
  242. otg_sx->is_u3_drd = of_property_read_bool(node, "mediatek,usb3-drd");
  243. otg_sx->manual_drd_enabled =
  244. of_property_read_bool(node, "enable-manual-drd");
  245. if (of_property_read_bool(node, "extcon")) {
  246. otg_sx->edev = extcon_get_edev_by_phandle(ssusb->dev, 0);
  247. if (IS_ERR(otg_sx->edev)) {
  248. dev_err(ssusb->dev, "couldn't get extcon device\n");
  249. return -EPROBE_DEFER;
  250. }
  251. if (otg_sx->manual_drd_enabled) {
  252. ret = get_iddig_pinctrl(ssusb);
  253. if (ret)
  254. return ret;
  255. }
  256. }
  257. dev_info(dev, "dr_mode: %d, is_u3_dr: %d\n",
  258. ssusb->dr_mode, otg_sx->is_u3_drd);
  259. return 0;
  260. }
  261. static int mtu3_probe(struct platform_device *pdev)
  262. {
  263. struct device_node *node = pdev->dev.of_node;
  264. struct device *dev = &pdev->dev;
  265. struct ssusb_mtk *ssusb;
  266. int ret = -ENOMEM;
  267. /* all elements are set to ZERO as default value */
  268. ssusb = devm_kzalloc(dev, sizeof(*ssusb), GFP_KERNEL);
  269. if (!ssusb)
  270. return -ENOMEM;
  271. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  272. if (ret) {
  273. dev_err(dev, "No suitable DMA config available\n");
  274. return -ENOTSUPP;
  275. }
  276. platform_set_drvdata(pdev, ssusb);
  277. ssusb->dev = dev;
  278. ret = get_ssusb_rscs(pdev, ssusb);
  279. if (ret)
  280. return ret;
  281. /* enable power domain */
  282. pm_runtime_enable(dev);
  283. pm_runtime_get_sync(dev);
  284. device_enable_async_suspend(dev);
  285. ret = ssusb_rscs_init(ssusb);
  286. if (ret)
  287. goto comm_init_err;
  288. ssusb_ip_sw_reset(ssusb);
  289. if (IS_ENABLED(CONFIG_USB_MTU3_HOST))
  290. ssusb->dr_mode = USB_DR_MODE_HOST;
  291. else if (IS_ENABLED(CONFIG_USB_MTU3_GADGET))
  292. ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
  293. /* default as host */
  294. ssusb->is_host = !(ssusb->dr_mode == USB_DR_MODE_PERIPHERAL);
  295. switch (ssusb->dr_mode) {
  296. case USB_DR_MODE_PERIPHERAL:
  297. ret = ssusb_gadget_init(ssusb);
  298. if (ret) {
  299. dev_err(dev, "failed to initialize gadget\n");
  300. goto comm_exit;
  301. }
  302. break;
  303. case USB_DR_MODE_HOST:
  304. ret = ssusb_host_init(ssusb, node);
  305. if (ret) {
  306. dev_err(dev, "failed to initialize host\n");
  307. goto comm_exit;
  308. }
  309. break;
  310. case USB_DR_MODE_OTG:
  311. ret = ssusb_gadget_init(ssusb);
  312. if (ret) {
  313. dev_err(dev, "failed to initialize gadget\n");
  314. goto comm_exit;
  315. }
  316. ret = ssusb_host_init(ssusb, node);
  317. if (ret) {
  318. dev_err(dev, "failed to initialize host\n");
  319. goto gadget_exit;
  320. }
  321. ssusb_otg_switch_init(ssusb);
  322. break;
  323. default:
  324. dev_err(dev, "unsupported mode: %d\n", ssusb->dr_mode);
  325. ret = -EINVAL;
  326. goto comm_exit;
  327. }
  328. return 0;
  329. gadget_exit:
  330. ssusb_gadget_exit(ssusb);
  331. comm_exit:
  332. ssusb_rscs_exit(ssusb);
  333. comm_init_err:
  334. pm_runtime_put_sync(dev);
  335. pm_runtime_disable(dev);
  336. return ret;
  337. }
  338. static int mtu3_remove(struct platform_device *pdev)
  339. {
  340. struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
  341. switch (ssusb->dr_mode) {
  342. case USB_DR_MODE_PERIPHERAL:
  343. ssusb_gadget_exit(ssusb);
  344. break;
  345. case USB_DR_MODE_HOST:
  346. ssusb_host_exit(ssusb);
  347. break;
  348. case USB_DR_MODE_OTG:
  349. ssusb_otg_switch_exit(ssusb);
  350. ssusb_gadget_exit(ssusb);
  351. ssusb_host_exit(ssusb);
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. ssusb_rscs_exit(ssusb);
  357. pm_runtime_put_sync(&pdev->dev);
  358. pm_runtime_disable(&pdev->dev);
  359. return 0;
  360. }
  361. /*
  362. * when support dual-role mode, we reject suspend when
  363. * it works as device mode;
  364. */
  365. static int __maybe_unused mtu3_suspend(struct device *dev)
  366. {
  367. struct platform_device *pdev = to_platform_device(dev);
  368. struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
  369. dev_dbg(dev, "%s\n", __func__);
  370. /* REVISIT: disconnect it for only device mode? */
  371. if (!ssusb->is_host)
  372. return 0;
  373. ssusb_host_disable(ssusb, true);
  374. ssusb_phy_power_off(ssusb);
  375. clk_disable_unprepare(ssusb->sys_clk);
  376. clk_disable_unprepare(ssusb->ref_clk);
  377. ssusb_wakeup_enable(ssusb);
  378. return 0;
  379. }
  380. static int __maybe_unused mtu3_resume(struct device *dev)
  381. {
  382. struct platform_device *pdev = to_platform_device(dev);
  383. struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
  384. dev_dbg(dev, "%s\n", __func__);
  385. if (!ssusb->is_host)
  386. return 0;
  387. ssusb_wakeup_disable(ssusb);
  388. clk_prepare_enable(ssusb->sys_clk);
  389. clk_prepare_enable(ssusb->ref_clk);
  390. ssusb_phy_power_on(ssusb);
  391. ssusb_host_enable(ssusb);
  392. return 0;
  393. }
  394. static const struct dev_pm_ops mtu3_pm_ops = {
  395. SET_SYSTEM_SLEEP_PM_OPS(mtu3_suspend, mtu3_resume)
  396. };
  397. #define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &mtu3_pm_ops : NULL)
  398. #ifdef CONFIG_OF
  399. static const struct of_device_id mtu3_of_match[] = {
  400. {.compatible = "mediatek,mt8173-mtu3",},
  401. {},
  402. };
  403. MODULE_DEVICE_TABLE(of, mtu3_of_match);
  404. #endif
  405. static struct platform_driver mtu3_driver = {
  406. .probe = mtu3_probe,
  407. .remove = mtu3_remove,
  408. .driver = {
  409. .name = MTU3_DRIVER_NAME,
  410. .pm = DEV_PM_OPS,
  411. .of_match_table = of_match_ptr(mtu3_of_match),
  412. },
  413. };
  414. module_platform_driver(mtu3_driver);
  415. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  416. MODULE_LICENSE("GPL v2");
  417. MODULE_DESCRIPTION("MediaTek USB3 DRD Controller Driver");