1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
3
4 #include <media/v4l2-common.h>
5 #include <media/v4l2-ioctl.h>
6 #include <media/videobuf2-core.h>
7 #include <media/videobuf2-vmalloc.h> /* for ISP params */
8 #include <media/v4l2-event.h>
9 #include <linux/rk-preisp.h>
10 #include "dev.h"
11 #include "isp_params.h"
12 #include "isp_params_v1x.h"
13 #include "isp_params_v2x.h"
14 #include "isp_params_v21.h"
15 #include "isp_params_v3x.h"
16
17 #define PARAMS_NAME DRIVER_NAME "-input-params"
18 #define RKISP_ISP_PARAMS_REQ_BUFS_MIN 2
19 #define RKISP_ISP_PARAMS_REQ_BUFS_MAX 8
20
rkisp_params_enum_fmt_meta_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)21 static int rkisp_params_enum_fmt_meta_out(struct file *file, void *priv,
22 struct v4l2_fmtdesc *f)
23 {
24 struct video_device *video = video_devdata(file);
25 struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(video);
26
27 if (f->index > 0 || f->type != video->queue->type)
28 return -EINVAL;
29
30 f->pixelformat = params_vdev->vdev_fmt.fmt.meta.dataformat;
31
32 return 0;
33 }
34
rkisp_params_g_fmt_meta_out(struct file * file,void * fh,struct v4l2_format * f)35 static int rkisp_params_g_fmt_meta_out(struct file *file, void *fh,
36 struct v4l2_format *f)
37 {
38 struct video_device *video = video_devdata(file);
39 struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(video);
40 struct v4l2_meta_format *meta = &f->fmt.meta;
41
42 if (f->type != video->queue->type)
43 return -EINVAL;
44
45 memset(meta, 0, sizeof(*meta));
46 meta->dataformat = params_vdev->vdev_fmt.fmt.meta.dataformat;
47 meta->buffersize = params_vdev->vdev_fmt.fmt.meta.buffersize;
48
49 return 0;
50 }
51
rkisp_params_querycap(struct file * file,void * priv,struct v4l2_capability * cap)52 static int rkisp_params_querycap(struct file *file,
53 void *priv, struct v4l2_capability *cap)
54 {
55 struct video_device *vdev = video_devdata(file);
56 struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(vdev);
57
58 snprintf(cap->driver, sizeof(cap->driver),
59 "%s_v%d", DRIVER_NAME,
60 params_vdev->dev->isp_ver >> 4);
61 strlcpy(cap->card, vdev->name, sizeof(cap->card));
62 strlcpy(cap->bus_info, "platform: " DRIVER_NAME, sizeof(cap->bus_info));
63
64 return 0;
65 }
66
rkisp_params_subs_evt(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)67 static int rkisp_params_subs_evt(struct v4l2_fh *fh,
68 const struct v4l2_event_subscription *sub)
69 {
70 struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(fh->vdev);
71
72 if (sub->id != 0)
73 return -EINVAL;
74
75 switch (sub->type) {
76 case CIFISP_V4L2_EVENT_STREAM_START:
77 case CIFISP_V4L2_EVENT_STREAM_STOP:
78 params_vdev->is_subs_evt = true;
79 return v4l2_event_subscribe(fh, sub, 0, NULL);
80 default:
81 return -EINVAL;
82 }
83 }
84
rkisp_params_unsubs_evt(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)85 static int rkisp_params_unsubs_evt(struct v4l2_fh *fh,
86 const struct v4l2_event_subscription *sub)
87 {
88 struct rkisp_isp_params_vdev *params_vdev = video_get_drvdata(fh->vdev);
89
90 params_vdev->is_subs_evt = false;
91 return v4l2_event_unsubscribe(fh, sub);
92 }
93
94 /* ISP params video device IOCTLs */
95 static const struct v4l2_ioctl_ops rkisp_params_ioctl = {
96 .vidioc_reqbufs = vb2_ioctl_reqbufs,
97 .vidioc_querybuf = vb2_ioctl_querybuf,
98 .vidioc_create_bufs = vb2_ioctl_create_bufs,
99 .vidioc_qbuf = vb2_ioctl_qbuf,
100 .vidioc_dqbuf = vb2_ioctl_dqbuf,
101 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
102 .vidioc_expbuf = vb2_ioctl_expbuf,
103 .vidioc_streamon = vb2_ioctl_streamon,
104 .vidioc_streamoff = vb2_ioctl_streamoff,
105 .vidioc_enum_fmt_meta_out = rkisp_params_enum_fmt_meta_out,
106 .vidioc_g_fmt_meta_out = rkisp_params_g_fmt_meta_out,
107 .vidioc_s_fmt_meta_out = rkisp_params_g_fmt_meta_out,
108 .vidioc_try_fmt_meta_out = rkisp_params_g_fmt_meta_out,
109 .vidioc_querycap = rkisp_params_querycap,
110 .vidioc_subscribe_event = rkisp_params_subs_evt,
111 .vidioc_unsubscribe_event = rkisp_params_unsubs_evt,
112 };
113
rkisp_params_vb2_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_ctxs[])114 static int rkisp_params_vb2_queue_setup(struct vb2_queue *vq,
115 unsigned int *num_buffers,
116 unsigned int *num_planes,
117 unsigned int sizes[],
118 struct device *alloc_ctxs[])
119 {
120 struct rkisp_isp_params_vdev *params_vdev = vq->drv_priv;
121
122 *num_buffers = clamp_t(u32, *num_buffers,
123 RKISP_ISP_PARAMS_REQ_BUFS_MIN,
124 RKISP_ISP_PARAMS_REQ_BUFS_MAX);
125
126 *num_planes = 1;
127 params_vdev->ops->get_param_size(params_vdev, sizes);
128
129 INIT_LIST_HEAD(¶ms_vdev->params);
130 params_vdev->first_params = true;
131
132 return 0;
133 }
134
rkisp_params_vb2_buf_queue(struct vb2_buffer * vb)135 static void rkisp_params_vb2_buf_queue(struct vb2_buffer *vb)
136 {
137 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
138 struct rkisp_buffer *params_buf = to_rkisp_buffer(vbuf);
139 struct vb2_queue *vq = vb->vb2_queue;
140 struct rkisp_isp_params_vdev *params_vdev = vq->drv_priv;
141 void *first_param;
142 unsigned long flags;
143
144 unsigned int cur_frame_id = -1;
145 cur_frame_id = atomic_read(¶ms_vdev->dev->isp_sdev.frm_sync_seq) - 1;
146 if (params_vdev->first_params) {
147 first_param = vb2_plane_vaddr(vb, 0);
148 params_vdev->ops->save_first_param(params_vdev, first_param);
149 vbuf->sequence = cur_frame_id;
150 vb2_buffer_done(¶ms_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
151 params_vdev->first_params = false;
152 wake_up(¶ms_vdev->dev->sync_onoff);
153 dev_info(params_vdev->dev->dev, "first params buf queue\n");
154 return;
155 }
156
157 params_buf->vaddr[0] = vb2_plane_vaddr(vb, 0);
158 spin_lock_irqsave(¶ms_vdev->config_lock, flags);
159 list_add_tail(¶ms_buf->queue, ¶ms_vdev->params);
160 spin_unlock_irqrestore(¶ms_vdev->config_lock, flags);
161 }
162
rkisp_params_vb2_stop_streaming(struct vb2_queue * vq)163 static void rkisp_params_vb2_stop_streaming(struct vb2_queue *vq)
164 {
165 struct rkisp_isp_params_vdev *params_vdev = vq->drv_priv;
166 struct rkisp_device *dev = params_vdev->dev;
167 struct rkisp_buffer *buf;
168 unsigned long flags;
169 int i;
170
171 /* stop params input firstly */
172 spin_lock_irqsave(¶ms_vdev->config_lock, flags);
173 params_vdev->streamon = false;
174 wake_up(&dev->sync_onoff);
175 spin_unlock_irqrestore(¶ms_vdev->config_lock, flags);
176
177 for (i = 0; i < RKISP_ISP_PARAMS_REQ_BUFS_MAX; i++) {
178 spin_lock_irqsave(¶ms_vdev->config_lock, flags);
179 if (!list_empty(¶ms_vdev->params)) {
180 buf = list_first_entry(¶ms_vdev->params,
181 struct rkisp_buffer, queue);
182 list_del(&buf->queue);
183 spin_unlock_irqrestore(¶ms_vdev->config_lock,
184 flags);
185 } else {
186 spin_unlock_irqrestore(¶ms_vdev->config_lock,
187 flags);
188 break;
189 }
190
191 if (buf)
192 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
193 buf = NULL;
194 }
195
196 if (params_vdev->cur_buf) {
197 buf = params_vdev->cur_buf;
198 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
199 params_vdev->cur_buf = NULL;
200 }
201
202 rkisp_params_disable_isp(params_vdev);
203 /* clean module params */
204 params_vdev->ops->clear_first_param(params_vdev);
205 params_vdev->rdbk_times = 0;
206 }
207
208 static int
rkisp_params_vb2_start_streaming(struct vb2_queue * queue,unsigned int count)209 rkisp_params_vb2_start_streaming(struct vb2_queue *queue, unsigned int count)
210 {
211 struct rkisp_isp_params_vdev *params_vdev = queue->drv_priv;
212 unsigned long flags;
213
214 params_vdev->is_first_cfg = true;
215 params_vdev->hdrtmo_en = false;
216 params_vdev->afaemode_en = false;
217 params_vdev->cur_buf = NULL;
218 spin_lock_irqsave(¶ms_vdev->config_lock, flags);
219 params_vdev->streamon = true;
220 spin_unlock_irqrestore(¶ms_vdev->config_lock, flags);
221
222 return 0;
223 }
224
225 static struct vb2_ops rkisp_params_vb2_ops = {
226 .queue_setup = rkisp_params_vb2_queue_setup,
227 .wait_prepare = vb2_ops_wait_prepare,
228 .wait_finish = vb2_ops_wait_finish,
229 .buf_queue = rkisp_params_vb2_buf_queue,
230 .start_streaming = rkisp_params_vb2_start_streaming,
231 .stop_streaming = rkisp_params_vb2_stop_streaming,
232
233 };
234
rkisp_params_fh_open(struct file * filp)235 static int rkisp_params_fh_open(struct file *filp)
236 {
237 struct rkisp_isp_params_vdev *params = video_drvdata(filp);
238 int ret;
239
240 ret = v4l2_fh_open(filp);
241 if (!ret) {
242 ret = v4l2_pipeline_pm_get(¶ms->vnode.vdev.entity);
243 if (ret < 0)
244 vb2_fop_release(filp);
245 }
246
247 return ret;
248 }
249
rkisp_params_fop_release(struct file * file)250 static int rkisp_params_fop_release(struct file *file)
251 {
252 struct rkisp_isp_params_vdev *params = video_drvdata(file);
253 struct video_device *vdev = video_devdata(file);
254 int ret;
255
256 if (file->private_data == vdev->queue->owner && params->ops->fop_release)
257 params->ops->fop_release(params);
258
259 ret = vb2_fop_release(file);
260 if (!ret)
261 v4l2_pipeline_pm_put(¶ms->vnode.vdev.entity);
262 return ret;
263 }
264
265 struct v4l2_file_operations rkisp_params_fops = {
266 .mmap = vb2_fop_mmap,
267 .unlocked_ioctl = video_ioctl2,
268 .poll = vb2_fop_poll,
269 .open = rkisp_params_fh_open,
270 .release = rkisp_params_fop_release
271 };
272
273 static int
rkisp_params_init_vb2_queue(struct vb2_queue * q,struct rkisp_isp_params_vdev * params_vdev)274 rkisp_params_init_vb2_queue(struct vb2_queue *q,
275 struct rkisp_isp_params_vdev *params_vdev)
276 {
277 q->type = V4L2_BUF_TYPE_META_OUTPUT;
278 q->io_modes = VB2_MMAP | VB2_USERPTR;
279 q->drv_priv = params_vdev;
280 q->ops = &rkisp_params_vb2_ops;
281 q->mem_ops = &vb2_vmalloc_memops;
282 q->buf_struct_size = sizeof(struct rkisp_buffer);
283 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
284 q->lock = ¶ms_vdev->dev->iqlock;
285 q->dev = params_vdev->dev->dev;
286
287 return vb2_queue_init(q);
288 }
289
rkisp_init_params_vdev(struct rkisp_isp_params_vdev * params_vdev)290 static int rkisp_init_params_vdev(struct rkisp_isp_params_vdev *params_vdev)
291 {
292 int ret;
293
294 if (params_vdev->dev->isp_ver <= ISP_V13)
295 ret = rkisp_init_params_vdev_v1x(params_vdev);
296 else if (params_vdev->dev->isp_ver == ISP_V21)
297 ret = rkisp_init_params_vdev_v21(params_vdev);
298 else if (params_vdev->dev->isp_ver == ISP_V20)
299 ret = rkisp_init_params_vdev_v2x(params_vdev);
300 else
301 ret = rkisp_init_params_vdev_v3x(params_vdev);
302
303 params_vdev->vdev_fmt.fmt.meta.dataformat =
304 V4L2_META_FMT_RK_ISP1_PARAMS;
305 if (params_vdev->ops && params_vdev->ops->get_param_size)
306 params_vdev->ops->get_param_size(params_vdev,
307 ¶ms_vdev->vdev_fmt.fmt.meta.buffersize);
308 return ret;
309 }
310
rkisp_uninit_params_vdev(struct rkisp_isp_params_vdev * params_vdev)311 static void rkisp_uninit_params_vdev(struct rkisp_isp_params_vdev *params_vdev)
312 {
313 if (params_vdev->dev->isp_ver <= ISP_V13)
314 rkisp_uninit_params_vdev_v1x(params_vdev);
315 else if (params_vdev->dev->isp_ver == ISP_V21)
316 rkisp_uninit_params_vdev_v21(params_vdev);
317 else if (params_vdev->dev->isp_ver == ISP_V20)
318 rkisp_uninit_params_vdev_v2x(params_vdev);
319 else
320 rkisp_uninit_params_vdev_v3x(params_vdev);
321 }
322
rkisp_params_cfg(struct rkisp_isp_params_vdev * params_vdev,u32 frame_id)323 void rkisp_params_cfg(struct rkisp_isp_params_vdev *params_vdev, u32 frame_id)
324 {
325 if (params_vdev->ops->param_cfg)
326 params_vdev->ops->param_cfg(params_vdev, frame_id, RKISP_PARAMS_IMD);
327 }
328
rkisp_params_cfgsram(struct rkisp_isp_params_vdev * params_vdev)329 void rkisp_params_cfgsram(struct rkisp_isp_params_vdev *params_vdev)
330 {
331 /* multi device to switch sram config */
332 if (params_vdev->dev->hw_dev->is_single)
333 return;
334
335 if (params_vdev->ops->param_cfgsram)
336 params_vdev->ops->param_cfgsram(params_vdev);
337 }
338
rkisp_params_isr(struct rkisp_isp_params_vdev * params_vdev,u32 isp_mis)339 void rkisp_params_isr(struct rkisp_isp_params_vdev *params_vdev,
340 u32 isp_mis)
341 {
342 params_vdev->ops->isr_hdl(params_vdev, isp_mis);
343 }
344
345 /* Not called when the camera active, thus not isr protection. */
rkisp_params_first_cfg(struct rkisp_isp_params_vdev * params_vdev,struct ispsd_in_fmt * in_fmt,enum v4l2_quantization quantization)346 void rkisp_params_first_cfg(struct rkisp_isp_params_vdev *params_vdev,
347 struct ispsd_in_fmt *in_fmt,
348 enum v4l2_quantization quantization)
349 {
350 if (!params_vdev->is_first_cfg)
351 return;
352 params_vdev->is_first_cfg = false;
353 params_vdev->quantization = quantization;
354 params_vdev->raw_type = in_fmt->bayer_pat;
355 params_vdev->in_mbus_code = in_fmt->mbus_code;
356 params_vdev->ops->first_cfg(params_vdev);
357 }
358
359 /* Not called when the camera active, thus not isr protection. */
rkisp_params_disable_isp(struct rkisp_isp_params_vdev * params_vdev)360 void rkisp_params_disable_isp(struct rkisp_isp_params_vdev *params_vdev)
361 {
362 if (params_vdev->ops->disable_isp)
363 params_vdev->ops->disable_isp(params_vdev);
364 }
365
rkisp_params_get_meshbuf_inf(struct rkisp_isp_params_vdev * params_vdev,void * meshbuf)366 void rkisp_params_get_meshbuf_inf(struct rkisp_isp_params_vdev *params_vdev,
367 void *meshbuf)
368 {
369 if (params_vdev->ops->get_meshbuf_inf)
370 params_vdev->ops->get_meshbuf_inf(params_vdev, meshbuf);
371 }
372
rkisp_params_set_meshbuf_size(struct rkisp_isp_params_vdev * params_vdev,void * meshsize)373 void rkisp_params_set_meshbuf_size(struct rkisp_isp_params_vdev *params_vdev,
374 void *meshsize)
375 {
376 if (params_vdev->ops->set_meshbuf_size)
377 params_vdev->ops->set_meshbuf_size(params_vdev, meshsize);
378 }
379
rkisp_params_stream_stop(struct rkisp_isp_params_vdev * params_vdev)380 void rkisp_params_stream_stop(struct rkisp_isp_params_vdev *params_vdev)
381 {
382 if (params_vdev->ops->stream_stop)
383 params_vdev->ops->stream_stop(params_vdev);
384 }
385
rkisp_register_params_vdev(struct rkisp_isp_params_vdev * params_vdev,struct v4l2_device * v4l2_dev,struct rkisp_device * dev)386 int rkisp_register_params_vdev(struct rkisp_isp_params_vdev *params_vdev,
387 struct v4l2_device *v4l2_dev,
388 struct rkisp_device *dev)
389 {
390 int ret;
391 struct rkisp_vdev_node *node = ¶ms_vdev->vnode;
392 struct video_device *vdev = &node->vdev;
393 struct media_entity *source, *sink;
394
395 params_vdev->dev = dev;
396 params_vdev->is_subs_evt = false;
397 spin_lock_init(¶ms_vdev->config_lock);
398
399 strlcpy(vdev->name, PARAMS_NAME, sizeof(vdev->name));
400
401 vdev->ioctl_ops = &rkisp_params_ioctl;
402 vdev->fops = &rkisp_params_fops;
403 vdev->release = video_device_release_empty;
404 /*
405 * Provide a mutex to v4l2 core. It will be used
406 * to protect all fops and v4l2 ioctls.
407 */
408 vdev->lock = &dev->iqlock;
409 vdev->v4l2_dev = v4l2_dev;
410 vdev->queue = &node->buf_queue;
411 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_META_OUTPUT;
412 vdev->vfl_dir = VFL_DIR_TX;
413 rkisp_params_init_vb2_queue(vdev->queue, params_vdev);
414 ret = rkisp_init_params_vdev(params_vdev);
415 if (ret < 0)
416 goto err_release_queue;
417 video_set_drvdata(vdev, params_vdev);
418
419 node->pad.flags = MEDIA_PAD_FL_SOURCE;
420 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
421 if (ret < 0)
422 goto err_release_queue;
423 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
424 if (ret < 0) {
425 dev_err(&vdev->dev,
426 "could not register Video for Linux device\n");
427 goto err_cleanup_media_entity;
428 }
429
430 source = ¶ms_vdev->vnode.vdev.entity;
431 sink = ¶ms_vdev->dev->isp_sdev.sd.entity;
432 ret = media_create_pad_link(source, 0, sink,
433 RKISP_ISP_PAD_SINK_PARAMS, MEDIA_LNK_FL_ENABLED);
434 if (ret < 0)
435 goto err_unregister_video;
436
437 return 0;
438
439 err_unregister_video:
440 video_unregister_device(vdev);
441 err_cleanup_media_entity:
442 media_entity_cleanup(&vdev->entity);
443 err_release_queue:
444 vb2_queue_release(vdev->queue);
445 rkisp_uninit_params_vdev(params_vdev);
446 return ret;
447 }
448
rkisp_unregister_params_vdev(struct rkisp_isp_params_vdev * params_vdev)449 void rkisp_unregister_params_vdev(struct rkisp_isp_params_vdev *params_vdev)
450 {
451 struct rkisp_vdev_node *node = ¶ms_vdev->vnode;
452 struct video_device *vdev = &node->vdev;
453
454 video_unregister_device(vdev);
455 media_entity_cleanup(&vdev->entity);
456 vb2_queue_release(vdev->queue);
457 rkisp_uninit_params_vdev(params_vdev);
458 }
459