]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/dvb-frontends/rtl2832_sdr.c
Linux 5.3-rc4
[linux.git] / drivers / media / dvb-frontends / rtl2832_sdr.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Realtek RTL2832U SDR driver
4  *
5  * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6  *
7  * GNU Radio plugin "gr-kernel" for device usage will be on:
8  * http://git.linuxtv.org/anttip/gr-kernel.git
9  */
10
11 #include "rtl2832_sdr.h"
12 #include "dvb_usb.h"
13
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-ioctl.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-vmalloc.h>
20
21 #include <linux/platform_device.h>
22 #include <linux/jiffies.h>
23 #include <linux/math64.h>
24 #include <linux/regmap.h>
25
26 static bool rtl2832_sdr_emulated_fmt;
27 module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
28 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
29
30 /* Original macro does not contain enough null pointer checks for our need */
31 #define V4L2_SUBDEV_HAS_OP(sd, o, f) \
32         ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
33
34 #define MAX_BULK_BUFS            (10)
35 #define BULK_BUFFER_SIZE         (128 * 512)
36
37 static const struct v4l2_frequency_band bands_adc[] = {
38         {
39                 .tuner = 0,
40                 .type = V4L2_TUNER_ADC,
41                 .index = 0,
42                 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
43                 .rangelow   =  300000,
44                 .rangehigh  =  300000,
45         },
46         {
47                 .tuner = 0,
48                 .type = V4L2_TUNER_ADC,
49                 .index = 1,
50                 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
51                 .rangelow   =  900001,
52                 .rangehigh  = 2800000,
53         },
54         {
55                 .tuner = 0,
56                 .type = V4L2_TUNER_ADC,
57                 .index = 2,
58                 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
59                 .rangelow   = 3200000,
60                 .rangehigh  = 3200000,
61         },
62 };
63
64 static const struct v4l2_frequency_band bands_fm[] = {
65         {
66                 .tuner = 1,
67                 .type = V4L2_TUNER_RF,
68                 .index = 0,
69                 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
70                 .rangelow   =    50000000,
71                 .rangehigh  =  2000000000,
72         },
73 };
74
75 /* stream formats */
76 struct rtl2832_sdr_format {
77         char    *name;
78         u32     pixelformat;
79         u32     buffersize;
80 };
81
82 static struct rtl2832_sdr_format formats[] = {
83         {
84                 .name           = "Complex U8",
85                 .pixelformat    = V4L2_SDR_FMT_CU8,
86                 .buffersize     = BULK_BUFFER_SIZE,
87         }, {
88                 .name           = "Complex U16LE (emulated)",
89                 .pixelformat    = V4L2_SDR_FMT_CU16LE,
90                 .buffersize     = BULK_BUFFER_SIZE * 2,
91         },
92 };
93
94 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
95
96 /* intermediate buffers with raw data from the USB device */
97 struct rtl2832_sdr_frame_buf {
98         /* common v4l buffer stuff -- must be first */
99         struct vb2_v4l2_buffer vb;
100         struct list_head list;
101 };
102
103 struct rtl2832_sdr_dev {
104 #define POWER_ON           0  /* BIT(0) */
105 #define URB_BUF            1  /* BIT(1) */
106         unsigned long flags;
107
108         struct platform_device *pdev;
109         struct regmap *regmap;
110
111         struct video_device vdev;
112         struct v4l2_device v4l2_dev;
113         struct v4l2_subdev *v4l2_subdev;
114
115         /* videobuf2 queue and queued buffers list */
116         struct vb2_queue vb_queue;
117         struct list_head queued_bufs;
118         spinlock_t queued_bufs_lock; /* Protects queued_bufs */
119         unsigned sequence;           /* buffer sequence counter */
120
121         /* Note if taking both locks v4l2_lock must always be locked first! */
122         struct mutex v4l2_lock;      /* Protects everything else */
123         struct mutex vb_queue_lock;  /* Protects vb_queue and capt_file */
124
125         /* Pointer to our usb_device, will be NULL after unplug */
126         struct usb_device *udev; /* Both mutexes most be hold when setting! */
127
128         unsigned int vb_full; /* vb is full and packets dropped */
129
130         struct urb     *urb_list[MAX_BULK_BUFS];
131         int            buf_num;
132         unsigned long  buf_size;
133         u8             *buf_list[MAX_BULK_BUFS];
134         dma_addr_t     dma_addr[MAX_BULK_BUFS];
135         int urbs_initialized;
136         int urbs_submitted;
137
138         unsigned int f_adc, f_tuner;
139         u32 pixelformat;
140         u32 buffersize;
141         unsigned int num_formats;
142
143         /* Controls */
144         struct v4l2_ctrl_handler hdl;
145         struct v4l2_ctrl *bandwidth_auto;
146         struct v4l2_ctrl *bandwidth;
147
148         /* for sample rate calc */
149         unsigned int sample;
150         unsigned int sample_measured;
151         unsigned long jiffies_next;
152 };
153
154 /* Private functions */
155 static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
156                 struct rtl2832_sdr_dev *dev)
157 {
158         unsigned long flags;
159         struct rtl2832_sdr_frame_buf *buf = NULL;
160
161         spin_lock_irqsave(&dev->queued_bufs_lock, flags);
162         if (list_empty(&dev->queued_bufs))
163                 goto leave;
164
165         buf = list_entry(dev->queued_bufs.next,
166                         struct rtl2832_sdr_frame_buf, list);
167         list_del(&buf->list);
168 leave:
169         spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
170         return buf;
171 }
172
173 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
174                 void *dst, const u8 *src, unsigned int src_len)
175 {
176         struct platform_device *pdev = dev->pdev;
177         unsigned int dst_len;
178
179         if (dev->pixelformat ==  V4L2_SDR_FMT_CU8) {
180                 /* native stream, no need to convert */
181                 memcpy(dst, src, src_len);
182                 dst_len = src_len;
183         } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
184                 /* convert u8 to u16 */
185                 unsigned int i;
186                 u16 *u16dst = dst;
187
188                 for (i = 0; i < src_len; i++)
189                         *u16dst++ = (src[i] << 8) | (src[i] >> 0);
190                 dst_len = 2 * src_len;
191         } else {
192                 dst_len = 0;
193         }
194
195         /* calculate sample rate and output it in 10 seconds intervals */
196         if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
197                 #define MSECS 10000UL
198                 unsigned int msecs = jiffies_to_msecs(jiffies -
199                                 dev->jiffies_next + msecs_to_jiffies(MSECS));
200                 unsigned int samples = dev->sample - dev->sample_measured;
201
202                 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
203                 dev->sample_measured = dev->sample;
204                 dev_dbg(&pdev->dev,
205                         "slen=%u samples=%u msecs=%u sample rate=%lu\n",
206                         src_len, samples, msecs, samples * 1000UL / msecs);
207         }
208
209         /* total number of I+Q pairs */
210         dev->sample += src_len / 2;
211
212         return dst_len;
213 }
214
215 /*
216  * This gets called for the bulk stream pipe. This is done in interrupt
217  * time, so it has to be fast, not crash, and not stall. Neat.
218  */
219 static void rtl2832_sdr_urb_complete(struct urb *urb)
220 {
221         struct rtl2832_sdr_dev *dev = urb->context;
222         struct platform_device *pdev = dev->pdev;
223         struct rtl2832_sdr_frame_buf *fbuf;
224
225         dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
226                             urb->status, urb->actual_length,
227                             urb->transfer_buffer_length, urb->error_count);
228
229         switch (urb->status) {
230         case 0:             /* success */
231         case -ETIMEDOUT:    /* NAK */
232                 break;
233         case -ECONNRESET:   /* kill */
234         case -ENOENT:
235         case -ESHUTDOWN:
236                 return;
237         default:            /* error */
238                 dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
239                 break;
240         }
241
242         if (likely(urb->actual_length > 0)) {
243                 void *ptr;
244                 unsigned int len;
245                 /* get free framebuffer */
246                 fbuf = rtl2832_sdr_get_next_fill_buf(dev);
247                 if (unlikely(fbuf == NULL)) {
248                         dev->vb_full++;
249                         dev_notice_ratelimited(&pdev->dev,
250                                                "videobuf is full, %d packets dropped\n",
251                                                dev->vb_full);
252                         goto skip;
253                 }
254
255                 /* fill framebuffer */
256                 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
257                 len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
258                                 urb->actual_length);
259                 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
260                 fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
261                 fbuf->vb.sequence = dev->sequence++;
262                 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
263         }
264 skip:
265         usb_submit_urb(urb, GFP_ATOMIC);
266 }
267
268 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
269 {
270         struct platform_device *pdev = dev->pdev;
271         int i;
272
273         for (i = dev->urbs_submitted - 1; i >= 0; i--) {
274                 dev_dbg(&pdev->dev, "kill urb=%d\n", i);
275                 /* stop the URB */
276                 usb_kill_urb(dev->urb_list[i]);
277         }
278         dev->urbs_submitted = 0;
279
280         return 0;
281 }
282
283 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
284 {
285         struct platform_device *pdev = dev->pdev;
286         int i, ret;
287
288         for (i = 0; i < dev->urbs_initialized; i++) {
289                 dev_dbg(&pdev->dev, "submit urb=%d\n", i);
290                 ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL);
291                 if (ret) {
292                         dev_err(&pdev->dev,
293                                 "Could not submit urb no. %d - get them all back\n",
294                                 i);
295                         rtl2832_sdr_kill_urbs(dev);
296                         return ret;
297                 }
298                 dev->urbs_submitted++;
299         }
300
301         return 0;
302 }
303
304 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
305 {
306         struct platform_device *pdev = dev->pdev;
307
308         if (test_bit(URB_BUF, &dev->flags)) {
309                 while (dev->buf_num) {
310                         dev->buf_num--;
311                         dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
312                         usb_free_coherent(dev->udev, dev->buf_size,
313                                           dev->buf_list[dev->buf_num],
314                                           dev->dma_addr[dev->buf_num]);
315                 }
316         }
317         clear_bit(URB_BUF, &dev->flags);
318
319         return 0;
320 }
321
322 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
323 {
324         struct platform_device *pdev = dev->pdev;
325
326         dev->buf_num = 0;
327         dev->buf_size = BULK_BUFFER_SIZE;
328
329         dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
330                 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
331
332         for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
333                 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
334                                 BULK_BUFFER_SIZE, GFP_KERNEL,
335                                 &dev->dma_addr[dev->buf_num]);
336                 if (!dev->buf_list[dev->buf_num]) {
337                         dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
338                                 dev->buf_num);
339                         rtl2832_sdr_free_stream_bufs(dev);
340                         return -ENOMEM;
341                 }
342
343                 dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
344                         dev->buf_num, dev->buf_list[dev->buf_num],
345                         (long long)dev->dma_addr[dev->buf_num]);
346                 set_bit(URB_BUF, &dev->flags);
347         }
348
349         return 0;
350 }
351
352 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
353 {
354         struct platform_device *pdev = dev->pdev;
355         int i;
356
357         rtl2832_sdr_kill_urbs(dev);
358
359         for (i = dev->urbs_initialized - 1; i >= 0; i--) {
360                 if (dev->urb_list[i]) {
361                         dev_dbg(&pdev->dev, "free urb=%d\n", i);
362                         /* free the URBs */
363                         usb_free_urb(dev->urb_list[i]);
364                 }
365         }
366         dev->urbs_initialized = 0;
367
368         return 0;
369 }
370
371 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
372 {
373         struct platform_device *pdev = dev->pdev;
374         int i, j;
375
376         /* allocate the URBs */
377         for (i = 0; i < MAX_BULK_BUFS; i++) {
378                 dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
379                 dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL);
380                 if (!dev->urb_list[i]) {
381                         for (j = 0; j < i; j++)
382                                 usb_free_urb(dev->urb_list[j]);
383                         return -ENOMEM;
384                 }
385                 usb_fill_bulk_urb(dev->urb_list[i],
386                                 dev->udev,
387                                 usb_rcvbulkpipe(dev->udev, 0x81),
388                                 dev->buf_list[i],
389                                 BULK_BUFFER_SIZE,
390                                 rtl2832_sdr_urb_complete, dev);
391
392                 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
393                 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
394                 dev->urbs_initialized++;
395         }
396
397         return 0;
398 }
399
400 /* Must be called with vb_queue_lock hold */
401 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
402 {
403         struct platform_device *pdev = dev->pdev;
404         unsigned long flags;
405
406         dev_dbg(&pdev->dev, "\n");
407
408         spin_lock_irqsave(&dev->queued_bufs_lock, flags);
409         while (!list_empty(&dev->queued_bufs)) {
410                 struct rtl2832_sdr_frame_buf *buf;
411
412                 buf = list_entry(dev->queued_bufs.next,
413                                 struct rtl2832_sdr_frame_buf, list);
414                 list_del(&buf->list);
415                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
416         }
417         spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
418 }
419
420 static int rtl2832_sdr_querycap(struct file *file, void *fh,
421                 struct v4l2_capability *cap)
422 {
423         struct rtl2832_sdr_dev *dev = video_drvdata(file);
424         struct platform_device *pdev = dev->pdev;
425
426         dev_dbg(&pdev->dev, "\n");
427
428         strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
429         strscpy(cap->card, dev->vdev.name, sizeof(cap->card));
430         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
431         return 0;
432 }
433
434 /* Videobuf2 operations */
435 static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
436                 unsigned int *nbuffers,
437                 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
438 {
439         struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
440         struct platform_device *pdev = dev->pdev;
441
442         dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
443
444         /* Need at least 8 buffers */
445         if (vq->num_buffers + *nbuffers < 8)
446                 *nbuffers = 8 - vq->num_buffers;
447         *nplanes = 1;
448         sizes[0] = PAGE_ALIGN(dev->buffersize);
449         dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
450         return 0;
451 }
452
453 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
454 {
455         struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
456
457         /* Don't allow queueing new buffers after device disconnection */
458         if (!dev->udev)
459                 return -ENODEV;
460
461         return 0;
462 }
463
464 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
465 {
466         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
467         struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
468         struct rtl2832_sdr_frame_buf *buf =
469                         container_of(vbuf, struct rtl2832_sdr_frame_buf, vb);
470         unsigned long flags;
471
472         /* Check the device has not disconnected between prep and queuing */
473         if (!dev->udev) {
474                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
475                 return;
476         }
477
478         spin_lock_irqsave(&dev->queued_bufs_lock, flags);
479         list_add_tail(&buf->list, &dev->queued_bufs);
480         spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
481 }
482
483 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
484 {
485         struct platform_device *pdev = dev->pdev;
486         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
487         struct dvb_frontend *fe = pdata->dvb_frontend;
488         int ret;
489         unsigned int f_sr, f_if;
490         u8 buf[4], u8tmp1, u8tmp2;
491         u64 u64tmp;
492         u32 u32tmp;
493
494         dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
495
496         if (!test_bit(POWER_ON, &dev->flags))
497                 return 0;
498
499         if (dev->f_adc == 0)
500                 return 0;
501
502         f_sr = dev->f_adc;
503
504         ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2);
505         if (ret)
506                 goto err;
507
508         ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4);
509         if (ret)
510                 goto err;
511
512         /* get IF from tuner */
513         if (fe->ops.tuner_ops.get_if_frequency)
514                 ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
515         else
516                 ret = -EINVAL;
517
518         if (ret)
519                 goto err;
520
521         /* program IF */
522         u64tmp = f_if % pdata->clk;
523         u64tmp *= 0x400000;
524         u64tmp = div_u64(u64tmp, pdata->clk);
525         u64tmp = -u64tmp;
526         u32tmp = u64tmp & 0x3fffff;
527
528         dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
529
530         buf[0] = (u32tmp >> 16) & 0xff;
531         buf[1] = (u32tmp >>  8) & 0xff;
532         buf[2] = (u32tmp >>  0) & 0xff;
533
534         ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3);
535         if (ret)
536                 goto err;
537
538         /* BB / IF mode */
539         /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
540         if (f_if) {
541                 u8tmp1 = 0x1a; /* disable Zero-IF */
542                 u8tmp2 = 0x8d; /* enable ADC I */
543         } else {
544                 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
545                 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
546         }
547
548         ret = regmap_write(dev->regmap, 0x1b1, u8tmp1);
549         if (ret)
550                 goto err;
551
552         ret = regmap_write(dev->regmap, 0x008, u8tmp2);
553         if (ret)
554                 goto err;
555
556         ret = regmap_write(dev->regmap, 0x006, 0x80);
557         if (ret)
558                 goto err;
559
560         /* program sampling rate (resampling down) */
561         u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
562         u32tmp <<= 2;
563         buf[0] = (u32tmp >> 24) & 0xff;
564         buf[1] = (u32tmp >> 16) & 0xff;
565         buf[2] = (u32tmp >>  8) & 0xff;
566         buf[3] = (u32tmp >>  0) & 0xff;
567         ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4);
568         if (ret)
569                 goto err;
570
571         /* low-pass filter */
572         ret = regmap_bulk_write(dev->regmap, 0x11c,
573                                 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
574                                 20);
575         if (ret)
576                 goto err;
577
578         ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
579         if (ret)
580                 goto err;
581
582         /* mode */
583         ret = regmap_write(dev->regmap, 0x019, 0x05);
584         if (ret)
585                 goto err;
586
587         ret = regmap_bulk_write(dev->regmap, 0x01a,
588                                 "\x1b\x16\x0d\x06\x01\xff", 6);
589         if (ret)
590                 goto err;
591
592         /* FSM */
593         ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
594         if (ret)
595                 goto err;
596
597         /* PID filter */
598         ret = regmap_write(dev->regmap, 0x061, 0x60);
599         if (ret)
600                 goto err;
601
602         /* used RF tuner based settings */
603         switch (pdata->tuner) {
604         case RTL2832_SDR_TUNER_E4000:
605                 ret = regmap_write(dev->regmap, 0x112, 0x5a);
606                 ret = regmap_write(dev->regmap, 0x102, 0x40);
607                 ret = regmap_write(dev->regmap, 0x103, 0x5a);
608                 ret = regmap_write(dev->regmap, 0x1c7, 0x30);
609                 ret = regmap_write(dev->regmap, 0x104, 0xd0);
610                 ret = regmap_write(dev->regmap, 0x105, 0xbe);
611                 ret = regmap_write(dev->regmap, 0x1c8, 0x18);
612                 ret = regmap_write(dev->regmap, 0x106, 0x35);
613                 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
614                 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
615                 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
616                 ret = regmap_write(dev->regmap, 0x107, 0x40);
617                 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
618                 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
619                 ret = regmap_write(dev->regmap, 0x108, 0x80);
620                 ret = regmap_write(dev->regmap, 0x109, 0x7f);
621                 ret = regmap_write(dev->regmap, 0x10a, 0x80);
622                 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
623                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
624                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
625                 ret = regmap_write(dev->regmap, 0x011, 0xd4);
626                 ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
627                 ret = regmap_write(dev->regmap, 0x1d9, 0x00);
628                 ret = regmap_write(dev->regmap, 0x1db, 0x00);
629                 ret = regmap_write(dev->regmap, 0x1dd, 0x14);
630                 ret = regmap_write(dev->regmap, 0x1de, 0xec);
631                 ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
632                 ret = regmap_write(dev->regmap, 0x1e6, 0x02);
633                 ret = regmap_write(dev->regmap, 0x1d7, 0x09);
634                 ret = regmap_write(dev->regmap, 0x00d, 0x83);
635                 ret = regmap_write(dev->regmap, 0x010, 0x49);
636                 ret = regmap_write(dev->regmap, 0x00d, 0x87);
637                 ret = regmap_write(dev->regmap, 0x00d, 0x85);
638                 ret = regmap_write(dev->regmap, 0x013, 0x02);
639                 break;
640         case RTL2832_SDR_TUNER_FC0012:
641         case RTL2832_SDR_TUNER_FC0013:
642                 ret = regmap_write(dev->regmap, 0x112, 0x5a);
643                 ret = regmap_write(dev->regmap, 0x102, 0x40);
644                 ret = regmap_write(dev->regmap, 0x103, 0x5a);
645                 ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
646                 ret = regmap_write(dev->regmap, 0x104, 0xcc);
647                 ret = regmap_write(dev->regmap, 0x105, 0xbe);
648                 ret = regmap_write(dev->regmap, 0x1c8, 0x16);
649                 ret = regmap_write(dev->regmap, 0x106, 0x35);
650                 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
651                 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
652                 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
653                 ret = regmap_write(dev->regmap, 0x107, 0x40);
654                 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
655                 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
656                 ret = regmap_write(dev->regmap, 0x108, 0x80);
657                 ret = regmap_write(dev->regmap, 0x109, 0x7f);
658                 ret = regmap_write(dev->regmap, 0x10a, 0x80);
659                 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
660                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
661                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
662                 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2);
663                 ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
664                 ret = regmap_write(dev->regmap, 0x1d9, 0x00);
665                 ret = regmap_write(dev->regmap, 0x1db, 0x00);
666                 ret = regmap_write(dev->regmap, 0x1dd, 0x11);
667                 ret = regmap_write(dev->regmap, 0x1de, 0xef);
668                 ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
669                 ret = regmap_write(dev->regmap, 0x1e6, 0x02);
670                 ret = regmap_write(dev->regmap, 0x1d7, 0x09);
671                 break;
672         case RTL2832_SDR_TUNER_R820T:
673         case RTL2832_SDR_TUNER_R828D:
674                 ret = regmap_write(dev->regmap, 0x112, 0x5a);
675                 ret = regmap_write(dev->regmap, 0x102, 0x40);
676                 ret = regmap_write(dev->regmap, 0x115, 0x01);
677                 ret = regmap_write(dev->regmap, 0x103, 0x80);
678                 ret = regmap_write(dev->regmap, 0x1c7, 0x24);
679                 ret = regmap_write(dev->regmap, 0x104, 0xcc);
680                 ret = regmap_write(dev->regmap, 0x105, 0xbe);
681                 ret = regmap_write(dev->regmap, 0x1c8, 0x14);
682                 ret = regmap_write(dev->regmap, 0x106, 0x35);
683                 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
684                 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
685                 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
686                 ret = regmap_write(dev->regmap, 0x107, 0x40);
687                 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
688                 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
689                 ret = regmap_write(dev->regmap, 0x108, 0x80);
690                 ret = regmap_write(dev->regmap, 0x109, 0x7f);
691                 ret = regmap_write(dev->regmap, 0x10a, 0x80);
692                 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
693                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
694                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
695                 ret = regmap_write(dev->regmap, 0x011, 0xf4);
696                 break;
697         case RTL2832_SDR_TUNER_FC2580:
698                 ret = regmap_write(dev->regmap, 0x112, 0x39);
699                 ret = regmap_write(dev->regmap, 0x102, 0x40);
700                 ret = regmap_write(dev->regmap, 0x103, 0x5a);
701                 ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
702                 ret = regmap_write(dev->regmap, 0x104, 0xcc);
703                 ret = regmap_write(dev->regmap, 0x105, 0xbe);
704                 ret = regmap_write(dev->regmap, 0x1c8, 0x16);
705                 ret = regmap_write(dev->regmap, 0x106, 0x35);
706                 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
707                 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
708                 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
709                 ret = regmap_write(dev->regmap, 0x107, 0x40);
710                 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
711                 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
712                 ret = regmap_write(dev->regmap, 0x108, 0x80);
713                 ret = regmap_write(dev->regmap, 0x109, 0x7f);
714                 ret = regmap_write(dev->regmap, 0x10a, 0x9c);
715                 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
716                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
717                 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
718                 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2);
719                 break;
720         default:
721                 dev_notice(&pdev->dev, "Unsupported tuner\n");
722         }
723
724         /* software reset */
725         ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04);
726         if (ret)
727                 goto err;
728
729         ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00);
730         if (ret)
731                 goto err;
732 err:
733         return ret;
734 };
735
736 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
737 {
738         struct platform_device *pdev = dev->pdev;
739         int ret;
740
741         dev_dbg(&pdev->dev, "\n");
742
743         /* PID filter */
744         ret = regmap_write(dev->regmap, 0x061, 0xe0);
745         if (ret)
746                 goto err;
747
748         /* mode */
749         ret = regmap_write(dev->regmap, 0x019, 0x20);
750         if (ret)
751                 goto err;
752
753         ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
754         if (ret)
755                 goto err;
756
757         /* FSM */
758         ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
759         if (ret)
760                 goto err;
761
762         ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2);
763         if (ret)
764                 goto err;
765
766         ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4);
767         if (ret)
768                 goto err;
769 err:
770         return;
771 };
772
773 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
774 {
775         struct platform_device *pdev = dev->pdev;
776         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
777         struct dvb_frontend *fe = pdata->dvb_frontend;
778         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
779         struct v4l2_ctrl *bandwidth_auto;
780         struct v4l2_ctrl *bandwidth;
781
782         /*
783          * tuner RF (Hz)
784          */
785         if (dev->f_tuner == 0)
786                 return 0;
787
788         /*
789          * bandwidth (Hz)
790          */
791         bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
792                                         V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
793         bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
794         if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
795                 c->bandwidth_hz = dev->f_adc;
796                 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
797         } else {
798                 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
799         }
800
801         c->frequency = dev->f_tuner;
802         c->delivery_system = SYS_DVBT;
803
804         dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
805                 c->frequency, c->bandwidth_hz);
806
807         if (!test_bit(POWER_ON, &dev->flags))
808                 return 0;
809
810         if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
811                 if (fe->ops.tuner_ops.set_params)
812                         fe->ops.tuner_ops.set_params(fe);
813         }
814
815         return 0;
816 };
817
818 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
819 {
820         struct platform_device *pdev = dev->pdev;
821         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
822         struct dvb_frontend *fe = pdata->dvb_frontend;
823
824         dev_dbg(&pdev->dev, "\n");
825
826         if (fe->ops.tuner_ops.init)
827                 fe->ops.tuner_ops.init(fe);
828
829         return 0;
830 };
831
832 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
833 {
834         struct platform_device *pdev = dev->pdev;
835         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
836         struct dvb_frontend *fe = pdata->dvb_frontend;
837
838         dev_dbg(&pdev->dev, "\n");
839
840         if (fe->ops.tuner_ops.sleep)
841                 fe->ops.tuner_ops.sleep(fe);
842
843         return;
844 };
845
846 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
847 {
848         struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
849         struct platform_device *pdev = dev->pdev;
850         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
851         struct dvb_usb_device *d = pdata->dvb_usb_device;
852         int ret;
853
854         dev_dbg(&pdev->dev, "\n");
855
856         if (!dev->udev)
857                 return -ENODEV;
858
859         if (mutex_lock_interruptible(&dev->v4l2_lock))
860                 return -ERESTARTSYS;
861
862         if (d->props->power_ctrl)
863                 d->props->power_ctrl(d, 1);
864
865         /* enable ADC */
866         if (d->props->frontend_ctrl)
867                 d->props->frontend_ctrl(pdata->dvb_frontend, 1);
868
869         set_bit(POWER_ON, &dev->flags);
870
871         /* wake-up tuner */
872         if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
873                 ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
874         else
875                 ret = rtl2832_sdr_set_tuner(dev);
876         if (ret)
877                 goto err;
878
879         ret = rtl2832_sdr_set_tuner_freq(dev);
880         if (ret)
881                 goto err;
882
883         ret = rtl2832_sdr_set_adc(dev);
884         if (ret)
885                 goto err;
886
887         ret = rtl2832_sdr_alloc_stream_bufs(dev);
888         if (ret)
889                 goto err;
890
891         ret = rtl2832_sdr_alloc_urbs(dev);
892         if (ret)
893                 goto err;
894
895         dev->sequence = 0;
896
897         ret = rtl2832_sdr_submit_urbs(dev);
898         if (ret)
899                 goto err;
900
901 err:
902         mutex_unlock(&dev->v4l2_lock);
903
904         return ret;
905 }
906
907 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
908 {
909         struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
910         struct platform_device *pdev = dev->pdev;
911         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
912         struct dvb_usb_device *d = pdata->dvb_usb_device;
913
914         dev_dbg(&pdev->dev, "\n");
915
916         mutex_lock(&dev->v4l2_lock);
917
918         rtl2832_sdr_kill_urbs(dev);
919         rtl2832_sdr_free_urbs(dev);
920         rtl2832_sdr_free_stream_bufs(dev);
921         rtl2832_sdr_cleanup_queued_bufs(dev);
922         rtl2832_sdr_unset_adc(dev);
923
924         /* sleep tuner */
925         if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
926                 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
927         else
928                 rtl2832_sdr_unset_tuner(dev);
929
930         clear_bit(POWER_ON, &dev->flags);
931
932         /* disable ADC */
933         if (d->props->frontend_ctrl)
934                 d->props->frontend_ctrl(pdata->dvb_frontend, 0);
935
936         if (d->props->power_ctrl)
937                 d->props->power_ctrl(d, 0);
938
939         mutex_unlock(&dev->v4l2_lock);
940 }
941
942 static const struct vb2_ops rtl2832_sdr_vb2_ops = {
943         .queue_setup            = rtl2832_sdr_queue_setup,
944         .buf_prepare            = rtl2832_sdr_buf_prepare,
945         .buf_queue              = rtl2832_sdr_buf_queue,
946         .start_streaming        = rtl2832_sdr_start_streaming,
947         .stop_streaming         = rtl2832_sdr_stop_streaming,
948         .wait_prepare           = vb2_ops_wait_prepare,
949         .wait_finish            = vb2_ops_wait_finish,
950 };
951
952 static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
953                 struct v4l2_tuner *v)
954 {
955         struct rtl2832_sdr_dev *dev = video_drvdata(file);
956         struct platform_device *pdev = dev->pdev;
957         int ret;
958
959         dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
960
961         if (v->index == 0) {
962                 strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
963                 v->type = V4L2_TUNER_ADC;
964                 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
965                 v->rangelow =   300000;
966                 v->rangehigh = 3200000;
967                 ret = 0;
968         } else if (v->index == 1 &&
969                    V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
970                 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
971         } else if (v->index == 1) {
972                 strscpy(v->name, "RF: <unknown>", sizeof(v->name));
973                 v->type = V4L2_TUNER_RF;
974                 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
975                 v->rangelow =    50000000;
976                 v->rangehigh = 2000000000;
977                 ret = 0;
978         } else {
979                 ret = -EINVAL;
980         }
981         return ret;
982 }
983
984 static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
985                 const struct v4l2_tuner *v)
986 {
987         struct rtl2832_sdr_dev *dev = video_drvdata(file);
988         struct platform_device *pdev = dev->pdev;
989         int ret;
990
991         dev_dbg(&pdev->dev, "\n");
992
993         if (v->index == 0) {
994                 ret = 0;
995         } else if (v->index == 1 &&
996                    V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
997                 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
998         } else if (v->index == 1) {
999                 ret = 0;
1000         } else {
1001                 ret = -EINVAL;
1002         }
1003         return ret;
1004 }
1005
1006 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
1007                 struct v4l2_frequency_band *band)
1008 {
1009         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1010         struct platform_device *pdev = dev->pdev;
1011         int ret;
1012
1013         dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1014                 band->tuner, band->type, band->index);
1015
1016         if (band->tuner == 0) {
1017                 if (band->index >= ARRAY_SIZE(bands_adc))
1018                         return -EINVAL;
1019
1020                 *band = bands_adc[band->index];
1021                 ret = 0;
1022         } else if (band->tuner == 1 &&
1023                    V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
1024                 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
1025         } else if (band->tuner == 1) {
1026                 if (band->index >= ARRAY_SIZE(bands_fm))
1027                         return -EINVAL;
1028
1029                 *band = bands_fm[band->index];
1030                 ret = 0;
1031         } else {
1032                 ret = -EINVAL;
1033         }
1034         return ret;
1035 }
1036
1037 static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1038                 struct v4l2_frequency *f)
1039 {
1040         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1041         struct platform_device *pdev = dev->pdev;
1042         int ret;
1043
1044         dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1045
1046         if (f->tuner == 0) {
1047                 f->frequency = dev->f_adc;
1048                 f->type = V4L2_TUNER_ADC;
1049                 ret = 0;
1050         } else if (f->tuner == 1 &&
1051                    V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
1052                 f->type = V4L2_TUNER_RF;
1053                 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
1054         } else if (f->tuner == 1) {
1055                 f->frequency = dev->f_tuner;
1056                 f->type = V4L2_TUNER_RF;
1057                 ret = 0;
1058         } else {
1059                 ret = -EINVAL;
1060         }
1061         return ret;
1062 }
1063
1064 static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1065                 const struct v4l2_frequency *f)
1066 {
1067         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1068         struct platform_device *pdev = dev->pdev;
1069         int ret, band;
1070
1071         dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1072                 f->tuner, f->type, f->frequency);
1073
1074         /* ADC band midpoints */
1075         #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1076         #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1077
1078         if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1079                 if (f->frequency < BAND_ADC_0)
1080                         band = 0;
1081                 else if (f->frequency < BAND_ADC_1)
1082                         band = 1;
1083                 else
1084                         band = 2;
1085
1086                 dev->f_adc = clamp_t(unsigned int, f->frequency,
1087                                      bands_adc[band].rangelow,
1088                                      bands_adc[band].rangehigh);
1089
1090                 dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1091                 ret = rtl2832_sdr_set_adc(dev);
1092         } else if (f->tuner == 1 &&
1093                    V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
1094                 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
1095         } else if (f->tuner == 1) {
1096                 dev->f_tuner = clamp_t(unsigned int, f->frequency,
1097                                 bands_fm[0].rangelow,
1098                                 bands_fm[0].rangehigh);
1099                 dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
1100
1101                 ret = rtl2832_sdr_set_tuner_freq(dev);
1102         } else {
1103                 ret = -EINVAL;
1104         }
1105         return ret;
1106 }
1107
1108 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1109                 struct v4l2_fmtdesc *f)
1110 {
1111         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1112         struct platform_device *pdev = dev->pdev;
1113
1114         dev_dbg(&pdev->dev, "\n");
1115
1116         if (f->index >= dev->num_formats)
1117                 return -EINVAL;
1118
1119         strscpy(f->description, formats[f->index].name, sizeof(f->description));
1120         f->pixelformat = formats[f->index].pixelformat;
1121
1122         return 0;
1123 }
1124
1125 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1126                 struct v4l2_format *f)
1127 {
1128         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1129         struct platform_device *pdev = dev->pdev;
1130
1131         dev_dbg(&pdev->dev, "\n");
1132
1133         f->fmt.sdr.pixelformat = dev->pixelformat;
1134         f->fmt.sdr.buffersize = dev->buffersize;
1135
1136         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1137
1138         return 0;
1139 }
1140
1141 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1142                 struct v4l2_format *f)
1143 {
1144         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1145         struct platform_device *pdev = dev->pdev;
1146         struct vb2_queue *q = &dev->vb_queue;
1147         int i;
1148
1149         dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1150                 (char *)&f->fmt.sdr.pixelformat);
1151
1152         if (vb2_is_busy(q))
1153                 return -EBUSY;
1154
1155         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1156         for (i = 0; i < dev->num_formats; i++) {
1157                 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1158                         dev->pixelformat = formats[i].pixelformat;
1159                         dev->buffersize = formats[i].buffersize;
1160                         f->fmt.sdr.buffersize = formats[i].buffersize;
1161                         return 0;
1162                 }
1163         }
1164
1165         dev->pixelformat = formats[0].pixelformat;
1166         dev->buffersize = formats[0].buffersize;
1167         f->fmt.sdr.pixelformat = formats[0].pixelformat;
1168         f->fmt.sdr.buffersize = formats[0].buffersize;
1169
1170         return 0;
1171 }
1172
1173 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1174                 struct v4l2_format *f)
1175 {
1176         struct rtl2832_sdr_dev *dev = video_drvdata(file);
1177         struct platform_device *pdev = dev->pdev;
1178         int i;
1179
1180         dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1181                 (char *)&f->fmt.sdr.pixelformat);
1182
1183         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1184         for (i = 0; i < dev->num_formats; i++) {
1185                 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1186                         f->fmt.sdr.buffersize = formats[i].buffersize;
1187                         return 0;
1188                 }
1189         }
1190
1191         f->fmt.sdr.pixelformat = formats[0].pixelformat;
1192         f->fmt.sdr.buffersize = formats[0].buffersize;
1193
1194         return 0;
1195 }
1196
1197 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1198         .vidioc_querycap          = rtl2832_sdr_querycap,
1199
1200         .vidioc_enum_fmt_sdr_cap  = rtl2832_sdr_enum_fmt_sdr_cap,
1201         .vidioc_g_fmt_sdr_cap     = rtl2832_sdr_g_fmt_sdr_cap,
1202         .vidioc_s_fmt_sdr_cap     = rtl2832_sdr_s_fmt_sdr_cap,
1203         .vidioc_try_fmt_sdr_cap   = rtl2832_sdr_try_fmt_sdr_cap,
1204
1205         .vidioc_reqbufs           = vb2_ioctl_reqbufs,
1206         .vidioc_create_bufs       = vb2_ioctl_create_bufs,
1207         .vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
1208         .vidioc_querybuf          = vb2_ioctl_querybuf,
1209         .vidioc_qbuf              = vb2_ioctl_qbuf,
1210         .vidioc_dqbuf             = vb2_ioctl_dqbuf,
1211
1212         .vidioc_streamon          = vb2_ioctl_streamon,
1213         .vidioc_streamoff         = vb2_ioctl_streamoff,
1214
1215         .vidioc_g_tuner           = rtl2832_sdr_g_tuner,
1216         .vidioc_s_tuner           = rtl2832_sdr_s_tuner,
1217
1218         .vidioc_enum_freq_bands   = rtl2832_sdr_enum_freq_bands,
1219         .vidioc_g_frequency       = rtl2832_sdr_g_frequency,
1220         .vidioc_s_frequency       = rtl2832_sdr_s_frequency,
1221
1222         .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
1223         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1224         .vidioc_log_status        = v4l2_ctrl_log_status,
1225 };
1226
1227 static const struct v4l2_file_operations rtl2832_sdr_fops = {
1228         .owner                    = THIS_MODULE,
1229         .open                     = v4l2_fh_open,
1230         .release                  = vb2_fop_release,
1231         .read                     = vb2_fop_read,
1232         .poll                     = vb2_fop_poll,
1233         .mmap                     = vb2_fop_mmap,
1234         .unlocked_ioctl           = video_ioctl2,
1235 };
1236
1237 static struct video_device rtl2832_sdr_template = {
1238         .name                     = "Realtek RTL2832 SDR",
1239         .release                  = video_device_release_empty,
1240         .fops                     = &rtl2832_sdr_fops,
1241         .ioctl_ops                = &rtl2832_sdr_ioctl_ops,
1242         .device_caps              = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
1243                                     V4L2_CAP_READWRITE | V4L2_CAP_TUNER,
1244 };
1245
1246 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1247 {
1248         struct rtl2832_sdr_dev *dev =
1249                         container_of(ctrl->handler, struct rtl2832_sdr_dev,
1250                                         hdl);
1251         struct platform_device *pdev = dev->pdev;
1252         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1253         struct dvb_frontend *fe = pdata->dvb_frontend;
1254         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1255         int ret;
1256
1257         dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1258                 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1259                 ctrl->step);
1260
1261         switch (ctrl->id) {
1262         case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1263         case V4L2_CID_RF_TUNER_BANDWIDTH:
1264                 /* TODO: these controls should be moved to tuner drivers */
1265                 if (dev->bandwidth_auto->val) {
1266                         /* Round towards the closest legal value */
1267                         s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
1268                         u32 offset;
1269
1270                         val = clamp_t(s32, val, dev->bandwidth->minimum,
1271                                       dev->bandwidth->maximum);
1272                         offset = val - dev->bandwidth->minimum;
1273                         offset = dev->bandwidth->step *
1274                                 div_u64(offset, dev->bandwidth->step);
1275                         dev->bandwidth->val = dev->bandwidth->minimum + offset;
1276                 }
1277                 c->bandwidth_hz = dev->bandwidth->val;
1278
1279                 if (!test_bit(POWER_ON, &dev->flags))
1280                         return 0;
1281
1282                 if (fe->ops.tuner_ops.set_params)
1283                         ret = fe->ops.tuner_ops.set_params(fe);
1284                 else
1285                         ret = 0;
1286                 break;
1287         default:
1288                 ret = -EINVAL;
1289         }
1290
1291         return ret;
1292 }
1293
1294 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1295         .s_ctrl = rtl2832_sdr_s_ctrl,
1296 };
1297
1298 static void rtl2832_sdr_video_release(struct v4l2_device *v)
1299 {
1300         struct rtl2832_sdr_dev *dev =
1301                         container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1302         struct platform_device *pdev = dev->pdev;
1303
1304         dev_dbg(&pdev->dev, "\n");
1305
1306         v4l2_ctrl_handler_free(&dev->hdl);
1307         v4l2_device_unregister(&dev->v4l2_dev);
1308         kfree(dev);
1309 }
1310
1311 /* Platform driver interface */
1312 static int rtl2832_sdr_probe(struct platform_device *pdev)
1313 {
1314         struct rtl2832_sdr_dev *dev;
1315         struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1316         const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
1317         struct v4l2_subdev *subdev;
1318         int ret;
1319
1320         dev_dbg(&pdev->dev, "\n");
1321
1322         if (!pdata) {
1323                 dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1324                 ret = -EINVAL;
1325                 goto err;
1326         }
1327         if (!pdev->dev.parent->driver) {
1328                 dev_dbg(&pdev->dev, "No parent device\n");
1329                 ret = -EINVAL;
1330                 goto err;
1331         }
1332         /* try to refcount host drv since we are the consumer */
1333         if (!try_module_get(pdev->dev.parent->driver->owner)) {
1334                 dev_err(&pdev->dev, "Refcount fail");
1335                 ret = -EINVAL;
1336                 goto err;
1337         }
1338         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1339         if (dev == NULL) {
1340                 ret = -ENOMEM;
1341                 goto err_module_put;
1342         }
1343
1344         /* setup the state */
1345         subdev = pdata->v4l2_subdev;
1346         dev->v4l2_subdev = pdata->v4l2_subdev;
1347         dev->pdev = pdev;
1348         dev->regmap = pdata->regmap;
1349         dev->udev = pdata->dvb_usb_device->udev;
1350         dev->f_adc = bands_adc[0].rangelow;
1351         dev->f_tuner = bands_fm[0].rangelow;
1352         dev->pixelformat = formats[0].pixelformat;
1353         dev->buffersize = formats[0].buffersize;
1354         dev->num_formats = NUM_FORMATS;
1355         if (!rtl2832_sdr_emulated_fmt)
1356                 dev->num_formats -= 1;
1357
1358         mutex_init(&dev->v4l2_lock);
1359         mutex_init(&dev->vb_queue_lock);
1360         spin_lock_init(&dev->queued_bufs_lock);
1361         INIT_LIST_HEAD(&dev->queued_bufs);
1362
1363         /* Init videobuf2 queue structure */
1364         dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1365         dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1366         dev->vb_queue.drv_priv = dev;
1367         dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1368         dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1369         dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1370         dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1371         ret = vb2_queue_init(&dev->vb_queue);
1372         if (ret) {
1373                 dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1374                 goto err_kfree;
1375         }
1376
1377         /* Register controls */
1378         switch (pdata->tuner) {
1379         case RTL2832_SDR_TUNER_E4000:
1380                 v4l2_ctrl_handler_init(&dev->hdl, 9);
1381                 if (subdev)
1382                         v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1383                                               NULL, true);
1384                 break;
1385         case RTL2832_SDR_TUNER_R820T:
1386         case RTL2832_SDR_TUNER_R828D:
1387                 v4l2_ctrl_handler_init(&dev->hdl, 2);
1388                 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1389                                                         V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1390                                                         0, 1, 1, 1);
1391                 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1392                                                    V4L2_CID_RF_TUNER_BANDWIDTH,
1393                                                    0, 8000000, 100000, 0);
1394                 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1395                 break;
1396         case RTL2832_SDR_TUNER_FC0012:
1397         case RTL2832_SDR_TUNER_FC0013:
1398                 v4l2_ctrl_handler_init(&dev->hdl, 2);
1399                 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1400                                                         V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1401                                                         0, 1, 1, 1);
1402                 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1403                                                    V4L2_CID_RF_TUNER_BANDWIDTH,
1404                                                    6000000, 8000000, 1000000,
1405                                                    6000000);
1406                 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1407                 break;
1408         case RTL2832_SDR_TUNER_FC2580:
1409                 v4l2_ctrl_handler_init(&dev->hdl, 2);
1410                 if (subdev)
1411                         v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1412                                               NULL, true);
1413                 break;
1414         default:
1415                 v4l2_ctrl_handler_init(&dev->hdl, 0);
1416                 dev_err(&pdev->dev, "Unsupported tuner\n");
1417                 goto err_v4l2_ctrl_handler_free;
1418         }
1419         if (dev->hdl.error) {
1420                 ret = dev->hdl.error;
1421                 dev_err(&pdev->dev, "Could not initialize controls\n");
1422                 goto err_v4l2_ctrl_handler_free;
1423         }
1424
1425         /* Init video_device structure */
1426         dev->vdev = rtl2832_sdr_template;
1427         dev->vdev.queue = &dev->vb_queue;
1428         dev->vdev.queue->lock = &dev->vb_queue_lock;
1429         video_set_drvdata(&dev->vdev, dev);
1430
1431         /* Register the v4l2_device structure */
1432         dev->v4l2_dev.release = rtl2832_sdr_video_release;
1433         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1434         if (ret) {
1435                 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1436                 goto err_v4l2_ctrl_handler_free;
1437         }
1438
1439         dev->v4l2_dev.ctrl_handler = &dev->hdl;
1440         dev->vdev.v4l2_dev = &dev->v4l2_dev;
1441         dev->vdev.lock = &dev->v4l2_lock;
1442         dev->vdev.vfl_dir = VFL_DIR_RX;
1443
1444         ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1445         if (ret) {
1446                 dev_err(&pdev->dev, "Failed to register as video device %d\n",
1447                         ret);
1448                 goto err_v4l2_device_unregister;
1449         }
1450         dev_info(&pdev->dev, "Registered as %s\n",
1451                  video_device_node_name(&dev->vdev));
1452         dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1453         dev_notice(&pdev->dev,
1454                    "SDR API is still slightly experimental and functionality changes may follow\n");
1455         platform_set_drvdata(pdev, dev);
1456         return 0;
1457 err_v4l2_device_unregister:
1458         v4l2_device_unregister(&dev->v4l2_dev);
1459 err_v4l2_ctrl_handler_free:
1460         v4l2_ctrl_handler_free(&dev->hdl);
1461 err_kfree:
1462         kfree(dev);
1463 err_module_put:
1464         module_put(pdev->dev.parent->driver->owner);
1465 err:
1466         return ret;
1467 }
1468
1469 static int rtl2832_sdr_remove(struct platform_device *pdev)
1470 {
1471         struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1472
1473         dev_dbg(&pdev->dev, "\n");
1474
1475         mutex_lock(&dev->vb_queue_lock);
1476         mutex_lock(&dev->v4l2_lock);
1477         /* No need to keep the urbs around after disconnection */
1478         dev->udev = NULL;
1479         v4l2_device_disconnect(&dev->v4l2_dev);
1480         video_unregister_device(&dev->vdev);
1481         mutex_unlock(&dev->v4l2_lock);
1482         mutex_unlock(&dev->vb_queue_lock);
1483         v4l2_device_put(&dev->v4l2_dev);
1484         module_put(pdev->dev.parent->driver->owner);
1485
1486         return 0;
1487 }
1488
1489 static struct platform_driver rtl2832_sdr_driver = {
1490         .driver = {
1491                 .name   = "rtl2832_sdr",
1492         },
1493         .probe          = rtl2832_sdr_probe,
1494         .remove         = rtl2832_sdr_remove,
1495 };
1496 module_platform_driver(rtl2832_sdr_driver);
1497
1498 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1499 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1500 MODULE_LICENSE("GPL");