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