fimc-capture.c 46 KB

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