perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / platform / vivid / vivid-sdr-cap.c
blobcfb7cb4d37a875b0715d1b4020a662c0338625eb
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vivid-sdr-cap.c - software defined radio support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/kthread.h>
12 #include <linux/freezer.h>
13 #include <linux/math64.h>
14 #include <linux/videodev2.h>
15 #include <linux/v4l2-dv-timings.h>
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-dv-timings.h>
19 #include <linux/fixp-arith.h>
21 #include "vivid-core.h"
22 #include "vivid-ctrls.h"
23 #include "vivid-sdr-cap.h"
25 /* stream formats */
26 struct vivid_format {
27 u32 pixelformat;
28 u32 buffersize;
31 /* format descriptions for capture and preview */
32 static const struct vivid_format formats[] = {
34 .pixelformat = V4L2_SDR_FMT_CU8,
35 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2,
36 }, {
37 .pixelformat = V4L2_SDR_FMT_CS8,
38 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2,
42 static const struct v4l2_frequency_band bands_adc[] = {
44 .tuner = 0,
45 .type = V4L2_TUNER_ADC,
46 .index = 0,
47 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
48 .rangelow = 300000,
49 .rangehigh = 300000,
52 .tuner = 0,
53 .type = V4L2_TUNER_ADC,
54 .index = 1,
55 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
56 .rangelow = 900001,
57 .rangehigh = 2800000,
60 .tuner = 0,
61 .type = V4L2_TUNER_ADC,
62 .index = 2,
63 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
64 .rangelow = 3200000,
65 .rangehigh = 3200000,
69 /* ADC band midpoints */
70 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
71 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
73 static const struct v4l2_frequency_band bands_fm[] = {
75 .tuner = 1,
76 .type = V4L2_TUNER_RF,
77 .index = 0,
78 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
79 .rangelow = 50000000,
80 .rangehigh = 2000000000,
84 static void vivid_thread_sdr_cap_tick(struct vivid_dev *dev)
86 struct vivid_buffer *sdr_cap_buf = NULL;
88 dprintk(dev, 1, "SDR Capture Thread Tick\n");
90 /* Drop a certain percentage of buffers. */
91 if (dev->perc_dropped_buffers &&
92 prandom_u32_max(100) < dev->perc_dropped_buffers)
93 return;
95 spin_lock(&dev->slock);
96 if (!list_empty(&dev->sdr_cap_active)) {
97 sdr_cap_buf = list_entry(dev->sdr_cap_active.next,
98 struct vivid_buffer, list);
99 list_del(&sdr_cap_buf->list);
101 spin_unlock(&dev->slock);
103 if (sdr_cap_buf) {
104 sdr_cap_buf->vb.sequence = dev->sdr_cap_seq_count;
105 vivid_sdr_cap_process(dev, sdr_cap_buf);
106 sdr_cap_buf->vb.vb2_buf.timestamp =
107 ktime_get_ns() + dev->time_wrap_offset;
108 vb2_buffer_done(&sdr_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
109 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
110 dev->dqbuf_error = false;
114 static int vivid_thread_sdr_cap(void *data)
116 struct vivid_dev *dev = data;
117 u64 samples_since_start;
118 u64 buffers_since_start;
119 u64 next_jiffies_since_start;
120 unsigned long jiffies_since_start;
121 unsigned long cur_jiffies;
122 unsigned wait_jiffies;
124 dprintk(dev, 1, "SDR Capture Thread Start\n");
126 set_freezable();
128 /* Resets frame counters */
129 dev->sdr_cap_seq_offset = 0;
130 if (dev->seq_wrap)
131 dev->sdr_cap_seq_offset = 0xffffff80U;
132 dev->jiffies_sdr_cap = jiffies;
133 dev->sdr_cap_seq_resync = false;
135 for (;;) {
136 try_to_freeze();
137 if (kthread_should_stop())
138 break;
140 mutex_lock(&dev->mutex);
141 cur_jiffies = jiffies;
142 if (dev->sdr_cap_seq_resync) {
143 dev->jiffies_sdr_cap = cur_jiffies;
144 dev->sdr_cap_seq_offset = dev->sdr_cap_seq_count + 1;
145 dev->sdr_cap_seq_count = 0;
146 dev->sdr_cap_seq_resync = false;
148 /* Calculate the number of jiffies since we started streaming */
149 jiffies_since_start = cur_jiffies - dev->jiffies_sdr_cap;
150 /* Get the number of buffers streamed since the start */
151 buffers_since_start =
152 (u64)jiffies_since_start * dev->sdr_adc_freq +
153 (HZ * SDR_CAP_SAMPLES_PER_BUF) / 2;
154 do_div(buffers_since_start, HZ * SDR_CAP_SAMPLES_PER_BUF);
157 * After more than 0xf0000000 (rounded down to a multiple of
158 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
159 * jiffies have passed since we started streaming reset the
160 * counters and keep track of the sequence offset.
162 if (jiffies_since_start > JIFFIES_RESYNC) {
163 dev->jiffies_sdr_cap = cur_jiffies;
164 dev->sdr_cap_seq_offset = buffers_since_start;
165 buffers_since_start = 0;
167 dev->sdr_cap_seq_count =
168 buffers_since_start + dev->sdr_cap_seq_offset;
170 vivid_thread_sdr_cap_tick(dev);
171 mutex_unlock(&dev->mutex);
174 * Calculate the number of samples streamed since we started,
175 * not including the current buffer.
177 samples_since_start = buffers_since_start * SDR_CAP_SAMPLES_PER_BUF;
179 /* And the number of jiffies since we started */
180 jiffies_since_start = jiffies - dev->jiffies_sdr_cap;
182 /* Increase by the number of samples in one buffer */
183 samples_since_start += SDR_CAP_SAMPLES_PER_BUF;
185 * Calculate when that next buffer is supposed to start
186 * in jiffies since we started streaming.
188 next_jiffies_since_start = samples_since_start * HZ +
189 dev->sdr_adc_freq / 2;
190 do_div(next_jiffies_since_start, dev->sdr_adc_freq);
191 /* If it is in the past, then just schedule asap */
192 if (next_jiffies_since_start < jiffies_since_start)
193 next_jiffies_since_start = jiffies_since_start;
195 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
196 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
198 dprintk(dev, 1, "SDR Capture Thread End\n");
199 return 0;
202 static int sdr_cap_queue_setup(struct vb2_queue *vq,
203 unsigned *nbuffers, unsigned *nplanes,
204 unsigned sizes[], struct device *alloc_devs[])
206 /* 2 = max 16-bit sample returned */
207 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
208 *nplanes = 1;
209 return 0;
212 static int sdr_cap_buf_prepare(struct vb2_buffer *vb)
214 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
215 unsigned size = SDR_CAP_SAMPLES_PER_BUF * 2;
217 dprintk(dev, 1, "%s\n", __func__);
219 if (dev->buf_prepare_error) {
221 * Error injection: test what happens if buf_prepare() returns
222 * an error.
224 dev->buf_prepare_error = false;
225 return -EINVAL;
227 if (vb2_plane_size(vb, 0) < size) {
228 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
229 __func__, vb2_plane_size(vb, 0), size);
230 return -EINVAL;
232 vb2_set_plane_payload(vb, 0, size);
234 return 0;
237 static void sdr_cap_buf_queue(struct vb2_buffer *vb)
239 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
240 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
241 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
243 dprintk(dev, 1, "%s\n", __func__);
245 spin_lock(&dev->slock);
246 list_add_tail(&buf->list, &dev->sdr_cap_active);
247 spin_unlock(&dev->slock);
250 static int sdr_cap_start_streaming(struct vb2_queue *vq, unsigned count)
252 struct vivid_dev *dev = vb2_get_drv_priv(vq);
253 int err = 0;
255 dprintk(dev, 1, "%s\n", __func__);
256 dev->sdr_cap_seq_count = 0;
257 if (dev->start_streaming_error) {
258 dev->start_streaming_error = false;
259 err = -EINVAL;
260 } else if (dev->kthread_sdr_cap == NULL) {
261 dev->kthread_sdr_cap = kthread_run(vivid_thread_sdr_cap, dev,
262 "%s-sdr-cap", dev->v4l2_dev.name);
264 if (IS_ERR(dev->kthread_sdr_cap)) {
265 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
266 err = PTR_ERR(dev->kthread_sdr_cap);
267 dev->kthread_sdr_cap = NULL;
270 if (err) {
271 struct vivid_buffer *buf, *tmp;
273 list_for_each_entry_safe(buf, tmp, &dev->sdr_cap_active, list) {
274 list_del(&buf->list);
275 vb2_buffer_done(&buf->vb.vb2_buf,
276 VB2_BUF_STATE_QUEUED);
279 return err;
282 /* abort streaming and wait for last buffer */
283 static void sdr_cap_stop_streaming(struct vb2_queue *vq)
285 struct vivid_dev *dev = vb2_get_drv_priv(vq);
287 if (dev->kthread_sdr_cap == NULL)
288 return;
290 while (!list_empty(&dev->sdr_cap_active)) {
291 struct vivid_buffer *buf;
293 buf = list_entry(dev->sdr_cap_active.next,
294 struct vivid_buffer, list);
295 list_del(&buf->list);
296 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
299 /* shutdown control thread */
300 mutex_unlock(&dev->mutex);
301 kthread_stop(dev->kthread_sdr_cap);
302 dev->kthread_sdr_cap = NULL;
303 mutex_lock(&dev->mutex);
306 const struct vb2_ops vivid_sdr_cap_qops = {
307 .queue_setup = sdr_cap_queue_setup,
308 .buf_prepare = sdr_cap_buf_prepare,
309 .buf_queue = sdr_cap_buf_queue,
310 .start_streaming = sdr_cap_start_streaming,
311 .stop_streaming = sdr_cap_stop_streaming,
312 .wait_prepare = vb2_ops_wait_prepare,
313 .wait_finish = vb2_ops_wait_finish,
316 int vivid_sdr_enum_freq_bands(struct file *file, void *fh,
317 struct v4l2_frequency_band *band)
319 switch (band->tuner) {
320 case 0:
321 if (band->index >= ARRAY_SIZE(bands_adc))
322 return -EINVAL;
323 *band = bands_adc[band->index];
324 return 0;
325 case 1:
326 if (band->index >= ARRAY_SIZE(bands_fm))
327 return -EINVAL;
328 *band = bands_fm[band->index];
329 return 0;
330 default:
331 return -EINVAL;
335 int vivid_sdr_g_frequency(struct file *file, void *fh,
336 struct v4l2_frequency *vf)
338 struct vivid_dev *dev = video_drvdata(file);
340 switch (vf->tuner) {
341 case 0:
342 vf->frequency = dev->sdr_adc_freq;
343 vf->type = V4L2_TUNER_ADC;
344 return 0;
345 case 1:
346 vf->frequency = dev->sdr_fm_freq;
347 vf->type = V4L2_TUNER_RF;
348 return 0;
349 default:
350 return -EINVAL;
354 int vivid_sdr_s_frequency(struct file *file, void *fh,
355 const struct v4l2_frequency *vf)
357 struct vivid_dev *dev = video_drvdata(file);
358 unsigned freq = vf->frequency;
359 unsigned band;
361 switch (vf->tuner) {
362 case 0:
363 if (vf->type != V4L2_TUNER_ADC)
364 return -EINVAL;
365 if (freq < BAND_ADC_0)
366 band = 0;
367 else if (freq < BAND_ADC_1)
368 band = 1;
369 else
370 band = 2;
372 freq = clamp_t(unsigned, freq,
373 bands_adc[band].rangelow,
374 bands_adc[band].rangehigh);
376 if (vb2_is_streaming(&dev->vb_sdr_cap_q) &&
377 freq != dev->sdr_adc_freq) {
378 /* resync the thread's timings */
379 dev->sdr_cap_seq_resync = true;
381 dev->sdr_adc_freq = freq;
382 return 0;
383 case 1:
384 if (vf->type != V4L2_TUNER_RF)
385 return -EINVAL;
386 dev->sdr_fm_freq = clamp_t(unsigned, freq,
387 bands_fm[0].rangelow,
388 bands_fm[0].rangehigh);
389 return 0;
390 default:
391 return -EINVAL;
395 int vivid_sdr_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
397 switch (vt->index) {
398 case 0:
399 strlcpy(vt->name, "ADC", sizeof(vt->name));
400 vt->type = V4L2_TUNER_ADC;
401 vt->capability =
402 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
403 vt->rangelow = bands_adc[0].rangelow;
404 vt->rangehigh = bands_adc[2].rangehigh;
405 return 0;
406 case 1:
407 strlcpy(vt->name, "RF", sizeof(vt->name));
408 vt->type = V4L2_TUNER_RF;
409 vt->capability =
410 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
411 vt->rangelow = bands_fm[0].rangelow;
412 vt->rangehigh = bands_fm[0].rangehigh;
413 return 0;
414 default:
415 return -EINVAL;
419 int vivid_sdr_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
421 if (vt->index > 1)
422 return -EINVAL;
423 return 0;
426 int vidioc_enum_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
428 if (f->index >= ARRAY_SIZE(formats))
429 return -EINVAL;
430 f->pixelformat = formats[f->index].pixelformat;
431 return 0;
434 int vidioc_g_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
436 struct vivid_dev *dev = video_drvdata(file);
438 f->fmt.sdr.pixelformat = dev->sdr_pixelformat;
439 f->fmt.sdr.buffersize = dev->sdr_buffersize;
440 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
441 return 0;
444 int vidioc_s_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
446 struct vivid_dev *dev = video_drvdata(file);
447 struct vb2_queue *q = &dev->vb_sdr_cap_q;
448 int i;
450 if (vb2_is_busy(q))
451 return -EBUSY;
453 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
454 for (i = 0; i < ARRAY_SIZE(formats); i++) {
455 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
456 dev->sdr_pixelformat = formats[i].pixelformat;
457 dev->sdr_buffersize = formats[i].buffersize;
458 f->fmt.sdr.buffersize = formats[i].buffersize;
459 return 0;
462 dev->sdr_pixelformat = formats[0].pixelformat;
463 dev->sdr_buffersize = formats[0].buffersize;
464 f->fmt.sdr.pixelformat = formats[0].pixelformat;
465 f->fmt.sdr.buffersize = formats[0].buffersize;
466 return 0;
469 int vidioc_try_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
471 int i;
473 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
474 for (i = 0; i < ARRAY_SIZE(formats); i++) {
475 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
476 f->fmt.sdr.buffersize = formats[i].buffersize;
477 return 0;
480 f->fmt.sdr.pixelformat = formats[0].pixelformat;
481 f->fmt.sdr.buffersize = formats[0].buffersize;
482 return 0;
485 #define FIXP_N (15)
486 #define FIXP_FRAC (1 << FIXP_N)
487 #define FIXP_2PI ((int)(2 * 3.141592653589 * FIXP_FRAC))
488 #define M_100000PI (3.14159 * 100000)
490 void vivid_sdr_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
492 u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
493 unsigned long i;
494 unsigned long plane_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
495 s64 s64tmp;
496 s32 src_phase_step;
497 s32 mod_phase_step;
498 s32 fixp_i;
499 s32 fixp_q;
501 /* calculate phase step */
502 #define BEEP_FREQ 1000 /* 1kHz beep */
503 src_phase_step = DIV_ROUND_CLOSEST(FIXP_2PI * BEEP_FREQ,
504 dev->sdr_adc_freq);
506 for (i = 0; i < plane_size; i += 2) {
507 mod_phase_step = fixp_cos32_rad(dev->sdr_fixp_src_phase,
508 FIXP_2PI) >> (31 - FIXP_N);
510 dev->sdr_fixp_src_phase += src_phase_step;
511 s64tmp = (s64) mod_phase_step * dev->sdr_fm_deviation;
512 dev->sdr_fixp_mod_phase += div_s64(s64tmp, M_100000PI);
515 * Transfer phase angle to [0, 2xPI] in order to avoid variable
516 * overflow and make it suitable for cosine implementation
517 * used, which does not support negative angles.
519 dev->sdr_fixp_src_phase %= FIXP_2PI;
520 dev->sdr_fixp_mod_phase %= FIXP_2PI;
522 if (dev->sdr_fixp_mod_phase < 0)
523 dev->sdr_fixp_mod_phase += FIXP_2PI;
525 fixp_i = fixp_cos32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
526 fixp_q = fixp_sin32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
528 /* Normalize fraction values represented with 32 bit precision
529 * to fixed point representation with FIXP_N bits */
530 fixp_i >>= (31 - FIXP_N);
531 fixp_q >>= (31 - FIXP_N);
533 switch (dev->sdr_pixelformat) {
534 case V4L2_SDR_FMT_CU8:
535 /* convert 'fixp float' to u8 [0, +255] */
536 /* u8 = X * 127.5 + 127.5; X is float [-1.0, +1.0] */
537 fixp_i = fixp_i * 1275 + FIXP_FRAC * 1275;
538 fixp_q = fixp_q * 1275 + FIXP_FRAC * 1275;
539 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
540 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);
541 break;
542 case V4L2_SDR_FMT_CS8:
543 /* convert 'fixp float' to s8 [-128, +127] */
544 /* s8 = X * 127.5 - 0.5; X is float [-1.0, +1.0] */
545 fixp_i = fixp_i * 1275 - FIXP_FRAC * 5;
546 fixp_q = fixp_q * 1275 - FIXP_FRAC * 5;
547 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
548 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);
549 break;
550 default:
551 break;