ispvideo.c 39 KB

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