1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
/*
* exynos-usb-switch.c - USB switch driver for Exynos
*
* Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
* Yulgon Kim <yulgon.kim@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/pm_runtime.h>
#include <linux/wakelock.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_data/exynos-usb-switch.h>
#include <linux/usb/gadget.h>
#include <linux/regulator/consumer.h>
#include "exynos-usb-switch.h"
#define DRIVER_DESC "Exynos USB Switch Driver"
#define SWITCH_WAIT_TIME 500
#define WAIT_TIMES 10
static const char switch_name[] = "exynos_usb_switch";
static struct exynos_usb_switch *our_switch;
const char *exynos_usbswitch_mode_string(unsigned long mode)
{
if (!mode)
return "IDLE";
else if (test_bit(USB_HOST_ATTACHED, &mode))
return "USB_HOST_ATTACHED";
else if (test_bit(USB_DEVICE_ATTACHED, &mode))
return "USB_DEVICE_ATTACHED";
else
/* something wrong */
return "undefined";
}
static int is_host_detect(struct exynos_usb_switch *usb_switch)
{
if (!gpio_is_valid(usb_switch->gpio_host_detect))
return 0;
return !gpio_get_value(usb_switch->gpio_host_detect);
}
static int is_device_detect(struct exynos_usb_switch *usb_switch)
{
if (!gpio_is_valid(usb_switch->gpio_device_detect))
return 0;
return gpio_get_value(usb_switch->gpio_device_detect);
}
static void set_host_vbus(struct exynos_usb_switch *usb_switch, int value)
{
if (gpio_is_valid(usb_switch->gpio_host_vbus)) {
gpio_set_value(usb_switch->gpio_host_vbus, value);
} else if (!IS_ERR(usb_switch->vbus_reg)) {
int ret;
if (value)
ret = regulator_enable(usb_switch->vbus_reg);
else
ret = regulator_disable(usb_switch->vbus_reg);
if (ret)
pr_err("Failed to turn UHost Vbus %s\n",
value ? "on" : "off");
}
}
struct usb_gadget *
__attribute__((weak)) s3c_udc_get_gadget(struct device *dev)
{
return NULL;
}
#if defined(CONFIG_USB_NOTIFIER)
int exynos_otg_vbus_event(struct platform_device *pdev, bool is_active)
{
struct usb_gadget *gadget = s3c_udc_get_gadget(&pdev->dev);
if (is_active)
usb_gadget_vbus_connect(gadget);
else
usb_gadget_vbus_disconnect(gadget);
return 0;
}
#endif
static int exynos_change_usb_mode(struct exynos_usb_switch *usb_switch,
enum usb_cable_status mode)
{
struct usb_gadget *gadget;
unsigned long cur_mode = usb_switch->connect;
int ret = 0;
if (test_bit(USB_DEVICE_ATTACHED, &cur_mode) ||
test_bit(USB_HOST_ATTACHED, &cur_mode)) {
if (mode == USB_DEVICE_ATTACHED ||
mode == USB_HOST_ATTACHED) {
printk(KERN_DEBUG "Skip request %d, current %lu\n",
mode, cur_mode);
return -EPERM;
}
}
if (!test_bit(USB_DEVICE_ATTACHED, &cur_mode) &&
mode == USB_DEVICE_DETACHED) {
printk(KERN_DEBUG "Skip request %d, current %lu\n",
mode, cur_mode);
return -EPERM;
} else if (!test_bit(USB_HOST_ATTACHED, &cur_mode) &&
mode == USB_HOST_DETACHED) {
printk(KERN_DEBUG "Skip request %d, current %lu\n",
mode, cur_mode);
return -EPERM;
}
/*
* FIXME: Currently we get gadget every time usb mode is going
* to change. This prevents from problems related to UDC module
* removing and wrong probe order. S3C UDC driver should provide
* corresponding helper function. If S3C UDC is not available weak
* analog is used instead.
*
* Correct solution would be to implement binding similar to OTG's
* set_host() and set_peripheral().
*/
gadget = s3c_udc_get_gadget(usb_switch->s3c_udc_dev);
switch (mode) {
case USB_DEVICE_DETACHED:
if (test_bit(USB_HOST_ATTACHED, &cur_mode)) {
printk(KERN_ERR "Abnormal request %d, current %lu\n",
mode, cur_mode);
return -EPERM;
}
if (gadget && gadget->ops)
usb_gadget_vbus_disconnect(gadget);
clear_bit(USB_DEVICE_ATTACHED, &usb_switch->connect);
break;
case USB_DEVICE_ATTACHED:
if (gadget && gadget->ops)
usb_gadget_vbus_connect(gadget);
set_bit(USB_DEVICE_ATTACHED, &usb_switch->connect);
break;
case USB_HOST_DETACHED:
if (test_bit(USB_DEVICE_ATTACHED, &cur_mode)) {
printk(KERN_ERR "Abnormal request %d, current %lu\n",
mode, cur_mode);
return -EPERM;
}
if (usb_switch->ohci_dev)
pm_runtime_allow(usb_switch->ohci_dev);
if (usb_switch->ehci_dev)
pm_runtime_allow(usb_switch->ehci_dev);
if (usb_switch->gpio_host_vbus)
set_host_vbus(usb_switch, 0);
wake_unlock(&usb_switch->wake_lock);
clear_bit(USB_HOST_ATTACHED, &usb_switch->connect);
break;
case USB_HOST_ATTACHED:
wake_lock(&usb_switch->wake_lock);
if (usb_switch->gpio_host_vbus)
set_host_vbus(usb_switch, 1);
if (usb_switch->ehci_dev)
pm_runtime_forbid(usb_switch->ehci_dev);
if (usb_switch->ohci_dev)
pm_runtime_forbid(usb_switch->ohci_dev);
set_bit(USB_HOST_ATTACHED, &usb_switch->connect);
break;
default:
printk(KERN_ERR "Does not changed\n");
}
printk(KERN_ERR "usb cable = %d\n", mode);
return ret;
}
static void exynos_usb_switch_worker(struct work_struct *work)
{
struct exynos_usb_switch *usb_switch =
container_of(work, struct exynos_usb_switch, switch_work);
int cnt = 0;
mutex_lock(&usb_switch->mutex);
/* If already device detached or host_detected, */
if (!is_device_detect(usb_switch) || is_host_detect(usb_switch))
goto done;
if (!usb_switch->ehci_dev || !usb_switch->ohci_dev)
goto detect;
while (!pm_runtime_suspended(usb_switch->ehci_dev) ||
!pm_runtime_suspended(usb_switch->ohci_dev)) {
mutex_unlock(&usb_switch->mutex);
msleep(SWITCH_WAIT_TIME);
mutex_lock(&usb_switch->mutex);
/* If already device detached or host_detected, */
if (!is_device_detect(usb_switch) || is_host_detect(usb_switch))
goto done;
if (cnt++ > WAIT_TIMES) {
printk(KERN_ERR "%s:device not attached by host\n",
__func__);
goto done;
}
}
if (cnt > 1)
printk(KERN_INFO "Device wait host power during %d\n", (cnt-1));
detect:
/* Check Device, VBUS PIN high active */
exynos_change_usb_mode(usb_switch, USB_DEVICE_ATTACHED);
done:
mutex_unlock(&usb_switch->mutex);
}
static irqreturn_t exynos_host_detect_thread(int irq, void *data)
{
struct exynos_usb_switch *usb_switch = data;
pr_err("%s\n", __func__);
mutex_lock(&usb_switch->mutex);
if (is_host_detect(usb_switch))
exynos_change_usb_mode(usb_switch, USB_HOST_ATTACHED);
else
exynos_change_usb_mode(usb_switch, USB_HOST_DETACHED);
mutex_unlock(&usb_switch->mutex);
return IRQ_HANDLED;
}
static irqreturn_t exynos_device_detect_thread(int irq, void *data)
{
struct exynos_usb_switch *usb_switch = data;
mutex_lock(&usb_switch->mutex);
/* Debounce connect delay */
msleep(20);
if (is_host_detect(usb_switch))
printk(KERN_DEBUG "Not expected situation\n");
else if (is_device_detect(usb_switch)) {
if (usb_switch->gpio_host_vbus)
exynos_change_usb_mode(usb_switch, USB_DEVICE_ATTACHED);
else
queue_work(usb_switch->workqueue, &usb_switch->switch_work);
} else {
/* VBUS PIN low */
exynos_change_usb_mode(usb_switch, USB_DEVICE_DETACHED);
}
mutex_unlock(&usb_switch->mutex);
return IRQ_HANDLED;
}
static int exynos_usb_status_init(struct exynos_usb_switch *usb_switch)
{
mutex_lock(&usb_switch->mutex);
/* 2.0 USB */
if (is_host_detect(usb_switch))
exynos_change_usb_mode(usb_switch, USB_HOST_ATTACHED);
else if (is_device_detect(usb_switch)) {
if (usb_switch->gpio_host_vbus)
exynos_change_usb_mode(usb_switch,
USB_DEVICE_ATTACHED);
else
queue_work(usb_switch->workqueue,
&usb_switch->switch_work);
}
mutex_unlock(&usb_switch->mutex);
return 0;
}
#ifdef CONFIG_PM
static int exynos_usbswitch_suspend(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct exynos_usb_switch *usb_switch = platform_get_drvdata(pdev);
dev_dbg(dev, "%s\n", __func__);
mutex_lock(&usb_switch->mutex);
if (test_bit(USB_DEVICE_ATTACHED, &usb_switch->connect))
exynos_change_usb_mode(usb_switch, USB_DEVICE_DETACHED);
usb_switch->connect = 0;
mutex_unlock(&usb_switch->mutex);
return 0;
}
static int exynos_usbswitch_resume(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct exynos_usb_switch *usb_switch = platform_get_drvdata(pdev);
dev_dbg(dev, "%s\n", __func__);
exynos_usb_status_init(usb_switch);
return 0;
}
#else
#define exynos_usbswitch_suspend NULL
#define exynos_usbswitch_resume NULL
#endif
/* SysFS interface */
static ssize_t
exynos_usbswitch_show_mode(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct exynos_usb_switch *usb_switch = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%s\n",
exynos_usbswitch_mode_string(usb_switch->connect));
}
static DEVICE_ATTR(mode, S_IRUSR | S_IRGRP,
exynos_usbswitch_show_mode, NULL);
static ssize_t
exynos_usbswitch_store_id(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n)
{
struct exynos_usb_switch *usb_switch = dev_get_drvdata(dev);
int id;
if (sscanf(buf, "%d", &id) != 1)
return -EINVAL;
mutex_lock(&usb_switch->mutex);
if (!id)
exynos_change_usb_mode(usb_switch, USB_HOST_ATTACHED);
else
exynos_change_usb_mode(usb_switch, USB_HOST_DETACHED);
mutex_unlock(&usb_switch->mutex);
return n;
}
static DEVICE_ATTR(id, S_IWUSR | S_IRUSR | S_IRGRP,
NULL, exynos_usbswitch_store_id);
static struct attribute *exynos_usbswitch_attributes[] = {
&dev_attr_id.attr,
&dev_attr_mode.attr,
NULL
};
static const struct attribute_group exynos_usbswitch_attr_group = {
.attrs = exynos_usbswitch_attributes,
};
static int exynos_usbswitch_parse_dt(struct exynos_usb_switch *usb_switch,
struct device *dev)
{
struct device_node *node;
struct platform_device *pdev;
struct pinctrl *pinctrl;
int ret;
/* Host detection */
usb_switch->gpio_host_detect = of_get_named_gpio(dev->of_node,
"samsung,id-gpio", 0);
if (!gpio_is_valid(usb_switch->gpio_host_detect)) {
dev_info(dev, "host detect gpio is not available\n");
} else {
ret = devm_gpio_request(dev, usb_switch->gpio_host_detect,
"usbswitch_id_gpio");
if (ret)
dev_err(dev, "failed to request host detect gpio");
else
usb_switch->host_detect_irq =
gpio_to_irq(usb_switch->gpio_host_detect);
}
/* Device detection */
usb_switch->gpio_device_detect = of_get_named_gpio(dev->of_node,
"samsung,bsess-gpio", 0);
if (!gpio_is_valid(usb_switch->gpio_device_detect)) {
dev_info(dev, "device detect gpio is not available\n");
} else {
ret = devm_gpio_request(dev, usb_switch->gpio_device_detect,
"usbswitch_b_sess_gpio");
if (ret)
dev_err(dev, "failed to request host detect gpio");
else
usb_switch->device_detect_irq =
gpio_to_irq(usb_switch->gpio_device_detect);
}
/* VBus control */
usb_switch->gpio_host_vbus = of_get_named_gpio(dev->of_node,
"samsung,vbus-gpio", 0);
if (gpio_is_valid(usb_switch->gpio_host_vbus)) {
ret = devm_gpio_request(dev, usb_switch->gpio_host_vbus,
"usbswitch_vbus_gpio");
if (ret)
dev_err(dev, "failed to request vbus control gpio");
} else {
/* fallback to regulator */
usb_switch->vbus_reg = devm_regulator_get(dev, "uhost-vbus");
if (IS_ERR(usb_switch->vbus_reg))
dev_info(dev, "vbus control is not available\n");
}
pinctrl = devm_pinctrl_get_select_default(dev);
if (IS_ERR(pinctrl))
dev_info(dev, "failed to configure pins\n");
/* EHCI */
node = of_parse_phandle(dev->of_node, "ehci", 0);
if (!node) {
dev_info(dev, "ehci device is not available\n");
} else {
pdev = of_find_device_by_node(node);
if (!pdev)
dev_err(dev, "failed to find ehci device\n");
else
usb_switch->ehci_dev = &pdev->dev;
of_node_put(node);
}
/* OHCI */
node = of_parse_phandle(dev->of_node, "ohci", 0);
if (!node) {
dev_info(dev, "ohci device is not available\n");
} else {
pdev = of_find_device_by_node(node);
if (!pdev)
dev_err(dev, "failed to find ohci device\n");
else
usb_switch->ohci_dev = &pdev->dev;
of_node_put(node);
}
/* UDC */
node = of_parse_phandle(dev->of_node, "udc", 0);
if (!node) {
dev_info(dev, "udc device is not available\n");
} else {
pdev = of_find_device_by_node(node);
if (!pdev)
dev_err(dev, "failed to find udc device\n");
else
usb_switch->s3c_udc_dev = &pdev->dev;
of_node_put(node);
}
return 0;
}
static int exynos_usbswitch_probe(struct platform_device *pdev)
{
struct s5p_usbswitch_platdata *pdata = dev_get_platdata(&pdev->dev);
struct device *dev = &pdev->dev;
struct exynos_usb_switch *usb_switch;
int ret = 0;
usb_switch = devm_kzalloc(dev, sizeof(struct exynos_usb_switch),
GFP_KERNEL);
if (!usb_switch)
return -ENOMEM;
our_switch = usb_switch;
mutex_init(&usb_switch->mutex);
wake_lock_init(&usb_switch->wake_lock, WAKE_LOCK_SUSPEND,
"usb_switch_present");
usb_switch->workqueue = create_singlethread_workqueue("usb_switch");
INIT_WORK(&usb_switch->switch_work, exynos_usb_switch_worker);
if (dev->of_node) {
ret = exynos_usbswitch_parse_dt(usb_switch, dev);
if (ret < 0) {
dev_err(dev, "Failed to parse dt\n");
goto fail;
}
} else if (pdata) {
usb_switch->gpio_host_detect = pdata->gpio_host_detect;
usb_switch->gpio_device_detect = pdata->gpio_device_detect;
usb_switch->gpio_host_vbus = pdata->gpio_host_vbus;
usb_switch->ehci_dev = pdata->ehci_dev;
usb_switch->ohci_dev = pdata->ohci_dev;
usb_switch->s3c_udc_dev = pdata->s3c_udc_dev;
usb_switch->host_detect_irq = platform_get_irq(pdev, 0);
usb_switch->device_detect_irq = platform_get_irq(pdev, 1);
} else {
dev_err(dev, "Platform data is not available\n");
ret = -ENODEV;
goto fail;
}
/* USB Device detect IRQ */
if (usb_switch->device_detect_irq > 0 && usb_switch->s3c_udc_dev) {
ret = devm_request_threaded_irq(dev,
usb_switch->device_detect_irq,
NULL, exynos_device_detect_thread,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
IRQF_ONESHOT, "DEVICE_DETECT", usb_switch);
if (ret) {
dev_err(dev, "Failed to request device irq %d\n",
usb_switch->device_detect_irq);
goto fail;
}
} else if (usb_switch->s3c_udc_dev) {
exynos_change_usb_mode(usb_switch, USB_DEVICE_ATTACHED);
} else {
dev_info(dev, "Disable device detect IRQ\n");
}
/* USB Host detect IRQ */
if (usb_switch->host_detect_irq > 0 && (usb_switch->ehci_dev ||
usb_switch->ohci_dev)) {
ret = devm_request_threaded_irq(dev,
usb_switch->host_detect_irq,
NULL, exynos_host_detect_thread,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
IRQF_ONESHOT, "HOST_DETECT", usb_switch);
if (ret) {
dev_err(dev, "Failed to request host irq %d\n",
usb_switch->host_detect_irq);
goto fail;
}
} else if (usb_switch->ehci_dev || usb_switch->ohci_dev) {
exynos_change_usb_mode(usb_switch, USB_HOST_ATTACHED);
} else {
dev_info(dev, "Disable host detect IRQ\n");
}
exynos_usb_status_init(usb_switch);
ret = sysfs_create_group(&dev->kobj, &exynos_usbswitch_attr_group);
if (ret)
dev_warn(dev, "failed to create dwc3 otg attributes\n");
platform_set_drvdata(pdev, usb_switch);
return 0;
fail:
wake_unlock(&usb_switch->wake_lock);
cancel_work_sync(&usb_switch->switch_work);
destroy_workqueue(usb_switch->workqueue);
mutex_destroy(&usb_switch->mutex);
return ret;
}
static int exynos_usbswitch_remove(struct platform_device *pdev)
{
struct exynos_usb_switch *usb_switch = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, 0);
sysfs_remove_group(&pdev->dev.kobj, &exynos_usbswitch_attr_group);
wake_unlock(&usb_switch->wake_lock);
cancel_work_sync(&usb_switch->switch_work);
destroy_workqueue(usb_switch->workqueue);
mutex_destroy(&usb_switch->mutex);
return 0;
}
static const struct dev_pm_ops exynos_usbswitch_pm_ops = {
.suspend = exynos_usbswitch_suspend,
.resume = exynos_usbswitch_resume,
};
static const struct of_device_id of_exynos_usbswitch_match[] = {
{
.compatible = "samsung,exynos-usb-switch"
},
{ },
};
MODULE_DEVICE_TABLE(of, of_exynos_usbswitch_match);
static struct platform_driver exynos_usbswitch_driver = {
.probe = exynos_usbswitch_probe,
.remove = exynos_usbswitch_remove,
.driver = {
.name = "exynos-usb-switch",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(of_exynos_usbswitch_match),
.pm = &exynos_usbswitch_pm_ops,
},
};
module_platform_driver(exynos_usbswitch_driver);
MODULE_DESCRIPTION("Exynos USB switch driver");
MODULE_AUTHOR("<yulgon.kim@samsung.com>");
MODULE_LICENSE("GPL");
|