2 * Frame Interval Monitor.
4 * Copyright (c) 2016 Mentor Graphics Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #include <linux/delay.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-subdev.h>
19 #include <media/imx.h>
20 #include "imx-media.h"
34 FIM_NUM_ICAP_CONTROLS,
37 #define FIM_CL_ENABLE_DEF 0 /* FIM disabled by default */
38 #define FIM_CL_NUM_DEF 8 /* average 8 frames */
39 #define FIM_CL_NUM_SKIP_DEF 2 /* skip 2 frames after restart */
40 #define FIM_CL_TOLERANCE_MIN_DEF 50 /* usec */
41 #define FIM_CL_TOLERANCE_MAX_DEF 0 /* no max tolerance (unbounded) */
43 struct imx_media_fim {
44 struct imx_media_dev *md;
46 /* the owning subdev of this fim instance */
47 struct v4l2_subdev *sd;
49 /* FIM's control handler */
50 struct v4l2_ctrl_handler ctrl_handler;
52 /* control clusters */
53 struct v4l2_ctrl *ctrl[FIM_NUM_CONTROLS];
54 struct v4l2_ctrl *icap_ctrl[FIM_NUM_ICAP_CONTROLS];
56 spinlock_t lock; /* protect control values */
58 /* current control values */
62 unsigned long tolerance_min; /* usec */
63 unsigned long tolerance_max; /* usec */
64 /* input capture method of measuring FI */
70 unsigned long sum; /* usec */
71 unsigned long nominal; /* usec */
73 struct completion icap_first_event;
77 #define icap_enabled(fim) ((fim)->icap_flags != IRQ_TYPE_NONE)
79 static void update_fim_nominal(struct imx_media_fim *fim,
80 const struct v4l2_fract *fi)
82 if (fi->denominator == 0) {
83 dev_dbg(fim->sd->dev, "no frame interval, FIM disabled\n");
88 fim->nominal = DIV_ROUND_CLOSEST_ULL(1000000ULL * (u64)fi->numerator,
91 dev_dbg(fim->sd->dev, "FI=%lu usec\n", fim->nominal);
94 static void reset_fim(struct imx_media_fim *fim, bool curval)
96 struct v4l2_ctrl *icap_chan = fim->icap_ctrl[FIM_CL_ICAP_CHANNEL];
97 struct v4l2_ctrl *icap_edge = fim->icap_ctrl[FIM_CL_ICAP_EDGE];
98 struct v4l2_ctrl *en = fim->ctrl[FIM_CL_ENABLE];
99 struct v4l2_ctrl *num = fim->ctrl[FIM_CL_NUM];
100 struct v4l2_ctrl *skip = fim->ctrl[FIM_CL_NUM_SKIP];
101 struct v4l2_ctrl *tol_min = fim->ctrl[FIM_CL_TOLERANCE_MIN];
102 struct v4l2_ctrl *tol_max = fim->ctrl[FIM_CL_TOLERANCE_MAX];
105 fim->enabled = en->cur.val;
106 fim->icap_flags = icap_edge->cur.val;
107 fim->icap_channel = icap_chan->cur.val;
108 fim->num_avg = num->cur.val;
109 fim->num_skip = skip->cur.val;
110 fim->tolerance_min = tol_min->cur.val;
111 fim->tolerance_max = tol_max->cur.val;
113 fim->enabled = en->val;
114 fim->icap_flags = icap_edge->val;
115 fim->icap_channel = icap_chan->val;
116 fim->num_avg = num->val;
117 fim->num_skip = skip->val;
118 fim->tolerance_min = tol_min->val;
119 fim->tolerance_max = tol_max->val;
122 /* disable tolerance range if max <= min */
123 if (fim->tolerance_max <= fim->tolerance_min)
124 fim->tolerance_max = 0;
126 /* num_skip must be >= 1 if input capture not used */
127 if (!icap_enabled(fim))
128 fim->num_skip = max_t(int, fim->num_skip, 1);
130 fim->counter = -fim->num_skip;
134 static void send_fim_event(struct imx_media_fim *fim, unsigned long error)
136 static const struct v4l2_event ev = {
137 .type = V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR,
140 v4l2_subdev_notify_event(fim->sd, &ev);
144 * Monitor an averaged frame interval. If the average deviates too much
145 * from the nominal frame rate, send the frame interval error event. The
146 * frame intervals are averaged in order to quiet noise from
147 * (presumably random) interrupt latency.
149 static void frame_interval_monitor(struct imx_media_fim *fim,
152 long long interval, error;
153 unsigned long error_avg;
154 bool send_event = false;
156 if (!fim->enabled || ++fim->counter <= 0)
159 /* max error is less than l00µs, so use 32-bit division or fail */
160 interval = ktime_to_ns(ktime_sub(timestamp, fim->last_ts));
161 error = abs(interval - NSEC_PER_USEC * (u64)fim->nominal);
165 error = abs((u32)error / NSEC_PER_USEC);
167 if (fim->tolerance_max && error >= fim->tolerance_max) {
168 dev_dbg(fim->sd->dev,
169 "FIM: %llu ignored, out of tolerance bounds\n",
177 if (fim->counter == fim->num_avg) {
178 error_avg = DIV_ROUND_CLOSEST(fim->sum, fim->num_avg);
180 if (error_avg > fim->tolerance_min)
183 dev_dbg(fim->sd->dev, "FIM: error: %lu usec%s\n",
184 error_avg, send_event ? " (!!!)" : "");
191 fim->last_ts = timestamp;
193 send_fim_event(fim, error_avg);
196 #ifdef CONFIG_IMX_GPT_ICAP
198 * Input Capture method of measuring frame intervals. Not subject
199 * to interrupt latency.
201 static void fim_input_capture_handler(int channel, void *dev_id,
204 struct imx_media_fim *fim = dev_id;
207 spin_lock_irqsave(&fim->lock, flags);
209 frame_interval_monitor(fim, timestamp);
211 if (!completion_done(&fim->icap_first_event))
212 complete(&fim->icap_first_event);
214 spin_unlock_irqrestore(&fim->lock, flags);
217 static int fim_request_input_capture(struct imx_media_fim *fim)
219 init_completion(&fim->icap_first_event);
221 return mxc_request_input_capture(fim->icap_channel,
222 fim_input_capture_handler,
223 fim->icap_flags, fim);
226 static void fim_free_input_capture(struct imx_media_fim *fim)
228 mxc_free_input_capture(fim->icap_channel, fim);
231 #else /* CONFIG_IMX_GPT_ICAP */
233 static int fim_request_input_capture(struct imx_media_fim *fim)
238 static void fim_free_input_capture(struct imx_media_fim *fim)
242 #endif /* CONFIG_IMX_GPT_ICAP */
245 * In case we are monitoring the first frame interval after streamon
246 * (when fim->num_skip = 0), we need a valid fim->last_ts before we
247 * can begin. This only applies to the input capture method. It is not
248 * possible to accurately measure the first FI after streamon using the
249 * EOF method, so fim->num_skip minimum is set to 1 in that case, so this
250 * function is a noop when the EOF method is used.
252 static void fim_acquire_first_ts(struct imx_media_fim *fim)
256 if (!fim->enabled || fim->num_skip > 0)
259 ret = wait_for_completion_timeout(
260 &fim->icap_first_event,
261 msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
263 v4l2_warn(fim->sd, "wait first icap event timeout\n");
267 static int fim_s_ctrl(struct v4l2_ctrl *ctrl)
269 struct imx_media_fim *fim = container_of(ctrl->handler,
270 struct imx_media_fim,
275 spin_lock_irqsave(&fim->lock, flags);
278 case V4L2_CID_IMX_FIM_ENABLE:
280 case V4L2_CID_IMX_FIM_ICAP_EDGE:
289 reset_fim(fim, false);
291 spin_unlock_irqrestore(&fim->lock, flags);
295 static const struct v4l2_ctrl_ops fim_ctrl_ops = {
296 .s_ctrl = fim_s_ctrl,
299 static const struct v4l2_ctrl_config fim_ctrl[] = {
301 .ops = &fim_ctrl_ops,
302 .id = V4L2_CID_IMX_FIM_ENABLE,
303 .name = "FIM Enable",
304 .type = V4L2_CTRL_TYPE_BOOLEAN,
305 .def = FIM_CL_ENABLE_DEF,
311 .ops = &fim_ctrl_ops,
312 .id = V4L2_CID_IMX_FIM_NUM,
313 .name = "FIM Num Average",
314 .type = V4L2_CTRL_TYPE_INTEGER,
315 .def = FIM_CL_NUM_DEF,
316 .min = 1, /* no averaging */
317 .max = 64, /* average 64 frames */
320 [FIM_CL_TOLERANCE_MIN] = {
321 .ops = &fim_ctrl_ops,
322 .id = V4L2_CID_IMX_FIM_TOLERANCE_MIN,
323 .name = "FIM Tolerance Min",
324 .type = V4L2_CTRL_TYPE_INTEGER,
325 .def = FIM_CL_TOLERANCE_MIN_DEF,
330 [FIM_CL_TOLERANCE_MAX] = {
331 .ops = &fim_ctrl_ops,
332 .id = V4L2_CID_IMX_FIM_TOLERANCE_MAX,
333 .name = "FIM Tolerance Max",
334 .type = V4L2_CTRL_TYPE_INTEGER,
335 .def = FIM_CL_TOLERANCE_MAX_DEF,
340 [FIM_CL_NUM_SKIP] = {
341 .ops = &fim_ctrl_ops,
342 .id = V4L2_CID_IMX_FIM_NUM_SKIP,
343 .name = "FIM Num Skip",
344 .type = V4L2_CTRL_TYPE_INTEGER,
345 .def = FIM_CL_NUM_SKIP_DEF,
346 .min = 0, /* skip no frames */
347 .max = 256, /* skip 256 frames */
352 static const struct v4l2_ctrl_config fim_icap_ctrl[] = {
353 [FIM_CL_ICAP_EDGE] = {
354 .ops = &fim_ctrl_ops,
355 .id = V4L2_CID_IMX_FIM_ICAP_EDGE,
356 .name = "FIM Input Capture Edge",
357 .type = V4L2_CTRL_TYPE_INTEGER,
358 .def = IRQ_TYPE_NONE, /* input capture disabled by default */
359 .min = IRQ_TYPE_NONE,
360 .max = IRQ_TYPE_EDGE_BOTH,
363 [FIM_CL_ICAP_CHANNEL] = {
364 .ops = &fim_ctrl_ops,
365 .id = V4L2_CID_IMX_FIM_ICAP_CHANNEL,
366 .name = "FIM Input Capture Channel",
367 .type = V4L2_CTRL_TYPE_INTEGER,
375 static int init_fim_controls(struct imx_media_fim *fim)
377 struct v4l2_ctrl_handler *hdlr = &fim->ctrl_handler;
380 v4l2_ctrl_handler_init(hdlr, FIM_NUM_CONTROLS + FIM_NUM_ICAP_CONTROLS);
382 for (i = 0; i < FIM_NUM_CONTROLS; i++)
383 fim->ctrl[i] = v4l2_ctrl_new_custom(hdlr,
386 for (i = 0; i < FIM_NUM_ICAP_CONTROLS; i++)
387 fim->icap_ctrl[i] = v4l2_ctrl_new_custom(hdlr,
395 v4l2_ctrl_cluster(FIM_NUM_CONTROLS, fim->ctrl);
396 v4l2_ctrl_cluster(FIM_NUM_ICAP_CONTROLS, fim->icap_ctrl);
400 v4l2_ctrl_handler_free(hdlr);
405 * Monitor frame intervals via EOF interrupt. This method is
406 * subject to uncertainty errors introduced by interrupt latency.
408 * This is a noop if the Input Capture method is being used, since
409 * the frame_interval_monitor() is called by the input capture event
410 * callback handler in that case.
412 void imx_media_fim_eof_monitor(struct imx_media_fim *fim, ktime_t timestamp)
416 spin_lock_irqsave(&fim->lock, flags);
418 if (!icap_enabled(fim))
419 frame_interval_monitor(fim, timestamp);
421 spin_unlock_irqrestore(&fim->lock, flags);
423 EXPORT_SYMBOL_GPL(imx_media_fim_eof_monitor);
425 /* Called by the subdev in its s_stream callback */
426 int imx_media_fim_set_stream(struct imx_media_fim *fim,
427 const struct v4l2_fract *fi,
433 v4l2_ctrl_lock(fim->ctrl[FIM_CL_ENABLE]);
435 if (fim->stream_on == on)
439 spin_lock_irqsave(&fim->lock, flags);
440 reset_fim(fim, true);
441 update_fim_nominal(fim, fi);
442 spin_unlock_irqrestore(&fim->lock, flags);
444 if (icap_enabled(fim)) {
445 ret = fim_request_input_capture(fim);
448 fim_acquire_first_ts(fim);
451 if (icap_enabled(fim))
452 fim_free_input_capture(fim);
457 v4l2_ctrl_unlock(fim->ctrl[FIM_CL_ENABLE]);
460 EXPORT_SYMBOL_GPL(imx_media_fim_set_stream);
462 int imx_media_fim_add_controls(struct imx_media_fim *fim)
464 /* add the FIM controls to the calling subdev ctrl handler */
465 return v4l2_ctrl_add_handler(fim->sd->ctrl_handler,
466 &fim->ctrl_handler, NULL);
468 EXPORT_SYMBOL_GPL(imx_media_fim_add_controls);
470 /* Called by the subdev in its subdev registered callback */
471 struct imx_media_fim *imx_media_fim_init(struct v4l2_subdev *sd)
473 struct imx_media_fim *fim;
476 fim = devm_kzalloc(sd->dev, sizeof(*fim), GFP_KERNEL);
478 return ERR_PTR(-ENOMEM);
480 /* get media device */
481 fim->md = dev_get_drvdata(sd->v4l2_dev->dev);
484 spin_lock_init(&fim->lock);
486 ret = init_fim_controls(fim);
492 EXPORT_SYMBOL_GPL(imx_media_fim_init);
494 void imx_media_fim_free(struct imx_media_fim *fim)
496 v4l2_ctrl_handler_free(&fim->ctrl_handler);
498 EXPORT_SYMBOL_GPL(imx_media_fim_free);