vsp1_video.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. mutex_lock(&video->lock);
  682. if (video->queue.owner && video->queue.owner != file->private_data)
  683. return -EBUSY;
  684. video->sequence = 0;
  685. /* Start streaming on the pipeline. No link touching an entity in the
  686. * pipeline can be activated or deactivated once streaming is started.
  687. *
  688. * Use the VSP1 pipeline object embedded in the first video object that
  689. * starts streaming.
  690. */
  691. pipe = video->video.entity.pipe
  692. ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
  693. ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
  694. if (ret < 0)
  695. return ret;
  696. /* Verify that the configured format matches the output of the connected
  697. * subdev.
  698. */
  699. ret = vsp1_video_verify_format(video);
  700. if (ret < 0)
  701. goto err_stop;
  702. ret = vsp1_pipeline_init(pipe, video);
  703. if (ret < 0)
  704. goto err_stop;
  705. /* Start the queue. */
  706. ret = vb2_streamon(&video->queue, type);
  707. if (ret < 0)
  708. goto err_cleanup;
  709. return 0;
  710. err_cleanup:
  711. vsp1_pipeline_cleanup(pipe);
  712. err_stop:
  713. media_entity_pipeline_stop(&video->video.entity);
  714. return ret;
  715. }
  716. static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
  717. .vidioc_querycap = vsp1_video_querycap,
  718. .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
  719. .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
  720. .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
  721. .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
  722. .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
  723. .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
  724. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  725. .vidioc_querybuf = vb2_ioctl_querybuf,
  726. .vidioc_qbuf = vb2_ioctl_qbuf,
  727. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  728. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  729. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  730. .vidioc_streamon = vsp1_video_streamon,
  731. .vidioc_streamoff = vb2_ioctl_streamoff,
  732. };
  733. /* -----------------------------------------------------------------------------
  734. * V4L2 File Operations
  735. */
  736. static int vsp1_video_open(struct file *file)
  737. {
  738. struct vsp1_video *video = video_drvdata(file);
  739. struct v4l2_fh *vfh;
  740. int ret = 0;
  741. vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
  742. if (vfh == NULL)
  743. return -ENOMEM;
  744. v4l2_fh_init(vfh, &video->video);
  745. v4l2_fh_add(vfh);
  746. file->private_data = vfh;
  747. if (!vsp1_device_get(video->vsp1)) {
  748. ret = -EBUSY;
  749. v4l2_fh_del(vfh);
  750. kfree(vfh);
  751. }
  752. return ret;
  753. }
  754. static int vsp1_video_release(struct file *file)
  755. {
  756. struct vsp1_video *video = video_drvdata(file);
  757. struct v4l2_fh *vfh = file->private_data;
  758. mutex_lock(&video->lock);
  759. if (video->queue.owner == vfh) {
  760. vb2_queue_release(&video->queue);
  761. video->queue.owner = NULL;
  762. }
  763. mutex_unlock(&video->lock);
  764. vsp1_device_put(video->vsp1);
  765. v4l2_fh_release(file);
  766. file->private_data = NULL;
  767. return 0;
  768. }
  769. static struct v4l2_file_operations vsp1_video_fops = {
  770. .owner = THIS_MODULE,
  771. .unlocked_ioctl = video_ioctl2,
  772. .open = vsp1_video_open,
  773. .release = vsp1_video_release,
  774. .poll = vb2_fop_poll,
  775. .mmap = vb2_fop_mmap,
  776. };
  777. /* -----------------------------------------------------------------------------
  778. * Initialization and Cleanup
  779. */
  780. int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
  781. {
  782. const char *direction;
  783. int ret;
  784. switch (video->type) {
  785. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  786. direction = "output";
  787. video->pad.flags = MEDIA_PAD_FL_SINK;
  788. break;
  789. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  790. direction = "input";
  791. video->pad.flags = MEDIA_PAD_FL_SOURCE;
  792. video->video.vfl_dir = VFL_DIR_TX;
  793. break;
  794. default:
  795. return -EINVAL;
  796. }
  797. video->rwpf = rwpf;
  798. mutex_init(&video->lock);
  799. spin_lock_init(&video->irqlock);
  800. INIT_LIST_HEAD(&video->irqqueue);
  801. mutex_init(&video->pipe.lock);
  802. spin_lock_init(&video->pipe.irqlock);
  803. INIT_LIST_HEAD(&video->pipe.entities);
  804. init_waitqueue_head(&video->pipe.wq);
  805. video->pipe.state = VSP1_PIPELINE_STOPPED;
  806. /* Initialize the media entity... */
  807. ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
  808. if (ret < 0)
  809. return ret;
  810. /* ... and the format ... */
  811. video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
  812. video->format.pixelformat = video->fmtinfo->fourcc;
  813. video->format.colorspace = V4L2_COLORSPACE_SRGB;
  814. video->format.field = V4L2_FIELD_NONE;
  815. video->format.width = VSP1_VIDEO_DEF_WIDTH;
  816. video->format.height = VSP1_VIDEO_DEF_HEIGHT;
  817. video->format.num_planes = 1;
  818. video->format.plane_fmt[0].bytesperline =
  819. video->format.width * video->fmtinfo->bpp[0] / 8;
  820. video->format.plane_fmt[0].sizeimage =
  821. video->format.plane_fmt[0].bytesperline * video->format.height;
  822. /* ... and the video node... */
  823. video->video.v4l2_dev = &video->vsp1->v4l2_dev;
  824. video->video.fops = &vsp1_video_fops;
  825. snprintf(video->video.name, sizeof(video->video.name), "%s %s",
  826. rwpf->subdev.name, direction);
  827. video->video.vfl_type = VFL_TYPE_GRABBER;
  828. video->video.release = video_device_release_empty;
  829. video->video.ioctl_ops = &vsp1_video_ioctl_ops;
  830. video_set_drvdata(&video->video, video);
  831. /* ... and the buffers queue... */
  832. video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
  833. if (IS_ERR(video->alloc_ctx))
  834. goto error;
  835. video->queue.type = video->type;
  836. video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  837. video->queue.lock = &video->lock;
  838. video->queue.drv_priv = video;
  839. video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
  840. video->queue.ops = &vsp1_video_queue_qops;
  841. video->queue.mem_ops = &vb2_dma_contig_memops;
  842. video->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  843. ret = vb2_queue_init(&video->queue);
  844. if (ret < 0) {
  845. dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
  846. goto error;
  847. }
  848. /* ... and register the video device. */
  849. video->video.queue = &video->queue;
  850. ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
  851. if (ret < 0) {
  852. dev_err(video->vsp1->dev, "failed to register video device\n");
  853. goto error;
  854. }
  855. return 0;
  856. error:
  857. vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
  858. vsp1_video_cleanup(video);
  859. return ret;
  860. }
  861. void vsp1_video_cleanup(struct vsp1_video *video)
  862. {
  863. if (video_is_registered(&video->video))
  864. video_unregister_device(&video->video);
  865. vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
  866. media_entity_cleanup(&video->video.entity);
  867. }