2 * Realtek RTL2832U SDR driver
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
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.
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.
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.
20 * GNU Radio plugin "gr-kernel" for device usage will be on:
21 * http://git.linuxtv.org/anttip/gr-kernel.git
25 #include "rtl2832_sdr.h"
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>
35 #include <linux/platform_device.h>
36 #include <linux/jiffies.h>
37 #include <linux/math64.h>
38 #include <linux/regmap.h>
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)");
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)
48 #define MAX_BULK_BUFS (10)
49 #define BULK_BUFFER_SIZE (128 * 512)
51 static const struct v4l2_frequency_band bands_adc[] = {
54 .type = V4L2_TUNER_ADC,
56 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
62 .type = V4L2_TUNER_ADC,
64 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
70 .type = V4L2_TUNER_ADC,
72 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
78 static const struct v4l2_frequency_band bands_fm[] = {
81 .type = V4L2_TUNER_RF,
83 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
85 .rangehigh = 2000000000,
90 struct rtl2832_sdr_format {
96 static struct rtl2832_sdr_format formats[] = {
99 .pixelformat = V4L2_SDR_FMT_CU8,
100 .buffersize = BULK_BUFFER_SIZE,
102 .name = "Complex U16LE (emulated)",
103 .pixelformat = V4L2_SDR_FMT_CU16LE,
104 .buffersize = BULK_BUFFER_SIZE * 2,
108 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
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;
117 struct rtl2832_sdr_dev {
118 #define POWER_ON 0 /* BIT(0) */
119 #define URB_BUF 1 /* BIT(1) */
122 struct platform_device *pdev;
124 struct video_device vdev;
125 struct v4l2_device v4l2_dev;
126 struct v4l2_subdev *v4l2_subdev;
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 */
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 */
138 /* Pointer to our usb_device, will be NULL after unplug */
139 struct usb_device *udev; /* Both mutexes most be hold when setting! */
141 unsigned int vb_full; /* vb is full and packets dropped */
143 struct urb *urb_list[MAX_BULK_BUFS];
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;
151 unsigned int f_adc, f_tuner;
154 unsigned int num_formats;
157 struct v4l2_ctrl_handler hdl;
158 struct v4l2_ctrl *bandwidth_auto;
159 struct v4l2_ctrl *bandwidth;
161 /* for sample rate calc */
163 unsigned int sample_measured;
164 unsigned long jiffies_next;
167 /* write multiple registers */
168 static int rtl2832_sdr_wr_regs(struct rtl2832_sdr_dev *dev, u16 reg,
169 const u8 *val, int len)
171 struct platform_device *pdev = dev->pdev;
172 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
173 struct regmap *regmap = pdata->regmap;
175 return regmap_bulk_write(regmap, reg, val, len);
179 /* read multiple registers */
180 static int rtl2832_sdr_rd_regs(struct rtl2832_sdr_dev *dev, u16 reg, u8 *val,
183 struct platform_device *pdev = dev->pdev;
184 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
185 struct regmap *regmap = pdata->regmap;
187 return regmap_bulk_read(regmap, reg, val, len);
191 /* write single register */
192 static int rtl2832_sdr_wr_reg(struct rtl2832_sdr_dev *dev, u16 reg, u8 val)
194 return rtl2832_sdr_wr_regs(dev, reg, &val, 1);
197 /* write single register with mask */
198 static int rtl2832_sdr_wr_reg_mask(struct rtl2832_sdr_dev *dev, u16 reg,
201 struct platform_device *pdev = dev->pdev;
202 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
203 struct regmap *regmap = pdata->regmap;
205 return regmap_update_bits(regmap, reg, mask, val);
208 /* Private functions */
209 static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
210 struct rtl2832_sdr_dev *dev)
213 struct rtl2832_sdr_frame_buf *buf = NULL;
215 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
216 if (list_empty(&dev->queued_bufs))
219 buf = list_entry(dev->queued_bufs.next,
220 struct rtl2832_sdr_frame_buf, list);
221 list_del(&buf->list);
223 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
227 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
228 void *dst, const u8 *src, unsigned int src_len)
230 struct platform_device *pdev = dev->pdev;
231 unsigned int dst_len;
233 if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
234 /* native stream, no need to convert */
235 memcpy(dst, src, src_len);
237 } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
238 /* convert u8 to u16 */
242 for (i = 0; i < src_len; i++)
243 *u16dst++ = (src[i] << 8) | (src[i] >> 0);
244 dst_len = 2 * src_len;
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;
256 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
257 dev->sample_measured = dev->sample;
259 "slen=%u samples=%u msecs=%u sample rate=%lu\n",
260 src_len, samples, msecs, samples * 1000UL / msecs);
263 /* total number of I+Q pairs */
264 dev->sample += src_len / 2;
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.
273 static void rtl2832_sdr_urb_complete(struct urb *urb)
275 struct rtl2832_sdr_dev *dev = urb->context;
276 struct platform_device *pdev = dev->pdev;
277 struct rtl2832_sdr_frame_buf *fbuf;
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);
283 switch (urb->status) {
284 case 0: /* success */
285 case -ETIMEDOUT: /* NAK */
287 case -ECONNRESET: /* kill */
292 dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
296 if (likely(urb->actual_length > 0)) {
299 /* get free framebuffer */
300 fbuf = rtl2832_sdr_get_next_fill_buf(dev);
301 if (unlikely(fbuf == NULL)) {
303 dev_notice_ratelimited(&pdev->dev,
304 "videobuf is full, %d packets dropped\n",
309 /* fill framebuffer */
310 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
311 len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
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);
319 usb_submit_urb(urb, GFP_ATOMIC);
322 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
324 struct platform_device *pdev = dev->pdev;
327 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
328 dev_dbg(&pdev->dev, "kill urb=%d\n", i);
330 usb_kill_urb(dev->urb_list[i]);
332 dev->urbs_submitted = 0;
337 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
339 struct platform_device *pdev = dev->pdev;
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);
347 "Could not submit urb no. %d - get them all back\n",
349 rtl2832_sdr_kill_urbs(dev);
352 dev->urbs_submitted++;
358 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
360 struct platform_device *pdev = dev->pdev;
362 if (test_bit(URB_BUF, &dev->flags)) {
363 while (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]);
371 clear_bit(URB_BUF, &dev->flags);
376 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
378 struct platform_device *pdev = dev->pdev;
381 dev->buf_size = BULK_BUFFER_SIZE;
383 dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
384 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
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",
393 rtl2832_sdr_free_stream_bufs(dev);
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);
406 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
408 struct platform_device *pdev = dev->pdev;
411 rtl2832_sdr_kill_urbs(dev);
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);
417 usb_free_urb(dev->urb_list[i]);
420 dev->urbs_initialized = 0;
425 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
427 struct platform_device *pdev = dev->pdev;
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]);
440 usb_fill_bulk_urb(dev->urb_list[i],
442 usb_rcvbulkpipe(dev->udev, 0x81),
445 rtl2832_sdr_urb_complete, dev);
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++;
455 /* Must be called with vb_queue_lock hold */
456 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
458 struct platform_device *pdev = dev->pdev;
461 dev_dbg(&pdev->dev, "\n");
463 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
464 while (!list_empty(&dev->queued_bufs)) {
465 struct rtl2832_sdr_frame_buf *buf;
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);
472 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
475 static int rtl2832_sdr_querycap(struct file *file, void *fh,
476 struct v4l2_capability *cap)
478 struct rtl2832_sdr_dev *dev = video_drvdata(file);
479 struct platform_device *pdev = dev->pdev;
481 dev_dbg(&pdev->dev, "\n");
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;
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[])
497 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
498 struct platform_device *pdev = dev->pdev;
500 dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
502 /* Need at least 8 buffers */
503 if (vq->num_buffers + *nbuffers < 8)
504 *nbuffers = 8 - vq->num_buffers;
506 sizes[0] = PAGE_ALIGN(dev->buffersize);
507 dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
511 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
513 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
515 /* Don't allow queing new buffers after device disconnection */
522 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
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);
530 /* Check the device has not disconnected between prep and queuing */
532 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
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);
541 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
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;
547 unsigned int f_sr, f_if;
548 u8 buf[4], u8tmp1, u8tmp2;
552 dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
554 if (!test_bit(POWER_ON, &dev->flags))
562 ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x00\x00", 2);
566 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x00\x00\x00\x00", 4);
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);
580 u64tmp = f_if % pdata->clk;
582 u64tmp = div_u64(u64tmp, pdata->clk);
584 u32tmp = u64tmp & 0x3fffff;
586 dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
588 buf[0] = (u32tmp >> 16) & 0xff;
589 buf[1] = (u32tmp >> 8) & 0xff;
590 buf[2] = (u32tmp >> 0) & 0xff;
592 ret = rtl2832_sdr_wr_regs(dev, 0x119, buf, 3);
597 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
599 u8tmp1 = 0x1a; /* disable Zero-IF */
600 u8tmp2 = 0x8d; /* enable ADC I */
602 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
603 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
606 ret = rtl2832_sdr_wr_reg(dev, 0x1b1, u8tmp1);
610 ret = rtl2832_sdr_wr_reg(dev, 0x008, u8tmp2);
614 ret = rtl2832_sdr_wr_reg(dev, 0x006, 0x80);
618 /* program sampling rate (resampling down) */
619 u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
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);
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",
636 ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
641 ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x05", 1);
645 ret = rtl2832_sdr_wr_regs(dev, 0x01a, "\x1b\x16\x0d\x06\x01\xff", 6);
650 ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\xf0\x0f", 3);
655 ret = rtl2832_sdr_wr_regs(dev, 0x061, "\x60", 1);
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);
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);
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);
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);
778 dev_notice(&pdev->dev, "Unsupported tuner\n");
782 ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x04, 0x04);
786 ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x00, 0x04);
793 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
795 struct platform_device *pdev = dev->pdev;
798 dev_dbg(&pdev->dev, "\n");
801 ret = rtl2832_sdr_wr_regs(dev, 0x061, "\xe0", 1);
806 ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x20", 1);
810 ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
815 ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\x0f\xff", 3);
819 ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x40\x00", 2);
823 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x06\x3f\xce\xcc", 4);
830 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
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;
842 if (dev->f_tuner == 0)
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);
855 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
858 c->frequency = dev->f_tuner;
859 c->delivery_system = SYS_DVBT;
861 dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
862 c->frequency, c->bandwidth_hz);
864 if (!test_bit(POWER_ON, &dev->flags))
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);
875 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
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;
881 dev_dbg(&pdev->dev, "\n");
883 if (fe->ops.tuner_ops.init)
884 fe->ops.tuner_ops.init(fe);
889 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
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;
895 dev_dbg(&pdev->dev, "\n");
897 if (fe->ops.tuner_ops.sleep)
898 fe->ops.tuner_ops.sleep(fe);
903 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
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;
911 dev_dbg(&pdev->dev, "\n");
916 if (mutex_lock_interruptible(&dev->v4l2_lock))
919 if (d->props->power_ctrl)
920 d->props->power_ctrl(d, 1);
923 if (d->props->frontend_ctrl)
924 d->props->frontend_ctrl(pdata->dvb_frontend, 1);
926 set_bit(POWER_ON, &dev->flags);
929 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
930 ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
932 ret = rtl2832_sdr_set_tuner(dev);
936 ret = rtl2832_sdr_set_tuner_freq(dev);
940 ret = rtl2832_sdr_set_adc(dev);
944 ret = rtl2832_sdr_alloc_stream_bufs(dev);
948 ret = rtl2832_sdr_alloc_urbs(dev);
954 ret = rtl2832_sdr_submit_urbs(dev);
959 mutex_unlock(&dev->v4l2_lock);
964 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
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;
971 dev_dbg(&pdev->dev, "\n");
973 mutex_lock(&dev->v4l2_lock);
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);
982 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
983 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
985 rtl2832_sdr_unset_tuner(dev);
987 clear_bit(POWER_ON, &dev->flags);
990 if (d->props->frontend_ctrl)
991 d->props->frontend_ctrl(pdata->dvb_frontend, 0);
993 if (d->props->power_ctrl)
994 d->props->power_ctrl(d, 0);
996 mutex_unlock(&dev->v4l2_lock);
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,
1009 static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
1010 struct v4l2_tuner *v)
1012 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1013 struct platform_device *pdev = dev->pdev;
1016 dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
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;
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;
1041 static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
1042 const struct v4l2_tuner *v)
1044 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1045 struct platform_device *pdev = dev->pdev;
1048 dev_dbg(&pdev->dev, "\n");
1050 if (v->index == 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) {
1063 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
1064 struct v4l2_frequency_band *band)
1066 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1067 struct platform_device *pdev = dev->pdev;
1070 dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1071 band->tuner, band->type, band->index);
1073 if (band->tuner == 0) {
1074 if (band->index >= ARRAY_SIZE(bands_adc))
1077 *band = bands_adc[band->index];
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))
1086 *band = bands_fm[band->index];
1094 static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1095 struct v4l2_frequency *f)
1097 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1098 struct platform_device *pdev = dev->pdev;
1101 dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1103 if (f->tuner == 0) {
1104 f->frequency = dev->f_adc;
1105 f->type = V4L2_TUNER_ADC;
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;
1121 static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1122 const struct v4l2_frequency *f)
1124 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1125 struct platform_device *pdev = dev->pdev;
1128 dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1129 f->tuner, f->type, f->frequency);
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)
1135 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1136 if (f->frequency < BAND_ADC_0)
1138 else if (f->frequency < BAND_ADC_1)
1143 dev->f_adc = clamp_t(unsigned int, f->frequency,
1144 bands_adc[band].rangelow,
1145 bands_adc[band].rangehigh);
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);
1158 ret = rtl2832_sdr_set_tuner_freq(dev);
1165 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1166 struct v4l2_fmtdesc *f)
1168 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1169 struct platform_device *pdev = dev->pdev;
1171 dev_dbg(&pdev->dev, "\n");
1173 if (f->index >= dev->num_formats)
1176 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1177 f->pixelformat = formats[f->index].pixelformat;
1182 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1183 struct v4l2_format *f)
1185 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1186 struct platform_device *pdev = dev->pdev;
1188 dev_dbg(&pdev->dev, "\n");
1190 f->fmt.sdr.pixelformat = dev->pixelformat;
1191 f->fmt.sdr.buffersize = dev->buffersize;
1193 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1198 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1199 struct v4l2_format *f)
1201 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1202 struct platform_device *pdev = dev->pdev;
1203 struct vb2_queue *q = &dev->vb_queue;
1206 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1207 (char *)&f->fmt.sdr.pixelformat);
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;
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;
1230 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1231 struct v4l2_format *f)
1233 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1234 struct platform_device *pdev = dev->pdev;
1237 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1238 (char *)&f->fmt.sdr.pixelformat);
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;
1248 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1249 f->fmt.sdr.buffersize = formats[0].buffersize;
1254 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1255 .vidioc_querycap = rtl2832_sdr_querycap,
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,
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,
1269 .vidioc_streamon = vb2_ioctl_streamon,
1270 .vidioc_streamoff = vb2_ioctl_streamoff,
1272 .vidioc_g_tuner = rtl2832_sdr_g_tuner,
1273 .vidioc_s_tuner = rtl2832_sdr_s_tuner,
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,
1279 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1280 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1281 .vidioc_log_status = v4l2_ctrl_log_status,
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,
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,
1301 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1303 struct rtl2832_sdr_dev *dev =
1304 container_of(ctrl->handler, struct rtl2832_sdr_dev,
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;
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,
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);
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;
1332 c->bandwidth_hz = dev->bandwidth->val;
1334 if (!test_bit(POWER_ON, &dev->flags))
1337 if (fe->ops.tuner_ops.set_params)
1338 ret = fe->ops.tuner_ops.set_params(fe);
1349 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1350 .s_ctrl = rtl2832_sdr_s_ctrl,
1353 static void rtl2832_sdr_video_release(struct v4l2_device *v)
1355 struct rtl2832_sdr_dev *dev =
1356 container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1357 struct platform_device *pdev = dev->pdev;
1359 dev_dbg(&pdev->dev, "\n");
1361 v4l2_ctrl_handler_free(&dev->hdl);
1362 v4l2_device_unregister(&dev->v4l2_dev);
1366 /* Platform driver interface */
1367 static int rtl2832_sdr_probe(struct platform_device *pdev)
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;
1375 dev_dbg(&pdev->dev, "\n");
1378 dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1382 if (!pdev->dev.parent->driver) {
1383 dev_dbg(&pdev->dev, "No parent device\n");
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");
1393 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1396 goto err_module_put;
1399 /* setup the state */
1400 subdev = pdata->v4l2_subdev;
1401 dev->v4l2_subdev = pdata->v4l2_subdev;
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;
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);
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);
1427 dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1431 /* Register controls */
1432 switch (pdata->tuner) {
1433 case RTL2832_SDR_TUNER_E4000:
1434 v4l2_ctrl_handler_init(&dev->hdl, 9);
1436 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
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,
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);
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,
1455 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1456 V4L2_CID_RF_TUNER_BANDWIDTH,
1457 6000000, 8000000, 1000000,
1459 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1461 case RTL2832_SDR_TUNER_FC2580:
1462 v4l2_ctrl_handler_init(&dev->hdl, 2);
1464 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1468 v4l2_ctrl_handler_init(&dev->hdl, 0);
1469 dev_err(&pdev->dev, "Unsupported tuner\n");
1470 goto err_v4l2_ctrl_handler_free;
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;
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);
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);
1488 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1489 goto err_v4l2_ctrl_handler_free;
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;
1497 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1499 dev_err(&pdev->dev, "Failed to register as video device %d\n",
1501 goto err_v4l2_device_unregister;
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);
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);
1517 module_put(pdev->dev.parent->driver->owner);
1522 static int rtl2832_sdr_remove(struct platform_device *pdev)
1524 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1526 dev_dbg(&pdev->dev, "\n");
1528 mutex_lock(&dev->vb_queue_lock);
1529 mutex_lock(&dev->v4l2_lock);
1530 /* No need to keep the urbs around after disconnection */
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);
1542 static struct platform_driver rtl2832_sdr_driver = {
1544 .name = "rtl2832_sdr",
1546 .probe = rtl2832_sdr_probe,
1547 .remove = rtl2832_sdr_remove,
1549 module_platform_driver(rtl2832_sdr_driver);
1551 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1552 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1553 MODULE_LICENSE("GPL");