fimc-capture.c 42 KB

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