vsp1_video.c 35 KB

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