1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Realtek RTL2832U SDR driver
5 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
7 * GNU Radio plugin "gr-kernel" for device usage will be on:
8 * http://git.linuxtv.org/anttip/gr-kernel.git
11 #include "rtl2832_sdr.h"
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-ioctl.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-vmalloc.h>
21 #include <linux/platform_device.h>
22 #include <linux/jiffies.h>
23 #include <linux/math64.h>
24 #include <linux/regmap.h>
26 static bool rtl2832_sdr_emulated_fmt;
27 module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
28 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
30 /* Original macro does not contain enough null pointer checks for our need */
31 #define V4L2_SUBDEV_HAS_OP(sd, o, f) \
32 ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
34 #define MAX_BULK_BUFS (10)
35 #define BULK_BUFFER_SIZE (128 * 512)
37 static const struct v4l2_frequency_band bands_adc[] = {
40 .type = V4L2_TUNER_ADC,
42 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
48 .type = V4L2_TUNER_ADC,
50 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
56 .type = V4L2_TUNER_ADC,
58 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
64 static const struct v4l2_frequency_band bands_fm[] = {
67 .type = V4L2_TUNER_RF,
69 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
71 .rangehigh = 2000000000,
76 struct rtl2832_sdr_format {
82 static struct rtl2832_sdr_format formats[] = {
85 .pixelformat = V4L2_SDR_FMT_CU8,
86 .buffersize = BULK_BUFFER_SIZE,
88 .name = "Complex U16LE (emulated)",
89 .pixelformat = V4L2_SDR_FMT_CU16LE,
90 .buffersize = BULK_BUFFER_SIZE * 2,
94 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
96 /* intermediate buffers with raw data from the USB device */
97 struct rtl2832_sdr_frame_buf {
98 /* common v4l buffer stuff -- must be first */
99 struct vb2_v4l2_buffer vb;
100 struct list_head list;
103 struct rtl2832_sdr_dev {
104 #define POWER_ON 0 /* BIT(0) */
105 #define URB_BUF 1 /* BIT(1) */
108 struct platform_device *pdev;
109 struct regmap *regmap;
111 struct video_device vdev;
112 struct v4l2_device v4l2_dev;
113 struct v4l2_subdev *v4l2_subdev;
115 /* videobuf2 queue and queued buffers list */
116 struct vb2_queue vb_queue;
117 struct list_head queued_bufs;
118 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
119 unsigned sequence; /* buffer sequence counter */
121 /* Note if taking both locks v4l2_lock must always be locked first! */
122 struct mutex v4l2_lock; /* Protects everything else */
123 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
125 /* Pointer to our usb_device, will be NULL after unplug */
126 struct usb_device *udev; /* Both mutexes most be hold when setting! */
128 unsigned int vb_full; /* vb is full and packets dropped */
130 struct urb *urb_list[MAX_BULK_BUFS];
132 unsigned long buf_size;
133 u8 *buf_list[MAX_BULK_BUFS];
134 dma_addr_t dma_addr[MAX_BULK_BUFS];
135 int urbs_initialized;
138 unsigned int f_adc, f_tuner;
141 unsigned int num_formats;
144 struct v4l2_ctrl_handler hdl;
145 struct v4l2_ctrl *bandwidth_auto;
146 struct v4l2_ctrl *bandwidth;
148 /* for sample rate calc */
150 unsigned int sample_measured;
151 unsigned long jiffies_next;
154 /* Private functions */
155 static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
156 struct rtl2832_sdr_dev *dev)
159 struct rtl2832_sdr_frame_buf *buf = NULL;
161 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
162 if (list_empty(&dev->queued_bufs))
165 buf = list_entry(dev->queued_bufs.next,
166 struct rtl2832_sdr_frame_buf, list);
167 list_del(&buf->list);
169 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
173 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
174 void *dst, const u8 *src, unsigned int src_len)
176 struct platform_device *pdev = dev->pdev;
177 unsigned int dst_len;
179 if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
180 /* native stream, no need to convert */
181 memcpy(dst, src, src_len);
183 } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
184 /* convert u8 to u16 */
188 for (i = 0; i < src_len; i++)
189 *u16dst++ = (src[i] << 8) | (src[i] >> 0);
190 dst_len = 2 * src_len;
195 /* calculate sample rate and output it in 10 seconds intervals */
196 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
197 #define MSECS 10000UL
198 unsigned int msecs = jiffies_to_msecs(jiffies -
199 dev->jiffies_next + msecs_to_jiffies(MSECS));
200 unsigned int samples = dev->sample - dev->sample_measured;
202 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
203 dev->sample_measured = dev->sample;
205 "slen=%u samples=%u msecs=%u sample rate=%lu\n",
206 src_len, samples, msecs, samples * 1000UL / msecs);
209 /* total number of I+Q pairs */
210 dev->sample += src_len / 2;
216 * This gets called for the bulk stream pipe. This is done in interrupt
217 * time, so it has to be fast, not crash, and not stall. Neat.
219 static void rtl2832_sdr_urb_complete(struct urb *urb)
221 struct rtl2832_sdr_dev *dev = urb->context;
222 struct platform_device *pdev = dev->pdev;
223 struct rtl2832_sdr_frame_buf *fbuf;
225 dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
226 urb->status, urb->actual_length,
227 urb->transfer_buffer_length, urb->error_count);
229 switch (urb->status) {
230 case 0: /* success */
231 case -ETIMEDOUT: /* NAK */
233 case -ECONNRESET: /* kill */
238 dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
242 if (likely(urb->actual_length > 0)) {
245 /* get free framebuffer */
246 fbuf = rtl2832_sdr_get_next_fill_buf(dev);
247 if (unlikely(fbuf == NULL)) {
249 dev_notice_ratelimited(&pdev->dev,
250 "videobuf is full, %d packets dropped\n",
255 /* fill framebuffer */
256 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
257 len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
259 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
260 fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
261 fbuf->vb.sequence = dev->sequence++;
262 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
265 usb_submit_urb(urb, GFP_ATOMIC);
268 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
270 struct platform_device *pdev = dev->pdev;
273 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
274 dev_dbg(&pdev->dev, "kill urb=%d\n", i);
276 usb_kill_urb(dev->urb_list[i]);
278 dev->urbs_submitted = 0;
283 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
285 struct platform_device *pdev = dev->pdev;
288 for (i = 0; i < dev->urbs_initialized; i++) {
289 dev_dbg(&pdev->dev, "submit urb=%d\n", i);
290 ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL);
293 "Could not submit urb no. %d - get them all back\n",
295 rtl2832_sdr_kill_urbs(dev);
298 dev->urbs_submitted++;
304 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
306 struct platform_device *pdev = dev->pdev;
308 if (test_bit(URB_BUF, &dev->flags)) {
309 while (dev->buf_num) {
311 dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
312 usb_free_coherent(dev->udev, dev->buf_size,
313 dev->buf_list[dev->buf_num],
314 dev->dma_addr[dev->buf_num]);
317 clear_bit(URB_BUF, &dev->flags);
322 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
324 struct platform_device *pdev = dev->pdev;
327 dev->buf_size = BULK_BUFFER_SIZE;
329 dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
330 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
332 for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
333 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
334 BULK_BUFFER_SIZE, GFP_KERNEL,
335 &dev->dma_addr[dev->buf_num]);
336 if (!dev->buf_list[dev->buf_num]) {
337 dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
339 rtl2832_sdr_free_stream_bufs(dev);
343 dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
344 dev->buf_num, dev->buf_list[dev->buf_num],
345 (long long)dev->dma_addr[dev->buf_num]);
346 set_bit(URB_BUF, &dev->flags);
352 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
354 struct platform_device *pdev = dev->pdev;
357 rtl2832_sdr_kill_urbs(dev);
359 for (i = dev->urbs_initialized - 1; i >= 0; i--) {
360 if (dev->urb_list[i]) {
361 dev_dbg(&pdev->dev, "free urb=%d\n", i);
363 usb_free_urb(dev->urb_list[i]);
366 dev->urbs_initialized = 0;
371 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
373 struct platform_device *pdev = dev->pdev;
376 /* allocate the URBs */
377 for (i = 0; i < MAX_BULK_BUFS; i++) {
378 dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
379 dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL);
380 if (!dev->urb_list[i]) {
381 for (j = 0; j < i; j++)
382 usb_free_urb(dev->urb_list[j]);
385 usb_fill_bulk_urb(dev->urb_list[i],
387 usb_rcvbulkpipe(dev->udev, 0x81),
390 rtl2832_sdr_urb_complete, dev);
392 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
393 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
394 dev->urbs_initialized++;
400 /* Must be called with vb_queue_lock hold */
401 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
403 struct platform_device *pdev = dev->pdev;
406 dev_dbg(&pdev->dev, "\n");
408 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
409 while (!list_empty(&dev->queued_bufs)) {
410 struct rtl2832_sdr_frame_buf *buf;
412 buf = list_entry(dev->queued_bufs.next,
413 struct rtl2832_sdr_frame_buf, list);
414 list_del(&buf->list);
415 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
417 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
420 static int rtl2832_sdr_querycap(struct file *file, void *fh,
421 struct v4l2_capability *cap)
423 struct rtl2832_sdr_dev *dev = video_drvdata(file);
424 struct platform_device *pdev = dev->pdev;
426 dev_dbg(&pdev->dev, "\n");
428 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
429 strscpy(cap->card, dev->vdev.name, sizeof(cap->card));
430 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
434 /* Videobuf2 operations */
435 static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
436 unsigned int *nbuffers,
437 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
439 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
440 struct platform_device *pdev = dev->pdev;
442 dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
444 /* Need at least 8 buffers */
445 if (vq->num_buffers + *nbuffers < 8)
446 *nbuffers = 8 - vq->num_buffers;
448 sizes[0] = PAGE_ALIGN(dev->buffersize);
449 dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
453 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
455 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
457 /* Don't allow queueing new buffers after device disconnection */
464 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
466 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
467 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
468 struct rtl2832_sdr_frame_buf *buf =
469 container_of(vbuf, struct rtl2832_sdr_frame_buf, vb);
472 /* Check the device has not disconnected between prep and queuing */
474 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
478 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
479 list_add_tail(&buf->list, &dev->queued_bufs);
480 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
483 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
485 struct platform_device *pdev = dev->pdev;
486 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
487 struct dvb_frontend *fe = pdata->dvb_frontend;
489 unsigned int f_sr, f_if;
490 u8 buf[4], u8tmp1, u8tmp2;
494 dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
496 if (!test_bit(POWER_ON, &dev->flags))
504 ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2);
508 ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4);
512 /* get IF from tuner */
513 if (fe->ops.tuner_ops.get_if_frequency)
514 ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
522 u64tmp = f_if % pdata->clk;
524 u64tmp = div_u64(u64tmp, pdata->clk);
526 u32tmp = u64tmp & 0x3fffff;
528 dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
530 buf[0] = (u32tmp >> 16) & 0xff;
531 buf[1] = (u32tmp >> 8) & 0xff;
532 buf[2] = (u32tmp >> 0) & 0xff;
534 ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3);
539 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
541 u8tmp1 = 0x1a; /* disable Zero-IF */
542 u8tmp2 = 0x8d; /* enable ADC I */
544 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
545 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
548 ret = regmap_write(dev->regmap, 0x1b1, u8tmp1);
552 ret = regmap_write(dev->regmap, 0x008, u8tmp2);
556 ret = regmap_write(dev->regmap, 0x006, 0x80);
560 /* program sampling rate (resampling down) */
561 u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
563 buf[0] = (u32tmp >> 24) & 0xff;
564 buf[1] = (u32tmp >> 16) & 0xff;
565 buf[2] = (u32tmp >> 8) & 0xff;
566 buf[3] = (u32tmp >> 0) & 0xff;
567 ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4);
571 /* low-pass filter */
572 ret = regmap_bulk_write(dev->regmap, 0x11c,
573 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
578 ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
583 ret = regmap_write(dev->regmap, 0x019, 0x05);
587 ret = regmap_bulk_write(dev->regmap, 0x01a,
588 "\x1b\x16\x0d\x06\x01\xff", 6);
593 ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
598 ret = regmap_write(dev->regmap, 0x061, 0x60);
602 /* used RF tuner based settings */
603 switch (pdata->tuner) {
604 case RTL2832_SDR_TUNER_E4000:
605 ret = regmap_write(dev->regmap, 0x112, 0x5a);
606 ret = regmap_write(dev->regmap, 0x102, 0x40);
607 ret = regmap_write(dev->regmap, 0x103, 0x5a);
608 ret = regmap_write(dev->regmap, 0x1c7, 0x30);
609 ret = regmap_write(dev->regmap, 0x104, 0xd0);
610 ret = regmap_write(dev->regmap, 0x105, 0xbe);
611 ret = regmap_write(dev->regmap, 0x1c8, 0x18);
612 ret = regmap_write(dev->regmap, 0x106, 0x35);
613 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
614 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
615 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
616 ret = regmap_write(dev->regmap, 0x107, 0x40);
617 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
618 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
619 ret = regmap_write(dev->regmap, 0x108, 0x80);
620 ret = regmap_write(dev->regmap, 0x109, 0x7f);
621 ret = regmap_write(dev->regmap, 0x10a, 0x80);
622 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
623 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
624 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
625 ret = regmap_write(dev->regmap, 0x011, 0xd4);
626 ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
627 ret = regmap_write(dev->regmap, 0x1d9, 0x00);
628 ret = regmap_write(dev->regmap, 0x1db, 0x00);
629 ret = regmap_write(dev->regmap, 0x1dd, 0x14);
630 ret = regmap_write(dev->regmap, 0x1de, 0xec);
631 ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
632 ret = regmap_write(dev->regmap, 0x1e6, 0x02);
633 ret = regmap_write(dev->regmap, 0x1d7, 0x09);
634 ret = regmap_write(dev->regmap, 0x00d, 0x83);
635 ret = regmap_write(dev->regmap, 0x010, 0x49);
636 ret = regmap_write(dev->regmap, 0x00d, 0x87);
637 ret = regmap_write(dev->regmap, 0x00d, 0x85);
638 ret = regmap_write(dev->regmap, 0x013, 0x02);
640 case RTL2832_SDR_TUNER_FC0012:
641 case RTL2832_SDR_TUNER_FC0013:
642 ret = regmap_write(dev->regmap, 0x112, 0x5a);
643 ret = regmap_write(dev->regmap, 0x102, 0x40);
644 ret = regmap_write(dev->regmap, 0x103, 0x5a);
645 ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
646 ret = regmap_write(dev->regmap, 0x104, 0xcc);
647 ret = regmap_write(dev->regmap, 0x105, 0xbe);
648 ret = regmap_write(dev->regmap, 0x1c8, 0x16);
649 ret = regmap_write(dev->regmap, 0x106, 0x35);
650 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
651 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
652 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
653 ret = regmap_write(dev->regmap, 0x107, 0x40);
654 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
655 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
656 ret = regmap_write(dev->regmap, 0x108, 0x80);
657 ret = regmap_write(dev->regmap, 0x109, 0x7f);
658 ret = regmap_write(dev->regmap, 0x10a, 0x80);
659 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
660 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
661 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
662 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2);
663 ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
664 ret = regmap_write(dev->regmap, 0x1d9, 0x00);
665 ret = regmap_write(dev->regmap, 0x1db, 0x00);
666 ret = regmap_write(dev->regmap, 0x1dd, 0x11);
667 ret = regmap_write(dev->regmap, 0x1de, 0xef);
668 ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
669 ret = regmap_write(dev->regmap, 0x1e6, 0x02);
670 ret = regmap_write(dev->regmap, 0x1d7, 0x09);
672 case RTL2832_SDR_TUNER_R820T:
673 case RTL2832_SDR_TUNER_R828D:
674 ret = regmap_write(dev->regmap, 0x112, 0x5a);
675 ret = regmap_write(dev->regmap, 0x102, 0x40);
676 ret = regmap_write(dev->regmap, 0x115, 0x01);
677 ret = regmap_write(dev->regmap, 0x103, 0x80);
678 ret = regmap_write(dev->regmap, 0x1c7, 0x24);
679 ret = regmap_write(dev->regmap, 0x104, 0xcc);
680 ret = regmap_write(dev->regmap, 0x105, 0xbe);
681 ret = regmap_write(dev->regmap, 0x1c8, 0x14);
682 ret = regmap_write(dev->regmap, 0x106, 0x35);
683 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
684 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
685 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
686 ret = regmap_write(dev->regmap, 0x107, 0x40);
687 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
688 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
689 ret = regmap_write(dev->regmap, 0x108, 0x80);
690 ret = regmap_write(dev->regmap, 0x109, 0x7f);
691 ret = regmap_write(dev->regmap, 0x10a, 0x80);
692 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
693 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
694 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
695 ret = regmap_write(dev->regmap, 0x011, 0xf4);
697 case RTL2832_SDR_TUNER_FC2580:
698 ret = regmap_write(dev->regmap, 0x112, 0x39);
699 ret = regmap_write(dev->regmap, 0x102, 0x40);
700 ret = regmap_write(dev->regmap, 0x103, 0x5a);
701 ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
702 ret = regmap_write(dev->regmap, 0x104, 0xcc);
703 ret = regmap_write(dev->regmap, 0x105, 0xbe);
704 ret = regmap_write(dev->regmap, 0x1c8, 0x16);
705 ret = regmap_write(dev->regmap, 0x106, 0x35);
706 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
707 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
708 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
709 ret = regmap_write(dev->regmap, 0x107, 0x40);
710 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
711 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
712 ret = regmap_write(dev->regmap, 0x108, 0x80);
713 ret = regmap_write(dev->regmap, 0x109, 0x7f);
714 ret = regmap_write(dev->regmap, 0x10a, 0x9c);
715 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
716 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
717 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
718 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2);
721 dev_notice(&pdev->dev, "Unsupported tuner\n");
725 ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04);
729 ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00);
736 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
738 struct platform_device *pdev = dev->pdev;
741 dev_dbg(&pdev->dev, "\n");
744 ret = regmap_write(dev->regmap, 0x061, 0xe0);
749 ret = regmap_write(dev->regmap, 0x019, 0x20);
753 ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
758 ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
762 ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2);
766 ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4);
773 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
775 struct platform_device *pdev = dev->pdev;
776 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
777 struct dvb_frontend *fe = pdata->dvb_frontend;
778 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
779 struct v4l2_ctrl *bandwidth_auto;
780 struct v4l2_ctrl *bandwidth;
785 if (dev->f_tuner == 0)
791 bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
792 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
793 bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
794 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
795 c->bandwidth_hz = dev->f_adc;
796 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
798 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
801 c->frequency = dev->f_tuner;
802 c->delivery_system = SYS_DVBT;
804 dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
805 c->frequency, c->bandwidth_hz);
807 if (!test_bit(POWER_ON, &dev->flags))
810 if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
811 if (fe->ops.tuner_ops.set_params)
812 fe->ops.tuner_ops.set_params(fe);
818 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
820 struct platform_device *pdev = dev->pdev;
821 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
822 struct dvb_frontend *fe = pdata->dvb_frontend;
824 dev_dbg(&pdev->dev, "\n");
826 if (fe->ops.tuner_ops.init)
827 fe->ops.tuner_ops.init(fe);
832 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
834 struct platform_device *pdev = dev->pdev;
835 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
836 struct dvb_frontend *fe = pdata->dvb_frontend;
838 dev_dbg(&pdev->dev, "\n");
840 if (fe->ops.tuner_ops.sleep)
841 fe->ops.tuner_ops.sleep(fe);
846 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
848 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
849 struct platform_device *pdev = dev->pdev;
850 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
851 struct dvb_usb_device *d = pdata->dvb_usb_device;
854 dev_dbg(&pdev->dev, "\n");
859 if (mutex_lock_interruptible(&dev->v4l2_lock))
862 if (d->props->power_ctrl)
863 d->props->power_ctrl(d, 1);
866 if (d->props->frontend_ctrl)
867 d->props->frontend_ctrl(pdata->dvb_frontend, 1);
869 set_bit(POWER_ON, &dev->flags);
872 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
873 ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
875 ret = rtl2832_sdr_set_tuner(dev);
879 ret = rtl2832_sdr_set_tuner_freq(dev);
883 ret = rtl2832_sdr_set_adc(dev);
887 ret = rtl2832_sdr_alloc_stream_bufs(dev);
891 ret = rtl2832_sdr_alloc_urbs(dev);
897 ret = rtl2832_sdr_submit_urbs(dev);
902 mutex_unlock(&dev->v4l2_lock);
907 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
909 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
910 struct platform_device *pdev = dev->pdev;
911 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
912 struct dvb_usb_device *d = pdata->dvb_usb_device;
914 dev_dbg(&pdev->dev, "\n");
916 mutex_lock(&dev->v4l2_lock);
918 rtl2832_sdr_kill_urbs(dev);
919 rtl2832_sdr_free_urbs(dev);
920 rtl2832_sdr_free_stream_bufs(dev);
921 rtl2832_sdr_cleanup_queued_bufs(dev);
922 rtl2832_sdr_unset_adc(dev);
925 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
926 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
928 rtl2832_sdr_unset_tuner(dev);
930 clear_bit(POWER_ON, &dev->flags);
933 if (d->props->frontend_ctrl)
934 d->props->frontend_ctrl(pdata->dvb_frontend, 0);
936 if (d->props->power_ctrl)
937 d->props->power_ctrl(d, 0);
939 mutex_unlock(&dev->v4l2_lock);
942 static const struct vb2_ops rtl2832_sdr_vb2_ops = {
943 .queue_setup = rtl2832_sdr_queue_setup,
944 .buf_prepare = rtl2832_sdr_buf_prepare,
945 .buf_queue = rtl2832_sdr_buf_queue,
946 .start_streaming = rtl2832_sdr_start_streaming,
947 .stop_streaming = rtl2832_sdr_stop_streaming,
948 .wait_prepare = vb2_ops_wait_prepare,
949 .wait_finish = vb2_ops_wait_finish,
952 static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
953 struct v4l2_tuner *v)
955 struct rtl2832_sdr_dev *dev = video_drvdata(file);
956 struct platform_device *pdev = dev->pdev;
959 dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
962 strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
963 v->type = V4L2_TUNER_ADC;
964 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
965 v->rangelow = 300000;
966 v->rangehigh = 3200000;
968 } else if (v->index == 1 &&
969 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
970 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
971 } else if (v->index == 1) {
972 strscpy(v->name, "RF: <unknown>", sizeof(v->name));
973 v->type = V4L2_TUNER_RF;
974 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
975 v->rangelow = 50000000;
976 v->rangehigh = 2000000000;
984 static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
985 const struct v4l2_tuner *v)
987 struct rtl2832_sdr_dev *dev = video_drvdata(file);
988 struct platform_device *pdev = dev->pdev;
991 dev_dbg(&pdev->dev, "\n");
995 } else if (v->index == 1 &&
996 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
997 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
998 } else if (v->index == 1) {
1006 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
1007 struct v4l2_frequency_band *band)
1009 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1010 struct platform_device *pdev = dev->pdev;
1013 dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1014 band->tuner, band->type, band->index);
1016 if (band->tuner == 0) {
1017 if (band->index >= ARRAY_SIZE(bands_adc))
1020 *band = bands_adc[band->index];
1022 } else if (band->tuner == 1 &&
1023 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
1024 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
1025 } else if (band->tuner == 1) {
1026 if (band->index >= ARRAY_SIZE(bands_fm))
1029 *band = bands_fm[band->index];
1037 static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1038 struct v4l2_frequency *f)
1040 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1041 struct platform_device *pdev = dev->pdev;
1044 dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1046 if (f->tuner == 0) {
1047 f->frequency = dev->f_adc;
1048 f->type = V4L2_TUNER_ADC;
1050 } else if (f->tuner == 1 &&
1051 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
1052 f->type = V4L2_TUNER_RF;
1053 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
1054 } else if (f->tuner == 1) {
1055 f->frequency = dev->f_tuner;
1056 f->type = V4L2_TUNER_RF;
1064 static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1065 const struct v4l2_frequency *f)
1067 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1068 struct platform_device *pdev = dev->pdev;
1071 dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1072 f->tuner, f->type, f->frequency);
1074 /* ADC band midpoints */
1075 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1076 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1078 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1079 if (f->frequency < BAND_ADC_0)
1081 else if (f->frequency < BAND_ADC_1)
1086 dev->f_adc = clamp_t(unsigned int, f->frequency,
1087 bands_adc[band].rangelow,
1088 bands_adc[band].rangehigh);
1090 dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1091 ret = rtl2832_sdr_set_adc(dev);
1092 } else if (f->tuner == 1 &&
1093 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
1094 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
1095 } else if (f->tuner == 1) {
1096 dev->f_tuner = clamp_t(unsigned int, f->frequency,
1097 bands_fm[0].rangelow,
1098 bands_fm[0].rangehigh);
1099 dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
1101 ret = rtl2832_sdr_set_tuner_freq(dev);
1108 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1109 struct v4l2_fmtdesc *f)
1111 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1112 struct platform_device *pdev = dev->pdev;
1114 dev_dbg(&pdev->dev, "\n");
1116 if (f->index >= dev->num_formats)
1119 strscpy(f->description, formats[f->index].name, sizeof(f->description));
1120 f->pixelformat = formats[f->index].pixelformat;
1125 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1126 struct v4l2_format *f)
1128 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1129 struct platform_device *pdev = dev->pdev;
1131 dev_dbg(&pdev->dev, "\n");
1133 f->fmt.sdr.pixelformat = dev->pixelformat;
1134 f->fmt.sdr.buffersize = dev->buffersize;
1136 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1141 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1142 struct v4l2_format *f)
1144 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1145 struct platform_device *pdev = dev->pdev;
1146 struct vb2_queue *q = &dev->vb_queue;
1149 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1150 (char *)&f->fmt.sdr.pixelformat);
1155 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1156 for (i = 0; i < dev->num_formats; i++) {
1157 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1158 dev->pixelformat = formats[i].pixelformat;
1159 dev->buffersize = formats[i].buffersize;
1160 f->fmt.sdr.buffersize = formats[i].buffersize;
1165 dev->pixelformat = formats[0].pixelformat;
1166 dev->buffersize = formats[0].buffersize;
1167 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1168 f->fmt.sdr.buffersize = formats[0].buffersize;
1173 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1174 struct v4l2_format *f)
1176 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1177 struct platform_device *pdev = dev->pdev;
1180 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1181 (char *)&f->fmt.sdr.pixelformat);
1183 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1184 for (i = 0; i < dev->num_formats; i++) {
1185 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1186 f->fmt.sdr.buffersize = formats[i].buffersize;
1191 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1192 f->fmt.sdr.buffersize = formats[0].buffersize;
1197 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1198 .vidioc_querycap = rtl2832_sdr_querycap,
1200 .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap,
1201 .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap,
1202 .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap,
1203 .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap,
1205 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1206 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1207 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1208 .vidioc_querybuf = vb2_ioctl_querybuf,
1209 .vidioc_qbuf = vb2_ioctl_qbuf,
1210 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1212 .vidioc_streamon = vb2_ioctl_streamon,
1213 .vidioc_streamoff = vb2_ioctl_streamoff,
1215 .vidioc_g_tuner = rtl2832_sdr_g_tuner,
1216 .vidioc_s_tuner = rtl2832_sdr_s_tuner,
1218 .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands,
1219 .vidioc_g_frequency = rtl2832_sdr_g_frequency,
1220 .vidioc_s_frequency = rtl2832_sdr_s_frequency,
1222 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1223 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1224 .vidioc_log_status = v4l2_ctrl_log_status,
1227 static const struct v4l2_file_operations rtl2832_sdr_fops = {
1228 .owner = THIS_MODULE,
1229 .open = v4l2_fh_open,
1230 .release = vb2_fop_release,
1231 .read = vb2_fop_read,
1232 .poll = vb2_fop_poll,
1233 .mmap = vb2_fop_mmap,
1234 .unlocked_ioctl = video_ioctl2,
1237 static struct video_device rtl2832_sdr_template = {
1238 .name = "Realtek RTL2832 SDR",
1239 .release = video_device_release_empty,
1240 .fops = &rtl2832_sdr_fops,
1241 .ioctl_ops = &rtl2832_sdr_ioctl_ops,
1242 .device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
1243 V4L2_CAP_READWRITE | V4L2_CAP_TUNER,
1246 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1248 struct rtl2832_sdr_dev *dev =
1249 container_of(ctrl->handler, struct rtl2832_sdr_dev,
1251 struct platform_device *pdev = dev->pdev;
1252 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1253 struct dvb_frontend *fe = pdata->dvb_frontend;
1254 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1257 dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1258 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1262 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1263 case V4L2_CID_RF_TUNER_BANDWIDTH:
1264 /* TODO: these controls should be moved to tuner drivers */
1265 if (dev->bandwidth_auto->val) {
1266 /* Round towards the closest legal value */
1267 s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
1270 val = clamp_t(s32, val, dev->bandwidth->minimum,
1271 dev->bandwidth->maximum);
1272 offset = val - dev->bandwidth->minimum;
1273 offset = dev->bandwidth->step *
1274 div_u64(offset, dev->bandwidth->step);
1275 dev->bandwidth->val = dev->bandwidth->minimum + offset;
1277 c->bandwidth_hz = dev->bandwidth->val;
1279 if (!test_bit(POWER_ON, &dev->flags))
1282 if (fe->ops.tuner_ops.set_params)
1283 ret = fe->ops.tuner_ops.set_params(fe);
1294 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1295 .s_ctrl = rtl2832_sdr_s_ctrl,
1298 static void rtl2832_sdr_video_release(struct v4l2_device *v)
1300 struct rtl2832_sdr_dev *dev =
1301 container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1302 struct platform_device *pdev = dev->pdev;
1304 dev_dbg(&pdev->dev, "\n");
1306 v4l2_ctrl_handler_free(&dev->hdl);
1307 v4l2_device_unregister(&dev->v4l2_dev);
1311 /* Platform driver interface */
1312 static int rtl2832_sdr_probe(struct platform_device *pdev)
1314 struct rtl2832_sdr_dev *dev;
1315 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1316 const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
1317 struct v4l2_subdev *subdev;
1320 dev_dbg(&pdev->dev, "\n");
1323 dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1327 if (!pdev->dev.parent->driver) {
1328 dev_dbg(&pdev->dev, "No parent device\n");
1332 /* try to refcount host drv since we are the consumer */
1333 if (!try_module_get(pdev->dev.parent->driver->owner)) {
1334 dev_err(&pdev->dev, "Refcount fail");
1338 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1341 goto err_module_put;
1344 /* setup the state */
1345 subdev = pdata->v4l2_subdev;
1346 dev->v4l2_subdev = pdata->v4l2_subdev;
1348 dev->regmap = pdata->regmap;
1349 dev->udev = pdata->dvb_usb_device->udev;
1350 dev->f_adc = bands_adc[0].rangelow;
1351 dev->f_tuner = bands_fm[0].rangelow;
1352 dev->pixelformat = formats[0].pixelformat;
1353 dev->buffersize = formats[0].buffersize;
1354 dev->num_formats = NUM_FORMATS;
1355 if (!rtl2832_sdr_emulated_fmt)
1356 dev->num_formats -= 1;
1358 mutex_init(&dev->v4l2_lock);
1359 mutex_init(&dev->vb_queue_lock);
1360 spin_lock_init(&dev->queued_bufs_lock);
1361 INIT_LIST_HEAD(&dev->queued_bufs);
1363 /* Init videobuf2 queue structure */
1364 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1365 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1366 dev->vb_queue.drv_priv = dev;
1367 dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1368 dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1369 dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1370 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1371 ret = vb2_queue_init(&dev->vb_queue);
1373 dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1377 /* Register controls */
1378 switch (pdata->tuner) {
1379 case RTL2832_SDR_TUNER_E4000:
1380 v4l2_ctrl_handler_init(&dev->hdl, 9);
1382 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1385 case RTL2832_SDR_TUNER_R820T:
1386 case RTL2832_SDR_TUNER_R828D:
1387 v4l2_ctrl_handler_init(&dev->hdl, 2);
1388 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1389 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1391 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1392 V4L2_CID_RF_TUNER_BANDWIDTH,
1393 0, 8000000, 100000, 0);
1394 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1396 case RTL2832_SDR_TUNER_FC0012:
1397 case RTL2832_SDR_TUNER_FC0013:
1398 v4l2_ctrl_handler_init(&dev->hdl, 2);
1399 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1400 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1402 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1403 V4L2_CID_RF_TUNER_BANDWIDTH,
1404 6000000, 8000000, 1000000,
1406 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1408 case RTL2832_SDR_TUNER_FC2580:
1409 v4l2_ctrl_handler_init(&dev->hdl, 2);
1411 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1415 v4l2_ctrl_handler_init(&dev->hdl, 0);
1416 dev_err(&pdev->dev, "Unsupported tuner\n");
1417 goto err_v4l2_ctrl_handler_free;
1419 if (dev->hdl.error) {
1420 ret = dev->hdl.error;
1421 dev_err(&pdev->dev, "Could not initialize controls\n");
1422 goto err_v4l2_ctrl_handler_free;
1425 /* Init video_device structure */
1426 dev->vdev = rtl2832_sdr_template;
1427 dev->vdev.queue = &dev->vb_queue;
1428 dev->vdev.queue->lock = &dev->vb_queue_lock;
1429 video_set_drvdata(&dev->vdev, dev);
1431 /* Register the v4l2_device structure */
1432 dev->v4l2_dev.release = rtl2832_sdr_video_release;
1433 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1435 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1436 goto err_v4l2_ctrl_handler_free;
1439 dev->v4l2_dev.ctrl_handler = &dev->hdl;
1440 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1441 dev->vdev.lock = &dev->v4l2_lock;
1442 dev->vdev.vfl_dir = VFL_DIR_RX;
1444 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1446 dev_err(&pdev->dev, "Failed to register as video device %d\n",
1448 goto err_v4l2_device_unregister;
1450 dev_info(&pdev->dev, "Registered as %s\n",
1451 video_device_node_name(&dev->vdev));
1452 dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1453 dev_notice(&pdev->dev,
1454 "SDR API is still slightly experimental and functionality changes may follow\n");
1455 platform_set_drvdata(pdev, dev);
1457 err_v4l2_device_unregister:
1458 v4l2_device_unregister(&dev->v4l2_dev);
1459 err_v4l2_ctrl_handler_free:
1460 v4l2_ctrl_handler_free(&dev->hdl);
1464 module_put(pdev->dev.parent->driver->owner);
1469 static int rtl2832_sdr_remove(struct platform_device *pdev)
1471 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1473 dev_dbg(&pdev->dev, "\n");
1475 mutex_lock(&dev->vb_queue_lock);
1476 mutex_lock(&dev->v4l2_lock);
1477 /* No need to keep the urbs around after disconnection */
1479 v4l2_device_disconnect(&dev->v4l2_dev);
1480 video_unregister_device(&dev->vdev);
1481 mutex_unlock(&dev->v4l2_lock);
1482 mutex_unlock(&dev->vb_queue_lock);
1483 v4l2_device_put(&dev->v4l2_dev);
1484 module_put(pdev->dev.parent->driver->owner);
1489 static struct platform_driver rtl2832_sdr_driver = {
1491 .name = "rtl2832_sdr",
1493 .probe = rtl2832_sdr_probe,
1494 .remove = rtl2832_sdr_remove,
1496 module_platform_driver(rtl2832_sdr_driver);
1498 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1499 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1500 MODULE_LICENSE("GPL");