ispvideo.c 37 KB

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