ispvideo.c 39 KB

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