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