Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1 | /* |
Sylwester Nawrocki | 3a3f944 | 2011-04-28 09:06:19 -0300 | [diff] [blame] | 2 | * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 3 | * |
Sylwester Nawrocki | 3a3f944 | 2011-04-28 09:06:19 -0300 | [diff] [blame] | 4 | * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd. |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 5 | * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/bug.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/device.h> |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 20 | #include <linux/list.h> |
| 21 | #include <linux/slab.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 22 | |
| 23 | #include <linux/videodev2.h> |
| 24 | #include <media/v4l2-device.h> |
| 25 | #include <media/v4l2-ioctl.h> |
| 26 | #include <media/v4l2-mem2mem.h> |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 27 | #include <media/videobuf2-core.h> |
| 28 | #include <media/videobuf2-dma-contig.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 29 | |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 30 | #include "fimc-mdevice.h" |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 31 | #include "fimc-core.h" |
| 32 | |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 33 | static int fimc_init_capture(struct fimc_dev *fimc) |
| 34 | { |
| 35 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 36 | struct fimc_sensor_info *sensor; |
| 37 | unsigned long flags; |
| 38 | int ret = 0; |
| 39 | |
| 40 | if (fimc->pipeline.sensor == NULL || ctx == NULL) |
| 41 | return -ENXIO; |
| 42 | if (ctx->s_frame.fmt == NULL) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor); |
| 46 | |
| 47 | spin_lock_irqsave(&fimc->slock, flags); |
| 48 | fimc_prepare_dma_offset(ctx, &ctx->d_frame); |
| 49 | fimc_set_yuv_order(ctx); |
| 50 | |
| 51 | fimc_hw_set_camera_polarity(fimc, sensor->pdata); |
| 52 | fimc_hw_set_camera_type(fimc, sensor->pdata); |
| 53 | fimc_hw_set_camera_source(fimc, sensor->pdata); |
| 54 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); |
| 55 | |
| 56 | ret = fimc_set_scaler_info(ctx); |
| 57 | if (!ret) { |
| 58 | fimc_hw_set_input_path(ctx); |
| 59 | fimc_hw_set_prescaler(ctx); |
| 60 | fimc_hw_set_mainscaler(ctx); |
| 61 | fimc_hw_set_target_format(ctx); |
| 62 | fimc_hw_set_rotation(ctx); |
Sylwester Nawrocki | ee7160e | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 63 | fimc_hw_set_effect(ctx, false); |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 64 | fimc_hw_set_output_path(ctx); |
| 65 | fimc_hw_set_out_dma(ctx); |
Sylwester Nawrocki | dafb9c7 | 2011-12-01 14:02:24 -0300 | [diff] [blame] | 66 | if (fimc->variant->has_alpha) |
| 67 | fimc_hw_set_rgb_alpha(ctx); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 68 | clear_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 69 | } |
| 70 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 71 | return ret; |
| 72 | } |
| 73 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 74 | static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 75 | { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 76 | struct fimc_vid_cap *cap = &fimc->vid_cap; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 77 | struct fimc_vid_buffer *buf; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 78 | unsigned long flags; |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 79 | bool streaming; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 80 | |
| 81 | spin_lock_irqsave(&fimc->slock, flags); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 82 | streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 83 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 84 | fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT | |
| 85 | 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM); |
Sylwester Nawrocki | aa333122 | 2012-03-16 11:47:51 -0300 | [diff] [blame] | 86 | if (suspend) |
| 87 | fimc->state |= (1 << ST_CAPT_SUSPENDED); |
| 88 | else |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 89 | fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 90 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 91 | /* Release unused buffers */ |
| 92 | while (!suspend && !list_empty(&cap->pending_buf_q)) { |
Sylwester Nawrocki | 0295202 | 2011-06-10 15:36:59 -0300 | [diff] [blame] | 93 | buf = fimc_pending_queue_pop(cap); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 94 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 95 | } |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 96 | /* If suspending put unused buffers onto pending queue */ |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 97 | while (!list_empty(&cap->active_buf_q)) { |
Sylwester Nawrocki | 0295202 | 2011-06-10 15:36:59 -0300 | [diff] [blame] | 98 | buf = fimc_active_queue_pop(cap); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 99 | if (suspend) |
| 100 | fimc_pending_queue_add(cap, buf); |
| 101 | else |
| 102 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 103 | } |
Sylwester Nawrocki | 2c1bb62 | 2011-10-05 14:20:45 -0300 | [diff] [blame] | 104 | |
| 105 | fimc_hw_reset(fimc); |
| 106 | cap->buf_index = 0; |
| 107 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 108 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 109 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 110 | if (streaming) |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 111 | return fimc_pipeline_s_stream(fimc, 0); |
| 112 | else |
| 113 | return 0; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 114 | } |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 115 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 116 | static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend) |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 117 | { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 118 | unsigned long flags; |
| 119 | |
| 120 | if (!fimc_capture_active(fimc)) |
| 121 | return 0; |
| 122 | |
| 123 | spin_lock_irqsave(&fimc->slock, flags); |
| 124 | set_bit(ST_CAPT_SHUT, &fimc->state); |
| 125 | fimc_deactivate_capture(fimc); |
| 126 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 127 | |
| 128 | wait_event_timeout(fimc->irq_queue, |
| 129 | !test_bit(ST_CAPT_SHUT, &fimc->state), |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 130 | (2*HZ/10)); /* 200 ms */ |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 131 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 132 | return fimc_capture_state_cleanup(fimc, suspend); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 133 | } |
| 134 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 135 | /** |
| 136 | * fimc_capture_config_update - apply the camera interface configuration |
| 137 | * |
| 138 | * To be called from within the interrupt handler with fimc.slock |
| 139 | * spinlock held. It updates the camera pixel crop, rotation and |
| 140 | * image flip in H/W. |
| 141 | */ |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame^] | 142 | static int fimc_capture_config_update(struct fimc_ctx *ctx) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 143 | { |
| 144 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 145 | int ret; |
| 146 | |
Sylwester Nawrocki | 2c1bb62 | 2011-10-05 14:20:45 -0300 | [diff] [blame] | 147 | if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state)) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 148 | return 0; |
| 149 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 150 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); |
Sylwester Nawrocki | efb13c3 | 2012-03-19 13:11:40 -0300 | [diff] [blame] | 151 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 152 | ret = fimc_set_scaler_info(ctx); |
Sylwester Nawrocki | efb13c3 | 2012-03-19 13:11:40 -0300 | [diff] [blame] | 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | fimc_hw_set_prescaler(ctx); |
| 157 | fimc_hw_set_mainscaler(ctx); |
| 158 | fimc_hw_set_target_format(ctx); |
| 159 | fimc_hw_set_rotation(ctx); |
| 160 | fimc_prepare_dma_offset(ctx, &ctx->d_frame); |
| 161 | fimc_hw_set_out_dma(ctx); |
| 162 | if (fimc->variant->has_alpha) |
| 163 | fimc_hw_set_rgb_alpha(ctx); |
| 164 | |
| 165 | clear_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 166 | return ret; |
| 167 | } |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 168 | |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame^] | 169 | void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf) |
| 170 | { |
| 171 | struct fimc_vid_cap *cap = &fimc->vid_cap; |
| 172 | struct fimc_vid_buffer *v_buf; |
| 173 | struct timeval *tv; |
| 174 | struct timespec ts; |
| 175 | |
| 176 | if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) { |
| 177 | wake_up(&fimc->irq_queue); |
| 178 | goto done; |
| 179 | } |
| 180 | |
| 181 | if (!list_empty(&cap->active_buf_q) && |
| 182 | test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) { |
| 183 | ktime_get_real_ts(&ts); |
| 184 | |
| 185 | v_buf = fimc_active_queue_pop(cap); |
| 186 | |
| 187 | tv = &v_buf->vb.v4l2_buf.timestamp; |
| 188 | tv->tv_sec = ts.tv_sec; |
| 189 | tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 190 | v_buf->vb.v4l2_buf.sequence = cap->frame_count++; |
| 191 | |
| 192 | vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE); |
| 193 | } |
| 194 | |
| 195 | if (!list_empty(&cap->pending_buf_q)) { |
| 196 | |
| 197 | v_buf = fimc_pending_queue_pop(cap); |
| 198 | fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index); |
| 199 | v_buf->index = cap->buf_index; |
| 200 | |
| 201 | /* Move the buffer to the capture active queue */ |
| 202 | fimc_active_queue_add(cap, v_buf); |
| 203 | |
| 204 | dbg("next frame: %d, done frame: %d", |
| 205 | fimc_hw_get_frame_index(fimc), v_buf->index); |
| 206 | |
| 207 | if (++cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 208 | cap->buf_index = 0; |
| 209 | } |
| 210 | |
| 211 | if (cap->active_buf_cnt == 0) { |
| 212 | if (deq_buf) |
| 213 | clear_bit(ST_CAPT_RUN, &fimc->state); |
| 214 | |
| 215 | if (++cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 216 | cap->buf_index = 0; |
| 217 | } else { |
| 218 | set_bit(ST_CAPT_RUN, &fimc->state); |
| 219 | } |
| 220 | |
| 221 | fimc_capture_config_update(cap->ctx); |
| 222 | done: |
| 223 | if (cap->active_buf_cnt == 1) { |
| 224 | fimc_deactivate_capture(fimc); |
| 225 | clear_bit(ST_CAPT_STREAM, &fimc->state); |
| 226 | } |
| 227 | |
| 228 | dbg("frame: %d, active_buf_cnt: %d", |
| 229 | fimc_hw_get_frame_index(fimc), cap->active_buf_cnt); |
| 230 | } |
| 231 | |
| 232 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 233 | static int start_streaming(struct vb2_queue *q, unsigned int count) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 234 | { |
| 235 | struct fimc_ctx *ctx = q->drv_priv; |
| 236 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 237 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 238 | int min_bufs; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 239 | int ret; |
| 240 | |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 241 | vid_cap->frame_count = 0; |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 242 | |
Sylwester Nawrocki | 9e803a0 | 2011-06-10 15:36:53 -0300 | [diff] [blame] | 243 | ret = fimc_init_capture(fimc); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 244 | if (ret) |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 245 | goto error; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 246 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 247 | set_bit(ST_CAPT_PEND, &fimc->state); |
| 248 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 249 | min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1; |
| 250 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 251 | if (vid_cap->active_buf_cnt >= min_bufs && |
| 252 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 253 | fimc_activate_capture(ctx); |
| 254 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 255 | if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state)) |
| 256 | fimc_pipeline_s_stream(fimc, 1); |
| 257 | } |
| 258 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 259 | return 0; |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 260 | error: |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 261 | fimc_capture_state_cleanup(fimc, false); |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 262 | return ret; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static int stop_streaming(struct vb2_queue *q) |
| 266 | { |
| 267 | struct fimc_ctx *ctx = q->drv_priv; |
| 268 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 269 | |
Sylwester Nawrocki | 4ecbf5d | 2011-02-23 08:24:33 -0300 | [diff] [blame] | 270 | if (!fimc_capture_active(fimc)) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 271 | return -EINVAL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 272 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 273 | return fimc_stop_capture(fimc, false); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 274 | } |
| 275 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 276 | int fimc_capture_suspend(struct fimc_dev *fimc) |
| 277 | { |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 278 | bool suspend = fimc_capture_busy(fimc); |
| 279 | |
| 280 | int ret = fimc_stop_capture(fimc, suspend); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | return fimc_pipeline_shutdown(fimc); |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 284 | } |
| 285 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 286 | static void buffer_queue(struct vb2_buffer *vb); |
| 287 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 288 | int fimc_capture_resume(struct fimc_dev *fimc) |
| 289 | { |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 290 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 291 | struct fimc_vid_buffer *buf; |
| 292 | int i; |
| 293 | |
| 294 | if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state)) |
| 295 | return 0; |
| 296 | |
| 297 | INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q); |
| 298 | vid_cap->buf_index = 0; |
| 299 | fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity, |
| 300 | false); |
| 301 | fimc_init_capture(fimc); |
| 302 | |
| 303 | clear_bit(ST_CAPT_SUSPENDED, &fimc->state); |
| 304 | |
| 305 | for (i = 0; i < vid_cap->reqbufs_count; i++) { |
| 306 | if (list_empty(&vid_cap->pending_buf_q)) |
| 307 | break; |
| 308 | buf = fimc_pending_queue_pop(vid_cap); |
| 309 | buffer_queue(&buf->vb); |
| 310 | } |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 311 | return 0; |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 312 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 313 | } |
| 314 | |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 315 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt, |
Guennadi Liakhovetski | fc714e7 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 316 | unsigned int *num_buffers, unsigned int *num_planes, |
| 317 | unsigned int sizes[], void *allocators[]) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 318 | { |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 319 | const struct v4l2_pix_format_mplane *pixm = NULL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 320 | struct fimc_ctx *ctx = vq->drv_priv; |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 321 | struct fimc_frame *frame = &ctx->d_frame; |
| 322 | struct fimc_fmt *fmt = frame->fmt; |
| 323 | unsigned long wh; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 324 | int i; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 325 | |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 326 | if (pfmt) { |
| 327 | pixm = &pfmt->fmt.pix_mp; |
| 328 | fmt = fimc_find_format(&pixm->pixelformat, NULL, |
| 329 | FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1); |
| 330 | wh = pixm->width * pixm->height; |
| 331 | } else { |
| 332 | wh = frame->f_width * frame->f_height; |
| 333 | } |
| 334 | |
| 335 | if (fmt == NULL) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 336 | return -EINVAL; |
| 337 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 338 | *num_planes = fmt->memplanes; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 339 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 340 | for (i = 0; i < fmt->memplanes; i++) { |
Sylwester Nawrocki | 63746be | 2012-04-22 17:07:09 -0300 | [diff] [blame] | 341 | unsigned int size = (wh * fmt->depth[i]) / 8; |
| 342 | if (pixm) |
| 343 | sizes[i] = max(size, pixm->plane_fmt[i].sizeimage); |
| 344 | else |
| 345 | sizes[i] = size; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 346 | allocators[i] = ctx->fimc_dev->alloc_ctx; |
| 347 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 348 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 349 | return 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 350 | } |
| 351 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 352 | static int buffer_prepare(struct vb2_buffer *vb) |
| 353 | { |
| 354 | struct vb2_queue *vq = vb->vb2_queue; |
| 355 | struct fimc_ctx *ctx = vq->drv_priv; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 356 | int i; |
| 357 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 358 | if (ctx->d_frame.fmt == NULL) |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 359 | return -EINVAL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 360 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 361 | for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) { |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 362 | unsigned long size = ctx->d_frame.payload[i]; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 363 | |
| 364 | if (vb2_plane_size(vb, i) < size) { |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 365 | v4l2_err(ctx->fimc_dev->vid_cap.vfd, |
| 366 | "User buffer too small (%ld < %ld)\n", |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 367 | vb2_plane_size(vb, i), size); |
| 368 | return -EINVAL; |
| 369 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 370 | vb2_set_plane_payload(vb, i, size); |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static void buffer_queue(struct vb2_buffer *vb) |
| 377 | { |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 378 | struct fimc_vid_buffer *buf |
| 379 | = container_of(vb, struct fimc_vid_buffer, vb); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 380 | struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 381 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 382 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 383 | unsigned long flags; |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 384 | int min_bufs; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 385 | |
| 386 | spin_lock_irqsave(&fimc->slock, flags); |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 387 | fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 388 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 389 | if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) && |
| 390 | !test_bit(ST_CAPT_STREAM, &fimc->state) && |
| 391 | vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) { |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 392 | /* Setup the buffer directly for processing. */ |
| 393 | int buf_id = (vid_cap->reqbufs_count == 1) ? -1 : |
| 394 | vid_cap->buf_index; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 395 | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 396 | fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id); |
| 397 | buf->index = vid_cap->buf_index; |
Sylwester Nawrocki | 0295202 | 2011-06-10 15:36:59 -0300 | [diff] [blame] | 398 | fimc_active_queue_add(vid_cap, buf); |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 399 | |
| 400 | if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 401 | vid_cap->buf_index = 0; |
| 402 | } else { |
| 403 | fimc_pending_queue_add(vid_cap, buf); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 404 | } |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 405 | |
| 406 | min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1; |
| 407 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 408 | |
Marek Szyprowski | bd323e2 | 2011-08-29 08:51:49 -0300 | [diff] [blame] | 409 | if (vb2_is_streaming(&vid_cap->vbq) && |
| 410 | vid_cap->active_buf_cnt >= min_bufs && |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 411 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 412 | fimc_activate_capture(ctx); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 413 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 414 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 415 | if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state)) |
| 416 | fimc_pipeline_s_stream(fimc, 1); |
| 417 | return; |
| 418 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 419 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 420 | } |
| 421 | |
| 422 | static void fimc_lock(struct vb2_queue *vq) |
| 423 | { |
| 424 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); |
| 425 | mutex_lock(&ctx->fimc_dev->lock); |
| 426 | } |
| 427 | |
| 428 | static void fimc_unlock(struct vb2_queue *vq) |
| 429 | { |
| 430 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); |
| 431 | mutex_unlock(&ctx->fimc_dev->lock); |
| 432 | } |
| 433 | |
| 434 | static struct vb2_ops fimc_capture_qops = { |
| 435 | .queue_setup = queue_setup, |
| 436 | .buf_prepare = buffer_prepare, |
| 437 | .buf_queue = buffer_queue, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 438 | .wait_prepare = fimc_unlock, |
| 439 | .wait_finish = fimc_lock, |
| 440 | .start_streaming = start_streaming, |
| 441 | .stop_streaming = stop_streaming, |
| 442 | }; |
| 443 | |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 444 | /** |
| 445 | * fimc_capture_ctrls_create - initialize the control handler |
| 446 | * Initialize the capture video node control handler and fill it |
| 447 | * with the FIMC controls. Inherit any sensor's controls if the |
| 448 | * 'user_subdev_api' flag is false (default behaviour). |
| 449 | * This function need to be called with the graph mutex held. |
| 450 | */ |
| 451 | int fimc_capture_ctrls_create(struct fimc_dev *fimc) |
| 452 | { |
| 453 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 454 | int ret; |
| 455 | |
| 456 | if (WARN_ON(vid_cap->ctx == NULL)) |
| 457 | return -ENXIO; |
| 458 | if (vid_cap->ctx->ctrls_rdy) |
| 459 | return 0; |
| 460 | |
| 461 | ret = fimc_ctrls_create(vid_cap->ctx); |
| 462 | if (ret || vid_cap->user_subdev_api) |
| 463 | return ret; |
| 464 | |
| 465 | return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler, |
| 466 | fimc->pipeline.sensor->ctrl_handler); |
| 467 | } |
| 468 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 469 | static int fimc_capture_set_default_format(struct fimc_dev *fimc); |
| 470 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 471 | static int fimc_capture_open(struct file *file) |
| 472 | { |
| 473 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 474 | int ret = v4l2_fh_open(file); |
| 475 | |
| 476 | if (ret) |
| 477 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 478 | |
| 479 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 480 | |
| 481 | /* Return if the corresponding video mem2mem node is already opened. */ |
| 482 | if (fimc_m2m_active(fimc)) |
| 483 | return -EBUSY; |
| 484 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 485 | set_bit(ST_CAPT_BUSY, &fimc->state); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 486 | pm_runtime_get_sync(&fimc->pdev->dev); |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 487 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 488 | if (++fimc->vid_cap.refcnt == 1) { |
| 489 | ret = fimc_pipeline_initialize(fimc, |
| 490 | &fimc->vid_cap.vfd->entity, true); |
| 491 | if (ret < 0) { |
| 492 | dev_err(&fimc->pdev->dev, |
| 493 | "Video pipeline initialization failed\n"); |
| 494 | pm_runtime_put_sync(&fimc->pdev->dev); |
| 495 | fimc->vid_cap.refcnt--; |
| 496 | v4l2_fh_release(file); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 497 | clear_bit(ST_CAPT_BUSY, &fimc->state); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 498 | return ret; |
| 499 | } |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 500 | ret = fimc_capture_ctrls_create(fimc); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 501 | |
| 502 | if (!ret && !fimc->vid_cap.user_subdev_api) |
| 503 | ret = fimc_capture_set_default_format(fimc); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 504 | } |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 505 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static int fimc_capture_close(struct file *file) |
| 509 | { |
| 510 | struct fimc_dev *fimc = video_drvdata(file); |
| 511 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 512 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 513 | |
| 514 | if (--fimc->vid_cap.refcnt == 0) { |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 515 | clear_bit(ST_CAPT_BUSY, &fimc->state); |
| 516 | fimc_stop_capture(fimc, false); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 517 | fimc_pipeline_shutdown(fimc); |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 518 | clear_bit(ST_CAPT_SUSPENDED, &fimc->state); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 519 | } |
| 520 | |
Sylwester Nawrocki | e9e2108 | 2011-09-02 06:25:32 -0300 | [diff] [blame] | 521 | pm_runtime_put(&fimc->pdev->dev); |
| 522 | |
Sylwester Nawrocki | 3e4748d | 2011-08-24 20:45:34 -0300 | [diff] [blame] | 523 | if (fimc->vid_cap.refcnt == 0) { |
| 524 | vb2_queue_release(&fimc->vid_cap.vbq); |
| 525 | fimc_ctrls_delete(fimc->vid_cap.ctx); |
| 526 | } |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 527 | return v4l2_fh_release(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static unsigned int fimc_capture_poll(struct file *file, |
| 531 | struct poll_table_struct *wait) |
| 532 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 533 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 534 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 535 | return vb2_poll(&fimc->vid_cap.vbq, file, wait); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma) |
| 539 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 540 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 541 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 542 | return vb2_mmap(&fimc->vid_cap.vbq, vma); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 543 | } |
| 544 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 545 | static const struct v4l2_file_operations fimc_capture_fops = { |
| 546 | .owner = THIS_MODULE, |
| 547 | .open = fimc_capture_open, |
| 548 | .release = fimc_capture_close, |
| 549 | .poll = fimc_capture_poll, |
| 550 | .unlocked_ioctl = video_ioctl2, |
| 551 | .mmap = fimc_capture_mmap, |
| 552 | }; |
| 553 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 554 | /* |
| 555 | * Format and crop negotiation helpers |
| 556 | */ |
| 557 | |
| 558 | static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx, |
| 559 | u32 *width, u32 *height, |
| 560 | u32 *code, u32 *fourcc, int pad) |
| 561 | { |
| 562 | bool rotation = ctx->rotation == 90 || ctx->rotation == 270; |
| 563 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 564 | struct samsung_fimc_variant *var = fimc->variant; |
| 565 | struct fimc_pix_limit *pl = var->pix_limit; |
| 566 | struct fimc_frame *dst = &ctx->d_frame; |
| 567 | u32 depth, min_w, max_w, min_h, align_h = 3; |
| 568 | u32 mask = FMT_FLAGS_CAM; |
| 569 | struct fimc_fmt *ffmt; |
| 570 | |
| 571 | /* Color conversion from/to JPEG is not supported */ |
| 572 | if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE && |
| 573 | fimc_fmt_is_jpeg(ctx->s_frame.fmt->color)) |
| 574 | *code = V4L2_MBUS_FMT_JPEG_1X8; |
| 575 | |
| 576 | if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK) |
| 577 | mask |= FMT_FLAGS_M2M; |
| 578 | |
| 579 | ffmt = fimc_find_format(fourcc, code, mask, 0); |
| 580 | if (WARN_ON(!ffmt)) |
| 581 | return NULL; |
| 582 | if (code) |
| 583 | *code = ffmt->mbus_code; |
| 584 | if (fourcc) |
| 585 | *fourcc = ffmt->fourcc; |
| 586 | |
| 587 | if (pad == FIMC_SD_PAD_SINK) { |
| 588 | max_w = fimc_fmt_is_jpeg(ffmt->color) ? |
| 589 | pl->scaler_dis_w : pl->scaler_en_w; |
| 590 | /* Apply the camera input interface pixel constraints */ |
| 591 | v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4, |
| 592 | height, max_t(u32, *height, 32), |
| 593 | FIMC_CAMIF_MAX_HEIGHT, |
| 594 | fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1, |
| 595 | 0); |
| 596 | return ffmt; |
| 597 | } |
| 598 | /* Can't scale or crop in transparent (JPEG) transfer mode */ |
| 599 | if (fimc_fmt_is_jpeg(ffmt->color)) { |
| 600 | *width = ctx->s_frame.f_width; |
| 601 | *height = ctx->s_frame.f_height; |
| 602 | return ffmt; |
| 603 | } |
| 604 | /* Apply the scaler and the output DMA constraints */ |
| 605 | max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w; |
| 606 | min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize; |
| 607 | min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize; |
Sylwester Nawrocki | 9c63afc | 2011-05-27 13:12:23 -0300 | [diff] [blame] | 608 | if (var->min_vsize_align == 1 && !rotation) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 609 | align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1; |
| 610 | |
| 611 | depth = fimc_get_format_depth(ffmt); |
| 612 | v4l_bound_align_image(width, min_w, max_w, |
| 613 | ffs(var->min_out_pixsize) - 1, |
| 614 | height, min_h, FIMC_CAMIF_MAX_HEIGHT, |
| 615 | align_h, |
| 616 | 64/(ALIGN(depth, 8))); |
| 617 | |
| 618 | dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d", |
| 619 | pad, code ? *code : 0, *width, *height, |
| 620 | dst->f_width, dst->f_height); |
| 621 | |
| 622 | return ffmt; |
| 623 | } |
| 624 | |
| 625 | static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r, |
| 626 | int pad) |
| 627 | { |
| 628 | bool rotate = ctx->rotation == 90 || ctx->rotation == 270; |
| 629 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 630 | struct samsung_fimc_variant *var = fimc->variant; |
| 631 | struct fimc_pix_limit *pl = var->pix_limit; |
| 632 | struct fimc_frame *sink = &ctx->s_frame; |
| 633 | u32 max_w, max_h, min_w = 0, min_h = 0, min_sz; |
| 634 | u32 align_sz = 0, align_h = 4; |
| 635 | u32 max_sc_h, max_sc_v; |
| 636 | |
| 637 | /* In JPEG transparent transfer mode cropping is not supported */ |
| 638 | if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) { |
| 639 | r->width = sink->f_width; |
| 640 | r->height = sink->f_height; |
| 641 | r->left = r->top = 0; |
| 642 | return; |
| 643 | } |
| 644 | if (pad == FIMC_SD_PAD_SOURCE) { |
| 645 | if (ctx->rotation != 90 && ctx->rotation != 270) |
| 646 | align_h = 1; |
| 647 | max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3)); |
| 648 | max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1)); |
| 649 | min_sz = var->min_out_pixsize; |
| 650 | } else { |
| 651 | u32 depth = fimc_get_format_depth(sink->fmt); |
| 652 | align_sz = 64/ALIGN(depth, 8); |
| 653 | min_sz = var->min_inp_pixsize; |
| 654 | min_w = min_h = min_sz; |
| 655 | max_sc_h = max_sc_v = 1; |
| 656 | } |
| 657 | /* |
| 658 | * For the crop rectangle at source pad the following constraints |
| 659 | * must be met: |
| 660 | * - it must fit in the sink pad format rectangle (f_width/f_height); |
| 661 | * - maximum downscaling ratio is 64; |
| 662 | * - maximum crop size depends if the rotator is used or not; |
| 663 | * - the sink pad format width/height must be 4 multiple of the |
| 664 | * prescaler ratios determined by sink pad size and source pad crop, |
| 665 | * the prescaler ratio is returned by fimc_get_scaler_factor(). |
| 666 | */ |
| 667 | max_w = min_t(u32, |
| 668 | rotate ? pl->out_rot_en_w : pl->out_rot_dis_w, |
| 669 | rotate ? sink->f_height : sink->f_width); |
| 670 | max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height); |
| 671 | if (pad == FIMC_SD_PAD_SOURCE) { |
| 672 | min_w = min_t(u32, max_w, sink->f_width / max_sc_h); |
| 673 | min_h = min_t(u32, max_h, sink->f_height / max_sc_v); |
| 674 | if (rotate) { |
| 675 | swap(max_sc_h, max_sc_v); |
| 676 | swap(min_w, min_h); |
| 677 | } |
| 678 | } |
| 679 | v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1, |
| 680 | &r->height, min_h, max_h, align_h, |
| 681 | align_sz); |
| 682 | /* Adjust left/top if cropping rectangle is out of bounds */ |
| 683 | r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width); |
| 684 | r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height); |
| 685 | r->left = round_down(r->left, var->hor_offs_align); |
| 686 | |
| 687 | dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d", |
| 688 | pad, r->left, r->top, r->width, r->height, |
| 689 | sink->f_width, sink->f_height); |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * The video node ioctl operations |
| 694 | */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 695 | static int fimc_vidioc_querycap_capture(struct file *file, void *priv, |
| 696 | struct v4l2_capability *cap) |
| 697 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 698 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 699 | |
| 700 | strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1); |
| 701 | strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1); |
| 702 | cap->bus_info[0] = 0; |
Sylwester Nawrocki | 8f40154 | 2011-07-07 06:42:05 -0300 | [diff] [blame] | 703 | cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
Sylwester Nawrocki | cf52df8 | 2011-06-13 11:09:40 -0300 | [diff] [blame] | 708 | static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv, |
| 709 | struct v4l2_fmtdesc *f) |
| 710 | { |
| 711 | struct fimc_fmt *fmt; |
| 712 | |
| 713 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M, |
| 714 | f->index); |
| 715 | if (!fmt) |
| 716 | return -EINVAL; |
| 717 | strncpy(f->description, fmt->name, sizeof(f->description) - 1); |
| 718 | f->pixelformat = fmt->fourcc; |
| 719 | if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8) |
| 720 | f->flags |= V4L2_FMT_FLAG_COMPRESSED; |
| 721 | return 0; |
| 722 | } |
| 723 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 724 | /** |
| 725 | * fimc_pipeline_try_format - negotiate and/or set formats at pipeline |
| 726 | * elements |
| 727 | * @ctx: FIMC capture context |
| 728 | * @tfmt: media bus format to try/set on subdevs |
| 729 | * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output) |
| 730 | * @set: true to set format on subdevs, false to try only |
| 731 | */ |
| 732 | static int fimc_pipeline_try_format(struct fimc_ctx *ctx, |
| 733 | struct v4l2_mbus_framefmt *tfmt, |
| 734 | struct fimc_fmt **fmt_id, |
| 735 | bool set) |
| 736 | { |
| 737 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 738 | struct v4l2_subdev *sd = fimc->pipeline.sensor; |
| 739 | struct v4l2_subdev *csis = fimc->pipeline.csis; |
| 740 | struct v4l2_subdev_format sfmt; |
| 741 | struct v4l2_mbus_framefmt *mf = &sfmt.format; |
| 742 | struct fimc_fmt *ffmt = NULL; |
| 743 | int ret, i = 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 744 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 745 | if (WARN_ON(!sd || !tfmt)) |
| 746 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 747 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 748 | memset(&sfmt, 0, sizeof(sfmt)); |
| 749 | sfmt.format = *tfmt; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 750 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 751 | sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY; |
| 752 | while (1) { |
| 753 | ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL, |
| 754 | FMT_FLAGS_CAM, i++); |
| 755 | if (ffmt == NULL) { |
| 756 | /* |
| 757 | * Notify user-space if common pixel code for |
| 758 | * host and sensor does not exist. |
| 759 | */ |
| 760 | return -EINVAL; |
| 761 | } |
| 762 | mf->code = tfmt->code = ffmt->mbus_code; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 763 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 764 | ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt); |
| 765 | if (ret) |
| 766 | return ret; |
| 767 | if (mf->code != tfmt->code) { |
| 768 | mf->code = 0; |
| 769 | continue; |
| 770 | } |
Julia Lawall | b1aa608 | 2012-01-12 17:49:28 -0300 | [diff] [blame] | 771 | if (mf->width != tfmt->width || mf->height != tfmt->height) { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 772 | u32 fcc = ffmt->fourcc; |
| 773 | tfmt->width = mf->width; |
| 774 | tfmt->height = mf->height; |
| 775 | ffmt = fimc_capture_try_format(ctx, |
| 776 | &tfmt->width, &tfmt->height, |
| 777 | NULL, &fcc, FIMC_SD_PAD_SOURCE); |
| 778 | if (ffmt && ffmt->mbus_code) |
| 779 | mf->code = ffmt->mbus_code; |
Julia Lawall | b1aa608 | 2012-01-12 17:49:28 -0300 | [diff] [blame] | 780 | if (mf->width != tfmt->width || |
| 781 | mf->height != tfmt->height) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 782 | continue; |
| 783 | tfmt->code = mf->code; |
| 784 | } |
| 785 | if (csis) |
| 786 | ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 787 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 788 | if (mf->code == tfmt->code && |
Julia Lawall | b1aa608 | 2012-01-12 17:49:28 -0300 | [diff] [blame] | 789 | mf->width == tfmt->width && mf->height == tfmt->height) |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 790 | break; |
| 791 | } |
| 792 | |
| 793 | if (fmt_id && ffmt) |
| 794 | *fmt_id = ffmt; |
| 795 | *tfmt = *mf; |
| 796 | |
| 797 | dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt); |
| 798 | return 0; |
| 799 | } |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 800 | |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 801 | static int fimc_cap_g_fmt_mplane(struct file *file, void *fh, |
| 802 | struct v4l2_format *f) |
| 803 | { |
| 804 | struct fimc_dev *fimc = video_drvdata(file); |
| 805 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 806 | |
| 807 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 808 | return -EINVAL; |
| 809 | |
| 810 | return fimc_fill_format(&ctx->d_frame, f); |
| 811 | } |
| 812 | |
| 813 | static int fimc_cap_try_fmt_mplane(struct file *file, void *fh, |
| 814 | struct v4l2_format *f) |
| 815 | { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 816 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 817 | struct fimc_dev *fimc = video_drvdata(file); |
| 818 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 819 | struct v4l2_mbus_framefmt mf; |
| 820 | struct fimc_fmt *ffmt = NULL; |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 821 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 822 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 823 | return -EINVAL; |
| 824 | |
| 825 | if (pix->pixelformat == V4L2_PIX_FMT_JPEG) { |
| 826 | fimc_capture_try_format(ctx, &pix->width, &pix->height, |
| 827 | NULL, &pix->pixelformat, |
| 828 | FIMC_SD_PAD_SINK); |
| 829 | ctx->s_frame.f_width = pix->width; |
| 830 | ctx->s_frame.f_height = pix->height; |
| 831 | } |
| 832 | ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height, |
| 833 | NULL, &pix->pixelformat, |
| 834 | FIMC_SD_PAD_SOURCE); |
| 835 | if (!ffmt) |
| 836 | return -EINVAL; |
| 837 | |
| 838 | if (!fimc->vid_cap.user_subdev_api) { |
| 839 | mf.width = pix->width; |
| 840 | mf.height = pix->height; |
| 841 | mf.code = ffmt->mbus_code; |
| 842 | fimc_md_graph_lock(fimc); |
| 843 | fimc_pipeline_try_format(ctx, &mf, &ffmt, false); |
| 844 | fimc_md_graph_unlock(fimc); |
| 845 | |
| 846 | pix->width = mf.width; |
| 847 | pix->height = mf.height; |
| 848 | if (ffmt) |
| 849 | pix->pixelformat = ffmt->fourcc; |
| 850 | } |
| 851 | |
| 852 | fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 853 | return 0; |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 854 | } |
| 855 | |
Sylwester Nawrocki | ee7160e | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 856 | static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg) |
| 857 | { |
| 858 | ctx->scaler.enabled = !jpeg; |
| 859 | fimc_ctrls_activate(ctx, !jpeg); |
| 860 | |
| 861 | if (jpeg) |
| 862 | set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state); |
| 863 | else |
| 864 | clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state); |
| 865 | } |
| 866 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 867 | static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f) |
| 868 | { |
| 869 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 870 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
| 871 | struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf; |
| 872 | struct fimc_frame *ff = &ctx->d_frame; |
| 873 | struct fimc_fmt *s_fmt = NULL; |
| 874 | int ret, i; |
| 875 | |
| 876 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 877 | return -EINVAL; |
| 878 | if (vb2_is_busy(&fimc->vid_cap.vbq)) |
| 879 | return -EBUSY; |
| 880 | |
| 881 | /* Pre-configure format at camera interface input, for JPEG only */ |
| 882 | if (pix->pixelformat == V4L2_PIX_FMT_JPEG) { |
| 883 | fimc_capture_try_format(ctx, &pix->width, &pix->height, |
| 884 | NULL, &pix->pixelformat, |
| 885 | FIMC_SD_PAD_SINK); |
| 886 | ctx->s_frame.f_width = pix->width; |
| 887 | ctx->s_frame.f_height = pix->height; |
| 888 | } |
| 889 | /* Try the format at the scaler and the DMA output */ |
| 890 | ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height, |
| 891 | NULL, &pix->pixelformat, |
| 892 | FIMC_SD_PAD_SOURCE); |
| 893 | if (!ff->fmt) |
| 894 | return -EINVAL; |
Sylwester Nawrocki | dafb9c7 | 2011-12-01 14:02:24 -0300 | [diff] [blame] | 895 | |
| 896 | /* Update RGB Alpha control state and value range */ |
| 897 | fimc_alpha_ctrl_update(ctx); |
| 898 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 899 | /* Try to match format at the host and the sensor */ |
| 900 | if (!fimc->vid_cap.user_subdev_api) { |
| 901 | mf->code = ff->fmt->mbus_code; |
| 902 | mf->width = pix->width; |
| 903 | mf->height = pix->height; |
| 904 | |
| 905 | fimc_md_graph_lock(fimc); |
| 906 | ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true); |
| 907 | fimc_md_graph_unlock(fimc); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | pix->width = mf->width; |
| 911 | pix->height = mf->height; |
| 912 | } |
| 913 | fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix); |
| 914 | for (i = 0; i < ff->fmt->colplanes; i++) |
| 915 | ff->payload[i] = |
| 916 | (pix->width * pix->height * ff->fmt->depth[i]) / 8; |
| 917 | |
| 918 | set_frame_bounds(ff, pix->width, pix->height); |
| 919 | /* Reset the composition rectangle if not yet configured */ |
| 920 | if (!(ctx->state & FIMC_DST_CROP)) |
| 921 | set_frame_crop(ff, 0, 0, pix->width, pix->height); |
| 922 | |
Sylwester Nawrocki | ee7160e | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 923 | fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color)); |
| 924 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 925 | /* Reset cropping and set format at the camera interface input */ |
| 926 | if (!fimc->vid_cap.user_subdev_api) { |
| 927 | ctx->s_frame.fmt = s_fmt; |
| 928 | set_frame_bounds(&ctx->s_frame, pix->width, pix->height); |
| 929 | set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height); |
| 930 | } |
| 931 | |
| 932 | return ret; |
| 933 | } |
| 934 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 935 | static int fimc_cap_s_fmt_mplane(struct file *file, void *priv, |
| 936 | struct v4l2_format *f) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 937 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 938 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 939 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 940 | return fimc_capture_set_format(fimc, f); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static int fimc_cap_enum_input(struct file *file, void *priv, |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 944 | struct v4l2_input *i) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 945 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 946 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 947 | struct v4l2_subdev *sd = fimc->pipeline.sensor; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 948 | |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 949 | if (i->index != 0) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 950 | return -EINVAL; |
| 951 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 952 | i->type = V4L2_INPUT_TYPE_CAMERA; |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 953 | if (sd) |
| 954 | strlcpy(i->name, sd->name, sizeof(i->name)); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 958 | 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] | 959 | { |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 960 | return i == 0 ? i : -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 961 | } |
| 962 | |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 963 | 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] | 964 | { |
Sylwester Nawrocki | 3e00218 | 2011-07-26 18:27:59 -0300 | [diff] [blame] | 965 | *i = 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 966 | return 0; |
| 967 | } |
| 968 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 969 | /** |
| 970 | * fimc_pipeline_validate - check for formats inconsistencies |
| 971 | * between source and sink pad of each link |
| 972 | * |
| 973 | * Return 0 if all formats match or -EPIPE otherwise. |
| 974 | */ |
| 975 | static int fimc_pipeline_validate(struct fimc_dev *fimc) |
| 976 | { |
| 977 | struct v4l2_subdev_format sink_fmt, src_fmt; |
| 978 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 979 | struct v4l2_subdev *sd; |
| 980 | struct media_pad *pad; |
| 981 | int ret; |
| 982 | |
| 983 | /* Start with the video capture node pad */ |
| 984 | pad = media_entity_remote_source(&vid_cap->vd_pad); |
| 985 | if (pad == NULL) |
| 986 | return -EPIPE; |
| 987 | /* FIMC.{N} subdevice */ |
| 988 | sd = media_entity_to_v4l2_subdev(pad->entity); |
| 989 | |
| 990 | while (1) { |
| 991 | /* Retrieve format at the sink pad */ |
| 992 | pad = &sd->entity.pads[0]; |
| 993 | if (!(pad->flags & MEDIA_PAD_FL_SINK)) |
| 994 | break; |
| 995 | /* Don't call FIMC subdev operation to avoid nested locking */ |
| 996 | if (sd == fimc->vid_cap.subdev) { |
| 997 | struct fimc_frame *ff = &vid_cap->ctx->s_frame; |
| 998 | sink_fmt.format.width = ff->f_width; |
| 999 | sink_fmt.format.height = ff->f_height; |
| 1000 | sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0; |
| 1001 | } else { |
| 1002 | sink_fmt.pad = pad->index; |
| 1003 | sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 1004 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt); |
| 1005 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 1006 | return -EPIPE; |
| 1007 | } |
| 1008 | /* Retrieve format at the source pad */ |
| 1009 | pad = media_entity_remote_source(pad); |
| 1010 | if (pad == NULL || |
| 1011 | media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 1012 | break; |
| 1013 | |
| 1014 | sd = media_entity_to_v4l2_subdev(pad->entity); |
| 1015 | src_fmt.pad = pad->index; |
| 1016 | src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 1017 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt); |
| 1018 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 1019 | return -EPIPE; |
| 1020 | |
| 1021 | if (src_fmt.format.width != sink_fmt.format.width || |
| 1022 | src_fmt.format.height != sink_fmt.format.height || |
| 1023 | src_fmt.format.code != sink_fmt.format.code) |
| 1024 | return -EPIPE; |
| 1025 | } |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1029 | static int fimc_cap_streamon(struct file *file, void *priv, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1030 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1031 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1032 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1033 | struct fimc_pipeline *p = &fimc->pipeline; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1034 | int ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1035 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1036 | if (fimc_capture_active(fimc)) |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1037 | return -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1038 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1039 | media_entity_pipeline_start(&p->sensor->entity, p->pipe); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1040 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1041 | if (fimc->vid_cap.user_subdev_api) { |
| 1042 | ret = fimc_pipeline_validate(fimc); |
| 1043 | if (ret) |
| 1044 | return ret; |
| 1045 | } |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1046 | return vb2_streamon(&fimc->vid_cap.vbq, type); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static int fimc_cap_streamoff(struct file *file, void *priv, |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1050 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1051 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1052 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1053 | struct v4l2_subdev *sd = fimc->pipeline.sensor; |
| 1054 | int ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1055 | |
Sylwester Nawrocki | 4db5e27 | 2011-08-26 14:51:00 -0300 | [diff] [blame] | 1056 | ret = vb2_streamoff(&fimc->vid_cap.vbq, type); |
| 1057 | if (ret == 0) |
| 1058 | media_entity_pipeline_stop(&sd->entity); |
| 1059 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static int fimc_cap_reqbufs(struct file *file, void *priv, |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1063 | struct v4l2_requestbuffers *reqbufs) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1064 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1065 | struct fimc_dev *fimc = video_drvdata(file); |
| 1066 | int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1067 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1068 | if (!ret) |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1069 | fimc->vid_cap.reqbufs_count = reqbufs->count; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | static int fimc_cap_querybuf(struct file *file, void *priv, |
| 1074 | struct v4l2_buffer *buf) |
| 1075 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1076 | struct fimc_dev *fimc = video_drvdata(file); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1077 | |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1078 | return vb2_querybuf(&fimc->vid_cap.vbq, buf); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static int fimc_cap_qbuf(struct file *file, void *priv, |
| 1082 | struct v4l2_buffer *buf) |
| 1083 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1084 | struct fimc_dev *fimc = video_drvdata(file); |
| 1085 | |
| 1086 | return vb2_qbuf(&fimc->vid_cap.vbq, buf); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static int fimc_cap_dqbuf(struct file *file, void *priv, |
| 1090 | struct v4l2_buffer *buf) |
| 1091 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1092 | struct fimc_dev *fimc = video_drvdata(file); |
| 1093 | |
| 1094 | return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1095 | } |
| 1096 | |
Sylwester Nawrocki | 3b4c34a | 2012-01-30 14:56:59 -0300 | [diff] [blame] | 1097 | static int fimc_cap_create_bufs(struct file *file, void *priv, |
| 1098 | struct v4l2_create_buffers *create) |
| 1099 | { |
| 1100 | struct fimc_dev *fimc = video_drvdata(file); |
| 1101 | |
| 1102 | return vb2_create_bufs(&fimc->vid_cap.vbq, create); |
| 1103 | } |
| 1104 | |
| 1105 | static int fimc_cap_prepare_buf(struct file *file, void *priv, |
| 1106 | struct v4l2_buffer *b) |
| 1107 | { |
| 1108 | struct fimc_dev *fimc = video_drvdata(file); |
| 1109 | |
| 1110 | return vb2_prepare_buf(&fimc->vid_cap.vbq, b); |
| 1111 | } |
| 1112 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1113 | static int fimc_cap_g_selection(struct file *file, void *fh, |
| 1114 | struct v4l2_selection *s) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1115 | { |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1116 | struct fimc_dev *fimc = video_drvdata(file); |
| 1117 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1118 | struct fimc_frame *f = &ctx->s_frame; |
| 1119 | |
| 1120 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | switch (s->target) { |
| 1124 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 1125 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1126 | f = &ctx->d_frame; |
| 1127 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 1128 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 1129 | s->r.left = 0; |
| 1130 | s->r.top = 0; |
| 1131 | s->r.width = f->o_width; |
| 1132 | s->r.height = f->o_height; |
| 1133 | return 0; |
| 1134 | |
| 1135 | case V4L2_SEL_TGT_COMPOSE_ACTIVE: |
| 1136 | f = &ctx->d_frame; |
| 1137 | case V4L2_SEL_TGT_CROP_ACTIVE: |
| 1138 | s->r.left = f->offs_h; |
| 1139 | s->r.top = f->offs_v; |
| 1140 | s->r.width = f->width; |
| 1141 | s->r.height = f->height; |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | return -EINVAL; |
| 1146 | } |
| 1147 | |
| 1148 | /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */ |
| 1149 | int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b) |
| 1150 | { |
| 1151 | if (a->left < b->left || a->top < b->top) |
| 1152 | return 0; |
| 1153 | if (a->left + a->width > b->left + b->width) |
| 1154 | return 0; |
| 1155 | if (a->top + a->height > b->top + b->height) |
| 1156 | return 0; |
| 1157 | |
| 1158 | return 1; |
| 1159 | } |
| 1160 | |
| 1161 | static int fimc_cap_s_selection(struct file *file, void *fh, |
| 1162 | struct v4l2_selection *s) |
| 1163 | { |
| 1164 | struct fimc_dev *fimc = video_drvdata(file); |
| 1165 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 1166 | struct v4l2_rect rect = s->r; |
| 1167 | struct fimc_frame *f; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1168 | unsigned long flags; |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1169 | unsigned int pad; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1170 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1171 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 1172 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1173 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1174 | switch (s->target) { |
| 1175 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 1176 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1177 | case V4L2_SEL_TGT_COMPOSE_ACTIVE: |
| 1178 | f = &ctx->d_frame; |
| 1179 | pad = FIMC_SD_PAD_SOURCE; |
| 1180 | break; |
| 1181 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 1182 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 1183 | case V4L2_SEL_TGT_CROP_ACTIVE: |
| 1184 | f = &ctx->s_frame; |
| 1185 | pad = FIMC_SD_PAD_SINK; |
| 1186 | break; |
| 1187 | default: |
| 1188 | return -EINVAL; |
| 1189 | } |
| 1190 | |
| 1191 | fimc_capture_try_crop(ctx, &rect, pad); |
| 1192 | |
| 1193 | if (s->flags & V4L2_SEL_FLAG_LE && |
| 1194 | !enclosed_rectangle(&rect, &s->r)) |
| 1195 | return -ERANGE; |
| 1196 | |
| 1197 | if (s->flags & V4L2_SEL_FLAG_GE && |
| 1198 | !enclosed_rectangle(&s->r, &rect)) |
| 1199 | return -ERANGE; |
| 1200 | |
| 1201 | s->r = rect; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1202 | spin_lock_irqsave(&fimc->slock, flags); |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1203 | set_frame_crop(f, s->r.left, s->r.top, s->r.width, |
| 1204 | s->r.height); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1205 | spin_unlock_irqrestore(&fimc->slock, flags); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1206 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1207 | set_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1208 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1209 | } |
| 1210 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1211 | static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = { |
| 1212 | .vidioc_querycap = fimc_vidioc_querycap_capture, |
| 1213 | |
Sylwester Nawrocki | cf52df8 | 2011-06-13 11:09:40 -0300 | [diff] [blame] | 1214 | .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane, |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1215 | .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane, |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1216 | .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane, |
Sylwester Nawrocki | e578588 | 2011-06-10 15:36:50 -0300 | [diff] [blame] | 1217 | .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1218 | |
| 1219 | .vidioc_reqbufs = fimc_cap_reqbufs, |
| 1220 | .vidioc_querybuf = fimc_cap_querybuf, |
| 1221 | |
| 1222 | .vidioc_qbuf = fimc_cap_qbuf, |
| 1223 | .vidioc_dqbuf = fimc_cap_dqbuf, |
| 1224 | |
Sylwester Nawrocki | 3b4c34a | 2012-01-30 14:56:59 -0300 | [diff] [blame] | 1225 | .vidioc_prepare_buf = fimc_cap_prepare_buf, |
| 1226 | .vidioc_create_bufs = fimc_cap_create_bufs, |
| 1227 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1228 | .vidioc_streamon = fimc_cap_streamon, |
| 1229 | .vidioc_streamoff = fimc_cap_streamoff, |
| 1230 | |
Sylwester Nawrocki | f9331d1 | 2012-02-04 17:03:54 -0300 | [diff] [blame] | 1231 | .vidioc_g_selection = fimc_cap_g_selection, |
| 1232 | .vidioc_s_selection = fimc_cap_s_selection, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1233 | |
| 1234 | .vidioc_enum_input = fimc_cap_enum_input, |
| 1235 | .vidioc_s_input = fimc_cap_s_input, |
| 1236 | .vidioc_g_input = fimc_cap_g_input, |
| 1237 | }; |
| 1238 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1239 | /* Capture subdev media entity operations */ |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1240 | static int fimc_link_setup(struct media_entity *entity, |
| 1241 | const struct media_pad *local, |
| 1242 | const struct media_pad *remote, u32 flags) |
| 1243 | { |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1244 | struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); |
| 1245 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1246 | |
| 1247 | if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 1248 | return -EINVAL; |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1249 | |
| 1250 | if (WARN_ON(fimc == NULL)) |
| 1251 | return 0; |
| 1252 | |
| 1253 | dbg("%s --> %s, flags: 0x%x. input: 0x%x", |
| 1254 | local->entity->name, remote->entity->name, flags, |
| 1255 | fimc->vid_cap.input); |
| 1256 | |
| 1257 | if (flags & MEDIA_LNK_FL_ENABLED) { |
| 1258 | if (fimc->vid_cap.input != 0) |
| 1259 | return -EBUSY; |
| 1260 | fimc->vid_cap.input = sd->grp_id; |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | fimc->vid_cap.input = 0; |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1268 | static const struct media_entity_operations fimc_sd_media_ops = { |
Sylwester Nawrocki | d09a7dc | 2011-08-24 19:28:18 -0300 | [diff] [blame] | 1269 | .link_setup = fimc_link_setup, |
| 1270 | }; |
| 1271 | |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1272 | /** |
| 1273 | * fimc_sensor_notify - v4l2_device notification from a sensor subdev |
| 1274 | * @sd: pointer to a subdev generating the notification |
| 1275 | * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY |
| 1276 | * @arg: pointer to an u32 type integer that stores the frame payload value |
| 1277 | * |
| 1278 | * The End Of Frame notification sent by sensor subdev in its still capture |
| 1279 | * mode. If there is only a single VSYNC generated by the sensor at the |
| 1280 | * beginning of a frame transmission, FIMC does not issue the LastIrq |
| 1281 | * (end of frame) interrupt. And this notification is used to complete the |
| 1282 | * frame capture and returning a buffer to user-space. Subdev drivers should |
| 1283 | * call this notification from their last 'End of frame capture' interrupt. |
| 1284 | */ |
| 1285 | void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification, |
| 1286 | void *arg) |
| 1287 | { |
| 1288 | struct fimc_sensor_info *sensor; |
| 1289 | struct fimc_vid_buffer *buf; |
| 1290 | struct fimc_md *fmd; |
| 1291 | struct fimc_dev *fimc; |
| 1292 | unsigned long flags; |
| 1293 | |
| 1294 | if (sd == NULL) |
| 1295 | return; |
| 1296 | |
| 1297 | sensor = v4l2_get_subdev_hostdata(sd); |
| 1298 | fmd = entity_to_fimc_mdev(&sd->entity); |
| 1299 | |
| 1300 | spin_lock_irqsave(&fmd->slock, flags); |
| 1301 | fimc = sensor ? sensor->host : NULL; |
| 1302 | |
| 1303 | if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY && |
| 1304 | test_bit(ST_CAPT_PEND, &fimc->state)) { |
| 1305 | unsigned long irq_flags; |
| 1306 | spin_lock_irqsave(&fimc->slock, irq_flags); |
| 1307 | if (!list_empty(&fimc->vid_cap.active_buf_q)) { |
| 1308 | buf = list_entry(fimc->vid_cap.active_buf_q.next, |
| 1309 | struct fimc_vid_buffer, list); |
| 1310 | vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg)); |
| 1311 | } |
Sylwester Nawrocki | 97d9742 | 2012-05-08 15:51:24 -0300 | [diff] [blame^] | 1312 | fimc_capture_irq_handler(fimc, 1); |
Sylwester Nawrocki | e1d72f4 | 2011-06-10 15:36:58 -0300 | [diff] [blame] | 1313 | fimc_deactivate_capture(fimc); |
| 1314 | spin_unlock_irqrestore(&fimc->slock, irq_flags); |
| 1315 | } |
| 1316 | spin_unlock_irqrestore(&fmd->slock, flags); |
| 1317 | } |
| 1318 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1319 | static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd, |
| 1320 | struct v4l2_subdev_fh *fh, |
| 1321 | struct v4l2_subdev_mbus_code_enum *code) |
| 1322 | { |
| 1323 | struct fimc_fmt *fmt; |
| 1324 | |
| 1325 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index); |
| 1326 | if (!fmt) |
| 1327 | return -EINVAL; |
| 1328 | code->code = fmt->mbus_code; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | static int fimc_subdev_get_fmt(struct v4l2_subdev *sd, |
| 1333 | struct v4l2_subdev_fh *fh, |
| 1334 | struct v4l2_subdev_format *fmt) |
| 1335 | { |
| 1336 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1337 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 1338 | struct v4l2_mbus_framefmt *mf; |
| 1339 | struct fimc_frame *ff; |
| 1340 | |
| 1341 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1342 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1343 | fmt->format = *mf; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | mf = &fmt->format; |
| 1347 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1348 | ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame; |
| 1349 | |
| 1350 | mutex_lock(&fimc->lock); |
| 1351 | /* The pixel code is same on both input and output pad */ |
| 1352 | if (!WARN_ON(ctx->s_frame.fmt == NULL)) |
| 1353 | mf->code = ctx->s_frame.fmt->mbus_code; |
| 1354 | mf->width = ff->f_width; |
| 1355 | mf->height = ff->f_height; |
| 1356 | mutex_unlock(&fimc->lock); |
| 1357 | |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | static int fimc_subdev_set_fmt(struct v4l2_subdev *sd, |
| 1362 | struct v4l2_subdev_fh *fh, |
| 1363 | struct v4l2_subdev_format *fmt) |
| 1364 | { |
| 1365 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1366 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
| 1367 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 1368 | struct fimc_frame *ff; |
| 1369 | struct fimc_fmt *ffmt; |
| 1370 | |
| 1371 | dbg("pad%d: code: 0x%x, %dx%d", |
| 1372 | fmt->pad, mf->code, mf->width, mf->height); |
| 1373 | |
| 1374 | if (fmt->pad == FIMC_SD_PAD_SOURCE && |
| 1375 | vb2_is_busy(&fimc->vid_cap.vbq)) |
| 1376 | return -EBUSY; |
| 1377 | |
| 1378 | mutex_lock(&fimc->lock); |
| 1379 | ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height, |
| 1380 | &mf->code, NULL, fmt->pad); |
| 1381 | mutex_unlock(&fimc->lock); |
| 1382 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1383 | |
| 1384 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1385 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1386 | *mf = fmt->format; |
| 1387 | return 0; |
| 1388 | } |
Sylwester Nawrocki | dafb9c7 | 2011-12-01 14:02:24 -0300 | [diff] [blame] | 1389 | /* Update RGB Alpha control state and value range */ |
| 1390 | fimc_alpha_ctrl_update(ctx); |
| 1391 | |
Sylwester Nawrocki | ee7160e | 2011-08-26 14:57:06 -0300 | [diff] [blame] | 1392 | fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color)); |
| 1393 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1394 | ff = fmt->pad == FIMC_SD_PAD_SINK ? |
| 1395 | &ctx->s_frame : &ctx->d_frame; |
| 1396 | |
| 1397 | mutex_lock(&fimc->lock); |
| 1398 | set_frame_bounds(ff, mf->width, mf->height); |
Sylwester Nawrocki | 393a23f | 2011-12-08 06:56:22 -0300 | [diff] [blame] | 1399 | fimc->vid_cap.mf = *mf; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1400 | ff->fmt = ffmt; |
| 1401 | |
| 1402 | /* Reset the crop rectangle if required. */ |
| 1403 | if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP))) |
| 1404 | set_frame_crop(ff, 0, 0, mf->width, mf->height); |
| 1405 | |
| 1406 | if (fmt->pad == FIMC_SD_PAD_SINK) |
| 1407 | ctx->state &= ~FIMC_DST_CROP; |
| 1408 | mutex_unlock(&fimc->lock); |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static int fimc_subdev_get_crop(struct v4l2_subdev *sd, |
| 1413 | struct v4l2_subdev_fh *fh, |
| 1414 | struct v4l2_subdev_crop *crop) |
| 1415 | { |
| 1416 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1417 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 1418 | struct v4l2_rect *r = &crop->rect; |
| 1419 | struct fimc_frame *ff; |
| 1420 | |
| 1421 | if (crop->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1422 | crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad); |
| 1423 | return 0; |
| 1424 | } |
| 1425 | ff = crop->pad == FIMC_SD_PAD_SINK ? |
| 1426 | &ctx->s_frame : &ctx->d_frame; |
| 1427 | |
| 1428 | mutex_lock(&fimc->lock); |
| 1429 | r->left = ff->offs_h; |
| 1430 | r->top = ff->offs_v; |
| 1431 | r->width = ff->width; |
| 1432 | r->height = ff->height; |
| 1433 | mutex_unlock(&fimc->lock); |
| 1434 | |
| 1435 | dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d", |
| 1436 | ff, crop->pad, r->left, r->top, r->width, r->height, |
| 1437 | ff->f_width, ff->f_height); |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static int fimc_subdev_set_crop(struct v4l2_subdev *sd, |
| 1443 | struct v4l2_subdev_fh *fh, |
| 1444 | struct v4l2_subdev_crop *crop) |
| 1445 | { |
| 1446 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); |
| 1447 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
| 1448 | struct v4l2_rect *r = &crop->rect; |
| 1449 | struct fimc_frame *ff; |
| 1450 | unsigned long flags; |
| 1451 | |
| 1452 | dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height); |
| 1453 | |
| 1454 | ff = crop->pad == FIMC_SD_PAD_SOURCE ? |
| 1455 | &ctx->d_frame : &ctx->s_frame; |
| 1456 | |
| 1457 | mutex_lock(&fimc->lock); |
| 1458 | fimc_capture_try_crop(ctx, r, crop->pad); |
| 1459 | |
| 1460 | if (crop->which == V4L2_SUBDEV_FORMAT_TRY) { |
Sylwester Nawrocki | 5be4fe6 | 2012-04-21 18:46:30 -0300 | [diff] [blame] | 1461 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1462 | *v4l2_subdev_get_try_crop(fh, crop->pad) = *r; |
| 1463 | return 0; |
| 1464 | } |
| 1465 | spin_lock_irqsave(&fimc->slock, flags); |
| 1466 | set_frame_crop(ff, r->left, r->top, r->width, r->height); |
| 1467 | if (crop->pad == FIMC_SD_PAD_SOURCE) |
| 1468 | ctx->state |= FIMC_DST_CROP; |
| 1469 | |
| 1470 | set_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
| 1471 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 1472 | |
| 1473 | dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top, |
| 1474 | r->width, r->height); |
| 1475 | |
| 1476 | mutex_unlock(&fimc->lock); |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = { |
| 1481 | .enum_mbus_code = fimc_subdev_enum_mbus_code, |
| 1482 | .get_fmt = fimc_subdev_get_fmt, |
| 1483 | .set_fmt = fimc_subdev_set_fmt, |
| 1484 | .get_crop = fimc_subdev_get_crop, |
| 1485 | .set_crop = fimc_subdev_set_crop, |
| 1486 | }; |
| 1487 | |
| 1488 | static struct v4l2_subdev_ops fimc_subdev_ops = { |
| 1489 | .pad = &fimc_subdev_pad_ops, |
| 1490 | }; |
| 1491 | |
| 1492 | static int fimc_create_capture_subdev(struct fimc_dev *fimc, |
| 1493 | struct v4l2_device *v4l2_dev) |
| 1494 | { |
| 1495 | struct v4l2_subdev *sd; |
| 1496 | int ret; |
| 1497 | |
| 1498 | sd = kzalloc(sizeof(*sd), GFP_KERNEL); |
| 1499 | if (!sd) |
| 1500 | return -ENOMEM; |
| 1501 | |
| 1502 | v4l2_subdev_init(sd, &fimc_subdev_ops); |
| 1503 | sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 1504 | snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id); |
| 1505 | |
| 1506 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK; |
| 1507 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE; |
| 1508 | ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM, |
| 1509 | fimc->vid_cap.sd_pads, 0); |
| 1510 | if (ret) |
| 1511 | goto me_err; |
| 1512 | ret = v4l2_device_register_subdev(v4l2_dev, sd); |
| 1513 | if (ret) |
| 1514 | goto sd_err; |
| 1515 | |
| 1516 | fimc->vid_cap.subdev = sd; |
| 1517 | v4l2_set_subdevdata(sd, fimc); |
| 1518 | sd->entity.ops = &fimc_sd_media_ops; |
| 1519 | return 0; |
| 1520 | sd_err: |
| 1521 | media_entity_cleanup(&sd->entity); |
| 1522 | me_err: |
| 1523 | kfree(sd); |
| 1524 | return ret; |
| 1525 | } |
| 1526 | |
| 1527 | static void fimc_destroy_capture_subdev(struct fimc_dev *fimc) |
| 1528 | { |
| 1529 | struct v4l2_subdev *sd = fimc->vid_cap.subdev; |
| 1530 | |
| 1531 | if (!sd) |
| 1532 | return; |
| 1533 | media_entity_cleanup(&sd->entity); |
| 1534 | v4l2_device_unregister_subdev(sd); |
| 1535 | kfree(sd); |
Sylwester Nawrocki | 64c570f | 2011-09-11 10:36:13 -0300 | [diff] [blame] | 1536 | fimc->vid_cap.subdev = NULL; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /* Set default format at the sensor and host interface */ |
| 1540 | static int fimc_capture_set_default_format(struct fimc_dev *fimc) |
| 1541 | { |
| 1542 | struct v4l2_format fmt = { |
| 1543 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, |
| 1544 | .fmt.pix_mp = { |
| 1545 | .width = 640, |
| 1546 | .height = 480, |
| 1547 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 1548 | .field = V4L2_FIELD_NONE, |
| 1549 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 1550 | }, |
| 1551 | }; |
| 1552 | |
| 1553 | return fimc_capture_set_format(fimc, &fmt); |
| 1554 | } |
| 1555 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1556 | /* fimc->lock must be already initialized */ |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1557 | int fimc_register_capture_device(struct fimc_dev *fimc, |
| 1558 | struct v4l2_device *v4l2_dev) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1559 | { |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1560 | struct video_device *vfd; |
| 1561 | struct fimc_vid_cap *vid_cap; |
| 1562 | struct fimc_ctx *ctx; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1563 | struct vb2_queue *q; |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1564 | int ret = -ENOMEM; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1565 | |
| 1566 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
| 1567 | if (!ctx) |
| 1568 | return -ENOMEM; |
| 1569 | |
| 1570 | ctx->fimc_dev = fimc; |
| 1571 | ctx->in_path = FIMC_CAMERA; |
| 1572 | ctx->out_path = FIMC_DMA; |
| 1573 | ctx->state = FIMC_CTX_CAP; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1574 | ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0); |
| 1575 | ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1576 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1577 | vfd = video_device_alloc(); |
| 1578 | if (!vfd) { |
| 1579 | v4l2_err(v4l2_dev, "Failed to allocate video device\n"); |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1580 | goto err_vd_alloc; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1581 | } |
| 1582 | |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1583 | snprintf(vfd->name, sizeof(vfd->name), "%s.capture", |
| 1584 | dev_name(&fimc->pdev->dev)); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1585 | |
| 1586 | vfd->fops = &fimc_capture_fops; |
| 1587 | vfd->ioctl_ops = &fimc_capture_ioctl_ops; |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1588 | vfd->v4l2_dev = v4l2_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1589 | vfd->minor = -1; |
| 1590 | vfd->release = video_device_release; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 1591 | vfd->lock = &fimc->lock; |
Hans Verkuil | 5126f25 | 2012-05-10 04:57:22 -0300 | [diff] [blame] | 1592 | /* Locking in file operations other than ioctl should be done |
| 1593 | by the driver, not the V4L2 core. |
| 1594 | This driver needs auditing so that this flag can be removed. */ |
| 1595 | set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1596 | video_set_drvdata(vfd, fimc); |
| 1597 | |
| 1598 | vid_cap = &fimc->vid_cap; |
| 1599 | vid_cap->vfd = vfd; |
| 1600 | vid_cap->active_buf_cnt = 0; |
| 1601 | vid_cap->reqbufs_count = 0; |
| 1602 | vid_cap->refcnt = 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1603 | |
| 1604 | INIT_LIST_HEAD(&vid_cap->pending_buf_q); |
| 1605 | INIT_LIST_HEAD(&vid_cap->active_buf_q); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1606 | vid_cap->ctx = ctx; |
| 1607 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1608 | q = &fimc->vid_cap.vbq; |
| 1609 | memset(q, 0, sizeof(*q)); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 1610 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 1611 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 1612 | q->drv_priv = fimc->vid_cap.ctx; |
| 1613 | q->ops = &fimc_capture_qops; |
| 1614 | q->mem_ops = &vb2_dma_contig_memops; |
| 1615 | q->buf_struct_size = sizeof(struct fimc_vid_buffer); |
| 1616 | |
| 1617 | vb2_queue_init(q); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1618 | |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1619 | fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK; |
| 1620 | ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0); |
| 1621 | if (ret) |
| 1622 | goto err_ent; |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1623 | ret = fimc_create_capture_subdev(fimc, v4l2_dev); |
| 1624 | if (ret) |
| 1625 | goto err_sd_reg; |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1626 | |
Sylwester Nawrocki | 131b6c6 | 2011-08-24 19:25:10 -0300 | [diff] [blame] | 1627 | vfd->ctrl_handler = &ctx->ctrl_handler; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1628 | return 0; |
| 1629 | |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1630 | err_sd_reg: |
| 1631 | media_entity_cleanup(&vfd->entity); |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1632 | err_ent: |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1633 | video_device_release(vfd); |
Sylwester Nawrocki | 30c9939 | 2011-06-10 15:36:48 -0300 | [diff] [blame] | 1634 | err_vd_alloc: |
Sylwester Nawrocki | cfd7731 | 2011-06-02 06:18:34 -0300 | [diff] [blame] | 1635 | kfree(ctx); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1636 | return ret; |
| 1637 | } |
| 1638 | |
| 1639 | void fimc_unregister_capture_device(struct fimc_dev *fimc) |
| 1640 | { |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1641 | struct video_device *vfd = fimc->vid_cap.vfd; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1642 | |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1643 | if (vfd) { |
| 1644 | media_entity_cleanup(&vfd->entity); |
Sylwester Nawrocki | 96a8574 | 2011-08-26 15:40:36 -0300 | [diff] [blame] | 1645 | /* Can also be called if video device was |
| 1646 | not registered */ |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1647 | video_unregister_device(vfd); |
| 1648 | } |
Sylwester Nawrocki | 237e026 | 2011-08-24 20:35:30 -0300 | [diff] [blame] | 1649 | fimc_destroy_capture_subdev(fimc); |
Sylwester Nawrocki | 574e171 | 2011-07-26 18:08:21 -0300 | [diff] [blame] | 1650 | kfree(fimc->vid_cap.ctx); |
Sylwester Nawrocki | 96a8574 | 2011-08-26 15:40:36 -0300 | [diff] [blame] | 1651 | fimc->vid_cap.ctx = NULL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1652 | } |