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