ispvideo.c 39 KB

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