rtl2832_sdr.c 41 KB

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