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