blob: 3fc752b9622c658c7bcc417e4d3b967d2eb41ccb [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
2 * (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
3 *
4 * This program is free software and is provided to you under the terms of the
5 * GNU General Public License version 2 as published by the Free Software
6 * Foundation, and any use by you of this program is subject to the terms
7 * of such GNU licence.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * SPDX-License-Identifier: GPL-2.0-only
19 */
20
21#include <linux/module.h>
22#include <linux/io.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/platform_device.h>
26
27#include "ethosu_device.h"
28
29/****************************************************************************
30 * Defines
31 ****************************************************************************/
32
33#define ETHOSU_DRIVER_VERSION "1.0"
34#define ETHOSU_DRIVER_NAME "ethosu"
35
36/****************************************************************************
37 * Variables
38 ****************************************************************************/
39
40struct class *ethosu_class;
41
42/****************************************************************************
43 * Arm Ethos-U
44 ****************************************************************************/
45
46static int ethosu_pdev_probe(struct platform_device *pdev)
47{
48 struct ethosu_device *edev;
49 struct resource *in_queue_res;
50 struct resource *out_queue_res;
51 int ret;
52
53 dev_info(&pdev->dev, "Probe\n");
54
55 /* Get path to TCM memory */
56 in_queue_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
57 "in_queue");
58 if (IS_ERR(in_queue_res)) {
59 dev_err(&pdev->dev, "Failed to get in_queue resource.\n");
60
61 return PTR_ERR(in_queue_res);
62 }
63
64 out_queue_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
65 "out_queue");
66 if (IS_ERR(out_queue_res)) {
67 dev_err(&pdev->dev, "Failed to get out_queue resource.\n");
68
69 return PTR_ERR(out_queue_res);
70 }
71
72 /* Allocate memory for Arm Ethos-U device */
73 edev = devm_kzalloc(&pdev->dev, sizeof(*edev), GFP_KERNEL);
74 if (!edev)
75 return -ENOMEM;
76
77 platform_set_drvdata(pdev, edev);
78
79 /* Initialize device */
80 ret = ethosu_dev_init(edev, &pdev->dev, ethosu_class, in_queue_res,
81 out_queue_res);
82 if (ret)
83 goto free_dev;
84
85 return 0;
86
87free_dev:
88 devm_kfree(&pdev->dev, edev);
89
90 return ret;
91}
92
93static int ethosu_pdev_remove(struct platform_device *pdev)
94{
95 struct ethosu_device *edev = platform_get_drvdata(pdev);
96
97 dev_info(&pdev->dev, "Remove\n");
98
99 ethosu_dev_deinit(edev);
100
101 return 0;
102}
103
104static const struct of_device_id ethosu_pdev_match[] = {
105 { .compatible = "arm,ethosu" },
106 { /* Sentinel */ },
107};
108
109MODULE_DEVICE_TABLE(of, ethosu_pdev_match);
110
111static struct platform_driver ethosu_pdev_driver = {
112 .probe = &ethosu_pdev_probe,
113 .remove = &ethosu_pdev_remove,
114 .driver = {
115 .name = ETHOSU_DRIVER_NAME,
116 .owner = THIS_MODULE,
117 .of_match_table = of_match_ptr(ethosu_pdev_match),
118 },
119};
120
121/****************************************************************************
122 * Module init and exit
123 ****************************************************************************/
124
125static int __init ethosu_init(void)
126{
127 int ret;
128
129 ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
130 if (IS_ERR(ethosu_class)) {
131 printk("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);
132
133 return PTR_ERR(ethosu_class);
134 }
135
136 ret = platform_driver_register(&ethosu_pdev_driver);
137 if (ret) {
138 printk("Failed to register Arm Ethos-U platform driver.\n");
139 goto destroy_class;
140 }
141
142 return 0;
143
144destroy_class:
145 class_destroy(ethosu_class);
146
147 return ret;
148}
149
150static void __exit ethosu_exit(void)
151{
152 platform_driver_unregister(&ethosu_pdev_driver);
153 class_destroy(ethosu_class);
154}
155
156module_init(ethosu_init)
157module_exit(ethosu_exit)
158MODULE_LICENSE("GPL v2");
159MODULE_AUTHOR("Arm Ltd");
160MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
161MODULE_VERSION(ETHOSU_DRIVER_VERSION);