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