vsp1_video.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. * vsp1_video.c -- R-Car VSP1 Video Node
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/v4l2-mediabus.h>
  19. #include <linux/videodev2.h>
  20. #include <media/media-entity.h>
  21. #include <media/v4l2-dev.h>
  22. #include <media/v4l2-fh.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-subdev.h>
  25. #include <media/videobuf2-core.h>
  26. #include <media/videobuf2-dma-contig.h>
  27. #include "vsp1.h"
  28. #include "vsp1_bru.h"
  29. #include "vsp1_entity.h"
  30. #include "vsp1_rwpf.h"
  31. #include "vsp1_video.h"
  32. #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
  33. #define VSP1_VIDEO_DEF_WIDTH 1024
  34. #define VSP1_VIDEO_DEF_HEIGHT 768
  35. #define VSP1_VIDEO_MIN_WIDTH 2U
  36. #define VSP1_VIDEO_MAX_WIDTH 8190U
  37. #define VSP1_VIDEO_MIN_HEIGHT 2U
  38. #define VSP1_VIDEO_MAX_HEIGHT 8190U
  39. /* -----------------------------------------------------------------------------
  40. * Helper functions
  41. */
  42. static const struct vsp1_format_info vsp1_video_formats[] = {
  43. { V4L2_PIX_FMT_RGB332, V4L2_MBUS_FMT_ARGB8888_1X32,
  44. VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  45. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  46. 1, { 8, 0, 0 }, false, false, 1, 1 },
  47. { V4L2_PIX_FMT_RGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
  48. VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  49. VI6_RPF_DSWAP_P_WDS,
  50. 1, { 16, 0, 0 }, false, false, 1, 1 },
  51. { V4L2_PIX_FMT_RGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
  52. VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  53. VI6_RPF_DSWAP_P_WDS,
  54. 1, { 16, 0, 0 }, false, false, 1, 1 },
  55. { V4L2_PIX_FMT_RGB565, V4L2_MBUS_FMT_ARGB8888_1X32,
  56. VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  57. VI6_RPF_DSWAP_P_WDS,
  58. 1, { 16, 0, 0 }, false, false, 1, 1 },
  59. { V4L2_PIX_FMT_BGR24, V4L2_MBUS_FMT_ARGB8888_1X32,
  60. VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  61. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  62. 1, { 24, 0, 0 }, false, false, 1, 1 },
  63. { V4L2_PIX_FMT_RGB24, V4L2_MBUS_FMT_ARGB8888_1X32,
  64. VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  65. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  66. 1, { 24, 0, 0 }, false, false, 1, 1 },
  67. { V4L2_PIX_FMT_BGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
  68. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
  69. 1, { 32, 0, 0 }, false, false, 1, 1 },
  70. { V4L2_PIX_FMT_RGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
  71. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  72. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  73. 1, { 32, 0, 0 }, false, false, 1, 1 },
  74. { V4L2_PIX_FMT_UYVY, V4L2_MBUS_FMT_AYUV8_1X32,
  75. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  76. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  77. 1, { 16, 0, 0 }, false, false, 2, 1 },
  78. { V4L2_PIX_FMT_VYUY, V4L2_MBUS_FMT_AYUV8_1X32,
  79. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  80. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  81. 1, { 16, 0, 0 }, false, true, 2, 1 },
  82. { V4L2_PIX_FMT_YUYV, V4L2_MBUS_FMT_AYUV8_1X32,
  83. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  84. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  85. 1, { 16, 0, 0 }, true, false, 2, 1 },
  86. { V4L2_PIX_FMT_YVYU, V4L2_MBUS_FMT_AYUV8_1X32,
  87. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  88. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  89. 1, { 16, 0, 0 }, true, true, 2, 1 },
  90. { V4L2_PIX_FMT_NV12M, V4L2_MBUS_FMT_AYUV8_1X32,
  91. VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  92. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  93. 2, { 8, 16, 0 }, false, false, 2, 2 },
  94. { V4L2_PIX_FMT_NV21M, V4L2_MBUS_FMT_AYUV8_1X32,
  95. VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  96. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  97. 2, { 8, 16, 0 }, false, true, 2, 2 },
  98. { V4L2_PIX_FMT_NV16M, V4L2_MBUS_FMT_AYUV8_1X32,
  99. VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  100. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  101. 2, { 8, 16, 0 }, false, false, 2, 1 },
  102. { V4L2_PIX_FMT_NV61M, V4L2_MBUS_FMT_AYUV8_1X32,
  103. VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  104. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  105. 2, { 8, 16, 0 }, false, true, 2, 1 },
  106. { V4L2_PIX_FMT_YUV420M, V4L2_MBUS_FMT_AYUV8_1X32,
  107. VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  108. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  109. 3, { 8, 8, 8 }, false, false, 2, 2 },
  110. };
  111. /*
  112. * vsp1_get_format_info - Retrieve format information for a 4CC
  113. * @fourcc: the format 4CC
  114. *
  115. * Return a pointer to the format information structure corresponding to the
  116. * given V4L2 format 4CC, or NULL if no corresponding format can be found.
  117. */
  118. static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
  119. {
  120. unsigned int i;
  121. for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
  122. const struct vsp1_format_info *info = &vsp1_video_formats[i];
  123. if (info->fourcc == fourcc)
  124. return info;
  125. }
  126. return NULL;
  127. }
  128. static struct v4l2_subdev *
  129. vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
  130. {
  131. struct media_pad *remote;
  132. remote = media_entity_remote_pad(local);
  133. if (remote == NULL ||
  134. media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  135. return NULL;
  136. if (pad)
  137. *pad = remote->index;
  138. return media_entity_to_v4l2_subdev(remote->entity);
  139. }
  140. static int vsp1_video_verify_format(struct vsp1_video *video)
  141. {
  142. struct v4l2_subdev_format fmt;
  143. struct v4l2_subdev *subdev;
  144. int ret;
  145. subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
  146. if (subdev == NULL)
  147. return -EINVAL;
  148. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  149. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  150. if (ret < 0)
  151. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  152. if (video->fmtinfo->mbus != fmt.format.code ||
  153. video->format.height != fmt.format.height ||
  154. video->format.width != fmt.format.width)
  155. return -EINVAL;
  156. return 0;
  157. }
  158. static int __vsp1_video_try_format(struct vsp1_video *video,
  159. struct v4l2_pix_format_mplane *pix,
  160. const struct vsp1_format_info **fmtinfo)
  161. {
  162. const struct vsp1_format_info *info;
  163. unsigned int width = pix->width;
  164. unsigned int height = pix->height;
  165. unsigned int i;
  166. /* Retrieve format information and select the default format if the
  167. * requested format isn't supported.
  168. */
  169. info = vsp1_get_format_info(pix->pixelformat);
  170. if (info == NULL)
  171. info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
  172. pix->pixelformat = info->fourcc;
  173. pix->colorspace = V4L2_COLORSPACE_SRGB;
  174. pix->field = V4L2_FIELD_NONE;
  175. memset(pix->reserved, 0, sizeof(pix->reserved));
  176. /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
  177. width = round_down(width, info->hsub);
  178. height = round_down(height, info->vsub);
  179. /* Clamp the width and height. */
  180. pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
  181. pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
  182. VSP1_VIDEO_MAX_HEIGHT);
  183. /* Compute and clamp the stride and image size. While not documented in
  184. * the datasheet, strides not aligned to a multiple of 128 bytes result
  185. * in image corruption.
  186. */
  187. for (i = 0; i < max(info->planes, 2U); ++i) {
  188. unsigned int hsub = i > 0 ? info->hsub : 1;
  189. unsigned int vsub = i > 0 ? info->vsub : 1;
  190. unsigned int align = 128;
  191. unsigned int bpl;
  192. bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
  193. pix->width / hsub * info->bpp[i] / 8,
  194. round_down(65535U, align));
  195. pix->plane_fmt[i].bytesperline = round_up(bpl, align);
  196. pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
  197. * pix->height / vsub;
  198. }
  199. if (info->planes == 3) {
  200. /* The second and third planes must have the same stride. */
  201. pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
  202. pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
  203. }
  204. pix->num_planes = info->planes;
  205. if (fmtinfo)
  206. *fmtinfo = info;
  207. return 0;
  208. }
  209. static bool
  210. vsp1_video_format_adjust(struct vsp1_video *video,
  211. const struct v4l2_pix_format_mplane *format,
  212. struct v4l2_pix_format_mplane *adjust)
  213. {
  214. unsigned int i;
  215. *adjust = *format;
  216. __vsp1_video_try_format(video, adjust, NULL);
  217. if (format->width != adjust->width ||
  218. format->height != adjust->height ||
  219. format->pixelformat != adjust->pixelformat ||
  220. format->num_planes != adjust->num_planes)
  221. return false;
  222. for (i = 0; i < format->num_planes; ++i) {
  223. if (format->plane_fmt[i].bytesperline !=
  224. adjust->plane_fmt[i].bytesperline)
  225. return false;
  226. adjust->plane_fmt[i].sizeimage =
  227. max(adjust->plane_fmt[i].sizeimage,
  228. format->plane_fmt[i].sizeimage);
  229. }
  230. return true;
  231. }
  232. /* -----------------------------------------------------------------------------
  233. * Pipeline Management
  234. */
  235. static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
  236. struct vsp1_rwpf *output)
  237. {
  238. struct vsp1_entity *entity;
  239. unsigned int entities = 0;
  240. struct media_pad *pad;
  241. bool uds_found = false;
  242. input->location.left = 0;
  243. input->location.top = 0;
  244. pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
  245. while (1) {
  246. if (pad == NULL)
  247. return -EPIPE;
  248. /* We've reached a video node, that shouldn't have happened. */
  249. if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  250. return -EPIPE;
  251. entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
  252. /* A BRU is present in the pipeline, store the compose rectangle
  253. * location in the input RPF for use when configuring the RPF.
  254. */
  255. if (entity->type == VSP1_ENTITY_BRU) {
  256. struct vsp1_bru *bru = to_bru(&entity->subdev);
  257. struct v4l2_rect *rect = &bru->compose[pad->index];
  258. input->location.left = rect->left;
  259. input->location.top = rect->top;
  260. }
  261. /* We've reached the WPF, we're done. */
  262. if (entity->type == VSP1_ENTITY_WPF)
  263. break;
  264. /* Ensure the branch has no loop. */
  265. if (entities & (1 << entity->subdev.entity.id))
  266. return -EPIPE;
  267. entities |= 1 << entity->subdev.entity.id;
  268. /* UDS can't be chained. */
  269. if (entity->type == VSP1_ENTITY_UDS) {
  270. if (uds_found)
  271. return -EPIPE;
  272. uds_found = true;
  273. }
  274. /* Follow the source link. The link setup operations ensure
  275. * that the output fan-out can't be more than one, there is thus
  276. * no need to verify here that only a single source link is
  277. * activated.
  278. */
  279. pad = &entity->pads[entity->source_pad];
  280. pad = media_entity_remote_pad(pad);
  281. }
  282. /* The last entity must be the output WPF. */
  283. if (entity != &output->entity)
  284. return -EPIPE;
  285. return 0;
  286. }
  287. static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
  288. struct vsp1_video *video)
  289. {
  290. struct media_entity_graph graph;
  291. struct media_entity *entity = &video->video.entity;
  292. struct media_device *mdev = entity->parent;
  293. unsigned int i;
  294. int ret;
  295. mutex_lock(&mdev->graph_mutex);
  296. /* Walk the graph to locate the entities and video nodes. */
  297. media_entity_graph_walk_start(&graph, entity);
  298. while ((entity = media_entity_graph_walk_next(&graph))) {
  299. struct v4l2_subdev *subdev;
  300. struct vsp1_rwpf *rwpf;
  301. struct vsp1_entity *e;
  302. if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) {
  303. pipe->num_video++;
  304. continue;
  305. }
  306. subdev = media_entity_to_v4l2_subdev(entity);
  307. e = to_vsp1_entity(subdev);
  308. list_add_tail(&e->list_pipe, &pipe->entities);
  309. if (e->type == VSP1_ENTITY_RPF) {
  310. rwpf = to_rwpf(subdev);
  311. pipe->inputs[pipe->num_inputs++] = rwpf;
  312. rwpf->video.pipe_index = pipe->num_inputs;
  313. } else if (e->type == VSP1_ENTITY_WPF) {
  314. rwpf = to_rwpf(subdev);
  315. pipe->output = to_rwpf(subdev);
  316. rwpf->video.pipe_index = 0;
  317. } else if (e->type == VSP1_ENTITY_LIF) {
  318. pipe->lif = e;
  319. } else if (e->type == VSP1_ENTITY_BRU) {
  320. pipe->bru = e;
  321. }
  322. }
  323. mutex_unlock(&mdev->graph_mutex);
  324. /* We need one output and at least one input. */
  325. if (pipe->num_inputs == 0 || !pipe->output) {
  326. ret = -EPIPE;
  327. goto error;
  328. }
  329. /* Follow links downstream for each input and make sure the graph
  330. * contains no loop and that all branches end at the output WPF.
  331. */
  332. for (i = 0; i < pipe->num_inputs; ++i) {
  333. ret = vsp1_pipeline_validate_branch(pipe->inputs[i],
  334. pipe->output);
  335. if (ret < 0)
  336. goto error;
  337. }
  338. return 0;
  339. error:
  340. INIT_LIST_HEAD(&pipe->entities);
  341. pipe->buffers_ready = 0;
  342. pipe->num_video = 0;
  343. pipe->num_inputs = 0;
  344. pipe->output = NULL;
  345. pipe->bru = NULL;
  346. pipe->lif = NULL;
  347. return ret;
  348. }
  349. static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
  350. struct vsp1_video *video)
  351. {
  352. int ret;
  353. mutex_lock(&pipe->lock);
  354. /* If we're the first user validate and initialize the pipeline. */
  355. if (pipe->use_count == 0) {
  356. ret = vsp1_pipeline_validate(pipe, video);
  357. if (ret < 0)
  358. goto done;
  359. }
  360. pipe->use_count++;
  361. ret = 0;
  362. done:
  363. mutex_unlock(&pipe->lock);
  364. return ret;
  365. }
  366. static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
  367. {
  368. mutex_lock(&pipe->lock);
  369. /* If we're the last user clean up the pipeline. */
  370. if (--pipe->use_count == 0) {
  371. INIT_LIST_HEAD(&pipe->entities);
  372. pipe->state = VSP1_PIPELINE_STOPPED;
  373. pipe->buffers_ready = 0;
  374. pipe->num_video = 0;
  375. pipe->num_inputs = 0;
  376. pipe->output = NULL;
  377. pipe->bru = NULL;
  378. pipe->lif = NULL;
  379. }
  380. mutex_unlock(&pipe->lock);
  381. }
  382. static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
  383. {
  384. struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
  385. vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
  386. pipe->state = VSP1_PIPELINE_RUNNING;
  387. pipe->buffers_ready = 0;
  388. }
  389. static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
  390. {
  391. struct vsp1_entity *entity;
  392. unsigned long flags;
  393. int ret;
  394. spin_lock_irqsave(&pipe->irqlock, flags);
  395. pipe->state = VSP1_PIPELINE_STOPPING;
  396. spin_unlock_irqrestore(&pipe->irqlock, flags);
  397. ret = wait_event_timeout(pipe->wq, pipe->state == VSP1_PIPELINE_STOPPED,
  398. msecs_to_jiffies(500));
  399. ret = ret == 0 ? -ETIMEDOUT : 0;
  400. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  401. if (entity->route)
  402. vsp1_write(entity->vsp1, entity->route->reg,
  403. VI6_DPR_NODE_UNUSED);
  404. v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
  405. }
  406. return ret;
  407. }
  408. static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
  409. {
  410. unsigned int mask;
  411. mask = ((1 << pipe->num_inputs) - 1) << 1;
  412. if (!pipe->lif)
  413. mask |= 1 << 0;
  414. return pipe->buffers_ready == mask;
  415. }
  416. /*
  417. * vsp1_video_complete_buffer - Complete the current buffer
  418. * @video: the video node
  419. *
  420. * This function completes the current buffer by filling its sequence number,
  421. * time stamp and payload size, and hands it back to the videobuf core.
  422. *
  423. * When operating in DU output mode (deep pipeline to the DU through the LIF),
  424. * the VSP1 needs to constantly supply frames to the display. In that case, if
  425. * no other buffer is queued, reuse the one that has just been processed instead
  426. * of handing it back to the videobuf core.
  427. *
  428. * Return the next queued buffer or NULL if the queue is empty.
  429. */
  430. static struct vsp1_video_buffer *
  431. vsp1_video_complete_buffer(struct vsp1_video *video)
  432. {
  433. struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
  434. struct vsp1_video_buffer *next = NULL;
  435. struct vsp1_video_buffer *done;
  436. unsigned long flags;
  437. unsigned int i;
  438. spin_lock_irqsave(&video->irqlock, flags);
  439. if (list_empty(&video->irqqueue)) {
  440. spin_unlock_irqrestore(&video->irqlock, flags);
  441. return NULL;
  442. }
  443. done = list_first_entry(&video->irqqueue,
  444. struct vsp1_video_buffer, queue);
  445. /* In DU output mode reuse the buffer if the list is singular. */
  446. if (pipe->lif && list_is_singular(&video->irqqueue)) {
  447. spin_unlock_irqrestore(&video->irqlock, flags);
  448. return done;
  449. }
  450. list_del(&done->queue);
  451. if (!list_empty(&video->irqqueue))
  452. next = list_first_entry(&video->irqqueue,
  453. struct vsp1_video_buffer, queue);
  454. spin_unlock_irqrestore(&video->irqlock, flags);
  455. done->buf.v4l2_buf.sequence = video->sequence++;
  456. v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp);
  457. for (i = 0; i < done->buf.num_planes; ++i)
  458. vb2_set_plane_payload(&done->buf, i, done->length[i]);
  459. vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE);
  460. return next;
  461. }
  462. static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
  463. struct vsp1_video *video)
  464. {
  465. struct vsp1_video_buffer *buf;
  466. unsigned long flags;
  467. buf = vsp1_video_complete_buffer(video);
  468. if (buf == NULL)
  469. return;
  470. spin_lock_irqsave(&pipe->irqlock, flags);
  471. video->ops->queue(video, buf);
  472. pipe->buffers_ready |= 1 << video->pipe_index;
  473. spin_unlock_irqrestore(&pipe->irqlock, flags);
  474. }
  475. void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
  476. {
  477. unsigned long flags;
  478. unsigned int i;
  479. if (pipe == NULL)
  480. return;
  481. /* Complete buffers on all video nodes. */
  482. for (i = 0; i < pipe->num_inputs; ++i)
  483. vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
  484. if (!pipe->lif)
  485. vsp1_video_frame_end(pipe, &pipe->output->video);
  486. spin_lock_irqsave(&pipe->irqlock, flags);
  487. /* If a stop has been requested, mark the pipeline as stopped and
  488. * return.
  489. */
  490. if (pipe->state == VSP1_PIPELINE_STOPPING) {
  491. pipe->state = VSP1_PIPELINE_STOPPED;
  492. wake_up(&pipe->wq);
  493. goto done;
  494. }
  495. /* Restart the pipeline if ready. */
  496. if (vsp1_pipeline_ready(pipe))
  497. vsp1_pipeline_run(pipe);
  498. done:
  499. spin_unlock_irqrestore(&pipe->irqlock, flags);
  500. }
  501. /* -----------------------------------------------------------------------------
  502. * videobuf2 Queue Operations
  503. */
  504. static int
  505. vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  506. unsigned int *nbuffers, unsigned int *nplanes,
  507. unsigned int sizes[], void *alloc_ctxs[])
  508. {
  509. struct vsp1_video *video = vb2_get_drv_priv(vq);
  510. const struct v4l2_pix_format_mplane *format;
  511. struct v4l2_pix_format_mplane pix_mp;
  512. unsigned int i;
  513. if (fmt) {
  514. /* Make sure the format is valid and adjust the sizeimage field
  515. * if needed.
  516. */
  517. if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp))
  518. return -EINVAL;
  519. format = &pix_mp;
  520. } else {
  521. format = &video->format;
  522. }
  523. *nplanes = format->num_planes;
  524. for (i = 0; i < format->num_planes; ++i) {
  525. sizes[i] = format->plane_fmt[i].sizeimage;
  526. alloc_ctxs[i] = video->alloc_ctx;
  527. }
  528. return 0;
  529. }
  530. static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
  531. {
  532. struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
  533. struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
  534. const struct v4l2_pix_format_mplane *format = &video->format;
  535. unsigned int i;
  536. if (vb->num_planes < format->num_planes)
  537. return -EINVAL;
  538. buf->video = video;
  539. for (i = 0; i < vb->num_planes; ++i) {
  540. buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
  541. buf->length[i] = vb2_plane_size(vb, i);
  542. if (buf->length[i] < format->plane_fmt[i].sizeimage)
  543. return -EINVAL;
  544. }
  545. return 0;
  546. }
  547. static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
  548. {
  549. struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
  550. struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
  551. struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
  552. unsigned long flags;
  553. bool empty;
  554. spin_lock_irqsave(&video->irqlock, flags);
  555. empty = list_empty(&video->irqqueue);
  556. list_add_tail(&buf->queue, &video->irqqueue);
  557. spin_unlock_irqrestore(&video->irqlock, flags);
  558. if (!empty)
  559. return;
  560. spin_lock_irqsave(&pipe->irqlock, flags);
  561. video->ops->queue(video, buf);
  562. pipe->buffers_ready |= 1 << video->pipe_index;
  563. if (vb2_is_streaming(&video->queue) &&
  564. vsp1_pipeline_ready(pipe))
  565. vsp1_pipeline_run(pipe);
  566. spin_unlock_irqrestore(&pipe->irqlock, flags);
  567. }
  568. static void vsp1_entity_route_setup(struct vsp1_entity *source)
  569. {
  570. struct vsp1_entity *sink;
  571. if (source->route->reg == 0)
  572. return;
  573. sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
  574. vsp1_write(source->vsp1, source->route->reg,
  575. sink->route->inputs[source->sink_pad]);
  576. }
  577. static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
  578. {
  579. struct vsp1_video *video = vb2_get_drv_priv(vq);
  580. struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
  581. struct vsp1_entity *entity;
  582. unsigned long flags;
  583. int ret;
  584. mutex_lock(&pipe->lock);
  585. if (pipe->stream_count == pipe->num_video - 1) {
  586. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  587. vsp1_entity_route_setup(entity);
  588. ret = v4l2_subdev_call(&entity->subdev, video,
  589. s_stream, 1);
  590. if (ret < 0) {
  591. mutex_unlock(&pipe->lock);
  592. return ret;
  593. }
  594. }
  595. }
  596. pipe->stream_count++;
  597. mutex_unlock(&pipe->lock);
  598. spin_lock_irqsave(&pipe->irqlock, flags);
  599. if (vsp1_pipeline_ready(pipe))
  600. vsp1_pipeline_run(pipe);
  601. spin_unlock_irqrestore(&pipe->irqlock, flags);
  602. return 0;
  603. }
  604. static void vsp1_video_stop_streaming(struct vb2_queue *vq)
  605. {
  606. struct vsp1_video *video = vb2_get_drv_priv(vq);
  607. struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
  608. unsigned long flags;
  609. int ret;
  610. mutex_lock(&pipe->lock);
  611. if (--pipe->stream_count == 0) {
  612. /* Stop the pipeline. */
  613. ret = vsp1_pipeline_stop(pipe);
  614. if (ret == -ETIMEDOUT)
  615. dev_err(video->vsp1->dev, "pipeline stop timeout\n");
  616. }
  617. mutex_unlock(&pipe->lock);
  618. vsp1_pipeline_cleanup(pipe);
  619. media_entity_pipeline_stop(&video->video.entity);
  620. /* Remove all buffers from the IRQ queue. */
  621. spin_lock_irqsave(&video->irqlock, flags);
  622. INIT_LIST_HEAD(&video->irqqueue);
  623. spin_unlock_irqrestore(&video->irqlock, flags);
  624. }
  625. static struct vb2_ops vsp1_video_queue_qops = {
  626. .queue_setup = vsp1_video_queue_setup,
  627. .buf_prepare = vsp1_video_buffer_prepare,
  628. .buf_queue = vsp1_video_buffer_queue,
  629. .wait_prepare = vb2_ops_wait_prepare,
  630. .wait_finish = vb2_ops_wait_finish,
  631. .start_streaming = vsp1_video_start_streaming,
  632. .stop_streaming = vsp1_video_stop_streaming,
  633. };
  634. /* -----------------------------------------------------------------------------
  635. * V4L2 ioctls
  636. */
  637. static int
  638. vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  639. {
  640. struct v4l2_fh *vfh = file->private_data;
  641. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  642. cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
  643. | V4L2_CAP_VIDEO_CAPTURE_MPLANE
  644. | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  645. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  646. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
  647. | V4L2_CAP_STREAMING;
  648. else
  649. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
  650. | V4L2_CAP_STREAMING;
  651. strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
  652. strlcpy(cap->card, video->video.name, sizeof(cap->card));
  653. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  654. dev_name(video->vsp1->dev));
  655. return 0;
  656. }
  657. static int
  658. vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
  659. {
  660. struct v4l2_fh *vfh = file->private_data;
  661. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  662. if (format->type != video->queue.type)
  663. return -EINVAL;
  664. mutex_lock(&video->lock);
  665. format->fmt.pix_mp = video->format;
  666. mutex_unlock(&video->lock);
  667. return 0;
  668. }
  669. static int
  670. vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
  671. {
  672. struct v4l2_fh *vfh = file->private_data;
  673. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  674. if (format->type != video->queue.type)
  675. return -EINVAL;
  676. return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
  677. }
  678. static int
  679. vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
  680. {
  681. struct v4l2_fh *vfh = file->private_data;
  682. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  683. const struct vsp1_format_info *info;
  684. int ret;
  685. if (format->type != video->queue.type)
  686. return -EINVAL;
  687. ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
  688. if (ret < 0)
  689. return ret;
  690. mutex_lock(&video->lock);
  691. if (vb2_is_busy(&video->queue)) {
  692. ret = -EBUSY;
  693. goto done;
  694. }
  695. video->format = format->fmt.pix_mp;
  696. video->fmtinfo = info;
  697. done:
  698. mutex_unlock(&video->lock);
  699. return ret;
  700. }
  701. static int
  702. vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  703. {
  704. struct v4l2_fh *vfh = file->private_data;
  705. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  706. struct vsp1_pipeline *pipe;
  707. int ret;
  708. if (video->queue.owner && video->queue.owner != file->private_data)
  709. return -EBUSY;
  710. video->sequence = 0;
  711. /* Start streaming on the pipeline. No link touching an entity in the
  712. * pipeline can be activated or deactivated once streaming is started.
  713. *
  714. * Use the VSP1 pipeline object embedded in the first video object that
  715. * starts streaming.
  716. */
  717. pipe = video->video.entity.pipe
  718. ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
  719. ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
  720. if (ret < 0)
  721. return ret;
  722. /* Verify that the configured format matches the output of the connected
  723. * subdev.
  724. */
  725. ret = vsp1_video_verify_format(video);
  726. if (ret < 0)
  727. goto err_stop;
  728. ret = vsp1_pipeline_init(pipe, video);
  729. if (ret < 0)
  730. goto err_stop;
  731. /* Start the queue. */
  732. ret = vb2_streamon(&video->queue, type);
  733. if (ret < 0)
  734. goto err_cleanup;
  735. return 0;
  736. err_cleanup:
  737. vsp1_pipeline_cleanup(pipe);
  738. err_stop:
  739. media_entity_pipeline_stop(&video->video.entity);
  740. return ret;
  741. }
  742. static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
  743. .vidioc_querycap = vsp1_video_querycap,
  744. .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
  745. .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
  746. .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
  747. .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
  748. .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
  749. .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
  750. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  751. .vidioc_querybuf = vb2_ioctl_querybuf,
  752. .vidioc_qbuf = vb2_ioctl_qbuf,
  753. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  754. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  755. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  756. .vidioc_streamon = vsp1_video_streamon,
  757. .vidioc_streamoff = vb2_ioctl_streamoff,
  758. };
  759. /* -----------------------------------------------------------------------------
  760. * V4L2 File Operations
  761. */
  762. static int vsp1_video_open(struct file *file)
  763. {
  764. struct vsp1_video *video = video_drvdata(file);
  765. struct v4l2_fh *vfh;
  766. int ret = 0;
  767. vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
  768. if (vfh == NULL)
  769. return -ENOMEM;
  770. v4l2_fh_init(vfh, &video->video);
  771. v4l2_fh_add(vfh);
  772. file->private_data = vfh;
  773. if (!vsp1_device_get(video->vsp1)) {
  774. ret = -EBUSY;
  775. v4l2_fh_del(vfh);
  776. kfree(vfh);
  777. }
  778. return ret;
  779. }
  780. static int vsp1_video_release(struct file *file)
  781. {
  782. struct vsp1_video *video = video_drvdata(file);
  783. struct v4l2_fh *vfh = file->private_data;
  784. mutex_lock(&video->lock);
  785. if (video->queue.owner == vfh) {
  786. vb2_queue_release(&video->queue);
  787. video->queue.owner = NULL;
  788. }
  789. mutex_unlock(&video->lock);
  790. vsp1_device_put(video->vsp1);
  791. v4l2_fh_release(file);
  792. file->private_data = NULL;
  793. return 0;
  794. }
  795. static struct v4l2_file_operations vsp1_video_fops = {
  796. .owner = THIS_MODULE,
  797. .unlocked_ioctl = video_ioctl2,
  798. .open = vsp1_video_open,
  799. .release = vsp1_video_release,
  800. .poll = vb2_fop_poll,
  801. .mmap = vb2_fop_mmap,
  802. };
  803. /* -----------------------------------------------------------------------------
  804. * Initialization and Cleanup
  805. */
  806. int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
  807. {
  808. const char *direction;
  809. int ret;
  810. switch (video->type) {
  811. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  812. direction = "output";
  813. video->pad.flags = MEDIA_PAD_FL_SINK;
  814. break;
  815. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  816. direction = "input";
  817. video->pad.flags = MEDIA_PAD_FL_SOURCE;
  818. video->video.vfl_dir = VFL_DIR_TX;
  819. break;
  820. default:
  821. return -EINVAL;
  822. }
  823. video->rwpf = rwpf;
  824. mutex_init(&video->lock);
  825. spin_lock_init(&video->irqlock);
  826. INIT_LIST_HEAD(&video->irqqueue);
  827. mutex_init(&video->pipe.lock);
  828. spin_lock_init(&video->pipe.irqlock);
  829. INIT_LIST_HEAD(&video->pipe.entities);
  830. init_waitqueue_head(&video->pipe.wq);
  831. video->pipe.state = VSP1_PIPELINE_STOPPED;
  832. /* Initialize the media entity... */
  833. ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
  834. if (ret < 0)
  835. return ret;
  836. /* ... and the format ... */
  837. video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
  838. video->format.pixelformat = video->fmtinfo->fourcc;
  839. video->format.colorspace = V4L2_COLORSPACE_SRGB;
  840. video->format.field = V4L2_FIELD_NONE;
  841. video->format.width = VSP1_VIDEO_DEF_WIDTH;
  842. video->format.height = VSP1_VIDEO_DEF_HEIGHT;
  843. video->format.num_planes = 1;
  844. video->format.plane_fmt[0].bytesperline =
  845. video->format.width * video->fmtinfo->bpp[0] / 8;
  846. video->format.plane_fmt[0].sizeimage =
  847. video->format.plane_fmt[0].bytesperline * video->format.height;
  848. /* ... and the video node... */
  849. video->video.v4l2_dev = &video->vsp1->v4l2_dev;
  850. video->video.fops = &vsp1_video_fops;
  851. snprintf(video->video.name, sizeof(video->video.name), "%s %s",
  852. rwpf->subdev.name, direction);
  853. video->video.vfl_type = VFL_TYPE_GRABBER;
  854. video->video.release = video_device_release_empty;
  855. video->video.ioctl_ops = &vsp1_video_ioctl_ops;
  856. video_set_drvdata(&video->video, video);
  857. /* ... and the buffers queue... */
  858. video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
  859. if (IS_ERR(video->alloc_ctx)) {
  860. ret = PTR_ERR(video->alloc_ctx);
  861. goto error;
  862. }
  863. video->queue.type = video->type;
  864. video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  865. video->queue.lock = &video->lock;
  866. video->queue.drv_priv = video;
  867. video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
  868. video->queue.ops = &vsp1_video_queue_qops;
  869. video->queue.mem_ops = &vb2_dma_contig_memops;
  870. video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  871. ret = vb2_queue_init(&video->queue);
  872. if (ret < 0) {
  873. dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
  874. goto error;
  875. }
  876. /* ... and register the video device. */
  877. video->video.queue = &video->queue;
  878. ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
  879. if (ret < 0) {
  880. dev_err(video->vsp1->dev, "failed to register video device\n");
  881. goto error;
  882. }
  883. return 0;
  884. error:
  885. vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
  886. vsp1_video_cleanup(video);
  887. return ret;
  888. }
  889. void vsp1_video_cleanup(struct vsp1_video *video)
  890. {
  891. if (video_is_registered(&video->video))
  892. video_unregister_device(&video->video);
  893. vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
  894. media_entity_cleanup(&video->video.entity);
  895. }