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 	ret = pm_runtime_get_sync(dev);
122 	if (ret)
123 		goto err_runtime_disable;
124 
125 	pm_runtime_put_sync(dev);
126 
127 	ret = v4l2_device_register(dev, &vpu->v4l2_dev);
128 	if (ret)
129 		goto err_vpu_deinit;
130 
131 	vpu_init_media_device(vpu);
132 	vpu->encoder.type = VPU_CORE_TYPE_ENC;
133 	vpu->encoder.function = MEDIA_ENT_F_PROC_VIDEO_ENCODER;
134 	vpu->decoder.type = VPU_CORE_TYPE_DEC;
135 	vpu->decoder.function = MEDIA_ENT_F_PROC_VIDEO_DECODER;
136 	vpu_add_func(vpu, &vpu->decoder);
137 	vpu_add_func(vpu, &vpu->encoder);
138 	ret = media_device_register(&vpu->mdev);
139 	if (ret)
140 		goto err_vpu_media;
141 	vpu->debugfs = debugfs_create_dir("amphion_vpu", NULL);
142 
143 	of_platform_populate(dev->of_node, NULL, NULL, dev);
144 
145 	return 0;
146 
147 err_vpu_media:
148 	vpu_remove_func(&vpu->encoder);
149 	vpu_remove_func(&vpu->decoder);
150 	v4l2_device_unregister(&vpu->v4l2_dev);
151 err_vpu_deinit:
152 err_runtime_disable:
153 	pm_runtime_set_suspended(dev);
154 	pm_runtime_disable(dev);
155 
156 	return ret;
157 }
158 
159 static int vpu_remove(struct platform_device *pdev)
160 {
161 	struct vpu_dev *vpu = platform_get_drvdata(pdev);
162 	struct device *dev = &pdev->dev;
163 
164 	debugfs_remove_recursive(vpu->debugfs);
165 	vpu->debugfs = NULL;
166 
167 	pm_runtime_disable(dev);
168 
169 	media_device_unregister(&vpu->mdev);
170 	vpu_remove_func(&vpu->decoder);
171 	vpu_remove_func(&vpu->encoder);
172 	media_device_cleanup(&vpu->mdev);
173 	v4l2_device_unregister(&vpu->v4l2_dev);
174 	mutex_destroy(&vpu->lock);
175 
176 	return 0;
177 }
178 
179 static int __maybe_unused vpu_runtime_resume(struct device *dev)
180 {
181 	return 0;
182 }
183 
184 static int __maybe_unused vpu_runtime_suspend(struct device *dev)
185 {
186 	return 0;
187 }
188 
189 static int __maybe_unused vpu_resume(struct device *dev)
190 {
191 	return 0;
192 }
193 
194 static int __maybe_unused vpu_suspend(struct device *dev)
195 {
196 	return 0;
197 }
198 
199 static const struct dev_pm_ops vpu_pm_ops = {
200 	SET_RUNTIME_PM_OPS(vpu_runtime_suspend, vpu_runtime_resume, NULL)
201 	SET_SYSTEM_SLEEP_PM_OPS(vpu_suspend, vpu_resume)
202 };
203 
204 static struct vpu_resources imx8qxp_res = {
205 	.plat_type = IMX8QXP,
206 	.mreg_base = 0x40000000,
207 	.setup = vpu_imx8q_setup,
208 	.setup_encoder = vpu_imx8q_setup_enc,
209 	.setup_decoder = vpu_imx8q_setup_dec,
210 	.reset = vpu_imx8q_reset
211 };
212 
213 static struct vpu_resources imx8qm_res = {
214 	.plat_type = IMX8QM,
215 	.mreg_base = 0x40000000,
216 	.setup = vpu_imx8q_setup,
217 	.setup_encoder = vpu_imx8q_setup_enc,
218 	.setup_decoder = vpu_imx8q_setup_dec,
219 	.reset = vpu_imx8q_reset
220 };
221 
222 static const struct of_device_id vpu_dt_match[] = {
223 	{ .compatible = "nxp,imx8qxp-vpu", .data = &imx8qxp_res },
224 	{ .compatible = "nxp,imx8qm-vpu", .data = &imx8qm_res },
225 	{}
226 };
227 MODULE_DEVICE_TABLE(of, vpu_dt_match);
228 
229 static struct platform_driver amphion_vpu_driver = {
230 	.probe = vpu_probe,
231 	.remove = vpu_remove,
232 	.driver = {
233 		.name = "amphion-vpu",
234 		.of_match_table = vpu_dt_match,
235 		.pm = &vpu_pm_ops,
236 	},
237 };
238 
239 static int __init vpu_driver_init(void)
240 {
241 	int ret;
242 
243 	ret = platform_driver_register(&amphion_vpu_driver);
244 	if (ret)
245 		return ret;
246 
247 	return vpu_core_driver_init();
248 }
249 
250 static void __exit vpu_driver_exit(void)
251 {
252 	vpu_core_driver_exit();
253 	platform_driver_unregister(&amphion_vpu_driver);
254 }
255 module_init(vpu_driver_init);
256 module_exit(vpu_driver_exit);
257 
258 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
259 MODULE_DESCRIPTION("Linux VPU driver for Freescale i.MX8Q");
260 MODULE_LICENSE("GPL v2");
261