fimc-capture.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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-core.h"
  28. static void fimc_capture_state_cleanup(struct fimc_dev *fimc)
  29. {
  30. struct fimc_vid_cap *cap = &fimc->vid_cap;
  31. struct fimc_vid_buffer *buf;
  32. unsigned long flags;
  33. spin_lock_irqsave(&fimc->slock, flags);
  34. fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
  35. 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM);
  36. fimc->vid_cap.active_buf_cnt = 0;
  37. /* Release buffers that were enqueued in the driver by videobuf2. */
  38. while (!list_empty(&cap->pending_buf_q)) {
  39. buf = pending_queue_pop(cap);
  40. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  41. }
  42. while (!list_empty(&cap->active_buf_q)) {
  43. buf = active_queue_pop(cap);
  44. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  45. }
  46. spin_unlock_irqrestore(&fimc->slock, flags);
  47. }
  48. static int fimc_stop_capture(struct fimc_dev *fimc)
  49. {
  50. struct fimc_vid_cap *cap = &fimc->vid_cap;
  51. unsigned long flags;
  52. if (!fimc_capture_active(fimc))
  53. return 0;
  54. spin_lock_irqsave(&fimc->slock, flags);
  55. set_bit(ST_CAPT_SHUT, &fimc->state);
  56. fimc_deactivate_capture(fimc);
  57. spin_unlock_irqrestore(&fimc->slock, flags);
  58. wait_event_timeout(fimc->irq_queue,
  59. !test_bit(ST_CAPT_SHUT, &fimc->state),
  60. FIMC_SHUTDOWN_TIMEOUT);
  61. v4l2_subdev_call(cap->sd, video, s_stream, 0);
  62. fimc_capture_state_cleanup(fimc);
  63. dbg("state: 0x%lx", fimc->state);
  64. return 0;
  65. }
  66. static int start_streaming(struct vb2_queue *q, unsigned int count)
  67. {
  68. struct fimc_ctx *ctx = q->drv_priv;
  69. struct fimc_dev *fimc = ctx->fimc_dev;
  70. struct s5p_fimc_isp_info *isp_info;
  71. int min_bufs;
  72. int ret;
  73. fimc_hw_reset(fimc);
  74. ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
  75. if (ret && ret != -ENOIOCTLCMD)
  76. goto error;
  77. ret = fimc_prepare_config(ctx, ctx->state);
  78. if (ret)
  79. goto error;
  80. isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index];
  81. fimc_hw_set_camera_type(fimc, isp_info);
  82. fimc_hw_set_camera_source(fimc, isp_info);
  83. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  84. if (ctx->state & FIMC_PARAMS) {
  85. ret = fimc_set_scaler_info(ctx);
  86. if (ret) {
  87. err("Scaler setup error");
  88. goto error;
  89. }
  90. fimc_hw_set_input_path(ctx);
  91. fimc_hw_set_prescaler(ctx);
  92. fimc_hw_set_mainscaler(ctx);
  93. fimc_hw_set_target_format(ctx);
  94. fimc_hw_set_rotation(ctx);
  95. fimc_hw_set_effect(ctx);
  96. }
  97. fimc_hw_set_output_path(ctx);
  98. fimc_hw_set_out_dma(ctx);
  99. INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
  100. INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
  101. fimc->vid_cap.frame_count = 0;
  102. fimc->vid_cap.buf_index = 0;
  103. set_bit(ST_CAPT_PEND, &fimc->state);
  104. min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
  105. if (fimc->vid_cap.active_buf_cnt >= min_bufs)
  106. fimc_activate_capture(ctx);
  107. return 0;
  108. error:
  109. fimc_capture_state_cleanup(fimc);
  110. return ret;
  111. }
  112. static int stop_streaming(struct vb2_queue *q)
  113. {
  114. struct fimc_ctx *ctx = q->drv_priv;
  115. struct fimc_dev *fimc = ctx->fimc_dev;
  116. if (!fimc_capture_active(fimc))
  117. return -EINVAL;
  118. return fimc_stop_capture(fimc);
  119. }
  120. int fimc_capture_suspend(struct fimc_dev *fimc)
  121. {
  122. return -EBUSY;
  123. }
  124. int fimc_capture_resume(struct fimc_dev *fimc)
  125. {
  126. return 0;
  127. }
  128. static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
  129. {
  130. if (!fr || plane >= fr->fmt->memplanes)
  131. return 0;
  132. return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
  133. }
  134. static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
  135. unsigned int *num_planes, unsigned int sizes[],
  136. void *allocators[])
  137. {
  138. struct fimc_ctx *ctx = vq->drv_priv;
  139. struct fimc_fmt *fmt = ctx->d_frame.fmt;
  140. int i;
  141. if (!fmt)
  142. return -EINVAL;
  143. *num_planes = fmt->memplanes;
  144. for (i = 0; i < fmt->memplanes; i++) {
  145. sizes[i] = get_plane_size(&ctx->d_frame, i);
  146. allocators[i] = ctx->fimc_dev->alloc_ctx;
  147. }
  148. return 0;
  149. }
  150. static int buffer_prepare(struct vb2_buffer *vb)
  151. {
  152. struct vb2_queue *vq = vb->vb2_queue;
  153. struct fimc_ctx *ctx = vq->drv_priv;
  154. struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
  155. int i;
  156. if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  157. return -EINVAL;
  158. for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
  159. unsigned long size = get_plane_size(&ctx->d_frame, i);
  160. if (vb2_plane_size(vb, i) < size) {
  161. v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n",
  162. vb2_plane_size(vb, i), size);
  163. return -EINVAL;
  164. }
  165. vb2_set_plane_payload(vb, i, size);
  166. }
  167. return 0;
  168. }
  169. static void buffer_queue(struct vb2_buffer *vb)
  170. {
  171. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  172. struct fimc_dev *fimc = ctx->fimc_dev;
  173. struct fimc_vid_buffer *buf
  174. = container_of(vb, struct fimc_vid_buffer, vb);
  175. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  176. unsigned long flags;
  177. int min_bufs;
  178. spin_lock_irqsave(&fimc->slock, flags);
  179. fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
  180. if (!test_bit(ST_CAPT_STREAM, &fimc->state)
  181. && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
  182. /* Setup the buffer directly for processing. */
  183. int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
  184. vid_cap->buf_index;
  185. fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
  186. buf->index = vid_cap->buf_index;
  187. active_queue_add(vid_cap, buf);
  188. if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
  189. vid_cap->buf_index = 0;
  190. } else {
  191. fimc_pending_queue_add(vid_cap, buf);
  192. }
  193. min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
  194. if (vb2_is_streaming(&vid_cap->vbq) &&
  195. vid_cap->active_buf_cnt >= min_bufs &&
  196. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state))
  197. fimc_activate_capture(ctx);
  198. spin_unlock_irqrestore(&fimc->slock, flags);
  199. }
  200. static void fimc_lock(struct vb2_queue *vq)
  201. {
  202. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  203. mutex_lock(&ctx->fimc_dev->lock);
  204. }
  205. static void fimc_unlock(struct vb2_queue *vq)
  206. {
  207. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  208. mutex_unlock(&ctx->fimc_dev->lock);
  209. }
  210. static struct vb2_ops fimc_capture_qops = {
  211. .queue_setup = queue_setup,
  212. .buf_prepare = buffer_prepare,
  213. .buf_queue = buffer_queue,
  214. .wait_prepare = fimc_unlock,
  215. .wait_finish = fimc_lock,
  216. .start_streaming = start_streaming,
  217. .stop_streaming = stop_streaming,
  218. };
  219. static int fimc_capture_open(struct file *file)
  220. {
  221. struct fimc_dev *fimc = video_drvdata(file);
  222. int ret = 0;
  223. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  224. /* Return if the corresponding video mem2mem node is already opened. */
  225. if (fimc_m2m_active(fimc))
  226. return -EBUSY;
  227. ret = pm_runtime_get_sync(&fimc->pdev->dev);
  228. if (ret)
  229. return ret;
  230. ++fimc->vid_cap.refcnt;
  231. file->private_data = fimc->vid_cap.ctx;
  232. return 0;
  233. }
  234. static int fimc_capture_close(struct file *file)
  235. {
  236. struct fimc_dev *fimc = video_drvdata(file);
  237. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  238. if (--fimc->vid_cap.refcnt == 0) {
  239. fimc_stop_capture(fimc);
  240. vb2_queue_release(&fimc->vid_cap.vbq);
  241. }
  242. pm_runtime_put(&fimc->pdev->dev);
  243. return 0;
  244. }
  245. static unsigned int fimc_capture_poll(struct file *file,
  246. struct poll_table_struct *wait)
  247. {
  248. struct fimc_ctx *ctx = file->private_data;
  249. struct fimc_dev *fimc = ctx->fimc_dev;
  250. return vb2_poll(&fimc->vid_cap.vbq, file, wait);
  251. }
  252. static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
  253. {
  254. struct fimc_ctx *ctx = file->private_data;
  255. struct fimc_dev *fimc = ctx->fimc_dev;
  256. return vb2_mmap(&fimc->vid_cap.vbq, vma);
  257. }
  258. /* video device file operations */
  259. static const struct v4l2_file_operations fimc_capture_fops = {
  260. .owner = THIS_MODULE,
  261. .open = fimc_capture_open,
  262. .release = fimc_capture_close,
  263. .poll = fimc_capture_poll,
  264. .unlocked_ioctl = video_ioctl2,
  265. .mmap = fimc_capture_mmap,
  266. };
  267. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  268. struct v4l2_capability *cap)
  269. {
  270. struct fimc_ctx *ctx = file->private_data;
  271. struct fimc_dev *fimc = ctx->fimc_dev;
  272. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  273. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  274. cap->bus_info[0] = 0;
  275. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
  276. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  277. return 0;
  278. }
  279. /* Synchronize formats of the camera interface input and attached sensor. */
  280. static int sync_capture_fmt(struct fimc_ctx *ctx)
  281. {
  282. struct fimc_frame *frame = &ctx->s_frame;
  283. struct fimc_dev *fimc = ctx->fimc_dev;
  284. struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
  285. int ret;
  286. fmt->width = ctx->d_frame.o_width;
  287. fmt->height = ctx->d_frame.o_height;
  288. ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
  289. if (ret == -ENOIOCTLCMD) {
  290. err("s_mbus_fmt failed");
  291. return ret;
  292. }
  293. dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
  294. frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
  295. if (!frame->fmt) {
  296. err("fimc source format not found\n");
  297. return -EINVAL;
  298. }
  299. frame->f_width = fmt->width;
  300. frame->f_height = fmt->height;
  301. frame->width = fmt->width;
  302. frame->height = fmt->height;
  303. frame->o_width = fmt->width;
  304. frame->o_height = fmt->height;
  305. frame->offs_h = 0;
  306. frame->offs_v = 0;
  307. return 0;
  308. }
  309. static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
  310. struct v4l2_format *f)
  311. {
  312. struct fimc_ctx *ctx = priv;
  313. struct fimc_dev *fimc = ctx->fimc_dev;
  314. struct fimc_frame *frame;
  315. struct v4l2_pix_format_mplane *pix;
  316. int ret;
  317. int i;
  318. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  319. return -EINVAL;
  320. ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
  321. if (ret)
  322. return ret;
  323. if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
  324. return -EBUSY;
  325. frame = &ctx->d_frame;
  326. pix = &f->fmt.pix_mp;
  327. frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
  328. if (!frame->fmt) {
  329. err("fimc target format not found\n");
  330. return -EINVAL;
  331. }
  332. for (i = 0; i < frame->fmt->colplanes; i++) {
  333. frame->payload[i] =
  334. (pix->width * pix->height * frame->fmt->depth[i]) >> 3;
  335. }
  336. /* Output DMA frame pixel size and offsets. */
  337. frame->f_width = pix->plane_fmt[0].bytesperline * 8
  338. / frame->fmt->depth[0];
  339. frame->f_height = pix->height;
  340. frame->width = pix->width;
  341. frame->height = pix->height;
  342. frame->o_width = pix->width;
  343. frame->o_height = pix->height;
  344. frame->offs_h = 0;
  345. frame->offs_v = 0;
  346. ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
  347. ret = sync_capture_fmt(ctx);
  348. return ret;
  349. }
  350. static int fimc_cap_enum_input(struct file *file, void *priv,
  351. struct v4l2_input *i)
  352. {
  353. struct fimc_ctx *ctx = priv;
  354. if (i->index != 0)
  355. return -EINVAL;
  356. i->type = V4L2_INPUT_TYPE_CAMERA;
  357. return 0;
  358. }
  359. static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
  360. {
  361. return i == 0 ? i : -EINVAL;
  362. }
  363. static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
  364. {
  365. *i = 0;
  366. return 0;
  367. }
  368. static int fimc_cap_streamon(struct file *file, void *priv,
  369. enum v4l2_buf_type type)
  370. {
  371. struct fimc_ctx *ctx = priv;
  372. struct fimc_dev *fimc = ctx->fimc_dev;
  373. if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
  374. return -EBUSY;
  375. if (!(ctx->state & FIMC_DST_FMT)) {
  376. v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
  377. return -EINVAL;
  378. }
  379. return vb2_streamon(&fimc->vid_cap.vbq, type);
  380. }
  381. static int fimc_cap_streamoff(struct file *file, void *priv,
  382. enum v4l2_buf_type type)
  383. {
  384. struct fimc_ctx *ctx = priv;
  385. struct fimc_dev *fimc = ctx->fimc_dev;
  386. return vb2_streamoff(&fimc->vid_cap.vbq, type);
  387. }
  388. static int fimc_cap_reqbufs(struct file *file, void *priv,
  389. struct v4l2_requestbuffers *reqbufs)
  390. {
  391. struct fimc_ctx *ctx = priv;
  392. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  393. int ret;
  394. ret = vb2_reqbufs(&cap->vbq, reqbufs);
  395. if (!ret)
  396. cap->reqbufs_count = reqbufs->count;
  397. return ret;
  398. }
  399. static int fimc_cap_querybuf(struct file *file, void *priv,
  400. struct v4l2_buffer *buf)
  401. {
  402. struct fimc_ctx *ctx = priv;
  403. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  404. return vb2_querybuf(&cap->vbq, buf);
  405. }
  406. static int fimc_cap_qbuf(struct file *file, void *priv,
  407. struct v4l2_buffer *buf)
  408. {
  409. struct fimc_ctx *ctx = priv;
  410. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  411. return vb2_qbuf(&cap->vbq, buf);
  412. }
  413. static int fimc_cap_dqbuf(struct file *file, void *priv,
  414. struct v4l2_buffer *buf)
  415. {
  416. struct fimc_ctx *ctx = priv;
  417. return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
  418. file->f_flags & O_NONBLOCK);
  419. }
  420. static int fimc_cap_s_ctrl(struct file *file, void *priv,
  421. struct v4l2_control *ctrl)
  422. {
  423. struct fimc_ctx *ctx = priv;
  424. int ret = -EINVAL;
  425. /* Allow any controls but 90/270 rotation while streaming */
  426. if (!fimc_capture_active(ctx->fimc_dev) ||
  427. ctrl->id != V4L2_CID_ROTATE ||
  428. (ctrl->value != 90 && ctrl->value != 270)) {
  429. ret = check_ctrl_val(ctx, ctrl);
  430. if (!ret) {
  431. ret = fimc_s_ctrl(ctx, ctrl);
  432. if (!ret)
  433. ctx->state |= FIMC_PARAMS;
  434. }
  435. }
  436. if (ret == -EINVAL)
  437. ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
  438. core, s_ctrl, ctrl);
  439. return ret;
  440. }
  441. static int fimc_cap_cropcap(struct file *file, void *fh,
  442. struct v4l2_cropcap *cr)
  443. {
  444. struct fimc_frame *f;
  445. struct fimc_ctx *ctx = fh;
  446. if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  447. return -EINVAL;
  448. f = &ctx->s_frame;
  449. cr->bounds.left = 0;
  450. cr->bounds.top = 0;
  451. cr->bounds.width = f->o_width;
  452. cr->bounds.height = f->o_height;
  453. cr->defrect = cr->bounds;
  454. return 0;
  455. }
  456. static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  457. {
  458. struct fimc_frame *f;
  459. struct fimc_ctx *ctx = file->private_data;
  460. f = &ctx->s_frame;
  461. cr->c.left = f->offs_h;
  462. cr->c.top = f->offs_v;
  463. cr->c.width = f->width;
  464. cr->c.height = f->height;
  465. return 0;
  466. }
  467. static int fimc_cap_s_crop(struct file *file, void *fh,
  468. struct v4l2_crop *cr)
  469. {
  470. struct fimc_frame *f;
  471. struct fimc_ctx *ctx = file->private_data;
  472. struct fimc_dev *fimc = ctx->fimc_dev;
  473. int ret = -EINVAL;
  474. if (fimc_capture_active(fimc))
  475. return -EBUSY;
  476. ret = fimc_try_crop(ctx, cr);
  477. if (ret)
  478. return ret;
  479. if (!(ctx->state & FIMC_DST_FMT)) {
  480. v4l2_err(&fimc->vid_cap.v4l2_dev,
  481. "Capture color format not set\n");
  482. return -EINVAL; /* TODO: make sure this is the right value */
  483. }
  484. f = &ctx->s_frame;
  485. /* Check for the pixel scaling ratio when cropping input image. */
  486. ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
  487. ctx->d_frame.width, ctx->d_frame.height,
  488. ctx->rotation);
  489. if (ret) {
  490. v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range\n");
  491. return ret;
  492. }
  493. f->offs_h = cr->c.left;
  494. f->offs_v = cr->c.top;
  495. f->width = cr->c.width;
  496. f->height = cr->c.height;
  497. return 0;
  498. }
  499. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  500. .vidioc_querycap = fimc_vidioc_querycap_capture,
  501. .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
  502. .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
  503. .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
  504. .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
  505. .vidioc_reqbufs = fimc_cap_reqbufs,
  506. .vidioc_querybuf = fimc_cap_querybuf,
  507. .vidioc_qbuf = fimc_cap_qbuf,
  508. .vidioc_dqbuf = fimc_cap_dqbuf,
  509. .vidioc_streamon = fimc_cap_streamon,
  510. .vidioc_streamoff = fimc_cap_streamoff,
  511. .vidioc_queryctrl = fimc_vidioc_queryctrl,
  512. .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
  513. .vidioc_s_ctrl = fimc_cap_s_ctrl,
  514. .vidioc_g_crop = fimc_cap_g_crop,
  515. .vidioc_s_crop = fimc_cap_s_crop,
  516. .vidioc_cropcap = fimc_cap_cropcap,
  517. .vidioc_enum_input = fimc_cap_enum_input,
  518. .vidioc_s_input = fimc_cap_s_input,
  519. .vidioc_g_input = fimc_cap_g_input,
  520. };
  521. /* fimc->lock must be already initialized */
  522. int fimc_register_capture_device(struct fimc_dev *fimc)
  523. {
  524. struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
  525. struct video_device *vfd;
  526. struct fimc_vid_cap *vid_cap;
  527. struct fimc_ctx *ctx;
  528. struct v4l2_format f;
  529. struct fimc_frame *fr;
  530. struct vb2_queue *q;
  531. int ret;
  532. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  533. if (!ctx)
  534. return -ENOMEM;
  535. ctx->fimc_dev = fimc;
  536. ctx->in_path = FIMC_CAMERA;
  537. ctx->out_path = FIMC_DMA;
  538. ctx->state = FIMC_CTX_CAP;
  539. /* Default format of the output frames */
  540. f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
  541. fr = &ctx->d_frame;
  542. fr->fmt = find_format(&f, FMT_FLAGS_M2M);
  543. fr->width = fr->f_width = fr->o_width = 640;
  544. fr->height = fr->f_height = fr->o_height = 480;
  545. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  546. "%s.capture", dev_name(&fimc->pdev->dev));
  547. ret = v4l2_device_register(NULL, v4l2_dev);
  548. if (ret)
  549. goto err_info;
  550. vfd = video_device_alloc();
  551. if (!vfd) {
  552. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  553. goto err_v4l2_reg;
  554. }
  555. strlcpy(vfd->name, v4l2_dev->name, sizeof(vfd->name));
  556. vfd->fops = &fimc_capture_fops;
  557. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  558. vfd->v4l2_dev = v4l2_dev;
  559. vfd->minor = -1;
  560. vfd->release = video_device_release;
  561. vfd->lock = &fimc->lock;
  562. video_set_drvdata(vfd, fimc);
  563. vid_cap = &fimc->vid_cap;
  564. vid_cap->vfd = vfd;
  565. vid_cap->active_buf_cnt = 0;
  566. vid_cap->reqbufs_count = 0;
  567. vid_cap->refcnt = 0;
  568. /* Default color format for image sensor */
  569. vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
  570. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  571. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  572. spin_lock_init(&ctx->slock);
  573. vid_cap->ctx = ctx;
  574. q = &fimc->vid_cap.vbq;
  575. memset(q, 0, sizeof(*q));
  576. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  577. q->io_modes = VB2_MMAP | VB2_USERPTR;
  578. q->drv_priv = fimc->vid_cap.ctx;
  579. q->ops = &fimc_capture_qops;
  580. q->mem_ops = &vb2_dma_contig_memops;
  581. q->buf_struct_size = sizeof(struct fimc_vid_buffer);
  582. vb2_queue_init(q);
  583. fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
  584. ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
  585. if (ret)
  586. goto err_ent;
  587. return 0;
  588. err_ent:
  589. video_device_release(vfd);
  590. err_v4l2_reg:
  591. v4l2_device_unregister(v4l2_dev);
  592. err_info:
  593. kfree(ctx);
  594. dev_err(&fimc->pdev->dev, "failed to install\n");
  595. return ret;
  596. }
  597. void fimc_unregister_capture_device(struct fimc_dev *fimc)
  598. {
  599. struct video_device *vfd = fimc->vid_cap.vfd;
  600. if (vfd) {
  601. media_entity_cleanup(&vfd->entity);
  602. /* Can also be called if video device was
  603. not registered */
  604. video_unregister_device(vfd);
  605. }
  606. kfree(fimc->vid_cap.ctx);
  607. fimc->vid_cap.ctx = NULL;
  608. }