rtl2832_sdr.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. #include "rtl2832_sdr.h"
  25. #include "dvb_usb.h"
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-ioctl.h>
  28. #include <media/v4l2-ctrls.h>
  29. #include <media/v4l2-event.h>
  30. #include <media/videobuf2-v4l2.h>
  31. #include <media/videobuf2-vmalloc.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/math64.h>
  35. #include <linux/regmap.h>
  36. static bool rtl2832_sdr_emulated_fmt;
  37. module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
  38. MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
  39. /* Original macro does not contain enough null pointer checks for our need */
  40. #define V4L2_SUBDEV_HAS_OP(sd, o, f) \
  41. ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
  42. #define MAX_BULK_BUFS (10)
  43. #define BULK_BUFFER_SIZE (128 * 512)
  44. static const struct v4l2_frequency_band bands_adc[] = {
  45. {
  46. .tuner = 0,
  47. .type = V4L2_TUNER_ADC,
  48. .index = 0,
  49. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  50. .rangelow = 300000,
  51. .rangehigh = 300000,
  52. },
  53. {
  54. .tuner = 0,
  55. .type = V4L2_TUNER_ADC,
  56. .index = 1,
  57. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  58. .rangelow = 900001,
  59. .rangehigh = 2800000,
  60. },
  61. {
  62. .tuner = 0,
  63. .type = V4L2_TUNER_ADC,
  64. .index = 2,
  65. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  66. .rangelow = 3200000,
  67. .rangehigh = 3200000,
  68. },
  69. };
  70. static const struct v4l2_frequency_band bands_fm[] = {
  71. {
  72. .tuner = 1,
  73. .type = V4L2_TUNER_RF,
  74. .index = 0,
  75. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  76. .rangelow = 50000000,
  77. .rangehigh = 2000000000,
  78. },
  79. };
  80. /* stream formats */
  81. struct rtl2832_sdr_format {
  82. char *name;
  83. u32 pixelformat;
  84. u32 buffersize;
  85. };
  86. static struct rtl2832_sdr_format formats[] = {
  87. {
  88. .name = "Complex U8",
  89. .pixelformat = V4L2_SDR_FMT_CU8,
  90. .buffersize = BULK_BUFFER_SIZE,
  91. }, {
  92. .name = "Complex U16LE (emulated)",
  93. .pixelformat = V4L2_SDR_FMT_CU16LE,
  94. .buffersize = BULK_BUFFER_SIZE * 2,
  95. },
  96. };
  97. static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
  98. /* intermediate buffers with raw data from the USB device */
  99. struct rtl2832_sdr_frame_buf {
  100. /* common v4l buffer stuff -- must be first */
  101. struct vb2_v4l2_buffer vb;
  102. struct list_head list;
  103. };
  104. struct rtl2832_sdr_dev {
  105. #define POWER_ON 0 /* BIT(0) */
  106. #define URB_BUF 1 /* BIT(1) */
  107. unsigned long flags;
  108. struct platform_device *pdev;
  109. struct regmap *regmap;
  110. struct video_device vdev;
  111. struct v4l2_device v4l2_dev;
  112. struct v4l2_subdev *v4l2_subdev;
  113. /* videobuf2 queue and queued buffers list */
  114. struct vb2_queue vb_queue;
  115. struct list_head queued_bufs;
  116. spinlock_t queued_bufs_lock; /* Protects queued_bufs */
  117. unsigned sequence; /* buffer sequence counter */
  118. /* Note if taking both locks v4l2_lock must always be locked first! */
  119. struct mutex v4l2_lock; /* Protects everything else */
  120. struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
  121. /* Pointer to our usb_device, will be NULL after unplug */
  122. struct usb_device *udev; /* Both mutexes most be hold when setting! */
  123. unsigned int vb_full; /* vb is full and packets dropped */
  124. struct urb *urb_list[MAX_BULK_BUFS];
  125. int buf_num;
  126. unsigned long buf_size;
  127. u8 *buf_list[MAX_BULK_BUFS];
  128. dma_addr_t dma_addr[MAX_BULK_BUFS];
  129. int urbs_initialized;
  130. int urbs_submitted;
  131. unsigned int f_adc, f_tuner;
  132. u32 pixelformat;
  133. u32 buffersize;
  134. unsigned int num_formats;
  135. /* Controls */
  136. struct v4l2_ctrl_handler hdl;
  137. struct v4l2_ctrl *bandwidth_auto;
  138. struct v4l2_ctrl *bandwidth;
  139. /* for sample rate calc */
  140. unsigned int sample;
  141. unsigned int sample_measured;
  142. unsigned long jiffies_next;
  143. };
  144. /* Private functions */
  145. static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
  146. struct rtl2832_sdr_dev *dev)
  147. {
  148. unsigned long flags;
  149. struct rtl2832_sdr_frame_buf *buf = NULL;
  150. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  151. if (list_empty(&dev->queued_bufs))
  152. goto leave;
  153. buf = list_entry(dev->queued_bufs.next,
  154. struct rtl2832_sdr_frame_buf, list);
  155. list_del(&buf->list);
  156. leave:
  157. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  158. return buf;
  159. }
  160. static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
  161. void *dst, const u8 *src, unsigned int src_len)
  162. {
  163. struct platform_device *pdev = dev->pdev;
  164. unsigned int dst_len;
  165. if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
  166. /* native stream, no need to convert */
  167. memcpy(dst, src, src_len);
  168. dst_len = src_len;
  169. } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
  170. /* convert u8 to u16 */
  171. unsigned int i;
  172. u16 *u16dst = dst;
  173. for (i = 0; i < src_len; i++)
  174. *u16dst++ = (src[i] << 8) | (src[i] >> 0);
  175. dst_len = 2 * src_len;
  176. } else {
  177. dst_len = 0;
  178. }
  179. /* calculate sample rate and output it in 10 seconds intervals */
  180. if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
  181. #define MSECS 10000UL
  182. unsigned int msecs = jiffies_to_msecs(jiffies -
  183. dev->jiffies_next + msecs_to_jiffies(MSECS));
  184. unsigned int samples = dev->sample - dev->sample_measured;
  185. dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
  186. dev->sample_measured = dev->sample;
  187. dev_dbg(&pdev->dev,
  188. "slen=%u samples=%u msecs=%u sample rate=%lu\n",
  189. src_len, samples, msecs, samples * 1000UL / msecs);
  190. }
  191. /* total number of I+Q pairs */
  192. dev->sample += src_len / 2;
  193. return dst_len;
  194. }
  195. /*
  196. * This gets called for the bulk stream pipe. This is done in interrupt
  197. * time, so it has to be fast, not crash, and not stall. Neat.
  198. */
  199. static void rtl2832_sdr_urb_complete(struct urb *urb)
  200. {
  201. struct rtl2832_sdr_dev *dev = urb->context;
  202. struct platform_device *pdev = dev->pdev;
  203. struct rtl2832_sdr_frame_buf *fbuf;
  204. dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
  205. urb->status, urb->actual_length,
  206. urb->transfer_buffer_length, urb->error_count);
  207. switch (urb->status) {
  208. case 0: /* success */
  209. case -ETIMEDOUT: /* NAK */
  210. break;
  211. case -ECONNRESET: /* kill */
  212. case -ENOENT:
  213. case -ESHUTDOWN:
  214. return;
  215. default: /* error */
  216. dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
  217. break;
  218. }
  219. if (likely(urb->actual_length > 0)) {
  220. void *ptr;
  221. unsigned int len;
  222. /* get free framebuffer */
  223. fbuf = rtl2832_sdr_get_next_fill_buf(dev);
  224. if (unlikely(fbuf == NULL)) {
  225. dev->vb_full++;
  226. dev_notice_ratelimited(&pdev->dev,
  227. "videobuf is full, %d packets dropped\n",
  228. dev->vb_full);
  229. goto skip;
  230. }
  231. /* fill framebuffer */
  232. ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
  233. len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
  234. urb->actual_length);
  235. vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
  236. fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
  237. fbuf->vb.sequence = dev->sequence++;
  238. vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  239. }
  240. skip:
  241. usb_submit_urb(urb, GFP_ATOMIC);
  242. }
  243. static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
  244. {
  245. struct platform_device *pdev = dev->pdev;
  246. int i;
  247. for (i = dev->urbs_submitted - 1; i >= 0; i--) {
  248. dev_dbg(&pdev->dev, "kill urb=%d\n", i);
  249. /* stop the URB */
  250. usb_kill_urb(dev->urb_list[i]);
  251. }
  252. dev->urbs_submitted = 0;
  253. return 0;
  254. }
  255. static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
  256. {
  257. struct platform_device *pdev = dev->pdev;
  258. int i, ret;
  259. for (i = 0; i < dev->urbs_initialized; i++) {
  260. dev_dbg(&pdev->dev, "submit urb=%d\n", i);
  261. ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL);
  262. if (ret) {
  263. dev_err(&pdev->dev,
  264. "Could not submit urb no. %d - get them all back\n",
  265. i);
  266. rtl2832_sdr_kill_urbs(dev);
  267. return ret;
  268. }
  269. dev->urbs_submitted++;
  270. }
  271. return 0;
  272. }
  273. static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
  274. {
  275. struct platform_device *pdev = dev->pdev;
  276. if (test_bit(URB_BUF, &dev->flags)) {
  277. while (dev->buf_num) {
  278. dev->buf_num--;
  279. dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
  280. usb_free_coherent(dev->udev, dev->buf_size,
  281. dev->buf_list[dev->buf_num],
  282. dev->dma_addr[dev->buf_num]);
  283. }
  284. }
  285. clear_bit(URB_BUF, &dev->flags);
  286. return 0;
  287. }
  288. static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
  289. {
  290. struct platform_device *pdev = dev->pdev;
  291. dev->buf_num = 0;
  292. dev->buf_size = BULK_BUFFER_SIZE;
  293. dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
  294. MAX_BULK_BUFS * BULK_BUFFER_SIZE);
  295. for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
  296. dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
  297. BULK_BUFFER_SIZE, GFP_KERNEL,
  298. &dev->dma_addr[dev->buf_num]);
  299. if (!dev->buf_list[dev->buf_num]) {
  300. dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
  301. dev->buf_num);
  302. rtl2832_sdr_free_stream_bufs(dev);
  303. return -ENOMEM;
  304. }
  305. dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
  306. dev->buf_num, dev->buf_list[dev->buf_num],
  307. (long long)dev->dma_addr[dev->buf_num]);
  308. set_bit(URB_BUF, &dev->flags);
  309. }
  310. return 0;
  311. }
  312. static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
  313. {
  314. struct platform_device *pdev = dev->pdev;
  315. int i;
  316. rtl2832_sdr_kill_urbs(dev);
  317. for (i = dev->urbs_initialized - 1; i >= 0; i--) {
  318. if (dev->urb_list[i]) {
  319. dev_dbg(&pdev->dev, "free urb=%d\n", i);
  320. /* free the URBs */
  321. usb_free_urb(dev->urb_list[i]);
  322. }
  323. }
  324. dev->urbs_initialized = 0;
  325. return 0;
  326. }
  327. static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
  328. {
  329. struct platform_device *pdev = dev->pdev;
  330. int i, j;
  331. /* allocate the URBs */
  332. for (i = 0; i < MAX_BULK_BUFS; i++) {
  333. dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
  334. dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL);
  335. if (!dev->urb_list[i]) {
  336. for (j = 0; j < i; j++)
  337. usb_free_urb(dev->urb_list[j]);
  338. return -ENOMEM;
  339. }
  340. usb_fill_bulk_urb(dev->urb_list[i],
  341. dev->udev,
  342. usb_rcvbulkpipe(dev->udev, 0x81),
  343. dev->buf_list[i],
  344. BULK_BUFFER_SIZE,
  345. rtl2832_sdr_urb_complete, dev);
  346. dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  347. dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
  348. dev->urbs_initialized++;
  349. }
  350. return 0;
  351. }
  352. /* Must be called with vb_queue_lock hold */
  353. static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
  354. {
  355. struct platform_device *pdev = dev->pdev;
  356. unsigned long flags;
  357. dev_dbg(&pdev->dev, "\n");
  358. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  359. while (!list_empty(&dev->queued_bufs)) {
  360. struct rtl2832_sdr_frame_buf *buf;
  361. buf = list_entry(dev->queued_bufs.next,
  362. struct rtl2832_sdr_frame_buf, list);
  363. list_del(&buf->list);
  364. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  365. }
  366. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  367. }
  368. static int rtl2832_sdr_querycap(struct file *file, void *fh,
  369. struct v4l2_capability *cap)
  370. {
  371. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  372. struct platform_device *pdev = dev->pdev;
  373. dev_dbg(&pdev->dev, "\n");
  374. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  375. strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
  376. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  377. cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
  378. V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
  379. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  380. return 0;
  381. }
  382. /* Videobuf2 operations */
  383. static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
  384. unsigned int *nbuffers,
  385. unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
  386. {
  387. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  388. struct platform_device *pdev = dev->pdev;
  389. dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
  390. /* Need at least 8 buffers */
  391. if (vq->num_buffers + *nbuffers < 8)
  392. *nbuffers = 8 - vq->num_buffers;
  393. *nplanes = 1;
  394. sizes[0] = PAGE_ALIGN(dev->buffersize);
  395. dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
  396. return 0;
  397. }
  398. static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
  399. {
  400. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  401. /* Don't allow queing new buffers after device disconnection */
  402. if (!dev->udev)
  403. return -ENODEV;
  404. return 0;
  405. }
  406. static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
  407. {
  408. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  409. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  410. struct rtl2832_sdr_frame_buf *buf =
  411. container_of(vbuf, struct rtl2832_sdr_frame_buf, vb);
  412. unsigned long flags;
  413. /* Check the device has not disconnected between prep and queuing */
  414. if (!dev->udev) {
  415. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  416. return;
  417. }
  418. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  419. list_add_tail(&buf->list, &dev->queued_bufs);
  420. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  421. }
  422. static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
  423. {
  424. struct platform_device *pdev = dev->pdev;
  425. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  426. struct dvb_frontend *fe = pdata->dvb_frontend;
  427. int ret;
  428. unsigned int f_sr, f_if;
  429. u8 buf[4], u8tmp1, u8tmp2;
  430. u64 u64tmp;
  431. u32 u32tmp;
  432. dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
  433. if (!test_bit(POWER_ON, &dev->flags))
  434. return 0;
  435. if (dev->f_adc == 0)
  436. return 0;
  437. f_sr = dev->f_adc;
  438. ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2);
  439. if (ret)
  440. goto err;
  441. ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4);
  442. if (ret)
  443. goto err;
  444. /* get IF from tuner */
  445. if (fe->ops.tuner_ops.get_if_frequency)
  446. ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
  447. else
  448. ret = -EINVAL;
  449. if (ret)
  450. goto err;
  451. /* program IF */
  452. u64tmp = f_if % pdata->clk;
  453. u64tmp *= 0x400000;
  454. u64tmp = div_u64(u64tmp, pdata->clk);
  455. u64tmp = -u64tmp;
  456. u32tmp = u64tmp & 0x3fffff;
  457. dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
  458. buf[0] = (u32tmp >> 16) & 0xff;
  459. buf[1] = (u32tmp >> 8) & 0xff;
  460. buf[2] = (u32tmp >> 0) & 0xff;
  461. ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3);
  462. if (ret)
  463. goto err;
  464. /* BB / IF mode */
  465. /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
  466. if (f_if) {
  467. u8tmp1 = 0x1a; /* disable Zero-IF */
  468. u8tmp2 = 0x8d; /* enable ADC I */
  469. } else {
  470. u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
  471. u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
  472. }
  473. ret = regmap_write(dev->regmap, 0x1b1, u8tmp1);
  474. if (ret)
  475. goto err;
  476. ret = regmap_write(dev->regmap, 0x008, u8tmp2);
  477. if (ret)
  478. goto err;
  479. ret = regmap_write(dev->regmap, 0x006, 0x80);
  480. if (ret)
  481. goto err;
  482. /* program sampling rate (resampling down) */
  483. u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
  484. u32tmp <<= 2;
  485. buf[0] = (u32tmp >> 24) & 0xff;
  486. buf[1] = (u32tmp >> 16) & 0xff;
  487. buf[2] = (u32tmp >> 8) & 0xff;
  488. buf[3] = (u32tmp >> 0) & 0xff;
  489. ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4);
  490. if (ret)
  491. goto err;
  492. /* low-pass filter */
  493. ret = regmap_bulk_write(dev->regmap, 0x11c,
  494. "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
  495. 20);
  496. if (ret)
  497. goto err;
  498. ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
  499. if (ret)
  500. goto err;
  501. /* mode */
  502. ret = regmap_write(dev->regmap, 0x019, 0x05);
  503. if (ret)
  504. goto err;
  505. ret = regmap_bulk_write(dev->regmap, 0x01a,
  506. "\x1b\x16\x0d\x06\x01\xff", 6);
  507. if (ret)
  508. goto err;
  509. /* FSM */
  510. ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
  511. if (ret)
  512. goto err;
  513. /* PID filter */
  514. ret = regmap_write(dev->regmap, 0x061, 0x60);
  515. if (ret)
  516. goto err;
  517. /* used RF tuner based settings */
  518. switch (pdata->tuner) {
  519. case RTL2832_SDR_TUNER_E4000:
  520. ret = regmap_write(dev->regmap, 0x112, 0x5a);
  521. ret = regmap_write(dev->regmap, 0x102, 0x40);
  522. ret = regmap_write(dev->regmap, 0x103, 0x5a);
  523. ret = regmap_write(dev->regmap, 0x1c7, 0x30);
  524. ret = regmap_write(dev->regmap, 0x104, 0xd0);
  525. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  526. ret = regmap_write(dev->regmap, 0x1c8, 0x18);
  527. ret = regmap_write(dev->regmap, 0x106, 0x35);
  528. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  529. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  530. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  531. ret = regmap_write(dev->regmap, 0x107, 0x40);
  532. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  533. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  534. ret = regmap_write(dev->regmap, 0x108, 0x80);
  535. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  536. ret = regmap_write(dev->regmap, 0x10a, 0x80);
  537. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  538. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  539. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  540. ret = regmap_write(dev->regmap, 0x011, 0xd4);
  541. ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
  542. ret = regmap_write(dev->regmap, 0x1d9, 0x00);
  543. ret = regmap_write(dev->regmap, 0x1db, 0x00);
  544. ret = regmap_write(dev->regmap, 0x1dd, 0x14);
  545. ret = regmap_write(dev->regmap, 0x1de, 0xec);
  546. ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
  547. ret = regmap_write(dev->regmap, 0x1e6, 0x02);
  548. ret = regmap_write(dev->regmap, 0x1d7, 0x09);
  549. ret = regmap_write(dev->regmap, 0x00d, 0x83);
  550. ret = regmap_write(dev->regmap, 0x010, 0x49);
  551. ret = regmap_write(dev->regmap, 0x00d, 0x87);
  552. ret = regmap_write(dev->regmap, 0x00d, 0x85);
  553. ret = regmap_write(dev->regmap, 0x013, 0x02);
  554. break;
  555. case RTL2832_SDR_TUNER_FC0012:
  556. case RTL2832_SDR_TUNER_FC0013:
  557. ret = regmap_write(dev->regmap, 0x112, 0x5a);
  558. ret = regmap_write(dev->regmap, 0x102, 0x40);
  559. ret = regmap_write(dev->regmap, 0x103, 0x5a);
  560. ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
  561. ret = regmap_write(dev->regmap, 0x104, 0xcc);
  562. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  563. ret = regmap_write(dev->regmap, 0x1c8, 0x16);
  564. ret = regmap_write(dev->regmap, 0x106, 0x35);
  565. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  566. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  567. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  568. ret = regmap_write(dev->regmap, 0x107, 0x40);
  569. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  570. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  571. ret = regmap_write(dev->regmap, 0x108, 0x80);
  572. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  573. ret = regmap_write(dev->regmap, 0x10a, 0x80);
  574. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  575. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  576. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  577. ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2);
  578. ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
  579. ret = regmap_write(dev->regmap, 0x1d9, 0x00);
  580. ret = regmap_write(dev->regmap, 0x1db, 0x00);
  581. ret = regmap_write(dev->regmap, 0x1dd, 0x11);
  582. ret = regmap_write(dev->regmap, 0x1de, 0xef);
  583. ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
  584. ret = regmap_write(dev->regmap, 0x1e6, 0x02);
  585. ret = regmap_write(dev->regmap, 0x1d7, 0x09);
  586. break;
  587. case RTL2832_SDR_TUNER_R820T:
  588. case RTL2832_SDR_TUNER_R828D:
  589. ret = regmap_write(dev->regmap, 0x112, 0x5a);
  590. ret = regmap_write(dev->regmap, 0x102, 0x40);
  591. ret = regmap_write(dev->regmap, 0x115, 0x01);
  592. ret = regmap_write(dev->regmap, 0x103, 0x80);
  593. ret = regmap_write(dev->regmap, 0x1c7, 0x24);
  594. ret = regmap_write(dev->regmap, 0x104, 0xcc);
  595. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  596. ret = regmap_write(dev->regmap, 0x1c8, 0x14);
  597. ret = regmap_write(dev->regmap, 0x106, 0x35);
  598. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  599. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  600. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  601. ret = regmap_write(dev->regmap, 0x107, 0x40);
  602. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  603. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  604. ret = regmap_write(dev->regmap, 0x108, 0x80);
  605. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  606. ret = regmap_write(dev->regmap, 0x10a, 0x80);
  607. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  608. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  609. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  610. ret = regmap_write(dev->regmap, 0x011, 0xf4);
  611. break;
  612. case RTL2832_SDR_TUNER_FC2580:
  613. ret = regmap_write(dev->regmap, 0x112, 0x39);
  614. ret = regmap_write(dev->regmap, 0x102, 0x40);
  615. ret = regmap_write(dev->regmap, 0x103, 0x5a);
  616. ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
  617. ret = regmap_write(dev->regmap, 0x104, 0xcc);
  618. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  619. ret = regmap_write(dev->regmap, 0x1c8, 0x16);
  620. ret = regmap_write(dev->regmap, 0x106, 0x35);
  621. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  622. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  623. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  624. ret = regmap_write(dev->regmap, 0x107, 0x40);
  625. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  626. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  627. ret = regmap_write(dev->regmap, 0x108, 0x80);
  628. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  629. ret = regmap_write(dev->regmap, 0x10a, 0x9c);
  630. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  631. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  632. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  633. ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2);
  634. break;
  635. default:
  636. dev_notice(&pdev->dev, "Unsupported tuner\n");
  637. }
  638. /* software reset */
  639. ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04);
  640. if (ret)
  641. goto err;
  642. ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00);
  643. if (ret)
  644. goto err;
  645. err:
  646. return ret;
  647. };
  648. static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
  649. {
  650. struct platform_device *pdev = dev->pdev;
  651. int ret;
  652. dev_dbg(&pdev->dev, "\n");
  653. /* PID filter */
  654. ret = regmap_write(dev->regmap, 0x061, 0xe0);
  655. if (ret)
  656. goto err;
  657. /* mode */
  658. ret = regmap_write(dev->regmap, 0x019, 0x20);
  659. if (ret)
  660. goto err;
  661. ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
  662. if (ret)
  663. goto err;
  664. /* FSM */
  665. ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
  666. if (ret)
  667. goto err;
  668. ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2);
  669. if (ret)
  670. goto err;
  671. ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4);
  672. if (ret)
  673. goto err;
  674. err:
  675. return;
  676. };
  677. static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
  678. {
  679. struct platform_device *pdev = dev->pdev;
  680. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  681. struct dvb_frontend *fe = pdata->dvb_frontend;
  682. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  683. struct v4l2_ctrl *bandwidth_auto;
  684. struct v4l2_ctrl *bandwidth;
  685. /*
  686. * tuner RF (Hz)
  687. */
  688. if (dev->f_tuner == 0)
  689. return 0;
  690. /*
  691. * bandwidth (Hz)
  692. */
  693. bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
  694. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
  695. bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
  696. if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
  697. c->bandwidth_hz = dev->f_adc;
  698. v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
  699. } else {
  700. c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
  701. }
  702. c->frequency = dev->f_tuner;
  703. c->delivery_system = SYS_DVBT;
  704. dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
  705. c->frequency, c->bandwidth_hz);
  706. if (!test_bit(POWER_ON, &dev->flags))
  707. return 0;
  708. if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
  709. if (fe->ops.tuner_ops.set_params)
  710. fe->ops.tuner_ops.set_params(fe);
  711. }
  712. return 0;
  713. };
  714. static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
  715. {
  716. struct platform_device *pdev = dev->pdev;
  717. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  718. struct dvb_frontend *fe = pdata->dvb_frontend;
  719. dev_dbg(&pdev->dev, "\n");
  720. if (fe->ops.tuner_ops.init)
  721. fe->ops.tuner_ops.init(fe);
  722. return 0;
  723. };
  724. static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
  725. {
  726. struct platform_device *pdev = dev->pdev;
  727. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  728. struct dvb_frontend *fe = pdata->dvb_frontend;
  729. dev_dbg(&pdev->dev, "\n");
  730. if (fe->ops.tuner_ops.sleep)
  731. fe->ops.tuner_ops.sleep(fe);
  732. return;
  733. };
  734. static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
  735. {
  736. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  737. struct platform_device *pdev = dev->pdev;
  738. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  739. struct dvb_usb_device *d = pdata->dvb_usb_device;
  740. int ret;
  741. dev_dbg(&pdev->dev, "\n");
  742. if (!dev->udev)
  743. return -ENODEV;
  744. if (mutex_lock_interruptible(&dev->v4l2_lock))
  745. return -ERESTARTSYS;
  746. if (d->props->power_ctrl)
  747. d->props->power_ctrl(d, 1);
  748. /* enable ADC */
  749. if (d->props->frontend_ctrl)
  750. d->props->frontend_ctrl(pdata->dvb_frontend, 1);
  751. set_bit(POWER_ON, &dev->flags);
  752. /* wake-up tuner */
  753. if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
  754. ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
  755. else
  756. ret = rtl2832_sdr_set_tuner(dev);
  757. if (ret)
  758. goto err;
  759. ret = rtl2832_sdr_set_tuner_freq(dev);
  760. if (ret)
  761. goto err;
  762. ret = rtl2832_sdr_set_adc(dev);
  763. if (ret)
  764. goto err;
  765. ret = rtl2832_sdr_alloc_stream_bufs(dev);
  766. if (ret)
  767. goto err;
  768. ret = rtl2832_sdr_alloc_urbs(dev);
  769. if (ret)
  770. goto err;
  771. dev->sequence = 0;
  772. ret = rtl2832_sdr_submit_urbs(dev);
  773. if (ret)
  774. goto err;
  775. err:
  776. mutex_unlock(&dev->v4l2_lock);
  777. return ret;
  778. }
  779. static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
  780. {
  781. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  782. struct platform_device *pdev = dev->pdev;
  783. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  784. struct dvb_usb_device *d = pdata->dvb_usb_device;
  785. dev_dbg(&pdev->dev, "\n");
  786. mutex_lock(&dev->v4l2_lock);
  787. rtl2832_sdr_kill_urbs(dev);
  788. rtl2832_sdr_free_urbs(dev);
  789. rtl2832_sdr_free_stream_bufs(dev);
  790. rtl2832_sdr_cleanup_queued_bufs(dev);
  791. rtl2832_sdr_unset_adc(dev);
  792. /* sleep tuner */
  793. if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
  794. v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
  795. else
  796. rtl2832_sdr_unset_tuner(dev);
  797. clear_bit(POWER_ON, &dev->flags);
  798. /* disable ADC */
  799. if (d->props->frontend_ctrl)
  800. d->props->frontend_ctrl(pdata->dvb_frontend, 0);
  801. if (d->props->power_ctrl)
  802. d->props->power_ctrl(d, 0);
  803. mutex_unlock(&dev->v4l2_lock);
  804. }
  805. static const struct vb2_ops rtl2832_sdr_vb2_ops = {
  806. .queue_setup = rtl2832_sdr_queue_setup,
  807. .buf_prepare = rtl2832_sdr_buf_prepare,
  808. .buf_queue = rtl2832_sdr_buf_queue,
  809. .start_streaming = rtl2832_sdr_start_streaming,
  810. .stop_streaming = rtl2832_sdr_stop_streaming,
  811. .wait_prepare = vb2_ops_wait_prepare,
  812. .wait_finish = vb2_ops_wait_finish,
  813. };
  814. static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
  815. struct v4l2_tuner *v)
  816. {
  817. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  818. struct platform_device *pdev = dev->pdev;
  819. int ret;
  820. dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
  821. if (v->index == 0) {
  822. strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
  823. v->type = V4L2_TUNER_ADC;
  824. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  825. v->rangelow = 300000;
  826. v->rangehigh = 3200000;
  827. ret = 0;
  828. } else if (v->index == 1 &&
  829. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
  830. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
  831. } else if (v->index == 1) {
  832. strlcpy(v->name, "RF: <unknown>", sizeof(v->name));
  833. v->type = V4L2_TUNER_RF;
  834. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  835. v->rangelow = 50000000;
  836. v->rangehigh = 2000000000;
  837. ret = 0;
  838. } else {
  839. ret = -EINVAL;
  840. }
  841. return ret;
  842. }
  843. static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
  844. const struct v4l2_tuner *v)
  845. {
  846. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  847. struct platform_device *pdev = dev->pdev;
  848. int ret;
  849. dev_dbg(&pdev->dev, "\n");
  850. if (v->index == 0) {
  851. ret = 0;
  852. } else if (v->index == 1 &&
  853. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
  854. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
  855. } else if (v->index == 1) {
  856. ret = 0;
  857. } else {
  858. ret = -EINVAL;
  859. }
  860. return ret;
  861. }
  862. static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
  863. struct v4l2_frequency_band *band)
  864. {
  865. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  866. struct platform_device *pdev = dev->pdev;
  867. int ret;
  868. dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
  869. band->tuner, band->type, band->index);
  870. if (band->tuner == 0) {
  871. if (band->index >= ARRAY_SIZE(bands_adc))
  872. return -EINVAL;
  873. *band = bands_adc[band->index];
  874. ret = 0;
  875. } else if (band->tuner == 1 &&
  876. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
  877. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
  878. } else if (band->tuner == 1) {
  879. if (band->index >= ARRAY_SIZE(bands_fm))
  880. return -EINVAL;
  881. *band = bands_fm[band->index];
  882. ret = 0;
  883. } else {
  884. ret = -EINVAL;
  885. }
  886. return ret;
  887. }
  888. static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
  889. struct v4l2_frequency *f)
  890. {
  891. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  892. struct platform_device *pdev = dev->pdev;
  893. int ret;
  894. dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
  895. if (f->tuner == 0) {
  896. f->frequency = dev->f_adc;
  897. f->type = V4L2_TUNER_ADC;
  898. ret = 0;
  899. } else if (f->tuner == 1 &&
  900. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
  901. f->type = V4L2_TUNER_RF;
  902. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
  903. } else if (f->tuner == 1) {
  904. f->frequency = dev->f_tuner;
  905. f->type = V4L2_TUNER_RF;
  906. ret = 0;
  907. } else {
  908. ret = -EINVAL;
  909. }
  910. return ret;
  911. }
  912. static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
  913. const struct v4l2_frequency *f)
  914. {
  915. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  916. struct platform_device *pdev = dev->pdev;
  917. int ret, band;
  918. dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
  919. f->tuner, f->type, f->frequency);
  920. /* ADC band midpoints */
  921. #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
  922. #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
  923. if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
  924. if (f->frequency < BAND_ADC_0)
  925. band = 0;
  926. else if (f->frequency < BAND_ADC_1)
  927. band = 1;
  928. else
  929. band = 2;
  930. dev->f_adc = clamp_t(unsigned int, f->frequency,
  931. bands_adc[band].rangelow,
  932. bands_adc[band].rangehigh);
  933. dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
  934. ret = rtl2832_sdr_set_adc(dev);
  935. } else if (f->tuner == 1 &&
  936. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
  937. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
  938. } else if (f->tuner == 1) {
  939. dev->f_tuner = clamp_t(unsigned int, f->frequency,
  940. bands_fm[0].rangelow,
  941. bands_fm[0].rangehigh);
  942. dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
  943. ret = rtl2832_sdr_set_tuner_freq(dev);
  944. } else {
  945. ret = -EINVAL;
  946. }
  947. return ret;
  948. }
  949. static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
  950. struct v4l2_fmtdesc *f)
  951. {
  952. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  953. struct platform_device *pdev = dev->pdev;
  954. dev_dbg(&pdev->dev, "\n");
  955. if (f->index >= dev->num_formats)
  956. return -EINVAL;
  957. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  958. f->pixelformat = formats[f->index].pixelformat;
  959. return 0;
  960. }
  961. static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
  962. struct v4l2_format *f)
  963. {
  964. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  965. struct platform_device *pdev = dev->pdev;
  966. dev_dbg(&pdev->dev, "\n");
  967. f->fmt.sdr.pixelformat = dev->pixelformat;
  968. f->fmt.sdr.buffersize = dev->buffersize;
  969. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  970. return 0;
  971. }
  972. static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
  973. struct v4l2_format *f)
  974. {
  975. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  976. struct platform_device *pdev = dev->pdev;
  977. struct vb2_queue *q = &dev->vb_queue;
  978. int i;
  979. dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
  980. (char *)&f->fmt.sdr.pixelformat);
  981. if (vb2_is_busy(q))
  982. return -EBUSY;
  983. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  984. for (i = 0; i < dev->num_formats; i++) {
  985. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  986. dev->pixelformat = formats[i].pixelformat;
  987. dev->buffersize = formats[i].buffersize;
  988. f->fmt.sdr.buffersize = formats[i].buffersize;
  989. return 0;
  990. }
  991. }
  992. dev->pixelformat = formats[0].pixelformat;
  993. dev->buffersize = formats[0].buffersize;
  994. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  995. f->fmt.sdr.buffersize = formats[0].buffersize;
  996. return 0;
  997. }
  998. static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
  999. struct v4l2_format *f)
  1000. {
  1001. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  1002. struct platform_device *pdev = dev->pdev;
  1003. int i;
  1004. dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
  1005. (char *)&f->fmt.sdr.pixelformat);
  1006. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  1007. for (i = 0; i < dev->num_formats; i++) {
  1008. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  1009. f->fmt.sdr.buffersize = formats[i].buffersize;
  1010. return 0;
  1011. }
  1012. }
  1013. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  1014. f->fmt.sdr.buffersize = formats[0].buffersize;
  1015. return 0;
  1016. }
  1017. static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
  1018. .vidioc_querycap = rtl2832_sdr_querycap,
  1019. .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap,
  1020. .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap,
  1021. .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap,
  1022. .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap,
  1023. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1024. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1025. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1026. .vidioc_querybuf = vb2_ioctl_querybuf,
  1027. .vidioc_qbuf = vb2_ioctl_qbuf,
  1028. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1029. .vidioc_streamon = vb2_ioctl_streamon,
  1030. .vidioc_streamoff = vb2_ioctl_streamoff,
  1031. .vidioc_g_tuner = rtl2832_sdr_g_tuner,
  1032. .vidioc_s_tuner = rtl2832_sdr_s_tuner,
  1033. .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands,
  1034. .vidioc_g_frequency = rtl2832_sdr_g_frequency,
  1035. .vidioc_s_frequency = rtl2832_sdr_s_frequency,
  1036. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1037. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1038. .vidioc_log_status = v4l2_ctrl_log_status,
  1039. };
  1040. static const struct v4l2_file_operations rtl2832_sdr_fops = {
  1041. .owner = THIS_MODULE,
  1042. .open = v4l2_fh_open,
  1043. .release = vb2_fop_release,
  1044. .read = vb2_fop_read,
  1045. .poll = vb2_fop_poll,
  1046. .mmap = vb2_fop_mmap,
  1047. .unlocked_ioctl = video_ioctl2,
  1048. };
  1049. static struct video_device rtl2832_sdr_template = {
  1050. .name = "Realtek RTL2832 SDR",
  1051. .release = video_device_release_empty,
  1052. .fops = &rtl2832_sdr_fops,
  1053. .ioctl_ops = &rtl2832_sdr_ioctl_ops,
  1054. };
  1055. static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
  1056. {
  1057. struct rtl2832_sdr_dev *dev =
  1058. container_of(ctrl->handler, struct rtl2832_sdr_dev,
  1059. hdl);
  1060. struct platform_device *pdev = dev->pdev;
  1061. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  1062. struct dvb_frontend *fe = pdata->dvb_frontend;
  1063. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1064. int ret;
  1065. dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
  1066. ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
  1067. ctrl->step);
  1068. switch (ctrl->id) {
  1069. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  1070. case V4L2_CID_RF_TUNER_BANDWIDTH:
  1071. /* TODO: these controls should be moved to tuner drivers */
  1072. if (dev->bandwidth_auto->val) {
  1073. /* Round towards the closest legal value */
  1074. s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
  1075. u32 offset;
  1076. val = clamp_t(s32, val, dev->bandwidth->minimum,
  1077. dev->bandwidth->maximum);
  1078. offset = val - dev->bandwidth->minimum;
  1079. offset = dev->bandwidth->step *
  1080. div_u64(offset, dev->bandwidth->step);
  1081. dev->bandwidth->val = dev->bandwidth->minimum + offset;
  1082. }
  1083. c->bandwidth_hz = dev->bandwidth->val;
  1084. if (!test_bit(POWER_ON, &dev->flags))
  1085. return 0;
  1086. if (fe->ops.tuner_ops.set_params)
  1087. ret = fe->ops.tuner_ops.set_params(fe);
  1088. else
  1089. ret = 0;
  1090. break;
  1091. default:
  1092. ret = -EINVAL;
  1093. }
  1094. return ret;
  1095. }
  1096. static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
  1097. .s_ctrl = rtl2832_sdr_s_ctrl,
  1098. };
  1099. static void rtl2832_sdr_video_release(struct v4l2_device *v)
  1100. {
  1101. struct rtl2832_sdr_dev *dev =
  1102. container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
  1103. struct platform_device *pdev = dev->pdev;
  1104. dev_dbg(&pdev->dev, "\n");
  1105. v4l2_ctrl_handler_free(&dev->hdl);
  1106. v4l2_device_unregister(&dev->v4l2_dev);
  1107. kfree(dev);
  1108. }
  1109. /* Platform driver interface */
  1110. static int rtl2832_sdr_probe(struct platform_device *pdev)
  1111. {
  1112. struct rtl2832_sdr_dev *dev;
  1113. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  1114. const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
  1115. struct v4l2_subdev *subdev;
  1116. int ret;
  1117. dev_dbg(&pdev->dev, "\n");
  1118. if (!pdata) {
  1119. dev_err(&pdev->dev, "Cannot proceed without platform data\n");
  1120. ret = -EINVAL;
  1121. goto err;
  1122. }
  1123. if (!pdev->dev.parent->driver) {
  1124. dev_dbg(&pdev->dev, "No parent device\n");
  1125. ret = -EINVAL;
  1126. goto err;
  1127. }
  1128. /* try to refcount host drv since we are the consumer */
  1129. if (!try_module_get(pdev->dev.parent->driver->owner)) {
  1130. dev_err(&pdev->dev, "Refcount fail");
  1131. ret = -EINVAL;
  1132. goto err;
  1133. }
  1134. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1135. if (dev == NULL) {
  1136. ret = -ENOMEM;
  1137. goto err_module_put;
  1138. }
  1139. /* setup the state */
  1140. subdev = pdata->v4l2_subdev;
  1141. dev->v4l2_subdev = pdata->v4l2_subdev;
  1142. dev->pdev = pdev;
  1143. dev->regmap = pdata->regmap;
  1144. dev->udev = pdata->dvb_usb_device->udev;
  1145. dev->f_adc = bands_adc[0].rangelow;
  1146. dev->f_tuner = bands_fm[0].rangelow;
  1147. dev->pixelformat = formats[0].pixelformat;
  1148. dev->buffersize = formats[0].buffersize;
  1149. dev->num_formats = NUM_FORMATS;
  1150. if (!rtl2832_sdr_emulated_fmt)
  1151. dev->num_formats -= 1;
  1152. mutex_init(&dev->v4l2_lock);
  1153. mutex_init(&dev->vb_queue_lock);
  1154. spin_lock_init(&dev->queued_bufs_lock);
  1155. INIT_LIST_HEAD(&dev->queued_bufs);
  1156. /* Init videobuf2 queue structure */
  1157. dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
  1158. dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  1159. dev->vb_queue.drv_priv = dev;
  1160. dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
  1161. dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
  1162. dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
  1163. dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1164. ret = vb2_queue_init(&dev->vb_queue);
  1165. if (ret) {
  1166. dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
  1167. goto err_kfree;
  1168. }
  1169. /* Register controls */
  1170. switch (pdata->tuner) {
  1171. case RTL2832_SDR_TUNER_E4000:
  1172. v4l2_ctrl_handler_init(&dev->hdl, 9);
  1173. if (subdev)
  1174. v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
  1175. break;
  1176. case RTL2832_SDR_TUNER_R820T:
  1177. case RTL2832_SDR_TUNER_R828D:
  1178. v4l2_ctrl_handler_init(&dev->hdl, 2);
  1179. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
  1180. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
  1181. 0, 1, 1, 1);
  1182. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
  1183. V4L2_CID_RF_TUNER_BANDWIDTH,
  1184. 0, 8000000, 100000, 0);
  1185. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  1186. break;
  1187. case RTL2832_SDR_TUNER_FC0012:
  1188. case RTL2832_SDR_TUNER_FC0013:
  1189. v4l2_ctrl_handler_init(&dev->hdl, 2);
  1190. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
  1191. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
  1192. 0, 1, 1, 1);
  1193. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
  1194. V4L2_CID_RF_TUNER_BANDWIDTH,
  1195. 6000000, 8000000, 1000000,
  1196. 6000000);
  1197. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  1198. break;
  1199. case RTL2832_SDR_TUNER_FC2580:
  1200. v4l2_ctrl_handler_init(&dev->hdl, 2);
  1201. if (subdev)
  1202. v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
  1203. NULL);
  1204. break;
  1205. default:
  1206. v4l2_ctrl_handler_init(&dev->hdl, 0);
  1207. dev_err(&pdev->dev, "Unsupported tuner\n");
  1208. goto err_v4l2_ctrl_handler_free;
  1209. }
  1210. if (dev->hdl.error) {
  1211. ret = dev->hdl.error;
  1212. dev_err(&pdev->dev, "Could not initialize controls\n");
  1213. goto err_v4l2_ctrl_handler_free;
  1214. }
  1215. /* Init video_device structure */
  1216. dev->vdev = rtl2832_sdr_template;
  1217. dev->vdev.queue = &dev->vb_queue;
  1218. dev->vdev.queue->lock = &dev->vb_queue_lock;
  1219. video_set_drvdata(&dev->vdev, dev);
  1220. /* Register the v4l2_device structure */
  1221. dev->v4l2_dev.release = rtl2832_sdr_video_release;
  1222. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1223. if (ret) {
  1224. dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
  1225. goto err_v4l2_ctrl_handler_free;
  1226. }
  1227. dev->v4l2_dev.ctrl_handler = &dev->hdl;
  1228. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1229. dev->vdev.lock = &dev->v4l2_lock;
  1230. dev->vdev.vfl_dir = VFL_DIR_RX;
  1231. ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
  1232. if (ret) {
  1233. dev_err(&pdev->dev, "Failed to register as video device %d\n",
  1234. ret);
  1235. goto err_v4l2_device_unregister;
  1236. }
  1237. dev_info(&pdev->dev, "Registered as %s\n",
  1238. video_device_node_name(&dev->vdev));
  1239. dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
  1240. dev_notice(&pdev->dev,
  1241. "SDR API is still slightly experimental and functionality changes may follow\n");
  1242. platform_set_drvdata(pdev, dev);
  1243. return 0;
  1244. err_v4l2_device_unregister:
  1245. v4l2_device_unregister(&dev->v4l2_dev);
  1246. err_v4l2_ctrl_handler_free:
  1247. v4l2_ctrl_handler_free(&dev->hdl);
  1248. err_kfree:
  1249. kfree(dev);
  1250. err_module_put:
  1251. module_put(pdev->dev.parent->driver->owner);
  1252. err:
  1253. return ret;
  1254. }
  1255. static int rtl2832_sdr_remove(struct platform_device *pdev)
  1256. {
  1257. struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
  1258. dev_dbg(&pdev->dev, "\n");
  1259. mutex_lock(&dev->vb_queue_lock);
  1260. mutex_lock(&dev->v4l2_lock);
  1261. /* No need to keep the urbs around after disconnection */
  1262. dev->udev = NULL;
  1263. v4l2_device_disconnect(&dev->v4l2_dev);
  1264. video_unregister_device(&dev->vdev);
  1265. mutex_unlock(&dev->v4l2_lock);
  1266. mutex_unlock(&dev->vb_queue_lock);
  1267. v4l2_device_put(&dev->v4l2_dev);
  1268. module_put(pdev->dev.parent->driver->owner);
  1269. return 0;
  1270. }
  1271. static struct platform_driver rtl2832_sdr_driver = {
  1272. .driver = {
  1273. .name = "rtl2832_sdr",
  1274. },
  1275. .probe = rtl2832_sdr_probe,
  1276. .remove = rtl2832_sdr_remove,
  1277. };
  1278. module_platform_driver(rtl2832_sdr_driver);
  1279. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1280. MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
  1281. MODULE_LICENSE("GPL");