ispvideo.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. * ispvideo.c
  3. *
  4. * TI OMAP3 ISP - Generic video node
  5. *
  6. * Copyright (C) 2009-2010 Nokia Corporation
  7. *
  8. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  9. * Sakari Ailus <sakari.ailus@iki.fi>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. */
  25. #include <asm/cacheflush.h>
  26. #include <linux/clk.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/vmalloc.h>
  34. #include <media/v4l2-dev.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/videobuf2-dma-contig.h>
  37. #include "ispvideo.h"
  38. #include "isp.h"
  39. /* -----------------------------------------------------------------------------
  40. * Helper functions
  41. */
  42. /*
  43. * NOTE: When adding new media bus codes, always remember to add
  44. * corresponding in-memory formats to the table below!!!
  45. */
  46. static struct isp_format_info formats[] = {
  47. { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
  48. V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
  49. V4L2_PIX_FMT_GREY, 8, 1, },
  50. { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
  51. V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
  52. V4L2_PIX_FMT_Y10, 10, 2, },
  53. { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
  54. V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
  55. V4L2_PIX_FMT_Y12, 12, 2, },
  56. { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
  57. V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
  58. V4L2_PIX_FMT_SBGGR8, 8, 1, },
  59. { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
  60. V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
  61. V4L2_PIX_FMT_SGBRG8, 8, 1, },
  62. { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
  63. V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
  64. V4L2_PIX_FMT_SGRBG8, 8, 1, },
  65. { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
  66. V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
  67. V4L2_PIX_FMT_SRGGB8, 8, 1, },
  68. { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
  69. V4L2_MBUS_FMT_SBGGR10_1X10, 0,
  70. V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
  71. { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
  72. V4L2_MBUS_FMT_SGBRG10_1X10, 0,
  73. V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
  74. { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
  75. V4L2_MBUS_FMT_SGRBG10_1X10, 0,
  76. V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
  77. { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
  78. V4L2_MBUS_FMT_SRGGB10_1X10, 0,
  79. V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
  80. { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
  81. V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
  82. V4L2_PIX_FMT_SBGGR10, 10, 2, },
  83. { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
  84. V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
  85. V4L2_PIX_FMT_SGBRG10, 10, 2, },
  86. { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
  87. V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
  88. V4L2_PIX_FMT_SGRBG10, 10, 2, },
  89. { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
  90. V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
  91. V4L2_PIX_FMT_SRGGB10, 10, 2, },
  92. { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
  93. V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
  94. V4L2_PIX_FMT_SBGGR12, 12, 2, },
  95. { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
  96. V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
  97. V4L2_PIX_FMT_SGBRG12, 12, 2, },
  98. { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
  99. V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
  100. V4L2_PIX_FMT_SGRBG12, 12, 2, },
  101. { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
  102. V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
  103. V4L2_PIX_FMT_SRGGB12, 12, 2, },
  104. { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
  105. V4L2_MBUS_FMT_UYVY8_1X16, 0,
  106. V4L2_PIX_FMT_UYVY, 16, 2, },
  107. { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
  108. V4L2_MBUS_FMT_YUYV8_1X16, 0,
  109. V4L2_PIX_FMT_YUYV, 16, 2, },
  110. { V4L2_MBUS_FMT_UYVY8_2X8, V4L2_MBUS_FMT_UYVY8_2X8,
  111. V4L2_MBUS_FMT_UYVY8_2X8, 0,
  112. V4L2_PIX_FMT_UYVY, 8, 2, },
  113. { V4L2_MBUS_FMT_YUYV8_2X8, V4L2_MBUS_FMT_YUYV8_2X8,
  114. V4L2_MBUS_FMT_YUYV8_2X8, 0,
  115. V4L2_PIX_FMT_YUYV, 8, 2, },
  116. /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
  117. * module and avoid NULL pointer dereferences.
  118. */
  119. { 0, }
  120. };
  121. const struct isp_format_info *
  122. omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  126. if (formats[i].code == code)
  127. return &formats[i];
  128. }
  129. return NULL;
  130. }
  131. /*
  132. * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
  133. * @video: ISP video instance
  134. * @mbus: v4l2_mbus_framefmt format (input)
  135. * @pix: v4l2_pix_format format (output)
  136. *
  137. * Fill the output pix structure with information from the input mbus format.
  138. * The bytesperline and sizeimage fields are computed from the requested bytes
  139. * per line value in the pix format and information from the video instance.
  140. *
  141. * Return the number of padding bytes at end of line.
  142. */
  143. static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
  144. const struct v4l2_mbus_framefmt *mbus,
  145. struct v4l2_pix_format *pix)
  146. {
  147. unsigned int bpl = pix->bytesperline;
  148. unsigned int min_bpl;
  149. unsigned int i;
  150. memset(pix, 0, sizeof(*pix));
  151. pix->width = mbus->width;
  152. pix->height = mbus->height;
  153. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  154. if (formats[i].code == mbus->code)
  155. break;
  156. }
  157. if (WARN_ON(i == ARRAY_SIZE(formats)))
  158. return 0;
  159. min_bpl = pix->width * formats[i].bpp;
  160. /* Clamp the requested bytes per line value. If the maximum bytes per
  161. * line value is zero, the module doesn't support user configurable line
  162. * sizes. Override the requested value with the minimum in that case.
  163. */
  164. if (video->bpl_max)
  165. bpl = clamp(bpl, min_bpl, video->bpl_max);
  166. else
  167. bpl = min_bpl;
  168. if (!video->bpl_zero_padding || bpl != min_bpl)
  169. bpl = ALIGN(bpl, video->bpl_alignment);
  170. pix->pixelformat = formats[i].pixelformat;
  171. pix->bytesperline = bpl;
  172. pix->sizeimage = pix->bytesperline * pix->height;
  173. pix->colorspace = mbus->colorspace;
  174. pix->field = mbus->field;
  175. return bpl - min_bpl;
  176. }
  177. static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
  178. struct v4l2_mbus_framefmt *mbus)
  179. {
  180. unsigned int i;
  181. memset(mbus, 0, sizeof(*mbus));
  182. mbus->width = pix->width;
  183. mbus->height = pix->height;
  184. /* Skip the last format in the loop so that it will be selected if no
  185. * match is found.
  186. */
  187. for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
  188. if (formats[i].pixelformat == pix->pixelformat)
  189. break;
  190. }
  191. mbus->code = formats[i].code;
  192. mbus->colorspace = pix->colorspace;
  193. mbus->field = pix->field;
  194. }
  195. static struct v4l2_subdev *
  196. isp_video_remote_subdev(struct isp_video *video, u32 *pad)
  197. {
  198. struct media_pad *remote;
  199. remote = media_entity_remote_pad(&video->pad);
  200. if (remote == NULL ||
  201. media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  202. return NULL;
  203. if (pad)
  204. *pad = remote->index;
  205. return media_entity_to_v4l2_subdev(remote->entity);
  206. }
  207. /* Return a pointer to the ISP video instance at the far end of the pipeline. */
  208. static int isp_video_get_graph_data(struct isp_video *video,
  209. struct isp_pipeline *pipe)
  210. {
  211. struct media_entity_graph graph;
  212. struct media_entity *entity = &video->video.entity;
  213. struct media_device *mdev = entity->parent;
  214. struct isp_video *far_end = NULL;
  215. mutex_lock(&mdev->graph_mutex);
  216. media_entity_graph_walk_start(&graph, entity);
  217. while ((entity = media_entity_graph_walk_next(&graph))) {
  218. struct isp_video *__video;
  219. pipe->entities |= 1 << entity->id;
  220. if (far_end != NULL)
  221. continue;
  222. if (entity == &video->video.entity)
  223. continue;
  224. if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
  225. continue;
  226. __video = to_isp_video(media_entity_to_video_device(entity));
  227. if (__video->type != video->type)
  228. far_end = __video;
  229. }
  230. mutex_unlock(&mdev->graph_mutex);
  231. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  232. pipe->input = far_end;
  233. pipe->output = video;
  234. } else {
  235. if (far_end == NULL)
  236. return -EPIPE;
  237. pipe->input = video;
  238. pipe->output = far_end;
  239. }
  240. return 0;
  241. }
  242. static int
  243. __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
  244. {
  245. struct v4l2_subdev_format fmt;
  246. struct v4l2_subdev *subdev;
  247. u32 pad;
  248. int ret;
  249. subdev = isp_video_remote_subdev(video, &pad);
  250. if (subdev == NULL)
  251. return -EINVAL;
  252. fmt.pad = pad;
  253. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  254. mutex_lock(&video->mutex);
  255. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  256. mutex_unlock(&video->mutex);
  257. if (ret)
  258. return ret;
  259. format->type = video->type;
  260. return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
  261. }
  262. static int
  263. isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
  264. {
  265. struct v4l2_format format;
  266. int ret;
  267. memcpy(&format, &vfh->format, sizeof(format));
  268. ret = __isp_video_get_format(video, &format);
  269. if (ret < 0)
  270. return ret;
  271. if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
  272. vfh->format.fmt.pix.height != format.fmt.pix.height ||
  273. vfh->format.fmt.pix.width != format.fmt.pix.width ||
  274. vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
  275. vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
  276. return -EINVAL;
  277. return ret;
  278. }
  279. /* -----------------------------------------------------------------------------
  280. * Video queue operations
  281. */
  282. static int isp_video_queue_setup(struct vb2_queue *queue,
  283. const struct v4l2_format *fmt,
  284. unsigned int *count, unsigned int *num_planes,
  285. unsigned int sizes[], void *alloc_ctxs[])
  286. {
  287. struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
  288. struct isp_video *video = vfh->video;
  289. *num_planes = 1;
  290. sizes[0] = vfh->format.fmt.pix.sizeimage;
  291. if (sizes[0] == 0)
  292. return -EINVAL;
  293. alloc_ctxs[0] = video->alloc_ctx;
  294. *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
  295. return 0;
  296. }
  297. static int isp_video_buffer_prepare(struct vb2_buffer *buf)
  298. {
  299. struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
  300. struct isp_buffer *buffer = to_isp_buffer(buf);
  301. struct isp_video *video = vfh->video;
  302. dma_addr_t addr;
  303. /* Refuse to prepare the buffer is the video node has registered an
  304. * error. We don't need to take any lock here as the operation is
  305. * inherently racy. The authoritative check will be performed in the
  306. * queue handler, which can't return an error, this check is just a best
  307. * effort to notify userspace as early as possible.
  308. */
  309. if (unlikely(video->error))
  310. return -EIO;
  311. addr = vb2_dma_contig_plane_dma_addr(buf, 0);
  312. if (!IS_ALIGNED(addr, 32)) {
  313. dev_dbg(video->isp->dev,
  314. "Buffer address must be aligned to 32 bytes boundary.\n");
  315. return -EINVAL;
  316. }
  317. vb2_set_plane_payload(&buffer->vb, 0, vfh->format.fmt.pix.sizeimage);
  318. buffer->dma = addr;
  319. return 0;
  320. }
  321. /*
  322. * isp_video_buffer_queue - Add buffer to streaming queue
  323. * @buf: Video buffer
  324. *
  325. * In memory-to-memory mode, start streaming on the pipeline if buffers are
  326. * queued on both the input and the output, if the pipeline isn't already busy.
  327. * If the pipeline is busy, it will be restarted in the output module interrupt
  328. * handler.
  329. */
  330. static void isp_video_buffer_queue(struct vb2_buffer *buf)
  331. {
  332. struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
  333. struct isp_buffer *buffer = to_isp_buffer(buf);
  334. struct isp_video *video = vfh->video;
  335. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  336. enum isp_pipeline_state state;
  337. unsigned long flags;
  338. unsigned int empty;
  339. unsigned int start;
  340. spin_lock_irqsave(&video->irqlock, flags);
  341. if (unlikely(video->error)) {
  342. vb2_buffer_done(&buffer->vb, VB2_BUF_STATE_ERROR);
  343. spin_unlock_irqrestore(&video->irqlock, flags);
  344. return;
  345. }
  346. empty = list_empty(&video->dmaqueue);
  347. list_add_tail(&buffer->irqlist, &video->dmaqueue);
  348. spin_unlock_irqrestore(&video->irqlock, flags);
  349. if (empty) {
  350. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  351. state = ISP_PIPELINE_QUEUE_OUTPUT;
  352. else
  353. state = ISP_PIPELINE_QUEUE_INPUT;
  354. spin_lock_irqsave(&pipe->lock, flags);
  355. pipe->state |= state;
  356. video->ops->queue(video, buffer);
  357. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
  358. start = isp_pipeline_ready(pipe);
  359. if (start)
  360. pipe->state |= ISP_PIPELINE_STREAM;
  361. spin_unlock_irqrestore(&pipe->lock, flags);
  362. if (start)
  363. omap3isp_pipeline_set_stream(pipe,
  364. ISP_PIPELINE_STREAM_SINGLESHOT);
  365. }
  366. }
  367. static const struct vb2_ops isp_video_queue_ops = {
  368. .queue_setup = isp_video_queue_setup,
  369. .buf_prepare = isp_video_buffer_prepare,
  370. .buf_queue = isp_video_buffer_queue,
  371. };
  372. /*
  373. * omap3isp_video_buffer_next - Complete the current buffer and return the next
  374. * @video: ISP video object
  375. *
  376. * Remove the current video buffer from the DMA queue and fill its timestamp and
  377. * field count before handing it back to videobuf2.
  378. *
  379. * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
  380. * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
  381. * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
  382. *
  383. * The DMA queue is expected to contain at least one buffer.
  384. *
  385. * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
  386. * empty.
  387. */
  388. struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
  389. {
  390. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  391. enum isp_pipeline_state state;
  392. struct isp_buffer *buf;
  393. unsigned long flags;
  394. struct timespec ts;
  395. spin_lock_irqsave(&video->irqlock, flags);
  396. if (WARN_ON(list_empty(&video->dmaqueue))) {
  397. spin_unlock_irqrestore(&video->irqlock, flags);
  398. return NULL;
  399. }
  400. buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
  401. irqlist);
  402. list_del(&buf->irqlist);
  403. spin_unlock_irqrestore(&video->irqlock, flags);
  404. ktime_get_ts(&ts);
  405. buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
  406. buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  407. /* Do frame number propagation only if this is the output video node.
  408. * Frame number either comes from the CSI receivers or it gets
  409. * incremented here if H3A is not active.
  410. * Note: There is no guarantee that the output buffer will finish
  411. * first, so the input number might lag behind by 1 in some cases.
  412. */
  413. if (video == pipe->output && !pipe->do_propagation)
  414. buf->vb.v4l2_buf.sequence =
  415. atomic_inc_return(&pipe->frame_number);
  416. else
  417. buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number);
  418. /* Report pipeline errors to userspace on the capture device side. */
  419. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
  420. state = VB2_BUF_STATE_ERROR;
  421. pipe->error = false;
  422. } else {
  423. state = VB2_BUF_STATE_DONE;
  424. }
  425. vb2_buffer_done(&buf->vb, state);
  426. spin_lock_irqsave(&video->irqlock, flags);
  427. if (list_empty(&video->dmaqueue)) {
  428. spin_unlock_irqrestore(&video->irqlock, flags);
  429. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  430. state = ISP_PIPELINE_QUEUE_OUTPUT
  431. | ISP_PIPELINE_STREAM;
  432. else
  433. state = ISP_PIPELINE_QUEUE_INPUT
  434. | ISP_PIPELINE_STREAM;
  435. spin_lock_irqsave(&pipe->lock, flags);
  436. pipe->state &= ~state;
  437. if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
  438. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  439. spin_unlock_irqrestore(&pipe->lock, flags);
  440. return NULL;
  441. }
  442. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
  443. spin_lock(&pipe->lock);
  444. pipe->state &= ~ISP_PIPELINE_STREAM;
  445. spin_unlock(&pipe->lock);
  446. }
  447. buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
  448. irqlist);
  449. buf->vb.state = VB2_BUF_STATE_ACTIVE;
  450. spin_unlock_irqrestore(&video->irqlock, flags);
  451. return buf;
  452. }
  453. /*
  454. * omap3isp_video_cancel_stream - Cancel stream on a video node
  455. * @video: ISP video object
  456. *
  457. * Cancelling a stream mark all buffers on the video node as erroneous and makes
  458. * sure no new buffer can be queued.
  459. */
  460. void omap3isp_video_cancel_stream(struct isp_video *video)
  461. {
  462. unsigned long flags;
  463. spin_lock_irqsave(&video->irqlock, flags);
  464. while (!list_empty(&video->dmaqueue)) {
  465. struct isp_buffer *buf;
  466. buf = list_first_entry(&video->dmaqueue,
  467. struct isp_buffer, irqlist);
  468. list_del(&buf->irqlist);
  469. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  470. }
  471. video->error = true;
  472. spin_unlock_irqrestore(&video->irqlock, flags);
  473. }
  474. /*
  475. * omap3isp_video_resume - Perform resume operation on the buffers
  476. * @video: ISP video object
  477. * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
  478. *
  479. * This function is intended to be used on suspend/resume scenario. It
  480. * requests video queue layer to discard buffers marked as DONE if it's in
  481. * continuous mode and requests ISP modules to queue again the ACTIVE buffer
  482. * if there's any.
  483. */
  484. void omap3isp_video_resume(struct isp_video *video, int continuous)
  485. {
  486. struct isp_buffer *buf = NULL;
  487. if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  488. mutex_lock(&video->queue_lock);
  489. vb2_discard_done(video->queue);
  490. mutex_unlock(&video->queue_lock);
  491. }
  492. if (!list_empty(&video->dmaqueue)) {
  493. buf = list_first_entry(&video->dmaqueue,
  494. struct isp_buffer, irqlist);
  495. video->ops->queue(video, buf);
  496. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
  497. } else {
  498. if (continuous)
  499. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  500. }
  501. }
  502. /* -----------------------------------------------------------------------------
  503. * V4L2 ioctls
  504. */
  505. static int
  506. isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  507. {
  508. struct isp_video *video = video_drvdata(file);
  509. strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
  510. strlcpy(cap->card, video->video.name, sizeof(cap->card));
  511. strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
  512. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  513. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  514. else
  515. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  516. return 0;
  517. }
  518. static int
  519. isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
  520. {
  521. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  522. struct isp_video *video = video_drvdata(file);
  523. if (format->type != video->type)
  524. return -EINVAL;
  525. mutex_lock(&video->mutex);
  526. *format = vfh->format;
  527. mutex_unlock(&video->mutex);
  528. return 0;
  529. }
  530. static int
  531. isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
  532. {
  533. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  534. struct isp_video *video = video_drvdata(file);
  535. struct v4l2_mbus_framefmt fmt;
  536. if (format->type != video->type)
  537. return -EINVAL;
  538. mutex_lock(&video->mutex);
  539. /* Fill the bytesperline and sizeimage fields by converting to media bus
  540. * format and back to pixel format.
  541. */
  542. isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
  543. isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
  544. vfh->format = *format;
  545. mutex_unlock(&video->mutex);
  546. return 0;
  547. }
  548. static int
  549. isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
  550. {
  551. struct isp_video *video = video_drvdata(file);
  552. struct v4l2_subdev_format fmt;
  553. struct v4l2_subdev *subdev;
  554. u32 pad;
  555. int ret;
  556. if (format->type != video->type)
  557. return -EINVAL;
  558. subdev = isp_video_remote_subdev(video, &pad);
  559. if (subdev == NULL)
  560. return -EINVAL;
  561. isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
  562. fmt.pad = pad;
  563. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  564. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  565. if (ret)
  566. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  567. isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
  568. return 0;
  569. }
  570. static int
  571. isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
  572. {
  573. struct isp_video *video = video_drvdata(file);
  574. struct v4l2_subdev *subdev;
  575. int ret;
  576. subdev = isp_video_remote_subdev(video, NULL);
  577. if (subdev == NULL)
  578. return -EINVAL;
  579. mutex_lock(&video->mutex);
  580. ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
  581. mutex_unlock(&video->mutex);
  582. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  583. }
  584. static int
  585. isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  586. {
  587. struct isp_video *video = video_drvdata(file);
  588. struct v4l2_subdev_format format;
  589. struct v4l2_subdev *subdev;
  590. u32 pad;
  591. int ret;
  592. subdev = isp_video_remote_subdev(video, &pad);
  593. if (subdev == NULL)
  594. return -EINVAL;
  595. /* Try the get crop operation first and fallback to get format if not
  596. * implemented.
  597. */
  598. ret = v4l2_subdev_call(subdev, video, g_crop, crop);
  599. if (ret != -ENOIOCTLCMD)
  600. return ret;
  601. format.pad = pad;
  602. format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  603. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
  604. if (ret < 0)
  605. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  606. crop->c.left = 0;
  607. crop->c.top = 0;
  608. crop->c.width = format.format.width;
  609. crop->c.height = format.format.height;
  610. return 0;
  611. }
  612. static int
  613. isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
  614. {
  615. struct isp_video *video = video_drvdata(file);
  616. struct v4l2_subdev *subdev;
  617. int ret;
  618. subdev = isp_video_remote_subdev(video, NULL);
  619. if (subdev == NULL)
  620. return -EINVAL;
  621. mutex_lock(&video->mutex);
  622. ret = v4l2_subdev_call(subdev, video, s_crop, crop);
  623. mutex_unlock(&video->mutex);
  624. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  625. }
  626. static int
  627. isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
  628. {
  629. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  630. struct isp_video *video = video_drvdata(file);
  631. if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  632. video->type != a->type)
  633. return -EINVAL;
  634. memset(a, 0, sizeof(*a));
  635. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  636. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  637. a->parm.output.timeperframe = vfh->timeperframe;
  638. return 0;
  639. }
  640. static int
  641. isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
  642. {
  643. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  644. struct isp_video *video = video_drvdata(file);
  645. if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  646. video->type != a->type)
  647. return -EINVAL;
  648. if (a->parm.output.timeperframe.denominator == 0)
  649. a->parm.output.timeperframe.denominator = 1;
  650. vfh->timeperframe = a->parm.output.timeperframe;
  651. return 0;
  652. }
  653. static int
  654. isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
  655. {
  656. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  657. struct isp_video *video = video_drvdata(file);
  658. int ret;
  659. mutex_lock(&video->queue_lock);
  660. ret = vb2_reqbufs(&vfh->queue, rb);
  661. mutex_unlock(&video->queue_lock);
  662. return ret;
  663. }
  664. static int
  665. isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
  666. {
  667. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  668. struct isp_video *video = video_drvdata(file);
  669. int ret;
  670. mutex_lock(&video->queue_lock);
  671. ret = vb2_querybuf(&vfh->queue, b);
  672. mutex_unlock(&video->queue_lock);
  673. return ret;
  674. }
  675. static int
  676. isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  677. {
  678. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  679. struct isp_video *video = video_drvdata(file);
  680. int ret;
  681. mutex_lock(&video->queue_lock);
  682. ret = vb2_qbuf(&vfh->queue, b);
  683. mutex_unlock(&video->queue_lock);
  684. return ret;
  685. }
  686. static int
  687. isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  688. {
  689. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  690. struct isp_video *video = video_drvdata(file);
  691. int ret;
  692. mutex_lock(&video->queue_lock);
  693. ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
  694. mutex_unlock(&video->queue_lock);
  695. return ret;
  696. }
  697. static int isp_video_check_external_subdevs(struct isp_video *video,
  698. struct isp_pipeline *pipe)
  699. {
  700. struct isp_device *isp = video->isp;
  701. struct media_entity *ents[] = {
  702. &isp->isp_csi2a.subdev.entity,
  703. &isp->isp_csi2c.subdev.entity,
  704. &isp->isp_ccp2.subdev.entity,
  705. &isp->isp_ccdc.subdev.entity
  706. };
  707. struct media_pad *source_pad;
  708. struct media_entity *source = NULL;
  709. struct media_entity *sink;
  710. struct v4l2_subdev_format fmt;
  711. struct v4l2_ext_controls ctrls;
  712. struct v4l2_ext_control ctrl;
  713. unsigned int i;
  714. int ret;
  715. /* Memory-to-memory pipelines have no external subdev. */
  716. if (pipe->input != NULL)
  717. return 0;
  718. for (i = 0; i < ARRAY_SIZE(ents); i++) {
  719. /* Is the entity part of the pipeline? */
  720. if (!(pipe->entities & (1 << ents[i]->id)))
  721. continue;
  722. /* ISP entities have always sink pad == 0. Find source. */
  723. source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
  724. if (source_pad == NULL)
  725. continue;
  726. source = source_pad->entity;
  727. sink = ents[i];
  728. break;
  729. }
  730. if (!source) {
  731. dev_warn(isp->dev, "can't find source, failing now\n");
  732. return -EINVAL;
  733. }
  734. if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
  735. return 0;
  736. pipe->external = media_entity_to_v4l2_subdev(source);
  737. fmt.pad = source_pad->index;
  738. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  739. ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
  740. pad, get_fmt, NULL, &fmt);
  741. if (unlikely(ret < 0)) {
  742. dev_warn(isp->dev, "get_fmt returned null!\n");
  743. return ret;
  744. }
  745. pipe->external_width =
  746. omap3isp_video_format_info(fmt.format.code)->width;
  747. memset(&ctrls, 0, sizeof(ctrls));
  748. memset(&ctrl, 0, sizeof(ctrl));
  749. ctrl.id = V4L2_CID_PIXEL_RATE;
  750. ctrls.count = 1;
  751. ctrls.controls = &ctrl;
  752. ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
  753. if (ret < 0) {
  754. dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
  755. pipe->external->name);
  756. return ret;
  757. }
  758. pipe->external_rate = ctrl.value64;
  759. if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
  760. unsigned int rate = UINT_MAX;
  761. /*
  762. * Check that maximum allowed CCDC pixel rate isn't
  763. * exceeded by the pixel rate.
  764. */
  765. omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
  766. if (pipe->external_rate > rate)
  767. return -ENOSPC;
  768. }
  769. return 0;
  770. }
  771. /*
  772. * Stream management
  773. *
  774. * Every ISP pipeline has a single input and a single output. The input can be
  775. * either a sensor or a video node. The output is always a video node.
  776. *
  777. * As every pipeline has an output video node, the ISP video objects at the
  778. * pipeline output stores the pipeline state. It tracks the streaming state of
  779. * both the input and output, as well as the availability of buffers.
  780. *
  781. * In sensor-to-memory mode, frames are always available at the pipeline input.
  782. * Starting the sensor usually requires I2C transfers and must be done in
  783. * interruptible context. The pipeline is started and stopped synchronously
  784. * to the stream on/off commands. All modules in the pipeline will get their
  785. * subdev set stream handler called. The module at the end of the pipeline must
  786. * delay starting the hardware until buffers are available at its output.
  787. *
  788. * In memory-to-memory mode, starting/stopping the stream requires
  789. * synchronization between the input and output. ISP modules can't be stopped
  790. * in the middle of a frame, and at least some of the modules seem to become
  791. * busy as soon as they're started, even if they don't receive a frame start
  792. * event. For that reason frames need to be processed in single-shot mode. The
  793. * driver needs to wait until a frame is completely processed and written to
  794. * memory before restarting the pipeline for the next frame. Pipelined
  795. * processing might be possible but requires more testing.
  796. *
  797. * Stream start must be delayed until buffers are available at both the input
  798. * and output. The pipeline must be started in the videobuf queue callback with
  799. * the buffers queue spinlock held. The modules subdev set stream operation must
  800. * not sleep.
  801. */
  802. static int
  803. isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  804. {
  805. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  806. struct isp_video *video = video_drvdata(file);
  807. enum isp_pipeline_state state;
  808. struct isp_pipeline *pipe;
  809. unsigned long flags;
  810. int ret;
  811. if (type != video->type)
  812. return -EINVAL;
  813. mutex_lock(&video->stream_lock);
  814. /* Start streaming on the pipeline. No link touching an entity in the
  815. * pipeline can be activated or deactivated once streaming is started.
  816. */
  817. pipe = video->video.entity.pipe
  818. ? to_isp_pipeline(&video->video.entity) : &video->pipe;
  819. pipe->entities = 0;
  820. if (video->isp->pdata->set_constraints)
  821. video->isp->pdata->set_constraints(video->isp, true);
  822. pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
  823. pipe->max_rate = pipe->l3_ick;
  824. ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
  825. if (ret < 0)
  826. goto err_pipeline_start;
  827. /* Verify that the currently configured format matches the output of
  828. * the connected subdev.
  829. */
  830. ret = isp_video_check_format(video, vfh);
  831. if (ret < 0)
  832. goto err_check_format;
  833. video->bpl_padding = ret;
  834. video->bpl_value = vfh->format.fmt.pix.bytesperline;
  835. ret = isp_video_get_graph_data(video, pipe);
  836. if (ret < 0)
  837. goto err_check_format;
  838. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  839. state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
  840. else
  841. state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
  842. ret = isp_video_check_external_subdevs(video, pipe);
  843. if (ret < 0)
  844. goto err_check_format;
  845. pipe->error = false;
  846. spin_lock_irqsave(&pipe->lock, flags);
  847. pipe->state &= ~ISP_PIPELINE_STREAM;
  848. pipe->state |= state;
  849. spin_unlock_irqrestore(&pipe->lock, flags);
  850. /* Set the maximum time per frame as the value requested by userspace.
  851. * This is a soft limit that can be overridden if the hardware doesn't
  852. * support the request limit.
  853. */
  854. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  855. pipe->max_timeperframe = vfh->timeperframe;
  856. video->queue = &vfh->queue;
  857. INIT_LIST_HEAD(&video->dmaqueue);
  858. atomic_set(&pipe->frame_number, -1);
  859. mutex_lock(&video->queue_lock);
  860. ret = vb2_streamon(&vfh->queue, type);
  861. mutex_unlock(&video->queue_lock);
  862. if (ret < 0)
  863. goto err_check_format;
  864. /* In sensor-to-memory mode, the stream can be started synchronously
  865. * to the stream on command. In memory-to-memory mode, it will be
  866. * started when buffers are queued on both the input and output.
  867. */
  868. if (pipe->input == NULL) {
  869. ret = omap3isp_pipeline_set_stream(pipe,
  870. ISP_PIPELINE_STREAM_CONTINUOUS);
  871. if (ret < 0)
  872. goto err_set_stream;
  873. spin_lock_irqsave(&video->irqlock, flags);
  874. if (list_empty(&video->dmaqueue))
  875. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  876. spin_unlock_irqrestore(&video->irqlock, flags);
  877. }
  878. mutex_unlock(&video->stream_lock);
  879. return 0;
  880. err_set_stream:
  881. mutex_lock(&video->queue_lock);
  882. vb2_streamoff(&vfh->queue, type);
  883. mutex_unlock(&video->queue_lock);
  884. err_check_format:
  885. media_entity_pipeline_stop(&video->video.entity);
  886. err_pipeline_start:
  887. if (video->isp->pdata->set_constraints)
  888. video->isp->pdata->set_constraints(video->isp, false);
  889. /* The DMA queue must be emptied here, otherwise CCDC interrupts that
  890. * will get triggered the next time the CCDC is powered up will try to
  891. * access buffers that might have been freed but still present in the
  892. * DMA queue. This can easily get triggered if the above
  893. * omap3isp_pipeline_set_stream() call fails on a system with a
  894. * free-running sensor.
  895. */
  896. INIT_LIST_HEAD(&video->dmaqueue);
  897. video->queue = NULL;
  898. mutex_unlock(&video->stream_lock);
  899. return ret;
  900. }
  901. static int
  902. isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  903. {
  904. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  905. struct isp_video *video = video_drvdata(file);
  906. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  907. enum isp_pipeline_state state;
  908. unsigned int streaming;
  909. unsigned long flags;
  910. if (type != video->type)
  911. return -EINVAL;
  912. mutex_lock(&video->stream_lock);
  913. /* Make sure we're not streaming yet. */
  914. mutex_lock(&video->queue_lock);
  915. streaming = vb2_is_streaming(&vfh->queue);
  916. mutex_unlock(&video->queue_lock);
  917. if (!streaming)
  918. goto done;
  919. /* Update the pipeline state. */
  920. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  921. state = ISP_PIPELINE_STREAM_OUTPUT
  922. | ISP_PIPELINE_QUEUE_OUTPUT;
  923. else
  924. state = ISP_PIPELINE_STREAM_INPUT
  925. | ISP_PIPELINE_QUEUE_INPUT;
  926. spin_lock_irqsave(&pipe->lock, flags);
  927. pipe->state &= ~state;
  928. spin_unlock_irqrestore(&pipe->lock, flags);
  929. /* Stop the stream. */
  930. omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
  931. omap3isp_video_cancel_stream(video);
  932. mutex_lock(&video->queue_lock);
  933. vb2_streamoff(&vfh->queue, type);
  934. mutex_unlock(&video->queue_lock);
  935. video->queue = NULL;
  936. video->error = false;
  937. if (video->isp->pdata->set_constraints)
  938. video->isp->pdata->set_constraints(video->isp, false);
  939. media_entity_pipeline_stop(&video->video.entity);
  940. done:
  941. mutex_unlock(&video->stream_lock);
  942. return 0;
  943. }
  944. static int
  945. isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
  946. {
  947. if (input->index > 0)
  948. return -EINVAL;
  949. strlcpy(input->name, "camera", sizeof(input->name));
  950. input->type = V4L2_INPUT_TYPE_CAMERA;
  951. return 0;
  952. }
  953. static int
  954. isp_video_g_input(struct file *file, void *fh, unsigned int *input)
  955. {
  956. *input = 0;
  957. return 0;
  958. }
  959. static int
  960. isp_video_s_input(struct file *file, void *fh, unsigned int input)
  961. {
  962. return input == 0 ? 0 : -EINVAL;
  963. }
  964. static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
  965. .vidioc_querycap = isp_video_querycap,
  966. .vidioc_g_fmt_vid_cap = isp_video_get_format,
  967. .vidioc_s_fmt_vid_cap = isp_video_set_format,
  968. .vidioc_try_fmt_vid_cap = isp_video_try_format,
  969. .vidioc_g_fmt_vid_out = isp_video_get_format,
  970. .vidioc_s_fmt_vid_out = isp_video_set_format,
  971. .vidioc_try_fmt_vid_out = isp_video_try_format,
  972. .vidioc_cropcap = isp_video_cropcap,
  973. .vidioc_g_crop = isp_video_get_crop,
  974. .vidioc_s_crop = isp_video_set_crop,
  975. .vidioc_g_parm = isp_video_get_param,
  976. .vidioc_s_parm = isp_video_set_param,
  977. .vidioc_reqbufs = isp_video_reqbufs,
  978. .vidioc_querybuf = isp_video_querybuf,
  979. .vidioc_qbuf = isp_video_qbuf,
  980. .vidioc_dqbuf = isp_video_dqbuf,
  981. .vidioc_streamon = isp_video_streamon,
  982. .vidioc_streamoff = isp_video_streamoff,
  983. .vidioc_enum_input = isp_video_enum_input,
  984. .vidioc_g_input = isp_video_g_input,
  985. .vidioc_s_input = isp_video_s_input,
  986. };
  987. /* -----------------------------------------------------------------------------
  988. * V4L2 file operations
  989. */
  990. static int isp_video_open(struct file *file)
  991. {
  992. struct isp_video *video = video_drvdata(file);
  993. struct isp_video_fh *handle;
  994. struct vb2_queue *queue;
  995. int ret = 0;
  996. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  997. if (handle == NULL)
  998. return -ENOMEM;
  999. v4l2_fh_init(&handle->vfh, &video->video);
  1000. v4l2_fh_add(&handle->vfh);
  1001. /* If this is the first user, initialise the pipeline. */
  1002. if (omap3isp_get(video->isp) == NULL) {
  1003. ret = -EBUSY;
  1004. goto done;
  1005. }
  1006. ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
  1007. if (ret < 0) {
  1008. omap3isp_put(video->isp);
  1009. goto done;
  1010. }
  1011. queue = &handle->queue;
  1012. queue->type = video->type;
  1013. queue->io_modes = VB2_MMAP | VB2_USERPTR;
  1014. queue->drv_priv = handle;
  1015. queue->ops = &isp_video_queue_ops;
  1016. queue->mem_ops = &vb2_dma_contig_memops;
  1017. queue->buf_struct_size = sizeof(struct isp_buffer);
  1018. queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1019. ret = vb2_queue_init(&handle->queue);
  1020. if (ret < 0) {
  1021. omap3isp_put(video->isp);
  1022. goto done;
  1023. }
  1024. memset(&handle->format, 0, sizeof(handle->format));
  1025. handle->format.type = video->type;
  1026. handle->timeperframe.denominator = 1;
  1027. handle->video = video;
  1028. file->private_data = &handle->vfh;
  1029. done:
  1030. if (ret < 0) {
  1031. v4l2_fh_del(&handle->vfh);
  1032. kfree(handle);
  1033. }
  1034. return ret;
  1035. }
  1036. static int isp_video_release(struct file *file)
  1037. {
  1038. struct isp_video *video = video_drvdata(file);
  1039. struct v4l2_fh *vfh = file->private_data;
  1040. struct isp_video_fh *handle = to_isp_video_fh(vfh);
  1041. /* Disable streaming and free the buffers queue resources. */
  1042. isp_video_streamoff(file, vfh, video->type);
  1043. mutex_lock(&video->queue_lock);
  1044. vb2_queue_release(&handle->queue);
  1045. mutex_unlock(&video->queue_lock);
  1046. omap3isp_pipeline_pm_use(&video->video.entity, 0);
  1047. /* Release the file handle. */
  1048. v4l2_fh_del(vfh);
  1049. kfree(handle);
  1050. file->private_data = NULL;
  1051. omap3isp_put(video->isp);
  1052. return 0;
  1053. }
  1054. static unsigned int isp_video_poll(struct file *file, poll_table *wait)
  1055. {
  1056. struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
  1057. struct isp_video *video = video_drvdata(file);
  1058. int ret;
  1059. mutex_lock(&video->queue_lock);
  1060. ret = vb2_poll(&vfh->queue, file, wait);
  1061. mutex_unlock(&video->queue_lock);
  1062. return ret;
  1063. }
  1064. static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
  1065. {
  1066. struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
  1067. struct isp_video *video = video_drvdata(file);
  1068. int ret;
  1069. mutex_lock(&video->queue_lock);
  1070. ret = vb2_mmap(&vfh->queue, vma);
  1071. mutex_unlock(&video->queue_lock);
  1072. return ret;
  1073. }
  1074. static struct v4l2_file_operations isp_video_fops = {
  1075. .owner = THIS_MODULE,
  1076. .unlocked_ioctl = video_ioctl2,
  1077. .open = isp_video_open,
  1078. .release = isp_video_release,
  1079. .poll = isp_video_poll,
  1080. .mmap = isp_video_mmap,
  1081. };
  1082. /* -----------------------------------------------------------------------------
  1083. * ISP video core
  1084. */
  1085. static const struct isp_video_operations isp_video_dummy_ops = {
  1086. };
  1087. int omap3isp_video_init(struct isp_video *video, const char *name)
  1088. {
  1089. const char *direction;
  1090. int ret;
  1091. switch (video->type) {
  1092. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1093. direction = "output";
  1094. video->pad.flags = MEDIA_PAD_FL_SINK
  1095. | MEDIA_PAD_FL_MUST_CONNECT;
  1096. break;
  1097. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1098. direction = "input";
  1099. video->pad.flags = MEDIA_PAD_FL_SOURCE
  1100. | MEDIA_PAD_FL_MUST_CONNECT;
  1101. video->video.vfl_dir = VFL_DIR_TX;
  1102. break;
  1103. default:
  1104. return -EINVAL;
  1105. }
  1106. video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
  1107. if (IS_ERR(video->alloc_ctx))
  1108. return PTR_ERR(video->alloc_ctx);
  1109. ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
  1110. if (ret < 0) {
  1111. vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
  1112. return ret;
  1113. }
  1114. mutex_init(&video->mutex);
  1115. atomic_set(&video->active, 0);
  1116. spin_lock_init(&video->pipe.lock);
  1117. mutex_init(&video->stream_lock);
  1118. mutex_init(&video->queue_lock);
  1119. spin_lock_init(&video->irqlock);
  1120. /* Initialize the video device. */
  1121. if (video->ops == NULL)
  1122. video->ops = &isp_video_dummy_ops;
  1123. video->video.fops = &isp_video_fops;
  1124. snprintf(video->video.name, sizeof(video->video.name),
  1125. "OMAP3 ISP %s %s", name, direction);
  1126. video->video.vfl_type = VFL_TYPE_GRABBER;
  1127. video->video.release = video_device_release_empty;
  1128. video->video.ioctl_ops = &isp_video_ioctl_ops;
  1129. video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
  1130. video_set_drvdata(&video->video, video);
  1131. return 0;
  1132. }
  1133. void omap3isp_video_cleanup(struct isp_video *video)
  1134. {
  1135. vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
  1136. media_entity_cleanup(&video->video.entity);
  1137. mutex_destroy(&video->queue_lock);
  1138. mutex_destroy(&video->stream_lock);
  1139. mutex_destroy(&video->mutex);
  1140. }
  1141. int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
  1142. {
  1143. int ret;
  1144. video->video.v4l2_dev = vdev;
  1145. ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
  1146. if (ret < 0)
  1147. dev_err(video->isp->dev,
  1148. "%s: could not register video device (%d)\n",
  1149. __func__, ret);
  1150. return ret;
  1151. }
  1152. void omap3isp_video_unregister(struct isp_video *video)
  1153. {
  1154. if (video_is_registered(&video->video))
  1155. video_unregister_device(&video->video);
  1156. }