fimc-capture.c 46 KB

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