vsp1_video.c 28 KB

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