ispvideo.c 39 KB

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