ispvideo.c 39 KB

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