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
;
123 struct regmap
*regmap
;
125 struct video_device vdev
;
126 struct v4l2_device v4l2_dev
;
127 struct v4l2_subdev
*v4l2_subdev
;
129 /* videobuf2 queue and queued buffers list */
130 struct vb2_queue vb_queue
;
131 struct list_head queued_bufs
;
132 spinlock_t queued_bufs_lock
; /* Protects queued_bufs */
133 unsigned sequence
; /* buffer sequence counter */
135 /* Note if taking both locks v4l2_lock must always be locked first! */
136 struct mutex v4l2_lock
; /* Protects everything else */
137 struct mutex vb_queue_lock
; /* Protects vb_queue and capt_file */
139 /* Pointer to our usb_device, will be NULL after unplug */
140 struct usb_device
*udev
; /* Both mutexes most be hold when setting! */
142 unsigned int vb_full
; /* vb is full and packets dropped */
144 struct urb
*urb_list
[MAX_BULK_BUFS
];
146 unsigned long buf_size
;
147 u8
*buf_list
[MAX_BULK_BUFS
];
148 dma_addr_t dma_addr
[MAX_BULK_BUFS
];
149 int urbs_initialized
;
152 unsigned int f_adc
, f_tuner
;
155 unsigned int num_formats
;
158 struct v4l2_ctrl_handler hdl
;
159 struct v4l2_ctrl
*bandwidth_auto
;
160 struct v4l2_ctrl
*bandwidth
;
162 /* for sample rate calc */
164 unsigned int sample_measured
;
165 unsigned long jiffies_next
;
168 /* Private functions */
169 static struct rtl2832_sdr_frame_buf
*rtl2832_sdr_get_next_fill_buf(
170 struct rtl2832_sdr_dev
*dev
)
173 struct rtl2832_sdr_frame_buf
*buf
= NULL
;
175 spin_lock_irqsave(&dev
->queued_bufs_lock
, flags
);
176 if (list_empty(&dev
->queued_bufs
))
179 buf
= list_entry(dev
->queued_bufs
.next
,
180 struct rtl2832_sdr_frame_buf
, list
);
181 list_del(&buf
->list
);
183 spin_unlock_irqrestore(&dev
->queued_bufs_lock
, flags
);
187 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev
*dev
,
188 void *dst
, const u8
*src
, unsigned int src_len
)
190 struct platform_device
*pdev
= dev
->pdev
;
191 unsigned int dst_len
;
193 if (dev
->pixelformat
== V4L2_SDR_FMT_CU8
) {
194 /* native stream, no need to convert */
195 memcpy(dst
, src
, src_len
);
197 } else if (dev
->pixelformat
== V4L2_SDR_FMT_CU16LE
) {
198 /* convert u8 to u16 */
202 for (i
= 0; i
< src_len
; i
++)
203 *u16dst
++ = (src
[i
] << 8) | (src
[i
] >> 0);
204 dst_len
= 2 * src_len
;
209 /* calculate sample rate and output it in 10 seconds intervals */
210 if (unlikely(time_is_before_jiffies(dev
->jiffies_next
))) {
211 #define MSECS 10000UL
212 unsigned int msecs
= jiffies_to_msecs(jiffies
-
213 dev
->jiffies_next
+ msecs_to_jiffies(MSECS
));
214 unsigned int samples
= dev
->sample
- dev
->sample_measured
;
216 dev
->jiffies_next
= jiffies
+ msecs_to_jiffies(MSECS
);
217 dev
->sample_measured
= dev
->sample
;
219 "slen=%u samples=%u msecs=%u sample rate=%lu\n",
220 src_len
, samples
, msecs
, samples
* 1000UL / msecs
);
223 /* total number of I+Q pairs */
224 dev
->sample
+= src_len
/ 2;
230 * This gets called for the bulk stream pipe. This is done in interrupt
231 * time, so it has to be fast, not crash, and not stall. Neat.
233 static void rtl2832_sdr_urb_complete(struct urb
*urb
)
235 struct rtl2832_sdr_dev
*dev
= urb
->context
;
236 struct platform_device
*pdev
= dev
->pdev
;
237 struct rtl2832_sdr_frame_buf
*fbuf
;
239 dev_dbg_ratelimited(&pdev
->dev
, "status=%d length=%d/%d errors=%d\n",
240 urb
->status
, urb
->actual_length
,
241 urb
->transfer_buffer_length
, urb
->error_count
);
243 switch (urb
->status
) {
244 case 0: /* success */
245 case -ETIMEDOUT
: /* NAK */
247 case -ECONNRESET
: /* kill */
252 dev_err_ratelimited(&pdev
->dev
, "urb failed=%d\n", urb
->status
);
256 if (likely(urb
->actual_length
> 0)) {
259 /* get free framebuffer */
260 fbuf
= rtl2832_sdr_get_next_fill_buf(dev
);
261 if (unlikely(fbuf
== NULL
)) {
263 dev_notice_ratelimited(&pdev
->dev
,
264 "videobuf is full, %d packets dropped\n",
269 /* fill framebuffer */
270 ptr
= vb2_plane_vaddr(&fbuf
->vb
.vb2_buf
, 0);
271 len
= rtl2832_sdr_convert_stream(dev
, ptr
, urb
->transfer_buffer
,
273 vb2_set_plane_payload(&fbuf
->vb
.vb2_buf
, 0, len
);
274 fbuf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
275 fbuf
->vb
.sequence
= dev
->sequence
++;
276 vb2_buffer_done(&fbuf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
279 usb_submit_urb(urb
, GFP_ATOMIC
);
282 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev
*dev
)
284 struct platform_device
*pdev
= dev
->pdev
;
287 for (i
= dev
->urbs_submitted
- 1; i
>= 0; i
--) {
288 dev_dbg(&pdev
->dev
, "kill urb=%d\n", i
);
290 usb_kill_urb(dev
->urb_list
[i
]);
292 dev
->urbs_submitted
= 0;
297 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev
*dev
)
299 struct platform_device
*pdev
= dev
->pdev
;
302 for (i
= 0; i
< dev
->urbs_initialized
; i
++) {
303 dev_dbg(&pdev
->dev
, "submit urb=%d\n", i
);
304 ret
= usb_submit_urb(dev
->urb_list
[i
], GFP_ATOMIC
);
307 "Could not submit urb no. %d - get them all back\n",
309 rtl2832_sdr_kill_urbs(dev
);
312 dev
->urbs_submitted
++;
318 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev
*dev
)
320 struct platform_device
*pdev
= dev
->pdev
;
322 if (test_bit(URB_BUF
, &dev
->flags
)) {
323 while (dev
->buf_num
) {
325 dev_dbg(&pdev
->dev
, "free buf=%d\n", dev
->buf_num
);
326 usb_free_coherent(dev
->udev
, dev
->buf_size
,
327 dev
->buf_list
[dev
->buf_num
],
328 dev
->dma_addr
[dev
->buf_num
]);
331 clear_bit(URB_BUF
, &dev
->flags
);
336 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev
*dev
)
338 struct platform_device
*pdev
= dev
->pdev
;
341 dev
->buf_size
= BULK_BUFFER_SIZE
;
343 dev_dbg(&pdev
->dev
, "all in all I will use %u bytes for streaming\n",
344 MAX_BULK_BUFS
* BULK_BUFFER_SIZE
);
346 for (dev
->buf_num
= 0; dev
->buf_num
< MAX_BULK_BUFS
; dev
->buf_num
++) {
347 dev
->buf_list
[dev
->buf_num
] = usb_alloc_coherent(dev
->udev
,
348 BULK_BUFFER_SIZE
, GFP_ATOMIC
,
349 &dev
->dma_addr
[dev
->buf_num
]);
350 if (!dev
->buf_list
[dev
->buf_num
]) {
351 dev_dbg(&pdev
->dev
, "alloc buf=%d failed\n",
353 rtl2832_sdr_free_stream_bufs(dev
);
357 dev_dbg(&pdev
->dev
, "alloc buf=%d %p (dma %llu)\n",
358 dev
->buf_num
, dev
->buf_list
[dev
->buf_num
],
359 (long long)dev
->dma_addr
[dev
->buf_num
]);
360 set_bit(URB_BUF
, &dev
->flags
);
366 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev
*dev
)
368 struct platform_device
*pdev
= dev
->pdev
;
371 rtl2832_sdr_kill_urbs(dev
);
373 for (i
= dev
->urbs_initialized
- 1; i
>= 0; i
--) {
374 if (dev
->urb_list
[i
]) {
375 dev_dbg(&pdev
->dev
, "free urb=%d\n", i
);
377 usb_free_urb(dev
->urb_list
[i
]);
380 dev
->urbs_initialized
= 0;
385 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev
*dev
)
387 struct platform_device
*pdev
= dev
->pdev
;
390 /* allocate the URBs */
391 for (i
= 0; i
< MAX_BULK_BUFS
; i
++) {
392 dev_dbg(&pdev
->dev
, "alloc urb=%d\n", i
);
393 dev
->urb_list
[i
] = usb_alloc_urb(0, GFP_ATOMIC
);
394 if (!dev
->urb_list
[i
]) {
395 dev_dbg(&pdev
->dev
, "failed\n");
396 for (j
= 0; j
< i
; j
++)
397 usb_free_urb(dev
->urb_list
[j
]);
400 usb_fill_bulk_urb(dev
->urb_list
[i
],
402 usb_rcvbulkpipe(dev
->udev
, 0x81),
405 rtl2832_sdr_urb_complete
, dev
);
407 dev
->urb_list
[i
]->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
408 dev
->urb_list
[i
]->transfer_dma
= dev
->dma_addr
[i
];
409 dev
->urbs_initialized
++;
415 /* Must be called with vb_queue_lock hold */
416 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev
*dev
)
418 struct platform_device
*pdev
= dev
->pdev
;
421 dev_dbg(&pdev
->dev
, "\n");
423 spin_lock_irqsave(&dev
->queued_bufs_lock
, flags
);
424 while (!list_empty(&dev
->queued_bufs
)) {
425 struct rtl2832_sdr_frame_buf
*buf
;
427 buf
= list_entry(dev
->queued_bufs
.next
,
428 struct rtl2832_sdr_frame_buf
, list
);
429 list_del(&buf
->list
);
430 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
432 spin_unlock_irqrestore(&dev
->queued_bufs_lock
, flags
);
435 static int rtl2832_sdr_querycap(struct file
*file
, void *fh
,
436 struct v4l2_capability
*cap
)
438 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
439 struct platform_device
*pdev
= dev
->pdev
;
441 dev_dbg(&pdev
->dev
, "\n");
443 strlcpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
444 strlcpy(cap
->card
, dev
->vdev
.name
, sizeof(cap
->card
));
445 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
446 cap
->device_caps
= V4L2_CAP_SDR_CAPTURE
| V4L2_CAP_STREAMING
|
447 V4L2_CAP_READWRITE
| V4L2_CAP_TUNER
;
448 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
452 /* Videobuf2 operations */
453 static int rtl2832_sdr_queue_setup(struct vb2_queue
*vq
,
454 unsigned int *nbuffers
,
455 unsigned int *nplanes
, unsigned int sizes
[], struct device
*alloc_devs
[])
457 struct rtl2832_sdr_dev
*dev
= vb2_get_drv_priv(vq
);
458 struct platform_device
*pdev
= dev
->pdev
;
460 dev_dbg(&pdev
->dev
, "nbuffers=%d\n", *nbuffers
);
462 /* Need at least 8 buffers */
463 if (vq
->num_buffers
+ *nbuffers
< 8)
464 *nbuffers
= 8 - vq
->num_buffers
;
466 sizes
[0] = PAGE_ALIGN(dev
->buffersize
);
467 dev_dbg(&pdev
->dev
, "nbuffers=%d sizes[0]=%d\n", *nbuffers
, sizes
[0]);
471 static int rtl2832_sdr_buf_prepare(struct vb2_buffer
*vb
)
473 struct rtl2832_sdr_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
475 /* Don't allow queing new buffers after device disconnection */
482 static void rtl2832_sdr_buf_queue(struct vb2_buffer
*vb
)
484 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
485 struct rtl2832_sdr_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
486 struct rtl2832_sdr_frame_buf
*buf
=
487 container_of(vbuf
, struct rtl2832_sdr_frame_buf
, vb
);
490 /* Check the device has not disconnected between prep and queuing */
492 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
496 spin_lock_irqsave(&dev
->queued_bufs_lock
, flags
);
497 list_add_tail(&buf
->list
, &dev
->queued_bufs
);
498 spin_unlock_irqrestore(&dev
->queued_bufs_lock
, flags
);
501 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev
*dev
)
503 struct platform_device
*pdev
= dev
->pdev
;
504 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
505 struct dvb_frontend
*fe
= pdata
->dvb_frontend
;
507 unsigned int f_sr
, f_if
;
508 u8 buf
[4], u8tmp1
, u8tmp2
;
512 dev_dbg(&pdev
->dev
, "f_adc=%u\n", dev
->f_adc
);
514 if (!test_bit(POWER_ON
, &dev
->flags
))
522 ret
= regmap_bulk_write(dev
->regmap
, 0x13e, "\x00\x00", 2);
526 ret
= regmap_bulk_write(dev
->regmap
, 0x115, "\x00\x00\x00\x00", 4);
530 /* get IF from tuner */
531 if (fe
->ops
.tuner_ops
.get_if_frequency
)
532 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
, &f_if
);
540 u64tmp
= f_if
% pdata
->clk
;
542 u64tmp
= div_u64(u64tmp
, pdata
->clk
);
544 u32tmp
= u64tmp
& 0x3fffff;
546 dev_dbg(&pdev
->dev
, "f_if=%u if_ctl=%08x\n", f_if
, u32tmp
);
548 buf
[0] = (u32tmp
>> 16) & 0xff;
549 buf
[1] = (u32tmp
>> 8) & 0xff;
550 buf
[2] = (u32tmp
>> 0) & 0xff;
552 ret
= regmap_bulk_write(dev
->regmap
, 0x119, buf
, 3);
557 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
559 u8tmp1
= 0x1a; /* disable Zero-IF */
560 u8tmp2
= 0x8d; /* enable ADC I */
562 u8tmp1
= 0x1b; /* enable Zero-IF, DC, IQ */
563 u8tmp2
= 0xcd; /* enable ADC I, ADC Q */
566 ret
= regmap_write(dev
->regmap
, 0x1b1, u8tmp1
);
570 ret
= regmap_write(dev
->regmap
, 0x008, u8tmp2
);
574 ret
= regmap_write(dev
->regmap
, 0x006, 0x80);
578 /* program sampling rate (resampling down) */
579 u32tmp
= div_u64(pdata
->clk
* 0x400000ULL
, f_sr
* 4U);
581 buf
[0] = (u32tmp
>> 24) & 0xff;
582 buf
[1] = (u32tmp
>> 16) & 0xff;
583 buf
[2] = (u32tmp
>> 8) & 0xff;
584 buf
[3] = (u32tmp
>> 0) & 0xff;
585 ret
= regmap_bulk_write(dev
->regmap
, 0x19f, buf
, 4);
589 /* low-pass filter */
590 ret
= regmap_bulk_write(dev
->regmap
, 0x11c,
591 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
596 ret
= regmap_bulk_write(dev
->regmap
, 0x017, "\x11\x10", 2);
601 ret
= regmap_write(dev
->regmap
, 0x019, 0x05);
605 ret
= regmap_bulk_write(dev
->regmap
, 0x01a,
606 "\x1b\x16\x0d\x06\x01\xff", 6);
611 ret
= regmap_bulk_write(dev
->regmap
, 0x192, "\x00\xf0\x0f", 3);
616 ret
= regmap_write(dev
->regmap
, 0x061, 0x60);
620 /* used RF tuner based settings */
621 switch (pdata
->tuner
) {
622 case RTL2832_SDR_TUNER_E4000
:
623 ret
= regmap_write(dev
->regmap
, 0x112, 0x5a);
624 ret
= regmap_write(dev
->regmap
, 0x102, 0x40);
625 ret
= regmap_write(dev
->regmap
, 0x103, 0x5a);
626 ret
= regmap_write(dev
->regmap
, 0x1c7, 0x30);
627 ret
= regmap_write(dev
->regmap
, 0x104, 0xd0);
628 ret
= regmap_write(dev
->regmap
, 0x105, 0xbe);
629 ret
= regmap_write(dev
->regmap
, 0x1c8, 0x18);
630 ret
= regmap_write(dev
->regmap
, 0x106, 0x35);
631 ret
= regmap_write(dev
->regmap
, 0x1c9, 0x21);
632 ret
= regmap_write(dev
->regmap
, 0x1ca, 0x21);
633 ret
= regmap_write(dev
->regmap
, 0x1cb, 0x00);
634 ret
= regmap_write(dev
->regmap
, 0x107, 0x40);
635 ret
= regmap_write(dev
->regmap
, 0x1cd, 0x10);
636 ret
= regmap_write(dev
->regmap
, 0x1ce, 0x10);
637 ret
= regmap_write(dev
->regmap
, 0x108, 0x80);
638 ret
= regmap_write(dev
->regmap
, 0x109, 0x7f);
639 ret
= regmap_write(dev
->regmap
, 0x10a, 0x80);
640 ret
= regmap_write(dev
->regmap
, 0x10b, 0x7f);
641 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
642 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
643 ret
= regmap_write(dev
->regmap
, 0x011, 0xd4);
644 ret
= regmap_write(dev
->regmap
, 0x1e5, 0xf0);
645 ret
= regmap_write(dev
->regmap
, 0x1d9, 0x00);
646 ret
= regmap_write(dev
->regmap
, 0x1db, 0x00);
647 ret
= regmap_write(dev
->regmap
, 0x1dd, 0x14);
648 ret
= regmap_write(dev
->regmap
, 0x1de, 0xec);
649 ret
= regmap_write(dev
->regmap
, 0x1d8, 0x0c);
650 ret
= regmap_write(dev
->regmap
, 0x1e6, 0x02);
651 ret
= regmap_write(dev
->regmap
, 0x1d7, 0x09);
652 ret
= regmap_write(dev
->regmap
, 0x00d, 0x83);
653 ret
= regmap_write(dev
->regmap
, 0x010, 0x49);
654 ret
= regmap_write(dev
->regmap
, 0x00d, 0x87);
655 ret
= regmap_write(dev
->regmap
, 0x00d, 0x85);
656 ret
= regmap_write(dev
->regmap
, 0x013, 0x02);
658 case RTL2832_SDR_TUNER_FC0012
:
659 case RTL2832_SDR_TUNER_FC0013
:
660 ret
= regmap_write(dev
->regmap
, 0x112, 0x5a);
661 ret
= regmap_write(dev
->regmap
, 0x102, 0x40);
662 ret
= regmap_write(dev
->regmap
, 0x103, 0x5a);
663 ret
= regmap_write(dev
->regmap
, 0x1c7, 0x2c);
664 ret
= regmap_write(dev
->regmap
, 0x104, 0xcc);
665 ret
= regmap_write(dev
->regmap
, 0x105, 0xbe);
666 ret
= regmap_write(dev
->regmap
, 0x1c8, 0x16);
667 ret
= regmap_write(dev
->regmap
, 0x106, 0x35);
668 ret
= regmap_write(dev
->regmap
, 0x1c9, 0x21);
669 ret
= regmap_write(dev
->regmap
, 0x1ca, 0x21);
670 ret
= regmap_write(dev
->regmap
, 0x1cb, 0x00);
671 ret
= regmap_write(dev
->regmap
, 0x107, 0x40);
672 ret
= regmap_write(dev
->regmap
, 0x1cd, 0x10);
673 ret
= regmap_write(dev
->regmap
, 0x1ce, 0x10);
674 ret
= regmap_write(dev
->regmap
, 0x108, 0x80);
675 ret
= regmap_write(dev
->regmap
, 0x109, 0x7f);
676 ret
= regmap_write(dev
->regmap
, 0x10a, 0x80);
677 ret
= regmap_write(dev
->regmap
, 0x10b, 0x7f);
678 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
679 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
680 ret
= regmap_bulk_write(dev
->regmap
, 0x011, "\xe9\xbf", 2);
681 ret
= regmap_write(dev
->regmap
, 0x1e5, 0xf0);
682 ret
= regmap_write(dev
->regmap
, 0x1d9, 0x00);
683 ret
= regmap_write(dev
->regmap
, 0x1db, 0x00);
684 ret
= regmap_write(dev
->regmap
, 0x1dd, 0x11);
685 ret
= regmap_write(dev
->regmap
, 0x1de, 0xef);
686 ret
= regmap_write(dev
->regmap
, 0x1d8, 0x0c);
687 ret
= regmap_write(dev
->regmap
, 0x1e6, 0x02);
688 ret
= regmap_write(dev
->regmap
, 0x1d7, 0x09);
690 case RTL2832_SDR_TUNER_R820T
:
691 case RTL2832_SDR_TUNER_R828D
:
692 ret
= regmap_write(dev
->regmap
, 0x112, 0x5a);
693 ret
= regmap_write(dev
->regmap
, 0x102, 0x40);
694 ret
= regmap_write(dev
->regmap
, 0x115, 0x01);
695 ret
= regmap_write(dev
->regmap
, 0x103, 0x80);
696 ret
= regmap_write(dev
->regmap
, 0x1c7, 0x24);
697 ret
= regmap_write(dev
->regmap
, 0x104, 0xcc);
698 ret
= regmap_write(dev
->regmap
, 0x105, 0xbe);
699 ret
= regmap_write(dev
->regmap
, 0x1c8, 0x14);
700 ret
= regmap_write(dev
->regmap
, 0x106, 0x35);
701 ret
= regmap_write(dev
->regmap
, 0x1c9, 0x21);
702 ret
= regmap_write(dev
->regmap
, 0x1ca, 0x21);
703 ret
= regmap_write(dev
->regmap
, 0x1cb, 0x00);
704 ret
= regmap_write(dev
->regmap
, 0x107, 0x40);
705 ret
= regmap_write(dev
->regmap
, 0x1cd, 0x10);
706 ret
= regmap_write(dev
->regmap
, 0x1ce, 0x10);
707 ret
= regmap_write(dev
->regmap
, 0x108, 0x80);
708 ret
= regmap_write(dev
->regmap
, 0x109, 0x7f);
709 ret
= regmap_write(dev
->regmap
, 0x10a, 0x80);
710 ret
= regmap_write(dev
->regmap
, 0x10b, 0x7f);
711 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
712 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
713 ret
= regmap_write(dev
->regmap
, 0x011, 0xf4);
715 case RTL2832_SDR_TUNER_FC2580
:
716 ret
= regmap_write(dev
->regmap
, 0x112, 0x39);
717 ret
= regmap_write(dev
->regmap
, 0x102, 0x40);
718 ret
= regmap_write(dev
->regmap
, 0x103, 0x5a);
719 ret
= regmap_write(dev
->regmap
, 0x1c7, 0x2c);
720 ret
= regmap_write(dev
->regmap
, 0x104, 0xcc);
721 ret
= regmap_write(dev
->regmap
, 0x105, 0xbe);
722 ret
= regmap_write(dev
->regmap
, 0x1c8, 0x16);
723 ret
= regmap_write(dev
->regmap
, 0x106, 0x35);
724 ret
= regmap_write(dev
->regmap
, 0x1c9, 0x21);
725 ret
= regmap_write(dev
->regmap
, 0x1ca, 0x21);
726 ret
= regmap_write(dev
->regmap
, 0x1cb, 0x00);
727 ret
= regmap_write(dev
->regmap
, 0x107, 0x40);
728 ret
= regmap_write(dev
->regmap
, 0x1cd, 0x10);
729 ret
= regmap_write(dev
->regmap
, 0x1ce, 0x10);
730 ret
= regmap_write(dev
->regmap
, 0x108, 0x80);
731 ret
= regmap_write(dev
->regmap
, 0x109, 0x7f);
732 ret
= regmap_write(dev
->regmap
, 0x10a, 0x9c);
733 ret
= regmap_write(dev
->regmap
, 0x10b, 0x7f);
734 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
735 ret
= regmap_write(dev
->regmap
, 0x00e, 0xfc);
736 ret
= regmap_bulk_write(dev
->regmap
, 0x011, "\xe9\xf4", 2);
739 dev_notice(&pdev
->dev
, "Unsupported tuner\n");
743 ret
= regmap_update_bits(dev
->regmap
, 0x101, 0x04, 0x04);
747 ret
= regmap_update_bits(dev
->regmap
, 0x101, 0x04, 0x00);
754 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev
*dev
)
756 struct platform_device
*pdev
= dev
->pdev
;
759 dev_dbg(&pdev
->dev
, "\n");
762 ret
= regmap_write(dev
->regmap
, 0x061, 0xe0);
767 ret
= regmap_write(dev
->regmap
, 0x019, 0x20);
771 ret
= regmap_bulk_write(dev
->regmap
, 0x017, "\x11\x10", 2);
776 ret
= regmap_bulk_write(dev
->regmap
, 0x192, "\x00\x0f\xff", 3);
780 ret
= regmap_bulk_write(dev
->regmap
, 0x13e, "\x40\x00", 2);
784 ret
= regmap_bulk_write(dev
->regmap
, 0x115, "\x06\x3f\xce\xcc", 4);
791 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev
*dev
)
793 struct platform_device
*pdev
= dev
->pdev
;
794 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
795 struct dvb_frontend
*fe
= pdata
->dvb_frontend
;
796 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
797 struct v4l2_ctrl
*bandwidth_auto
;
798 struct v4l2_ctrl
*bandwidth
;
803 if (dev
->f_tuner
== 0)
809 bandwidth_auto
= v4l2_ctrl_find(&dev
->hdl
,
810 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
);
811 bandwidth
= v4l2_ctrl_find(&dev
->hdl
, V4L2_CID_RF_TUNER_BANDWIDTH
);
812 if (v4l2_ctrl_g_ctrl(bandwidth_auto
)) {
813 c
->bandwidth_hz
= dev
->f_adc
;
814 v4l2_ctrl_s_ctrl(bandwidth
, dev
->f_adc
);
816 c
->bandwidth_hz
= v4l2_ctrl_g_ctrl(bandwidth
);
819 c
->frequency
= dev
->f_tuner
;
820 c
->delivery_system
= SYS_DVBT
;
822 dev_dbg(&pdev
->dev
, "frequency=%u bandwidth=%d\n",
823 c
->frequency
, c
->bandwidth_hz
);
825 if (!test_bit(POWER_ON
, &dev
->flags
))
828 if (!V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, tuner
, s_frequency
)) {
829 if (fe
->ops
.tuner_ops
.set_params
)
830 fe
->ops
.tuner_ops
.set_params(fe
);
836 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev
*dev
)
838 struct platform_device
*pdev
= dev
->pdev
;
839 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
840 struct dvb_frontend
*fe
= pdata
->dvb_frontend
;
842 dev_dbg(&pdev
->dev
, "\n");
844 if (fe
->ops
.tuner_ops
.init
)
845 fe
->ops
.tuner_ops
.init(fe
);
850 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev
*dev
)
852 struct platform_device
*pdev
= dev
->pdev
;
853 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
854 struct dvb_frontend
*fe
= pdata
->dvb_frontend
;
856 dev_dbg(&pdev
->dev
, "\n");
858 if (fe
->ops
.tuner_ops
.sleep
)
859 fe
->ops
.tuner_ops
.sleep(fe
);
864 static int rtl2832_sdr_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
866 struct rtl2832_sdr_dev
*dev
= vb2_get_drv_priv(vq
);
867 struct platform_device
*pdev
= dev
->pdev
;
868 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
869 struct dvb_usb_device
*d
= pdata
->dvb_usb_device
;
872 dev_dbg(&pdev
->dev
, "\n");
877 if (mutex_lock_interruptible(&dev
->v4l2_lock
))
880 if (d
->props
->power_ctrl
)
881 d
->props
->power_ctrl(d
, 1);
884 if (d
->props
->frontend_ctrl
)
885 d
->props
->frontend_ctrl(pdata
->dvb_frontend
, 1);
887 set_bit(POWER_ON
, &dev
->flags
);
890 if (V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, core
, s_power
))
891 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, core
, s_power
, 1);
893 ret
= rtl2832_sdr_set_tuner(dev
);
897 ret
= rtl2832_sdr_set_tuner_freq(dev
);
901 ret
= rtl2832_sdr_set_adc(dev
);
905 ret
= rtl2832_sdr_alloc_stream_bufs(dev
);
909 ret
= rtl2832_sdr_alloc_urbs(dev
);
915 ret
= rtl2832_sdr_submit_urbs(dev
);
920 mutex_unlock(&dev
->v4l2_lock
);
925 static void rtl2832_sdr_stop_streaming(struct vb2_queue
*vq
)
927 struct rtl2832_sdr_dev
*dev
= vb2_get_drv_priv(vq
);
928 struct platform_device
*pdev
= dev
->pdev
;
929 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
930 struct dvb_usb_device
*d
= pdata
->dvb_usb_device
;
932 dev_dbg(&pdev
->dev
, "\n");
934 mutex_lock(&dev
->v4l2_lock
);
936 rtl2832_sdr_kill_urbs(dev
);
937 rtl2832_sdr_free_urbs(dev
);
938 rtl2832_sdr_free_stream_bufs(dev
);
939 rtl2832_sdr_cleanup_queued_bufs(dev
);
940 rtl2832_sdr_unset_adc(dev
);
943 if (V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, core
, s_power
))
944 v4l2_subdev_call(dev
->v4l2_subdev
, core
, s_power
, 0);
946 rtl2832_sdr_unset_tuner(dev
);
948 clear_bit(POWER_ON
, &dev
->flags
);
951 if (d
->props
->frontend_ctrl
)
952 d
->props
->frontend_ctrl(pdata
->dvb_frontend
, 0);
954 if (d
->props
->power_ctrl
)
955 d
->props
->power_ctrl(d
, 0);
957 mutex_unlock(&dev
->v4l2_lock
);
960 static struct vb2_ops rtl2832_sdr_vb2_ops
= {
961 .queue_setup
= rtl2832_sdr_queue_setup
,
962 .buf_prepare
= rtl2832_sdr_buf_prepare
,
963 .buf_queue
= rtl2832_sdr_buf_queue
,
964 .start_streaming
= rtl2832_sdr_start_streaming
,
965 .stop_streaming
= rtl2832_sdr_stop_streaming
,
966 .wait_prepare
= vb2_ops_wait_prepare
,
967 .wait_finish
= vb2_ops_wait_finish
,
970 static int rtl2832_sdr_g_tuner(struct file
*file
, void *priv
,
971 struct v4l2_tuner
*v
)
973 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
974 struct platform_device
*pdev
= dev
->pdev
;
977 dev_dbg(&pdev
->dev
, "index=%d type=%d\n", v
->index
, v
->type
);
980 strlcpy(v
->name
, "ADC: Realtek RTL2832", sizeof(v
->name
));
981 v
->type
= V4L2_TUNER_ADC
;
982 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
983 v
->rangelow
= 300000;
984 v
->rangehigh
= 3200000;
986 } else if (v
->index
== 1 &&
987 V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, tuner
, g_tuner
)) {
988 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, g_tuner
, v
);
989 } else if (v
->index
== 1) {
990 strlcpy(v
->name
, "RF: <unknown>", sizeof(v
->name
));
991 v
->type
= V4L2_TUNER_RF
;
992 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
993 v
->rangelow
= 50000000;
994 v
->rangehigh
= 2000000000;
1002 static int rtl2832_sdr_s_tuner(struct file
*file
, void *priv
,
1003 const struct v4l2_tuner
*v
)
1005 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1006 struct platform_device
*pdev
= dev
->pdev
;
1009 dev_dbg(&pdev
->dev
, "\n");
1011 if (v
->index
== 0) {
1013 } else if (v
->index
== 1 &&
1014 V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, tuner
, s_tuner
)) {
1015 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, s_tuner
, v
);
1016 } else if (v
->index
== 1) {
1024 static int rtl2832_sdr_enum_freq_bands(struct file
*file
, void *priv
,
1025 struct v4l2_frequency_band
*band
)
1027 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1028 struct platform_device
*pdev
= dev
->pdev
;
1031 dev_dbg(&pdev
->dev
, "tuner=%d type=%d index=%d\n",
1032 band
->tuner
, band
->type
, band
->index
);
1034 if (band
->tuner
== 0) {
1035 if (band
->index
>= ARRAY_SIZE(bands_adc
))
1038 *band
= bands_adc
[band
->index
];
1040 } else if (band
->tuner
== 1 &&
1041 V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, tuner
, enum_freq_bands
)) {
1042 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, enum_freq_bands
, band
);
1043 } else if (band
->tuner
== 1) {
1044 if (band
->index
>= ARRAY_SIZE(bands_fm
))
1047 *band
= bands_fm
[band
->index
];
1055 static int rtl2832_sdr_g_frequency(struct file
*file
, void *priv
,
1056 struct v4l2_frequency
*f
)
1058 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1059 struct platform_device
*pdev
= dev
->pdev
;
1062 dev_dbg(&pdev
->dev
, "tuner=%d type=%d\n", f
->tuner
, f
->type
);
1064 if (f
->tuner
== 0) {
1065 f
->frequency
= dev
->f_adc
;
1066 f
->type
= V4L2_TUNER_ADC
;
1068 } else if (f
->tuner
== 1 &&
1069 V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, tuner
, g_frequency
)) {
1070 f
->type
= V4L2_TUNER_RF
;
1071 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, g_frequency
, f
);
1072 } else if (f
->tuner
== 1) {
1073 f
->frequency
= dev
->f_tuner
;
1074 f
->type
= V4L2_TUNER_RF
;
1082 static int rtl2832_sdr_s_frequency(struct file
*file
, void *priv
,
1083 const struct v4l2_frequency
*f
)
1085 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1086 struct platform_device
*pdev
= dev
->pdev
;
1089 dev_dbg(&pdev
->dev
, "tuner=%d type=%d frequency=%u\n",
1090 f
->tuner
, f
->type
, f
->frequency
);
1092 /* ADC band midpoints */
1093 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1094 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1096 if (f
->tuner
== 0 && f
->type
== V4L2_TUNER_ADC
) {
1097 if (f
->frequency
< BAND_ADC_0
)
1099 else if (f
->frequency
< BAND_ADC_1
)
1104 dev
->f_adc
= clamp_t(unsigned int, f
->frequency
,
1105 bands_adc
[band
].rangelow
,
1106 bands_adc
[band
].rangehigh
);
1108 dev_dbg(&pdev
->dev
, "ADC frequency=%u Hz\n", dev
->f_adc
);
1109 ret
= rtl2832_sdr_set_adc(dev
);
1110 } else if (f
->tuner
== 1 &&
1111 V4L2_SUBDEV_HAS_OP(dev
->v4l2_subdev
, tuner
, s_frequency
)) {
1112 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, s_frequency
, f
);
1113 } else if (f
->tuner
== 1) {
1114 dev
->f_tuner
= clamp_t(unsigned int, f
->frequency
,
1115 bands_fm
[0].rangelow
,
1116 bands_fm
[0].rangehigh
);
1117 dev_dbg(&pdev
->dev
, "RF frequency=%u Hz\n", f
->frequency
);
1119 ret
= rtl2832_sdr_set_tuner_freq(dev
);
1126 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file
*file
, void *priv
,
1127 struct v4l2_fmtdesc
*f
)
1129 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1130 struct platform_device
*pdev
= dev
->pdev
;
1132 dev_dbg(&pdev
->dev
, "\n");
1134 if (f
->index
>= dev
->num_formats
)
1137 strlcpy(f
->description
, formats
[f
->index
].name
, sizeof(f
->description
));
1138 f
->pixelformat
= formats
[f
->index
].pixelformat
;
1143 static int rtl2832_sdr_g_fmt_sdr_cap(struct file
*file
, void *priv
,
1144 struct v4l2_format
*f
)
1146 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1147 struct platform_device
*pdev
= dev
->pdev
;
1149 dev_dbg(&pdev
->dev
, "\n");
1151 f
->fmt
.sdr
.pixelformat
= dev
->pixelformat
;
1152 f
->fmt
.sdr
.buffersize
= dev
->buffersize
;
1154 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
1159 static int rtl2832_sdr_s_fmt_sdr_cap(struct file
*file
, void *priv
,
1160 struct v4l2_format
*f
)
1162 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1163 struct platform_device
*pdev
= dev
->pdev
;
1164 struct vb2_queue
*q
= &dev
->vb_queue
;
1167 dev_dbg(&pdev
->dev
, "pixelformat fourcc %4.4s\n",
1168 (char *)&f
->fmt
.sdr
.pixelformat
);
1173 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
1174 for (i
= 0; i
< dev
->num_formats
; i
++) {
1175 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
1176 dev
->pixelformat
= formats
[i
].pixelformat
;
1177 dev
->buffersize
= formats
[i
].buffersize
;
1178 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
1183 dev
->pixelformat
= formats
[0].pixelformat
;
1184 dev
->buffersize
= formats
[0].buffersize
;
1185 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
1186 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
1191 static int rtl2832_sdr_try_fmt_sdr_cap(struct file
*file
, void *priv
,
1192 struct v4l2_format
*f
)
1194 struct rtl2832_sdr_dev
*dev
= video_drvdata(file
);
1195 struct platform_device
*pdev
= dev
->pdev
;
1198 dev_dbg(&pdev
->dev
, "pixelformat fourcc %4.4s\n",
1199 (char *)&f
->fmt
.sdr
.pixelformat
);
1201 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
1202 for (i
= 0; i
< dev
->num_formats
; i
++) {
1203 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
1204 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
1209 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
1210 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
1215 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops
= {
1216 .vidioc_querycap
= rtl2832_sdr_querycap
,
1218 .vidioc_enum_fmt_sdr_cap
= rtl2832_sdr_enum_fmt_sdr_cap
,
1219 .vidioc_g_fmt_sdr_cap
= rtl2832_sdr_g_fmt_sdr_cap
,
1220 .vidioc_s_fmt_sdr_cap
= rtl2832_sdr_s_fmt_sdr_cap
,
1221 .vidioc_try_fmt_sdr_cap
= rtl2832_sdr_try_fmt_sdr_cap
,
1223 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1224 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1225 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1226 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1227 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1228 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1230 .vidioc_streamon
= vb2_ioctl_streamon
,
1231 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1233 .vidioc_g_tuner
= rtl2832_sdr_g_tuner
,
1234 .vidioc_s_tuner
= rtl2832_sdr_s_tuner
,
1236 .vidioc_enum_freq_bands
= rtl2832_sdr_enum_freq_bands
,
1237 .vidioc_g_frequency
= rtl2832_sdr_g_frequency
,
1238 .vidioc_s_frequency
= rtl2832_sdr_s_frequency
,
1240 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1241 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1242 .vidioc_log_status
= v4l2_ctrl_log_status
,
1245 static const struct v4l2_file_operations rtl2832_sdr_fops
= {
1246 .owner
= THIS_MODULE
,
1247 .open
= v4l2_fh_open
,
1248 .release
= vb2_fop_release
,
1249 .read
= vb2_fop_read
,
1250 .poll
= vb2_fop_poll
,
1251 .mmap
= vb2_fop_mmap
,
1252 .unlocked_ioctl
= video_ioctl2
,
1255 static struct video_device rtl2832_sdr_template
= {
1256 .name
= "Realtek RTL2832 SDR",
1257 .release
= video_device_release_empty
,
1258 .fops
= &rtl2832_sdr_fops
,
1259 .ioctl_ops
= &rtl2832_sdr_ioctl_ops
,
1262 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl
*ctrl
)
1264 struct rtl2832_sdr_dev
*dev
=
1265 container_of(ctrl
->handler
, struct rtl2832_sdr_dev
,
1267 struct platform_device
*pdev
= dev
->pdev
;
1268 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
1269 struct dvb_frontend
*fe
= pdata
->dvb_frontend
;
1270 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1273 dev_dbg(&pdev
->dev
, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1274 ctrl
->id
, ctrl
->name
, ctrl
->val
, ctrl
->minimum
, ctrl
->maximum
,
1278 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
1279 case V4L2_CID_RF_TUNER_BANDWIDTH
:
1280 /* TODO: these controls should be moved to tuner drivers */
1281 if (dev
->bandwidth_auto
->val
) {
1282 /* Round towards the closest legal value */
1283 s32 val
= dev
->f_adc
+ div_u64(dev
->bandwidth
->step
, 2);
1286 val
= clamp_t(s32
, val
, dev
->bandwidth
->minimum
,
1287 dev
->bandwidth
->maximum
);
1288 offset
= val
- dev
->bandwidth
->minimum
;
1289 offset
= dev
->bandwidth
->step
*
1290 div_u64(offset
, dev
->bandwidth
->step
);
1291 dev
->bandwidth
->val
= dev
->bandwidth
->minimum
+ offset
;
1293 c
->bandwidth_hz
= dev
->bandwidth
->val
;
1295 if (!test_bit(POWER_ON
, &dev
->flags
))
1298 if (fe
->ops
.tuner_ops
.set_params
)
1299 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
1310 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops
= {
1311 .s_ctrl
= rtl2832_sdr_s_ctrl
,
1314 static void rtl2832_sdr_video_release(struct v4l2_device
*v
)
1316 struct rtl2832_sdr_dev
*dev
=
1317 container_of(v
, struct rtl2832_sdr_dev
, v4l2_dev
);
1318 struct platform_device
*pdev
= dev
->pdev
;
1320 dev_dbg(&pdev
->dev
, "\n");
1322 v4l2_ctrl_handler_free(&dev
->hdl
);
1323 v4l2_device_unregister(&dev
->v4l2_dev
);
1327 /* Platform driver interface */
1328 static int rtl2832_sdr_probe(struct platform_device
*pdev
)
1330 struct rtl2832_sdr_dev
*dev
;
1331 struct rtl2832_sdr_platform_data
*pdata
= pdev
->dev
.platform_data
;
1332 const struct v4l2_ctrl_ops
*ops
= &rtl2832_sdr_ctrl_ops
;
1333 struct v4l2_subdev
*subdev
;
1336 dev_dbg(&pdev
->dev
, "\n");
1339 dev_err(&pdev
->dev
, "Cannot proceed without platform data\n");
1343 if (!pdev
->dev
.parent
->driver
) {
1344 dev_dbg(&pdev
->dev
, "No parent device\n");
1348 /* try to refcount host drv since we are the consumer */
1349 if (!try_module_get(pdev
->dev
.parent
->driver
->owner
)) {
1350 dev_err(&pdev
->dev
, "Refcount fail");
1354 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1357 goto err_module_put
;
1360 /* setup the state */
1361 subdev
= pdata
->v4l2_subdev
;
1362 dev
->v4l2_subdev
= pdata
->v4l2_subdev
;
1364 dev
->regmap
= pdata
->regmap
;
1365 dev
->udev
= pdata
->dvb_usb_device
->udev
;
1366 dev
->f_adc
= bands_adc
[0].rangelow
;
1367 dev
->f_tuner
= bands_fm
[0].rangelow
;
1368 dev
->pixelformat
= formats
[0].pixelformat
;
1369 dev
->buffersize
= formats
[0].buffersize
;
1370 dev
->num_formats
= NUM_FORMATS
;
1371 if (!rtl2832_sdr_emulated_fmt
)
1372 dev
->num_formats
-= 1;
1374 mutex_init(&dev
->v4l2_lock
);
1375 mutex_init(&dev
->vb_queue_lock
);
1376 spin_lock_init(&dev
->queued_bufs_lock
);
1377 INIT_LIST_HEAD(&dev
->queued_bufs
);
1379 /* Init videobuf2 queue structure */
1380 dev
->vb_queue
.type
= V4L2_BUF_TYPE_SDR_CAPTURE
;
1381 dev
->vb_queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1382 dev
->vb_queue
.drv_priv
= dev
;
1383 dev
->vb_queue
.buf_struct_size
= sizeof(struct rtl2832_sdr_frame_buf
);
1384 dev
->vb_queue
.ops
= &rtl2832_sdr_vb2_ops
;
1385 dev
->vb_queue
.mem_ops
= &vb2_vmalloc_memops
;
1386 dev
->vb_queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1387 ret
= vb2_queue_init(&dev
->vb_queue
);
1389 dev_err(&pdev
->dev
, "Could not initialize vb2 queue\n");
1393 /* Register controls */
1394 switch (pdata
->tuner
) {
1395 case RTL2832_SDR_TUNER_E4000
:
1396 v4l2_ctrl_handler_init(&dev
->hdl
, 9);
1398 v4l2_ctrl_add_handler(&dev
->hdl
, subdev
->ctrl_handler
, NULL
);
1400 case RTL2832_SDR_TUNER_R820T
:
1401 case RTL2832_SDR_TUNER_R828D
:
1402 v4l2_ctrl_handler_init(&dev
->hdl
, 2);
1403 dev
->bandwidth_auto
= v4l2_ctrl_new_std(&dev
->hdl
, ops
,
1404 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
,
1406 dev
->bandwidth
= v4l2_ctrl_new_std(&dev
->hdl
, ops
,
1407 V4L2_CID_RF_TUNER_BANDWIDTH
,
1408 0, 8000000, 100000, 0);
1409 v4l2_ctrl_auto_cluster(2, &dev
->bandwidth_auto
, 0, false);
1411 case RTL2832_SDR_TUNER_FC0012
:
1412 case RTL2832_SDR_TUNER_FC0013
:
1413 v4l2_ctrl_handler_init(&dev
->hdl
, 2);
1414 dev
->bandwidth_auto
= v4l2_ctrl_new_std(&dev
->hdl
, ops
,
1415 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
,
1417 dev
->bandwidth
= v4l2_ctrl_new_std(&dev
->hdl
, ops
,
1418 V4L2_CID_RF_TUNER_BANDWIDTH
,
1419 6000000, 8000000, 1000000,
1421 v4l2_ctrl_auto_cluster(2, &dev
->bandwidth_auto
, 0, false);
1423 case RTL2832_SDR_TUNER_FC2580
:
1424 v4l2_ctrl_handler_init(&dev
->hdl
, 2);
1426 v4l2_ctrl_add_handler(&dev
->hdl
, subdev
->ctrl_handler
,
1430 v4l2_ctrl_handler_init(&dev
->hdl
, 0);
1431 dev_err(&pdev
->dev
, "Unsupported tuner\n");
1432 goto err_v4l2_ctrl_handler_free
;
1434 if (dev
->hdl
.error
) {
1435 ret
= dev
->hdl
.error
;
1436 dev_err(&pdev
->dev
, "Could not initialize controls\n");
1437 goto err_v4l2_ctrl_handler_free
;
1440 /* Init video_device structure */
1441 dev
->vdev
= rtl2832_sdr_template
;
1442 dev
->vdev
.queue
= &dev
->vb_queue
;
1443 dev
->vdev
.queue
->lock
= &dev
->vb_queue_lock
;
1444 video_set_drvdata(&dev
->vdev
, dev
);
1446 /* Register the v4l2_device structure */
1447 dev
->v4l2_dev
.release
= rtl2832_sdr_video_release
;
1448 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
1450 dev_err(&pdev
->dev
, "Failed to register v4l2-device %d\n", ret
);
1451 goto err_v4l2_ctrl_handler_free
;
1454 dev
->v4l2_dev
.ctrl_handler
= &dev
->hdl
;
1455 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1456 dev
->vdev
.lock
= &dev
->v4l2_lock
;
1457 dev
->vdev
.vfl_dir
= VFL_DIR_RX
;
1459 ret
= video_register_device(&dev
->vdev
, VFL_TYPE_SDR
, -1);
1461 dev_err(&pdev
->dev
, "Failed to register as video device %d\n",
1463 goto err_v4l2_device_unregister
;
1465 dev_info(&pdev
->dev
, "Registered as %s\n",
1466 video_device_node_name(&dev
->vdev
));
1467 dev_info(&pdev
->dev
, "Realtek RTL2832 SDR attached\n");
1468 dev_notice(&pdev
->dev
,
1469 "SDR API is still slightly experimental and functionality changes may follow\n");
1470 platform_set_drvdata(pdev
, dev
);
1472 err_v4l2_device_unregister
:
1473 v4l2_device_unregister(&dev
->v4l2_dev
);
1474 err_v4l2_ctrl_handler_free
:
1475 v4l2_ctrl_handler_free(&dev
->hdl
);
1479 module_put(pdev
->dev
.parent
->driver
->owner
);
1484 static int rtl2832_sdr_remove(struct platform_device
*pdev
)
1486 struct rtl2832_sdr_dev
*dev
= platform_get_drvdata(pdev
);
1488 dev_dbg(&pdev
->dev
, "\n");
1490 mutex_lock(&dev
->vb_queue_lock
);
1491 mutex_lock(&dev
->v4l2_lock
);
1492 /* No need to keep the urbs around after disconnection */
1494 v4l2_device_disconnect(&dev
->v4l2_dev
);
1495 video_unregister_device(&dev
->vdev
);
1496 mutex_unlock(&dev
->v4l2_lock
);
1497 mutex_unlock(&dev
->vb_queue_lock
);
1498 v4l2_device_put(&dev
->v4l2_dev
);
1499 module_put(pdev
->dev
.parent
->driver
->owner
);
1504 static struct platform_driver rtl2832_sdr_driver
= {
1506 .name
= "rtl2832_sdr",
1508 .probe
= rtl2832_sdr_probe
,
1509 .remove
= rtl2832_sdr_remove
,
1511 module_platform_driver(rtl2832_sdr_driver
);
1513 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1514 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1515 MODULE_LICENSE("GPL");