cobalt-v4l2.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * cobalt V4L2 API
  3. *
  4. * Derived from ivtv-ioctl.c and cx18-fileops.c
  5. *
  6. * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
  7. * All rights reserved.
  8. *
  9. * This program is free software; you may redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include <linux/dma-mapping.h>
  23. #include <linux/delay.h>
  24. #include <linux/math64.h>
  25. #include <linux/pci.h>
  26. #include <linux/v4l2-dv-timings.h>
  27. #include <media/v4l2-ctrls.h>
  28. #include <media/v4l2-event.h>
  29. #include <media/v4l2-dv-timings.h>
  30. #include <media/adv7604.h>
  31. #include <media/adv7842.h>
  32. #include "cobalt-alsa.h"
  33. #include "cobalt-cpld.h"
  34. #include "cobalt-driver.h"
  35. #include "cobalt-v4l2.h"
  36. #include "cobalt-irq.h"
  37. #include "cobalt-omnitek.h"
  38. static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
  39. /* vb2 DMA streaming ops */
  40. static int cobalt_queue_setup(struct vb2_queue *q, const void *parg,
  41. unsigned int *num_buffers, unsigned int *num_planes,
  42. unsigned int sizes[], void *alloc_ctxs[])
  43. {
  44. const struct v4l2_format *fmt = parg;
  45. struct cobalt_stream *s = q->drv_priv;
  46. unsigned size = s->stride * s->height;
  47. if (*num_buffers < 3)
  48. *num_buffers = 3;
  49. if (*num_buffers > NR_BUFS)
  50. *num_buffers = NR_BUFS;
  51. *num_planes = 1;
  52. if (fmt) {
  53. if (fmt->fmt.pix.sizeimage < size)
  54. return -EINVAL;
  55. size = fmt->fmt.pix.sizeimage;
  56. }
  57. sizes[0] = size;
  58. alloc_ctxs[0] = s->cobalt->alloc_ctx;
  59. return 0;
  60. }
  61. static int cobalt_buf_init(struct vb2_buffer *vb)
  62. {
  63. struct cobalt_stream *s = vb->vb2_queue->drv_priv;
  64. struct cobalt *cobalt = s->cobalt;
  65. const size_t max_pages_per_line =
  66. (COBALT_MAX_WIDTH * COBALT_MAX_BPP) / PAGE_SIZE + 2;
  67. const size_t bytes =
  68. COBALT_MAX_HEIGHT * max_pages_per_line * 0x20;
  69. const size_t audio_bytes = ((1920 * 4) / PAGE_SIZE + 1) * 0x20;
  70. struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
  71. struct sg_table *sg_desc = vb2_dma_sg_plane_desc(vb, 0);
  72. unsigned size;
  73. int ret;
  74. size = s->stride * s->height;
  75. if (vb2_plane_size(vb, 0) < size) {
  76. cobalt_info("data will not fit into plane (%lu < %u)\n",
  77. vb2_plane_size(vb, 0), size);
  78. return -EINVAL;
  79. }
  80. if (desc->virt == NULL) {
  81. desc->dev = &cobalt->pci_dev->dev;
  82. descriptor_list_allocate(desc,
  83. s->is_audio ? audio_bytes : bytes);
  84. if (desc->virt == NULL)
  85. return -ENOMEM;
  86. }
  87. ret = descriptor_list_create(cobalt, sg_desc->sgl,
  88. !s->is_output, sg_desc->nents, size,
  89. s->width * s->bpp, s->stride, desc);
  90. if (ret)
  91. descriptor_list_free(desc);
  92. return ret;
  93. }
  94. static void cobalt_buf_cleanup(struct vb2_buffer *vb)
  95. {
  96. struct cobalt_stream *s = vb->vb2_queue->drv_priv;
  97. struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
  98. descriptor_list_free(desc);
  99. }
  100. static int cobalt_buf_prepare(struct vb2_buffer *vb)
  101. {
  102. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  103. struct cobalt_stream *s = vb->vb2_queue->drv_priv;
  104. vb2_set_plane_payload(vb, 0, s->stride * s->height);
  105. vbuf->field = V4L2_FIELD_NONE;
  106. return 0;
  107. }
  108. static void chain_all_buffers(struct cobalt_stream *s)
  109. {
  110. struct sg_dma_desc_info *desc[NR_BUFS];
  111. struct cobalt_buffer *cb;
  112. struct list_head *p;
  113. int i = 0;
  114. list_for_each(p, &s->bufs) {
  115. cb = list_entry(p, struct cobalt_buffer, list);
  116. desc[i] = &s->dma_desc_info[cb->vb.vb2_buf.index];
  117. if (i > 0)
  118. descriptor_list_chain(desc[i-1], desc[i]);
  119. i++;
  120. }
  121. }
  122. static void cobalt_buf_queue(struct vb2_buffer *vb)
  123. {
  124. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  125. struct vb2_queue *q = vb->vb2_queue;
  126. struct cobalt_stream *s = q->drv_priv;
  127. struct cobalt_buffer *cb = to_cobalt_buffer(vbuf);
  128. struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
  129. unsigned long flags;
  130. /* Prepare new buffer */
  131. descriptor_list_loopback(desc);
  132. descriptor_list_interrupt_disable(desc);
  133. spin_lock_irqsave(&s->irqlock, flags);
  134. list_add_tail(&cb->list, &s->bufs);
  135. chain_all_buffers(s);
  136. spin_unlock_irqrestore(&s->irqlock, flags);
  137. }
  138. static void cobalt_enable_output(struct cobalt_stream *s)
  139. {
  140. struct cobalt *cobalt = s->cobalt;
  141. struct v4l2_bt_timings *bt = &s->timings.bt;
  142. struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
  143. COBALT_TX_BASE(cobalt);
  144. unsigned fmt = s->pixfmt != V4L2_PIX_FMT_BGR32 ?
  145. M00514_CONTROL_BITMAP_FORMAT_16_BPP_MSK : 0;
  146. struct v4l2_subdev_format sd_fmt = {
  147. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  148. };
  149. if (!cobalt_cpld_set_freq(cobalt, bt->pixelclock)) {
  150. cobalt_err("pixelclock out of range\n");
  151. return;
  152. }
  153. sd_fmt.format.colorspace = s->colorspace;
  154. sd_fmt.format.xfer_func = s->xfer_func;
  155. sd_fmt.format.ycbcr_enc = s->ycbcr_enc;
  156. sd_fmt.format.quantization = s->quantization;
  157. sd_fmt.format.width = bt->width;
  158. sd_fmt.format.height = bt->height;
  159. /* Set up FDMA packer */
  160. switch (s->pixfmt) {
  161. case V4L2_PIX_FMT_YUYV:
  162. sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
  163. break;
  164. case V4L2_PIX_FMT_BGR32:
  165. sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24;
  166. break;
  167. }
  168. v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
  169. iowrite32(0, &vo->control);
  170. /* 1080p60 */
  171. iowrite32(bt->hsync, &vo->sync_generator_h_sync_length);
  172. iowrite32(bt->hbackporch, &vo->sync_generator_h_backporch_length);
  173. iowrite32(bt->width, &vo->sync_generator_h_active_length);
  174. iowrite32(bt->hfrontporch, &vo->sync_generator_h_frontporch_length);
  175. iowrite32(bt->vsync, &vo->sync_generator_v_sync_length);
  176. iowrite32(bt->vbackporch, &vo->sync_generator_v_backporch_length);
  177. iowrite32(bt->height, &vo->sync_generator_v_active_length);
  178. iowrite32(bt->vfrontporch, &vo->sync_generator_v_frontporch_length);
  179. iowrite32(0x9900c1, &vo->error_color);
  180. iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt,
  181. &vo->control);
  182. iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt, &vo->control);
  183. iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK |
  184. M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK |
  185. fmt, &vo->control);
  186. }
  187. static void cobalt_enable_input(struct cobalt_stream *s)
  188. {
  189. struct cobalt *cobalt = s->cobalt;
  190. int ch = (int)s->video_channel;
  191. struct m00235_fdma_packer_regmap __iomem *packer;
  192. struct v4l2_subdev_format sd_fmt_yuyv = {
  193. .pad = s->pad_source,
  194. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  195. .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
  196. };
  197. struct v4l2_subdev_format sd_fmt_rgb = {
  198. .pad = s->pad_source,
  199. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  200. .format.code = MEDIA_BUS_FMT_RGB888_1X24,
  201. };
  202. cobalt_dbg(1, "video_channel %d (%s, %s)\n",
  203. s->video_channel,
  204. s->input == 0 ? "hdmi" : "generator",
  205. "YUYV");
  206. packer = COBALT_CVI_PACKER(cobalt, ch);
  207. /* Set up FDMA packer */
  208. switch (s->pixfmt) {
  209. case V4L2_PIX_FMT_YUYV:
  210. iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
  211. (1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
  212. &packer->control);
  213. v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
  214. &sd_fmt_yuyv);
  215. break;
  216. case V4L2_PIX_FMT_RGB24:
  217. iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
  218. (2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
  219. &packer->control);
  220. v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
  221. &sd_fmt_rgb);
  222. break;
  223. case V4L2_PIX_FMT_BGR32:
  224. iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
  225. M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK |
  226. (3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
  227. &packer->control);
  228. v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
  229. &sd_fmt_rgb);
  230. break;
  231. }
  232. }
  233. static void cobalt_dma_start_streaming(struct cobalt_stream *s)
  234. {
  235. struct cobalt *cobalt = s->cobalt;
  236. int rx = s->video_channel;
  237. struct m00460_evcnt_regmap __iomem *evcnt =
  238. COBALT_CVI_EVCNT(cobalt, rx);
  239. struct cobalt_buffer *cb;
  240. unsigned long flags;
  241. spin_lock_irqsave(&s->irqlock, flags);
  242. if (!s->is_output) {
  243. iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
  244. iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
  245. } else {
  246. struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
  247. COBALT_TX_BASE(cobalt);
  248. u32 ctrl = ioread32(&vo->control);
  249. ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK |
  250. M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK);
  251. iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK,
  252. &vo->control);
  253. iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK,
  254. &vo->control);
  255. }
  256. cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
  257. omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.vb2_buf.index]);
  258. spin_unlock_irqrestore(&s->irqlock, flags);
  259. }
  260. static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count)
  261. {
  262. struct cobalt_stream *s = q->drv_priv;
  263. struct cobalt *cobalt = s->cobalt;
  264. struct m00233_video_measure_regmap __iomem *vmr;
  265. struct m00473_freewheel_regmap __iomem *fw;
  266. struct m00479_clk_loss_detector_regmap __iomem *clkloss;
  267. int rx = s->video_channel;
  268. struct m00389_cvi_regmap __iomem *cvi = COBALT_CVI(cobalt, rx);
  269. struct m00460_evcnt_regmap __iomem *evcnt = COBALT_CVI_EVCNT(cobalt, rx);
  270. struct v4l2_bt_timings *bt = &s->timings.bt;
  271. u64 tot_size;
  272. u32 clk_freq;
  273. if (s->is_audio)
  274. goto done;
  275. if (s->is_output) {
  276. s->unstable_frame = false;
  277. cobalt_enable_output(s);
  278. goto done;
  279. }
  280. cobalt_enable_input(s);
  281. fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
  282. vmr = COBALT_CVI_VMR(cobalt, rx);
  283. clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
  284. iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
  285. iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
  286. iowrite32(bt->width, &cvi->frame_width);
  287. iowrite32(bt->height, &cvi->frame_height);
  288. tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
  289. iowrite32(div_u64((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4,
  290. bt->pixelclock), &vmr->hsync_timeout_val);
  291. iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
  292. clk_freq = ioread32(&fw->clk_freq);
  293. iowrite32(clk_freq / 1000000, &clkloss->ref_clk_cnt_val);
  294. /* The lower bound for the clock frequency is 0.5% lower as is
  295. * allowed by the spec */
  296. iowrite32(div_u64(bt->pixelclock * 995, 1000000000),
  297. &clkloss->test_clk_cnt_val);
  298. /* will be enabled after the first frame has been received */
  299. iowrite32(bt->width * bt->height, &fw->active_length);
  300. iowrite32(div_u64((u64)clk_freq * tot_size, bt->pixelclock),
  301. &fw->total_length);
  302. iowrite32(M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK |
  303. M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK,
  304. &vmr->irq_triggers);
  305. iowrite32(0, &cvi->control);
  306. iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
  307. iowrite32(0xff, &fw->output_color);
  308. iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
  309. iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
  310. M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK, &fw->ctrl);
  311. s->unstable_frame = true;
  312. s->enable_freewheel = false;
  313. s->enable_cvi = false;
  314. s->skip_first_frames = 0;
  315. done:
  316. s->sequence = 0;
  317. cobalt_dma_start_streaming(s);
  318. return 0;
  319. }
  320. static void cobalt_dma_stop_streaming(struct cobalt_stream *s)
  321. {
  322. struct cobalt *cobalt = s->cobalt;
  323. struct sg_dma_desc_info *desc;
  324. struct cobalt_buffer *cb;
  325. struct list_head *p;
  326. unsigned long flags;
  327. int timeout_msec = 100;
  328. int rx = s->video_channel;
  329. struct m00460_evcnt_regmap __iomem *evcnt =
  330. COBALT_CVI_EVCNT(cobalt, rx);
  331. if (!s->is_output) {
  332. iowrite32(0, &evcnt->control);
  333. } else if (!s->is_audio) {
  334. struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
  335. COBALT_TX_BASE(cobalt);
  336. iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, &vo->control);
  337. iowrite32(0, &vo->control);
  338. }
  339. /* Try to stop the DMA engine gracefully */
  340. spin_lock_irqsave(&s->irqlock, flags);
  341. list_for_each(p, &s->bufs) {
  342. cb = list_entry(p, struct cobalt_buffer, list);
  343. desc = &s->dma_desc_info[cb->vb.vb2_buf.index];
  344. /* Stop DMA after this descriptor chain */
  345. descriptor_list_end_of_chain(desc);
  346. }
  347. spin_unlock_irqrestore(&s->irqlock, flags);
  348. /* Wait 100 milisecond for DMA to finish, abort on timeout. */
  349. if (!wait_event_timeout(s->q.done_wq, is_dma_done(s),
  350. msecs_to_jiffies(timeout_msec))) {
  351. omni_sg_dma_abort_channel(s);
  352. pr_warn("aborted\n");
  353. }
  354. cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG,
  355. 1 << s->dma_channel);
  356. }
  357. static void cobalt_stop_streaming(struct vb2_queue *q)
  358. {
  359. struct cobalt_stream *s = q->drv_priv;
  360. struct cobalt *cobalt = s->cobalt;
  361. int rx = s->video_channel;
  362. struct m00233_video_measure_regmap __iomem *vmr;
  363. struct m00473_freewheel_regmap __iomem *fw;
  364. struct m00479_clk_loss_detector_regmap __iomem *clkloss;
  365. struct cobalt_buffer *cb;
  366. struct list_head *p, *safe;
  367. unsigned long flags;
  368. cobalt_dma_stop_streaming(s);
  369. /* Return all buffers to user space */
  370. spin_lock_irqsave(&s->irqlock, flags);
  371. list_for_each_safe(p, safe, &s->bufs) {
  372. cb = list_entry(p, struct cobalt_buffer, list);
  373. list_del(&cb->list);
  374. vb2_buffer_done(&cb->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  375. }
  376. spin_unlock_irqrestore(&s->irqlock, flags);
  377. if (s->is_audio || s->is_output)
  378. return;
  379. fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
  380. vmr = COBALT_CVI_VMR(cobalt, rx);
  381. clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
  382. iowrite32(0, &vmr->control);
  383. iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
  384. iowrite32(0, &fw->ctrl);
  385. iowrite32(0, &clkloss->ctrl);
  386. }
  387. static const struct vb2_ops cobalt_qops = {
  388. .queue_setup = cobalt_queue_setup,
  389. .buf_init = cobalt_buf_init,
  390. .buf_cleanup = cobalt_buf_cleanup,
  391. .buf_prepare = cobalt_buf_prepare,
  392. .buf_queue = cobalt_buf_queue,
  393. .start_streaming = cobalt_start_streaming,
  394. .stop_streaming = cobalt_stop_streaming,
  395. .wait_prepare = vb2_ops_wait_prepare,
  396. .wait_finish = vb2_ops_wait_finish,
  397. };
  398. /* V4L2 ioctls */
  399. #ifdef CONFIG_VIDEO_ADV_DEBUG
  400. static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg)
  401. {
  402. struct v4l2_dbg_register *regs = arg;
  403. void __iomem *adrs = cobalt->bar1 + regs->reg;
  404. cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs);
  405. if (!capable(CAP_SYS_ADMIN))
  406. return -EPERM;
  407. regs->size = 4;
  408. if (cmd == VIDIOC_DBG_S_REGISTER)
  409. iowrite32(regs->val, adrs);
  410. else
  411. regs->val = ioread32(adrs);
  412. return 0;
  413. }
  414. static int cobalt_g_register(struct file *file, void *priv_fh,
  415. struct v4l2_dbg_register *reg)
  416. {
  417. struct cobalt_stream *s = video_drvdata(file);
  418. struct cobalt *cobalt = s->cobalt;
  419. return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg);
  420. }
  421. static int cobalt_s_register(struct file *file, void *priv_fh,
  422. const struct v4l2_dbg_register *reg)
  423. {
  424. struct cobalt_stream *s = video_drvdata(file);
  425. struct cobalt *cobalt = s->cobalt;
  426. return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER,
  427. (struct v4l2_dbg_register *)reg);
  428. }
  429. #endif
  430. static int cobalt_querycap(struct file *file, void *priv_fh,
  431. struct v4l2_capability *vcap)
  432. {
  433. struct cobalt_stream *s = video_drvdata(file);
  434. struct cobalt *cobalt = s->cobalt;
  435. strlcpy(vcap->driver, "cobalt", sizeof(vcap->driver));
  436. strlcpy(vcap->card, "cobalt", sizeof(vcap->card));
  437. snprintf(vcap->bus_info, sizeof(vcap->bus_info),
  438. "PCIe:%s", pci_name(cobalt->pci_dev));
  439. vcap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  440. if (s->is_output)
  441. vcap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
  442. else
  443. vcap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  444. vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS |
  445. V4L2_CAP_VIDEO_CAPTURE;
  446. if (cobalt->have_hsma_tx)
  447. vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
  448. return 0;
  449. }
  450. static void cobalt_video_input_status_show(struct cobalt_stream *s)
  451. {
  452. struct m00389_cvi_regmap __iomem *cvi;
  453. struct m00233_video_measure_regmap __iomem *vmr;
  454. struct m00473_freewheel_regmap __iomem *fw;
  455. struct m00479_clk_loss_detector_regmap __iomem *clkloss;
  456. struct m00235_fdma_packer_regmap __iomem *packer;
  457. int rx = s->video_channel;
  458. struct cobalt *cobalt = s->cobalt;
  459. u32 cvi_ctrl, cvi_stat;
  460. u32 vmr_ctrl, vmr_stat;
  461. cvi = COBALT_CVI(cobalt, rx);
  462. vmr = COBALT_CVI_VMR(cobalt, rx);
  463. fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
  464. clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
  465. packer = COBALT_CVI_PACKER(cobalt, rx);
  466. cvi_ctrl = ioread32(&cvi->control);
  467. cvi_stat = ioread32(&cvi->status);
  468. vmr_ctrl = ioread32(&vmr->control);
  469. vmr_stat = ioread32(&vmr->control);
  470. cobalt_info("rx%d: cvi resolution: %dx%d\n", rx,
  471. ioread32(&cvi->frame_width), ioread32(&cvi->frame_height));
  472. cobalt_info("rx%d: cvi control: %s%s%s\n", rx,
  473. (cvi_ctrl & M00389_CONTROL_BITMAP_ENABLE_MSK) ?
  474. "enable " : "disable ",
  475. (cvi_ctrl & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
  476. "HSync- " : "HSync+ ",
  477. (cvi_ctrl & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
  478. "VSync- " : "VSync+ ");
  479. cobalt_info("rx%d: cvi status: %s%s\n", rx,
  480. (cvi_stat & M00389_STATUS_BITMAP_LOCK_MSK) ?
  481. "lock " : "no-lock ",
  482. (cvi_stat & M00389_STATUS_BITMAP_ERROR_MSK) ?
  483. "error " : "no-error ");
  484. cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx,
  485. (vmr_ctrl & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
  486. "HSync- " : "HSync+ ",
  487. (vmr_ctrl & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
  488. "VSync- " : "VSync+ ",
  489. (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ?
  490. "enabled " : "disabled ",
  491. (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ?
  492. "irq-enabled " : "irq-disabled ",
  493. (vmr_ctrl & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ?
  494. "update-on-hsync " : "",
  495. (vmr_stat & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ?
  496. "hsync-timeout " : "",
  497. (vmr_stat & M00233_STATUS_BITMAP_INIT_DONE_MSK) ?
  498. "init-done" : "");
  499. cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx,
  500. ioread32(&vmr->irq_status) & 0xff,
  501. ioread32(&vmr->irq_triggers) & 0xff);
  502. cobalt_info("rx%d: vsync: %d\n", rx, ioread32(&vmr->vsync_time));
  503. cobalt_info("rx%d: vbp: %d\n", rx, ioread32(&vmr->vback_porch));
  504. cobalt_info("rx%d: vact: %d\n", rx, ioread32(&vmr->vactive_area));
  505. cobalt_info("rx%d: vfb: %d\n", rx, ioread32(&vmr->vfront_porch));
  506. cobalt_info("rx%d: hsync: %d\n", rx, ioread32(&vmr->hsync_time));
  507. cobalt_info("rx%d: hbp: %d\n", rx, ioread32(&vmr->hback_porch));
  508. cobalt_info("rx%d: hact: %d\n", rx, ioread32(&vmr->hactive_area));
  509. cobalt_info("rx%d: hfb: %d\n", rx, ioread32(&vmr->hfront_porch));
  510. cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx,
  511. (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_ENABLE_MSK) ?
  512. "enabled " : "disabled ",
  513. (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ?
  514. "forced " : "",
  515. (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ?
  516. "freewheeling " : "video-passthrough ");
  517. iowrite32(0xff, &vmr->irq_status);
  518. cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx,
  519. (ioread32(&clkloss->ctrl) & M00479_CTRL_BITMAP_ENABLE_MSK) ?
  520. "enabled " : "disabled ",
  521. (ioread32(&clkloss->status) & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ?
  522. "clock-missing " : "found-clock ");
  523. cobalt_info("rx%d: Packer: %x\n", rx, ioread32(&packer->control));
  524. }
  525. static int cobalt_log_status(struct file *file, void *priv_fh)
  526. {
  527. struct cobalt_stream *s = video_drvdata(file);
  528. struct cobalt *cobalt = s->cobalt;
  529. struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
  530. COBALT_TX_BASE(cobalt);
  531. u8 stat;
  532. cobalt_info("%s", cobalt->hdl_info);
  533. cobalt_info("sysctrl: %08x, sysstat: %08x\n",
  534. cobalt_g_sysctrl(cobalt),
  535. cobalt_g_sysstat(cobalt));
  536. cobalt_info("dma channel: %d, video channel: %d\n",
  537. s->dma_channel, s->video_channel);
  538. cobalt_pcie_status_show(cobalt);
  539. cobalt_cpld_status(cobalt);
  540. cobalt_irq_log_status(cobalt);
  541. v4l2_subdev_call(s->sd, core, log_status);
  542. if (!s->is_output) {
  543. cobalt_video_input_status_show(s);
  544. return 0;
  545. }
  546. stat = ioread32(&vo->rd_status);
  547. cobalt_info("tx: status: %s%s\n",
  548. (stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ?
  549. "no_data " : "",
  550. (stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ?
  551. "ready_buffer_full " : "");
  552. cobalt_info("tx: evcnt: %d\n", ioread32(&vo->rd_evcnt_count));
  553. return 0;
  554. }
  555. static int cobalt_enum_dv_timings(struct file *file, void *priv_fh,
  556. struct v4l2_enum_dv_timings *timings)
  557. {
  558. struct cobalt_stream *s = video_drvdata(file);
  559. if (s->input == 1) {
  560. if (timings->index)
  561. return -EINVAL;
  562. memset(timings->reserved, 0, sizeof(timings->reserved));
  563. timings->timings = cea1080p60;
  564. return 0;
  565. }
  566. timings->pad = 0;
  567. return v4l2_subdev_call(s->sd,
  568. pad, enum_dv_timings, timings);
  569. }
  570. static int cobalt_s_dv_timings(struct file *file, void *priv_fh,
  571. struct v4l2_dv_timings *timings)
  572. {
  573. struct cobalt_stream *s = video_drvdata(file);
  574. int err;
  575. if (s->input == 1) {
  576. *timings = cea1080p60;
  577. return 0;
  578. }
  579. if (v4l2_match_dv_timings(timings, &s->timings, 0))
  580. return 0;
  581. if (vb2_is_busy(&s->q))
  582. return -EBUSY;
  583. err = v4l2_subdev_call(s->sd,
  584. video, s_dv_timings, timings);
  585. if (!err) {
  586. s->timings = *timings;
  587. s->width = timings->bt.width;
  588. s->height = timings->bt.height;
  589. s->stride = timings->bt.width * s->bpp;
  590. }
  591. return err;
  592. }
  593. static int cobalt_g_dv_timings(struct file *file, void *priv_fh,
  594. struct v4l2_dv_timings *timings)
  595. {
  596. struct cobalt_stream *s = video_drvdata(file);
  597. if (s->input == 1) {
  598. *timings = cea1080p60;
  599. return 0;
  600. }
  601. return v4l2_subdev_call(s->sd,
  602. video, g_dv_timings, timings);
  603. }
  604. static int cobalt_query_dv_timings(struct file *file, void *priv_fh,
  605. struct v4l2_dv_timings *timings)
  606. {
  607. struct cobalt_stream *s = video_drvdata(file);
  608. if (s->input == 1) {
  609. *timings = cea1080p60;
  610. return 0;
  611. }
  612. return v4l2_subdev_call(s->sd,
  613. video, query_dv_timings, timings);
  614. }
  615. static int cobalt_dv_timings_cap(struct file *file, void *priv_fh,
  616. struct v4l2_dv_timings_cap *cap)
  617. {
  618. struct cobalt_stream *s = video_drvdata(file);
  619. cap->pad = 0;
  620. return v4l2_subdev_call(s->sd,
  621. pad, dv_timings_cap, cap);
  622. }
  623. static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh,
  624. struct v4l2_fmtdesc *f)
  625. {
  626. switch (f->index) {
  627. case 0:
  628. strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
  629. f->pixelformat = V4L2_PIX_FMT_YUYV;
  630. break;
  631. case 1:
  632. strlcpy(f->description, "RGB24", sizeof(f->description));
  633. f->pixelformat = V4L2_PIX_FMT_RGB24;
  634. break;
  635. case 2:
  636. strlcpy(f->description, "RGB32", sizeof(f->description));
  637. f->pixelformat = V4L2_PIX_FMT_BGR32;
  638. break;
  639. default:
  640. return -EINVAL;
  641. }
  642. return 0;
  643. }
  644. static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh,
  645. struct v4l2_format *f)
  646. {
  647. struct cobalt_stream *s = video_drvdata(file);
  648. struct v4l2_pix_format *pix = &f->fmt.pix;
  649. struct v4l2_subdev_format sd_fmt;
  650. pix->width = s->width;
  651. pix->height = s->height;
  652. pix->bytesperline = s->stride;
  653. pix->field = V4L2_FIELD_NONE;
  654. if (s->input == 1) {
  655. pix->colorspace = V4L2_COLORSPACE_SRGB;
  656. } else {
  657. sd_fmt.pad = s->pad_source;
  658. sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  659. v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
  660. v4l2_fill_pix_format(pix, &sd_fmt.format);
  661. }
  662. pix->pixelformat = s->pixfmt;
  663. pix->sizeimage = pix->bytesperline * pix->height;
  664. return 0;
  665. }
  666. static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh,
  667. struct v4l2_format *f)
  668. {
  669. struct cobalt_stream *s = video_drvdata(file);
  670. struct v4l2_pix_format *pix = &f->fmt.pix;
  671. struct v4l2_subdev_format sd_fmt;
  672. /* Check for min (QCIF) and max (Full HD) size */
  673. if ((pix->width < 176) || (pix->height < 144)) {
  674. pix->width = 176;
  675. pix->height = 144;
  676. }
  677. if ((pix->width > 1920) || (pix->height > 1080)) {
  678. pix->width = 1920;
  679. pix->height = 1080;
  680. }
  681. /* Make width multiple of 4 */
  682. pix->width &= ~0x3;
  683. /* Make height multiple of 2 */
  684. pix->height &= ~0x1;
  685. if (s->input == 1) {
  686. /* Generator => fixed format only */
  687. pix->width = 1920;
  688. pix->height = 1080;
  689. pix->colorspace = V4L2_COLORSPACE_SRGB;
  690. } else {
  691. sd_fmt.pad = s->pad_source;
  692. sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  693. v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
  694. v4l2_fill_pix_format(pix, &sd_fmt.format);
  695. }
  696. switch (pix->pixelformat) {
  697. case V4L2_PIX_FMT_YUYV:
  698. default:
  699. pix->bytesperline = max(pix->bytesperline & ~0x3,
  700. pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
  701. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  702. break;
  703. case V4L2_PIX_FMT_RGB24:
  704. pix->bytesperline = max(pix->bytesperline & ~0x3,
  705. pix->width * COBALT_BYTES_PER_PIXEL_RGB24);
  706. break;
  707. case V4L2_PIX_FMT_BGR32:
  708. pix->bytesperline = max(pix->bytesperline & ~0x3,
  709. pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
  710. break;
  711. }
  712. pix->sizeimage = pix->bytesperline * pix->height;
  713. pix->field = V4L2_FIELD_NONE;
  714. pix->priv = 0;
  715. return 0;
  716. }
  717. static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh,
  718. struct v4l2_format *f)
  719. {
  720. struct cobalt_stream *s = video_drvdata(file);
  721. struct v4l2_pix_format *pix = &f->fmt.pix;
  722. if (vb2_is_busy(&s->q))
  723. return -EBUSY;
  724. if (cobalt_try_fmt_vid_cap(file, priv_fh, f))
  725. return -EINVAL;
  726. s->width = pix->width;
  727. s->height = pix->height;
  728. s->stride = pix->bytesperline;
  729. switch (pix->pixelformat) {
  730. case V4L2_PIX_FMT_YUYV:
  731. s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
  732. break;
  733. case V4L2_PIX_FMT_RGB24:
  734. s->bpp = COBALT_BYTES_PER_PIXEL_RGB24;
  735. break;
  736. case V4L2_PIX_FMT_BGR32:
  737. s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
  738. break;
  739. default:
  740. return -EINVAL;
  741. }
  742. s->pixfmt = pix->pixelformat;
  743. cobalt_enable_input(s);
  744. return 0;
  745. }
  746. static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh,
  747. struct v4l2_format *f)
  748. {
  749. struct v4l2_pix_format *pix = &f->fmt.pix;
  750. /* Check for min (QCIF) and max (Full HD) size */
  751. if ((pix->width < 176) || (pix->height < 144)) {
  752. pix->width = 176;
  753. pix->height = 144;
  754. }
  755. if ((pix->width > 1920) || (pix->height > 1080)) {
  756. pix->width = 1920;
  757. pix->height = 1080;
  758. }
  759. /* Make width multiple of 4 */
  760. pix->width &= ~0x3;
  761. /* Make height multiple of 2 */
  762. pix->height &= ~0x1;
  763. switch (pix->pixelformat) {
  764. case V4L2_PIX_FMT_YUYV:
  765. default:
  766. pix->bytesperline = max(pix->bytesperline & ~0x3,
  767. pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
  768. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  769. break;
  770. case V4L2_PIX_FMT_BGR32:
  771. pix->bytesperline = max(pix->bytesperline & ~0x3,
  772. pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
  773. break;
  774. }
  775. pix->sizeimage = pix->bytesperline * pix->height;
  776. pix->field = V4L2_FIELD_NONE;
  777. return 0;
  778. }
  779. static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh,
  780. struct v4l2_format *f)
  781. {
  782. struct cobalt_stream *s = video_drvdata(file);
  783. struct v4l2_pix_format *pix = &f->fmt.pix;
  784. pix->width = s->width;
  785. pix->height = s->height;
  786. pix->bytesperline = s->stride;
  787. pix->field = V4L2_FIELD_NONE;
  788. pix->pixelformat = s->pixfmt;
  789. pix->colorspace = s->colorspace;
  790. pix->xfer_func = s->xfer_func;
  791. pix->ycbcr_enc = s->ycbcr_enc;
  792. pix->quantization = s->quantization;
  793. pix->sizeimage = pix->bytesperline * pix->height;
  794. return 0;
  795. }
  796. static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh,
  797. struct v4l2_fmtdesc *f)
  798. {
  799. switch (f->index) {
  800. case 0:
  801. strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
  802. f->pixelformat = V4L2_PIX_FMT_YUYV;
  803. break;
  804. case 1:
  805. strlcpy(f->description, "RGB32", sizeof(f->description));
  806. f->pixelformat = V4L2_PIX_FMT_BGR32;
  807. break;
  808. default:
  809. return -EINVAL;
  810. }
  811. return 0;
  812. }
  813. static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh,
  814. struct v4l2_format *f)
  815. {
  816. struct cobalt_stream *s = video_drvdata(file);
  817. struct v4l2_pix_format *pix = &f->fmt.pix;
  818. struct v4l2_subdev_format sd_fmt = { 0 };
  819. u32 code;
  820. if (cobalt_try_fmt_vid_out(file, priv_fh, f))
  821. return -EINVAL;
  822. if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt ||
  823. pix->width != s->width || pix->height != s->height ||
  824. pix->bytesperline != s->stride))
  825. return -EBUSY;
  826. switch (pix->pixelformat) {
  827. case V4L2_PIX_FMT_YUYV:
  828. s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
  829. code = MEDIA_BUS_FMT_UYVY8_1X16;
  830. break;
  831. case V4L2_PIX_FMT_BGR32:
  832. s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
  833. code = MEDIA_BUS_FMT_RGB888_1X24;
  834. break;
  835. default:
  836. return -EINVAL;
  837. }
  838. s->width = pix->width;
  839. s->height = pix->height;
  840. s->stride = pix->bytesperline;
  841. s->pixfmt = pix->pixelformat;
  842. s->colorspace = pix->colorspace;
  843. s->xfer_func = pix->xfer_func;
  844. s->ycbcr_enc = pix->ycbcr_enc;
  845. s->quantization = pix->quantization;
  846. sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  847. v4l2_fill_mbus_format(&sd_fmt.format, pix, code);
  848. v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
  849. return 0;
  850. }
  851. static int cobalt_enum_input(struct file *file, void *priv_fh,
  852. struct v4l2_input *inp)
  853. {
  854. struct cobalt_stream *s = video_drvdata(file);
  855. if (inp->index > 1)
  856. return -EINVAL;
  857. if (inp->index == 0)
  858. snprintf(inp->name, sizeof(inp->name),
  859. "HDMI-%d", s->video_channel);
  860. else
  861. snprintf(inp->name, sizeof(inp->name),
  862. "Generator-%d", s->video_channel);
  863. inp->type = V4L2_INPUT_TYPE_CAMERA;
  864. inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  865. if (inp->index == 1)
  866. return 0;
  867. return v4l2_subdev_call(s->sd,
  868. video, g_input_status, &inp->status);
  869. }
  870. static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i)
  871. {
  872. struct cobalt_stream *s = video_drvdata(file);
  873. *i = s->input;
  874. return 0;
  875. }
  876. static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i)
  877. {
  878. struct cobalt_stream *s = video_drvdata(file);
  879. if (i >= 2)
  880. return -EINVAL;
  881. if (vb2_is_busy(&s->q))
  882. return -EBUSY;
  883. s->input = i;
  884. cobalt_enable_input(s);
  885. if (s->input == 1) /* Test Pattern Generator */
  886. return 0;
  887. return v4l2_subdev_call(s->sd, video, s_routing,
  888. ADV76XX_PAD_HDMI_PORT_A, 0, 0);
  889. }
  890. static int cobalt_enum_output(struct file *file, void *priv_fh,
  891. struct v4l2_output *out)
  892. {
  893. if (out->index)
  894. return -EINVAL;
  895. snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index);
  896. out->type = V4L2_OUTPUT_TYPE_ANALOG;
  897. out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
  898. return 0;
  899. }
  900. static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i)
  901. {
  902. *i = 0;
  903. return 0;
  904. }
  905. static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i)
  906. {
  907. return i ? -EINVAL : 0;
  908. }
  909. static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
  910. {
  911. struct cobalt_stream *s = video_drvdata(file);
  912. u32 pad = edid->pad;
  913. int ret;
  914. if (edid->pad >= (s->is_output ? 1 : 2))
  915. return -EINVAL;
  916. edid->pad = 0;
  917. ret = v4l2_subdev_call(s->sd, pad, get_edid, edid);
  918. edid->pad = pad;
  919. return ret;
  920. }
  921. static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
  922. {
  923. struct cobalt_stream *s = video_drvdata(file);
  924. u32 pad = edid->pad;
  925. int ret;
  926. if (edid->pad >= 2)
  927. return -EINVAL;
  928. edid->pad = 0;
  929. ret = v4l2_subdev_call(s->sd, pad, set_edid, edid);
  930. edid->pad = pad;
  931. return ret;
  932. }
  933. static int cobalt_subscribe_event(struct v4l2_fh *fh,
  934. const struct v4l2_event_subscription *sub)
  935. {
  936. switch (sub->type) {
  937. case V4L2_EVENT_SOURCE_CHANGE:
  938. return v4l2_event_subscribe(fh, sub, 4, NULL);
  939. }
  940. return v4l2_ctrl_subscribe_event(fh, sub);
  941. }
  942. static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
  943. {
  944. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  945. return -EINVAL;
  946. a->parm.capture.timeperframe.numerator = 1;
  947. a->parm.capture.timeperframe.denominator = 60;
  948. a->parm.capture.readbuffers = 3;
  949. return 0;
  950. }
  951. static const struct v4l2_ioctl_ops cobalt_ioctl_ops = {
  952. .vidioc_querycap = cobalt_querycap,
  953. .vidioc_g_parm = cobalt_g_parm,
  954. .vidioc_log_status = cobalt_log_status,
  955. .vidioc_streamon = vb2_ioctl_streamon,
  956. .vidioc_streamoff = vb2_ioctl_streamoff,
  957. .vidioc_enum_input = cobalt_enum_input,
  958. .vidioc_g_input = cobalt_g_input,
  959. .vidioc_s_input = cobalt_s_input,
  960. .vidioc_enum_fmt_vid_cap = cobalt_enum_fmt_vid_cap,
  961. .vidioc_g_fmt_vid_cap = cobalt_g_fmt_vid_cap,
  962. .vidioc_s_fmt_vid_cap = cobalt_s_fmt_vid_cap,
  963. .vidioc_try_fmt_vid_cap = cobalt_try_fmt_vid_cap,
  964. .vidioc_enum_output = cobalt_enum_output,
  965. .vidioc_g_output = cobalt_g_output,
  966. .vidioc_s_output = cobalt_s_output,
  967. .vidioc_enum_fmt_vid_out = cobalt_enum_fmt_vid_out,
  968. .vidioc_g_fmt_vid_out = cobalt_g_fmt_vid_out,
  969. .vidioc_s_fmt_vid_out = cobalt_s_fmt_vid_out,
  970. .vidioc_try_fmt_vid_out = cobalt_try_fmt_vid_out,
  971. .vidioc_s_dv_timings = cobalt_s_dv_timings,
  972. .vidioc_g_dv_timings = cobalt_g_dv_timings,
  973. .vidioc_query_dv_timings = cobalt_query_dv_timings,
  974. .vidioc_enum_dv_timings = cobalt_enum_dv_timings,
  975. .vidioc_dv_timings_cap = cobalt_dv_timings_cap,
  976. .vidioc_g_edid = cobalt_g_edid,
  977. .vidioc_s_edid = cobalt_s_edid,
  978. .vidioc_subscribe_event = cobalt_subscribe_event,
  979. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  980. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  981. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  982. .vidioc_querybuf = vb2_ioctl_querybuf,
  983. .vidioc_qbuf = vb2_ioctl_qbuf,
  984. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  985. .vidioc_expbuf = vb2_ioctl_expbuf,
  986. #ifdef CONFIG_VIDEO_ADV_DEBUG
  987. .vidioc_g_register = cobalt_g_register,
  988. .vidioc_s_register = cobalt_s_register,
  989. #endif
  990. };
  991. static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = {
  992. #ifdef CONFIG_VIDEO_ADV_DEBUG
  993. .vidioc_g_register = cobalt_g_register,
  994. .vidioc_s_register = cobalt_s_register,
  995. #endif
  996. };
  997. /* Register device nodes */
  998. static const struct v4l2_file_operations cobalt_fops = {
  999. .owner = THIS_MODULE,
  1000. .open = v4l2_fh_open,
  1001. .unlocked_ioctl = video_ioctl2,
  1002. .release = vb2_fop_release,
  1003. .poll = vb2_fop_poll,
  1004. .mmap = vb2_fop_mmap,
  1005. .read = vb2_fop_read,
  1006. };
  1007. static const struct v4l2_file_operations cobalt_out_fops = {
  1008. .owner = THIS_MODULE,
  1009. .open = v4l2_fh_open,
  1010. .unlocked_ioctl = video_ioctl2,
  1011. .release = vb2_fop_release,
  1012. .poll = vb2_fop_poll,
  1013. .mmap = vb2_fop_mmap,
  1014. .write = vb2_fop_write,
  1015. };
  1016. static const struct v4l2_file_operations cobalt_empty_fops = {
  1017. .owner = THIS_MODULE,
  1018. .open = v4l2_fh_open,
  1019. .unlocked_ioctl = video_ioctl2,
  1020. .release = v4l2_fh_release,
  1021. };
  1022. static int cobalt_node_register(struct cobalt *cobalt, int node)
  1023. {
  1024. static const struct v4l2_dv_timings dv1080p60 =
  1025. V4L2_DV_BT_CEA_1920X1080P60;
  1026. struct cobalt_stream *s = cobalt->streams + node;
  1027. struct video_device *vdev = &s->vdev;
  1028. struct vb2_queue *q = &s->q;
  1029. int ret;
  1030. mutex_init(&s->lock);
  1031. spin_lock_init(&s->irqlock);
  1032. snprintf(vdev->name, sizeof(vdev->name),
  1033. "%s-%d", cobalt->v4l2_dev.name, node);
  1034. s->width = 1920;
  1035. /* Audio frames are just 4 lines of 1920 bytes */
  1036. s->height = s->is_audio ? 4 : 1080;
  1037. if (s->is_audio) {
  1038. s->bpp = 1;
  1039. s->pixfmt = V4L2_PIX_FMT_GREY;
  1040. } else if (s->is_output) {
  1041. s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
  1042. s->pixfmt = V4L2_PIX_FMT_BGR32;
  1043. } else {
  1044. s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
  1045. s->pixfmt = V4L2_PIX_FMT_YUYV;
  1046. }
  1047. s->colorspace = V4L2_COLORSPACE_SRGB;
  1048. s->stride = s->width * s->bpp;
  1049. if (!s->is_audio) {
  1050. if (s->is_dummy)
  1051. cobalt_warn("Setting up dummy video node %d\n", node);
  1052. vdev->v4l2_dev = &cobalt->v4l2_dev;
  1053. if (s->is_dummy)
  1054. vdev->fops = &cobalt_empty_fops;
  1055. else
  1056. vdev->fops = s->is_output ? &cobalt_out_fops :
  1057. &cobalt_fops;
  1058. vdev->release = video_device_release_empty;
  1059. vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX;
  1060. vdev->lock = &s->lock;
  1061. if (s->sd)
  1062. vdev->ctrl_handler = s->sd->ctrl_handler;
  1063. s->timings = dv1080p60;
  1064. v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings);
  1065. if (!s->is_output && s->sd)
  1066. cobalt_enable_input(s);
  1067. vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops :
  1068. &cobalt_ioctl_ops;
  1069. }
  1070. INIT_LIST_HEAD(&s->bufs);
  1071. q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
  1072. V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1073. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1074. q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ;
  1075. q->drv_priv = s;
  1076. q->buf_struct_size = sizeof(struct cobalt_buffer);
  1077. q->ops = &cobalt_qops;
  1078. q->mem_ops = &vb2_dma_sg_memops;
  1079. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1080. q->min_buffers_needed = 2;
  1081. q->lock = &s->lock;
  1082. vdev->queue = q;
  1083. video_set_drvdata(vdev, s);
  1084. ret = vb2_queue_init(q);
  1085. if (!s->is_audio && ret == 0)
  1086. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1087. else if (!s->is_dummy)
  1088. ret = cobalt_alsa_init(s);
  1089. if (ret < 0) {
  1090. if (!s->is_audio)
  1091. cobalt_err("couldn't register v4l2 device node %d\n",
  1092. node);
  1093. return ret;
  1094. }
  1095. cobalt_info("registered node %d\n", node);
  1096. return 0;
  1097. }
  1098. /* Initialize v4l2 variables and register v4l2 devices */
  1099. int cobalt_nodes_register(struct cobalt *cobalt)
  1100. {
  1101. int node, ret;
  1102. /* Setup V4L2 Devices */
  1103. for (node = 0; node < COBALT_NUM_STREAMS; node++) {
  1104. ret = cobalt_node_register(cobalt, node);
  1105. if (ret)
  1106. return ret;
  1107. }
  1108. return 0;
  1109. }
  1110. /* Unregister v4l2 devices */
  1111. void cobalt_nodes_unregister(struct cobalt *cobalt)
  1112. {
  1113. int node;
  1114. /* Teardown all streams */
  1115. for (node = 0; node < COBALT_NUM_STREAMS; node++) {
  1116. struct cobalt_stream *s = cobalt->streams + node;
  1117. struct video_device *vdev = &s->vdev;
  1118. if (!s->is_audio)
  1119. video_unregister_device(vdev);
  1120. else if (!s->is_dummy)
  1121. cobalt_alsa_exit(s);
  1122. }
  1123. }