fimc-capture.c 42 KB

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