mtu3_dr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mtu3_dr.c - dual role switch and host glue layer
  4. *
  5. * Copyright (C) 2016 MediaTek Inc.
  6. *
  7. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/irq.h>
  11. #include <linux/kernel.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pinctrl/consumer.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/uaccess.h>
  16. #include "mtu3.h"
  17. #include "mtu3_dr.h"
  18. #define USB2_PORT 2
  19. #define USB3_PORT 3
  20. enum mtu3_vbus_id_state {
  21. MTU3_ID_FLOAT = 1,
  22. MTU3_ID_GROUND,
  23. MTU3_VBUS_OFF,
  24. MTU3_VBUS_VALID,
  25. };
  26. static void toggle_opstate(struct ssusb_mtk *ssusb)
  27. {
  28. if (!ssusb->otg_switch.is_u3_drd) {
  29. mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION);
  30. mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN);
  31. }
  32. }
  33. /* only port0 supports dual-role mode */
  34. static int ssusb_port0_switch(struct ssusb_mtk *ssusb,
  35. int version, bool tohost)
  36. {
  37. void __iomem *ibase = ssusb->ippc_base;
  38. u32 value;
  39. dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__,
  40. version, tohost ? "host" : "device");
  41. if (version == USB2_PORT) {
  42. /* 1. power off and disable u2 port0 */
  43. value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
  44. value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
  45. mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
  46. /* 2. power on, enable u2 port0 and select its mode */
  47. value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
  48. value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
  49. value = tohost ? (value | SSUSB_U2_PORT_HOST_SEL) :
  50. (value & (~SSUSB_U2_PORT_HOST_SEL));
  51. mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
  52. } else {
  53. /* 1. power off and disable u3 port0 */
  54. value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
  55. value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
  56. mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
  57. /* 2. power on, enable u3 port0 and select its mode */
  58. value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
  59. value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
  60. value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) :
  61. (value & (~SSUSB_U3_PORT_HOST_SEL));
  62. mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
  63. }
  64. return 0;
  65. }
  66. static void switch_port_to_host(struct ssusb_mtk *ssusb)
  67. {
  68. u32 check_clk = 0;
  69. dev_dbg(ssusb->dev, "%s\n", __func__);
  70. ssusb_port0_switch(ssusb, USB2_PORT, true);
  71. if (ssusb->otg_switch.is_u3_drd) {
  72. ssusb_port0_switch(ssusb, USB3_PORT, true);
  73. check_clk = SSUSB_U3_MAC_RST_B_STS;
  74. }
  75. ssusb_check_clocks(ssusb, check_clk);
  76. /* after all clocks are stable */
  77. toggle_opstate(ssusb);
  78. }
  79. static void switch_port_to_device(struct ssusb_mtk *ssusb)
  80. {
  81. u32 check_clk = 0;
  82. dev_dbg(ssusb->dev, "%s\n", __func__);
  83. ssusb_port0_switch(ssusb, USB2_PORT, false);
  84. if (ssusb->otg_switch.is_u3_drd) {
  85. ssusb_port0_switch(ssusb, USB3_PORT, false);
  86. check_clk = SSUSB_U3_MAC_RST_B_STS;
  87. }
  88. ssusb_check_clocks(ssusb, check_clk);
  89. }
  90. int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
  91. {
  92. struct ssusb_mtk *ssusb =
  93. container_of(otg_sx, struct ssusb_mtk, otg_switch);
  94. struct regulator *vbus = otg_sx->vbus;
  95. int ret;
  96. /* vbus is optional */
  97. if (!vbus)
  98. return 0;
  99. dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off");
  100. if (is_on) {
  101. ret = regulator_enable(vbus);
  102. if (ret) {
  103. dev_err(ssusb->dev, "vbus regulator enable failed\n");
  104. return ret;
  105. }
  106. } else {
  107. regulator_disable(vbus);
  108. }
  109. return 0;
  110. }
  111. /*
  112. * switch to host: -> MTU3_VBUS_OFF --> MTU3_ID_GROUND
  113. * switch to device: -> MTU3_ID_FLOAT --> MTU3_VBUS_VALID
  114. */
  115. static void ssusb_set_mailbox(struct otg_switch_mtk *otg_sx,
  116. enum mtu3_vbus_id_state status)
  117. {
  118. struct ssusb_mtk *ssusb =
  119. container_of(otg_sx, struct ssusb_mtk, otg_switch);
  120. struct mtu3 *mtu = ssusb->u3d;
  121. dev_dbg(ssusb->dev, "mailbox state(%d)\n", status);
  122. switch (status) {
  123. case MTU3_ID_GROUND:
  124. switch_port_to_host(ssusb);
  125. ssusb_set_vbus(otg_sx, 1);
  126. ssusb->is_host = true;
  127. break;
  128. case MTU3_ID_FLOAT:
  129. ssusb->is_host = false;
  130. ssusb_set_vbus(otg_sx, 0);
  131. switch_port_to_device(ssusb);
  132. break;
  133. case MTU3_VBUS_OFF:
  134. mtu3_stop(mtu);
  135. pm_relax(ssusb->dev);
  136. break;
  137. case MTU3_VBUS_VALID:
  138. /* avoid suspend when works as device */
  139. pm_stay_awake(ssusb->dev);
  140. mtu3_start(mtu);
  141. break;
  142. default:
  143. dev_err(ssusb->dev, "invalid state\n");
  144. }
  145. }
  146. static int ssusb_id_notifier(struct notifier_block *nb,
  147. unsigned long event, void *ptr)
  148. {
  149. struct otg_switch_mtk *otg_sx =
  150. container_of(nb, struct otg_switch_mtk, id_nb);
  151. if (event)
  152. ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
  153. else
  154. ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
  155. return NOTIFY_DONE;
  156. }
  157. static int ssusb_vbus_notifier(struct notifier_block *nb,
  158. unsigned long event, void *ptr)
  159. {
  160. struct otg_switch_mtk *otg_sx =
  161. container_of(nb, struct otg_switch_mtk, vbus_nb);
  162. if (event)
  163. ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
  164. else
  165. ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
  166. return NOTIFY_DONE;
  167. }
  168. static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
  169. {
  170. struct ssusb_mtk *ssusb =
  171. container_of(otg_sx, struct ssusb_mtk, otg_switch);
  172. struct extcon_dev *edev = otg_sx->edev;
  173. int ret;
  174. /* extcon is optional */
  175. if (!edev)
  176. return 0;
  177. otg_sx->vbus_nb.notifier_call = ssusb_vbus_notifier;
  178. ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB,
  179. &otg_sx->vbus_nb);
  180. if (ret < 0)
  181. dev_err(ssusb->dev, "failed to register notifier for USB\n");
  182. otg_sx->id_nb.notifier_call = ssusb_id_notifier;
  183. ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
  184. &otg_sx->id_nb);
  185. if (ret < 0)
  186. dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
  187. dev_dbg(ssusb->dev, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
  188. extcon_get_state(edev, EXTCON_USB),
  189. extcon_get_state(edev, EXTCON_USB_HOST));
  190. /* default as host, switch to device mode if needed */
  191. if (extcon_get_state(edev, EXTCON_USB_HOST) == false)
  192. ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
  193. if (extcon_get_state(edev, EXTCON_USB) == true)
  194. ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
  195. return 0;
  196. }
  197. static void extcon_register_dwork(struct work_struct *work)
  198. {
  199. struct delayed_work *dwork = to_delayed_work(work);
  200. struct otg_switch_mtk *otg_sx =
  201. container_of(dwork, struct otg_switch_mtk, extcon_reg_dwork);
  202. ssusb_extcon_register(otg_sx);
  203. }
  204. /*
  205. * We provide an interface via debugfs to switch between host and device modes
  206. * depending on user input.
  207. * This is useful in special cases, such as uses TYPE-A receptacle but also
  208. * wants to support dual-role mode.
  209. */
  210. static void ssusb_mode_manual_switch(struct ssusb_mtk *ssusb, int to_host)
  211. {
  212. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  213. if (to_host) {
  214. ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
  215. ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
  216. ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
  217. } else {
  218. ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
  219. ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
  220. ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
  221. }
  222. }
  223. static int ssusb_mode_show(struct seq_file *sf, void *unused)
  224. {
  225. struct ssusb_mtk *ssusb = sf->private;
  226. seq_printf(sf, "current mode: %s(%s drd)\n(echo device/host)\n",
  227. ssusb->is_host ? "host" : "device",
  228. ssusb->otg_switch.manual_drd_enabled ? "manual" : "auto");
  229. return 0;
  230. }
  231. static int ssusb_mode_open(struct inode *inode, struct file *file)
  232. {
  233. return single_open(file, ssusb_mode_show, inode->i_private);
  234. }
  235. static ssize_t ssusb_mode_write(struct file *file,
  236. const char __user *ubuf, size_t count, loff_t *ppos)
  237. {
  238. struct seq_file *sf = file->private_data;
  239. struct ssusb_mtk *ssusb = sf->private;
  240. char buf[16];
  241. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  242. return -EFAULT;
  243. if (!strncmp(buf, "host", 4) && !ssusb->is_host) {
  244. ssusb_mode_manual_switch(ssusb, 1);
  245. } else if (!strncmp(buf, "device", 6) && ssusb->is_host) {
  246. ssusb_mode_manual_switch(ssusb, 0);
  247. } else {
  248. dev_err(ssusb->dev, "wrong or duplicated setting\n");
  249. return -EINVAL;
  250. }
  251. return count;
  252. }
  253. static const struct file_operations ssusb_mode_fops = {
  254. .open = ssusb_mode_open,
  255. .write = ssusb_mode_write,
  256. .read = seq_read,
  257. .llseek = seq_lseek,
  258. .release = single_release,
  259. };
  260. static int ssusb_vbus_show(struct seq_file *sf, void *unused)
  261. {
  262. struct ssusb_mtk *ssusb = sf->private;
  263. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  264. seq_printf(sf, "vbus state: %s\n(echo on/off)\n",
  265. regulator_is_enabled(otg_sx->vbus) ? "on" : "off");
  266. return 0;
  267. }
  268. static int ssusb_vbus_open(struct inode *inode, struct file *file)
  269. {
  270. return single_open(file, ssusb_vbus_show, inode->i_private);
  271. }
  272. static ssize_t ssusb_vbus_write(struct file *file,
  273. const char __user *ubuf, size_t count, loff_t *ppos)
  274. {
  275. struct seq_file *sf = file->private_data;
  276. struct ssusb_mtk *ssusb = sf->private;
  277. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  278. char buf[16];
  279. bool enable;
  280. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  281. return -EFAULT;
  282. if (kstrtobool(buf, &enable)) {
  283. dev_err(ssusb->dev, "wrong setting\n");
  284. return -EINVAL;
  285. }
  286. ssusb_set_vbus(otg_sx, enable);
  287. return count;
  288. }
  289. static const struct file_operations ssusb_vbus_fops = {
  290. .open = ssusb_vbus_open,
  291. .write = ssusb_vbus_write,
  292. .read = seq_read,
  293. .llseek = seq_lseek,
  294. .release = single_release,
  295. };
  296. static void ssusb_debugfs_init(struct ssusb_mtk *ssusb)
  297. {
  298. struct dentry *root;
  299. root = debugfs_create_dir(dev_name(ssusb->dev), usb_debug_root);
  300. if (!root) {
  301. dev_err(ssusb->dev, "create debugfs root failed\n");
  302. return;
  303. }
  304. ssusb->dbgfs_root = root;
  305. debugfs_create_file("mode", 0644, root, ssusb, &ssusb_mode_fops);
  306. debugfs_create_file("vbus", 0644, root, ssusb, &ssusb_vbus_fops);
  307. }
  308. static void ssusb_debugfs_exit(struct ssusb_mtk *ssusb)
  309. {
  310. debugfs_remove_recursive(ssusb->dbgfs_root);
  311. }
  312. void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
  313. enum mtu3_dr_force_mode mode)
  314. {
  315. u32 value;
  316. value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
  317. switch (mode) {
  318. case MTU3_DR_FORCE_DEVICE:
  319. value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
  320. break;
  321. case MTU3_DR_FORCE_HOST:
  322. value |= SSUSB_U2_PORT_FORCE_IDDIG;
  323. value &= ~SSUSB_U2_PORT_RG_IDDIG;
  324. break;
  325. case MTU3_DR_FORCE_NONE:
  326. value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
  327. break;
  328. default:
  329. return;
  330. }
  331. mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
  332. }
  333. int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
  334. {
  335. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  336. if (otg_sx->manual_drd_enabled) {
  337. ssusb_debugfs_init(ssusb);
  338. } else {
  339. INIT_DELAYED_WORK(&otg_sx->extcon_reg_dwork,
  340. extcon_register_dwork);
  341. /*
  342. * It is enough to delay 1s for waiting for
  343. * host initialization
  344. */
  345. schedule_delayed_work(&otg_sx->extcon_reg_dwork, HZ);
  346. }
  347. return 0;
  348. }
  349. void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
  350. {
  351. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  352. if (otg_sx->manual_drd_enabled)
  353. ssusb_debugfs_exit(ssusb);
  354. else
  355. cancel_delayed_work(&otg_sx->extcon_reg_dwork);
  356. }