ispvideo.c 39 KB

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