ispvideo.c 41 KB

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