aboutsummaryrefslogtreecommitdiff
path: root/kernel/ethosu_driver.c
blob: 3fc752b9622c658c7bcc417e4d3b967d2eb41ccb (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
/*
 * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

#include <linux/module.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>

#include "ethosu_device.h"

/****************************************************************************
 * Defines
 ****************************************************************************/

#define ETHOSU_DRIVER_VERSION "1.0"
#define ETHOSU_DRIVER_NAME    "ethosu"

/****************************************************************************
 * Variables
 ****************************************************************************/

struct class *ethosu_class;

/****************************************************************************
 * Arm Ethos-U
 ****************************************************************************/

static int ethosu_pdev_probe(struct platform_device *pdev)
{
	struct ethosu_device *edev;
	struct resource *in_queue_res;
	struct resource *out_queue_res;
	int ret;

	dev_info(&pdev->dev, "Probe\n");

	/* Get path to TCM memory */
	in_queue_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
						    "in_queue");
	if (IS_ERR(in_queue_res)) {
		dev_err(&pdev->dev, "Failed to get in_queue resource.\n");

		return PTR_ERR(in_queue_res);
	}

	out_queue_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
						     "out_queue");
	if (IS_ERR(out_queue_res)) {
		dev_err(&pdev->dev, "Failed to get out_queue resource.\n");

		return PTR_ERR(out_queue_res);
	}

	/* Allocate memory for Arm Ethos-U device */
	edev = devm_kzalloc(&pdev->dev, sizeof(*edev), GFP_KERNEL);
	if (!edev)
		return -ENOMEM;

	platform_set_drvdata(pdev, edev);

	/* Initialize device */
	ret = ethosu_dev_init(edev, &pdev->dev, ethosu_class, in_queue_res,
			      out_queue_res);
	if (ret)
		goto free_dev;

	return 0;

free_dev:
	devm_kfree(&pdev->dev, edev);

	return ret;
}

static int ethosu_pdev_remove(struct platform_device *pdev)
{
	struct ethosu_device *edev = platform_get_drvdata(pdev);

	dev_info(&pdev->dev, "Remove\n");

	ethosu_dev_deinit(edev);

	return 0;
}

static const struct of_device_id ethosu_pdev_match[] = {
	{ .compatible = "arm,ethosu" },
	{ /* Sentinel */ },
};

MODULE_DEVICE_TABLE(of, ethosu_pdev_match);

static struct platform_driver ethosu_pdev_driver = {
	.probe                  = &ethosu_pdev_probe,
	.remove                 = &ethosu_pdev_remove,
	.driver                 = {
		.name           = ETHOSU_DRIVER_NAME,
		.owner          = THIS_MODULE,
		.of_match_table = of_match_ptr(ethosu_pdev_match),
	},
};

/****************************************************************************
 * Module init and exit
 ****************************************************************************/

static int __init ethosu_init(void)
{
	int ret;

	ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
	if (IS_ERR(ethosu_class)) {
		printk("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);

		return PTR_ERR(ethosu_class);
	}

	ret = platform_driver_register(&ethosu_pdev_driver);
	if (ret) {
		printk("Failed to register Arm Ethos-U platform driver.\n");
		goto destroy_class;
	}

	return 0;

destroy_class:
	class_destroy(ethosu_class);

	return ret;
}

static void __exit ethosu_exit(void)
{
	platform_driver_unregister(&ethosu_pdev_driver);
	class_destroy(ethosu_class);
}

module_init(ethosu_init)
module_exit(ethosu_exit)
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Arm Ltd");
MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
MODULE_VERSION(ETHOSU_DRIVER_VERSION);