mtu3_dr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 void ssusb_id_work(struct work_struct *work)
  147. {
  148. struct otg_switch_mtk *otg_sx =
  149. container_of(work, struct otg_switch_mtk, id_work);
  150. if (otg_sx->id_event)
  151. ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
  152. else
  153. ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
  154. }
  155. static void ssusb_vbus_work(struct work_struct *work)
  156. {
  157. struct otg_switch_mtk *otg_sx =
  158. container_of(work, struct otg_switch_mtk, vbus_work);
  159. if (otg_sx->vbus_event)
  160. ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
  161. else
  162. ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
  163. }
  164. /*
  165. * @ssusb_id_notifier is called in atomic context, but @ssusb_set_mailbox
  166. * may sleep, so use work queue here
  167. */
  168. static int ssusb_id_notifier(struct notifier_block *nb,
  169. unsigned long event, void *ptr)
  170. {
  171. struct otg_switch_mtk *otg_sx =
  172. container_of(nb, struct otg_switch_mtk, id_nb);
  173. otg_sx->id_event = event;
  174. schedule_work(&otg_sx->id_work);
  175. return NOTIFY_DONE;
  176. }
  177. static int ssusb_vbus_notifier(struct notifier_block *nb,
  178. unsigned long event, void *ptr)
  179. {
  180. struct otg_switch_mtk *otg_sx =
  181. container_of(nb, struct otg_switch_mtk, vbus_nb);
  182. otg_sx->vbus_event = event;
  183. schedule_work(&otg_sx->vbus_work);
  184. return NOTIFY_DONE;
  185. }
  186. static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
  187. {
  188. struct ssusb_mtk *ssusb =
  189. container_of(otg_sx, struct ssusb_mtk, otg_switch);
  190. struct extcon_dev *edev = otg_sx->edev;
  191. int ret;
  192. /* extcon is optional */
  193. if (!edev)
  194. return 0;
  195. otg_sx->vbus_nb.notifier_call = ssusb_vbus_notifier;
  196. ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB,
  197. &otg_sx->vbus_nb);
  198. if (ret < 0)
  199. dev_err(ssusb->dev, "failed to register notifier for USB\n");
  200. otg_sx->id_nb.notifier_call = ssusb_id_notifier;
  201. ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
  202. &otg_sx->id_nb);
  203. if (ret < 0)
  204. dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
  205. dev_dbg(ssusb->dev, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
  206. extcon_get_state(edev, EXTCON_USB),
  207. extcon_get_state(edev, EXTCON_USB_HOST));
  208. /* default as host, switch to device mode if needed */
  209. if (extcon_get_state(edev, EXTCON_USB_HOST) == false)
  210. ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
  211. if (extcon_get_state(edev, EXTCON_USB) == true)
  212. ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
  213. return 0;
  214. }
  215. /*
  216. * We provide an interface via debugfs to switch between host and device modes
  217. * depending on user input.
  218. * This is useful in special cases, such as uses TYPE-A receptacle but also
  219. * wants to support dual-role mode.
  220. */
  221. static void ssusb_mode_manual_switch(struct ssusb_mtk *ssusb, int to_host)
  222. {
  223. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  224. if (to_host) {
  225. ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
  226. ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
  227. ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
  228. } else {
  229. ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
  230. ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
  231. ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
  232. }
  233. }
  234. static int ssusb_mode_show(struct seq_file *sf, void *unused)
  235. {
  236. struct ssusb_mtk *ssusb = sf->private;
  237. seq_printf(sf, "current mode: %s(%s drd)\n(echo device/host)\n",
  238. ssusb->is_host ? "host" : "device",
  239. ssusb->otg_switch.manual_drd_enabled ? "manual" : "auto");
  240. return 0;
  241. }
  242. static int ssusb_mode_open(struct inode *inode, struct file *file)
  243. {
  244. return single_open(file, ssusb_mode_show, inode->i_private);
  245. }
  246. static ssize_t ssusb_mode_write(struct file *file,
  247. const char __user *ubuf, size_t count, loff_t *ppos)
  248. {
  249. struct seq_file *sf = file->private_data;
  250. struct ssusb_mtk *ssusb = sf->private;
  251. char buf[16];
  252. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  253. return -EFAULT;
  254. if (!strncmp(buf, "host", 4) && !ssusb->is_host) {
  255. ssusb_mode_manual_switch(ssusb, 1);
  256. } else if (!strncmp(buf, "device", 6) && ssusb->is_host) {
  257. ssusb_mode_manual_switch(ssusb, 0);
  258. } else {
  259. dev_err(ssusb->dev, "wrong or duplicated setting\n");
  260. return -EINVAL;
  261. }
  262. return count;
  263. }
  264. static const struct file_operations ssusb_mode_fops = {
  265. .open = ssusb_mode_open,
  266. .write = ssusb_mode_write,
  267. .read = seq_read,
  268. .llseek = seq_lseek,
  269. .release = single_release,
  270. };
  271. static int ssusb_vbus_show(struct seq_file *sf, void *unused)
  272. {
  273. struct ssusb_mtk *ssusb = sf->private;
  274. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  275. seq_printf(sf, "vbus state: %s\n(echo on/off)\n",
  276. regulator_is_enabled(otg_sx->vbus) ? "on" : "off");
  277. return 0;
  278. }
  279. static int ssusb_vbus_open(struct inode *inode, struct file *file)
  280. {
  281. return single_open(file, ssusb_vbus_show, inode->i_private);
  282. }
  283. static ssize_t ssusb_vbus_write(struct file *file,
  284. const char __user *ubuf, size_t count, loff_t *ppos)
  285. {
  286. struct seq_file *sf = file->private_data;
  287. struct ssusb_mtk *ssusb = sf->private;
  288. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  289. char buf[16];
  290. bool enable;
  291. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  292. return -EFAULT;
  293. if (kstrtobool(buf, &enable)) {
  294. dev_err(ssusb->dev, "wrong setting\n");
  295. return -EINVAL;
  296. }
  297. ssusb_set_vbus(otg_sx, enable);
  298. return count;
  299. }
  300. static const struct file_operations ssusb_vbus_fops = {
  301. .open = ssusb_vbus_open,
  302. .write = ssusb_vbus_write,
  303. .read = seq_read,
  304. .llseek = seq_lseek,
  305. .release = single_release,
  306. };
  307. static void ssusb_debugfs_init(struct ssusb_mtk *ssusb)
  308. {
  309. struct dentry *root;
  310. root = debugfs_create_dir(dev_name(ssusb->dev), usb_debug_root);
  311. ssusb->dbgfs_root = root;
  312. debugfs_create_file("mode", 0644, root, ssusb, &ssusb_mode_fops);
  313. debugfs_create_file("vbus", 0644, root, ssusb, &ssusb_vbus_fops);
  314. }
  315. static void ssusb_debugfs_exit(struct ssusb_mtk *ssusb)
  316. {
  317. debugfs_remove_recursive(ssusb->dbgfs_root);
  318. }
  319. void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
  320. enum mtu3_dr_force_mode mode)
  321. {
  322. u32 value;
  323. value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
  324. switch (mode) {
  325. case MTU3_DR_FORCE_DEVICE:
  326. value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
  327. break;
  328. case MTU3_DR_FORCE_HOST:
  329. value |= SSUSB_U2_PORT_FORCE_IDDIG;
  330. value &= ~SSUSB_U2_PORT_RG_IDDIG;
  331. break;
  332. case MTU3_DR_FORCE_NONE:
  333. value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
  334. break;
  335. default:
  336. return;
  337. }
  338. mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
  339. }
  340. int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
  341. {
  342. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  343. INIT_WORK(&otg_sx->id_work, ssusb_id_work);
  344. INIT_WORK(&otg_sx->vbus_work, ssusb_vbus_work);
  345. if (otg_sx->manual_drd_enabled)
  346. ssusb_debugfs_init(ssusb);
  347. else
  348. ssusb_extcon_register(otg_sx);
  349. return 0;
  350. }
  351. void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
  352. {
  353. struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
  354. if (otg_sx->manual_drd_enabled)
  355. ssusb_debugfs_exit(ssusb);
  356. cancel_work_sync(&otg_sx->id_work);
  357. cancel_work_sync(&otg_sx->vbus_work);
  358. }