GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / platform / vivid / vivid-sdr-cap.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-sdr-cap.c - software defined radio support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7
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>
20
21 #include "vivid-core.h"
22 #include "vivid-ctrls.h"
23 #include "vivid-sdr-cap.h"
24
25 /* stream formats */
26 struct vivid_format {
27         u32     pixelformat;
28         u32     buffersize;
29 };
30
31 /* format descriptions for capture and preview */
32 static const struct vivid_format formats[] = {
33         {
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,
39         },
40 };
41
42 static const struct v4l2_frequency_band bands_adc[] = {
43         {
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,
50         },
51         {
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,
58         },
59         {
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,
66         },
67 };
68
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)
72
73 static const struct v4l2_frequency_band bands_fm[] = {
74         {
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,
81         },
82 };
83
84 static void vivid_thread_sdr_cap_tick(struct vivid_dev *dev)
85 {
86         struct vivid_buffer *sdr_cap_buf = NULL;
87
88         dprintk(dev, 1, "SDR Capture Thread Tick\n");
89
90         /* Drop a certain percentage of buffers. */
91         if (dev->perc_dropped_buffers &&
92             prandom_u32_max(100) < dev->perc_dropped_buffers)
93                 return;
94
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);
100         }
101         spin_unlock(&dev->slock);
102
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;
111         }
112 }
113
114 static int vivid_thread_sdr_cap(void *data)
115 {
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;
123
124         dprintk(dev, 1, "SDR Capture Thread Start\n");
125
126         set_freezable();
127
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;
134
135         for (;;) {
136                 try_to_freeze();
137                 if (kthread_should_stop())
138                         break;
139
140                 if (!mutex_trylock(&dev->mutex)) {
141                         schedule_timeout_uninterruptible(1);
142                         continue;
143                 }
144
145                 cur_jiffies = jiffies;
146                 if (dev->sdr_cap_seq_resync) {
147                         dev->jiffies_sdr_cap = cur_jiffies;
148                         dev->sdr_cap_seq_offset = dev->sdr_cap_seq_count + 1;
149                         dev->sdr_cap_seq_count = 0;
150                         dev->sdr_cap_seq_resync = false;
151                 }
152                 /* Calculate the number of jiffies since we started streaming */
153                 jiffies_since_start = cur_jiffies - dev->jiffies_sdr_cap;
154                 /* Get the number of buffers streamed since the start */
155                 buffers_since_start =
156                         (u64)jiffies_since_start * dev->sdr_adc_freq +
157                                       (HZ * SDR_CAP_SAMPLES_PER_BUF) / 2;
158                 do_div(buffers_since_start, HZ * SDR_CAP_SAMPLES_PER_BUF);
159
160                 /*
161                  * After more than 0xf0000000 (rounded down to a multiple of
162                  * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
163                  * jiffies have passed since we started streaming reset the
164                  * counters and keep track of the sequence offset.
165                  */
166                 if (jiffies_since_start > JIFFIES_RESYNC) {
167                         dev->jiffies_sdr_cap = cur_jiffies;
168                         dev->sdr_cap_seq_offset = buffers_since_start;
169                         buffers_since_start = 0;
170                 }
171                 dev->sdr_cap_seq_count =
172                         buffers_since_start + dev->sdr_cap_seq_offset;
173
174                 vivid_thread_sdr_cap_tick(dev);
175                 mutex_unlock(&dev->mutex);
176
177                 /*
178                  * Calculate the number of samples streamed since we started,
179                  * not including the current buffer.
180                  */
181                 samples_since_start = buffers_since_start * SDR_CAP_SAMPLES_PER_BUF;
182
183                 /* And the number of jiffies since we started */
184                 jiffies_since_start = jiffies - dev->jiffies_sdr_cap;
185
186                 /* Increase by the number of samples in one buffer */
187                 samples_since_start += SDR_CAP_SAMPLES_PER_BUF;
188                 /*
189                  * Calculate when that next buffer is supposed to start
190                  * in jiffies since we started streaming.
191                  */
192                 next_jiffies_since_start = samples_since_start * HZ +
193                                            dev->sdr_adc_freq / 2;
194                 do_div(next_jiffies_since_start, dev->sdr_adc_freq);
195                 /* If it is in the past, then just schedule asap */
196                 if (next_jiffies_since_start < jiffies_since_start)
197                         next_jiffies_since_start = jiffies_since_start;
198
199                 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
200                 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
201         }
202         dprintk(dev, 1, "SDR Capture Thread End\n");
203         return 0;
204 }
205
206 static int sdr_cap_queue_setup(struct vb2_queue *vq,
207                        unsigned *nbuffers, unsigned *nplanes,
208                        unsigned sizes[], struct device *alloc_devs[])
209 {
210         /* 2 = max 16-bit sample returned */
211         sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
212         *nplanes = 1;
213         return 0;
214 }
215
216 static int sdr_cap_buf_prepare(struct vb2_buffer *vb)
217 {
218         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
219         unsigned size = SDR_CAP_SAMPLES_PER_BUF * 2;
220
221         dprintk(dev, 1, "%s\n", __func__);
222
223         if (dev->buf_prepare_error) {
224                 /*
225                  * Error injection: test what happens if buf_prepare() returns
226                  * an error.
227                  */
228                 dev->buf_prepare_error = false;
229                 return -EINVAL;
230         }
231         if (vb2_plane_size(vb, 0) < size) {
232                 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
233                                 __func__, vb2_plane_size(vb, 0), size);
234                 return -EINVAL;
235         }
236         vb2_set_plane_payload(vb, 0, size);
237
238         return 0;
239 }
240
241 static void sdr_cap_buf_queue(struct vb2_buffer *vb)
242 {
243         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
244         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
245         struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
246
247         dprintk(dev, 1, "%s\n", __func__);
248
249         spin_lock(&dev->slock);
250         list_add_tail(&buf->list, &dev->sdr_cap_active);
251         spin_unlock(&dev->slock);
252 }
253
254 static int sdr_cap_start_streaming(struct vb2_queue *vq, unsigned count)
255 {
256         struct vivid_dev *dev = vb2_get_drv_priv(vq);
257         int err = 0;
258
259         dprintk(dev, 1, "%s\n", __func__);
260         dev->sdr_cap_seq_count = 0;
261         if (dev->start_streaming_error) {
262                 dev->start_streaming_error = false;
263                 err = -EINVAL;
264         } else if (dev->kthread_sdr_cap == NULL) {
265                 dev->kthread_sdr_cap = kthread_run(vivid_thread_sdr_cap, dev,
266                                 "%s-sdr-cap", dev->v4l2_dev.name);
267
268                 if (IS_ERR(dev->kthread_sdr_cap)) {
269                         v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
270                         err = PTR_ERR(dev->kthread_sdr_cap);
271                         dev->kthread_sdr_cap = NULL;
272                 }
273         }
274         if (err) {
275                 struct vivid_buffer *buf, *tmp;
276
277                 list_for_each_entry_safe(buf, tmp, &dev->sdr_cap_active, list) {
278                         list_del(&buf->list);
279                         vb2_buffer_done(&buf->vb.vb2_buf,
280                                         VB2_BUF_STATE_QUEUED);
281                 }
282         }
283         return err;
284 }
285
286 /* abort streaming and wait for last buffer */
287 static void sdr_cap_stop_streaming(struct vb2_queue *vq)
288 {
289         struct vivid_dev *dev = vb2_get_drv_priv(vq);
290
291         if (dev->kthread_sdr_cap == NULL)
292                 return;
293
294         while (!list_empty(&dev->sdr_cap_active)) {
295                 struct vivid_buffer *buf;
296
297                 buf = list_entry(dev->sdr_cap_active.next,
298                                 struct vivid_buffer, list);
299                 list_del(&buf->list);
300                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
301         }
302
303         /* shutdown control thread */
304         kthread_stop(dev->kthread_sdr_cap);
305         dev->kthread_sdr_cap = NULL;
306 }
307
308 const struct vb2_ops vivid_sdr_cap_qops = {
309         .queue_setup            = sdr_cap_queue_setup,
310         .buf_prepare            = sdr_cap_buf_prepare,
311         .buf_queue              = sdr_cap_buf_queue,
312         .start_streaming        = sdr_cap_start_streaming,
313         .stop_streaming         = sdr_cap_stop_streaming,
314         .wait_prepare           = vb2_ops_wait_prepare,
315         .wait_finish            = vb2_ops_wait_finish,
316 };
317
318 int vivid_sdr_enum_freq_bands(struct file *file, void *fh,
319                 struct v4l2_frequency_band *band)
320 {
321         switch (band->tuner) {
322         case 0:
323                 if (band->index >= ARRAY_SIZE(bands_adc))
324                         return -EINVAL;
325                 *band = bands_adc[band->index];
326                 return 0;
327         case 1:
328                 if (band->index >= ARRAY_SIZE(bands_fm))
329                         return -EINVAL;
330                 *band = bands_fm[band->index];
331                 return 0;
332         default:
333                 return -EINVAL;
334         }
335 }
336
337 int vivid_sdr_g_frequency(struct file *file, void *fh,
338                 struct v4l2_frequency *vf)
339 {
340         struct vivid_dev *dev = video_drvdata(file);
341
342         switch (vf->tuner) {
343         case 0:
344                 vf->frequency = dev->sdr_adc_freq;
345                 vf->type = V4L2_TUNER_ADC;
346                 return 0;
347         case 1:
348                 vf->frequency = dev->sdr_fm_freq;
349                 vf->type = V4L2_TUNER_RF;
350                 return 0;
351         default:
352                 return -EINVAL;
353         }
354 }
355
356 int vivid_sdr_s_frequency(struct file *file, void *fh,
357                 const struct v4l2_frequency *vf)
358 {
359         struct vivid_dev *dev = video_drvdata(file);
360         unsigned freq = vf->frequency;
361         unsigned band;
362
363         switch (vf->tuner) {
364         case 0:
365                 if (vf->type != V4L2_TUNER_ADC)
366                         return -EINVAL;
367                 if (freq < BAND_ADC_0)
368                         band = 0;
369                 else if (freq < BAND_ADC_1)
370                         band = 1;
371                 else
372                         band = 2;
373
374                 freq = clamp_t(unsigned, freq,
375                                 bands_adc[band].rangelow,
376                                 bands_adc[band].rangehigh);
377
378                 if (vb2_is_streaming(&dev->vb_sdr_cap_q) &&
379                     freq != dev->sdr_adc_freq) {
380                         /* resync the thread's timings */
381                         dev->sdr_cap_seq_resync = true;
382                 }
383                 dev->sdr_adc_freq = freq;
384                 return 0;
385         case 1:
386                 if (vf->type != V4L2_TUNER_RF)
387                         return -EINVAL;
388                 dev->sdr_fm_freq = clamp_t(unsigned, freq,
389                                 bands_fm[0].rangelow,
390                                 bands_fm[0].rangehigh);
391                 return 0;
392         default:
393                 return -EINVAL;
394         }
395 }
396
397 int vivid_sdr_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
398 {
399         switch (vt->index) {
400         case 0:
401                 strlcpy(vt->name, "ADC", sizeof(vt->name));
402                 vt->type = V4L2_TUNER_ADC;
403                 vt->capability =
404                         V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
405                 vt->rangelow = bands_adc[0].rangelow;
406                 vt->rangehigh = bands_adc[2].rangehigh;
407                 return 0;
408         case 1:
409                 strlcpy(vt->name, "RF", sizeof(vt->name));
410                 vt->type = V4L2_TUNER_RF;
411                 vt->capability =
412                         V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
413                 vt->rangelow = bands_fm[0].rangelow;
414                 vt->rangehigh = bands_fm[0].rangehigh;
415                 return 0;
416         default:
417                 return -EINVAL;
418         }
419 }
420
421 int vivid_sdr_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
422 {
423         if (vt->index > 1)
424                 return -EINVAL;
425         return 0;
426 }
427
428 int vidioc_enum_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
429 {
430         if (f->index >= ARRAY_SIZE(formats))
431                 return -EINVAL;
432         f->pixelformat = formats[f->index].pixelformat;
433         return 0;
434 }
435
436 int vidioc_g_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
437 {
438         struct vivid_dev *dev = video_drvdata(file);
439
440         f->fmt.sdr.pixelformat = dev->sdr_pixelformat;
441         f->fmt.sdr.buffersize = dev->sdr_buffersize;
442         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
443         return 0;
444 }
445
446 int vidioc_s_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
447 {
448         struct vivid_dev *dev = video_drvdata(file);
449         struct vb2_queue *q = &dev->vb_sdr_cap_q;
450         int i;
451
452         if (vb2_is_busy(q))
453                 return -EBUSY;
454
455         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
456         for (i = 0; i < ARRAY_SIZE(formats); i++) {
457                 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
458                         dev->sdr_pixelformat = formats[i].pixelformat;
459                         dev->sdr_buffersize = formats[i].buffersize;
460                         f->fmt.sdr.buffersize = formats[i].buffersize;
461                         return 0;
462                 }
463         }
464         dev->sdr_pixelformat = formats[0].pixelformat;
465         dev->sdr_buffersize = formats[0].buffersize;
466         f->fmt.sdr.pixelformat = formats[0].pixelformat;
467         f->fmt.sdr.buffersize = formats[0].buffersize;
468         return 0;
469 }
470
471 int vidioc_try_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
472 {
473         int i;
474
475         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
476         for (i = 0; i < ARRAY_SIZE(formats); i++) {
477                 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
478                         f->fmt.sdr.buffersize = formats[i].buffersize;
479                         return 0;
480                 }
481         }
482         f->fmt.sdr.pixelformat = formats[0].pixelformat;
483         f->fmt.sdr.buffersize = formats[0].buffersize;
484         return 0;
485 }
486
487 #define FIXP_N    (15)
488 #define FIXP_FRAC (1 << FIXP_N)
489 #define FIXP_2PI  ((int)(2 * 3.141592653589 * FIXP_FRAC))
490 #define M_100000PI (3.14159 * 100000)
491
492 void vivid_sdr_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
493 {
494         u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
495         unsigned long i;
496         unsigned long plane_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
497         s64 s64tmp;
498         s32 src_phase_step;
499         s32 mod_phase_step;
500         s32 fixp_i;
501         s32 fixp_q;
502
503         /* calculate phase step */
504         #define BEEP_FREQ 1000 /* 1kHz beep */
505         src_phase_step = DIV_ROUND_CLOSEST(FIXP_2PI * BEEP_FREQ,
506                                            dev->sdr_adc_freq);
507
508         for (i = 0; i < plane_size; i += 2) {
509                 mod_phase_step = fixp_cos32_rad(dev->sdr_fixp_src_phase,
510                                                 FIXP_2PI) >> (31 - FIXP_N);
511
512                 dev->sdr_fixp_src_phase += src_phase_step;
513                 s64tmp = (s64) mod_phase_step * dev->sdr_fm_deviation;
514                 dev->sdr_fixp_mod_phase += div_s64(s64tmp, M_100000PI);
515
516                 /*
517                  * Transfer phase angle to [0, 2xPI] in order to avoid variable
518                  * overflow and make it suitable for cosine implementation
519                  * used, which does not support negative angles.
520                  */
521                 dev->sdr_fixp_src_phase %= FIXP_2PI;
522                 dev->sdr_fixp_mod_phase %= FIXP_2PI;
523
524                 if (dev->sdr_fixp_mod_phase < 0)
525                         dev->sdr_fixp_mod_phase += FIXP_2PI;
526
527                 fixp_i = fixp_cos32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
528                 fixp_q = fixp_sin32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
529
530                 /* Normalize fraction values represented with 32 bit precision
531                  * to fixed point representation with FIXP_N bits */
532                 fixp_i >>= (31 - FIXP_N);
533                 fixp_q >>= (31 - FIXP_N);
534
535                 switch (dev->sdr_pixelformat) {
536                 case V4L2_SDR_FMT_CU8:
537                         /* convert 'fixp float' to u8 [0, +255] */
538                         /* u8 = X * 127.5 + 127.5; X is float [-1.0, +1.0] */
539                         fixp_i = fixp_i * 1275 + FIXP_FRAC * 1275;
540                         fixp_q = fixp_q * 1275 + FIXP_FRAC * 1275;
541                         *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
542                         *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);
543                         break;
544                 case V4L2_SDR_FMT_CS8:
545                         /* convert 'fixp float' to s8 [-128, +127] */
546                         /* s8 = X * 127.5 - 0.5; X is float [-1.0, +1.0] */
547                         fixp_i = fixp_i * 1275 - FIXP_FRAC * 5;
548                         fixp_q = fixp_q * 1275 - FIXP_FRAC * 5;
549                         *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
550                         *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);
551                         break;
552                 default:
553                         break;
554                 }
555         }
556 }