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