ispvideo.c 38 KB

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