Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1 | /* |
| 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 25 | #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 Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 32 | #include <media/videobuf2-v4l2.h> |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 33 | #include <media/videobuf2-vmalloc.h> |
| 34 | |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 35 | #include <linux/platform_device.h> |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 36 | #include <linux/jiffies.h> |
| 37 | #include <linux/math64.h> |
Peter Rosin | 1cf79db | 2016-05-04 22:15:33 +0200 | [diff] [blame] | 38 | #include <linux/regmap.h> |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 39 | |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 40 | static bool rtl2832_sdr_emulated_fmt; |
| 41 | module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644); |
| 42 | MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)"); |
| 43 | |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 44 | /* 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 48 | #define MAX_BULK_BUFS (10) |
| 49 | #define BULK_BUFFER_SIZE (128 * 512) |
| 50 | |
| 51 | static 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 | |
| 78 | static 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 */ |
| 90 | struct rtl2832_sdr_format { |
| 91 | char *name; |
| 92 | u32 pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 93 | u32 buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static struct rtl2832_sdr_format formats[] = { |
| 97 | { |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 98 | .name = "Complex U8", |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 99 | .pixelformat = V4L2_SDR_FMT_CU8, |
| 100 | .buffersize = BULK_BUFFER_SIZE, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 101 | }, { |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 102 | .name = "Complex U16LE (emulated)", |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 103 | .pixelformat = V4L2_SDR_FMT_CU16LE, |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 104 | .buffersize = BULK_BUFFER_SIZE * 2, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 105 | }, |
| 106 | }; |
| 107 | |
| 108 | static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); |
| 109 | |
| 110 | /* intermediate buffers with raw data from the USB device */ |
| 111 | struct rtl2832_sdr_frame_buf { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 112 | /* common v4l buffer stuff -- must be first */ |
| 113 | struct vb2_v4l2_buffer vb; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 114 | struct list_head list; |
| 115 | }; |
| 116 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 117 | struct rtl2832_sdr_dev { |
Dan Carpenter | e4d131d | 2015-05-14 05:39:17 -0300 | [diff] [blame] | 118 | #define POWER_ON 0 /* BIT(0) */ |
| 119 | #define URB_BUF 1 /* BIT(1) */ |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 120 | unsigned long flags; |
| 121 | |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 122 | struct platform_device *pdev; |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 123 | struct regmap *regmap; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 124 | |
| 125 | struct video_device vdev; |
| 126 | struct v4l2_device v4l2_dev; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 127 | struct v4l2_subdev *v4l2_subdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 128 | |
| 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 Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 133 | unsigned sequence; /* buffer sequence counter */ |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 134 | |
| 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 Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 154 | u32 buffersize; |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 155 | unsigned int num_formats; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 156 | |
| 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 168 | /* Private functions */ |
| 169 | static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf( |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 170 | struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 171 | { |
Antti Palosaari | abfc8d6 | 2014-08-24 23:37:57 -0300 | [diff] [blame] | 172 | unsigned long flags; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 173 | struct rtl2832_sdr_frame_buf *buf = NULL; |
| 174 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 175 | spin_lock_irqsave(&dev->queued_bufs_lock, flags); |
| 176 | if (list_empty(&dev->queued_bufs)) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 177 | goto leave; |
| 178 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 179 | buf = list_entry(dev->queued_bufs.next, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 180 | struct rtl2832_sdr_frame_buf, list); |
| 181 | list_del(&buf->list); |
| 182 | leave: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 183 | spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 184 | return buf; |
| 185 | } |
| 186 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 187 | static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 188 | void *dst, const u8 *src, unsigned int src_len) |
| 189 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 190 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 191 | unsigned int dst_len; |
| 192 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 193 | if (dev->pixelformat == V4L2_SDR_FMT_CU8) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 194 | /* native stream, no need to convert */ |
| 195 | memcpy(dst, src, src_len); |
| 196 | dst_len = src_len; |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 197 | } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 198 | /* convert u8 to u16 */ |
| 199 | unsigned int i; |
| 200 | u16 *u16dst = dst; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 201 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 202 | 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 Palosaari | b538a8e | 2014-08-24 23:44:13 -0300 | [diff] [blame] | 209 | /* calculate sample rate and output it in 10 seconds intervals */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 210 | if (unlikely(time_is_before_jiffies(dev->jiffies_next))) { |
Antti Palosaari | b538a8e | 2014-08-24 23:44:13 -0300 | [diff] [blame] | 211 | #define MSECS 10000UL |
| 212 | unsigned int msecs = jiffies_to_msecs(jiffies - |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 213 | dev->jiffies_next + msecs_to_jiffies(MSECS)); |
| 214 | unsigned int samples = dev->sample - dev->sample_measured; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 215 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 216 | dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS); |
| 217 | dev->sample_measured = dev->sample; |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 218 | dev_dbg(&pdev->dev, |
| 219 | "slen=%u samples=%u msecs=%u sample rate=%lu\n", |
| 220 | src_len, samples, msecs, samples * 1000UL / msecs); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* total number of I+Q pairs */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 224 | dev->sample += src_len / 2; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 225 | |
| 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 | */ |
| 233 | static void rtl2832_sdr_urb_complete(struct urb *urb) |
| 234 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 235 | struct rtl2832_sdr_dev *dev = urb->context; |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 236 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 237 | struct rtl2832_sdr_frame_buf *fbuf; |
| 238 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 239 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 242 | |
| 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 Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 252 | dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | |
| 256 | if (likely(urb->actual_length > 0)) { |
| 257 | void *ptr; |
| 258 | unsigned int len; |
| 259 | /* get free framebuffer */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 260 | fbuf = rtl2832_sdr_get_next_fill_buf(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 261 | if (unlikely(fbuf == NULL)) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 262 | dev->vb_full++; |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 263 | dev_notice_ratelimited(&pdev->dev, |
| 264 | "videobuf is full, %d packets dropped\n", |
| 265 | dev->vb_full); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 266 | goto skip; |
| 267 | } |
| 268 | |
| 269 | /* fill framebuffer */ |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 270 | ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 271 | len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 272 | urb->actual_length); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 273 | vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len); |
Junghak Sung | d6dd645 | 2015-11-03 08:16:37 -0200 | [diff] [blame] | 274 | fbuf->vb.vb2_buf.timestamp = ktime_get_ns(); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 275 | fbuf->vb.sequence = dev->sequence++; |
| 276 | vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 277 | } |
| 278 | skip: |
| 279 | usb_submit_urb(urb, GFP_ATOMIC); |
| 280 | } |
| 281 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 282 | static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 283 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 284 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 285 | int i; |
| 286 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 287 | for (i = dev->urbs_submitted - 1; i >= 0; i--) { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 288 | dev_dbg(&pdev->dev, "kill urb=%d\n", i); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 289 | /* stop the URB */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 290 | usb_kill_urb(dev->urb_list[i]); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 291 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 292 | dev->urbs_submitted = 0; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 297 | static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 298 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 299 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 300 | int i, ret; |
| 301 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 302 | for (i = 0; i < dev->urbs_initialized; i++) { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 303 | dev_dbg(&pdev->dev, "submit urb=%d\n", i); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 304 | ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 305 | if (ret) { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 306 | dev_err(&pdev->dev, |
| 307 | "Could not submit urb no. %d - get them all back\n", |
| 308 | i); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 309 | rtl2832_sdr_kill_urbs(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 310 | return ret; |
| 311 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 312 | dev->urbs_submitted++; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 318 | static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 319 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 320 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 321 | |
Dan Carpenter | e4d131d | 2015-05-14 05:39:17 -0300 | [diff] [blame] | 322 | if (test_bit(URB_BUF, &dev->flags)) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 323 | while (dev->buf_num) { |
| 324 | dev->buf_num--; |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 325 | dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 326 | usb_free_coherent(dev->udev, dev->buf_size, |
| 327 | dev->buf_list[dev->buf_num], |
| 328 | dev->dma_addr[dev->buf_num]); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 329 | } |
| 330 | } |
Dan Carpenter | e4d131d | 2015-05-14 05:39:17 -0300 | [diff] [blame] | 331 | clear_bit(URB_BUF, &dev->flags); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 336 | static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 337 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 338 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 339 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 340 | dev->buf_num = 0; |
| 341 | dev->buf_size = BULK_BUFFER_SIZE; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 342 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 343 | dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n", |
| 344 | MAX_BULK_BUFS * BULK_BUFFER_SIZE); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 345 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 346 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 348 | BULK_BUFFER_SIZE, GFP_ATOMIC, |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 349 | &dev->dma_addr[dev->buf_num]); |
| 350 | if (!dev->buf_list[dev->buf_num]) { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 351 | dev_dbg(&pdev->dev, "alloc buf=%d failed\n", |
| 352 | dev->buf_num); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 353 | rtl2832_sdr_free_stream_bufs(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 354 | return -ENOMEM; |
| 355 | } |
| 356 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 357 | 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 Carpenter | e4d131d | 2015-05-14 05:39:17 -0300 | [diff] [blame] | 360 | set_bit(URB_BUF, &dev->flags); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 366 | static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 367 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 368 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 369 | int i; |
| 370 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 371 | rtl2832_sdr_kill_urbs(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 372 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 373 | for (i = dev->urbs_initialized - 1; i >= 0; i--) { |
| 374 | if (dev->urb_list[i]) { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 375 | dev_dbg(&pdev->dev, "free urb=%d\n", i); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 376 | /* free the URBs */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 377 | usb_free_urb(dev->urb_list[i]); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 378 | } |
| 379 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 380 | dev->urbs_initialized = 0; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 385 | static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 386 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 387 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 388 | int i, j; |
| 389 | |
| 390 | /* allocate the URBs */ |
| 391 | for (i = 0; i < MAX_BULK_BUFS; i++) { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 392 | dev_dbg(&pdev->dev, "alloc urb=%d\n", i); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 393 | dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); |
| 394 | if (!dev->urb_list[i]) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 395 | for (j = 0; j < i; j++) |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 396 | usb_free_urb(dev->urb_list[j]); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 397 | return -ENOMEM; |
| 398 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 399 | usb_fill_bulk_urb(dev->urb_list[i], |
| 400 | dev->udev, |
| 401 | usb_rcvbulkpipe(dev->udev, 0x81), |
| 402 | dev->buf_list[i], |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 403 | BULK_BUFFER_SIZE, |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 404 | rtl2832_sdr_urb_complete, dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 405 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 406 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* Must be called with vb_queue_lock hold */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 415 | static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 416 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 417 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | abfc8d6 | 2014-08-24 23:37:57 -0300 | [diff] [blame] | 418 | unsigned long flags; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 419 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 420 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 421 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 422 | spin_lock_irqsave(&dev->queued_bufs_lock, flags); |
| 423 | while (!list_empty(&dev->queued_bufs)) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 424 | struct rtl2832_sdr_frame_buf *buf; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 425 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 426 | buf = list_entry(dev->queued_bufs.next, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 427 | struct rtl2832_sdr_frame_buf, list); |
| 428 | list_del(&buf->list); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 429 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 430 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 431 | spin_unlock_irqrestore(&dev->queued_bufs_lock, flags); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 432 | } |
| 433 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 434 | static int rtl2832_sdr_querycap(struct file *file, void *fh, |
| 435 | struct v4l2_capability *cap) |
| 436 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 437 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 438 | struct platform_device *pdev = dev->pdev; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 439 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 440 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 441 | |
| 442 | strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 443 | strlcpy(cap->card, dev->vdev.name, sizeof(cap->card)); |
| 444 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 445 | 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 */ |
| 452 | static int rtl2832_sdr_queue_setup(struct vb2_queue *vq, |
Hans Verkuil | df9ecb0 | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 453 | unsigned int *nbuffers, |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 454 | unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 455 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 456 | struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 457 | struct platform_device *pdev = dev->pdev; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 458 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 459 | dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 460 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 461 | /* Need at least 8 buffers */ |
| 462 | if (vq->num_buffers + *nbuffers < 8) |
| 463 | *nbuffers = 8 - vq->num_buffers; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 464 | *nplanes = 1; |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 465 | sizes[0] = PAGE_ALIGN(dev->buffersize); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 466 | dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb) |
| 471 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 472 | struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 473 | |
| 474 | /* Don't allow queing new buffers after device disconnection */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 475 | if (!dev->udev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 476 | return -ENODEV; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb) |
| 482 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 483 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 484 | struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 485 | struct rtl2832_sdr_frame_buf *buf = |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 486 | container_of(vbuf, struct rtl2832_sdr_frame_buf, vb); |
Antti Palosaari | abfc8d6 | 2014-08-24 23:37:57 -0300 | [diff] [blame] | 487 | unsigned long flags; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 488 | |
| 489 | /* Check the device has not disconnected between prep and queuing */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 490 | if (!dev->udev) { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 491 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 495 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 498 | } |
| 499 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 500 | static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 501 | { |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 502 | struct platform_device *pdev = dev->pdev; |
| 503 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 504 | struct dvb_frontend *fe = pdata->dvb_frontend; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 505 | int ret; |
| 506 | unsigned int f_sr, f_if; |
| 507 | u8 buf[4], u8tmp1, u8tmp2; |
| 508 | u64 u64tmp; |
| 509 | u32 u32tmp; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 510 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 511 | dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 512 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 513 | if (!test_bit(POWER_ON, &dev->flags)) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 514 | return 0; |
| 515 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 516 | if (dev->f_adc == 0) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 517 | return 0; |
| 518 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 519 | f_sr = dev->f_adc; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 520 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 521 | ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 522 | if (ret) |
| 523 | goto err; |
| 524 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 525 | ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 526 | 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 Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 539 | u64tmp = f_if % pdata->clk; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 540 | u64tmp *= 0x400000; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 541 | u64tmp = div_u64(u64tmp, pdata->clk); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 542 | u64tmp = -u64tmp; |
| 543 | u32tmp = u64tmp & 0x3fffff; |
| 544 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 545 | dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 546 | |
| 547 | buf[0] = (u32tmp >> 16) & 0xff; |
| 548 | buf[1] = (u32tmp >> 8) & 0xff; |
| 549 | buf[2] = (u32tmp >> 0) & 0xff; |
| 550 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 551 | ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 552 | 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 Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 565 | ret = regmap_write(dev->regmap, 0x1b1, u8tmp1); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 566 | if (ret) |
| 567 | goto err; |
| 568 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 569 | ret = regmap_write(dev->regmap, 0x008, u8tmp2); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 570 | if (ret) |
| 571 | goto err; |
| 572 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 573 | ret = regmap_write(dev->regmap, 0x006, 0x80); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 574 | if (ret) |
| 575 | goto err; |
| 576 | |
| 577 | /* program sampling rate (resampling down) */ |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 578 | u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 579 | 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 Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 584 | ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 585 | if (ret) |
| 586 | goto err; |
| 587 | |
| 588 | /* low-pass filter */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 589 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 592 | if (ret) |
| 593 | goto err; |
| 594 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 595 | ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 596 | if (ret) |
| 597 | goto err; |
| 598 | |
| 599 | /* mode */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 600 | ret = regmap_write(dev->regmap, 0x019, 0x05); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 601 | if (ret) |
| 602 | goto err; |
| 603 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 604 | ret = regmap_bulk_write(dev->regmap, 0x01a, |
| 605 | "\x1b\x16\x0d\x06\x01\xff", 6); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 606 | if (ret) |
| 607 | goto err; |
| 608 | |
| 609 | /* FSM */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 610 | ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 611 | if (ret) |
| 612 | goto err; |
| 613 | |
| 614 | /* PID filter */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 615 | ret = regmap_write(dev->regmap, 0x061, 0x60); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 616 | if (ret) |
| 617 | goto err; |
| 618 | |
| 619 | /* used RF tuner based settings */ |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 620 | switch (pdata->tuner) { |
| 621 | case RTL2832_SDR_TUNER_E4000: |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 622 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 656 | break; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 657 | case RTL2832_SDR_TUNER_FC0012: |
| 658 | case RTL2832_SDR_TUNER_FC0013: |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 659 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 688 | break; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 689 | case RTL2832_SDR_TUNER_R820T: |
| 690 | case RTL2832_SDR_TUNER_R828D: |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 691 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 713 | break; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 714 | case RTL2832_SDR_TUNER_FC2580: |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 715 | 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 Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 736 | break; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 737 | default: |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 738 | dev_notice(&pdev->dev, "Unsupported tuner\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* software reset */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 742 | ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 743 | if (ret) |
| 744 | goto err; |
| 745 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 746 | ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 747 | if (ret) |
| 748 | goto err; |
| 749 | err: |
| 750 | return ret; |
| 751 | }; |
| 752 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 753 | static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 754 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 755 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 756 | int ret; |
| 757 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 758 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 759 | |
| 760 | /* PID filter */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 761 | ret = regmap_write(dev->regmap, 0x061, 0xe0); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 762 | if (ret) |
| 763 | goto err; |
| 764 | |
| 765 | /* mode */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 766 | ret = regmap_write(dev->regmap, 0x019, 0x20); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 767 | if (ret) |
| 768 | goto err; |
| 769 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 770 | ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 771 | if (ret) |
| 772 | goto err; |
| 773 | |
| 774 | /* FSM */ |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 775 | ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 776 | if (ret) |
| 777 | goto err; |
| 778 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 779 | ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 780 | if (ret) |
| 781 | goto err; |
| 782 | |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 783 | ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 784 | if (ret) |
| 785 | goto err; |
| 786 | err: |
| 787 | return; |
| 788 | }; |
| 789 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 790 | static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 791 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 792 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 793 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
| 794 | struct dvb_frontend *fe = pdata->dvb_frontend; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 795 | 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 Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 802 | if (dev->f_tuner == 0) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 803 | return 0; |
| 804 | |
| 805 | /* |
| 806 | * bandwidth (Hz) |
| 807 | */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 808 | bandwidth_auto = v4l2_ctrl_find(&dev->hdl, |
Anthony DeStefano | 8559496 | 2014-04-09 23:08:49 -0300 | [diff] [blame] | 809 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 810 | bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 811 | if (v4l2_ctrl_g_ctrl(bandwidth_auto)) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 812 | c->bandwidth_hz = dev->f_adc; |
| 813 | v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 814 | } else { |
| 815 | c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth); |
| 816 | } |
| 817 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 818 | c->frequency = dev->f_tuner; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 819 | c->delivery_system = SYS_DVBT; |
| 820 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 821 | dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n", |
| 822 | c->frequency, c->bandwidth_hz); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 823 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 824 | if (!test_bit(POWER_ON, &dev->flags)) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 825 | return 0; |
| 826 | |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 827 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 831 | |
| 832 | return 0; |
| 833 | }; |
| 834 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 835 | static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 836 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 837 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 838 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
| 839 | struct dvb_frontend *fe = pdata->dvb_frontend; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 840 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 841 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 842 | |
| 843 | if (fe->ops.tuner_ops.init) |
| 844 | fe->ops.tuner_ops.init(fe); |
| 845 | |
| 846 | return 0; |
| 847 | }; |
| 848 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 849 | static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 850 | { |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 851 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 852 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
| 853 | struct dvb_frontend *fe = pdata->dvb_frontend; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 854 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 855 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 856 | |
| 857 | if (fe->ops.tuner_ops.sleep) |
| 858 | fe->ops.tuner_ops.sleep(fe); |
| 859 | |
| 860 | return; |
| 861 | }; |
| 862 | |
| 863 | static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count) |
| 864 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 865 | struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 866 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 867 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
| 868 | struct dvb_usb_device *d = pdata->dvb_usb_device; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 869 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 870 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 871 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 872 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 873 | if (!dev->udev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 874 | return -ENODEV; |
| 875 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 876 | if (mutex_lock_interruptible(&dev->v4l2_lock)) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 877 | return -ERESTARTSYS; |
| 878 | |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 879 | if (d->props->power_ctrl) |
| 880 | d->props->power_ctrl(d, 1); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 881 | |
Antti Palosaari | 2281c82 | 2014-12-03 20:11:03 -0300 | [diff] [blame] | 882 | /* enable ADC */ |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 883 | if (d->props->frontend_ctrl) |
| 884 | d->props->frontend_ctrl(pdata->dvb_frontend, 1); |
Antti Palosaari | 2281c82 | 2014-12-03 20:11:03 -0300 | [diff] [blame] | 885 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 886 | set_bit(POWER_ON, &dev->flags); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 887 | |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 888 | /* 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 893 | if (ret) |
| 894 | goto err; |
| 895 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 896 | ret = rtl2832_sdr_set_tuner_freq(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 897 | if (ret) |
| 898 | goto err; |
| 899 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 900 | ret = rtl2832_sdr_set_adc(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 901 | if (ret) |
| 902 | goto err; |
| 903 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 904 | ret = rtl2832_sdr_alloc_stream_bufs(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 905 | if (ret) |
| 906 | goto err; |
| 907 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 908 | ret = rtl2832_sdr_alloc_urbs(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 909 | if (ret) |
| 910 | goto err; |
| 911 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 912 | dev->sequence = 0; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 913 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 914 | ret = rtl2832_sdr_submit_urbs(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 915 | if (ret) |
| 916 | goto err; |
| 917 | |
| 918 | err: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 919 | mutex_unlock(&dev->v4l2_lock); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 920 | |
| 921 | return ret; |
| 922 | } |
| 923 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 924 | static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 925 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 926 | struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 927 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 928 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
| 929 | struct dvb_usb_device *d = pdata->dvb_usb_device; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 930 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 931 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 932 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 933 | mutex_lock(&dev->v4l2_lock); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 934 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 935 | 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 Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 940 | |
| 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 946 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 947 | clear_bit(POWER_ON, &dev->flags); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 948 | |
Antti Palosaari | 2281c82 | 2014-12-03 20:11:03 -0300 | [diff] [blame] | 949 | /* disable ADC */ |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 950 | if (d->props->frontend_ctrl) |
| 951 | d->props->frontend_ctrl(pdata->dvb_frontend, 0); |
Antti Palosaari | 2281c82 | 2014-12-03 20:11:03 -0300 | [diff] [blame] | 952 | |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 953 | if (d->props->power_ctrl) |
| 954 | d->props->power_ctrl(d, 0); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 955 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 956 | mutex_unlock(&dev->v4l2_lock); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static 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 | |
| 969 | static int rtl2832_sdr_g_tuner(struct file *file, void *priv, |
| 970 | struct v4l2_tuner *v) |
| 971 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 972 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 973 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 974 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 975 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 976 | dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 977 | |
| 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 Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 984 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 988 | } 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 Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 994 | ret = 0; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 995 | } else { |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 996 | ret = -EINVAL; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 997 | } |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 998 | return ret; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static int rtl2832_sdr_s_tuner(struct file *file, void *priv, |
| 1002 | const struct v4l2_tuner *v) |
| 1003 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1004 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1005 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1006 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1007 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1008 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1009 | |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1010 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv, |
| 1024 | struct v4l2_frequency_band *band) |
| 1025 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1026 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1027 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1028 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1029 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1030 | dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n", |
| 1031 | band->tuner, band->type, band->index); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1032 | |
| 1033 | if (band->tuner == 0) { |
| 1034 | if (band->index >= ARRAY_SIZE(bands_adc)) |
| 1035 | return -EINVAL; |
| 1036 | |
| 1037 | *band = bands_adc[band->index]; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1038 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1042 | } else if (band->tuner == 1) { |
| 1043 | if (band->index >= ARRAY_SIZE(bands_fm)) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | *band = bands_fm[band->index]; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1047 | ret = 0; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1048 | } else { |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1049 | ret = -EINVAL; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1050 | } |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1051 | return ret; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | static int rtl2832_sdr_g_frequency(struct file *file, void *priv, |
| 1055 | struct v4l2_frequency *f) |
| 1056 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1057 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1058 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1059 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1060 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1061 | dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1062 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1063 | if (f->tuner == 0) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1064 | f->frequency = dev->f_adc; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1065 | f->type = V4L2_TUNER_ADC; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1066 | 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 Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1071 | } else if (f->tuner == 1) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1072 | f->frequency = dev->f_tuner; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1073 | f->type = V4L2_TUNER_RF; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1074 | ret = 0; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1075 | } else { |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1076 | ret = -EINVAL; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1077 | } |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | static int rtl2832_sdr_s_frequency(struct file *file, void *priv, |
| 1082 | const struct v4l2_frequency *f) |
| 1083 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1084 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1085 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1086 | int ret, band; |
| 1087 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1088 | dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n", |
| 1089 | f->tuner, f->type, f->frequency); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1090 | |
| 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 Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1103 | dev->f_adc = clamp_t(unsigned int, f->frequency, |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1104 | bands_adc[band].rangelow, |
| 1105 | bands_adc[band].rangehigh); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1106 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1107 | dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1108 | ret = rtl2832_sdr_set_adc(dev); |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1109 | } 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1112 | } else if (f->tuner == 1) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1113 | dev->f_tuner = clamp_t(unsigned int, f->frequency, |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1114 | bands_fm[0].rangelow, |
| 1115 | bands_fm[0].rangehigh); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1116 | dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1117 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1118 | ret = rtl2832_sdr_set_tuner_freq(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1119 | } else { |
| 1120 | ret = -EINVAL; |
| 1121 | } |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv, |
| 1126 | struct v4l2_fmtdesc *f) |
| 1127 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1128 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1129 | struct platform_device *pdev = dev->pdev; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1130 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1131 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1132 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1133 | if (f->index >= dev->num_formats) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1134 | 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 | |
| 1142 | static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv, |
| 1143 | struct v4l2_format *f) |
| 1144 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1145 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1146 | struct platform_device *pdev = dev->pdev; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1147 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1148 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1149 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1150 | f->fmt.sdr.pixelformat = dev->pixelformat; |
| 1151 | f->fmt.sdr.buffersize = dev->buffersize; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1152 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1153 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1154 | |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv, |
| 1159 | struct v4l2_format *f) |
| 1160 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1161 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1162 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1163 | struct vb2_queue *q = &dev->vb_queue; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1164 | int i; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1165 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1166 | dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", |
| 1167 | (char *)&f->fmt.sdr.pixelformat); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1168 | |
| 1169 | if (vb2_is_busy(q)) |
| 1170 | return -EBUSY; |
| 1171 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1172 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1173 | for (i = 0; i < dev->num_formats; i++) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1174 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1175 | dev->pixelformat = formats[i].pixelformat; |
| 1176 | dev->buffersize = formats[i].buffersize; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1177 | f->fmt.sdr.buffersize = formats[i].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1178 | return 0; |
| 1179 | } |
| 1180 | } |
| 1181 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1182 | dev->pixelformat = formats[0].pixelformat; |
| 1183 | dev->buffersize = formats[0].buffersize; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1184 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
| 1185 | f->fmt.sdr.buffersize = formats[0].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv, |
| 1191 | struct v4l2_format *f) |
| 1192 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1193 | struct rtl2832_sdr_dev *dev = video_drvdata(file); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1194 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1195 | int i; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1196 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1197 | dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n", |
| 1198 | (char *)&f->fmt.sdr.pixelformat); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1199 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1200 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1201 | for (i = 0; i < dev->num_formats; i++) { |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1202 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { |
| 1203 | f->fmt.sdr.buffersize = formats[i].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1204 | return 0; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1205 | } |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1209 | f->fmt.sdr.buffersize = formats[0].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | static 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 | |
| 1244 | static 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 | |
| 1254 | static 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 | |
| 1261 | static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1262 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1263 | struct rtl2832_sdr_dev *dev = |
| 1264 | container_of(ctrl->handler, struct rtl2832_sdr_dev, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1265 | hdl); |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1266 | struct platform_device *pdev = dev->pdev; |
Antti Palosaari | 3bda2bf | 2014-12-16 12:49:14 -0300 | [diff] [blame] | 1267 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
| 1268 | struct dvb_frontend *fe = pdata->dvb_frontend; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1269 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1270 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1271 | |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1272 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1275 | |
| 1276 | switch (ctrl->id) { |
| 1277 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 1278 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1279 | /* TODO: these controls should be moved to tuner drivers */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1280 | if (dev->bandwidth_auto->val) { |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1281 | /* Round towards the closest legal value */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1282 | s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2); |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1283 | u32 offset; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1284 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1285 | 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 Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1291 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1292 | c->bandwidth_hz = dev->bandwidth->val; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1293 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1294 | if (!test_bit(POWER_ON, &dev->flags)) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1295 | 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 | |
| 1309 | static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = { |
| 1310 | .s_ctrl = rtl2832_sdr_s_ctrl, |
| 1311 | }; |
| 1312 | |
| 1313 | static void rtl2832_sdr_video_release(struct v4l2_device *v) |
| 1314 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1315 | struct rtl2832_sdr_dev *dev = |
| 1316 | container_of(v, struct rtl2832_sdr_dev, v4l2_dev); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1317 | struct platform_device *pdev = dev->pdev; |
| 1318 | |
| 1319 | dev_dbg(&pdev->dev, "\n"); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1320 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1321 | v4l2_ctrl_handler_free(&dev->hdl); |
| 1322 | v4l2_device_unregister(&dev->v4l2_dev); |
| 1323 | kfree(dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1324 | } |
| 1325 | |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1326 | /* Platform driver interface */ |
| 1327 | static int rtl2832_sdr_probe(struct platform_device *pdev) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1328 | { |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1329 | struct rtl2832_sdr_dev *dev; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1330 | struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1331 | const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1332 | struct v4l2_subdev *subdev; |
| 1333 | int ret; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1334 | |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1335 | 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 Palosaari | d5eec23 | 2014-12-17 08:25:03 -0300 | [diff] [blame] | 1342 | 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 Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1353 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1354 | if (dev == NULL) { |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1355 | ret = -ENOMEM; |
Antti Palosaari | d5eec23 | 2014-12-17 08:25:03 -0300 | [diff] [blame] | 1356 | goto err_module_put; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /* setup the state */ |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1360 | subdev = pdata->v4l2_subdev; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1361 | dev->v4l2_subdev = pdata->v4l2_subdev; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1362 | dev->pdev = pdev; |
Peter Rosin | e01fc42 | 2016-05-04 22:15:34 +0200 | [diff] [blame] | 1363 | dev->regmap = pdata->regmap; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1364 | dev->udev = pdata->dvb_usb_device->udev; |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1365 | 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 Chehab | 2fe15e2 | 2014-09-03 15:25:39 -0300 | [diff] [blame] | 1370 | if (!rtl2832_sdr_emulated_fmt) |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1371 | dev->num_formats -= 1; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1372 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1373 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1377 | |
| 1378 | /* Init videobuf2 queue structure */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1379 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1387 | if (ret) { |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1388 | dev_err(&pdev->dev, "Could not initialize vb2 queue\n"); |
| 1389 | goto err_kfree; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* Register controls */ |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1393 | switch (pdata->tuner) { |
| 1394 | case RTL2832_SDR_TUNER_E4000: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1395 | v4l2_ctrl_handler_init(&dev->hdl, 9); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1396 | if (subdev) |
| 1397 | v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1398 | break; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1399 | case RTL2832_SDR_TUNER_R820T: |
| 1400 | case RTL2832_SDR_TUNER_R828D: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1401 | v4l2_ctrl_handler_init(&dev->hdl, 2); |
| 1402 | dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1403 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, |
| 1404 | 0, 1, 1, 1); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1405 | dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1406 | V4L2_CID_RF_TUNER_BANDWIDTH, |
| 1407 | 0, 8000000, 100000, 0); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1408 | v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1409 | break; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1410 | case RTL2832_SDR_TUNER_FC0012: |
| 1411 | case RTL2832_SDR_TUNER_FC0013: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1412 | v4l2_ctrl_handler_init(&dev->hdl, 2); |
| 1413 | dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops, |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1414 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, |
| 1415 | 0, 1, 1, 1); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1416 | dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops, |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1417 | V4L2_CID_RF_TUNER_BANDWIDTH, |
| 1418 | 6000000, 8000000, 1000000, |
| 1419 | 6000000); |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1420 | v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1421 | break; |
Antti Palosaari | fc3a049 | 2015-05-03 21:45:43 -0300 | [diff] [blame] | 1422 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1428 | default: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1429 | v4l2_ctrl_handler_init(&dev->hdl, 0); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1430 | dev_err(&pdev->dev, "Unsupported tuner\n"); |
| 1431 | goto err_v4l2_ctrl_handler_free; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1432 | } |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1433 | if (dev->hdl.error) { |
| 1434 | ret = dev->hdl.error; |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1435 | dev_err(&pdev->dev, "Could not initialize controls\n"); |
| 1436 | goto err_v4l2_ctrl_handler_free; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* Init video_device structure */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1440 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1444 | |
| 1445 | /* Register the v4l2_device structure */ |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1446 | dev->v4l2_dev.release = rtl2832_sdr_video_release; |
Antti Palosaari | e4e7869 | 2014-12-16 12:18:01 -0300 | [diff] [blame] | 1447 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1448 | if (ret) { |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1449 | dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret); |
| 1450 | goto err_v4l2_ctrl_handler_free; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1451 | } |
| 1452 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1453 | 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 Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1457 | |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1458 | ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1459 | if (ret) { |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1460 | dev_err(&pdev->dev, "Failed to register as video device %d\n", |
| 1461 | ret); |
| 1462 | goto err_v4l2_device_unregister; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1463 | } |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1464 | 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; |
| 1471 | err_v4l2_device_unregister: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1472 | v4l2_device_unregister(&dev->v4l2_dev); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1473 | err_v4l2_ctrl_handler_free: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1474 | v4l2_ctrl_handler_free(&dev->hdl); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1475 | err_kfree: |
Antti Palosaari | 725b7f3 | 2014-12-15 14:30:02 -0300 | [diff] [blame] | 1476 | kfree(dev); |
Antti Palosaari | d5eec23 | 2014-12-17 08:25:03 -0300 | [diff] [blame] | 1477 | err_module_put: |
| 1478 | module_put(pdev->dev.parent->driver->owner); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1479 | err: |
| 1480 | return ret; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1481 | } |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1482 | |
| 1483 | static 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 Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1497 | v4l2_device_put(&dev->v4l2_dev); |
Antti Palosaari | d5eec23 | 2014-12-17 08:25:03 -0300 | [diff] [blame] | 1498 | module_put(pdev->dev.parent->driver->owner); |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | static struct platform_driver rtl2832_sdr_driver = { |
| 1504 | .driver = { |
| 1505 | .name = "rtl2832_sdr", |
Antti Palosaari | 63bdab5 | 2014-12-16 10:09:20 -0300 | [diff] [blame] | 1506 | }, |
| 1507 | .probe = rtl2832_sdr_probe, |
| 1508 | .remove = rtl2832_sdr_remove, |
| 1509 | }; |
| 1510 | module_platform_driver(rtl2832_sdr_driver); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1511 | |
| 1512 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1513 | MODULE_DESCRIPTION("Realtek RTL2832 SDR driver"); |
| 1514 | MODULE_LICENSE("GPL"); |