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