ispvideo.c 39 KB

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