mtu3_dr.c 9.7 KB

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