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. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  127. ret = fimc_set_scaler_info(ctx);
  128. if (ret)
  129. return ret;
  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. return ret;
  140. }
  141. static int start_streaming(struct vb2_queue *q, unsigned int count)
  142. {
  143. struct fimc_ctx *ctx = q->drv_priv;
  144. struct fimc_dev *fimc = ctx->fimc_dev;
  145. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  146. int min_bufs;
  147. int ret;
  148. vid_cap->frame_count = 0;
  149. ret = fimc_init_capture(fimc);
  150. if (ret)
  151. goto error;
  152. set_bit(ST_CAPT_PEND, &fimc->state);
  153. min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
  154. if (vid_cap->active_buf_cnt >= min_bufs &&
  155. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
  156. fimc_activate_capture(ctx);
  157. if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  158. fimc_pipeline_s_stream(fimc, 1);
  159. }
  160. return 0;
  161. error:
  162. fimc_capture_state_cleanup(fimc, false);
  163. return ret;
  164. }
  165. static int stop_streaming(struct vb2_queue *q)
  166. {
  167. struct fimc_ctx *ctx = q->drv_priv;
  168. struct fimc_dev *fimc = ctx->fimc_dev;
  169. if (!fimc_capture_active(fimc))
  170. return -EINVAL;
  171. return fimc_stop_capture(fimc, false);
  172. }
  173. int fimc_capture_suspend(struct fimc_dev *fimc)
  174. {
  175. bool suspend = fimc_capture_busy(fimc);
  176. int ret = fimc_stop_capture(fimc, suspend);
  177. if (ret)
  178. return ret;
  179. return fimc_pipeline_shutdown(fimc);
  180. }
  181. static void buffer_queue(struct vb2_buffer *vb);
  182. int fimc_capture_resume(struct fimc_dev *fimc)
  183. {
  184. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  185. struct fimc_vid_buffer *buf;
  186. int i;
  187. if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
  188. return 0;
  189. INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
  190. vid_cap->buf_index = 0;
  191. fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
  192. false);
  193. fimc_init_capture(fimc);
  194. clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
  195. for (i = 0; i < vid_cap->reqbufs_count; i++) {
  196. if (list_empty(&vid_cap->pending_buf_q))
  197. break;
  198. buf = fimc_pending_queue_pop(vid_cap);
  199. buffer_queue(&buf->vb);
  200. }
  201. return 0;
  202. }
  203. static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
  204. {
  205. if (!fr || plane >= fr->fmt->memplanes)
  206. return 0;
  207. return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
  208. }
  209. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
  210. unsigned int *num_buffers, unsigned int *num_planes,
  211. unsigned int sizes[], void *allocators[])
  212. {
  213. struct fimc_ctx *ctx = vq->drv_priv;
  214. struct fimc_fmt *fmt = ctx->d_frame.fmt;
  215. int i;
  216. if (!fmt)
  217. return -EINVAL;
  218. *num_planes = fmt->memplanes;
  219. for (i = 0; i < fmt->memplanes; i++) {
  220. sizes[i] = get_plane_size(&ctx->d_frame, i);
  221. allocators[i] = ctx->fimc_dev->alloc_ctx;
  222. }
  223. return 0;
  224. }
  225. static int buffer_prepare(struct vb2_buffer *vb)
  226. {
  227. struct vb2_queue *vq = vb->vb2_queue;
  228. struct fimc_ctx *ctx = vq->drv_priv;
  229. int i;
  230. if (ctx->d_frame.fmt == NULL)
  231. return -EINVAL;
  232. for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
  233. unsigned long size = ctx->d_frame.payload[i];
  234. if (vb2_plane_size(vb, i) < size) {
  235. v4l2_err(ctx->fimc_dev->vid_cap.vfd,
  236. "User buffer too small (%ld < %ld)\n",
  237. vb2_plane_size(vb, i), size);
  238. return -EINVAL;
  239. }
  240. vb2_set_plane_payload(vb, i, size);
  241. }
  242. return 0;
  243. }
  244. static void buffer_queue(struct vb2_buffer *vb)
  245. {
  246. struct fimc_vid_buffer *buf
  247. = container_of(vb, struct fimc_vid_buffer, vb);
  248. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  249. struct fimc_dev *fimc = ctx->fimc_dev;
  250. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  251. unsigned long flags;
  252. int min_bufs;
  253. spin_lock_irqsave(&fimc->slock, flags);
  254. fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
  255. if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
  256. !test_bit(ST_CAPT_STREAM, &fimc->state) &&
  257. vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
  258. /* Setup the buffer directly for processing. */
  259. int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
  260. vid_cap->buf_index;
  261. fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
  262. buf->index = vid_cap->buf_index;
  263. fimc_active_queue_add(vid_cap, buf);
  264. if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
  265. vid_cap->buf_index = 0;
  266. } else {
  267. fimc_pending_queue_add(vid_cap, buf);
  268. }
  269. min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
  270. if (vb2_is_streaming(&vid_cap->vbq) &&
  271. vid_cap->active_buf_cnt >= min_bufs &&
  272. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
  273. fimc_activate_capture(ctx);
  274. spin_unlock_irqrestore(&fimc->slock, flags);
  275. if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
  276. fimc_pipeline_s_stream(fimc, 1);
  277. return;
  278. }
  279. spin_unlock_irqrestore(&fimc->slock, flags);
  280. }
  281. static void fimc_lock(struct vb2_queue *vq)
  282. {
  283. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  284. mutex_lock(&ctx->fimc_dev->lock);
  285. }
  286. static void fimc_unlock(struct vb2_queue *vq)
  287. {
  288. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  289. mutex_unlock(&ctx->fimc_dev->lock);
  290. }
  291. static struct vb2_ops fimc_capture_qops = {
  292. .queue_setup = queue_setup,
  293. .buf_prepare = buffer_prepare,
  294. .buf_queue = buffer_queue,
  295. .wait_prepare = fimc_unlock,
  296. .wait_finish = fimc_lock,
  297. .start_streaming = start_streaming,
  298. .stop_streaming = stop_streaming,
  299. };
  300. /**
  301. * fimc_capture_ctrls_create - initialize the control handler
  302. * Initialize the capture video node control handler and fill it
  303. * with the FIMC controls. Inherit any sensor's controls if the
  304. * 'user_subdev_api' flag is false (default behaviour).
  305. * This function need to be called with the graph mutex held.
  306. */
  307. int fimc_capture_ctrls_create(struct fimc_dev *fimc)
  308. {
  309. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  310. int ret;
  311. if (WARN_ON(vid_cap->ctx == NULL))
  312. return -ENXIO;
  313. if (vid_cap->ctx->ctrls_rdy)
  314. return 0;
  315. ret = fimc_ctrls_create(vid_cap->ctx);
  316. if (ret || vid_cap->user_subdev_api)
  317. return ret;
  318. return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
  319. fimc->pipeline.sensor->ctrl_handler);
  320. }
  321. static int fimc_capture_set_default_format(struct fimc_dev *fimc);
  322. static int fimc_capture_open(struct file *file)
  323. {
  324. struct fimc_dev *fimc = video_drvdata(file);
  325. int ret = v4l2_fh_open(file);
  326. if (ret)
  327. return ret;
  328. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  329. /* Return if the corresponding video mem2mem node is already opened. */
  330. if (fimc_m2m_active(fimc))
  331. return -EBUSY;
  332. set_bit(ST_CAPT_BUSY, &fimc->state);
  333. pm_runtime_get_sync(&fimc->pdev->dev);
  334. if (++fimc->vid_cap.refcnt == 1) {
  335. ret = fimc_pipeline_initialize(fimc,
  336. &fimc->vid_cap.vfd->entity, true);
  337. if (ret < 0) {
  338. dev_err(&fimc->pdev->dev,
  339. "Video pipeline initialization failed\n");
  340. pm_runtime_put_sync(&fimc->pdev->dev);
  341. fimc->vid_cap.refcnt--;
  342. v4l2_fh_release(file);
  343. clear_bit(ST_CAPT_BUSY, &fimc->state);
  344. return ret;
  345. }
  346. ret = fimc_capture_ctrls_create(fimc);
  347. if (!ret && !fimc->vid_cap.user_subdev_api)
  348. ret = fimc_capture_set_default_format(fimc);
  349. }
  350. return ret;
  351. }
  352. static int fimc_capture_close(struct file *file)
  353. {
  354. struct fimc_dev *fimc = video_drvdata(file);
  355. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  356. if (--fimc->vid_cap.refcnt == 0) {
  357. clear_bit(ST_CAPT_BUSY, &fimc->state);
  358. fimc_stop_capture(fimc, false);
  359. fimc_pipeline_shutdown(fimc);
  360. clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
  361. }
  362. pm_runtime_put(&fimc->pdev->dev);
  363. if (fimc->vid_cap.refcnt == 0) {
  364. vb2_queue_release(&fimc->vid_cap.vbq);
  365. fimc_ctrls_delete(fimc->vid_cap.ctx);
  366. }
  367. return v4l2_fh_release(file);
  368. }
  369. static unsigned int fimc_capture_poll(struct file *file,
  370. struct poll_table_struct *wait)
  371. {
  372. struct fimc_dev *fimc = video_drvdata(file);
  373. return vb2_poll(&fimc->vid_cap.vbq, file, wait);
  374. }
  375. static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
  376. {
  377. struct fimc_dev *fimc = video_drvdata(file);
  378. return vb2_mmap(&fimc->vid_cap.vbq, vma);
  379. }
  380. static const struct v4l2_file_operations fimc_capture_fops = {
  381. .owner = THIS_MODULE,
  382. .open = fimc_capture_open,
  383. .release = fimc_capture_close,
  384. .poll = fimc_capture_poll,
  385. .unlocked_ioctl = video_ioctl2,
  386. .mmap = fimc_capture_mmap,
  387. };
  388. /*
  389. * Format and crop negotiation helpers
  390. */
  391. static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
  392. u32 *width, u32 *height,
  393. u32 *code, u32 *fourcc, int pad)
  394. {
  395. bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
  396. struct fimc_dev *fimc = ctx->fimc_dev;
  397. struct samsung_fimc_variant *var = fimc->variant;
  398. struct fimc_pix_limit *pl = var->pix_limit;
  399. struct fimc_frame *dst = &ctx->d_frame;
  400. u32 depth, min_w, max_w, min_h, align_h = 3;
  401. u32 mask = FMT_FLAGS_CAM;
  402. struct fimc_fmt *ffmt;
  403. /* Color conversion from/to JPEG is not supported */
  404. if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
  405. fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
  406. *code = V4L2_MBUS_FMT_JPEG_1X8;
  407. if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
  408. mask |= FMT_FLAGS_M2M;
  409. ffmt = fimc_find_format(fourcc, code, mask, 0);
  410. if (WARN_ON(!ffmt))
  411. return NULL;
  412. if (code)
  413. *code = ffmt->mbus_code;
  414. if (fourcc)
  415. *fourcc = ffmt->fourcc;
  416. if (pad == FIMC_SD_PAD_SINK) {
  417. max_w = fimc_fmt_is_jpeg(ffmt->color) ?
  418. pl->scaler_dis_w : pl->scaler_en_w;
  419. /* Apply the camera input interface pixel constraints */
  420. v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
  421. height, max_t(u32, *height, 32),
  422. FIMC_CAMIF_MAX_HEIGHT,
  423. fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
  424. 0);
  425. return ffmt;
  426. }
  427. /* Can't scale or crop in transparent (JPEG) transfer mode */
  428. if (fimc_fmt_is_jpeg(ffmt->color)) {
  429. *width = ctx->s_frame.f_width;
  430. *height = ctx->s_frame.f_height;
  431. return ffmt;
  432. }
  433. /* Apply the scaler and the output DMA constraints */
  434. max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
  435. min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
  436. min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
  437. if (var->min_vsize_align == 1 && !rotation)
  438. align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
  439. depth = fimc_get_format_depth(ffmt);
  440. v4l_bound_align_image(width, min_w, max_w,
  441. ffs(var->min_out_pixsize) - 1,
  442. height, min_h, FIMC_CAMIF_MAX_HEIGHT,
  443. align_h,
  444. 64/(ALIGN(depth, 8)));
  445. dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
  446. pad, code ? *code : 0, *width, *height,
  447. dst->f_width, dst->f_height);
  448. return ffmt;
  449. }
  450. static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
  451. int pad)
  452. {
  453. bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
  454. struct fimc_dev *fimc = ctx->fimc_dev;
  455. struct samsung_fimc_variant *var = fimc->variant;
  456. struct fimc_pix_limit *pl = var->pix_limit;
  457. struct fimc_frame *sink = &ctx->s_frame;
  458. u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
  459. u32 align_sz = 0, align_h = 4;
  460. u32 max_sc_h, max_sc_v;
  461. /* In JPEG transparent transfer mode cropping is not supported */
  462. if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
  463. r->width = sink->f_width;
  464. r->height = sink->f_height;
  465. r->left = r->top = 0;
  466. return;
  467. }
  468. if (pad == FIMC_SD_PAD_SOURCE) {
  469. if (ctx->rotation != 90 && ctx->rotation != 270)
  470. align_h = 1;
  471. max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
  472. max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
  473. min_sz = var->min_out_pixsize;
  474. } else {
  475. u32 depth = fimc_get_format_depth(sink->fmt);
  476. align_sz = 64/ALIGN(depth, 8);
  477. min_sz = var->min_inp_pixsize;
  478. min_w = min_h = min_sz;
  479. max_sc_h = max_sc_v = 1;
  480. }
  481. /*
  482. * For the crop rectangle at source pad the following constraints
  483. * must be met:
  484. * - it must fit in the sink pad format rectangle (f_width/f_height);
  485. * - maximum downscaling ratio is 64;
  486. * - maximum crop size depends if the rotator is used or not;
  487. * - the sink pad format width/height must be 4 multiple of the
  488. * prescaler ratios determined by sink pad size and source pad crop,
  489. * the prescaler ratio is returned by fimc_get_scaler_factor().
  490. */
  491. max_w = min_t(u32,
  492. rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
  493. rotate ? sink->f_height : sink->f_width);
  494. max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
  495. if (pad == FIMC_SD_PAD_SOURCE) {
  496. min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
  497. min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
  498. if (rotate) {
  499. swap(max_sc_h, max_sc_v);
  500. swap(min_w, min_h);
  501. }
  502. }
  503. v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
  504. &r->height, min_h, max_h, align_h,
  505. align_sz);
  506. /* Adjust left/top if cropping rectangle is out of bounds */
  507. r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
  508. r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
  509. r->left = round_down(r->left, var->hor_offs_align);
  510. dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
  511. pad, r->left, r->top, r->width, r->height,
  512. sink->f_width, sink->f_height);
  513. }
  514. /*
  515. * The video node ioctl operations
  516. */
  517. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  518. struct v4l2_capability *cap)
  519. {
  520. struct fimc_dev *fimc = video_drvdata(file);
  521. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  522. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  523. cap->bus_info[0] = 0;
  524. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  525. return 0;
  526. }
  527. static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
  528. struct v4l2_fmtdesc *f)
  529. {
  530. struct fimc_fmt *fmt;
  531. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
  532. f->index);
  533. if (!fmt)
  534. return -EINVAL;
  535. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  536. f->pixelformat = fmt->fourcc;
  537. if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
  538. f->flags |= V4L2_FMT_FLAG_COMPRESSED;
  539. return 0;
  540. }
  541. /**
  542. * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
  543. * elements
  544. * @ctx: FIMC capture context
  545. * @tfmt: media bus format to try/set on subdevs
  546. * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
  547. * @set: true to set format on subdevs, false to try only
  548. */
  549. static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
  550. struct v4l2_mbus_framefmt *tfmt,
  551. struct fimc_fmt **fmt_id,
  552. bool set)
  553. {
  554. struct fimc_dev *fimc = ctx->fimc_dev;
  555. struct v4l2_subdev *sd = fimc->pipeline.sensor;
  556. struct v4l2_subdev *csis = fimc->pipeline.csis;
  557. struct v4l2_subdev_format sfmt;
  558. struct v4l2_mbus_framefmt *mf = &sfmt.format;
  559. struct fimc_fmt *ffmt = NULL;
  560. int ret, i = 0;
  561. if (WARN_ON(!sd || !tfmt))
  562. return -EINVAL;
  563. memset(&sfmt, 0, sizeof(sfmt));
  564. sfmt.format = *tfmt;
  565. sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
  566. while (1) {
  567. ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
  568. FMT_FLAGS_CAM, i++);
  569. if (ffmt == NULL) {
  570. /*
  571. * Notify user-space if common pixel code for
  572. * host and sensor does not exist.
  573. */
  574. return -EINVAL;
  575. }
  576. mf->code = tfmt->code = ffmt->mbus_code;
  577. ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
  578. if (ret)
  579. return ret;
  580. if (mf->code != tfmt->code) {
  581. mf->code = 0;
  582. continue;
  583. }
  584. if (mf->width != tfmt->width || mf->height != tfmt->height) {
  585. u32 fcc = ffmt->fourcc;
  586. tfmt->width = mf->width;
  587. tfmt->height = mf->height;
  588. ffmt = fimc_capture_try_format(ctx,
  589. &tfmt->width, &tfmt->height,
  590. NULL, &fcc, FIMC_SD_PAD_SOURCE);
  591. if (ffmt && ffmt->mbus_code)
  592. mf->code = ffmt->mbus_code;
  593. if (mf->width != tfmt->width ||
  594. mf->height != tfmt->height)
  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->height == tfmt->height)
  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_create_bufs(struct file *file, void *priv,
  860. struct v4l2_create_buffers *create)
  861. {
  862. struct fimc_dev *fimc = video_drvdata(file);
  863. return vb2_create_bufs(&fimc->vid_cap.vbq, create);
  864. }
  865. static int fimc_cap_prepare_buf(struct file *file, void *priv,
  866. struct v4l2_buffer *b)
  867. {
  868. struct fimc_dev *fimc = video_drvdata(file);
  869. return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
  870. }
  871. static int fimc_cap_g_selection(struct file *file, void *fh,
  872. struct v4l2_selection *s)
  873. {
  874. struct fimc_dev *fimc = video_drvdata(file);
  875. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  876. struct fimc_frame *f = &ctx->s_frame;
  877. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  878. return -EINVAL;
  879. switch (s->target) {
  880. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  881. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  882. f = &ctx->d_frame;
  883. case V4L2_SEL_TGT_CROP_BOUNDS:
  884. case V4L2_SEL_TGT_CROP_DEFAULT:
  885. s->r.left = 0;
  886. s->r.top = 0;
  887. s->r.width = f->o_width;
  888. s->r.height = f->o_height;
  889. return 0;
  890. case V4L2_SEL_TGT_COMPOSE_ACTIVE:
  891. f = &ctx->d_frame;
  892. case V4L2_SEL_TGT_CROP_ACTIVE:
  893. s->r.left = f->offs_h;
  894. s->r.top = f->offs_v;
  895. s->r.width = f->width;
  896. s->r.height = f->height;
  897. return 0;
  898. }
  899. return -EINVAL;
  900. }
  901. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  902. int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
  903. {
  904. if (a->left < b->left || a->top < b->top)
  905. return 0;
  906. if (a->left + a->width > b->left + b->width)
  907. return 0;
  908. if (a->top + a->height > b->top + b->height)
  909. return 0;
  910. return 1;
  911. }
  912. static int fimc_cap_s_selection(struct file *file, void *fh,
  913. struct v4l2_selection *s)
  914. {
  915. struct fimc_dev *fimc = video_drvdata(file);
  916. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  917. struct v4l2_rect rect = s->r;
  918. struct fimc_frame *f;
  919. unsigned long flags;
  920. unsigned int pad;
  921. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  922. return -EINVAL;
  923. switch (s->target) {
  924. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  925. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  926. case V4L2_SEL_TGT_COMPOSE_ACTIVE:
  927. f = &ctx->d_frame;
  928. pad = FIMC_SD_PAD_SOURCE;
  929. break;
  930. case V4L2_SEL_TGT_CROP_BOUNDS:
  931. case V4L2_SEL_TGT_CROP_DEFAULT:
  932. case V4L2_SEL_TGT_CROP_ACTIVE:
  933. f = &ctx->s_frame;
  934. pad = FIMC_SD_PAD_SINK;
  935. break;
  936. default:
  937. return -EINVAL;
  938. }
  939. fimc_capture_try_crop(ctx, &rect, pad);
  940. if (s->flags & V4L2_SEL_FLAG_LE &&
  941. !enclosed_rectangle(&rect, &s->r))
  942. return -ERANGE;
  943. if (s->flags & V4L2_SEL_FLAG_GE &&
  944. !enclosed_rectangle(&s->r, &rect))
  945. return -ERANGE;
  946. s->r = rect;
  947. spin_lock_irqsave(&fimc->slock, flags);
  948. set_frame_crop(f, s->r.left, s->r.top, s->r.width,
  949. s->r.height);
  950. spin_unlock_irqrestore(&fimc->slock, flags);
  951. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  952. return 0;
  953. }
  954. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  955. .vidioc_querycap = fimc_vidioc_querycap_capture,
  956. .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
  957. .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
  958. .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
  959. .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
  960. .vidioc_reqbufs = fimc_cap_reqbufs,
  961. .vidioc_querybuf = fimc_cap_querybuf,
  962. .vidioc_qbuf = fimc_cap_qbuf,
  963. .vidioc_dqbuf = fimc_cap_dqbuf,
  964. .vidioc_prepare_buf = fimc_cap_prepare_buf,
  965. .vidioc_create_bufs = fimc_cap_create_bufs,
  966. .vidioc_streamon = fimc_cap_streamon,
  967. .vidioc_streamoff = fimc_cap_streamoff,
  968. .vidioc_g_selection = fimc_cap_g_selection,
  969. .vidioc_s_selection = fimc_cap_s_selection,
  970. .vidioc_enum_input = fimc_cap_enum_input,
  971. .vidioc_s_input = fimc_cap_s_input,
  972. .vidioc_g_input = fimc_cap_g_input,
  973. };
  974. /* Capture subdev media entity operations */
  975. static int fimc_link_setup(struct media_entity *entity,
  976. const struct media_pad *local,
  977. const struct media_pad *remote, u32 flags)
  978. {
  979. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  980. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  981. if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  982. return -EINVAL;
  983. if (WARN_ON(fimc == NULL))
  984. return 0;
  985. dbg("%s --> %s, flags: 0x%x. input: 0x%x",
  986. local->entity->name, remote->entity->name, flags,
  987. fimc->vid_cap.input);
  988. if (flags & MEDIA_LNK_FL_ENABLED) {
  989. if (fimc->vid_cap.input != 0)
  990. return -EBUSY;
  991. fimc->vid_cap.input = sd->grp_id;
  992. return 0;
  993. }
  994. fimc->vid_cap.input = 0;
  995. return 0;
  996. }
  997. static const struct media_entity_operations fimc_sd_media_ops = {
  998. .link_setup = fimc_link_setup,
  999. };
  1000. /**
  1001. * fimc_sensor_notify - v4l2_device notification from a sensor subdev
  1002. * @sd: pointer to a subdev generating the notification
  1003. * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
  1004. * @arg: pointer to an u32 type integer that stores the frame payload value
  1005. *
  1006. * The End Of Frame notification sent by sensor subdev in its still capture
  1007. * mode. If there is only a single VSYNC generated by the sensor at the
  1008. * beginning of a frame transmission, FIMC does not issue the LastIrq
  1009. * (end of frame) interrupt. And this notification is used to complete the
  1010. * frame capture and returning a buffer to user-space. Subdev drivers should
  1011. * call this notification from their last 'End of frame capture' interrupt.
  1012. */
  1013. void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
  1014. void *arg)
  1015. {
  1016. struct fimc_sensor_info *sensor;
  1017. struct fimc_vid_buffer *buf;
  1018. struct fimc_md *fmd;
  1019. struct fimc_dev *fimc;
  1020. unsigned long flags;
  1021. if (sd == NULL)
  1022. return;
  1023. sensor = v4l2_get_subdev_hostdata(sd);
  1024. fmd = entity_to_fimc_mdev(&sd->entity);
  1025. spin_lock_irqsave(&fmd->slock, flags);
  1026. fimc = sensor ? sensor->host : NULL;
  1027. if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
  1028. test_bit(ST_CAPT_PEND, &fimc->state)) {
  1029. unsigned long irq_flags;
  1030. spin_lock_irqsave(&fimc->slock, irq_flags);
  1031. if (!list_empty(&fimc->vid_cap.active_buf_q)) {
  1032. buf = list_entry(fimc->vid_cap.active_buf_q.next,
  1033. struct fimc_vid_buffer, list);
  1034. vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
  1035. }
  1036. fimc_capture_irq_handler(fimc, true);
  1037. fimc_deactivate_capture(fimc);
  1038. spin_unlock_irqrestore(&fimc->slock, irq_flags);
  1039. }
  1040. spin_unlock_irqrestore(&fmd->slock, flags);
  1041. }
  1042. static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  1043. struct v4l2_subdev_fh *fh,
  1044. struct v4l2_subdev_mbus_code_enum *code)
  1045. {
  1046. struct fimc_fmt *fmt;
  1047. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
  1048. if (!fmt)
  1049. return -EINVAL;
  1050. code->code = fmt->mbus_code;
  1051. return 0;
  1052. }
  1053. static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
  1054. struct v4l2_subdev_fh *fh,
  1055. struct v4l2_subdev_format *fmt)
  1056. {
  1057. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1058. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1059. struct v4l2_mbus_framefmt *mf;
  1060. struct fimc_frame *ff;
  1061. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1062. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1063. fmt->format = *mf;
  1064. return 0;
  1065. }
  1066. mf = &fmt->format;
  1067. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1068. ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
  1069. mutex_lock(&fimc->lock);
  1070. /* The pixel code is same on both input and output pad */
  1071. if (!WARN_ON(ctx->s_frame.fmt == NULL))
  1072. mf->code = ctx->s_frame.fmt->mbus_code;
  1073. mf->width = ff->f_width;
  1074. mf->height = ff->f_height;
  1075. mutex_unlock(&fimc->lock);
  1076. return 0;
  1077. }
  1078. static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
  1079. struct v4l2_subdev_fh *fh,
  1080. struct v4l2_subdev_format *fmt)
  1081. {
  1082. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1083. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1084. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1085. struct fimc_frame *ff;
  1086. struct fimc_fmt *ffmt;
  1087. dbg("pad%d: code: 0x%x, %dx%d",
  1088. fmt->pad, mf->code, mf->width, mf->height);
  1089. if (fmt->pad == FIMC_SD_PAD_SOURCE &&
  1090. vb2_is_busy(&fimc->vid_cap.vbq))
  1091. return -EBUSY;
  1092. mutex_lock(&fimc->lock);
  1093. ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
  1094. &mf->code, NULL, fmt->pad);
  1095. mutex_unlock(&fimc->lock);
  1096. mf->colorspace = V4L2_COLORSPACE_JPEG;
  1097. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1098. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  1099. *mf = fmt->format;
  1100. return 0;
  1101. }
  1102. /* Update RGB Alpha control state and value range */
  1103. fimc_alpha_ctrl_update(ctx);
  1104. fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
  1105. ff = fmt->pad == FIMC_SD_PAD_SINK ?
  1106. &ctx->s_frame : &ctx->d_frame;
  1107. mutex_lock(&fimc->lock);
  1108. set_frame_bounds(ff, mf->width, mf->height);
  1109. fimc->vid_cap.mf = *mf;
  1110. ff->fmt = ffmt;
  1111. /* Reset the crop rectangle if required. */
  1112. if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
  1113. set_frame_crop(ff, 0, 0, mf->width, mf->height);
  1114. if (fmt->pad == FIMC_SD_PAD_SINK)
  1115. ctx->state &= ~FIMC_DST_CROP;
  1116. mutex_unlock(&fimc->lock);
  1117. return 0;
  1118. }
  1119. static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
  1120. struct v4l2_subdev_fh *fh,
  1121. struct v4l2_subdev_crop *crop)
  1122. {
  1123. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1124. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1125. struct v4l2_rect *r = &crop->rect;
  1126. struct fimc_frame *ff;
  1127. if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
  1128. crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
  1129. return 0;
  1130. }
  1131. ff = crop->pad == FIMC_SD_PAD_SINK ?
  1132. &ctx->s_frame : &ctx->d_frame;
  1133. mutex_lock(&fimc->lock);
  1134. r->left = ff->offs_h;
  1135. r->top = ff->offs_v;
  1136. r->width = ff->width;
  1137. r->height = ff->height;
  1138. mutex_unlock(&fimc->lock);
  1139. dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
  1140. ff, crop->pad, r->left, r->top, r->width, r->height,
  1141. ff->f_width, ff->f_height);
  1142. return 0;
  1143. }
  1144. static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
  1145. struct v4l2_subdev_fh *fh,
  1146. struct v4l2_subdev_crop *crop)
  1147. {
  1148. struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
  1149. struct fimc_ctx *ctx = fimc->vid_cap.ctx;
  1150. struct v4l2_rect *r = &crop->rect;
  1151. struct fimc_frame *ff;
  1152. unsigned long flags;
  1153. dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
  1154. ff = crop->pad == FIMC_SD_PAD_SOURCE ?
  1155. &ctx->d_frame : &ctx->s_frame;
  1156. mutex_lock(&fimc->lock);
  1157. fimc_capture_try_crop(ctx, r, crop->pad);
  1158. if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
  1159. mutex_lock(&fimc->lock);
  1160. *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
  1161. return 0;
  1162. }
  1163. spin_lock_irqsave(&fimc->slock, flags);
  1164. set_frame_crop(ff, r->left, r->top, r->width, r->height);
  1165. if (crop->pad == FIMC_SD_PAD_SOURCE)
  1166. ctx->state |= FIMC_DST_CROP;
  1167. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  1168. spin_unlock_irqrestore(&fimc->slock, flags);
  1169. dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
  1170. r->width, r->height);
  1171. mutex_unlock(&fimc->lock);
  1172. return 0;
  1173. }
  1174. static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
  1175. .enum_mbus_code = fimc_subdev_enum_mbus_code,
  1176. .get_fmt = fimc_subdev_get_fmt,
  1177. .set_fmt = fimc_subdev_set_fmt,
  1178. .get_crop = fimc_subdev_get_crop,
  1179. .set_crop = fimc_subdev_set_crop,
  1180. };
  1181. static struct v4l2_subdev_ops fimc_subdev_ops = {
  1182. .pad = &fimc_subdev_pad_ops,
  1183. };
  1184. static int fimc_create_capture_subdev(struct fimc_dev *fimc,
  1185. struct v4l2_device *v4l2_dev)
  1186. {
  1187. struct v4l2_subdev *sd;
  1188. int ret;
  1189. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  1190. if (!sd)
  1191. return -ENOMEM;
  1192. v4l2_subdev_init(sd, &fimc_subdev_ops);
  1193. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1194. snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
  1195. fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1196. fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1197. ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
  1198. fimc->vid_cap.sd_pads, 0);
  1199. if (ret)
  1200. goto me_err;
  1201. ret = v4l2_device_register_subdev(v4l2_dev, sd);
  1202. if (ret)
  1203. goto sd_err;
  1204. fimc->vid_cap.subdev = sd;
  1205. v4l2_set_subdevdata(sd, fimc);
  1206. sd->entity.ops = &fimc_sd_media_ops;
  1207. return 0;
  1208. sd_err:
  1209. media_entity_cleanup(&sd->entity);
  1210. me_err:
  1211. kfree(sd);
  1212. return ret;
  1213. }
  1214. static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
  1215. {
  1216. struct v4l2_subdev *sd = fimc->vid_cap.subdev;
  1217. if (!sd)
  1218. return;
  1219. media_entity_cleanup(&sd->entity);
  1220. v4l2_device_unregister_subdev(sd);
  1221. kfree(sd);
  1222. fimc->vid_cap.subdev = NULL;
  1223. }
  1224. /* Set default format at the sensor and host interface */
  1225. static int fimc_capture_set_default_format(struct fimc_dev *fimc)
  1226. {
  1227. struct v4l2_format fmt = {
  1228. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  1229. .fmt.pix_mp = {
  1230. .width = 640,
  1231. .height = 480,
  1232. .pixelformat = V4L2_PIX_FMT_YUYV,
  1233. .field = V4L2_FIELD_NONE,
  1234. .colorspace = V4L2_COLORSPACE_JPEG,
  1235. },
  1236. };
  1237. return fimc_capture_set_format(fimc, &fmt);
  1238. }
  1239. /* fimc->lock must be already initialized */
  1240. int fimc_register_capture_device(struct fimc_dev *fimc,
  1241. struct v4l2_device *v4l2_dev)
  1242. {
  1243. struct video_device *vfd;
  1244. struct fimc_vid_cap *vid_cap;
  1245. struct fimc_ctx *ctx;
  1246. struct vb2_queue *q;
  1247. int ret = -ENOMEM;
  1248. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  1249. if (!ctx)
  1250. return -ENOMEM;
  1251. ctx->fimc_dev = fimc;
  1252. ctx->in_path = FIMC_CAMERA;
  1253. ctx->out_path = FIMC_DMA;
  1254. ctx->state = FIMC_CTX_CAP;
  1255. ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
  1256. ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
  1257. vfd = video_device_alloc();
  1258. if (!vfd) {
  1259. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  1260. goto err_vd_alloc;
  1261. }
  1262. snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
  1263. dev_name(&fimc->pdev->dev));
  1264. vfd->fops = &fimc_capture_fops;
  1265. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  1266. vfd->v4l2_dev = v4l2_dev;
  1267. vfd->minor = -1;
  1268. vfd->release = video_device_release;
  1269. vfd->lock = &fimc->lock;
  1270. video_set_drvdata(vfd, fimc);
  1271. vid_cap = &fimc->vid_cap;
  1272. vid_cap->vfd = vfd;
  1273. vid_cap->active_buf_cnt = 0;
  1274. vid_cap->reqbufs_count = 0;
  1275. vid_cap->refcnt = 0;
  1276. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  1277. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  1278. vid_cap->ctx = ctx;
  1279. q = &fimc->vid_cap.vbq;
  1280. memset(q, 0, sizeof(*q));
  1281. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1282. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1283. q->drv_priv = fimc->vid_cap.ctx;
  1284. q->ops = &fimc_capture_qops;
  1285. q->mem_ops = &vb2_dma_contig_memops;
  1286. q->buf_struct_size = sizeof(struct fimc_vid_buffer);
  1287. vb2_queue_init(q);
  1288. fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
  1289. ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
  1290. if (ret)
  1291. goto err_ent;
  1292. ret = fimc_create_capture_subdev(fimc, v4l2_dev);
  1293. if (ret)
  1294. goto err_sd_reg;
  1295. vfd->ctrl_handler = &ctx->ctrl_handler;
  1296. return 0;
  1297. err_sd_reg:
  1298. media_entity_cleanup(&vfd->entity);
  1299. err_ent:
  1300. video_device_release(vfd);
  1301. err_vd_alloc:
  1302. kfree(ctx);
  1303. return ret;
  1304. }
  1305. void fimc_unregister_capture_device(struct fimc_dev *fimc)
  1306. {
  1307. struct video_device *vfd = fimc->vid_cap.vfd;
  1308. if (vfd) {
  1309. media_entity_cleanup(&vfd->entity);
  1310. /* Can also be called if video device was
  1311. not registered */
  1312. video_unregister_device(vfd);
  1313. }
  1314. fimc_destroy_capture_subdev(fimc);
  1315. kfree(fimc->vid_cap.ctx);
  1316. fimc->vid_cap.ctx = NULL;
  1317. }