Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / usb / dwc3 / drd.c
blob2765c51c7ef5fe0c0242ef0f09750fae2d727598
1 /**
2 * drd.c - DesignWare USB3 DRD Controller Dual-role support
4 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Roger Quadros <rogerq@ti.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 of
10 * the License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/extcon.h>
23 #include "debug.h"
24 #include "core.h"
25 #include "gadget.h"
27 static void dwc3_drd_update(struct dwc3 *dwc)
29 int id;
31 id = extcon_get_state(dwc->edev, EXTCON_USB_HOST);
32 if (id < 0)
33 id = 0;
35 dwc3_set_mode(dwc, id ?
36 DWC3_GCTL_PRTCAP_HOST :
37 DWC3_GCTL_PRTCAP_DEVICE);
40 static int dwc3_drd_notifier(struct notifier_block *nb,
41 unsigned long event, void *ptr)
43 struct dwc3 *dwc = container_of(nb, struct dwc3, edev_nb);
45 dwc3_set_mode(dwc, event ?
46 DWC3_GCTL_PRTCAP_HOST :
47 DWC3_GCTL_PRTCAP_DEVICE);
49 return NOTIFY_DONE;
52 int dwc3_drd_init(struct dwc3 *dwc)
54 int ret;
56 if (dwc->dev->of_node) {
57 if (of_property_read_bool(dwc->dev->of_node, "extcon"))
58 dwc->edev = extcon_get_edev_by_phandle(dwc->dev, 0);
60 if (IS_ERR(dwc->edev))
61 return PTR_ERR(dwc->edev);
63 dwc->edev_nb.notifier_call = dwc3_drd_notifier;
64 ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST,
65 &dwc->edev_nb);
66 if (ret < 0) {
67 dev_err(dwc->dev, "couldn't register cable notifier\n");
68 return ret;
72 dwc3_drd_update(dwc);
74 return 0;
77 void dwc3_drd_exit(struct dwc3 *dwc)
79 extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST,
80 &dwc->edev_nb);
82 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
83 flush_work(&dwc->drd_work);
84 dwc3_gadget_exit(dwc);