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