rtl2832_sdr.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Realtek RTL2832U SDR driver
  4. *
  5. * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
  6. *
  7. * GNU Radio plugin "gr-kernel" for device usage will be on:
  8. * http://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. "videobuf 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. return -ENOMEM;
  323. }
  324. usb_fill_bulk_urb(dev->urb_list[i],
  325. dev->udev,
  326. usb_rcvbulkpipe(dev->udev, 0x81),
  327. dev->buf_list[i],
  328. BULK_BUFFER_SIZE,
  329. rtl2832_sdr_urb_complete, dev);
  330. dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  331. dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
  332. dev->urbs_initialized++;
  333. }
  334. return 0;
  335. }
  336. /* Must be called with vb_queue_lock hold */
  337. static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
  338. {
  339. struct platform_device *pdev = dev->pdev;
  340. unsigned long flags;
  341. dev_dbg(&pdev->dev, "\n");
  342. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  343. while (!list_empty(&dev->queued_bufs)) {
  344. struct rtl2832_sdr_frame_buf *buf;
  345. buf = list_entry(dev->queued_bufs.next,
  346. struct rtl2832_sdr_frame_buf, list);
  347. list_del(&buf->list);
  348. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  349. }
  350. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  351. }
  352. static int rtl2832_sdr_querycap(struct file *file, void *fh,
  353. struct v4l2_capability *cap)
  354. {
  355. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  356. struct platform_device *pdev = dev->pdev;
  357. dev_dbg(&pdev->dev, "\n");
  358. strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  359. strscpy(cap->card, dev->vdev.name, sizeof(cap->card));
  360. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  361. return 0;
  362. }
  363. /* Videobuf2 operations */
  364. static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
  365. unsigned int *nbuffers,
  366. unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
  367. {
  368. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  369. struct platform_device *pdev = dev->pdev;
  370. dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
  371. /* Need at least 8 buffers */
  372. if (vq->num_buffers + *nbuffers < 8)
  373. *nbuffers = 8 - vq->num_buffers;
  374. *nplanes = 1;
  375. sizes[0] = PAGE_ALIGN(dev->buffersize);
  376. dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
  377. return 0;
  378. }
  379. static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
  380. {
  381. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  382. /* Don't allow queueing new buffers after device disconnection */
  383. if (!dev->udev)
  384. return -ENODEV;
  385. return 0;
  386. }
  387. static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
  388. {
  389. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  390. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  391. struct rtl2832_sdr_frame_buf *buf =
  392. container_of(vbuf, struct rtl2832_sdr_frame_buf, vb);
  393. unsigned long flags;
  394. /* Check the device has not disconnected between prep and queuing */
  395. if (!dev->udev) {
  396. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  397. return;
  398. }
  399. spin_lock_irqsave(&dev->queued_bufs_lock, flags);
  400. list_add_tail(&buf->list, &dev->queued_bufs);
  401. spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
  402. }
  403. static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
  404. {
  405. struct platform_device *pdev = dev->pdev;
  406. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  407. struct dvb_frontend *fe = pdata->dvb_frontend;
  408. int ret;
  409. unsigned int f_sr, f_if;
  410. u8 buf[4], u8tmp1, u8tmp2;
  411. u64 u64tmp;
  412. u32 u32tmp;
  413. dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
  414. if (!test_bit(POWER_ON, &dev->flags))
  415. return 0;
  416. if (dev->f_adc == 0)
  417. return 0;
  418. f_sr = dev->f_adc;
  419. ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2);
  420. if (ret)
  421. goto err;
  422. ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4);
  423. if (ret)
  424. goto err;
  425. /* get IF from tuner */
  426. if (fe->ops.tuner_ops.get_if_frequency)
  427. ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
  428. else
  429. ret = -EINVAL;
  430. if (ret)
  431. goto err;
  432. /* program IF */
  433. u64tmp = f_if % pdata->clk;
  434. u64tmp *= 0x400000;
  435. u64tmp = div_u64(u64tmp, pdata->clk);
  436. u64tmp = -u64tmp;
  437. u32tmp = u64tmp & 0x3fffff;
  438. dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
  439. buf[0] = (u32tmp >> 16) & 0xff;
  440. buf[1] = (u32tmp >> 8) & 0xff;
  441. buf[2] = (u32tmp >> 0) & 0xff;
  442. ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3);
  443. if (ret)
  444. goto err;
  445. /* BB / IF mode */
  446. /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
  447. if (f_if) {
  448. u8tmp1 = 0x1a; /* disable Zero-IF */
  449. u8tmp2 = 0x8d; /* enable ADC I */
  450. } else {
  451. u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
  452. u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
  453. }
  454. ret = regmap_write(dev->regmap, 0x1b1, u8tmp1);
  455. if (ret)
  456. goto err;
  457. ret = regmap_write(dev->regmap, 0x008, u8tmp2);
  458. if (ret)
  459. goto err;
  460. ret = regmap_write(dev->regmap, 0x006, 0x80);
  461. if (ret)
  462. goto err;
  463. /* program sampling rate (resampling down) */
  464. u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
  465. u32tmp <<= 2;
  466. buf[0] = (u32tmp >> 24) & 0xff;
  467. buf[1] = (u32tmp >> 16) & 0xff;
  468. buf[2] = (u32tmp >> 8) & 0xff;
  469. buf[3] = (u32tmp >> 0) & 0xff;
  470. ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4);
  471. if (ret)
  472. goto err;
  473. /* low-pass filter */
  474. ret = regmap_bulk_write(dev->regmap, 0x11c,
  475. "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
  476. 20);
  477. if (ret)
  478. goto err;
  479. ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
  480. if (ret)
  481. goto err;
  482. /* mode */
  483. ret = regmap_write(dev->regmap, 0x019, 0x05);
  484. if (ret)
  485. goto err;
  486. ret = regmap_bulk_write(dev->regmap, 0x01a,
  487. "\x1b\x16\x0d\x06\x01\xff", 6);
  488. if (ret)
  489. goto err;
  490. /* FSM */
  491. ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
  492. if (ret)
  493. goto err;
  494. /* PID filter */
  495. ret = regmap_write(dev->regmap, 0x061, 0x60);
  496. if (ret)
  497. goto err;
  498. /* used RF tuner based settings */
  499. switch (pdata->tuner) {
  500. case RTL2832_SDR_TUNER_E4000:
  501. ret = regmap_write(dev->regmap, 0x112, 0x5a);
  502. ret = regmap_write(dev->regmap, 0x102, 0x40);
  503. ret = regmap_write(dev->regmap, 0x103, 0x5a);
  504. ret = regmap_write(dev->regmap, 0x1c7, 0x30);
  505. ret = regmap_write(dev->regmap, 0x104, 0xd0);
  506. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  507. ret = regmap_write(dev->regmap, 0x1c8, 0x18);
  508. ret = regmap_write(dev->regmap, 0x106, 0x35);
  509. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  510. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  511. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  512. ret = regmap_write(dev->regmap, 0x107, 0x40);
  513. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  514. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  515. ret = regmap_write(dev->regmap, 0x108, 0x80);
  516. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  517. ret = regmap_write(dev->regmap, 0x10a, 0x80);
  518. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  519. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  520. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  521. ret = regmap_write(dev->regmap, 0x011, 0xd4);
  522. ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
  523. ret = regmap_write(dev->regmap, 0x1d9, 0x00);
  524. ret = regmap_write(dev->regmap, 0x1db, 0x00);
  525. ret = regmap_write(dev->regmap, 0x1dd, 0x14);
  526. ret = regmap_write(dev->regmap, 0x1de, 0xec);
  527. ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
  528. ret = regmap_write(dev->regmap, 0x1e6, 0x02);
  529. ret = regmap_write(dev->regmap, 0x1d7, 0x09);
  530. ret = regmap_write(dev->regmap, 0x00d, 0x83);
  531. ret = regmap_write(dev->regmap, 0x010, 0x49);
  532. ret = regmap_write(dev->regmap, 0x00d, 0x87);
  533. ret = regmap_write(dev->regmap, 0x00d, 0x85);
  534. ret = regmap_write(dev->regmap, 0x013, 0x02);
  535. break;
  536. case RTL2832_SDR_TUNER_FC0012:
  537. case RTL2832_SDR_TUNER_FC0013:
  538. ret = regmap_write(dev->regmap, 0x112, 0x5a);
  539. ret = regmap_write(dev->regmap, 0x102, 0x40);
  540. ret = regmap_write(dev->regmap, 0x103, 0x5a);
  541. ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
  542. ret = regmap_write(dev->regmap, 0x104, 0xcc);
  543. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  544. ret = regmap_write(dev->regmap, 0x1c8, 0x16);
  545. ret = regmap_write(dev->regmap, 0x106, 0x35);
  546. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  547. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  548. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  549. ret = regmap_write(dev->regmap, 0x107, 0x40);
  550. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  551. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  552. ret = regmap_write(dev->regmap, 0x108, 0x80);
  553. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  554. ret = regmap_write(dev->regmap, 0x10a, 0x80);
  555. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  556. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  557. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  558. ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2);
  559. ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
  560. ret = regmap_write(dev->regmap, 0x1d9, 0x00);
  561. ret = regmap_write(dev->regmap, 0x1db, 0x00);
  562. ret = regmap_write(dev->regmap, 0x1dd, 0x11);
  563. ret = regmap_write(dev->regmap, 0x1de, 0xef);
  564. ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
  565. ret = regmap_write(dev->regmap, 0x1e6, 0x02);
  566. ret = regmap_write(dev->regmap, 0x1d7, 0x09);
  567. break;
  568. case RTL2832_SDR_TUNER_R820T:
  569. case RTL2832_SDR_TUNER_R828D:
  570. ret = regmap_write(dev->regmap, 0x112, 0x5a);
  571. ret = regmap_write(dev->regmap, 0x102, 0x40);
  572. ret = regmap_write(dev->regmap, 0x115, 0x01);
  573. ret = regmap_write(dev->regmap, 0x103, 0x80);
  574. ret = regmap_write(dev->regmap, 0x1c7, 0x24);
  575. ret = regmap_write(dev->regmap, 0x104, 0xcc);
  576. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  577. ret = regmap_write(dev->regmap, 0x1c8, 0x14);
  578. ret = regmap_write(dev->regmap, 0x106, 0x35);
  579. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  580. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  581. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  582. ret = regmap_write(dev->regmap, 0x107, 0x40);
  583. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  584. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  585. ret = regmap_write(dev->regmap, 0x108, 0x80);
  586. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  587. ret = regmap_write(dev->regmap, 0x10a, 0x80);
  588. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  589. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  590. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  591. ret = regmap_write(dev->regmap, 0x011, 0xf4);
  592. break;
  593. case RTL2832_SDR_TUNER_FC2580:
  594. ret = regmap_write(dev->regmap, 0x112, 0x39);
  595. ret = regmap_write(dev->regmap, 0x102, 0x40);
  596. ret = regmap_write(dev->regmap, 0x103, 0x5a);
  597. ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
  598. ret = regmap_write(dev->regmap, 0x104, 0xcc);
  599. ret = regmap_write(dev->regmap, 0x105, 0xbe);
  600. ret = regmap_write(dev->regmap, 0x1c8, 0x16);
  601. ret = regmap_write(dev->regmap, 0x106, 0x35);
  602. ret = regmap_write(dev->regmap, 0x1c9, 0x21);
  603. ret = regmap_write(dev->regmap, 0x1ca, 0x21);
  604. ret = regmap_write(dev->regmap, 0x1cb, 0x00);
  605. ret = regmap_write(dev->regmap, 0x107, 0x40);
  606. ret = regmap_write(dev->regmap, 0x1cd, 0x10);
  607. ret = regmap_write(dev->regmap, 0x1ce, 0x10);
  608. ret = regmap_write(dev->regmap, 0x108, 0x80);
  609. ret = regmap_write(dev->regmap, 0x109, 0x7f);
  610. ret = regmap_write(dev->regmap, 0x10a, 0x9c);
  611. ret = regmap_write(dev->regmap, 0x10b, 0x7f);
  612. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  613. ret = regmap_write(dev->regmap, 0x00e, 0xfc);
  614. ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2);
  615. break;
  616. default:
  617. dev_notice(&pdev->dev, "Unsupported tuner\n");
  618. }
  619. /* software reset */
  620. ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04);
  621. if (ret)
  622. goto err;
  623. ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00);
  624. if (ret)
  625. goto err;
  626. err:
  627. return ret;
  628. };
  629. static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
  630. {
  631. struct platform_device *pdev = dev->pdev;
  632. int ret;
  633. dev_dbg(&pdev->dev, "\n");
  634. /* PID filter */
  635. ret = regmap_write(dev->regmap, 0x061, 0xe0);
  636. if (ret)
  637. goto err;
  638. /* mode */
  639. ret = regmap_write(dev->regmap, 0x019, 0x20);
  640. if (ret)
  641. goto err;
  642. ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
  643. if (ret)
  644. goto err;
  645. /* FSM */
  646. ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
  647. if (ret)
  648. goto err;
  649. ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2);
  650. if (ret)
  651. goto err;
  652. ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4);
  653. if (ret)
  654. goto err;
  655. err:
  656. return;
  657. };
  658. static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
  659. {
  660. struct platform_device *pdev = dev->pdev;
  661. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  662. struct dvb_frontend *fe = pdata->dvb_frontend;
  663. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  664. struct v4l2_ctrl *bandwidth_auto;
  665. struct v4l2_ctrl *bandwidth;
  666. /*
  667. * tuner RF (Hz)
  668. */
  669. if (dev->f_tuner == 0)
  670. return 0;
  671. /*
  672. * bandwidth (Hz)
  673. */
  674. bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
  675. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
  676. bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
  677. if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
  678. c->bandwidth_hz = dev->f_adc;
  679. v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
  680. } else {
  681. c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
  682. }
  683. c->frequency = dev->f_tuner;
  684. c->delivery_system = SYS_DVBT;
  685. dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
  686. c->frequency, c->bandwidth_hz);
  687. if (!test_bit(POWER_ON, &dev->flags))
  688. return 0;
  689. if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
  690. if (fe->ops.tuner_ops.set_params)
  691. fe->ops.tuner_ops.set_params(fe);
  692. }
  693. return 0;
  694. };
  695. static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
  696. {
  697. struct platform_device *pdev = dev->pdev;
  698. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  699. struct dvb_frontend *fe = pdata->dvb_frontend;
  700. dev_dbg(&pdev->dev, "\n");
  701. if (fe->ops.tuner_ops.init)
  702. fe->ops.tuner_ops.init(fe);
  703. return 0;
  704. };
  705. static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
  706. {
  707. struct platform_device *pdev = dev->pdev;
  708. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  709. struct dvb_frontend *fe = pdata->dvb_frontend;
  710. dev_dbg(&pdev->dev, "\n");
  711. if (fe->ops.tuner_ops.sleep)
  712. fe->ops.tuner_ops.sleep(fe);
  713. return;
  714. };
  715. static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
  716. {
  717. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  718. struct platform_device *pdev = dev->pdev;
  719. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  720. struct dvb_usb_device *d = pdata->dvb_usb_device;
  721. int ret;
  722. dev_dbg(&pdev->dev, "\n");
  723. if (!dev->udev)
  724. return -ENODEV;
  725. if (mutex_lock_interruptible(&dev->v4l2_lock))
  726. return -ERESTARTSYS;
  727. if (d->props->power_ctrl)
  728. d->props->power_ctrl(d, 1);
  729. /* enable ADC */
  730. if (d->props->frontend_ctrl)
  731. d->props->frontend_ctrl(pdata->dvb_frontend, 1);
  732. set_bit(POWER_ON, &dev->flags);
  733. /* wake-up tuner */
  734. if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
  735. ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
  736. else
  737. ret = rtl2832_sdr_set_tuner(dev);
  738. if (ret)
  739. goto err;
  740. ret = rtl2832_sdr_set_tuner_freq(dev);
  741. if (ret)
  742. goto err;
  743. ret = rtl2832_sdr_set_adc(dev);
  744. if (ret)
  745. goto err;
  746. ret = rtl2832_sdr_alloc_stream_bufs(dev);
  747. if (ret)
  748. goto err;
  749. ret = rtl2832_sdr_alloc_urbs(dev);
  750. if (ret)
  751. goto err;
  752. dev->sequence = 0;
  753. ret = rtl2832_sdr_submit_urbs(dev);
  754. if (ret)
  755. goto err;
  756. err:
  757. mutex_unlock(&dev->v4l2_lock);
  758. return ret;
  759. }
  760. static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
  761. {
  762. struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
  763. struct platform_device *pdev = dev->pdev;
  764. struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
  765. struct dvb_usb_device *d = pdata->dvb_usb_device;
  766. dev_dbg(&pdev->dev, "\n");
  767. mutex_lock(&dev->v4l2_lock);
  768. rtl2832_sdr_kill_urbs(dev);
  769. rtl2832_sdr_free_urbs(dev);
  770. rtl2832_sdr_free_stream_bufs(dev);
  771. rtl2832_sdr_cleanup_queued_bufs(dev);
  772. rtl2832_sdr_unset_adc(dev);
  773. /* sleep tuner */
  774. if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
  775. v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
  776. else
  777. rtl2832_sdr_unset_tuner(dev);
  778. clear_bit(POWER_ON, &dev->flags);
  779. /* disable ADC */
  780. if (d->props->frontend_ctrl)
  781. d->props->frontend_ctrl(pdata->dvb_frontend, 0);
  782. if (d->props->power_ctrl)
  783. d->props->power_ctrl(d, 0);
  784. mutex_unlock(&dev->v4l2_lock);
  785. }
  786. static const struct vb2_ops rtl2832_sdr_vb2_ops = {
  787. .queue_setup = rtl2832_sdr_queue_setup,
  788. .buf_prepare = rtl2832_sdr_buf_prepare,
  789. .buf_queue = rtl2832_sdr_buf_queue,
  790. .start_streaming = rtl2832_sdr_start_streaming,
  791. .stop_streaming = rtl2832_sdr_stop_streaming,
  792. .wait_prepare = vb2_ops_wait_prepare,
  793. .wait_finish = vb2_ops_wait_finish,
  794. };
  795. static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
  796. struct v4l2_tuner *v)
  797. {
  798. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  799. struct platform_device *pdev = dev->pdev;
  800. int ret;
  801. dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
  802. if (v->index == 0) {
  803. strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
  804. v->type = V4L2_TUNER_ADC;
  805. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  806. v->rangelow = 300000;
  807. v->rangehigh = 3200000;
  808. ret = 0;
  809. } else if (v->index == 1 &&
  810. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
  811. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
  812. } else if (v->index == 1) {
  813. strscpy(v->name, "RF: <unknown>", sizeof(v->name));
  814. v->type = V4L2_TUNER_RF;
  815. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  816. v->rangelow = 50000000;
  817. v->rangehigh = 2000000000;
  818. ret = 0;
  819. } else {
  820. ret = -EINVAL;
  821. }
  822. return ret;
  823. }
  824. static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
  825. const struct v4l2_tuner *v)
  826. {
  827. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  828. struct platform_device *pdev = dev->pdev;
  829. int ret;
  830. dev_dbg(&pdev->dev, "\n");
  831. if (v->index == 0) {
  832. ret = 0;
  833. } else if (v->index == 1 &&
  834. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
  835. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
  836. } else if (v->index == 1) {
  837. ret = 0;
  838. } else {
  839. ret = -EINVAL;
  840. }
  841. return ret;
  842. }
  843. static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
  844. struct v4l2_frequency_band *band)
  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, "tuner=%d type=%d index=%d\n",
  850. band->tuner, band->type, band->index);
  851. if (band->tuner == 0) {
  852. if (band->index >= ARRAY_SIZE(bands_adc))
  853. return -EINVAL;
  854. *band = bands_adc[band->index];
  855. ret = 0;
  856. } else if (band->tuner == 1 &&
  857. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
  858. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
  859. } else if (band->tuner == 1) {
  860. if (band->index >= ARRAY_SIZE(bands_fm))
  861. return -EINVAL;
  862. *band = bands_fm[band->index];
  863. ret = 0;
  864. } else {
  865. ret = -EINVAL;
  866. }
  867. return ret;
  868. }
  869. static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
  870. struct v4l2_frequency *f)
  871. {
  872. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  873. struct platform_device *pdev = dev->pdev;
  874. int ret;
  875. dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
  876. if (f->tuner == 0) {
  877. f->frequency = dev->f_adc;
  878. f->type = V4L2_TUNER_ADC;
  879. ret = 0;
  880. } else if (f->tuner == 1 &&
  881. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
  882. f->type = V4L2_TUNER_RF;
  883. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
  884. } else if (f->tuner == 1) {
  885. f->frequency = dev->f_tuner;
  886. f->type = V4L2_TUNER_RF;
  887. ret = 0;
  888. } else {
  889. ret = -EINVAL;
  890. }
  891. return ret;
  892. }
  893. static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
  894. const struct v4l2_frequency *f)
  895. {
  896. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  897. struct platform_device *pdev = dev->pdev;
  898. int ret, band;
  899. dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
  900. f->tuner, f->type, f->frequency);
  901. /* ADC band midpoints */
  902. #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
  903. #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
  904. if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
  905. if (f->frequency < BAND_ADC_0)
  906. band = 0;
  907. else if (f->frequency < BAND_ADC_1)
  908. band = 1;
  909. else
  910. band = 2;
  911. dev->f_adc = clamp_t(unsigned int, f->frequency,
  912. bands_adc[band].rangelow,
  913. bands_adc[band].rangehigh);
  914. dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
  915. ret = rtl2832_sdr_set_adc(dev);
  916. } else if (f->tuner == 1 &&
  917. V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
  918. ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
  919. } else if (f->tuner == 1) {
  920. dev->f_tuner = clamp_t(unsigned int, f->frequency,
  921. bands_fm[0].rangelow,
  922. bands_fm[0].rangehigh);
  923. dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
  924. ret = rtl2832_sdr_set_tuner_freq(dev);
  925. } else {
  926. ret = -EINVAL;
  927. }
  928. return ret;
  929. }
  930. static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
  931. struct v4l2_fmtdesc *f)
  932. {
  933. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  934. struct platform_device *pdev = dev->pdev;
  935. dev_dbg(&pdev->dev, "\n");
  936. if (f->index >= dev->num_formats)
  937. return -EINVAL;
  938. f->pixelformat = formats[f->index].pixelformat;
  939. return 0;
  940. }
  941. static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
  942. struct v4l2_format *f)
  943. {
  944. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  945. struct platform_device *pdev = dev->pdev;
  946. dev_dbg(&pdev->dev, "\n");
  947. f->fmt.sdr.pixelformat = dev->pixelformat;
  948. f->fmt.sdr.buffersize = dev->buffersize;
  949. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  950. return 0;
  951. }
  952. static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
  953. struct v4l2_format *f)
  954. {
  955. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  956. struct platform_device *pdev = dev->pdev;
  957. struct vb2_queue *q = &dev->vb_queue;
  958. int i;
  959. dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
  960. (char *)&f->fmt.sdr.pixelformat);
  961. if (vb2_is_busy(q))
  962. return -EBUSY;
  963. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  964. for (i = 0; i < dev->num_formats; i++) {
  965. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  966. dev->pixelformat = formats[i].pixelformat;
  967. dev->buffersize = formats[i].buffersize;
  968. f->fmt.sdr.buffersize = formats[i].buffersize;
  969. return 0;
  970. }
  971. }
  972. dev->pixelformat = formats[0].pixelformat;
  973. dev->buffersize = formats[0].buffersize;
  974. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  975. f->fmt.sdr.buffersize = formats[0].buffersize;
  976. return 0;
  977. }
  978. static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
  979. struct v4l2_format *f)
  980. {
  981. struct rtl2832_sdr_dev *dev = video_drvdata(file);
  982. struct platform_device *pdev = dev->pdev;
  983. int i;
  984. dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
  985. (char *)&f->fmt.sdr.pixelformat);
  986. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  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. goto err_v4l2_ctrl_handler_free;
  1192. }
  1193. if (dev->hdl.error) {
  1194. ret = dev->hdl.error;
  1195. dev_err(&pdev->dev, "Could not initialize controls\n");
  1196. goto err_v4l2_ctrl_handler_free;
  1197. }
  1198. /* Init video_device structure */
  1199. dev->vdev = rtl2832_sdr_template;
  1200. dev->vdev.queue = &dev->vb_queue;
  1201. dev->vdev.queue->lock = &dev->vb_queue_lock;
  1202. video_set_drvdata(&dev->vdev, dev);
  1203. /* Register the v4l2_device structure */
  1204. dev->v4l2_dev.release = rtl2832_sdr_video_release;
  1205. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1206. if (ret) {
  1207. dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
  1208. goto err_v4l2_ctrl_handler_free;
  1209. }
  1210. dev->v4l2_dev.ctrl_handler = &dev->hdl;
  1211. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1212. dev->vdev.lock = &dev->v4l2_lock;
  1213. dev->vdev.vfl_dir = VFL_DIR_RX;
  1214. ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
  1215. if (ret) {
  1216. dev_err(&pdev->dev, "Failed to register as video device %d\n",
  1217. ret);
  1218. goto err_v4l2_device_unregister;
  1219. }
  1220. dev_info(&pdev->dev, "Registered as %s\n",
  1221. video_device_node_name(&dev->vdev));
  1222. dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
  1223. dev_notice(&pdev->dev,
  1224. "SDR API is still slightly experimental and functionality changes may follow\n");
  1225. platform_set_drvdata(pdev, dev);
  1226. return 0;
  1227. err_v4l2_device_unregister:
  1228. v4l2_device_unregister(&dev->v4l2_dev);
  1229. err_v4l2_ctrl_handler_free:
  1230. v4l2_ctrl_handler_free(&dev->hdl);
  1231. err_kfree:
  1232. kfree(dev);
  1233. err_module_put:
  1234. module_put(pdev->dev.parent->driver->owner);
  1235. err:
  1236. return ret;
  1237. }
  1238. static int rtl2832_sdr_remove(struct platform_device *pdev)
  1239. {
  1240. struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
  1241. dev_dbg(&pdev->dev, "\n");
  1242. mutex_lock(&dev->vb_queue_lock);
  1243. mutex_lock(&dev->v4l2_lock);
  1244. /* No need to keep the urbs around after disconnection */
  1245. dev->udev = NULL;
  1246. v4l2_device_disconnect(&dev->v4l2_dev);
  1247. video_unregister_device(&dev->vdev);
  1248. mutex_unlock(&dev->v4l2_lock);
  1249. mutex_unlock(&dev->vb_queue_lock);
  1250. v4l2_device_put(&dev->v4l2_dev);
  1251. module_put(pdev->dev.parent->driver->owner);
  1252. return 0;
  1253. }
  1254. static struct platform_driver rtl2832_sdr_driver = {
  1255. .driver = {
  1256. .name = "rtl2832_sdr",
  1257. },
  1258. .probe = rtl2832_sdr_probe,
  1259. .remove = rtl2832_sdr_remove,
  1260. };
  1261. module_platform_driver(rtl2832_sdr_driver);
  1262. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1263. MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
  1264. MODULE_LICENSE("GPL");