GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / media / platform / amphion / vpu_drv.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/dma-map-ops.h>
13 #include <linux/of_device.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/videodev2.h>
20 #include <linux/of_reserved_mem.h>
21 #include <media/v4l2-device.h>
22 #include <media/videobuf2-v4l2.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/v4l2-ioctl.h>
25 #include <linux/debugfs.h>
26 #include "vpu.h"
27 #include "vpu_imx8q.h"
28
29 bool debug;
30 module_param(debug, bool, 0644);
31
32 void vpu_writel(struct vpu_dev *vpu, u32 reg, u32 val)
33 {
34         writel(val, vpu->base + reg);
35 }
36
37 u32 vpu_readl(struct vpu_dev *vpu, u32 reg)
38 {
39         return readl(vpu->base + reg);
40 }
41
42 static void vpu_dev_get(struct vpu_dev *vpu)
43 {
44         if (atomic_inc_return(&vpu->ref_vpu) == 1 && vpu->res->setup)
45                 vpu->res->setup(vpu);
46 }
47
48 static void vpu_dev_put(struct vpu_dev *vpu)
49 {
50         atomic_dec(&vpu->ref_vpu);
51 }
52
53 static void vpu_enc_get(struct vpu_dev *vpu)
54 {
55         if (atomic_inc_return(&vpu->ref_enc) == 1 && vpu->res->setup_encoder)
56                 vpu->res->setup_encoder(vpu);
57 }
58
59 static void vpu_enc_put(struct vpu_dev *vpu)
60 {
61         atomic_dec(&vpu->ref_enc);
62 }
63
64 static void vpu_dec_get(struct vpu_dev *vpu)
65 {
66         if (atomic_inc_return(&vpu->ref_dec) == 1 && vpu->res->setup_decoder)
67                 vpu->res->setup_decoder(vpu);
68 }
69
70 static void vpu_dec_put(struct vpu_dev *vpu)
71 {
72         atomic_dec(&vpu->ref_dec);
73 }
74
75 static int vpu_init_media_device(struct vpu_dev *vpu)
76 {
77         vpu->mdev.dev = vpu->dev;
78         strscpy(vpu->mdev.model, "amphion-vpu", sizeof(vpu->mdev.model));
79         strscpy(vpu->mdev.bus_info, "platform: amphion-vpu", sizeof(vpu->mdev.bus_info));
80         media_device_init(&vpu->mdev);
81         vpu->v4l2_dev.mdev = &vpu->mdev;
82
83         return 0;
84 }
85
86 static int vpu_probe(struct platform_device *pdev)
87 {
88         struct device *dev = &pdev->dev;
89         struct vpu_dev *vpu;
90         int ret;
91
92         dev_dbg(dev, "probe\n");
93         vpu = devm_kzalloc(dev, sizeof(*vpu), GFP_KERNEL);
94         if (!vpu)
95                 return -ENOMEM;
96
97         vpu->pdev = pdev;
98         vpu->dev = dev;
99         mutex_init(&vpu->lock);
100         INIT_LIST_HEAD(&vpu->cores);
101         platform_set_drvdata(pdev, vpu);
102         atomic_set(&vpu->ref_vpu, 0);
103         atomic_set(&vpu->ref_enc, 0);
104         atomic_set(&vpu->ref_dec, 0);
105         vpu->get_vpu = vpu_dev_get;
106         vpu->put_vpu = vpu_dev_put;
107         vpu->get_enc = vpu_enc_get;
108         vpu->put_enc = vpu_enc_put;
109         vpu->get_dec = vpu_dec_get;
110         vpu->put_dec = vpu_dec_put;
111
112         vpu->base = devm_platform_ioremap_resource(pdev, 0);
113         if (IS_ERR(vpu->base))
114                 return PTR_ERR(vpu->base);
115
116         vpu->res = of_device_get_match_data(dev);
117         if (!vpu->res)
118                 return -ENODEV;
119
120         pm_runtime_enable(dev);
121
122         ret = v4l2_device_register(dev, &vpu->v4l2_dev);
123         if (ret)
124                 goto err_vpu_deinit;
125
126         vpu_init_media_device(vpu);
127         vpu->encoder.type = VPU_CORE_TYPE_ENC;
128         vpu->encoder.function = MEDIA_ENT_F_PROC_VIDEO_ENCODER;
129         vpu->decoder.type = VPU_CORE_TYPE_DEC;
130         vpu->decoder.function = MEDIA_ENT_F_PROC_VIDEO_DECODER;
131         ret = vpu_add_func(vpu, &vpu->decoder);
132         if (ret)
133                 goto err_add_decoder;
134         ret = vpu_add_func(vpu, &vpu->encoder);
135         if (ret)
136                 goto err_add_encoder;
137         ret = media_device_register(&vpu->mdev);
138         if (ret)
139                 goto err_vpu_media;
140         vpu->debugfs = debugfs_create_dir("amphion_vpu", NULL);
141
142         of_platform_populate(dev->of_node, NULL, NULL, dev);
143
144         return 0;
145
146 err_vpu_media:
147         vpu_remove_func(&vpu->encoder);
148 err_add_encoder:
149         vpu_remove_func(&vpu->decoder);
150 err_add_decoder:
151         media_device_cleanup(&vpu->mdev);
152         v4l2_device_unregister(&vpu->v4l2_dev);
153 err_vpu_deinit:
154         pm_runtime_set_suspended(dev);
155         pm_runtime_disable(dev);
156
157         return ret;
158 }
159
160 static int vpu_remove(struct platform_device *pdev)
161 {
162         struct vpu_dev *vpu = platform_get_drvdata(pdev);
163         struct device *dev = &pdev->dev;
164
165         debugfs_remove_recursive(vpu->debugfs);
166         vpu->debugfs = NULL;
167
168         pm_runtime_disable(dev);
169
170         media_device_unregister(&vpu->mdev);
171         vpu_remove_func(&vpu->decoder);
172         vpu_remove_func(&vpu->encoder);
173         media_device_cleanup(&vpu->mdev);
174         v4l2_device_unregister(&vpu->v4l2_dev);
175         mutex_destroy(&vpu->lock);
176
177         return 0;
178 }
179
180 static int __maybe_unused vpu_runtime_resume(struct device *dev)
181 {
182         return 0;
183 }
184
185 static int __maybe_unused vpu_runtime_suspend(struct device *dev)
186 {
187         return 0;
188 }
189
190 static int __maybe_unused vpu_resume(struct device *dev)
191 {
192         return 0;
193 }
194
195 static int __maybe_unused vpu_suspend(struct device *dev)
196 {
197         return 0;
198 }
199
200 static const struct dev_pm_ops vpu_pm_ops = {
201         SET_RUNTIME_PM_OPS(vpu_runtime_suspend, vpu_runtime_resume, NULL)
202         SET_SYSTEM_SLEEP_PM_OPS(vpu_suspend, vpu_resume)
203 };
204
205 static struct vpu_resources imx8qxp_res = {
206         .plat_type = IMX8QXP,
207         .mreg_base = 0x40000000,
208         .setup = vpu_imx8q_setup,
209         .setup_encoder = vpu_imx8q_setup_enc,
210         .setup_decoder = vpu_imx8q_setup_dec,
211         .reset = vpu_imx8q_reset
212 };
213
214 static struct vpu_resources imx8qm_res = {
215         .plat_type = IMX8QM,
216         .mreg_base = 0x40000000,
217         .setup = vpu_imx8q_setup,
218         .setup_encoder = vpu_imx8q_setup_enc,
219         .setup_decoder = vpu_imx8q_setup_dec,
220         .reset = vpu_imx8q_reset
221 };
222
223 static const struct of_device_id vpu_dt_match[] = {
224         { .compatible = "nxp,imx8qxp-vpu", .data = &imx8qxp_res },
225         { .compatible = "nxp,imx8qm-vpu", .data = &imx8qm_res },
226         {}
227 };
228 MODULE_DEVICE_TABLE(of, vpu_dt_match);
229
230 static struct platform_driver amphion_vpu_driver = {
231         .probe = vpu_probe,
232         .remove = vpu_remove,
233         .driver = {
234                 .name = "amphion-vpu",
235                 .of_match_table = vpu_dt_match,
236                 .pm = &vpu_pm_ops,
237         },
238 };
239
240 static int __init vpu_driver_init(void)
241 {
242         int ret;
243
244         ret = platform_driver_register(&amphion_vpu_driver);
245         if (ret)
246                 return ret;
247
248         return vpu_core_driver_init();
249 }
250
251 static void __exit vpu_driver_exit(void)
252 {
253         vpu_core_driver_exit();
254         platform_driver_unregister(&amphion_vpu_driver);
255 }
256 module_init(vpu_driver_init);
257 module_exit(vpu_driver_exit);
258
259 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
260 MODULE_DESCRIPTION("Linux VPU driver for Freescale i.MX8Q");
261 MODULE_LICENSE("GPL v2");