1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Realtek RTL2832U SDR driver 4 * 5 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> 6 * 7 * GNU Radio plugin "gr-kernel" for device usage will be on: 8 * https://git.linuxtv.org/anttip/gr-kernel.git 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 26 static bool rtl2832_sdr_emulated_fmt; 27 module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644); 28 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)"); 29 30 /* Original macro does not contain enough null pointer checks for our need */ 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 37 static 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 64 static 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 /* stream formats */ 76 struct rtl2832_sdr_format { 77 char *name; 78 u32 pixelformat; 79 u32 buffersize; 80 }; 81 82 static 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 92 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); 93 94 /* intermediate buffers with raw data from the USB device */ 95 struct rtl2832_sdr_frame_buf { 96 /* common v4l buffer stuff -- must be first */ 97 struct vb2_v4l2_buffer vb; 98 struct list_head list; 99 }; 100 101 struct rtl2832_sdr_dev { 102 #define POWER_ON 0 /* BIT(0) */ 103 #define URB_BUF 1 /* BIT(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 /* videobuf2 queue and queued buffers list */ 114 struct vb2_queue vb_queue; 115 struct list_head queued_bufs; 116 spinlock_t queued_bufs_lock; /* Protects queued_bufs */ 117 unsigned sequence; /* buffer sequence counter */ 118 119 /* Note if taking both locks v4l2_lock must always be locked first! */ 120 struct mutex v4l2_lock; /* Protects everything else */ 121 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ 122 123 /* Pointer to our usb_device, will be NULL after unplug */ 124 struct usb_device *udev; /* Both mutexes most be hold when setting! */ 125 126 unsigned int vb_full; /* vb is full and packets dropped */ 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 /* Controls */ 142 struct v4l2_ctrl_handler hdl; 143 struct v4l2_ctrl *bandwidth_auto; 144 struct v4l2_ctrl *bandwidth; 145 146 /* for sample rate calc */ 147 unsigned int sample; 148 unsigned int sample_measured; 149 unsigned long jiffies_next; 150 }; 151 152 /* Private functions */ 153 static 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); 166 leave: 167 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 168 return buf; 169 } 170 171 static 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 /* native stream, no need to convert */ 179 memcpy(dst, src, src_len); 180 dst_len = src_len; 181 } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) { 182 /* convert u8 to u16 */ 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 /* calculate sample rate and output it in 10 seconds intervals */ 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 /* total number of I+Q pairs */ 208 dev->sample += src_len / 2; 209 210 return dst_len; 211 } 212 213 /* 214 * This gets called for the bulk stream pipe. This is done in interrupt 215 * time, so it has to be fast, not crash, and not stall. Neat. 216 */ 217 static 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: /* success */ 229 case -ETIMEDOUT: /* NAK */ 230 break; 231 case -ECONNRESET: /* kill */ 232 case -ENOENT: 233 case -ESHUTDOWN: 234 return; 235 default: /* error */ 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 /* get free framebuffer */ 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 "video buffer is full, %d packets dropped\n", 249 dev->vb_full); 250 goto skip; 251 } 252 253 /* fill framebuffer */ 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 } 262 skip: 263 usb_submit_urb(urb, GFP_ATOMIC); 264 } 265 266 static 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 /* stop the URB */ 274 usb_kill_urb(dev->urb_list[i]); 275 } 276 dev->urbs_submitted = 0; 277 278 return 0; 279 } 280 281 static 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 302 static 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 320 static 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 350 static 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 /* free the URBs */ 361 usb_free_urb(dev->urb_list[i]); 362 } 363 } 364 dev->urbs_initialized = 0; 365 366 return 0; 367 } 368 369 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev) 370 { 371 struct platform_device *pdev = dev->pdev; 372 int i, j; 373 374 /* allocate the URBs */ 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 dev->urb_list[j] = NULL; 382 } 383 dev->urbs_initialized = 0; 384 return -ENOMEM; 385 } 386 usb_fill_bulk_urb(dev->urb_list[i], 387 dev->udev, 388 usb_rcvbulkpipe(dev->udev, 0x81), 389 dev->buf_list[i], 390 BULK_BUFFER_SIZE, 391 rtl2832_sdr_urb_complete, dev); 392 393 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 394 dev->urb_list[i]->transfer_dma = dev->dma_addr[i]; 395 dev->urbs_initialized++; 396 } 397 398 return 0; 399 } 400 401 /* Must be called with vb_queue_lock hold */ 402 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev, 403 enum vb2_buffer_state state) 404 { 405 struct platform_device *pdev = dev->pdev; 406 unsigned long flags; 407 408 dev_dbg(&pdev->dev, "\n"); 409 410 spin_lock_irqsave(&dev->queued_bufs_lock, flags); 411 while (!list_empty(&dev->queued_bufs)) { 412 struct rtl2832_sdr_frame_buf *buf; 413 414 buf = list_entry(dev->queued_bufs.next, 415 struct rtl2832_sdr_frame_buf, list); 416 list_del(&buf->list); 417 vb2_buffer_done(&buf->vb.vb2_buf, state); 418 } 419 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 420 } 421 422 static int rtl2832_sdr_querycap(struct file *file, void *fh, 423 struct v4l2_capability *cap) 424 { 425 struct rtl2832_sdr_dev *dev = video_drvdata(file); 426 struct platform_device *pdev = dev->pdev; 427 428 dev_dbg(&pdev->dev, "\n"); 429 430 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 431 strscpy(cap->card, dev->vdev.name, sizeof(cap->card)); 432 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 433 return 0; 434 } 435 436 /* Videobuf2 operations */ 437 static int rtl2832_sdr_queue_setup(struct vb2_queue *vq, 438 unsigned int *nbuffers, 439 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) 440 { 441 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 442 struct platform_device *pdev = dev->pdev; 443 unsigned int q_num_bufs = vb2_get_num_buffers(vq); 444 445 dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers); 446 447 /* Need at least 8 buffers */ 448 if (q_num_bufs + *nbuffers < 8) 449 *nbuffers = 8 - q_num_bufs; 450 *nplanes = 1; 451 sizes[0] = PAGE_ALIGN(dev->buffersize); 452 dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); 453 return 0; 454 } 455 456 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb) 457 { 458 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 459 460 /* Don't allow queueing new buffers after device disconnection */ 461 if (!dev->udev) 462 return -ENODEV; 463 464 return 0; 465 } 466 467 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb) 468 { 469 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 470 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 471 struct rtl2832_sdr_frame_buf *buf = 472 container_of(vbuf, struct rtl2832_sdr_frame_buf, vb); 473 unsigned long flags; 474 475 /* Check the device has not disconnected between prep and queuing */ 476 if (!dev->udev) { 477 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 478 return; 479 } 480 481 spin_lock_irqsave(&dev->queued_bufs_lock, flags); 482 list_add_tail(&buf->list, &dev->queued_bufs); 483 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 484 } 485 486 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev) 487 { 488 struct platform_device *pdev = dev->pdev; 489 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 490 struct dvb_frontend *fe = pdata->dvb_frontend; 491 int ret; 492 unsigned int f_sr, f_if; 493 u8 buf[4], u8tmp1, u8tmp2; 494 u64 u64tmp; 495 u32 u32tmp; 496 497 dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc); 498 499 if (!test_bit(POWER_ON, &dev->flags)) 500 return 0; 501 502 if (dev->f_adc == 0) 503 return 0; 504 505 f_sr = dev->f_adc; 506 507 ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2); 508 if (ret) 509 goto err; 510 511 ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4); 512 if (ret) 513 goto err; 514 515 /* get IF from tuner */ 516 if (fe->ops.tuner_ops.get_if_frequency) 517 ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if); 518 else 519 ret = -EINVAL; 520 521 if (ret) 522 goto err; 523 524 /* program IF */ 525 u64tmp = f_if % pdata->clk; 526 u64tmp *= 0x400000; 527 u64tmp = div_u64(u64tmp, pdata->clk); 528 u64tmp = -u64tmp; 529 u32tmp = u64tmp & 0x3fffff; 530 531 dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp); 532 533 buf[0] = (u32tmp >> 16) & 0xff; 534 buf[1] = (u32tmp >> 8) & 0xff; 535 buf[2] = (u32tmp >> 0) & 0xff; 536 537 ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3); 538 if (ret) 539 goto err; 540 541 /* BB / IF mode */ 542 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */ 543 if (f_if) { 544 u8tmp1 = 0x1a; /* disable Zero-IF */ 545 u8tmp2 = 0x8d; /* enable ADC I */ 546 } else { 547 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */ 548 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */ 549 } 550 551 ret = regmap_write(dev->regmap, 0x1b1, u8tmp1); 552 if (ret) 553 goto err; 554 555 ret = regmap_write(dev->regmap, 0x008, u8tmp2); 556 if (ret) 557 goto err; 558 559 ret = regmap_write(dev->regmap, 0x006, 0x80); 560 if (ret) 561 goto err; 562 563 /* program sampling rate (resampling down) */ 564 u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U); 565 u32tmp <<= 2; 566 buf[0] = (u32tmp >> 24) & 0xff; 567 buf[1] = (u32tmp >> 16) & 0xff; 568 buf[2] = (u32tmp >> 8) & 0xff; 569 buf[3] = (u32tmp >> 0) & 0xff; 570 ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4); 571 if (ret) 572 goto err; 573 574 /* low-pass filter */ 575 ret = regmap_bulk_write(dev->regmap, 0x11c, 576 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5", 577 20); 578 if (ret) 579 goto err; 580 581 ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2); 582 if (ret) 583 goto err; 584 585 /* mode */ 586 ret = regmap_write(dev->regmap, 0x019, 0x05); 587 if (ret) 588 goto err; 589 590 ret = regmap_bulk_write(dev->regmap, 0x01a, 591 "\x1b\x16\x0d\x06\x01\xff", 6); 592 if (ret) 593 goto err; 594 595 /* FSM */ 596 ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3); 597 if (ret) 598 goto err; 599 600 /* PID filter */ 601 ret = regmap_write(dev->regmap, 0x061, 0x60); 602 if (ret) 603 goto err; 604 605 /* used RF tuner based settings */ 606 switch (pdata->tuner) { 607 case RTL2832_SDR_TUNER_E4000: 608 ret = regmap_write(dev->regmap, 0x112, 0x5a); 609 ret = regmap_write(dev->regmap, 0x102, 0x40); 610 ret = regmap_write(dev->regmap, 0x103, 0x5a); 611 ret = regmap_write(dev->regmap, 0x1c7, 0x30); 612 ret = regmap_write(dev->regmap, 0x104, 0xd0); 613 ret = regmap_write(dev->regmap, 0x105, 0xbe); 614 ret = regmap_write(dev->regmap, 0x1c8, 0x18); 615 ret = regmap_write(dev->regmap, 0x106, 0x35); 616 ret = regmap_write(dev->regmap, 0x1c9, 0x21); 617 ret = regmap_write(dev->regmap, 0x1ca, 0x21); 618 ret = regmap_write(dev->regmap, 0x1cb, 0x00); 619 ret = regmap_write(dev->regmap, 0x107, 0x40); 620 ret = regmap_write(dev->regmap, 0x1cd, 0x10); 621 ret = regmap_write(dev->regmap, 0x1ce, 0x10); 622 ret = regmap_write(dev->regmap, 0x108, 0x80); 623 ret = regmap_write(dev->regmap, 0x109, 0x7f); 624 ret = regmap_write(dev->regmap, 0x10a, 0x80); 625 ret = regmap_write(dev->regmap, 0x10b, 0x7f); 626 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 627 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 628 ret = regmap_write(dev->regmap, 0x011, 0xd4); 629 ret = regmap_write(dev->regmap, 0x1e5, 0xf0); 630 ret = regmap_write(dev->regmap, 0x1d9, 0x00); 631 ret = regmap_write(dev->regmap, 0x1db, 0x00); 632 ret = regmap_write(dev->regmap, 0x1dd, 0x14); 633 ret = regmap_write(dev->regmap, 0x1de, 0xec); 634 ret = regmap_write(dev->regmap, 0x1d8, 0x0c); 635 ret = regmap_write(dev->regmap, 0x1e6, 0x02); 636 ret = regmap_write(dev->regmap, 0x1d7, 0x09); 637 ret = regmap_write(dev->regmap, 0x00d, 0x83); 638 ret = regmap_write(dev->regmap, 0x010, 0x49); 639 ret = regmap_write(dev->regmap, 0x00d, 0x87); 640 ret = regmap_write(dev->regmap, 0x00d, 0x85); 641 ret = regmap_write(dev->regmap, 0x013, 0x02); 642 break; 643 case RTL2832_SDR_TUNER_FC0012: 644 case RTL2832_SDR_TUNER_FC0013: 645 ret = regmap_write(dev->regmap, 0x112, 0x5a); 646 ret = regmap_write(dev->regmap, 0x102, 0x40); 647 ret = regmap_write(dev->regmap, 0x103, 0x5a); 648 ret = regmap_write(dev->regmap, 0x1c7, 0x2c); 649 ret = regmap_write(dev->regmap, 0x104, 0xcc); 650 ret = regmap_write(dev->regmap, 0x105, 0xbe); 651 ret = regmap_write(dev->regmap, 0x1c8, 0x16); 652 ret = regmap_write(dev->regmap, 0x106, 0x35); 653 ret = regmap_write(dev->regmap, 0x1c9, 0x21); 654 ret = regmap_write(dev->regmap, 0x1ca, 0x21); 655 ret = regmap_write(dev->regmap, 0x1cb, 0x00); 656 ret = regmap_write(dev->regmap, 0x107, 0x40); 657 ret = regmap_write(dev->regmap, 0x1cd, 0x10); 658 ret = regmap_write(dev->regmap, 0x1ce, 0x10); 659 ret = regmap_write(dev->regmap, 0x108, 0x80); 660 ret = regmap_write(dev->regmap, 0x109, 0x7f); 661 ret = regmap_write(dev->regmap, 0x10a, 0x80); 662 ret = regmap_write(dev->regmap, 0x10b, 0x7f); 663 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 664 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 665 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2); 666 ret = regmap_write(dev->regmap, 0x1e5, 0xf0); 667 ret = regmap_write(dev->regmap, 0x1d9, 0x00); 668 ret = regmap_write(dev->regmap, 0x1db, 0x00); 669 ret = regmap_write(dev->regmap, 0x1dd, 0x11); 670 ret = regmap_write(dev->regmap, 0x1de, 0xef); 671 ret = regmap_write(dev->regmap, 0x1d8, 0x0c); 672 ret = regmap_write(dev->regmap, 0x1e6, 0x02); 673 ret = regmap_write(dev->regmap, 0x1d7, 0x09); 674 break; 675 case RTL2832_SDR_TUNER_R820T: 676 case RTL2832_SDR_TUNER_R828D: 677 ret = regmap_write(dev->regmap, 0x112, 0x5a); 678 ret = regmap_write(dev->regmap, 0x102, 0x40); 679 ret = regmap_write(dev->regmap, 0x115, 0x01); 680 ret = regmap_write(dev->regmap, 0x103, 0x80); 681 ret = regmap_write(dev->regmap, 0x1c7, 0x24); 682 ret = regmap_write(dev->regmap, 0x104, 0xcc); 683 ret = regmap_write(dev->regmap, 0x105, 0xbe); 684 ret = regmap_write(dev->regmap, 0x1c8, 0x14); 685 ret = regmap_write(dev->regmap, 0x106, 0x35); 686 ret = regmap_write(dev->regmap, 0x1c9, 0x21); 687 ret = regmap_write(dev->regmap, 0x1ca, 0x21); 688 ret = regmap_write(dev->regmap, 0x1cb, 0x00); 689 ret = regmap_write(dev->regmap, 0x107, 0x40); 690 ret = regmap_write(dev->regmap, 0x1cd, 0x10); 691 ret = regmap_write(dev->regmap, 0x1ce, 0x10); 692 ret = regmap_write(dev->regmap, 0x108, 0x80); 693 ret = regmap_write(dev->regmap, 0x109, 0x7f); 694 ret = regmap_write(dev->regmap, 0x10a, 0x80); 695 ret = regmap_write(dev->regmap, 0x10b, 0x7f); 696 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 697 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 698 ret = regmap_write(dev->regmap, 0x011, 0xf4); 699 break; 700 case RTL2832_SDR_TUNER_FC2580: 701 ret = regmap_write(dev->regmap, 0x112, 0x39); 702 ret = regmap_write(dev->regmap, 0x102, 0x40); 703 ret = regmap_write(dev->regmap, 0x103, 0x5a); 704 ret = regmap_write(dev->regmap, 0x1c7, 0x2c); 705 ret = regmap_write(dev->regmap, 0x104, 0xcc); 706 ret = regmap_write(dev->regmap, 0x105, 0xbe); 707 ret = regmap_write(dev->regmap, 0x1c8, 0x16); 708 ret = regmap_write(dev->regmap, 0x106, 0x35); 709 ret = regmap_write(dev->regmap, 0x1c9, 0x21); 710 ret = regmap_write(dev->regmap, 0x1ca, 0x21); 711 ret = regmap_write(dev->regmap, 0x1cb, 0x00); 712 ret = regmap_write(dev->regmap, 0x107, 0x40); 713 ret = regmap_write(dev->regmap, 0x1cd, 0x10); 714 ret = regmap_write(dev->regmap, 0x1ce, 0x10); 715 ret = regmap_write(dev->regmap, 0x108, 0x80); 716 ret = regmap_write(dev->regmap, 0x109, 0x7f); 717 ret = regmap_write(dev->regmap, 0x10a, 0x9c); 718 ret = regmap_write(dev->regmap, 0x10b, 0x7f); 719 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 720 ret = regmap_write(dev->regmap, 0x00e, 0xfc); 721 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2); 722 break; 723 default: 724 dev_notice(&pdev->dev, "Unsupported tuner\n"); 725 } 726 727 /* software reset */ 728 ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04); 729 if (ret) 730 goto err; 731 732 ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00); 733 if (ret) 734 goto err; 735 err: 736 return ret; 737 }; 738 739 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev) 740 { 741 struct platform_device *pdev = dev->pdev; 742 int ret; 743 744 dev_dbg(&pdev->dev, "\n"); 745 746 /* PID filter */ 747 ret = regmap_write(dev->regmap, 0x061, 0xe0); 748 if (ret) 749 goto err; 750 751 /* mode */ 752 ret = regmap_write(dev->regmap, 0x019, 0x20); 753 if (ret) 754 goto err; 755 756 ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2); 757 if (ret) 758 goto err; 759 760 /* FSM */ 761 ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3); 762 if (ret) 763 goto err; 764 765 ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2); 766 if (ret) 767 goto err; 768 769 ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4); 770 if (ret) 771 goto err; 772 err: 773 return; 774 }; 775 776 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev) 777 { 778 struct platform_device *pdev = dev->pdev; 779 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 780 struct dvb_frontend *fe = pdata->dvb_frontend; 781 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 782 struct v4l2_ctrl *bandwidth_auto; 783 struct v4l2_ctrl *bandwidth; 784 785 /* 786 * tuner RF (Hz) 787 */ 788 if (dev->f_tuner == 0) 789 return 0; 790 791 /* 792 * bandwidth (Hz) 793 */ 794 bandwidth_auto = v4l2_ctrl_find(&dev->hdl, 795 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO); 796 bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH); 797 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) { 798 c->bandwidth_hz = dev->f_adc; 799 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc); 800 } else { 801 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth); 802 } 803 804 c->frequency = dev->f_tuner; 805 c->delivery_system = SYS_DVBT; 806 807 dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n", 808 c->frequency, c->bandwidth_hz); 809 810 if (!test_bit(POWER_ON, &dev->flags)) 811 return 0; 812 813 if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) { 814 if (fe->ops.tuner_ops.set_params) 815 fe->ops.tuner_ops.set_params(fe); 816 } 817 818 return 0; 819 }; 820 821 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev) 822 { 823 struct platform_device *pdev = dev->pdev; 824 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 825 struct dvb_frontend *fe = pdata->dvb_frontend; 826 827 dev_dbg(&pdev->dev, "\n"); 828 829 if (fe->ops.tuner_ops.init) 830 fe->ops.tuner_ops.init(fe); 831 832 return 0; 833 }; 834 835 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev) 836 { 837 struct platform_device *pdev = dev->pdev; 838 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 839 struct dvb_frontend *fe = pdata->dvb_frontend; 840 841 dev_dbg(&pdev->dev, "\n"); 842 843 if (fe->ops.tuner_ops.sleep) 844 fe->ops.tuner_ops.sleep(fe); 845 846 return; 847 }; 848 849 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count) 850 { 851 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 852 struct platform_device *pdev = dev->pdev; 853 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 854 struct dvb_usb_device *d = pdata->dvb_usb_device; 855 int ret; 856 857 dev_dbg(&pdev->dev, "\n"); 858 859 if (!dev->udev) { 860 rtl2832_sdr_cleanup_queued_bufs(dev, VB2_BUF_STATE_QUEUED); 861 return -ENODEV; 862 } 863 864 if (mutex_lock_interruptible(&dev->v4l2_lock)) { 865 rtl2832_sdr_cleanup_queued_bufs(dev, VB2_BUF_STATE_QUEUED); 866 return -ERESTARTSYS; 867 } 868 869 if (d->props->power_ctrl) 870 d->props->power_ctrl(d, 1); 871 872 /* enable ADC */ 873 if (d->props->frontend_ctrl) 874 d->props->frontend_ctrl(pdata->dvb_frontend, 1); 875 876 set_bit(POWER_ON, &dev->flags); 877 878 /* wake-up tuner */ 879 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power)) 880 ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1); 881 else 882 ret = rtl2832_sdr_set_tuner(dev); 883 if (ret) 884 goto err; 885 886 ret = rtl2832_sdr_set_tuner_freq(dev); 887 if (ret) 888 goto err; 889 890 ret = rtl2832_sdr_set_adc(dev); 891 if (ret) 892 goto err; 893 894 ret = rtl2832_sdr_alloc_stream_bufs(dev); 895 if (ret) 896 goto err; 897 898 ret = rtl2832_sdr_alloc_urbs(dev); 899 if (ret) 900 goto err; 901 902 dev->sequence = 0; 903 904 ret = rtl2832_sdr_submit_urbs(dev); 905 if (ret) 906 goto err; 907 908 mutex_unlock(&dev->v4l2_lock); 909 return 0; 910 911 err: 912 rtl2832_sdr_cleanup_queued_bufs(dev, VB2_BUF_STATE_QUEUED); 913 mutex_unlock(&dev->v4l2_lock); 914 915 return ret; 916 } 917 918 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq) 919 { 920 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 921 struct platform_device *pdev = dev->pdev; 922 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 923 struct dvb_usb_device *d = pdata->dvb_usb_device; 924 925 dev_dbg(&pdev->dev, "\n"); 926 927 mutex_lock(&dev->v4l2_lock); 928 929 rtl2832_sdr_kill_urbs(dev); 930 rtl2832_sdr_free_urbs(dev); 931 rtl2832_sdr_free_stream_bufs(dev); 932 rtl2832_sdr_cleanup_queued_bufs(dev, VB2_BUF_STATE_ERROR); 933 rtl2832_sdr_unset_adc(dev); 934 935 /* sleep tuner */ 936 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power)) 937 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0); 938 else 939 rtl2832_sdr_unset_tuner(dev); 940 941 clear_bit(POWER_ON, &dev->flags); 942 943 /* disable ADC */ 944 if (d->props->frontend_ctrl) 945 d->props->frontend_ctrl(pdata->dvb_frontend, 0); 946 947 if (d->props->power_ctrl) 948 d->props->power_ctrl(d, 0); 949 950 mutex_unlock(&dev->v4l2_lock); 951 } 952 953 static const struct vb2_ops rtl2832_sdr_vb2_ops = { 954 .queue_setup = rtl2832_sdr_queue_setup, 955 .buf_prepare = rtl2832_sdr_buf_prepare, 956 .buf_queue = rtl2832_sdr_buf_queue, 957 .start_streaming = rtl2832_sdr_start_streaming, 958 .stop_streaming = rtl2832_sdr_stop_streaming, 959 }; 960 961 static int rtl2832_sdr_g_tuner(struct file *file, void *priv, 962 struct v4l2_tuner *v) 963 { 964 struct rtl2832_sdr_dev *dev = video_drvdata(file); 965 struct platform_device *pdev = dev->pdev; 966 int ret; 967 968 dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type); 969 970 if (v->index == 0) { 971 strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name)); 972 v->type = V4L2_TUNER_ADC; 973 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 974 v->rangelow = 300000; 975 v->rangehigh = 3200000; 976 ret = 0; 977 } else if (v->index == 1 && 978 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) { 979 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v); 980 } else if (v->index == 1) { 981 strscpy(v->name, "RF: <unknown>", sizeof(v->name)); 982 v->type = V4L2_TUNER_RF; 983 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 984 v->rangelow = 50000000; 985 v->rangehigh = 2000000000; 986 ret = 0; 987 } else { 988 ret = -EINVAL; 989 } 990 return ret; 991 } 992 993 static int rtl2832_sdr_s_tuner(struct file *file, void *priv, 994 const struct v4l2_tuner *v) 995 { 996 struct rtl2832_sdr_dev *dev = video_drvdata(file); 997 struct platform_device *pdev = dev->pdev; 998 int ret; 999 1000 dev_dbg(&pdev->dev, "\n"); 1001 1002 if (v->index == 0) { 1003 ret = 0; 1004 } else if (v->index == 1 && 1005 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) { 1006 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v); 1007 } else if (v->index == 1) { 1008 ret = 0; 1009 } else { 1010 ret = -EINVAL; 1011 } 1012 return ret; 1013 } 1014 1015 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv, 1016 struct v4l2_frequency_band *band) 1017 { 1018 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1019 struct platform_device *pdev = dev->pdev; 1020 int ret; 1021 1022 dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n", 1023 band->tuner, band->type, band->index); 1024 1025 if (band->tuner == 0) { 1026 if (band->index >= ARRAY_SIZE(bands_adc)) 1027 return -EINVAL; 1028 1029 *band = bands_adc[band->index]; 1030 ret = 0; 1031 } else if (band->tuner == 1 && 1032 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) { 1033 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band); 1034 } else if (band->tuner == 1) { 1035 if (band->index >= ARRAY_SIZE(bands_fm)) 1036 return -EINVAL; 1037 1038 *band = bands_fm[band->index]; 1039 ret = 0; 1040 } else { 1041 ret = -EINVAL; 1042 } 1043 return ret; 1044 } 1045 1046 static int rtl2832_sdr_g_frequency(struct file *file, void *priv, 1047 struct v4l2_frequency *f) 1048 { 1049 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1050 struct platform_device *pdev = dev->pdev; 1051 int ret; 1052 1053 dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type); 1054 1055 if (f->tuner == 0) { 1056 f->frequency = dev->f_adc; 1057 f->type = V4L2_TUNER_ADC; 1058 ret = 0; 1059 } else if (f->tuner == 1 && 1060 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) { 1061 f->type = V4L2_TUNER_RF; 1062 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f); 1063 } else if (f->tuner == 1) { 1064 f->frequency = dev->f_tuner; 1065 f->type = V4L2_TUNER_RF; 1066 ret = 0; 1067 } else { 1068 ret = -EINVAL; 1069 } 1070 return ret; 1071 } 1072 1073 static int rtl2832_sdr_s_frequency(struct file *file, void *priv, 1074 const struct v4l2_frequency *f) 1075 { 1076 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1077 struct platform_device *pdev = dev->pdev; 1078 int ret, band; 1079 1080 dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n", 1081 f->tuner, f->type, f->frequency); 1082 1083 /* ADC band midpoints */ 1084 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2) 1085 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2) 1086 1087 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) { 1088 if (f->frequency < BAND_ADC_0) 1089 band = 0; 1090 else if (f->frequency < BAND_ADC_1) 1091 band = 1; 1092 else 1093 band = 2; 1094 1095 dev->f_adc = clamp_t(unsigned int, f->frequency, 1096 bands_adc[band].rangelow, 1097 bands_adc[band].rangehigh); 1098 1099 dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc); 1100 ret = rtl2832_sdr_set_adc(dev); 1101 } else if (f->tuner == 1 && 1102 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) { 1103 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f); 1104 } else if (f->tuner == 1) { 1105 dev->f_tuner = clamp_t(unsigned int, f->frequency, 1106 bands_fm[0].rangelow, 1107 bands_fm[0].rangehigh); 1108 dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency); 1109 1110 ret = rtl2832_sdr_set_tuner_freq(dev); 1111 } else { 1112 ret = -EINVAL; 1113 } 1114 return ret; 1115 } 1116 1117 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv, 1118 struct v4l2_fmtdesc *f) 1119 { 1120 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1121 struct platform_device *pdev = dev->pdev; 1122 1123 dev_dbg(&pdev->dev, "\n"); 1124 1125 if (f->index >= dev->num_formats) 1126 return -EINVAL; 1127 1128 f->pixelformat = formats[f->index].pixelformat; 1129 1130 return 0; 1131 } 1132 1133 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv, 1134 struct v4l2_format *f) 1135 { 1136 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1137 struct platform_device *pdev = dev->pdev; 1138 1139 dev_dbg(&pdev->dev, "\n"); 1140 1141 f->fmt.sdr.pixelformat = dev->pixelformat; 1142 f->fmt.sdr.buffersize = dev->buffersize; 1143 1144 return 0; 1145 } 1146 1147 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv, 1148 struct v4l2_format *f) 1149 { 1150 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1151 struct platform_device *pdev = dev->pdev; 1152 struct vb2_queue *q = &dev->vb_queue; 1153 int i; 1154 1155 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", 1156 (char *)&f->fmt.sdr.pixelformat); 1157 1158 if (vb2_is_busy(q)) 1159 return -EBUSY; 1160 1161 for (i = 0; i < dev->num_formats; i++) { 1162 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 1163 dev->pixelformat = formats[i].pixelformat; 1164 dev->buffersize = formats[i].buffersize; 1165 f->fmt.sdr.buffersize = formats[i].buffersize; 1166 return 0; 1167 } 1168 } 1169 1170 dev->pixelformat = formats[0].pixelformat; 1171 dev->buffersize = formats[0].buffersize; 1172 f->fmt.sdr.pixelformat = formats[0].pixelformat; 1173 f->fmt.sdr.buffersize = formats[0].buffersize; 1174 1175 return 0; 1176 } 1177 1178 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv, 1179 struct v4l2_format *f) 1180 { 1181 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1182 struct platform_device *pdev = dev->pdev; 1183 int i; 1184 1185 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", 1186 (char *)&f->fmt.sdr.pixelformat); 1187 1188 for (i = 0; i < dev->num_formats; i++) { 1189 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 1190 f->fmt.sdr.buffersize = formats[i].buffersize; 1191 return 0; 1192 } 1193 } 1194 1195 f->fmt.sdr.pixelformat = formats[0].pixelformat; 1196 f->fmt.sdr.buffersize = formats[0].buffersize; 1197 1198 return 0; 1199 } 1200 1201 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = { 1202 .vidioc_querycap = rtl2832_sdr_querycap, 1203 1204 .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap, 1205 .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap, 1206 .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap, 1207 .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap, 1208 1209 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1210 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1211 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1212 .vidioc_querybuf = vb2_ioctl_querybuf, 1213 .vidioc_qbuf = vb2_ioctl_qbuf, 1214 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1215 1216 .vidioc_streamon = vb2_ioctl_streamon, 1217 .vidioc_streamoff = vb2_ioctl_streamoff, 1218 1219 .vidioc_g_tuner = rtl2832_sdr_g_tuner, 1220 .vidioc_s_tuner = rtl2832_sdr_s_tuner, 1221 1222 .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands, 1223 .vidioc_g_frequency = rtl2832_sdr_g_frequency, 1224 .vidioc_s_frequency = rtl2832_sdr_s_frequency, 1225 1226 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1227 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1228 .vidioc_log_status = v4l2_ctrl_log_status, 1229 }; 1230 1231 static const struct v4l2_file_operations rtl2832_sdr_fops = { 1232 .owner = THIS_MODULE, 1233 .open = v4l2_fh_open, 1234 .release = vb2_fop_release, 1235 .read = vb2_fop_read, 1236 .poll = vb2_fop_poll, 1237 .mmap = vb2_fop_mmap, 1238 .unlocked_ioctl = video_ioctl2, 1239 }; 1240 1241 static struct video_device rtl2832_sdr_template = { 1242 .name = "Realtek RTL2832 SDR", 1243 .release = video_device_release_empty, 1244 .fops = &rtl2832_sdr_fops, 1245 .ioctl_ops = &rtl2832_sdr_ioctl_ops, 1246 .device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | 1247 V4L2_CAP_READWRITE | V4L2_CAP_TUNER, 1248 }; 1249 1250 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl) 1251 { 1252 struct rtl2832_sdr_dev *dev = 1253 container_of(ctrl->handler, struct rtl2832_sdr_dev, 1254 hdl); 1255 struct platform_device *pdev = dev->pdev; 1256 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 1257 struct dvb_frontend *fe = pdata->dvb_frontend; 1258 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1259 int ret; 1260 1261 dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n", 1262 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum, 1263 ctrl->step); 1264 1265 switch (ctrl->id) { 1266 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: 1267 case V4L2_CID_RF_TUNER_BANDWIDTH: 1268 /* TODO: these controls should be moved to tuner drivers */ 1269 if (dev->bandwidth_auto->val) { 1270 /* Round towards the closest legal value */ 1271 s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2); 1272 u32 offset; 1273 1274 val = clamp_t(s32, val, dev->bandwidth->minimum, 1275 dev->bandwidth->maximum); 1276 offset = val - dev->bandwidth->minimum; 1277 offset = dev->bandwidth->step * 1278 div_u64(offset, dev->bandwidth->step); 1279 dev->bandwidth->val = dev->bandwidth->minimum + offset; 1280 } 1281 c->bandwidth_hz = dev->bandwidth->val; 1282 1283 if (!test_bit(POWER_ON, &dev->flags)) 1284 return 0; 1285 1286 if (fe->ops.tuner_ops.set_params) 1287 ret = fe->ops.tuner_ops.set_params(fe); 1288 else 1289 ret = 0; 1290 break; 1291 default: 1292 ret = -EINVAL; 1293 } 1294 1295 return ret; 1296 } 1297 1298 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = { 1299 .s_ctrl = rtl2832_sdr_s_ctrl, 1300 }; 1301 1302 static void rtl2832_sdr_video_release(struct v4l2_device *v) 1303 { 1304 struct rtl2832_sdr_dev *dev = 1305 container_of(v, struct rtl2832_sdr_dev, v4l2_dev); 1306 struct platform_device *pdev = dev->pdev; 1307 1308 dev_dbg(&pdev->dev, "\n"); 1309 1310 v4l2_ctrl_handler_free(&dev->hdl); 1311 v4l2_device_unregister(&dev->v4l2_dev); 1312 kfree(dev); 1313 } 1314 1315 /* Platform driver interface */ 1316 static int rtl2832_sdr_probe(struct platform_device *pdev) 1317 { 1318 struct rtl2832_sdr_dev *dev; 1319 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 1320 const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops; 1321 struct v4l2_subdev *subdev; 1322 int ret; 1323 1324 dev_dbg(&pdev->dev, "\n"); 1325 1326 if (!pdata) { 1327 dev_err(&pdev->dev, "Cannot proceed without platform data\n"); 1328 ret = -EINVAL; 1329 goto err; 1330 } 1331 if (!pdev->dev.parent->driver) { 1332 dev_dbg(&pdev->dev, "No parent device\n"); 1333 ret = -EINVAL; 1334 goto err; 1335 } 1336 /* try to refcount host drv since we are the consumer */ 1337 if (!try_module_get(pdev->dev.parent->driver->owner)) { 1338 dev_err(&pdev->dev, "Refcount fail"); 1339 ret = -EINVAL; 1340 goto err; 1341 } 1342 dev = kzalloc_obj(*dev); 1343 if (dev == NULL) { 1344 ret = -ENOMEM; 1345 goto err_module_put; 1346 } 1347 1348 /* setup the state */ 1349 subdev = pdata->v4l2_subdev; 1350 dev->v4l2_subdev = pdata->v4l2_subdev; 1351 dev->pdev = pdev; 1352 dev->regmap = pdata->regmap; 1353 dev->udev = pdata->dvb_usb_device->udev; 1354 dev->f_adc = bands_adc[0].rangelow; 1355 dev->f_tuner = bands_fm[0].rangelow; 1356 dev->pixelformat = formats[0].pixelformat; 1357 dev->buffersize = formats[0].buffersize; 1358 dev->num_formats = NUM_FORMATS; 1359 if (!rtl2832_sdr_emulated_fmt) 1360 dev->num_formats -= 1; 1361 1362 mutex_init(&dev->v4l2_lock); 1363 mutex_init(&dev->vb_queue_lock); 1364 spin_lock_init(&dev->queued_bufs_lock); 1365 INIT_LIST_HEAD(&dev->queued_bufs); 1366 1367 /* Init videobuf2 queue structure */ 1368 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; 1369 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1370 dev->vb_queue.drv_priv = dev; 1371 dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf); 1372 dev->vb_queue.ops = &rtl2832_sdr_vb2_ops; 1373 dev->vb_queue.mem_ops = &vb2_vmalloc_memops; 1374 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1375 dev->vb_queue.lock = &dev->vb_queue_lock; 1376 ret = vb2_queue_init(&dev->vb_queue); 1377 if (ret) { 1378 dev_err(&pdev->dev, "Could not initialize vb2 queue\n"); 1379 goto err_kfree; 1380 } 1381 1382 /* Register controls */ 1383 switch (pdata->tuner) { 1384 case RTL2832_SDR_TUNER_E4000: 1385 v4l2_ctrl_handler_init(&dev->hdl, 9); 1386 if (subdev) 1387 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, 1388 NULL, true); 1389 break; 1390 case RTL2832_SDR_TUNER_R820T: 1391 case RTL2832_SDR_TUNER_R828D: 1392 v4l2_ctrl_handler_init(&dev->hdl, 2); 1393 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, 1394 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 1395 0, 1, 1, 1); 1396 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, 1397 V4L2_CID_RF_TUNER_BANDWIDTH, 1398 0, 8000000, 100000, 0); 1399 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); 1400 break; 1401 case RTL2832_SDR_TUNER_FC0012: 1402 case RTL2832_SDR_TUNER_FC0013: 1403 v4l2_ctrl_handler_init(&dev->hdl, 2); 1404 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, 1405 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 1406 0, 1, 1, 1); 1407 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, 1408 V4L2_CID_RF_TUNER_BANDWIDTH, 1409 6000000, 8000000, 1000000, 1410 6000000); 1411 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); 1412 break; 1413 case RTL2832_SDR_TUNER_FC2580: 1414 v4l2_ctrl_handler_init(&dev->hdl, 2); 1415 if (subdev) 1416 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, 1417 NULL, true); 1418 break; 1419 default: 1420 v4l2_ctrl_handler_init(&dev->hdl, 0); 1421 dev_err(&pdev->dev, "Unsupported tuner\n"); 1422 ret = -ENODEV; 1423 goto err_v4l2_ctrl_handler_free; 1424 } 1425 if (dev->hdl.error) { 1426 ret = dev->hdl.error; 1427 dev_err(&pdev->dev, "Could not initialize controls\n"); 1428 goto err_v4l2_ctrl_handler_free; 1429 } 1430 1431 /* Init video_device structure */ 1432 dev->vdev = rtl2832_sdr_template; 1433 dev->vdev.queue = &dev->vb_queue; 1434 video_set_drvdata(&dev->vdev, dev); 1435 1436 /* Register the v4l2_device structure */ 1437 dev->v4l2_dev.release = rtl2832_sdr_video_release; 1438 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 1439 if (ret) { 1440 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret); 1441 goto err_v4l2_ctrl_handler_free; 1442 } 1443 1444 dev->v4l2_dev.ctrl_handler = &dev->hdl; 1445 dev->vdev.v4l2_dev = &dev->v4l2_dev; 1446 dev->vdev.lock = &dev->v4l2_lock; 1447 dev->vdev.vfl_dir = VFL_DIR_RX; 1448 1449 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1); 1450 if (ret) { 1451 dev_err(&pdev->dev, "Failed to register as video device %d\n", 1452 ret); 1453 goto err_v4l2_device_unregister; 1454 } 1455 dev_info(&pdev->dev, "Registered as %s\n", 1456 video_device_node_name(&dev->vdev)); 1457 dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n"); 1458 dev_notice(&pdev->dev, 1459 "SDR API is still slightly experimental and functionality changes may follow\n"); 1460 platform_set_drvdata(pdev, dev); 1461 return 0; 1462 err_v4l2_device_unregister: 1463 v4l2_device_unregister(&dev->v4l2_dev); 1464 err_v4l2_ctrl_handler_free: 1465 v4l2_ctrl_handler_free(&dev->hdl); 1466 err_kfree: 1467 kfree(dev); 1468 err_module_put: 1469 module_put(pdev->dev.parent->driver->owner); 1470 err: 1471 return ret; 1472 } 1473 1474 static void rtl2832_sdr_remove(struct platform_device *pdev) 1475 { 1476 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev); 1477 1478 dev_dbg(&pdev->dev, "\n"); 1479 1480 mutex_lock(&dev->vb_queue_lock); 1481 mutex_lock(&dev->v4l2_lock); 1482 /* No need to keep the urbs around after disconnection */ 1483 dev->udev = NULL; 1484 v4l2_device_disconnect(&dev->v4l2_dev); 1485 video_unregister_device(&dev->vdev); 1486 mutex_unlock(&dev->v4l2_lock); 1487 mutex_unlock(&dev->vb_queue_lock); 1488 v4l2_device_put(&dev->v4l2_dev); 1489 module_put(pdev->dev.parent->driver->owner); 1490 } 1491 1492 static struct platform_driver rtl2832_sdr_driver = { 1493 .driver = { 1494 .name = "rtl2832_sdr", 1495 }, 1496 .probe = rtl2832_sdr_probe, 1497 .remove = rtl2832_sdr_remove, 1498 }; 1499 module_platform_driver(rtl2832_sdr_driver); 1500 1501 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1502 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver"); 1503 MODULE_LICENSE("GPL"); 1504