iss_ipipeif.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * TI OMAP4 ISS V4L2 Driver - ISP IPIPEIF module
  3. *
  4. * Copyright (C) 2012 Texas Instruments, Inc.
  5. *
  6. * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/mm.h>
  19. #include <linux/sched.h>
  20. #include "iss.h"
  21. #include "iss_regs.h"
  22. #include "iss_ipipeif.h"
  23. static const unsigned int ipipeif_fmts[] = {
  24. MEDIA_BUS_FMT_SGRBG10_1X10,
  25. MEDIA_BUS_FMT_SRGGB10_1X10,
  26. MEDIA_BUS_FMT_SBGGR10_1X10,
  27. MEDIA_BUS_FMT_SGBRG10_1X10,
  28. MEDIA_BUS_FMT_UYVY8_1X16,
  29. MEDIA_BUS_FMT_YUYV8_1X16,
  30. };
  31. /*
  32. * ipipeif_print_status - Print current IPIPEIF Module register values.
  33. * @ipipeif: Pointer to ISS ISP IPIPEIF device.
  34. *
  35. * Also prints other debug information stored in the IPIPEIF module.
  36. */
  37. #define IPIPEIF_PRINT_REGISTER(iss, name)\
  38. dev_dbg(iss->dev, "###IPIPEIF " #name "=0x%08x\n", \
  39. iss_reg_read(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_##name))
  40. #define ISIF_PRINT_REGISTER(iss, name)\
  41. dev_dbg(iss->dev, "###ISIF " #name "=0x%08x\n", \
  42. iss_reg_read(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_##name))
  43. #define ISP5_PRINT_REGISTER(iss, name)\
  44. dev_dbg(iss->dev, "###ISP5 " #name "=0x%08x\n", \
  45. iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_##name))
  46. static void ipipeif_print_status(struct iss_ipipeif_device *ipipeif)
  47. {
  48. struct iss_device *iss = to_iss_device(ipipeif);
  49. dev_dbg(iss->dev, "-------------IPIPEIF Register dump-------------\n");
  50. IPIPEIF_PRINT_REGISTER(iss, CFG1);
  51. IPIPEIF_PRINT_REGISTER(iss, CFG2);
  52. ISIF_PRINT_REGISTER(iss, SYNCEN);
  53. ISIF_PRINT_REGISTER(iss, CADU);
  54. ISIF_PRINT_REGISTER(iss, CADL);
  55. ISIF_PRINT_REGISTER(iss, MODESET);
  56. ISIF_PRINT_REGISTER(iss, CCOLP);
  57. ISIF_PRINT_REGISTER(iss, SPH);
  58. ISIF_PRINT_REGISTER(iss, LNH);
  59. ISIF_PRINT_REGISTER(iss, LNV);
  60. ISIF_PRINT_REGISTER(iss, VDINT(0));
  61. ISIF_PRINT_REGISTER(iss, HSIZE);
  62. ISP5_PRINT_REGISTER(iss, SYSCONFIG);
  63. ISP5_PRINT_REGISTER(iss, CTRL);
  64. ISP5_PRINT_REGISTER(iss, IRQSTATUS(0));
  65. ISP5_PRINT_REGISTER(iss, IRQENABLE_SET(0));
  66. ISP5_PRINT_REGISTER(iss, IRQENABLE_CLR(0));
  67. dev_dbg(iss->dev, "-----------------------------------------------\n");
  68. }
  69. static void ipipeif_write_enable(struct iss_ipipeif_device *ipipeif, u8 enable)
  70. {
  71. struct iss_device *iss = to_iss_device(ipipeif);
  72. iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_SYNCEN,
  73. ISIF_SYNCEN_DWEN, enable ? ISIF_SYNCEN_DWEN : 0);
  74. }
  75. /*
  76. * ipipeif_enable - Enable/Disable IPIPEIF.
  77. * @enable: enable flag
  78. *
  79. */
  80. static void ipipeif_enable(struct iss_ipipeif_device *ipipeif, u8 enable)
  81. {
  82. struct iss_device *iss = to_iss_device(ipipeif);
  83. iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_SYNCEN,
  84. ISIF_SYNCEN_SYEN, enable ? ISIF_SYNCEN_SYEN : 0);
  85. }
  86. /* -----------------------------------------------------------------------------
  87. * Format- and pipeline-related configuration helpers
  88. */
  89. /*
  90. * ipipeif_set_outaddr - Set memory address to save output image
  91. * @ipipeif: Pointer to ISP IPIPEIF device.
  92. * @addr: 32-bit memory address aligned on 32 byte boundary.
  93. *
  94. * Sets the memory address where the output will be saved.
  95. */
  96. static void ipipeif_set_outaddr(struct iss_ipipeif_device *ipipeif, u32 addr)
  97. {
  98. struct iss_device *iss = to_iss_device(ipipeif);
  99. /* Save address split in Base Address H & L */
  100. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CADU,
  101. (addr >> (16 + 5)) & ISIF_CADU_MASK);
  102. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CADL,
  103. (addr >> 5) & ISIF_CADL_MASK);
  104. }
  105. static void ipipeif_configure(struct iss_ipipeif_device *ipipeif)
  106. {
  107. struct iss_device *iss = to_iss_device(ipipeif);
  108. const struct iss_format_info *info;
  109. struct v4l2_mbus_framefmt *format;
  110. u32 isif_ccolp = 0;
  111. omap4iss_configure_bridge(iss, ipipeif->input);
  112. /* IPIPEIF_PAD_SINK */
  113. format = &ipipeif->formats[IPIPEIF_PAD_SINK];
  114. /* IPIPEIF with YUV422 input from ISIF */
  115. iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_CFG1,
  116. IPIPEIF_CFG1_INPSRC1_MASK | IPIPEIF_CFG1_INPSRC2_MASK);
  117. /* Select ISIF/IPIPEIF input format */
  118. switch (format->code) {
  119. case MEDIA_BUS_FMT_UYVY8_1X16:
  120. case MEDIA_BUS_FMT_YUYV8_1X16:
  121. iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_MODESET,
  122. ISIF_MODESET_CCDMD | ISIF_MODESET_INPMOD_MASK |
  123. ISIF_MODESET_CCDW_MASK,
  124. ISIF_MODESET_INPMOD_YCBCR16);
  125. iss_reg_update(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_CFG2,
  126. IPIPEIF_CFG2_YUV8, IPIPEIF_CFG2_YUV16);
  127. break;
  128. case MEDIA_BUS_FMT_SGRBG10_1X10:
  129. isif_ccolp = ISIF_CCOLP_CP0_F0_GR |
  130. ISIF_CCOLP_CP1_F0_R |
  131. ISIF_CCOLP_CP2_F0_B |
  132. ISIF_CCOLP_CP3_F0_GB;
  133. goto cont_raw;
  134. case MEDIA_BUS_FMT_SRGGB10_1X10:
  135. isif_ccolp = ISIF_CCOLP_CP0_F0_R |
  136. ISIF_CCOLP_CP1_F0_GR |
  137. ISIF_CCOLP_CP2_F0_GB |
  138. ISIF_CCOLP_CP3_F0_B;
  139. goto cont_raw;
  140. case MEDIA_BUS_FMT_SBGGR10_1X10:
  141. isif_ccolp = ISIF_CCOLP_CP0_F0_B |
  142. ISIF_CCOLP_CP1_F0_GB |
  143. ISIF_CCOLP_CP2_F0_GR |
  144. ISIF_CCOLP_CP3_F0_R;
  145. goto cont_raw;
  146. case MEDIA_BUS_FMT_SGBRG10_1X10:
  147. isif_ccolp = ISIF_CCOLP_CP0_F0_GB |
  148. ISIF_CCOLP_CP1_F0_B |
  149. ISIF_CCOLP_CP2_F0_R |
  150. ISIF_CCOLP_CP3_F0_GR;
  151. cont_raw:
  152. iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_CFG2,
  153. IPIPEIF_CFG2_YUV16);
  154. iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_MODESET,
  155. ISIF_MODESET_CCDMD | ISIF_MODESET_INPMOD_MASK |
  156. ISIF_MODESET_CCDW_MASK, ISIF_MODESET_INPMOD_RAW |
  157. ISIF_MODESET_CCDW_2BIT);
  158. info = omap4iss_video_format_info(format->code);
  159. iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CGAMMAWD,
  160. ISIF_CGAMMAWD_GWDI_MASK,
  161. ISIF_CGAMMAWD_GWDI(info->bpp));
  162. /* Set RAW Bayer pattern */
  163. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CCOLP,
  164. isif_ccolp);
  165. break;
  166. }
  167. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_SPH, 0 & ISIF_SPH_MASK);
  168. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_LNH,
  169. (format->width - 1) & ISIF_LNH_MASK);
  170. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_LNV,
  171. (format->height - 1) & ISIF_LNV_MASK);
  172. /* Generate ISIF0 on the last line of the image */
  173. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_VDINT(0),
  174. format->height - 1);
  175. /* IPIPEIF_PAD_SOURCE_ISIF_SF */
  176. format = &ipipeif->formats[IPIPEIF_PAD_SOURCE_ISIF_SF];
  177. iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_HSIZE,
  178. (ipipeif->video_out.bpl_value >> 5) &
  179. ISIF_HSIZE_HSIZE_MASK);
  180. /* IPIPEIF_PAD_SOURCE_VP */
  181. /* Do nothing? */
  182. }
  183. /* -----------------------------------------------------------------------------
  184. * Interrupt handling
  185. */
  186. static void ipipeif_isr_buffer(struct iss_ipipeif_device *ipipeif)
  187. {
  188. struct iss_buffer *buffer;
  189. /* The ISIF generates VD0 interrupts even when writes are disabled.
  190. * deal with it anyway). Disabling the ISIF when no buffer is available
  191. * is thus not be enough, we need to handle the situation explicitly.
  192. */
  193. if (list_empty(&ipipeif->video_out.dmaqueue))
  194. return;
  195. ipipeif_write_enable(ipipeif, 0);
  196. buffer = omap4iss_video_buffer_next(&ipipeif->video_out);
  197. if (!buffer)
  198. return;
  199. ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
  200. ipipeif_write_enable(ipipeif, 1);
  201. }
  202. /*
  203. * omap4iss_ipipeif_isr - Configure ipipeif during interframe time.
  204. * @ipipeif: Pointer to ISP IPIPEIF device.
  205. * @events: IPIPEIF events
  206. */
  207. void omap4iss_ipipeif_isr(struct iss_ipipeif_device *ipipeif, u32 events)
  208. {
  209. if (omap4iss_module_sync_is_stopping(&ipipeif->wait,
  210. &ipipeif->stopping))
  211. return;
  212. if ((events & ISP5_IRQ_ISIF_INT(0)) &&
  213. (ipipeif->output & IPIPEIF_OUTPUT_MEMORY))
  214. ipipeif_isr_buffer(ipipeif);
  215. }
  216. /* -----------------------------------------------------------------------------
  217. * ISP video operations
  218. */
  219. static int ipipeif_video_queue(struct iss_video *video,
  220. struct iss_buffer *buffer)
  221. {
  222. struct iss_ipipeif_device *ipipeif = container_of(video,
  223. struct iss_ipipeif_device, video_out);
  224. if (!(ipipeif->output & IPIPEIF_OUTPUT_MEMORY))
  225. return -ENODEV;
  226. ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
  227. /*
  228. * If streaming was enabled before there was a buffer queued
  229. * or underrun happened in the ISR, the hardware was not enabled
  230. * and DMA queue flag ISS_VIDEO_DMAQUEUE_UNDERRUN is still set.
  231. * Enable it now.
  232. */
  233. if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
  234. if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
  235. ipipeif_write_enable(ipipeif, 1);
  236. ipipeif_enable(ipipeif, 1);
  237. iss_video_dmaqueue_flags_clr(video);
  238. }
  239. return 0;
  240. }
  241. static const struct iss_video_operations ipipeif_video_ops = {
  242. .queue = ipipeif_video_queue,
  243. };
  244. /* -----------------------------------------------------------------------------
  245. * V4L2 subdev operations
  246. */
  247. #define IPIPEIF_DRV_SUBCLK_MASK (OMAP4_ISS_ISP_SUBCLK_IPIPEIF |\
  248. OMAP4_ISS_ISP_SUBCLK_ISIF)
  249. /*
  250. * ipipeif_set_stream - Enable/Disable streaming on the IPIPEIF module
  251. * @sd: ISP IPIPEIF V4L2 subdevice
  252. * @enable: Enable/disable stream
  253. */
  254. static int ipipeif_set_stream(struct v4l2_subdev *sd, int enable)
  255. {
  256. struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
  257. struct iss_device *iss = to_iss_device(ipipeif);
  258. struct iss_video *video_out = &ipipeif->video_out;
  259. int ret = 0;
  260. if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED) {
  261. if (enable == ISS_PIPELINE_STREAM_STOPPED)
  262. return 0;
  263. omap4iss_isp_subclk_enable(iss, IPIPEIF_DRV_SUBCLK_MASK);
  264. }
  265. switch (enable) {
  266. case ISS_PIPELINE_STREAM_CONTINUOUS:
  267. ipipeif_configure(ipipeif);
  268. ipipeif_print_status(ipipeif);
  269. /*
  270. * When outputting to memory with no buffer available, let the
  271. * buffer queue handler start the hardware. A DMA queue flag
  272. * ISS_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
  273. * a buffer available.
  274. */
  275. if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY &&
  276. !(video_out->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_QUEUED))
  277. break;
  278. atomic_set(&ipipeif->stopping, 0);
  279. if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
  280. ipipeif_write_enable(ipipeif, 1);
  281. ipipeif_enable(ipipeif, 1);
  282. iss_video_dmaqueue_flags_clr(video_out);
  283. break;
  284. case ISS_PIPELINE_STREAM_STOPPED:
  285. if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED)
  286. return 0;
  287. if (omap4iss_module_sync_idle(&sd->entity, &ipipeif->wait,
  288. &ipipeif->stopping))
  289. ret = -ETIMEDOUT;
  290. if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
  291. ipipeif_write_enable(ipipeif, 0);
  292. ipipeif_enable(ipipeif, 0);
  293. omap4iss_isp_subclk_disable(iss, IPIPEIF_DRV_SUBCLK_MASK);
  294. iss_video_dmaqueue_flags_clr(video_out);
  295. break;
  296. }
  297. ipipeif->state = enable;
  298. return ret;
  299. }
  300. static struct v4l2_mbus_framefmt *
  301. __ipipeif_get_format(struct iss_ipipeif_device *ipipeif,
  302. struct v4l2_subdev_pad_config *cfg, unsigned int pad,
  303. enum v4l2_subdev_format_whence which)
  304. {
  305. if (which == V4L2_SUBDEV_FORMAT_TRY)
  306. return v4l2_subdev_get_try_format(&ipipeif->subdev, cfg, pad);
  307. return &ipipeif->formats[pad];
  308. }
  309. /*
  310. * ipipeif_try_format - Try video format on a pad
  311. * @ipipeif: ISS IPIPEIF device
  312. * @cfg: V4L2 subdev pad config
  313. * @pad: Pad number
  314. * @fmt: Format
  315. */
  316. static void
  317. ipipeif_try_format(struct iss_ipipeif_device *ipipeif,
  318. struct v4l2_subdev_pad_config *cfg, unsigned int pad,
  319. struct v4l2_mbus_framefmt *fmt,
  320. enum v4l2_subdev_format_whence which)
  321. {
  322. struct v4l2_mbus_framefmt *format;
  323. unsigned int width = fmt->width;
  324. unsigned int height = fmt->height;
  325. unsigned int i;
  326. switch (pad) {
  327. case IPIPEIF_PAD_SINK:
  328. /* TODO: If the IPIPEIF output formatter pad is connected
  329. * directly to the resizer, only YUV formats can be used.
  330. */
  331. for (i = 0; i < ARRAY_SIZE(ipipeif_fmts); i++) {
  332. if (fmt->code == ipipeif_fmts[i])
  333. break;
  334. }
  335. /* If not found, use SGRBG10 as default */
  336. if (i >= ARRAY_SIZE(ipipeif_fmts))
  337. fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  338. /* Clamp the input size. */
  339. fmt->width = clamp_t(u32, width, 1, 8192);
  340. fmt->height = clamp_t(u32, height, 1, 8192);
  341. break;
  342. case IPIPEIF_PAD_SOURCE_ISIF_SF:
  343. format = __ipipeif_get_format(ipipeif, cfg, IPIPEIF_PAD_SINK,
  344. which);
  345. memcpy(fmt, format, sizeof(*fmt));
  346. /* The data formatter truncates the number of horizontal output
  347. * pixels to a multiple of 16. To avoid clipping data, allow
  348. * callers to request an output size bigger than the input size
  349. * up to the nearest multiple of 16.
  350. */
  351. fmt->width = clamp_t(u32, width, 32, (fmt->width + 15) & ~15);
  352. fmt->width &= ~15;
  353. fmt->height = clamp_t(u32, height, 32, fmt->height);
  354. break;
  355. case IPIPEIF_PAD_SOURCE_VP:
  356. format = __ipipeif_get_format(ipipeif, cfg, IPIPEIF_PAD_SINK,
  357. which);
  358. memcpy(fmt, format, sizeof(*fmt));
  359. fmt->width = clamp_t(u32, width, 32, fmt->width);
  360. fmt->height = clamp_t(u32, height, 32, fmt->height);
  361. break;
  362. }
  363. /* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
  364. * stored on 2 bytes.
  365. */
  366. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  367. fmt->field = V4L2_FIELD_NONE;
  368. }
  369. /*
  370. * ipipeif_enum_mbus_code - Handle pixel format enumeration
  371. * @sd : pointer to v4l2 subdev structure
  372. * @cfg : V4L2 subdev pad config
  373. * @code : pointer to v4l2_subdev_mbus_code_enum structure
  374. * return -EINVAL or zero on success
  375. */
  376. static int ipipeif_enum_mbus_code(struct v4l2_subdev *sd,
  377. struct v4l2_subdev_pad_config *cfg,
  378. struct v4l2_subdev_mbus_code_enum *code)
  379. {
  380. struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
  381. struct v4l2_mbus_framefmt *format;
  382. switch (code->pad) {
  383. case IPIPEIF_PAD_SINK:
  384. if (code->index >= ARRAY_SIZE(ipipeif_fmts))
  385. return -EINVAL;
  386. code->code = ipipeif_fmts[code->index];
  387. break;
  388. case IPIPEIF_PAD_SOURCE_ISIF_SF:
  389. case IPIPEIF_PAD_SOURCE_VP:
  390. /* No format conversion inside IPIPEIF */
  391. if (code->index != 0)
  392. return -EINVAL;
  393. format = __ipipeif_get_format(ipipeif, cfg, IPIPEIF_PAD_SINK,
  394. code->which);
  395. code->code = format->code;
  396. break;
  397. default:
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. static int ipipeif_enum_frame_size(struct v4l2_subdev *sd,
  403. struct v4l2_subdev_pad_config *cfg,
  404. struct v4l2_subdev_frame_size_enum *fse)
  405. {
  406. struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
  407. struct v4l2_mbus_framefmt format;
  408. if (fse->index != 0)
  409. return -EINVAL;
  410. format.code = fse->code;
  411. format.width = 1;
  412. format.height = 1;
  413. ipipeif_try_format(ipipeif, cfg, fse->pad, &format, fse->which);
  414. fse->min_width = format.width;
  415. fse->min_height = format.height;
  416. if (format.code != fse->code)
  417. return -EINVAL;
  418. format.code = fse->code;
  419. format.width = -1;
  420. format.height = -1;
  421. ipipeif_try_format(ipipeif, cfg, fse->pad, &format, fse->which);
  422. fse->max_width = format.width;
  423. fse->max_height = format.height;
  424. return 0;
  425. }
  426. /*
  427. * ipipeif_get_format - Retrieve the video format on a pad
  428. * @sd : ISP IPIPEIF V4L2 subdevice
  429. * @cfg: V4L2 subdev pad config
  430. * @fmt: Format
  431. *
  432. * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
  433. * to the format type.
  434. */
  435. static int ipipeif_get_format(struct v4l2_subdev *sd,
  436. struct v4l2_subdev_pad_config *cfg,
  437. struct v4l2_subdev_format *fmt)
  438. {
  439. struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
  440. struct v4l2_mbus_framefmt *format;
  441. format = __ipipeif_get_format(ipipeif, cfg, fmt->pad, fmt->which);
  442. if (!format)
  443. return -EINVAL;
  444. fmt->format = *format;
  445. return 0;
  446. }
  447. /*
  448. * ipipeif_set_format - Set the video format on a pad
  449. * @sd : ISP IPIPEIF V4L2 subdevice
  450. * @cfg: V4L2 subdev pad config
  451. * @fmt: Format
  452. *
  453. * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
  454. * to the format type.
  455. */
  456. static int ipipeif_set_format(struct v4l2_subdev *sd,
  457. struct v4l2_subdev_pad_config *cfg,
  458. struct v4l2_subdev_format *fmt)
  459. {
  460. struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
  461. struct v4l2_mbus_framefmt *format;
  462. format = __ipipeif_get_format(ipipeif, cfg, fmt->pad, fmt->which);
  463. if (!format)
  464. return -EINVAL;
  465. ipipeif_try_format(ipipeif, cfg, fmt->pad, &fmt->format, fmt->which);
  466. *format = fmt->format;
  467. /* Propagate the format from sink to source */
  468. if (fmt->pad == IPIPEIF_PAD_SINK) {
  469. format = __ipipeif_get_format(ipipeif, cfg,
  470. IPIPEIF_PAD_SOURCE_ISIF_SF,
  471. fmt->which);
  472. *format = fmt->format;
  473. ipipeif_try_format(ipipeif, cfg, IPIPEIF_PAD_SOURCE_ISIF_SF,
  474. format, fmt->which);
  475. format = __ipipeif_get_format(ipipeif, cfg,
  476. IPIPEIF_PAD_SOURCE_VP,
  477. fmt->which);
  478. *format = fmt->format;
  479. ipipeif_try_format(ipipeif, cfg, IPIPEIF_PAD_SOURCE_VP, format,
  480. fmt->which);
  481. }
  482. return 0;
  483. }
  484. static int ipipeif_link_validate(struct v4l2_subdev *sd,
  485. struct media_link *link,
  486. struct v4l2_subdev_format *source_fmt,
  487. struct v4l2_subdev_format *sink_fmt)
  488. {
  489. /* Check if the two ends match */
  490. if (source_fmt->format.width != sink_fmt->format.width ||
  491. source_fmt->format.height != sink_fmt->format.height)
  492. return -EPIPE;
  493. if (source_fmt->format.code != sink_fmt->format.code)
  494. return -EPIPE;
  495. return 0;
  496. }
  497. /*
  498. * ipipeif_init_formats - Initialize formats on all pads
  499. * @sd: ISP IPIPEIF V4L2 subdevice
  500. * @fh: V4L2 subdev file handle
  501. *
  502. * Initialize all pad formats with default values. If fh is not NULL, try
  503. * formats are initialized on the file handle. Otherwise active formats are
  504. * initialized on the device.
  505. */
  506. static int ipipeif_init_formats(struct v4l2_subdev *sd,
  507. struct v4l2_subdev_fh *fh)
  508. {
  509. struct v4l2_subdev_format format;
  510. memset(&format, 0, sizeof(format));
  511. format.pad = IPIPEIF_PAD_SINK;
  512. format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  513. format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  514. format.format.width = 4096;
  515. format.format.height = 4096;
  516. ipipeif_set_format(sd, fh ? fh->pad : NULL, &format);
  517. return 0;
  518. }
  519. /* V4L2 subdev video operations */
  520. static const struct v4l2_subdev_video_ops ipipeif_v4l2_video_ops = {
  521. .s_stream = ipipeif_set_stream,
  522. };
  523. /* V4L2 subdev pad operations */
  524. static const struct v4l2_subdev_pad_ops ipipeif_v4l2_pad_ops = {
  525. .enum_mbus_code = ipipeif_enum_mbus_code,
  526. .enum_frame_size = ipipeif_enum_frame_size,
  527. .get_fmt = ipipeif_get_format,
  528. .set_fmt = ipipeif_set_format,
  529. .link_validate = ipipeif_link_validate,
  530. };
  531. /* V4L2 subdev operations */
  532. static const struct v4l2_subdev_ops ipipeif_v4l2_ops = {
  533. .video = &ipipeif_v4l2_video_ops,
  534. .pad = &ipipeif_v4l2_pad_ops,
  535. };
  536. /* V4L2 subdev internal operations */
  537. static const struct v4l2_subdev_internal_ops ipipeif_v4l2_internal_ops = {
  538. .open = ipipeif_init_formats,
  539. };
  540. /* -----------------------------------------------------------------------------
  541. * Media entity operations
  542. */
  543. /*
  544. * ipipeif_link_setup - Setup IPIPEIF connections
  545. * @entity: IPIPEIF media entity
  546. * @local: Pad at the local end of the link
  547. * @remote: Pad at the remote end of the link
  548. * @flags: Link flags
  549. *
  550. * return -EINVAL or zero on success
  551. */
  552. static int ipipeif_link_setup(struct media_entity *entity,
  553. const struct media_pad *local,
  554. const struct media_pad *remote, u32 flags)
  555. {
  556. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  557. struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
  558. struct iss_device *iss = to_iss_device(ipipeif);
  559. switch (local->index | media_entity_type(remote->entity)) {
  560. case IPIPEIF_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
  561. /* Read from the sensor CSI2a or CSI2b. */
  562. if (!(flags & MEDIA_LNK_FL_ENABLED)) {
  563. ipipeif->input = IPIPEIF_INPUT_NONE;
  564. break;
  565. }
  566. if (ipipeif->input != IPIPEIF_INPUT_NONE)
  567. return -EBUSY;
  568. if (remote->entity == &iss->csi2a.subdev.entity)
  569. ipipeif->input = IPIPEIF_INPUT_CSI2A;
  570. else if (remote->entity == &iss->csi2b.subdev.entity)
  571. ipipeif->input = IPIPEIF_INPUT_CSI2B;
  572. break;
  573. case IPIPEIF_PAD_SOURCE_ISIF_SF | MEDIA_ENT_T_DEVNODE:
  574. /* Write to memory */
  575. if (flags & MEDIA_LNK_FL_ENABLED) {
  576. if (ipipeif->output & ~IPIPEIF_OUTPUT_MEMORY)
  577. return -EBUSY;
  578. ipipeif->output |= IPIPEIF_OUTPUT_MEMORY;
  579. } else {
  580. ipipeif->output &= ~IPIPEIF_OUTPUT_MEMORY;
  581. }
  582. break;
  583. case IPIPEIF_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
  584. /* Send to IPIPE/RESIZER */
  585. if (flags & MEDIA_LNK_FL_ENABLED) {
  586. if (ipipeif->output & ~IPIPEIF_OUTPUT_VP)
  587. return -EBUSY;
  588. ipipeif->output |= IPIPEIF_OUTPUT_VP;
  589. } else {
  590. ipipeif->output &= ~IPIPEIF_OUTPUT_VP;
  591. }
  592. break;
  593. default:
  594. return -EINVAL;
  595. }
  596. return 0;
  597. }
  598. /* media operations */
  599. static const struct media_entity_operations ipipeif_media_ops = {
  600. .link_setup = ipipeif_link_setup,
  601. .link_validate = v4l2_subdev_link_validate,
  602. };
  603. /*
  604. * ipipeif_init_entities - Initialize V4L2 subdev and media entity
  605. * @ipipeif: ISS ISP IPIPEIF module
  606. *
  607. * Return 0 on success and a negative error code on failure.
  608. */
  609. static int ipipeif_init_entities(struct iss_ipipeif_device *ipipeif)
  610. {
  611. struct v4l2_subdev *sd = &ipipeif->subdev;
  612. struct media_pad *pads = ipipeif->pads;
  613. struct media_entity *me = &sd->entity;
  614. int ret;
  615. ipipeif->input = IPIPEIF_INPUT_NONE;
  616. v4l2_subdev_init(sd, &ipipeif_v4l2_ops);
  617. sd->internal_ops = &ipipeif_v4l2_internal_ops;
  618. strlcpy(sd->name, "OMAP4 ISS ISP IPIPEIF", sizeof(sd->name));
  619. sd->grp_id = 1 << 16; /* group ID for iss subdevs */
  620. v4l2_set_subdevdata(sd, ipipeif);
  621. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  622. pads[IPIPEIF_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  623. pads[IPIPEIF_PAD_SOURCE_ISIF_SF].flags = MEDIA_PAD_FL_SOURCE;
  624. pads[IPIPEIF_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
  625. me->ops = &ipipeif_media_ops;
  626. ret = media_entity_init(me, IPIPEIF_PADS_NUM, pads, 0);
  627. if (ret < 0)
  628. return ret;
  629. ipipeif_init_formats(sd, NULL);
  630. ipipeif->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  631. ipipeif->video_out.ops = &ipipeif_video_ops;
  632. ipipeif->video_out.iss = to_iss_device(ipipeif);
  633. ipipeif->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
  634. ipipeif->video_out.bpl_alignment = 32;
  635. ipipeif->video_out.bpl_zero_padding = 1;
  636. ipipeif->video_out.bpl_max = 0x1ffe0;
  637. ret = omap4iss_video_init(&ipipeif->video_out, "ISP IPIPEIF");
  638. if (ret < 0)
  639. return ret;
  640. /* Connect the IPIPEIF subdev to the video node. */
  641. ret = media_entity_create_link(&ipipeif->subdev.entity,
  642. IPIPEIF_PAD_SOURCE_ISIF_SF,
  643. &ipipeif->video_out.video.entity, 0, 0);
  644. if (ret < 0)
  645. return ret;
  646. return 0;
  647. }
  648. void omap4iss_ipipeif_unregister_entities(struct iss_ipipeif_device *ipipeif)
  649. {
  650. v4l2_device_unregister_subdev(&ipipeif->subdev);
  651. omap4iss_video_unregister(&ipipeif->video_out);
  652. }
  653. int omap4iss_ipipeif_register_entities(struct iss_ipipeif_device *ipipeif,
  654. struct v4l2_device *vdev)
  655. {
  656. int ret;
  657. /* Register the subdev and video node. */
  658. ret = v4l2_device_register_subdev(vdev, &ipipeif->subdev);
  659. if (ret < 0)
  660. goto error;
  661. ret = omap4iss_video_register(&ipipeif->video_out, vdev);
  662. if (ret < 0)
  663. goto error;
  664. return 0;
  665. error:
  666. omap4iss_ipipeif_unregister_entities(ipipeif);
  667. return ret;
  668. }
  669. /* -----------------------------------------------------------------------------
  670. * ISP IPIPEIF initialisation and cleanup
  671. */
  672. /*
  673. * omap4iss_ipipeif_init - IPIPEIF module initialization.
  674. * @iss: Device pointer specific to the OMAP4 ISS.
  675. *
  676. * TODO: Get the initialisation values from platform data.
  677. *
  678. * Return 0 on success or a negative error code otherwise.
  679. */
  680. int omap4iss_ipipeif_init(struct iss_device *iss)
  681. {
  682. struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
  683. ipipeif->state = ISS_PIPELINE_STREAM_STOPPED;
  684. init_waitqueue_head(&ipipeif->wait);
  685. return ipipeif_init_entities(ipipeif);
  686. }
  687. /*
  688. * omap4iss_ipipeif_cleanup - IPIPEIF module cleanup.
  689. * @iss: Device pointer specific to the OMAP4 ISS.
  690. */
  691. void omap4iss_ipipeif_cleanup(struct iss_device *iss)
  692. {
  693. struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
  694. media_entity_cleanup(&ipipeif->subdev.entity);
  695. }