ispvideo.c 39 KB

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