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