fimc-capture.c 50 KB

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