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