Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v6.8
  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
 
 
 
 
 
 
 
 
 10#include "mtu3.h"
 11#include "mtu3_dr.h"
 12#include "mtu3_debug.h"
 13
 14#define USB2_PORT 2
 15#define USB3_PORT 3
 16
 17static inline struct ssusb_mtk *otg_sx_to_ssusb(struct otg_switch_mtk *otg_sx)
 18{
 19	return container_of(otg_sx, struct ssusb_mtk, otg_switch);
 20}
 
 
 21
 22static void toggle_opstate(struct ssusb_mtk *ssusb)
 23{
 24	mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION);
 25	mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN);
 
 
 26}
 27
 28/* only port0 supports dual-role mode */
 29static int ssusb_port0_switch(struct ssusb_mtk *ssusb,
 30	int version, bool tohost)
 31{
 32	void __iomem *ibase = ssusb->ippc_base;
 33	u32 value;
 34
 35	dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__,
 36		version, tohost ? "host" : "device");
 37
 38	if (version == USB2_PORT) {
 39		/* 1. power off and disable u2 port0 */
 40		value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
 41		value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
 42		mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
 43
 44		/* 2. power on, enable u2 port0 and select its mode */
 45		value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
 46		value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
 47		value = tohost ? (value | SSUSB_U2_PORT_HOST_SEL) :
 48			(value & (~SSUSB_U2_PORT_HOST_SEL));
 49		mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
 50	} else {
 51		/* 1. power off and disable u3 port0 */
 52		value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
 53		value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
 54		mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
 55
 56		/* 2. power on, enable u3 port0 and select its mode */
 57		value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
 58		value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
 59		value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) :
 60			(value & (~SSUSB_U3_PORT_HOST_SEL));
 61		mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
 62	}
 63
 64	return 0;
 65}
 66
 67static void switch_port_to_host(struct ssusb_mtk *ssusb)
 68{
 69	u32 check_clk = 0;
 70
 71	dev_dbg(ssusb->dev, "%s\n", __func__);
 72
 73	ssusb_port0_switch(ssusb, USB2_PORT, true);
 74
 75	if (ssusb->otg_switch.is_u3_drd) {
 76		ssusb_port0_switch(ssusb, USB3_PORT, true);
 77		check_clk = SSUSB_U3_MAC_RST_B_STS;
 78	}
 79
 80	ssusb_check_clocks(ssusb, check_clk);
 81
 82	/* after all clocks are stable */
 83	toggle_opstate(ssusb);
 84}
 85
 86static void switch_port_to_device(struct ssusb_mtk *ssusb)
 87{
 88	u32 check_clk = 0;
 89
 90	dev_dbg(ssusb->dev, "%s\n", __func__);
 91
 92	ssusb_port0_switch(ssusb, USB2_PORT, false);
 93
 94	if (ssusb->otg_switch.is_u3_drd) {
 95		ssusb_port0_switch(ssusb, USB3_PORT, false);
 96		check_clk = SSUSB_U3_MAC_RST_B_STS;
 97	}
 98
 99	ssusb_check_clocks(ssusb, check_clk);
100}
101
102int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
103{
104	struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
 
105	struct regulator *vbus = otg_sx->vbus;
106	int ret;
107
108	/* vbus is optional */
109	if (!vbus)
110		return 0;
111
112	dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off");
113
114	if (is_on) {
115		ret = regulator_enable(vbus);
116		if (ret) {
117			dev_err(ssusb->dev, "vbus regulator enable failed\n");
118			return ret;
119		}
120	} else {
121		regulator_disable(vbus);
122	}
123
124	return 0;
125}
126
127static void ssusb_mode_sw_work(struct work_struct *work)
 
 
 
 
 
128{
129	struct otg_switch_mtk *otg_sx =
130		container_of(work, struct otg_switch_mtk, dr_work);
131	struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
132	struct mtu3 *mtu = ssusb->u3d;
133	enum usb_role desired_role = otg_sx->desired_role;
134	enum usb_role current_role;
135
136	current_role = ssusb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
137
138	if (desired_role == USB_ROLE_NONE) {
139		/* the default mode is host as probe does */
140		desired_role = USB_ROLE_HOST;
141		if (otg_sx->default_role == USB_ROLE_DEVICE)
142			desired_role = USB_ROLE_DEVICE;
143	}
144
145	if (current_role == desired_role)
146		return;
147
148	dev_dbg(ssusb->dev, "set role : %s\n", usb_role_string(desired_role));
149	mtu3_dbg_trace(ssusb->dev, "set role : %s", usb_role_string(desired_role));
150	pm_runtime_get_sync(ssusb->dev);
151
152	switch (desired_role) {
153	case USB_ROLE_HOST:
154		ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
155		mtu3_stop(mtu);
156		switch_port_to_host(ssusb);
157		ssusb_set_vbus(otg_sx, 1);
158		ssusb->is_host = true;
159		break;
160	case USB_ROLE_DEVICE:
161		ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
162		ssusb->is_host = false;
163		ssusb_set_vbus(otg_sx, 0);
164		switch_port_to_device(ssusb);
 
 
 
 
 
 
 
 
165		mtu3_start(mtu);
166		break;
167	case USB_ROLE_NONE:
168	default:
169		dev_err(ssusb->dev, "invalid role\n");
170	}
171	pm_runtime_put(ssusb->dev);
172}
173
174static void ssusb_set_mode(struct otg_switch_mtk *otg_sx, enum usb_role role)
 
175{
176	struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
 
177
178	if (ssusb->dr_mode != USB_DR_MODE_OTG)
179		return;
 
 
180
181	otg_sx->desired_role = role;
182	queue_work(system_freezable_wq, &otg_sx->dr_work);
183}
184
185static int ssusb_id_notifier(struct notifier_block *nb,
186	unsigned long event, void *ptr)
187{
188	struct otg_switch_mtk *otg_sx =
189		container_of(nb, struct otg_switch_mtk, id_nb);
190
191	ssusb_set_mode(otg_sx, event ? USB_ROLE_HOST : USB_ROLE_DEVICE);
 
 
 
192
193	return NOTIFY_DONE;
194}
195
196static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
197{
198	struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
 
199	struct extcon_dev *edev = otg_sx->edev;
200	int ret;
201
202	/* extcon is optional */
203	if (!edev)
204		return 0;
205
 
 
 
 
 
 
206	otg_sx->id_nb.notifier_call = ssusb_id_notifier;
207	ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
208					&otg_sx->id_nb);
209	if (ret < 0) {
210		dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
211		return ret;
212	}
213
214	ret = extcon_get_state(edev, EXTCON_USB_HOST);
215	dev_dbg(ssusb->dev, "EXTCON_USB_HOST: %d\n", ret);
 
216
217	/* default as host, switch to device mode if needed */
218	if (!ret)
219		ssusb_set_mode(otg_sx, USB_ROLE_DEVICE);
 
 
220
221	return 0;
222}
223
 
 
 
 
 
 
 
 
 
224/*
225 * We provide an interface via debugfs to switch between host and device modes
226 * depending on user input.
227 * This is useful in special cases, such as uses TYPE-A receptacle but also
228 * wants to support dual-role mode.
229 */
230void ssusb_mode_switch(struct ssusb_mtk *ssusb, int to_host)
231{
232	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
233
234	ssusb_set_mode(otg_sx, to_host ? USB_ROLE_HOST : USB_ROLE_DEVICE);
 
 
 
 
 
 
 
 
235}
236
237void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
238			  enum mtu3_dr_force_mode mode)
239{
240	u32 value;
241
242	value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
243	switch (mode) {
244	case MTU3_DR_FORCE_DEVICE:
245		value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
246		break;
247	case MTU3_DR_FORCE_HOST:
248		value |= SSUSB_U2_PORT_FORCE_IDDIG;
249		value &= ~SSUSB_U2_PORT_RG_IDDIG;
250		break;
251	case MTU3_DR_FORCE_NONE:
252		value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
253		break;
254	default:
255		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256	}
257	mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
 
258}
259
260static int ssusb_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
 
 
 
 
 
 
 
 
261{
262	struct ssusb_mtk *ssusb = usb_role_switch_get_drvdata(sw);
263	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
264
265	ssusb_set_mode(otg_sx, role);
 
266
267	return 0;
268}
269
270static enum usb_role ssusb_role_sw_get(struct usb_role_switch *sw)
271{
272	struct ssusb_mtk *ssusb = usb_role_switch_get_drvdata(sw);
 
273
274	return ssusb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275}
276
277static int ssusb_role_sw_register(struct otg_switch_mtk *otg_sx)
 
 
 
 
 
 
 
 
278{
279	struct usb_role_switch_desc role_sx_desc = { 0 };
280	struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
281	struct device *dev = ssusb->dev;
282	enum usb_dr_mode mode;
283
284	if (!otg_sx->role_sw_used)
285		return 0;
 
 
 
 
286
287	mode = usb_get_role_switch_default_mode(dev);
288	if (mode == USB_DR_MODE_PERIPHERAL)
289		otg_sx->default_role = USB_ROLE_DEVICE;
290	else
291		otg_sx->default_role = USB_ROLE_HOST;
292
293	role_sx_desc.set = ssusb_role_sw_set;
294	role_sx_desc.get = ssusb_role_sw_get;
295	role_sx_desc.fwnode = dev_fwnode(dev);
296	role_sx_desc.driver_data = ssusb;
297	role_sx_desc.allow_userspace_control = true;
298	otg_sx->role_sw = usb_role_switch_register(dev, &role_sx_desc);
299	if (IS_ERR(otg_sx->role_sw))
300		return PTR_ERR(otg_sx->role_sw);
301
302	ssusb_set_mode(otg_sx, otg_sx->default_role);
 
 
 
303
304	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305}
306
307int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
308{
309	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
310	int ret = 0;
311
312	INIT_WORK(&otg_sx->dr_work, ssusb_mode_sw_work);
 
 
 
 
313
314	if (otg_sx->manual_drd_enabled)
315		ssusb_dr_debugfs_init(ssusb);
316	else if (otg_sx->role_sw_used)
317		ret = ssusb_role_sw_register(otg_sx);
318	else
319		ret = ssusb_extcon_register(otg_sx);
320
321	return ret;
322}
323
324void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
325{
326	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
327
328	cancel_work_sync(&otg_sx->dr_work);
329	usb_role_switch_unregister(otg_sx->role_sw);
 
 
330}
v4.17
  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
 10#include <linux/debugfs.h>
 11#include <linux/irq.h>
 12#include <linux/kernel.h>
 13#include <linux/of_device.h>
 14#include <linux/pinctrl/consumer.h>
 15#include <linux/seq_file.h>
 16#include <linux/uaccess.h>
 17
 18#include "mtu3.h"
 19#include "mtu3_dr.h"
 
 20
 21#define USB2_PORT 2
 22#define USB3_PORT 3
 23
 24enum mtu3_vbus_id_state {
 25	MTU3_ID_FLOAT = 1,
 26	MTU3_ID_GROUND,
 27	MTU3_VBUS_OFF,
 28	MTU3_VBUS_VALID,
 29};
 30
 31static void toggle_opstate(struct ssusb_mtk *ssusb)
 32{
 33	if (!ssusb->otg_switch.is_u3_drd) {
 34		mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION);
 35		mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN);
 36	}
 37}
 38
 39/* only port0 supports dual-role mode */
 40static int ssusb_port0_switch(struct ssusb_mtk *ssusb,
 41	int version, bool tohost)
 42{
 43	void __iomem *ibase = ssusb->ippc_base;
 44	u32 value;
 45
 46	dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__,
 47		version, tohost ? "host" : "device");
 48
 49	if (version == USB2_PORT) {
 50		/* 1. power off and disable u2 port0 */
 51		value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
 52		value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
 53		mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
 54
 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
 67		/* 2. power on, enable u3 port0 and select its mode */
 68		value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
 69		value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
 70		value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) :
 71			(value & (~SSUSB_U3_PORT_HOST_SEL));
 72		mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
 73	}
 74
 75	return 0;
 76}
 77
 78static void switch_port_to_host(struct ssusb_mtk *ssusb)
 79{
 80	u32 check_clk = 0;
 81
 82	dev_dbg(ssusb->dev, "%s\n", __func__);
 83
 84	ssusb_port0_switch(ssusb, USB2_PORT, true);
 85
 86	if (ssusb->otg_switch.is_u3_drd) {
 87		ssusb_port0_switch(ssusb, USB3_PORT, true);
 88		check_clk = SSUSB_U3_MAC_RST_B_STS;
 89	}
 90
 91	ssusb_check_clocks(ssusb, check_clk);
 92
 93	/* after all clocks are stable */
 94	toggle_opstate(ssusb);
 95}
 96
 97static void switch_port_to_device(struct ssusb_mtk *ssusb)
 98{
 99	u32 check_clk = 0;
100
101	dev_dbg(ssusb->dev, "%s\n", __func__);
102
103	ssusb_port0_switch(ssusb, USB2_PORT, false);
104
105	if (ssusb->otg_switch.is_u3_drd) {
106		ssusb_port0_switch(ssusb, USB3_PORT, false);
107		check_clk = SSUSB_U3_MAC_RST_B_STS;
108	}
109
110	ssusb_check_clocks(ssusb, check_clk);
111}
112
113int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
114{
115	struct ssusb_mtk *ssusb =
116		container_of(otg_sx, struct ssusb_mtk, otg_switch);
117	struct regulator *vbus = otg_sx->vbus;
118	int ret;
119
120	/* vbus is optional */
121	if (!vbus)
122		return 0;
123
124	dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off");
125
126	if (is_on) {
127		ret = regulator_enable(vbus);
128		if (ret) {
129			dev_err(ssusb->dev, "vbus regulator enable failed\n");
130			return ret;
131		}
132	} else {
133		regulator_disable(vbus);
134	}
135
136	return 0;
137}
138
139/*
140 * switch to host: -> MTU3_VBUS_OFF --> MTU3_ID_GROUND
141 * switch to device: -> MTU3_ID_FLOAT --> MTU3_VBUS_VALID
142 */
143static void ssusb_set_mailbox(struct otg_switch_mtk *otg_sx,
144	enum mtu3_vbus_id_state status)
145{
146	struct ssusb_mtk *ssusb =
147		container_of(otg_sx, struct ssusb_mtk, otg_switch);
 
148	struct mtu3 *mtu = ssusb->u3d;
 
 
149
150	dev_dbg(ssusb->dev, "mailbox state(%d)\n", status);
151
152	switch (status) {
153	case MTU3_ID_GROUND:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154		switch_port_to_host(ssusb);
155		ssusb_set_vbus(otg_sx, 1);
156		ssusb->is_host = true;
157		break;
158	case MTU3_ID_FLOAT:
 
159		ssusb->is_host = false;
160		ssusb_set_vbus(otg_sx, 0);
161		switch_port_to_device(ssusb);
162		break;
163	case MTU3_VBUS_OFF:
164		mtu3_stop(mtu);
165		pm_relax(ssusb->dev);
166		break;
167	case MTU3_VBUS_VALID:
168		/* avoid suspend when works as device */
169		pm_stay_awake(ssusb->dev);
170		mtu3_start(mtu);
171		break;
 
172	default:
173		dev_err(ssusb->dev, "invalid state\n");
174	}
 
175}
176
177static int ssusb_id_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, id_nb);
182
183	if (event)
184		ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
185	else
186		ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
187
188	return NOTIFY_DONE;
 
189}
190
191static int ssusb_vbus_notifier(struct notifier_block *nb,
192	unsigned long event, void *ptr)
193{
194	struct otg_switch_mtk *otg_sx =
195		container_of(nb, struct otg_switch_mtk, vbus_nb);
196
197	if (event)
198		ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
199	else
200		ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
201
202	return NOTIFY_DONE;
203}
204
205static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
206{
207	struct ssusb_mtk *ssusb =
208		container_of(otg_sx, struct ssusb_mtk, otg_switch);
209	struct extcon_dev *edev = otg_sx->edev;
210	int ret;
211
212	/* extcon is optional */
213	if (!edev)
214		return 0;
215
216	otg_sx->vbus_nb.notifier_call = ssusb_vbus_notifier;
217	ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB,
218					&otg_sx->vbus_nb);
219	if (ret < 0)
220		dev_err(ssusb->dev, "failed to register notifier for USB\n");
221
222	otg_sx->id_nb.notifier_call = ssusb_id_notifier;
223	ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
224					&otg_sx->id_nb);
225	if (ret < 0)
226		dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
 
 
227
228	dev_dbg(ssusb->dev, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
229		extcon_get_state(edev, EXTCON_USB),
230		extcon_get_state(edev, EXTCON_USB_HOST));
231
232	/* default as host, switch to device mode if needed */
233	if (extcon_get_state(edev, EXTCON_USB_HOST) == false)
234		ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
235	if (extcon_get_state(edev, EXTCON_USB) == true)
236		ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
237
238	return 0;
239}
240
241static void extcon_register_dwork(struct work_struct *work)
242{
243	struct delayed_work *dwork = to_delayed_work(work);
244	struct otg_switch_mtk *otg_sx =
245	    container_of(dwork, struct otg_switch_mtk, extcon_reg_dwork);
246
247	ssusb_extcon_register(otg_sx);
248}
249
250/*
251 * We provide an interface via debugfs to switch between host and device modes
252 * depending on user input.
253 * This is useful in special cases, such as uses TYPE-A receptacle but also
254 * wants to support dual-role mode.
255 */
256static void ssusb_mode_manual_switch(struct ssusb_mtk *ssusb, int to_host)
257{
258	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
259
260	if (to_host) {
261		ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
262		ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
263		ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
264	} else {
265		ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
266		ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
267		ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
268	}
269}
270
271static int ssusb_mode_show(struct seq_file *sf, void *unused)
 
272{
273	struct ssusb_mtk *ssusb = sf->private;
274
275	seq_printf(sf, "current mode: %s(%s drd)\n(echo device/host)\n",
276		ssusb->is_host ? "host" : "device",
277		ssusb->otg_switch.manual_drd_enabled ? "manual" : "auto");
278
279	return 0;
280}
281
282static int ssusb_mode_open(struct inode *inode, struct file *file)
283{
284	return single_open(file, ssusb_mode_show, inode->i_private);
285}
286
287static ssize_t ssusb_mode_write(struct file *file,
288	const char __user *ubuf, size_t count, loff_t *ppos)
289{
290	struct seq_file *sf = file->private_data;
291	struct ssusb_mtk *ssusb = sf->private;
292	char buf[16];
293
294	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
295		return -EFAULT;
296
297	if (!strncmp(buf, "host", 4) && !ssusb->is_host) {
298		ssusb_mode_manual_switch(ssusb, 1);
299	} else if (!strncmp(buf, "device", 6) && ssusb->is_host) {
300		ssusb_mode_manual_switch(ssusb, 0);
301	} else {
302		dev_err(ssusb->dev, "wrong or duplicated setting\n");
303		return -EINVAL;
304	}
305
306	return count;
307}
308
309static const struct file_operations ssusb_mode_fops = {
310	.open = ssusb_mode_open,
311	.write = ssusb_mode_write,
312	.read = seq_read,
313	.llseek = seq_lseek,
314	.release = single_release,
315};
316
317static int ssusb_vbus_show(struct seq_file *sf, void *unused)
318{
319	struct ssusb_mtk *ssusb = sf->private;
320	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
321
322	seq_printf(sf, "vbus state: %s\n(echo on/off)\n",
323		regulator_is_enabled(otg_sx->vbus) ? "on" : "off");
324
325	return 0;
326}
327
328static int ssusb_vbus_open(struct inode *inode, struct file *file)
329{
330	return single_open(file, ssusb_vbus_show, inode->i_private);
331}
332
333static ssize_t ssusb_vbus_write(struct file *file,
334	const char __user *ubuf, size_t count, loff_t *ppos)
335{
336	struct seq_file *sf = file->private_data;
337	struct ssusb_mtk *ssusb = sf->private;
338	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
339	char buf[16];
340	bool enable;
341
342	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
343		return -EFAULT;
344
345	if (kstrtobool(buf, &enable)) {
346		dev_err(ssusb->dev, "wrong setting\n");
347		return -EINVAL;
348	}
349
350	ssusb_set_vbus(otg_sx, enable);
351
352	return count;
353}
354
355static const struct file_operations ssusb_vbus_fops = {
356	.open = ssusb_vbus_open,
357	.write = ssusb_vbus_write,
358	.read = seq_read,
359	.llseek = seq_lseek,
360	.release = single_release,
361};
362
363static void ssusb_debugfs_init(struct ssusb_mtk *ssusb)
364{
365	struct dentry *root;
 
 
 
366
367	root = debugfs_create_dir(dev_name(ssusb->dev), usb_debug_root);
368	if (!root) {
369		dev_err(ssusb->dev, "create debugfs root failed\n");
370		return;
371	}
372	ssusb->dbgfs_root = root;
373
374	debugfs_create_file("mode", 0644, root, ssusb, &ssusb_mode_fops);
375	debugfs_create_file("vbus", 0644, root, ssusb, &ssusb_vbus_fops);
376}
 
 
377
378static void ssusb_debugfs_exit(struct ssusb_mtk *ssusb)
379{
380	debugfs_remove_recursive(ssusb->dbgfs_root);
381}
 
 
 
 
382
383void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
384			  enum mtu3_dr_force_mode mode)
385{
386	u32 value;
387
388	value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
389	switch (mode) {
390	case MTU3_DR_FORCE_DEVICE:
391		value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
392		break;
393	case MTU3_DR_FORCE_HOST:
394		value |= SSUSB_U2_PORT_FORCE_IDDIG;
395		value &= ~SSUSB_U2_PORT_RG_IDDIG;
396		break;
397	case MTU3_DR_FORCE_NONE:
398		value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
399		break;
400	default:
401		return;
402	}
403	mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
404}
405
406int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
407{
408	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
 
409
410	if (otg_sx->manual_drd_enabled) {
411		ssusb_debugfs_init(ssusb);
412	} else {
413		INIT_DELAYED_WORK(&otg_sx->extcon_reg_dwork,
414				  extcon_register_dwork);
415
416		/*
417		 * It is enough to delay 1s for waiting for
418		 * host initialization
419		 */
420		schedule_delayed_work(&otg_sx->extcon_reg_dwork, HZ);
421	}
422
423	return 0;
424}
425
426void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
427{
428	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
429
430	if (otg_sx->manual_drd_enabled)
431		ssusb_debugfs_exit(ssusb);
432	else
433		cancel_delayed_work(&otg_sx->extcon_reg_dwork);
434}