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-vmalloc.h>
34 #include <linux/platform_device.h>
35 #include <linux/jiffies.h>
36 #include <linux/math64.h>
38 static bool rtl2832_sdr_emulated_fmt;
39 module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
40 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
42 /* Original macro does not contain enough null pointer checks for our need */
43 #define V4L2_SUBDEV_HAS_OP(sd, o, f) \
44 ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
46 #define MAX_BULK_BUFS (10)
47 #define BULK_BUFFER_SIZE (128 * 512)
49 static const struct v4l2_frequency_band bands_adc[] = {
52 .type = V4L2_TUNER_ADC,
54 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
60 .type = V4L2_TUNER_ADC,
62 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
68 .type = V4L2_TUNER_ADC,
70 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
76 static const struct v4l2_frequency_band bands_fm[] = {
79 .type = V4L2_TUNER_RF,
81 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
83 .rangehigh = 2000000000,
88 struct rtl2832_sdr_format {
94 static struct rtl2832_sdr_format formats[] = {
97 .pixelformat = V4L2_SDR_FMT_CU8,
98 .buffersize = BULK_BUFFER_SIZE,
100 .name = "Complex U16LE (emulated)",
101 .pixelformat = V4L2_SDR_FMT_CU16LE,
102 .buffersize = BULK_BUFFER_SIZE * 2,
106 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
108 /* intermediate buffers with raw data from the USB device */
109 struct rtl2832_sdr_frame_buf {
110 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
111 struct list_head list;
114 struct rtl2832_sdr_dev {
115 #define POWER_ON 0 /* BIT(0) */
116 #define URB_BUF 1 /* BIT(1) */
119 struct platform_device *pdev;
121 struct video_device vdev;
122 struct v4l2_device v4l2_dev;
123 struct v4l2_subdev *v4l2_subdev;
125 /* videobuf2 queue and queued buffers list */
126 struct vb2_queue vb_queue;
127 struct list_head queued_bufs;
128 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
129 unsigned sequence; /* buffer sequence counter */
131 /* Note if taking both locks v4l2_lock must always be locked first! */
132 struct mutex v4l2_lock; /* Protects everything else */
133 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
135 /* Pointer to our usb_device, will be NULL after unplug */
136 struct usb_device *udev; /* Both mutexes most be hold when setting! */
138 unsigned int vb_full; /* vb is full and packets dropped */
140 struct urb *urb_list[MAX_BULK_BUFS];
142 unsigned long buf_size;
143 u8 *buf_list[MAX_BULK_BUFS];
144 dma_addr_t dma_addr[MAX_BULK_BUFS];
145 int urbs_initialized;
148 unsigned int f_adc, f_tuner;
151 unsigned int num_formats;
154 struct v4l2_ctrl_handler hdl;
155 struct v4l2_ctrl *bandwidth_auto;
156 struct v4l2_ctrl *bandwidth;
158 /* for sample rate calc */
160 unsigned int sample_measured;
161 unsigned long jiffies_next;
164 /* write multiple registers */
165 static int rtl2832_sdr_wr_regs(struct rtl2832_sdr_dev *dev, u16 reg,
166 const u8 *val, int len)
168 struct platform_device *pdev = dev->pdev;
169 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
170 struct i2c_client *client = pdata->i2c_client;
172 return pdata->bulk_write(client, reg, val, len);
176 /* read multiple registers */
177 static int rtl2832_sdr_rd_regs(struct rtl2832_sdr_dev *dev, u16 reg, u8 *val,
180 struct platform_device *pdev = dev->pdev;
181 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
182 struct i2c_client *client = pdata->i2c_client;
184 return pdata->bulk_read(client, reg, val, len);
188 /* write single register */
189 static int rtl2832_sdr_wr_reg(struct rtl2832_sdr_dev *dev, u16 reg, u8 val)
191 return rtl2832_sdr_wr_regs(dev, reg, &val, 1);
194 /* write single register with mask */
195 static int rtl2832_sdr_wr_reg_mask(struct rtl2832_sdr_dev *dev, u16 reg,
198 struct platform_device *pdev = dev->pdev;
199 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
200 struct i2c_client *client = pdata->i2c_client;
202 return pdata->update_bits(client, reg, mask, val);
205 /* Private functions */
206 static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
207 struct rtl2832_sdr_dev *dev)
210 struct rtl2832_sdr_frame_buf *buf = NULL;
212 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
213 if (list_empty(&dev->queued_bufs))
216 buf = list_entry(dev->queued_bufs.next,
217 struct rtl2832_sdr_frame_buf, list);
218 list_del(&buf->list);
220 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
224 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
225 void *dst, const u8 *src, unsigned int src_len)
227 struct platform_device *pdev = dev->pdev;
228 unsigned int dst_len;
230 if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
231 /* native stream, no need to convert */
232 memcpy(dst, src, src_len);
234 } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
235 /* convert u8 to u16 */
239 for (i = 0; i < src_len; i++)
240 *u16dst++ = (src[i] << 8) | (src[i] >> 0);
241 dst_len = 2 * src_len;
246 /* calculate sample rate and output it in 10 seconds intervals */
247 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
248 #define MSECS 10000UL
249 unsigned int msecs = jiffies_to_msecs(jiffies -
250 dev->jiffies_next + msecs_to_jiffies(MSECS));
251 unsigned int samples = dev->sample - dev->sample_measured;
253 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
254 dev->sample_measured = dev->sample;
256 "slen=%u samples=%u msecs=%u sample rate=%lu\n",
257 src_len, samples, msecs, samples * 1000UL / msecs);
260 /* total number of I+Q pairs */
261 dev->sample += src_len / 2;
267 * This gets called for the bulk stream pipe. This is done in interrupt
268 * time, so it has to be fast, not crash, and not stall. Neat.
270 static void rtl2832_sdr_urb_complete(struct urb *urb)
272 struct rtl2832_sdr_dev *dev = urb->context;
273 struct platform_device *pdev = dev->pdev;
274 struct rtl2832_sdr_frame_buf *fbuf;
276 dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
277 urb->status, urb->actual_length,
278 urb->transfer_buffer_length, urb->error_count);
280 switch (urb->status) {
281 case 0: /* success */
282 case -ETIMEDOUT: /* NAK */
284 case -ECONNRESET: /* kill */
289 dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
293 if (likely(urb->actual_length > 0)) {
296 /* get free framebuffer */
297 fbuf = rtl2832_sdr_get_next_fill_buf(dev);
298 if (unlikely(fbuf == NULL)) {
300 dev_notice_ratelimited(&pdev->dev,
301 "videobuf is full, %d packets dropped\n",
306 /* fill framebuffer */
307 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
308 len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
310 vb2_set_plane_payload(&fbuf->vb, 0, len);
311 v4l2_get_timestamp(&fbuf->vb.v4l2_buf.timestamp);
312 fbuf->vb.v4l2_buf.sequence = dev->sequence++;
313 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
316 usb_submit_urb(urb, GFP_ATOMIC);
319 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
321 struct platform_device *pdev = dev->pdev;
324 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
325 dev_dbg(&pdev->dev, "kill urb=%d\n", i);
327 usb_kill_urb(dev->urb_list[i]);
329 dev->urbs_submitted = 0;
334 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
336 struct platform_device *pdev = dev->pdev;
339 for (i = 0; i < dev->urbs_initialized; i++) {
340 dev_dbg(&pdev->dev, "submit urb=%d\n", i);
341 ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
344 "Could not submit urb no. %d - get them all back\n",
346 rtl2832_sdr_kill_urbs(dev);
349 dev->urbs_submitted++;
355 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
357 struct platform_device *pdev = dev->pdev;
359 if (test_bit(URB_BUF, &dev->flags)) {
360 while (dev->buf_num) {
362 dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
363 usb_free_coherent(dev->udev, dev->buf_size,
364 dev->buf_list[dev->buf_num],
365 dev->dma_addr[dev->buf_num]);
368 clear_bit(URB_BUF, &dev->flags);
373 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
375 struct platform_device *pdev = dev->pdev;
378 dev->buf_size = BULK_BUFFER_SIZE;
380 dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
381 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
383 for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
384 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
385 BULK_BUFFER_SIZE, GFP_ATOMIC,
386 &dev->dma_addr[dev->buf_num]);
387 if (!dev->buf_list[dev->buf_num]) {
388 dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
390 rtl2832_sdr_free_stream_bufs(dev);
394 dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
395 dev->buf_num, dev->buf_list[dev->buf_num],
396 (long long)dev->dma_addr[dev->buf_num]);
397 set_bit(URB_BUF, &dev->flags);
403 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
405 struct platform_device *pdev = dev->pdev;
408 rtl2832_sdr_kill_urbs(dev);
410 for (i = dev->urbs_initialized - 1; i >= 0; i--) {
411 if (dev->urb_list[i]) {
412 dev_dbg(&pdev->dev, "free urb=%d\n", i);
414 usb_free_urb(dev->urb_list[i]);
417 dev->urbs_initialized = 0;
422 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
424 struct platform_device *pdev = dev->pdev;
427 /* allocate the URBs */
428 for (i = 0; i < MAX_BULK_BUFS; i++) {
429 dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
430 dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
431 if (!dev->urb_list[i]) {
432 dev_dbg(&pdev->dev, "failed\n");
433 for (j = 0; j < i; j++)
434 usb_free_urb(dev->urb_list[j]);
437 usb_fill_bulk_urb(dev->urb_list[i],
439 usb_rcvbulkpipe(dev->udev, 0x81),
442 rtl2832_sdr_urb_complete, dev);
444 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
445 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
446 dev->urbs_initialized++;
452 /* Must be called with vb_queue_lock hold */
453 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
455 struct platform_device *pdev = dev->pdev;
458 dev_dbg(&pdev->dev, "\n");
460 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
461 while (!list_empty(&dev->queued_bufs)) {
462 struct rtl2832_sdr_frame_buf *buf;
464 buf = list_entry(dev->queued_bufs.next,
465 struct rtl2832_sdr_frame_buf, list);
466 list_del(&buf->list);
467 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
469 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
472 static int rtl2832_sdr_querycap(struct file *file, void *fh,
473 struct v4l2_capability *cap)
475 struct rtl2832_sdr_dev *dev = video_drvdata(file);
476 struct platform_device *pdev = dev->pdev;
478 dev_dbg(&pdev->dev, "\n");
480 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
481 strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
482 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
483 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
484 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
485 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
489 /* Videobuf2 operations */
490 static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
491 const struct v4l2_format *fmt, unsigned int *nbuffers,
492 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
494 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
495 struct platform_device *pdev = dev->pdev;
497 dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
499 /* Need at least 8 buffers */
500 if (vq->num_buffers + *nbuffers < 8)
501 *nbuffers = 8 - vq->num_buffers;
503 sizes[0] = PAGE_ALIGN(dev->buffersize);
504 dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
508 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
510 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
512 /* Don't allow queing new buffers after device disconnection */
519 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
521 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
522 struct rtl2832_sdr_frame_buf *buf =
523 container_of(vb, struct rtl2832_sdr_frame_buf, vb);
526 /* Check the device has not disconnected between prep and queuing */
528 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
532 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
533 list_add_tail(&buf->list, &dev->queued_bufs);
534 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
537 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
539 struct platform_device *pdev = dev->pdev;
540 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
541 struct dvb_frontend *fe = pdata->dvb_frontend;
543 unsigned int f_sr, f_if;
544 u8 buf[4], u8tmp1, u8tmp2;
548 dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
550 if (!test_bit(POWER_ON, &dev->flags))
558 ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x00\x00", 2);
562 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x00\x00\x00\x00", 4);
566 /* get IF from tuner */
567 if (fe->ops.tuner_ops.get_if_frequency)
568 ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
576 u64tmp = f_if % pdata->clk;
578 u64tmp = div_u64(u64tmp, pdata->clk);
580 u32tmp = u64tmp & 0x3fffff;
582 dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
584 buf[0] = (u32tmp >> 16) & 0xff;
585 buf[1] = (u32tmp >> 8) & 0xff;
586 buf[2] = (u32tmp >> 0) & 0xff;
588 ret = rtl2832_sdr_wr_regs(dev, 0x119, buf, 3);
593 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
595 u8tmp1 = 0x1a; /* disable Zero-IF */
596 u8tmp2 = 0x8d; /* enable ADC I */
598 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
599 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
602 ret = rtl2832_sdr_wr_reg(dev, 0x1b1, u8tmp1);
606 ret = rtl2832_sdr_wr_reg(dev, 0x008, u8tmp2);
610 ret = rtl2832_sdr_wr_reg(dev, 0x006, 0x80);
614 /* program sampling rate (resampling down) */
615 u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
617 buf[0] = (u32tmp >> 24) & 0xff;
618 buf[1] = (u32tmp >> 16) & 0xff;
619 buf[2] = (u32tmp >> 8) & 0xff;
620 buf[3] = (u32tmp >> 0) & 0xff;
621 ret = rtl2832_sdr_wr_regs(dev, 0x19f, buf, 4);
625 /* low-pass filter */
626 ret = rtl2832_sdr_wr_regs(dev, 0x11c,
627 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
632 ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
637 ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x05", 1);
641 ret = rtl2832_sdr_wr_regs(dev, 0x01a, "\x1b\x16\x0d\x06\x01\xff", 6);
646 ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\xf0\x0f", 3);
651 ret = rtl2832_sdr_wr_regs(dev, 0x061, "\x60", 1);
655 /* used RF tuner based settings */
656 switch (pdata->tuner) {
657 case RTL2832_SDR_TUNER_E4000:
658 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
659 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
660 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
661 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x30", 1);
662 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xd0", 1);
663 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
664 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x18", 1);
665 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
666 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
667 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
668 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
669 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
670 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
671 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
672 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
673 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
674 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
675 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
676 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
677 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
678 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xd4", 1);
679 ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
680 ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
681 ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
682 ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x14", 1);
683 ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xec", 1);
684 ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
685 ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
686 ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
687 ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x83", 1);
688 ret = rtl2832_sdr_wr_regs(dev, 0x010, "\x49", 1);
689 ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x87", 1);
690 ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x85", 1);
691 ret = rtl2832_sdr_wr_regs(dev, 0x013, "\x02", 1);
693 case RTL2832_SDR_TUNER_FC0012:
694 case RTL2832_SDR_TUNER_FC0013:
695 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
696 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
697 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
698 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x2c", 1);
699 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
700 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
701 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x16", 1);
702 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
703 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
704 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
705 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
706 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
707 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
708 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
709 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
710 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
711 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
712 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
713 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
714 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
715 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xe9\xbf", 2);
716 ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
717 ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
718 ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
719 ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x11", 1);
720 ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xef", 1);
721 ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
722 ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
723 ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
725 case RTL2832_SDR_TUNER_R820T:
726 case RTL2832_SDR_TUNER_R828D:
727 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
728 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
729 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x01", 1);
730 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x80", 1);
731 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x24", 1);
732 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
733 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
734 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x14", 1);
735 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
736 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
737 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
738 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
739 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
740 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
741 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
742 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
743 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
744 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
745 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
746 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
747 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
748 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xf4", 1);
750 case RTL2832_SDR_TUNER_FC2580:
751 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x39", 1);
752 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
753 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
754 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x2c", 1);
755 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
756 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
757 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x16", 1);
758 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
759 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
760 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
761 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
762 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
763 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
764 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
765 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
766 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
767 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x9c", 1);
768 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
769 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
770 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
771 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xe9\xf4", 2);
774 dev_notice(&pdev->dev, "Unsupported tuner\n");
778 ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x04, 0x04);
782 ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x00, 0x04);
789 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
791 struct platform_device *pdev = dev->pdev;
794 dev_dbg(&pdev->dev, "\n");
797 ret = rtl2832_sdr_wr_regs(dev, 0x061, "\xe0", 1);
802 ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x20", 1);
806 ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
811 ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\x0f\xff", 3);
815 ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x40\x00", 2);
819 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x06\x3f\xce\xcc", 4);
826 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
828 struct platform_device *pdev = dev->pdev;
829 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
830 struct dvb_frontend *fe = pdata->dvb_frontend;
831 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
832 struct v4l2_ctrl *bandwidth_auto;
833 struct v4l2_ctrl *bandwidth;
838 if (dev->f_tuner == 0)
844 bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
845 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
846 bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
847 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
848 c->bandwidth_hz = dev->f_adc;
849 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
851 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
854 c->frequency = dev->f_tuner;
855 c->delivery_system = SYS_DVBT;
857 dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
858 c->frequency, c->bandwidth_hz);
860 if (!test_bit(POWER_ON, &dev->flags))
863 if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
864 if (fe->ops.tuner_ops.set_params)
865 fe->ops.tuner_ops.set_params(fe);
871 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
873 struct platform_device *pdev = dev->pdev;
874 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
875 struct dvb_frontend *fe = pdata->dvb_frontend;
877 dev_dbg(&pdev->dev, "\n");
879 if (fe->ops.tuner_ops.init)
880 fe->ops.tuner_ops.init(fe);
885 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
887 struct platform_device *pdev = dev->pdev;
888 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
889 struct dvb_frontend *fe = pdata->dvb_frontend;
891 dev_dbg(&pdev->dev, "\n");
893 if (fe->ops.tuner_ops.sleep)
894 fe->ops.tuner_ops.sleep(fe);
899 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
901 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
902 struct platform_device *pdev = dev->pdev;
903 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
904 struct dvb_usb_device *d = pdata->dvb_usb_device;
907 dev_dbg(&pdev->dev, "\n");
912 if (mutex_lock_interruptible(&dev->v4l2_lock))
915 if (d->props->power_ctrl)
916 d->props->power_ctrl(d, 1);
919 if (d->props->frontend_ctrl)
920 d->props->frontend_ctrl(pdata->dvb_frontend, 1);
922 set_bit(POWER_ON, &dev->flags);
925 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
926 ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
928 ret = rtl2832_sdr_set_tuner(dev);
932 ret = rtl2832_sdr_set_tuner_freq(dev);
936 ret = rtl2832_sdr_set_adc(dev);
940 ret = rtl2832_sdr_alloc_stream_bufs(dev);
944 ret = rtl2832_sdr_alloc_urbs(dev);
950 ret = rtl2832_sdr_submit_urbs(dev);
955 mutex_unlock(&dev->v4l2_lock);
960 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
962 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
963 struct platform_device *pdev = dev->pdev;
964 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
965 struct dvb_usb_device *d = pdata->dvb_usb_device;
967 dev_dbg(&pdev->dev, "\n");
969 mutex_lock(&dev->v4l2_lock);
971 rtl2832_sdr_kill_urbs(dev);
972 rtl2832_sdr_free_urbs(dev);
973 rtl2832_sdr_free_stream_bufs(dev);
974 rtl2832_sdr_cleanup_queued_bufs(dev);
975 rtl2832_sdr_unset_adc(dev);
978 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
979 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
981 rtl2832_sdr_unset_tuner(dev);
983 clear_bit(POWER_ON, &dev->flags);
986 if (d->props->frontend_ctrl)
987 d->props->frontend_ctrl(pdata->dvb_frontend, 0);
989 if (d->props->power_ctrl)
990 d->props->power_ctrl(d, 0);
992 mutex_unlock(&dev->v4l2_lock);
995 static struct vb2_ops rtl2832_sdr_vb2_ops = {
996 .queue_setup = rtl2832_sdr_queue_setup,
997 .buf_prepare = rtl2832_sdr_buf_prepare,
998 .buf_queue = rtl2832_sdr_buf_queue,
999 .start_streaming = rtl2832_sdr_start_streaming,
1000 .stop_streaming = rtl2832_sdr_stop_streaming,
1001 .wait_prepare = vb2_ops_wait_prepare,
1002 .wait_finish = vb2_ops_wait_finish,
1005 static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
1006 struct v4l2_tuner *v)
1008 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1009 struct platform_device *pdev = dev->pdev;
1012 dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
1014 if (v->index == 0) {
1015 strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
1016 v->type = V4L2_TUNER_ADC;
1017 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1018 v->rangelow = 300000;
1019 v->rangehigh = 3200000;
1021 } else if (v->index == 1 &&
1022 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
1023 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
1024 } else if (v->index == 1) {
1025 strlcpy(v->name, "RF: <unknown>", sizeof(v->name));
1026 v->type = V4L2_TUNER_RF;
1027 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1028 v->rangelow = 50000000;
1029 v->rangehigh = 2000000000;
1037 static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
1038 const struct v4l2_tuner *v)
1040 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1041 struct platform_device *pdev = dev->pdev;
1044 dev_dbg(&pdev->dev, "\n");
1046 if (v->index == 0) {
1048 } else if (v->index == 1 &&
1049 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
1050 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
1051 } else if (v->index == 1) {
1059 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
1060 struct v4l2_frequency_band *band)
1062 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1063 struct platform_device *pdev = dev->pdev;
1066 dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1067 band->tuner, band->type, band->index);
1069 if (band->tuner == 0) {
1070 if (band->index >= ARRAY_SIZE(bands_adc))
1073 *band = bands_adc[band->index];
1075 } else if (band->tuner == 1 &&
1076 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
1077 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
1078 } else if (band->tuner == 1) {
1079 if (band->index >= ARRAY_SIZE(bands_fm))
1082 *band = bands_fm[band->index];
1090 static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1091 struct v4l2_frequency *f)
1093 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1094 struct platform_device *pdev = dev->pdev;
1097 dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1099 if (f->tuner == 0) {
1100 f->frequency = dev->f_adc;
1101 f->type = V4L2_TUNER_ADC;
1103 } else if (f->tuner == 1 &&
1104 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
1105 f->type = V4L2_TUNER_RF;
1106 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
1107 } else if (f->tuner == 1) {
1108 f->frequency = dev->f_tuner;
1109 f->type = V4L2_TUNER_RF;
1117 static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1118 const struct v4l2_frequency *f)
1120 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1121 struct platform_device *pdev = dev->pdev;
1124 dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1125 f->tuner, f->type, f->frequency);
1127 /* ADC band midpoints */
1128 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1129 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1131 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1132 if (f->frequency < BAND_ADC_0)
1134 else if (f->frequency < BAND_ADC_1)
1139 dev->f_adc = clamp_t(unsigned int, f->frequency,
1140 bands_adc[band].rangelow,
1141 bands_adc[band].rangehigh);
1143 dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1144 ret = rtl2832_sdr_set_adc(dev);
1145 } else if (f->tuner == 1 &&
1146 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
1147 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
1148 } else if (f->tuner == 1) {
1149 dev->f_tuner = clamp_t(unsigned int, f->frequency,
1150 bands_fm[0].rangelow,
1151 bands_fm[0].rangehigh);
1152 dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
1154 ret = rtl2832_sdr_set_tuner_freq(dev);
1161 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1162 struct v4l2_fmtdesc *f)
1164 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1165 struct platform_device *pdev = dev->pdev;
1167 dev_dbg(&pdev->dev, "\n");
1169 if (f->index >= dev->num_formats)
1172 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1173 f->pixelformat = formats[f->index].pixelformat;
1178 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1179 struct v4l2_format *f)
1181 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1182 struct platform_device *pdev = dev->pdev;
1184 dev_dbg(&pdev->dev, "\n");
1186 f->fmt.sdr.pixelformat = dev->pixelformat;
1187 f->fmt.sdr.buffersize = dev->buffersize;
1189 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1194 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1195 struct v4l2_format *f)
1197 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1198 struct platform_device *pdev = dev->pdev;
1199 struct vb2_queue *q = &dev->vb_queue;
1202 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1203 (char *)&f->fmt.sdr.pixelformat);
1208 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1209 for (i = 0; i < dev->num_formats; i++) {
1210 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1211 dev->pixelformat = formats[i].pixelformat;
1212 dev->buffersize = formats[i].buffersize;
1213 f->fmt.sdr.buffersize = formats[i].buffersize;
1218 dev->pixelformat = formats[0].pixelformat;
1219 dev->buffersize = formats[0].buffersize;
1220 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1221 f->fmt.sdr.buffersize = formats[0].buffersize;
1226 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1227 struct v4l2_format *f)
1229 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1230 struct platform_device *pdev = dev->pdev;
1233 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1234 (char *)&f->fmt.sdr.pixelformat);
1236 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1237 for (i = 0; i < dev->num_formats; i++) {
1238 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1239 f->fmt.sdr.buffersize = formats[i].buffersize;
1244 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1245 f->fmt.sdr.buffersize = formats[0].buffersize;
1250 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1251 .vidioc_querycap = rtl2832_sdr_querycap,
1253 .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap,
1254 .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap,
1255 .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap,
1256 .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap,
1258 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1259 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1260 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1261 .vidioc_querybuf = vb2_ioctl_querybuf,
1262 .vidioc_qbuf = vb2_ioctl_qbuf,
1263 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1265 .vidioc_streamon = vb2_ioctl_streamon,
1266 .vidioc_streamoff = vb2_ioctl_streamoff,
1268 .vidioc_g_tuner = rtl2832_sdr_g_tuner,
1269 .vidioc_s_tuner = rtl2832_sdr_s_tuner,
1271 .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands,
1272 .vidioc_g_frequency = rtl2832_sdr_g_frequency,
1273 .vidioc_s_frequency = rtl2832_sdr_s_frequency,
1275 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1276 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1277 .vidioc_log_status = v4l2_ctrl_log_status,
1280 static const struct v4l2_file_operations rtl2832_sdr_fops = {
1281 .owner = THIS_MODULE,
1282 .open = v4l2_fh_open,
1283 .release = vb2_fop_release,
1284 .read = vb2_fop_read,
1285 .poll = vb2_fop_poll,
1286 .mmap = vb2_fop_mmap,
1287 .unlocked_ioctl = video_ioctl2,
1290 static struct video_device rtl2832_sdr_template = {
1291 .name = "Realtek RTL2832 SDR",
1292 .release = video_device_release_empty,
1293 .fops = &rtl2832_sdr_fops,
1294 .ioctl_ops = &rtl2832_sdr_ioctl_ops,
1297 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1299 struct rtl2832_sdr_dev *dev =
1300 container_of(ctrl->handler, struct rtl2832_sdr_dev,
1302 struct platform_device *pdev = dev->pdev;
1303 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1304 struct dvb_frontend *fe = pdata->dvb_frontend;
1305 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1308 dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1309 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1313 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1314 case V4L2_CID_RF_TUNER_BANDWIDTH:
1315 /* TODO: these controls should be moved to tuner drivers */
1316 if (dev->bandwidth_auto->val) {
1317 /* Round towards the closest legal value */
1318 s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
1321 val = clamp_t(s32, val, dev->bandwidth->minimum,
1322 dev->bandwidth->maximum);
1323 offset = val - dev->bandwidth->minimum;
1324 offset = dev->bandwidth->step *
1325 div_u64(offset, dev->bandwidth->step);
1326 dev->bandwidth->val = dev->bandwidth->minimum + offset;
1328 c->bandwidth_hz = dev->bandwidth->val;
1330 if (!test_bit(POWER_ON, &dev->flags))
1333 if (fe->ops.tuner_ops.set_params)
1334 ret = fe->ops.tuner_ops.set_params(fe);
1345 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1346 .s_ctrl = rtl2832_sdr_s_ctrl,
1349 static void rtl2832_sdr_video_release(struct v4l2_device *v)
1351 struct rtl2832_sdr_dev *dev =
1352 container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1353 struct platform_device *pdev = dev->pdev;
1355 dev_dbg(&pdev->dev, "\n");
1357 v4l2_ctrl_handler_free(&dev->hdl);
1358 v4l2_device_unregister(&dev->v4l2_dev);
1362 /* Platform driver interface */
1363 static int rtl2832_sdr_probe(struct platform_device *pdev)
1365 struct rtl2832_sdr_dev *dev;
1366 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1367 const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
1368 struct v4l2_subdev *subdev;
1371 dev_dbg(&pdev->dev, "\n");
1374 dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1378 if (!pdev->dev.parent->driver) {
1379 dev_dbg(&pdev->dev, "No parent device\n");
1383 /* try to refcount host drv since we are the consumer */
1384 if (!try_module_get(pdev->dev.parent->driver->owner)) {
1385 dev_err(&pdev->dev, "Refcount fail");
1389 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1392 goto err_module_put;
1395 /* setup the state */
1396 subdev = pdata->v4l2_subdev;
1397 dev->v4l2_subdev = pdata->v4l2_subdev;
1399 dev->udev = pdata->dvb_usb_device->udev;
1400 dev->f_adc = bands_adc[0].rangelow;
1401 dev->f_tuner = bands_fm[0].rangelow;
1402 dev->pixelformat = formats[0].pixelformat;
1403 dev->buffersize = formats[0].buffersize;
1404 dev->num_formats = NUM_FORMATS;
1405 if (!rtl2832_sdr_emulated_fmt)
1406 dev->num_formats -= 1;
1408 mutex_init(&dev->v4l2_lock);
1409 mutex_init(&dev->vb_queue_lock);
1410 spin_lock_init(&dev->queued_bufs_lock);
1411 INIT_LIST_HEAD(&dev->queued_bufs);
1413 /* Init videobuf2 queue structure */
1414 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1415 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1416 dev->vb_queue.drv_priv = dev;
1417 dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1418 dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1419 dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1420 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1421 ret = vb2_queue_init(&dev->vb_queue);
1423 dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1427 /* Register controls */
1428 switch (pdata->tuner) {
1429 case RTL2832_SDR_TUNER_E4000:
1430 v4l2_ctrl_handler_init(&dev->hdl, 9);
1432 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
1434 case RTL2832_SDR_TUNER_R820T:
1435 case RTL2832_SDR_TUNER_R828D:
1436 v4l2_ctrl_handler_init(&dev->hdl, 2);
1437 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1438 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1440 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1441 V4L2_CID_RF_TUNER_BANDWIDTH,
1442 0, 8000000, 100000, 0);
1443 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1445 case RTL2832_SDR_TUNER_FC0012:
1446 case RTL2832_SDR_TUNER_FC0013:
1447 v4l2_ctrl_handler_init(&dev->hdl, 2);
1448 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1449 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1451 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1452 V4L2_CID_RF_TUNER_BANDWIDTH,
1453 6000000, 8000000, 1000000,
1455 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1457 case RTL2832_SDR_TUNER_FC2580:
1458 v4l2_ctrl_handler_init(&dev->hdl, 2);
1460 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1464 v4l2_ctrl_handler_init(&dev->hdl, 0);
1465 dev_err(&pdev->dev, "Unsupported tuner\n");
1466 goto err_v4l2_ctrl_handler_free;
1468 if (dev->hdl.error) {
1469 ret = dev->hdl.error;
1470 dev_err(&pdev->dev, "Could not initialize controls\n");
1471 goto err_v4l2_ctrl_handler_free;
1474 /* Init video_device structure */
1475 dev->vdev = rtl2832_sdr_template;
1476 dev->vdev.queue = &dev->vb_queue;
1477 dev->vdev.queue->lock = &dev->vb_queue_lock;
1478 video_set_drvdata(&dev->vdev, dev);
1480 /* Register the v4l2_device structure */
1481 dev->v4l2_dev.release = rtl2832_sdr_video_release;
1482 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1484 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1485 goto err_v4l2_ctrl_handler_free;
1488 dev->v4l2_dev.ctrl_handler = &dev->hdl;
1489 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1490 dev->vdev.lock = &dev->v4l2_lock;
1491 dev->vdev.vfl_dir = VFL_DIR_RX;
1493 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1495 dev_err(&pdev->dev, "Failed to register as video device %d\n",
1497 goto err_v4l2_device_unregister;
1499 dev_info(&pdev->dev, "Registered as %s\n",
1500 video_device_node_name(&dev->vdev));
1501 dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1502 dev_notice(&pdev->dev,
1503 "SDR API is still slightly experimental and functionality changes may follow\n");
1504 platform_set_drvdata(pdev, dev);
1506 err_v4l2_device_unregister:
1507 v4l2_device_unregister(&dev->v4l2_dev);
1508 err_v4l2_ctrl_handler_free:
1509 v4l2_ctrl_handler_free(&dev->hdl);
1513 module_put(pdev->dev.parent->driver->owner);
1518 static int rtl2832_sdr_remove(struct platform_device *pdev)
1520 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1522 dev_dbg(&pdev->dev, "\n");
1524 mutex_lock(&dev->vb_queue_lock);
1525 mutex_lock(&dev->v4l2_lock);
1526 /* No need to keep the urbs around after disconnection */
1528 v4l2_device_disconnect(&dev->v4l2_dev);
1529 video_unregister_device(&dev->vdev);
1530 mutex_unlock(&dev->v4l2_lock);
1531 mutex_unlock(&dev->vb_queue_lock);
1532 v4l2_device_put(&dev->v4l2_dev);
1533 module_put(pdev->dev.parent->driver->owner);
1538 static struct platform_driver rtl2832_sdr_driver = {
1540 .name = "rtl2832_sdr",
1541 .owner = THIS_MODULE,
1543 .probe = rtl2832_sdr_probe,
1544 .remove = rtl2832_sdr_remove,
1546 module_platform_driver(rtl2832_sdr_driver);
1548 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1549 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1550 MODULE_LICENSE("GPL");