fimc-capture.c 40 KB

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