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