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