Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 2 | /* |
Sylwester Nawrocki | 3a3f944 | 2011-04-28 09:06:19 -0300 | [diff] [blame] | 3 | * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 4 | * |
Sylwester Nawrocki | 0c9204d | 2012-04-25 06:55:42 -0300 | [diff] [blame] | 5 | * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd. |
| 6 | * Sylwester Nawrocki <s.nawrocki@samsung.com> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/bug.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/device.h> |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 16 | #include <linux/pm_runtime.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 17 | #include <linux/list.h> |
| 18 | #include <linux/slab.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 19 | |
| 20 | #include <linux/videodev2.h> |
| 21 | #include <media/v4l2-device.h> |
| 22 | #include <media/v4l2-ioctl.h> |
| 23 | #include <media/v4l2-mem2mem.h> |
Benoit Parrot | 030b161 | 2020-05-28 15:26:05 +0200 | [diff] [blame] | 24 | #include <media/v4l2-rect.h> |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame] | 25 | #include <media/videobuf2-v4l2.h> |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 26 | #include <media/videobuf2-dma-contig.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 27 | |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 28 | #include "common.h" |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 29 | #include "fimc-core.h" |
Sylwester Nawrocki | c83a1ff | 2012-05-02 06:14:49 -0300 | [diff] [blame] | 30 | #include "fimc-reg.h" |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 31 | #include "media-dev.h" |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 32 | |
Sylwester Nawrocki | bb7c276 | 2012-04-27 09:33:23 -0300 | [diff] [blame] | 33 | static int fimc_capture_hw_init(struct fimc_dev *fimc) |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 34 | { |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 35 | struct fimc_source_info *si = &fimc->vid_cap.source_config; |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 36 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 37 | int ret; |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 38 | unsigned long flags; |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 39 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 40 | if (ctx == NULL || ctx->s_frame.fmt == NULL) |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 41 | return -EINVAL; |
| 42 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 43 | if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) { |
| 44 | ret = fimc_hw_camblk_cfg_writeback(fimc); |
| 45 | if (ret < 0) |
| 46 | return ret; |
| 47 | } |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 48 | |
| 49 | spin_lock_irqsave(&fimc->slock, flags); |
| 50 | fimc_prepare_dma_offset(ctx, &ctx->d_frame); |
| 51 | fimc_set_yuv_order(ctx); |
| 52 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 53 | fimc_hw_set_camera_polarity(fimc, si); |
| 54 | fimc_hw_set_camera_type(fimc, si); |
| 55 | fimc_hw_set_camera_source(fimc, si); |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 56 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); |
| 57 | |
| 58 | ret = fimc_set_scaler_info(ctx); |
| 59 | if (!ret) { |
| 60 | fimc_hw_set_input_path(ctx); |
| 61 | fimc_hw_set_prescaler(ctx); |
| 62 | fimc_hw_set_mainscaler(ctx); |
| 63 | fimc_hw_set_target_format(ctx); |
| 64 | fimc_hw_set_rotation(ctx); |
Sylwester Nawrocki | 9448ab7 | 2012-04-02 06:41:22 -0300 | [diff] [blame] | 65 | fimc_hw_set_effect(ctx); |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 66 | fimc_hw_set_output_path(ctx); |
| 67 | fimc_hw_set_out_dma(ctx); |
Sylwester Nawrocki | e80cb1f | 2013-03-26 08:22:21 -0300 | [diff] [blame] | 68 | if (fimc->drv_data->alpha_color) |
Sylwester Nawrocki | dafb9c7 | 2011-12-01 14:02:24 -0300 | [diff] [blame] | 69 | fimc_hw_set_rgb_alpha(ctx); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 70 | clear_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 71 | } |
| 72 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 73 | return ret; |
| 74 | } |
| 75 | |
Sylwester Nawrocki | bb7c276 | 2012-04-27 09:33:23 -0300 | [diff] [blame] | 76 | /* |
| 77 | * Reinitialize the driver so it is ready to start the streaming again. |
| 78 | * Set fimc->state to indicate stream off and the hardware shut down state. |
| 79 | * If not suspending (@suspend is false), return any buffers to videobuf2. |
| 80 | * Otherwise put any owned buffers onto the pending buffers queue, so they |
| 81 | * can be re-spun when the device is being resumed. Also perform FIMC |
| 82 | * software reset and disable streaming on the whole pipeline if required. |
| 83 | */ |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 84 | static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 85 | { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 86 | struct fimc_vid_cap *cap = &fimc->vid_cap; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 87 | struct fimc_vid_buffer *buf; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 88 | unsigned long flags; |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 89 | bool streaming; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 90 | |
| 91 | spin_lock_irqsave(&fimc->slock, flags); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 92 | streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 93 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 94 | fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT | |
| 95 | 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM); |
Sylwester Nawrocki | aa333122 | 2012-03-16 11:47:51 -0300 | [diff] [blame] | 96 | if (suspend) |
| 97 | fimc->state |= (1 << ST_CAPT_SUSPENDED); |
| 98 | else |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 99 | fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 100 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 101 | /* Release unused buffers */ |
| 102 | while (!suspend && !list_empty(&cap->pending_buf_q)) { |
Sylwester Nawrocki | 0295202 | 2011-06-10 15:36:59 -0300 | [diff] [blame] | 103 | buf = fimc_pending_queue_pop(cap); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 104 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 105 | } |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 106 | /* If suspending put unused buffers onto pending queue */ |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 107 | while (!list_empty(&cap->active_buf_q)) { |
Sylwester Nawrocki | 0295202 | 2011-06-10 15:36:59 -0300 | [diff] [blame] | 108 | buf = fimc_active_queue_pop(cap); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 109 | if (suspend) |
| 110 | fimc_pending_queue_add(cap, buf); |
| 111 | else |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 112 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 113 | } |
Sylwester Nawrocki | 2c1bb62 | 2011-10-05 14:20:45 -0300 | [diff] [blame] | 114 | |
| 115 | fimc_hw_reset(fimc); |
| 116 | cap->buf_index = 0; |
| 117 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 118 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 119 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 120 | if (streaming) |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 121 | return fimc_pipeline_call(&cap->ve, set_stream, 0); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 122 | else |
| 123 | return 0; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 124 | } |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 125 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 126 | static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend) |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 127 | { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 128 | unsigned long flags; |
| 129 | |
| 130 | if (!fimc_capture_active(fimc)) |
| 131 | return 0; |
| 132 | |
| 133 | spin_lock_irqsave(&fimc->slock, flags); |
| 134 | set_bit(ST_CAPT_SHUT, &fimc->state); |
| 135 | fimc_deactivate_capture(fimc); |
| 136 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 137 | |
| 138 | wait_event_timeout(fimc->irq_queue, |
| 139 | !test_bit(ST_CAPT_SHUT, &fimc->state), |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 140 | (2*HZ/10)); /* 200 ms */ |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 141 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 142 | return fimc_capture_state_cleanup(fimc, suspend); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 143 | } |
| 144 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 145 | /** |
| 146 | * fimc_capture_config_update - apply the camera interface configuration |
Mauro Carvalho Chehab | 3b060ba0 | 2017-11-29 04:32:51 -0500 | [diff] [blame] | 147 | * @ctx: FIMC capture context |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 148 | * |
| 149 | * To be called from within the interrupt handler with fimc.slock |
| 150 | * spinlock held. It updates the camera pixel crop, rotation and |
| 151 | * image flip in H/W. |
| 152 | */ |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 153 | static int fimc_capture_config_update(struct fimc_ctx *ctx) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 154 | { |
| 155 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 156 | int ret; |
| 157 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 158 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); |
Sylwester Nawrocki | efb13c3d | 2012-03-19 13:11:40 -0300 | [diff] [blame] | 159 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 160 | ret = fimc_set_scaler_info(ctx); |
Sylwester Nawrocki | efb13c3d | 2012-03-19 13:11:40 -0300 | [diff] [blame] | 161 | if (ret) |
| 162 | return ret; |
| 163 | |
| 164 | fimc_hw_set_prescaler(ctx); |
| 165 | fimc_hw_set_mainscaler(ctx); |
| 166 | fimc_hw_set_target_format(ctx); |
| 167 | fimc_hw_set_rotation(ctx); |
Sylwester Nawrocki | 9448ab7 | 2012-04-02 06:41:22 -0300 | [diff] [blame] | 168 | fimc_hw_set_effect(ctx); |
Sylwester Nawrocki | efb13c3d | 2012-03-19 13:11:40 -0300 | [diff] [blame] | 169 | fimc_prepare_dma_offset(ctx, &ctx->d_frame); |
| 170 | fimc_hw_set_out_dma(ctx); |
Sylwester Nawrocki | e80cb1f | 2013-03-26 08:22:21 -0300 | [diff] [blame] | 171 | if (fimc->drv_data->alpha_color) |
Sylwester Nawrocki | efb13c3d | 2012-03-19 13:11:40 -0300 | [diff] [blame] | 172 | fimc_hw_set_rgb_alpha(ctx); |
| 173 | |
| 174 | clear_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 175 | return ret; |
| 176 | } |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 177 | |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 178 | void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf) |
| 179 | { |
| 180 | struct fimc_vid_cap *cap = &fimc->vid_cap; |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 181 | struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe); |
| 182 | struct v4l2_subdev *csis = p->subdevs[IDX_CSIS]; |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 183 | struct fimc_frame *f = &cap->ctx->d_frame; |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 184 | struct fimc_vid_buffer *v_buf; |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 185 | |
| 186 | if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) { |
| 187 | wake_up(&fimc->irq_queue); |
| 188 | goto done; |
| 189 | } |
| 190 | |
| 191 | if (!list_empty(&cap->active_buf_q) && |
| 192 | test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) { |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 193 | v_buf = fimc_active_queue_pop(cap); |
| 194 | |
Junghak Sung | d6dd645 | 2015-11-03 08:16:37 -0200 | [diff] [blame] | 195 | v_buf->vb.vb2_buf.timestamp = ktime_get_ns(); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 196 | v_buf->vb.sequence = cap->frame_count++; |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 197 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 198 | vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (!list_empty(&cap->pending_buf_q)) { |
| 202 | |
| 203 | v_buf = fimc_pending_queue_pop(cap); |
| 204 | fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index); |
| 205 | v_buf->index = cap->buf_index; |
| 206 | |
| 207 | /* Move the buffer to the capture active queue */ |
| 208 | fimc_active_queue_add(cap, v_buf); |
| 209 | |
| 210 | dbg("next frame: %d, done frame: %d", |
| 211 | fimc_hw_get_frame_index(fimc), v_buf->index); |
| 212 | |
| 213 | if (++cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 214 | cap->buf_index = 0; |
| 215 | } |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 216 | /* |
| 217 | * Set up a buffer at MIPI-CSIS if current image format |
| 218 | * requires the frame embedded data capture. |
| 219 | */ |
| 220 | if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) { |
| 221 | unsigned int plane = ffs(f->fmt->mdataplanes) - 1; |
| 222 | unsigned int size = f->payload[plane]; |
| 223 | s32 index = fimc_hw_get_frame_index(fimc); |
| 224 | void *vaddr; |
| 225 | |
| 226 | list_for_each_entry(v_buf, &cap->active_buf_q, list) { |
| 227 | if (v_buf->index != index) |
| 228 | continue; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 229 | vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane); |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 230 | v4l2_subdev_call(csis, video, s_rx_buffer, |
| 231 | vaddr, &size); |
| 232 | break; |
| 233 | } |
| 234 | } |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 235 | |
| 236 | if (cap->active_buf_cnt == 0) { |
| 237 | if (deq_buf) |
| 238 | clear_bit(ST_CAPT_RUN, &fimc->state); |
| 239 | |
| 240 | if (++cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 241 | cap->buf_index = 0; |
| 242 | } else { |
| 243 | set_bit(ST_CAPT_RUN, &fimc->state); |
| 244 | } |
| 245 | |
Sylwester Nawrocki | bb7c276 | 2012-04-27 09:33:23 -0300 | [diff] [blame] | 246 | if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state)) |
| 247 | fimc_capture_config_update(cap->ctx); |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 248 | done: |
| 249 | if (cap->active_buf_cnt == 1) { |
| 250 | fimc_deactivate_capture(fimc); |
| 251 | clear_bit(ST_CAPT_STREAM, &fimc->state); |
| 252 | } |
| 253 | |
| 254 | dbg("frame: %d, active_buf_cnt: %d", |
| 255 | fimc_hw_get_frame_index(fimc), cap->active_buf_cnt); |
| 256 | } |
| 257 | |
| 258 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 259 | static int start_streaming(struct vb2_queue *q, unsigned int count) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 260 | { |
| 261 | struct fimc_ctx *ctx = q->drv_priv; |
| 262 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 263 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 264 | int min_bufs; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 265 | int ret; |
| 266 | |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 267 | vid_cap->frame_count = 0; |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 268 | |
Sylwester Nawrocki | bb7c276 | 2012-04-27 09:33:23 -0300 | [diff] [blame] | 269 | ret = fimc_capture_hw_init(fimc); |
| 270 | if (ret) { |
| 271 | fimc_capture_state_cleanup(fimc, false); |
| 272 | return ret; |
| 273 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 274 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 275 | set_bit(ST_CAPT_PEND, &fimc->state); |
| 276 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 277 | min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1; |
| 278 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 279 | if (vid_cap->active_buf_cnt >= min_bufs && |
| 280 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 281 | fimc_activate_capture(ctx); |
| 282 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 283 | if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state)) |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 284 | return fimc_pipeline_call(&vid_cap->ve, set_stream, 1); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 285 | } |
| 286 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 290 | static void stop_streaming(struct vb2_queue *q) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 291 | { |
| 292 | struct fimc_ctx *ctx = q->drv_priv; |
| 293 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 294 | |
Sylwester Nawrocki | 4ecbf5d | 2011-02-23 08:24:33 -0300 | [diff] [blame] | 295 | if (!fimc_capture_active(fimc)) |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 296 | return; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 297 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 298 | fimc_stop_capture(fimc, false); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 299 | } |
| 300 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 301 | int fimc_capture_suspend(struct fimc_dev *fimc) |
| 302 | { |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 303 | bool suspend = fimc_capture_busy(fimc); |
| 304 | |
| 305 | int ret = fimc_stop_capture(fimc, suspend); |
| 306 | if (ret) |
| 307 | return ret; |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 308 | return fimc_pipeline_call(&fimc->vid_cap.ve, close); |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 309 | } |
| 310 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 311 | static void buffer_queue(struct vb2_buffer *vb); |
| 312 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 313 | int fimc_capture_resume(struct fimc_dev *fimc) |
| 314 | { |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 315 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 316 | struct exynos_video_entity *ve = &vid_cap->ve; |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 317 | struct fimc_vid_buffer *buf; |
| 318 | int i; |
| 319 | |
| 320 | if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state)) |
| 321 | return 0; |
| 322 | |
| 323 | INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q); |
| 324 | vid_cap->buf_index = 0; |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 325 | fimc_pipeline_call(ve, open, &ve->vdev.entity, false); |
Sylwester Nawrocki | bb7c276 | 2012-04-27 09:33:23 -0300 | [diff] [blame] | 326 | fimc_capture_hw_init(fimc); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 327 | |
| 328 | clear_bit(ST_CAPT_SUSPENDED, &fimc->state); |
| 329 | |
| 330 | for (i = 0; i < vid_cap->reqbufs_count; i++) { |
| 331 | if (list_empty(&vid_cap->pending_buf_q)) |
| 332 | break; |
| 333 | buf = fimc_pending_queue_pop(vid_cap); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 334 | buffer_queue(&buf->vb.vb2_buf); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 335 | } |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 336 | return 0; |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 337 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 338 | } |
| 339 | |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 340 | static int queue_setup(struct vb2_queue *vq, |
Guennadi Liakhovetski | fc714e70 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 341 | unsigned int *num_buffers, unsigned int *num_planes, |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 342 | unsigned int sizes[], struct device *alloc_devs[]) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 343 | { |
| 344 | struct fimc_ctx *ctx = vq->drv_priv; |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 345 | struct fimc_frame *frame = &ctx->d_frame; |
| 346 | struct fimc_fmt *fmt = frame->fmt; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 347 | unsigned long wh = frame->f_width * frame->f_height; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 348 | int i; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 349 | |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 350 | if (fmt == NULL) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 351 | return -EINVAL; |
| 352 | |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 353 | if (*num_planes) { |
| 354 | if (*num_planes != fmt->memplanes) |
| 355 | return -EINVAL; |
Hans Verkuil | 2548fee | 2016-02-16 07:30:19 -0200 | [diff] [blame] | 356 | for (i = 0; i < *num_planes; i++) |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 357 | if (sizes[i] < (wh * fmt->depth[i]) / 8) |
| 358 | return -EINVAL; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 362 | *num_planes = fmt->memplanes; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 363 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 364 | for (i = 0; i < fmt->memplanes; i++) { |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 365 | unsigned int size = (wh * fmt->depth[i]) / 8; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 366 | |
| 367 | if (fimc_fmt_is_user_defined(fmt->color)) |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 368 | sizes[i] = frame->payload[i]; |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 369 | else |
Sylwester Nawrocki | d547ab6 | 2012-05-16 15:00:26 -0300 | [diff] [blame] | 370 | sizes[i] = max_t(u32, size, frame->payload[i]); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 371 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 372 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 373 | return 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 374 | } |
| 375 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 376 | static int buffer_prepare(struct vb2_buffer *vb) |
| 377 | { |
| 378 | struct vb2_queue *vq = vb->vb2_queue; |
| 379 | struct fimc_ctx *ctx = vq->drv_priv; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 380 | int i; |
| 381 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 382 | if (ctx->d_frame.fmt == NULL) |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 383 | return -EINVAL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 384 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 385 | for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) { |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 386 | unsigned long size = ctx->d_frame.payload[i]; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 387 | |
| 388 | if (vb2_plane_size(vb, i) < size) { |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 389 | v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev, |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 390 | "User buffer too small (%ld < %ld)\n", |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 391 | vb2_plane_size(vb, i), size); |
| 392 | return -EINVAL; |
| 393 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 394 | vb2_set_plane_payload(vb, i, size); |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static void buffer_queue(struct vb2_buffer *vb) |
| 401 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 402 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 403 | struct fimc_vid_buffer *buf |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 404 | = container_of(vbuf, struct fimc_vid_buffer, vb); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 405 | struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 406 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 407 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 408 | struct exynos_video_entity *ve = &vid_cap->ve; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 409 | unsigned long flags; |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 410 | int min_bufs; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 411 | |
| 412 | spin_lock_irqsave(&fimc->slock, flags); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 413 | fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 414 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 415 | if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) && |
| 416 | !test_bit(ST_CAPT_STREAM, &fimc->state) && |
| 417 | vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) { |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 418 | /* Setup the buffer directly for processing. */ |
| 419 | int buf_id = (vid_cap->reqbufs_count == 1) ? -1 : |
| 420 | vid_cap->buf_index; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 421 | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 422 | fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id); |
| 423 | buf->index = vid_cap->buf_index; |
Sylwester Nawrocki | 0295202 | 2011-06-10 15:36:59 -0300 | [diff] [blame] | 424 | fimc_active_queue_add(vid_cap, buf); |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 425 | |
| 426 | if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 427 | vid_cap->buf_index = 0; |
| 428 | } else { |
| 429 | fimc_pending_queue_add(vid_cap, buf); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 430 | } |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 431 | |
| 432 | min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1; |
| 433 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 434 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 435 | if (vb2_is_streaming(&vid_cap->vbq) && |
| 436 | vid_cap->active_buf_cnt >= min_bufs && |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 437 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { |
Andrzej Hajda | 76323e5 | 2013-02-27 08:13:59 -0300 | [diff] [blame] | 438 | int ret; |
| 439 | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 440 | fimc_activate_capture(ctx); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 441 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 442 | |
Andrzej Hajda | 76323e5 | 2013-02-27 08:13:59 -0300 | [diff] [blame] | 443 | if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state)) |
| 444 | return; |
| 445 | |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 446 | ret = fimc_pipeline_call(ve, set_stream, 1); |
Andrzej Hajda | 76323e5 | 2013-02-27 08:13:59 -0300 | [diff] [blame] | 447 | if (ret < 0) |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 448 | v4l2_err(&ve->vdev, "stream on failed: %d\n", ret); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 449 | return; |
| 450 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 451 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 452 | } |
| 453 | |
Julia Lawall | b7b361f | 2016-09-08 20:59:10 -0300 | [diff] [blame] | 454 | static const struct vb2_ops fimc_capture_qops = { |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 455 | .queue_setup = queue_setup, |
| 456 | .buf_prepare = buffer_prepare, |
| 457 | .buf_queue = buffer_queue, |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 458 | .wait_prepare = vb2_ops_wait_prepare, |
| 459 | .wait_finish = vb2_ops_wait_finish, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 460 | .start_streaming = start_streaming, |
| 461 | .stop_streaming = stop_streaming, |
| 462 | }; |
| 463 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 464 | static int fimc_capture_set_default_format(struct fimc_dev *fimc); |
| 465 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 466 | static int fimc_capture_open(struct file *file) |
| 467 | { |
| 468 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 469 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
| 470 | struct exynos_video_entity *ve = &vc->ve; |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 471 | int ret = -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 472 | |
| 473 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 474 | |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 475 | mutex_lock(&fimc->lock); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 476 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 477 | if (fimc_m2m_active(fimc)) |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 478 | goto unlock; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 479 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 480 | set_bit(ST_CAPT_BUSY, &fimc->state); |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 481 | ret = pm_runtime_get_sync(&fimc->pdev->dev); |
Dinghao Liu | e40a877 | 2020-05-21 12:22:04 +0200 | [diff] [blame] | 482 | if (ret < 0) { |
| 483 | pm_runtime_put_sync(&fimc->pdev->dev); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 484 | goto unlock; |
Dinghao Liu | e40a877 | 2020-05-21 12:22:04 +0200 | [diff] [blame] | 485 | } |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 486 | |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 487 | ret = v4l2_fh_open(file); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 488 | if (ret) { |
Sylwester Nawrocki | 4bd728a | 2013-05-31 11:37:21 -0300 | [diff] [blame] | 489 | pm_runtime_put_sync(&fimc->pdev->dev); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 490 | goto unlock; |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 491 | } |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 492 | |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 493 | if (v4l2_fh_is_singular_file(file)) { |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 494 | fimc_md_graph_lock(ve); |
| 495 | |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 496 | ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true); |
| 497 | |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 498 | if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) { |
| 499 | /* |
| 500 | * Recreate controls of the the video node to drop |
| 501 | * any controls inherited from the sensor subdev. |
| 502 | */ |
| 503 | fimc_ctrls_delete(vc->ctx); |
| 504 | |
| 505 | ret = fimc_ctrls_create(vc->ctx); |
| 506 | if (ret == 0) |
| 507 | vc->inh_sensor_ctrls = false; |
| 508 | } |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 509 | if (ret == 0) |
| 510 | ve->vdev.entity.use_count++; |
| 511 | |
| 512 | fimc_md_graph_unlock(ve); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 513 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 514 | if (ret == 0) |
| 515 | ret = fimc_capture_set_default_format(fimc); |
| 516 | |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 517 | if (ret < 0) { |
| 518 | clear_bit(ST_CAPT_BUSY, &fimc->state); |
| 519 | pm_runtime_put_sync(&fimc->pdev->dev); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 520 | v4l2_fh_release(file); |
| 521 | } |
| 522 | } |
| 523 | unlock: |
| 524 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 525 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 526 | } |
| 527 | |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 528 | static int fimc_capture_release(struct file *file) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 529 | { |
| 530 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 9ea89e2 | 2013-03-25 16:50:50 -0300 | [diff] [blame] | 531 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 532 | bool close = v4l2_fh_is_singular_file(file); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 533 | int ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 534 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 535 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 536 | |
Sylwester Nawrocki | ba6b372 | 2012-11-22 11:12:16 -0300 | [diff] [blame] | 537 | mutex_lock(&fimc->lock); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 538 | |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 539 | if (close && vc->streaming) { |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 540 | media_pipeline_stop(&vc->ve.vdev.entity); |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 541 | vc->streaming = false; |
| 542 | } |
| 543 | |
Ricardo Ribalda | 1380f57 | 2013-11-25 05:49:02 -0300 | [diff] [blame] | 544 | ret = _vb2_fop_release(file, NULL); |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 545 | |
| 546 | if (close) { |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 547 | clear_bit(ST_CAPT_BUSY, &fimc->state); |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 548 | fimc_pipeline_call(&vc->ve, close); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 549 | clear_bit(ST_CAPT_SUSPENDED, &fimc->state); |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 550 | |
| 551 | fimc_md_graph_lock(&vc->ve); |
| 552 | vc->ve.vdev.entity.use_count--; |
| 553 | fimc_md_graph_unlock(&vc->ve); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 554 | } |
| 555 | |
Sylwester Nawrocki | 4bd728a | 2013-05-31 11:37:21 -0300 | [diff] [blame] | 556 | pm_runtime_put_sync(&fimc->pdev->dev); |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 557 | mutex_unlock(&fimc->lock); |
| 558 | |
| 559 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 560 | } |
| 561 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 562 | static const struct v4l2_file_operations fimc_capture_fops = { |
| 563 | .owner = THIS_MODULE, |
| 564 | .open = fimc_capture_open, |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 565 | .release = fimc_capture_release, |
| 566 | .poll = vb2_fop_poll, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 567 | .unlocked_ioctl = video_ioctl2, |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 568 | .mmap = vb2_fop_mmap, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 569 | }; |
| 570 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 571 | /* |
| 572 | * Format and crop negotiation helpers |
| 573 | */ |
| 574 | |
| 575 | static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx, |
| 576 | u32 *width, u32 *height, |
| 577 | u32 *code, u32 *fourcc, int pad) |
| 578 | { |
| 579 | bool rotation = ctx->rotation == 90 || ctx->rotation == 270; |
| 580 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 405f230 | 2012-08-02 10:27:46 -0300 | [diff] [blame] | 581 | const struct fimc_variant *var = fimc->variant; |
| 582 | const struct fimc_pix_limit *pl = var->pix_limit; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 583 | struct fimc_frame *dst = &ctx->d_frame; |
| 584 | u32 depth, min_w, max_w, min_h, align_h = 3; |
| 585 | u32 mask = FMT_FLAGS_CAM; |
| 586 | struct fimc_fmt *ffmt; |
| 587 | |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 588 | /* Conversion from/to JPEG or User Defined format is not supported */ |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 589 | if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE && |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 590 | fimc_fmt_is_user_defined(ctx->s_frame.fmt->color)) |
| 591 | *code = ctx->s_frame.fmt->mbus_code; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 592 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 593 | if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 594 | mask |= FMT_FLAGS_M2M; |
| 595 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 596 | if (pad == FIMC_SD_PAD_SINK_FIFO) |
| 597 | mask = FMT_FLAGS_WRITEBACK; |
| 598 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 599 | ffmt = fimc_find_format(fourcc, code, mask, 0); |
| 600 | if (WARN_ON(!ffmt)) |
| 601 | return NULL; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 602 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 603 | if (code) |
| 604 | *code = ffmt->mbus_code; |
| 605 | if (fourcc) |
| 606 | *fourcc = ffmt->fourcc; |
| 607 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 608 | if (pad != FIMC_SD_PAD_SOURCE) { |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 609 | max_w = fimc_fmt_is_user_defined(ffmt->color) ? |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 610 | pl->scaler_dis_w : pl->scaler_en_w; |
| 611 | /* Apply the camera input interface pixel constraints */ |
| 612 | v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4, |
| 613 | height, max_t(u32, *height, 32), |
| 614 | FIMC_CAMIF_MAX_HEIGHT, |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 615 | fimc_fmt_is_user_defined(ffmt->color) ? |
| 616 | 3 : 1, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 617 | 0); |
| 618 | return ffmt; |
| 619 | } |
| 620 | /* Can't scale or crop in transparent (JPEG) transfer mode */ |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 621 | if (fimc_fmt_is_user_defined(ffmt->color)) { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 622 | *width = ctx->s_frame.f_width; |
| 623 | *height = ctx->s_frame.f_height; |
| 624 | return ffmt; |
| 625 | } |
| 626 | /* Apply the scaler and the output DMA constraints */ |
| 627 | max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w; |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 628 | if (ctx->state & FIMC_COMPOSE) { |
| 629 | min_w = dst->offs_h + dst->width; |
| 630 | min_h = dst->offs_v + dst->height; |
| 631 | } else { |
| 632 | min_w = var->min_out_pixsize; |
| 633 | min_h = var->min_out_pixsize; |
| 634 | } |
Sylwester Nawrocki | 9c63afc | 2011-05-27 13:12:23 -0300 | [diff] [blame] | 635 | if (var->min_vsize_align == 1 && !rotation) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 636 | align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1; |
| 637 | |
| 638 | depth = fimc_get_format_depth(ffmt); |
| 639 | v4l_bound_align_image(width, min_w, max_w, |
| 640 | ffs(var->min_out_pixsize) - 1, |
| 641 | height, min_h, FIMC_CAMIF_MAX_HEIGHT, |
| 642 | align_h, |
| 643 | 64/(ALIGN(depth, 8))); |
| 644 | |
| 645 | dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d", |
| 646 | pad, code ? *code : 0, *width, *height, |
| 647 | dst->f_width, dst->f_height); |
| 648 | |
| 649 | return ffmt; |
| 650 | } |
| 651 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 652 | static void fimc_capture_try_selection(struct fimc_ctx *ctx, |
| 653 | struct v4l2_rect *r, |
| 654 | int target) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 655 | { |
| 656 | bool rotate = ctx->rotation == 90 || ctx->rotation == 270; |
| 657 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 405f230 | 2012-08-02 10:27:46 -0300 | [diff] [blame] | 658 | const struct fimc_variant *var = fimc->variant; |
| 659 | const struct fimc_pix_limit *pl = var->pix_limit; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 660 | struct fimc_frame *sink = &ctx->s_frame; |
| 661 | u32 max_w, max_h, min_w = 0, min_h = 0, min_sz; |
| 662 | u32 align_sz = 0, align_h = 4; |
| 663 | u32 max_sc_h, max_sc_v; |
| 664 | |
| 665 | /* In JPEG transparent transfer mode cropping is not supported */ |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 666 | if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 667 | r->width = sink->f_width; |
| 668 | r->height = sink->f_height; |
| 669 | r->left = r->top = 0; |
| 670 | return; |
| 671 | } |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 672 | if (target == V4L2_SEL_TGT_COMPOSE) { |
Mauro Carvalho Chehab | 6ccd228 | 2018-03-23 08:54:22 -0400 | [diff] [blame] | 673 | u32 tmp_min_h = ffs(sink->width) - 3; |
| 674 | u32 tmp_min_v = ffs(sink->height) - 1; |
| 675 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 676 | if (ctx->rotation != 90 && ctx->rotation != 270) |
| 677 | align_h = 1; |
Mauro Carvalho Chehab | 6ccd228 | 2018-03-23 08:54:22 -0400 | [diff] [blame] | 678 | max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h); |
| 679 | max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 680 | min_sz = var->min_out_pixsize; |
| 681 | } else { |
| 682 | u32 depth = fimc_get_format_depth(sink->fmt); |
| 683 | align_sz = 64/ALIGN(depth, 8); |
| 684 | min_sz = var->min_inp_pixsize; |
| 685 | min_w = min_h = min_sz; |
| 686 | max_sc_h = max_sc_v = 1; |
| 687 | } |
| 688 | /* |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 689 | * For the compose rectangle the following constraints must be met: |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 690 | * - it must fit in the sink pad format rectangle (f_width/f_height); |
| 691 | * - maximum downscaling ratio is 64; |
| 692 | * - maximum crop size depends if the rotator is used or not; |
| 693 | * - the sink pad format width/height must be 4 multiple of the |
| 694 | * prescaler ratios determined by sink pad size and source pad crop, |
| 695 | * the prescaler ratio is returned by fimc_get_scaler_factor(). |
| 696 | */ |
| 697 | max_w = min_t(u32, |
| 698 | rotate ? pl->out_rot_en_w : pl->out_rot_dis_w, |
| 699 | rotate ? sink->f_height : sink->f_width); |
| 700 | max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 701 | |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 702 | if (target == V4L2_SEL_TGT_COMPOSE) { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 703 | min_w = min_t(u32, max_w, sink->f_width / max_sc_h); |
| 704 | min_h = min_t(u32, max_h, sink->f_height / max_sc_v); |
| 705 | if (rotate) { |
| 706 | swap(max_sc_h, max_sc_v); |
| 707 | swap(min_w, min_h); |
| 708 | } |
| 709 | } |
| 710 | v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1, |
| 711 | &r->height, min_h, max_h, align_h, |
| 712 | align_sz); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 713 | /* Adjust left/top if crop/compose rectangle is out of bounds */ |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 714 | r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width); |
| 715 | r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height); |
| 716 | r->left = round_down(r->left, var->hor_offs_align); |
| 717 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 718 | dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d", |
| 719 | target, r->left, r->top, r->width, r->height, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 720 | sink->f_width, sink->f_height); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * The video node ioctl operations |
| 725 | */ |
Sylwester Nawrocki | aceb59e | 2013-04-23 11:36:04 -0300 | [diff] [blame] | 726 | static int fimc_cap_querycap(struct file *file, void *priv, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 727 | struct v4l2_capability *cap) |
| 728 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 729 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 730 | |
Boris Brezillon | 1ddc8a9 | 2019-06-04 03:06:24 -0400 | [diff] [blame] | 731 | __fimc_vidioc_querycap(&fimc->pdev->dev, cap); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Boris Brezillon | 7e98b7b | 2019-06-04 03:06:25 -0400 | [diff] [blame] | 735 | static int fimc_cap_enum_fmt(struct file *file, void *priv, |
| 736 | struct v4l2_fmtdesc *f) |
Sylwester Nawrocki | cf52df8 | 2011-06-13 11:09:40 -0300 | [diff] [blame] | 737 | { |
| 738 | struct fimc_fmt *fmt; |
| 739 | |
| 740 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M, |
| 741 | f->index); |
| 742 | if (!fmt) |
| 743 | return -EINVAL; |
Sylwester Nawrocki | cf52df8 | 2011-06-13 11:09:40 -0300 | [diff] [blame] | 744 | f->pixelformat = fmt->fourcc; |
Sylwester Nawrocki | cf52df8 | 2011-06-13 11:09:40 -0300 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 748 | static struct media_entity *fimc_pipeline_get_head(struct media_entity *me) |
| 749 | { |
| 750 | struct media_pad *pad = &me->pads[0]; |
| 751 | |
| 752 | while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) { |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 753 | pad = media_entity_remote_pad(pad); |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 754 | if (!pad) |
| 755 | break; |
| 756 | me = pad->entity; |
| 757 | pad = &me->pads[0]; |
| 758 | } |
| 759 | |
| 760 | return me; |
| 761 | } |
| 762 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 763 | /** |
| 764 | * fimc_pipeline_try_format - negotiate and/or set formats at pipeline |
| 765 | * elements |
| 766 | * @ctx: FIMC capture context |
| 767 | * @tfmt: media bus format to try/set on subdevs |
| 768 | * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output) |
| 769 | * @set: true to set format on subdevs, false to try only |
| 770 | */ |
| 771 | static int fimc_pipeline_try_format(struct fimc_ctx *ctx, |
| 772 | struct v4l2_mbus_framefmt *tfmt, |
| 773 | struct fimc_fmt **fmt_id, |
| 774 | bool set) |
| 775 | { |
| 776 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 777 | struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe); |
| 778 | struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR]; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 779 | struct v4l2_subdev_format sfmt; |
| 780 | struct v4l2_mbus_framefmt *mf = &sfmt.format; |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 781 | struct media_entity *me; |
| 782 | struct fimc_fmt *ffmt; |
| 783 | struct media_pad *pad; |
| 784 | int ret, i = 1; |
| 785 | u32 fcc; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 786 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 787 | if (WARN_ON(!sd || !tfmt)) |
| 788 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 789 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 790 | memset(&sfmt, 0, sizeof(sfmt)); |
| 791 | sfmt.format = *tfmt; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 792 | sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY; |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 793 | |
| 794 | me = fimc_pipeline_get_head(&sd->entity); |
| 795 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 796 | while (1) { |
| 797 | ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL, |
| 798 | FMT_FLAGS_CAM, i++); |
| 799 | if (ffmt == NULL) { |
| 800 | /* |
| 801 | * Notify user-space if common pixel code for |
| 802 | * host and sensor does not exist. |
| 803 | */ |
| 804 | return -EINVAL; |
| 805 | } |
| 806 | mf->code = tfmt->code = ffmt->mbus_code; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 807 | |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 808 | /* set format on all pipeline subdevs */ |
| 809 | while (me != &fimc->vid_cap.subdev.entity) { |
| 810 | sd = media_entity_to_v4l2_subdev(me); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 811 | |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 812 | sfmt.pad = 0; |
| 813 | ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt); |
| 814 | if (ret) |
| 815 | return ret; |
| 816 | |
| 817 | if (me->pads[0].flags & MEDIA_PAD_FL_SINK) { |
| 818 | sfmt.pad = me->num_pads - 1; |
| 819 | mf->code = tfmt->code; |
| 820 | ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, |
| 821 | &sfmt); |
| 822 | if (ret) |
| 823 | return ret; |
| 824 | } |
| 825 | |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 826 | pad = media_entity_remote_pad(&me->pads[sfmt.pad]); |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 827 | if (!pad) |
| 828 | return -EINVAL; |
| 829 | me = pad->entity; |
| 830 | } |
| 831 | |
| 832 | if (mf->code != tfmt->code) |
| 833 | continue; |
| 834 | |
| 835 | fcc = ffmt->fourcc; |
| 836 | tfmt->width = mf->width; |
| 837 | tfmt->height = mf->height; |
| 838 | ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height, |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 839 | NULL, &fcc, FIMC_SD_PAD_SINK_CAM); |
Sylwester Nawrocki | 47800bc | 2012-12-03 06:24:32 -0300 | [diff] [blame] | 840 | ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height, |
| 841 | NULL, &fcc, FIMC_SD_PAD_SOURCE); |
| 842 | if (ffmt && ffmt->mbus_code) |
| 843 | mf->code = ffmt->mbus_code; |
| 844 | if (mf->width != tfmt->width || mf->height != tfmt->height) |
| 845 | continue; |
| 846 | tfmt->code = mf->code; |
| 847 | break; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | if (fmt_id && ffmt) |
| 851 | *fmt_id = ffmt; |
| 852 | *tfmt = *mf; |
| 853 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 854 | return 0; |
| 855 | } |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 856 | |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 857 | /** |
| 858 | * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters |
| 859 | * @sensor: pointer to the sensor subdev |
| 860 | * @plane_fmt: provides plane sizes corresponding to the frame layout entries |
Mauro Carvalho Chehab | 3b060ba0 | 2017-11-29 04:32:51 -0500 | [diff] [blame] | 861 | * @num_planes: number of planes |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 862 | * @try: true to set the frame parameters, false to query only |
| 863 | * |
| 864 | * This function is used by this driver only for compressed/blob data formats. |
| 865 | */ |
| 866 | static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor, |
| 867 | struct v4l2_plane_pix_format *plane_fmt, |
| 868 | unsigned int num_planes, bool try) |
| 869 | { |
| 870 | struct v4l2_mbus_frame_desc fd; |
| 871 | int i, ret; |
Andrzej Hajda | 1c9f5bd | 2012-11-22 12:13:27 -0300 | [diff] [blame] | 872 | int pad; |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 873 | |
| 874 | for (i = 0; i < num_planes; i++) |
| 875 | fd.entry[i].length = plane_fmt[i].sizeimage; |
| 876 | |
Andrzej Hajda | 1c9f5bd | 2012-11-22 12:13:27 -0300 | [diff] [blame] | 877 | pad = sensor->entity.num_pads - 1; |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 878 | if (try) |
Andrzej Hajda | 1c9f5bd | 2012-11-22 12:13:27 -0300 | [diff] [blame] | 879 | ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd); |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 880 | else |
Andrzej Hajda | 1c9f5bd | 2012-11-22 12:13:27 -0300 | [diff] [blame] | 881 | ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd); |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 882 | |
| 883 | if (ret < 0) |
| 884 | return ret; |
| 885 | |
| 886 | if (num_planes != fd.num_entries) |
| 887 | return -EINVAL; |
| 888 | |
| 889 | for (i = 0; i < num_planes; i++) |
| 890 | plane_fmt[i].sizeimage = fd.entry[i].length; |
| 891 | |
| 892 | if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) { |
| 893 | v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n", |
| 894 | fd.entry[0].length); |
| 895 | |
| 896 | return -EINVAL; |
| 897 | } |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 902 | static int fimc_cap_g_fmt_mplane(struct file *file, void *fh, |
| 903 | struct v4l2_format *f) |
| 904 | { |
| 905 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 906 | |
Sylwester Nawrocki | fa8880b | 2013-01-11 06:36:19 -0300 | [diff] [blame] | 907 | __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f); |
| 908 | return 0; |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 909 | } |
| 910 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 911 | /* |
| 912 | * Try or set format on the fimc.X.capture video node and additionally |
| 913 | * on the whole pipeline if @try is false. |
| 914 | * Locking: the caller must _not_ hold the graph mutex. |
| 915 | */ |
| 916 | static int __video_try_or_set_format(struct fimc_dev *fimc, |
| 917 | struct v4l2_format *f, bool try, |
| 918 | struct fimc_fmt **inp_fmt, |
| 919 | struct fimc_fmt **out_fmt) |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 920 | { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 921 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 922 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
| 923 | struct exynos_video_entity *ve = &vc->ve; |
| 924 | struct fimc_ctx *ctx = vc->ctx; |
| 925 | unsigned int width = 0, height = 0; |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 926 | int ret = 0; |
| 927 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 928 | /* Pre-configure format at the camera input interface, for JPEG only */ |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 929 | if (fimc_jpeg_fourcc(pix->pixelformat)) { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 930 | fimc_capture_try_format(ctx, &pix->width, &pix->height, |
| 931 | NULL, &pix->pixelformat, |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 932 | FIMC_SD_PAD_SINK_CAM); |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 933 | if (try) { |
| 934 | width = pix->width; |
| 935 | height = pix->height; |
| 936 | } else { |
| 937 | ctx->s_frame.f_width = pix->width; |
| 938 | ctx->s_frame.f_height = pix->height; |
| 939 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 940 | } |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 941 | |
| 942 | /* Try the format at the scaler and the DMA output */ |
| 943 | *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height, |
| 944 | NULL, &pix->pixelformat, |
| 945 | FIMC_SD_PAD_SOURCE); |
| 946 | if (*out_fmt == NULL) |
Sylwester Nawrocki | 42625fd | 2013-05-31 11:37:20 -0300 | [diff] [blame] | 947 | return -EINVAL; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 948 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 949 | /* Restore image width/height for JPEG (no resizing supported). */ |
| 950 | if (try && fimc_jpeg_fourcc(pix->pixelformat)) { |
| 951 | pix->width = width; |
| 952 | pix->height = height; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 953 | } |
| 954 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 955 | /* Try to match format at the host and the sensor */ |
| 956 | if (!vc->user_subdev_api) { |
| 957 | struct v4l2_mbus_framefmt mbus_fmt; |
| 958 | struct v4l2_mbus_framefmt *mf; |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 959 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 960 | mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt; |
| 961 | |
| 962 | mf->code = (*out_fmt)->mbus_code; |
| 963 | mf->width = pix->width; |
| 964 | mf->height = pix->height; |
| 965 | |
| 966 | fimc_md_graph_lock(ve); |
| 967 | ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try); |
| 968 | fimc_md_graph_unlock(ve); |
| 969 | |
| 970 | if (ret < 0) |
| 971 | return ret; |
| 972 | |
| 973 | pix->width = mf->width; |
| 974 | pix->height = mf->height; |
| 975 | } |
| 976 | |
| 977 | fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix); |
| 978 | |
| 979 | if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) { |
| 980 | struct v4l2_subdev *sensor; |
| 981 | |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 982 | fimc_md_graph_lock(ve); |
| 983 | |
| 984 | sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR); |
| 985 | if (sensor) |
| 986 | fimc_get_sensor_frame_desc(sensor, pix->plane_fmt, |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 987 | (*out_fmt)->memplanes, try); |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 988 | else |
| 989 | ret = -EPIPE; |
| 990 | |
| 991 | fimc_md_graph_unlock(ve); |
| 992 | } |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 993 | |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 994 | return ret; |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 995 | } |
| 996 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 997 | static int fimc_cap_try_fmt_mplane(struct file *file, void *fh, |
| 998 | struct v4l2_format *f) |
| 999 | { |
| 1000 | struct fimc_dev *fimc = video_drvdata(file); |
| 1001 | struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL; |
| 1002 | |
| 1003 | return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt); |
| 1004 | } |
| 1005 | |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1006 | static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, |
| 1007 | enum fimc_color_fmt color) |
Sylwester Nawrocki | ee7160e5 | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 1008 | { |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1009 | bool jpeg = fimc_fmt_is_user_defined(color); |
| 1010 | |
Sylwester Nawrocki | ee7160e5 | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 1011 | ctx->scaler.enabled = !jpeg; |
| 1012 | fimc_ctrls_activate(ctx, !jpeg); |
| 1013 | |
| 1014 | if (jpeg) |
| 1015 | set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state); |
| 1016 | else |
| 1017 | clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state); |
| 1018 | } |
| 1019 | |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1020 | static int __fimc_capture_set_format(struct fimc_dev *fimc, |
| 1021 | struct v4l2_format *f) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1022 | { |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 1023 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
| 1024 | struct fimc_ctx *ctx = vc->ctx; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1025 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1026 | struct fimc_frame *ff = &ctx->d_frame; |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 1027 | struct fimc_fmt *inp_fmt = NULL; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1028 | int ret, i; |
| 1029 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1030 | if (vb2_is_busy(&fimc->vid_cap.vbq)) |
| 1031 | return -EBUSY; |
| 1032 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 1033 | ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt); |
| 1034 | if (ret < 0) |
| 1035 | return ret; |
Sylwester Nawrocki | dafb9c7 | 2011-12-01 14:02:24 -0300 | [diff] [blame] | 1036 | |
| 1037 | /* Update RGB Alpha control state and value range */ |
| 1038 | fimc_alpha_ctrl_update(ctx); |
| 1039 | |
Sylwester Nawrocki | fa8880b | 2013-01-11 06:36:19 -0300 | [diff] [blame] | 1040 | for (i = 0; i < ff->fmt->memplanes; i++) { |
| 1041 | ff->bytesperline[i] = pix->plane_fmt[i].bytesperline; |
Sylwester Nawrocki | d547ab6 | 2012-05-16 15:00:26 -0300 | [diff] [blame] | 1042 | ff->payload[i] = pix->plane_fmt[i].sizeimage; |
Sylwester Nawrocki | fa8880b | 2013-01-11 06:36:19 -0300 | [diff] [blame] | 1043 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1044 | |
| 1045 | set_frame_bounds(ff, pix->width, pix->height); |
| 1046 | /* Reset the composition rectangle if not yet configured */ |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1047 | if (!(ctx->state & FIMC_COMPOSE)) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1048 | set_frame_crop(ff, 0, 0, pix->width, pix->height); |
| 1049 | |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1050 | fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color); |
Sylwester Nawrocki | ee7160e5 | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 1051 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1052 | /* Reset cropping and set format at the camera interface input */ |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 1053 | if (!vc->user_subdev_api) { |
| 1054 | ctx->s_frame.fmt = inp_fmt; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1055 | set_frame_bounds(&ctx->s_frame, pix->width, pix->height); |
| 1056 | set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height); |
| 1057 | } |
| 1058 | |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1062 | static int fimc_cap_s_fmt_mplane(struct file *file, void *priv, |
| 1063 | struct v4l2_format *f) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1064 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1065 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1066 | |
Sylwester Nawrocki | 7536b42 | 2013-05-31 13:47:02 -0300 | [diff] [blame] | 1067 | return __fimc_capture_set_format(fimc, f); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static int fimc_cap_enum_input(struct file *file, void *priv, |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 1071 | struct v4l2_input *i) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1072 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1073 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1074 | struct exynos_video_entity *ve = &fimc->vid_cap.ve; |
| 1075 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1076 | |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 1077 | if (i->index != 0) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1078 | return -EINVAL; |
| 1079 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1080 | i->type = V4L2_INPUT_TYPE_CAMERA; |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1081 | fimc_md_graph_lock(ve); |
| 1082 | sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR); |
| 1083 | fimc_md_graph_unlock(ve); |
| 1084 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1085 | if (sd) |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 1086 | strscpy(i->name, sd->name, sizeof(i->name)); |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1087 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 1091 | static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1092 | { |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 1093 | return i == 0 ? i : -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1094 | } |
| 1095 | |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 1096 | static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1097 | { |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 1098 | *i = 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1099 | return 0; |
| 1100 | } |
| 1101 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1102 | /** |
| 1103 | * fimc_pipeline_validate - check for formats inconsistencies |
| 1104 | * between source and sink pad of each link |
Mauro Carvalho Chehab | 3b060ba0 | 2017-11-29 04:32:51 -0500 | [diff] [blame] | 1105 | * @fimc: the FIMC device this context applies to |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1106 | * |
| 1107 | * Return 0 if all formats match or -EPIPE otherwise. |
| 1108 | */ |
| 1109 | static int fimc_pipeline_validate(struct fimc_dev *fimc) |
| 1110 | { |
| 1111 | struct v4l2_subdev_format sink_fmt, src_fmt; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1112 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
| 1113 | struct v4l2_subdev *sd = &vc->subdev; |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1114 | struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1115 | struct media_pad *sink_pad, *src_pad; |
| 1116 | int i, ret; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1117 | |
| 1118 | while (1) { |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1119 | /* |
| 1120 | * Find current entity sink pad and any remote sink pad linked |
| 1121 | * to it. We stop if there is no sink pad in current entity or |
| 1122 | * it is not linked to any other remote entity. |
| 1123 | */ |
| 1124 | src_pad = NULL; |
| 1125 | |
| 1126 | for (i = 0; i < sd->entity.num_pads; i++) { |
| 1127 | struct media_pad *p = &sd->entity.pads[i]; |
| 1128 | |
| 1129 | if (p->flags & MEDIA_PAD_FL_SINK) { |
| 1130 | sink_pad = p; |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 1131 | src_pad = media_entity_remote_pad(sink_pad); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1132 | if (src_pad) |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | |
Mauro Carvalho Chehab | 3efdf62 | 2015-05-07 22:12:32 -0300 | [diff] [blame] | 1137 | if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity)) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1138 | break; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1139 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1140 | /* Don't call FIMC subdev operation to avoid nested locking */ |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1141 | if (sd == &vc->subdev) { |
| 1142 | struct fimc_frame *ff = &vc->ctx->s_frame; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1143 | sink_fmt.format.width = ff->f_width; |
| 1144 | sink_fmt.format.height = ff->f_height; |
| 1145 | sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0; |
| 1146 | } else { |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1147 | sink_fmt.pad = sink_pad->index; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1148 | sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 1149 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt); |
| 1150 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 1151 | return -EPIPE; |
| 1152 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1153 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1154 | /* Retrieve format at the source pad */ |
| 1155 | sd = media_entity_to_v4l2_subdev(src_pad->entity); |
| 1156 | src_fmt.pad = src_pad->index; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1157 | src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 1158 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt); |
| 1159 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 1160 | return -EPIPE; |
| 1161 | |
| 1162 | if (src_fmt.format.width != sink_fmt.format.width || |
| 1163 | src_fmt.format.height != sink_fmt.format.height || |
| 1164 | src_fmt.format.code != sink_fmt.format.code) |
| 1165 | return -EPIPE; |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1166 | |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1167 | if (sd == p->subdevs[IDX_SENSOR] && |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1168 | fimc_user_defined_mbus_fmt(src_fmt.format.code)) { |
| 1169 | struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES]; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1170 | struct fimc_frame *frame = &vc->ctx->d_frame; |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1171 | unsigned int i; |
| 1172 | |
| 1173 | ret = fimc_get_sensor_frame_desc(sd, plane_fmt, |
| 1174 | frame->fmt->memplanes, |
| 1175 | false); |
| 1176 | if (ret < 0) |
| 1177 | return -EPIPE; |
| 1178 | |
| 1179 | for (i = 0; i < frame->fmt->memplanes; i++) |
| 1180 | if (frame->payload[i] < plane_fmt[i].sizeimage) |
| 1181 | return -EPIPE; |
| 1182 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1183 | } |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1187 | static int fimc_cap_streamon(struct file *file, void *priv, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1188 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1189 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1190 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1191 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1192 | struct media_entity *entity = &vc->ve.vdev.entity; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1193 | struct fimc_source_info *si = NULL; |
| 1194 | struct v4l2_subdev *sd; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1195 | int ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1196 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1197 | if (fimc_capture_active(fimc)) |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1198 | return -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1199 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 1200 | ret = media_pipeline_start(entity, &vc->ve.pipe->mp); |
Sakari Ailus | a60a295 | 2012-05-25 07:08:05 -0300 | [diff] [blame] | 1201 | if (ret < 0) |
| 1202 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1203 | |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1204 | sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1205 | if (sd) |
| 1206 | si = v4l2_get_subdev_hostdata(sd); |
| 1207 | |
| 1208 | if (si == NULL) { |
| 1209 | ret = -EPIPE; |
| 1210 | goto err_p_stop; |
| 1211 | } |
| 1212 | /* |
| 1213 | * Save configuration data related to currently attached image |
| 1214 | * sensor or other data source, e.g. FIMC-IS. |
| 1215 | */ |
| 1216 | vc->source_config = *si; |
| 1217 | |
| 1218 | if (vc->input == GRP_ID_FIMC_IS) |
| 1219 | vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK; |
| 1220 | |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1221 | if (vc->user_subdev_api) { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1222 | ret = fimc_pipeline_validate(fimc); |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1223 | if (ret < 0) |
| 1224 | goto err_p_stop; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1225 | } |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1226 | |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1227 | ret = vb2_ioctl_streamon(file, priv, type); |
Sylwester Nawrocki | 9ea89e2 | 2013-03-25 16:50:50 -0300 | [diff] [blame] | 1228 | if (!ret) { |
| 1229 | vc->streaming = true; |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1230 | return ret; |
Sylwester Nawrocki | 9ea89e2 | 2013-03-25 16:50:50 -0300 | [diff] [blame] | 1231 | } |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1232 | |
| 1233 | err_p_stop: |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 1234 | media_pipeline_stop(entity); |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1235 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | static int fimc_cap_streamoff(struct file *file, void *priv, |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1239 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1240 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1241 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1242 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1243 | int ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1244 | |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1245 | ret = vb2_ioctl_streamoff(file, priv, type); |
Sylwester Nawrocki | 9ea89e2 | 2013-03-25 16:50:50 -0300 | [diff] [blame] | 1246 | if (ret < 0) |
| 1247 | return ret; |
Sylwester Nawrocki | 95c4a17 | 2013-03-20 10:35:43 -0300 | [diff] [blame] | 1248 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 1249 | media_pipeline_stop(&vc->ve.vdev.entity); |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1250 | vc->streaming = false; |
Sylwester Nawrocki | 9ea89e2 | 2013-03-25 16:50:50 -0300 | [diff] [blame] | 1251 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | static int fimc_cap_reqbufs(struct file *file, void *priv, |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1255 | struct v4l2_requestbuffers *reqbufs) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1256 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1257 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1258 | int ret; |
| 1259 | |
| 1260 | ret = vb2_ioctl_reqbufs(file, priv, reqbufs); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1261 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1262 | if (!ret) |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1263 | fimc->vid_cap.reqbufs_count = reqbufs->count; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1264 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1265 | return ret; |
| 1266 | } |
| 1267 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1268 | static int fimc_cap_g_selection(struct file *file, void *fh, |
| 1269 | struct v4l2_selection *s) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1270 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1271 | struct fimc_dev *fimc = video_drvdata(file); |
| 1272 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1273 | struct fimc_frame *f = &ctx->s_frame; |
| 1274 | |
Hans Verkuil | eaec420 | 2017-05-08 11:35:05 -0300 | [diff] [blame] | 1275 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1276 | return -EINVAL; |
| 1277 | |
| 1278 | switch (s->target) { |
| 1279 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 1280 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1281 | f = &ctx->d_frame; |
Gustavo A. R. Silva | 1771e9f | 2020-07-25 00:10:14 +0200 | [diff] [blame^] | 1282 | fallthrough; |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1283 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 1284 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 1285 | s->r.left = 0; |
| 1286 | s->r.top = 0; |
| 1287 | s->r.width = f->o_width; |
| 1288 | s->r.height = f->o_height; |
| 1289 | return 0; |
| 1290 | |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 1291 | case V4L2_SEL_TGT_COMPOSE: |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1292 | f = &ctx->d_frame; |
Gustavo A. R. Silva | 1771e9f | 2020-07-25 00:10:14 +0200 | [diff] [blame^] | 1293 | fallthrough; |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 1294 | case V4L2_SEL_TGT_CROP: |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1295 | s->r.left = f->offs_h; |
| 1296 | s->r.top = f->offs_v; |
| 1297 | s->r.width = f->width; |
| 1298 | s->r.height = f->height; |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1305 | static int fimc_cap_s_selection(struct file *file, void *fh, |
| 1306 | struct v4l2_selection *s) |
| 1307 | { |
| 1308 | struct fimc_dev *fimc = video_drvdata(file); |
| 1309 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 1310 | struct v4l2_rect rect = s->r; |
| 1311 | struct fimc_frame *f; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1312 | unsigned long flags; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1313 | |
Hans Verkuil | eaec420 | 2017-05-08 11:35:05 -0300 | [diff] [blame] | 1314 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1315 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1316 | |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 1317 | if (s->target == V4L2_SEL_TGT_COMPOSE) |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1318 | f = &ctx->d_frame; |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 1319 | else if (s->target == V4L2_SEL_TGT_CROP) |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1320 | f = &ctx->s_frame; |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1321 | else |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1322 | return -EINVAL; |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1323 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1324 | fimc_capture_try_selection(ctx, &rect, s->target); |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1325 | |
| 1326 | if (s->flags & V4L2_SEL_FLAG_LE && |
Benoit Parrot | 030b161 | 2020-05-28 15:26:05 +0200 | [diff] [blame] | 1327 | !v4l2_rect_enclosed(&rect, &s->r)) |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1328 | return -ERANGE; |
| 1329 | |
| 1330 | if (s->flags & V4L2_SEL_FLAG_GE && |
Benoit Parrot | 030b161 | 2020-05-28 15:26:05 +0200 | [diff] [blame] | 1331 | !v4l2_rect_enclosed(&s->r, &rect)) |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1332 | return -ERANGE; |
| 1333 | |
| 1334 | s->r = rect; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1335 | spin_lock_irqsave(&fimc->slock, flags); |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1336 | set_frame_crop(f, s->r.left, s->r.top, s->r.width, |
| 1337 | s->r.height); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1338 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1339 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1340 | set_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1341 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1342 | } |
| 1343 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1344 | static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = { |
Sylwester Nawrocki | aceb59e | 2013-04-23 11:36:04 -0300 | [diff] [blame] | 1345 | .vidioc_querycap = fimc_cap_querycap, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1346 | |
Boris Brezillon | 7e98b7b | 2019-06-04 03:06:25 -0400 | [diff] [blame] | 1347 | .vidioc_enum_fmt_vid_cap = fimc_cap_enum_fmt, |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1348 | .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane, |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1349 | .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane, |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1350 | .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1351 | |
| 1352 | .vidioc_reqbufs = fimc_cap_reqbufs, |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1353 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 1354 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 1355 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
| 1356 | .vidioc_expbuf = vb2_ioctl_expbuf, |
| 1357 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, |
| 1358 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
Sylwester Nawrocki | 3b4c34a | 2012-01-30 14:56:59 -0300 | [diff] [blame] | 1359 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1360 | .vidioc_streamon = fimc_cap_streamon, |
| 1361 | .vidioc_streamoff = fimc_cap_streamoff, |
| 1362 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1363 | .vidioc_g_selection = fimc_cap_g_selection, |
| 1364 | .vidioc_s_selection = fimc_cap_s_selection, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1365 | |
| 1366 | .vidioc_enum_input = fimc_cap_enum_input, |
| 1367 | .vidioc_s_input = fimc_cap_s_input, |
| 1368 | .vidioc_g_input = fimc_cap_g_input, |
| 1369 | }; |
| 1370 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1371 | /* Capture subdev media entity operations */ |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1372 | static int fimc_link_setup(struct media_entity *entity, |
| 1373 | const struct media_pad *local, |
| 1374 | const struct media_pad *remote, u32 flags) |
| 1375 | { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1376 | struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); |
| 1377 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1378 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
| 1379 | struct v4l2_subdev *sensor; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1380 | |
Mauro Carvalho Chehab | 3efdf62 | 2015-05-07 22:12:32 -0300 | [diff] [blame] | 1381 | if (!is_media_entity_v4l2_subdev(remote->entity)) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1382 | return -EINVAL; |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1383 | |
| 1384 | if (WARN_ON(fimc == NULL)) |
| 1385 | return 0; |
| 1386 | |
| 1387 | dbg("%s --> %s, flags: 0x%x. input: 0x%x", |
| 1388 | local->entity->name, remote->entity->name, flags, |
| 1389 | fimc->vid_cap.input); |
| 1390 | |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1391 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
| 1392 | fimc->vid_cap.input = 0; |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1396 | if (vc->input != 0) |
| 1397 | return -EBUSY; |
| 1398 | |
| 1399 | vc->input = sd->grp_id; |
| 1400 | |
| 1401 | if (vc->user_subdev_api || vc->inh_sensor_ctrls) |
| 1402 | return 0; |
| 1403 | |
| 1404 | /* Inherit V4L2 controls from the image sensor subdev. */ |
| 1405 | sensor = fimc_find_remote_sensor(&vc->subdev.entity); |
| 1406 | if (sensor == NULL) |
| 1407 | return 0; |
| 1408 | |
| 1409 | return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler, |
Hans Verkuil | da1b1ae | 2018-05-21 04:54:36 -0400 | [diff] [blame] | 1410 | sensor->ctrl_handler, NULL, true); |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1411 | } |
| 1412 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1413 | static const struct media_entity_operations fimc_sd_media_ops = { |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1414 | .link_setup = fimc_link_setup, |
| 1415 | }; |
| 1416 | |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1417 | /** |
| 1418 | * fimc_sensor_notify - v4l2_device notification from a sensor subdev |
| 1419 | * @sd: pointer to a subdev generating the notification |
| 1420 | * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY |
| 1421 | * @arg: pointer to an u32 type integer that stores the frame payload value |
| 1422 | * |
| 1423 | * The End Of Frame notification sent by sensor subdev in its still capture |
| 1424 | * mode. If there is only a single VSYNC generated by the sensor at the |
| 1425 | * beginning of a frame transmission, FIMC does not issue the LastIrq |
| 1426 | * (end of frame) interrupt. And this notification is used to complete the |
| 1427 | * frame capture and returning a buffer to user-space. Subdev drivers should |
| 1428 | * call this notification from their last 'End of frame capture' interrupt. |
| 1429 | */ |
| 1430 | void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification, |
| 1431 | void *arg) |
| 1432 | { |
Sylwester Nawrocki | 4c8f062 | 2013-04-09 11:11:58 -0300 | [diff] [blame] | 1433 | struct fimc_source_info *si; |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1434 | struct fimc_vid_buffer *buf; |
| 1435 | struct fimc_md *fmd; |
| 1436 | struct fimc_dev *fimc; |
| 1437 | unsigned long flags; |
| 1438 | |
| 1439 | if (sd == NULL) |
| 1440 | return; |
| 1441 | |
Sylwester Nawrocki | 4c8f062 | 2013-04-09 11:11:58 -0300 | [diff] [blame] | 1442 | si = v4l2_get_subdev_hostdata(sd); |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1443 | fmd = entity_to_fimc_mdev(&sd->entity); |
| 1444 | |
| 1445 | spin_lock_irqsave(&fmd->slock, flags); |
Sylwester Nawrocki | 4c8f062 | 2013-04-09 11:11:58 -0300 | [diff] [blame] | 1446 | |
| 1447 | fimc = si ? source_to_sensor_info(si)->host : NULL; |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1448 | |
| 1449 | if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY && |
| 1450 | test_bit(ST_CAPT_PEND, &fimc->state)) { |
| 1451 | unsigned long irq_flags; |
| 1452 | spin_lock_irqsave(&fimc->slock, irq_flags); |
| 1453 | if (!list_empty(&fimc->vid_cap.active_buf_q)) { |
| 1454 | buf = list_entry(fimc->vid_cap.active_buf_q.next, |
| 1455 | struct fimc_vid_buffer, list); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1456 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, |
| 1457 | *((u32 *)arg)); |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1458 | } |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame] | 1459 | fimc_capture_irq_handler(fimc, 1); |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1460 | fimc_deactivate_capture(fimc); |
| 1461 | spin_unlock_irqrestore(&fimc->slock, irq_flags); |
| 1462 | } |
| 1463 | spin_unlock_irqrestore(&fmd->slock, flags); |
| 1464 | } |
| 1465 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1466 | static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1467 | struct v4l2_subdev_pad_config *cfg, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1468 | struct v4l2_subdev_mbus_code_enum *code) |
| 1469 | { |
| 1470 | struct fimc_fmt *fmt; |
| 1471 | |
| 1472 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index); |
| 1473 | if (!fmt) |
| 1474 | return -EINVAL; |
| 1475 | code->code = fmt->mbus_code; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | static int fimc_subdev_get_fmt(struct v4l2_subdev *sd, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1480 | struct v4l2_subdev_pad_config *cfg, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1481 | struct v4l2_subdev_format *fmt) |
| 1482 | { |
| 1483 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1484 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1485 | struct fimc_frame *ff = &ctx->s_frame; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1486 | struct v4l2_mbus_framefmt *mf; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1487 | |
| 1488 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1489 | mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1490 | fmt->format = *mf; |
| 1491 | return 0; |
| 1492 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1493 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1494 | mf = &fmt->format; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1495 | mutex_lock(&fimc->lock); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1496 | |
| 1497 | switch (fmt->pad) { |
| 1498 | case FIMC_SD_PAD_SOURCE: |
| 1499 | if (!WARN_ON(ff->fmt == NULL)) |
| 1500 | mf->code = ff->fmt->mbus_code; |
| 1501 | /* Sink pads crop rectangle size */ |
| 1502 | mf->width = ff->width; |
| 1503 | mf->height = ff->height; |
| 1504 | break; |
| 1505 | case FIMC_SD_PAD_SINK_FIFO: |
| 1506 | *mf = fimc->vid_cap.wb_fmt; |
| 1507 | break; |
| 1508 | case FIMC_SD_PAD_SINK_CAM: |
| 1509 | default: |
| 1510 | *mf = fimc->vid_cap.ci_fmt; |
| 1511 | break; |
| 1512 | } |
| 1513 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1514 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1515 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static int fimc_subdev_set_fmt(struct v4l2_subdev *sd, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1521 | struct v4l2_subdev_pad_config *cfg, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1522 | struct v4l2_subdev_format *fmt) |
| 1523 | { |
| 1524 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1525 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1526 | struct fimc_vid_cap *vc = &fimc->vid_cap; |
| 1527 | struct fimc_ctx *ctx = vc->ctx; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1528 | struct fimc_frame *ff; |
| 1529 | struct fimc_fmt *ffmt; |
| 1530 | |
| 1531 | dbg("pad%d: code: 0x%x, %dx%d", |
| 1532 | fmt->pad, mf->code, mf->width, mf->height); |
| 1533 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1534 | if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq)) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1535 | return -EBUSY; |
| 1536 | |
| 1537 | mutex_lock(&fimc->lock); |
| 1538 | ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height, |
| 1539 | &mf->code, NULL, fmt->pad); |
| 1540 | mutex_unlock(&fimc->lock); |
| 1541 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1542 | |
| 1543 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1544 | mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1545 | *mf = fmt->format; |
| 1546 | return 0; |
| 1547 | } |
Sylwester Nawrocki | 6612545 | 2012-12-05 13:52:00 -0300 | [diff] [blame] | 1548 | /* There must be a bug in the driver if this happens */ |
| 1549 | if (WARN_ON(ffmt == NULL)) |
| 1550 | return -EINVAL; |
| 1551 | |
Sylwester Nawrocki | dafb9c7 | 2011-12-01 14:02:24 -0300 | [diff] [blame] | 1552 | /* Update RGB Alpha control state and value range */ |
| 1553 | fimc_alpha_ctrl_update(ctx); |
| 1554 | |
Sylwester Nawrocki | 14783d2 | 2012-09-24 11:08:45 -0300 | [diff] [blame] | 1555 | fimc_capture_mark_jpeg_xfer(ctx, ffmt->color); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1556 | if (fmt->pad == FIMC_SD_PAD_SOURCE) { |
| 1557 | ff = &ctx->d_frame; |
| 1558 | /* Sink pads crop rectangle size */ |
| 1559 | mf->width = ctx->s_frame.width; |
| 1560 | mf->height = ctx->s_frame.height; |
| 1561 | } else { |
| 1562 | ff = &ctx->s_frame; |
| 1563 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1564 | |
| 1565 | mutex_lock(&fimc->lock); |
| 1566 | set_frame_bounds(ff, mf->width, mf->height); |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1567 | |
| 1568 | if (fmt->pad == FIMC_SD_PAD_SINK_FIFO) |
| 1569 | vc->wb_fmt = *mf; |
| 1570 | else if (fmt->pad == FIMC_SD_PAD_SINK_CAM) |
| 1571 | vc->ci_fmt = *mf; |
| 1572 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1573 | ff->fmt = ffmt; |
| 1574 | |
| 1575 | /* Reset the crop rectangle if required. */ |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1576 | if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE))) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1577 | set_frame_crop(ff, 0, 0, mf->width, mf->height); |
| 1578 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1579 | if (fmt->pad != FIMC_SD_PAD_SOURCE) |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1580 | ctx->state &= ~FIMC_COMPOSE; |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1581 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1582 | mutex_unlock(&fimc->lock); |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1586 | static int fimc_subdev_get_selection(struct v4l2_subdev *sd, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1587 | struct v4l2_subdev_pad_config *cfg, |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1588 | struct v4l2_subdev_selection *sel) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1589 | { |
| 1590 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1591 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1592 | struct fimc_frame *f = &ctx->s_frame; |
| 1593 | struct v4l2_rect *r = &sel->r; |
| 1594 | struct v4l2_rect *try_sel; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1595 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1596 | if (sel->pad == FIMC_SD_PAD_SOURCE) |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1597 | return -EINVAL; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1598 | |
| 1599 | mutex_lock(&fimc->lock); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1600 | |
| 1601 | switch (sel->target) { |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1602 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1603 | f = &ctx->d_frame; |
Gustavo A. R. Silva | 1771e9f | 2020-07-25 00:10:14 +0200 | [diff] [blame^] | 1604 | fallthrough; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1605 | case V4L2_SEL_TGT_CROP_BOUNDS: |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1606 | r->width = f->o_width; |
| 1607 | r->height = f->o_height; |
| 1608 | r->left = 0; |
| 1609 | r->top = 0; |
| 1610 | mutex_unlock(&fimc->lock); |
| 1611 | return 0; |
| 1612 | |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1613 | case V4L2_SEL_TGT_CROP: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1614 | try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1615 | break; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1616 | case V4L2_SEL_TGT_COMPOSE: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1617 | try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1618 | f = &ctx->d_frame; |
| 1619 | break; |
| 1620 | default: |
| 1621 | mutex_unlock(&fimc->lock); |
| 1622 | return -EINVAL; |
| 1623 | } |
| 1624 | |
| 1625 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1626 | sel->r = *try_sel; |
| 1627 | } else { |
| 1628 | r->left = f->offs_h; |
| 1629 | r->top = f->offs_v; |
| 1630 | r->width = f->width; |
| 1631 | r->height = f->height; |
| 1632 | } |
| 1633 | |
| 1634 | dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d", |
| 1635 | sel->pad, r->left, r->top, r->width, r->height, |
| 1636 | f->f_width, f->f_height); |
| 1637 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1638 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1639 | return 0; |
| 1640 | } |
| 1641 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1642 | static int fimc_subdev_set_selection(struct v4l2_subdev *sd, |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1643 | struct v4l2_subdev_pad_config *cfg, |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1644 | struct v4l2_subdev_selection *sel) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1645 | { |
| 1646 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1647 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1648 | struct fimc_frame *f = &ctx->s_frame; |
| 1649 | struct v4l2_rect *r = &sel->r; |
| 1650 | struct v4l2_rect *try_sel; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1651 | unsigned long flags; |
| 1652 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1653 | if (sel->pad == FIMC_SD_PAD_SOURCE) |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1654 | return -EINVAL; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1655 | |
| 1656 | mutex_lock(&fimc->lock); |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 1657 | fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1658 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1659 | switch (sel->target) { |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1660 | case V4L2_SEL_TGT_CROP: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1661 | try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1662 | break; |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1663 | case V4L2_SEL_TGT_COMPOSE: |
Hans Verkuil | f723413 | 2015-03-04 01:47:54 -0800 | [diff] [blame] | 1664 | try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1665 | f = &ctx->d_frame; |
| 1666 | break; |
| 1667 | default: |
| 1668 | mutex_unlock(&fimc->lock); |
| 1669 | return -EINVAL; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1670 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1671 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1672 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1673 | *try_sel = sel->r; |
| 1674 | } else { |
| 1675 | spin_lock_irqsave(&fimc->slock, flags); |
| 1676 | set_frame_crop(f, r->left, r->top, r->width, r->height); |
| 1677 | set_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1678 | if (sel->target == V4L2_SEL_TGT_COMPOSE) |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1679 | ctx->state |= FIMC_COMPOSE; |
Sylwester Nawrocki | 8b16410 | 2013-01-30 09:55:46 -0300 | [diff] [blame] | 1680 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1681 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1682 | |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1683 | dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1684 | r->width, r->height); |
| 1685 | |
| 1686 | mutex_unlock(&fimc->lock); |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
Bhumika Goyal | a2fafda | 2016-12-12 16:03:57 -0200 | [diff] [blame] | 1690 | static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1691 | .enum_mbus_code = fimc_subdev_enum_mbus_code, |
Sylwester Nawrocki | fed07f8 | 2012-04-29 17:46:03 -0300 | [diff] [blame] | 1692 | .get_selection = fimc_subdev_get_selection, |
| 1693 | .set_selection = fimc_subdev_set_selection, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1694 | .get_fmt = fimc_subdev_get_fmt, |
| 1695 | .set_fmt = fimc_subdev_set_fmt, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1696 | }; |
| 1697 | |
Bhumika Goyal | a2fafda | 2016-12-12 16:03:57 -0200 | [diff] [blame] | 1698 | static const struct v4l2_subdev_ops fimc_subdev_ops = { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1699 | .pad = &fimc_subdev_pad_ops, |
| 1700 | }; |
| 1701 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1702 | /* Set default format at the sensor and host interface */ |
| 1703 | static int fimc_capture_set_default_format(struct fimc_dev *fimc) |
| 1704 | { |
| 1705 | struct v4l2_format fmt = { |
| 1706 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, |
| 1707 | .fmt.pix_mp = { |
Sylwester Nawrocki | 3ad8624 | 2013-06-18 14:56:11 -0300 | [diff] [blame] | 1708 | .width = FIMC_DEFAULT_WIDTH, |
| 1709 | .height = FIMC_DEFAULT_HEIGHT, |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1710 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 1711 | .field = V4L2_FIELD_NONE, |
| 1712 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 1713 | }, |
| 1714 | }; |
| 1715 | |
Sylwester Nawrocki | 740ad92 | 2012-12-06 10:26:19 -0300 | [diff] [blame] | 1716 | return __fimc_capture_set_format(fimc, &fmt); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1717 | } |
| 1718 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1719 | /* fimc->lock must be already initialized */ |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1720 | static int fimc_register_capture_device(struct fimc_dev *fimc, |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1721 | struct v4l2_device *v4l2_dev) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1722 | { |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1723 | struct video_device *vfd = &fimc->vid_cap.ve.vdev; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1724 | struct vb2_queue *q = &fimc->vid_cap.vbq; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1725 | struct fimc_ctx *ctx; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1726 | struct fimc_vid_cap *vid_cap; |
Sylwester Nawrocki | 3ad8624 | 2013-06-18 14:56:11 -0300 | [diff] [blame] | 1727 | struct fimc_fmt *fmt; |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1728 | int ret = -ENOMEM; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1729 | |
Sachin Kamat | 26ee7f4 | 2012-08-17 03:28:27 -0300 | [diff] [blame] | 1730 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1731 | if (!ctx) |
| 1732 | return -ENOMEM; |
| 1733 | |
| 1734 | ctx->fimc_dev = fimc; |
Sylwester Nawrocki | 3d112d9 | 2012-04-26 06:26:29 -0300 | [diff] [blame] | 1735 | ctx->in_path = FIMC_IO_CAMERA; |
| 1736 | ctx->out_path = FIMC_IO_DMA; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1737 | ctx->state = FIMC_CTX_CAP; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1738 | ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1739 | ctx->d_frame.fmt = ctx->s_frame.fmt; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1740 | |
Sylwester Nawrocki | 31d34d9 | 2012-07-26 07:15:42 -0300 | [diff] [blame] | 1741 | memset(vfd, 0, sizeof(*vfd)); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1742 | snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1743 | |
| 1744 | vfd->fops = &fimc_capture_fops; |
| 1745 | vfd->ioctl_ops = &fimc_capture_ioctl_ops; |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1746 | vfd->v4l2_dev = v4l2_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1747 | vfd->minor = -1; |
Sylwester Nawrocki | 31d34d9 | 2012-07-26 07:15:42 -0300 | [diff] [blame] | 1748 | vfd->release = video_device_release_empty; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1749 | vfd->queue = q; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1750 | vfd->lock = &fimc->lock; |
Boris Brezillon | 1ddc8a9 | 2019-06-04 03:06:24 -0400 | [diff] [blame] | 1751 | vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | c2d430a | 2012-06-27 09:56:17 -0300 | [diff] [blame] | 1752 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1753 | video_set_drvdata(vfd, fimc); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1754 | vid_cap = &fimc->vid_cap; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1755 | vid_cap->active_buf_cnt = 0; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1756 | vid_cap->reqbufs_count = 0; |
| 1757 | vid_cap->ctx = ctx; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1758 | |
| 1759 | INIT_LIST_HEAD(&vid_cap->pending_buf_q); |
| 1760 | INIT_LIST_HEAD(&vid_cap->active_buf_q); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1761 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1762 | memset(q, 0, sizeof(*q)); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1763 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
Tomasz Stanislawski | 9bd09fd | 2012-06-14 10:37:49 -0300 | [diff] [blame] | 1764 | q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1765 | q->drv_priv = ctx; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1766 | q->ops = &fimc_capture_qops; |
| 1767 | q->mem_ops = &vb2_dma_contig_memops; |
| 1768 | q->buf_struct_size = sizeof(struct fimc_vid_buffer); |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 1769 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Sylwester Nawrocki | c444914 | 2013-03-25 16:36:35 -0300 | [diff] [blame] | 1770 | q->lock = &fimc->lock; |
Hans Verkuil | 2548fee | 2016-02-16 07:30:19 -0200 | [diff] [blame] | 1771 | q->dev = &fimc->pdev->dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1772 | |
Sylwester Nawrocki | 41fd087 | 2012-10-19 08:24:18 -0300 | [diff] [blame] | 1773 | ret = vb2_queue_init(q); |
| 1774 | if (ret) |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1775 | goto err_free_ctx; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1776 | |
Sylwester Nawrocki | 3ad8624 | 2013-06-18 14:56:11 -0300 | [diff] [blame] | 1777 | /* Default format configuration */ |
| 1778 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0); |
| 1779 | vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH; |
| 1780 | vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT; |
| 1781 | vid_cap->ci_fmt.code = fmt->mbus_code; |
| 1782 | |
| 1783 | ctx->s_frame.width = FIMC_DEFAULT_WIDTH; |
| 1784 | ctx->s_frame.height = FIMC_DEFAULT_HEIGHT; |
| 1785 | ctx->s_frame.fmt = fmt; |
| 1786 | |
| 1787 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0); |
| 1788 | vid_cap->wb_fmt = vid_cap->ci_fmt; |
| 1789 | vid_cap->wb_fmt.code = fmt->mbus_code; |
| 1790 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1791 | vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK; |
Sylwester Nawrocki | bae4500 | 2016-09-01 08:47:05 -0300 | [diff] [blame] | 1792 | vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER; |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame] | 1793 | ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad); |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1794 | if (ret) |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1795 | goto err_free_ctx; |
| 1796 | |
| 1797 | ret = fimc_ctrls_create(ctx); |
| 1798 | if (ret) |
| 1799 | goto err_me_cleanup; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1800 | |
Hans Verkuil | 70cad44 | 2020-02-03 12:41:18 +0100 | [diff] [blame] | 1801 | ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1802 | if (ret) |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1803 | goto err_ctrl_free; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1804 | |
| 1805 | v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n", |
| 1806 | vfd->name, video_device_node_name(vfd)); |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1807 | |
Sylwester Nawrocki | 9448ab7 | 2012-04-02 06:41:22 -0300 | [diff] [blame] | 1808 | vfd->ctrl_handler = &ctx->ctrls.handler; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1809 | return 0; |
| 1810 | |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1811 | err_ctrl_free: |
| 1812 | fimc_ctrls_delete(ctx); |
| 1813 | err_me_cleanup: |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1814 | media_entity_cleanup(&vfd->entity); |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1815 | err_free_ctx: |
Sylwester Nawrocki | cfd7731 | 2011-06-02 06:18:34 -0300 | [diff] [blame] | 1816 | kfree(ctx); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1817 | return ret; |
| 1818 | } |
| 1819 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1820 | static int fimc_capture_subdev_registered(struct v4l2_subdev *sd) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1821 | { |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1822 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1823 | int ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1824 | |
Sylwester Nawrocki | bbc5296 | 2012-07-26 07:59:11 -0300 | [diff] [blame] | 1825 | if (fimc == NULL) |
| 1826 | return -ENXIO; |
| 1827 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1828 | ret = fimc_register_m2m_device(fimc, sd->v4l2_dev); |
| 1829 | if (ret) |
| 1830 | return ret; |
| 1831 | |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1832 | fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd); |
Sylwester Nawrocki | 97d66c4 | 2012-11-22 06:17:19 -0300 | [diff] [blame] | 1833 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1834 | ret = fimc_register_capture_device(fimc, sd->v4l2_dev); |
Sylwester Nawrocki | 97d66c4 | 2012-11-22 06:17:19 -0300 | [diff] [blame] | 1835 | if (ret) { |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1836 | fimc_unregister_m2m_device(fimc); |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1837 | fimc->vid_cap.ve.pipe = NULL; |
Sylwester Nawrocki | 97d66c4 | 2012-11-22 06:17:19 -0300 | [diff] [blame] | 1838 | } |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1839 | |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd) |
| 1844 | { |
| 1845 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1846 | struct video_device *vdev; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1847 | |
| 1848 | if (fimc == NULL) |
| 1849 | return; |
| 1850 | |
Sylwester Nawrocki | 26d63d1 | 2013-05-31 11:37:25 -0300 | [diff] [blame] | 1851 | mutex_lock(&fimc->lock); |
| 1852 | |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1853 | fimc_unregister_m2m_device(fimc); |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1854 | vdev = &fimc->vid_cap.ve.vdev; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1855 | |
Sylwester Nawrocki | bc7584b | 2013-05-31 11:37:18 -0300 | [diff] [blame] | 1856 | if (video_is_registered(vdev)) { |
| 1857 | video_unregister_device(vdev); |
| 1858 | media_entity_cleanup(&vdev->entity); |
Sylwester Nawrocki | 4403106 | 2013-05-31 11:37:19 -0300 | [diff] [blame] | 1859 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
Sylwester Nawrocki | 403dfbe | 2013-05-31 11:37:22 -0300 | [diff] [blame] | 1860 | fimc->vid_cap.ve.pipe = NULL; |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1861 | } |
| 1862 | kfree(fimc->vid_cap.ctx); |
Sylwester Nawrocki | 96a8574 | 2011-08-26 15:40:36 -0300 | [diff] [blame] | 1863 | fimc->vid_cap.ctx = NULL; |
Sylwester Nawrocki | 26d63d1 | 2013-05-31 11:37:25 -0300 | [diff] [blame] | 1864 | |
| 1865 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1866 | } |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1867 | |
| 1868 | static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = { |
| 1869 | .registered = fimc_capture_subdev_registered, |
| 1870 | .unregistered = fimc_capture_subdev_unregistered, |
| 1871 | }; |
| 1872 | |
| 1873 | int fimc_initialize_capture_subdev(struct fimc_dev *fimc) |
| 1874 | { |
| 1875 | struct v4l2_subdev *sd = &fimc->vid_cap.subdev; |
| 1876 | int ret; |
| 1877 | |
| 1878 | v4l2_subdev_init(sd, &fimc_subdev_ops); |
Sylwester Nawrocki | 5a66561 | 2013-04-18 08:49:54 -0300 | [diff] [blame] | 1879 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; |
Sylwester Nawrocki | e80cb1f | 2013-03-26 08:22:21 -0300 | [diff] [blame] | 1880 | snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1881 | |
Sylwester Nawrocki | 88fa831 | 2013-03-20 10:44:39 -0300 | [diff] [blame] | 1882 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK; |
| 1883 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK; |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1884 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE; |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame] | 1885 | ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM, |
Mauro Carvalho Chehab | 1809510 | 2015-08-06 09:25:57 -0300 | [diff] [blame] | 1886 | fimc->vid_cap.sd_pads); |
Sylwester Nawrocki | 693f5c4 | 2012-04-20 18:57:25 -0300 | [diff] [blame] | 1887 | if (ret) |
| 1888 | return ret; |
| 1889 | |
| 1890 | sd->entity.ops = &fimc_sd_media_ops; |
| 1891 | sd->internal_ops = &fimc_capture_sd_internal_ops; |
| 1892 | v4l2_set_subdevdata(sd, fimc); |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | void fimc_unregister_capture_subdev(struct fimc_dev *fimc) |
| 1897 | { |
| 1898 | struct v4l2_subdev *sd = &fimc->vid_cap.subdev; |
| 1899 | |
| 1900 | v4l2_device_unregister_subdev(sd); |
| 1901 | media_entity_cleanup(&sd->entity); |
| 1902 | v4l2_set_subdevdata(sd, NULL); |
| 1903 | } |