hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
 
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-core.h>
#include <media/videobuf2-vmalloc.h>    /* for ISP params */
#include <media/v4l2-event.h>
#include <linux/rk-preisp.h>
#include "dev.h"
#include "isp_params.h"
#include "isp_params_v1x.h"
#include "isp_params_v2x.h"
#include "isp_params_v21.h"
 
#define PARAMS_NAME DRIVER_NAME "-input-params"
#define RKISP_ISP_PARAMS_REQ_BUFS_MIN    2
#define RKISP_ISP_PARAMS_REQ_BUFS_MAX    8
 
static int rkisp_params_enum_fmt_meta_out(struct file *file, void *priv,
                     struct v4l2_fmtdesc *f)
{
   struct video_device *video = video_devdata(file);
   struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(video);
 
   if (f->index > 0 || f->type != video->queue->type)
       return -EINVAL;
 
   f->pixelformat = params_vdev->vdev_fmt.fmt.meta.dataformat;
 
   return 0;
}
 
static int rkisp_params_g_fmt_meta_out(struct file *file, void *fh,
                      struct v4l2_format *f)
{
   struct video_device *video = video_devdata(file);
   struct rkisp_isp_params_vdev *params_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 = params_vdev->vdev_fmt.fmt.meta.dataformat;
   meta->buffersize = params_vdev->vdev_fmt.fmt.meta.buffersize;
 
   return 0;
}
 
static int rkisp_params_querycap(struct file *file,
                void *priv, struct v4l2_capability *cap)
{
   struct video_device *vdev = video_devdata(file);
   struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(vdev);
 
   snprintf(cap->driver, sizeof(cap->driver),
        "%s_v%d", DRIVER_NAME,
        params_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;
}
 
static int rkisp_params_subs_evt(struct v4l2_fh *fh,
                const struct v4l2_event_subscription *sub)
{
   struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(fh->vdev);
 
   if (sub->id != 0)
       return -EINVAL;
 
   switch (sub->type) {
   case CIFISP_V4L2_EVENT_STREAM_START:
   case CIFISP_V4L2_EVENT_STREAM_STOP:
       params_vdev->is_subs_evt = true;
       return v4l2_event_subscribe(fh, sub, 0, NULL);
   default:
       return -EINVAL;
   }
}
 
static int rkisp_params_unsubs_evt(struct v4l2_fh *fh,
                  const struct v4l2_event_subscription *sub)
{
   struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(fh->vdev);
 
   params_vdev->is_subs_evt = false;
   return v4l2_event_unsubscribe(fh, sub);
}
 
/* ISP params video device IOCTLs */
static const struct v4l2_ioctl_ops rkisp_params_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_out = rkisp_params_enum_fmt_meta_out,
   .vidioc_g_fmt_meta_out = rkisp_params_g_fmt_meta_out,
   .vidioc_s_fmt_meta_out = rkisp_params_g_fmt_meta_out,
   .vidioc_try_fmt_meta_out = rkisp_params_g_fmt_meta_out,
   .vidioc_querycap = rkisp_params_querycap,
   .vidioc_subscribe_event = rkisp_params_subs_evt,
   .vidioc_unsubscribe_event = rkisp_params_unsubs_evt,
};
 
static int rkisp_params_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_params_vdev *params_vdev = vq->drv_priv;
 
   *num_buffers = clamp_t(u32, *num_buffers,
                  RKISP_ISP_PARAMS_REQ_BUFS_MIN,
                  RKISP_ISP_PARAMS_REQ_BUFS_MAX);
 
   *num_planes = 1;
   params_vdev->ops->get_param_size(params_vdev, sizes);
 
   INIT_LIST_HEAD(&params_vdev->params);
   params_vdev->first_params = true;
 
   return 0;
}
 
static void rkisp_params_vb2_buf_queue(struct vb2_buffer *vb)
{
   struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
   struct rkisp_buffer *params_buf = to_rkisp_buffer(vbuf);
   struct vb2_queue *vq = vb->vb2_queue;
   struct rkisp_isp_params_vdev *params_vdev = vq->drv_priv;
   void *first_param;
   unsigned long flags;
 
   unsigned int cur_frame_id = -1;
   cur_frame_id = atomic_read(&params_vdev->dev->isp_sdev.frm_sync_seq) - 1;
   if (params_vdev->first_params) {
       first_param = vb2_plane_vaddr(vb, 0);
       params_vdev->ops->save_first_param(params_vdev, first_param);
       vbuf->sequence = cur_frame_id;
       vb2_buffer_done(&params_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
       params_vdev->first_params = false;
       wake_up(&params_vdev->dev->sync_onoff);
       dev_info(params_vdev->dev->dev, "first params buf queue\n");
       return;
   }
 
   params_buf->vaddr[0] = vb2_plane_vaddr(vb, 0);
   spin_lock_irqsave(&params_vdev->config_lock, flags);
   list_add_tail(&params_buf->queue, &params_vdev->params);
   spin_unlock_irqrestore(&params_vdev->config_lock, flags);
}
 
static void rkisp_params_vb2_stop_streaming(struct vb2_queue *vq)
{
   struct rkisp_isp_params_vdev *params_vdev = vq->drv_priv;
   struct rkisp_device *dev = params_vdev->dev;
   struct rkisp_buffer *buf;
   unsigned long flags;
   int i;
 
   /* stop params input firstly */
   spin_lock_irqsave(&params_vdev->config_lock, flags);
   params_vdev->streamon = false;
   wake_up(&dev->sync_onoff);
   spin_unlock_irqrestore(&params_vdev->config_lock, flags);
 
   for (i = 0; i < RKISP_ISP_PARAMS_REQ_BUFS_MAX; i++) {
       spin_lock_irqsave(&params_vdev->config_lock, flags);
       if (!list_empty(&params_vdev->params)) {
           buf = list_first_entry(&params_vdev->params,
                          struct rkisp_buffer, queue);
           list_del(&buf->queue);
           spin_unlock_irqrestore(&params_vdev->config_lock,
                          flags);
       } else {
           spin_unlock_irqrestore(&params_vdev->config_lock,
                          flags);
           break;
       }
 
       if (buf)
           vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
       buf = NULL;
   }
 
   if (params_vdev->cur_buf) {
       buf = params_vdev->cur_buf;
       vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
       params_vdev->cur_buf = NULL;
   }
 
   rkisp_params_disable_isp(params_vdev);
   /* clean module params */
   params_vdev->ops->clear_first_param(params_vdev);
   params_vdev->rdbk_times = 0;
}
 
static int
rkisp_params_vb2_start_streaming(struct vb2_queue *queue, unsigned int count)
{
   struct rkisp_isp_params_vdev *params_vdev = queue->drv_priv;
   unsigned long flags;
 
   params_vdev->is_first_cfg = true;
   params_vdev->hdrtmo_en = false;
   params_vdev->cur_buf = NULL;
   spin_lock_irqsave(&params_vdev->config_lock, flags);
   params_vdev->streamon = true;
   spin_unlock_irqrestore(&params_vdev->config_lock, flags);
 
   return 0;
}
 
static struct vb2_ops rkisp_params_vb2_ops = {
   .queue_setup = rkisp_params_vb2_queue_setup,
   .wait_prepare = vb2_ops_wait_prepare,
   .wait_finish = vb2_ops_wait_finish,
   .buf_queue = rkisp_params_vb2_buf_queue,
   .start_streaming = rkisp_params_vb2_start_streaming,
   .stop_streaming = rkisp_params_vb2_stop_streaming,
 
};
 
static int rkisp_params_fh_open(struct file *filp)
{
   struct rkisp_isp_params_vdev *params = video_drvdata(filp);
   int ret;
 
   ret = v4l2_fh_open(filp);
   if (!ret) {
       ret = v4l2_pipeline_pm_use(&params->vnode.vdev.entity, 1);
       if (ret < 0)
           vb2_fop_release(filp);
   }
 
   return ret;
}
 
static int rkisp_params_fop_release(struct file *file)
{
   struct rkisp_isp_params_vdev *params = video_drvdata(file);
   struct video_device *vdev = video_devdata(file);
   int ret;
 
   if (file->private_data == vdev->queue->owner && params->ops->fop_release)
       params->ops->fop_release(params);
 
   ret = vb2_fop_release(file);
   if (!ret) {
       ret = v4l2_pipeline_pm_use(&params->vnode.vdev.entity, 0);
       if (ret < 0)
           v4l2_err(&params->dev->v4l2_dev,
                "set pipeline power failed %d\n", ret);
   }
   return ret;
}
 
struct v4l2_file_operations rkisp_params_fops = {
   .mmap = vb2_fop_mmap,
   .unlocked_ioctl = video_ioctl2,
   .poll = vb2_fop_poll,
   .open = rkisp_params_fh_open,
   .release = rkisp_params_fop_release
};
 
static int
rkisp_params_init_vb2_queue(struct vb2_queue *q,
               struct rkisp_isp_params_vdev *params_vdev)
{
   q->type = V4L2_BUF_TYPE_META_OUTPUT;
   q->io_modes = VB2_MMAP | VB2_USERPTR;
   q->drv_priv = params_vdev;
   q->ops = &rkisp_params_vb2_ops;
   q->mem_ops = &vb2_vmalloc_memops;
   q->buf_struct_size = sizeof(struct rkisp_buffer);
   q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
   q->lock = &params_vdev->dev->iqlock;
   q->dev = params_vdev->dev->dev;
 
   return vb2_queue_init(q);
}
 
static int rkisp_init_params_vdev(struct rkisp_isp_params_vdev *params_vdev)
{
   params_vdev->vdev_fmt.fmt.meta.dataformat =
       V4L2_META_FMT_RK_ISP1_PARAMS;
   params_vdev->vdev_fmt.fmt.meta.buffersize =
       sizeof(struct rkisp1_isp_params_cfg);
 
   if (params_vdev->dev->isp_ver <= ISP_V13)
       return rkisp_init_params_vdev_v1x(params_vdev);
   else if (params_vdev->dev->isp_ver == ISP_V21)
       return rkisp_init_params_vdev_v21(params_vdev);
   else
       return rkisp_init_params_vdev_v2x(params_vdev);
}
 
static void rkisp_uninit_params_vdev(struct rkisp_isp_params_vdev *params_vdev)
{
   if (params_vdev->dev->isp_ver <= ISP_V13)
       rkisp_uninit_params_vdev_v1x(params_vdev);
   else if (params_vdev->dev->isp_ver == ISP_V21)
       rkisp_uninit_params_vdev_v21(params_vdev);
   else
       rkisp_uninit_params_vdev_v2x(params_vdev);
}
 
void rkisp_params_cfg(struct rkisp_isp_params_vdev *params_vdev, u32 frame_id)
{
   if (params_vdev->ops->param_cfg)
       params_vdev->ops->param_cfg(params_vdev, frame_id, RKISP_PARAMS_IMD);
}
 
void rkisp_params_cfgsram(struct rkisp_isp_params_vdev *params_vdev)
{
   /* multi device to switch sram config */
   if (params_vdev->dev->hw_dev->is_single)
       return;
 
   if (params_vdev->ops->param_cfgsram)
       params_vdev->ops->param_cfgsram(params_vdev);
}
 
void rkisp_params_isr(struct rkisp_isp_params_vdev *params_vdev,
             u32 isp_mis)
{
   params_vdev->ops->isr_hdl(params_vdev, isp_mis);
}
 
/* Not called when the camera active, thus not isr protection. */
void rkisp_params_first_cfg(struct rkisp_isp_params_vdev *params_vdev,
               struct ispsd_in_fmt *in_fmt,
               enum v4l2_quantization quantization)
{
   if (!params_vdev->is_first_cfg)
       return;
   params_vdev->is_first_cfg = false;
   params_vdev->quantization = quantization;
   params_vdev->raw_type = in_fmt->bayer_pat;
   params_vdev->in_mbus_code = in_fmt->mbus_code;
   params_vdev->ops->first_cfg(params_vdev);
}
 
/* Not called when the camera active, thus not isr protection. */
void rkisp_params_disable_isp(struct rkisp_isp_params_vdev *params_vdev)
{
   if (params_vdev->ops->disable_isp)
       params_vdev->ops->disable_isp(params_vdev);
}
 
void rkisp_params_get_meshbuf_inf(struct rkisp_isp_params_vdev *params_vdev,
                 void *meshbuf)
{
   if (params_vdev->ops->get_meshbuf_inf)
       params_vdev->ops->get_meshbuf_inf(params_vdev, meshbuf);
}
 
void rkisp_params_set_meshbuf_size(struct rkisp_isp_params_vdev *params_vdev,
                  void *meshsize)
{
   if (params_vdev->ops->set_meshbuf_size)
       params_vdev->ops->set_meshbuf_size(params_vdev, meshsize);
}
 
void rkisp_params_stream_stop(struct rkisp_isp_params_vdev *params_vdev)
{
   if (params_vdev->ops->stream_stop)
       params_vdev->ops->stream_stop(params_vdev);
}
 
int rkisp_register_params_vdev(struct rkisp_isp_params_vdev *params_vdev,
               struct v4l2_device *v4l2_dev,
               struct rkisp_device *dev)
{
   int ret;
   struct rkisp_vdev_node *node = &params_vdev->vnode;
   struct video_device *vdev = &node->vdev;
   struct media_entity *source, *sink;
 
   params_vdev->dev = dev;
   params_vdev->is_subs_evt = false;
   spin_lock_init(&params_vdev->config_lock);
 
   strlcpy(vdev->name, PARAMS_NAME, sizeof(vdev->name));
 
   vdev->ioctl_ops = &rkisp_params_ioctl;
   vdev->fops = &rkisp_params_fops;
   vdev->release = video_device_release_empty;
   /*
    * Provide a mutex to v4l2 core. It will be used
    * to protect all fops and v4l2 ioctls.
    */
   vdev->lock = &dev->iqlock;
   vdev->v4l2_dev = v4l2_dev;
   vdev->queue = &node->buf_queue;
   vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_META_OUTPUT;
   vdev->vfl_dir = VFL_DIR_TX;
   rkisp_params_init_vb2_queue(vdev->queue, params_vdev);
   ret = rkisp_init_params_vdev(params_vdev);
   if (ret < 0)
       goto err_release_queue;
   video_set_drvdata(vdev, params_vdev);
 
   node->pad.flags = MEDIA_PAD_FL_SOURCE;
   ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
   if (ret < 0)
       goto err_release_queue;
   ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
   if (ret < 0) {
       dev_err(&vdev->dev,
           "could not register Video for Linux device\n");
       goto err_cleanup_media_entity;
   }
 
   source = &params_vdev->vnode.vdev.entity;
   sink = &params_vdev->dev->isp_sdev.sd.entity;
   ret = media_create_pad_link(source, 0, sink,
       RKISP_ISP_PAD_SINK_PARAMS, MEDIA_LNK_FL_ENABLED);
   if (ret < 0)
       goto err_unregister_video;
 
   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_params_vdev(params_vdev);
   return ret;
}
 
void rkisp_unregister_params_vdev(struct rkisp_isp_params_vdev *params_vdev)
{
   struct rkisp_vdev_node *node = &params_vdev->vnode;
   struct video_device *vdev = &node->vdev;
 
   video_unregister_device(vdev);
   media_entity_cleanup(&vdev->entity);
   vb2_queue_release(vdev->queue);
   rkisp_uninit_params_vdev(params_vdev);
}