linux/drivers/media/dvb-frontends/rtl2832_sdr.c
<<
>>
Prefs
   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
  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/* 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
  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/* stream formats */
  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/* intermediate buffers with raw data from the USB device */
  95struct 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
 101struct 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 */
 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                /* 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 */
 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:             /* 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                                               "videobuf 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        }
 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                /* stop the URB */
 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                        /* free the URBs */
 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        /* 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                        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/* Must be called with vb_queue_lock hold */
 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/* Videobuf2 operations */
 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        /* Need at least 8 buffers */
 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        /* Don't allow queueing new buffers after device disconnection */
 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        /* Check the device has not disconnected between prep and queuing */
 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        /* get IF from tuner */
 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        /* program IF */
 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        /* BB / IF mode */
 537        /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
 538        if (f_if) {
 539                u8tmp1 = 0x1a; /* disable Zero-IF */
 540                u8tmp2 = 0x8d; /* enable ADC I */
 541        } else {
 542                u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
 543                u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
 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        /* program sampling rate (resampling down) */
 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        /* low-pass filter */
 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        /* mode */
 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        /* FSM */
 591        ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
 592        if (ret)
 593                goto err;
 594
 595        /* PID filter */
 596        ret = regmap_write(dev->regmap, 0x061, 0x60);
 597        if (ret)
 598                goto err;
 599
 600        /* used RF tuner based settings */
 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        /* software reset */
 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        /* PID filter */
 742        ret = regmap_write(dev->regmap, 0x061, 0xe0);
 743        if (ret)
 744                goto err;
 745
 746        /* mode */
 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        /* FSM */
 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         * tuner RF (Hz)
 782         */
 783        if (dev->f_tuner == 0)
 784                return 0;
 785
 786        /*
 787         * bandwidth (Hz)
 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        /* enable ADC */
 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        /* wake-up tuner */
 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        /* sleep tuner */
 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        /* disable ADC */
 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        /* ADC band midpoints */
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        return 0;
1134}
1135
1136static int rtl2832_sdr_s_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        struct vb2_queue *q = &dev->vb_queue;
1142        int i;
1143
1144        dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1145                (char *)&f->fmt.sdr.pixelformat);
1146
1147        if (vb2_is_busy(q))
1148                return -EBUSY;
1149
1150        for (i = 0; i < dev->num_formats; i++) {
1151                if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1152                        dev->pixelformat = formats[i].pixelformat;
1153                        dev->buffersize = formats[i].buffersize;
1154                        f->fmt.sdr.buffersize = formats[i].buffersize;
1155                        return 0;
1156                }
1157        }
1158
1159        dev->pixelformat = formats[0].pixelformat;
1160        dev->buffersize = formats[0].buffersize;
1161        f->fmt.sdr.pixelformat = formats[0].pixelformat;
1162        f->fmt.sdr.buffersize = formats[0].buffersize;
1163
1164        return 0;
1165}
1166
1167static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1168                struct v4l2_format *f)
1169{
1170        struct rtl2832_sdr_dev *dev = video_drvdata(file);
1171        struct platform_device *pdev = dev->pdev;
1172        int i;
1173
1174        dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1175                (char *)&f->fmt.sdr.pixelformat);
1176
1177        for (i = 0; i < dev->num_formats; i++) {
1178                if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1179                        f->fmt.sdr.buffersize = formats[i].buffersize;
1180                        return 0;
1181                }
1182        }
1183
1184        f->fmt.sdr.pixelformat = formats[0].pixelformat;
1185        f->fmt.sdr.buffersize = formats[0].buffersize;
1186
1187        return 0;
1188}
1189
1190static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1191        .vidioc_querycap          = rtl2832_sdr_querycap,
1192
1193        .vidioc_enum_fmt_sdr_cap  = rtl2832_sdr_enum_fmt_sdr_cap,
1194        .vidioc_g_fmt_sdr_cap     = rtl2832_sdr_g_fmt_sdr_cap,
1195        .vidioc_s_fmt_sdr_cap     = rtl2832_sdr_s_fmt_sdr_cap,
1196        .vidioc_try_fmt_sdr_cap   = rtl2832_sdr_try_fmt_sdr_cap,
1197
1198        .vidioc_reqbufs           = vb2_ioctl_reqbufs,
1199        .vidioc_create_bufs       = vb2_ioctl_create_bufs,
1200        .vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
1201        .vidioc_querybuf          = vb2_ioctl_querybuf,
1202        .vidioc_qbuf              = vb2_ioctl_qbuf,
1203        .vidioc_dqbuf             = vb2_ioctl_dqbuf,
1204
1205        .vidioc_streamon          = vb2_ioctl_streamon,
1206        .vidioc_streamoff         = vb2_ioctl_streamoff,
1207
1208        .vidioc_g_tuner           = rtl2832_sdr_g_tuner,
1209        .vidioc_s_tuner           = rtl2832_sdr_s_tuner,
1210
1211        .vidioc_enum_freq_bands   = rtl2832_sdr_enum_freq_bands,
1212        .vidioc_g_frequency       = rtl2832_sdr_g_frequency,
1213        .vidioc_s_frequency       = rtl2832_sdr_s_frequency,
1214
1215        .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
1216        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1217        .vidioc_log_status        = v4l2_ctrl_log_status,
1218};
1219
1220static const struct v4l2_file_operations rtl2832_sdr_fops = {
1221        .owner                    = THIS_MODULE,
1222        .open                     = v4l2_fh_open,
1223        .release                  = vb2_fop_release,
1224        .read                     = vb2_fop_read,
1225        .poll                     = vb2_fop_poll,
1226        .mmap                     = vb2_fop_mmap,
1227        .unlocked_ioctl           = video_ioctl2,
1228};
1229
1230static struct video_device rtl2832_sdr_template = {
1231        .name                     = "Realtek RTL2832 SDR",
1232        .release                  = video_device_release_empty,
1233        .fops                     = &rtl2832_sdr_fops,
1234        .ioctl_ops                = &rtl2832_sdr_ioctl_ops,
1235        .device_caps              = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
1236                                    V4L2_CAP_READWRITE | V4L2_CAP_TUNER,
1237};
1238
1239static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1240{
1241        struct rtl2832_sdr_dev *dev =
1242                        container_of(ctrl->handler, struct rtl2832_sdr_dev,
1243                                        hdl);
1244        struct platform_device *pdev = dev->pdev;
1245        struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1246        struct dvb_frontend *fe = pdata->dvb_frontend;
1247        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1248        int ret;
1249
1250        dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1251                ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1252                ctrl->step);
1253
1254        switch (ctrl->id) {
1255        case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1256        case V4L2_CID_RF_TUNER_BANDWIDTH:
1257                /* TODO: these controls should be moved to tuner drivers */
1258                if (dev->bandwidth_auto->val) {
1259                        /* Round towards the closest legal value */
1260                        s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
1261                        u32 offset;
1262
1263                        val = clamp_t(s32, val, dev->bandwidth->minimum,
1264                                      dev->bandwidth->maximum);
1265                        offset = val - dev->bandwidth->minimum;
1266                        offset = dev->bandwidth->step *
1267                                div_u64(offset, dev->bandwidth->step);
1268                        dev->bandwidth->val = dev->bandwidth->minimum + offset;
1269                }
1270                c->bandwidth_hz = dev->bandwidth->val;
1271
1272                if (!test_bit(POWER_ON, &dev->flags))
1273                        return 0;
1274
1275                if (fe->ops.tuner_ops.set_params)
1276                        ret = fe->ops.tuner_ops.set_params(fe);
1277                else
1278                        ret = 0;
1279                break;
1280        default:
1281                ret = -EINVAL;
1282        }
1283
1284        return ret;
1285}
1286
1287static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1288        .s_ctrl = rtl2832_sdr_s_ctrl,
1289};
1290
1291static void rtl2832_sdr_video_release(struct v4l2_device *v)
1292{
1293        struct rtl2832_sdr_dev *dev =
1294                        container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1295        struct platform_device *pdev = dev->pdev;
1296
1297        dev_dbg(&pdev->dev, "\n");
1298
1299        v4l2_ctrl_handler_free(&dev->hdl);
1300        v4l2_device_unregister(&dev->v4l2_dev);
1301        kfree(dev);
1302}
1303
1304/* Platform driver interface */
1305static int rtl2832_sdr_probe(struct platform_device *pdev)
1306{
1307        struct rtl2832_sdr_dev *dev;
1308        struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1309        const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
1310        struct v4l2_subdev *subdev;
1311        int ret;
1312
1313        dev_dbg(&pdev->dev, "\n");
1314
1315        if (!pdata) {
1316                dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1317                ret = -EINVAL;
1318                goto err;
1319        }
1320        if (!pdev->dev.parent->driver) {
1321                dev_dbg(&pdev->dev, "No parent device\n");
1322                ret = -EINVAL;
1323                goto err;
1324        }
1325        /* try to refcount host drv since we are the consumer */
1326        if (!try_module_get(pdev->dev.parent->driver->owner)) {
1327                dev_err(&pdev->dev, "Refcount fail");
1328                ret = -EINVAL;
1329                goto err;
1330        }
1331        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1332        if (dev == NULL) {
1333                ret = -ENOMEM;
1334                goto err_module_put;
1335        }
1336
1337        /* setup the state */
1338        subdev = pdata->v4l2_subdev;
1339        dev->v4l2_subdev = pdata->v4l2_subdev;
1340        dev->pdev = pdev;
1341        dev->regmap = pdata->regmap;
1342        dev->udev = pdata->dvb_usb_device->udev;
1343        dev->f_adc = bands_adc[0].rangelow;
1344        dev->f_tuner = bands_fm[0].rangelow;
1345        dev->pixelformat = formats[0].pixelformat;
1346        dev->buffersize = formats[0].buffersize;
1347        dev->num_formats = NUM_FORMATS;
1348        if (!rtl2832_sdr_emulated_fmt)
1349                dev->num_formats -= 1;
1350
1351        mutex_init(&dev->v4l2_lock);
1352        mutex_init(&dev->vb_queue_lock);
1353        spin_lock_init(&dev->queued_bufs_lock);
1354        INIT_LIST_HEAD(&dev->queued_bufs);
1355
1356        /* Init videobuf2 queue structure */
1357        dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1358        dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1359        dev->vb_queue.drv_priv = dev;
1360        dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1361        dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1362        dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1363        dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1364        ret = vb2_queue_init(&dev->vb_queue);
1365        if (ret) {
1366                dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1367                goto err_kfree;
1368        }
1369
1370        /* Register controls */
1371        switch (pdata->tuner) {
1372        case RTL2832_SDR_TUNER_E4000:
1373                v4l2_ctrl_handler_init(&dev->hdl, 9);
1374                if (subdev)
1375                        v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1376                                              NULL, true);
1377                break;
1378        case RTL2832_SDR_TUNER_R820T:
1379        case RTL2832_SDR_TUNER_R828D:
1380                v4l2_ctrl_handler_init(&dev->hdl, 2);
1381                dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1382                                                        V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1383                                                        0, 1, 1, 1);
1384                dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1385                                                   V4L2_CID_RF_TUNER_BANDWIDTH,
1386                                                   0, 8000000, 100000, 0);
1387                v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1388                break;
1389        case RTL2832_SDR_TUNER_FC0012:
1390        case RTL2832_SDR_TUNER_FC0013:
1391                v4l2_ctrl_handler_init(&dev->hdl, 2);
1392                dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1393                                                        V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1394                                                        0, 1, 1, 1);
1395                dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1396                                                   V4L2_CID_RF_TUNER_BANDWIDTH,
1397                                                   6000000, 8000000, 1000000,
1398                                                   6000000);
1399                v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1400                break;
1401        case RTL2832_SDR_TUNER_FC2580:
1402                v4l2_ctrl_handler_init(&dev->hdl, 2);
1403                if (subdev)
1404                        v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1405                                              NULL, true);
1406                break;
1407        default:
1408                v4l2_ctrl_handler_init(&dev->hdl, 0);
1409                dev_err(&pdev->dev, "Unsupported tuner\n");
1410                ret = -ENODEV;
1411                goto err_v4l2_ctrl_handler_free;
1412        }
1413        if (dev->hdl.error) {
1414                ret = dev->hdl.error;
1415                dev_err(&pdev->dev, "Could not initialize controls\n");
1416                goto err_v4l2_ctrl_handler_free;
1417        }
1418
1419        /* Init video_device structure */
1420        dev->vdev = rtl2832_sdr_template;
1421        dev->vdev.queue = &dev->vb_queue;
1422        dev->vdev.queue->lock = &dev->vb_queue_lock;
1423        video_set_drvdata(&dev->vdev, dev);
1424
1425        /* Register the v4l2_device structure */
1426        dev->v4l2_dev.release = rtl2832_sdr_video_release;
1427        ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1428        if (ret) {
1429                dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1430                goto err_v4l2_ctrl_handler_free;
1431        }
1432
1433        dev->v4l2_dev.ctrl_handler = &dev->hdl;
1434        dev->vdev.v4l2_dev = &dev->v4l2_dev;
1435        dev->vdev.lock = &dev->v4l2_lock;
1436        dev->vdev.vfl_dir = VFL_DIR_RX;
1437
1438        ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1439        if (ret) {
1440                dev_err(&pdev->dev, "Failed to register as video device %d\n",
1441                        ret);
1442                goto err_v4l2_device_unregister;
1443        }
1444        dev_info(&pdev->dev, "Registered as %s\n",
1445                 video_device_node_name(&dev->vdev));
1446        dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1447        dev_notice(&pdev->dev,
1448                   "SDR API is still slightly experimental and functionality changes may follow\n");
1449        platform_set_drvdata(pdev, dev);
1450        return 0;
1451err_v4l2_device_unregister:
1452        v4l2_device_unregister(&dev->v4l2_dev);
1453err_v4l2_ctrl_handler_free:
1454        v4l2_ctrl_handler_free(&dev->hdl);
1455err_kfree:
1456        kfree(dev);
1457err_module_put:
1458        module_put(pdev->dev.parent->driver->owner);
1459err:
1460        return ret;
1461}
1462
1463static int rtl2832_sdr_remove(struct platform_device *pdev)
1464{
1465        struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1466
1467        dev_dbg(&pdev->dev, "\n");
1468
1469        mutex_lock(&dev->vb_queue_lock);
1470        mutex_lock(&dev->v4l2_lock);
1471        /* No need to keep the urbs around after disconnection */
1472        dev->udev = NULL;
1473        v4l2_device_disconnect(&dev->v4l2_dev);
1474        video_unregister_device(&dev->vdev);
1475        mutex_unlock(&dev->v4l2_lock);
1476        mutex_unlock(&dev->vb_queue_lock);
1477        v4l2_device_put(&dev->v4l2_dev);
1478        module_put(pdev->dev.parent->driver->owner);
1479
1480        return 0;
1481}
1482
1483static struct platform_driver rtl2832_sdr_driver = {
1484        .driver = {
1485                .name   = "rtl2832_sdr",
1486        },
1487        .probe          = rtl2832_sdr_probe,
1488        .remove         = rtl2832_sdr_remove,
1489};
1490module_platform_driver(rtl2832_sdr_driver);
1491
1492MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1493MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1494MODULE_LICENSE("GPL");
1495