ispvideo.c 38 KB

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