1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Frame Interval Monitor.
*
* Copyright (c) 2016 Mentor Graphics Inc.
*/
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-subdev.h>
#include <media/imx.h>
#include "imx-media.h"
enum {
FIM_CL_ENABLE = 0,
FIM_CL_NUM,
FIM_CL_TOLERANCE_MIN,
FIM_CL_TOLERANCE_MAX,
FIM_CL_NUM_SKIP,
FIM_NUM_CONTROLS,
};
enum {
FIM_CL_ICAP_EDGE = 0,
FIM_CL_ICAP_CHANNEL,
FIM_NUM_ICAP_CONTROLS,
};
#define FIM_CL_ENABLE_DEF 0 /* FIM disabled by default */
#define FIM_CL_NUM_DEF 8 /* average 8 frames */
#define FIM_CL_NUM_SKIP_DEF 2 /* skip 2 frames after restart */
#define FIM_CL_TOLERANCE_MIN_DEF 50 /* usec */
#define FIM_CL_TOLERANCE_MAX_DEF 0 /* no max tolerance (unbounded) */
struct imx_media_fim {
/* the owning subdev of this fim instance */
struct v4l2_subdev *sd;
/* FIM's control handler */
struct v4l2_ctrl_handler ctrl_handler;
/* control clusters */
struct v4l2_ctrl *ctrl[FIM_NUM_CONTROLS];
struct v4l2_ctrl *icap_ctrl[FIM_NUM_ICAP_CONTROLS];
spinlock_t lock; /* protect control values */
/* current control values */
bool enabled;
int num_avg;
int num_skip;
unsigned long tolerance_min; /* usec */
unsigned long tolerance_max; /* usec */
/* input capture method of measuring FI */
int icap_channel;
int icap_flags;
int counter;
ktime_t last_ts;
unsigned long sum; /* usec */
unsigned long nominal; /* usec */
struct completion icap_first_event;
bool stream_on;
};
#define icap_enabled(fim) ((fim)->icap_flags != IRQ_TYPE_NONE)
static void update_fim_nominal(struct imx_media_fim *fim,
const struct v4l2_fract *fi)
{
if (fi->denominator == 0) {
dev_dbg(fim->sd->dev, "no frame interval, FIM disabled\n");
fim->enabled = false;
return;
}
fim->nominal = DIV_ROUND_CLOSEST_ULL(1000000ULL * (u64)fi->numerator,
fi->denominator);
dev_dbg(fim->sd->dev, "FI=%lu usec\n", fim->nominal);
}
static void reset_fim(struct imx_media_fim *fim, bool curval)
{
struct v4l2_ctrl *icap_chan = fim->icap_ctrl[FIM_CL_ICAP_CHANNEL];
struct v4l2_ctrl *icap_edge = fim->icap_ctrl[FIM_CL_ICAP_EDGE];
struct v4l2_ctrl *en = fim->ctrl[FIM_CL_ENABLE];
struct v4l2_ctrl *num = fim->ctrl[FIM_CL_NUM];
struct v4l2_ctrl *skip = fim->ctrl[FIM_CL_NUM_SKIP];
struct v4l2_ctrl *tol_min = fim->ctrl[FIM_CL_TOLERANCE_MIN];
struct v4l2_ctrl *tol_max = fim->ctrl[FIM_CL_TOLERANCE_MAX];
if (curval) {
fim->enabled = en->cur.val;
fim->icap_flags = icap_edge->cur.val;
fim->icap_channel = icap_chan->cur.val;
fim->num_avg = num->cur.val;
fim->num_skip = skip->cur.val;
fim->tolerance_min = tol_min->cur.val;
fim->tolerance_max = tol_max->cur.val;
} else {
fim->enabled = en->val;
fim->icap_flags = icap_edge->val;
fim->icap_channel = icap_chan->val;
fim->num_avg = num->val;
fim->num_skip = skip->val;
fim->tolerance_min = tol_min->val;
fim->tolerance_max = tol_max->val;
}
/* disable tolerance range if max <= min */
if (fim->tolerance_max <= fim->tolerance_min)
fim->tolerance_max = 0;
/* num_skip must be >= 1 if input capture not used */
if (!icap_enabled(fim))
fim->num_skip = max_t(int, fim->num_skip, 1);
fim->counter = -fim->num_skip;
fim->sum = 0;
}
static void send_fim_event(struct imx_media_fim *fim, unsigned long error)
{
static const struct v4l2_event ev = {
.type = V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR,
};
v4l2_subdev_notify_event(fim->sd, &ev);
}
/*
* Monitor an averaged frame interval. If the average deviates too much
* from the nominal frame rate, send the frame interval error event. The
* frame intervals are averaged in order to quiet noise from
* (presumably random) interrupt latency.
*/
static void frame_interval_monitor(struct imx_media_fim *fim,
ktime_t timestamp)
{
long long interval, error;
unsigned long error_avg;
bool send_event = false;
if (!fim->enabled || ++fim->counter <= 0)
goto out_update_ts;
/* max error is less than l00µs, so use 32-bit division or fail */
interval = ktime_to_ns(ktime_sub(timestamp, fim->last_ts));
error = abs(interval - NSEC_PER_USEC * (u64)fim->nominal);
if (error > U32_MAX)
error = U32_MAX;
else
error = abs((u32)error / NSEC_PER_USEC);
if (fim->tolerance_max && error >= fim->tolerance_max) {
dev_dbg(fim->sd->dev,
"FIM: %llu ignored, out of tolerance bounds\n",
error);
fim->counter--;
goto out_update_ts;
}
fim->sum += error;
if (fim->counter == fim->num_avg) {
error_avg = DIV_ROUND_CLOSEST(fim->sum, fim->num_avg);
if (error_avg > fim->tolerance_min)
send_event = true;
dev_dbg(fim->sd->dev, "FIM: error: %lu usec%s\n",
error_avg, send_event ? " (!!!)" : "");
fim->counter = 0;
fim->sum = 0;
}
out_update_ts:
fim->last_ts = timestamp;
if (send_event)
send_fim_event(fim, error_avg);
}
#ifdef CONFIG_IMX_GPT_ICAP
/*
* Input Capture method of measuring frame intervals. Not subject
* to interrupt latency.
*/
static void fim_input_capture_handler(int channel, void *dev_id,
ktime_t timestamp)
{
struct imx_media_fim *fim = dev_id;
unsigned long flags;
spin_lock_irqsave(&fim->lock, flags);
frame_interval_monitor(fim, timestamp);
if (!completion_done(&fim->icap_first_event))
complete(&fim->icap_first_event);
spin_unlock_irqrestore(&fim->lock, flags);
}
static int fim_request_input_capture(struct imx_media_fim *fim)
{
init_completion(&fim->icap_first_event);
return mxc_request_input_capture(fim->icap_channel,
fim_input_capture_handler,
fim->icap_flags, fim);
}
static void fim_free_input_capture(struct imx_media_fim *fim)
{
mxc_free_input_capture(fim->icap_channel, fim);
}
#else /* CONFIG_IMX_GPT_ICAP */
static int fim_request_input_capture(struct imx_media_fim *fim)
{
return 0;
}
static void fim_free_input_capture(struct imx_media_fim *fim)
{
}
#endif /* CONFIG_IMX_GPT_ICAP */
/*
* In case we are monitoring the first frame interval after streamon
* (when fim->num_skip = 0), we need a valid fim->last_ts before we
* can begin. This only applies to the input capture method. It is not
* possible to accurately measure the first FI after streamon using the
* EOF method, so fim->num_skip minimum is set to 1 in that case, so this
* function is a noop when the EOF method is used.
*/
static void fim_acquire_first_ts(struct imx_media_fim *fim)
{
unsigned long ret;
if (!fim->enabled || fim->num_skip > 0)
return;
ret = wait_for_completion_timeout(
&fim->icap_first_event,
msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
if (ret == 0)
v4l2_warn(fim->sd, "wait first icap event timeout\n");<
|