fimc-capture.c 40 KB

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