1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3 * Rockchip ISP1 Driver - Stats subdevice
4 *
5 * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
6 */
7
8 #include <media/v4l2-common.h>
9 #include <media/v4l2-event.h>
10 #include <media/v4l2-ioctl.h>
11 #include <media/videobuf2-core.h>
12 #include <media/videobuf2-vmalloc.h> /* for ISP statistics */
13
14 #include "rkisp1-common.h"
15
16 #define RKISP1_STATS_DEV_NAME RKISP1_DRIVER_NAME "_stats"
17
18 #define RKISP1_ISP_STATS_REQ_BUFS_MIN 2
19 #define RKISP1_ISP_STATS_REQ_BUFS_MAX 8
20
rkisp1_stats_enum_fmt_meta_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)21 static int rkisp1_stats_enum_fmt_meta_cap(struct file *file, void *priv,
22 struct v4l2_fmtdesc *f)
23 {
24 struct video_device *video = video_devdata(file);
25 struct rkisp1_stats *stats = video_get_drvdata(video);
26
27 if (f->index > 0 || f->type != video->queue->type)
28 return -EINVAL;
29
30 f->pixelformat = stats->vdev_fmt.fmt.meta.dataformat;
31 return 0;
32 }
33
rkisp1_stats_g_fmt_meta_cap(struct file * file,void * priv,struct v4l2_format * f)34 static int rkisp1_stats_g_fmt_meta_cap(struct file *file, void *priv,
35 struct v4l2_format *f)
36 {
37 struct video_device *video = video_devdata(file);
38 struct rkisp1_stats *stats = video_get_drvdata(video);
39 struct v4l2_meta_format *meta = &f->fmt.meta;
40
41 if (f->type != video->queue->type)
42 return -EINVAL;
43
44 memset(meta, 0, sizeof(*meta));
45 meta->dataformat = stats->vdev_fmt.fmt.meta.dataformat;
46 meta->buffersize = stats->vdev_fmt.fmt.meta.buffersize;
47
48 return 0;
49 }
50
rkisp1_stats_querycap(struct file * file,void * priv,struct v4l2_capability * cap)51 static int rkisp1_stats_querycap(struct file *file,
52 void *priv, struct v4l2_capability *cap)
53 {
54 struct video_device *vdev = video_devdata(file);
55
56 strscpy(cap->driver, RKISP1_DRIVER_NAME, sizeof(cap->driver));
57 strscpy(cap->card, vdev->name, sizeof(cap->card));
58 strscpy(cap->bus_info, RKISP1_BUS_INFO, sizeof(cap->bus_info));
59
60 return 0;
61 }
62
63 /* ISP video device IOCTLs */
64 static const struct v4l2_ioctl_ops rkisp1_stats_ioctl = {
65 .vidioc_reqbufs = vb2_ioctl_reqbufs,
66 .vidioc_querybuf = vb2_ioctl_querybuf,
67 .vidioc_create_bufs = vb2_ioctl_create_bufs,
68 .vidioc_qbuf = vb2_ioctl_qbuf,
69 .vidioc_dqbuf = vb2_ioctl_dqbuf,
70 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
71 .vidioc_expbuf = vb2_ioctl_expbuf,
72 .vidioc_streamon = vb2_ioctl_streamon,
73 .vidioc_streamoff = vb2_ioctl_streamoff,
74 .vidioc_enum_fmt_meta_cap = rkisp1_stats_enum_fmt_meta_cap,
75 .vidioc_g_fmt_meta_cap = rkisp1_stats_g_fmt_meta_cap,
76 .vidioc_s_fmt_meta_cap = rkisp1_stats_g_fmt_meta_cap,
77 .vidioc_try_fmt_meta_cap = rkisp1_stats_g_fmt_meta_cap,
78 .vidioc_querycap = rkisp1_stats_querycap,
79 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
80 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
81 };
82
83 static const struct v4l2_file_operations rkisp1_stats_fops = {
84 .mmap = vb2_fop_mmap,
85 .unlocked_ioctl = video_ioctl2,
86 .poll = vb2_fop_poll,
87 .open = v4l2_fh_open,
88 .release = vb2_fop_release
89 };
90
rkisp1_stats_vb2_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])91 static int rkisp1_stats_vb2_queue_setup(struct vb2_queue *vq,
92 unsigned int *num_buffers,
93 unsigned int *num_planes,
94 unsigned int sizes[],
95 struct device *alloc_devs[])
96 {
97 struct rkisp1_stats *stats = vq->drv_priv;
98
99 *num_planes = 1;
100
101 *num_buffers = clamp_t(u32, *num_buffers, RKISP1_ISP_STATS_REQ_BUFS_MIN,
102 RKISP1_ISP_STATS_REQ_BUFS_MAX);
103
104 sizes[0] = sizeof(struct rkisp1_stat_buffer);
105
106 INIT_LIST_HEAD(&stats->stat);
107
108 return 0;
109 }
110
rkisp1_stats_vb2_buf_queue(struct vb2_buffer * vb)111 static void rkisp1_stats_vb2_buf_queue(struct vb2_buffer *vb)
112 {
113 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
114 struct rkisp1_buffer *stats_buf =
115 container_of(vbuf, struct rkisp1_buffer, vb);
116 struct vb2_queue *vq = vb->vb2_queue;
117 struct rkisp1_stats *stats_dev = vq->drv_priv;
118
119 stats_buf->vaddr = vb2_plane_vaddr(vb, 0);
120
121 spin_lock_irq(&stats_dev->lock);
122 list_add_tail(&stats_buf->queue, &stats_dev->stat);
123 spin_unlock_irq(&stats_dev->lock);
124 }
125
rkisp1_stats_vb2_buf_prepare(struct vb2_buffer * vb)126 static int rkisp1_stats_vb2_buf_prepare(struct vb2_buffer *vb)
127 {
128 if (vb2_plane_size(vb, 0) < sizeof(struct rkisp1_stat_buffer))
129 return -EINVAL;
130
131 vb2_set_plane_payload(vb, 0, sizeof(struct rkisp1_stat_buffer));
132
133 return 0;
134 }
135
rkisp1_stats_vb2_stop_streaming(struct vb2_queue * vq)136 static void rkisp1_stats_vb2_stop_streaming(struct vb2_queue *vq)
137 {
138 struct rkisp1_stats *stats = vq->drv_priv;
139 struct rkisp1_buffer *buf;
140 unsigned int i;
141
142 spin_lock_irq(&stats->lock);
143 stats->is_streaming = false;
144 for (i = 0; i < RKISP1_ISP_STATS_REQ_BUFS_MAX; i++) {
145 if (list_empty(&stats->stat))
146 break;
147 buf = list_first_entry(&stats->stat,
148 struct rkisp1_buffer, queue);
149 list_del(&buf->queue);
150 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
151 }
152 spin_unlock_irq(&stats->lock);
153 }
154
155 static int
rkisp1_stats_vb2_start_streaming(struct vb2_queue * queue,unsigned int count)156 rkisp1_stats_vb2_start_streaming(struct vb2_queue *queue, unsigned int count)
157 {
158 struct rkisp1_stats *stats = queue->drv_priv;
159
160 spin_lock_irq(&stats->lock);
161 stats->is_streaming = true;
162 spin_unlock_irq(&stats->lock);
163
164 return 0;
165 }
166
167 static const struct vb2_ops rkisp1_stats_vb2_ops = {
168 .queue_setup = rkisp1_stats_vb2_queue_setup,
169 .buf_queue = rkisp1_stats_vb2_buf_queue,
170 .buf_prepare = rkisp1_stats_vb2_buf_prepare,
171 .wait_prepare = vb2_ops_wait_prepare,
172 .wait_finish = vb2_ops_wait_finish,
173 .stop_streaming = rkisp1_stats_vb2_stop_streaming,
174 .start_streaming = rkisp1_stats_vb2_start_streaming,
175 };
176
177 static int
rkisp1_stats_init_vb2_queue(struct vb2_queue * q,struct rkisp1_stats * stats)178 rkisp1_stats_init_vb2_queue(struct vb2_queue *q, struct rkisp1_stats *stats)
179 {
180 struct rkisp1_vdev_node *node;
181
182 node = container_of(q, struct rkisp1_vdev_node, buf_queue);
183
184 q->type = V4L2_BUF_TYPE_META_CAPTURE;
185 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
186 q->drv_priv = stats;
187 q->ops = &rkisp1_stats_vb2_ops;
188 q->mem_ops = &vb2_vmalloc_memops;
189 q->buf_struct_size = sizeof(struct rkisp1_buffer);
190 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
191 q->lock = &node->vlock;
192
193 return vb2_queue_init(q);
194 }
195
rkisp1_stats_get_awb_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)196 static void rkisp1_stats_get_awb_meas(struct rkisp1_stats *stats,
197 struct rkisp1_stat_buffer *pbuf)
198 {
199 /* Protect against concurrent access from ISR? */
200 struct rkisp1_device *rkisp1 = stats->rkisp1;
201 u32 reg_val;
202
203 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AWB;
204 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AWB_WHITE_CNT);
205 pbuf->params.awb.awb_mean[0].cnt =
206 RKISP1_CIF_ISP_AWB_GET_PIXEL_CNT(reg_val);
207 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AWB_MEAN);
208
209 pbuf->params.awb.awb_mean[0].mean_cr_or_r =
210 RKISP1_CIF_ISP_AWB_GET_MEAN_CR_R(reg_val);
211 pbuf->params.awb.awb_mean[0].mean_cb_or_b =
212 RKISP1_CIF_ISP_AWB_GET_MEAN_CB_B(reg_val);
213 pbuf->params.awb.awb_mean[0].mean_y_or_g =
214 RKISP1_CIF_ISP_AWB_GET_MEAN_Y_G(reg_val);
215 }
216
rkisp1_stats_get_aec_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)217 static void rkisp1_stats_get_aec_meas(struct rkisp1_stats *stats,
218 struct rkisp1_stat_buffer *pbuf)
219 {
220 struct rkisp1_device *rkisp1 = stats->rkisp1;
221 unsigned int i;
222
223 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AUTOEXP;
224 for (i = 0; i < RKISP1_CIF_ISP_AE_MEAN_MAX; i++)
225 pbuf->params.ae.exp_mean[i] =
226 (u8)rkisp1_read(rkisp1,
227 RKISP1_CIF_ISP_EXP_MEAN_00 + i * 4);
228 }
229
rkisp1_stats_get_afc_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)230 static void rkisp1_stats_get_afc_meas(struct rkisp1_stats *stats,
231 struct rkisp1_stat_buffer *pbuf)
232 {
233 struct rkisp1_device *rkisp1 = stats->rkisp1;
234 struct rkisp1_cif_isp_af_stat *af;
235
236 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AFM;
237
238 af = &pbuf->params.af;
239 af->window[0].sum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_SUM_A);
240 af->window[0].lum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_LUM_A);
241 af->window[1].sum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_SUM_B);
242 af->window[1].lum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_LUM_B);
243 af->window[2].sum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_SUM_C);
244 af->window[2].lum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_LUM_C);
245 }
246
rkisp1_stats_get_hst_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)247 static void rkisp1_stats_get_hst_meas(struct rkisp1_stats *stats,
248 struct rkisp1_stat_buffer *pbuf)
249 {
250 struct rkisp1_device *rkisp1 = stats->rkisp1;
251 unsigned int i;
252
253 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_HIST;
254 for (i = 0; i < RKISP1_CIF_ISP_HIST_BIN_N_MAX; i++)
255 pbuf->params.hist.hist_bins[i] =
256 (u8)rkisp1_read(rkisp1,
257 RKISP1_CIF_ISP_HIST_BIN_0 + i * 4);
258 }
259
rkisp1_stats_get_bls_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)260 static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats,
261 struct rkisp1_stat_buffer *pbuf)
262 {
263 struct rkisp1_device *rkisp1 = stats->rkisp1;
264 const struct rkisp1_isp_mbus_info *in_fmt = rkisp1->isp.sink_fmt;
265 struct rkisp1_cif_isp_bls_meas_val *bls_val;
266
267 bls_val = &pbuf->params.ae.bls_val;
268 if (in_fmt->bayer_pat == RKISP1_RAW_BGGR) {
269 bls_val->meas_b =
270 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
271 bls_val->meas_gb =
272 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
273 bls_val->meas_gr =
274 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
275 bls_val->meas_r =
276 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
277 } else if (in_fmt->bayer_pat == RKISP1_RAW_GBRG) {
278 bls_val->meas_gb =
279 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
280 bls_val->meas_b =
281 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
282 bls_val->meas_r =
283 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
284 bls_val->meas_gr =
285 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
286 } else if (in_fmt->bayer_pat == RKISP1_RAW_GRBG) {
287 bls_val->meas_gr =
288 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
289 bls_val->meas_r =
290 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
291 bls_val->meas_b =
292 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
293 bls_val->meas_gb =
294 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
295 } else if (in_fmt->bayer_pat == RKISP1_RAW_RGGB) {
296 bls_val->meas_r =
297 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
298 bls_val->meas_gr =
299 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
300 bls_val->meas_gb =
301 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
302 bls_val->meas_b =
303 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
304 }
305 }
306
307 static void
rkisp1_stats_send_measurement(struct rkisp1_stats * stats,u32 isp_ris)308 rkisp1_stats_send_measurement(struct rkisp1_stats *stats, u32 isp_ris)
309 {
310 struct rkisp1_stat_buffer *cur_stat_buf;
311 struct rkisp1_buffer *cur_buf = NULL;
312 unsigned int frame_sequence = stats->rkisp1->isp.frame_sequence;
313 u64 timestamp = ktime_get_ns();
314
315 /* get one empty buffer */
316 if (!list_empty(&stats->stat)) {
317 cur_buf = list_first_entry(&stats->stat,
318 struct rkisp1_buffer, queue);
319 list_del(&cur_buf->queue);
320 }
321
322 if (!cur_buf)
323 return;
324
325 cur_stat_buf =
326 (struct rkisp1_stat_buffer *)(cur_buf->vaddr);
327
328 if (isp_ris & RKISP1_CIF_ISP_AWB_DONE)
329 rkisp1_stats_get_awb_meas(stats, cur_stat_buf);
330
331 if (isp_ris & RKISP1_CIF_ISP_AFM_FIN)
332 rkisp1_stats_get_afc_meas(stats, cur_stat_buf);
333
334 if (isp_ris & RKISP1_CIF_ISP_EXP_END) {
335 rkisp1_stats_get_aec_meas(stats, cur_stat_buf);
336 rkisp1_stats_get_bls_meas(stats, cur_stat_buf);
337 }
338
339 if (isp_ris & RKISP1_CIF_ISP_HIST_MEASURE_RDY)
340 rkisp1_stats_get_hst_meas(stats, cur_stat_buf);
341
342 vb2_set_plane_payload(&cur_buf->vb.vb2_buf, 0,
343 sizeof(struct rkisp1_stat_buffer));
344 cur_buf->vb.sequence = frame_sequence;
345 cur_buf->vb.vb2_buf.timestamp = timestamp;
346 vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
347 }
348
rkisp1_stats_isr(struct rkisp1_stats * stats,u32 isp_ris)349 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)
350 {
351 struct rkisp1_device *rkisp1 = stats->rkisp1;
352 unsigned int isp_mis_tmp = 0;
353
354 spin_lock(&stats->lock);
355
356 rkisp1_write(rkisp1, RKISP1_STATS_MEAS_MASK, RKISP1_CIF_ISP_ICR);
357
358 isp_mis_tmp = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
359 if (isp_mis_tmp & RKISP1_STATS_MEAS_MASK)
360 rkisp1->debug.stats_error++;
361
362 if (!stats->is_streaming)
363 goto unlock;
364 if (isp_ris & RKISP1_STATS_MEAS_MASK)
365 rkisp1_stats_send_measurement(stats, isp_ris);
366
367 unlock:
368 spin_unlock(&stats->lock);
369 }
370
rkisp1_init_stats(struct rkisp1_stats * stats)371 static void rkisp1_init_stats(struct rkisp1_stats *stats)
372 {
373 stats->vdev_fmt.fmt.meta.dataformat =
374 V4L2_META_FMT_RK_ISP1_STAT_3A;
375 stats->vdev_fmt.fmt.meta.buffersize =
376 sizeof(struct rkisp1_stat_buffer);
377 }
378
rkisp1_stats_register(struct rkisp1_device * rkisp1)379 int rkisp1_stats_register(struct rkisp1_device *rkisp1)
380 {
381 struct rkisp1_stats *stats = &rkisp1->stats;
382 struct rkisp1_vdev_node *node = &stats->vnode;
383 struct video_device *vdev = &node->vdev;
384 int ret;
385
386 stats->rkisp1 = rkisp1;
387 mutex_init(&node->vlock);
388 INIT_LIST_HEAD(&stats->stat);
389 spin_lock_init(&stats->lock);
390
391 strscpy(vdev->name, RKISP1_STATS_DEV_NAME, sizeof(vdev->name));
392
393 video_set_drvdata(vdev, stats);
394 vdev->ioctl_ops = &rkisp1_stats_ioctl;
395 vdev->fops = &rkisp1_stats_fops;
396 vdev->release = video_device_release_empty;
397 vdev->lock = &node->vlock;
398 vdev->v4l2_dev = &rkisp1->v4l2_dev;
399 vdev->queue = &node->buf_queue;
400 vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
401 vdev->vfl_dir = VFL_DIR_RX;
402 rkisp1_stats_init_vb2_queue(vdev->queue, stats);
403 rkisp1_init_stats(stats);
404 video_set_drvdata(vdev, stats);
405
406 node->pad.flags = MEDIA_PAD_FL_SINK;
407 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
408 if (ret)
409 goto err_mutex_destroy;
410
411 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
412 if (ret) {
413 dev_err(&vdev->dev,
414 "failed to register %s, ret=%d\n", vdev->name, ret);
415 goto err_cleanup_media_entity;
416 }
417
418 return 0;
419
420 err_cleanup_media_entity:
421 media_entity_cleanup(&vdev->entity);
422 err_mutex_destroy:
423 mutex_destroy(&node->vlock);
424 return ret;
425 }
426
rkisp1_stats_unregister(struct rkisp1_device * rkisp1)427 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1)
428 {
429 struct rkisp1_stats *stats = &rkisp1->stats;
430 struct rkisp1_vdev_node *node = &stats->vnode;
431 struct video_device *vdev = &node->vdev;
432
433 vb2_video_unregister_device(vdev);
434 media_entity_cleanup(&vdev->entity);
435 mutex_destroy(&node->vlock);
436 }
437