hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
 
#include <linux/kfifo.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-dma-sg.h>
#include <media/videobuf2-vmalloc.h>    /* for ISP statistics */
#include "dev.h"
#include "isp_stats.h"
#include "isp_stats_v1x.h"
#include "isp_stats_v2x.h"
#include "isp_stats_v21.h"
#include "isp_stats_v3x.h"
#include "isp_stats_v32.h"
 
#define STATS_NAME DRIVER_NAME "-statistics"
#define RKISP_ISP_STATS_REQ_BUFS_MIN 2
#define RKISP_ISP_STATS_REQ_BUFS_MAX 8
 
static int rkisp_stats_enum_fmt_meta_cap(struct file *file, void *priv,
                     struct v4l2_fmtdesc *f)
{
   struct video_device *video = video_devdata(file);
   struct rkisp_isp_stats_vdev *stats_vdev = video_get_drvdata(video);
 
   if (f->index > 0 || f->type != video->queue->type)
       return -EINVAL;
 
   f->pixelformat = stats_vdev->vdev_fmt.fmt.meta.dataformat;
   return 0;
}
 
static int rkisp_stats_g_fmt_meta_cap(struct file *file, void *priv,
                      struct v4l2_format *f)
{
   struct video_device *video = video_devdata(file);
   struct rkisp_isp_stats_vdev *stats_vdev = video_get_drvdata(video);
   struct v4l2_meta_format *meta = &f->fmt.meta;
 
   if (f->type != video->queue->type)
       return -EINVAL;
 
   memset(meta, 0, sizeof(*meta));
   meta->dataformat = stats_vdev->vdev_fmt.fmt.meta.dataformat;
   meta->buffersize = stats_vdev->vdev_fmt.fmt.meta.buffersize;
 
   return 0;
}
 
static int rkisp_stats_querycap(struct file *file,
                void *priv, struct v4l2_capability *cap)
{
   struct video_device *vdev = video_devdata(file);
   struct rkisp_isp_stats_vdev *stats_vdev = video_get_drvdata(vdev);
 
   strcpy(cap->driver, DRIVER_NAME);
   snprintf(cap->driver, sizeof(cap->driver),
        "%s_v%d", DRIVER_NAME,
        stats_vdev->dev->isp_ver >> 4);
   strlcpy(cap->card, vdev->name, sizeof(cap->card));
   strlcpy(cap->bus_info, "platform: " DRIVER_NAME, sizeof(cap->bus_info));
   cap->version = RKISP_DRIVER_VERSION;
   return 0;
}
 
/* ISP video device IOCTLs */
static const struct v4l2_ioctl_ops rkisp_stats_ioctl = {
   .vidioc_reqbufs = vb2_ioctl_reqbufs,
   .vidioc_querybuf = vb2_ioctl_querybuf,
   .vidioc_create_bufs = vb2_ioctl_create_bufs,
   .vidioc_qbuf = vb2_ioctl_qbuf,
   .vidioc_dqbuf = vb2_ioctl_dqbuf,
   .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
   .vidioc_expbuf = vb2_ioctl_expbuf,
   .vidioc_streamon = vb2_ioctl_streamon,
   .vidioc_streamoff = vb2_ioctl_streamoff,
   .vidioc_enum_fmt_meta_cap = rkisp_stats_enum_fmt_meta_cap,
   .vidioc_g_fmt_meta_cap = rkisp_stats_g_fmt_meta_cap,
   .vidioc_s_fmt_meta_cap = rkisp_stats_g_fmt_meta_cap,
   .vidioc_try_fmt_meta_cap = rkisp_stats_g_fmt_meta_cap,
   .vidioc_querycap = rkisp_stats_querycap
};
 
static int rkisp_stats_fh_open(struct file *filp)
{
   struct rkisp_isp_stats_vdev *stats = video_drvdata(filp);
   int ret;
 
   if (!stats->dev->is_probe_end)
       return -EINVAL;
 
   ret = v4l2_fh_open(filp);
   if (!ret) {
       ret = v4l2_pipeline_pm_get(&stats->vnode.vdev.entity);
       if (ret < 0)
           vb2_fop_release(filp);
   }
 
   return ret;
}
 
static int rkisp_stats_fop_release(struct file *file)
{
   struct rkisp_isp_stats_vdev *stats = video_drvdata(file);
   int ret;
 
   ret = vb2_fop_release(file);
   if (!ret)
       v4l2_pipeline_pm_put(&stats->vnode.vdev.entity);
   return ret;
}
 
struct v4l2_file_operations rkisp_stats_fops = {
   .mmap = vb2_fop_mmap,
   .unlocked_ioctl = video_ioctl2,
   .poll = vb2_fop_poll,
   .open = rkisp_stats_fh_open,
   .release = rkisp_stats_fop_release
};
 
static int rkisp_stats_vb2_queue_setup(struct vb2_queue *vq,
                   unsigned int *num_buffers,
                   unsigned int *num_planes,
                   unsigned int sizes[],
                   struct device *alloc_ctxs[])
{
   struct rkisp_isp_stats_vdev *stats_vdev = vq->drv_priv;
 
   *num_planes = 1;
 
   *num_buffers = clamp_t(u32, *num_buffers, RKISP_ISP_STATS_REQ_BUFS_MIN,
                  RKISP_ISP_STATS_REQ_BUFS_MAX);
 
   sizes[0] = stats_vdev->vdev_fmt.fmt.meta.buffersize;
   INIT_LIST_HEAD(&stats_vdev->stat);
 
   return 0;
}
 
static void rkisp_stats_vb2_buf_queue(struct vb2_buffer *vb)
{
   struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
   struct rkisp_buffer *stats_buf = to_rkisp_buffer(vbuf);
   struct vb2_queue *vq = vb->vb2_queue;
   struct rkisp_isp_stats_vdev *stats_dev = vq->drv_priv;
   u32 size = stats_dev->vdev_fmt.fmt.meta.buffersize;
   unsigned long flags;
 
   stats_buf->vaddr[0] = vb2_plane_vaddr(vb, 0);
   if (stats_dev->dev->isp_ver == ISP_V32) {
       struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
 
       stats_buf->buff_addr[0] = sg_dma_address(sgt->sgl);
   }
   if (stats_buf->vaddr[0])
       memset(stats_buf->vaddr[0], 0, size);
   spin_lock_irqsave(&stats_dev->rd_lock, flags);
   if (stats_dev->dev->isp_ver == ISP_V32 && stats_dev->dev->is_pre_on) {
       struct rkisp32_isp_stat_buffer *buf = stats_dev->stats_buf[0].vaddr;
 
       if (buf && !buf->frame_id && buf->meas_type && stats_buf->vaddr[0]) {
           dev_info(stats_dev->dev->dev,
                "tb stat seq:%d meas_type:0x%x\n",
                buf->frame_id, buf->meas_type);
           memcpy(stats_buf->vaddr[0], buf, sizeof(struct rkisp32_isp_stat_buffer));
           buf->meas_type = 0;
           vb2_set_plane_payload(vb, 0, sizeof(struct rkisp32_isp_stat_buffer));
           vbuf->sequence = buf->frame_id;
           spin_unlock_irqrestore(&stats_dev->rd_lock, flags);
           vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
           return;
       }
   }
   list_add_tail(&stats_buf->queue, &stats_dev->stat);
   spin_unlock_irqrestore(&stats_dev->rd_lock, flags);
}
 
static void rkisp_stats_vb2_stop_streaming(struct vb2_queue *vq)
{
   struct rkisp_isp_stats_vdev *stats_vdev = vq->drv_priv;
   struct rkisp_buffer *buf;
   unsigned long flags;
   int i;
 
   /* Make sure no new work queued in isr before draining wq */
   spin_lock_irqsave(&stats_vdev->irq_lock, flags);
   stats_vdev->streamon = false;
   spin_unlock_irqrestore(&stats_vdev->irq_lock, flags);
 
   tasklet_disable(&stats_vdev->rd_tasklet);
 
   spin_lock_irqsave(&stats_vdev->rd_lock, flags);
   for (i = 0; i < RKISP_ISP_STATS_REQ_BUFS_MAX; i++) {
       if (list_empty(&stats_vdev->stat))
           break;
       buf = list_first_entry(&stats_vdev->stat,
                      struct rkisp_buffer, queue);
       list_del(&buf->queue);
       vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
   }
   if (stats_vdev->cur_buf) {
       vb2_buffer_done(&stats_vdev->cur_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
       if (stats_vdev->cur_buf == stats_vdev->nxt_buf)
           stats_vdev->nxt_buf = NULL;
       stats_vdev->cur_buf = NULL;
   }
   if (stats_vdev->nxt_buf) {
       vb2_buffer_done(&stats_vdev->nxt_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
       stats_vdev->nxt_buf = NULL;
   }
   spin_unlock_irqrestore(&stats_vdev->rd_lock, flags);
 
   stats_vdev->ae_meas_done_next = false;
   stats_vdev->af_meas_done_next = false;
}
 
static int
rkisp_stats_vb2_start_streaming(struct vb2_queue *queue,
                unsigned int count)
{
   struct rkisp_isp_stats_vdev *stats_vdev = queue->drv_priv;
 
   stats_vdev->cur_buf = NULL;
   stats_vdev->ops->rdbk_enable(stats_vdev, false);
   stats_vdev->streamon = true;
   kfifo_reset(&stats_vdev->rd_kfifo);
   tasklet_enable(&stats_vdev->rd_tasklet);
 
   return 0;
}
 
static struct vb2_ops rkisp_stats_vb2_ops = {
   .queue_setup = rkisp_stats_vb2_queue_setup,
   .buf_queue = rkisp_stats_vb2_buf_queue,
   .wait_prepare = vb2_ops_wait_prepare,
   .wait_finish = vb2_ops_wait_finish,
   .stop_streaming = rkisp_stats_vb2_stop_streaming,
   .start_streaming = rkisp_stats_vb2_start_streaming,
};
 
static int rkisp_stats_init_vb2_queue(struct vb2_queue *q,
                      struct rkisp_isp_stats_vdev *stats_vdev)
{
   q->type = V4L2_BUF_TYPE_META_CAPTURE;
   q->io_modes = VB2_MMAP | VB2_USERPTR;
   q->drv_priv = stats_vdev;
   q->ops = &rkisp_stats_vb2_ops;
   if (stats_vdev->dev->isp_ver == ISP_V32) {
       q->mem_ops = stats_vdev->dev->hw_dev->mem_ops;
       if (stats_vdev->dev->hw_dev->is_dma_contig)
           q->dma_attrs = DMA_ATTR_FORCE_CONTIGUOUS;
   } else {
       q->mem_ops = &vb2_vmalloc_memops;
   }
   q->buf_struct_size = sizeof(struct rkisp_buffer);
   q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
   q->lock = &stats_vdev->dev->iqlock;
   q->dev = stats_vdev->dev->dev;
   return vb2_queue_init(q);
}
 
static void rkisp_stats_readout_task(unsigned long data)
{
   unsigned int out = 0;
   struct rkisp_isp_readout_work work;
   struct rkisp_isp_stats_vdev *vdev =
       (struct rkisp_isp_stats_vdev *)data;
 
   while (!kfifo_is_empty(&vdev->rd_kfifo)) {
       out = kfifo_out(&vdev->rd_kfifo,
               &work, sizeof(work));
       if (!out)
           break;
 
       if (work.readout == RKISP_ISP_READOUT_MEAS)
           vdev->ops->send_meas(vdev, &work);
   }
}
 
static void rkisp_init_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev)
{
   stats_vdev->rd_buf_idx = 0;
   stats_vdev->wr_buf_idx = 0;
   memset(stats_vdev->stats_buf, 0, sizeof(stats_vdev->stats_buf));
 
   if (stats_vdev->dev->isp_ver <= ISP_V13)
       rkisp_init_stats_vdev_v1x(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V21)
       rkisp_init_stats_vdev_v21(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V20)
       rkisp_init_stats_vdev_v2x(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V30)
       rkisp_init_stats_vdev_v3x(stats_vdev);
   else
       rkisp_init_stats_vdev_v32(stats_vdev);
}
 
static void rkisp_uninit_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev)
{
   if (stats_vdev->dev->isp_ver <= ISP_V13)
       rkisp_uninit_stats_vdev_v1x(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V21)
       rkisp_uninit_stats_vdev_v21(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V20)
       rkisp_uninit_stats_vdev_v2x(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V30)
       rkisp_uninit_stats_vdev_v3x(stats_vdev);
   else
       rkisp_uninit_stats_vdev_v32(stats_vdev);
}
 
void rkisp_stats_rdbk_enable(struct rkisp_isp_stats_vdev *stats_vdev, bool en)
{
   stats_vdev->ops->rdbk_enable(stats_vdev, en);
}
 
void rkisp_stats_first_ddr_config(struct rkisp_isp_stats_vdev *stats_vdev)
{
   if (stats_vdev->dev->isp_ver == ISP_V20)
       rkisp_stats_first_ddr_config_v2x(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V21)
       rkisp_stats_first_ddr_config_v21(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V30)
       rkisp_stats_first_ddr_config_v3x(stats_vdev);
   else if (stats_vdev->dev->isp_ver == ISP_V32)
       rkisp_stats_first_ddr_config_v32(stats_vdev);
}
 
void rkisp_stats_next_ddr_config(struct rkisp_isp_stats_vdev *stats_vdev)
{
   if (stats_vdev->dev->isp_ver == ISP_V32)
       rkisp_stats_next_ddr_config_v32(stats_vdev);
}
 
void rkisp_stats_isr(struct rkisp_isp_stats_vdev *stats_vdev,
             u32 isp_ris, u32 isp3a_ris)
{
   stats_vdev->ops->isr_hdl(stats_vdev, isp_ris, isp3a_ris);
}
 
int rkisp_register_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev,
                 struct v4l2_device *v4l2_dev,
                 struct rkisp_device *dev)
{
   int ret;
   struct rkisp_vdev_node *node = &stats_vdev->vnode;
   struct video_device *vdev = &node->vdev;
   struct media_entity *source, *sink;
 
   stats_vdev->dev = dev;
   INIT_LIST_HEAD(&stats_vdev->stat);
   spin_lock_init(&stats_vdev->irq_lock);
   spin_lock_init(&stats_vdev->rd_lock);
 
   strlcpy(vdev->name, STATS_NAME, sizeof(vdev->name));
 
   vdev->ioctl_ops = &rkisp_stats_ioctl;
   vdev->fops = &rkisp_stats_fops;
   vdev->release = video_device_release_empty;
   vdev->lock = &dev->iqlock;
   vdev->v4l2_dev = v4l2_dev;
   vdev->queue = &node->buf_queue;
   vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
   vdev->vfl_dir =  VFL_DIR_RX;
   rkisp_stats_init_vb2_queue(vdev->queue, stats_vdev);
   rkisp_init_stats_vdev(stats_vdev);
   video_set_drvdata(vdev, stats_vdev);
 
   node->pad.flags = MEDIA_PAD_FL_SINK;
   ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
   if (ret < 0)
       goto err_release_queue;
 
   ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
   if (ret < 0) {
       dev_err(&vdev->dev,
           "could not register Video for Linux device\n");
       goto err_cleanup_media_entity;
   }
 
   source = &dev->isp_sdev.sd.entity;
   sink = &stats_vdev->vnode.vdev.entity;
   ret = media_create_pad_link(source, RKISP_ISP_PAD_SOURCE_STATS,
       sink, 0, MEDIA_LNK_FL_ENABLED);
   if (ret < 0)
       goto err_unregister_video;
 
   ret = kfifo_alloc(&stats_vdev->rd_kfifo,
             RKISP_READOUT_WORK_SIZE,
             GFP_KERNEL);
   if (ret) {
       dev_err(&vdev->dev,
           "kfifo_alloc failed with error %d\n",
           ret);
       goto err_unregister_video;
   }
 
   tasklet_init(&stats_vdev->rd_tasklet,
            rkisp_stats_readout_task,
            (unsigned long)stats_vdev);
   tasklet_disable(&stats_vdev->rd_tasklet);
 
   return 0;
 
err_unregister_video:
   video_unregister_device(vdev);
err_cleanup_media_entity:
   media_entity_cleanup(&vdev->entity);
err_release_queue:
   vb2_queue_release(vdev->queue);
   rkisp_uninit_stats_vdev(stats_vdev);
   return ret;
}
 
void rkisp_unregister_stats_vdev(struct rkisp_isp_stats_vdev *stats_vdev)
{
   struct rkisp_vdev_node *node = &stats_vdev->vnode;
   struct video_device *vdev = &node->vdev;
 
   kfifo_free(&stats_vdev->rd_kfifo);
   tasklet_kill(&stats_vdev->rd_tasklet);
   video_unregister_device(vdev);
   media_entity_cleanup(&vdev->entity);
   vb2_queue_release(vdev->queue);
   rkisp_uninit_stats_vdev(stats_vdev);
}