ispvideo.c 38 KB

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