xref: /openbmc/linux/drivers/media/dvb-frontends/rtl2832_sdr.c (revision 3bda2bf1f85f94f6a76f0260cd2fbe6ded71104b)
177bbb2b0SAntti Palosaari /*
277bbb2b0SAntti Palosaari  * Realtek RTL2832U SDR driver
377bbb2b0SAntti Palosaari  *
477bbb2b0SAntti Palosaari  * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
577bbb2b0SAntti Palosaari  *
677bbb2b0SAntti Palosaari  *    This program is free software; you can redistribute it and/or modify
777bbb2b0SAntti Palosaari  *    it under the terms of the GNU General Public License as published by
877bbb2b0SAntti Palosaari  *    the Free Software Foundation; either version 2 of the License, or
977bbb2b0SAntti Palosaari  *    (at your option) any later version.
1077bbb2b0SAntti Palosaari  *
1177bbb2b0SAntti Palosaari  *    This program is distributed in the hope that it will be useful,
1277bbb2b0SAntti Palosaari  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
1377bbb2b0SAntti Palosaari  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1477bbb2b0SAntti Palosaari  *    GNU General Public License for more details.
1577bbb2b0SAntti Palosaari  *
1677bbb2b0SAntti Palosaari  *    You should have received a copy of the GNU General Public License along
1777bbb2b0SAntti Palosaari  *    with this program; if not, write to the Free Software Foundation, Inc.,
1877bbb2b0SAntti Palosaari  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1977bbb2b0SAntti Palosaari  *
2077bbb2b0SAntti Palosaari  * GNU Radio plugin "gr-kernel" for device usage will be on:
2177bbb2b0SAntti Palosaari  * http://git.linuxtv.org/anttip/gr-kernel.git
2277bbb2b0SAntti Palosaari  *
2377bbb2b0SAntti Palosaari  */
2477bbb2b0SAntti Palosaari 
2577bbb2b0SAntti Palosaari #include "rtl2832_sdr.h"
2677bbb2b0SAntti Palosaari #include "dvb_usb.h"
2777bbb2b0SAntti Palosaari 
2877bbb2b0SAntti Palosaari #include <media/v4l2-device.h>
2977bbb2b0SAntti Palosaari #include <media/v4l2-ioctl.h>
3077bbb2b0SAntti Palosaari #include <media/v4l2-ctrls.h>
3177bbb2b0SAntti Palosaari #include <media/v4l2-event.h>
3277bbb2b0SAntti Palosaari #include <media/videobuf2-vmalloc.h>
3377bbb2b0SAntti Palosaari 
3463bdab5dSAntti Palosaari #include <linux/platform_device.h>
3577bbb2b0SAntti Palosaari #include <linux/jiffies.h>
3677bbb2b0SAntti Palosaari #include <linux/math64.h>
3777bbb2b0SAntti Palosaari 
382970c0d5SAntti Palosaari static bool rtl2832_sdr_emulated_fmt;
392970c0d5SAntti Palosaari module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
402970c0d5SAntti Palosaari MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
412970c0d5SAntti Palosaari 
4277bbb2b0SAntti Palosaari #define MAX_BULK_BUFS            (10)
4377bbb2b0SAntti Palosaari #define BULK_BUFFER_SIZE         (128 * 512)
4477bbb2b0SAntti Palosaari 
4577bbb2b0SAntti Palosaari static const struct v4l2_frequency_band bands_adc[] = {
4677bbb2b0SAntti Palosaari 	{
4777bbb2b0SAntti Palosaari 		.tuner = 0,
4877bbb2b0SAntti Palosaari 		.type = V4L2_TUNER_ADC,
4977bbb2b0SAntti Palosaari 		.index = 0,
5077bbb2b0SAntti Palosaari 		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
5177bbb2b0SAntti Palosaari 		.rangelow   =  300000,
5277bbb2b0SAntti Palosaari 		.rangehigh  =  300000,
5377bbb2b0SAntti Palosaari 	},
5477bbb2b0SAntti Palosaari 	{
5577bbb2b0SAntti Palosaari 		.tuner = 0,
5677bbb2b0SAntti Palosaari 		.type = V4L2_TUNER_ADC,
5777bbb2b0SAntti Palosaari 		.index = 1,
5877bbb2b0SAntti Palosaari 		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
5977bbb2b0SAntti Palosaari 		.rangelow   =  900001,
6077bbb2b0SAntti Palosaari 		.rangehigh  = 2800000,
6177bbb2b0SAntti Palosaari 	},
6277bbb2b0SAntti Palosaari 	{
6377bbb2b0SAntti Palosaari 		.tuner = 0,
6477bbb2b0SAntti Palosaari 		.type = V4L2_TUNER_ADC,
6577bbb2b0SAntti Palosaari 		.index = 2,
6677bbb2b0SAntti Palosaari 		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
6777bbb2b0SAntti Palosaari 		.rangelow   = 3200000,
6877bbb2b0SAntti Palosaari 		.rangehigh  = 3200000,
6977bbb2b0SAntti Palosaari 	},
7077bbb2b0SAntti Palosaari };
7177bbb2b0SAntti Palosaari 
7277bbb2b0SAntti Palosaari static const struct v4l2_frequency_band bands_fm[] = {
7377bbb2b0SAntti Palosaari 	{
7477bbb2b0SAntti Palosaari 		.tuner = 1,
7577bbb2b0SAntti Palosaari 		.type = V4L2_TUNER_RF,
7677bbb2b0SAntti Palosaari 		.index = 0,
7777bbb2b0SAntti Palosaari 		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
7877bbb2b0SAntti Palosaari 		.rangelow   =    50000000,
7977bbb2b0SAntti Palosaari 		.rangehigh  =  2000000000,
8077bbb2b0SAntti Palosaari 	},
8177bbb2b0SAntti Palosaari };
8277bbb2b0SAntti Palosaari 
8377bbb2b0SAntti Palosaari /* stream formats */
8477bbb2b0SAntti Palosaari struct rtl2832_sdr_format {
8577bbb2b0SAntti Palosaari 	char	*name;
8677bbb2b0SAntti Palosaari 	u32	pixelformat;
87d57fe404SAntti Palosaari 	u32	buffersize;
8877bbb2b0SAntti Palosaari };
8977bbb2b0SAntti Palosaari 
9077bbb2b0SAntti Palosaari static struct rtl2832_sdr_format formats[] = {
9177bbb2b0SAntti Palosaari 	{
922970c0d5SAntti Palosaari 		.name		= "Complex U8",
9377bbb2b0SAntti Palosaari 		.pixelformat	= V4L2_SDR_FMT_CU8,
94d57fe404SAntti Palosaari 		.buffersize	= BULK_BUFFER_SIZE,
9577bbb2b0SAntti Palosaari 	}, {
962970c0d5SAntti Palosaari 		.name		= "Complex U16LE (emulated)",
9777bbb2b0SAntti Palosaari 		.pixelformat	= V4L2_SDR_FMT_CU16LE,
98d57fe404SAntti Palosaari 		.buffersize	= BULK_BUFFER_SIZE * 2,
9977bbb2b0SAntti Palosaari 	},
10077bbb2b0SAntti Palosaari };
10177bbb2b0SAntti Palosaari 
10277bbb2b0SAntti Palosaari static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
10377bbb2b0SAntti Palosaari 
10477bbb2b0SAntti Palosaari /* intermediate buffers with raw data from the USB device */
10577bbb2b0SAntti Palosaari struct rtl2832_sdr_frame_buf {
10677bbb2b0SAntti Palosaari 	struct vb2_buffer vb;   /* common v4l buffer stuff -- must be first */
10777bbb2b0SAntti Palosaari 	struct list_head list;
10877bbb2b0SAntti Palosaari };
10977bbb2b0SAntti Palosaari 
110725b7f30SAntti Palosaari struct rtl2832_sdr_dev {
11177bbb2b0SAntti Palosaari #define POWER_ON           (1 << 1)
11277bbb2b0SAntti Palosaari #define URB_BUF            (1 << 2)
11377bbb2b0SAntti Palosaari 	unsigned long flags;
11477bbb2b0SAntti Palosaari 
11563bdab5dSAntti Palosaari 	struct platform_device *pdev;
11677bbb2b0SAntti Palosaari 
11777bbb2b0SAntti Palosaari 	struct video_device vdev;
11877bbb2b0SAntti Palosaari 	struct v4l2_device v4l2_dev;
11977bbb2b0SAntti Palosaari 
12077bbb2b0SAntti Palosaari 	/* videobuf2 queue and queued buffers list */
12177bbb2b0SAntti Palosaari 	struct vb2_queue vb_queue;
12277bbb2b0SAntti Palosaari 	struct list_head queued_bufs;
12377bbb2b0SAntti Palosaari 	spinlock_t queued_bufs_lock; /* Protects queued_bufs */
12477bbb2b0SAntti Palosaari 	unsigned sequence;	     /* buffer sequence counter */
12577bbb2b0SAntti Palosaari 
12677bbb2b0SAntti Palosaari 	/* Note if taking both locks v4l2_lock must always be locked first! */
12777bbb2b0SAntti Palosaari 	struct mutex v4l2_lock;      /* Protects everything else */
12877bbb2b0SAntti Palosaari 	struct mutex vb_queue_lock;  /* Protects vb_queue and capt_file */
12977bbb2b0SAntti Palosaari 
13077bbb2b0SAntti Palosaari 	/* Pointer to our usb_device, will be NULL after unplug */
13177bbb2b0SAntti Palosaari 	struct usb_device *udev; /* Both mutexes most be hold when setting! */
13277bbb2b0SAntti Palosaari 
13377bbb2b0SAntti Palosaari 	unsigned int vb_full; /* vb is full and packets dropped */
13477bbb2b0SAntti Palosaari 
13577bbb2b0SAntti Palosaari 	struct urb     *urb_list[MAX_BULK_BUFS];
13677bbb2b0SAntti Palosaari 	int            buf_num;
13777bbb2b0SAntti Palosaari 	unsigned long  buf_size;
13877bbb2b0SAntti Palosaari 	u8             *buf_list[MAX_BULK_BUFS];
13977bbb2b0SAntti Palosaari 	dma_addr_t     dma_addr[MAX_BULK_BUFS];
14077bbb2b0SAntti Palosaari 	int urbs_initialized;
14177bbb2b0SAntti Palosaari 	int urbs_submitted;
14277bbb2b0SAntti Palosaari 
14377bbb2b0SAntti Palosaari 	unsigned int f_adc, f_tuner;
14477bbb2b0SAntti Palosaari 	u32 pixelformat;
145d57fe404SAntti Palosaari 	u32 buffersize;
1462970c0d5SAntti Palosaari 	unsigned int num_formats;
14777bbb2b0SAntti Palosaari 
14877bbb2b0SAntti Palosaari 	/* Controls */
14977bbb2b0SAntti Palosaari 	struct v4l2_ctrl_handler hdl;
15077bbb2b0SAntti Palosaari 	struct v4l2_ctrl *bandwidth_auto;
15177bbb2b0SAntti Palosaari 	struct v4l2_ctrl *bandwidth;
15277bbb2b0SAntti Palosaari 
15377bbb2b0SAntti Palosaari 	/* for sample rate calc */
15477bbb2b0SAntti Palosaari 	unsigned int sample;
15577bbb2b0SAntti Palosaari 	unsigned int sample_measured;
15677bbb2b0SAntti Palosaari 	unsigned long jiffies_next;
15777bbb2b0SAntti Palosaari };
15877bbb2b0SAntti Palosaari 
15977bbb2b0SAntti Palosaari /* write multiple registers */
160725b7f30SAntti Palosaari static int rtl2832_sdr_wr_regs(struct rtl2832_sdr_dev *dev, u16 reg,
16177bbb2b0SAntti Palosaari 		const u8 *val, int len)
16277bbb2b0SAntti Palosaari {
16363bdab5dSAntti Palosaari 	struct platform_device *pdev = dev->pdev;
16463bdab5dSAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
16563bdab5dSAntti Palosaari 	struct i2c_client *client = pdata->i2c_client;
16677bbb2b0SAntti Palosaari 
16763bdab5dSAntti Palosaari 	return pdata->bulk_write(client, reg, val, len);
16877bbb2b0SAntti Palosaari }
16977bbb2b0SAntti Palosaari 
17063bdab5dSAntti Palosaari #if 0
17177bbb2b0SAntti Palosaari /* read multiple registers */
172725b7f30SAntti Palosaari static int rtl2832_sdr_rd_regs(struct rtl2832_sdr_dev *dev, u16 reg, u8 *val,
17377bbb2b0SAntti Palosaari 		int len)
17477bbb2b0SAntti Palosaari {
17563bdab5dSAntti Palosaari 	struct platform_device *pdev = dev->pdev;
17663bdab5dSAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
17763bdab5dSAntti Palosaari 	struct i2c_client *client = pdata->i2c_client;
17877bbb2b0SAntti Palosaari 
17963bdab5dSAntti Palosaari 	return pdata->bulk_read(client, reg, val, len);
18077bbb2b0SAntti Palosaari }
18163bdab5dSAntti Palosaari #endif
18277bbb2b0SAntti Palosaari 
18377bbb2b0SAntti Palosaari /* write single register */
184725b7f30SAntti Palosaari static int rtl2832_sdr_wr_reg(struct rtl2832_sdr_dev *dev, u16 reg, u8 val)
18577bbb2b0SAntti Palosaari {
186725b7f30SAntti Palosaari 	return rtl2832_sdr_wr_regs(dev, reg, &val, 1);
18777bbb2b0SAntti Palosaari }
18877bbb2b0SAntti Palosaari 
18977bbb2b0SAntti Palosaari /* write single register with mask */
190725b7f30SAntti Palosaari static int rtl2832_sdr_wr_reg_mask(struct rtl2832_sdr_dev *dev, u16 reg,
19177bbb2b0SAntti Palosaari 		u8 val, u8 mask)
19277bbb2b0SAntti Palosaari {
19363bdab5dSAntti Palosaari 	struct platform_device *pdev = dev->pdev;
19463bdab5dSAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
19563bdab5dSAntti Palosaari 	struct i2c_client *client = pdata->i2c_client;
19677bbb2b0SAntti Palosaari 
19763bdab5dSAntti Palosaari 	return pdata->update_bits(client, reg, mask, val);
19877bbb2b0SAntti Palosaari }
19977bbb2b0SAntti Palosaari 
20077bbb2b0SAntti Palosaari /* Private functions */
20177bbb2b0SAntti Palosaari static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
202725b7f30SAntti Palosaari 		struct rtl2832_sdr_dev *dev)
20377bbb2b0SAntti Palosaari {
204abfc8d66SAntti Palosaari 	unsigned long flags;
20577bbb2b0SAntti Palosaari 	struct rtl2832_sdr_frame_buf *buf = NULL;
20677bbb2b0SAntti Palosaari 
207725b7f30SAntti Palosaari 	spin_lock_irqsave(&dev->queued_bufs_lock, flags);
208725b7f30SAntti Palosaari 	if (list_empty(&dev->queued_bufs))
20977bbb2b0SAntti Palosaari 		goto leave;
21077bbb2b0SAntti Palosaari 
211725b7f30SAntti Palosaari 	buf = list_entry(dev->queued_bufs.next,
21277bbb2b0SAntti Palosaari 			struct rtl2832_sdr_frame_buf, list);
21377bbb2b0SAntti Palosaari 	list_del(&buf->list);
21477bbb2b0SAntti Palosaari leave:
215725b7f30SAntti Palosaari 	spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
21677bbb2b0SAntti Palosaari 	return buf;
21777bbb2b0SAntti Palosaari }
21877bbb2b0SAntti Palosaari 
219725b7f30SAntti Palosaari static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
22077bbb2b0SAntti Palosaari 		void *dst, const u8 *src, unsigned int src_len)
22177bbb2b0SAntti Palosaari {
222e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
22377bbb2b0SAntti Palosaari 	unsigned int dst_len;
22477bbb2b0SAntti Palosaari 
225725b7f30SAntti Palosaari 	if (dev->pixelformat ==  V4L2_SDR_FMT_CU8) {
22677bbb2b0SAntti Palosaari 		/* native stream, no need to convert */
22777bbb2b0SAntti Palosaari 		memcpy(dst, src, src_len);
22877bbb2b0SAntti Palosaari 		dst_len = src_len;
229725b7f30SAntti Palosaari 	} else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
23077bbb2b0SAntti Palosaari 		/* convert u8 to u16 */
23177bbb2b0SAntti Palosaari 		unsigned int i;
23277bbb2b0SAntti Palosaari 		u16 *u16dst = dst;
23377bbb2b0SAntti Palosaari 
23477bbb2b0SAntti Palosaari 		for (i = 0; i < src_len; i++)
23577bbb2b0SAntti Palosaari 			*u16dst++ = (src[i] << 8) | (src[i] >> 0);
23677bbb2b0SAntti Palosaari 		dst_len = 2 * src_len;
23777bbb2b0SAntti Palosaari 	} else {
23877bbb2b0SAntti Palosaari 		dst_len = 0;
23977bbb2b0SAntti Palosaari 	}
24077bbb2b0SAntti Palosaari 
241b538a8e8SAntti Palosaari 	/* calculate sample rate and output it in 10 seconds intervals */
242725b7f30SAntti Palosaari 	if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
24377bbb2b0SAntti Palosaari 		#define MSECS 10000UL
244b538a8e8SAntti Palosaari 		unsigned int msecs = jiffies_to_msecs(jiffies -
245725b7f30SAntti Palosaari 				dev->jiffies_next + msecs_to_jiffies(MSECS));
246725b7f30SAntti Palosaari 		unsigned int samples = dev->sample - dev->sample_measured;
24777bbb2b0SAntti Palosaari 
248725b7f30SAntti Palosaari 		dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
249725b7f30SAntti Palosaari 		dev->sample_measured = dev->sample;
250e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev,
251b538a8e8SAntti Palosaari 			"slen=%u samples=%u msecs=%u sample rate=%lu\n",
252e4e78693SAntti Palosaari 			src_len, samples, msecs, samples * 1000UL / msecs);
25377bbb2b0SAntti Palosaari 	}
25477bbb2b0SAntti Palosaari 
25577bbb2b0SAntti Palosaari 	/* total number of I+Q pairs */
256725b7f30SAntti Palosaari 	dev->sample += src_len / 2;
25777bbb2b0SAntti Palosaari 
25877bbb2b0SAntti Palosaari 	return dst_len;
25977bbb2b0SAntti Palosaari }
26077bbb2b0SAntti Palosaari 
26177bbb2b0SAntti Palosaari /*
26277bbb2b0SAntti Palosaari  * This gets called for the bulk stream pipe. This is done in interrupt
26377bbb2b0SAntti Palosaari  * time, so it has to be fast, not crash, and not stall. Neat.
26477bbb2b0SAntti Palosaari  */
26577bbb2b0SAntti Palosaari static void rtl2832_sdr_urb_complete(struct urb *urb)
26677bbb2b0SAntti Palosaari {
267725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = urb->context;
268e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
26977bbb2b0SAntti Palosaari 	struct rtl2832_sdr_frame_buf *fbuf;
27077bbb2b0SAntti Palosaari 
271e4e78693SAntti Palosaari 	dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
272d4d20500SAntti Palosaari 			    urb->status, urb->actual_length,
27377bbb2b0SAntti Palosaari 			    urb->transfer_buffer_length, urb->error_count);
27477bbb2b0SAntti Palosaari 
27577bbb2b0SAntti Palosaari 	switch (urb->status) {
27677bbb2b0SAntti Palosaari 	case 0:             /* success */
27777bbb2b0SAntti Palosaari 	case -ETIMEDOUT:    /* NAK */
27877bbb2b0SAntti Palosaari 		break;
27977bbb2b0SAntti Palosaari 	case -ECONNRESET:   /* kill */
28077bbb2b0SAntti Palosaari 	case -ENOENT:
28177bbb2b0SAntti Palosaari 	case -ESHUTDOWN:
28277bbb2b0SAntti Palosaari 		return;
28377bbb2b0SAntti Palosaari 	default:            /* error */
284e4e78693SAntti Palosaari 		dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
28577bbb2b0SAntti Palosaari 		break;
28677bbb2b0SAntti Palosaari 	}
28777bbb2b0SAntti Palosaari 
28877bbb2b0SAntti Palosaari 	if (likely(urb->actual_length > 0)) {
28977bbb2b0SAntti Palosaari 		void *ptr;
29077bbb2b0SAntti Palosaari 		unsigned int len;
29177bbb2b0SAntti Palosaari 		/* get free framebuffer */
292725b7f30SAntti Palosaari 		fbuf = rtl2832_sdr_get_next_fill_buf(dev);
29377bbb2b0SAntti Palosaari 		if (unlikely(fbuf == NULL)) {
294725b7f30SAntti Palosaari 			dev->vb_full++;
295e4e78693SAntti Palosaari 			dev_notice_ratelimited(&pdev->dev,
29677bbb2b0SAntti Palosaari 					       "videobuf is full, %d packets dropped\n",
297725b7f30SAntti Palosaari 					       dev->vb_full);
29877bbb2b0SAntti Palosaari 			goto skip;
29977bbb2b0SAntti Palosaari 		}
30077bbb2b0SAntti Palosaari 
30177bbb2b0SAntti Palosaari 		/* fill framebuffer */
30277bbb2b0SAntti Palosaari 		ptr = vb2_plane_vaddr(&fbuf->vb, 0);
303725b7f30SAntti Palosaari 		len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
30477bbb2b0SAntti Palosaari 				urb->actual_length);
30577bbb2b0SAntti Palosaari 		vb2_set_plane_payload(&fbuf->vb, 0, len);
30677bbb2b0SAntti Palosaari 		v4l2_get_timestamp(&fbuf->vb.v4l2_buf.timestamp);
307725b7f30SAntti Palosaari 		fbuf->vb.v4l2_buf.sequence = dev->sequence++;
30877bbb2b0SAntti Palosaari 		vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
30977bbb2b0SAntti Palosaari 	}
31077bbb2b0SAntti Palosaari skip:
31177bbb2b0SAntti Palosaari 	usb_submit_urb(urb, GFP_ATOMIC);
31277bbb2b0SAntti Palosaari }
31377bbb2b0SAntti Palosaari 
314725b7f30SAntti Palosaari static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
31577bbb2b0SAntti Palosaari {
316e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
31777bbb2b0SAntti Palosaari 	int i;
31877bbb2b0SAntti Palosaari 
319725b7f30SAntti Palosaari 	for (i = dev->urbs_submitted - 1; i >= 0; i--) {
320e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev, "kill urb=%d\n", i);
32177bbb2b0SAntti Palosaari 		/* stop the URB */
322725b7f30SAntti Palosaari 		usb_kill_urb(dev->urb_list[i]);
32377bbb2b0SAntti Palosaari 	}
324725b7f30SAntti Palosaari 	dev->urbs_submitted = 0;
32577bbb2b0SAntti Palosaari 
32677bbb2b0SAntti Palosaari 	return 0;
32777bbb2b0SAntti Palosaari }
32877bbb2b0SAntti Palosaari 
329725b7f30SAntti Palosaari static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
33077bbb2b0SAntti Palosaari {
331e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
33277bbb2b0SAntti Palosaari 	int i, ret;
33377bbb2b0SAntti Palosaari 
334725b7f30SAntti Palosaari 	for (i = 0; i < dev->urbs_initialized; i++) {
335e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev, "submit urb=%d\n", i);
336725b7f30SAntti Palosaari 		ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
33777bbb2b0SAntti Palosaari 		if (ret) {
338e4e78693SAntti Palosaari 			dev_err(&pdev->dev,
33977bbb2b0SAntti Palosaari 				"Could not submit urb no. %d - get them all back\n",
34077bbb2b0SAntti Palosaari 				i);
341725b7f30SAntti Palosaari 			rtl2832_sdr_kill_urbs(dev);
34277bbb2b0SAntti Palosaari 			return ret;
34377bbb2b0SAntti Palosaari 		}
344725b7f30SAntti Palosaari 		dev->urbs_submitted++;
34577bbb2b0SAntti Palosaari 	}
34677bbb2b0SAntti Palosaari 
34777bbb2b0SAntti Palosaari 	return 0;
34877bbb2b0SAntti Palosaari }
34977bbb2b0SAntti Palosaari 
350725b7f30SAntti Palosaari static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
35177bbb2b0SAntti Palosaari {
352e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
353*3bda2bf1SAntti Palosaari 
354725b7f30SAntti Palosaari 	if (dev->flags & USB_STATE_URB_BUF) {
355725b7f30SAntti Palosaari 		while (dev->buf_num) {
356725b7f30SAntti Palosaari 			dev->buf_num--;
357e4e78693SAntti Palosaari 			dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
358725b7f30SAntti Palosaari 			usb_free_coherent(dev->udev, dev->buf_size,
359725b7f30SAntti Palosaari 					  dev->buf_list[dev->buf_num],
360725b7f30SAntti Palosaari 					  dev->dma_addr[dev->buf_num]);
36177bbb2b0SAntti Palosaari 		}
36277bbb2b0SAntti Palosaari 	}
363725b7f30SAntti Palosaari 	dev->flags &= ~USB_STATE_URB_BUF;
36477bbb2b0SAntti Palosaari 
36577bbb2b0SAntti Palosaari 	return 0;
36677bbb2b0SAntti Palosaari }
36777bbb2b0SAntti Palosaari 
368725b7f30SAntti Palosaari static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
36977bbb2b0SAntti Palosaari {
370e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
371*3bda2bf1SAntti Palosaari 
372725b7f30SAntti Palosaari 	dev->buf_num = 0;
373725b7f30SAntti Palosaari 	dev->buf_size = BULK_BUFFER_SIZE;
37477bbb2b0SAntti Palosaari 
375e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
376d4d20500SAntti Palosaari 		MAX_BULK_BUFS * BULK_BUFFER_SIZE);
37777bbb2b0SAntti Palosaari 
378725b7f30SAntti Palosaari 	for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
379725b7f30SAntti Palosaari 		dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
38077bbb2b0SAntti Palosaari 				BULK_BUFFER_SIZE, GFP_ATOMIC,
381725b7f30SAntti Palosaari 				&dev->dma_addr[dev->buf_num]);
382725b7f30SAntti Palosaari 		if (!dev->buf_list[dev->buf_num]) {
383e4e78693SAntti Palosaari 			dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
384725b7f30SAntti Palosaari 				dev->buf_num);
385725b7f30SAntti Palosaari 			rtl2832_sdr_free_stream_bufs(dev);
38677bbb2b0SAntti Palosaari 			return -ENOMEM;
38777bbb2b0SAntti Palosaari 		}
38877bbb2b0SAntti Palosaari 
389e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
390725b7f30SAntti Palosaari 			dev->buf_num, dev->buf_list[dev->buf_num],
391725b7f30SAntti Palosaari 			(long long)dev->dma_addr[dev->buf_num]);
392725b7f30SAntti Palosaari 		dev->flags |= USB_STATE_URB_BUF;
39377bbb2b0SAntti Palosaari 	}
39477bbb2b0SAntti Palosaari 
39577bbb2b0SAntti Palosaari 	return 0;
39677bbb2b0SAntti Palosaari }
39777bbb2b0SAntti Palosaari 
398725b7f30SAntti Palosaari static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
39977bbb2b0SAntti Palosaari {
400e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
40177bbb2b0SAntti Palosaari 	int i;
40277bbb2b0SAntti Palosaari 
403725b7f30SAntti Palosaari 	rtl2832_sdr_kill_urbs(dev);
40477bbb2b0SAntti Palosaari 
405725b7f30SAntti Palosaari 	for (i = dev->urbs_initialized - 1; i >= 0; i--) {
406725b7f30SAntti Palosaari 		if (dev->urb_list[i]) {
407e4e78693SAntti Palosaari 			dev_dbg(&pdev->dev, "free urb=%d\n", i);
40877bbb2b0SAntti Palosaari 			/* free the URBs */
409725b7f30SAntti Palosaari 			usb_free_urb(dev->urb_list[i]);
41077bbb2b0SAntti Palosaari 		}
41177bbb2b0SAntti Palosaari 	}
412725b7f30SAntti Palosaari 	dev->urbs_initialized = 0;
41377bbb2b0SAntti Palosaari 
41477bbb2b0SAntti Palosaari 	return 0;
41577bbb2b0SAntti Palosaari }
41677bbb2b0SAntti Palosaari 
417725b7f30SAntti Palosaari static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
41877bbb2b0SAntti Palosaari {
419e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
42077bbb2b0SAntti Palosaari 	int i, j;
42177bbb2b0SAntti Palosaari 
42277bbb2b0SAntti Palosaari 	/* allocate the URBs */
42377bbb2b0SAntti Palosaari 	for (i = 0; i < MAX_BULK_BUFS; i++) {
424e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
425725b7f30SAntti Palosaari 		dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
426725b7f30SAntti Palosaari 		if (!dev->urb_list[i]) {
427e4e78693SAntti Palosaari 			dev_dbg(&pdev->dev, "failed\n");
42877bbb2b0SAntti Palosaari 			for (j = 0; j < i; j++)
429725b7f30SAntti Palosaari 				usb_free_urb(dev->urb_list[j]);
43077bbb2b0SAntti Palosaari 			return -ENOMEM;
43177bbb2b0SAntti Palosaari 		}
432725b7f30SAntti Palosaari 		usb_fill_bulk_urb(dev->urb_list[i],
433725b7f30SAntti Palosaari 				dev->udev,
434725b7f30SAntti Palosaari 				usb_rcvbulkpipe(dev->udev, 0x81),
435725b7f30SAntti Palosaari 				dev->buf_list[i],
43677bbb2b0SAntti Palosaari 				BULK_BUFFER_SIZE,
437725b7f30SAntti Palosaari 				rtl2832_sdr_urb_complete, dev);
43877bbb2b0SAntti Palosaari 
439725b7f30SAntti Palosaari 		dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
440725b7f30SAntti Palosaari 		dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
441725b7f30SAntti Palosaari 		dev->urbs_initialized++;
44277bbb2b0SAntti Palosaari 	}
44377bbb2b0SAntti Palosaari 
44477bbb2b0SAntti Palosaari 	return 0;
44577bbb2b0SAntti Palosaari }
44677bbb2b0SAntti Palosaari 
44777bbb2b0SAntti Palosaari /* Must be called with vb_queue_lock hold */
448725b7f30SAntti Palosaari static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
44977bbb2b0SAntti Palosaari {
450e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
451abfc8d66SAntti Palosaari 	unsigned long flags;
45277bbb2b0SAntti Palosaari 
453e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
45477bbb2b0SAntti Palosaari 
455725b7f30SAntti Palosaari 	spin_lock_irqsave(&dev->queued_bufs_lock, flags);
456725b7f30SAntti Palosaari 	while (!list_empty(&dev->queued_bufs)) {
45777bbb2b0SAntti Palosaari 		struct rtl2832_sdr_frame_buf *buf;
45877bbb2b0SAntti Palosaari 
459725b7f30SAntti Palosaari 		buf = list_entry(dev->queued_bufs.next,
46077bbb2b0SAntti Palosaari 				struct rtl2832_sdr_frame_buf, list);
46177bbb2b0SAntti Palosaari 		list_del(&buf->list);
46277bbb2b0SAntti Palosaari 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
46377bbb2b0SAntti Palosaari 	}
464725b7f30SAntti Palosaari 	spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
46577bbb2b0SAntti Palosaari }
46677bbb2b0SAntti Palosaari 
46777bbb2b0SAntti Palosaari static int rtl2832_sdr_querycap(struct file *file, void *fh,
46877bbb2b0SAntti Palosaari 		struct v4l2_capability *cap)
46977bbb2b0SAntti Palosaari {
470725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
471e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
47277bbb2b0SAntti Palosaari 
473e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
47477bbb2b0SAntti Palosaari 
47577bbb2b0SAntti Palosaari 	strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
476725b7f30SAntti Palosaari 	strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
477725b7f30SAntti Palosaari 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
47877bbb2b0SAntti Palosaari 	cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
47977bbb2b0SAntti Palosaari 			V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
48077bbb2b0SAntti Palosaari 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
48177bbb2b0SAntti Palosaari 	return 0;
48277bbb2b0SAntti Palosaari }
48377bbb2b0SAntti Palosaari 
48477bbb2b0SAntti Palosaari /* Videobuf2 operations */
48577bbb2b0SAntti Palosaari static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
48677bbb2b0SAntti Palosaari 		const struct v4l2_format *fmt, unsigned int *nbuffers,
48777bbb2b0SAntti Palosaari 		unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
48877bbb2b0SAntti Palosaari {
489725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
490e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
49177bbb2b0SAntti Palosaari 
492e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
49377bbb2b0SAntti Palosaari 
49477bbb2b0SAntti Palosaari 	/* Need at least 8 buffers */
49577bbb2b0SAntti Palosaari 	if (vq->num_buffers + *nbuffers < 8)
49677bbb2b0SAntti Palosaari 		*nbuffers = 8 - vq->num_buffers;
49777bbb2b0SAntti Palosaari 	*nplanes = 1;
498725b7f30SAntti Palosaari 	sizes[0] = PAGE_ALIGN(dev->buffersize);
499e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
50077bbb2b0SAntti Palosaari 	return 0;
50177bbb2b0SAntti Palosaari }
50277bbb2b0SAntti Palosaari 
50377bbb2b0SAntti Palosaari static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
50477bbb2b0SAntti Palosaari {
505725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
50677bbb2b0SAntti Palosaari 
50777bbb2b0SAntti Palosaari 	/* Don't allow queing new buffers after device disconnection */
508725b7f30SAntti Palosaari 	if (!dev->udev)
50977bbb2b0SAntti Palosaari 		return -ENODEV;
51077bbb2b0SAntti Palosaari 
51177bbb2b0SAntti Palosaari 	return 0;
51277bbb2b0SAntti Palosaari }
51377bbb2b0SAntti Palosaari 
51477bbb2b0SAntti Palosaari static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
51577bbb2b0SAntti Palosaari {
516725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
51777bbb2b0SAntti Palosaari 	struct rtl2832_sdr_frame_buf *buf =
51877bbb2b0SAntti Palosaari 			container_of(vb, struct rtl2832_sdr_frame_buf, vb);
519abfc8d66SAntti Palosaari 	unsigned long flags;
52077bbb2b0SAntti Palosaari 
52177bbb2b0SAntti Palosaari 	/* Check the device has not disconnected between prep and queuing */
522725b7f30SAntti Palosaari 	if (!dev->udev) {
52377bbb2b0SAntti Palosaari 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
52477bbb2b0SAntti Palosaari 		return;
52577bbb2b0SAntti Palosaari 	}
52677bbb2b0SAntti Palosaari 
527725b7f30SAntti Palosaari 	spin_lock_irqsave(&dev->queued_bufs_lock, flags);
528725b7f30SAntti Palosaari 	list_add_tail(&buf->list, &dev->queued_bufs);
529725b7f30SAntti Palosaari 	spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
53077bbb2b0SAntti Palosaari }
53177bbb2b0SAntti Palosaari 
532725b7f30SAntti Palosaari static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
53377bbb2b0SAntti Palosaari {
53463bdab5dSAntti Palosaari 	struct platform_device *pdev = dev->pdev;
53563bdab5dSAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
536*3bda2bf1SAntti Palosaari 	struct dvb_frontend *fe = pdata->dvb_frontend;
53777bbb2b0SAntti Palosaari 	int ret;
53877bbb2b0SAntti Palosaari 	unsigned int f_sr, f_if;
53977bbb2b0SAntti Palosaari 	u8 buf[4], u8tmp1, u8tmp2;
54077bbb2b0SAntti Palosaari 	u64 u64tmp;
54177bbb2b0SAntti Palosaari 	u32 u32tmp;
54277bbb2b0SAntti Palosaari 
543e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
54477bbb2b0SAntti Palosaari 
545725b7f30SAntti Palosaari 	if (!test_bit(POWER_ON, &dev->flags))
54677bbb2b0SAntti Palosaari 		return 0;
54777bbb2b0SAntti Palosaari 
548725b7f30SAntti Palosaari 	if (dev->f_adc == 0)
54977bbb2b0SAntti Palosaari 		return 0;
55077bbb2b0SAntti Palosaari 
551725b7f30SAntti Palosaari 	f_sr = dev->f_adc;
55277bbb2b0SAntti Palosaari 
553725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x00\x00", 2);
55477bbb2b0SAntti Palosaari 	if (ret)
55577bbb2b0SAntti Palosaari 		goto err;
55677bbb2b0SAntti Palosaari 
557725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x00\x00\x00\x00", 4);
55877bbb2b0SAntti Palosaari 	if (ret)
55977bbb2b0SAntti Palosaari 		goto err;
56077bbb2b0SAntti Palosaari 
56177bbb2b0SAntti Palosaari 	/* get IF from tuner */
56277bbb2b0SAntti Palosaari 	if (fe->ops.tuner_ops.get_if_frequency)
56377bbb2b0SAntti Palosaari 		ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
56477bbb2b0SAntti Palosaari 	else
56577bbb2b0SAntti Palosaari 		ret = -EINVAL;
56677bbb2b0SAntti Palosaari 
56777bbb2b0SAntti Palosaari 	if (ret)
56877bbb2b0SAntti Palosaari 		goto err;
56977bbb2b0SAntti Palosaari 
57077bbb2b0SAntti Palosaari 	/* program IF */
57163bdab5dSAntti Palosaari 	u64tmp = f_if % pdata->clk;
57277bbb2b0SAntti Palosaari 	u64tmp *= 0x400000;
57363bdab5dSAntti Palosaari 	u64tmp = div_u64(u64tmp, pdata->clk);
57477bbb2b0SAntti Palosaari 	u64tmp = -u64tmp;
57577bbb2b0SAntti Palosaari 	u32tmp = u64tmp & 0x3fffff;
57677bbb2b0SAntti Palosaari 
577e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
57877bbb2b0SAntti Palosaari 
57977bbb2b0SAntti Palosaari 	buf[0] = (u32tmp >> 16) & 0xff;
58077bbb2b0SAntti Palosaari 	buf[1] = (u32tmp >>  8) & 0xff;
58177bbb2b0SAntti Palosaari 	buf[2] = (u32tmp >>  0) & 0xff;
58277bbb2b0SAntti Palosaari 
583725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x119, buf, 3);
58477bbb2b0SAntti Palosaari 	if (ret)
58577bbb2b0SAntti Palosaari 		goto err;
58677bbb2b0SAntti Palosaari 
58777bbb2b0SAntti Palosaari 	/* BB / IF mode */
58877bbb2b0SAntti Palosaari 	/* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
58977bbb2b0SAntti Palosaari 	if (f_if) {
59077bbb2b0SAntti Palosaari 		u8tmp1 = 0x1a; /* disable Zero-IF */
59177bbb2b0SAntti Palosaari 		u8tmp2 = 0x8d; /* enable ADC I */
59277bbb2b0SAntti Palosaari 	} else {
59377bbb2b0SAntti Palosaari 		u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
59477bbb2b0SAntti Palosaari 		u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
59577bbb2b0SAntti Palosaari 	}
59677bbb2b0SAntti Palosaari 
597725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_reg(dev, 0x1b1, u8tmp1);
59877bbb2b0SAntti Palosaari 	if (ret)
59977bbb2b0SAntti Palosaari 		goto err;
60077bbb2b0SAntti Palosaari 
601725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_reg(dev, 0x008, u8tmp2);
60277bbb2b0SAntti Palosaari 	if (ret)
60377bbb2b0SAntti Palosaari 		goto err;
60477bbb2b0SAntti Palosaari 
605725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_reg(dev, 0x006, 0x80);
60677bbb2b0SAntti Palosaari 	if (ret)
60777bbb2b0SAntti Palosaari 		goto err;
60877bbb2b0SAntti Palosaari 
60977bbb2b0SAntti Palosaari 	/* program sampling rate (resampling down) */
61063bdab5dSAntti Palosaari 	u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
61177bbb2b0SAntti Palosaari 	u32tmp <<= 2;
61277bbb2b0SAntti Palosaari 	buf[0] = (u32tmp >> 24) & 0xff;
61377bbb2b0SAntti Palosaari 	buf[1] = (u32tmp >> 16) & 0xff;
61477bbb2b0SAntti Palosaari 	buf[2] = (u32tmp >>  8) & 0xff;
61577bbb2b0SAntti Palosaari 	buf[3] = (u32tmp >>  0) & 0xff;
616725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x19f, buf, 4);
61777bbb2b0SAntti Palosaari 	if (ret)
61877bbb2b0SAntti Palosaari 		goto err;
61977bbb2b0SAntti Palosaari 
62077bbb2b0SAntti Palosaari 	/* low-pass filter */
621725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x11c,
62277bbb2b0SAntti Palosaari 			"\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
62377bbb2b0SAntti Palosaari 			20);
62477bbb2b0SAntti Palosaari 	if (ret)
62577bbb2b0SAntti Palosaari 		goto err;
62677bbb2b0SAntti Palosaari 
627725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
62877bbb2b0SAntti Palosaari 	if (ret)
62977bbb2b0SAntti Palosaari 		goto err;
63077bbb2b0SAntti Palosaari 
63177bbb2b0SAntti Palosaari 	/* mode */
632725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x05", 1);
63377bbb2b0SAntti Palosaari 	if (ret)
63477bbb2b0SAntti Palosaari 		goto err;
63577bbb2b0SAntti Palosaari 
636725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x01a, "\x1b\x16\x0d\x06\x01\xff", 6);
63777bbb2b0SAntti Palosaari 	if (ret)
63877bbb2b0SAntti Palosaari 		goto err;
63977bbb2b0SAntti Palosaari 
64077bbb2b0SAntti Palosaari 	/* FSM */
641725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\xf0\x0f", 3);
64277bbb2b0SAntti Palosaari 	if (ret)
64377bbb2b0SAntti Palosaari 		goto err;
64477bbb2b0SAntti Palosaari 
64577bbb2b0SAntti Palosaari 	/* PID filter */
646725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x061, "\x60", 1);
64777bbb2b0SAntti Palosaari 	if (ret)
64877bbb2b0SAntti Palosaari 		goto err;
64977bbb2b0SAntti Palosaari 
65077bbb2b0SAntti Palosaari 	/* used RF tuner based settings */
65163bdab5dSAntti Palosaari 	switch (pdata->tuner) {
65263bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_E4000:
653725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
654725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
655725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
656725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x30", 1);
657725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xd0", 1);
658725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
659725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x18", 1);
660725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
661725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
662725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
663725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
664725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
665725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
666725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
667725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
668725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
669725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
670725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
671725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
672725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
673725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xd4", 1);
674725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
675725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
676725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
677725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x14", 1);
678725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xec", 1);
679725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
680725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
681725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
682725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x83", 1);
683725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x010, "\x49", 1);
684725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x87", 1);
685725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x85", 1);
686725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x013, "\x02", 1);
68777bbb2b0SAntti Palosaari 		break;
68863bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_FC0012:
68963bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_FC0013:
690725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
691725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
692725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
693725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x2c", 1);
694725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
695725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
696725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x16", 1);
697725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
698725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
699725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
700725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
701725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
702725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
703725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
704725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
705725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
706725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
707725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
708725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
709725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
710725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xe9\xbf", 2);
711725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
712725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
713725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
714725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x11", 1);
715725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xef", 1);
716725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
717725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
718725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
71977bbb2b0SAntti Palosaari 		break;
72063bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_R820T:
72163bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_R828D:
722725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
723725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
724725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x01", 1);
725725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x80", 1);
726725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x24", 1);
727725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
728725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
729725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x14", 1);
730725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
731725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
732725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
733725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
734725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
735725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
736725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
737725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
738725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
739725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
740725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
741725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
742725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
743725b7f30SAntti Palosaari 		ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xf4", 1);
74477bbb2b0SAntti Palosaari 		break;
74577bbb2b0SAntti Palosaari 	default:
746e4e78693SAntti Palosaari 		dev_notice(&pdev->dev, "Unsupported tuner\n");
74777bbb2b0SAntti Palosaari 	}
74877bbb2b0SAntti Palosaari 
74977bbb2b0SAntti Palosaari 	/* software reset */
750725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x04, 0x04);
75177bbb2b0SAntti Palosaari 	if (ret)
75277bbb2b0SAntti Palosaari 		goto err;
75377bbb2b0SAntti Palosaari 
754725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x00, 0x04);
75577bbb2b0SAntti Palosaari 	if (ret)
75677bbb2b0SAntti Palosaari 		goto err;
75777bbb2b0SAntti Palosaari err:
75877bbb2b0SAntti Palosaari 	return ret;
75977bbb2b0SAntti Palosaari };
76077bbb2b0SAntti Palosaari 
761725b7f30SAntti Palosaari static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
76277bbb2b0SAntti Palosaari {
763e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
76477bbb2b0SAntti Palosaari 	int ret;
76577bbb2b0SAntti Palosaari 
766e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
76777bbb2b0SAntti Palosaari 
76877bbb2b0SAntti Palosaari 	/* PID filter */
769725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x061, "\xe0", 1);
77077bbb2b0SAntti Palosaari 	if (ret)
77177bbb2b0SAntti Palosaari 		goto err;
77277bbb2b0SAntti Palosaari 
77377bbb2b0SAntti Palosaari 	/* mode */
774725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x20", 1);
77577bbb2b0SAntti Palosaari 	if (ret)
77677bbb2b0SAntti Palosaari 		goto err;
77777bbb2b0SAntti Palosaari 
778725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
77977bbb2b0SAntti Palosaari 	if (ret)
78077bbb2b0SAntti Palosaari 		goto err;
78177bbb2b0SAntti Palosaari 
78277bbb2b0SAntti Palosaari 	/* FSM */
783725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\x0f\xff", 3);
78477bbb2b0SAntti Palosaari 	if (ret)
78577bbb2b0SAntti Palosaari 		goto err;
78677bbb2b0SAntti Palosaari 
787725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x40\x00", 2);
78877bbb2b0SAntti Palosaari 	if (ret)
78977bbb2b0SAntti Palosaari 		goto err;
79077bbb2b0SAntti Palosaari 
791725b7f30SAntti Palosaari 	ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x06\x3f\xce\xcc", 4);
79277bbb2b0SAntti Palosaari 	if (ret)
79377bbb2b0SAntti Palosaari 		goto err;
79477bbb2b0SAntti Palosaari err:
79577bbb2b0SAntti Palosaari 	return;
79677bbb2b0SAntti Palosaari };
79777bbb2b0SAntti Palosaari 
798725b7f30SAntti Palosaari static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
79977bbb2b0SAntti Palosaari {
800e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
801*3bda2bf1SAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
802*3bda2bf1SAntti Palosaari 	struct dvb_frontend *fe = pdata->dvb_frontend;
80377bbb2b0SAntti Palosaari 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
80477bbb2b0SAntti Palosaari 	struct v4l2_ctrl *bandwidth_auto;
80577bbb2b0SAntti Palosaari 	struct v4l2_ctrl *bandwidth;
80677bbb2b0SAntti Palosaari 
80777bbb2b0SAntti Palosaari 	/*
80877bbb2b0SAntti Palosaari 	 * tuner RF (Hz)
80977bbb2b0SAntti Palosaari 	 */
810725b7f30SAntti Palosaari 	if (dev->f_tuner == 0)
81177bbb2b0SAntti Palosaari 		return 0;
81277bbb2b0SAntti Palosaari 
81377bbb2b0SAntti Palosaari 	/*
81477bbb2b0SAntti Palosaari 	 * bandwidth (Hz)
81577bbb2b0SAntti Palosaari 	 */
816725b7f30SAntti Palosaari 	bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
81777bbb2b0SAntti Palosaari 					V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
818725b7f30SAntti Palosaari 	bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
81977bbb2b0SAntti Palosaari 	if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
820725b7f30SAntti Palosaari 		c->bandwidth_hz = dev->f_adc;
821725b7f30SAntti Palosaari 		v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
82277bbb2b0SAntti Palosaari 	} else {
82377bbb2b0SAntti Palosaari 		c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
82477bbb2b0SAntti Palosaari 	}
82577bbb2b0SAntti Palosaari 
826725b7f30SAntti Palosaari 	c->frequency = dev->f_tuner;
82777bbb2b0SAntti Palosaari 	c->delivery_system = SYS_DVBT;
82877bbb2b0SAntti Palosaari 
829e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
830d4d20500SAntti Palosaari 		c->frequency, c->bandwidth_hz);
83177bbb2b0SAntti Palosaari 
832725b7f30SAntti Palosaari 	if (!test_bit(POWER_ON, &dev->flags))
83377bbb2b0SAntti Palosaari 		return 0;
83477bbb2b0SAntti Palosaari 
83577bbb2b0SAntti Palosaari 	if (fe->ops.tuner_ops.set_params)
83677bbb2b0SAntti Palosaari 		fe->ops.tuner_ops.set_params(fe);
83777bbb2b0SAntti Palosaari 
83877bbb2b0SAntti Palosaari 	return 0;
83977bbb2b0SAntti Palosaari };
84077bbb2b0SAntti Palosaari 
841725b7f30SAntti Palosaari static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
84277bbb2b0SAntti Palosaari {
843e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
844*3bda2bf1SAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
845*3bda2bf1SAntti Palosaari 	struct dvb_frontend *fe = pdata->dvb_frontend;
84677bbb2b0SAntti Palosaari 
847e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
84877bbb2b0SAntti Palosaari 
84977bbb2b0SAntti Palosaari 	if (fe->ops.tuner_ops.init)
85077bbb2b0SAntti Palosaari 		fe->ops.tuner_ops.init(fe);
85177bbb2b0SAntti Palosaari 
85277bbb2b0SAntti Palosaari 	return 0;
85377bbb2b0SAntti Palosaari };
85477bbb2b0SAntti Palosaari 
855725b7f30SAntti Palosaari static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
85677bbb2b0SAntti Palosaari {
857e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
858*3bda2bf1SAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
859*3bda2bf1SAntti Palosaari 	struct dvb_frontend *fe = pdata->dvb_frontend;
86077bbb2b0SAntti Palosaari 
861e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
86277bbb2b0SAntti Palosaari 
86377bbb2b0SAntti Palosaari 	if (fe->ops.tuner_ops.sleep)
86477bbb2b0SAntti Palosaari 		fe->ops.tuner_ops.sleep(fe);
86577bbb2b0SAntti Palosaari 
86677bbb2b0SAntti Palosaari 	return;
86777bbb2b0SAntti Palosaari };
86877bbb2b0SAntti Palosaari 
86977bbb2b0SAntti Palosaari static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
87077bbb2b0SAntti Palosaari {
871725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
872e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
873*3bda2bf1SAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
874*3bda2bf1SAntti Palosaari 	struct dvb_usb_device *d = pdata->dvb_usb_device;
87577bbb2b0SAntti Palosaari 	int ret;
87677bbb2b0SAntti Palosaari 
877e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
87877bbb2b0SAntti Palosaari 
879725b7f30SAntti Palosaari 	if (!dev->udev)
88077bbb2b0SAntti Palosaari 		return -ENODEV;
88177bbb2b0SAntti Palosaari 
882725b7f30SAntti Palosaari 	if (mutex_lock_interruptible(&dev->v4l2_lock))
88377bbb2b0SAntti Palosaari 		return -ERESTARTSYS;
88477bbb2b0SAntti Palosaari 
885*3bda2bf1SAntti Palosaari 	if (d->props->power_ctrl)
886*3bda2bf1SAntti Palosaari 		d->props->power_ctrl(d, 1);
88777bbb2b0SAntti Palosaari 
8882281c824SAntti Palosaari 	/* enable ADC */
889*3bda2bf1SAntti Palosaari 	if (d->props->frontend_ctrl)
890*3bda2bf1SAntti Palosaari 		d->props->frontend_ctrl(pdata->dvb_frontend, 1);
8912281c824SAntti Palosaari 
892725b7f30SAntti Palosaari 	set_bit(POWER_ON, &dev->flags);
89377bbb2b0SAntti Palosaari 
894725b7f30SAntti Palosaari 	ret = rtl2832_sdr_set_tuner(dev);
89577bbb2b0SAntti Palosaari 	if (ret)
89677bbb2b0SAntti Palosaari 		goto err;
89777bbb2b0SAntti Palosaari 
898725b7f30SAntti Palosaari 	ret = rtl2832_sdr_set_tuner_freq(dev);
89977bbb2b0SAntti Palosaari 	if (ret)
90077bbb2b0SAntti Palosaari 		goto err;
90177bbb2b0SAntti Palosaari 
902725b7f30SAntti Palosaari 	ret = rtl2832_sdr_set_adc(dev);
90377bbb2b0SAntti Palosaari 	if (ret)
90477bbb2b0SAntti Palosaari 		goto err;
90577bbb2b0SAntti Palosaari 
906725b7f30SAntti Palosaari 	ret = rtl2832_sdr_alloc_stream_bufs(dev);
90777bbb2b0SAntti Palosaari 	if (ret)
90877bbb2b0SAntti Palosaari 		goto err;
90977bbb2b0SAntti Palosaari 
910725b7f30SAntti Palosaari 	ret = rtl2832_sdr_alloc_urbs(dev);
91177bbb2b0SAntti Palosaari 	if (ret)
91277bbb2b0SAntti Palosaari 		goto err;
91377bbb2b0SAntti Palosaari 
914725b7f30SAntti Palosaari 	dev->sequence = 0;
91577bbb2b0SAntti Palosaari 
916725b7f30SAntti Palosaari 	ret = rtl2832_sdr_submit_urbs(dev);
91777bbb2b0SAntti Palosaari 	if (ret)
91877bbb2b0SAntti Palosaari 		goto err;
91977bbb2b0SAntti Palosaari 
92077bbb2b0SAntti Palosaari err:
921725b7f30SAntti Palosaari 	mutex_unlock(&dev->v4l2_lock);
92277bbb2b0SAntti Palosaari 
92377bbb2b0SAntti Palosaari 	return ret;
92477bbb2b0SAntti Palosaari }
92577bbb2b0SAntti Palosaari 
92677bbb2b0SAntti Palosaari static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
92777bbb2b0SAntti Palosaari {
928725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
929e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
930*3bda2bf1SAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
931*3bda2bf1SAntti Palosaari 	struct dvb_usb_device *d = pdata->dvb_usb_device;
93277bbb2b0SAntti Palosaari 
933e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
93477bbb2b0SAntti Palosaari 
935725b7f30SAntti Palosaari 	mutex_lock(&dev->v4l2_lock);
93677bbb2b0SAntti Palosaari 
937725b7f30SAntti Palosaari 	rtl2832_sdr_kill_urbs(dev);
938725b7f30SAntti Palosaari 	rtl2832_sdr_free_urbs(dev);
939725b7f30SAntti Palosaari 	rtl2832_sdr_free_stream_bufs(dev);
940725b7f30SAntti Palosaari 	rtl2832_sdr_cleanup_queued_bufs(dev);
941725b7f30SAntti Palosaari 	rtl2832_sdr_unset_adc(dev);
942725b7f30SAntti Palosaari 	rtl2832_sdr_unset_tuner(dev);
94377bbb2b0SAntti Palosaari 
944725b7f30SAntti Palosaari 	clear_bit(POWER_ON, &dev->flags);
94577bbb2b0SAntti Palosaari 
9462281c824SAntti Palosaari 	/* disable ADC */
947*3bda2bf1SAntti Palosaari 	if (d->props->frontend_ctrl)
948*3bda2bf1SAntti Palosaari 		d->props->frontend_ctrl(pdata->dvb_frontend, 0);
9492281c824SAntti Palosaari 
950*3bda2bf1SAntti Palosaari 	if (d->props->power_ctrl)
951*3bda2bf1SAntti Palosaari 		d->props->power_ctrl(d, 0);
95277bbb2b0SAntti Palosaari 
953725b7f30SAntti Palosaari 	mutex_unlock(&dev->v4l2_lock);
95477bbb2b0SAntti Palosaari }
95577bbb2b0SAntti Palosaari 
95677bbb2b0SAntti Palosaari static struct vb2_ops rtl2832_sdr_vb2_ops = {
95777bbb2b0SAntti Palosaari 	.queue_setup            = rtl2832_sdr_queue_setup,
95877bbb2b0SAntti Palosaari 	.buf_prepare            = rtl2832_sdr_buf_prepare,
95977bbb2b0SAntti Palosaari 	.buf_queue              = rtl2832_sdr_buf_queue,
96077bbb2b0SAntti Palosaari 	.start_streaming        = rtl2832_sdr_start_streaming,
96177bbb2b0SAntti Palosaari 	.stop_streaming         = rtl2832_sdr_stop_streaming,
96277bbb2b0SAntti Palosaari 	.wait_prepare           = vb2_ops_wait_prepare,
96377bbb2b0SAntti Palosaari 	.wait_finish            = vb2_ops_wait_finish,
96477bbb2b0SAntti Palosaari };
96577bbb2b0SAntti Palosaari 
96677bbb2b0SAntti Palosaari static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
96777bbb2b0SAntti Palosaari 		struct v4l2_tuner *v)
96877bbb2b0SAntti Palosaari {
969725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
970e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
97177bbb2b0SAntti Palosaari 
972e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
97377bbb2b0SAntti Palosaari 
97477bbb2b0SAntti Palosaari 	if (v->index == 0) {
97577bbb2b0SAntti Palosaari 		strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
97677bbb2b0SAntti Palosaari 		v->type = V4L2_TUNER_ADC;
97777bbb2b0SAntti Palosaari 		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
97877bbb2b0SAntti Palosaari 		v->rangelow =   300000;
97977bbb2b0SAntti Palosaari 		v->rangehigh = 3200000;
98077bbb2b0SAntti Palosaari 	} else if (v->index == 1) {
98177bbb2b0SAntti Palosaari 		strlcpy(v->name, "RF: <unknown>", sizeof(v->name));
98277bbb2b0SAntti Palosaari 		v->type = V4L2_TUNER_RF;
98377bbb2b0SAntti Palosaari 		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
98477bbb2b0SAntti Palosaari 		v->rangelow =    50000000;
98577bbb2b0SAntti Palosaari 		v->rangehigh = 2000000000;
98677bbb2b0SAntti Palosaari 	} else {
98777bbb2b0SAntti Palosaari 		return -EINVAL;
98877bbb2b0SAntti Palosaari 	}
98977bbb2b0SAntti Palosaari 
99077bbb2b0SAntti Palosaari 	return 0;
99177bbb2b0SAntti Palosaari }
99277bbb2b0SAntti Palosaari 
99377bbb2b0SAntti Palosaari static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
99477bbb2b0SAntti Palosaari 		const struct v4l2_tuner *v)
99577bbb2b0SAntti Palosaari {
996725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
997e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
99877bbb2b0SAntti Palosaari 
999e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
100077bbb2b0SAntti Palosaari 
100177bbb2b0SAntti Palosaari 	if (v->index > 1)
100277bbb2b0SAntti Palosaari 		return -EINVAL;
100377bbb2b0SAntti Palosaari 	return 0;
100477bbb2b0SAntti Palosaari }
100577bbb2b0SAntti Palosaari 
100677bbb2b0SAntti Palosaari static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
100777bbb2b0SAntti Palosaari 		struct v4l2_frequency_band *band)
100877bbb2b0SAntti Palosaari {
1009725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1010e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
101177bbb2b0SAntti Palosaari 
1012e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1013d4d20500SAntti Palosaari 		band->tuner, band->type, band->index);
101477bbb2b0SAntti Palosaari 
101577bbb2b0SAntti Palosaari 	if (band->tuner == 0) {
101677bbb2b0SAntti Palosaari 		if (band->index >= ARRAY_SIZE(bands_adc))
101777bbb2b0SAntti Palosaari 			return -EINVAL;
101877bbb2b0SAntti Palosaari 
101977bbb2b0SAntti Palosaari 		*band = bands_adc[band->index];
102077bbb2b0SAntti Palosaari 	} else if (band->tuner == 1) {
102177bbb2b0SAntti Palosaari 		if (band->index >= ARRAY_SIZE(bands_fm))
102277bbb2b0SAntti Palosaari 			return -EINVAL;
102377bbb2b0SAntti Palosaari 
102477bbb2b0SAntti Palosaari 		*band = bands_fm[band->index];
102577bbb2b0SAntti Palosaari 	} else {
102677bbb2b0SAntti Palosaari 		return -EINVAL;
102777bbb2b0SAntti Palosaari 	}
102877bbb2b0SAntti Palosaari 
102977bbb2b0SAntti Palosaari 	return 0;
103077bbb2b0SAntti Palosaari }
103177bbb2b0SAntti Palosaari 
103277bbb2b0SAntti Palosaari static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
103377bbb2b0SAntti Palosaari 		struct v4l2_frequency *f)
103477bbb2b0SAntti Palosaari {
1035725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1036e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
103777bbb2b0SAntti Palosaari 	int ret  = 0;
103877bbb2b0SAntti Palosaari 
1039e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
104077bbb2b0SAntti Palosaari 
104177bbb2b0SAntti Palosaari 	if (f->tuner == 0) {
1042725b7f30SAntti Palosaari 		f->frequency = dev->f_adc;
104377bbb2b0SAntti Palosaari 		f->type = V4L2_TUNER_ADC;
104477bbb2b0SAntti Palosaari 	} else if (f->tuner == 1) {
1045725b7f30SAntti Palosaari 		f->frequency = dev->f_tuner;
104677bbb2b0SAntti Palosaari 		f->type = V4L2_TUNER_RF;
104777bbb2b0SAntti Palosaari 	} else {
104877bbb2b0SAntti Palosaari 		return -EINVAL;
104977bbb2b0SAntti Palosaari 	}
105077bbb2b0SAntti Palosaari 
105177bbb2b0SAntti Palosaari 	return ret;
105277bbb2b0SAntti Palosaari }
105377bbb2b0SAntti Palosaari 
105477bbb2b0SAntti Palosaari static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
105577bbb2b0SAntti Palosaari 		const struct v4l2_frequency *f)
105677bbb2b0SAntti Palosaari {
1057725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1058e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
105977bbb2b0SAntti Palosaari 	int ret, band;
106077bbb2b0SAntti Palosaari 
1061e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1062d4d20500SAntti Palosaari 		f->tuner, f->type, f->frequency);
106377bbb2b0SAntti Palosaari 
106477bbb2b0SAntti Palosaari 	/* ADC band midpoints */
106577bbb2b0SAntti Palosaari 	#define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
106677bbb2b0SAntti Palosaari 	#define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
106777bbb2b0SAntti Palosaari 
106877bbb2b0SAntti Palosaari 	if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
106977bbb2b0SAntti Palosaari 		if (f->frequency < BAND_ADC_0)
107077bbb2b0SAntti Palosaari 			band = 0;
107177bbb2b0SAntti Palosaari 		else if (f->frequency < BAND_ADC_1)
107277bbb2b0SAntti Palosaari 			band = 1;
107377bbb2b0SAntti Palosaari 		else
107477bbb2b0SAntti Palosaari 			band = 2;
107577bbb2b0SAntti Palosaari 
1076725b7f30SAntti Palosaari 		dev->f_adc = clamp_t(unsigned int, f->frequency,
107777bbb2b0SAntti Palosaari 				bands_adc[band].rangelow,
107877bbb2b0SAntti Palosaari 				bands_adc[band].rangehigh);
107977bbb2b0SAntti Palosaari 
1080e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1081725b7f30SAntti Palosaari 		ret = rtl2832_sdr_set_adc(dev);
108277bbb2b0SAntti Palosaari 	} else if (f->tuner == 1) {
1083725b7f30SAntti Palosaari 		dev->f_tuner = clamp_t(unsigned int, f->frequency,
108477bbb2b0SAntti Palosaari 				bands_fm[0].rangelow,
108577bbb2b0SAntti Palosaari 				bands_fm[0].rangehigh);
1086e4e78693SAntti Palosaari 		dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
108777bbb2b0SAntti Palosaari 
1088725b7f30SAntti Palosaari 		ret = rtl2832_sdr_set_tuner_freq(dev);
108977bbb2b0SAntti Palosaari 	} else {
109077bbb2b0SAntti Palosaari 		ret = -EINVAL;
109177bbb2b0SAntti Palosaari 	}
109277bbb2b0SAntti Palosaari 
109377bbb2b0SAntti Palosaari 	return ret;
109477bbb2b0SAntti Palosaari }
109577bbb2b0SAntti Palosaari 
109677bbb2b0SAntti Palosaari static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
109777bbb2b0SAntti Palosaari 		struct v4l2_fmtdesc *f)
109877bbb2b0SAntti Palosaari {
1099725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1100e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
110177bbb2b0SAntti Palosaari 
1102e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
110377bbb2b0SAntti Palosaari 
1104725b7f30SAntti Palosaari 	if (f->index >= dev->num_formats)
110577bbb2b0SAntti Palosaari 		return -EINVAL;
110677bbb2b0SAntti Palosaari 
110777bbb2b0SAntti Palosaari 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
110877bbb2b0SAntti Palosaari 	f->pixelformat = formats[f->index].pixelformat;
110977bbb2b0SAntti Palosaari 
111077bbb2b0SAntti Palosaari 	return 0;
111177bbb2b0SAntti Palosaari }
111277bbb2b0SAntti Palosaari 
111377bbb2b0SAntti Palosaari static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
111477bbb2b0SAntti Palosaari 		struct v4l2_format *f)
111577bbb2b0SAntti Palosaari {
1116725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1117e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
111877bbb2b0SAntti Palosaari 
1119e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
112077bbb2b0SAntti Palosaari 
1121725b7f30SAntti Palosaari 	f->fmt.sdr.pixelformat = dev->pixelformat;
1122725b7f30SAntti Palosaari 	f->fmt.sdr.buffersize = dev->buffersize;
1123d57fe404SAntti Palosaari 
112477bbb2b0SAntti Palosaari 	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
112577bbb2b0SAntti Palosaari 
112677bbb2b0SAntti Palosaari 	return 0;
112777bbb2b0SAntti Palosaari }
112877bbb2b0SAntti Palosaari 
112977bbb2b0SAntti Palosaari static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
113077bbb2b0SAntti Palosaari 		struct v4l2_format *f)
113177bbb2b0SAntti Palosaari {
1132725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1133e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
1134725b7f30SAntti Palosaari 	struct vb2_queue *q = &dev->vb_queue;
113577bbb2b0SAntti Palosaari 	int i;
113677bbb2b0SAntti Palosaari 
1137e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
113877bbb2b0SAntti Palosaari 		(char *)&f->fmt.sdr.pixelformat);
113977bbb2b0SAntti Palosaari 
114077bbb2b0SAntti Palosaari 	if (vb2_is_busy(q))
114177bbb2b0SAntti Palosaari 		return -EBUSY;
114277bbb2b0SAntti Palosaari 
114377bbb2b0SAntti Palosaari 	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1144725b7f30SAntti Palosaari 	for (i = 0; i < dev->num_formats; i++) {
114577bbb2b0SAntti Palosaari 		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1146725b7f30SAntti Palosaari 			dev->pixelformat = formats[i].pixelformat;
1147725b7f30SAntti Palosaari 			dev->buffersize = formats[i].buffersize;
1148d57fe404SAntti Palosaari 			f->fmt.sdr.buffersize = formats[i].buffersize;
114977bbb2b0SAntti Palosaari 			return 0;
115077bbb2b0SAntti Palosaari 		}
115177bbb2b0SAntti Palosaari 	}
115277bbb2b0SAntti Palosaari 
1153725b7f30SAntti Palosaari 	dev->pixelformat = formats[0].pixelformat;
1154725b7f30SAntti Palosaari 	dev->buffersize = formats[0].buffersize;
1155d57fe404SAntti Palosaari 	f->fmt.sdr.pixelformat = formats[0].pixelformat;
1156d57fe404SAntti Palosaari 	f->fmt.sdr.buffersize = formats[0].buffersize;
115777bbb2b0SAntti Palosaari 
115877bbb2b0SAntti Palosaari 	return 0;
115977bbb2b0SAntti Palosaari }
116077bbb2b0SAntti Palosaari 
116177bbb2b0SAntti Palosaari static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
116277bbb2b0SAntti Palosaari 		struct v4l2_format *f)
116377bbb2b0SAntti Palosaari {
1164725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev = video_drvdata(file);
1165e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
116677bbb2b0SAntti Palosaari 	int i;
116777bbb2b0SAntti Palosaari 
1168e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
116977bbb2b0SAntti Palosaari 		(char *)&f->fmt.sdr.pixelformat);
117077bbb2b0SAntti Palosaari 
117177bbb2b0SAntti Palosaari 	memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1172725b7f30SAntti Palosaari 	for (i = 0; i < dev->num_formats; i++) {
1173d57fe404SAntti Palosaari 		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1174d57fe404SAntti Palosaari 			f->fmt.sdr.buffersize = formats[i].buffersize;
117577bbb2b0SAntti Palosaari 			return 0;
117677bbb2b0SAntti Palosaari 		}
1177d57fe404SAntti Palosaari 	}
117877bbb2b0SAntti Palosaari 
117977bbb2b0SAntti Palosaari 	f->fmt.sdr.pixelformat = formats[0].pixelformat;
1180d57fe404SAntti Palosaari 	f->fmt.sdr.buffersize = formats[0].buffersize;
118177bbb2b0SAntti Palosaari 
118277bbb2b0SAntti Palosaari 	return 0;
118377bbb2b0SAntti Palosaari }
118477bbb2b0SAntti Palosaari 
118577bbb2b0SAntti Palosaari static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
118677bbb2b0SAntti Palosaari 	.vidioc_querycap          = rtl2832_sdr_querycap,
118777bbb2b0SAntti Palosaari 
118877bbb2b0SAntti Palosaari 	.vidioc_enum_fmt_sdr_cap  = rtl2832_sdr_enum_fmt_sdr_cap,
118977bbb2b0SAntti Palosaari 	.vidioc_g_fmt_sdr_cap     = rtl2832_sdr_g_fmt_sdr_cap,
119077bbb2b0SAntti Palosaari 	.vidioc_s_fmt_sdr_cap     = rtl2832_sdr_s_fmt_sdr_cap,
119177bbb2b0SAntti Palosaari 	.vidioc_try_fmt_sdr_cap   = rtl2832_sdr_try_fmt_sdr_cap,
119277bbb2b0SAntti Palosaari 
119377bbb2b0SAntti Palosaari 	.vidioc_reqbufs           = vb2_ioctl_reqbufs,
119477bbb2b0SAntti Palosaari 	.vidioc_create_bufs       = vb2_ioctl_create_bufs,
119577bbb2b0SAntti Palosaari 	.vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
119677bbb2b0SAntti Palosaari 	.vidioc_querybuf          = vb2_ioctl_querybuf,
119777bbb2b0SAntti Palosaari 	.vidioc_qbuf              = vb2_ioctl_qbuf,
119877bbb2b0SAntti Palosaari 	.vidioc_dqbuf             = vb2_ioctl_dqbuf,
119977bbb2b0SAntti Palosaari 
120077bbb2b0SAntti Palosaari 	.vidioc_streamon          = vb2_ioctl_streamon,
120177bbb2b0SAntti Palosaari 	.vidioc_streamoff         = vb2_ioctl_streamoff,
120277bbb2b0SAntti Palosaari 
120377bbb2b0SAntti Palosaari 	.vidioc_g_tuner           = rtl2832_sdr_g_tuner,
120477bbb2b0SAntti Palosaari 	.vidioc_s_tuner           = rtl2832_sdr_s_tuner,
120577bbb2b0SAntti Palosaari 
120677bbb2b0SAntti Palosaari 	.vidioc_enum_freq_bands   = rtl2832_sdr_enum_freq_bands,
120777bbb2b0SAntti Palosaari 	.vidioc_g_frequency       = rtl2832_sdr_g_frequency,
120877bbb2b0SAntti Palosaari 	.vidioc_s_frequency       = rtl2832_sdr_s_frequency,
120977bbb2b0SAntti Palosaari 
121077bbb2b0SAntti Palosaari 	.vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
121177bbb2b0SAntti Palosaari 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
121277bbb2b0SAntti Palosaari 	.vidioc_log_status        = v4l2_ctrl_log_status,
121377bbb2b0SAntti Palosaari };
121477bbb2b0SAntti Palosaari 
121577bbb2b0SAntti Palosaari static const struct v4l2_file_operations rtl2832_sdr_fops = {
121677bbb2b0SAntti Palosaari 	.owner                    = THIS_MODULE,
121777bbb2b0SAntti Palosaari 	.open                     = v4l2_fh_open,
121877bbb2b0SAntti Palosaari 	.release                  = vb2_fop_release,
121977bbb2b0SAntti Palosaari 	.read                     = vb2_fop_read,
122077bbb2b0SAntti Palosaari 	.poll                     = vb2_fop_poll,
122177bbb2b0SAntti Palosaari 	.mmap                     = vb2_fop_mmap,
122277bbb2b0SAntti Palosaari 	.unlocked_ioctl           = video_ioctl2,
122377bbb2b0SAntti Palosaari };
122477bbb2b0SAntti Palosaari 
122577bbb2b0SAntti Palosaari static struct video_device rtl2832_sdr_template = {
122677bbb2b0SAntti Palosaari 	.name                     = "Realtek RTL2832 SDR",
122777bbb2b0SAntti Palosaari 	.release                  = video_device_release_empty,
122877bbb2b0SAntti Palosaari 	.fops                     = &rtl2832_sdr_fops,
122977bbb2b0SAntti Palosaari 	.ioctl_ops                = &rtl2832_sdr_ioctl_ops,
123077bbb2b0SAntti Palosaari };
123177bbb2b0SAntti Palosaari 
123277bbb2b0SAntti Palosaari static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
123377bbb2b0SAntti Palosaari {
1234725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev =
1235725b7f30SAntti Palosaari 			container_of(ctrl->handler, struct rtl2832_sdr_dev,
123677bbb2b0SAntti Palosaari 					hdl);
1237e4e78693SAntti Palosaari 	struct platform_device *pdev = dev->pdev;
1238*3bda2bf1SAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1239*3bda2bf1SAntti Palosaari 	struct dvb_frontend *fe = pdata->dvb_frontend;
124077bbb2b0SAntti Palosaari 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
124177bbb2b0SAntti Palosaari 	int ret;
124277bbb2b0SAntti Palosaari 
1243e4e78693SAntti Palosaari 	dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1244e4e78693SAntti Palosaari 		ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1245e4e78693SAntti Palosaari 		ctrl->step);
124677bbb2b0SAntti Palosaari 
124777bbb2b0SAntti Palosaari 	switch (ctrl->id) {
124877bbb2b0SAntti Palosaari 	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
124977bbb2b0SAntti Palosaari 	case V4L2_CID_RF_TUNER_BANDWIDTH:
125077bbb2b0SAntti Palosaari 		/* TODO: these controls should be moved to tuner drivers */
1251725b7f30SAntti Palosaari 		if (dev->bandwidth_auto->val) {
125277bbb2b0SAntti Palosaari 			/* Round towards the closest legal value */
1253725b7f30SAntti Palosaari 			s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
125477bbb2b0SAntti Palosaari 			u32 offset;
125577bbb2b0SAntti Palosaari 
1256725b7f30SAntti Palosaari 			val = clamp_t(s32, val, dev->bandwidth->minimum,
1257725b7f30SAntti Palosaari 				      dev->bandwidth->maximum);
1258725b7f30SAntti Palosaari 			offset = val - dev->bandwidth->minimum;
1259725b7f30SAntti Palosaari 			offset = dev->bandwidth->step *
1260725b7f30SAntti Palosaari 				div_u64(offset, dev->bandwidth->step);
1261725b7f30SAntti Palosaari 			dev->bandwidth->val = dev->bandwidth->minimum + offset;
126277bbb2b0SAntti Palosaari 		}
1263725b7f30SAntti Palosaari 		c->bandwidth_hz = dev->bandwidth->val;
126477bbb2b0SAntti Palosaari 
1265725b7f30SAntti Palosaari 		if (!test_bit(POWER_ON, &dev->flags))
126677bbb2b0SAntti Palosaari 			return 0;
126777bbb2b0SAntti Palosaari 
126877bbb2b0SAntti Palosaari 		if (fe->ops.tuner_ops.set_params)
126977bbb2b0SAntti Palosaari 			ret = fe->ops.tuner_ops.set_params(fe);
127077bbb2b0SAntti Palosaari 		else
127177bbb2b0SAntti Palosaari 			ret = 0;
127277bbb2b0SAntti Palosaari 		break;
127377bbb2b0SAntti Palosaari 	default:
127477bbb2b0SAntti Palosaari 		ret = -EINVAL;
127577bbb2b0SAntti Palosaari 	}
127677bbb2b0SAntti Palosaari 
127777bbb2b0SAntti Palosaari 	return ret;
127877bbb2b0SAntti Palosaari }
127977bbb2b0SAntti Palosaari 
128077bbb2b0SAntti Palosaari static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
128177bbb2b0SAntti Palosaari 	.s_ctrl = rtl2832_sdr_s_ctrl,
128277bbb2b0SAntti Palosaari };
128377bbb2b0SAntti Palosaari 
128477bbb2b0SAntti Palosaari static void rtl2832_sdr_video_release(struct v4l2_device *v)
128577bbb2b0SAntti Palosaari {
1286725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev =
1287725b7f30SAntti Palosaari 			container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
128863bdab5dSAntti Palosaari 	struct platform_device *pdev = dev->pdev;
128963bdab5dSAntti Palosaari 
129063bdab5dSAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
129177bbb2b0SAntti Palosaari 
1292725b7f30SAntti Palosaari 	v4l2_ctrl_handler_free(&dev->hdl);
1293725b7f30SAntti Palosaari 	v4l2_device_unregister(&dev->v4l2_dev);
1294725b7f30SAntti Palosaari 	kfree(dev);
129577bbb2b0SAntti Palosaari }
129677bbb2b0SAntti Palosaari 
129763bdab5dSAntti Palosaari /* Platform driver interface */
129863bdab5dSAntti Palosaari static int rtl2832_sdr_probe(struct platform_device *pdev)
129977bbb2b0SAntti Palosaari {
1300725b7f30SAntti Palosaari 	struct rtl2832_sdr_dev *dev;
130163bdab5dSAntti Palosaari 	struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
130277bbb2b0SAntti Palosaari 	const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
130363bdab5dSAntti Palosaari 	struct v4l2_subdev *subdev;
130463bdab5dSAntti Palosaari 	int ret;
130577bbb2b0SAntti Palosaari 
130663bdab5dSAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
130763bdab5dSAntti Palosaari 
130863bdab5dSAntti Palosaari 	if (!pdata) {
130963bdab5dSAntti Palosaari 		dev_err(&pdev->dev, "Cannot proceed without platform data\n");
131063bdab5dSAntti Palosaari 		ret = -EINVAL;
131163bdab5dSAntti Palosaari 		goto err;
131263bdab5dSAntti Palosaari 	}
131363bdab5dSAntti Palosaari 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1314725b7f30SAntti Palosaari 	if (dev == NULL) {
131563bdab5dSAntti Palosaari 		ret = -ENOMEM;
131663bdab5dSAntti Palosaari 		goto err;
131777bbb2b0SAntti Palosaari 	}
131877bbb2b0SAntti Palosaari 
131977bbb2b0SAntti Palosaari 	/* setup the state */
132063bdab5dSAntti Palosaari 	subdev = pdata->v4l2_subdev;
132163bdab5dSAntti Palosaari 	dev->pdev = pdev;
132263bdab5dSAntti Palosaari 	dev->udev = pdata->dvb_usb_device->udev;
1323725b7f30SAntti Palosaari 	dev->f_adc = bands_adc[0].rangelow;
1324725b7f30SAntti Palosaari 	dev->f_tuner = bands_fm[0].rangelow;
1325725b7f30SAntti Palosaari 	dev->pixelformat = formats[0].pixelformat;
1326725b7f30SAntti Palosaari 	dev->buffersize = formats[0].buffersize;
1327725b7f30SAntti Palosaari 	dev->num_formats = NUM_FORMATS;
13282fe15e20SMauro Carvalho Chehab 	if (!rtl2832_sdr_emulated_fmt)
1329725b7f30SAntti Palosaari 		dev->num_formats -= 1;
133077bbb2b0SAntti Palosaari 
1331725b7f30SAntti Palosaari 	mutex_init(&dev->v4l2_lock);
1332725b7f30SAntti Palosaari 	mutex_init(&dev->vb_queue_lock);
1333725b7f30SAntti Palosaari 	spin_lock_init(&dev->queued_bufs_lock);
1334725b7f30SAntti Palosaari 	INIT_LIST_HEAD(&dev->queued_bufs);
133577bbb2b0SAntti Palosaari 
133677bbb2b0SAntti Palosaari 	/* Init videobuf2 queue structure */
1337725b7f30SAntti Palosaari 	dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1338725b7f30SAntti Palosaari 	dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1339725b7f30SAntti Palosaari 	dev->vb_queue.drv_priv = dev;
1340725b7f30SAntti Palosaari 	dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1341725b7f30SAntti Palosaari 	dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1342725b7f30SAntti Palosaari 	dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1343725b7f30SAntti Palosaari 	dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1344725b7f30SAntti Palosaari 	ret = vb2_queue_init(&dev->vb_queue);
134577bbb2b0SAntti Palosaari 	if (ret) {
134663bdab5dSAntti Palosaari 		dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
134763bdab5dSAntti Palosaari 		goto err_kfree;
134877bbb2b0SAntti Palosaari 	}
134977bbb2b0SAntti Palosaari 
135077bbb2b0SAntti Palosaari 	/* Register controls */
135163bdab5dSAntti Palosaari 	switch (pdata->tuner) {
135263bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_E4000:
1353725b7f30SAntti Palosaari 		v4l2_ctrl_handler_init(&dev->hdl, 9);
135463bdab5dSAntti Palosaari 		if (subdev)
135563bdab5dSAntti Palosaari 			v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
135677bbb2b0SAntti Palosaari 		break;
135763bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_R820T:
135863bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_R828D:
1359725b7f30SAntti Palosaari 		v4l2_ctrl_handler_init(&dev->hdl, 2);
1360725b7f30SAntti Palosaari 		dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
136177bbb2b0SAntti Palosaari 							V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
136277bbb2b0SAntti Palosaari 							0, 1, 1, 1);
1363725b7f30SAntti Palosaari 		dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
136477bbb2b0SAntti Palosaari 						   V4L2_CID_RF_TUNER_BANDWIDTH,
136577bbb2b0SAntti Palosaari 						   0, 8000000, 100000, 0);
1366725b7f30SAntti Palosaari 		v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
136777bbb2b0SAntti Palosaari 		break;
136863bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_FC0012:
136963bdab5dSAntti Palosaari 	case RTL2832_SDR_TUNER_FC0013:
1370725b7f30SAntti Palosaari 		v4l2_ctrl_handler_init(&dev->hdl, 2);
1371725b7f30SAntti Palosaari 		dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
137277bbb2b0SAntti Palosaari 							V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
137377bbb2b0SAntti Palosaari 							0, 1, 1, 1);
1374725b7f30SAntti Palosaari 		dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
137577bbb2b0SAntti Palosaari 						   V4L2_CID_RF_TUNER_BANDWIDTH,
137677bbb2b0SAntti Palosaari 						   6000000, 8000000, 1000000,
137777bbb2b0SAntti Palosaari 						   6000000);
1378725b7f30SAntti Palosaari 		v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
137977bbb2b0SAntti Palosaari 		break;
138077bbb2b0SAntti Palosaari 	default:
1381725b7f30SAntti Palosaari 		v4l2_ctrl_handler_init(&dev->hdl, 0);
138263bdab5dSAntti Palosaari 		dev_err(&pdev->dev, "Unsupported tuner\n");
138363bdab5dSAntti Palosaari 		goto err_v4l2_ctrl_handler_free;
138477bbb2b0SAntti Palosaari 	}
1385725b7f30SAntti Palosaari 	if (dev->hdl.error) {
1386725b7f30SAntti Palosaari 		ret = dev->hdl.error;
138763bdab5dSAntti Palosaari 		dev_err(&pdev->dev, "Could not initialize controls\n");
138863bdab5dSAntti Palosaari 		goto err_v4l2_ctrl_handler_free;
138977bbb2b0SAntti Palosaari 	}
139077bbb2b0SAntti Palosaari 
139177bbb2b0SAntti Palosaari 	/* Init video_device structure */
1392725b7f30SAntti Palosaari 	dev->vdev = rtl2832_sdr_template;
1393725b7f30SAntti Palosaari 	dev->vdev.queue = &dev->vb_queue;
1394725b7f30SAntti Palosaari 	dev->vdev.queue->lock = &dev->vb_queue_lock;
1395725b7f30SAntti Palosaari 	video_set_drvdata(&dev->vdev, dev);
139677bbb2b0SAntti Palosaari 
139777bbb2b0SAntti Palosaari 	/* Register the v4l2_device structure */
1398725b7f30SAntti Palosaari 	dev->v4l2_dev.release = rtl2832_sdr_video_release;
1399e4e78693SAntti Palosaari 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
140077bbb2b0SAntti Palosaari 	if (ret) {
140163bdab5dSAntti Palosaari 		dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
140263bdab5dSAntti Palosaari 		goto err_v4l2_ctrl_handler_free;
140377bbb2b0SAntti Palosaari 	}
140477bbb2b0SAntti Palosaari 
1405725b7f30SAntti Palosaari 	dev->v4l2_dev.ctrl_handler = &dev->hdl;
1406725b7f30SAntti Palosaari 	dev->vdev.v4l2_dev = &dev->v4l2_dev;
1407725b7f30SAntti Palosaari 	dev->vdev.lock = &dev->v4l2_lock;
1408725b7f30SAntti Palosaari 	dev->vdev.vfl_dir = VFL_DIR_RX;
140977bbb2b0SAntti Palosaari 
1410725b7f30SAntti Palosaari 	ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
141177bbb2b0SAntti Palosaari 	if (ret) {
141263bdab5dSAntti Palosaari 		dev_err(&pdev->dev, "Failed to register as video device %d\n",
141377bbb2b0SAntti Palosaari 			ret);
141463bdab5dSAntti Palosaari 		goto err_v4l2_device_unregister;
141577bbb2b0SAntti Palosaari 	}
141663bdab5dSAntti Palosaari 	dev_info(&pdev->dev, "Registered as %s\n",
1417725b7f30SAntti Palosaari 		 video_device_node_name(&dev->vdev));
141863bdab5dSAntti Palosaari 	dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
141963bdab5dSAntti Palosaari 	dev_notice(&pdev->dev,
142063bdab5dSAntti Palosaari 		   "SDR API is still slightly experimental and functionality changes may follow\n");
142163bdab5dSAntti Palosaari 	platform_set_drvdata(pdev, dev);
142263bdab5dSAntti Palosaari 	return 0;
142363bdab5dSAntti Palosaari err_v4l2_device_unregister:
1424725b7f30SAntti Palosaari 	v4l2_device_unregister(&dev->v4l2_dev);
142563bdab5dSAntti Palosaari err_v4l2_ctrl_handler_free:
1426725b7f30SAntti Palosaari 	v4l2_ctrl_handler_free(&dev->hdl);
142763bdab5dSAntti Palosaari err_kfree:
1428725b7f30SAntti Palosaari 	kfree(dev);
142963bdab5dSAntti Palosaari err:
143063bdab5dSAntti Palosaari 	return ret;
143177bbb2b0SAntti Palosaari }
143263bdab5dSAntti Palosaari 
143363bdab5dSAntti Palosaari static int rtl2832_sdr_remove(struct platform_device *pdev)
143463bdab5dSAntti Palosaari {
143563bdab5dSAntti Palosaari 	struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
143663bdab5dSAntti Palosaari 
143763bdab5dSAntti Palosaari 	dev_dbg(&pdev->dev, "\n");
143863bdab5dSAntti Palosaari 
143963bdab5dSAntti Palosaari 	mutex_lock(&dev->vb_queue_lock);
144063bdab5dSAntti Palosaari 	mutex_lock(&dev->v4l2_lock);
144163bdab5dSAntti Palosaari 	/* No need to keep the urbs around after disconnection */
144263bdab5dSAntti Palosaari 	dev->udev = NULL;
144363bdab5dSAntti Palosaari 	v4l2_device_disconnect(&dev->v4l2_dev);
144463bdab5dSAntti Palosaari 	video_unregister_device(&dev->vdev);
144563bdab5dSAntti Palosaari 	mutex_unlock(&dev->v4l2_lock);
144663bdab5dSAntti Palosaari 	mutex_unlock(&dev->vb_queue_lock);
144763bdab5dSAntti Palosaari 
144863bdab5dSAntti Palosaari 	v4l2_device_put(&dev->v4l2_dev);
144963bdab5dSAntti Palosaari 
145063bdab5dSAntti Palosaari 	return 0;
145163bdab5dSAntti Palosaari }
145263bdab5dSAntti Palosaari 
145363bdab5dSAntti Palosaari static struct platform_driver rtl2832_sdr_driver = {
145463bdab5dSAntti Palosaari 	.driver = {
145563bdab5dSAntti Palosaari 		.name   = "rtl2832_sdr",
145663bdab5dSAntti Palosaari 		.owner  = THIS_MODULE,
145763bdab5dSAntti Palosaari 	},
145863bdab5dSAntti Palosaari 	.probe          = rtl2832_sdr_probe,
145963bdab5dSAntti Palosaari 	.remove         = rtl2832_sdr_remove,
146063bdab5dSAntti Palosaari };
146163bdab5dSAntti Palosaari module_platform_driver(rtl2832_sdr_driver);
146277bbb2b0SAntti Palosaari 
146377bbb2b0SAntti Palosaari MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
146477bbb2b0SAntti Palosaari MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
146577bbb2b0SAntti Palosaari MODULE_LICENSE("GPL");
1466