fimc-capture.c 40 KB

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