116216333SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 277bbb2b0SAntti Palosaari /* 377bbb2b0SAntti Palosaari * Realtek RTL2832U SDR driver 477bbb2b0SAntti Palosaari * 577bbb2b0SAntti Palosaari * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> 677bbb2b0SAntti Palosaari * 777bbb2b0SAntti Palosaari * GNU Radio plugin "gr-kernel" for device usage will be on: 8e6e059a8SAlexander A. Klimov * https://git.linuxtv.org/anttip/gr-kernel.git 977bbb2b0SAntti Palosaari */ 1077bbb2b0SAntti Palosaari 1177bbb2b0SAntti Palosaari #include "rtl2832_sdr.h" 1277bbb2b0SAntti Palosaari #include "dvb_usb.h" 1377bbb2b0SAntti Palosaari 1477bbb2b0SAntti Palosaari #include <media/v4l2-device.h> 1577bbb2b0SAntti Palosaari #include <media/v4l2-ioctl.h> 1677bbb2b0SAntti Palosaari #include <media/v4l2-ctrls.h> 1777bbb2b0SAntti Palosaari #include <media/v4l2-event.h> 182d700715SJunghak Sung #include <media/videobuf2-v4l2.h> 1977bbb2b0SAntti Palosaari #include <media/videobuf2-vmalloc.h> 2077bbb2b0SAntti Palosaari 2163bdab5dSAntti Palosaari #include <linux/platform_device.h> 2277bbb2b0SAntti Palosaari #include <linux/jiffies.h> 2377bbb2b0SAntti Palosaari #include <linux/math64.h> 241cf79db2SPeter Rosin #include <linux/regmap.h> 2577bbb2b0SAntti Palosaari 262970c0d5SAntti Palosaari static bool rtl2832_sdr_emulated_fmt; 272970c0d5SAntti Palosaari module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644); 282970c0d5SAntti Palosaari MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)"); 292970c0d5SAntti Palosaari 30fc3a0497SAntti Palosaari /* Original macro does not contain enough null pointer checks for our need */ 31fc3a0497SAntti Palosaari #define V4L2_SUBDEV_HAS_OP(sd, o, f) \ 32fc3a0497SAntti Palosaari ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f) 33fc3a0497SAntti Palosaari 3477bbb2b0SAntti Palosaari #define MAX_BULK_BUFS (10) 3577bbb2b0SAntti Palosaari #define BULK_BUFFER_SIZE (128 * 512) 3677bbb2b0SAntti Palosaari 3777bbb2b0SAntti Palosaari static const struct v4l2_frequency_band bands_adc[] = { 3877bbb2b0SAntti Palosaari { 3977bbb2b0SAntti Palosaari .tuner = 0, 4077bbb2b0SAntti Palosaari .type = V4L2_TUNER_ADC, 4177bbb2b0SAntti Palosaari .index = 0, 4277bbb2b0SAntti Palosaari .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 4377bbb2b0SAntti Palosaari .rangelow = 300000, 4477bbb2b0SAntti Palosaari .rangehigh = 300000, 4577bbb2b0SAntti Palosaari }, 4677bbb2b0SAntti Palosaari { 4777bbb2b0SAntti Palosaari .tuner = 0, 4877bbb2b0SAntti Palosaari .type = V4L2_TUNER_ADC, 4977bbb2b0SAntti Palosaari .index = 1, 5077bbb2b0SAntti Palosaari .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 5177bbb2b0SAntti Palosaari .rangelow = 900001, 5277bbb2b0SAntti Palosaari .rangehigh = 2800000, 5377bbb2b0SAntti Palosaari }, 5477bbb2b0SAntti Palosaari { 5577bbb2b0SAntti Palosaari .tuner = 0, 5677bbb2b0SAntti Palosaari .type = V4L2_TUNER_ADC, 5777bbb2b0SAntti Palosaari .index = 2, 5877bbb2b0SAntti Palosaari .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 5977bbb2b0SAntti Palosaari .rangelow = 3200000, 6077bbb2b0SAntti Palosaari .rangehigh = 3200000, 6177bbb2b0SAntti Palosaari }, 6277bbb2b0SAntti Palosaari }; 6377bbb2b0SAntti Palosaari 6477bbb2b0SAntti Palosaari static const struct v4l2_frequency_band bands_fm[] = { 6577bbb2b0SAntti Palosaari { 6677bbb2b0SAntti Palosaari .tuner = 1, 6777bbb2b0SAntti Palosaari .type = V4L2_TUNER_RF, 6877bbb2b0SAntti Palosaari .index = 0, 6977bbb2b0SAntti Palosaari .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 7077bbb2b0SAntti Palosaari .rangelow = 50000000, 7177bbb2b0SAntti Palosaari .rangehigh = 2000000000, 7277bbb2b0SAntti Palosaari }, 7377bbb2b0SAntti Palosaari }; 7477bbb2b0SAntti Palosaari 7577bbb2b0SAntti Palosaari /* stream formats */ 7677bbb2b0SAntti Palosaari struct rtl2832_sdr_format { 7777bbb2b0SAntti Palosaari char *name; 7877bbb2b0SAntti Palosaari u32 pixelformat; 79d57fe404SAntti Palosaari u32 buffersize; 8077bbb2b0SAntti Palosaari }; 8177bbb2b0SAntti Palosaari 8277bbb2b0SAntti Palosaari static struct rtl2832_sdr_format formats[] = { 8377bbb2b0SAntti Palosaari { 8477bbb2b0SAntti Palosaari .pixelformat = V4L2_SDR_FMT_CU8, 85d57fe404SAntti Palosaari .buffersize = BULK_BUFFER_SIZE, 8677bbb2b0SAntti Palosaari }, { 8777bbb2b0SAntti Palosaari .pixelformat = V4L2_SDR_FMT_CU16LE, 88d57fe404SAntti Palosaari .buffersize = BULK_BUFFER_SIZE * 2, 8977bbb2b0SAntti Palosaari }, 9077bbb2b0SAntti Palosaari }; 9177bbb2b0SAntti Palosaari 9277bbb2b0SAntti Palosaari static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); 9377bbb2b0SAntti Palosaari 9477bbb2b0SAntti Palosaari /* intermediate buffers with raw data from the USB device */ 9577bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf { 962d700715SJunghak Sung /* common v4l buffer stuff -- must be first */ 972d700715SJunghak Sung struct vb2_v4l2_buffer vb; 9877bbb2b0SAntti Palosaari struct list_head list; 9977bbb2b0SAntti Palosaari }; 10077bbb2b0SAntti Palosaari 101725b7f30SAntti Palosaari struct rtl2832_sdr_dev { 102e4d131d3SDan Carpenter #define POWER_ON 0 /* BIT(0) */ 103e4d131d3SDan Carpenter #define URB_BUF 1 /* BIT(1) */ 10477bbb2b0SAntti Palosaari unsigned long flags; 10577bbb2b0SAntti Palosaari 10663bdab5dSAntti Palosaari struct platform_device *pdev; 107e01fc42dSPeter Rosin struct regmap *regmap; 10877bbb2b0SAntti Palosaari 10977bbb2b0SAntti Palosaari struct video_device vdev; 11077bbb2b0SAntti Palosaari struct v4l2_device v4l2_dev; 111fc3a0497SAntti Palosaari struct v4l2_subdev *v4l2_subdev; 11277bbb2b0SAntti Palosaari 11377bbb2b0SAntti Palosaari /* videobuf2 queue and queued buffers list */ 11477bbb2b0SAntti Palosaari struct vb2_queue vb_queue; 11577bbb2b0SAntti Palosaari struct list_head queued_bufs; 11677bbb2b0SAntti Palosaari spinlock_t queued_bufs_lock; /* Protects queued_bufs */ 11777bbb2b0SAntti Palosaari unsigned sequence; /* buffer sequence counter */ 11877bbb2b0SAntti Palosaari 11977bbb2b0SAntti Palosaari /* Note if taking both locks v4l2_lock must always be locked first! */ 12077bbb2b0SAntti Palosaari struct mutex v4l2_lock; /* Protects everything else */ 12177bbb2b0SAntti Palosaari struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ 12277bbb2b0SAntti Palosaari 12377bbb2b0SAntti Palosaari /* Pointer to our usb_device, will be NULL after unplug */ 12477bbb2b0SAntti Palosaari struct usb_device *udev; /* Both mutexes most be hold when setting! */ 12577bbb2b0SAntti Palosaari 12677bbb2b0SAntti Palosaari unsigned int vb_full; /* vb is full and packets dropped */ 12777bbb2b0SAntti Palosaari 12877bbb2b0SAntti Palosaari struct urb *urb_list[MAX_BULK_BUFS]; 12977bbb2b0SAntti Palosaari int buf_num; 13077bbb2b0SAntti Palosaari unsigned long buf_size; 13177bbb2b0SAntti Palosaari u8 *buf_list[MAX_BULK_BUFS]; 13277bbb2b0SAntti Palosaari dma_addr_t dma_addr[MAX_BULK_BUFS]; 13377bbb2b0SAntti Palosaari int urbs_initialized; 13477bbb2b0SAntti Palosaari int urbs_submitted; 13577bbb2b0SAntti Palosaari 13677bbb2b0SAntti Palosaari unsigned int f_adc, f_tuner; 13777bbb2b0SAntti Palosaari u32 pixelformat; 138d57fe404SAntti Palosaari u32 buffersize; 1392970c0d5SAntti Palosaari unsigned int num_formats; 14077bbb2b0SAntti Palosaari 14177bbb2b0SAntti Palosaari /* Controls */ 14277bbb2b0SAntti Palosaari struct v4l2_ctrl_handler hdl; 14377bbb2b0SAntti Palosaari struct v4l2_ctrl *bandwidth_auto; 14477bbb2b0SAntti Palosaari struct v4l2_ctrl *bandwidth; 14577bbb2b0SAntti Palosaari 14677bbb2b0SAntti Palosaari /* for sample rate calc */ 14777bbb2b0SAntti Palosaari unsigned int sample; 14877bbb2b0SAntti Palosaari unsigned int sample_measured; 14977bbb2b0SAntti Palosaari unsigned long jiffies_next; 15077bbb2b0SAntti Palosaari }; 15177bbb2b0SAntti Palosaari 15277bbb2b0SAntti Palosaari /* Private functions */ 15377bbb2b0SAntti Palosaari static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf( 154725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev) 15577bbb2b0SAntti Palosaari { 156abfc8d66SAntti Palosaari unsigned long flags; 15777bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf *buf = NULL; 15877bbb2b0SAntti Palosaari 159725b7f30SAntti Palosaari spin_lock_irqsave(&dev->queued_bufs_lock, flags); 160725b7f30SAntti Palosaari if (list_empty(&dev->queued_bufs)) 16177bbb2b0SAntti Palosaari goto leave; 16277bbb2b0SAntti Palosaari 163725b7f30SAntti Palosaari buf = list_entry(dev->queued_bufs.next, 16477bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf, list); 16577bbb2b0SAntti Palosaari list_del(&buf->list); 16677bbb2b0SAntti Palosaari leave: 167725b7f30SAntti Palosaari spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 16877bbb2b0SAntti Palosaari return buf; 16977bbb2b0SAntti Palosaari } 17077bbb2b0SAntti Palosaari 171725b7f30SAntti Palosaari static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev, 17277bbb2b0SAntti Palosaari void *dst, const u8 *src, unsigned int src_len) 17377bbb2b0SAntti Palosaari { 174e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 17577bbb2b0SAntti Palosaari unsigned int dst_len; 17677bbb2b0SAntti Palosaari 177725b7f30SAntti Palosaari if (dev->pixelformat == V4L2_SDR_FMT_CU8) { 17877bbb2b0SAntti Palosaari /* native stream, no need to convert */ 17977bbb2b0SAntti Palosaari memcpy(dst, src, src_len); 18077bbb2b0SAntti Palosaari dst_len = src_len; 181725b7f30SAntti Palosaari } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) { 18277bbb2b0SAntti Palosaari /* convert u8 to u16 */ 18377bbb2b0SAntti Palosaari unsigned int i; 18477bbb2b0SAntti Palosaari u16 *u16dst = dst; 18577bbb2b0SAntti Palosaari 18677bbb2b0SAntti Palosaari for (i = 0; i < src_len; i++) 18777bbb2b0SAntti Palosaari *u16dst++ = (src[i] << 8) | (src[i] >> 0); 18877bbb2b0SAntti Palosaari dst_len = 2 * src_len; 18977bbb2b0SAntti Palosaari } else { 19077bbb2b0SAntti Palosaari dst_len = 0; 19177bbb2b0SAntti Palosaari } 19277bbb2b0SAntti Palosaari 193b538a8e8SAntti Palosaari /* calculate sample rate and output it in 10 seconds intervals */ 194725b7f30SAntti Palosaari if (unlikely(time_is_before_jiffies(dev->jiffies_next))) { 19577bbb2b0SAntti Palosaari #define MSECS 10000UL 196b538a8e8SAntti Palosaari unsigned int msecs = jiffies_to_msecs(jiffies - 197725b7f30SAntti Palosaari dev->jiffies_next + msecs_to_jiffies(MSECS)); 198725b7f30SAntti Palosaari unsigned int samples = dev->sample - dev->sample_measured; 19977bbb2b0SAntti Palosaari 200725b7f30SAntti Palosaari dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS); 201725b7f30SAntti Palosaari dev->sample_measured = dev->sample; 202e4e78693SAntti Palosaari dev_dbg(&pdev->dev, 203b538a8e8SAntti Palosaari "slen=%u samples=%u msecs=%u sample rate=%lu\n", 204e4e78693SAntti Palosaari src_len, samples, msecs, samples * 1000UL / msecs); 20577bbb2b0SAntti Palosaari } 20677bbb2b0SAntti Palosaari 20777bbb2b0SAntti Palosaari /* total number of I+Q pairs */ 208725b7f30SAntti Palosaari dev->sample += src_len / 2; 20977bbb2b0SAntti Palosaari 21077bbb2b0SAntti Palosaari return dst_len; 21177bbb2b0SAntti Palosaari } 21277bbb2b0SAntti Palosaari 21377bbb2b0SAntti Palosaari /* 21477bbb2b0SAntti Palosaari * This gets called for the bulk stream pipe. This is done in interrupt 21577bbb2b0SAntti Palosaari * time, so it has to be fast, not crash, and not stall. Neat. 21677bbb2b0SAntti Palosaari */ 21777bbb2b0SAntti Palosaari static void rtl2832_sdr_urb_complete(struct urb *urb) 21877bbb2b0SAntti Palosaari { 219725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = urb->context; 220e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 22177bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf *fbuf; 22277bbb2b0SAntti Palosaari 223e4e78693SAntti Palosaari dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n", 224d4d20500SAntti Palosaari urb->status, urb->actual_length, 22577bbb2b0SAntti Palosaari urb->transfer_buffer_length, urb->error_count); 22677bbb2b0SAntti Palosaari 22777bbb2b0SAntti Palosaari switch (urb->status) { 22877bbb2b0SAntti Palosaari case 0: /* success */ 22977bbb2b0SAntti Palosaari case -ETIMEDOUT: /* NAK */ 23077bbb2b0SAntti Palosaari break; 23177bbb2b0SAntti Palosaari case -ECONNRESET: /* kill */ 23277bbb2b0SAntti Palosaari case -ENOENT: 23377bbb2b0SAntti Palosaari case -ESHUTDOWN: 23477bbb2b0SAntti Palosaari return; 23577bbb2b0SAntti Palosaari default: /* error */ 236e4e78693SAntti Palosaari dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status); 23777bbb2b0SAntti Palosaari break; 23877bbb2b0SAntti Palosaari } 23977bbb2b0SAntti Palosaari 24077bbb2b0SAntti Palosaari if (likely(urb->actual_length > 0)) { 24177bbb2b0SAntti Palosaari void *ptr; 24277bbb2b0SAntti Palosaari unsigned int len; 24377bbb2b0SAntti Palosaari /* get free framebuffer */ 244725b7f30SAntti Palosaari fbuf = rtl2832_sdr_get_next_fill_buf(dev); 24577bbb2b0SAntti Palosaari if (unlikely(fbuf == NULL)) { 246725b7f30SAntti Palosaari dev->vb_full++; 247e4e78693SAntti Palosaari dev_notice_ratelimited(&pdev->dev, 248f068a6ceSHans Verkuil "video buffer is full, %d packets dropped\n", 249725b7f30SAntti Palosaari dev->vb_full); 25077bbb2b0SAntti Palosaari goto skip; 25177bbb2b0SAntti Palosaari } 25277bbb2b0SAntti Palosaari 25377bbb2b0SAntti Palosaari /* fill framebuffer */ 2542d700715SJunghak Sung ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0); 255725b7f30SAntti Palosaari len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer, 25677bbb2b0SAntti Palosaari urb->actual_length); 2572d700715SJunghak Sung vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len); 258d6dd645eSJunghak Sung fbuf->vb.vb2_buf.timestamp = ktime_get_ns(); 2592d700715SJunghak Sung fbuf->vb.sequence = dev->sequence++; 2602d700715SJunghak Sung vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE); 26177bbb2b0SAntti Palosaari } 26277bbb2b0SAntti Palosaari skip: 26377bbb2b0SAntti Palosaari usb_submit_urb(urb, GFP_ATOMIC); 26477bbb2b0SAntti Palosaari } 26577bbb2b0SAntti Palosaari 266725b7f30SAntti Palosaari static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev) 26777bbb2b0SAntti Palosaari { 268e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 26977bbb2b0SAntti Palosaari int i; 27077bbb2b0SAntti Palosaari 271725b7f30SAntti Palosaari for (i = dev->urbs_submitted - 1; i >= 0; i--) { 272e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "kill urb=%d\n", i); 27377bbb2b0SAntti Palosaari /* stop the URB */ 274725b7f30SAntti Palosaari usb_kill_urb(dev->urb_list[i]); 27577bbb2b0SAntti Palosaari } 276725b7f30SAntti Palosaari dev->urbs_submitted = 0; 27777bbb2b0SAntti Palosaari 27877bbb2b0SAntti Palosaari return 0; 27977bbb2b0SAntti Palosaari } 28077bbb2b0SAntti Palosaari 281725b7f30SAntti Palosaari static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev) 28277bbb2b0SAntti Palosaari { 283e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 28477bbb2b0SAntti Palosaari int i, ret; 28577bbb2b0SAntti Palosaari 286725b7f30SAntti Palosaari for (i = 0; i < dev->urbs_initialized; i++) { 287e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "submit urb=%d\n", i); 288730129e9SJia-Ju Bai ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL); 28977bbb2b0SAntti Palosaari if (ret) { 290e4e78693SAntti Palosaari dev_err(&pdev->dev, 29177bbb2b0SAntti Palosaari "Could not submit urb no. %d - get them all back\n", 29277bbb2b0SAntti Palosaari i); 293725b7f30SAntti Palosaari rtl2832_sdr_kill_urbs(dev); 29477bbb2b0SAntti Palosaari return ret; 29577bbb2b0SAntti Palosaari } 296725b7f30SAntti Palosaari dev->urbs_submitted++; 29777bbb2b0SAntti Palosaari } 29877bbb2b0SAntti Palosaari 29977bbb2b0SAntti Palosaari return 0; 30077bbb2b0SAntti Palosaari } 30177bbb2b0SAntti Palosaari 302725b7f30SAntti Palosaari static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev) 30377bbb2b0SAntti Palosaari { 304e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 3053bda2bf1SAntti Palosaari 306e4d131d3SDan Carpenter if (test_bit(URB_BUF, &dev->flags)) { 307725b7f30SAntti Palosaari while (dev->buf_num) { 308725b7f30SAntti Palosaari dev->buf_num--; 309e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num); 310725b7f30SAntti Palosaari usb_free_coherent(dev->udev, dev->buf_size, 311725b7f30SAntti Palosaari dev->buf_list[dev->buf_num], 312725b7f30SAntti Palosaari dev->dma_addr[dev->buf_num]); 31377bbb2b0SAntti Palosaari } 31477bbb2b0SAntti Palosaari } 315e4d131d3SDan Carpenter clear_bit(URB_BUF, &dev->flags); 31677bbb2b0SAntti Palosaari 31777bbb2b0SAntti Palosaari return 0; 31877bbb2b0SAntti Palosaari } 31977bbb2b0SAntti Palosaari 320725b7f30SAntti Palosaari static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev) 32177bbb2b0SAntti Palosaari { 322e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 3233bda2bf1SAntti Palosaari 324725b7f30SAntti Palosaari dev->buf_num = 0; 325725b7f30SAntti Palosaari dev->buf_size = BULK_BUFFER_SIZE; 32677bbb2b0SAntti Palosaari 327e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n", 328d4d20500SAntti Palosaari MAX_BULK_BUFS * BULK_BUFFER_SIZE); 32977bbb2b0SAntti Palosaari 330725b7f30SAntti Palosaari for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) { 331725b7f30SAntti Palosaari dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev, 332730129e9SJia-Ju Bai BULK_BUFFER_SIZE, GFP_KERNEL, 333725b7f30SAntti Palosaari &dev->dma_addr[dev->buf_num]); 334725b7f30SAntti Palosaari if (!dev->buf_list[dev->buf_num]) { 335e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "alloc buf=%d failed\n", 336725b7f30SAntti Palosaari dev->buf_num); 337725b7f30SAntti Palosaari rtl2832_sdr_free_stream_bufs(dev); 33877bbb2b0SAntti Palosaari return -ENOMEM; 33977bbb2b0SAntti Palosaari } 34077bbb2b0SAntti Palosaari 341e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n", 342725b7f30SAntti Palosaari dev->buf_num, dev->buf_list[dev->buf_num], 343725b7f30SAntti Palosaari (long long)dev->dma_addr[dev->buf_num]); 344e4d131d3SDan Carpenter set_bit(URB_BUF, &dev->flags); 34577bbb2b0SAntti Palosaari } 34677bbb2b0SAntti Palosaari 34777bbb2b0SAntti Palosaari return 0; 34877bbb2b0SAntti Palosaari } 34977bbb2b0SAntti Palosaari 350725b7f30SAntti Palosaari static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev) 35177bbb2b0SAntti Palosaari { 352e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 35377bbb2b0SAntti Palosaari int i; 35477bbb2b0SAntti Palosaari 355725b7f30SAntti Palosaari rtl2832_sdr_kill_urbs(dev); 35677bbb2b0SAntti Palosaari 357725b7f30SAntti Palosaari for (i = dev->urbs_initialized - 1; i >= 0; i--) { 358725b7f30SAntti Palosaari if (dev->urb_list[i]) { 359e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "free urb=%d\n", i); 36077bbb2b0SAntti Palosaari /* free the URBs */ 361725b7f30SAntti Palosaari usb_free_urb(dev->urb_list[i]); 36277bbb2b0SAntti Palosaari } 36377bbb2b0SAntti Palosaari } 364725b7f30SAntti Palosaari dev->urbs_initialized = 0; 36577bbb2b0SAntti Palosaari 36677bbb2b0SAntti Palosaari return 0; 36777bbb2b0SAntti Palosaari } 36877bbb2b0SAntti Palosaari 369725b7f30SAntti Palosaari static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev) 37077bbb2b0SAntti Palosaari { 371e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 37277bbb2b0SAntti Palosaari int i, j; 37377bbb2b0SAntti Palosaari 37477bbb2b0SAntti Palosaari /* allocate the URBs */ 37577bbb2b0SAntti Palosaari for (i = 0; i < MAX_BULK_BUFS; i++) { 376e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "alloc urb=%d\n", i); 377730129e9SJia-Ju Bai dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL); 378725b7f30SAntti Palosaari if (!dev->urb_list[i]) { 379cefdc951SNil Yi for (j = 0; j < i; j++) { 380725b7f30SAntti Palosaari usb_free_urb(dev->urb_list[j]); 381cefdc951SNil Yi dev->urb_list[j] = NULL; 382cefdc951SNil Yi } 383cefdc951SNil Yi dev->urbs_initialized = 0; 38477bbb2b0SAntti Palosaari return -ENOMEM; 38577bbb2b0SAntti Palosaari } 386725b7f30SAntti Palosaari usb_fill_bulk_urb(dev->urb_list[i], 387725b7f30SAntti Palosaari dev->udev, 388725b7f30SAntti Palosaari usb_rcvbulkpipe(dev->udev, 0x81), 389725b7f30SAntti Palosaari dev->buf_list[i], 39077bbb2b0SAntti Palosaari BULK_BUFFER_SIZE, 391725b7f30SAntti Palosaari rtl2832_sdr_urb_complete, dev); 39277bbb2b0SAntti Palosaari 393725b7f30SAntti Palosaari dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 394725b7f30SAntti Palosaari dev->urb_list[i]->transfer_dma = dev->dma_addr[i]; 395725b7f30SAntti Palosaari dev->urbs_initialized++; 39677bbb2b0SAntti Palosaari } 39777bbb2b0SAntti Palosaari 39877bbb2b0SAntti Palosaari return 0; 39977bbb2b0SAntti Palosaari } 40077bbb2b0SAntti Palosaari 40177bbb2b0SAntti Palosaari /* Must be called with vb_queue_lock hold */ 402725b7f30SAntti Palosaari static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev) 40377bbb2b0SAntti Palosaari { 404e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 405abfc8d66SAntti Palosaari unsigned long flags; 40677bbb2b0SAntti Palosaari 407e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 40877bbb2b0SAntti Palosaari 409725b7f30SAntti Palosaari spin_lock_irqsave(&dev->queued_bufs_lock, flags); 410725b7f30SAntti Palosaari while (!list_empty(&dev->queued_bufs)) { 41177bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf *buf; 41277bbb2b0SAntti Palosaari 413725b7f30SAntti Palosaari buf = list_entry(dev->queued_bufs.next, 41477bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf, list); 41577bbb2b0SAntti Palosaari list_del(&buf->list); 4162d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 41777bbb2b0SAntti Palosaari } 418725b7f30SAntti Palosaari spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 41977bbb2b0SAntti Palosaari } 42077bbb2b0SAntti Palosaari 42177bbb2b0SAntti Palosaari static int rtl2832_sdr_querycap(struct file *file, void *fh, 42277bbb2b0SAntti Palosaari struct v4l2_capability *cap) 42377bbb2b0SAntti Palosaari { 424725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 425e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 42677bbb2b0SAntti Palosaari 427e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 42877bbb2b0SAntti Palosaari 429c0decac1SMauro Carvalho Chehab strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 430c0decac1SMauro Carvalho Chehab strscpy(cap->card, dev->vdev.name, sizeof(cap->card)); 431725b7f30SAntti Palosaari usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 43277bbb2b0SAntti Palosaari return 0; 43377bbb2b0SAntti Palosaari } 43477bbb2b0SAntti Palosaari 43577bbb2b0SAntti Palosaari /* Videobuf2 operations */ 43677bbb2b0SAntti Palosaari static int rtl2832_sdr_queue_setup(struct vb2_queue *vq, 437df9ecb0cSHans Verkuil unsigned int *nbuffers, 43836c0f8b3SHans Verkuil unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) 43977bbb2b0SAntti Palosaari { 440725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 441e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 44277bbb2b0SAntti Palosaari 443e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers); 44477bbb2b0SAntti Palosaari 44577bbb2b0SAntti Palosaari /* Need at least 8 buffers */ 44677bbb2b0SAntti Palosaari if (vq->num_buffers + *nbuffers < 8) 44777bbb2b0SAntti Palosaari *nbuffers = 8 - vq->num_buffers; 44877bbb2b0SAntti Palosaari *nplanes = 1; 449725b7f30SAntti Palosaari sizes[0] = PAGE_ALIGN(dev->buffersize); 450e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); 45177bbb2b0SAntti Palosaari return 0; 45277bbb2b0SAntti Palosaari } 45377bbb2b0SAntti Palosaari 45477bbb2b0SAntti Palosaari static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb) 45577bbb2b0SAntti Palosaari { 456725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 45777bbb2b0SAntti Palosaari 458868c9a17SMauro Carvalho Chehab /* Don't allow queueing new buffers after device disconnection */ 459725b7f30SAntti Palosaari if (!dev->udev) 46077bbb2b0SAntti Palosaari return -ENODEV; 46177bbb2b0SAntti Palosaari 46277bbb2b0SAntti Palosaari return 0; 46377bbb2b0SAntti Palosaari } 46477bbb2b0SAntti Palosaari 46577bbb2b0SAntti Palosaari static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb) 46677bbb2b0SAntti Palosaari { 4672d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 468725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 46977bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf *buf = 4702d700715SJunghak Sung container_of(vbuf, struct rtl2832_sdr_frame_buf, vb); 471abfc8d66SAntti Palosaari unsigned long flags; 47277bbb2b0SAntti Palosaari 47377bbb2b0SAntti Palosaari /* Check the device has not disconnected between prep and queuing */ 474725b7f30SAntti Palosaari if (!dev->udev) { 4752d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 47677bbb2b0SAntti Palosaari return; 47777bbb2b0SAntti Palosaari } 47877bbb2b0SAntti Palosaari 479725b7f30SAntti Palosaari spin_lock_irqsave(&dev->queued_bufs_lock, flags); 480725b7f30SAntti Palosaari list_add_tail(&buf->list, &dev->queued_bufs); 481725b7f30SAntti Palosaari spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); 48277bbb2b0SAntti Palosaari } 48377bbb2b0SAntti Palosaari 484725b7f30SAntti Palosaari static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev) 48577bbb2b0SAntti Palosaari { 48663bdab5dSAntti Palosaari struct platform_device *pdev = dev->pdev; 48763bdab5dSAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 4883bda2bf1SAntti Palosaari struct dvb_frontend *fe = pdata->dvb_frontend; 48977bbb2b0SAntti Palosaari int ret; 49077bbb2b0SAntti Palosaari unsigned int f_sr, f_if; 49177bbb2b0SAntti Palosaari u8 buf[4], u8tmp1, u8tmp2; 49277bbb2b0SAntti Palosaari u64 u64tmp; 49377bbb2b0SAntti Palosaari u32 u32tmp; 49477bbb2b0SAntti Palosaari 495e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc); 49677bbb2b0SAntti Palosaari 497725b7f30SAntti Palosaari if (!test_bit(POWER_ON, &dev->flags)) 49877bbb2b0SAntti Palosaari return 0; 49977bbb2b0SAntti Palosaari 500725b7f30SAntti Palosaari if (dev->f_adc == 0) 50177bbb2b0SAntti Palosaari return 0; 50277bbb2b0SAntti Palosaari 503725b7f30SAntti Palosaari f_sr = dev->f_adc; 50477bbb2b0SAntti Palosaari 505e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2); 50677bbb2b0SAntti Palosaari if (ret) 50777bbb2b0SAntti Palosaari goto err; 50877bbb2b0SAntti Palosaari 509e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4); 51077bbb2b0SAntti Palosaari if (ret) 51177bbb2b0SAntti Palosaari goto err; 51277bbb2b0SAntti Palosaari 51377bbb2b0SAntti Palosaari /* get IF from tuner */ 51477bbb2b0SAntti Palosaari if (fe->ops.tuner_ops.get_if_frequency) 51577bbb2b0SAntti Palosaari ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if); 51677bbb2b0SAntti Palosaari else 51777bbb2b0SAntti Palosaari ret = -EINVAL; 51877bbb2b0SAntti Palosaari 51977bbb2b0SAntti Palosaari if (ret) 52077bbb2b0SAntti Palosaari goto err; 52177bbb2b0SAntti Palosaari 52277bbb2b0SAntti Palosaari /* program IF */ 52363bdab5dSAntti Palosaari u64tmp = f_if % pdata->clk; 52477bbb2b0SAntti Palosaari u64tmp *= 0x400000; 52563bdab5dSAntti Palosaari u64tmp = div_u64(u64tmp, pdata->clk); 52677bbb2b0SAntti Palosaari u64tmp = -u64tmp; 52777bbb2b0SAntti Palosaari u32tmp = u64tmp & 0x3fffff; 52877bbb2b0SAntti Palosaari 529e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp); 53077bbb2b0SAntti Palosaari 53177bbb2b0SAntti Palosaari buf[0] = (u32tmp >> 16) & 0xff; 53277bbb2b0SAntti Palosaari buf[1] = (u32tmp >> 8) & 0xff; 53377bbb2b0SAntti Palosaari buf[2] = (u32tmp >> 0) & 0xff; 53477bbb2b0SAntti Palosaari 535e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3); 53677bbb2b0SAntti Palosaari if (ret) 53777bbb2b0SAntti Palosaari goto err; 53877bbb2b0SAntti Palosaari 53977bbb2b0SAntti Palosaari /* BB / IF mode */ 54077bbb2b0SAntti Palosaari /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */ 54177bbb2b0SAntti Palosaari if (f_if) { 54277bbb2b0SAntti Palosaari u8tmp1 = 0x1a; /* disable Zero-IF */ 54377bbb2b0SAntti Palosaari u8tmp2 = 0x8d; /* enable ADC I */ 54477bbb2b0SAntti Palosaari } else { 54577bbb2b0SAntti Palosaari u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */ 54677bbb2b0SAntti Palosaari u8tmp2 = 0xcd; /* enable ADC I, ADC Q */ 54777bbb2b0SAntti Palosaari } 54877bbb2b0SAntti Palosaari 549e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1b1, u8tmp1); 55077bbb2b0SAntti Palosaari if (ret) 55177bbb2b0SAntti Palosaari goto err; 55277bbb2b0SAntti Palosaari 553e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x008, u8tmp2); 55477bbb2b0SAntti Palosaari if (ret) 55577bbb2b0SAntti Palosaari goto err; 55677bbb2b0SAntti Palosaari 557e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x006, 0x80); 55877bbb2b0SAntti Palosaari if (ret) 55977bbb2b0SAntti Palosaari goto err; 56077bbb2b0SAntti Palosaari 56177bbb2b0SAntti Palosaari /* program sampling rate (resampling down) */ 56263bdab5dSAntti Palosaari u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U); 56377bbb2b0SAntti Palosaari u32tmp <<= 2; 56477bbb2b0SAntti Palosaari buf[0] = (u32tmp >> 24) & 0xff; 56577bbb2b0SAntti Palosaari buf[1] = (u32tmp >> 16) & 0xff; 56677bbb2b0SAntti Palosaari buf[2] = (u32tmp >> 8) & 0xff; 56777bbb2b0SAntti Palosaari buf[3] = (u32tmp >> 0) & 0xff; 568e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4); 56977bbb2b0SAntti Palosaari if (ret) 57077bbb2b0SAntti Palosaari goto err; 57177bbb2b0SAntti Palosaari 57277bbb2b0SAntti Palosaari /* low-pass filter */ 573e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x11c, 57477bbb2b0SAntti Palosaari "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5", 57577bbb2b0SAntti Palosaari 20); 57677bbb2b0SAntti Palosaari if (ret) 57777bbb2b0SAntti Palosaari goto err; 57877bbb2b0SAntti Palosaari 579e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2); 58077bbb2b0SAntti Palosaari if (ret) 58177bbb2b0SAntti Palosaari goto err; 58277bbb2b0SAntti Palosaari 58377bbb2b0SAntti Palosaari /* mode */ 584e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x019, 0x05); 58577bbb2b0SAntti Palosaari if (ret) 58677bbb2b0SAntti Palosaari goto err; 58777bbb2b0SAntti Palosaari 588e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x01a, 589e01fc42dSPeter Rosin "\x1b\x16\x0d\x06\x01\xff", 6); 59077bbb2b0SAntti Palosaari if (ret) 59177bbb2b0SAntti Palosaari goto err; 59277bbb2b0SAntti Palosaari 59377bbb2b0SAntti Palosaari /* FSM */ 594e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3); 59577bbb2b0SAntti Palosaari if (ret) 59677bbb2b0SAntti Palosaari goto err; 59777bbb2b0SAntti Palosaari 59877bbb2b0SAntti Palosaari /* PID filter */ 599e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x061, 0x60); 60077bbb2b0SAntti Palosaari if (ret) 60177bbb2b0SAntti Palosaari goto err; 60277bbb2b0SAntti Palosaari 60377bbb2b0SAntti Palosaari /* used RF tuner based settings */ 60463bdab5dSAntti Palosaari switch (pdata->tuner) { 60563bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_E4000: 606e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x112, 0x5a); 607e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x102, 0x40); 608e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x103, 0x5a); 609e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c7, 0x30); 610e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x104, 0xd0); 611e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x105, 0xbe); 612e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c8, 0x18); 613e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x106, 0x35); 614e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c9, 0x21); 615e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ca, 0x21); 616e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cb, 0x00); 617e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x107, 0x40); 618e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cd, 0x10); 619e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ce, 0x10); 620e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x108, 0x80); 621e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x109, 0x7f); 622e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10a, 0x80); 623e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10b, 0x7f); 624e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 625e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 626e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x011, 0xd4); 627e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1e5, 0xf0); 628e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1d9, 0x00); 629e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1db, 0x00); 630e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1dd, 0x14); 631e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1de, 0xec); 632e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1d8, 0x0c); 633e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1e6, 0x02); 634e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1d7, 0x09); 635e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00d, 0x83); 636e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x010, 0x49); 637e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00d, 0x87); 638e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00d, 0x85); 639e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x013, 0x02); 64077bbb2b0SAntti Palosaari break; 64163bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_FC0012: 64263bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_FC0013: 643e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x112, 0x5a); 644e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x102, 0x40); 645e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x103, 0x5a); 646e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c7, 0x2c); 647e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x104, 0xcc); 648e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x105, 0xbe); 649e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c8, 0x16); 650e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x106, 0x35); 651e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c9, 0x21); 652e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ca, 0x21); 653e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cb, 0x00); 654e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x107, 0x40); 655e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cd, 0x10); 656e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ce, 0x10); 657e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x108, 0x80); 658e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x109, 0x7f); 659e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10a, 0x80); 660e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10b, 0x7f); 661e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 662e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 663e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2); 664e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1e5, 0xf0); 665e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1d9, 0x00); 666e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1db, 0x00); 667e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1dd, 0x11); 668e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1de, 0xef); 669e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1d8, 0x0c); 670e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1e6, 0x02); 671e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1d7, 0x09); 67277bbb2b0SAntti Palosaari break; 67363bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_R820T: 67463bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_R828D: 675e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x112, 0x5a); 676e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x102, 0x40); 677e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x115, 0x01); 678e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x103, 0x80); 679e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c7, 0x24); 680e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x104, 0xcc); 681e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x105, 0xbe); 682e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c8, 0x14); 683e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x106, 0x35); 684e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c9, 0x21); 685e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ca, 0x21); 686e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cb, 0x00); 687e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x107, 0x40); 688e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cd, 0x10); 689e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ce, 0x10); 690e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x108, 0x80); 691e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x109, 0x7f); 692e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10a, 0x80); 693e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10b, 0x7f); 694e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 695e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 696e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x011, 0xf4); 69777bbb2b0SAntti Palosaari break; 698fc3a0497SAntti Palosaari case RTL2832_SDR_TUNER_FC2580: 699e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x112, 0x39); 700e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x102, 0x40); 701e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x103, 0x5a); 702e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c7, 0x2c); 703e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x104, 0xcc); 704e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x105, 0xbe); 705e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c8, 0x16); 706e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x106, 0x35); 707e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1c9, 0x21); 708e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ca, 0x21); 709e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cb, 0x00); 710e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x107, 0x40); 711e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1cd, 0x10); 712e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x1ce, 0x10); 713e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x108, 0x80); 714e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x109, 0x7f); 715e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10a, 0x9c); 716e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x10b, 0x7f); 717e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 718e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x00e, 0xfc); 719e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2); 720fc3a0497SAntti Palosaari break; 72177bbb2b0SAntti Palosaari default: 722e4e78693SAntti Palosaari dev_notice(&pdev->dev, "Unsupported tuner\n"); 72377bbb2b0SAntti Palosaari } 72477bbb2b0SAntti Palosaari 72577bbb2b0SAntti Palosaari /* software reset */ 726e01fc42dSPeter Rosin ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04); 72777bbb2b0SAntti Palosaari if (ret) 72877bbb2b0SAntti Palosaari goto err; 72977bbb2b0SAntti Palosaari 730e01fc42dSPeter Rosin ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00); 73177bbb2b0SAntti Palosaari if (ret) 73277bbb2b0SAntti Palosaari goto err; 73377bbb2b0SAntti Palosaari err: 73477bbb2b0SAntti Palosaari return ret; 73577bbb2b0SAntti Palosaari }; 73677bbb2b0SAntti Palosaari 737725b7f30SAntti Palosaari static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev) 73877bbb2b0SAntti Palosaari { 739e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 74077bbb2b0SAntti Palosaari int ret; 74177bbb2b0SAntti Palosaari 742e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 74377bbb2b0SAntti Palosaari 74477bbb2b0SAntti Palosaari /* PID filter */ 745e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x061, 0xe0); 74677bbb2b0SAntti Palosaari if (ret) 74777bbb2b0SAntti Palosaari goto err; 74877bbb2b0SAntti Palosaari 74977bbb2b0SAntti Palosaari /* mode */ 750e01fc42dSPeter Rosin ret = regmap_write(dev->regmap, 0x019, 0x20); 75177bbb2b0SAntti Palosaari if (ret) 75277bbb2b0SAntti Palosaari goto err; 75377bbb2b0SAntti Palosaari 754e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2); 75577bbb2b0SAntti Palosaari if (ret) 75677bbb2b0SAntti Palosaari goto err; 75777bbb2b0SAntti Palosaari 75877bbb2b0SAntti Palosaari /* FSM */ 759e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3); 76077bbb2b0SAntti Palosaari if (ret) 76177bbb2b0SAntti Palosaari goto err; 76277bbb2b0SAntti Palosaari 763e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2); 76477bbb2b0SAntti Palosaari if (ret) 76577bbb2b0SAntti Palosaari goto err; 76677bbb2b0SAntti Palosaari 767e01fc42dSPeter Rosin ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4); 76877bbb2b0SAntti Palosaari if (ret) 76977bbb2b0SAntti Palosaari goto err; 77077bbb2b0SAntti Palosaari err: 77177bbb2b0SAntti Palosaari return; 77277bbb2b0SAntti Palosaari }; 77377bbb2b0SAntti Palosaari 774725b7f30SAntti Palosaari static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev) 77577bbb2b0SAntti Palosaari { 776e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 7773bda2bf1SAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 7783bda2bf1SAntti Palosaari struct dvb_frontend *fe = pdata->dvb_frontend; 77977bbb2b0SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 78077bbb2b0SAntti Palosaari struct v4l2_ctrl *bandwidth_auto; 78177bbb2b0SAntti Palosaari struct v4l2_ctrl *bandwidth; 78277bbb2b0SAntti Palosaari 78377bbb2b0SAntti Palosaari /* 78477bbb2b0SAntti Palosaari * tuner RF (Hz) 78577bbb2b0SAntti Palosaari */ 786725b7f30SAntti Palosaari if (dev->f_tuner == 0) 78777bbb2b0SAntti Palosaari return 0; 78877bbb2b0SAntti Palosaari 78977bbb2b0SAntti Palosaari /* 79077bbb2b0SAntti Palosaari * bandwidth (Hz) 79177bbb2b0SAntti Palosaari */ 792725b7f30SAntti Palosaari bandwidth_auto = v4l2_ctrl_find(&dev->hdl, 79377bbb2b0SAntti Palosaari V4L2_CID_RF_TUNER_BANDWIDTH_AUTO); 794725b7f30SAntti Palosaari bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH); 79577bbb2b0SAntti Palosaari if (v4l2_ctrl_g_ctrl(bandwidth_auto)) { 796725b7f30SAntti Palosaari c->bandwidth_hz = dev->f_adc; 797725b7f30SAntti Palosaari v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc); 79877bbb2b0SAntti Palosaari } else { 79977bbb2b0SAntti Palosaari c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth); 80077bbb2b0SAntti Palosaari } 80177bbb2b0SAntti Palosaari 802725b7f30SAntti Palosaari c->frequency = dev->f_tuner; 80377bbb2b0SAntti Palosaari c->delivery_system = SYS_DVBT; 80477bbb2b0SAntti Palosaari 805e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n", 806d4d20500SAntti Palosaari c->frequency, c->bandwidth_hz); 80777bbb2b0SAntti Palosaari 808725b7f30SAntti Palosaari if (!test_bit(POWER_ON, &dev->flags)) 80977bbb2b0SAntti Palosaari return 0; 81077bbb2b0SAntti Palosaari 811fc3a0497SAntti Palosaari if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) { 81277bbb2b0SAntti Palosaari if (fe->ops.tuner_ops.set_params) 81377bbb2b0SAntti Palosaari fe->ops.tuner_ops.set_params(fe); 814fc3a0497SAntti Palosaari } 81577bbb2b0SAntti Palosaari 81677bbb2b0SAntti Palosaari return 0; 81777bbb2b0SAntti Palosaari }; 81877bbb2b0SAntti Palosaari 819725b7f30SAntti Palosaari static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev) 82077bbb2b0SAntti Palosaari { 821e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 8223bda2bf1SAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 8233bda2bf1SAntti Palosaari struct dvb_frontend *fe = pdata->dvb_frontend; 82477bbb2b0SAntti Palosaari 825e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 82677bbb2b0SAntti Palosaari 82777bbb2b0SAntti Palosaari if (fe->ops.tuner_ops.init) 82877bbb2b0SAntti Palosaari fe->ops.tuner_ops.init(fe); 82977bbb2b0SAntti Palosaari 83077bbb2b0SAntti Palosaari return 0; 83177bbb2b0SAntti Palosaari }; 83277bbb2b0SAntti Palosaari 833725b7f30SAntti Palosaari static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev) 83477bbb2b0SAntti Palosaari { 835e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 8363bda2bf1SAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 8373bda2bf1SAntti Palosaari struct dvb_frontend *fe = pdata->dvb_frontend; 83877bbb2b0SAntti Palosaari 839e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 84077bbb2b0SAntti Palosaari 84177bbb2b0SAntti Palosaari if (fe->ops.tuner_ops.sleep) 84277bbb2b0SAntti Palosaari fe->ops.tuner_ops.sleep(fe); 84377bbb2b0SAntti Palosaari 84477bbb2b0SAntti Palosaari return; 84577bbb2b0SAntti Palosaari }; 84677bbb2b0SAntti Palosaari 84777bbb2b0SAntti Palosaari static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count) 84877bbb2b0SAntti Palosaari { 849725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 850e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 8513bda2bf1SAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 8523bda2bf1SAntti Palosaari struct dvb_usb_device *d = pdata->dvb_usb_device; 85377bbb2b0SAntti Palosaari int ret; 85477bbb2b0SAntti Palosaari 855e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 85677bbb2b0SAntti Palosaari 857725b7f30SAntti Palosaari if (!dev->udev) 85877bbb2b0SAntti Palosaari return -ENODEV; 85977bbb2b0SAntti Palosaari 860725b7f30SAntti Palosaari if (mutex_lock_interruptible(&dev->v4l2_lock)) 86177bbb2b0SAntti Palosaari return -ERESTARTSYS; 86277bbb2b0SAntti Palosaari 8633bda2bf1SAntti Palosaari if (d->props->power_ctrl) 8643bda2bf1SAntti Palosaari d->props->power_ctrl(d, 1); 86577bbb2b0SAntti Palosaari 8662281c824SAntti Palosaari /* enable ADC */ 8673bda2bf1SAntti Palosaari if (d->props->frontend_ctrl) 8683bda2bf1SAntti Palosaari d->props->frontend_ctrl(pdata->dvb_frontend, 1); 8692281c824SAntti Palosaari 870725b7f30SAntti Palosaari set_bit(POWER_ON, &dev->flags); 87177bbb2b0SAntti Palosaari 872fc3a0497SAntti Palosaari /* wake-up tuner */ 873fc3a0497SAntti Palosaari if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power)) 874fc3a0497SAntti Palosaari ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1); 875fc3a0497SAntti Palosaari else 876725b7f30SAntti Palosaari ret = rtl2832_sdr_set_tuner(dev); 87777bbb2b0SAntti Palosaari if (ret) 87877bbb2b0SAntti Palosaari goto err; 87977bbb2b0SAntti Palosaari 880725b7f30SAntti Palosaari ret = rtl2832_sdr_set_tuner_freq(dev); 88177bbb2b0SAntti Palosaari if (ret) 88277bbb2b0SAntti Palosaari goto err; 88377bbb2b0SAntti Palosaari 884725b7f30SAntti Palosaari ret = rtl2832_sdr_set_adc(dev); 88577bbb2b0SAntti Palosaari if (ret) 88677bbb2b0SAntti Palosaari goto err; 88777bbb2b0SAntti Palosaari 888725b7f30SAntti Palosaari ret = rtl2832_sdr_alloc_stream_bufs(dev); 88977bbb2b0SAntti Palosaari if (ret) 89077bbb2b0SAntti Palosaari goto err; 89177bbb2b0SAntti Palosaari 892725b7f30SAntti Palosaari ret = rtl2832_sdr_alloc_urbs(dev); 89377bbb2b0SAntti Palosaari if (ret) 89477bbb2b0SAntti Palosaari goto err; 89577bbb2b0SAntti Palosaari 896725b7f30SAntti Palosaari dev->sequence = 0; 89777bbb2b0SAntti Palosaari 898725b7f30SAntti Palosaari ret = rtl2832_sdr_submit_urbs(dev); 89977bbb2b0SAntti Palosaari if (ret) 90077bbb2b0SAntti Palosaari goto err; 90177bbb2b0SAntti Palosaari 90277bbb2b0SAntti Palosaari err: 903725b7f30SAntti Palosaari mutex_unlock(&dev->v4l2_lock); 90477bbb2b0SAntti Palosaari 90577bbb2b0SAntti Palosaari return ret; 90677bbb2b0SAntti Palosaari } 90777bbb2b0SAntti Palosaari 90877bbb2b0SAntti Palosaari static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq) 90977bbb2b0SAntti Palosaari { 910725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); 911e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 9123bda2bf1SAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 9133bda2bf1SAntti Palosaari struct dvb_usb_device *d = pdata->dvb_usb_device; 91477bbb2b0SAntti Palosaari 915e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 91677bbb2b0SAntti Palosaari 917725b7f30SAntti Palosaari mutex_lock(&dev->v4l2_lock); 91877bbb2b0SAntti Palosaari 919725b7f30SAntti Palosaari rtl2832_sdr_kill_urbs(dev); 920725b7f30SAntti Palosaari rtl2832_sdr_free_urbs(dev); 921725b7f30SAntti Palosaari rtl2832_sdr_free_stream_bufs(dev); 922725b7f30SAntti Palosaari rtl2832_sdr_cleanup_queued_bufs(dev); 923725b7f30SAntti Palosaari rtl2832_sdr_unset_adc(dev); 924fc3a0497SAntti Palosaari 925fc3a0497SAntti Palosaari /* sleep tuner */ 926fc3a0497SAntti Palosaari if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power)) 927fc3a0497SAntti Palosaari v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0); 928fc3a0497SAntti Palosaari else 929725b7f30SAntti Palosaari rtl2832_sdr_unset_tuner(dev); 93077bbb2b0SAntti Palosaari 931725b7f30SAntti Palosaari clear_bit(POWER_ON, &dev->flags); 93277bbb2b0SAntti Palosaari 9332281c824SAntti Palosaari /* disable ADC */ 9343bda2bf1SAntti Palosaari if (d->props->frontend_ctrl) 9353bda2bf1SAntti Palosaari d->props->frontend_ctrl(pdata->dvb_frontend, 0); 9362281c824SAntti Palosaari 9373bda2bf1SAntti Palosaari if (d->props->power_ctrl) 9383bda2bf1SAntti Palosaari d->props->power_ctrl(d, 0); 93977bbb2b0SAntti Palosaari 940725b7f30SAntti Palosaari mutex_unlock(&dev->v4l2_lock); 94177bbb2b0SAntti Palosaari } 94277bbb2b0SAntti Palosaari 943dd93e79cSBhumika Goyal static const struct vb2_ops rtl2832_sdr_vb2_ops = { 94477bbb2b0SAntti Palosaari .queue_setup = rtl2832_sdr_queue_setup, 94577bbb2b0SAntti Palosaari .buf_prepare = rtl2832_sdr_buf_prepare, 94677bbb2b0SAntti Palosaari .buf_queue = rtl2832_sdr_buf_queue, 94777bbb2b0SAntti Palosaari .start_streaming = rtl2832_sdr_start_streaming, 94877bbb2b0SAntti Palosaari .stop_streaming = rtl2832_sdr_stop_streaming, 94977bbb2b0SAntti Palosaari .wait_prepare = vb2_ops_wait_prepare, 95077bbb2b0SAntti Palosaari .wait_finish = vb2_ops_wait_finish, 95177bbb2b0SAntti Palosaari }; 95277bbb2b0SAntti Palosaari 95377bbb2b0SAntti Palosaari static int rtl2832_sdr_g_tuner(struct file *file, void *priv, 95477bbb2b0SAntti Palosaari struct v4l2_tuner *v) 95577bbb2b0SAntti Palosaari { 956725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 957e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 958fc3a0497SAntti Palosaari int ret; 95977bbb2b0SAntti Palosaari 960e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type); 96177bbb2b0SAntti Palosaari 96277bbb2b0SAntti Palosaari if (v->index == 0) { 963c0decac1SMauro Carvalho Chehab strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name)); 96477bbb2b0SAntti Palosaari v->type = V4L2_TUNER_ADC; 96577bbb2b0SAntti Palosaari v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 96677bbb2b0SAntti Palosaari v->rangelow = 300000; 96777bbb2b0SAntti Palosaari v->rangehigh = 3200000; 968fc3a0497SAntti Palosaari ret = 0; 969fc3a0497SAntti Palosaari } else if (v->index == 1 && 970fc3a0497SAntti Palosaari V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) { 971fc3a0497SAntti Palosaari ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v); 97277bbb2b0SAntti Palosaari } else if (v->index == 1) { 973c0decac1SMauro Carvalho Chehab strscpy(v->name, "RF: <unknown>", sizeof(v->name)); 97477bbb2b0SAntti Palosaari v->type = V4L2_TUNER_RF; 97577bbb2b0SAntti Palosaari v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 97677bbb2b0SAntti Palosaari v->rangelow = 50000000; 97777bbb2b0SAntti Palosaari v->rangehigh = 2000000000; 978fc3a0497SAntti Palosaari ret = 0; 97977bbb2b0SAntti Palosaari } else { 980fc3a0497SAntti Palosaari ret = -EINVAL; 98177bbb2b0SAntti Palosaari } 982fc3a0497SAntti Palosaari return ret; 98377bbb2b0SAntti Palosaari } 98477bbb2b0SAntti Palosaari 98577bbb2b0SAntti Palosaari static int rtl2832_sdr_s_tuner(struct file *file, void *priv, 98677bbb2b0SAntti Palosaari const struct v4l2_tuner *v) 98777bbb2b0SAntti Palosaari { 988725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 989e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 990fc3a0497SAntti Palosaari int ret; 99177bbb2b0SAntti Palosaari 992e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 99377bbb2b0SAntti Palosaari 994fc3a0497SAntti Palosaari if (v->index == 0) { 995fc3a0497SAntti Palosaari ret = 0; 996fc3a0497SAntti Palosaari } else if (v->index == 1 && 997fc3a0497SAntti Palosaari V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) { 998fc3a0497SAntti Palosaari ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v); 999fc3a0497SAntti Palosaari } else if (v->index == 1) { 1000fc3a0497SAntti Palosaari ret = 0; 1001fc3a0497SAntti Palosaari } else { 1002fc3a0497SAntti Palosaari ret = -EINVAL; 1003fc3a0497SAntti Palosaari } 1004fc3a0497SAntti Palosaari return ret; 100577bbb2b0SAntti Palosaari } 100677bbb2b0SAntti Palosaari 100777bbb2b0SAntti Palosaari static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv, 100877bbb2b0SAntti Palosaari struct v4l2_frequency_band *band) 100977bbb2b0SAntti Palosaari { 1010725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1011e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 1012fc3a0497SAntti Palosaari int ret; 101377bbb2b0SAntti Palosaari 1014e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n", 1015d4d20500SAntti Palosaari band->tuner, band->type, band->index); 101677bbb2b0SAntti Palosaari 101777bbb2b0SAntti Palosaari if (band->tuner == 0) { 101877bbb2b0SAntti Palosaari if (band->index >= ARRAY_SIZE(bands_adc)) 101977bbb2b0SAntti Palosaari return -EINVAL; 102077bbb2b0SAntti Palosaari 102177bbb2b0SAntti Palosaari *band = bands_adc[band->index]; 1022fc3a0497SAntti Palosaari ret = 0; 1023fc3a0497SAntti Palosaari } else if (band->tuner == 1 && 1024fc3a0497SAntti Palosaari V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) { 1025fc3a0497SAntti Palosaari ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band); 102677bbb2b0SAntti Palosaari } else if (band->tuner == 1) { 102777bbb2b0SAntti Palosaari if (band->index >= ARRAY_SIZE(bands_fm)) 102877bbb2b0SAntti Palosaari return -EINVAL; 102977bbb2b0SAntti Palosaari 103077bbb2b0SAntti Palosaari *band = bands_fm[band->index]; 1031fc3a0497SAntti Palosaari ret = 0; 103277bbb2b0SAntti Palosaari } else { 1033fc3a0497SAntti Palosaari ret = -EINVAL; 103477bbb2b0SAntti Palosaari } 1035fc3a0497SAntti Palosaari return ret; 103677bbb2b0SAntti Palosaari } 103777bbb2b0SAntti Palosaari 103877bbb2b0SAntti Palosaari static int rtl2832_sdr_g_frequency(struct file *file, void *priv, 103977bbb2b0SAntti Palosaari struct v4l2_frequency *f) 104077bbb2b0SAntti Palosaari { 1041725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1042e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 1043fc3a0497SAntti Palosaari int ret; 104477bbb2b0SAntti Palosaari 1045e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type); 104677bbb2b0SAntti Palosaari 104777bbb2b0SAntti Palosaari if (f->tuner == 0) { 1048725b7f30SAntti Palosaari f->frequency = dev->f_adc; 104977bbb2b0SAntti Palosaari f->type = V4L2_TUNER_ADC; 1050fc3a0497SAntti Palosaari ret = 0; 1051fc3a0497SAntti Palosaari } else if (f->tuner == 1 && 1052fc3a0497SAntti Palosaari V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) { 1053fc3a0497SAntti Palosaari f->type = V4L2_TUNER_RF; 1054fc3a0497SAntti Palosaari ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f); 105577bbb2b0SAntti Palosaari } else if (f->tuner == 1) { 1056725b7f30SAntti Palosaari f->frequency = dev->f_tuner; 105777bbb2b0SAntti Palosaari f->type = V4L2_TUNER_RF; 1058fc3a0497SAntti Palosaari ret = 0; 105977bbb2b0SAntti Palosaari } else { 1060fc3a0497SAntti Palosaari ret = -EINVAL; 106177bbb2b0SAntti Palosaari } 106277bbb2b0SAntti Palosaari return ret; 106377bbb2b0SAntti Palosaari } 106477bbb2b0SAntti Palosaari 106577bbb2b0SAntti Palosaari static int rtl2832_sdr_s_frequency(struct file *file, void *priv, 106677bbb2b0SAntti Palosaari const struct v4l2_frequency *f) 106777bbb2b0SAntti Palosaari { 1068725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1069e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 107077bbb2b0SAntti Palosaari int ret, band; 107177bbb2b0SAntti Palosaari 1072e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n", 1073d4d20500SAntti Palosaari f->tuner, f->type, f->frequency); 107477bbb2b0SAntti Palosaari 107577bbb2b0SAntti Palosaari /* ADC band midpoints */ 107677bbb2b0SAntti Palosaari #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2) 107777bbb2b0SAntti Palosaari #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2) 107877bbb2b0SAntti Palosaari 107977bbb2b0SAntti Palosaari if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) { 108077bbb2b0SAntti Palosaari if (f->frequency < BAND_ADC_0) 108177bbb2b0SAntti Palosaari band = 0; 108277bbb2b0SAntti Palosaari else if (f->frequency < BAND_ADC_1) 108377bbb2b0SAntti Palosaari band = 1; 108477bbb2b0SAntti Palosaari else 108577bbb2b0SAntti Palosaari band = 2; 108677bbb2b0SAntti Palosaari 1087725b7f30SAntti Palosaari dev->f_adc = clamp_t(unsigned int, f->frequency, 108877bbb2b0SAntti Palosaari bands_adc[band].rangelow, 108977bbb2b0SAntti Palosaari bands_adc[band].rangehigh); 109077bbb2b0SAntti Palosaari 1091e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc); 1092725b7f30SAntti Palosaari ret = rtl2832_sdr_set_adc(dev); 1093fc3a0497SAntti Palosaari } else if (f->tuner == 1 && 1094fc3a0497SAntti Palosaari V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) { 1095fc3a0497SAntti Palosaari ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f); 109677bbb2b0SAntti Palosaari } else if (f->tuner == 1) { 1097725b7f30SAntti Palosaari dev->f_tuner = clamp_t(unsigned int, f->frequency, 109877bbb2b0SAntti Palosaari bands_fm[0].rangelow, 109977bbb2b0SAntti Palosaari bands_fm[0].rangehigh); 1100e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency); 110177bbb2b0SAntti Palosaari 1102725b7f30SAntti Palosaari ret = rtl2832_sdr_set_tuner_freq(dev); 110377bbb2b0SAntti Palosaari } else { 110477bbb2b0SAntti Palosaari ret = -EINVAL; 110577bbb2b0SAntti Palosaari } 110677bbb2b0SAntti Palosaari return ret; 110777bbb2b0SAntti Palosaari } 110877bbb2b0SAntti Palosaari 110977bbb2b0SAntti Palosaari static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv, 111077bbb2b0SAntti Palosaari struct v4l2_fmtdesc *f) 111177bbb2b0SAntti Palosaari { 1112725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1113e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 111477bbb2b0SAntti Palosaari 1115e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 111677bbb2b0SAntti Palosaari 1117725b7f30SAntti Palosaari if (f->index >= dev->num_formats) 111877bbb2b0SAntti Palosaari return -EINVAL; 111977bbb2b0SAntti Palosaari 112077bbb2b0SAntti Palosaari f->pixelformat = formats[f->index].pixelformat; 112177bbb2b0SAntti Palosaari 112277bbb2b0SAntti Palosaari return 0; 112377bbb2b0SAntti Palosaari } 112477bbb2b0SAntti Palosaari 112577bbb2b0SAntti Palosaari static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv, 112677bbb2b0SAntti Palosaari struct v4l2_format *f) 112777bbb2b0SAntti Palosaari { 1128725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1129e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 113077bbb2b0SAntti Palosaari 1131e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "\n"); 113277bbb2b0SAntti Palosaari 1133725b7f30SAntti Palosaari f->fmt.sdr.pixelformat = dev->pixelformat; 1134725b7f30SAntti Palosaari f->fmt.sdr.buffersize = dev->buffersize; 1135d57fe404SAntti Palosaari 113677bbb2b0SAntti Palosaari return 0; 113777bbb2b0SAntti Palosaari } 113877bbb2b0SAntti Palosaari 113977bbb2b0SAntti Palosaari static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv, 114077bbb2b0SAntti Palosaari struct v4l2_format *f) 114177bbb2b0SAntti Palosaari { 1142725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1143e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 1144725b7f30SAntti Palosaari struct vb2_queue *q = &dev->vb_queue; 114577bbb2b0SAntti Palosaari int i; 114677bbb2b0SAntti Palosaari 1147e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", 114877bbb2b0SAntti Palosaari (char *)&f->fmt.sdr.pixelformat); 114977bbb2b0SAntti Palosaari 115077bbb2b0SAntti Palosaari if (vb2_is_busy(q)) 115177bbb2b0SAntti Palosaari return -EBUSY; 115277bbb2b0SAntti Palosaari 1153725b7f30SAntti Palosaari for (i = 0; i < dev->num_formats; i++) { 115477bbb2b0SAntti Palosaari if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 1155725b7f30SAntti Palosaari dev->pixelformat = formats[i].pixelformat; 1156725b7f30SAntti Palosaari dev->buffersize = formats[i].buffersize; 1157d57fe404SAntti Palosaari f->fmt.sdr.buffersize = formats[i].buffersize; 115877bbb2b0SAntti Palosaari return 0; 115977bbb2b0SAntti Palosaari } 116077bbb2b0SAntti Palosaari } 116177bbb2b0SAntti Palosaari 1162725b7f30SAntti Palosaari dev->pixelformat = formats[0].pixelformat; 1163725b7f30SAntti Palosaari dev->buffersize = formats[0].buffersize; 1164d57fe404SAntti Palosaari f->fmt.sdr.pixelformat = formats[0].pixelformat; 1165d57fe404SAntti Palosaari f->fmt.sdr.buffersize = formats[0].buffersize; 116677bbb2b0SAntti Palosaari 116777bbb2b0SAntti Palosaari return 0; 116877bbb2b0SAntti Palosaari } 116977bbb2b0SAntti Palosaari 117077bbb2b0SAntti Palosaari static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv, 117177bbb2b0SAntti Palosaari struct v4l2_format *f) 117277bbb2b0SAntti Palosaari { 1173725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = video_drvdata(file); 1174e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 117577bbb2b0SAntti Palosaari int i; 117677bbb2b0SAntti Palosaari 1177e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", 117877bbb2b0SAntti Palosaari (char *)&f->fmt.sdr.pixelformat); 117977bbb2b0SAntti Palosaari 1180725b7f30SAntti Palosaari for (i = 0; i < dev->num_formats; i++) { 1181d57fe404SAntti Palosaari if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 1182d57fe404SAntti Palosaari f->fmt.sdr.buffersize = formats[i].buffersize; 118377bbb2b0SAntti Palosaari return 0; 118477bbb2b0SAntti Palosaari } 1185d57fe404SAntti Palosaari } 118677bbb2b0SAntti Palosaari 118777bbb2b0SAntti Palosaari f->fmt.sdr.pixelformat = formats[0].pixelformat; 1188d57fe404SAntti Palosaari f->fmt.sdr.buffersize = formats[0].buffersize; 118977bbb2b0SAntti Palosaari 119077bbb2b0SAntti Palosaari return 0; 119177bbb2b0SAntti Palosaari } 119277bbb2b0SAntti Palosaari 119377bbb2b0SAntti Palosaari static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = { 119477bbb2b0SAntti Palosaari .vidioc_querycap = rtl2832_sdr_querycap, 119577bbb2b0SAntti Palosaari 119677bbb2b0SAntti Palosaari .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap, 119777bbb2b0SAntti Palosaari .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap, 119877bbb2b0SAntti Palosaari .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap, 119977bbb2b0SAntti Palosaari .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap, 120077bbb2b0SAntti Palosaari 120177bbb2b0SAntti Palosaari .vidioc_reqbufs = vb2_ioctl_reqbufs, 120277bbb2b0SAntti Palosaari .vidioc_create_bufs = vb2_ioctl_create_bufs, 120377bbb2b0SAntti Palosaari .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 120477bbb2b0SAntti Palosaari .vidioc_querybuf = vb2_ioctl_querybuf, 120577bbb2b0SAntti Palosaari .vidioc_qbuf = vb2_ioctl_qbuf, 120677bbb2b0SAntti Palosaari .vidioc_dqbuf = vb2_ioctl_dqbuf, 120777bbb2b0SAntti Palosaari 120877bbb2b0SAntti Palosaari .vidioc_streamon = vb2_ioctl_streamon, 120977bbb2b0SAntti Palosaari .vidioc_streamoff = vb2_ioctl_streamoff, 121077bbb2b0SAntti Palosaari 121177bbb2b0SAntti Palosaari .vidioc_g_tuner = rtl2832_sdr_g_tuner, 121277bbb2b0SAntti Palosaari .vidioc_s_tuner = rtl2832_sdr_s_tuner, 121377bbb2b0SAntti Palosaari 121477bbb2b0SAntti Palosaari .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands, 121577bbb2b0SAntti Palosaari .vidioc_g_frequency = rtl2832_sdr_g_frequency, 121677bbb2b0SAntti Palosaari .vidioc_s_frequency = rtl2832_sdr_s_frequency, 121777bbb2b0SAntti Palosaari 121877bbb2b0SAntti Palosaari .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 121977bbb2b0SAntti Palosaari .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 122077bbb2b0SAntti Palosaari .vidioc_log_status = v4l2_ctrl_log_status, 122177bbb2b0SAntti Palosaari }; 122277bbb2b0SAntti Palosaari 122377bbb2b0SAntti Palosaari static const struct v4l2_file_operations rtl2832_sdr_fops = { 122477bbb2b0SAntti Palosaari .owner = THIS_MODULE, 122577bbb2b0SAntti Palosaari .open = v4l2_fh_open, 122677bbb2b0SAntti Palosaari .release = vb2_fop_release, 122777bbb2b0SAntti Palosaari .read = vb2_fop_read, 122877bbb2b0SAntti Palosaari .poll = vb2_fop_poll, 122977bbb2b0SAntti Palosaari .mmap = vb2_fop_mmap, 123077bbb2b0SAntti Palosaari .unlocked_ioctl = video_ioctl2, 123177bbb2b0SAntti Palosaari }; 123277bbb2b0SAntti Palosaari 123377bbb2b0SAntti Palosaari static struct video_device rtl2832_sdr_template = { 123477bbb2b0SAntti Palosaari .name = "Realtek RTL2832 SDR", 123577bbb2b0SAntti Palosaari .release = video_device_release_empty, 123677bbb2b0SAntti Palosaari .fops = &rtl2832_sdr_fops, 123777bbb2b0SAntti Palosaari .ioctl_ops = &rtl2832_sdr_ioctl_ops, 1238372332b1SHans Verkuil .device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | 1239372332b1SHans Verkuil V4L2_CAP_READWRITE | V4L2_CAP_TUNER, 124077bbb2b0SAntti Palosaari }; 124177bbb2b0SAntti Palosaari 124277bbb2b0SAntti Palosaari static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl) 124377bbb2b0SAntti Palosaari { 1244725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = 1245725b7f30SAntti Palosaari container_of(ctrl->handler, struct rtl2832_sdr_dev, 124677bbb2b0SAntti Palosaari hdl); 1247e4e78693SAntti Palosaari struct platform_device *pdev = dev->pdev; 12483bda2bf1SAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 12493bda2bf1SAntti Palosaari struct dvb_frontend *fe = pdata->dvb_frontend; 125077bbb2b0SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 125177bbb2b0SAntti Palosaari int ret; 125277bbb2b0SAntti Palosaari 1253e4e78693SAntti Palosaari dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n", 1254e4e78693SAntti Palosaari ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum, 1255e4e78693SAntti Palosaari ctrl->step); 125677bbb2b0SAntti Palosaari 125777bbb2b0SAntti Palosaari switch (ctrl->id) { 125877bbb2b0SAntti Palosaari case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: 125977bbb2b0SAntti Palosaari case V4L2_CID_RF_TUNER_BANDWIDTH: 126077bbb2b0SAntti Palosaari /* TODO: these controls should be moved to tuner drivers */ 1261725b7f30SAntti Palosaari if (dev->bandwidth_auto->val) { 126277bbb2b0SAntti Palosaari /* Round towards the closest legal value */ 1263725b7f30SAntti Palosaari s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2); 126477bbb2b0SAntti Palosaari u32 offset; 126577bbb2b0SAntti Palosaari 1266725b7f30SAntti Palosaari val = clamp_t(s32, val, dev->bandwidth->minimum, 1267725b7f30SAntti Palosaari dev->bandwidth->maximum); 1268725b7f30SAntti Palosaari offset = val - dev->bandwidth->minimum; 1269725b7f30SAntti Palosaari offset = dev->bandwidth->step * 1270725b7f30SAntti Palosaari div_u64(offset, dev->bandwidth->step); 1271725b7f30SAntti Palosaari dev->bandwidth->val = dev->bandwidth->minimum + offset; 127277bbb2b0SAntti Palosaari } 1273725b7f30SAntti Palosaari c->bandwidth_hz = dev->bandwidth->val; 127477bbb2b0SAntti Palosaari 1275725b7f30SAntti Palosaari if (!test_bit(POWER_ON, &dev->flags)) 127677bbb2b0SAntti Palosaari return 0; 127777bbb2b0SAntti Palosaari 127877bbb2b0SAntti Palosaari if (fe->ops.tuner_ops.set_params) 127977bbb2b0SAntti Palosaari ret = fe->ops.tuner_ops.set_params(fe); 128077bbb2b0SAntti Palosaari else 128177bbb2b0SAntti Palosaari ret = 0; 128277bbb2b0SAntti Palosaari break; 128377bbb2b0SAntti Palosaari default: 128477bbb2b0SAntti Palosaari ret = -EINVAL; 128577bbb2b0SAntti Palosaari } 128677bbb2b0SAntti Palosaari 128777bbb2b0SAntti Palosaari return ret; 128877bbb2b0SAntti Palosaari } 128977bbb2b0SAntti Palosaari 129077bbb2b0SAntti Palosaari static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = { 129177bbb2b0SAntti Palosaari .s_ctrl = rtl2832_sdr_s_ctrl, 129277bbb2b0SAntti Palosaari }; 129377bbb2b0SAntti Palosaari 129477bbb2b0SAntti Palosaari static void rtl2832_sdr_video_release(struct v4l2_device *v) 129577bbb2b0SAntti Palosaari { 1296725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev = 1297725b7f30SAntti Palosaari container_of(v, struct rtl2832_sdr_dev, v4l2_dev); 129863bdab5dSAntti Palosaari struct platform_device *pdev = dev->pdev; 129963bdab5dSAntti Palosaari 130063bdab5dSAntti Palosaari dev_dbg(&pdev->dev, "\n"); 130177bbb2b0SAntti Palosaari 1302725b7f30SAntti Palosaari v4l2_ctrl_handler_free(&dev->hdl); 1303725b7f30SAntti Palosaari v4l2_device_unregister(&dev->v4l2_dev); 1304725b7f30SAntti Palosaari kfree(dev); 130577bbb2b0SAntti Palosaari } 130677bbb2b0SAntti Palosaari 130763bdab5dSAntti Palosaari /* Platform driver interface */ 130863bdab5dSAntti Palosaari static int rtl2832_sdr_probe(struct platform_device *pdev) 130977bbb2b0SAntti Palosaari { 1310725b7f30SAntti Palosaari struct rtl2832_sdr_dev *dev; 131163bdab5dSAntti Palosaari struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; 131277bbb2b0SAntti Palosaari const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops; 131363bdab5dSAntti Palosaari struct v4l2_subdev *subdev; 131463bdab5dSAntti Palosaari int ret; 131577bbb2b0SAntti Palosaari 131663bdab5dSAntti Palosaari dev_dbg(&pdev->dev, "\n"); 131763bdab5dSAntti Palosaari 131863bdab5dSAntti Palosaari if (!pdata) { 131963bdab5dSAntti Palosaari dev_err(&pdev->dev, "Cannot proceed without platform data\n"); 132063bdab5dSAntti Palosaari ret = -EINVAL; 132163bdab5dSAntti Palosaari goto err; 132263bdab5dSAntti Palosaari } 1323d5eec231SAntti Palosaari if (!pdev->dev.parent->driver) { 1324d5eec231SAntti Palosaari dev_dbg(&pdev->dev, "No parent device\n"); 1325d5eec231SAntti Palosaari ret = -EINVAL; 1326d5eec231SAntti Palosaari goto err; 1327d5eec231SAntti Palosaari } 1328d5eec231SAntti Palosaari /* try to refcount host drv since we are the consumer */ 1329d5eec231SAntti Palosaari if (!try_module_get(pdev->dev.parent->driver->owner)) { 1330d5eec231SAntti Palosaari dev_err(&pdev->dev, "Refcount fail"); 1331d5eec231SAntti Palosaari ret = -EINVAL; 1332d5eec231SAntti Palosaari goto err; 1333d5eec231SAntti Palosaari } 133463bdab5dSAntti Palosaari dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1335725b7f30SAntti Palosaari if (dev == NULL) { 133663bdab5dSAntti Palosaari ret = -ENOMEM; 1337d5eec231SAntti Palosaari goto err_module_put; 133877bbb2b0SAntti Palosaari } 133977bbb2b0SAntti Palosaari 134077bbb2b0SAntti Palosaari /* setup the state */ 134163bdab5dSAntti Palosaari subdev = pdata->v4l2_subdev; 1342fc3a0497SAntti Palosaari dev->v4l2_subdev = pdata->v4l2_subdev; 134363bdab5dSAntti Palosaari dev->pdev = pdev; 1344e01fc42dSPeter Rosin dev->regmap = pdata->regmap; 134563bdab5dSAntti Palosaari dev->udev = pdata->dvb_usb_device->udev; 1346725b7f30SAntti Palosaari dev->f_adc = bands_adc[0].rangelow; 1347725b7f30SAntti Palosaari dev->f_tuner = bands_fm[0].rangelow; 1348725b7f30SAntti Palosaari dev->pixelformat = formats[0].pixelformat; 1349725b7f30SAntti Palosaari dev->buffersize = formats[0].buffersize; 1350725b7f30SAntti Palosaari dev->num_formats = NUM_FORMATS; 13512fe15e20SMauro Carvalho Chehab if (!rtl2832_sdr_emulated_fmt) 1352725b7f30SAntti Palosaari dev->num_formats -= 1; 135377bbb2b0SAntti Palosaari 1354725b7f30SAntti Palosaari mutex_init(&dev->v4l2_lock); 1355725b7f30SAntti Palosaari mutex_init(&dev->vb_queue_lock); 1356725b7f30SAntti Palosaari spin_lock_init(&dev->queued_bufs_lock); 1357725b7f30SAntti Palosaari INIT_LIST_HEAD(&dev->queued_bufs); 135877bbb2b0SAntti Palosaari 135977bbb2b0SAntti Palosaari /* Init videobuf2 queue structure */ 1360725b7f30SAntti Palosaari dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; 1361725b7f30SAntti Palosaari dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1362725b7f30SAntti Palosaari dev->vb_queue.drv_priv = dev; 1363725b7f30SAntti Palosaari dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf); 1364725b7f30SAntti Palosaari dev->vb_queue.ops = &rtl2832_sdr_vb2_ops; 1365725b7f30SAntti Palosaari dev->vb_queue.mem_ops = &vb2_vmalloc_memops; 1366725b7f30SAntti Palosaari dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1367725b7f30SAntti Palosaari ret = vb2_queue_init(&dev->vb_queue); 136877bbb2b0SAntti Palosaari if (ret) { 136963bdab5dSAntti Palosaari dev_err(&pdev->dev, "Could not initialize vb2 queue\n"); 137063bdab5dSAntti Palosaari goto err_kfree; 137177bbb2b0SAntti Palosaari } 137277bbb2b0SAntti Palosaari 137377bbb2b0SAntti Palosaari /* Register controls */ 137463bdab5dSAntti Palosaari switch (pdata->tuner) { 137563bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_E4000: 1376725b7f30SAntti Palosaari v4l2_ctrl_handler_init(&dev->hdl, 9); 137763bdab5dSAntti Palosaari if (subdev) 1378da1b1aeaSHans Verkuil v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, 1379da1b1aeaSHans Verkuil NULL, true); 138077bbb2b0SAntti Palosaari break; 138163bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_R820T: 138263bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_R828D: 1383725b7f30SAntti Palosaari v4l2_ctrl_handler_init(&dev->hdl, 2); 1384725b7f30SAntti Palosaari dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, 138577bbb2b0SAntti Palosaari V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 138677bbb2b0SAntti Palosaari 0, 1, 1, 1); 1387725b7f30SAntti Palosaari dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, 138877bbb2b0SAntti Palosaari V4L2_CID_RF_TUNER_BANDWIDTH, 138977bbb2b0SAntti Palosaari 0, 8000000, 100000, 0); 1390725b7f30SAntti Palosaari v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); 139177bbb2b0SAntti Palosaari break; 139263bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_FC0012: 139363bdab5dSAntti Palosaari case RTL2832_SDR_TUNER_FC0013: 1394725b7f30SAntti Palosaari v4l2_ctrl_handler_init(&dev->hdl, 2); 1395725b7f30SAntti Palosaari dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, 139677bbb2b0SAntti Palosaari V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 139777bbb2b0SAntti Palosaari 0, 1, 1, 1); 1398725b7f30SAntti Palosaari dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, 139977bbb2b0SAntti Palosaari V4L2_CID_RF_TUNER_BANDWIDTH, 140077bbb2b0SAntti Palosaari 6000000, 8000000, 1000000, 140177bbb2b0SAntti Palosaari 6000000); 1402725b7f30SAntti Palosaari v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); 140377bbb2b0SAntti Palosaari break; 1404fc3a0497SAntti Palosaari case RTL2832_SDR_TUNER_FC2580: 1405fc3a0497SAntti Palosaari v4l2_ctrl_handler_init(&dev->hdl, 2); 1406fc3a0497SAntti Palosaari if (subdev) 1407fc3a0497SAntti Palosaari v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, 1408da1b1aeaSHans Verkuil NULL, true); 1409fc3a0497SAntti Palosaari break; 141077bbb2b0SAntti Palosaari default: 1411725b7f30SAntti Palosaari v4l2_ctrl_handler_init(&dev->hdl, 0); 141263bdab5dSAntti Palosaari dev_err(&pdev->dev, "Unsupported tuner\n"); 1413ce5d72b6SEvgeny Novikov ret = -ENODEV; 141463bdab5dSAntti Palosaari goto err_v4l2_ctrl_handler_free; 141577bbb2b0SAntti Palosaari } 1416725b7f30SAntti Palosaari if (dev->hdl.error) { 1417725b7f30SAntti Palosaari ret = dev->hdl.error; 141863bdab5dSAntti Palosaari dev_err(&pdev->dev, "Could not initialize controls\n"); 141963bdab5dSAntti Palosaari goto err_v4l2_ctrl_handler_free; 142077bbb2b0SAntti Palosaari } 142177bbb2b0SAntti Palosaari 142277bbb2b0SAntti Palosaari /* Init video_device structure */ 1423725b7f30SAntti Palosaari dev->vdev = rtl2832_sdr_template; 1424725b7f30SAntti Palosaari dev->vdev.queue = &dev->vb_queue; 1425725b7f30SAntti Palosaari dev->vdev.queue->lock = &dev->vb_queue_lock; 1426725b7f30SAntti Palosaari video_set_drvdata(&dev->vdev, dev); 142777bbb2b0SAntti Palosaari 142877bbb2b0SAntti Palosaari /* Register the v4l2_device structure */ 1429725b7f30SAntti Palosaari dev->v4l2_dev.release = rtl2832_sdr_video_release; 1430e4e78693SAntti Palosaari ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 143177bbb2b0SAntti Palosaari if (ret) { 143263bdab5dSAntti Palosaari dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret); 143363bdab5dSAntti Palosaari goto err_v4l2_ctrl_handler_free; 143477bbb2b0SAntti Palosaari } 143577bbb2b0SAntti Palosaari 1436725b7f30SAntti Palosaari dev->v4l2_dev.ctrl_handler = &dev->hdl; 1437725b7f30SAntti Palosaari dev->vdev.v4l2_dev = &dev->v4l2_dev; 1438725b7f30SAntti Palosaari dev->vdev.lock = &dev->v4l2_lock; 1439725b7f30SAntti Palosaari dev->vdev.vfl_dir = VFL_DIR_RX; 144077bbb2b0SAntti Palosaari 1441725b7f30SAntti Palosaari ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1); 144277bbb2b0SAntti Palosaari if (ret) { 144363bdab5dSAntti Palosaari dev_err(&pdev->dev, "Failed to register as video device %d\n", 144477bbb2b0SAntti Palosaari ret); 144563bdab5dSAntti Palosaari goto err_v4l2_device_unregister; 144677bbb2b0SAntti Palosaari } 144763bdab5dSAntti Palosaari dev_info(&pdev->dev, "Registered as %s\n", 1448725b7f30SAntti Palosaari video_device_node_name(&dev->vdev)); 144963bdab5dSAntti Palosaari dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n"); 145063bdab5dSAntti Palosaari dev_notice(&pdev->dev, 145163bdab5dSAntti Palosaari "SDR API is still slightly experimental and functionality changes may follow\n"); 145263bdab5dSAntti Palosaari platform_set_drvdata(pdev, dev); 145363bdab5dSAntti Palosaari return 0; 145463bdab5dSAntti Palosaari err_v4l2_device_unregister: 1455725b7f30SAntti Palosaari v4l2_device_unregister(&dev->v4l2_dev); 145663bdab5dSAntti Palosaari err_v4l2_ctrl_handler_free: 1457725b7f30SAntti Palosaari v4l2_ctrl_handler_free(&dev->hdl); 145863bdab5dSAntti Palosaari err_kfree: 1459725b7f30SAntti Palosaari kfree(dev); 1460d5eec231SAntti Palosaari err_module_put: 1461d5eec231SAntti Palosaari module_put(pdev->dev.parent->driver->owner); 146263bdab5dSAntti Palosaari err: 146363bdab5dSAntti Palosaari return ret; 146477bbb2b0SAntti Palosaari } 146563bdab5dSAntti Palosaari 1466*297bb597SUwe Kleine-König static void rtl2832_sdr_remove(struct platform_device *pdev) 146763bdab5dSAntti Palosaari { 146863bdab5dSAntti Palosaari struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev); 146963bdab5dSAntti Palosaari 147063bdab5dSAntti Palosaari dev_dbg(&pdev->dev, "\n"); 147163bdab5dSAntti Palosaari 147263bdab5dSAntti Palosaari mutex_lock(&dev->vb_queue_lock); 147363bdab5dSAntti Palosaari mutex_lock(&dev->v4l2_lock); 147463bdab5dSAntti Palosaari /* No need to keep the urbs around after disconnection */ 147563bdab5dSAntti Palosaari dev->udev = NULL; 147663bdab5dSAntti Palosaari v4l2_device_disconnect(&dev->v4l2_dev); 147763bdab5dSAntti Palosaari video_unregister_device(&dev->vdev); 147863bdab5dSAntti Palosaari mutex_unlock(&dev->v4l2_lock); 147963bdab5dSAntti Palosaari mutex_unlock(&dev->vb_queue_lock); 148063bdab5dSAntti Palosaari v4l2_device_put(&dev->v4l2_dev); 1481d5eec231SAntti Palosaari module_put(pdev->dev.parent->driver->owner); 148263bdab5dSAntti Palosaari } 148363bdab5dSAntti Palosaari 148463bdab5dSAntti Palosaari static struct platform_driver rtl2832_sdr_driver = { 148563bdab5dSAntti Palosaari .driver = { 148663bdab5dSAntti Palosaari .name = "rtl2832_sdr", 148763bdab5dSAntti Palosaari }, 148863bdab5dSAntti Palosaari .probe = rtl2832_sdr_probe, 1489*297bb597SUwe Kleine-König .remove_new = rtl2832_sdr_remove, 149063bdab5dSAntti Palosaari }; 149163bdab5dSAntti Palosaari module_platform_driver(rtl2832_sdr_driver); 149277bbb2b0SAntti Palosaari 149377bbb2b0SAntti Palosaari MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 149477bbb2b0SAntti Palosaari MODULE_DESCRIPTION("Realtek RTL2832 SDR driver"); 149577bbb2b0SAntti Palosaari MODULE_LICENSE("GPL"); 1496