fimc-capture.c 46 KB

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