1 // SPDX-License-Identifier: GPL-2.0+
3 * Frame Interval Monitor.
5 * Copyright (c) 2016 Mentor Graphics Inc.
7 #include <linux/delay.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-subdev.h>
15 #include <media/imx.h>
16 #include "imx-media.h"
30 FIM_NUM_ICAP_CONTROLS,
33 #define FIM_CL_ENABLE_DEF 0 /* FIM disabled by default */
34 #define FIM_CL_NUM_DEF 8 /* average 8 frames */
35 #define FIM_CL_NUM_SKIP_DEF 2 /* skip 2 frames after restart */
36 #define FIM_CL_TOLERANCE_MIN_DEF 50 /* usec */
37 #define FIM_CL_TOLERANCE_MAX_DEF 0 /* no max tolerance (unbounded) */
39 struct imx_media_fim {
40 /* the owning subdev of this fim instance */
41 struct v4l2_subdev *sd;
43 /* FIM's control handler */
44 struct v4l2_ctrl_handler ctrl_handler;
46 /* control clusters */
47 struct v4l2_ctrl *ctrl[FIM_NUM_CONTROLS];
48 struct v4l2_ctrl *icap_ctrl[FIM_NUM_ICAP_CONTROLS];
50 spinlock_t lock; /* protect control values */
52 /* current control values */
56 unsigned long tolerance_min; /* usec */
57 unsigned long tolerance_max; /* usec */
58 /* input capture method of measuring FI */
64 unsigned long sum; /* usec */
65 unsigned long nominal; /* usec */
67 struct completion icap_first_event;
71 #define icap_enabled(fim) ((fim)->icap_flags != IRQ_TYPE_NONE)
73 static void update_fim_nominal(struct imx_media_fim *fim,
74 const struct v4l2_fract *fi)
76 if (fi->denominator == 0) {
77 dev_dbg(fim->sd->dev, "no frame interval, FIM disabled\n");
82 fim->nominal = DIV_ROUND_CLOSEST_ULL(1000000ULL * (u64)fi->numerator,
85 dev_dbg(fim->sd->dev, "FI=%lu usec\n", fim->nominal);
88 static void reset_fim(struct imx_media_fim *fim, bool curval)
90 struct v4l2_ctrl *icap_chan = fim->icap_ctrl[FIM_CL_ICAP_CHANNEL];
91 struct v4l2_ctrl *icap_edge = fim->icap_ctrl[FIM_CL_ICAP_EDGE];
92 struct v4l2_ctrl *en = fim->ctrl[FIM_CL_ENABLE];
93 struct v4l2_ctrl *num = fim->ctrl[FIM_CL_NUM];
94 struct v4l2_ctrl *skip = fim->ctrl[FIM_CL_NUM_SKIP];
95 struct v4l2_ctrl *tol_min = fim->ctrl[FIM_CL_TOLERANCE_MIN];
96 struct v4l2_ctrl *tol_max = fim->ctrl[FIM_CL_TOLERANCE_MAX];
99 fim->enabled = en->cur.val;
100 fim->icap_flags = icap_edge->cur.val;
101 fim->icap_channel = icap_chan->cur.val;
102 fim->num_avg = num->cur.val;
103 fim->num_skip = skip->cur.val;
104 fim->tolerance_min = tol_min->cur.val;
105 fim->tolerance_max = tol_max->cur.val;
107 fim->enabled = en->val;
108 fim->icap_flags = icap_edge->val;
109 fim->icap_channel = icap_chan->val;
110 fim->num_avg = num->val;
111 fim->num_skip = skip->val;
112 fim->tolerance_min = tol_min->val;
113 fim->tolerance_max = tol_max->val;
116 /* disable tolerance range if max <= min */
117 if (fim->tolerance_max <= fim->tolerance_min)
118 fim->tolerance_max = 0;
120 /* num_skip must be >= 1 if input capture not used */
121 if (!icap_enabled(fim))
122 fim->num_skip = max_t(int, fim->num_skip, 1);
124 fim->counter = -fim->num_skip;
128 static void send_fim_event(struct imx_media_fim *fim, unsigned long error)
130 static const struct v4l2_event ev = {
131 .type = V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR,
134 v4l2_subdev_notify_event(fim->sd, &ev);
138 * Monitor an averaged frame interval. If the average deviates too much
139 * from the nominal frame rate, send the frame interval error event. The
140 * frame intervals are averaged in order to quiet noise from
141 * (presumably random) interrupt latency.
143 static void frame_interval_monitor(struct imx_media_fim *fim,
146 long long interval, error;
147 unsigned long error_avg;
148 bool send_event = false;
150 if (!fim->enabled || ++fim->counter <= 0)
153 /* max error is less than l00µs, so use 32-bit division or fail */
154 interval = ktime_to_ns(ktime_sub(timestamp, fim->last_ts));
155 error = abs(interval - NSEC_PER_USEC * (u64)fim->nominal);
159 error = abs((u32)error / NSEC_PER_USEC);
161 if (fim->tolerance_max && error >= fim->tolerance_max) {
162 dev_dbg(fim->sd->dev,
163 "FIM: %llu ignored, out of tolerance bounds\n",
171 if (fim->counter == fim->num_avg) {
172 error_avg = DIV_ROUND_CLOSEST(fim->sum, fim->num_avg);
174 if (error_avg > fim->tolerance_min)
177 dev_dbg(fim->sd->dev, "FIM: error: %lu usec%s\n",
178 error_avg, send_event ? " (!!!)" : "");
185 fim->last_ts = timestamp;
187 send_fim_event(fim, error_avg);
190 #ifdef CONFIG_IMX_GPT_ICAP
192 * Input Capture method of measuring frame intervals. Not subject
193 * to interrupt latency.
195 static void fim_input_capture_handler(int channel, void *dev_id,
198 struct imx_media_fim *fim = dev_id;
201 spin_lock_irqsave(&fim->lock, flags);
203 frame_interval_monitor(fim, timestamp);
205 if (!completion_done(&fim->icap_first_event))
206 complete(&fim->icap_first_event);
208 spin_unlock_irqrestore(&fim->lock, flags);
211 static int fim_request_input_capture(struct imx_media_fim *fim)
213 init_completion(&fim->icap_first_event);
215 return mxc_request_input_capture(fim->icap_channel,
216 fim_input_capture_handler,
217 fim->icap_flags, fim);
220 static void fim_free_input_capture(struct imx_media_fim *fim)
222 mxc_free_input_capture(fim->icap_channel, fim);
225 #else /* CONFIG_IMX_GPT_ICAP */
227 static int fim_request_input_capture(struct imx_media_fim *fim)
232 static void fim_free_input_capture(struct imx_media_fim *fim)
236 #endif /* CONFIG_IMX_GPT_ICAP */
239 * In case we are monitoring the first frame interval after streamon
240 * (when fim->num_skip = 0), we need a valid fim->last_ts before we
241 * can begin. This only applies to the input capture method. It is not
242 * possible to accurately measure the first FI after streamon using the
243 * EOF method, so fim->num_skip minimum is set to 1 in that case, so this
244 * function is a noop when the EOF method is used.
246 static void fim_acquire_first_ts(struct imx_media_fim *fim)
250 if (!fim->enabled || fim->num_skip > 0)
253 ret = wait_for_completion_timeout(
254 &fim->icap_first_event,
255 msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
257 v4l2_warn(fim->sd, "wait first icap event timeout\n");
261 static int fim_s_ctrl(struct v4l2_ctrl *ctrl)
263 struct imx_media_fim *fim = container_of(ctrl->handler,
264 struct imx_media_fim,
269 spin_lock_irqsave(&fim->lock, flags);
272 case V4L2_CID_IMX_FIM_ENABLE:
274 case V4L2_CID_IMX_FIM_ICAP_EDGE:
283 reset_fim(fim, false);
285 spin_unlock_irqrestore(&fim->lock, flags);
289 static const struct v4l2_ctrl_ops fim_ctrl_ops = {
290 .s_ctrl = fim_s_ctrl,
293 static const struct v4l2_ctrl_config fim_ctrl[] = {
295 .ops = &fim_ctrl_ops,
296 .id = V4L2_CID_IMX_FIM_ENABLE,
297 .name = "FIM Enable",
298 .type = V4L2_CTRL_TYPE_BOOLEAN,
299 .def = FIM_CL_ENABLE_DEF,
305 .ops = &fim_ctrl_ops,
306 .id = V4L2_CID_IMX_FIM_NUM,
307 .name = "FIM Num Average",
308 .type = V4L2_CTRL_TYPE_INTEGER,
309 .def = FIM_CL_NUM_DEF,
310 .min = 1, /* no averaging */
311 .max = 64, /* average 64 frames */
314 [FIM_CL_TOLERANCE_MIN] = {
315 .ops = &fim_ctrl_ops,
316 .id = V4L2_CID_IMX_FIM_TOLERANCE_MIN,
317 .name = "FIM Tolerance Min",
318 .type = V4L2_CTRL_TYPE_INTEGER,
319 .def = FIM_CL_TOLERANCE_MIN_DEF,
324 [FIM_CL_TOLERANCE_MAX] = {
325 .ops = &fim_ctrl_ops,
326 .id = V4L2_CID_IMX_FIM_TOLERANCE_MAX,
327 .name = "FIM Tolerance Max",
328 .type = V4L2_CTRL_TYPE_INTEGER,
329 .def = FIM_CL_TOLERANCE_MAX_DEF,
334 [FIM_CL_NUM_SKIP] = {
335 .ops = &fim_ctrl_ops,
336 .id = V4L2_CID_IMX_FIM_NUM_SKIP,
337 .name = "FIM Num Skip",
338 .type = V4L2_CTRL_TYPE_INTEGER,
339 .def = FIM_CL_NUM_SKIP_DEF,
340 .min = 0, /* skip no frames */
341 .max = 256, /* skip 256 frames */
346 static const struct v4l2_ctrl_config fim_icap_ctrl[] = {
347 [FIM_CL_ICAP_EDGE] = {
348 .ops = &fim_ctrl_ops,
349 .id = V4L2_CID_IMX_FIM_ICAP_EDGE,
350 .name = "FIM Input Capture Edge",
351 .type = V4L2_CTRL_TYPE_INTEGER,
352 .def = IRQ_TYPE_NONE, /* input capture disabled by default */
353 .min = IRQ_TYPE_NONE,
354 .max = IRQ_TYPE_EDGE_BOTH,
357 [FIM_CL_ICAP_CHANNEL] = {
358 .ops = &fim_ctrl_ops,
359 .id = V4L2_CID_IMX_FIM_ICAP_CHANNEL,
360 .name = "FIM Input Capture Channel",
361 .type = V4L2_CTRL_TYPE_INTEGER,
369 static int init_fim_controls(struct imx_media_fim *fim)
371 struct v4l2_ctrl_handler *hdlr = &fim->ctrl_handler;
374 v4l2_ctrl_handler_init(hdlr, FIM_NUM_CONTROLS + FIM_NUM_ICAP_CONTROLS);
376 for (i = 0; i < FIM_NUM_CONTROLS; i++)
377 fim->ctrl[i] = v4l2_ctrl_new_custom(hdlr,
380 for (i = 0; i < FIM_NUM_ICAP_CONTROLS; i++)
381 fim->icap_ctrl[i] = v4l2_ctrl_new_custom(hdlr,
389 v4l2_ctrl_cluster(FIM_NUM_CONTROLS, fim->ctrl);
390 v4l2_ctrl_cluster(FIM_NUM_ICAP_CONTROLS, fim->icap_ctrl);
394 v4l2_ctrl_handler_free(hdlr);
399 * Monitor frame intervals via EOF interrupt. This method is
400 * subject to uncertainty errors introduced by interrupt latency.
402 * This is a noop if the Input Capture method is being used, since
403 * the frame_interval_monitor() is called by the input capture event
404 * callback handler in that case.
406 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp)
410 spin_lock_irqsave(&fim->lock, flags);
412 if (!icap_enabled(fim))
413 frame_interval_monitor(fim, timestamp);
415 spin_unlock_irqrestore(&fim->lock, flags);
418 /* Called by the subdev in its s_stream callback */
419 int imx_media_fim_set_stream(struct imx_media_fim *fim,
420 const struct v4l2_fract *fi,
426 v4l2_ctrl_lock(fim->ctrl[FIM_CL_ENABLE]);
428 if (fim->stream_on == on)
432 spin_lock_irqsave(&fim->lock, flags);
433 reset_fim(fim, true);
434 update_fim_nominal(fim, fi);
435 spin_unlock_irqrestore(&fim->lock, flags);
437 if (icap_enabled(fim)) {
438 ret = fim_request_input_capture(fim);
441 fim_acquire_first_ts(fim);
444 if (icap_enabled(fim))
445 fim_free_input_capture(fim);
450 v4l2_ctrl_unlock(fim->ctrl[FIM_CL_ENABLE]);
454 int imx_media_fim_add_controls(struct imx_media_fim *fim)
456 /* add the FIM controls to the calling subdev ctrl handler */
457 return v4l2_ctrl_add_handler(fim->sd->ctrl_handler,
458 &fim->ctrl_handler, NULL, false);
461 /* Called by the subdev in its subdev registered callback */
462 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd)
464 struct imx_media_fim *fim;
467 fim = devm_kzalloc(sd->dev, sizeof(*fim), GFP_KERNEL);
469 return ERR_PTR(-ENOMEM);
473 spin_lock_init(&fim->lock);
475 ret = init_fim_controls(fim);
482 void imx_media_fim_free(struct imx_media_fim *fim)
484 v4l2_ctrl_handler_free(&fim->ctrl_handler);