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