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