fimc-capture.c 44 KB

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