ispvideo.c 41 KB

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