ispvideo.c 39 KB

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