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 *num_planes = 1;
98
99 *num_buffers = clamp_t(u32, *num_buffers, RKISP1_ISP_STATS_REQ_BUFS_MIN,
100 RKISP1_ISP_STATS_REQ_BUFS_MAX);
101
102 sizes[0] = sizeof(struct rkisp1_stat_buffer);
103
104 return 0;
105 }
106
rkisp1_stats_vb2_buf_queue(struct vb2_buffer * vb)107 static void rkisp1_stats_vb2_buf_queue(struct vb2_buffer *vb)
108 {
109 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
110 struct rkisp1_buffer *stats_buf =
111 container_of(vbuf, struct rkisp1_buffer, vb);
112 struct vb2_queue *vq = vb->vb2_queue;
113 struct rkisp1_stats *stats_dev = vq->drv_priv;
114
115
116 spin_lock_irq(&stats_dev->lock);
117 list_add_tail(&stats_buf->queue, &stats_dev->stat);
118 spin_unlock_irq(&stats_dev->lock);
119 }
120
rkisp1_stats_vb2_buf_prepare(struct vb2_buffer * vb)121 static int rkisp1_stats_vb2_buf_prepare(struct vb2_buffer *vb)
122 {
123 if (vb2_plane_size(vb, 0) < sizeof(struct rkisp1_stat_buffer))
124 return -EINVAL;
125
126 vb2_set_plane_payload(vb, 0, sizeof(struct rkisp1_stat_buffer));
127
128 return 0;
129 }
130
rkisp1_stats_vb2_stop_streaming(struct vb2_queue * vq)131 static void rkisp1_stats_vb2_stop_streaming(struct vb2_queue *vq)
132 {
133 struct rkisp1_stats *stats = vq->drv_priv;
134 struct rkisp1_buffer *buf;
135 unsigned int i;
136
137 spin_lock_irq(&stats->lock);
138 for (i = 0; i < RKISP1_ISP_STATS_REQ_BUFS_MAX; i++) {
139 if (list_empty(&stats->stat))
140 break;
141 buf = list_first_entry(&stats->stat,
142 struct rkisp1_buffer, queue);
143 list_del(&buf->queue);
144 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
145 }
146 spin_unlock_irq(&stats->lock);
147 }
148
149 static const struct vb2_ops rkisp1_stats_vb2_ops = {
150 .queue_setup = rkisp1_stats_vb2_queue_setup,
151 .buf_queue = rkisp1_stats_vb2_buf_queue,
152 .buf_prepare = rkisp1_stats_vb2_buf_prepare,
153 .wait_prepare = vb2_ops_wait_prepare,
154 .wait_finish = vb2_ops_wait_finish,
155 .stop_streaming = rkisp1_stats_vb2_stop_streaming,
156 };
157
158 static int
rkisp1_stats_init_vb2_queue(struct vb2_queue * q,struct rkisp1_stats * stats)159 rkisp1_stats_init_vb2_queue(struct vb2_queue *q, struct rkisp1_stats *stats)
160 {
161 struct rkisp1_vdev_node *node;
162
163 node = container_of(q, struct rkisp1_vdev_node, buf_queue);
164
165 q->type = V4L2_BUF_TYPE_META_CAPTURE;
166 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
167 q->drv_priv = stats;
168 q->ops = &rkisp1_stats_vb2_ops;
169 q->mem_ops = &vb2_vmalloc_memops;
170 q->buf_struct_size = sizeof(struct rkisp1_buffer);
171 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
172 q->lock = &node->vlock;
173
174 return vb2_queue_init(q);
175 }
176
rkisp1_stats_get_awb_meas_v10(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)177 static void rkisp1_stats_get_awb_meas_v10(struct rkisp1_stats *stats,
178 struct rkisp1_stat_buffer *pbuf)
179 {
180 /* Protect against concurrent access from ISR? */
181 struct rkisp1_device *rkisp1 = stats->rkisp1;
182 u32 reg_val;
183
184 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AWB;
185 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AWB_WHITE_CNT_V10);
186 pbuf->params.awb.awb_mean[0].cnt =
187 RKISP1_CIF_ISP_AWB_GET_PIXEL_CNT(reg_val);
188 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AWB_MEAN_V10);
189
190 pbuf->params.awb.awb_mean[0].mean_cr_or_r =
191 RKISP1_CIF_ISP_AWB_GET_MEAN_CR_R(reg_val);
192 pbuf->params.awb.awb_mean[0].mean_cb_or_b =
193 RKISP1_CIF_ISP_AWB_GET_MEAN_CB_B(reg_val);
194 pbuf->params.awb.awb_mean[0].mean_y_or_g =
195 RKISP1_CIF_ISP_AWB_GET_MEAN_Y_G(reg_val);
196 }
197
rkisp1_stats_get_awb_meas_v12(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)198 static void rkisp1_stats_get_awb_meas_v12(struct rkisp1_stats *stats,
199 struct rkisp1_stat_buffer *pbuf)
200 {
201 /* Protect against concurrent access from ISR? */
202 struct rkisp1_device *rkisp1 = stats->rkisp1;
203 u32 reg_val;
204
205 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AWB;
206 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AWB_WHITE_CNT_V12);
207 pbuf->params.awb.awb_mean[0].cnt =
208 RKISP1_CIF_ISP_AWB_GET_PIXEL_CNT(reg_val);
209 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AWB_MEAN_V12);
210
211 pbuf->params.awb.awb_mean[0].mean_cr_or_r =
212 RKISP1_CIF_ISP_AWB_GET_MEAN_CR_R(reg_val);
213 pbuf->params.awb.awb_mean[0].mean_cb_or_b =
214 RKISP1_CIF_ISP_AWB_GET_MEAN_CB_B(reg_val);
215 pbuf->params.awb.awb_mean[0].mean_y_or_g =
216 RKISP1_CIF_ISP_AWB_GET_MEAN_Y_G(reg_val);
217 }
218
rkisp1_stats_get_aec_meas_v10(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)219 static void rkisp1_stats_get_aec_meas_v10(struct rkisp1_stats *stats,
220 struct rkisp1_stat_buffer *pbuf)
221 {
222 struct rkisp1_device *rkisp1 = stats->rkisp1;
223 unsigned int i;
224
225 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AUTOEXP;
226 for (i = 0; i < RKISP1_CIF_ISP_AE_MEAN_MAX_V10; i++)
227 pbuf->params.ae.exp_mean[i] =
228 (u8)rkisp1_read(rkisp1,
229 RKISP1_CIF_ISP_EXP_MEAN_00_V10 + i * 4);
230 }
231
rkisp1_stats_get_aec_meas_v12(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)232 static void rkisp1_stats_get_aec_meas_v12(struct rkisp1_stats *stats,
233 struct rkisp1_stat_buffer *pbuf)
234 {
235 struct rkisp1_device *rkisp1 = stats->rkisp1;
236 u32 value;
237 int i;
238
239 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AUTOEXP;
240 for (i = 0; i < RKISP1_CIF_ISP_AE_MEAN_MAX_V12 / 4; i++) {
241 value = rkisp1_read(rkisp1, RKISP1_CIF_ISP_EXP_MEAN_V12 + i * 4);
242 pbuf->params.ae.exp_mean[4 * i + 0] =
243 RKISP1_CIF_ISP_EXP_GET_MEAN_xy0_V12(value);
244 pbuf->params.ae.exp_mean[4 * i + 1] =
245 RKISP1_CIF_ISP_EXP_GET_MEAN_xy1_V12(value);
246 pbuf->params.ae.exp_mean[4 * i + 2] =
247 RKISP1_CIF_ISP_EXP_GET_MEAN_xy2_V12(value);
248 pbuf->params.ae.exp_mean[4 * i + 3] =
249 RKISP1_CIF_ISP_EXP_GET_MEAN_xy3_V12(value);
250 }
251
252 value = rkisp1_read(rkisp1, RKISP1_CIF_ISP_EXP_MEAN_V12 + i * 4);
253 pbuf->params.ae.exp_mean[4 * i + 0] = RKISP1_CIF_ISP_EXP_GET_MEAN_xy0_V12(value);
254 }
255
rkisp1_stats_get_afc_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)256 static void rkisp1_stats_get_afc_meas(struct rkisp1_stats *stats,
257 struct rkisp1_stat_buffer *pbuf)
258 {
259 struct rkisp1_device *rkisp1 = stats->rkisp1;
260 struct rkisp1_cif_isp_af_stat *af;
261
262 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_AFM;
263
264 af = &pbuf->params.af;
265 af->window[0].sum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_SUM_A);
266 af->window[0].lum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_LUM_A);
267 af->window[1].sum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_SUM_B);
268 af->window[1].lum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_LUM_B);
269 af->window[2].sum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_SUM_C);
270 af->window[2].lum = rkisp1_read(rkisp1, RKISP1_CIF_ISP_AFM_LUM_C);
271 }
272
rkisp1_stats_get_hst_meas_v10(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)273 static void rkisp1_stats_get_hst_meas_v10(struct rkisp1_stats *stats,
274 struct rkisp1_stat_buffer *pbuf)
275 {
276 struct rkisp1_device *rkisp1 = stats->rkisp1;
277 unsigned int i;
278
279 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_HIST;
280 for (i = 0; i < RKISP1_CIF_ISP_HIST_BIN_N_MAX_V10; i++) {
281 u32 reg_val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_HIST_BIN_0_V10 + i * 4);
282
283 pbuf->params.hist.hist_bins[i] = RKISP1_CIF_ISP_HIST_GET_BIN_V10(reg_val);
284 }
285 }
286
rkisp1_stats_get_hst_meas_v12(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)287 static void rkisp1_stats_get_hst_meas_v12(struct rkisp1_stats *stats,
288 struct rkisp1_stat_buffer *pbuf)
289 {
290 struct rkisp1_device *rkisp1 = stats->rkisp1;
291 u32 value;
292 int i;
293
294 pbuf->meas_type |= RKISP1_CIF_ISP_STAT_HIST;
295 for (i = 0; i < RKISP1_CIF_ISP_HIST_BIN_N_MAX_V12 / 2; i++) {
296 value = rkisp1_read(rkisp1, RKISP1_CIF_ISP_HIST_BIN_V12 + i * 4);
297 pbuf->params.hist.hist_bins[2 * i] =
298 RKISP1_CIF_ISP_HIST_GET_BIN0_V12(value);
299 pbuf->params.hist.hist_bins[2 * i + 1] =
300 RKISP1_CIF_ISP_HIST_GET_BIN1_V12(value);
301 }
302 }
303
rkisp1_stats_get_bls_meas(struct rkisp1_stats * stats,struct rkisp1_stat_buffer * pbuf)304 static void rkisp1_stats_get_bls_meas(struct rkisp1_stats *stats,
305 struct rkisp1_stat_buffer *pbuf)
306 {
307 struct rkisp1_device *rkisp1 = stats->rkisp1;
308 const struct rkisp1_isp_mbus_info *in_fmt = rkisp1->isp.sink_fmt;
309 struct rkisp1_cif_isp_bls_meas_val *bls_val;
310
311 bls_val = &pbuf->params.ae.bls_val;
312 if (in_fmt->bayer_pat == RKISP1_RAW_BGGR) {
313 bls_val->meas_b =
314 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
315 bls_val->meas_gb =
316 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
317 bls_val->meas_gr =
318 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
319 bls_val->meas_r =
320 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
321 } else if (in_fmt->bayer_pat == RKISP1_RAW_GBRG) {
322 bls_val->meas_gb =
323 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
324 bls_val->meas_b =
325 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
326 bls_val->meas_r =
327 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
328 bls_val->meas_gr =
329 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
330 } else if (in_fmt->bayer_pat == RKISP1_RAW_GRBG) {
331 bls_val->meas_gr =
332 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
333 bls_val->meas_r =
334 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
335 bls_val->meas_b =
336 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
337 bls_val->meas_gb =
338 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
339 } else if (in_fmt->bayer_pat == RKISP1_RAW_RGGB) {
340 bls_val->meas_r =
341 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_A_MEASURED);
342 bls_val->meas_gr =
343 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_B_MEASURED);
344 bls_val->meas_gb =
345 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_C_MEASURED);
346 bls_val->meas_b =
347 rkisp1_read(rkisp1, RKISP1_CIF_ISP_BLS_D_MEASURED);
348 }
349 }
350
351 static const struct rkisp1_stats_ops rkisp1_v10_stats_ops = {
352 .get_awb_meas = rkisp1_stats_get_awb_meas_v10,
353 .get_aec_meas = rkisp1_stats_get_aec_meas_v10,
354 .get_hst_meas = rkisp1_stats_get_hst_meas_v10,
355 };
356
357 static struct rkisp1_stats_ops rkisp1_v12_stats_ops = {
358 .get_awb_meas = rkisp1_stats_get_awb_meas_v12,
359 .get_aec_meas = rkisp1_stats_get_aec_meas_v12,
360 .get_hst_meas = rkisp1_stats_get_hst_meas_v12,
361 };
362
363 static void
rkisp1_stats_send_measurement(struct rkisp1_stats * stats,u32 isp_ris)364 rkisp1_stats_send_measurement(struct rkisp1_stats *stats, u32 isp_ris)
365 {
366 struct rkisp1_stat_buffer *cur_stat_buf;
367 struct rkisp1_buffer *cur_buf = NULL;
368 unsigned int frame_sequence = stats->rkisp1->isp.frame_sequence;
369 u64 timestamp = ktime_get_ns();
370
371 /* get one empty buffer */
372 if (!list_empty(&stats->stat)) {
373 cur_buf = list_first_entry(&stats->stat,
374 struct rkisp1_buffer, queue);
375 list_del(&cur_buf->queue);
376 }
377
378 if (!cur_buf)
379 return;
380
381 cur_stat_buf = (struct rkisp1_stat_buffer *)
382 vb2_plane_vaddr(&cur_buf->vb.vb2_buf, 0);
383 if (isp_ris & RKISP1_CIF_ISP_AWB_DONE)
384 stats->ops->get_awb_meas(stats, cur_stat_buf);
385
386 if (isp_ris & RKISP1_CIF_ISP_AFM_FIN)
387 rkisp1_stats_get_afc_meas(stats, cur_stat_buf);
388
389 if (isp_ris & RKISP1_CIF_ISP_EXP_END) {
390 stats->ops->get_aec_meas(stats, cur_stat_buf);
391 rkisp1_stats_get_bls_meas(stats, cur_stat_buf);
392 }
393
394 if (isp_ris & RKISP1_CIF_ISP_HIST_MEASURE_RDY)
395 stats->ops->get_hst_meas(stats, cur_stat_buf);
396
397 vb2_set_plane_payload(&cur_buf->vb.vb2_buf, 0,
398 sizeof(struct rkisp1_stat_buffer));
399 cur_buf->vb.sequence = frame_sequence;
400 cur_buf->vb.vb2_buf.timestamp = timestamp;
401 vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
402 }
403
rkisp1_stats_isr(struct rkisp1_stats * stats,u32 isp_ris)404 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris)
405 {
406 struct rkisp1_device *rkisp1 = stats->rkisp1;
407 unsigned int isp_mis_tmp = 0;
408
409 spin_lock(&stats->lock);
410
411 rkisp1_write(rkisp1, RKISP1_STATS_MEAS_MASK, RKISP1_CIF_ISP_ICR);
412
413 isp_mis_tmp = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
414 if (isp_mis_tmp & RKISP1_STATS_MEAS_MASK)
415 rkisp1->debug.stats_error++;
416
417 if (isp_ris & RKISP1_STATS_MEAS_MASK)
418 rkisp1_stats_send_measurement(stats, isp_ris);
419
420 spin_unlock(&stats->lock);
421 }
422
rkisp1_init_stats(struct rkisp1_stats * stats)423 static void rkisp1_init_stats(struct rkisp1_stats *stats)
424 {
425 stats->vdev_fmt.fmt.meta.dataformat =
426 V4L2_META_FMT_RK_ISP1_STAT_3A;
427 stats->vdev_fmt.fmt.meta.buffersize =
428 sizeof(struct rkisp1_stat_buffer);
429
430 if (stats->rkisp1->media_dev.hw_revision == RKISP1_V12)
431 stats->ops = &rkisp1_v12_stats_ops;
432 else
433 stats->ops = &rkisp1_v10_stats_ops;
434 }
435
rkisp1_stats_register(struct rkisp1_device * rkisp1)436 int rkisp1_stats_register(struct rkisp1_device *rkisp1)
437 {
438 struct rkisp1_stats *stats = &rkisp1->stats;
439 struct rkisp1_vdev_node *node = &stats->vnode;
440 struct video_device *vdev = &node->vdev;
441 int ret;
442
443 stats->rkisp1 = rkisp1;
444 mutex_init(&node->vlock);
445 INIT_LIST_HEAD(&stats->stat);
446 spin_lock_init(&stats->lock);
447
448 strscpy(vdev->name, RKISP1_STATS_DEV_NAME, sizeof(vdev->name));
449
450 video_set_drvdata(vdev, stats);
451 vdev->ioctl_ops = &rkisp1_stats_ioctl;
452 vdev->fops = &rkisp1_stats_fops;
453 vdev->release = video_device_release_empty;
454 vdev->lock = &node->vlock;
455 vdev->v4l2_dev = &rkisp1->v4l2_dev;
456 vdev->queue = &node->buf_queue;
457 vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
458 vdev->vfl_dir = VFL_DIR_RX;
459 rkisp1_stats_init_vb2_queue(vdev->queue, stats);
460 rkisp1_init_stats(stats);
461 video_set_drvdata(vdev, stats);
462
463 node->pad.flags = MEDIA_PAD_FL_SINK;
464 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
465 if (ret)
466 goto err_mutex_destroy;
467
468 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
469 if (ret) {
470 dev_err(&vdev->dev,
471 "failed to register %s, ret=%d\n", vdev->name, ret);
472 goto err_cleanup_media_entity;
473 }
474
475 return 0;
476
477 err_cleanup_media_entity:
478 media_entity_cleanup(&vdev->entity);
479 err_mutex_destroy:
480 mutex_destroy(&node->vlock);
481 return ret;
482 }
483
rkisp1_stats_unregister(struct rkisp1_device * rkisp1)484 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1)
485 {
486 struct rkisp1_stats *stats = &rkisp1->stats;
487 struct rkisp1_vdev_node *node = &stats->vnode;
488 struct video_device *vdev = &node->vdev;
489
490 vb2_video_unregister_device(vdev);
491 media_entity_cleanup(&vdev->entity);
492 mutex_destroy(&node->vlock);
493 }
494