fimc-capture.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
  3. *
  4. * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
  5. * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/bug.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/list.h>
  20. #include <linux/slab.h>
  21. #include <linux/videodev2.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-mem2mem.h>
  25. #include <media/videobuf2-core.h>
  26. #include <media/videobuf2-dma-contig.h>
  27. #include "fimc-mdevice.h"
  28. #include "fimc-core.h"
  29. static int fimc_init_capture(struct fimc_dev *fimc)
  30. {
  31. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  32. struct fimc_sensor_info *sensor;
  33. unsigned long flags;
  34. int ret = 0;
  35. if (fimc->pipeline.sensor == NULL || ctx == NULL)
  36. return -ENXIO;
  37. if (ctx->s_frame.fmt == NULL)
  38. return -EINVAL;
  39. sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
  40. spin_lock_irqsave(&fimc->slock, flags);
  41. fimc_prepare_dma_offset(ctx, &ctx->d_frame);
  42. fimc_set_yuv_order(ctx);
  43. fimc_hw_set_camera_polarity(fimc, sensor->pdata);
  44. fimc_hw_set_camera_type(fimc, sensor->pdata);
  45. fimc_hw_set_camera_source(fimc, sensor->pdata);
  46. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  47. ret = fimc_set_scaler_info(ctx);
  48. if (!ret) {
  49. fimc_hw_set_input_path(ctx);
  50. fimc_hw_set_prescaler(ctx);
  51. fimc_hw_set_mainscaler(ctx);
  52. fimc_hw_set_target_format(ctx);
  53. fimc_hw_set_rotation(ctx);
  54. fimc_hw_set_effect(ctx, false);
  55. fimc_hw_set_output_path(ctx);
  56. fimc_hw_set_out_dma(ctx);
  57. clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  58. }
  59. spin_unlock_irqrestore(&fimc->slock, flags);
  60. return ret;
  61. }
  62. static int fimc_capture_state_cleanup(struct fimc_dev *fimc)
  63. {
  64. struct fimc_vid_cap *cap = &fimc->vid_cap;
  65. struct fimc_vid_buffer *buf;
  66. unsigned long flags;
  67. spin_lock_irqsave(&fimc->slock, flags);
  68. fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
  69. 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM |
  70. 1 << ST_CAPT_ISP_STREAM);
  71. fimc->vid_cap.active_buf_cnt = 0;
  72. /* Release buffers that were enqueued in the driver by videobuf2. */
  73. while (!list_empty(&cap->pending_buf_q)) {
  74. buf = pending_queue_pop(cap);
  75. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  76. }
  77. while (!list_empty(&cap->active_buf_q)) {
  78. buf = active_queue_pop(cap);
  79. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  80. }
  81. spin_unlock_irqrestore(&fimc->slock, flags);
  82. if (test_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  83. return fimc_pipeline_s_stream(fimc, 0);
  84. else
  85. return 0;
  86. }
  87. static int fimc_stop_capture(struct fimc_dev *fimc)
  88. {
  89. struct fimc_vid_cap *cap = &fimc->vid_cap;
  90. unsigned long flags;
  91. if (!fimc_capture_active(fimc))
  92. return 0;
  93. spin_lock_irqsave(&fimc->slock, flags);
  94. set_bit(ST_CAPT_SHUT, &fimc->state);
  95. fimc_deactivate_capture(fimc);
  96. spin_unlock_irqrestore(&fimc->slock, flags);
  97. wait_event_timeout(fimc->irq_queue,
  98. !test_bit(ST_CAPT_SHUT, &fimc->state),
  99. FIMC_SHUTDOWN_TIMEOUT);
  100. return fimc_capture_state_cleanup(fimc);
  101. }
  102. /**
  103. * fimc_capture_config_update - apply the camera interface configuration
  104. *
  105. * To be called from within the interrupt handler with fimc.slock
  106. * spinlock held. It updates the camera pixel crop, rotation and
  107. * image flip in H/W.
  108. */
  109. int fimc_capture_config_update(struct fimc_ctx *ctx)
  110. {
  111. struct fimc_dev *fimc = ctx->fimc_dev;
  112. int ret;
  113. if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
  114. return 0;
  115. spin_lock(&ctx->slock);
  116. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  117. ret = fimc_set_scaler_info(ctx);
  118. if (ret == 0) {
  119. fimc_hw_set_prescaler(ctx);
  120. fimc_hw_set_mainscaler(ctx);
  121. fimc_hw_set_target_format(ctx);
  122. fimc_hw_set_rotation(ctx);
  123. fimc_prepare_dma_offset(ctx, &ctx->d_frame);
  124. fimc_hw_set_out_dma(ctx);
  125. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  126. }
  127. spin_unlock(&ctx->slock);
  128. return ret;
  129. }
  130. static int start_streaming(struct vb2_queue *q, unsigned int count)
  131. {
  132. struct fimc_ctx *ctx = q->drv_priv;
  133. struct fimc_dev *fimc = ctx->fimc_dev;
  134. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  135. int min_bufs;
  136. int ret;
  137. fimc_hw_reset(fimc);
  138. vid_cap->frame_count = 0;
  139. ret = fimc_init_capture(fimc);
  140. if (ret)
  141. goto error;
  142. set_bit(ST_CAPT_PEND, &fimc->state);
  143. min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
  144. if (vid_cap->active_buf_cnt >= min_bufs &&
  145. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
  146. fimc_activate_capture(ctx);
  147. if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  148. fimc_pipeline_s_stream(fimc, 1);
  149. }
  150. return 0;
  151. error:
  152. fimc_capture_state_cleanup(fimc);
  153. return ret;
  154. }
  155. static int stop_streaming(struct vb2_queue *q)
  156. {
  157. struct fimc_ctx *ctx = q->drv_priv;
  158. struct fimc_dev *fimc = ctx->fimc_dev;
  159. if (!fimc_capture_active(fimc))
  160. return -EINVAL;
  161. return fimc_stop_capture(fimc);
  162. }
  163. int fimc_capture_suspend(struct fimc_dev *fimc)
  164. {
  165. return -EBUSY;
  166. }
  167. int fimc_capture_resume(struct fimc_dev *fimc)
  168. {
  169. return 0;
  170. }
  171. static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
  172. {
  173. if (!fr || plane >= fr->fmt->memplanes)
  174. return 0;
  175. return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
  176. }
  177. static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
  178. unsigned int *num_planes, unsigned int sizes[],
  179. void *allocators[])
  180. {
  181. struct fimc_ctx *ctx = vq->drv_priv;
  182. struct fimc_fmt *fmt = ctx->d_frame.fmt;
  183. int i;
  184. if (!fmt)
  185. return -EINVAL;
  186. *num_planes = fmt->memplanes;
  187. for (i = 0; i < fmt->memplanes; i++) {
  188. sizes[i] = get_plane_size(&ctx->d_frame, i);
  189. allocators[i] = ctx->fimc_dev->alloc_ctx;
  190. }
  191. return 0;
  192. }
  193. static int buffer_prepare(struct vb2_buffer *vb)
  194. {
  195. struct vb2_queue *vq = vb->vb2_queue;
  196. struct fimc_ctx *ctx = vq->drv_priv;
  197. int i;
  198. if (ctx->d_frame.fmt == NULL)
  199. return -EINVAL;
  200. for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
  201. unsigned long size = ctx->d_frame.payload[i];
  202. if (vb2_plane_size(vb, i) < size) {
  203. v4l2_err(ctx->fimc_dev->vid_cap.vfd,
  204. "User buffer too small (%ld < %ld)\n",
  205. vb2_plane_size(vb, i), size);
  206. return -EINVAL;
  207. }
  208. vb2_set_plane_payload(vb, i, size);
  209. }
  210. return 0;
  211. }
  212. static void buffer_queue(struct vb2_buffer *vb)
  213. {
  214. struct fimc_vid_buffer *buf
  215. = container_of(vb, struct fimc_vid_buffer, vb);
  216. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  217. struct fimc_dev *fimc = ctx->fimc_dev;
  218. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  219. unsigned long flags;
  220. int min_bufs;
  221. spin_lock_irqsave(&fimc->slock, flags);
  222. fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
  223. if (!test_bit(ST_CAPT_STREAM, &fimc->state)
  224. && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
  225. /* Setup the buffer directly for processing. */
  226. int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
  227. vid_cap->buf_index;
  228. fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
  229. buf->index = vid_cap->buf_index;
  230. active_queue_add(vid_cap, buf);
  231. if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
  232. vid_cap->buf_index = 0;
  233. } else {
  234. fimc_pending_queue_add(vid_cap, buf);
  235. }
  236. min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
  237. if (vb2_is_streaming(&vid_cap->vbq) &&
  238. vid_cap->active_buf_cnt >= min_bufs &&
  239. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
  240. fimc_activate_capture(ctx);
  241. spin_unlock_irqrestore(&fimc->slock, flags);
  242. if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  243. fimc_pipeline_s_stream(fimc, 1);
  244. return;
  245. }
  246. spin_unlock_irqrestore(&fimc->slock, flags);
  247. }
  248. static void fimc_lock(struct vb2_queue *vq)
  249. {
  250. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  251. mutex_lock(&ctx->fimc_dev->lock);
  252. }
  253. static void fimc_unlock(struct vb2_queue *vq)
  254. {
  255. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  256. mutex_unlock(&ctx->fimc_dev->lock);
  257. }
  258. static struct vb2_ops fimc_capture_qops = {
  259. .queue_setup = queue_setup,
  260. .buf_prepare = buffer_prepare,
  261. .buf_queue = buffer_queue,
  262. .wait_prepare = fimc_unlock,
  263. .wait_finish = fimc_lock,
  264. .start_streaming = start_streaming,
  265. .stop_streaming = stop_streaming,
  266. };
  267. /**
  268. * fimc_capture_ctrls_create - initialize the control handler
  269. * Initialize the capture video node control handler and fill it
  270. * with the FIMC controls. Inherit any sensor's controls if the
  271. * 'user_subdev_api' flag is false (default behaviour).
  272. * This function need to be called with the graph mutex held.
  273. */
  274. int fimc_capture_ctrls_create(struct fimc_dev *fimc)
  275. {
  276. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  277. int ret;
  278. if (WARN_ON(vid_cap->ctx == NULL))
  279. return -ENXIO;
  280. if (vid_cap->ctx->ctrls_rdy)
  281. return 0;
  282. ret = fimc_ctrls_create(vid_cap->ctx);
  283. if (ret || vid_cap->user_subdev_api)
  284. return ret;
  285. return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
  286. fimc->pipeline.sensor->ctrl_handler);
  287. }
  288. static int fimc_capture_set_default_format(struct fimc_dev *fimc);
  289. static int fimc_capture_open(struct file *file)
  290. {
  291. struct fimc_dev *fimc = video_drvdata(file);
  292. int ret = v4l2_fh_open(file);
  293. if (ret)
  294. return ret;
  295. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  296. /* Return if the corresponding video mem2mem node is already opened. */
  297. if (fimc_m2m_active(fimc))
  298. return -EBUSY;
  299. pm_runtime_get_sync(&fimc->pdev->dev);
  300. if (++fimc->vid_cap.refcnt == 1) {
  301. ret = fimc_pipeline_initialize(fimc,
  302. &fimc->vid_cap.vfd->entity, true);
  303. if (ret < 0) {
  304. dev_err(&fimc->pdev->dev,
  305. "Video pipeline initialization failed\n");
  306. pm_runtime_put_sync(&fimc->pdev->dev);
  307. fimc->vid_cap.refcnt--;
  308. v4l2_fh_release(file);
  309. return ret;
  310. }
  311. ret = fimc_capture_ctrls_create(fimc);
  312. if (!ret && !fimc->vid_cap.user_subdev_api)
  313. ret = fimc_capture_set_default_format(fimc);
  314. }
  315. return ret;
  316. }
  317. static int fimc_capture_close(struct file *file)
  318. {
  319. struct fimc_dev *fimc = video_drvdata(file);
  320. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  321. if (--fimc->vid_cap.refcnt == 0) {
  322. fimc_stop_capture(fimc);
  323. fimc_pipeline_shutdown(fimc);
  324. fimc_ctrls_delete(fimc->vid_cap.ctx);
  325. vb2_queue_release(&fimc->vid_cap.vbq);
  326. }
  327. pm_runtime_put(&fimc->pdev->dev);
  328. return v4l2_fh_release(file);
  329. }
  330. static unsigned int fimc_capture_poll(struct file *file,
  331. struct poll_table_struct *wait)
  332. {
  333. struct fimc_dev *fimc = video_drvdata(file);
  334. return vb2_poll(&fimc->vid_cap.vbq, file, wait);
  335. }
  336. static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
  337. {
  338. struct fimc_dev *fimc = video_drvdata(file);
  339. return vb2_mmap(&fimc->vid_cap.vbq, vma);
  340. }
  341. static const struct v4l2_file_operations fimc_capture_fops = {
  342. .owner = THIS_MODULE,
  343. .open = fimc_capture_open,
  344. .release = fimc_capture_close,
  345. .poll = fimc_capture_poll,
  346. .unlocked_ioctl = video_ioctl2,
  347. .mmap = fimc_capture_mmap,
  348. };
  349. /*
  350. * Format and crop negotiation helpers
  351. */
  352. static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
  353. u32 *width, u32 *height,
  354. u32 *code, u32 *fourcc, int pad)
  355. {
  356. bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
  357. struct fimc_dev *fimc = ctx->fimc_dev;
  358. struct samsung_fimc_variant *var = fimc->variant;
  359. struct fimc_pix_limit *pl = var->pix_limit;
  360. struct fimc_frame *dst = &ctx->d_frame;
  361. u32 depth, min_w, max_w, min_h, align_h = 3;
  362. u32 mask = FMT_FLAGS_CAM;
  363. struct fimc_fmt *ffmt;
  364. /* Color conversion from/to JPEG is not supported */
  365. if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
  366. fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
  367. *code = V4L2_MBUS_FMT_JPEG_1X8;
  368. if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
  369. mask |= FMT_FLAGS_M2M;
  370. ffmt = fimc_find_format(fourcc, code, mask, 0);
  371. if (WARN_ON(!ffmt))
  372. return NULL;
  373. if (code)
  374. *code = ffmt->mbus_code;
  375. if (fourcc)
  376. *fourcc = ffmt->fourcc;
  377. if (pad == FIMC_SD_PAD_SINK) {
  378. max_w = fimc_fmt_is_jpeg(ffmt->color) ?
  379. pl->scaler_dis_w : pl->scaler_en_w;
  380. /* Apply the camera input interface pixel constraints */
  381. v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
  382. height, max_t(u32, *height, 32),
  383. FIMC_CAMIF_MAX_HEIGHT,
  384. fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
  385. 0);
  386. return ffmt;
  387. }
  388. /* Can't scale or crop in transparent (JPEG) transfer mode */
  389. if (fimc_fmt_is_jpeg(ffmt->color)) {
  390. *width = ctx->s_frame.f_width;
  391. *height = ctx->s_frame.f_height;
  392. return ffmt;
  393. }
  394. /* Apply the scaler and the output DMA constraints */
  395. max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
  396. min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
  397. min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
  398. if (fimc->id == 1 && var->pix_hoff)
  399. align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
  400. depth = fimc_get_format_depth(ffmt);
  401. v4l_bound_align_image(width, min_w, max_w,
  402. ffs(var->min_out_pixsize) - 1,
  403. height, min_h, FIMC_CAMIF_MAX_HEIGHT,
  404. align_h,
  405. 64/(ALIGN(depth, 8)));
  406. dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
  407. pad, code ? *code : 0, *width, *height,
  408. dst->f_width, dst->f_height);
  409. return ffmt;
  410. }
  411. static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
  412. int pad)
  413. {
  414. bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
  415. struct fimc_dev *fimc = ctx->fimc_dev;
  416. struct samsung_fimc_variant *var = fimc->variant;
  417. struct fimc_pix_limit *pl = var->pix_limit;
  418. struct fimc_frame *sink = &ctx->s_frame;
  419. u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
  420. u32 align_sz = 0, align_h = 4;
  421. u32 max_sc_h, max_sc_v;
  422. /* In JPEG transparent transfer mode cropping is not supported */
  423. if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
  424. r->width = sink->f_width;
  425. r->height = sink->f_height;
  426. r->left = r->top = 0;
  427. return;
  428. }
  429. if (pad == FIMC_SD_PAD_SOURCE) {
  430. if (ctx->rotation != 90 && ctx->rotation != 270)
  431. align_h = 1;
  432. max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
  433. max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
  434. min_sz = var->min_out_pixsize;
  435. } else {
  436. u32 depth = fimc_get_format_depth(sink->fmt);
  437. align_sz = 64/ALIGN(depth, 8);
  438. min_sz = var->min_inp_pixsize;
  439. min_w = min_h = min_sz;
  440. max_sc_h = max_sc_v = 1;
  441. }
  442. /*
  443. * For the crop rectangle at source pad the following constraints
  444. * must be met:
  445. * - it must fit in the sink pad format rectangle (f_width/f_height);
  446. * - maximum downscaling ratio is 64;
  447. * - maximum crop size depends if the rotator is used or not;
  448. * - the sink pad format width/height must be 4 multiple of the
  449. * prescaler ratios determined by sink pad size and source pad crop,
  450. * the prescaler ratio is returned by fimc_get_scaler_factor().
  451. */
  452. max_w = min_t(u32,
  453. rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
  454. rotate ? sink->f_height : sink->f_width);
  455. max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
  456. if (pad == FIMC_SD_PAD_SOURCE) {
  457. min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
  458. min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
  459. if (rotate) {
  460. swap(max_sc_h, max_sc_v);
  461. swap(min_w, min_h);
  462. }
  463. }
  464. v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
  465. &r->height, min_h, max_h, align_h,
  466. align_sz);
  467. /* Adjust left/top if cropping rectangle is out of bounds */
  468. r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
  469. r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
  470. r->left = round_down(r->left, var->hor_offs_align);
  471. dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
  472. pad, r->left, r->top, r->width, r->height,
  473. sink->f_width, sink->f_height);
  474. }
  475. /*
  476. * The video node ioctl operations
  477. */
  478. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  479. struct v4l2_capability *cap)
  480. {
  481. struct fimc_dev *fimc = video_drvdata(file);
  482. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  483. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  484. cap->bus_info[0] = 0;
  485. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
  486. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  487. return 0;
  488. }
  489. static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
  490. struct v4l2_fmtdesc *f)
  491. {
  492. struct fimc_fmt *fmt;
  493. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
  494. f->index);
  495. if (!fmt)
  496. return -EINVAL;
  497. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  498. f->pixelformat = fmt->fourcc;
  499. if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
  500. f->flags |= V4L2_FMT_FLAG_COMPRESSED;
  501. return 0;
  502. }
  503. /**
  504. * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
  505. * elements
  506. * @ctx: FIMC capture context
  507. * @tfmt: media bus format to try/set on subdevs
  508. * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
  509. * @set: true to set format on subdevs, false to try only
  510. */
  511. static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
  512. struct v4l2_mbus_framefmt *tfmt,
  513. struct fimc_fmt **fmt_id,
  514. bool set)
  515. {
  516. struct fimc_dev *fimc = ctx->fimc_dev;
  517. struct v4l2_subdev *sd = fimc->pipeline.sensor;
  518. struct v4l2_subdev *csis = fimc->pipeline.csis;
  519. struct v4l2_subdev_format sfmt;
  520. struct v4l2_mbus_framefmt *mf = &sfmt.format;
  521. struct fimc_fmt *ffmt = NULL;
  522. int ret, i = 0;
  523. if (WARN_ON(!sd || !tfmt))
  524. return -EINVAL;
  525. memset(&sfmt, 0, sizeof(sfmt));
  526. sfmt.format = *tfmt;
  527. sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
  528. while (1) {
  529. ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
  530. FMT_FLAGS_CAM, i++);
  531. if (ffmt == NULL) {
  532. /*
  533. * Notify user-space if common pixel code for
  534. * host and sensor does not exist.
  535. */
  536. return -EINVAL;
  537. }
  538. mf->code = tfmt->code = ffmt->mbus_code;
  539. ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
  540. if (ret)
  541. return ret;
  542. if (mf->code != tfmt->code) {
  543. mf->code = 0;
  544. continue;
  545. }
  546. if (mf->width != tfmt->width || mf->width != tfmt->width) {
  547. u32 fcc = ffmt->fourcc;
  548. tfmt->width = mf->width;
  549. tfmt->height = mf->height;
  550. ffmt = fimc_capture_try_format(ctx,
  551. &tfmt->width, &tfmt->height,
  552. NULL, &fcc, FIMC_SD_PAD_SOURCE);
  553. if (ffmt && ffmt->mbus_code)
  554. mf->code = ffmt->mbus_code;
  555. if (mf->width != tfmt->width || mf->width != tfmt->width)
  556. continue;
  557. tfmt->code = mf->code;
  558. }
  559. if (csis)
  560. ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
  561. if (mf->code == tfmt->code &&
  562. mf->width == tfmt->width && mf->width == tfmt->width)
  563. break;
  564. }
  565. if (fmt_id && ffmt)
  566. *fmt_id = ffmt;
  567. *tfmt = *mf;
  568. dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
  569. return 0;
  570. }
  571. static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
  572. struct v4l2_format *f)
  573. {
  574. struct fimc_dev *fimc = video_drvdata(file);
  575. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  576. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  577. return -EINVAL;
  578. return fimc_fill_format(&ctx->d_frame, f);
  579. }
  580. static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
  581. struct v4l2_format *f)
  582. {
  583. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  584. struct fimc_dev *fimc = video_drvdata(file);
  585. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  586. struct v4l2_mbus_framefmt mf;
  587. struct fimc_fmt *ffmt = NULL;
  588. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  589. return -EINVAL;
  590. if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
  591. fimc_capture_try_format(ctx, &pix->width, &pix->height,
  592. NULL, &pix->pixelformat,
  593. FIMC_SD_PAD_SINK);
  594. ctx->s_frame.f_width = pix->width;
  595. ctx->s_frame.f_height = pix->height;
  596. }
  597. ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
  598. NULL, &pix->pixelformat,
  599. FIMC_SD_PAD_SOURCE);
  600. if (!ffmt)
  601. return -EINVAL;
  602. if (!fimc->vid_cap.user_subdev_api) {
  603. mf.width = pix->width;
  604. mf.height = pix->height;
  605. mf.code = ffmt->mbus_code;
  606. fimc_md_graph_lock(fimc);
  607. fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
  608. fimc_md_graph_unlock(fimc);
  609. pix->width = mf.width;
  610. pix->height = mf.height;
  611. if (ffmt)
  612. pix->pixelformat = ffmt->fourcc;
  613. }
  614. fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
  615. return 0;
  616. }
  617. static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
  618. {
  619. ctx->scaler.enabled = !jpeg;
  620. fimc_ctrls_activate(ctx, !jpeg);
  621. if (jpeg)
  622. set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
  623. else
  624. clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
  625. }
  626. static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
  627. {
  628. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  629. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  630. struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
  631. struct fimc_frame *ff = &ctx->d_frame;
  632. struct fimc_fmt *s_fmt = NULL;
  633. int ret, i;
  634. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  635. return -EINVAL;
  636. if (vb2_is_busy(&fimc->vid_cap.vbq))
  637. return -EBUSY;
  638. /* Pre-configure format at camera interface input, for JPEG only */
  639. if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
  640. fimc_capture_try_format(ctx, &pix->width, &pix->height,
  641. NULL, &pix->pixelformat,
  642. FIMC_SD_PAD_SINK);
  643. ctx->s_frame.f_width = pix->width;
  644. ctx->s_frame.f_height = pix->height;
  645. }
  646. /* Try the format at the scaler and the DMA output */
  647. ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
  648. NULL, &pix->pixelformat,
  649. FIMC_SD_PAD_SOURCE);
  650. if (!ff->fmt)
  651. return -EINVAL;
  652. /* Try to match format at the host and the sensor */
  653. if (!fimc->vid_cap.user_subdev_api) {
  654. mf->code = ff->fmt->mbus_code;
  655. mf->width = pix->width;
  656. mf->height = pix->height;
  657. fimc_md_graph_lock(fimc);
  658. ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
  659. fimc_md_graph_unlock(fimc);
  660. if (ret)
  661. return ret;
  662. pix->width = mf->width;
  663. pix->height = mf->height;
  664. }
  665. fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
  666. for (i = 0; i < ff->fmt->colplanes; i++)
  667. ff->payload[i] =
  668. (pix->width * pix->height * ff->fmt->depth[i]) / 8;
  669. set_frame_bounds(ff, pix->width, pix->height);
  670. /* Reset the composition rectangle if not yet configured */
  671. if (!(ctx->state & FIMC_DST_CROP))
  672. set_frame_crop(ff, 0, 0, pix->width, pix->height);
  673. fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
  674. /* Reset cropping and set format at the camera interface input */
  675. if (!fimc->vid_cap.user_subdev_api) {
  676. ctx->s_frame.fmt = s_fmt;
  677. set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
  678. set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
  679. }
  680. return ret;
  681. }
  682. static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
  683. struct v4l2_format *f)
  684. {
  685. struct fimc_dev *fimc = video_drvdata(file);
  686. return fimc_capture_set_format(fimc, f);
  687. }
  688. static int fimc_cap_enum_input(struct file *file, void *priv,
  689. struct v4l2_input *i)
  690. {
  691. struct fimc_dev *fimc = video_drvdata(file);
  692. struct v4l2_subdev *sd = fimc->pipeline.sensor;
  693. if (i->index != 0)
  694. return -EINVAL;
  695. i->type = V4L2_INPUT_TYPE_CAMERA;
  696. if (sd)
  697. strlcpy(i->name, sd->name, sizeof(i->name));
  698. return 0;
  699. }
  700. static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
  701. {
  702. return i == 0 ? i : -EINVAL;
  703. }
  704. static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
  705. {
  706. *i = 0;
  707. return 0;
  708. }
  709. /**
  710. * fimc_pipeline_validate - check for formats inconsistencies
  711. * between source and sink pad of each link
  712. *
  713. * Return 0 if all formats match or -EPIPE otherwise.
  714. */
  715. static int fimc_pipeline_validate(struct fimc_dev *fimc)
  716. {
  717. struct v4l2_subdev_format sink_fmt, src_fmt;
  718. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  719. struct v4l2_subdev *sd;
  720. struct media_pad *pad;
  721. int ret;
  722. /* Start with the video capture node pad */
  723. pad = media_entity_remote_source(&vid_cap->vd_pad);
  724. if (pad == NULL)
  725. return -EPIPE;
  726. /* FIMC.{N} subdevice */
  727. sd = media_entity_to_v4l2_subdev(pad->entity);
  728. while (1) {
  729. /* Retrieve format at the sink pad */
  730. pad = &sd->entity.pads[0];
  731. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  732. break;
  733. /* Don't call FIMC subdev operation to avoid nested locking */
  734. if (sd == fimc->vid_cap.subdev) {
  735. struct fimc_frame *ff = &vid_cap->ctx->s_frame;
  736. sink_fmt.format.width = ff->f_width;
  737. sink_fmt.format.height = ff->f_height;
  738. sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
  739. } else {
  740. sink_fmt.pad = pad->index;
  741. sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  742. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
  743. if (ret < 0 && ret != -ENOIOCTLCMD)
  744. return -EPIPE;
  745. }
  746. /* Retrieve format at the source pad */
  747. pad = media_entity_remote_source(pad);
  748. if (pad == NULL ||
  749. media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  750. break;
  751. sd = media_entity_to_v4l2_subdev(pad->entity);
  752. src_fmt.pad = pad->index;
  753. src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  754. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
  755. if (ret < 0 && ret != -ENOIOCTLCMD)
  756. return -EPIPE;
  757. if (src_fmt.format.width != sink_fmt.format.width ||
  758. src_fmt.format.height != sink_fmt.format.height ||
  759. src_fmt.format.code != sink_fmt.format.code)
  760. return -EPIPE;
  761. }
  762. return 0;
  763. }
  764. static int fimc_cap_streamon(struct file *file, void *priv,
  765. enum v4l2_buf_type type)
  766. {
  767. struct fimc_dev *fimc = video_drvdata(file);
  768. struct fimc_pipeline *p = &fimc->pipeline;
  769. int ret;
  770. if (fimc_capture_active(fimc))
  771. return -EBUSY;
  772. media_entity_pipeline_start(&p->sensor->entity, p->pipe);
  773. if (fimc->vid_cap.user_subdev_api) {
  774. ret = fimc_pipeline_validate(fimc);
  775. if (ret)
  776. return ret;
  777. }
  778. return vb2_streamon(&fimc->vid_cap.vbq, type);
  779. }
  780. static int fimc_cap_streamoff(struct file *file, void *priv,
  781. enum v4l2_buf_type type)
  782. {
  783. struct fimc_dev *fimc = video_drvdata(file);
  784. struct v4l2_subdev *sd = fimc->pipeline.sensor;
  785. int ret;
  786. ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
  787. if (ret == 0)
  788. media_entity_pipeline_stop(&sd->entity);
  789. return ret;
  790. }
  791. static int fimc_cap_reqbufs(struct file *file, void *priv,
  792. struct v4l2_requestbuffers *reqbufs)
  793. {
  794. struct fimc_dev *fimc = video_drvdata(file);
  795. int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
  796. if (!ret)
  797. fimc->vid_cap.reqbufs_count = reqbufs->count;
  798. return ret;
  799. }
  800. static int fimc_cap_querybuf(struct file *file, void *priv,
  801. struct v4l2_buffer *buf)
  802. {
  803. struct fimc_dev *fimc = video_drvdata(file);
  804. return vb2_querybuf(&fimc->vid_cap.vbq, buf);
  805. }
  806. static int fimc_cap_qbuf(struct file *file, void *priv,
  807. struct v4l2_buffer *buf)
  808. {
  809. struct fimc_dev *fimc = video_drvdata(file);
  810. return vb2_qbuf(&fimc->vid_cap.vbq, buf);
  811. }
  812. static int fimc_cap_dqbuf(struct file *file, void *priv,
  813. struct v4l2_buffer *buf)
  814. {
  815. struct fimc_dev *fimc = video_drvdata(file);
  816. return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
  817. }
  818. static int fimc_cap_cropcap(struct file *file, void *fh,
  819. struct v4l2_cropcap *cr)
  820. {
  821. struct fimc_dev *fimc = video_drvdata(file);
  822. struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
  823. if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  824. return -EINVAL;
  825. cr->bounds.left = 0;
  826. cr->bounds.top = 0;
  827. cr->bounds.width = f->o_width;
  828. cr->bounds.height = f->o_height;
  829. cr->defrect = cr->bounds;
  830. return 0;
  831. }
  832. static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  833. {
  834. struct fimc_dev *fimc = video_drvdata(file);
  835. struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
  836. cr->c.left = f->offs_h;
  837. cr->c.top = f->offs_v;
  838. cr->c.width = f->width;
  839. cr->c.height = f->height;
  840. return 0;
  841. }
  842. static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  843. {
  844. struct fimc_dev *fimc = video_drvdata(file);
  845. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  846. struct fimc_frame *ff;
  847. unsigned long flags;
  848. fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK);
  849. ff = &ctx->s_frame;
  850. spin_lock_irqsave(&fimc->slock, flags);
  851. set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height);
  852. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  853. spin_unlock_irqrestore(&fimc->slock, flags);
  854. return 0;
  855. }
  856. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  857. .vidioc_querycap = fimc_vidioc_querycap_capture,
  858. .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
  859. .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
  860. .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
  861. .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
  862. .vidioc_reqbufs = fimc_cap_reqbufs,
  863. .vidioc_querybuf = fimc_cap_querybuf,
  864. .vidioc_qbuf = fimc_cap_qbuf,
  865. .vidioc_dqbuf = fimc_cap_dqbuf,
  866. .vidioc_streamon = fimc_cap_streamon,
  867. .vidioc_streamoff = fimc_cap_streamoff,
  868. .vidioc_g_crop = fimc_cap_g_crop,
  869. .vidioc_s_crop = fimc_cap_s_crop,
  870. .vidioc_cropcap = fimc_cap_cropcap,
  871. .vidioc_enum_input = fimc_cap_enum_input,
  872. .vidioc_s_input = fimc_cap_s_input,
  873. .vidioc_g_input = fimc_cap_g_input,
  874. };
  875. /* Capture subdev media entity operations */
  876. static int fimc_link_setup(struct media_entity *entity,
  877. const struct media_pad *local,
  878. const struct media_pad *remote, u32 flags)
  879. {
  880. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  881. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  882. if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  883. return -EINVAL;
  884. if (WARN_ON(fimc == NULL))
  885. return 0;
  886. dbg("%s --> %s, flags: 0x%x. input: 0x%x",
  887. local->entity->name, remote->entity->name, flags,
  888. fimc->vid_cap.input);
  889. if (flags & MEDIA_LNK_FL_ENABLED) {
  890. if (fimc->vid_cap.input != 0)
  891. return -EBUSY;
  892. fimc->vid_cap.input = sd->grp_id;
  893. return 0;
  894. }
  895. fimc->vid_cap.input = 0;
  896. return 0;
  897. }
  898. static const struct media_entity_operations fimc_sd_media_ops = {
  899. .link_setup = fimc_link_setup,
  900. };
  901. static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  902. struct v4l2_subdev_fh *fh,
  903. struct v4l2_subdev_mbus_code_enum *code)
  904. {
  905. struct fimc_fmt *fmt;
  906. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
  907. if (!fmt)
  908. return -EINVAL;
  909. code->code = fmt->mbus_code;
  910. return 0;
  911. }
  912. static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
  913. struct v4l2_subdev_fh *fh,
  914. struct v4l2_subdev_format *fmt)
  915. {
  916. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  917. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  918. struct v4l2_mbus_framefmt *mf;
  919. struct fimc_frame *ff;
  920. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  921. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  922. fmt->format = *mf;
  923. return 0;
  924. }
  925. mf = &fmt->format;
  926. mf->colorspace = V4L2_COLORSPACE_JPEG;
  927. ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
  928. mutex_lock(&fimc->lock);
  929. /* The pixel code is same on both input and output pad */
  930. if (!WARN_ON(ctx->s_frame.fmt == NULL))
  931. mf->code = ctx->s_frame.fmt->mbus_code;
  932. mf->width = ff->f_width;
  933. mf->height = ff->f_height;
  934. mutex_unlock(&fimc->lock);
  935. return 0;
  936. }
  937. static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
  938. struct v4l2_subdev_fh *fh,
  939. struct v4l2_subdev_format *fmt)
  940. {
  941. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  942. struct v4l2_mbus_framefmt *mf = &fmt->format;
  943. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  944. struct fimc_frame *ff;
  945. struct fimc_fmt *ffmt;
  946. dbg("pad%d: code: 0x%x, %dx%d",
  947. fmt->pad, mf->code, mf->width, mf->height);
  948. if (fmt->pad == FIMC_SD_PAD_SOURCE &&
  949. vb2_is_busy(&fimc->vid_cap.vbq))
  950. return -EBUSY;
  951. mutex_lock(&fimc->lock);
  952. ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
  953. &mf->code, NULL, fmt->pad);
  954. mutex_unlock(&fimc->lock);
  955. mf->colorspace = V4L2_COLORSPACE_JPEG;
  956. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  957. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  958. *mf = fmt->format;
  959. return 0;
  960. }
  961. fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
  962. ff = fmt->pad == FIMC_SD_PAD_SINK ?
  963. &ctx->s_frame : &ctx->d_frame;
  964. mutex_lock(&fimc->lock);
  965. set_frame_bounds(ff, mf->width, mf->height);
  966. ff->fmt = ffmt;
  967. /* Reset the crop rectangle if required. */
  968. if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
  969. set_frame_crop(ff, 0, 0, mf->width, mf->height);
  970. if (fmt->pad == FIMC_SD_PAD_SINK)
  971. ctx->state &= ~FIMC_DST_CROP;
  972. mutex_unlock(&fimc->lock);
  973. return 0;
  974. }
  975. static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
  976. struct v4l2_subdev_fh *fh,
  977. struct v4l2_subdev_crop *crop)
  978. {
  979. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  980. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  981. struct v4l2_rect *r = &crop->rect;
  982. struct fimc_frame *ff;
  983. if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
  984. crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
  985. return 0;
  986. }
  987. ff = crop->pad == FIMC_SD_PAD_SINK ?
  988. &ctx->s_frame : &ctx->d_frame;
  989. mutex_lock(&fimc->lock);
  990. r->left = ff->offs_h;
  991. r->top = ff->offs_v;
  992. r->width = ff->width;
  993. r->height = ff->height;
  994. mutex_unlock(&fimc->lock);
  995. dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
  996. ff, crop->pad, r->left, r->top, r->width, r->height,
  997. ff->f_width, ff->f_height);
  998. return 0;
  999. }
  1000. static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
  1001. struct v4l2_subdev_fh *fh,
  1002. struct v4l2_subdev_crop *crop)
  1003. {
  1004. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1005. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1006. struct v4l2_rect *r = &crop->rect;
  1007. struct fimc_frame *ff;
  1008. unsigned long flags;
  1009. dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
  1010. ff = crop->pad == FIMC_SD_PAD_SOURCE ?
  1011. &ctx->d_frame : &ctx->s_frame;
  1012. mutex_lock(&fimc->lock);
  1013. fimc_capture_try_crop(ctx, r, crop->pad);
  1014. if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
  1015. mutex_lock(&fimc->lock);
  1016. *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
  1017. return 0;
  1018. }
  1019. spin_lock_irqsave(&fimc->slock, flags);
  1020. set_frame_crop(ff, r->left, r->top, r->width, r->height);
  1021. if (crop->pad == FIMC_SD_PAD_SOURCE)
  1022. ctx->state |= FIMC_DST_CROP;
  1023. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  1024. spin_unlock_irqrestore(&fimc->slock, flags);
  1025. dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
  1026. r->width, r->height);
  1027. mutex_unlock(&fimc->lock);
  1028. return 0;
  1029. }
  1030. static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
  1031. .enum_mbus_code = fimc_subdev_enum_mbus_code,
  1032. .get_fmt = fimc_subdev_get_fmt,
  1033. .set_fmt = fimc_subdev_set_fmt,
  1034. .get_crop = fimc_subdev_get_crop,
  1035. .set_crop = fimc_subdev_set_crop,
  1036. };
  1037. static struct v4l2_subdev_ops fimc_subdev_ops = {
  1038. .pad = &fimc_subdev_pad_ops,
  1039. };
  1040. static int fimc_create_capture_subdev(struct fimc_dev *fimc,
  1041. struct v4l2_device *v4l2_dev)
  1042. {
  1043. struct v4l2_subdev *sd;
  1044. int ret;
  1045. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  1046. if (!sd)
  1047. return -ENOMEM;
  1048. v4l2_subdev_init(sd, &fimc_subdev_ops);
  1049. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1050. snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
  1051. fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1052. fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1053. ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
  1054. fimc->vid_cap.sd_pads, 0);
  1055. if (ret)
  1056. goto me_err;
  1057. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  1058. if (ret)
  1059. goto sd_err;
  1060. fimc->vid_cap.subdev = sd;
  1061. v4l2_set_subdevdata(sd, fimc);
  1062. sd->entity.ops = &fimc_sd_media_ops;
  1063. return 0;
  1064. sd_err:
  1065. media_entity_cleanup(&sd->entity);
  1066. me_err:
  1067. kfree(sd);
  1068. return ret;
  1069. }
  1070. static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
  1071. {
  1072. struct v4l2_subdev *sd = fimc->vid_cap.subdev;
  1073. if (!sd)
  1074. return;
  1075. media_entity_cleanup(&sd->entity);
  1076. v4l2_device_unregister_subdev(sd);
  1077. kfree(sd);
  1078. sd = NULL;
  1079. }
  1080. /* Set default format at the sensor and host interface */
  1081. static int fimc_capture_set_default_format(struct fimc_dev *fimc)
  1082. {
  1083. struct v4l2_format fmt = {
  1084. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  1085. .fmt.pix_mp = {
  1086. .width = 640,
  1087. .height = 480,
  1088. .pixelformat = V4L2_PIX_FMT_YUYV,
  1089. .field = V4L2_FIELD_NONE,
  1090. .colorspace = V4L2_COLORSPACE_JPEG,
  1091. },
  1092. };
  1093. return fimc_capture_set_format(fimc, &fmt);
  1094. }
  1095. /* fimc->lock must be already initialized */
  1096. int fimc_register_capture_device(struct fimc_dev *fimc,
  1097. struct v4l2_device *v4l2_dev)
  1098. {
  1099. struct video_device *vfd;
  1100. struct fimc_vid_cap *vid_cap;
  1101. struct fimc_ctx *ctx;
  1102. struct vb2_queue *q;
  1103. int ret = -ENOMEM;
  1104. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  1105. if (!ctx)
  1106. return -ENOMEM;
  1107. ctx->fimc_dev = fimc;
  1108. ctx->in_path = FIMC_CAMERA;
  1109. ctx->out_path = FIMC_DMA;
  1110. ctx->state = FIMC_CTX_CAP;
  1111. ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
  1112. ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
  1113. vfd = video_device_alloc();
  1114. if (!vfd) {
  1115. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  1116. goto err_vd_alloc;
  1117. }
  1118. snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
  1119. dev_name(&fimc->pdev->dev));
  1120. vfd->fops = &fimc_capture_fops;
  1121. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  1122. vfd->v4l2_dev = v4l2_dev;
  1123. vfd->minor = -1;
  1124. vfd->release = video_device_release;
  1125. vfd->lock = &fimc->lock;
  1126. video_set_drvdata(vfd, fimc);
  1127. vid_cap = &fimc->vid_cap;
  1128. vid_cap->vfd = vfd;
  1129. vid_cap->active_buf_cnt = 0;
  1130. vid_cap->reqbufs_count = 0;
  1131. vid_cap->refcnt = 0;
  1132. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  1133. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  1134. spin_lock_init(&ctx->slock);
  1135. vid_cap->ctx = ctx;
  1136. q = &fimc->vid_cap.vbq;
  1137. memset(q, 0, sizeof(*q));
  1138. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1139. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1140. q->drv_priv = fimc->vid_cap.ctx;
  1141. q->ops = &fimc_capture_qops;
  1142. q->mem_ops = &vb2_dma_contig_memops;
  1143. q->buf_struct_size = sizeof(struct fimc_vid_buffer);
  1144. vb2_queue_init(q);
  1145. fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
  1146. ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
  1147. if (ret)
  1148. goto err_ent;
  1149. ret = fimc_create_capture_subdev(fimc, v4l2_dev);
  1150. if (ret)
  1151. goto err_sd_reg;
  1152. vfd->ctrl_handler = &ctx->ctrl_handler;
  1153. return 0;
  1154. err_sd_reg:
  1155. media_entity_cleanup(&vfd->entity);
  1156. err_ent:
  1157. video_device_release(vfd);
  1158. err_vd_alloc:
  1159. kfree(ctx);
  1160. return ret;
  1161. }
  1162. void fimc_unregister_capture_device(struct fimc_dev *fimc)
  1163. {
  1164. struct video_device *vfd = fimc->vid_cap.vfd;
  1165. if (vfd) {
  1166. media_entity_cleanup(&vfd->entity);
  1167. /* Can also be called if video device was
  1168. not registered */
  1169. video_unregister_device(vfd);
  1170. }
  1171. fimc_destroy_capture_subdev(fimc);
  1172. kfree(fimc->vid_cap.ctx);
  1173. fimc->vid_cap.ctx = NULL;
  1174. }