ispvideo.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. #include <asm/cacheflush.h>
  16. #include <linux/clk.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <media/v4l2-dev.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-mc.h>
  27. #include <media/videobuf2-dma-contig.h>
  28. #include "ispvideo.h"
  29. #include "isp.h"
  30. /* -----------------------------------------------------------------------------
  31. * Helper functions
  32. */
  33. /*
  34. * NOTE: When adding new media bus codes, always remember to add
  35. * corresponding in-memory formats to the table below!!!
  36. */
  37. static struct isp_format_info formats[] = {
  38. { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
  39. MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
  40. V4L2_PIX_FMT_GREY, 8, 1, },
  41. { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
  42. MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
  43. V4L2_PIX_FMT_Y10, 10, 2, },
  44. { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
  45. MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
  46. V4L2_PIX_FMT_Y12, 12, 2, },
  47. { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
  48. MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
  49. V4L2_PIX_FMT_SBGGR8, 8, 1, },
  50. { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
  51. MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
  52. V4L2_PIX_FMT_SGBRG8, 8, 1, },
  53. { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
  54. MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
  55. V4L2_PIX_FMT_SGRBG8, 8, 1, },
  56. { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
  57. MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
  58. V4L2_PIX_FMT_SRGGB8, 8, 1, },
  59. { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
  60. MEDIA_BUS_FMT_SBGGR10_1X10, 0,
  61. V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
  62. { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
  63. MEDIA_BUS_FMT_SGBRG10_1X10, 0,
  64. V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
  65. { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
  66. MEDIA_BUS_FMT_SGRBG10_1X10, 0,
  67. V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
  68. { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
  69. MEDIA_BUS_FMT_SRGGB10_1X10, 0,
  70. V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
  71. { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
  72. MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
  73. V4L2_PIX_FMT_SBGGR10, 10, 2, },
  74. { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
  75. MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
  76. V4L2_PIX_FMT_SGBRG10, 10, 2, },
  77. { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
  78. MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
  79. V4L2_PIX_FMT_SGRBG10, 10, 2, },
  80. { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
  81. MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
  82. V4L2_PIX_FMT_SRGGB10, 10, 2, },
  83. { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
  84. MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
  85. V4L2_PIX_FMT_SBGGR12, 12, 2, },
  86. { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
  87. MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
  88. V4L2_PIX_FMT_SGBRG12, 12, 2, },
  89. { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
  90. MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
  91. V4L2_PIX_FMT_SGRBG12, 12, 2, },
  92. { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
  93. MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
  94. V4L2_PIX_FMT_SRGGB12, 12, 2, },
  95. { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
  96. MEDIA_BUS_FMT_UYVY8_1X16, 0,
  97. V4L2_PIX_FMT_UYVY, 16, 2, },
  98. { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
  99. MEDIA_BUS_FMT_YUYV8_1X16, 0,
  100. V4L2_PIX_FMT_YUYV, 16, 2, },
  101. { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8,
  102. MEDIA_BUS_FMT_UYVY8_2X8, 0,
  103. V4L2_PIX_FMT_UYVY, 8, 2, },
  104. { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8,
  105. MEDIA_BUS_FMT_YUYV8_2X8, 0,
  106. V4L2_PIX_FMT_YUYV, 8, 2, },
  107. /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
  108. * module and avoid NULL pointer dereferences.
  109. */
  110. { 0, }
  111. };
  112. const struct isp_format_info *omap3isp_video_format_info(u32 code)
  113. {
  114. unsigned int i;
  115. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  116. if (formats[i].code == code)
  117. return &formats[i];
  118. }
  119. return NULL;
  120. }
  121. /*
  122. * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
  123. * @video: ISP video instance
  124. * @mbus: v4l2_mbus_framefmt format (input)
  125. * @pix: v4l2_pix_format format (output)
  126. *
  127. * Fill the output pix structure with information from the input mbus format.
  128. * The bytesperline and sizeimage fields are computed from the requested bytes
  129. * per line value in the pix format and information from the video instance.
  130. *
  131. * Return the number of padding bytes at end of line.
  132. */
  133. static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
  134. const struct v4l2_mbus_framefmt *mbus,
  135. struct v4l2_pix_format *pix)
  136. {
  137. unsigned int bpl = pix->bytesperline;
  138. unsigned int min_bpl;
  139. unsigned int i;
  140. memset(pix, 0, sizeof(*pix));
  141. pix->width = mbus->width;
  142. pix->height = mbus->height;
  143. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  144. if (formats[i].code == mbus->code)
  145. break;
  146. }
  147. if (WARN_ON(i == ARRAY_SIZE(formats)))
  148. return 0;
  149. min_bpl = pix->width * formats[i].bpp;
  150. /* Clamp the requested bytes per line value. If the maximum bytes per
  151. * line value is zero, the module doesn't support user configurable line
  152. * sizes. Override the requested value with the minimum in that case.
  153. */
  154. if (video->bpl_max)
  155. bpl = clamp(bpl, min_bpl, video->bpl_max);
  156. else
  157. bpl = min_bpl;
  158. if (!video->bpl_zero_padding || bpl != min_bpl)
  159. bpl = ALIGN(bpl, video->bpl_alignment);
  160. pix->pixelformat = formats[i].pixelformat;
  161. pix->bytesperline = bpl;
  162. pix->sizeimage = pix->bytesperline * pix->height;
  163. pix->colorspace = mbus->colorspace;
  164. pix->field = mbus->field;
  165. return bpl - min_bpl;
  166. }
  167. static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
  168. struct v4l2_mbus_framefmt *mbus)
  169. {
  170. unsigned int i;
  171. memset(mbus, 0, sizeof(*mbus));
  172. mbus->width = pix->width;
  173. mbus->height = pix->height;
  174. /* Skip the last format in the loop so that it will be selected if no
  175. * match is found.
  176. */
  177. for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
  178. if (formats[i].pixelformat == pix->pixelformat)
  179. break;
  180. }
  181. mbus->code = formats[i].code;
  182. mbus->colorspace = pix->colorspace;
  183. mbus->field = pix->field;
  184. }
  185. static struct v4l2_subdev *
  186. isp_video_remote_subdev(struct isp_video *video, u32 *pad)
  187. {
  188. struct media_pad *remote;
  189. remote = media_entity_remote_pad(&video->pad);
  190. if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
  191. return NULL;
  192. if (pad)
  193. *pad = remote->index;
  194. return media_entity_to_v4l2_subdev(remote->entity);
  195. }
  196. /* Return a pointer to the ISP video instance at the far end of the pipeline. */
  197. static int isp_video_get_graph_data(struct isp_video *video,
  198. struct isp_pipeline *pipe)
  199. {
  200. struct media_graph graph;
  201. struct media_entity *entity = &video->video.entity;
  202. struct media_device *mdev = entity->graph_obj.mdev;
  203. struct isp_video *far_end = NULL;
  204. int ret;
  205. mutex_lock(&mdev->graph_mutex);
  206. ret = media_graph_walk_init(&graph, mdev);
  207. if (ret) {
  208. mutex_unlock(&mdev->graph_mutex);
  209. return ret;
  210. }
  211. media_graph_walk_start(&graph, entity);
  212. while ((entity = media_graph_walk_next(&graph))) {
  213. struct isp_video *__video;
  214. media_entity_enum_set(&pipe->ent_enum, entity);
  215. if (far_end != NULL)
  216. continue;
  217. if (entity == &video->video.entity)
  218. continue;
  219. if (!is_media_entity_v4l2_video_device(entity))
  220. continue;
  221. __video = to_isp_video(media_entity_to_video_device(entity));
  222. if (__video->type != video->type)
  223. far_end = __video;
  224. }
  225. mutex_unlock(&mdev->graph_mutex);
  226. media_graph_walk_cleanup(&graph);
  227. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  228. pipe->input = far_end;
  229. pipe->output = video;
  230. } else {
  231. if (far_end == NULL)
  232. return -EPIPE;
  233. pipe->input = video;
  234. pipe->output = far_end;
  235. }
  236. return 0;
  237. }
  238. static int
  239. __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
  240. {
  241. struct v4l2_subdev_format fmt;
  242. struct v4l2_subdev *subdev;
  243. u32 pad;
  244. int ret;
  245. subdev = isp_video_remote_subdev(video, &pad);
  246. if (subdev == NULL)
  247. return -EINVAL;
  248. fmt.pad = pad;
  249. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  250. mutex_lock(&video->mutex);
  251. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  252. mutex_unlock(&video->mutex);
  253. if (ret)
  254. return ret;
  255. format->type = video->type;
  256. return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
  257. }
  258. static int
  259. isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
  260. {
  261. struct v4l2_format format;
  262. int ret;
  263. memcpy(&format, &vfh->format, sizeof(format));
  264. ret = __isp_video_get_format(video, &format);
  265. if (ret < 0)
  266. return ret;
  267. if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
  268. vfh->format.fmt.pix.height != format.fmt.pix.height ||
  269. vfh->format.fmt.pix.width != format.fmt.pix.width ||
  270. vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
  271. vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
  272. vfh->format.fmt.pix.field != format.fmt.pix.field)
  273. return -EINVAL;
  274. return 0;
  275. }
  276. /* -----------------------------------------------------------------------------
  277. * Video queue operations
  278. */
  279. static int isp_video_queue_setup(struct vb2_queue *queue,
  280. unsigned int *count, unsigned int *num_planes,
  281. unsigned int sizes[], struct device *alloc_devs[])
  282. {
  283. struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
  284. struct isp_video *video = vfh->video;
  285. *num_planes = 1;
  286. sizes[0] = vfh->format.fmt.pix.sizeimage;
  287. if (sizes[0] == 0)
  288. return -EINVAL;
  289. *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
  290. return 0;
  291. }
  292. static int isp_video_buffer_prepare(struct vb2_buffer *buf)
  293. {
  294. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
  295. struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
  296. struct isp_buffer *buffer = to_isp_buffer(vbuf);
  297. struct isp_video *video = vfh->video;
  298. dma_addr_t addr;
  299. /* Refuse to prepare the buffer is the video node has registered an
  300. * error. We don't need to take any lock here as the operation is
  301. * inherently racy. The authoritative check will be performed in the
  302. * queue handler, which can't return an error, this check is just a best
  303. * effort to notify userspace as early as possible.
  304. */
  305. if (unlikely(video->error))
  306. return -EIO;
  307. addr = vb2_dma_contig_plane_dma_addr(buf, 0);
  308. if (!IS_ALIGNED(addr, 32)) {
  309. dev_dbg(video->isp->dev,
  310. "Buffer address must be aligned to 32 bytes boundary.\n");
  311. return -EINVAL;
  312. }
  313. vb2_set_plane_payload(&buffer->vb.vb2_buf, 0,
  314. vfh->format.fmt.pix.sizeimage);
  315. buffer->dma = addr;
  316. return 0;
  317. }
  318. /*
  319. * isp_video_buffer_queue - Add buffer to streaming queue
  320. * @buf: Video buffer
  321. *
  322. * In memory-to-memory mode, start streaming on the pipeline if buffers are
  323. * queued on both the input and the output, if the pipeline isn't already busy.
  324. * If the pipeline is busy, it will be restarted in the output module interrupt
  325. * handler.
  326. */
  327. static void isp_video_buffer_queue(struct vb2_buffer *buf)
  328. {
  329. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
  330. struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
  331. struct isp_buffer *buffer = to_isp_buffer(vbuf);
  332. struct isp_video *video = vfh->video;
  333. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  334. enum isp_pipeline_state state;
  335. unsigned long flags;
  336. unsigned int empty;
  337. unsigned int start;
  338. spin_lock_irqsave(&video->irqlock, flags);
  339. if (unlikely(video->error)) {
  340. vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  341. spin_unlock_irqrestore(&video->irqlock, flags);
  342. return;
  343. }
  344. empty = list_empty(&video->dmaqueue);
  345. list_add_tail(&buffer->irqlist, &video->dmaqueue);
  346. spin_unlock_irqrestore(&video->irqlock, flags);
  347. if (empty) {
  348. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  349. state = ISP_PIPELINE_QUEUE_OUTPUT;
  350. else
  351. state = ISP_PIPELINE_QUEUE_INPUT;
  352. spin_lock_irqsave(&pipe->lock, flags);
  353. pipe->state |= state;
  354. video->ops->queue(video, buffer);
  355. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
  356. start = isp_pipeline_ready(pipe);
  357. if (start)
  358. pipe->state |= ISP_PIPELINE_STREAM;
  359. spin_unlock_irqrestore(&pipe->lock, flags);
  360. if (start)
  361. omap3isp_pipeline_set_stream(pipe,
  362. ISP_PIPELINE_STREAM_SINGLESHOT);
  363. }
  364. }
  365. /*
  366. * omap3isp_video_return_buffers - Return all queued buffers to videobuf2
  367. * @video: ISP video object
  368. * @state: new state for the returned buffers
  369. *
  370. * Return all buffers queued on the video node to videobuf2 in the given state.
  371. * The buffer state should be VB2_BUF_STATE_QUEUED if called due to an error
  372. * when starting the stream, or VB2_BUF_STATE_ERROR otherwise.
  373. *
  374. * The function must be called with the video irqlock held.
  375. */
  376. static void omap3isp_video_return_buffers(struct isp_video *video,
  377. enum vb2_buffer_state state)
  378. {
  379. while (!list_empty(&video->dmaqueue)) {
  380. struct isp_buffer *buf;
  381. buf = list_first_entry(&video->dmaqueue,
  382. struct isp_buffer, irqlist);
  383. list_del(&buf->irqlist);
  384. vb2_buffer_done(&buf->vb.vb2_buf, state);
  385. }
  386. }
  387. static int isp_video_start_streaming(struct vb2_queue *queue,
  388. unsigned int count)
  389. {
  390. struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
  391. struct isp_video *video = vfh->video;
  392. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  393. unsigned long flags;
  394. int ret;
  395. /* In sensor-to-memory mode, the stream can be started synchronously
  396. * to the stream on command. In memory-to-memory mode, it will be
  397. * started when buffers are queued on both the input and output.
  398. */
  399. if (pipe->input)
  400. return 0;
  401. ret = omap3isp_pipeline_set_stream(pipe,
  402. ISP_PIPELINE_STREAM_CONTINUOUS);
  403. if (ret < 0) {
  404. spin_lock_irqsave(&video->irqlock, flags);
  405. omap3isp_video_return_buffers(video, VB2_BUF_STATE_QUEUED);
  406. spin_unlock_irqrestore(&video->irqlock, flags);
  407. return ret;
  408. }
  409. spin_lock_irqsave(&video->irqlock, flags);
  410. if (list_empty(&video->dmaqueue))
  411. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  412. spin_unlock_irqrestore(&video->irqlock, flags);
  413. return 0;
  414. }
  415. static const struct vb2_ops isp_video_queue_ops = {
  416. .queue_setup = isp_video_queue_setup,
  417. .buf_prepare = isp_video_buffer_prepare,
  418. .buf_queue = isp_video_buffer_queue,
  419. .start_streaming = isp_video_start_streaming,
  420. };
  421. /*
  422. * omap3isp_video_buffer_next - Complete the current buffer and return the next
  423. * @video: ISP video object
  424. *
  425. * Remove the current video buffer from the DMA queue and fill its timestamp and
  426. * field count before handing it back to videobuf2.
  427. *
  428. * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
  429. * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
  430. * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
  431. *
  432. * The DMA queue is expected to contain at least one buffer.
  433. *
  434. * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
  435. * empty.
  436. */
  437. struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
  438. {
  439. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  440. enum vb2_buffer_state vb_state;
  441. struct isp_buffer *buf;
  442. unsigned long flags;
  443. spin_lock_irqsave(&video->irqlock, flags);
  444. if (WARN_ON(list_empty(&video->dmaqueue))) {
  445. spin_unlock_irqrestore(&video->irqlock, flags);
  446. return NULL;
  447. }
  448. buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
  449. irqlist);
  450. list_del(&buf->irqlist);
  451. spin_unlock_irqrestore(&video->irqlock, flags);
  452. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  453. /* Do frame number propagation only if this is the output video node.
  454. * Frame number either comes from the CSI receivers or it gets
  455. * incremented here if H3A is not active.
  456. * Note: There is no guarantee that the output buffer will finish
  457. * first, so the input number might lag behind by 1 in some cases.
  458. */
  459. if (video == pipe->output && !pipe->do_propagation)
  460. buf->vb.sequence =
  461. atomic_inc_return(&pipe->frame_number);
  462. else
  463. buf->vb.sequence = atomic_read(&pipe->frame_number);
  464. if (pipe->field != V4L2_FIELD_NONE)
  465. buf->vb.sequence /= 2;
  466. buf->vb.field = pipe->field;
  467. /* Report pipeline errors to userspace on the capture device side. */
  468. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
  469. vb_state = VB2_BUF_STATE_ERROR;
  470. pipe->error = false;
  471. } else {
  472. vb_state = VB2_BUF_STATE_DONE;
  473. }
  474. vb2_buffer_done(&buf->vb.vb2_buf, vb_state);
  475. spin_lock_irqsave(&video->irqlock, flags);
  476. if (list_empty(&video->dmaqueue)) {
  477. enum isp_pipeline_state state;
  478. spin_unlock_irqrestore(&video->irqlock, flags);
  479. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  480. state = ISP_PIPELINE_QUEUE_OUTPUT
  481. | ISP_PIPELINE_STREAM;
  482. else
  483. state = ISP_PIPELINE_QUEUE_INPUT
  484. | ISP_PIPELINE_STREAM;
  485. spin_lock_irqsave(&pipe->lock, flags);
  486. pipe->state &= ~state;
  487. if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
  488. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  489. spin_unlock_irqrestore(&pipe->lock, flags);
  490. return NULL;
  491. }
  492. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
  493. spin_lock(&pipe->lock);
  494. pipe->state &= ~ISP_PIPELINE_STREAM;
  495. spin_unlock(&pipe->lock);
  496. }
  497. buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
  498. irqlist);
  499. spin_unlock_irqrestore(&video->irqlock, flags);
  500. return buf;
  501. }
  502. /*
  503. * omap3isp_video_cancel_stream - Cancel stream on a video node
  504. * @video: ISP video object
  505. *
  506. * Cancelling a stream returns all buffers queued on the video node to videobuf2
  507. * in the erroneous state and makes sure no new buffer can be queued.
  508. */
  509. void omap3isp_video_cancel_stream(struct isp_video *video)
  510. {
  511. unsigned long flags;
  512. spin_lock_irqsave(&video->irqlock, flags);
  513. omap3isp_video_return_buffers(video, VB2_BUF_STATE_ERROR);
  514. video->error = true;
  515. spin_unlock_irqrestore(&video->irqlock, flags);
  516. }
  517. /*
  518. * omap3isp_video_resume - Perform resume operation on the buffers
  519. * @video: ISP video object
  520. * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
  521. *
  522. * This function is intended to be used on suspend/resume scenario. It
  523. * requests video queue layer to discard buffers marked as DONE if it's in
  524. * continuous mode and requests ISP modules to queue again the ACTIVE buffer
  525. * if there's any.
  526. */
  527. void omap3isp_video_resume(struct isp_video *video, int continuous)
  528. {
  529. struct isp_buffer *buf = NULL;
  530. if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  531. mutex_lock(&video->queue_lock);
  532. vb2_discard_done(video->queue);
  533. mutex_unlock(&video->queue_lock);
  534. }
  535. if (!list_empty(&video->dmaqueue)) {
  536. buf = list_first_entry(&video->dmaqueue,
  537. struct isp_buffer, irqlist);
  538. video->ops->queue(video, buf);
  539. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
  540. } else {
  541. if (continuous)
  542. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  543. }
  544. }
  545. /* -----------------------------------------------------------------------------
  546. * V4L2 ioctls
  547. */
  548. static int
  549. isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  550. {
  551. struct isp_video *video = video_drvdata(file);
  552. strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
  553. strlcpy(cap->card, video->video.name, sizeof(cap->card));
  554. strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
  555. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  556. | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  557. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  558. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  559. else
  560. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  561. return 0;
  562. }
  563. static int
  564. isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
  565. {
  566. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  567. struct isp_video *video = video_drvdata(file);
  568. if (format->type != video->type)
  569. return -EINVAL;
  570. mutex_lock(&video->mutex);
  571. *format = vfh->format;
  572. mutex_unlock(&video->mutex);
  573. return 0;
  574. }
  575. static int
  576. isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
  577. {
  578. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  579. struct isp_video *video = video_drvdata(file);
  580. struct v4l2_mbus_framefmt fmt;
  581. if (format->type != video->type)
  582. return -EINVAL;
  583. /* Replace unsupported field orders with sane defaults. */
  584. switch (format->fmt.pix.field) {
  585. case V4L2_FIELD_NONE:
  586. /* Progressive is supported everywhere. */
  587. break;
  588. case V4L2_FIELD_ALTERNATE:
  589. /* ALTERNATE is not supported on output nodes. */
  590. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  591. format->fmt.pix.field = V4L2_FIELD_NONE;
  592. break;
  593. case V4L2_FIELD_INTERLACED:
  594. /* The ISP has no concept of video standard, select the
  595. * top-bottom order when the unqualified interlaced order is
  596. * requested.
  597. */
  598. format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
  599. /* Fall-through */
  600. case V4L2_FIELD_INTERLACED_TB:
  601. case V4L2_FIELD_INTERLACED_BT:
  602. /* Interlaced orders are only supported at the CCDC output. */
  603. if (video != &video->isp->isp_ccdc.video_out)
  604. format->fmt.pix.field = V4L2_FIELD_NONE;
  605. break;
  606. case V4L2_FIELD_TOP:
  607. case V4L2_FIELD_BOTTOM:
  608. case V4L2_FIELD_SEQ_TB:
  609. case V4L2_FIELD_SEQ_BT:
  610. default:
  611. /* All other field orders are currently unsupported, default to
  612. * progressive.
  613. */
  614. format->fmt.pix.field = V4L2_FIELD_NONE;
  615. break;
  616. }
  617. /* Fill the bytesperline and sizeimage fields by converting to media bus
  618. * format and back to pixel format.
  619. */
  620. isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
  621. isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
  622. mutex_lock(&video->mutex);
  623. vfh->format = *format;
  624. mutex_unlock(&video->mutex);
  625. return 0;
  626. }
  627. static int
  628. isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
  629. {
  630. struct isp_video *video = video_drvdata(file);
  631. struct v4l2_subdev_format fmt;
  632. struct v4l2_subdev *subdev;
  633. u32 pad;
  634. int ret;
  635. if (format->type != video->type)
  636. return -EINVAL;
  637. subdev = isp_video_remote_subdev(video, &pad);
  638. if (subdev == NULL)
  639. return -EINVAL;
  640. isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
  641. fmt.pad = pad;
  642. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  643. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  644. if (ret)
  645. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  646. isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
  647. return 0;
  648. }
  649. static int
  650. isp_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
  651. {
  652. struct isp_video *video = video_drvdata(file);
  653. struct v4l2_subdev_format format;
  654. struct v4l2_subdev *subdev;
  655. struct v4l2_subdev_selection sdsel = {
  656. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  657. .target = sel->target,
  658. };
  659. u32 pad;
  660. int ret;
  661. switch (sel->target) {
  662. case V4L2_SEL_TGT_CROP:
  663. case V4L2_SEL_TGT_CROP_BOUNDS:
  664. case V4L2_SEL_TGT_CROP_DEFAULT:
  665. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  666. return -EINVAL;
  667. break;
  668. case V4L2_SEL_TGT_COMPOSE:
  669. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  670. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  671. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  672. return -EINVAL;
  673. break;
  674. default:
  675. return -EINVAL;
  676. }
  677. subdev = isp_video_remote_subdev(video, &pad);
  678. if (subdev == NULL)
  679. return -EINVAL;
  680. /* Try the get selection operation first and fallback to get format if not
  681. * implemented.
  682. */
  683. sdsel.pad = pad;
  684. ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
  685. if (!ret)
  686. sel->r = sdsel.r;
  687. if (ret != -ENOIOCTLCMD)
  688. return ret;
  689. format.pad = pad;
  690. format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  691. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
  692. if (ret < 0)
  693. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  694. sel->r.left = 0;
  695. sel->r.top = 0;
  696. sel->r.width = format.format.width;
  697. sel->r.height = format.format.height;
  698. return 0;
  699. }
  700. static int
  701. isp_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
  702. {
  703. struct isp_video *video = video_drvdata(file);
  704. struct v4l2_subdev *subdev;
  705. struct v4l2_subdev_selection sdsel = {
  706. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  707. .target = sel->target,
  708. .flags = sel->flags,
  709. .r = sel->r,
  710. };
  711. u32 pad;
  712. int ret;
  713. switch (sel->target) {
  714. case V4L2_SEL_TGT_CROP:
  715. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  716. return -EINVAL;
  717. break;
  718. case V4L2_SEL_TGT_COMPOSE:
  719. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  720. return -EINVAL;
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. subdev = isp_video_remote_subdev(video, &pad);
  726. if (subdev == NULL)
  727. return -EINVAL;
  728. sdsel.pad = pad;
  729. mutex_lock(&video->mutex);
  730. ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
  731. mutex_unlock(&video->mutex);
  732. if (!ret)
  733. sel->r = sdsel.r;
  734. return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
  735. }
  736. static int
  737. isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
  738. {
  739. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  740. struct isp_video *video = video_drvdata(file);
  741. if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  742. video->type != a->type)
  743. return -EINVAL;
  744. memset(a, 0, sizeof(*a));
  745. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  746. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  747. a->parm.output.timeperframe = vfh->timeperframe;
  748. return 0;
  749. }
  750. static int
  751. isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
  752. {
  753. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  754. struct isp_video *video = video_drvdata(file);
  755. if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  756. video->type != a->type)
  757. return -EINVAL;
  758. if (a->parm.output.timeperframe.denominator == 0)
  759. a->parm.output.timeperframe.denominator = 1;
  760. vfh->timeperframe = a->parm.output.timeperframe;
  761. return 0;
  762. }
  763. static int
  764. isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
  765. {
  766. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  767. struct isp_video *video = video_drvdata(file);
  768. int ret;
  769. mutex_lock(&video->queue_lock);
  770. ret = vb2_reqbufs(&vfh->queue, rb);
  771. mutex_unlock(&video->queue_lock);
  772. return ret;
  773. }
  774. static int
  775. isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
  776. {
  777. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  778. struct isp_video *video = video_drvdata(file);
  779. int ret;
  780. mutex_lock(&video->queue_lock);
  781. ret = vb2_querybuf(&vfh->queue, b);
  782. mutex_unlock(&video->queue_lock);
  783. return ret;
  784. }
  785. static int
  786. isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  787. {
  788. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  789. struct isp_video *video = video_drvdata(file);
  790. int ret;
  791. mutex_lock(&video->queue_lock);
  792. ret = vb2_qbuf(&vfh->queue, b);
  793. mutex_unlock(&video->queue_lock);
  794. return ret;
  795. }
  796. static int
  797. isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  798. {
  799. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  800. struct isp_video *video = video_drvdata(file);
  801. int ret;
  802. mutex_lock(&video->queue_lock);
  803. ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
  804. mutex_unlock(&video->queue_lock);
  805. return ret;
  806. }
  807. static int isp_video_check_external_subdevs(struct isp_video *video,
  808. struct isp_pipeline *pipe)
  809. {
  810. struct isp_device *isp = video->isp;
  811. struct media_entity *ents[] = {
  812. &isp->isp_csi2a.subdev.entity,
  813. &isp->isp_csi2c.subdev.entity,
  814. &isp->isp_ccp2.subdev.entity,
  815. &isp->isp_ccdc.subdev.entity
  816. };
  817. struct media_pad *source_pad;
  818. struct media_entity *source = NULL;
  819. struct media_entity *sink;
  820. struct v4l2_subdev_format fmt;
  821. struct v4l2_ext_controls ctrls;
  822. struct v4l2_ext_control ctrl;
  823. unsigned int i;
  824. int ret;
  825. /* Memory-to-memory pipelines have no external subdev. */
  826. if (pipe->input != NULL)
  827. return 0;
  828. for (i = 0; i < ARRAY_SIZE(ents); i++) {
  829. /* Is the entity part of the pipeline? */
  830. if (!media_entity_enum_test(&pipe->ent_enum, ents[i]))
  831. continue;
  832. /* ISP entities have always sink pad == 0. Find source. */
  833. source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
  834. if (source_pad == NULL)
  835. continue;
  836. source = source_pad->entity;
  837. sink = ents[i];
  838. break;
  839. }
  840. if (!source) {
  841. dev_warn(isp->dev, "can't find source, failing now\n");
  842. return -EINVAL;
  843. }
  844. if (!is_media_entity_v4l2_subdev(source))
  845. return 0;
  846. pipe->external = media_entity_to_v4l2_subdev(source);
  847. fmt.pad = source_pad->index;
  848. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  849. ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
  850. pad, get_fmt, NULL, &fmt);
  851. if (unlikely(ret < 0)) {
  852. dev_warn(isp->dev, "get_fmt returned null!\n");
  853. return ret;
  854. }
  855. pipe->external_width =
  856. omap3isp_video_format_info(fmt.format.code)->width;
  857. memset(&ctrls, 0, sizeof(ctrls));
  858. memset(&ctrl, 0, sizeof(ctrl));
  859. ctrl.id = V4L2_CID_PIXEL_RATE;
  860. ctrls.count = 1;
  861. ctrls.controls = &ctrl;
  862. ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
  863. if (ret < 0) {
  864. dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
  865. pipe->external->name);
  866. return ret;
  867. }
  868. pipe->external_rate = ctrl.value64;
  869. if (media_entity_enum_test(&pipe->ent_enum,
  870. &isp->isp_ccdc.subdev.entity)) {
  871. unsigned int rate = UINT_MAX;
  872. /*
  873. * Check that maximum allowed CCDC pixel rate isn't
  874. * exceeded by the pixel rate.
  875. */
  876. omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
  877. if (pipe->external_rate > rate)
  878. return -ENOSPC;
  879. }
  880. return 0;
  881. }
  882. /*
  883. * Stream management
  884. *
  885. * Every ISP pipeline has a single input and a single output. The input can be
  886. * either a sensor or a video node. The output is always a video node.
  887. *
  888. * As every pipeline has an output video node, the ISP video objects at the
  889. * pipeline output stores the pipeline state. It tracks the streaming state of
  890. * both the input and output, as well as the availability of buffers.
  891. *
  892. * In sensor-to-memory mode, frames are always available at the pipeline input.
  893. * Starting the sensor usually requires I2C transfers and must be done in
  894. * interruptible context. The pipeline is started and stopped synchronously
  895. * to the stream on/off commands. All modules in the pipeline will get their
  896. * subdev set stream handler called. The module at the end of the pipeline must
  897. * delay starting the hardware until buffers are available at its output.
  898. *
  899. * In memory-to-memory mode, starting/stopping the stream requires
  900. * synchronization between the input and output. ISP modules can't be stopped
  901. * in the middle of a frame, and at least some of the modules seem to become
  902. * busy as soon as they're started, even if they don't receive a frame start
  903. * event. For that reason frames need to be processed in single-shot mode. The
  904. * driver needs to wait until a frame is completely processed and written to
  905. * memory before restarting the pipeline for the next frame. Pipelined
  906. * processing might be possible but requires more testing.
  907. *
  908. * Stream start must be delayed until buffers are available at both the input
  909. * and output. The pipeline must be started in the videobuf queue callback with
  910. * the buffers queue spinlock held. The modules subdev set stream operation must
  911. * not sleep.
  912. */
  913. static int
  914. isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  915. {
  916. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  917. struct isp_video *video = video_drvdata(file);
  918. enum isp_pipeline_state state;
  919. struct isp_pipeline *pipe;
  920. unsigned long flags;
  921. int ret;
  922. if (type != video->type)
  923. return -EINVAL;
  924. mutex_lock(&video->stream_lock);
  925. /* Start streaming on the pipeline. No link touching an entity in the
  926. * pipeline can be activated or deactivated once streaming is started.
  927. */
  928. pipe = video->video.entity.pipe
  929. ? to_isp_pipeline(&video->video.entity) : &video->pipe;
  930. ret = media_entity_enum_init(&pipe->ent_enum, &video->isp->media_dev);
  931. if (ret)
  932. goto err_enum_init;
  933. /* TODO: Implement PM QoS */
  934. pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
  935. pipe->max_rate = pipe->l3_ick;
  936. ret = media_pipeline_start(&video->video.entity, &pipe->pipe);
  937. if (ret < 0)
  938. goto err_pipeline_start;
  939. /* Verify that the currently configured format matches the output of
  940. * the connected subdev.
  941. */
  942. ret = isp_video_check_format(video, vfh);
  943. if (ret < 0)
  944. goto err_check_format;
  945. video->bpl_padding = ret;
  946. video->bpl_value = vfh->format.fmt.pix.bytesperline;
  947. ret = isp_video_get_graph_data(video, pipe);
  948. if (ret < 0)
  949. goto err_check_format;
  950. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  951. state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
  952. else
  953. state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
  954. ret = isp_video_check_external_subdevs(video, pipe);
  955. if (ret < 0)
  956. goto err_check_format;
  957. pipe->error = false;
  958. spin_lock_irqsave(&pipe->lock, flags);
  959. pipe->state &= ~ISP_PIPELINE_STREAM;
  960. pipe->state |= state;
  961. spin_unlock_irqrestore(&pipe->lock, flags);
  962. /* Set the maximum time per frame as the value requested by userspace.
  963. * This is a soft limit that can be overridden if the hardware doesn't
  964. * support the request limit.
  965. */
  966. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  967. pipe->max_timeperframe = vfh->timeperframe;
  968. video->queue = &vfh->queue;
  969. INIT_LIST_HEAD(&video->dmaqueue);
  970. atomic_set(&pipe->frame_number, -1);
  971. pipe->field = vfh->format.fmt.pix.field;
  972. mutex_lock(&video->queue_lock);
  973. ret = vb2_streamon(&vfh->queue, type);
  974. mutex_unlock(&video->queue_lock);
  975. if (ret < 0)
  976. goto err_check_format;
  977. mutex_unlock(&video->stream_lock);
  978. return 0;
  979. err_check_format:
  980. media_pipeline_stop(&video->video.entity);
  981. err_pipeline_start:
  982. /* TODO: Implement PM QoS */
  983. /* The DMA queue must be emptied here, otherwise CCDC interrupts that
  984. * will get triggered the next time the CCDC is powered up will try to
  985. * access buffers that might have been freed but still present in the
  986. * DMA queue. This can easily get triggered if the above
  987. * omap3isp_pipeline_set_stream() call fails on a system with a
  988. * free-running sensor.
  989. */
  990. INIT_LIST_HEAD(&video->dmaqueue);
  991. video->queue = NULL;
  992. media_entity_enum_cleanup(&pipe->ent_enum);
  993. err_enum_init:
  994. mutex_unlock(&video->stream_lock);
  995. return ret;
  996. }
  997. static int
  998. isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  999. {
  1000. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  1001. struct isp_video *video = video_drvdata(file);
  1002. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  1003. enum isp_pipeline_state state;
  1004. unsigned int streaming;
  1005. unsigned long flags;
  1006. if (type != video->type)
  1007. return -EINVAL;
  1008. mutex_lock(&video->stream_lock);
  1009. /* Make sure we're not streaming yet. */
  1010. mutex_lock(&video->queue_lock);
  1011. streaming = vb2_is_streaming(&vfh->queue);
  1012. mutex_unlock(&video->queue_lock);
  1013. if (!streaming)
  1014. goto done;
  1015. /* Update the pipeline state. */
  1016. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1017. state = ISP_PIPELINE_STREAM_OUTPUT
  1018. | ISP_PIPELINE_QUEUE_OUTPUT;
  1019. else
  1020. state = ISP_PIPELINE_STREAM_INPUT
  1021. | ISP_PIPELINE_QUEUE_INPUT;
  1022. spin_lock_irqsave(&pipe->lock, flags);
  1023. pipe->state &= ~state;
  1024. spin_unlock_irqrestore(&pipe->lock, flags);
  1025. /* Stop the stream. */
  1026. omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
  1027. omap3isp_video_cancel_stream(video);
  1028. mutex_lock(&video->queue_lock);
  1029. vb2_streamoff(&vfh->queue, type);
  1030. mutex_unlock(&video->queue_lock);
  1031. video->queue = NULL;
  1032. video->error = false;
  1033. /* TODO: Implement PM QoS */
  1034. media_pipeline_stop(&video->video.entity);
  1035. media_entity_enum_cleanup(&pipe->ent_enum);
  1036. done:
  1037. mutex_unlock(&video->stream_lock);
  1038. return 0;
  1039. }
  1040. static int
  1041. isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
  1042. {
  1043. if (input->index > 0)
  1044. return -EINVAL;
  1045. strlcpy(input->name, "camera", sizeof(input->name));
  1046. input->type = V4L2_INPUT_TYPE_CAMERA;
  1047. return 0;
  1048. }
  1049. static int
  1050. isp_video_g_input(struct file *file, void *fh, unsigned int *input)
  1051. {
  1052. *input = 0;
  1053. return 0;
  1054. }
  1055. static int
  1056. isp_video_s_input(struct file *file, void *fh, unsigned int input)
  1057. {
  1058. return input == 0 ? 0 : -EINVAL;
  1059. }
  1060. static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
  1061. .vidioc_querycap = isp_video_querycap,
  1062. .vidioc_g_fmt_vid_cap = isp_video_get_format,
  1063. .vidioc_s_fmt_vid_cap = isp_video_set_format,
  1064. .vidioc_try_fmt_vid_cap = isp_video_try_format,
  1065. .vidioc_g_fmt_vid_out = isp_video_get_format,
  1066. .vidioc_s_fmt_vid_out = isp_video_set_format,
  1067. .vidioc_try_fmt_vid_out = isp_video_try_format,
  1068. .vidioc_g_selection = isp_video_get_selection,
  1069. .vidioc_s_selection = isp_video_set_selection,
  1070. .vidioc_g_parm = isp_video_get_param,
  1071. .vidioc_s_parm = isp_video_set_param,
  1072. .vidioc_reqbufs = isp_video_reqbufs,
  1073. .vidioc_querybuf = isp_video_querybuf,
  1074. .vidioc_qbuf = isp_video_qbuf,
  1075. .vidioc_dqbuf = isp_video_dqbuf,
  1076. .vidioc_streamon = isp_video_streamon,
  1077. .vidioc_streamoff = isp_video_streamoff,
  1078. .vidioc_enum_input = isp_video_enum_input,
  1079. .vidioc_g_input = isp_video_g_input,
  1080. .vidioc_s_input = isp_video_s_input,
  1081. };
  1082. /* -----------------------------------------------------------------------------
  1083. * V4L2 file operations
  1084. */
  1085. static int isp_video_open(struct file *file)
  1086. {
  1087. struct isp_video *video = video_drvdata(file);
  1088. struct isp_video_fh *handle;
  1089. struct vb2_queue *queue;
  1090. int ret = 0;
  1091. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  1092. if (handle == NULL)
  1093. return -ENOMEM;
  1094. v4l2_fh_init(&handle->vfh, &video->video);
  1095. v4l2_fh_add(&handle->vfh);
  1096. /* If this is the first user, initialise the pipeline. */
  1097. if (omap3isp_get(video->isp) == NULL) {
  1098. ret = -EBUSY;
  1099. goto done;
  1100. }
  1101. ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
  1102. if (ret < 0) {
  1103. omap3isp_put(video->isp);
  1104. goto done;
  1105. }
  1106. queue = &handle->queue;
  1107. queue->type = video->type;
  1108. queue->io_modes = VB2_MMAP | VB2_USERPTR;
  1109. queue->drv_priv = handle;
  1110. queue->ops = &isp_video_queue_ops;
  1111. queue->mem_ops = &vb2_dma_contig_memops;
  1112. queue->buf_struct_size = sizeof(struct isp_buffer);
  1113. queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1114. queue->dev = video->isp->dev;
  1115. ret = vb2_queue_init(&handle->queue);
  1116. if (ret < 0) {
  1117. omap3isp_put(video->isp);
  1118. goto done;
  1119. }
  1120. memset(&handle->format, 0, sizeof(handle->format));
  1121. handle->format.type = video->type;
  1122. handle->timeperframe.denominator = 1;
  1123. handle->video = video;
  1124. file->private_data = &handle->vfh;
  1125. done:
  1126. if (ret < 0) {
  1127. v4l2_fh_del(&handle->vfh);
  1128. v4l2_fh_exit(&handle->vfh);
  1129. kfree(handle);
  1130. }
  1131. return ret;
  1132. }
  1133. static int isp_video_release(struct file *file)
  1134. {
  1135. struct isp_video *video = video_drvdata(file);
  1136. struct v4l2_fh *vfh = file->private_data;
  1137. struct isp_video_fh *handle = to_isp_video_fh(vfh);
  1138. /* Disable streaming and free the buffers queue resources. */
  1139. isp_video_streamoff(file, vfh, video->type);
  1140. mutex_lock(&video->queue_lock);
  1141. vb2_queue_release(&handle->queue);
  1142. mutex_unlock(&video->queue_lock);
  1143. v4l2_pipeline_pm_use(&video->video.entity, 0);
  1144. /* Release the file handle. */
  1145. v4l2_fh_del(vfh);
  1146. v4l2_fh_exit(vfh);
  1147. kfree(handle);
  1148. file->private_data = NULL;
  1149. omap3isp_put(video->isp);
  1150. return 0;
  1151. }
  1152. static __poll_t isp_video_poll(struct file *file, poll_table *wait)
  1153. {
  1154. struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
  1155. struct isp_video *video = video_drvdata(file);
  1156. __poll_t ret;
  1157. mutex_lock(&video->queue_lock);
  1158. ret = vb2_poll(&vfh->queue, file, wait);
  1159. mutex_unlock(&video->queue_lock);
  1160. return ret;
  1161. }
  1162. static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
  1163. {
  1164. struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
  1165. return vb2_mmap(&vfh->queue, vma);
  1166. }
  1167. static const struct v4l2_file_operations isp_video_fops = {
  1168. .owner = THIS_MODULE,
  1169. .unlocked_ioctl = video_ioctl2,
  1170. .open = isp_video_open,
  1171. .release = isp_video_release,
  1172. .poll = isp_video_poll,
  1173. .mmap = isp_video_mmap,
  1174. };
  1175. /* -----------------------------------------------------------------------------
  1176. * ISP video core
  1177. */
  1178. static const struct isp_video_operations isp_video_dummy_ops = {
  1179. };
  1180. int omap3isp_video_init(struct isp_video *video, const char *name)
  1181. {
  1182. const char *direction;
  1183. int ret;
  1184. switch (video->type) {
  1185. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1186. direction = "output";
  1187. video->pad.flags = MEDIA_PAD_FL_SINK
  1188. | MEDIA_PAD_FL_MUST_CONNECT;
  1189. break;
  1190. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  1191. direction = "input";
  1192. video->pad.flags = MEDIA_PAD_FL_SOURCE
  1193. | MEDIA_PAD_FL_MUST_CONNECT;
  1194. video->video.vfl_dir = VFL_DIR_TX;
  1195. break;
  1196. default:
  1197. return -EINVAL;
  1198. }
  1199. ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
  1200. if (ret < 0)
  1201. return ret;
  1202. mutex_init(&video->mutex);
  1203. atomic_set(&video->active, 0);
  1204. spin_lock_init(&video->pipe.lock);
  1205. mutex_init(&video->stream_lock);
  1206. mutex_init(&video->queue_lock);
  1207. spin_lock_init(&video->irqlock);
  1208. /* Initialize the video device. */
  1209. if (video->ops == NULL)
  1210. video->ops = &isp_video_dummy_ops;
  1211. video->video.fops = &isp_video_fops;
  1212. snprintf(video->video.name, sizeof(video->video.name),
  1213. "OMAP3 ISP %s %s", name, direction);
  1214. video->video.vfl_type = VFL_TYPE_GRABBER;
  1215. video->video.release = video_device_release_empty;
  1216. video->video.ioctl_ops = &isp_video_ioctl_ops;
  1217. video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
  1218. video_set_drvdata(&video->video, video);
  1219. return 0;
  1220. }
  1221. void omap3isp_video_cleanup(struct isp_video *video)
  1222. {
  1223. media_entity_cleanup(&video->video.entity);
  1224. mutex_destroy(&video->queue_lock);
  1225. mutex_destroy(&video->stream_lock);
  1226. mutex_destroy(&video->mutex);
  1227. }
  1228. int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
  1229. {
  1230. int ret;
  1231. video->video.v4l2_dev = vdev;
  1232. ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
  1233. if (ret < 0)
  1234. dev_err(video->isp->dev,
  1235. "%s: could not register video device (%d)\n",
  1236. __func__, ret);
  1237. return ret;
  1238. }
  1239. void omap3isp_video_unregister(struct isp_video *video)
  1240. {
  1241. if (video_is_registered(&video->video))
  1242. video_unregister_device(&video->video);
  1243. }