aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/misc/exynos-usb-switch.c
blob: 3acb66e75eb91d9f383b11b4f03441475b133f33 (plain)
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
 * 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/pm_runtime.h>

#include <plat/devs.h>
#include <plat/ehci.h>
#include <plat/usbgadget.h>
#include <plat/usb-switch.h>

#include <mach/regs-clock.h>

#include "../gadget/s3c_udc.h"
#include "../gadget/exynos_ss_udc.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;

#if defined(CONFIG_BATTERY_SAMSUNG)
void exynos_usb_cable_connect(void)
{
	samsung_cable_check_status(1);
}

void exynos_usb_cable_disconnect(void)
{
	samsung_cable_check_status(0);
}
#endif

static int is_host_detect(struct exynos_usb_switch *usb_switch)
{
	if (!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 (!usb_switch->gpio_device_detect)
		return 0;
	return gpio_get_value(usb_switch->gpio_device_detect);
}

static int is_drd_host_detect(struct exynos_usb_switch *usb_switch)
{
	if (!usb_switch->gpio_drd_host_detect)
		return 0;
	return !gpio_get_value(usb_switch->gpio_drd_host_detect);
}

static int is_drd_device_detect(struct exynos_usb_switch *usb_switch)
{
	if (!usb_switch->gpio_drd_device_detect)
		return 0;
	return gpio_get_value(usb_switch->gpio_drd_device_detect);
}

static void set_host_vbus(struct exynos_usb_switch *usb_switch, int value)
{
	gpio_set_value(usb_switch->gpio_host_vbus, value);
}

static int exynos_change_usb_mode(struct exynos_usb_switch *usb_switch,
				enum usb_cable_status mode)
{
	struct s3c_udc *udc;
	struct exynos_ss_udc *ss_udc;
	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;
		}
	} else if (test_bit(USB_DRD_DEVICE_ATTACHED, &cur_mode) ||
		   test_bit(USB_DRD_HOST_ATTACHED, &cur_mode)) {
		if (mode == USB_DRD_DEVICE_ATTACHED ||
			mode == USB_DRD_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;
	} else if (!test_bit(USB_DRD_DEVICE_ATTACHED, &cur_mode) &&
			mode == USB_DRD_DEVICE_DETACHED) {
		printk(KERN_DEBUG "Skip request %d, current %lu\n",
			mode, cur_mode);
		return -EPERM;
	} else if (!test_bit(USB_DRD_HOST_ATTACHED, &cur_mode) &&
			mode == USB_DRD_HOST_DETACHED) {
		printk(KERN_DEBUG "Skip request %d, current %lu\n",
			mode, cur_mode);
		return -EPERM;
	}

	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;
		}
		udc = dev_get_drvdata(usb_switch->s3c_udc_dev);
		if (udc && udc->gadget.ops && udc->gadget.ops->vbus_session)
			udc->gadget.ops->vbus_session(&udc->gadget, 0);
		clear_bit(USB_DEVICE_ATTACHED, &usb_switch->connect);
		break;
	case USB_DEVICE_ATTACHED:
		udc = dev_get_drvdata(usb_switch->s3c_udc_dev);
		if (udc && udc->gadget.ops && udc->gadget.ops->vbus_session)
			udc->gadget.ops->vbus_session(&udc->gadget, 1);
		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_put(usb_switch->ohci_dev);
		if (usb_switch->ehci_dev)
			pm_runtime_put(usb_switch->ehci_dev);
		if (usb_switch->gpio_host_vbus)
			set_host_vbus(usb_switch, 0);

#if defined(CONFIG_BATTERY_SAMSUNG)
		exynos_usb_cable_disconnect();
#endif
		clear_bit(USB_HOST_ATTACHED, &usb_switch->connect);
		break;
	case USB_HOST_ATTACHED:
#if defined(CONFIG_BATTERY_SAMSUNG)
		exynos_usb_cable_connect();
#endif
		if (usb_switch->gpio_host_vbus)
			set_host_vbus(usb_switch, 1);

		if (usb_switch->ehci_dev)
			pm_runtime_get_sync(usb_switch->ehci_dev);
		if (usb_switch->ohci_dev)
			pm_runtime_get_sync(usb_switch->ohci_dev);
		set_bit(USB_HOST_ATTACHED, &usb_switch->connect);
		break;
	case USB_DRD_DEVICE_DETACHED:
		if (test_bit(USB_DRD_HOST_ATTACHED, &cur_mode)) {
			printk(KERN_ERR "Abnormal request %d, current %lu\n",
				mode, cur_mode);
			return -EPERM;
		}

		ss_udc = dev_get_drvdata(usb_switch->exynos_udc_dev);
		if (ss_udc && ss_udc->gadget.ops &&
			ss_udc->gadget.ops->vbus_session)
			ss_udc->gadget.ops->vbus_session(&ss_udc->gadget, 0);
		clear_bit(USB_DRD_DEVICE_ATTACHED, &usb_switch->connect);
		break;
	case USB_DRD_DEVICE_ATTACHED:
		ss_udc = dev_get_drvdata(usb_switch->exynos_udc_dev);
		if (ss_udc && ss_udc->gadget.ops &&
			ss_udc->gadget.ops->vbus_session)
			ss_udc->gadget.ops->vbus_session(&ss_udc->gadget, 1);
		set_bit(USB_DRD_DEVICE_ATTACHED, &usb_switch->connect);
		break;
	case USB_DRD_HOST_DETACHED:
		if (test_bit(USB_DRD_DEVICE_ATTACHED, &cur_mode)) {
			printk(KERN_ERR "Abnormal request %d, current %lu\n",
				mode, cur_mode);
			return -EPERM;
		}

		if (usb_switch->xhci_dev)
			pm_runtime_put_sync(usb_switch->xhci_dev);
#if defined(CONFIG_BATTERY_SAMSUNG)
		exynos_usb_cable_disconnect();
#endif
		clear_bit(USB_DRD_HOST_ATTACHED, &usb_switch->connect);
		break;
	case USB_DRD_HOST_ATTACHED:
#if defined(CONFIG_BATTERY_SAMSUNG)
		exynos_usb_cable_connect();
#endif
		if (usb_switch->xhci_dev)
			pm_runtime_get_sync(usb_switch->xhci_dev);
		set_bit(USB_DRD_HOST_ATTACHED, &usb_switch->connect);
		break;
	default:
		printk(KERN_ERR "Does not changed\n");
	}
	printk(KERN_INFO "usb cable = %d\n", mode);

	return ret;
}

static void exnos_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 void exnos_usb_drd_switch_worker(struct work_struct *work)
{
	struct exynos_usb_switch *usb_switch =
		container_of(work, struct exynos_usb_switch, switch_drd_work);
	int cnt = 0;

	mutex_lock(&usb_switch->mutex);
	/* If already device detached or host_detected, */
	if (!is_drd_device_detect(usb_switch) || is_drd_host_detect(usb_switch))
		goto done;
	if (!usb_switch->xhci_dev)
		goto detect;

	while (!pm_runtime_suspended(usb_switch->xhci_dev) ||
		usb_switch->xhci_dev->power.is_suspended) {
		mutex_unlock(&usb_switch->mutex);
		msleep(SWITCH_WAIT_TIME);
		mutex_lock(&usb_switch->mutex);

		/* If already device detached or host_detected, */
		if (!is_drd_device_detect(usb_switch) ||
			is_drd_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_DRD_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;

	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 irqreturn_t exynos_drd_host_detect_thread(int irq, void *data)
{
	struct exynos_usb_switch *usb_switch = data;

	mutex_lock(&usb_switch->mutex);

	if (is_drd_host_detect(usb_switch))
		exynos_change_usb_mode(usb_switch, USB_DRD_HOST_ATTACHED);
	else
		exynos_change_usb_mode(usb_switch, USB_DRD_HOST_DETACHED);

	mutex_unlock(&usb_switch->mutex);

	return IRQ_HANDLED;
}

static irqreturn_t exynos_drd_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_drd_host_detect(usb_switch))
		printk(KERN_DEBUG "Not expected situation\n");
	else if (is_drd_device_detect(usb_switch))
		queue_work(usb_switch->workqueue, &usb_switch->switch_drd_work);
	else {
		/* VBUS PIN low */
		exynos_change_usb_mode(usb_switch, USB_DRD_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 (!test_bit(USB_DRD_DEVICE_ATTACHED, &usb_switch->connect) ||
		!test_bit(USB_DRD_HOST_ATTACHED, &usb_switch->connect)) {
		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);
		}
	}

	/* 3.0 USB */
	if (!test_bit(USB_DRD_DEVICE_ATTACHED, &usb_switch->connect) ||
		!test_bit(USB_DRD_HOST_ATTACHED, &usb_switch->connect)) {
		if (is_drd_host_detect(usb_switch))
			exynos_change_usb_mode(usb_switch,
				USB_DRD_HOST_ATTACHED);
		else if (is_drd_device_detect(usb_switch))
			queue_work(usb_switch->workqueue,
				&usb_switch->switch_drd_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);

	if (test_bit(USB_DRD_DEVICE_ATTACHED, &usb_switch->connect))
		exynos_change_usb_mode(usb_switch, USB_DRD_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

static int __devinit 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 irq;
	int ret = 0;

	usb_switch = kzalloc(sizeof(struct exynos_usb_switch), GFP_KERNEL);
	if (!usb_switch) {
		ret = -ENOMEM;
		return ret;
	}

	our_switch = usb_switch;
	mutex_init(&usb_switch->mutex);
	usb_switch->workqueue = create_singlethread_workqueue("usb_switch");
	INIT_WORK(&usb_switch->switch_work, exnos_usb_switch_worker);
	INIT_WORK(&usb_switch->switch_drd_work, exnos_usb_drd_switch_worker);

	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->gpio_drd_host_detect = pdata->gpio_drd_host_detect;
	usb_switch->gpio_drd_device_detect = pdata->gpio_drd_device_detect;

	usb_switch->ehci_dev = pdata->ehci_dev;
	usb_switch->ohci_dev = pdata->ohci_dev;
	usb_switch->xhci_dev = pdata->xhci_dev;
	usb_switch->s3c_udc_dev = pdata->s3c_udc_dev;
	usb_switch->exynos_udc_dev = pdata->exynos_udc_dev;

	/* USB Device detect IRQ */
	irq = platform_get_irq(pdev, 1);
	if (irq > 0 && usb_switch->s3c_udc_dev) {
		ret = request_threaded_irq(irq, NULL,
				exynos_device_detect_thread,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"DEVICE_DETECT", usb_switch);
		if (ret) {
			dev_err(dev, "Failed to request device irq %d\n", irq);
			goto fail;
		}
		usb_switch->device_detect_irq = irq;
	} 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 */
	irq = platform_get_irq(pdev, 0);
	if (irq > 0 && (usb_switch->ehci_dev || usb_switch->ohci_dev)) {
		ret = request_threaded_irq(irq, NULL,
				exynos_host_detect_thread,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"HOST_DETECT", usb_switch);
		if (ret) {
			dev_err(dev, "Failed to request host irq %d\n", irq);
			goto fail_gpio_device_detect;
		}
		usb_switch->host_detect_irq = irq;
	} 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");


	/* USB DRD Device detect IRQ */
	irq = platform_get_irq(pdev, 3);
	if (irq > 0 && usb_switch->exynos_udc_dev) {
		ret = request_threaded_irq(irq, NULL,
				exynos_drd_device_detect_thread,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"DRD_DEVICE_DETECT", usb_switch);
		if (ret) {
			dev_err(dev, "Failed to request drd device irq %d\n",
				irq);
			goto fail_gpio_host_detect;
		}
		usb_switch->device_drd_detect_irq = irq;
	} else if (usb_switch->exynos_udc_dev)
		exynos_change_usb_mode(usb_switch, USB_DRD_DEVICE_ATTACHED);
	else
		dev_info(dev, "Disable drd device detect IRQ\n");

	/* USB DRD Host detect IRQ */
	irq = platform_get_irq(pdev, 2);
	if (irq > 0 && usb_switch->xhci_dev) {
		ret = request_threaded_irq(irq, NULL,
				exynos_drd_host_detect_thread,
				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"DRD_HOST_DETECT", usb_switch);
		if (ret) {
			dev_err(dev, "Failed to request drd host irq %d\n",
				irq);
			goto fail_gpio_drd_device_detect;
		}
		usb_switch->host_drd_detect_irq = irq;
	} else if (usb_switch->xhci_dev)
		exynos_change_usb_mode(usb_switch, USB_DRD_HOST_ATTACHED);
	else
		dev_info(dev, "Disable drd host detect IRQ\n");

	exynos_usb_status_init(usb_switch);

	platform_set_drvdata(pdev, usb_switch);

	return ret;
fail_gpio_drd_device_detect:
	free_irq(usb_switch->device_drd_detect_irq, usb_switch);
fail_gpio_host_detect:
	free_irq(usb_switch->host_detect_irq, usb_switch);
fail_gpio_device_detect:
	free_irq(usb_switch->device_detect_irq, usb_switch);
fail:
	cancel_work_sync(&usb_switch->switch_work);
	destroy_workqueue(usb_switch->workqueue);
	mutex_destroy(&usb_switch->mutex);
	kfree(usb_switch);
	return ret;
}

static int __devexit exynos_usbswitch_remove(struct platform_device *pdev)
{
	struct exynos_usb_switch *usb_switch = platform_get_drvdata(pdev);

	free_irq(usb_switch->host_drd_detect_irq, usb_switch);
	free_irq(usb_switch->device_drd_detect_irq, usb_switch);
	free_irq(usb_switch->host_detect_irq, usb_switch);
	free_irq(usb_switch->device_detect_irq, usb_switch);
	platform_set_drvdata(pdev, 0);

	cancel_work_sync(&usb_switch->switch_work);
	destroy_workqueue(usb_switch->workqueue);
	mutex_destroy(&usb_switch->mutex);
	kfree(usb_switch);

	return 0;
}

static const struct dev_pm_ops exynos_usbswitch_pm_ops = {
	.suspend                = exynos_usbswitch_suspend,
	.resume                 = exynos_usbswitch_resume,
};

static struct platform_driver exynos_usbswitch_driver = {
	.probe		= exynos_usbswitch_probe,
	.remove		= __devexit_p(exynos_usbswitch_remove),
	.driver		= {
		.name	= "exynos-usb-switch",
		.owner	= THIS_MODULE,
		.pm	= &exynos_usbswitch_pm_ops,
	},
};

static int __init exynos_usbswitch_init(void)
{
	int ret;

	ret = platform_device_register(&s5p_device_usbswitch);
	if (ret < 0)
		return ret;

	ret = platform_driver_register(&exynos_usbswitch_driver);
	if (!ret)
		printk(KERN_INFO "%s: " DRIVER_DESC "\n", switch_name);

	return ret;
}
late_initcall(exynos_usbswitch_init);

static void __exit exynos_usbswitch_exit(void)
{
	platform_driver_unregister(&exynos_usbswitch_driver);
}
module_exit(exynos_usbswitch_exit);

MODULE_DESCRIPTION("Exynos USB switch driver");
MODULE_AUTHOR("<yulgon.kim@samsung.com>");
MODULE_LICENSE("GPL");