xref: /openbmc/linux/drivers/usb/cdns3/host.c (revision 55fd7e02)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence USBSS DRD Driver - host side
4  *
5  * Copyright (C) 2018-2019 Cadence Design Systems.
6  * Copyright (C) 2017-2018 NXP
7  *
8  * Authors: Peter Chen <peter.chen@nxp.com>
9  *          Pawel Laszczak <pawell@cadence.com>
10  */
11 
12 #include <linux/platform_device.h>
13 #include "core.h"
14 #include "drd.h"
15 #include "host-export.h"
16 
17 static int __cdns3_host_init(struct cdns3 *cdns)
18 {
19 	struct platform_device *xhci;
20 	int ret;
21 
22 	cdns3_drd_switch_host(cdns, 1);
23 
24 	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
25 	if (!xhci) {
26 		dev_err(cdns->dev, "couldn't allocate xHCI device\n");
27 		return -ENOMEM;
28 	}
29 
30 	xhci->dev.parent = cdns->dev;
31 	cdns->host_dev = xhci;
32 
33 	ret = platform_device_add_resources(xhci, cdns->xhci_res,
34 					    CDNS3_XHCI_RESOURCES_NUM);
35 	if (ret) {
36 		dev_err(cdns->dev, "couldn't add resources to xHCI device\n");
37 		goto err1;
38 	}
39 
40 	ret = platform_device_add(xhci);
41 	if (ret) {
42 		dev_err(cdns->dev, "failed to register xHCI device\n");
43 		goto err1;
44 	}
45 
46 	return 0;
47 err1:
48 	platform_device_put(xhci);
49 	return ret;
50 }
51 
52 static void cdns3_host_exit(struct cdns3 *cdns)
53 {
54 	platform_device_unregister(cdns->host_dev);
55 	cdns->host_dev = NULL;
56 	cdns3_drd_switch_host(cdns, 0);
57 }
58 
59 int cdns3_host_init(struct cdns3 *cdns)
60 {
61 	struct cdns3_role_driver *rdrv;
62 
63 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
64 	if (!rdrv)
65 		return -ENOMEM;
66 
67 	rdrv->start	= __cdns3_host_init;
68 	rdrv->stop	= cdns3_host_exit;
69 	rdrv->state	= CDNS3_ROLE_STATE_INACTIVE;
70 	rdrv->name	= "host";
71 
72 	cdns->roles[USB_ROLE_HOST] = rdrv;
73 
74 	return 0;
75 }
76