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 910 return 0; 911 912 err: 913 rtl2832_sdr_free_urbs(dev); 914 rtl2832_sdr_free_stream_bufs(dev); 915 rtl2832_sdr_cleanup_queued_bufs(dev, VB2_BUF_STATE_QUEUED); 916 mutex_unlock(&dev->v4l2_lock); 917 918 return ret; 919 } 920 921 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq) 922 { 923 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 924 struct platform_device *pdev = dev->pdev; 925 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 926 struct dvb_usb_device *d = pdata->dvb_usb_device; 927 928 dev_dbg(&pdev->dev, "\n"); 929 930 mutex_lock(&dev->v4l2_lock); 931 932 rtl2832_sdr_kill_urbs(dev); 933 rtl2832_sdr_free_urbs(dev); 934 rtl2832_sdr_free_stream_bufs(dev); 935 rtl2832_sdr_cleanup_queued_bufs(dev, VB2_BUF_STATE_ERROR); 936 rtl2832_sdr_unset_adc(dev); 937 938 /* sleep tuner */ 939 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power)) 940 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0); 941 else 942 rtl2832_sdr_unset_tuner(dev); 943 944 clear_bit(POWER_ON, &dev->flags); 945 946 /* disable ADC */ 947 if (d->props->frontend_ctrl) 948 d->props->frontend_ctrl(pdata->dvb_frontend, 0); 949 950 if (d->props->power_ctrl) 951 d->props->power_ctrl(d, 0); 952 953 mutex_unlock(&dev->v4l2_lock); 954 } 955 956 static const struct vb2_ops rtl2832_sdr_vb2_ops = { 957 .queue_setup = rtl2832_sdr_queue_setup, 958 .buf_prepare = rtl2832_sdr_buf_prepare, 959 .buf_queue = rtl2832_sdr_buf_queue, 960 .start_streaming = rtl2832_sdr_start_streaming, 961 .stop_streaming = rtl2832_sdr_stop_streaming, 962 }; 963 964 static int rtl2832_sdr_g_tuner(struct file *file, void *priv, 965 struct v4l2_tuner *v) 966 { 967 struct rtl2832_sdr_dev *dev = video_drvdata(file); 968 struct platform_device *pdev = dev->pdev; 969 int ret; 970 971 dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type); 972 973 if (v->index == 0) { 974 strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name)); 975 v->type = V4L2_TUNER_ADC; 976 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 977 v->rangelow = 300000; 978 v->rangehigh = 3200000; 979 ret = 0; 980 } else if (v->index == 1 && 981 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) { 982 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v); 983 } else if (v->index == 1) { 984 strscpy(v->name, "RF: <unknown>", sizeof(v->name)); 985 v->type = V4L2_TUNER_RF; 986 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 987 v->rangelow = 50000000; 988 v->rangehigh = 2000000000; 989 ret = 0; 990 } else { 991 ret = -EINVAL; 992 } 993 return ret; 994 } 995 996 static int rtl2832_sdr_s_tuner(struct file *file, void *priv, 997 const struct v4l2_tuner *v) 998 { 999 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1000 struct platform_device *pdev = dev->pdev; 1001 int ret; 1002 1003 dev_dbg(&pdev->dev, "\n"); 1004 1005 if (v->index == 0) { 1006 ret = 0; 1007 } else if (v->index == 1 && 1008 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) { 1009 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v); 1010 } else if (v->index == 1) { 1011 ret = 0; 1012 } else { 1013 ret = -EINVAL; 1014 } 1015 return ret; 1016 } 1017 1018 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv, 1019 struct v4l2_frequency_band *band) 1020 { 1021 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1022 struct platform_device *pdev = dev->pdev; 1023 int ret; 1024 1025 dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n", 1026 band->tuner, band->type, band->index); 1027 1028 if (band->tuner == 0) { 1029 if (band->index >= ARRAY_SIZE(bands_adc)) 1030 return -EINVAL; 1031 1032 *band = bands_adc[band->index]; 1033 ret = 0; 1034 } else if (band->tuner == 1 && 1035 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) { 1036 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band); 1037 } else if (band->tuner == 1) { 1038 if (band->index >= ARRAY_SIZE(bands_fm)) 1039 return -EINVAL; 1040 1041 *band = bands_fm[band->index]; 1042 ret = 0; 1043 } else { 1044 ret = -EINVAL; 1045 } 1046 return ret; 1047 } 1048 1049 static int rtl2832_sdr_g_frequency(struct file *file, void *priv, 1050 struct v4l2_frequency *f) 1051 { 1052 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1053 struct platform_device *pdev = dev->pdev; 1054 int ret; 1055 1056 dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type); 1057 1058 if (f->tuner == 0) { 1059 f->frequency = dev->f_adc; 1060 f->type = V4L2_TUNER_ADC; 1061 ret = 0; 1062 } else if (f->tuner == 1 && 1063 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) { 1064 f->type = V4L2_TUNER_RF; 1065 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f); 1066 } else if (f->tuner == 1) { 1067 f->frequency = dev->f_tuner; 1068 f->type = V4L2_TUNER_RF; 1069 ret = 0; 1070 } else { 1071 ret = -EINVAL; 1072 } 1073 return ret; 1074 } 1075 1076 static int rtl2832_sdr_s_frequency(struct file *file, void *priv, 1077 const struct v4l2_frequency *f) 1078 { 1079 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1080 struct platform_device *pdev = dev->pdev; 1081 int ret, band; 1082 1083 dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n", 1084 f->tuner, f->type, f->frequency); 1085 1086 /* ADC band midpoints */ 1087 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2) 1088 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2) 1089 1090 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) { 1091 if (f->frequency < BAND_ADC_0) 1092 band = 0; 1093 else if (f->frequency < BAND_ADC_1) 1094 band = 1; 1095 else 1096 band = 2; 1097 1098 dev->f_adc = clamp_t(unsigned int, f->frequency, 1099 bands_adc[band].rangelow, 1100 bands_adc[band].rangehigh); 1101 1102 dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc); 1103 ret = rtl2832_sdr_set_adc(dev); 1104 } else if (f->tuner == 1 && 1105 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) { 1106 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f); 1107 } else if (f->tuner == 1) { 1108 dev->f_tuner = clamp_t(unsigned int, f->frequency, 1109 bands_fm[0].rangelow, 1110 bands_fm[0].rangehigh); 1111 dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency); 1112 1113 ret = rtl2832_sdr_set_tuner_freq(dev); 1114 } else { 1115 ret = -EINVAL; 1116 } 1117 return ret; 1118 } 1119 1120 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv, 1121 struct v4l2_fmtdesc *f) 1122 { 1123 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1124 struct platform_device *pdev = dev->pdev; 1125 1126 dev_dbg(&pdev->dev, "\n"); 1127 1128 if (f->index >= dev->num_formats) 1129 return -EINVAL; 1130 1131 f->pixelformat = formats[f->index].pixelformat; 1132 1133 return 0; 1134 } 1135 1136 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv, 1137 struct v4l2_format *f) 1138 { 1139 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1140 struct platform_device *pdev = dev->pdev; 1141 1142 dev_dbg(&pdev->dev, "\n"); 1143 1144 f->fmt.sdr.pixelformat = dev->pixelformat; 1145 f->fmt.sdr.buffersize = dev->buffersize; 1146 1147 return 0; 1148 } 1149 1150 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv, 1151 struct v4l2_format *f) 1152 { 1153 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1154 struct platform_device *pdev = dev->pdev; 1155 struct vb2_queue *q = &dev->vb_queue; 1156 int i; 1157 1158 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", 1159 (char *)&f->fmt.sdr.pixelformat); 1160 1161 if (vb2_is_busy(q)) 1162 return -EBUSY; 1163 1164 for (i = 0; i < dev->num_formats; i++) { 1165 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 1166 dev->pixelformat = formats[i].pixelformat; 1167 dev->buffersize = formats[i].buffersize; 1168 f->fmt.sdr.buffersize = formats[i].buffersize; 1169 return 0; 1170 } 1171 } 1172 1173 dev->pixelformat = formats[0].pixelformat; 1174 dev->buffersize = formats[0].buffersize; 1175 f->fmt.sdr.pixelformat = formats[0].pixelformat; 1176 f->fmt.sdr.buffersize = formats[0].buffersize; 1177 1178 return 0; 1179 } 1180 1181 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv, 1182 struct v4l2_format *f) 1183 { 1184 struct rtl2832_sdr_dev *dev = video_drvdata(file); 1185 struct platform_device *pdev = dev->pdev; 1186 int i; 1187 1188 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", 1189 (char *)&f->fmt.sdr.pixelformat); 1190 1191 for (i = 0; i < dev->num_formats; i++) { 1192 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 1193 f->fmt.sdr.buffersize = formats[i].buffersize; 1194 return 0; 1195 } 1196 } 1197 1198 f->fmt.sdr.pixelformat = formats[0].pixelformat; 1199 f->fmt.sdr.buffersize = formats[0].buffersize; 1200 1201 return 0; 1202 } 1203 1204 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = { 1205 .vidioc_querycap = rtl2832_sdr_querycap, 1206 1207 .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap, 1208 .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap, 1209 .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap, 1210 .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap, 1211 1212 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1213 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1214 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1215 .vidioc_querybuf = vb2_ioctl_querybuf, 1216 .vidioc_qbuf = vb2_ioctl_qbuf, 1217 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1218 1219 .vidioc_streamon = vb2_ioctl_streamon, 1220 .vidioc_streamoff = vb2_ioctl_streamoff, 1221 1222 .vidioc_g_tuner = rtl2832_sdr_g_tuner, 1223 .vidioc_s_tuner = rtl2832_sdr_s_tuner, 1224 1225 .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands, 1226 .vidioc_g_frequency = rtl2832_sdr_g_frequency, 1227 .vidioc_s_frequency = rtl2832_sdr_s_frequency, 1228 1229 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1230 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1231 .vidioc_log_status = v4l2_ctrl_log_status, 1232 }; 1233 1234 static const struct v4l2_file_operations rtl2832_sdr_fops = { 1235 .owner = THIS_MODULE, 1236 .open = v4l2_fh_open, 1237 .release = vb2_fop_release, 1238 .read = vb2_fop_read, 1239 .poll = vb2_fop_poll, 1240 .mmap = vb2_fop_mmap, 1241 .unlocked_ioctl = video_ioctl2, 1242 }; 1243 1244 static struct video_device rtl2832_sdr_template = { 1245 .name = "Realtek RTL2832 SDR", 1246 .release = video_device_release_empty, 1247 .fops = &rtl2832_sdr_fops, 1248 .ioctl_ops = &rtl2832_sdr_ioctl_ops, 1249 .device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | 1250 V4L2_CAP_READWRITE | V4L2_CAP_TUNER, 1251 }; 1252 1253 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl) 1254 { 1255 struct rtl2832_sdr_dev *dev = 1256 container_of(ctrl->handler, struct rtl2832_sdr_dev, 1257 hdl); 1258 struct platform_device *pdev = dev->pdev; 1259 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 1260 struct dvb_frontend *fe = pdata->dvb_frontend; 1261 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1262 int ret; 1263 1264 dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n", 1265 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum, 1266 ctrl->step); 1267 1268 switch (ctrl->id) { 1269 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: 1270 case V4L2_CID_RF_TUNER_BANDWIDTH: 1271 /* TODO: these controls should be moved to tuner drivers */ 1272 if (dev->bandwidth_auto->val) { 1273 /* Round towards the closest legal value */ 1274 s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2); 1275 u32 offset; 1276 1277 val = clamp_t(s32, val, dev->bandwidth->minimum, 1278 dev->bandwidth->maximum); 1279 offset = val - dev->bandwidth->minimum; 1280 offset = dev->bandwidth->step * 1281 div_u64(offset, dev->bandwidth->step); 1282 dev->bandwidth->val = dev->bandwidth->minimum + offset; 1283 } 1284 c->bandwidth_hz = dev->bandwidth->val; 1285 1286 if (!test_bit(POWER_ON, &dev->flags)) 1287 return 0; 1288 1289 if (fe->ops.tuner_ops.set_params) 1290 ret = fe->ops.tuner_ops.set_params(fe); 1291 else 1292 ret = 0; 1293 break; 1294 default: 1295 ret = -EINVAL; 1296 } 1297 1298 return ret; 1299 } 1300 1301 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = { 1302 .s_ctrl = rtl2832_sdr_s_ctrl, 1303 }; 1304 1305 static void rtl2832_sdr_video_release(struct v4l2_device *v) 1306 { 1307 struct rtl2832_sdr_dev *dev = 1308 container_of(v, struct rtl2832_sdr_dev, v4l2_dev); 1309 struct platform_device *pdev = dev->pdev; 1310 1311 dev_dbg(&pdev->dev, "\n"); 1312 1313 v4l2_ctrl_handler_free(&dev->hdl); 1314 v4l2_device_unregister(&dev->v4l2_dev); 1315 kfree(dev); 1316 } 1317 1318 /* Platform driver interface */ 1319 static int rtl2832_sdr_probe(struct platform_device *pdev) 1320 { 1321 struct rtl2832_sdr_dev *dev; 1322 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 1323 const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops; 1324 struct v4l2_subdev *subdev; 1325 int ret; 1326 1327 dev_dbg(&pdev->dev, "\n"); 1328 1329 if (!pdata) { 1330 dev_err(&pdev->dev, "Cannot proceed without platform data\n"); 1331 ret = -EINVAL; 1332 goto err; 1333 } 1334 if (!pdev->dev.parent->driver) { 1335 dev_dbg(&pdev->dev, "No parent device\n"); 1336 ret = -EINVAL; 1337 goto err; 1338 } 1339 /* try to refcount host drv since we are the consumer */ 1340 if (!try_module_get(pdev->dev.parent->driver->owner)) { 1341 dev_err(&pdev->dev, "Refcount fail"); 1342 ret = -EINVAL; 1343 goto err; 1344 } 1345 dev = kzalloc_obj(*dev); 1346 if (dev == NULL) { 1347 ret = -ENOMEM; 1348 goto err_module_put; 1349 } 1350 1351 /* setup the state */ 1352 subdev = pdata->v4l2_subdev; 1353 dev->v4l2_subdev = pdata->v4l2_subdev; 1354 dev->pdev = pdev; 1355 dev->regmap = pdata->regmap; 1356 dev->udev = pdata->dvb_usb_device->udev; 1357 dev->f_adc = bands_adc[0].rangelow; 1358 dev->f_tuner = bands_fm[0].rangelow; 1359 dev->pixelformat = formats[0].pixelformat; 1360 dev->buffersize = formats[0].buffersize; 1361 dev->num_formats = NUM_FORMATS; 1362 if (!rtl2832_sdr_emulated_fmt) 1363 dev->num_formats -= 1; 1364 1365 mutex_init(&dev->v4l2_lock); 1366 mutex_init(&dev->vb_queue_lock); 1367 spin_lock_init(&dev->queued_bufs_lock); 1368 INIT_LIST_HEAD(&dev->queued_bufs); 1369 1370 /* Init videobuf2 queue structure */ 1371 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; 1372 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1373 dev->vb_queue.drv_priv = dev; 1374 dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf); 1375 dev->vb_queue.ops = &rtl2832_sdr_vb2_ops; 1376 dev->vb_queue.mem_ops = &vb2_vmalloc_memops; 1377 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1378 dev->vb_queue.lock = &dev->vb_queue_lock; 1379 ret = vb2_queue_init(&dev->vb_queue); 1380 if (ret) { 1381 dev_err(&pdev->dev, "Could not initialize vb2 queue\n"); 1382 goto err_kfree; 1383 } 1384 1385 /* Register controls */ 1386 switch (pdata->tuner) { 1387 case RTL2832_SDR_TUNER_E4000: 1388 v4l2_ctrl_handler_init(&dev->hdl, 9); 1389 if (subdev) 1390 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, 1391 NULL, true); 1392 break; 1393 case RTL2832_SDR_TUNER_R820T: 1394 case RTL2832_SDR_TUNER_R828D: 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 0, 8000000, 100000, 0); 1402 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); 1403 break; 1404 case RTL2832_SDR_TUNER_FC0012: 1405 case RTL2832_SDR_TUNER_FC0013: 1406 v4l2_ctrl_handler_init(&dev->hdl, 2); 1407 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, 1408 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 1409 0, 1, 1, 1); 1410 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, 1411 V4L2_CID_RF_TUNER_BANDWIDTH, 1412 6000000, 8000000, 1000000, 1413 6000000); 1414 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); 1415 break; 1416 case RTL2832_SDR_TUNER_FC2580: 1417 v4l2_ctrl_handler_init(&dev->hdl, 2); 1418 if (subdev) 1419 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, 1420 NULL, true); 1421 break; 1422 default: 1423 v4l2_ctrl_handler_init(&dev->hdl, 0); 1424 dev_err(&pdev->dev, "Unsupported tuner\n"); 1425 ret = -ENODEV; 1426 goto err_v4l2_ctrl_handler_free; 1427 } 1428 if (dev->hdl.error) { 1429 ret = dev->hdl.error; 1430 dev_err(&pdev->dev, "Could not initialize controls\n"); 1431 goto err_v4l2_ctrl_handler_free; 1432 } 1433 1434 /* Init video_device structure */ 1435 dev->vdev = rtl2832_sdr_template; 1436 dev->vdev.queue = &dev->vb_queue; 1437 video_set_drvdata(&dev->vdev, dev); 1438 1439 /* Register the v4l2_device structure */ 1440 dev->v4l2_dev.release = rtl2832_sdr_video_release; 1441 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 1442 if (ret) { 1443 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret); 1444 goto err_v4l2_ctrl_handler_free; 1445 } 1446 1447 dev->v4l2_dev.ctrl_handler = &dev->hdl; 1448 dev->vdev.v4l2_dev = &dev->v4l2_dev; 1449 dev->vdev.lock = &dev->v4l2_lock; 1450 dev->vdev.vfl_dir = VFL_DIR_RX; 1451 1452 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1); 1453 if (ret) { 1454 dev_err(&pdev->dev, "Failed to register as video device %d\n", 1455 ret); 1456 goto err_v4l2_device_unregister; 1457 } 1458 dev_info(&pdev->dev, "Registered as %s\n", 1459 video_device_node_name(&dev->vdev)); 1460 dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n"); 1461 dev_notice(&pdev->dev, 1462 "SDR API is still slightly experimental and functionality changes may follow\n"); 1463 platform_set_drvdata(pdev, dev); 1464 return 0; 1465 err_v4l2_device_unregister: 1466 v4l2_device_unregister(&dev->v4l2_dev); 1467 err_v4l2_ctrl_handler_free: 1468 v4l2_ctrl_handler_free(&dev->hdl); 1469 err_kfree: 1470 kfree(dev); 1471 err_module_put: 1472 module_put(pdev->dev.parent->driver->owner); 1473 err: 1474 return ret; 1475 } 1476 1477 static void rtl2832_sdr_remove(struct platform_device *pdev) 1478 { 1479 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev); 1480 1481 dev_dbg(&pdev->dev, "\n"); 1482 1483 /* 1484 * vb2_video_unregister_device() releases the vb2 queue, which 1485 * triggers rtl2832_sdr_stop_streaming() if streaming is active. 1486 * stop_streaming() uses dev->udev to free URBs and coherent DMA 1487 * stream buffers via usb_free_coherent(), so it must run before 1488 * dev->udev is cleared. vb2_video_unregister_device() locks 1489 * vb_queue_lock internally and stop_streaming() locks v4l2_lock, 1490 * so neither may be held by the caller. 1491 */ 1492 v4l2_device_disconnect(&dev->v4l2_dev); 1493 vb2_video_unregister_device(&dev->vdev); 1494 1495 mutex_lock(&dev->v4l2_lock); 1496 dev->udev = NULL; 1497 mutex_unlock(&dev->v4l2_lock); 1498 1499 v4l2_device_put(&dev->v4l2_dev); 1500 module_put(pdev->dev.parent->driver->owner); 1501 } 1502 1503 static struct platform_driver rtl2832_sdr_driver = { 1504 .driver = { 1505 .name = "rtl2832_sdr", 1506 }, 1507 .probe = rtl2832_sdr_probe, 1508 .remove = rtl2832_sdr_remove, 1509 }; 1510 module_platform_driver(rtl2832_sdr_driver); 1511 1512 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1513 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver"); 1514 MODULE_LICENSE("GPL"); 1515