ispvideo.c 39 KB

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