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