ispvideo.c 37 KB

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