fimc-capture.c 40 KB

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