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