summaryrefslogtreecommitdiffstats
path: root/drivers/usb/dwc3/drd.c
blob: cc8ab9a8e9d2762081718b936aa7fa6a806c9cf5 (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
// SPDX-License-Identifier: GPL-2.0
/**
 * drd.c - DesignWare USB3 DRD Controller Dual-role support
 *
 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com
 *
 * Authors: Roger Quadros <rogerq@ti.com>
 */

#include <linux/extcon.h>

#include "debug.h"
#include "core.h"
#include "gadget.h"

static void dwc3_drd_update(struct dwc3 *dwc)
{
	int id;

	id = extcon_get_state(dwc->edev, EXTCON_USB_HOST);
	if (id < 0)
		id = 0;

	dwc3_set_mode(dwc, id ?
		      DWC3_GCTL_PRTCAP_HOST :
		      DWC3_GCTL_PRTCAP_DEVICE);
}

static int dwc3_drd_notifier(struct notifier_block *nb,
			     unsigned long event, void *ptr)
{
	struct dwc3 *dwc = container_of(nb, struct dwc3, edev_nb);

	dwc3_set_mode(dwc, event ?
		      DWC3_GCTL_PRTCAP_HOST :
		      DWC3_GCTL_PRTCAP_DEVICE);

	return NOTIFY_DONE;
}

int dwc3_drd_init(struct dwc3 *dwc)
{
	int ret;

	if (dwc->dev->of_node) {
		if (of_property_read_bool(dwc->dev->of_node, "extcon"))
			dwc->edev = extcon_get_edev_by_phandle(dwc->dev, 0);

		if (IS_ERR(dwc->edev))
			return PTR_ERR(dwc->edev);

		dwc->edev_nb.notifier_call = dwc3_drd_notifier;
		ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST,
					       &dwc->edev_nb);
		if (ret < 0) {
			dev_err(dwc->dev, "couldn't register cable notifier\n");
			return ret;
		}
	}

	dwc3_drd_update(dwc);

	return 0;
}

void dwc3_drd_exit(struct dwc3 *dwc)
{
	extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST,
				   &dwc->edev_nb);

	dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
	flush_work(&dwc->drd_work);
	dwc3_gadget_exit(dwc);
}
OpenPOWER on IntegriCloud