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 static bool icap_enabled(struct imx_media_fim *fim)
73 return fim->icap_flags != IRQ_TYPE_NONE;
76 static void update_fim_nominal(struct imx_media_fim *fim,
77 const struct v4l2_fract *fi)
79 if (fi->denominator == 0) {
80 dev_dbg(fim->sd->dev, "no frame interval, FIM disabled\n");
85 fim->nominal = DIV_ROUND_CLOSEST_ULL(1000000ULL * (u64)fi->numerator,
88 dev_dbg(fim->sd->dev, "FI=%lu usec\n", fim->nominal);
91 static void reset_fim(struct imx_media_fim *fim, bool curval)
93 struct v4l2_ctrl *icap_chan = fim->icap_ctrl[FIM_CL_ICAP_CHANNEL];
94 struct v4l2_ctrl *icap_edge = fim->icap_ctrl[FIM_CL_ICAP_EDGE];
95 struct v4l2_ctrl *en = fim->ctrl[FIM_CL_ENABLE];
96 struct v4l2_ctrl *num = fim->ctrl[FIM_CL_NUM];
97 struct v4l2_ctrl *skip = fim->ctrl[FIM_CL_NUM_SKIP];
98 struct v4l2_ctrl *tol_min = fim->ctrl[FIM_CL_TOLERANCE_MIN];
99 struct v4l2_ctrl *tol_max = fim->ctrl[FIM_CL_TOLERANCE_MAX];
102 fim->enabled = en->cur.val;
103 fim->icap_flags = icap_edge->cur.val;
104 fim->icap_channel = icap_chan->cur.val;
105 fim->num_avg = num->cur.val;
106 fim->num_skip = skip->cur.val;
107 fim->tolerance_min = tol_min->cur.val;
108 fim->tolerance_max = tol_max->cur.val;
110 fim->enabled = en->val;
111 fim->icap_flags = icap_edge->val;
112 fim->icap_channel = icap_chan->val;
113 fim->num_avg = num->val;
114 fim->num_skip = skip->val;
115 fim->tolerance_min = tol_min->val;
116 fim->tolerance_max = tol_max->val;
119 /* disable tolerance range if max <= min */
120 if (fim->tolerance_max <= fim->tolerance_min)
121 fim->tolerance_max = 0;
123 /* num_skip must be >= 1 if input capture not used */
124 if (!icap_enabled(fim))
125 fim->num_skip = max_t(int, fim->num_skip, 1);
127 fim->counter = -fim->num_skip;
131 static void send_fim_event(struct imx_media_fim *fim, unsigned long error)
133 static const struct v4l2_event ev = {
134 .type = V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR,
137 v4l2_subdev_notify_event(fim->sd, &ev);
141 * Monitor an averaged frame interval. If the average deviates too much
142 * from the nominal frame rate, send the frame interval error event. The
143 * frame intervals are averaged in order to quiet noise from
144 * (presumably random) interrupt latency.
146 static void frame_interval_monitor(struct imx_media_fim *fim,
149 long long interval, error;
150 unsigned long error_avg;
151 bool send_event = false;
153 if (!fim->enabled || ++fim->counter <= 0)
156 /* max error is less than l00µs, so use 32-bit division or fail */
157 interval = ktime_to_ns(ktime_sub(timestamp, fim->last_ts));
158 error = abs(interval - NSEC_PER_USEC * (u64)fim->nominal);
162 error = abs((u32)error / NSEC_PER_USEC);
164 if (fim->tolerance_max && error >= fim->tolerance_max) {
165 dev_dbg(fim->sd->dev,
166 "FIM: %llu ignored, out of tolerance bounds\n",
174 if (fim->counter == fim->num_avg) {
175 error_avg = DIV_ROUND_CLOSEST(fim->sum, fim->num_avg);
177 if (error_avg > fim->tolerance_min)
180 dev_dbg(fim->sd->dev, "FIM: error: %lu usec%s\n",
181 error_avg, send_event ? " (!!!)" : "");
188 fim->last_ts = timestamp;
190 send_fim_event(fim, error_avg);
194 * In case we are monitoring the first frame interval after streamon
195 * (when fim->num_skip = 0), we need a valid fim->last_ts before we
196 * can begin. This only applies to the input capture method. It is not
197 * possible to accurately measure the first FI after streamon using the
198 * EOF method, so fim->num_skip minimum is set to 1 in that case, so this
199 * function is a noop when the EOF method is used.
201 static void fim_acquire_first_ts(struct imx_media_fim *fim)
205 if (!fim->enabled || fim->num_skip > 0)
208 ret = wait_for_completion_timeout(
209 &fim->icap_first_event,
210 msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
212 v4l2_warn(fim->sd, "wait first icap event timeout\n");
216 static int fim_s_ctrl(struct v4l2_ctrl *ctrl)
218 struct imx_media_fim *fim = container_of(ctrl->handler,
219 struct imx_media_fim,
224 spin_lock_irqsave(&fim->lock, flags);
227 case V4L2_CID_IMX_FIM_ENABLE:
229 case V4L2_CID_IMX_FIM_ICAP_EDGE:
238 reset_fim(fim, false);
240 spin_unlock_irqrestore(&fim->lock, flags);
244 static const struct v4l2_ctrl_ops fim_ctrl_ops = {
245 .s_ctrl = fim_s_ctrl,
248 static const struct v4l2_ctrl_config fim_ctrl[] = {
250 .ops = &fim_ctrl_ops,
251 .id = V4L2_CID_IMX_FIM_ENABLE,
252 .name = "FIM Enable",
253 .type = V4L2_CTRL_TYPE_BOOLEAN,
254 .def = FIM_CL_ENABLE_DEF,
260 .ops = &fim_ctrl_ops,
261 .id = V4L2_CID_IMX_FIM_NUM,
262 .name = "FIM Num Average",
263 .type = V4L2_CTRL_TYPE_INTEGER,
264 .def = FIM_CL_NUM_DEF,
265 .min = 1, /* no averaging */
266 .max = 64, /* average 64 frames */
269 [FIM_CL_TOLERANCE_MIN] = {
270 .ops = &fim_ctrl_ops,
271 .id = V4L2_CID_IMX_FIM_TOLERANCE_MIN,
272 .name = "FIM Tolerance Min",
273 .type = V4L2_CTRL_TYPE_INTEGER,
274 .def = FIM_CL_TOLERANCE_MIN_DEF,
279 [FIM_CL_TOLERANCE_MAX] = {
280 .ops = &fim_ctrl_ops,
281 .id = V4L2_CID_IMX_FIM_TOLERANCE_MAX,
282 .name = "FIM Tolerance Max",
283 .type = V4L2_CTRL_TYPE_INTEGER,
284 .def = FIM_CL_TOLERANCE_MAX_DEF,
289 [FIM_CL_NUM_SKIP] = {
290 .ops = &fim_ctrl_ops,
291 .id = V4L2_CID_IMX_FIM_NUM_SKIP,
292 .name = "FIM Num Skip",
293 .type = V4L2_CTRL_TYPE_INTEGER,
294 .def = FIM_CL_NUM_SKIP_DEF,
295 .min = 0, /* skip no frames */
296 .max = 256, /* skip 256 frames */
301 static const struct v4l2_ctrl_config fim_icap_ctrl[] = {
302 [FIM_CL_ICAP_EDGE] = {
303 .ops = &fim_ctrl_ops,
304 .id = V4L2_CID_IMX_FIM_ICAP_EDGE,
305 .name = "FIM Input Capture Edge",
306 .type = V4L2_CTRL_TYPE_INTEGER,
307 .def = IRQ_TYPE_NONE, /* input capture disabled by default */
308 .min = IRQ_TYPE_NONE,
309 .max = IRQ_TYPE_EDGE_BOTH,
312 [FIM_CL_ICAP_CHANNEL] = {
313 .ops = &fim_ctrl_ops,
314 .id = V4L2_CID_IMX_FIM_ICAP_CHANNEL,
315 .name = "FIM Input Capture Channel",
316 .type = V4L2_CTRL_TYPE_INTEGER,
324 static int init_fim_controls(struct imx_media_fim *fim)
326 struct v4l2_ctrl_handler *hdlr = &fim->ctrl_handler;
329 v4l2_ctrl_handler_init(hdlr, FIM_NUM_CONTROLS + FIM_NUM_ICAP_CONTROLS);
331 for (i = 0; i < FIM_NUM_CONTROLS; i++)
332 fim->ctrl[i] = v4l2_ctrl_new_custom(hdlr,
335 for (i = 0; i < FIM_NUM_ICAP_CONTROLS; i++)
336 fim->icap_ctrl[i] = v4l2_ctrl_new_custom(hdlr,
344 v4l2_ctrl_cluster(FIM_NUM_CONTROLS, fim->ctrl);
345 v4l2_ctrl_cluster(FIM_NUM_ICAP_CONTROLS, fim->icap_ctrl);
349 v4l2_ctrl_handler_free(hdlr);
354 * Monitor frame intervals via EOF interrupt. This method is
355 * subject to uncertainty errors introduced by interrupt latency.
357 * This is a noop if the Input Capture method is being used, since
358 * the frame_interval_monitor() is called by the input capture event
359 * callback handler in that case.
361 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp)
365 spin_lock_irqsave(&fim->lock, flags);
367 if (!icap_enabled(fim))
368 frame_interval_monitor(fim, timestamp);
370 spin_unlock_irqrestore(&fim->lock, flags);
373 /* Called by the subdev in its s_stream callback */
374 void imx_media_fim_set_stream(struct imx_media_fim *fim,
375 const struct v4l2_fract *fi,
380 v4l2_ctrl_lock(fim->ctrl[FIM_CL_ENABLE]);
382 if (fim->stream_on == on)
386 spin_lock_irqsave(&fim->lock, flags);
387 reset_fim(fim, true);
388 update_fim_nominal(fim, fi);
389 spin_unlock_irqrestore(&fim->lock, flags);
391 if (icap_enabled(fim))
392 fim_acquire_first_ts(fim);
397 v4l2_ctrl_unlock(fim->ctrl[FIM_CL_ENABLE]);
400 int imx_media_fim_add_controls(struct imx_media_fim *fim)
402 /* add the FIM controls to the calling subdev ctrl handler */
403 return v4l2_ctrl_add_handler(fim->sd->ctrl_handler,
404 &fim->ctrl_handler, NULL, false);
407 /* Called by the subdev in its subdev registered callback */
408 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd)
410 struct imx_media_fim *fim;
413 fim = devm_kzalloc(sd->dev, sizeof(*fim), GFP_KERNEL);
415 return ERR_PTR(-ENOMEM);
419 spin_lock_init(&fim->lock);
421 ret = init_fim_controls(fim);
428 void imx_media_fim_free(struct imx_media_fim *fim)
430 v4l2_ctrl_handler_free(&fim->ctrl_handler);