ispvideo.c 38 KB

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