1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
3
4 #include <linux/kfifo.h>
5 #include <media/v4l2-common.h>
6 #include <media/v4l2-ioctl.h>
7 #include <media/videobuf2-core.h>
8 #include <media/videobuf2-vmalloc.h> /* for ISP statistics */
9 #include "dev.h"
10 #include "isp_stats.h"
11 #include "isp_stats_v1x.h"
12 #include "isp_stats_v2x.h"
13 #include "isp_stats_v21.h"
14 #include "isp_stats_v3x.h"
15
16 #define STATS_NAME DRIVER_NAME "-statistics"
17 #define RKISP_ISP_STATS_REQ_BUFS_MIN 2
18 #define RKISP_ISP_STATS_REQ_BUFS_MAX 8
19
rkisp_stats_enum_fmt_meta_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)20 static int rkisp_stats_enum_fmt_meta_cap(struct file *file, void *priv,
21 struct v4l2_fmtdesc *f)
22 {
23 struct video_device *video = video_devdata(file);
24 struct rkisp_isp_stats_vdev *stats_vdev = video_get_drvdata(video);
25
26 if (f->index > 0 || f->type != video->queue->type)
27 return -EINVAL;
28
29 f->pixelformat = stats_vdev->vdev_fmt.fmt.meta.dataformat;
30 return 0;
31 }
32
rkisp_stats_g_fmt_meta_cap(struct file * file,void * priv,struct v4l2_format * f)33 static int rkisp_stats_g_fmt_meta_cap(struct file *file, void *priv,
34 struct v4l2_format *f)
35 {
36 struct video_device *video = video_devdata(file);
37 struct rkisp_isp_stats_vdev *stats_vdev = video_get_drvdata(video);
38 struct v4l2_meta_format *meta = &f->fmt.meta;
39
40 if (f->type != video->queue->type)
41 return -EINVAL;
42
43 memset(meta, 0, sizeof(*meta));
44 meta->dataformat = stats_vdev->vdev_fmt.fmt.meta.dataformat;
45 meta->buffersize = stats_vdev->vdev_fmt.fmt.meta.buffersize;
46
47 return 0;
48 }
49
rkisp_stats_querycap(struct file * file,void * priv,struct v4l2_capability * cap)50 static int rkisp_stats_querycap(struct file *file,
51 void *priv, struct v4l2_capability *cap)
52 {
53 struct video_device *vdev = video_devdata(file);
54 struct rkisp_isp_stats_vdev *stats_vdev = video_get_drvdata(vdev);
55
56 strcpy(cap->driver, DRIVER_NAME);
57 snprintf(cap->driver, sizeof(cap->driver),
58 "%s_v%d", DRIVER_NAME,
59 stats_vdev->dev->isp_ver >> 4);
60 strlcpy(cap->card, vdev->name, sizeof(cap->card));
61 strlcpy(cap->bus_info, "platform: " DRIVER_NAME, sizeof(cap->bus_info));
62
63 return 0;
64 }
65
66 /* ISP video device IOCTLs */
67 static const struct v4l2_ioctl_ops rkisp_stats_ioctl = {
68 .vidioc_reqbufs = vb2_ioctl_reqbufs,
69 .vidioc_querybuf = vb2_ioctl_querybuf,
70 .vidioc_create_bufs = vb2_ioctl_create_bufs,
71 .vidioc_qbuf = vb2_ioctl_qbuf,
72 .vidioc_dqbuf = vb2_ioctl_dqbuf,
73 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
74 .vidioc_expbuf = vb2_ioctl_expbuf,
75 .vidioc_streamon = vb2_ioctl_streamon,
76 .vidioc_streamoff = vb2_ioctl_streamoff,
77 .vidioc_enum_fmt_meta_cap = rkisp_stats_enum_fmt_meta_cap,
78 .vidioc_g_fmt_meta_cap = rkisp_stats_g_fmt_meta_cap,
79 .vidioc_s_fmt_meta_cap = rkisp_stats_g_fmt_meta_cap,
80 .vidioc_try_fmt_meta_cap = rkisp_stats_g_fmt_meta_cap,
81 .vidioc_querycap = rkisp_stats_querycap
82 };
83
rkisp_stats_fh_open(struct file * filp)84 static int rkisp_stats_fh_open(struct file *filp)
85 {
86 struct rkisp_isp_stats_vdev *stats = video_drvdata(filp);
87 int ret;
88
89 ret = v4l2_fh_open(filp);
90 if (!ret) {
91 ret = v4l2_pipeline_pm_get(&stats->vnode.vdev.entity);
92 if (ret < 0)
93 vb2_fop_release(filp);
94 }
95
96 return ret;
97 }
98
rkisp_stats_fop_release(struct file * file)99 static int rkisp_stats_fop_release(struct file *file)
100 {
101 struct rkisp_isp_stats_vdev *stats = video_drvdata(file);
102 int ret;
103
104 ret = vb2_fop_release(file);
105 if (!ret)
106 v4l2_pipeline_pm_put(&stats->vnode.vdev.entity);
107 return ret;
108 }
109
110 struct v4l2_file_operations rkisp_stats_fops = {
111 .mmap = vb2_fop_mmap,
112 .unlocked_ioctl = video_ioctl2,
113 .poll = vb2_fop_poll,
114 .open = rkisp_stats_fh_open,
115 .release = rkisp_stats_fop_release
116 };
117
rkisp_stats_vb2_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_ctxs[])118 static int rkisp_stats_vb2_queue_setup(struct vb2_queue *vq,
119 unsigned int *num_buffers,
120 unsigned int *num_planes,
121 unsigned int sizes[],
122 struct device *alloc_ctxs[])
123 {
124 struct rkisp_isp_stats_vdev *stats_vdev = vq->drv_priv;
125
126 *num_planes = 1;
127
128 *num_buffers = clamp_t(u32, *num_buffers, RKISP_ISP_STATS_REQ_BUFS_MIN,
129 RKISP_ISP_STATS_REQ_BUFS_MAX);
130
131 sizes[0] = stats_vdev->vdev_fmt.fmt.meta.buffersize;
132 INIT_LIST_HEAD(&stats_vdev->stat);
133
134 return 0;
135 }
136
rkisp_stats_vb2_buf_queue(struct vb2_buffer * vb)137 static void rkisp_stats_vb2_buf_queue(struct vb2_buffer *vb)
138 {
139 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
140 struct rkisp_buffer *stats_buf = to_rkisp_buffer(vbuf);
141 struct vb2_queue *vq = vb->vb2_queue;
142 struct rkisp_isp_stats_vdev *stats_dev = vq->drv_priv;
143 unsigned long flags;
144
145 stats_buf->vaddr[0] = vb2_plane_vaddr(vb, 0);
146
147 spin_lock_irqsave(&stats_dev->rd_lock, flags);
148 list_add_tail(&stats_buf->queue, &stats_dev->stat);
149 spin_unlock_irqrestore(&stats_dev->rd_lock, flags);
150 }
151
rkisp_stats_vb2_stop_streaming(struct vb2_queue * vq)152 static void rkisp_stats_vb2_stop_streaming(struct vb2_queue *vq)
153 {
154 struct rkisp_isp_stats_vdev *stats_vdev = vq->drv_priv;
155 struct rkisp_buffer *buf;
156 unsigned long flags;
157 int i;
158
159 /* Make sure no new work queued in isr before draining wq */
160 spin_lock_irqsave(&stats_vdev->irq_lock, flags);
161 stats_vdev->streamon = false;
162 spin_unlock_irqrestore(&stats_vdev->irq_lock, flags);
163
164 tasklet_disable(&stats_vdev->rd_tasklet);
165
166 spin_lock_irqsave(&stats_vdev->rd_lock, flags);
167 for (i = 0; i < RKISP_ISP_STATS_REQ_BUFS_MAX; i++) {
168 if (list_empty(&stats_vdev->stat))
169 break;
170 buf = list_first_entry(&stats_vdev->stat,
171 struct rkisp_buffer, queue);
172 list_del(&buf->queue);
173 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
174 }
175 if (stats_vdev->cur_buf)
176 vb2_buffer_done(&stats_vdev->cur_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
177 spin_unlock_irqrestore(&stats_vdev->rd_lock, flags);
178 }
179
180 static int
rkisp_stats_vb2_start_streaming(struct vb2_queue * queue,unsigned int count)181 rkisp_stats_vb2_start_streaming(struct vb2_queue *queue,
182 unsigned int count)
183 {
184 struct rkisp_isp_stats_vdev *stats_vdev = queue->drv_priv;
185
186 stats_vdev->cur_buf = NULL;
187 stats_vdev->ops->rdbk_enable(stats_vdev, false);
188 stats_vdev->streamon = true;
189 kfifo_reset(&stats_vdev->rd_kfifo);
190 tasklet_enable(&stats_vdev->rd_tasklet);
191
192 return 0;
193 }
194
195 static struct vb2_ops rkisp_stats_vb2_ops = {
196 .queue_setup = rkisp_stats_vb2_queue_setup,
197 .buf_queue = rkisp_stats_vb2_buf_queue,
198 .wait_prepare = vb2_ops_wait_prepare,
199 .wait_finish = vb2_ops_wait_finish,
200 .stop_streaming = rkisp_stats_vb2_stop_streaming,
201 .start_streaming = rkisp_stats_vb2_start_streaming,
202 };
203
rkisp_stats_init_vb2_queue(struct vb2_queue * q,struct rkisp_isp_stats_vdev * stats_vdev)204 static int rkisp_stats_init_vb2_queue(struct vb2_queue *q,
205 struct rkisp_isp_stats_vdev *stats_vdev)
206 {
207 q->type = V4L2_BUF_TYPE_META_CAPTURE;
208 q->io_modes = VB2_MMAP | VB2_USERPTR;
209 q->drv_priv = stats_vdev;
210 q->ops = &rkisp_stats_vb2_ops;
211 q->mem_ops = &vb2_vmalloc_memops;
212 q->buf_struct_size = sizeof(struct rkisp_buffer);
213 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
214 q->lock = &stats_vdev->dev->iqlock;
215 q->dev = stats_vdev->dev->dev;
216
217 return vb2_queue_init(q);
218 }
219
rkisp_stats_readout_task(unsigned long data)220 static void rkisp_stats_readout_task(unsigned long data)
221 {
222 unsigned int out = 0;
223 struct rkisp_isp_readout_work work;
224 struct rkisp_isp_stats_vdev *vdev =
225 (struct rkisp_isp_stats_vdev *)data;
226
227 while (!kfifo_is_empty(&vdev->rd_kfifo)) {
228 out = kfifo_out(&vdev->rd_kfifo,
229 &work, sizeof(work));
230 if (!out)
231 break;
232
233 if (work.readout == RKISP_ISP_READOUT_MEAS)
234 vdev->ops->send_meas(vdev, &work);
235 }
236 }
237
rkisp_init_stats_vdev(struct rkisp_isp_stats_vdev * stats_vdev)238 static void rkisp_init_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev)
239 {
240 stats_vdev->rd_buf_idx = 0;
241 stats_vdev->wr_buf_idx = 0;
242 memset(stats_vdev->stats_buf, 0, sizeof(stats_vdev->stats_buf));
243
244 if (stats_vdev->dev->isp_ver <= ISP_V13)
245 rkisp_init_stats_vdev_v1x(stats_vdev);
246 else if (stats_vdev->dev->isp_ver == ISP_V21)
247 rkisp_init_stats_vdev_v21(stats_vdev);
248 else if (stats_vdev->dev->isp_ver == ISP_V20)
249 rkisp_init_stats_vdev_v2x(stats_vdev);
250 else
251 rkisp_init_stats_vdev_v3x(stats_vdev);
252 }
253
rkisp_uninit_stats_vdev(struct rkisp_isp_stats_vdev * stats_vdev)254 static void rkisp_uninit_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev)
255 {
256 if (stats_vdev->dev->isp_ver <= ISP_V13)
257 rkisp_uninit_stats_vdev_v1x(stats_vdev);
258 else if (stats_vdev->dev->isp_ver == ISP_V21)
259 rkisp_uninit_stats_vdev_v21(stats_vdev);
260 else if (stats_vdev->dev->isp_ver == ISP_V20)
261 rkisp_uninit_stats_vdev_v2x(stats_vdev);
262 else
263 rkisp_uninit_stats_vdev_v3x(stats_vdev);
264 }
265
rkisp_stats_rdbk_enable(struct rkisp_isp_stats_vdev * stats_vdev,bool en)266 void rkisp_stats_rdbk_enable(struct rkisp_isp_stats_vdev *stats_vdev, bool en)
267 {
268 stats_vdev->ops->rdbk_enable(stats_vdev, en);
269 }
270
rkisp_stats_first_ddr_config(struct rkisp_isp_stats_vdev * stats_vdev)271 void rkisp_stats_first_ddr_config(struct rkisp_isp_stats_vdev *stats_vdev)
272 {
273 if (stats_vdev->dev->isp_ver == ISP_V20)
274 rkisp_stats_first_ddr_config_v2x(stats_vdev);
275 else if (stats_vdev->dev->isp_ver == ISP_V21)
276 rkisp_stats_first_ddr_config_v21(stats_vdev);
277 else if (stats_vdev->dev->isp_ver == ISP_V30)
278 rkisp_stats_first_ddr_config_v3x(stats_vdev);
279 }
280
rkisp_stats_isr(struct rkisp_isp_stats_vdev * stats_vdev,u32 isp_ris,u32 isp3a_ris)281 void rkisp_stats_isr(struct rkisp_isp_stats_vdev *stats_vdev,
282 u32 isp_ris, u32 isp3a_ris)
283 {
284 stats_vdev->ops->isr_hdl(stats_vdev, isp_ris, isp3a_ris);
285 }
286
rkisp_register_stats_vdev(struct rkisp_isp_stats_vdev * stats_vdev,struct v4l2_device * v4l2_dev,struct rkisp_device * dev)287 int rkisp_register_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev,
288 struct v4l2_device *v4l2_dev,
289 struct rkisp_device *dev)
290 {
291 int ret;
292 struct rkisp_vdev_node *node = &stats_vdev->vnode;
293 struct video_device *vdev = &node->vdev;
294 struct media_entity *source, *sink;
295
296 stats_vdev->dev = dev;
297 INIT_LIST_HEAD(&stats_vdev->stat);
298 spin_lock_init(&stats_vdev->irq_lock);
299 spin_lock_init(&stats_vdev->rd_lock);
300
301 strlcpy(vdev->name, STATS_NAME, sizeof(vdev->name));
302
303 vdev->ioctl_ops = &rkisp_stats_ioctl;
304 vdev->fops = &rkisp_stats_fops;
305 vdev->release = video_device_release_empty;
306 vdev->lock = &dev->iqlock;
307 vdev->v4l2_dev = v4l2_dev;
308 vdev->queue = &node->buf_queue;
309 vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
310 vdev->vfl_dir = VFL_DIR_RX;
311 rkisp_stats_init_vb2_queue(vdev->queue, stats_vdev);
312 rkisp_init_stats_vdev(stats_vdev);
313 video_set_drvdata(vdev, stats_vdev);
314
315 node->pad.flags = MEDIA_PAD_FL_SINK;
316 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
317 if (ret < 0)
318 goto err_release_queue;
319
320 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
321 if (ret < 0) {
322 dev_err(&vdev->dev,
323 "could not register Video for Linux device\n");
324 goto err_cleanup_media_entity;
325 }
326
327 source = &dev->isp_sdev.sd.entity;
328 sink = &stats_vdev->vnode.vdev.entity;
329 ret = media_create_pad_link(source, RKISP_ISP_PAD_SOURCE_STATS,
330 sink, 0, MEDIA_LNK_FL_ENABLED);
331 if (ret < 0)
332 goto err_unregister_video;
333
334 ret = kfifo_alloc(&stats_vdev->rd_kfifo,
335 RKISP_READOUT_WORK_SIZE,
336 GFP_KERNEL);
337 if (ret) {
338 dev_err(&vdev->dev,
339 "kfifo_alloc failed with error %d\n",
340 ret);
341 goto err_unregister_video;
342 }
343
344 tasklet_init(&stats_vdev->rd_tasklet,
345 rkisp_stats_readout_task,
346 (unsigned long)stats_vdev);
347 tasklet_disable(&stats_vdev->rd_tasklet);
348
349 return 0;
350
351 err_unregister_video:
352 video_unregister_device(vdev);
353 err_cleanup_media_entity:
354 media_entity_cleanup(&vdev->entity);
355 err_release_queue:
356 vb2_queue_release(vdev->queue);
357 rkisp_uninit_stats_vdev(stats_vdev);
358 return ret;
359 }
360
rkisp_unregister_stats_vdev(struct rkisp_isp_stats_vdev * stats_vdev)361 void rkisp_unregister_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev)
362 {
363 struct rkisp_vdev_node *node = &stats_vdev->vnode;
364 struct video_device *vdev = &node->vdev;
365
366 kfifo_free(&stats_vdev->rd_kfifo);
367 tasklet_kill(&stats_vdev->rd_tasklet);
368 video_unregister_device(vdev);
369 media_entity_cleanup(&vdev->entity);
370 vb2_queue_release(vdev->queue);
371 rkisp_uninit_stats_vdev(stats_vdev);
372 }
373