blob: e038e886731b1326d584d2fcc767492f3d0e9f85 [file] [log] [blame]
Antti Palosaari77113892013-09-10 00:07:56 -03001/*
2 * Realtek RTL2832U SDR driver
3 *
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * GNU Radio plugin "gr-kernel" for device usage will be on:
21 * http://git.linuxtv.org/anttip/gr-kernel.git
22 *
23 */
24
Antti Palosaari77113892013-09-10 00:07:56 -030025#include "rtl2832_sdr.h"
26#include "dvb_usb.h"
27
28#include <media/v4l2-device.h>
29#include <media/v4l2-ioctl.h>
30#include <media/v4l2-ctrls.h>
31#include <media/v4l2-event.h>
Junghak Sung2d700712015-09-22 10:30:30 -030032#include <media/videobuf2-v4l2.h>
Antti Palosaari77113892013-09-10 00:07:56 -030033#include <media/videobuf2-vmalloc.h>
34
Antti Palosaari63bdab52014-12-16 10:09:20 -030035#include <linux/platform_device.h>
Antti Palosaari77113892013-09-10 00:07:56 -030036#include <linux/jiffies.h>
37#include <linux/math64.h>
Peter Rosin1cf79db2016-05-04 22:15:33 +020038#include <linux/regmap.h>
Antti Palosaari77113892013-09-10 00:07:56 -030039
Antti Palosaari2970c0d2014-07-14 21:11:12 -030040static bool rtl2832_sdr_emulated_fmt;
41module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
42MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
43
Antti Palosaarifc3a0492015-05-03 21:45:43 -030044/* Original macro does not contain enough null pointer checks for our need */
45#define V4L2_SUBDEV_HAS_OP(sd, o, f) \
46 ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
47
Antti Palosaari77113892013-09-10 00:07:56 -030048#define MAX_BULK_BUFS (10)
49#define BULK_BUFFER_SIZE (128 * 512)
50
51static const struct v4l2_frequency_band bands_adc[] = {
52 {
53 .tuner = 0,
54 .type = V4L2_TUNER_ADC,
55 .index = 0,
56 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
57 .rangelow = 300000,
58 .rangehigh = 300000,
59 },
60 {
61 .tuner = 0,
62 .type = V4L2_TUNER_ADC,
63 .index = 1,
64 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
65 .rangelow = 900001,
66 .rangehigh = 2800000,
67 },
68 {
69 .tuner = 0,
70 .type = V4L2_TUNER_ADC,
71 .index = 2,
72 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
73 .rangelow = 3200000,
74 .rangehigh = 3200000,
75 },
76};
77
78static const struct v4l2_frequency_band bands_fm[] = {
79 {
80 .tuner = 1,
81 .type = V4L2_TUNER_RF,
82 .index = 0,
83 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
84 .rangelow = 50000000,
85 .rangehigh = 2000000000,
86 },
87};
88
89/* stream formats */
90struct rtl2832_sdr_format {
91 char *name;
92 u32 pixelformat;
Antti Palosaarid57fe402014-07-18 16:22:29 -030093 u32 buffersize;
Antti Palosaari77113892013-09-10 00:07:56 -030094};
95
96static struct rtl2832_sdr_format formats[] = {
97 {
Antti Palosaari2970c0d2014-07-14 21:11:12 -030098 .name = "Complex U8",
Antti Palosaarid57fe402014-07-18 16:22:29 -030099 .pixelformat = V4L2_SDR_FMT_CU8,
100 .buffersize = BULK_BUFFER_SIZE,
Antti Palosaari77113892013-09-10 00:07:56 -0300101 }, {
Antti Palosaari2970c0d2014-07-14 21:11:12 -0300102 .name = "Complex U16LE (emulated)",
Antti Palosaari77113892013-09-10 00:07:56 -0300103 .pixelformat = V4L2_SDR_FMT_CU16LE,
Antti Palosaarid57fe402014-07-18 16:22:29 -0300104 .buffersize = BULK_BUFFER_SIZE * 2,
Antti Palosaari77113892013-09-10 00:07:56 -0300105 },
106};
107
108static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
109
110/* intermediate buffers with raw data from the USB device */
111struct rtl2832_sdr_frame_buf {
Junghak Sung2d700712015-09-22 10:30:30 -0300112 /* common v4l buffer stuff -- must be first */
113 struct vb2_v4l2_buffer vb;
Antti Palosaari77113892013-09-10 00:07:56 -0300114 struct list_head list;
115};
116
Antti Palosaari725b7f32014-12-15 14:30:02 -0300117struct rtl2832_sdr_dev {
Dan Carpentere4d131d2015-05-14 05:39:17 -0300118#define POWER_ON 0 /* BIT(0) */
119#define URB_BUF 1 /* BIT(1) */
Antti Palosaari77113892013-09-10 00:07:56 -0300120 unsigned long flags;
121
Antti Palosaari63bdab52014-12-16 10:09:20 -0300122 struct platform_device *pdev;
Peter Rosine01fc422016-05-04 22:15:34 +0200123 struct regmap *regmap;
Antti Palosaari77113892013-09-10 00:07:56 -0300124
125 struct video_device vdev;
126 struct v4l2_device v4l2_dev;
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300127 struct v4l2_subdev *v4l2_subdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300128
129 /* videobuf2 queue and queued buffers list */
130 struct vb2_queue vb_queue;
131 struct list_head queued_bufs;
132 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
Hans Verkuilbc908752014-03-05 20:21:38 -0300133 unsigned sequence; /* buffer sequence counter */
Antti Palosaari77113892013-09-10 00:07:56 -0300134
135 /* Note if taking both locks v4l2_lock must always be locked first! */
136 struct mutex v4l2_lock; /* Protects everything else */
137 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
138
139 /* Pointer to our usb_device, will be NULL after unplug */
140 struct usb_device *udev; /* Both mutexes most be hold when setting! */
141
142 unsigned int vb_full; /* vb is full and packets dropped */
143
144 struct urb *urb_list[MAX_BULK_BUFS];
145 int buf_num;
146 unsigned long buf_size;
147 u8 *buf_list[MAX_BULK_BUFS];
148 dma_addr_t dma_addr[MAX_BULK_BUFS];
149 int urbs_initialized;
150 int urbs_submitted;
151
152 unsigned int f_adc, f_tuner;
153 u32 pixelformat;
Antti Palosaarid57fe402014-07-18 16:22:29 -0300154 u32 buffersize;
Antti Palosaari2970c0d2014-07-14 21:11:12 -0300155 unsigned int num_formats;
Antti Palosaari77113892013-09-10 00:07:56 -0300156
157 /* Controls */
158 struct v4l2_ctrl_handler hdl;
159 struct v4l2_ctrl *bandwidth_auto;
160 struct v4l2_ctrl *bandwidth;
161
162 /* for sample rate calc */
163 unsigned int sample;
164 unsigned int sample_measured;
165 unsigned long jiffies_next;
166};
167
Antti Palosaari77113892013-09-10 00:07:56 -0300168/* Private functions */
169static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
Antti Palosaari725b7f32014-12-15 14:30:02 -0300170 struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300171{
Antti Palosaariabfc8d62014-08-24 23:37:57 -0300172 unsigned long flags;
Antti Palosaari77113892013-09-10 00:07:56 -0300173 struct rtl2832_sdr_frame_buf *buf = NULL;
174
Antti Palosaari725b7f32014-12-15 14:30:02 -0300175 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
176 if (list_empty(&dev->queued_bufs))
Antti Palosaari77113892013-09-10 00:07:56 -0300177 goto leave;
178
Antti Palosaari725b7f32014-12-15 14:30:02 -0300179 buf = list_entry(dev->queued_bufs.next,
Antti Palosaari77113892013-09-10 00:07:56 -0300180 struct rtl2832_sdr_frame_buf, list);
181 list_del(&buf->list);
182leave:
Antti Palosaari725b7f32014-12-15 14:30:02 -0300183 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300184 return buf;
185}
186
Antti Palosaari725b7f32014-12-15 14:30:02 -0300187static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
Antti Palosaari77113892013-09-10 00:07:56 -0300188 void *dst, const u8 *src, unsigned int src_len)
189{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300190 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300191 unsigned int dst_len;
192
Antti Palosaari725b7f32014-12-15 14:30:02 -0300193 if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
Antti Palosaari77113892013-09-10 00:07:56 -0300194 /* native stream, no need to convert */
195 memcpy(dst, src, src_len);
196 dst_len = src_len;
Antti Palosaari725b7f32014-12-15 14:30:02 -0300197 } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
Antti Palosaari77113892013-09-10 00:07:56 -0300198 /* convert u8 to u16 */
199 unsigned int i;
200 u16 *u16dst = dst;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300201
Antti Palosaari77113892013-09-10 00:07:56 -0300202 for (i = 0; i < src_len; i++)
203 *u16dst++ = (src[i] << 8) | (src[i] >> 0);
204 dst_len = 2 * src_len;
205 } else {
206 dst_len = 0;
207 }
208
Antti Palosaarib538a8e2014-08-24 23:44:13 -0300209 /* calculate sample rate and output it in 10 seconds intervals */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300210 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
Antti Palosaarib538a8e2014-08-24 23:44:13 -0300211 #define MSECS 10000UL
212 unsigned int msecs = jiffies_to_msecs(jiffies -
Antti Palosaari725b7f32014-12-15 14:30:02 -0300213 dev->jiffies_next + msecs_to_jiffies(MSECS));
214 unsigned int samples = dev->sample - dev->sample_measured;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300215
Antti Palosaari725b7f32014-12-15 14:30:02 -0300216 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
217 dev->sample_measured = dev->sample;
Antti Palosaarie4e78692014-12-16 12:18:01 -0300218 dev_dbg(&pdev->dev,
219 "slen=%u samples=%u msecs=%u sample rate=%lu\n",
220 src_len, samples, msecs, samples * 1000UL / msecs);
Antti Palosaari77113892013-09-10 00:07:56 -0300221 }
222
223 /* total number of I+Q pairs */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300224 dev->sample += src_len / 2;
Antti Palosaari77113892013-09-10 00:07:56 -0300225
226 return dst_len;
227}
228
229/*
230 * This gets called for the bulk stream pipe. This is done in interrupt
231 * time, so it has to be fast, not crash, and not stall. Neat.
232 */
233static void rtl2832_sdr_urb_complete(struct urb *urb)
234{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300235 struct rtl2832_sdr_dev *dev = urb->context;
Antti Palosaarie4e78692014-12-16 12:18:01 -0300236 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300237 struct rtl2832_sdr_frame_buf *fbuf;
238
Antti Palosaarie4e78692014-12-16 12:18:01 -0300239 dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
240 urb->status, urb->actual_length,
241 urb->transfer_buffer_length, urb->error_count);
Antti Palosaari77113892013-09-10 00:07:56 -0300242
243 switch (urb->status) {
244 case 0: /* success */
245 case -ETIMEDOUT: /* NAK */
246 break;
247 case -ECONNRESET: /* kill */
248 case -ENOENT:
249 case -ESHUTDOWN:
250 return;
251 default: /* error */
Antti Palosaarie4e78692014-12-16 12:18:01 -0300252 dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
Antti Palosaari77113892013-09-10 00:07:56 -0300253 break;
254 }
255
256 if (likely(urb->actual_length > 0)) {
257 void *ptr;
258 unsigned int len;
259 /* get free framebuffer */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300260 fbuf = rtl2832_sdr_get_next_fill_buf(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300261 if (unlikely(fbuf == NULL)) {
Antti Palosaari725b7f32014-12-15 14:30:02 -0300262 dev->vb_full++;
Antti Palosaarie4e78692014-12-16 12:18:01 -0300263 dev_notice_ratelimited(&pdev->dev,
264 "videobuf is full, %d packets dropped\n",
265 dev->vb_full);
Antti Palosaari77113892013-09-10 00:07:56 -0300266 goto skip;
267 }
268
269 /* fill framebuffer */
Junghak Sung2d700712015-09-22 10:30:30 -0300270 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300271 len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
Antti Palosaari77113892013-09-10 00:07:56 -0300272 urb->actual_length);
Junghak Sung2d700712015-09-22 10:30:30 -0300273 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
Junghak Sungd6dd6452015-11-03 08:16:37 -0200274 fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300275 fbuf->vb.sequence = dev->sequence++;
276 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Antti Palosaari77113892013-09-10 00:07:56 -0300277 }
278skip:
279 usb_submit_urb(urb, GFP_ATOMIC);
280}
281
Antti Palosaari725b7f32014-12-15 14:30:02 -0300282static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300283{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300284 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300285 int i;
286
Antti Palosaari725b7f32014-12-15 14:30:02 -0300287 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
Antti Palosaarie4e78692014-12-16 12:18:01 -0300288 dev_dbg(&pdev->dev, "kill urb=%d\n", i);
Antti Palosaari77113892013-09-10 00:07:56 -0300289 /* stop the URB */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300290 usb_kill_urb(dev->urb_list[i]);
Antti Palosaari77113892013-09-10 00:07:56 -0300291 }
Antti Palosaari725b7f32014-12-15 14:30:02 -0300292 dev->urbs_submitted = 0;
Antti Palosaari77113892013-09-10 00:07:56 -0300293
294 return 0;
295}
296
Antti Palosaari725b7f32014-12-15 14:30:02 -0300297static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300298{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300299 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300300 int i, ret;
301
Antti Palosaari725b7f32014-12-15 14:30:02 -0300302 for (i = 0; i < dev->urbs_initialized; i++) {
Antti Palosaarie4e78692014-12-16 12:18:01 -0300303 dev_dbg(&pdev->dev, "submit urb=%d\n", i);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300304 ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
Antti Palosaari77113892013-09-10 00:07:56 -0300305 if (ret) {
Antti Palosaarie4e78692014-12-16 12:18:01 -0300306 dev_err(&pdev->dev,
307 "Could not submit urb no. %d - get them all back\n",
308 i);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300309 rtl2832_sdr_kill_urbs(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300310 return ret;
311 }
Antti Palosaari725b7f32014-12-15 14:30:02 -0300312 dev->urbs_submitted++;
Antti Palosaari77113892013-09-10 00:07:56 -0300313 }
314
315 return 0;
316}
317
Antti Palosaari725b7f32014-12-15 14:30:02 -0300318static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300319{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300320 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300321
Dan Carpentere4d131d2015-05-14 05:39:17 -0300322 if (test_bit(URB_BUF, &dev->flags)) {
Antti Palosaari725b7f32014-12-15 14:30:02 -0300323 while (dev->buf_num) {
324 dev->buf_num--;
Antti Palosaarie4e78692014-12-16 12:18:01 -0300325 dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300326 usb_free_coherent(dev->udev, dev->buf_size,
327 dev->buf_list[dev->buf_num],
328 dev->dma_addr[dev->buf_num]);
Antti Palosaari77113892013-09-10 00:07:56 -0300329 }
330 }
Dan Carpentere4d131d2015-05-14 05:39:17 -0300331 clear_bit(URB_BUF, &dev->flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300332
333 return 0;
334}
335
Antti Palosaari725b7f32014-12-15 14:30:02 -0300336static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300337{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300338 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300339
Antti Palosaari725b7f32014-12-15 14:30:02 -0300340 dev->buf_num = 0;
341 dev->buf_size = BULK_BUFFER_SIZE;
Antti Palosaari77113892013-09-10 00:07:56 -0300342
Antti Palosaarie4e78692014-12-16 12:18:01 -0300343 dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
344 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
Antti Palosaari77113892013-09-10 00:07:56 -0300345
Antti Palosaari725b7f32014-12-15 14:30:02 -0300346 for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
347 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
Antti Palosaari77113892013-09-10 00:07:56 -0300348 BULK_BUFFER_SIZE, GFP_ATOMIC,
Antti Palosaari725b7f32014-12-15 14:30:02 -0300349 &dev->dma_addr[dev->buf_num]);
350 if (!dev->buf_list[dev->buf_num]) {
Antti Palosaarie4e78692014-12-16 12:18:01 -0300351 dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
352 dev->buf_num);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300353 rtl2832_sdr_free_stream_bufs(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300354 return -ENOMEM;
355 }
356
Antti Palosaarie4e78692014-12-16 12:18:01 -0300357 dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
358 dev->buf_num, dev->buf_list[dev->buf_num],
359 (long long)dev->dma_addr[dev->buf_num]);
Dan Carpentere4d131d2015-05-14 05:39:17 -0300360 set_bit(URB_BUF, &dev->flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300361 }
362
363 return 0;
364}
365
Antti Palosaari725b7f32014-12-15 14:30:02 -0300366static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300367{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300368 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300369 int i;
370
Antti Palosaari725b7f32014-12-15 14:30:02 -0300371 rtl2832_sdr_kill_urbs(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300372
Antti Palosaari725b7f32014-12-15 14:30:02 -0300373 for (i = dev->urbs_initialized - 1; i >= 0; i--) {
374 if (dev->urb_list[i]) {
Antti Palosaarie4e78692014-12-16 12:18:01 -0300375 dev_dbg(&pdev->dev, "free urb=%d\n", i);
Antti Palosaari77113892013-09-10 00:07:56 -0300376 /* free the URBs */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300377 usb_free_urb(dev->urb_list[i]);
Antti Palosaari77113892013-09-10 00:07:56 -0300378 }
379 }
Antti Palosaari725b7f32014-12-15 14:30:02 -0300380 dev->urbs_initialized = 0;
Antti Palosaari77113892013-09-10 00:07:56 -0300381
382 return 0;
383}
384
Antti Palosaari725b7f32014-12-15 14:30:02 -0300385static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300386{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300387 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300388 int i, j;
389
390 /* allocate the URBs */
391 for (i = 0; i < MAX_BULK_BUFS; i++) {
Antti Palosaarie4e78692014-12-16 12:18:01 -0300392 dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300393 dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
394 if (!dev->urb_list[i]) {
Antti Palosaari77113892013-09-10 00:07:56 -0300395 for (j = 0; j < i; j++)
Antti Palosaari725b7f32014-12-15 14:30:02 -0300396 usb_free_urb(dev->urb_list[j]);
Antti Palosaari77113892013-09-10 00:07:56 -0300397 return -ENOMEM;
398 }
Antti Palosaari725b7f32014-12-15 14:30:02 -0300399 usb_fill_bulk_urb(dev->urb_list[i],
400 dev->udev,
401 usb_rcvbulkpipe(dev->udev, 0x81),
402 dev->buf_list[i],
Antti Palosaari77113892013-09-10 00:07:56 -0300403 BULK_BUFFER_SIZE,
Antti Palosaari725b7f32014-12-15 14:30:02 -0300404 rtl2832_sdr_urb_complete, dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300405
Antti Palosaari725b7f32014-12-15 14:30:02 -0300406 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
407 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
408 dev->urbs_initialized++;
Antti Palosaari77113892013-09-10 00:07:56 -0300409 }
410
411 return 0;
412}
413
414/* Must be called with vb_queue_lock hold */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300415static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300416{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300417 struct platform_device *pdev = dev->pdev;
Antti Palosaariabfc8d62014-08-24 23:37:57 -0300418 unsigned long flags;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300419
Antti Palosaarie4e78692014-12-16 12:18:01 -0300420 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300421
Antti Palosaari725b7f32014-12-15 14:30:02 -0300422 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
423 while (!list_empty(&dev->queued_bufs)) {
Antti Palosaari77113892013-09-10 00:07:56 -0300424 struct rtl2832_sdr_frame_buf *buf;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300425
Antti Palosaari725b7f32014-12-15 14:30:02 -0300426 buf = list_entry(dev->queued_bufs.next,
Antti Palosaari77113892013-09-10 00:07:56 -0300427 struct rtl2832_sdr_frame_buf, list);
428 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300429 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Antti Palosaari77113892013-09-10 00:07:56 -0300430 }
Antti Palosaari725b7f32014-12-15 14:30:02 -0300431 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300432}
433
Antti Palosaari77113892013-09-10 00:07:56 -0300434static int rtl2832_sdr_querycap(struct file *file, void *fh,
435 struct v4l2_capability *cap)
436{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300437 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -0300438 struct platform_device *pdev = dev->pdev;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300439
Antti Palosaarie4e78692014-12-16 12:18:01 -0300440 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300441
442 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
Antti Palosaari725b7f32014-12-15 14:30:02 -0300443 strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
444 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Antti Palosaari77113892013-09-10 00:07:56 -0300445 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
446 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
447 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
448 return 0;
449}
450
451/* Videobuf2 operations */
452static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200453 unsigned int *nbuffers,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300454 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
Antti Palosaari77113892013-09-10 00:07:56 -0300455{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300456 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
Antti Palosaarie4e78692014-12-16 12:18:01 -0300457 struct platform_device *pdev = dev->pdev;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300458
Antti Palosaarie4e78692014-12-16 12:18:01 -0300459 dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
Antti Palosaari77113892013-09-10 00:07:56 -0300460
Hans Verkuilbc908752014-03-05 20:21:38 -0300461 /* Need at least 8 buffers */
462 if (vq->num_buffers + *nbuffers < 8)
463 *nbuffers = 8 - vq->num_buffers;
Antti Palosaari77113892013-09-10 00:07:56 -0300464 *nplanes = 1;
Antti Palosaari725b7f32014-12-15 14:30:02 -0300465 sizes[0] = PAGE_ALIGN(dev->buffersize);
Antti Palosaarie4e78692014-12-16 12:18:01 -0300466 dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
Antti Palosaari77113892013-09-10 00:07:56 -0300467 return 0;
468}
469
470static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
471{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300472 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Antti Palosaari77113892013-09-10 00:07:56 -0300473
474 /* Don't allow queing new buffers after device disconnection */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300475 if (!dev->udev)
Antti Palosaari77113892013-09-10 00:07:56 -0300476 return -ENODEV;
477
478 return 0;
479}
480
481static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
482{
Junghak Sung2d700712015-09-22 10:30:30 -0300483 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300484 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Antti Palosaari77113892013-09-10 00:07:56 -0300485 struct rtl2832_sdr_frame_buf *buf =
Junghak Sung2d700712015-09-22 10:30:30 -0300486 container_of(vbuf, struct rtl2832_sdr_frame_buf, vb);
Antti Palosaariabfc8d62014-08-24 23:37:57 -0300487 unsigned long flags;
Antti Palosaari77113892013-09-10 00:07:56 -0300488
489 /* Check the device has not disconnected between prep and queuing */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300490 if (!dev->udev) {
Junghak Sung2d700712015-09-22 10:30:30 -0300491 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Antti Palosaari77113892013-09-10 00:07:56 -0300492 return;
493 }
494
Antti Palosaari725b7f32014-12-15 14:30:02 -0300495 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
496 list_add_tail(&buf->list, &dev->queued_bufs);
497 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300498}
499
Antti Palosaari725b7f32014-12-15 14:30:02 -0300500static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300501{
Antti Palosaari63bdab52014-12-16 10:09:20 -0300502 struct platform_device *pdev = dev->pdev;
503 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300504 struct dvb_frontend *fe = pdata->dvb_frontend;
Antti Palosaari77113892013-09-10 00:07:56 -0300505 int ret;
506 unsigned int f_sr, f_if;
507 u8 buf[4], u8tmp1, u8tmp2;
508 u64 u64tmp;
509 u32 u32tmp;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300510
Antti Palosaarie4e78692014-12-16 12:18:01 -0300511 dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
Antti Palosaari77113892013-09-10 00:07:56 -0300512
Antti Palosaari725b7f32014-12-15 14:30:02 -0300513 if (!test_bit(POWER_ON, &dev->flags))
Antti Palosaari77113892013-09-10 00:07:56 -0300514 return 0;
515
Antti Palosaari725b7f32014-12-15 14:30:02 -0300516 if (dev->f_adc == 0)
Antti Palosaari77113892013-09-10 00:07:56 -0300517 return 0;
518
Antti Palosaari725b7f32014-12-15 14:30:02 -0300519 f_sr = dev->f_adc;
Antti Palosaari77113892013-09-10 00:07:56 -0300520
Peter Rosine01fc422016-05-04 22:15:34 +0200521 ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2);
Antti Palosaari77113892013-09-10 00:07:56 -0300522 if (ret)
523 goto err;
524
Peter Rosine01fc422016-05-04 22:15:34 +0200525 ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4);
Antti Palosaari77113892013-09-10 00:07:56 -0300526 if (ret)
527 goto err;
528
529 /* get IF from tuner */
530 if (fe->ops.tuner_ops.get_if_frequency)
531 ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
532 else
533 ret = -EINVAL;
534
535 if (ret)
536 goto err;
537
538 /* program IF */
Antti Palosaari63bdab52014-12-16 10:09:20 -0300539 u64tmp = f_if % pdata->clk;
Antti Palosaari77113892013-09-10 00:07:56 -0300540 u64tmp *= 0x400000;
Antti Palosaari63bdab52014-12-16 10:09:20 -0300541 u64tmp = div_u64(u64tmp, pdata->clk);
Antti Palosaari77113892013-09-10 00:07:56 -0300542 u64tmp = -u64tmp;
543 u32tmp = u64tmp & 0x3fffff;
544
Antti Palosaarie4e78692014-12-16 12:18:01 -0300545 dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
Antti Palosaari77113892013-09-10 00:07:56 -0300546
547 buf[0] = (u32tmp >> 16) & 0xff;
548 buf[1] = (u32tmp >> 8) & 0xff;
549 buf[2] = (u32tmp >> 0) & 0xff;
550
Peter Rosine01fc422016-05-04 22:15:34 +0200551 ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3);
Antti Palosaari77113892013-09-10 00:07:56 -0300552 if (ret)
553 goto err;
554
555 /* BB / IF mode */
556 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
557 if (f_if) {
558 u8tmp1 = 0x1a; /* disable Zero-IF */
559 u8tmp2 = 0x8d; /* enable ADC I */
560 } else {
561 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
562 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
563 }
564
Peter Rosine01fc422016-05-04 22:15:34 +0200565 ret = regmap_write(dev->regmap, 0x1b1, u8tmp1);
Antti Palosaari77113892013-09-10 00:07:56 -0300566 if (ret)
567 goto err;
568
Peter Rosine01fc422016-05-04 22:15:34 +0200569 ret = regmap_write(dev->regmap, 0x008, u8tmp2);
Antti Palosaari77113892013-09-10 00:07:56 -0300570 if (ret)
571 goto err;
572
Peter Rosine01fc422016-05-04 22:15:34 +0200573 ret = regmap_write(dev->regmap, 0x006, 0x80);
Antti Palosaari77113892013-09-10 00:07:56 -0300574 if (ret)
575 goto err;
576
577 /* program sampling rate (resampling down) */
Antti Palosaari63bdab52014-12-16 10:09:20 -0300578 u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
Antti Palosaari77113892013-09-10 00:07:56 -0300579 u32tmp <<= 2;
580 buf[0] = (u32tmp >> 24) & 0xff;
581 buf[1] = (u32tmp >> 16) & 0xff;
582 buf[2] = (u32tmp >> 8) & 0xff;
583 buf[3] = (u32tmp >> 0) & 0xff;
Peter Rosine01fc422016-05-04 22:15:34 +0200584 ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4);
Antti Palosaari77113892013-09-10 00:07:56 -0300585 if (ret)
586 goto err;
587
588 /* low-pass filter */
Peter Rosine01fc422016-05-04 22:15:34 +0200589 ret = regmap_bulk_write(dev->regmap, 0x11c,
590 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
591 20);
Antti Palosaari77113892013-09-10 00:07:56 -0300592 if (ret)
593 goto err;
594
Peter Rosine01fc422016-05-04 22:15:34 +0200595 ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
Antti Palosaari77113892013-09-10 00:07:56 -0300596 if (ret)
597 goto err;
598
599 /* mode */
Peter Rosine01fc422016-05-04 22:15:34 +0200600 ret = regmap_write(dev->regmap, 0x019, 0x05);
Antti Palosaari77113892013-09-10 00:07:56 -0300601 if (ret)
602 goto err;
603
Peter Rosine01fc422016-05-04 22:15:34 +0200604 ret = regmap_bulk_write(dev->regmap, 0x01a,
605 "\x1b\x16\x0d\x06\x01\xff", 6);
Antti Palosaari77113892013-09-10 00:07:56 -0300606 if (ret)
607 goto err;
608
609 /* FSM */
Peter Rosine01fc422016-05-04 22:15:34 +0200610 ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
Antti Palosaari77113892013-09-10 00:07:56 -0300611 if (ret)
612 goto err;
613
614 /* PID filter */
Peter Rosine01fc422016-05-04 22:15:34 +0200615 ret = regmap_write(dev->regmap, 0x061, 0x60);
Antti Palosaari77113892013-09-10 00:07:56 -0300616 if (ret)
617 goto err;
618
619 /* used RF tuner based settings */
Antti Palosaari63bdab52014-12-16 10:09:20 -0300620 switch (pdata->tuner) {
621 case RTL2832_SDR_TUNER_E4000:
Peter Rosine01fc422016-05-04 22:15:34 +0200622 ret = regmap_write(dev->regmap, 0x112, 0x5a);
623 ret = regmap_write(dev->regmap, 0x102, 0x40);
624 ret = regmap_write(dev->regmap, 0x103, 0x5a);
625 ret = regmap_write(dev->regmap, 0x1c7, 0x30);
626 ret = regmap_write(dev->regmap, 0x104, 0xd0);
627 ret = regmap_write(dev->regmap, 0x105, 0xbe);
628 ret = regmap_write(dev->regmap, 0x1c8, 0x18);
629 ret = regmap_write(dev->regmap, 0x106, 0x35);
630 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
631 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
632 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
633 ret = regmap_write(dev->regmap, 0x107, 0x40);
634 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
635 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
636 ret = regmap_write(dev->regmap, 0x108, 0x80);
637 ret = regmap_write(dev->regmap, 0x109, 0x7f);
638 ret = regmap_write(dev->regmap, 0x10a, 0x80);
639 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
640 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
641 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
642 ret = regmap_write(dev->regmap, 0x011, 0xd4);
643 ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
644 ret = regmap_write(dev->regmap, 0x1d9, 0x00);
645 ret = regmap_write(dev->regmap, 0x1db, 0x00);
646 ret = regmap_write(dev->regmap, 0x1dd, 0x14);
647 ret = regmap_write(dev->regmap, 0x1de, 0xec);
648 ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
649 ret = regmap_write(dev->regmap, 0x1e6, 0x02);
650 ret = regmap_write(dev->regmap, 0x1d7, 0x09);
651 ret = regmap_write(dev->regmap, 0x00d, 0x83);
652 ret = regmap_write(dev->regmap, 0x010, 0x49);
653 ret = regmap_write(dev->regmap, 0x00d, 0x87);
654 ret = regmap_write(dev->regmap, 0x00d, 0x85);
655 ret = regmap_write(dev->regmap, 0x013, 0x02);
Antti Palosaari77113892013-09-10 00:07:56 -0300656 break;
Antti Palosaari63bdab52014-12-16 10:09:20 -0300657 case RTL2832_SDR_TUNER_FC0012:
658 case RTL2832_SDR_TUNER_FC0013:
Peter Rosine01fc422016-05-04 22:15:34 +0200659 ret = regmap_write(dev->regmap, 0x112, 0x5a);
660 ret = regmap_write(dev->regmap, 0x102, 0x40);
661 ret = regmap_write(dev->regmap, 0x103, 0x5a);
662 ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
663 ret = regmap_write(dev->regmap, 0x104, 0xcc);
664 ret = regmap_write(dev->regmap, 0x105, 0xbe);
665 ret = regmap_write(dev->regmap, 0x1c8, 0x16);
666 ret = regmap_write(dev->regmap, 0x106, 0x35);
667 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
668 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
669 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
670 ret = regmap_write(dev->regmap, 0x107, 0x40);
671 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
672 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
673 ret = regmap_write(dev->regmap, 0x108, 0x80);
674 ret = regmap_write(dev->regmap, 0x109, 0x7f);
675 ret = regmap_write(dev->regmap, 0x10a, 0x80);
676 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
677 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
678 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
679 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2);
680 ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
681 ret = regmap_write(dev->regmap, 0x1d9, 0x00);
682 ret = regmap_write(dev->regmap, 0x1db, 0x00);
683 ret = regmap_write(dev->regmap, 0x1dd, 0x11);
684 ret = regmap_write(dev->regmap, 0x1de, 0xef);
685 ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
686 ret = regmap_write(dev->regmap, 0x1e6, 0x02);
687 ret = regmap_write(dev->regmap, 0x1d7, 0x09);
Antti Palosaari77113892013-09-10 00:07:56 -0300688 break;
Antti Palosaari63bdab52014-12-16 10:09:20 -0300689 case RTL2832_SDR_TUNER_R820T:
690 case RTL2832_SDR_TUNER_R828D:
Peter Rosine01fc422016-05-04 22:15:34 +0200691 ret = regmap_write(dev->regmap, 0x112, 0x5a);
692 ret = regmap_write(dev->regmap, 0x102, 0x40);
693 ret = regmap_write(dev->regmap, 0x115, 0x01);
694 ret = regmap_write(dev->regmap, 0x103, 0x80);
695 ret = regmap_write(dev->regmap, 0x1c7, 0x24);
696 ret = regmap_write(dev->regmap, 0x104, 0xcc);
697 ret = regmap_write(dev->regmap, 0x105, 0xbe);
698 ret = regmap_write(dev->regmap, 0x1c8, 0x14);
699 ret = regmap_write(dev->regmap, 0x106, 0x35);
700 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
701 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
702 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
703 ret = regmap_write(dev->regmap, 0x107, 0x40);
704 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
705 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
706 ret = regmap_write(dev->regmap, 0x108, 0x80);
707 ret = regmap_write(dev->regmap, 0x109, 0x7f);
708 ret = regmap_write(dev->regmap, 0x10a, 0x80);
709 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
710 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
711 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
712 ret = regmap_write(dev->regmap, 0x011, 0xf4);
Antti Palosaari77113892013-09-10 00:07:56 -0300713 break;
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300714 case RTL2832_SDR_TUNER_FC2580:
Peter Rosine01fc422016-05-04 22:15:34 +0200715 ret = regmap_write(dev->regmap, 0x112, 0x39);
716 ret = regmap_write(dev->regmap, 0x102, 0x40);
717 ret = regmap_write(dev->regmap, 0x103, 0x5a);
718 ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
719 ret = regmap_write(dev->regmap, 0x104, 0xcc);
720 ret = regmap_write(dev->regmap, 0x105, 0xbe);
721 ret = regmap_write(dev->regmap, 0x1c8, 0x16);
722 ret = regmap_write(dev->regmap, 0x106, 0x35);
723 ret = regmap_write(dev->regmap, 0x1c9, 0x21);
724 ret = regmap_write(dev->regmap, 0x1ca, 0x21);
725 ret = regmap_write(dev->regmap, 0x1cb, 0x00);
726 ret = regmap_write(dev->regmap, 0x107, 0x40);
727 ret = regmap_write(dev->regmap, 0x1cd, 0x10);
728 ret = regmap_write(dev->regmap, 0x1ce, 0x10);
729 ret = regmap_write(dev->regmap, 0x108, 0x80);
730 ret = regmap_write(dev->regmap, 0x109, 0x7f);
731 ret = regmap_write(dev->regmap, 0x10a, 0x9c);
732 ret = regmap_write(dev->regmap, 0x10b, 0x7f);
733 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
734 ret = regmap_write(dev->regmap, 0x00e, 0xfc);
735 ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2);
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300736 break;
Antti Palosaari77113892013-09-10 00:07:56 -0300737 default:
Antti Palosaarie4e78692014-12-16 12:18:01 -0300738 dev_notice(&pdev->dev, "Unsupported tuner\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300739 }
740
741 /* software reset */
Peter Rosine01fc422016-05-04 22:15:34 +0200742 ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04);
Antti Palosaari77113892013-09-10 00:07:56 -0300743 if (ret)
744 goto err;
745
Peter Rosine01fc422016-05-04 22:15:34 +0200746 ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00);
Antti Palosaari77113892013-09-10 00:07:56 -0300747 if (ret)
748 goto err;
749err:
750 return ret;
751};
752
Antti Palosaari725b7f32014-12-15 14:30:02 -0300753static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300754{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300755 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -0300756 int ret;
757
Antti Palosaarie4e78692014-12-16 12:18:01 -0300758 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300759
760 /* PID filter */
Peter Rosine01fc422016-05-04 22:15:34 +0200761 ret = regmap_write(dev->regmap, 0x061, 0xe0);
Antti Palosaari77113892013-09-10 00:07:56 -0300762 if (ret)
763 goto err;
764
765 /* mode */
Peter Rosine01fc422016-05-04 22:15:34 +0200766 ret = regmap_write(dev->regmap, 0x019, 0x20);
Antti Palosaari77113892013-09-10 00:07:56 -0300767 if (ret)
768 goto err;
769
Peter Rosine01fc422016-05-04 22:15:34 +0200770 ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
Antti Palosaari77113892013-09-10 00:07:56 -0300771 if (ret)
772 goto err;
773
774 /* FSM */
Peter Rosine01fc422016-05-04 22:15:34 +0200775 ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
Antti Palosaari77113892013-09-10 00:07:56 -0300776 if (ret)
777 goto err;
778
Peter Rosine01fc422016-05-04 22:15:34 +0200779 ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2);
Antti Palosaari77113892013-09-10 00:07:56 -0300780 if (ret)
781 goto err;
782
Peter Rosine01fc422016-05-04 22:15:34 +0200783 ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4);
Antti Palosaari77113892013-09-10 00:07:56 -0300784 if (ret)
785 goto err;
786err:
787 return;
788};
789
Antti Palosaari725b7f32014-12-15 14:30:02 -0300790static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300791{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300792 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300793 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
794 struct dvb_frontend *fe = pdata->dvb_frontend;
Antti Palosaari77113892013-09-10 00:07:56 -0300795 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
796 struct v4l2_ctrl *bandwidth_auto;
797 struct v4l2_ctrl *bandwidth;
798
799 /*
800 * tuner RF (Hz)
801 */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300802 if (dev->f_tuner == 0)
Antti Palosaari77113892013-09-10 00:07:56 -0300803 return 0;
804
805 /*
806 * bandwidth (Hz)
807 */
Antti Palosaari725b7f32014-12-15 14:30:02 -0300808 bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
Anthony DeStefano85594962014-04-09 23:08:49 -0300809 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
Antti Palosaari725b7f32014-12-15 14:30:02 -0300810 bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
Antti Palosaari77113892013-09-10 00:07:56 -0300811 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
Antti Palosaari725b7f32014-12-15 14:30:02 -0300812 c->bandwidth_hz = dev->f_adc;
813 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
Antti Palosaari77113892013-09-10 00:07:56 -0300814 } else {
815 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
816 }
817
Antti Palosaari725b7f32014-12-15 14:30:02 -0300818 c->frequency = dev->f_tuner;
Antti Palosaari77113892013-09-10 00:07:56 -0300819 c->delivery_system = SYS_DVBT;
820
Antti Palosaarie4e78692014-12-16 12:18:01 -0300821 dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
822 c->frequency, c->bandwidth_hz);
Antti Palosaari77113892013-09-10 00:07:56 -0300823
Antti Palosaari725b7f32014-12-15 14:30:02 -0300824 if (!test_bit(POWER_ON, &dev->flags))
Antti Palosaari77113892013-09-10 00:07:56 -0300825 return 0;
826
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300827 if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
828 if (fe->ops.tuner_ops.set_params)
829 fe->ops.tuner_ops.set_params(fe);
830 }
Antti Palosaari77113892013-09-10 00:07:56 -0300831
832 return 0;
833};
834
Antti Palosaari725b7f32014-12-15 14:30:02 -0300835static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300836{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300837 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300838 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
839 struct dvb_frontend *fe = pdata->dvb_frontend;
Antti Palosaari77113892013-09-10 00:07:56 -0300840
Antti Palosaarie4e78692014-12-16 12:18:01 -0300841 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300842
843 if (fe->ops.tuner_ops.init)
844 fe->ops.tuner_ops.init(fe);
845
846 return 0;
847};
848
Antti Palosaari725b7f32014-12-15 14:30:02 -0300849static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
Antti Palosaari77113892013-09-10 00:07:56 -0300850{
Antti Palosaarie4e78692014-12-16 12:18:01 -0300851 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300852 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
853 struct dvb_frontend *fe = pdata->dvb_frontend;
Antti Palosaari77113892013-09-10 00:07:56 -0300854
Antti Palosaarie4e78692014-12-16 12:18:01 -0300855 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300856
857 if (fe->ops.tuner_ops.sleep)
858 fe->ops.tuner_ops.sleep(fe);
859
860 return;
861};
862
863static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
864{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300865 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
Antti Palosaarie4e78692014-12-16 12:18:01 -0300866 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300867 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
868 struct dvb_usb_device *d = pdata->dvb_usb_device;
Antti Palosaari77113892013-09-10 00:07:56 -0300869 int ret;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300870
Antti Palosaarie4e78692014-12-16 12:18:01 -0300871 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300872
Antti Palosaari725b7f32014-12-15 14:30:02 -0300873 if (!dev->udev)
Antti Palosaari77113892013-09-10 00:07:56 -0300874 return -ENODEV;
875
Antti Palosaari725b7f32014-12-15 14:30:02 -0300876 if (mutex_lock_interruptible(&dev->v4l2_lock))
Antti Palosaari77113892013-09-10 00:07:56 -0300877 return -ERESTARTSYS;
878
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300879 if (d->props->power_ctrl)
880 d->props->power_ctrl(d, 1);
Antti Palosaari77113892013-09-10 00:07:56 -0300881
Antti Palosaari2281c822014-12-03 20:11:03 -0300882 /* enable ADC */
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300883 if (d->props->frontend_ctrl)
884 d->props->frontend_ctrl(pdata->dvb_frontend, 1);
Antti Palosaari2281c822014-12-03 20:11:03 -0300885
Antti Palosaari725b7f32014-12-15 14:30:02 -0300886 set_bit(POWER_ON, &dev->flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300887
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300888 /* wake-up tuner */
889 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
890 ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
891 else
892 ret = rtl2832_sdr_set_tuner(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300893 if (ret)
894 goto err;
895
Antti Palosaari725b7f32014-12-15 14:30:02 -0300896 ret = rtl2832_sdr_set_tuner_freq(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300897 if (ret)
898 goto err;
899
Antti Palosaari725b7f32014-12-15 14:30:02 -0300900 ret = rtl2832_sdr_set_adc(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300901 if (ret)
902 goto err;
903
Antti Palosaari725b7f32014-12-15 14:30:02 -0300904 ret = rtl2832_sdr_alloc_stream_bufs(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300905 if (ret)
906 goto err;
907
Antti Palosaari725b7f32014-12-15 14:30:02 -0300908 ret = rtl2832_sdr_alloc_urbs(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300909 if (ret)
910 goto err;
911
Antti Palosaari725b7f32014-12-15 14:30:02 -0300912 dev->sequence = 0;
Hans Verkuilbc908752014-03-05 20:21:38 -0300913
Antti Palosaari725b7f32014-12-15 14:30:02 -0300914 ret = rtl2832_sdr_submit_urbs(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300915 if (ret)
916 goto err;
917
918err:
Antti Palosaari725b7f32014-12-15 14:30:02 -0300919 mutex_unlock(&dev->v4l2_lock);
Antti Palosaari77113892013-09-10 00:07:56 -0300920
921 return ret;
922}
923
Hans Verkuile37559b2014-04-17 02:47:21 -0300924static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
Antti Palosaari77113892013-09-10 00:07:56 -0300925{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300926 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
Antti Palosaarie4e78692014-12-16 12:18:01 -0300927 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300928 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
929 struct dvb_usb_device *d = pdata->dvb_usb_device;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300930
Antti Palosaarie4e78692014-12-16 12:18:01 -0300931 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -0300932
Antti Palosaari725b7f32014-12-15 14:30:02 -0300933 mutex_lock(&dev->v4l2_lock);
Antti Palosaari77113892013-09-10 00:07:56 -0300934
Antti Palosaari725b7f32014-12-15 14:30:02 -0300935 rtl2832_sdr_kill_urbs(dev);
936 rtl2832_sdr_free_urbs(dev);
937 rtl2832_sdr_free_stream_bufs(dev);
938 rtl2832_sdr_cleanup_queued_bufs(dev);
939 rtl2832_sdr_unset_adc(dev);
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300940
941 /* sleep tuner */
942 if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
943 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
944 else
945 rtl2832_sdr_unset_tuner(dev);
Antti Palosaari77113892013-09-10 00:07:56 -0300946
Antti Palosaari725b7f32014-12-15 14:30:02 -0300947 clear_bit(POWER_ON, &dev->flags);
Antti Palosaari77113892013-09-10 00:07:56 -0300948
Antti Palosaari2281c822014-12-03 20:11:03 -0300949 /* disable ADC */
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300950 if (d->props->frontend_ctrl)
951 d->props->frontend_ctrl(pdata->dvb_frontend, 0);
Antti Palosaari2281c822014-12-03 20:11:03 -0300952
Antti Palosaari3bda2bf2014-12-16 12:49:14 -0300953 if (d->props->power_ctrl)
954 d->props->power_ctrl(d, 0);
Antti Palosaari77113892013-09-10 00:07:56 -0300955
Antti Palosaari725b7f32014-12-15 14:30:02 -0300956 mutex_unlock(&dev->v4l2_lock);
Antti Palosaari77113892013-09-10 00:07:56 -0300957}
958
959static struct vb2_ops rtl2832_sdr_vb2_ops = {
960 .queue_setup = rtl2832_sdr_queue_setup,
961 .buf_prepare = rtl2832_sdr_buf_prepare,
962 .buf_queue = rtl2832_sdr_buf_queue,
963 .start_streaming = rtl2832_sdr_start_streaming,
964 .stop_streaming = rtl2832_sdr_stop_streaming,
965 .wait_prepare = vb2_ops_wait_prepare,
966 .wait_finish = vb2_ops_wait_finish,
967};
968
969static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
970 struct v4l2_tuner *v)
971{
Antti Palosaari725b7f32014-12-15 14:30:02 -0300972 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -0300973 struct platform_device *pdev = dev->pdev;
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300974 int ret;
Ovidiu Toader941a8202014-06-02 21:38:06 -0300975
Antti Palosaarie4e78692014-12-16 12:18:01 -0300976 dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
Antti Palosaari77113892013-09-10 00:07:56 -0300977
978 if (v->index == 0) {
979 strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
980 v->type = V4L2_TUNER_ADC;
981 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
982 v->rangelow = 300000;
983 v->rangehigh = 3200000;
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300984 ret = 0;
985 } else if (v->index == 1 &&
986 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
987 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
Antti Palosaari77113892013-09-10 00:07:56 -0300988 } else if (v->index == 1) {
989 strlcpy(v->name, "RF: <unknown>", sizeof(v->name));
990 v->type = V4L2_TUNER_RF;
991 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
992 v->rangelow = 50000000;
993 v->rangehigh = 2000000000;
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300994 ret = 0;
Antti Palosaari77113892013-09-10 00:07:56 -0300995 } else {
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300996 ret = -EINVAL;
Antti Palosaari77113892013-09-10 00:07:56 -0300997 }
Antti Palosaarifc3a0492015-05-03 21:45:43 -0300998 return ret;
Antti Palosaari77113892013-09-10 00:07:56 -0300999}
1000
1001static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
1002 const struct v4l2_tuner *v)
1003{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001004 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001005 struct platform_device *pdev = dev->pdev;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001006 int ret;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001007
Antti Palosaarie4e78692014-12-16 12:18:01 -03001008 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -03001009
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001010 if (v->index == 0) {
1011 ret = 0;
1012 } else if (v->index == 1 &&
1013 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
1014 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
1015 } else if (v->index == 1) {
1016 ret = 0;
1017 } else {
1018 ret = -EINVAL;
1019 }
1020 return ret;
Antti Palosaari77113892013-09-10 00:07:56 -03001021}
1022
1023static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
1024 struct v4l2_frequency_band *band)
1025{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001026 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001027 struct platform_device *pdev = dev->pdev;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001028 int ret;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001029
Antti Palosaarie4e78692014-12-16 12:18:01 -03001030 dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1031 band->tuner, band->type, band->index);
Antti Palosaari77113892013-09-10 00:07:56 -03001032
1033 if (band->tuner == 0) {
1034 if (band->index >= ARRAY_SIZE(bands_adc))
1035 return -EINVAL;
1036
1037 *band = bands_adc[band->index];
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001038 ret = 0;
1039 } else if (band->tuner == 1 &&
1040 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
1041 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
Antti Palosaari77113892013-09-10 00:07:56 -03001042 } else if (band->tuner == 1) {
1043 if (band->index >= ARRAY_SIZE(bands_fm))
1044 return -EINVAL;
1045
1046 *band = bands_fm[band->index];
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001047 ret = 0;
Antti Palosaari77113892013-09-10 00:07:56 -03001048 } else {
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001049 ret = -EINVAL;
Antti Palosaari77113892013-09-10 00:07:56 -03001050 }
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001051 return ret;
Antti Palosaari77113892013-09-10 00:07:56 -03001052}
1053
1054static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1055 struct v4l2_frequency *f)
1056{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001057 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001058 struct platform_device *pdev = dev->pdev;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001059 int ret;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001060
Antti Palosaarie4e78692014-12-16 12:18:01 -03001061 dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
Antti Palosaari77113892013-09-10 00:07:56 -03001062
Hans Verkuilbc908752014-03-05 20:21:38 -03001063 if (f->tuner == 0) {
Antti Palosaari725b7f32014-12-15 14:30:02 -03001064 f->frequency = dev->f_adc;
Hans Verkuilbc908752014-03-05 20:21:38 -03001065 f->type = V4L2_TUNER_ADC;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001066 ret = 0;
1067 } else if (f->tuner == 1 &&
1068 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
1069 f->type = V4L2_TUNER_RF;
1070 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
Hans Verkuilbc908752014-03-05 20:21:38 -03001071 } else if (f->tuner == 1) {
Antti Palosaari725b7f32014-12-15 14:30:02 -03001072 f->frequency = dev->f_tuner;
Hans Verkuilbc908752014-03-05 20:21:38 -03001073 f->type = V4L2_TUNER_RF;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001074 ret = 0;
Hans Verkuilbc908752014-03-05 20:21:38 -03001075 } else {
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001076 ret = -EINVAL;
Hans Verkuilbc908752014-03-05 20:21:38 -03001077 }
Antti Palosaari77113892013-09-10 00:07:56 -03001078 return ret;
1079}
1080
1081static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1082 const struct v4l2_frequency *f)
1083{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001084 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001085 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -03001086 int ret, band;
1087
Antti Palosaarie4e78692014-12-16 12:18:01 -03001088 dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1089 f->tuner, f->type, f->frequency);
Antti Palosaari77113892013-09-10 00:07:56 -03001090
1091 /* ADC band midpoints */
1092 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1093 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1094
1095 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1096 if (f->frequency < BAND_ADC_0)
1097 band = 0;
1098 else if (f->frequency < BAND_ADC_1)
1099 band = 1;
1100 else
1101 band = 2;
1102
Antti Palosaari725b7f32014-12-15 14:30:02 -03001103 dev->f_adc = clamp_t(unsigned int, f->frequency,
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001104 bands_adc[band].rangelow,
1105 bands_adc[band].rangehigh);
Antti Palosaari77113892013-09-10 00:07:56 -03001106
Antti Palosaarie4e78692014-12-16 12:18:01 -03001107 dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
Antti Palosaari725b7f32014-12-15 14:30:02 -03001108 ret = rtl2832_sdr_set_adc(dev);
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001109 } else if (f->tuner == 1 &&
1110 V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
1111 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
Antti Palosaari77113892013-09-10 00:07:56 -03001112 } else if (f->tuner == 1) {
Antti Palosaari725b7f32014-12-15 14:30:02 -03001113 dev->f_tuner = clamp_t(unsigned int, f->frequency,
Hans Verkuilbc908752014-03-05 20:21:38 -03001114 bands_fm[0].rangelow,
1115 bands_fm[0].rangehigh);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001116 dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
Antti Palosaari77113892013-09-10 00:07:56 -03001117
Antti Palosaari725b7f32014-12-15 14:30:02 -03001118 ret = rtl2832_sdr_set_tuner_freq(dev);
Antti Palosaari77113892013-09-10 00:07:56 -03001119 } else {
1120 ret = -EINVAL;
1121 }
Antti Palosaari77113892013-09-10 00:07:56 -03001122 return ret;
1123}
1124
1125static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1126 struct v4l2_fmtdesc *f)
1127{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001128 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001129 struct platform_device *pdev = dev->pdev;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001130
Antti Palosaarie4e78692014-12-16 12:18:01 -03001131 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -03001132
Antti Palosaari725b7f32014-12-15 14:30:02 -03001133 if (f->index >= dev->num_formats)
Antti Palosaari77113892013-09-10 00:07:56 -03001134 return -EINVAL;
1135
1136 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1137 f->pixelformat = formats[f->index].pixelformat;
1138
1139 return 0;
1140}
1141
1142static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1143 struct v4l2_format *f)
1144{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001145 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001146 struct platform_device *pdev = dev->pdev;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001147
Antti Palosaarie4e78692014-12-16 12:18:01 -03001148 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -03001149
Antti Palosaari725b7f32014-12-15 14:30:02 -03001150 f->fmt.sdr.pixelformat = dev->pixelformat;
1151 f->fmt.sdr.buffersize = dev->buffersize;
Antti Palosaarid57fe402014-07-18 16:22:29 -03001152
Hans Verkuilbc908752014-03-05 20:21:38 -03001153 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari77113892013-09-10 00:07:56 -03001154
1155 return 0;
1156}
1157
1158static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1159 struct v4l2_format *f)
1160{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001161 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001162 struct platform_device *pdev = dev->pdev;
Antti Palosaari725b7f32014-12-15 14:30:02 -03001163 struct vb2_queue *q = &dev->vb_queue;
Antti Palosaari77113892013-09-10 00:07:56 -03001164 int i;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001165
Antti Palosaarie4e78692014-12-16 12:18:01 -03001166 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1167 (char *)&f->fmt.sdr.pixelformat);
Antti Palosaari77113892013-09-10 00:07:56 -03001168
1169 if (vb2_is_busy(q))
1170 return -EBUSY;
1171
Hans Verkuilbc908752014-03-05 20:21:38 -03001172 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari725b7f32014-12-15 14:30:02 -03001173 for (i = 0; i < dev->num_formats; i++) {
Antti Palosaari77113892013-09-10 00:07:56 -03001174 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
Antti Palosaari725b7f32014-12-15 14:30:02 -03001175 dev->pixelformat = formats[i].pixelformat;
1176 dev->buffersize = formats[i].buffersize;
Antti Palosaarid57fe402014-07-18 16:22:29 -03001177 f->fmt.sdr.buffersize = formats[i].buffersize;
Antti Palosaari77113892013-09-10 00:07:56 -03001178 return 0;
1179 }
1180 }
1181
Antti Palosaari725b7f32014-12-15 14:30:02 -03001182 dev->pixelformat = formats[0].pixelformat;
1183 dev->buffersize = formats[0].buffersize;
Antti Palosaarid57fe402014-07-18 16:22:29 -03001184 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1185 f->fmt.sdr.buffersize = formats[0].buffersize;
Antti Palosaari77113892013-09-10 00:07:56 -03001186
1187 return 0;
1188}
1189
1190static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1191 struct v4l2_format *f)
1192{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001193 struct rtl2832_sdr_dev *dev = video_drvdata(file);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001194 struct platform_device *pdev = dev->pdev;
Antti Palosaari77113892013-09-10 00:07:56 -03001195 int i;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001196
Antti Palosaarie4e78692014-12-16 12:18:01 -03001197 dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1198 (char *)&f->fmt.sdr.pixelformat);
Antti Palosaari77113892013-09-10 00:07:56 -03001199
Hans Verkuilbc908752014-03-05 20:21:38 -03001200 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari725b7f32014-12-15 14:30:02 -03001201 for (i = 0; i < dev->num_formats; i++) {
Antti Palosaarid57fe402014-07-18 16:22:29 -03001202 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1203 f->fmt.sdr.buffersize = formats[i].buffersize;
Antti Palosaari77113892013-09-10 00:07:56 -03001204 return 0;
Antti Palosaarid57fe402014-07-18 16:22:29 -03001205 }
Antti Palosaari77113892013-09-10 00:07:56 -03001206 }
1207
1208 f->fmt.sdr.pixelformat = formats[0].pixelformat;
Antti Palosaarid57fe402014-07-18 16:22:29 -03001209 f->fmt.sdr.buffersize = formats[0].buffersize;
Antti Palosaari77113892013-09-10 00:07:56 -03001210
1211 return 0;
1212}
1213
1214static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1215 .vidioc_querycap = rtl2832_sdr_querycap,
1216
1217 .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap,
1218 .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap,
1219 .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap,
1220 .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap,
1221
1222 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1223 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1224 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1225 .vidioc_querybuf = vb2_ioctl_querybuf,
1226 .vidioc_qbuf = vb2_ioctl_qbuf,
1227 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1228
1229 .vidioc_streamon = vb2_ioctl_streamon,
1230 .vidioc_streamoff = vb2_ioctl_streamoff,
1231
1232 .vidioc_g_tuner = rtl2832_sdr_g_tuner,
1233 .vidioc_s_tuner = rtl2832_sdr_s_tuner,
1234
1235 .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands,
1236 .vidioc_g_frequency = rtl2832_sdr_g_frequency,
1237 .vidioc_s_frequency = rtl2832_sdr_s_frequency,
1238
1239 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1240 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1241 .vidioc_log_status = v4l2_ctrl_log_status,
1242};
1243
1244static const struct v4l2_file_operations rtl2832_sdr_fops = {
1245 .owner = THIS_MODULE,
1246 .open = v4l2_fh_open,
1247 .release = vb2_fop_release,
1248 .read = vb2_fop_read,
1249 .poll = vb2_fop_poll,
1250 .mmap = vb2_fop_mmap,
1251 .unlocked_ioctl = video_ioctl2,
1252};
1253
1254static struct video_device rtl2832_sdr_template = {
1255 .name = "Realtek RTL2832 SDR",
1256 .release = video_device_release_empty,
1257 .fops = &rtl2832_sdr_fops,
1258 .ioctl_ops = &rtl2832_sdr_ioctl_ops,
1259};
1260
1261static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1262{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001263 struct rtl2832_sdr_dev *dev =
1264 container_of(ctrl->handler, struct rtl2832_sdr_dev,
Antti Palosaari77113892013-09-10 00:07:56 -03001265 hdl);
Antti Palosaarie4e78692014-12-16 12:18:01 -03001266 struct platform_device *pdev = dev->pdev;
Antti Palosaari3bda2bf2014-12-16 12:49:14 -03001267 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1268 struct dvb_frontend *fe = pdata->dvb_frontend;
Antti Palosaari77113892013-09-10 00:07:56 -03001269 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1270 int ret;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001271
Antti Palosaarie4e78692014-12-16 12:18:01 -03001272 dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1273 ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1274 ctrl->step);
Antti Palosaari77113892013-09-10 00:07:56 -03001275
1276 switch (ctrl->id) {
1277 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1278 case V4L2_CID_RF_TUNER_BANDWIDTH:
Antti Palosaariea4d04f2014-03-10 14:28:45 -03001279 /* TODO: these controls should be moved to tuner drivers */
Antti Palosaari725b7f32014-12-15 14:30:02 -03001280 if (dev->bandwidth_auto->val) {
Antti Palosaariea4d04f2014-03-10 14:28:45 -03001281 /* Round towards the closest legal value */
Antti Palosaari725b7f32014-12-15 14:30:02 -03001282 s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
Antti Palosaariea4d04f2014-03-10 14:28:45 -03001283 u32 offset;
Ovidiu Toader941a8202014-06-02 21:38:06 -03001284
Antti Palosaari725b7f32014-12-15 14:30:02 -03001285 val = clamp_t(s32, val, dev->bandwidth->minimum,
1286 dev->bandwidth->maximum);
1287 offset = val - dev->bandwidth->minimum;
1288 offset = dev->bandwidth->step *
1289 div_u64(offset, dev->bandwidth->step);
1290 dev->bandwidth->val = dev->bandwidth->minimum + offset;
Antti Palosaariea4d04f2014-03-10 14:28:45 -03001291 }
Antti Palosaari725b7f32014-12-15 14:30:02 -03001292 c->bandwidth_hz = dev->bandwidth->val;
Antti Palosaari77113892013-09-10 00:07:56 -03001293
Antti Palosaari725b7f32014-12-15 14:30:02 -03001294 if (!test_bit(POWER_ON, &dev->flags))
Antti Palosaari77113892013-09-10 00:07:56 -03001295 return 0;
1296
1297 if (fe->ops.tuner_ops.set_params)
1298 ret = fe->ops.tuner_ops.set_params(fe);
1299 else
1300 ret = 0;
1301 break;
1302 default:
1303 ret = -EINVAL;
1304 }
1305
1306 return ret;
1307}
1308
1309static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1310 .s_ctrl = rtl2832_sdr_s_ctrl,
1311};
1312
1313static void rtl2832_sdr_video_release(struct v4l2_device *v)
1314{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001315 struct rtl2832_sdr_dev *dev =
1316 container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001317 struct platform_device *pdev = dev->pdev;
1318
1319 dev_dbg(&pdev->dev, "\n");
Antti Palosaari77113892013-09-10 00:07:56 -03001320
Antti Palosaari725b7f32014-12-15 14:30:02 -03001321 v4l2_ctrl_handler_free(&dev->hdl);
1322 v4l2_device_unregister(&dev->v4l2_dev);
1323 kfree(dev);
Antti Palosaari77113892013-09-10 00:07:56 -03001324}
1325
Antti Palosaari63bdab52014-12-16 10:09:20 -03001326/* Platform driver interface */
1327static int rtl2832_sdr_probe(struct platform_device *pdev)
Antti Palosaari77113892013-09-10 00:07:56 -03001328{
Antti Palosaari725b7f32014-12-15 14:30:02 -03001329 struct rtl2832_sdr_dev *dev;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001330 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
Antti Palosaari77113892013-09-10 00:07:56 -03001331 const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001332 struct v4l2_subdev *subdev;
1333 int ret;
Antti Palosaari77113892013-09-10 00:07:56 -03001334
Antti Palosaari63bdab52014-12-16 10:09:20 -03001335 dev_dbg(&pdev->dev, "\n");
1336
1337 if (!pdata) {
1338 dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1339 ret = -EINVAL;
1340 goto err;
1341 }
Antti Palosaarid5eec232014-12-17 08:25:03 -03001342 if (!pdev->dev.parent->driver) {
1343 dev_dbg(&pdev->dev, "No parent device\n");
1344 ret = -EINVAL;
1345 goto err;
1346 }
1347 /* try to refcount host drv since we are the consumer */
1348 if (!try_module_get(pdev->dev.parent->driver->owner)) {
1349 dev_err(&pdev->dev, "Refcount fail");
1350 ret = -EINVAL;
1351 goto err;
1352 }
Antti Palosaari63bdab52014-12-16 10:09:20 -03001353 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Antti Palosaari725b7f32014-12-15 14:30:02 -03001354 if (dev == NULL) {
Antti Palosaari63bdab52014-12-16 10:09:20 -03001355 ret = -ENOMEM;
Antti Palosaarid5eec232014-12-17 08:25:03 -03001356 goto err_module_put;
Antti Palosaari77113892013-09-10 00:07:56 -03001357 }
1358
1359 /* setup the state */
Antti Palosaari63bdab52014-12-16 10:09:20 -03001360 subdev = pdata->v4l2_subdev;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001361 dev->v4l2_subdev = pdata->v4l2_subdev;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001362 dev->pdev = pdev;
Peter Rosine01fc422016-05-04 22:15:34 +02001363 dev->regmap = pdata->regmap;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001364 dev->udev = pdata->dvb_usb_device->udev;
Antti Palosaari725b7f32014-12-15 14:30:02 -03001365 dev->f_adc = bands_adc[0].rangelow;
1366 dev->f_tuner = bands_fm[0].rangelow;
1367 dev->pixelformat = formats[0].pixelformat;
1368 dev->buffersize = formats[0].buffersize;
1369 dev->num_formats = NUM_FORMATS;
Mauro Carvalho Chehab2fe15e22014-09-03 15:25:39 -03001370 if (!rtl2832_sdr_emulated_fmt)
Antti Palosaari725b7f32014-12-15 14:30:02 -03001371 dev->num_formats -= 1;
Antti Palosaari77113892013-09-10 00:07:56 -03001372
Antti Palosaari725b7f32014-12-15 14:30:02 -03001373 mutex_init(&dev->v4l2_lock);
1374 mutex_init(&dev->vb_queue_lock);
1375 spin_lock_init(&dev->queued_bufs_lock);
1376 INIT_LIST_HEAD(&dev->queued_bufs);
Antti Palosaari77113892013-09-10 00:07:56 -03001377
1378 /* Init videobuf2 queue structure */
Antti Palosaari725b7f32014-12-15 14:30:02 -03001379 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1380 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1381 dev->vb_queue.drv_priv = dev;
1382 dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1383 dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1384 dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1385 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1386 ret = vb2_queue_init(&dev->vb_queue);
Antti Palosaari77113892013-09-10 00:07:56 -03001387 if (ret) {
Antti Palosaari63bdab52014-12-16 10:09:20 -03001388 dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1389 goto err_kfree;
Antti Palosaari77113892013-09-10 00:07:56 -03001390 }
1391
1392 /* Register controls */
Antti Palosaari63bdab52014-12-16 10:09:20 -03001393 switch (pdata->tuner) {
1394 case RTL2832_SDR_TUNER_E4000:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001395 v4l2_ctrl_handler_init(&dev->hdl, 9);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001396 if (subdev)
1397 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
Antti Palosaari77113892013-09-10 00:07:56 -03001398 break;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001399 case RTL2832_SDR_TUNER_R820T:
1400 case RTL2832_SDR_TUNER_R828D:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001401 v4l2_ctrl_handler_init(&dev->hdl, 2);
1402 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
Antti Palosaari63bdab52014-12-16 10:09:20 -03001403 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1404 0, 1, 1, 1);
Antti Palosaari725b7f32014-12-15 14:30:02 -03001405 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
Antti Palosaari63bdab52014-12-16 10:09:20 -03001406 V4L2_CID_RF_TUNER_BANDWIDTH,
1407 0, 8000000, 100000, 0);
Antti Palosaari725b7f32014-12-15 14:30:02 -03001408 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
Antti Palosaari77113892013-09-10 00:07:56 -03001409 break;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001410 case RTL2832_SDR_TUNER_FC0012:
1411 case RTL2832_SDR_TUNER_FC0013:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001412 v4l2_ctrl_handler_init(&dev->hdl, 2);
1413 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
Antti Palosaari63bdab52014-12-16 10:09:20 -03001414 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1415 0, 1, 1, 1);
Antti Palosaari725b7f32014-12-15 14:30:02 -03001416 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
Antti Palosaari63bdab52014-12-16 10:09:20 -03001417 V4L2_CID_RF_TUNER_BANDWIDTH,
1418 6000000, 8000000, 1000000,
1419 6000000);
Antti Palosaari725b7f32014-12-15 14:30:02 -03001420 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
Antti Palosaari77113892013-09-10 00:07:56 -03001421 break;
Antti Palosaarifc3a0492015-05-03 21:45:43 -03001422 case RTL2832_SDR_TUNER_FC2580:
1423 v4l2_ctrl_handler_init(&dev->hdl, 2);
1424 if (subdev)
1425 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1426 NULL);
1427 break;
Antti Palosaari77113892013-09-10 00:07:56 -03001428 default:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001429 v4l2_ctrl_handler_init(&dev->hdl, 0);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001430 dev_err(&pdev->dev, "Unsupported tuner\n");
1431 goto err_v4l2_ctrl_handler_free;
Antti Palosaari77113892013-09-10 00:07:56 -03001432 }
Antti Palosaari725b7f32014-12-15 14:30:02 -03001433 if (dev->hdl.error) {
1434 ret = dev->hdl.error;
Antti Palosaari63bdab52014-12-16 10:09:20 -03001435 dev_err(&pdev->dev, "Could not initialize controls\n");
1436 goto err_v4l2_ctrl_handler_free;
Antti Palosaari77113892013-09-10 00:07:56 -03001437 }
1438
1439 /* Init video_device structure */
Antti Palosaari725b7f32014-12-15 14:30:02 -03001440 dev->vdev = rtl2832_sdr_template;
1441 dev->vdev.queue = &dev->vb_queue;
1442 dev->vdev.queue->lock = &dev->vb_queue_lock;
1443 video_set_drvdata(&dev->vdev, dev);
Antti Palosaari77113892013-09-10 00:07:56 -03001444
1445 /* Register the v4l2_device structure */
Antti Palosaari725b7f32014-12-15 14:30:02 -03001446 dev->v4l2_dev.release = rtl2832_sdr_video_release;
Antti Palosaarie4e78692014-12-16 12:18:01 -03001447 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
Antti Palosaari77113892013-09-10 00:07:56 -03001448 if (ret) {
Antti Palosaari63bdab52014-12-16 10:09:20 -03001449 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1450 goto err_v4l2_ctrl_handler_free;
Antti Palosaari77113892013-09-10 00:07:56 -03001451 }
1452
Antti Palosaari725b7f32014-12-15 14:30:02 -03001453 dev->v4l2_dev.ctrl_handler = &dev->hdl;
1454 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1455 dev->vdev.lock = &dev->v4l2_lock;
1456 dev->vdev.vfl_dir = VFL_DIR_RX;
Antti Palosaari77113892013-09-10 00:07:56 -03001457
Antti Palosaari725b7f32014-12-15 14:30:02 -03001458 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
Antti Palosaari77113892013-09-10 00:07:56 -03001459 if (ret) {
Antti Palosaari63bdab52014-12-16 10:09:20 -03001460 dev_err(&pdev->dev, "Failed to register as video device %d\n",
1461 ret);
1462 goto err_v4l2_device_unregister;
Antti Palosaari77113892013-09-10 00:07:56 -03001463 }
Antti Palosaari63bdab52014-12-16 10:09:20 -03001464 dev_info(&pdev->dev, "Registered as %s\n",
1465 video_device_node_name(&dev->vdev));
1466 dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1467 dev_notice(&pdev->dev,
1468 "SDR API is still slightly experimental and functionality changes may follow\n");
1469 platform_set_drvdata(pdev, dev);
1470 return 0;
1471err_v4l2_device_unregister:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001472 v4l2_device_unregister(&dev->v4l2_dev);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001473err_v4l2_ctrl_handler_free:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001474 v4l2_ctrl_handler_free(&dev->hdl);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001475err_kfree:
Antti Palosaari725b7f32014-12-15 14:30:02 -03001476 kfree(dev);
Antti Palosaarid5eec232014-12-17 08:25:03 -03001477err_module_put:
1478 module_put(pdev->dev.parent->driver->owner);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001479err:
1480 return ret;
Antti Palosaari77113892013-09-10 00:07:56 -03001481}
Antti Palosaari63bdab52014-12-16 10:09:20 -03001482
1483static int rtl2832_sdr_remove(struct platform_device *pdev)
1484{
1485 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1486
1487 dev_dbg(&pdev->dev, "\n");
1488
1489 mutex_lock(&dev->vb_queue_lock);
1490 mutex_lock(&dev->v4l2_lock);
1491 /* No need to keep the urbs around after disconnection */
1492 dev->udev = NULL;
1493 v4l2_device_disconnect(&dev->v4l2_dev);
1494 video_unregister_device(&dev->vdev);
1495 mutex_unlock(&dev->v4l2_lock);
1496 mutex_unlock(&dev->vb_queue_lock);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001497 v4l2_device_put(&dev->v4l2_dev);
Antti Palosaarid5eec232014-12-17 08:25:03 -03001498 module_put(pdev->dev.parent->driver->owner);
Antti Palosaari63bdab52014-12-16 10:09:20 -03001499
1500 return 0;
1501}
1502
1503static struct platform_driver rtl2832_sdr_driver = {
1504 .driver = {
1505 .name = "rtl2832_sdr",
Antti Palosaari63bdab52014-12-16 10:09:20 -03001506 },
1507 .probe = rtl2832_sdr_probe,
1508 .remove = rtl2832_sdr_remove,
1509};
1510module_platform_driver(rtl2832_sdr_driver);
Antti Palosaari77113892013-09-10 00:07:56 -03001511
1512MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1513MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1514MODULE_LICENSE("GPL");