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