fimc-capture.c 44 KB

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