1238c84f7SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0-only
2238c84f7SMauro Carvalho Chehab /*
3238c84f7SMauro Carvalho Chehab * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4238c84f7SMauro Carvalho Chehab *
5238c84f7SMauro Carvalho Chehab * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
6238c84f7SMauro Carvalho Chehab * Sylwester Nawrocki <s.nawrocki@samsung.com>
7238c84f7SMauro Carvalho Chehab */
8238c84f7SMauro Carvalho Chehab
9238c84f7SMauro Carvalho Chehab #include <linux/module.h>
10238c84f7SMauro Carvalho Chehab #include <linux/kernel.h>
11238c84f7SMauro Carvalho Chehab #include <linux/types.h>
12238c84f7SMauro Carvalho Chehab #include <linux/errno.h>
13238c84f7SMauro Carvalho Chehab #include <linux/bug.h>
14238c84f7SMauro Carvalho Chehab #include <linux/interrupt.h>
15238c84f7SMauro Carvalho Chehab #include <linux/device.h>
16238c84f7SMauro Carvalho Chehab #include <linux/pm_runtime.h>
17238c84f7SMauro Carvalho Chehab #include <linux/list.h>
18238c84f7SMauro Carvalho Chehab #include <linux/slab.h>
19238c84f7SMauro Carvalho Chehab
20238c84f7SMauro Carvalho Chehab #include <linux/videodev2.h>
21238c84f7SMauro Carvalho Chehab #include <media/v4l2-device.h>
22238c84f7SMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
23238c84f7SMauro Carvalho Chehab #include <media/v4l2-mem2mem.h>
24238c84f7SMauro Carvalho Chehab #include <media/v4l2-rect.h>
25238c84f7SMauro Carvalho Chehab #include <media/videobuf2-v4l2.h>
26238c84f7SMauro Carvalho Chehab #include <media/videobuf2-dma-contig.h>
27238c84f7SMauro Carvalho Chehab
28238c84f7SMauro Carvalho Chehab #include "common.h"
29238c84f7SMauro Carvalho Chehab #include "fimc-core.h"
30238c84f7SMauro Carvalho Chehab #include "fimc-reg.h"
31238c84f7SMauro Carvalho Chehab #include "media-dev.h"
32238c84f7SMauro Carvalho Chehab
fimc_capture_hw_init(struct fimc_dev * fimc)33238c84f7SMauro Carvalho Chehab static int fimc_capture_hw_init(struct fimc_dev *fimc)
34238c84f7SMauro Carvalho Chehab {
35238c84f7SMauro Carvalho Chehab struct fimc_source_info *si = &fimc->vid_cap.source_config;
36238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = fimc->vid_cap.ctx;
37238c84f7SMauro Carvalho Chehab int ret;
38238c84f7SMauro Carvalho Chehab unsigned long flags;
39238c84f7SMauro Carvalho Chehab
40238c84f7SMauro Carvalho Chehab if (ctx == NULL || ctx->s_frame.fmt == NULL)
41238c84f7SMauro Carvalho Chehab return -EINVAL;
42238c84f7SMauro Carvalho Chehab
43238c84f7SMauro Carvalho Chehab if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
44238c84f7SMauro Carvalho Chehab ret = fimc_hw_camblk_cfg_writeback(fimc);
45238c84f7SMauro Carvalho Chehab if (ret < 0)
46238c84f7SMauro Carvalho Chehab return ret;
47238c84f7SMauro Carvalho Chehab }
48238c84f7SMauro Carvalho Chehab
49238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, flags);
50238c84f7SMauro Carvalho Chehab fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51238c84f7SMauro Carvalho Chehab fimc_set_yuv_order(ctx);
52238c84f7SMauro Carvalho Chehab
53238c84f7SMauro Carvalho Chehab fimc_hw_set_camera_polarity(fimc, si);
54238c84f7SMauro Carvalho Chehab fimc_hw_set_camera_type(fimc, si);
55238c84f7SMauro Carvalho Chehab fimc_hw_set_camera_source(fimc, si);
56238c84f7SMauro Carvalho Chehab fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57238c84f7SMauro Carvalho Chehab
58238c84f7SMauro Carvalho Chehab ret = fimc_set_scaler_info(ctx);
59238c84f7SMauro Carvalho Chehab if (!ret) {
60238c84f7SMauro Carvalho Chehab fimc_hw_set_input_path(ctx);
61238c84f7SMauro Carvalho Chehab fimc_hw_set_prescaler(ctx);
62238c84f7SMauro Carvalho Chehab fimc_hw_set_mainscaler(ctx);
63238c84f7SMauro Carvalho Chehab fimc_hw_set_target_format(ctx);
64238c84f7SMauro Carvalho Chehab fimc_hw_set_rotation(ctx);
65238c84f7SMauro Carvalho Chehab fimc_hw_set_effect(ctx);
66238c84f7SMauro Carvalho Chehab fimc_hw_set_output_path(ctx);
67238c84f7SMauro Carvalho Chehab fimc_hw_set_out_dma(ctx);
68238c84f7SMauro Carvalho Chehab if (fimc->drv_data->alpha_color)
69238c84f7SMauro Carvalho Chehab fimc_hw_set_rgb_alpha(ctx);
70238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
71238c84f7SMauro Carvalho Chehab }
72238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
73238c84f7SMauro Carvalho Chehab return ret;
74238c84f7SMauro Carvalho Chehab }
75238c84f7SMauro Carvalho Chehab
76238c84f7SMauro Carvalho Chehab /*
77238c84f7SMauro Carvalho Chehab * Reinitialize the driver so it is ready to start the streaming again.
78238c84f7SMauro Carvalho Chehab * Set fimc->state to indicate stream off and the hardware shut down state.
79238c84f7SMauro Carvalho Chehab * If not suspending (@suspend is false), return any buffers to videobuf2.
80238c84f7SMauro Carvalho Chehab * Otherwise put any owned buffers onto the pending buffers queue, so they
81238c84f7SMauro Carvalho Chehab * can be re-spun when the device is being resumed. Also perform FIMC
82238c84f7SMauro Carvalho Chehab * software reset and disable streaming on the whole pipeline if required.
83238c84f7SMauro Carvalho Chehab */
fimc_capture_state_cleanup(struct fimc_dev * fimc,bool suspend)84238c84f7SMauro Carvalho Chehab static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
85238c84f7SMauro Carvalho Chehab {
86238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *cap = &fimc->vid_cap;
87238c84f7SMauro Carvalho Chehab struct fimc_vid_buffer *buf;
88238c84f7SMauro Carvalho Chehab unsigned long flags;
89238c84f7SMauro Carvalho Chehab bool streaming;
90238c84f7SMauro Carvalho Chehab
91238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, flags);
92238c84f7SMauro Carvalho Chehab streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
93238c84f7SMauro Carvalho Chehab
94238c84f7SMauro Carvalho Chehab fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
95238c84f7SMauro Carvalho Chehab 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
96238c84f7SMauro Carvalho Chehab if (suspend)
97238c84f7SMauro Carvalho Chehab fimc->state |= (1 << ST_CAPT_SUSPENDED);
98238c84f7SMauro Carvalho Chehab else
99238c84f7SMauro Carvalho Chehab fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
100238c84f7SMauro Carvalho Chehab
101238c84f7SMauro Carvalho Chehab /* Release unused buffers */
102238c84f7SMauro Carvalho Chehab while (!suspend && !list_empty(&cap->pending_buf_q)) {
103238c84f7SMauro Carvalho Chehab buf = fimc_pending_queue_pop(cap);
104238c84f7SMauro Carvalho Chehab vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
105238c84f7SMauro Carvalho Chehab }
106238c84f7SMauro Carvalho Chehab /* If suspending put unused buffers onto pending queue */
107238c84f7SMauro Carvalho Chehab while (!list_empty(&cap->active_buf_q)) {
108238c84f7SMauro Carvalho Chehab buf = fimc_active_queue_pop(cap);
109238c84f7SMauro Carvalho Chehab if (suspend)
110238c84f7SMauro Carvalho Chehab fimc_pending_queue_add(cap, buf);
111238c84f7SMauro Carvalho Chehab else
112238c84f7SMauro Carvalho Chehab vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
113238c84f7SMauro Carvalho Chehab }
114238c84f7SMauro Carvalho Chehab
115238c84f7SMauro Carvalho Chehab fimc_hw_reset(fimc);
116238c84f7SMauro Carvalho Chehab cap->buf_index = 0;
117238c84f7SMauro Carvalho Chehab
118238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
119238c84f7SMauro Carvalho Chehab
120238c84f7SMauro Carvalho Chehab if (streaming)
121238c84f7SMauro Carvalho Chehab return fimc_pipeline_call(&cap->ve, set_stream, 0);
122238c84f7SMauro Carvalho Chehab else
123238c84f7SMauro Carvalho Chehab return 0;
124238c84f7SMauro Carvalho Chehab }
125238c84f7SMauro Carvalho Chehab
fimc_stop_capture(struct fimc_dev * fimc,bool suspend)126238c84f7SMauro Carvalho Chehab static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
127238c84f7SMauro Carvalho Chehab {
128238c84f7SMauro Carvalho Chehab unsigned long flags;
129238c84f7SMauro Carvalho Chehab
130238c84f7SMauro Carvalho Chehab if (!fimc_capture_active(fimc))
131238c84f7SMauro Carvalho Chehab return 0;
132238c84f7SMauro Carvalho Chehab
133238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, flags);
134238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_SHUT, &fimc->state);
135238c84f7SMauro Carvalho Chehab fimc_deactivate_capture(fimc);
136238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
137238c84f7SMauro Carvalho Chehab
138238c84f7SMauro Carvalho Chehab wait_event_timeout(fimc->irq_queue,
139238c84f7SMauro Carvalho Chehab !test_bit(ST_CAPT_SHUT, &fimc->state),
140238c84f7SMauro Carvalho Chehab (2*HZ/10)); /* 200 ms */
141238c84f7SMauro Carvalho Chehab
142238c84f7SMauro Carvalho Chehab return fimc_capture_state_cleanup(fimc, suspend);
143238c84f7SMauro Carvalho Chehab }
144238c84f7SMauro Carvalho Chehab
145238c84f7SMauro Carvalho Chehab /**
146238c84f7SMauro Carvalho Chehab * fimc_capture_config_update - apply the camera interface configuration
147238c84f7SMauro Carvalho Chehab * @ctx: FIMC capture context
148238c84f7SMauro Carvalho Chehab *
149238c84f7SMauro Carvalho Chehab * To be called from within the interrupt handler with fimc.slock
150238c84f7SMauro Carvalho Chehab * spinlock held. It updates the camera pixel crop, rotation and
151238c84f7SMauro Carvalho Chehab * image flip in H/W.
152238c84f7SMauro Carvalho Chehab */
fimc_capture_config_update(struct fimc_ctx * ctx)153238c84f7SMauro Carvalho Chehab static int fimc_capture_config_update(struct fimc_ctx *ctx)
154238c84f7SMauro Carvalho Chehab {
155238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
156238c84f7SMauro Carvalho Chehab int ret;
157238c84f7SMauro Carvalho Chehab
158238c84f7SMauro Carvalho Chehab fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
159238c84f7SMauro Carvalho Chehab
160238c84f7SMauro Carvalho Chehab ret = fimc_set_scaler_info(ctx);
161238c84f7SMauro Carvalho Chehab if (ret)
162238c84f7SMauro Carvalho Chehab return ret;
163238c84f7SMauro Carvalho Chehab
164238c84f7SMauro Carvalho Chehab fimc_hw_set_prescaler(ctx);
165238c84f7SMauro Carvalho Chehab fimc_hw_set_mainscaler(ctx);
166238c84f7SMauro Carvalho Chehab fimc_hw_set_target_format(ctx);
167238c84f7SMauro Carvalho Chehab fimc_hw_set_rotation(ctx);
168238c84f7SMauro Carvalho Chehab fimc_hw_set_effect(ctx);
169238c84f7SMauro Carvalho Chehab fimc_prepare_dma_offset(ctx, &ctx->d_frame);
170238c84f7SMauro Carvalho Chehab fimc_hw_set_out_dma(ctx);
171238c84f7SMauro Carvalho Chehab if (fimc->drv_data->alpha_color)
172238c84f7SMauro Carvalho Chehab fimc_hw_set_rgb_alpha(ctx);
173238c84f7SMauro Carvalho Chehab
174238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
175238c84f7SMauro Carvalho Chehab return ret;
176238c84f7SMauro Carvalho Chehab }
177238c84f7SMauro Carvalho Chehab
fimc_capture_irq_handler(struct fimc_dev * fimc,int deq_buf)178238c84f7SMauro Carvalho Chehab void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
179238c84f7SMauro Carvalho Chehab {
180238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *cap = &fimc->vid_cap;
181238c84f7SMauro Carvalho Chehab struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
182238c84f7SMauro Carvalho Chehab struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
183238c84f7SMauro Carvalho Chehab struct fimc_frame *f = &cap->ctx->d_frame;
184238c84f7SMauro Carvalho Chehab struct fimc_vid_buffer *v_buf;
185238c84f7SMauro Carvalho Chehab
186238c84f7SMauro Carvalho Chehab if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
187238c84f7SMauro Carvalho Chehab wake_up(&fimc->irq_queue);
188238c84f7SMauro Carvalho Chehab goto done;
189238c84f7SMauro Carvalho Chehab }
190238c84f7SMauro Carvalho Chehab
191238c84f7SMauro Carvalho Chehab if (!list_empty(&cap->active_buf_q) &&
192238c84f7SMauro Carvalho Chehab test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
193238c84f7SMauro Carvalho Chehab v_buf = fimc_active_queue_pop(cap);
194238c84f7SMauro Carvalho Chehab
195238c84f7SMauro Carvalho Chehab v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
196238c84f7SMauro Carvalho Chehab v_buf->vb.sequence = cap->frame_count++;
197238c84f7SMauro Carvalho Chehab
198238c84f7SMauro Carvalho Chehab vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
199238c84f7SMauro Carvalho Chehab }
200238c84f7SMauro Carvalho Chehab
201238c84f7SMauro Carvalho Chehab if (!list_empty(&cap->pending_buf_q)) {
202238c84f7SMauro Carvalho Chehab
203238c84f7SMauro Carvalho Chehab v_buf = fimc_pending_queue_pop(cap);
204238c84f7SMauro Carvalho Chehab fimc_hw_set_output_addr(fimc, &v_buf->addr, cap->buf_index);
205238c84f7SMauro Carvalho Chehab v_buf->index = cap->buf_index;
206238c84f7SMauro Carvalho Chehab
207238c84f7SMauro Carvalho Chehab /* Move the buffer to the capture active queue */
208238c84f7SMauro Carvalho Chehab fimc_active_queue_add(cap, v_buf);
209238c84f7SMauro Carvalho Chehab
210238c84f7SMauro Carvalho Chehab dbg("next frame: %d, done frame: %d",
211238c84f7SMauro Carvalho Chehab fimc_hw_get_frame_index(fimc), v_buf->index);
212238c84f7SMauro Carvalho Chehab
213238c84f7SMauro Carvalho Chehab if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
214238c84f7SMauro Carvalho Chehab cap->buf_index = 0;
215238c84f7SMauro Carvalho Chehab }
216238c84f7SMauro Carvalho Chehab /*
217238c84f7SMauro Carvalho Chehab * Set up a buffer at MIPI-CSIS if current image format
218238c84f7SMauro Carvalho Chehab * requires the frame embedded data capture.
219238c84f7SMauro Carvalho Chehab */
220238c84f7SMauro Carvalho Chehab if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
221238c84f7SMauro Carvalho Chehab unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
222238c84f7SMauro Carvalho Chehab unsigned int size = f->payload[plane];
223238c84f7SMauro Carvalho Chehab s32 index = fimc_hw_get_frame_index(fimc);
224238c84f7SMauro Carvalho Chehab void *vaddr;
225238c84f7SMauro Carvalho Chehab
226238c84f7SMauro Carvalho Chehab list_for_each_entry(v_buf, &cap->active_buf_q, list) {
227238c84f7SMauro Carvalho Chehab if (v_buf->index != index)
228238c84f7SMauro Carvalho Chehab continue;
229238c84f7SMauro Carvalho Chehab vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
230238c84f7SMauro Carvalho Chehab v4l2_subdev_call(csis, video, s_rx_buffer,
231238c84f7SMauro Carvalho Chehab vaddr, &size);
232238c84f7SMauro Carvalho Chehab break;
233238c84f7SMauro Carvalho Chehab }
234238c84f7SMauro Carvalho Chehab }
235238c84f7SMauro Carvalho Chehab
236238c84f7SMauro Carvalho Chehab if (cap->active_buf_cnt == 0) {
237238c84f7SMauro Carvalho Chehab if (deq_buf)
238238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_RUN, &fimc->state);
239238c84f7SMauro Carvalho Chehab
240238c84f7SMauro Carvalho Chehab if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
241238c84f7SMauro Carvalho Chehab cap->buf_index = 0;
242238c84f7SMauro Carvalho Chehab } else {
243238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_RUN, &fimc->state);
244238c84f7SMauro Carvalho Chehab }
245238c84f7SMauro Carvalho Chehab
246238c84f7SMauro Carvalho Chehab if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
247238c84f7SMauro Carvalho Chehab fimc_capture_config_update(cap->ctx);
248238c84f7SMauro Carvalho Chehab done:
249238c84f7SMauro Carvalho Chehab if (cap->active_buf_cnt == 1) {
250238c84f7SMauro Carvalho Chehab fimc_deactivate_capture(fimc);
251238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_STREAM, &fimc->state);
252238c84f7SMauro Carvalho Chehab }
253238c84f7SMauro Carvalho Chehab
254238c84f7SMauro Carvalho Chehab dbg("frame: %d, active_buf_cnt: %d",
255238c84f7SMauro Carvalho Chehab fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
256238c84f7SMauro Carvalho Chehab }
257238c84f7SMauro Carvalho Chehab
258238c84f7SMauro Carvalho Chehab
start_streaming(struct vb2_queue * q,unsigned int count)259238c84f7SMauro Carvalho Chehab static int start_streaming(struct vb2_queue *q, unsigned int count)
260238c84f7SMauro Carvalho Chehab {
261238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = q->drv_priv;
262238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
263238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
264238c84f7SMauro Carvalho Chehab int min_bufs;
265238c84f7SMauro Carvalho Chehab int ret;
266238c84f7SMauro Carvalho Chehab
267238c84f7SMauro Carvalho Chehab vid_cap->frame_count = 0;
268238c84f7SMauro Carvalho Chehab
269238c84f7SMauro Carvalho Chehab ret = fimc_capture_hw_init(fimc);
270238c84f7SMauro Carvalho Chehab if (ret) {
271238c84f7SMauro Carvalho Chehab fimc_capture_state_cleanup(fimc, false);
272238c84f7SMauro Carvalho Chehab return ret;
273238c84f7SMauro Carvalho Chehab }
274238c84f7SMauro Carvalho Chehab
275238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_PEND, &fimc->state);
276238c84f7SMauro Carvalho Chehab
277238c84f7SMauro Carvalho Chehab min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
278238c84f7SMauro Carvalho Chehab
279238c84f7SMauro Carvalho Chehab if (vid_cap->active_buf_cnt >= min_bufs &&
280238c84f7SMauro Carvalho Chehab !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
281238c84f7SMauro Carvalho Chehab fimc_activate_capture(ctx);
282238c84f7SMauro Carvalho Chehab
283238c84f7SMauro Carvalho Chehab if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
284238c84f7SMauro Carvalho Chehab return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
285238c84f7SMauro Carvalho Chehab }
286238c84f7SMauro Carvalho Chehab
287238c84f7SMauro Carvalho Chehab return 0;
288238c84f7SMauro Carvalho Chehab }
289238c84f7SMauro Carvalho Chehab
stop_streaming(struct vb2_queue * q)290238c84f7SMauro Carvalho Chehab static void stop_streaming(struct vb2_queue *q)
291238c84f7SMauro Carvalho Chehab {
292238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = q->drv_priv;
293238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
294238c84f7SMauro Carvalho Chehab
295238c84f7SMauro Carvalho Chehab if (!fimc_capture_active(fimc))
296238c84f7SMauro Carvalho Chehab return;
297238c84f7SMauro Carvalho Chehab
298238c84f7SMauro Carvalho Chehab fimc_stop_capture(fimc, false);
299238c84f7SMauro Carvalho Chehab }
300238c84f7SMauro Carvalho Chehab
fimc_capture_suspend(struct fimc_dev * fimc)301238c84f7SMauro Carvalho Chehab int fimc_capture_suspend(struct fimc_dev *fimc)
302238c84f7SMauro Carvalho Chehab {
303238c84f7SMauro Carvalho Chehab bool suspend = fimc_capture_busy(fimc);
304238c84f7SMauro Carvalho Chehab
305238c84f7SMauro Carvalho Chehab int ret = fimc_stop_capture(fimc, suspend);
306238c84f7SMauro Carvalho Chehab if (ret)
307238c84f7SMauro Carvalho Chehab return ret;
308238c84f7SMauro Carvalho Chehab return fimc_pipeline_call(&fimc->vid_cap.ve, close);
309238c84f7SMauro Carvalho Chehab }
310238c84f7SMauro Carvalho Chehab
311238c84f7SMauro Carvalho Chehab static void buffer_queue(struct vb2_buffer *vb);
312238c84f7SMauro Carvalho Chehab
fimc_capture_resume(struct fimc_dev * fimc)313238c84f7SMauro Carvalho Chehab int fimc_capture_resume(struct fimc_dev *fimc)
314238c84f7SMauro Carvalho Chehab {
315238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
316238c84f7SMauro Carvalho Chehab struct exynos_video_entity *ve = &vid_cap->ve;
317238c84f7SMauro Carvalho Chehab struct fimc_vid_buffer *buf;
318238c84f7SMauro Carvalho Chehab int i;
319238c84f7SMauro Carvalho Chehab
320238c84f7SMauro Carvalho Chehab if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
321238c84f7SMauro Carvalho Chehab return 0;
322238c84f7SMauro Carvalho Chehab
323238c84f7SMauro Carvalho Chehab INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
324238c84f7SMauro Carvalho Chehab vid_cap->buf_index = 0;
325238c84f7SMauro Carvalho Chehab fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
326238c84f7SMauro Carvalho Chehab fimc_capture_hw_init(fimc);
327238c84f7SMauro Carvalho Chehab
328238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
329238c84f7SMauro Carvalho Chehab
330238c84f7SMauro Carvalho Chehab for (i = 0; i < vid_cap->reqbufs_count; i++) {
331238c84f7SMauro Carvalho Chehab if (list_empty(&vid_cap->pending_buf_q))
332238c84f7SMauro Carvalho Chehab break;
333238c84f7SMauro Carvalho Chehab buf = fimc_pending_queue_pop(vid_cap);
334238c84f7SMauro Carvalho Chehab buffer_queue(&buf->vb.vb2_buf);
335238c84f7SMauro Carvalho Chehab }
336238c84f7SMauro Carvalho Chehab return 0;
337238c84f7SMauro Carvalho Chehab
338238c84f7SMauro Carvalho Chehab }
339238c84f7SMauro Carvalho Chehab
queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])340238c84f7SMauro Carvalho Chehab static int queue_setup(struct vb2_queue *vq,
341238c84f7SMauro Carvalho Chehab unsigned int *num_buffers, unsigned int *num_planes,
342238c84f7SMauro Carvalho Chehab unsigned int sizes[], struct device *alloc_devs[])
343238c84f7SMauro Carvalho Chehab {
344238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = vq->drv_priv;
345238c84f7SMauro Carvalho Chehab struct fimc_frame *frame = &ctx->d_frame;
346238c84f7SMauro Carvalho Chehab struct fimc_fmt *fmt = frame->fmt;
347238c84f7SMauro Carvalho Chehab unsigned long wh = frame->f_width * frame->f_height;
348238c84f7SMauro Carvalho Chehab int i;
349238c84f7SMauro Carvalho Chehab
350238c84f7SMauro Carvalho Chehab if (fmt == NULL)
351238c84f7SMauro Carvalho Chehab return -EINVAL;
352238c84f7SMauro Carvalho Chehab
353238c84f7SMauro Carvalho Chehab if (*num_planes) {
354238c84f7SMauro Carvalho Chehab if (*num_planes != fmt->memplanes)
355238c84f7SMauro Carvalho Chehab return -EINVAL;
356238c84f7SMauro Carvalho Chehab for (i = 0; i < *num_planes; i++)
357238c84f7SMauro Carvalho Chehab if (sizes[i] < (wh * fmt->depth[i]) / 8)
358238c84f7SMauro Carvalho Chehab return -EINVAL;
359238c84f7SMauro Carvalho Chehab return 0;
360238c84f7SMauro Carvalho Chehab }
361238c84f7SMauro Carvalho Chehab
362238c84f7SMauro Carvalho Chehab *num_planes = fmt->memplanes;
363238c84f7SMauro Carvalho Chehab
364238c84f7SMauro Carvalho Chehab for (i = 0; i < fmt->memplanes; i++) {
365238c84f7SMauro Carvalho Chehab unsigned int size = (wh * fmt->depth[i]) / 8;
366238c84f7SMauro Carvalho Chehab
367238c84f7SMauro Carvalho Chehab if (fimc_fmt_is_user_defined(fmt->color))
368238c84f7SMauro Carvalho Chehab sizes[i] = frame->payload[i];
369238c84f7SMauro Carvalho Chehab else
370238c84f7SMauro Carvalho Chehab sizes[i] = max_t(u32, size, frame->payload[i]);
371238c84f7SMauro Carvalho Chehab }
372238c84f7SMauro Carvalho Chehab
373238c84f7SMauro Carvalho Chehab return 0;
374238c84f7SMauro Carvalho Chehab }
375238c84f7SMauro Carvalho Chehab
buffer_prepare(struct vb2_buffer * vb)376238c84f7SMauro Carvalho Chehab static int buffer_prepare(struct vb2_buffer *vb)
377238c84f7SMauro Carvalho Chehab {
378238c84f7SMauro Carvalho Chehab struct vb2_queue *vq = vb->vb2_queue;
379238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = vq->drv_priv;
380238c84f7SMauro Carvalho Chehab int i;
381238c84f7SMauro Carvalho Chehab
382238c84f7SMauro Carvalho Chehab if (ctx->d_frame.fmt == NULL)
383238c84f7SMauro Carvalho Chehab return -EINVAL;
384238c84f7SMauro Carvalho Chehab
385238c84f7SMauro Carvalho Chehab for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
386238c84f7SMauro Carvalho Chehab unsigned long size = ctx->d_frame.payload[i];
387238c84f7SMauro Carvalho Chehab
388238c84f7SMauro Carvalho Chehab if (vb2_plane_size(vb, i) < size) {
389238c84f7SMauro Carvalho Chehab v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
390238c84f7SMauro Carvalho Chehab "User buffer too small (%ld < %ld)\n",
391238c84f7SMauro Carvalho Chehab vb2_plane_size(vb, i), size);
392238c84f7SMauro Carvalho Chehab return -EINVAL;
393238c84f7SMauro Carvalho Chehab }
394238c84f7SMauro Carvalho Chehab vb2_set_plane_payload(vb, i, size);
395238c84f7SMauro Carvalho Chehab }
396238c84f7SMauro Carvalho Chehab
397238c84f7SMauro Carvalho Chehab return 0;
398238c84f7SMauro Carvalho Chehab }
399238c84f7SMauro Carvalho Chehab
buffer_queue(struct vb2_buffer * vb)400238c84f7SMauro Carvalho Chehab static void buffer_queue(struct vb2_buffer *vb)
401238c84f7SMauro Carvalho Chehab {
402238c84f7SMauro Carvalho Chehab struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
403238c84f7SMauro Carvalho Chehab struct fimc_vid_buffer *buf
404238c84f7SMauro Carvalho Chehab = container_of(vbuf, struct fimc_vid_buffer, vb);
405238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
406238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
407238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
408238c84f7SMauro Carvalho Chehab struct exynos_video_entity *ve = &vid_cap->ve;
409238c84f7SMauro Carvalho Chehab unsigned long flags;
410238c84f7SMauro Carvalho Chehab int min_bufs;
411238c84f7SMauro Carvalho Chehab
412238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, flags);
413238c84f7SMauro Carvalho Chehab fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->addr);
414238c84f7SMauro Carvalho Chehab
415238c84f7SMauro Carvalho Chehab if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
416238c84f7SMauro Carvalho Chehab !test_bit(ST_CAPT_STREAM, &fimc->state) &&
417238c84f7SMauro Carvalho Chehab vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
418238c84f7SMauro Carvalho Chehab /* Setup the buffer directly for processing. */
419238c84f7SMauro Carvalho Chehab int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
420238c84f7SMauro Carvalho Chehab vid_cap->buf_index;
421238c84f7SMauro Carvalho Chehab
422238c84f7SMauro Carvalho Chehab fimc_hw_set_output_addr(fimc, &buf->addr, buf_id);
423238c84f7SMauro Carvalho Chehab buf->index = vid_cap->buf_index;
424238c84f7SMauro Carvalho Chehab fimc_active_queue_add(vid_cap, buf);
425238c84f7SMauro Carvalho Chehab
426238c84f7SMauro Carvalho Chehab if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
427238c84f7SMauro Carvalho Chehab vid_cap->buf_index = 0;
428238c84f7SMauro Carvalho Chehab } else {
429238c84f7SMauro Carvalho Chehab fimc_pending_queue_add(vid_cap, buf);
430238c84f7SMauro Carvalho Chehab }
431238c84f7SMauro Carvalho Chehab
432238c84f7SMauro Carvalho Chehab min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
433238c84f7SMauro Carvalho Chehab
434238c84f7SMauro Carvalho Chehab
435238c84f7SMauro Carvalho Chehab if (vb2_is_streaming(&vid_cap->vbq) &&
436238c84f7SMauro Carvalho Chehab vid_cap->active_buf_cnt >= min_bufs &&
437238c84f7SMauro Carvalho Chehab !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
438238c84f7SMauro Carvalho Chehab int ret;
439238c84f7SMauro Carvalho Chehab
440238c84f7SMauro Carvalho Chehab fimc_activate_capture(ctx);
441238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
442238c84f7SMauro Carvalho Chehab
443238c84f7SMauro Carvalho Chehab if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
444238c84f7SMauro Carvalho Chehab return;
445238c84f7SMauro Carvalho Chehab
446238c84f7SMauro Carvalho Chehab ret = fimc_pipeline_call(ve, set_stream, 1);
447238c84f7SMauro Carvalho Chehab if (ret < 0)
448238c84f7SMauro Carvalho Chehab v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
449238c84f7SMauro Carvalho Chehab return;
450238c84f7SMauro Carvalho Chehab }
451238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
452238c84f7SMauro Carvalho Chehab }
453238c84f7SMauro Carvalho Chehab
454238c84f7SMauro Carvalho Chehab static const struct vb2_ops fimc_capture_qops = {
455238c84f7SMauro Carvalho Chehab .queue_setup = queue_setup,
456238c84f7SMauro Carvalho Chehab .buf_prepare = buffer_prepare,
457238c84f7SMauro Carvalho Chehab .buf_queue = buffer_queue,
458238c84f7SMauro Carvalho Chehab .wait_prepare = vb2_ops_wait_prepare,
459238c84f7SMauro Carvalho Chehab .wait_finish = vb2_ops_wait_finish,
460238c84f7SMauro Carvalho Chehab .start_streaming = start_streaming,
461238c84f7SMauro Carvalho Chehab .stop_streaming = stop_streaming,
462238c84f7SMauro Carvalho Chehab };
463238c84f7SMauro Carvalho Chehab
464238c84f7SMauro Carvalho Chehab static int fimc_capture_set_default_format(struct fimc_dev *fimc);
465238c84f7SMauro Carvalho Chehab
fimc_capture_open(struct file * file)466238c84f7SMauro Carvalho Chehab static int fimc_capture_open(struct file *file)
467238c84f7SMauro Carvalho Chehab {
468238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
469238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
470238c84f7SMauro Carvalho Chehab struct exynos_video_entity *ve = &vc->ve;
471238c84f7SMauro Carvalho Chehab int ret = -EBUSY;
472238c84f7SMauro Carvalho Chehab
473238c84f7SMauro Carvalho Chehab dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
474238c84f7SMauro Carvalho Chehab
475238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
476238c84f7SMauro Carvalho Chehab
477238c84f7SMauro Carvalho Chehab if (fimc_m2m_active(fimc))
478238c84f7SMauro Carvalho Chehab goto unlock;
479238c84f7SMauro Carvalho Chehab
480238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_BUSY, &fimc->state);
481238c84f7SMauro Carvalho Chehab ret = pm_runtime_resume_and_get(&fimc->pdev->dev);
482238c84f7SMauro Carvalho Chehab if (ret < 0)
483238c84f7SMauro Carvalho Chehab goto unlock;
484238c84f7SMauro Carvalho Chehab
485238c84f7SMauro Carvalho Chehab ret = v4l2_fh_open(file);
486238c84f7SMauro Carvalho Chehab if (ret) {
487238c84f7SMauro Carvalho Chehab pm_runtime_put_sync(&fimc->pdev->dev);
488238c84f7SMauro Carvalho Chehab goto unlock;
489238c84f7SMauro Carvalho Chehab }
490238c84f7SMauro Carvalho Chehab
491238c84f7SMauro Carvalho Chehab if (v4l2_fh_is_singular_file(file)) {
492238c84f7SMauro Carvalho Chehab fimc_md_graph_lock(ve);
493238c84f7SMauro Carvalho Chehab
494238c84f7SMauro Carvalho Chehab ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
495238c84f7SMauro Carvalho Chehab
496238c84f7SMauro Carvalho Chehab if (ret == 0)
497238c84f7SMauro Carvalho Chehab ve->vdev.entity.use_count++;
498238c84f7SMauro Carvalho Chehab
499238c84f7SMauro Carvalho Chehab fimc_md_graph_unlock(ve);
500238c84f7SMauro Carvalho Chehab
501238c84f7SMauro Carvalho Chehab if (ret == 0)
502238c84f7SMauro Carvalho Chehab ret = fimc_capture_set_default_format(fimc);
503238c84f7SMauro Carvalho Chehab
504238c84f7SMauro Carvalho Chehab if (ret < 0) {
505238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_BUSY, &fimc->state);
506238c84f7SMauro Carvalho Chehab pm_runtime_put_sync(&fimc->pdev->dev);
507238c84f7SMauro Carvalho Chehab v4l2_fh_release(file);
508238c84f7SMauro Carvalho Chehab }
509238c84f7SMauro Carvalho Chehab }
510238c84f7SMauro Carvalho Chehab unlock:
511238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
512238c84f7SMauro Carvalho Chehab return ret;
513238c84f7SMauro Carvalho Chehab }
514238c84f7SMauro Carvalho Chehab
fimc_capture_release(struct file * file)515238c84f7SMauro Carvalho Chehab static int fimc_capture_release(struct file *file)
516238c84f7SMauro Carvalho Chehab {
517238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
518238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
519238c84f7SMauro Carvalho Chehab bool close = v4l2_fh_is_singular_file(file);
520238c84f7SMauro Carvalho Chehab int ret;
521238c84f7SMauro Carvalho Chehab
522238c84f7SMauro Carvalho Chehab dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
523238c84f7SMauro Carvalho Chehab
524238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
525238c84f7SMauro Carvalho Chehab
526238c84f7SMauro Carvalho Chehab if (close && vc->streaming) {
52712cecbf9STomi Valkeinen video_device_pipeline_stop(&vc->ve.vdev);
528238c84f7SMauro Carvalho Chehab vc->streaming = false;
529238c84f7SMauro Carvalho Chehab }
530238c84f7SMauro Carvalho Chehab
531238c84f7SMauro Carvalho Chehab ret = _vb2_fop_release(file, NULL);
532238c84f7SMauro Carvalho Chehab
533238c84f7SMauro Carvalho Chehab if (close) {
534238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_BUSY, &fimc->state);
535238c84f7SMauro Carvalho Chehab fimc_pipeline_call(&vc->ve, close);
536238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
537238c84f7SMauro Carvalho Chehab
538238c84f7SMauro Carvalho Chehab fimc_md_graph_lock(&vc->ve);
539238c84f7SMauro Carvalho Chehab vc->ve.vdev.entity.use_count--;
540238c84f7SMauro Carvalho Chehab fimc_md_graph_unlock(&vc->ve);
541238c84f7SMauro Carvalho Chehab }
542238c84f7SMauro Carvalho Chehab
543238c84f7SMauro Carvalho Chehab pm_runtime_put_sync(&fimc->pdev->dev);
544238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
545238c84f7SMauro Carvalho Chehab
546238c84f7SMauro Carvalho Chehab return ret;
547238c84f7SMauro Carvalho Chehab }
548238c84f7SMauro Carvalho Chehab
549238c84f7SMauro Carvalho Chehab static const struct v4l2_file_operations fimc_capture_fops = {
550238c84f7SMauro Carvalho Chehab .owner = THIS_MODULE,
551238c84f7SMauro Carvalho Chehab .open = fimc_capture_open,
552238c84f7SMauro Carvalho Chehab .release = fimc_capture_release,
553238c84f7SMauro Carvalho Chehab .poll = vb2_fop_poll,
554238c84f7SMauro Carvalho Chehab .unlocked_ioctl = video_ioctl2,
555238c84f7SMauro Carvalho Chehab .mmap = vb2_fop_mmap,
556238c84f7SMauro Carvalho Chehab };
557238c84f7SMauro Carvalho Chehab
558238c84f7SMauro Carvalho Chehab /*
559238c84f7SMauro Carvalho Chehab * Format and crop negotiation helpers
560238c84f7SMauro Carvalho Chehab */
561238c84f7SMauro Carvalho Chehab
fimc_capture_try_format(struct fimc_ctx * ctx,u32 * width,u32 * height,u32 * code,u32 * fourcc,int pad)562238c84f7SMauro Carvalho Chehab static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
563238c84f7SMauro Carvalho Chehab u32 *width, u32 *height,
564238c84f7SMauro Carvalho Chehab u32 *code, u32 *fourcc, int pad)
565238c84f7SMauro Carvalho Chehab {
566238c84f7SMauro Carvalho Chehab bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
567238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
568238c84f7SMauro Carvalho Chehab const struct fimc_variant *var = fimc->variant;
569238c84f7SMauro Carvalho Chehab const struct fimc_pix_limit *pl = var->pix_limit;
570238c84f7SMauro Carvalho Chehab struct fimc_frame *dst = &ctx->d_frame;
571238c84f7SMauro Carvalho Chehab u32 depth, min_w, max_w, min_h, align_h = 3;
572238c84f7SMauro Carvalho Chehab u32 mask = FMT_FLAGS_CAM;
573238c84f7SMauro Carvalho Chehab struct fimc_fmt *ffmt;
574238c84f7SMauro Carvalho Chehab
575238c84f7SMauro Carvalho Chehab /* Conversion from/to JPEG or User Defined format is not supported */
576238c84f7SMauro Carvalho Chehab if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
577238c84f7SMauro Carvalho Chehab fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
578238c84f7SMauro Carvalho Chehab *code = ctx->s_frame.fmt->mbus_code;
579238c84f7SMauro Carvalho Chehab
580238c84f7SMauro Carvalho Chehab if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
581238c84f7SMauro Carvalho Chehab mask |= FMT_FLAGS_M2M;
582238c84f7SMauro Carvalho Chehab
583238c84f7SMauro Carvalho Chehab if (pad == FIMC_SD_PAD_SINK_FIFO)
584238c84f7SMauro Carvalho Chehab mask = FMT_FLAGS_WRITEBACK;
585238c84f7SMauro Carvalho Chehab
586238c84f7SMauro Carvalho Chehab ffmt = fimc_find_format(fourcc, code, mask, 0);
587238c84f7SMauro Carvalho Chehab if (WARN_ON(!ffmt))
588238c84f7SMauro Carvalho Chehab return NULL;
589238c84f7SMauro Carvalho Chehab
590238c84f7SMauro Carvalho Chehab if (code)
591238c84f7SMauro Carvalho Chehab *code = ffmt->mbus_code;
592238c84f7SMauro Carvalho Chehab if (fourcc)
593238c84f7SMauro Carvalho Chehab *fourcc = ffmt->fourcc;
594238c84f7SMauro Carvalho Chehab
595238c84f7SMauro Carvalho Chehab if (pad != FIMC_SD_PAD_SOURCE) {
596238c84f7SMauro Carvalho Chehab max_w = fimc_fmt_is_user_defined(ffmt->color) ?
597238c84f7SMauro Carvalho Chehab pl->scaler_dis_w : pl->scaler_en_w;
598238c84f7SMauro Carvalho Chehab /* Apply the camera input interface pixel constraints */
599238c84f7SMauro Carvalho Chehab v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
600238c84f7SMauro Carvalho Chehab height, max_t(u32, *height, 32),
601238c84f7SMauro Carvalho Chehab FIMC_CAMIF_MAX_HEIGHT,
602238c84f7SMauro Carvalho Chehab fimc_fmt_is_user_defined(ffmt->color) ?
603238c84f7SMauro Carvalho Chehab 3 : 1,
604238c84f7SMauro Carvalho Chehab 0);
605238c84f7SMauro Carvalho Chehab return ffmt;
606238c84f7SMauro Carvalho Chehab }
607238c84f7SMauro Carvalho Chehab /* Can't scale or crop in transparent (JPEG) transfer mode */
608238c84f7SMauro Carvalho Chehab if (fimc_fmt_is_user_defined(ffmt->color)) {
609238c84f7SMauro Carvalho Chehab *width = ctx->s_frame.f_width;
610238c84f7SMauro Carvalho Chehab *height = ctx->s_frame.f_height;
611238c84f7SMauro Carvalho Chehab return ffmt;
612238c84f7SMauro Carvalho Chehab }
613238c84f7SMauro Carvalho Chehab /* Apply the scaler and the output DMA constraints */
614238c84f7SMauro Carvalho Chehab max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
615238c84f7SMauro Carvalho Chehab if (ctx->state & FIMC_COMPOSE) {
616238c84f7SMauro Carvalho Chehab min_w = dst->offs_h + dst->width;
617238c84f7SMauro Carvalho Chehab min_h = dst->offs_v + dst->height;
618238c84f7SMauro Carvalho Chehab } else {
619238c84f7SMauro Carvalho Chehab min_w = var->min_out_pixsize;
620238c84f7SMauro Carvalho Chehab min_h = var->min_out_pixsize;
621238c84f7SMauro Carvalho Chehab }
622238c84f7SMauro Carvalho Chehab if (var->min_vsize_align == 1 && !rotation)
623238c84f7SMauro Carvalho Chehab align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
624238c84f7SMauro Carvalho Chehab
625238c84f7SMauro Carvalho Chehab depth = fimc_get_format_depth(ffmt);
626238c84f7SMauro Carvalho Chehab v4l_bound_align_image(width, min_w, max_w,
627238c84f7SMauro Carvalho Chehab ffs(var->min_out_pixsize) - 1,
628238c84f7SMauro Carvalho Chehab height, min_h, FIMC_CAMIF_MAX_HEIGHT,
629238c84f7SMauro Carvalho Chehab align_h,
630238c84f7SMauro Carvalho Chehab 64/(ALIGN(depth, 8)));
631238c84f7SMauro Carvalho Chehab
632238c84f7SMauro Carvalho Chehab dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
633238c84f7SMauro Carvalho Chehab pad, code ? *code : 0, *width, *height,
634238c84f7SMauro Carvalho Chehab dst->f_width, dst->f_height);
635238c84f7SMauro Carvalho Chehab
636238c84f7SMauro Carvalho Chehab return ffmt;
637238c84f7SMauro Carvalho Chehab }
638238c84f7SMauro Carvalho Chehab
fimc_capture_try_selection(struct fimc_ctx * ctx,struct v4l2_rect * r,int target)639238c84f7SMauro Carvalho Chehab static void fimc_capture_try_selection(struct fimc_ctx *ctx,
640238c84f7SMauro Carvalho Chehab struct v4l2_rect *r,
641238c84f7SMauro Carvalho Chehab int target)
642238c84f7SMauro Carvalho Chehab {
643238c84f7SMauro Carvalho Chehab bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
644238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
645238c84f7SMauro Carvalho Chehab const struct fimc_variant *var = fimc->variant;
646238c84f7SMauro Carvalho Chehab const struct fimc_pix_limit *pl = var->pix_limit;
647238c84f7SMauro Carvalho Chehab struct fimc_frame *sink = &ctx->s_frame;
648238c84f7SMauro Carvalho Chehab u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
649238c84f7SMauro Carvalho Chehab u32 align_sz = 0, align_h = 4;
650238c84f7SMauro Carvalho Chehab u32 max_sc_h, max_sc_v;
651238c84f7SMauro Carvalho Chehab
652238c84f7SMauro Carvalho Chehab /* In JPEG transparent transfer mode cropping is not supported */
653238c84f7SMauro Carvalho Chehab if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
654238c84f7SMauro Carvalho Chehab r->width = sink->f_width;
655238c84f7SMauro Carvalho Chehab r->height = sink->f_height;
656238c84f7SMauro Carvalho Chehab r->left = r->top = 0;
657238c84f7SMauro Carvalho Chehab return;
658238c84f7SMauro Carvalho Chehab }
659238c84f7SMauro Carvalho Chehab if (target == V4L2_SEL_TGT_COMPOSE) {
660238c84f7SMauro Carvalho Chehab u32 tmp_min_h = ffs(sink->width) - 3;
661238c84f7SMauro Carvalho Chehab u32 tmp_min_v = ffs(sink->height) - 1;
662238c84f7SMauro Carvalho Chehab
663238c84f7SMauro Carvalho Chehab if (ctx->rotation != 90 && ctx->rotation != 270)
664238c84f7SMauro Carvalho Chehab align_h = 1;
665238c84f7SMauro Carvalho Chehab max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h);
666238c84f7SMauro Carvalho Chehab max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v);
667238c84f7SMauro Carvalho Chehab min_sz = var->min_out_pixsize;
668238c84f7SMauro Carvalho Chehab } else {
669238c84f7SMauro Carvalho Chehab u32 depth = fimc_get_format_depth(sink->fmt);
670238c84f7SMauro Carvalho Chehab align_sz = 64/ALIGN(depth, 8);
671238c84f7SMauro Carvalho Chehab min_sz = var->min_inp_pixsize;
672238c84f7SMauro Carvalho Chehab min_w = min_h = min_sz;
673238c84f7SMauro Carvalho Chehab max_sc_h = max_sc_v = 1;
674238c84f7SMauro Carvalho Chehab }
675238c84f7SMauro Carvalho Chehab /*
676238c84f7SMauro Carvalho Chehab * For the compose rectangle the following constraints must be met:
677238c84f7SMauro Carvalho Chehab * - it must fit in the sink pad format rectangle (f_width/f_height);
678238c84f7SMauro Carvalho Chehab * - maximum downscaling ratio is 64;
679238c84f7SMauro Carvalho Chehab * - maximum crop size depends if the rotator is used or not;
680238c84f7SMauro Carvalho Chehab * - the sink pad format width/height must be 4 multiple of the
681238c84f7SMauro Carvalho Chehab * prescaler ratios determined by sink pad size and source pad crop,
682238c84f7SMauro Carvalho Chehab * the prescaler ratio is returned by fimc_get_scaler_factor().
683238c84f7SMauro Carvalho Chehab */
684238c84f7SMauro Carvalho Chehab max_w = min_t(u32,
685238c84f7SMauro Carvalho Chehab rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
686238c84f7SMauro Carvalho Chehab rotate ? sink->f_height : sink->f_width);
687238c84f7SMauro Carvalho Chehab max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
688238c84f7SMauro Carvalho Chehab
689238c84f7SMauro Carvalho Chehab if (target == V4L2_SEL_TGT_COMPOSE) {
690238c84f7SMauro Carvalho Chehab min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
691238c84f7SMauro Carvalho Chehab min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
692238c84f7SMauro Carvalho Chehab if (rotate) {
693238c84f7SMauro Carvalho Chehab swap(max_sc_h, max_sc_v);
694238c84f7SMauro Carvalho Chehab swap(min_w, min_h);
695238c84f7SMauro Carvalho Chehab }
696238c84f7SMauro Carvalho Chehab }
697238c84f7SMauro Carvalho Chehab v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
698238c84f7SMauro Carvalho Chehab &r->height, min_h, max_h, align_h,
699238c84f7SMauro Carvalho Chehab align_sz);
700238c84f7SMauro Carvalho Chehab /* Adjust left/top if crop/compose rectangle is out of bounds */
701238c84f7SMauro Carvalho Chehab r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
702238c84f7SMauro Carvalho Chehab r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
703238c84f7SMauro Carvalho Chehab r->left = round_down(r->left, var->hor_offs_align);
704238c84f7SMauro Carvalho Chehab
705238c84f7SMauro Carvalho Chehab dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
706238c84f7SMauro Carvalho Chehab target, r->left, r->top, r->width, r->height,
707238c84f7SMauro Carvalho Chehab sink->f_width, sink->f_height);
708238c84f7SMauro Carvalho Chehab }
709238c84f7SMauro Carvalho Chehab
710238c84f7SMauro Carvalho Chehab /*
711238c84f7SMauro Carvalho Chehab * The video node ioctl operations
712238c84f7SMauro Carvalho Chehab */
fimc_cap_querycap(struct file * file,void * priv,struct v4l2_capability * cap)713238c84f7SMauro Carvalho Chehab static int fimc_cap_querycap(struct file *file, void *priv,
714238c84f7SMauro Carvalho Chehab struct v4l2_capability *cap)
715238c84f7SMauro Carvalho Chehab {
716238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
717238c84f7SMauro Carvalho Chehab
718238c84f7SMauro Carvalho Chehab __fimc_vidioc_querycap(&fimc->pdev->dev, cap);
719238c84f7SMauro Carvalho Chehab return 0;
720238c84f7SMauro Carvalho Chehab }
721238c84f7SMauro Carvalho Chehab
fimc_cap_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)722238c84f7SMauro Carvalho Chehab static int fimc_cap_enum_fmt(struct file *file, void *priv,
723238c84f7SMauro Carvalho Chehab struct v4l2_fmtdesc *f)
724238c84f7SMauro Carvalho Chehab {
725238c84f7SMauro Carvalho Chehab struct fimc_fmt *fmt;
726238c84f7SMauro Carvalho Chehab
727238c84f7SMauro Carvalho Chehab fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
728238c84f7SMauro Carvalho Chehab f->index);
729238c84f7SMauro Carvalho Chehab if (!fmt)
730238c84f7SMauro Carvalho Chehab return -EINVAL;
731238c84f7SMauro Carvalho Chehab f->pixelformat = fmt->fourcc;
732238c84f7SMauro Carvalho Chehab return 0;
733238c84f7SMauro Carvalho Chehab }
734238c84f7SMauro Carvalho Chehab
fimc_pipeline_get_head(struct media_entity * me)735238c84f7SMauro Carvalho Chehab static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
736238c84f7SMauro Carvalho Chehab {
737238c84f7SMauro Carvalho Chehab struct media_pad *pad = &me->pads[0];
738238c84f7SMauro Carvalho Chehab
739238c84f7SMauro Carvalho Chehab while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
740b2e44430SLaurent Pinchart pad = media_pad_remote_pad_first(pad);
741238c84f7SMauro Carvalho Chehab if (!pad)
742238c84f7SMauro Carvalho Chehab break;
743238c84f7SMauro Carvalho Chehab me = pad->entity;
744238c84f7SMauro Carvalho Chehab pad = &me->pads[0];
745238c84f7SMauro Carvalho Chehab }
746238c84f7SMauro Carvalho Chehab
747238c84f7SMauro Carvalho Chehab return me;
748238c84f7SMauro Carvalho Chehab }
749238c84f7SMauro Carvalho Chehab
750238c84f7SMauro Carvalho Chehab /**
751238c84f7SMauro Carvalho Chehab * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
752238c84f7SMauro Carvalho Chehab * elements
753238c84f7SMauro Carvalho Chehab * @ctx: FIMC capture context
754238c84f7SMauro Carvalho Chehab * @tfmt: media bus format to try/set on subdevs
755238c84f7SMauro Carvalho Chehab * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
756238c84f7SMauro Carvalho Chehab * @set: true to set format on subdevs, false to try only
757238c84f7SMauro Carvalho Chehab */
fimc_pipeline_try_format(struct fimc_ctx * ctx,struct v4l2_mbus_framefmt * tfmt,struct fimc_fmt ** fmt_id,bool set)758238c84f7SMauro Carvalho Chehab static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
759238c84f7SMauro Carvalho Chehab struct v4l2_mbus_framefmt *tfmt,
760238c84f7SMauro Carvalho Chehab struct fimc_fmt **fmt_id,
761238c84f7SMauro Carvalho Chehab bool set)
762238c84f7SMauro Carvalho Chehab {
763238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = ctx->fimc_dev;
764238c84f7SMauro Carvalho Chehab struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
765238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
766*e3a69496SLaurent Pinchart struct v4l2_subdev_format sfmt = {
767*e3a69496SLaurent Pinchart .which = set ? V4L2_SUBDEV_FORMAT_ACTIVE
768*e3a69496SLaurent Pinchart : V4L2_SUBDEV_FORMAT_TRY,
769*e3a69496SLaurent Pinchart };
770238c84f7SMauro Carvalho Chehab struct v4l2_mbus_framefmt *mf = &sfmt.format;
771238c84f7SMauro Carvalho Chehab struct media_entity *me;
772238c84f7SMauro Carvalho Chehab struct fimc_fmt *ffmt;
773238c84f7SMauro Carvalho Chehab struct media_pad *pad;
774238c84f7SMauro Carvalho Chehab int ret, i = 1;
775238c84f7SMauro Carvalho Chehab u32 fcc;
776238c84f7SMauro Carvalho Chehab
777238c84f7SMauro Carvalho Chehab if (WARN_ON(!sd || !tfmt))
778238c84f7SMauro Carvalho Chehab return -EINVAL;
779238c84f7SMauro Carvalho Chehab
780238c84f7SMauro Carvalho Chehab sfmt.format = *tfmt;
781238c84f7SMauro Carvalho Chehab
782238c84f7SMauro Carvalho Chehab me = fimc_pipeline_get_head(&sd->entity);
783238c84f7SMauro Carvalho Chehab
784238c84f7SMauro Carvalho Chehab while (1) {
785238c84f7SMauro Carvalho Chehab ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
786238c84f7SMauro Carvalho Chehab FMT_FLAGS_CAM, i++);
787238c84f7SMauro Carvalho Chehab if (ffmt == NULL) {
788238c84f7SMauro Carvalho Chehab /*
789238c84f7SMauro Carvalho Chehab * Notify user-space if common pixel code for
790238c84f7SMauro Carvalho Chehab * host and sensor does not exist.
791238c84f7SMauro Carvalho Chehab */
792238c84f7SMauro Carvalho Chehab return -EINVAL;
793238c84f7SMauro Carvalho Chehab }
794238c84f7SMauro Carvalho Chehab mf->code = tfmt->code = ffmt->mbus_code;
795238c84f7SMauro Carvalho Chehab
796238c84f7SMauro Carvalho Chehab /* set format on all pipeline subdevs */
797238c84f7SMauro Carvalho Chehab while (me != &fimc->vid_cap.subdev.entity) {
798238c84f7SMauro Carvalho Chehab sd = media_entity_to_v4l2_subdev(me);
799238c84f7SMauro Carvalho Chehab
800238c84f7SMauro Carvalho Chehab sfmt.pad = 0;
801238c84f7SMauro Carvalho Chehab ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
802238c84f7SMauro Carvalho Chehab if (ret)
803238c84f7SMauro Carvalho Chehab return ret;
804238c84f7SMauro Carvalho Chehab
805238c84f7SMauro Carvalho Chehab if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
806238c84f7SMauro Carvalho Chehab sfmt.pad = me->num_pads - 1;
807238c84f7SMauro Carvalho Chehab mf->code = tfmt->code;
808238c84f7SMauro Carvalho Chehab ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
809238c84f7SMauro Carvalho Chehab &sfmt);
810238c84f7SMauro Carvalho Chehab if (ret)
811238c84f7SMauro Carvalho Chehab return ret;
812238c84f7SMauro Carvalho Chehab }
813238c84f7SMauro Carvalho Chehab
814b2e44430SLaurent Pinchart pad = media_pad_remote_pad_first(&me->pads[sfmt.pad]);
815238c84f7SMauro Carvalho Chehab if (!pad)
816238c84f7SMauro Carvalho Chehab return -EINVAL;
817238c84f7SMauro Carvalho Chehab me = pad->entity;
818238c84f7SMauro Carvalho Chehab }
819238c84f7SMauro Carvalho Chehab
820238c84f7SMauro Carvalho Chehab if (mf->code != tfmt->code)
821238c84f7SMauro Carvalho Chehab continue;
822238c84f7SMauro Carvalho Chehab
823238c84f7SMauro Carvalho Chehab fcc = ffmt->fourcc;
824238c84f7SMauro Carvalho Chehab tfmt->width = mf->width;
825238c84f7SMauro Carvalho Chehab tfmt->height = mf->height;
826238c84f7SMauro Carvalho Chehab ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
827238c84f7SMauro Carvalho Chehab NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
828238c84f7SMauro Carvalho Chehab ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
829238c84f7SMauro Carvalho Chehab NULL, &fcc, FIMC_SD_PAD_SOURCE);
830238c84f7SMauro Carvalho Chehab if (ffmt && ffmt->mbus_code)
831238c84f7SMauro Carvalho Chehab mf->code = ffmt->mbus_code;
832238c84f7SMauro Carvalho Chehab if (mf->width != tfmt->width || mf->height != tfmt->height)
833238c84f7SMauro Carvalho Chehab continue;
834238c84f7SMauro Carvalho Chehab tfmt->code = mf->code;
835238c84f7SMauro Carvalho Chehab break;
836238c84f7SMauro Carvalho Chehab }
837238c84f7SMauro Carvalho Chehab
838238c84f7SMauro Carvalho Chehab if (fmt_id && ffmt)
839238c84f7SMauro Carvalho Chehab *fmt_id = ffmt;
840238c84f7SMauro Carvalho Chehab *tfmt = *mf;
841238c84f7SMauro Carvalho Chehab
842238c84f7SMauro Carvalho Chehab return 0;
843238c84f7SMauro Carvalho Chehab }
844238c84f7SMauro Carvalho Chehab
845238c84f7SMauro Carvalho Chehab /**
846238c84f7SMauro Carvalho Chehab * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
847238c84f7SMauro Carvalho Chehab * @sensor: pointer to the sensor subdev
848238c84f7SMauro Carvalho Chehab * @plane_fmt: provides plane sizes corresponding to the frame layout entries
849238c84f7SMauro Carvalho Chehab * @num_planes: number of planes
850238c84f7SMauro Carvalho Chehab * @try: true to set the frame parameters, false to query only
851238c84f7SMauro Carvalho Chehab *
852238c84f7SMauro Carvalho Chehab * This function is used by this driver only for compressed/blob data formats.
853238c84f7SMauro Carvalho Chehab */
fimc_get_sensor_frame_desc(struct v4l2_subdev * sensor,struct v4l2_plane_pix_format * plane_fmt,unsigned int num_planes,bool try)854238c84f7SMauro Carvalho Chehab static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
855238c84f7SMauro Carvalho Chehab struct v4l2_plane_pix_format *plane_fmt,
856238c84f7SMauro Carvalho Chehab unsigned int num_planes, bool try)
857238c84f7SMauro Carvalho Chehab {
858ecefa105SLaurent Pinchart struct v4l2_mbus_frame_desc fd = { };
859238c84f7SMauro Carvalho Chehab int i, ret;
860238c84f7SMauro Carvalho Chehab int pad;
861238c84f7SMauro Carvalho Chehab
862238c84f7SMauro Carvalho Chehab for (i = 0; i < num_planes; i++)
863238c84f7SMauro Carvalho Chehab fd.entry[i].length = plane_fmt[i].sizeimage;
864238c84f7SMauro Carvalho Chehab
865238c84f7SMauro Carvalho Chehab pad = sensor->entity.num_pads - 1;
866238c84f7SMauro Carvalho Chehab if (try)
867238c84f7SMauro Carvalho Chehab ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
868238c84f7SMauro Carvalho Chehab else
869238c84f7SMauro Carvalho Chehab ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
870238c84f7SMauro Carvalho Chehab
871238c84f7SMauro Carvalho Chehab if (ret < 0)
872238c84f7SMauro Carvalho Chehab return ret;
873238c84f7SMauro Carvalho Chehab
874238c84f7SMauro Carvalho Chehab if (num_planes != fd.num_entries)
875238c84f7SMauro Carvalho Chehab return -EINVAL;
876238c84f7SMauro Carvalho Chehab
877238c84f7SMauro Carvalho Chehab for (i = 0; i < num_planes; i++)
878238c84f7SMauro Carvalho Chehab plane_fmt[i].sizeimage = fd.entry[i].length;
879238c84f7SMauro Carvalho Chehab
880238c84f7SMauro Carvalho Chehab if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
881238c84f7SMauro Carvalho Chehab v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
882238c84f7SMauro Carvalho Chehab fd.entry[0].length);
883238c84f7SMauro Carvalho Chehab
884238c84f7SMauro Carvalho Chehab return -EINVAL;
885238c84f7SMauro Carvalho Chehab }
886238c84f7SMauro Carvalho Chehab
887238c84f7SMauro Carvalho Chehab return 0;
888238c84f7SMauro Carvalho Chehab }
889238c84f7SMauro Carvalho Chehab
fimc_cap_g_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)890238c84f7SMauro Carvalho Chehab static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
891238c84f7SMauro Carvalho Chehab struct v4l2_format *f)
892238c84f7SMauro Carvalho Chehab {
893238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
894238c84f7SMauro Carvalho Chehab
895238c84f7SMauro Carvalho Chehab __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
896238c84f7SMauro Carvalho Chehab return 0;
897238c84f7SMauro Carvalho Chehab }
898238c84f7SMauro Carvalho Chehab
899238c84f7SMauro Carvalho Chehab /*
900238c84f7SMauro Carvalho Chehab * Try or set format on the fimc.X.capture video node and additionally
901238c84f7SMauro Carvalho Chehab * on the whole pipeline if @try is false.
902238c84f7SMauro Carvalho Chehab * Locking: the caller must _not_ hold the graph mutex.
903238c84f7SMauro Carvalho Chehab */
__video_try_or_set_format(struct fimc_dev * fimc,struct v4l2_format * f,bool try,struct fimc_fmt ** inp_fmt,struct fimc_fmt ** out_fmt)904238c84f7SMauro Carvalho Chehab static int __video_try_or_set_format(struct fimc_dev *fimc,
905238c84f7SMauro Carvalho Chehab struct v4l2_format *f, bool try,
906238c84f7SMauro Carvalho Chehab struct fimc_fmt **inp_fmt,
907238c84f7SMauro Carvalho Chehab struct fimc_fmt **out_fmt)
908238c84f7SMauro Carvalho Chehab {
909238c84f7SMauro Carvalho Chehab struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
910238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
911238c84f7SMauro Carvalho Chehab struct exynos_video_entity *ve = &vc->ve;
912238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = vc->ctx;
913238c84f7SMauro Carvalho Chehab unsigned int width = 0, height = 0;
914238c84f7SMauro Carvalho Chehab int ret = 0;
915238c84f7SMauro Carvalho Chehab
916238c84f7SMauro Carvalho Chehab /* Pre-configure format at the camera input interface, for JPEG only */
917238c84f7SMauro Carvalho Chehab if (fimc_jpeg_fourcc(pix->pixelformat)) {
918238c84f7SMauro Carvalho Chehab fimc_capture_try_format(ctx, &pix->width, &pix->height,
919238c84f7SMauro Carvalho Chehab NULL, &pix->pixelformat,
920238c84f7SMauro Carvalho Chehab FIMC_SD_PAD_SINK_CAM);
921238c84f7SMauro Carvalho Chehab if (try) {
922238c84f7SMauro Carvalho Chehab width = pix->width;
923238c84f7SMauro Carvalho Chehab height = pix->height;
924238c84f7SMauro Carvalho Chehab } else {
925238c84f7SMauro Carvalho Chehab ctx->s_frame.f_width = pix->width;
926238c84f7SMauro Carvalho Chehab ctx->s_frame.f_height = pix->height;
927238c84f7SMauro Carvalho Chehab }
928238c84f7SMauro Carvalho Chehab }
929238c84f7SMauro Carvalho Chehab
930238c84f7SMauro Carvalho Chehab /* Try the format at the scaler and the DMA output */
931238c84f7SMauro Carvalho Chehab *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
932238c84f7SMauro Carvalho Chehab NULL, &pix->pixelformat,
933238c84f7SMauro Carvalho Chehab FIMC_SD_PAD_SOURCE);
934238c84f7SMauro Carvalho Chehab if (*out_fmt == NULL)
935238c84f7SMauro Carvalho Chehab return -EINVAL;
936238c84f7SMauro Carvalho Chehab
937238c84f7SMauro Carvalho Chehab /* Restore image width/height for JPEG (no resizing supported). */
938238c84f7SMauro Carvalho Chehab if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
939238c84f7SMauro Carvalho Chehab pix->width = width;
940238c84f7SMauro Carvalho Chehab pix->height = height;
941238c84f7SMauro Carvalho Chehab }
942238c84f7SMauro Carvalho Chehab
943238c84f7SMauro Carvalho Chehab /* Try to match format at the host and the sensor */
944238c84f7SMauro Carvalho Chehab if (!vc->user_subdev_api) {
945238c84f7SMauro Carvalho Chehab struct v4l2_mbus_framefmt mbus_fmt;
946238c84f7SMauro Carvalho Chehab struct v4l2_mbus_framefmt *mf;
947238c84f7SMauro Carvalho Chehab
948238c84f7SMauro Carvalho Chehab mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
949238c84f7SMauro Carvalho Chehab
950238c84f7SMauro Carvalho Chehab mf->code = (*out_fmt)->mbus_code;
951238c84f7SMauro Carvalho Chehab mf->width = pix->width;
952238c84f7SMauro Carvalho Chehab mf->height = pix->height;
953238c84f7SMauro Carvalho Chehab
954238c84f7SMauro Carvalho Chehab fimc_md_graph_lock(ve);
955238c84f7SMauro Carvalho Chehab ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
956238c84f7SMauro Carvalho Chehab fimc_md_graph_unlock(ve);
957238c84f7SMauro Carvalho Chehab
958238c84f7SMauro Carvalho Chehab if (ret < 0)
959238c84f7SMauro Carvalho Chehab return ret;
960238c84f7SMauro Carvalho Chehab
961238c84f7SMauro Carvalho Chehab pix->width = mf->width;
962238c84f7SMauro Carvalho Chehab pix->height = mf->height;
963238c84f7SMauro Carvalho Chehab }
964238c84f7SMauro Carvalho Chehab
965238c84f7SMauro Carvalho Chehab fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
966238c84f7SMauro Carvalho Chehab
967238c84f7SMauro Carvalho Chehab if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
968238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sensor;
969238c84f7SMauro Carvalho Chehab
970238c84f7SMauro Carvalho Chehab fimc_md_graph_lock(ve);
971238c84f7SMauro Carvalho Chehab
972238c84f7SMauro Carvalho Chehab sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
973238c84f7SMauro Carvalho Chehab if (sensor)
974238c84f7SMauro Carvalho Chehab fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
975238c84f7SMauro Carvalho Chehab (*out_fmt)->memplanes, try);
976238c84f7SMauro Carvalho Chehab else
977238c84f7SMauro Carvalho Chehab ret = -EPIPE;
978238c84f7SMauro Carvalho Chehab
979238c84f7SMauro Carvalho Chehab fimc_md_graph_unlock(ve);
980238c84f7SMauro Carvalho Chehab }
981238c84f7SMauro Carvalho Chehab
982238c84f7SMauro Carvalho Chehab return ret;
983238c84f7SMauro Carvalho Chehab }
984238c84f7SMauro Carvalho Chehab
fimc_cap_try_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)985238c84f7SMauro Carvalho Chehab static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
986238c84f7SMauro Carvalho Chehab struct v4l2_format *f)
987238c84f7SMauro Carvalho Chehab {
988238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
989238c84f7SMauro Carvalho Chehab struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
990238c84f7SMauro Carvalho Chehab
991238c84f7SMauro Carvalho Chehab return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
992238c84f7SMauro Carvalho Chehab }
993238c84f7SMauro Carvalho Chehab
fimc_capture_mark_jpeg_xfer(struct fimc_ctx * ctx,enum fimc_color_fmt color)994238c84f7SMauro Carvalho Chehab static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
995238c84f7SMauro Carvalho Chehab enum fimc_color_fmt color)
996238c84f7SMauro Carvalho Chehab {
997238c84f7SMauro Carvalho Chehab bool jpeg = fimc_fmt_is_user_defined(color);
998238c84f7SMauro Carvalho Chehab
999238c84f7SMauro Carvalho Chehab ctx->scaler.enabled = !jpeg;
1000238c84f7SMauro Carvalho Chehab fimc_ctrls_activate(ctx, !jpeg);
1001238c84f7SMauro Carvalho Chehab
1002238c84f7SMauro Carvalho Chehab if (jpeg)
1003238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1004238c84f7SMauro Carvalho Chehab else
1005238c84f7SMauro Carvalho Chehab clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1006238c84f7SMauro Carvalho Chehab }
1007238c84f7SMauro Carvalho Chehab
__fimc_capture_set_format(struct fimc_dev * fimc,struct v4l2_format * f)1008238c84f7SMauro Carvalho Chehab static int __fimc_capture_set_format(struct fimc_dev *fimc,
1009238c84f7SMauro Carvalho Chehab struct v4l2_format *f)
1010238c84f7SMauro Carvalho Chehab {
1011238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
1012238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = vc->ctx;
1013238c84f7SMauro Carvalho Chehab struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1014238c84f7SMauro Carvalho Chehab struct fimc_frame *ff = &ctx->d_frame;
1015238c84f7SMauro Carvalho Chehab struct fimc_fmt *inp_fmt = NULL;
1016238c84f7SMauro Carvalho Chehab int ret, i;
1017238c84f7SMauro Carvalho Chehab
1018238c84f7SMauro Carvalho Chehab if (vb2_is_busy(&fimc->vid_cap.vbq))
1019238c84f7SMauro Carvalho Chehab return -EBUSY;
1020238c84f7SMauro Carvalho Chehab
1021238c84f7SMauro Carvalho Chehab ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1022238c84f7SMauro Carvalho Chehab if (ret < 0)
1023238c84f7SMauro Carvalho Chehab return ret;
1024238c84f7SMauro Carvalho Chehab
1025238c84f7SMauro Carvalho Chehab /* Update RGB Alpha control state and value range */
1026238c84f7SMauro Carvalho Chehab fimc_alpha_ctrl_update(ctx);
1027238c84f7SMauro Carvalho Chehab
1028238c84f7SMauro Carvalho Chehab for (i = 0; i < ff->fmt->memplanes; i++) {
1029238c84f7SMauro Carvalho Chehab ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1030238c84f7SMauro Carvalho Chehab ff->payload[i] = pix->plane_fmt[i].sizeimage;
1031238c84f7SMauro Carvalho Chehab }
1032238c84f7SMauro Carvalho Chehab
1033238c84f7SMauro Carvalho Chehab set_frame_bounds(ff, pix->width, pix->height);
1034238c84f7SMauro Carvalho Chehab /* Reset the composition rectangle if not yet configured */
1035238c84f7SMauro Carvalho Chehab if (!(ctx->state & FIMC_COMPOSE))
1036238c84f7SMauro Carvalho Chehab set_frame_crop(ff, 0, 0, pix->width, pix->height);
1037238c84f7SMauro Carvalho Chehab
1038238c84f7SMauro Carvalho Chehab fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1039238c84f7SMauro Carvalho Chehab
1040238c84f7SMauro Carvalho Chehab /* Reset cropping and set format at the camera interface input */
1041238c84f7SMauro Carvalho Chehab if (!vc->user_subdev_api) {
1042238c84f7SMauro Carvalho Chehab ctx->s_frame.fmt = inp_fmt;
1043238c84f7SMauro Carvalho Chehab set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1044238c84f7SMauro Carvalho Chehab set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1045238c84f7SMauro Carvalho Chehab }
1046238c84f7SMauro Carvalho Chehab
1047238c84f7SMauro Carvalho Chehab return ret;
1048238c84f7SMauro Carvalho Chehab }
1049238c84f7SMauro Carvalho Chehab
fimc_cap_s_fmt_mplane(struct file * file,void * priv,struct v4l2_format * f)1050238c84f7SMauro Carvalho Chehab static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1051238c84f7SMauro Carvalho Chehab struct v4l2_format *f)
1052238c84f7SMauro Carvalho Chehab {
1053238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1054238c84f7SMauro Carvalho Chehab
1055238c84f7SMauro Carvalho Chehab return __fimc_capture_set_format(fimc, f);
1056238c84f7SMauro Carvalho Chehab }
1057238c84f7SMauro Carvalho Chehab
fimc_cap_enum_input(struct file * file,void * priv,struct v4l2_input * i)1058238c84f7SMauro Carvalho Chehab static int fimc_cap_enum_input(struct file *file, void *priv,
1059238c84f7SMauro Carvalho Chehab struct v4l2_input *i)
1060238c84f7SMauro Carvalho Chehab {
1061238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1062238c84f7SMauro Carvalho Chehab struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1063238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd;
1064238c84f7SMauro Carvalho Chehab
1065238c84f7SMauro Carvalho Chehab if (i->index != 0)
1066238c84f7SMauro Carvalho Chehab return -EINVAL;
1067238c84f7SMauro Carvalho Chehab
1068238c84f7SMauro Carvalho Chehab i->type = V4L2_INPUT_TYPE_CAMERA;
1069238c84f7SMauro Carvalho Chehab fimc_md_graph_lock(ve);
1070238c84f7SMauro Carvalho Chehab sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1071238c84f7SMauro Carvalho Chehab fimc_md_graph_unlock(ve);
1072238c84f7SMauro Carvalho Chehab
1073238c84f7SMauro Carvalho Chehab if (sd)
1074238c84f7SMauro Carvalho Chehab strscpy(i->name, sd->name, sizeof(i->name));
1075238c84f7SMauro Carvalho Chehab
1076238c84f7SMauro Carvalho Chehab return 0;
1077238c84f7SMauro Carvalho Chehab }
1078238c84f7SMauro Carvalho Chehab
fimc_cap_s_input(struct file * file,void * priv,unsigned int i)1079238c84f7SMauro Carvalho Chehab static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1080238c84f7SMauro Carvalho Chehab {
1081238c84f7SMauro Carvalho Chehab return i == 0 ? i : -EINVAL;
1082238c84f7SMauro Carvalho Chehab }
1083238c84f7SMauro Carvalho Chehab
fimc_cap_g_input(struct file * file,void * priv,unsigned int * i)1084238c84f7SMauro Carvalho Chehab static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1085238c84f7SMauro Carvalho Chehab {
1086238c84f7SMauro Carvalho Chehab *i = 0;
1087238c84f7SMauro Carvalho Chehab return 0;
1088238c84f7SMauro Carvalho Chehab }
1089238c84f7SMauro Carvalho Chehab
1090238c84f7SMauro Carvalho Chehab /**
1091238c84f7SMauro Carvalho Chehab * fimc_pipeline_validate - check for formats inconsistencies
1092238c84f7SMauro Carvalho Chehab * between source and sink pad of each link
1093238c84f7SMauro Carvalho Chehab * @fimc: the FIMC device this context applies to
1094238c84f7SMauro Carvalho Chehab *
1095238c84f7SMauro Carvalho Chehab * Return 0 if all formats match or -EPIPE otherwise.
1096238c84f7SMauro Carvalho Chehab */
fimc_pipeline_validate(struct fimc_dev * fimc)1097238c84f7SMauro Carvalho Chehab static int fimc_pipeline_validate(struct fimc_dev *fimc)
1098238c84f7SMauro Carvalho Chehab {
1099ecefa105SLaurent Pinchart struct v4l2_subdev_format sink_fmt = {
1100ecefa105SLaurent Pinchart .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1101ecefa105SLaurent Pinchart };
1102ecefa105SLaurent Pinchart struct v4l2_subdev_format src_fmt = {
1103ecefa105SLaurent Pinchart .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1104ecefa105SLaurent Pinchart };
1105238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
1106238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd = &vc->subdev;
1107238c84f7SMauro Carvalho Chehab struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
1108238c84f7SMauro Carvalho Chehab struct media_pad *sink_pad, *src_pad;
1109238c84f7SMauro Carvalho Chehab int i, ret;
1110238c84f7SMauro Carvalho Chehab
1111238c84f7SMauro Carvalho Chehab while (1) {
1112238c84f7SMauro Carvalho Chehab /*
1113238c84f7SMauro Carvalho Chehab * Find current entity sink pad and any remote sink pad linked
1114238c84f7SMauro Carvalho Chehab * to it. We stop if there is no sink pad in current entity or
1115238c84f7SMauro Carvalho Chehab * it is not linked to any other remote entity.
1116238c84f7SMauro Carvalho Chehab */
1117238c84f7SMauro Carvalho Chehab src_pad = NULL;
1118238c84f7SMauro Carvalho Chehab
1119238c84f7SMauro Carvalho Chehab for (i = 0; i < sd->entity.num_pads; i++) {
1120238c84f7SMauro Carvalho Chehab struct media_pad *p = &sd->entity.pads[i];
1121238c84f7SMauro Carvalho Chehab
1122238c84f7SMauro Carvalho Chehab if (p->flags & MEDIA_PAD_FL_SINK) {
1123238c84f7SMauro Carvalho Chehab sink_pad = p;
1124b2e44430SLaurent Pinchart src_pad = media_pad_remote_pad_first(sink_pad);
1125238c84f7SMauro Carvalho Chehab if (src_pad)
1126238c84f7SMauro Carvalho Chehab break;
1127238c84f7SMauro Carvalho Chehab }
1128238c84f7SMauro Carvalho Chehab }
1129238c84f7SMauro Carvalho Chehab
1130238c84f7SMauro Carvalho Chehab if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
1131238c84f7SMauro Carvalho Chehab break;
1132238c84f7SMauro Carvalho Chehab
1133238c84f7SMauro Carvalho Chehab /* Don't call FIMC subdev operation to avoid nested locking */
1134238c84f7SMauro Carvalho Chehab if (sd == &vc->subdev) {
1135238c84f7SMauro Carvalho Chehab struct fimc_frame *ff = &vc->ctx->s_frame;
1136238c84f7SMauro Carvalho Chehab sink_fmt.format.width = ff->f_width;
1137238c84f7SMauro Carvalho Chehab sink_fmt.format.height = ff->f_height;
1138238c84f7SMauro Carvalho Chehab sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1139238c84f7SMauro Carvalho Chehab } else {
1140238c84f7SMauro Carvalho Chehab sink_fmt.pad = sink_pad->index;
1141238c84f7SMauro Carvalho Chehab ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1142238c84f7SMauro Carvalho Chehab if (ret < 0 && ret != -ENOIOCTLCMD)
1143238c84f7SMauro Carvalho Chehab return -EPIPE;
1144238c84f7SMauro Carvalho Chehab }
1145238c84f7SMauro Carvalho Chehab
1146238c84f7SMauro Carvalho Chehab /* Retrieve format at the source pad */
1147238c84f7SMauro Carvalho Chehab sd = media_entity_to_v4l2_subdev(src_pad->entity);
1148238c84f7SMauro Carvalho Chehab src_fmt.pad = src_pad->index;
1149238c84f7SMauro Carvalho Chehab ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1150238c84f7SMauro Carvalho Chehab if (ret < 0 && ret != -ENOIOCTLCMD)
1151238c84f7SMauro Carvalho Chehab return -EPIPE;
1152238c84f7SMauro Carvalho Chehab
1153238c84f7SMauro Carvalho Chehab if (src_fmt.format.width != sink_fmt.format.width ||
1154238c84f7SMauro Carvalho Chehab src_fmt.format.height != sink_fmt.format.height ||
1155238c84f7SMauro Carvalho Chehab src_fmt.format.code != sink_fmt.format.code)
1156238c84f7SMauro Carvalho Chehab return -EPIPE;
1157238c84f7SMauro Carvalho Chehab
1158238c84f7SMauro Carvalho Chehab if (sd == p->subdevs[IDX_SENSOR] &&
1159238c84f7SMauro Carvalho Chehab fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1160238c84f7SMauro Carvalho Chehab struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1161238c84f7SMauro Carvalho Chehab struct fimc_frame *frame = &vc->ctx->d_frame;
1162238c84f7SMauro Carvalho Chehab unsigned int i;
1163238c84f7SMauro Carvalho Chehab
1164238c84f7SMauro Carvalho Chehab ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1165238c84f7SMauro Carvalho Chehab frame->fmt->memplanes,
1166238c84f7SMauro Carvalho Chehab false);
1167238c84f7SMauro Carvalho Chehab if (ret < 0)
1168238c84f7SMauro Carvalho Chehab return -EPIPE;
1169238c84f7SMauro Carvalho Chehab
1170238c84f7SMauro Carvalho Chehab for (i = 0; i < frame->fmt->memplanes; i++)
1171238c84f7SMauro Carvalho Chehab if (frame->payload[i] < plane_fmt[i].sizeimage)
1172238c84f7SMauro Carvalho Chehab return -EPIPE;
1173238c84f7SMauro Carvalho Chehab }
1174238c84f7SMauro Carvalho Chehab }
1175238c84f7SMauro Carvalho Chehab return 0;
1176238c84f7SMauro Carvalho Chehab }
1177238c84f7SMauro Carvalho Chehab
fimc_cap_streamon(struct file * file,void * priv,enum v4l2_buf_type type)1178238c84f7SMauro Carvalho Chehab static int fimc_cap_streamon(struct file *file, void *priv,
1179238c84f7SMauro Carvalho Chehab enum v4l2_buf_type type)
1180238c84f7SMauro Carvalho Chehab {
1181238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1182238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
1183238c84f7SMauro Carvalho Chehab struct fimc_source_info *si = NULL;
1184238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd;
1185238c84f7SMauro Carvalho Chehab int ret;
1186238c84f7SMauro Carvalho Chehab
1187238c84f7SMauro Carvalho Chehab if (fimc_capture_active(fimc))
1188238c84f7SMauro Carvalho Chehab return -EBUSY;
1189238c84f7SMauro Carvalho Chehab
119012cecbf9STomi Valkeinen ret = video_device_pipeline_start(&vc->ve.vdev, &vc->ve.pipe->mp);
1191238c84f7SMauro Carvalho Chehab if (ret < 0)
1192238c84f7SMauro Carvalho Chehab return ret;
1193238c84f7SMauro Carvalho Chehab
1194238c84f7SMauro Carvalho Chehab sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
1195238c84f7SMauro Carvalho Chehab if (sd)
1196238c84f7SMauro Carvalho Chehab si = v4l2_get_subdev_hostdata(sd);
1197238c84f7SMauro Carvalho Chehab
1198238c84f7SMauro Carvalho Chehab if (si == NULL) {
1199238c84f7SMauro Carvalho Chehab ret = -EPIPE;
1200238c84f7SMauro Carvalho Chehab goto err_p_stop;
1201238c84f7SMauro Carvalho Chehab }
1202238c84f7SMauro Carvalho Chehab /*
1203238c84f7SMauro Carvalho Chehab * Save configuration data related to currently attached image
1204238c84f7SMauro Carvalho Chehab * sensor or other data source, e.g. FIMC-IS.
1205238c84f7SMauro Carvalho Chehab */
1206238c84f7SMauro Carvalho Chehab vc->source_config = *si;
1207238c84f7SMauro Carvalho Chehab
1208238c84f7SMauro Carvalho Chehab if (vc->input == GRP_ID_FIMC_IS)
1209238c84f7SMauro Carvalho Chehab vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1210238c84f7SMauro Carvalho Chehab
1211238c84f7SMauro Carvalho Chehab if (vc->user_subdev_api) {
1212238c84f7SMauro Carvalho Chehab ret = fimc_pipeline_validate(fimc);
1213238c84f7SMauro Carvalho Chehab if (ret < 0)
1214238c84f7SMauro Carvalho Chehab goto err_p_stop;
1215238c84f7SMauro Carvalho Chehab }
1216238c84f7SMauro Carvalho Chehab
1217238c84f7SMauro Carvalho Chehab ret = vb2_ioctl_streamon(file, priv, type);
1218238c84f7SMauro Carvalho Chehab if (!ret) {
1219238c84f7SMauro Carvalho Chehab vc->streaming = true;
1220238c84f7SMauro Carvalho Chehab return ret;
1221238c84f7SMauro Carvalho Chehab }
1222238c84f7SMauro Carvalho Chehab
1223238c84f7SMauro Carvalho Chehab err_p_stop:
122412cecbf9STomi Valkeinen video_device_pipeline_stop(&vc->ve.vdev);
1225238c84f7SMauro Carvalho Chehab return ret;
1226238c84f7SMauro Carvalho Chehab }
1227238c84f7SMauro Carvalho Chehab
fimc_cap_streamoff(struct file * file,void * priv,enum v4l2_buf_type type)1228238c84f7SMauro Carvalho Chehab static int fimc_cap_streamoff(struct file *file, void *priv,
1229238c84f7SMauro Carvalho Chehab enum v4l2_buf_type type)
1230238c84f7SMauro Carvalho Chehab {
1231238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1232238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
1233238c84f7SMauro Carvalho Chehab int ret;
1234238c84f7SMauro Carvalho Chehab
1235238c84f7SMauro Carvalho Chehab ret = vb2_ioctl_streamoff(file, priv, type);
1236238c84f7SMauro Carvalho Chehab if (ret < 0)
1237238c84f7SMauro Carvalho Chehab return ret;
1238238c84f7SMauro Carvalho Chehab
1239238c84f7SMauro Carvalho Chehab if (vc->streaming) {
124012cecbf9STomi Valkeinen video_device_pipeline_stop(&vc->ve.vdev);
1241238c84f7SMauro Carvalho Chehab vc->streaming = false;
1242238c84f7SMauro Carvalho Chehab }
1243238c84f7SMauro Carvalho Chehab
1244238c84f7SMauro Carvalho Chehab return 0;
1245238c84f7SMauro Carvalho Chehab }
1246238c84f7SMauro Carvalho Chehab
fimc_cap_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * reqbufs)1247238c84f7SMauro Carvalho Chehab static int fimc_cap_reqbufs(struct file *file, void *priv,
1248238c84f7SMauro Carvalho Chehab struct v4l2_requestbuffers *reqbufs)
1249238c84f7SMauro Carvalho Chehab {
1250238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1251238c84f7SMauro Carvalho Chehab int ret;
1252238c84f7SMauro Carvalho Chehab
1253238c84f7SMauro Carvalho Chehab ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1254238c84f7SMauro Carvalho Chehab
1255238c84f7SMauro Carvalho Chehab if (!ret)
1256238c84f7SMauro Carvalho Chehab fimc->vid_cap.reqbufs_count = reqbufs->count;
1257238c84f7SMauro Carvalho Chehab
1258238c84f7SMauro Carvalho Chehab return ret;
1259238c84f7SMauro Carvalho Chehab }
1260238c84f7SMauro Carvalho Chehab
fimc_cap_g_selection(struct file * file,void * fh,struct v4l2_selection * s)1261238c84f7SMauro Carvalho Chehab static int fimc_cap_g_selection(struct file *file, void *fh,
1262238c84f7SMauro Carvalho Chehab struct v4l2_selection *s)
1263238c84f7SMauro Carvalho Chehab {
1264238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1265238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1266238c84f7SMauro Carvalho Chehab struct fimc_frame *f = &ctx->s_frame;
1267238c84f7SMauro Carvalho Chehab
1268238c84f7SMauro Carvalho Chehab if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1269238c84f7SMauro Carvalho Chehab return -EINVAL;
1270238c84f7SMauro Carvalho Chehab
1271238c84f7SMauro Carvalho Chehab switch (s->target) {
1272238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1273238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1274238c84f7SMauro Carvalho Chehab f = &ctx->d_frame;
1275238c84f7SMauro Carvalho Chehab fallthrough;
1276238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_CROP_BOUNDS:
1277238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_CROP_DEFAULT:
1278238c84f7SMauro Carvalho Chehab s->r.left = 0;
1279238c84f7SMauro Carvalho Chehab s->r.top = 0;
1280238c84f7SMauro Carvalho Chehab s->r.width = f->o_width;
1281238c84f7SMauro Carvalho Chehab s->r.height = f->o_height;
1282238c84f7SMauro Carvalho Chehab return 0;
1283238c84f7SMauro Carvalho Chehab
1284238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_COMPOSE:
1285238c84f7SMauro Carvalho Chehab f = &ctx->d_frame;
1286238c84f7SMauro Carvalho Chehab fallthrough;
1287238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_CROP:
1288238c84f7SMauro Carvalho Chehab s->r.left = f->offs_h;
1289238c84f7SMauro Carvalho Chehab s->r.top = f->offs_v;
1290238c84f7SMauro Carvalho Chehab s->r.width = f->width;
1291238c84f7SMauro Carvalho Chehab s->r.height = f->height;
1292238c84f7SMauro Carvalho Chehab return 0;
1293238c84f7SMauro Carvalho Chehab }
1294238c84f7SMauro Carvalho Chehab
1295238c84f7SMauro Carvalho Chehab return -EINVAL;
1296238c84f7SMauro Carvalho Chehab }
1297238c84f7SMauro Carvalho Chehab
fimc_cap_s_selection(struct file * file,void * fh,struct v4l2_selection * s)1298238c84f7SMauro Carvalho Chehab static int fimc_cap_s_selection(struct file *file, void *fh,
1299238c84f7SMauro Carvalho Chehab struct v4l2_selection *s)
1300238c84f7SMauro Carvalho Chehab {
1301238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = video_drvdata(file);
1302238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1303238c84f7SMauro Carvalho Chehab struct v4l2_rect rect = s->r;
1304238c84f7SMauro Carvalho Chehab struct fimc_frame *f;
1305238c84f7SMauro Carvalho Chehab unsigned long flags;
1306238c84f7SMauro Carvalho Chehab
1307238c84f7SMauro Carvalho Chehab if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1308238c84f7SMauro Carvalho Chehab return -EINVAL;
1309238c84f7SMauro Carvalho Chehab
1310238c84f7SMauro Carvalho Chehab if (s->target == V4L2_SEL_TGT_COMPOSE)
1311238c84f7SMauro Carvalho Chehab f = &ctx->d_frame;
1312238c84f7SMauro Carvalho Chehab else if (s->target == V4L2_SEL_TGT_CROP)
1313238c84f7SMauro Carvalho Chehab f = &ctx->s_frame;
1314238c84f7SMauro Carvalho Chehab else
1315238c84f7SMauro Carvalho Chehab return -EINVAL;
1316238c84f7SMauro Carvalho Chehab
1317238c84f7SMauro Carvalho Chehab fimc_capture_try_selection(ctx, &rect, s->target);
1318238c84f7SMauro Carvalho Chehab
1319238c84f7SMauro Carvalho Chehab if (s->flags & V4L2_SEL_FLAG_LE &&
1320238c84f7SMauro Carvalho Chehab !v4l2_rect_enclosed(&rect, &s->r))
1321238c84f7SMauro Carvalho Chehab return -ERANGE;
1322238c84f7SMauro Carvalho Chehab
1323238c84f7SMauro Carvalho Chehab if (s->flags & V4L2_SEL_FLAG_GE &&
1324238c84f7SMauro Carvalho Chehab !v4l2_rect_enclosed(&s->r, &rect))
1325238c84f7SMauro Carvalho Chehab return -ERANGE;
1326238c84f7SMauro Carvalho Chehab
1327238c84f7SMauro Carvalho Chehab s->r = rect;
1328238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, flags);
1329238c84f7SMauro Carvalho Chehab set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1330238c84f7SMauro Carvalho Chehab s->r.height);
1331238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
1332238c84f7SMauro Carvalho Chehab
1333238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1334238c84f7SMauro Carvalho Chehab return 0;
1335238c84f7SMauro Carvalho Chehab }
1336238c84f7SMauro Carvalho Chehab
1337238c84f7SMauro Carvalho Chehab static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1338238c84f7SMauro Carvalho Chehab .vidioc_querycap = fimc_cap_querycap,
1339238c84f7SMauro Carvalho Chehab
1340238c84f7SMauro Carvalho Chehab .vidioc_enum_fmt_vid_cap = fimc_cap_enum_fmt,
1341238c84f7SMauro Carvalho Chehab .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1342238c84f7SMauro Carvalho Chehab .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1343238c84f7SMauro Carvalho Chehab .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1344238c84f7SMauro Carvalho Chehab
1345238c84f7SMauro Carvalho Chehab .vidioc_reqbufs = fimc_cap_reqbufs,
1346238c84f7SMauro Carvalho Chehab .vidioc_querybuf = vb2_ioctl_querybuf,
1347238c84f7SMauro Carvalho Chehab .vidioc_qbuf = vb2_ioctl_qbuf,
1348238c84f7SMauro Carvalho Chehab .vidioc_dqbuf = vb2_ioctl_dqbuf,
1349238c84f7SMauro Carvalho Chehab .vidioc_expbuf = vb2_ioctl_expbuf,
1350238c84f7SMauro Carvalho Chehab .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1351238c84f7SMauro Carvalho Chehab .vidioc_create_bufs = vb2_ioctl_create_bufs,
1352238c84f7SMauro Carvalho Chehab
1353238c84f7SMauro Carvalho Chehab .vidioc_streamon = fimc_cap_streamon,
1354238c84f7SMauro Carvalho Chehab .vidioc_streamoff = fimc_cap_streamoff,
1355238c84f7SMauro Carvalho Chehab
1356238c84f7SMauro Carvalho Chehab .vidioc_g_selection = fimc_cap_g_selection,
1357238c84f7SMauro Carvalho Chehab .vidioc_s_selection = fimc_cap_s_selection,
1358238c84f7SMauro Carvalho Chehab
1359238c84f7SMauro Carvalho Chehab .vidioc_enum_input = fimc_cap_enum_input,
1360238c84f7SMauro Carvalho Chehab .vidioc_s_input = fimc_cap_s_input,
1361238c84f7SMauro Carvalho Chehab .vidioc_g_input = fimc_cap_g_input,
1362238c84f7SMauro Carvalho Chehab };
1363238c84f7SMauro Carvalho Chehab
1364238c84f7SMauro Carvalho Chehab /* Capture subdev media entity operations */
fimc_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)1365238c84f7SMauro Carvalho Chehab static int fimc_link_setup(struct media_entity *entity,
1366238c84f7SMauro Carvalho Chehab const struct media_pad *local,
1367238c84f7SMauro Carvalho Chehab const struct media_pad *remote, u32 flags)
1368238c84f7SMauro Carvalho Chehab {
1369238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1370238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1371238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
1372238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sensor;
1373238c84f7SMauro Carvalho Chehab
1374238c84f7SMauro Carvalho Chehab if (!is_media_entity_v4l2_subdev(remote->entity))
1375238c84f7SMauro Carvalho Chehab return -EINVAL;
1376238c84f7SMauro Carvalho Chehab
1377238c84f7SMauro Carvalho Chehab if (WARN_ON(fimc == NULL))
1378238c84f7SMauro Carvalho Chehab return 0;
1379238c84f7SMauro Carvalho Chehab
1380238c84f7SMauro Carvalho Chehab dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1381238c84f7SMauro Carvalho Chehab local->entity->name, remote->entity->name, flags,
1382238c84f7SMauro Carvalho Chehab fimc->vid_cap.input);
1383238c84f7SMauro Carvalho Chehab
1384238c84f7SMauro Carvalho Chehab if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1385238c84f7SMauro Carvalho Chehab fimc->vid_cap.input = 0;
1386238c84f7SMauro Carvalho Chehab return 0;
1387238c84f7SMauro Carvalho Chehab }
1388238c84f7SMauro Carvalho Chehab
1389238c84f7SMauro Carvalho Chehab if (vc->input != 0)
1390238c84f7SMauro Carvalho Chehab return -EBUSY;
1391238c84f7SMauro Carvalho Chehab
1392238c84f7SMauro Carvalho Chehab vc->input = sd->grp_id;
1393238c84f7SMauro Carvalho Chehab
1394238c84f7SMauro Carvalho Chehab if (vc->user_subdev_api)
1395238c84f7SMauro Carvalho Chehab return 0;
1396238c84f7SMauro Carvalho Chehab
1397238c84f7SMauro Carvalho Chehab /* Inherit V4L2 controls from the image sensor subdev. */
1398238c84f7SMauro Carvalho Chehab sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1399238c84f7SMauro Carvalho Chehab if (sensor == NULL)
1400238c84f7SMauro Carvalho Chehab return 0;
1401238c84f7SMauro Carvalho Chehab
1402238c84f7SMauro Carvalho Chehab return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1403238c84f7SMauro Carvalho Chehab sensor->ctrl_handler, NULL, true);
1404238c84f7SMauro Carvalho Chehab }
1405238c84f7SMauro Carvalho Chehab
1406238c84f7SMauro Carvalho Chehab static const struct media_entity_operations fimc_sd_media_ops = {
1407238c84f7SMauro Carvalho Chehab .link_setup = fimc_link_setup,
1408238c84f7SMauro Carvalho Chehab };
1409238c84f7SMauro Carvalho Chehab
1410238c84f7SMauro Carvalho Chehab /**
1411238c84f7SMauro Carvalho Chehab * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1412238c84f7SMauro Carvalho Chehab * @sd: pointer to a subdev generating the notification
1413238c84f7SMauro Carvalho Chehab * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1414238c84f7SMauro Carvalho Chehab * @arg: pointer to an u32 type integer that stores the frame payload value
1415238c84f7SMauro Carvalho Chehab *
1416238c84f7SMauro Carvalho Chehab * The End Of Frame notification sent by sensor subdev in its still capture
1417238c84f7SMauro Carvalho Chehab * mode. If there is only a single VSYNC generated by the sensor at the
1418238c84f7SMauro Carvalho Chehab * beginning of a frame transmission, FIMC does not issue the LastIrq
1419238c84f7SMauro Carvalho Chehab * (end of frame) interrupt. And this notification is used to complete the
1420238c84f7SMauro Carvalho Chehab * frame capture and returning a buffer to user-space. Subdev drivers should
1421238c84f7SMauro Carvalho Chehab * call this notification from their last 'End of frame capture' interrupt.
1422238c84f7SMauro Carvalho Chehab */
fimc_sensor_notify(struct v4l2_subdev * sd,unsigned int notification,void * arg)1423238c84f7SMauro Carvalho Chehab void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1424238c84f7SMauro Carvalho Chehab void *arg)
1425238c84f7SMauro Carvalho Chehab {
1426238c84f7SMauro Carvalho Chehab struct fimc_source_info *si;
1427238c84f7SMauro Carvalho Chehab struct fimc_vid_buffer *buf;
1428238c84f7SMauro Carvalho Chehab struct fimc_md *fmd;
1429238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc;
1430238c84f7SMauro Carvalho Chehab unsigned long flags;
1431238c84f7SMauro Carvalho Chehab
1432238c84f7SMauro Carvalho Chehab if (sd == NULL)
1433238c84f7SMauro Carvalho Chehab return;
1434238c84f7SMauro Carvalho Chehab
1435238c84f7SMauro Carvalho Chehab si = v4l2_get_subdev_hostdata(sd);
1436238c84f7SMauro Carvalho Chehab fmd = entity_to_fimc_mdev(&sd->entity);
1437238c84f7SMauro Carvalho Chehab
1438238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fmd->slock, flags);
1439238c84f7SMauro Carvalho Chehab
1440238c84f7SMauro Carvalho Chehab fimc = si ? source_to_sensor_info(si)->host : NULL;
1441238c84f7SMauro Carvalho Chehab
1442238c84f7SMauro Carvalho Chehab if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1443238c84f7SMauro Carvalho Chehab test_bit(ST_CAPT_PEND, &fimc->state)) {
1444238c84f7SMauro Carvalho Chehab unsigned long irq_flags;
1445238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, irq_flags);
1446238c84f7SMauro Carvalho Chehab if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1447238c84f7SMauro Carvalho Chehab buf = list_entry(fimc->vid_cap.active_buf_q.next,
1448238c84f7SMauro Carvalho Chehab struct fimc_vid_buffer, list);
1449238c84f7SMauro Carvalho Chehab vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1450238c84f7SMauro Carvalho Chehab *((u32 *)arg));
1451238c84f7SMauro Carvalho Chehab }
1452238c84f7SMauro Carvalho Chehab fimc_capture_irq_handler(fimc, 1);
1453238c84f7SMauro Carvalho Chehab fimc_deactivate_capture(fimc);
1454238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, irq_flags);
1455238c84f7SMauro Carvalho Chehab }
1456238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fmd->slock, flags);
1457238c84f7SMauro Carvalho Chehab }
1458238c84f7SMauro Carvalho Chehab
fimc_subdev_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1459238c84f7SMauro Carvalho Chehab static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1460238c84f7SMauro Carvalho Chehab struct v4l2_subdev_state *sd_state,
1461238c84f7SMauro Carvalho Chehab struct v4l2_subdev_mbus_code_enum *code)
1462238c84f7SMauro Carvalho Chehab {
1463238c84f7SMauro Carvalho Chehab struct fimc_fmt *fmt;
1464238c84f7SMauro Carvalho Chehab
1465238c84f7SMauro Carvalho Chehab fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1466238c84f7SMauro Carvalho Chehab if (!fmt)
1467238c84f7SMauro Carvalho Chehab return -EINVAL;
1468238c84f7SMauro Carvalho Chehab code->code = fmt->mbus_code;
1469238c84f7SMauro Carvalho Chehab return 0;
1470238c84f7SMauro Carvalho Chehab }
1471238c84f7SMauro Carvalho Chehab
fimc_subdev_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1472238c84f7SMauro Carvalho Chehab static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1473238c84f7SMauro Carvalho Chehab struct v4l2_subdev_state *sd_state,
1474238c84f7SMauro Carvalho Chehab struct v4l2_subdev_format *fmt)
1475238c84f7SMauro Carvalho Chehab {
1476238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1477238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1478238c84f7SMauro Carvalho Chehab struct fimc_frame *ff = &ctx->s_frame;
1479238c84f7SMauro Carvalho Chehab struct v4l2_mbus_framefmt *mf;
1480238c84f7SMauro Carvalho Chehab
1481238c84f7SMauro Carvalho Chehab if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1482238c84f7SMauro Carvalho Chehab mf = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
1483238c84f7SMauro Carvalho Chehab fmt->format = *mf;
1484238c84f7SMauro Carvalho Chehab return 0;
1485238c84f7SMauro Carvalho Chehab }
1486238c84f7SMauro Carvalho Chehab
1487238c84f7SMauro Carvalho Chehab mf = &fmt->format;
1488238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
1489238c84f7SMauro Carvalho Chehab
1490238c84f7SMauro Carvalho Chehab switch (fmt->pad) {
1491238c84f7SMauro Carvalho Chehab case FIMC_SD_PAD_SOURCE:
1492238c84f7SMauro Carvalho Chehab if (!WARN_ON(ff->fmt == NULL))
1493238c84f7SMauro Carvalho Chehab mf->code = ff->fmt->mbus_code;
1494238c84f7SMauro Carvalho Chehab /* Sink pads crop rectangle size */
1495238c84f7SMauro Carvalho Chehab mf->width = ff->width;
1496238c84f7SMauro Carvalho Chehab mf->height = ff->height;
1497238c84f7SMauro Carvalho Chehab break;
1498238c84f7SMauro Carvalho Chehab case FIMC_SD_PAD_SINK_FIFO:
1499238c84f7SMauro Carvalho Chehab *mf = fimc->vid_cap.wb_fmt;
1500238c84f7SMauro Carvalho Chehab break;
1501238c84f7SMauro Carvalho Chehab case FIMC_SD_PAD_SINK_CAM:
1502238c84f7SMauro Carvalho Chehab default:
1503238c84f7SMauro Carvalho Chehab *mf = fimc->vid_cap.ci_fmt;
1504238c84f7SMauro Carvalho Chehab break;
1505238c84f7SMauro Carvalho Chehab }
1506238c84f7SMauro Carvalho Chehab
1507238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1508238c84f7SMauro Carvalho Chehab mf->colorspace = V4L2_COLORSPACE_JPEG;
1509238c84f7SMauro Carvalho Chehab
1510238c84f7SMauro Carvalho Chehab return 0;
1511238c84f7SMauro Carvalho Chehab }
1512238c84f7SMauro Carvalho Chehab
fimc_subdev_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1513238c84f7SMauro Carvalho Chehab static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1514238c84f7SMauro Carvalho Chehab struct v4l2_subdev_state *sd_state,
1515238c84f7SMauro Carvalho Chehab struct v4l2_subdev_format *fmt)
1516238c84f7SMauro Carvalho Chehab {
1517238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1518238c84f7SMauro Carvalho Chehab struct v4l2_mbus_framefmt *mf = &fmt->format;
1519238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vc = &fimc->vid_cap;
1520238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = vc->ctx;
1521238c84f7SMauro Carvalho Chehab struct fimc_frame *ff;
1522238c84f7SMauro Carvalho Chehab struct fimc_fmt *ffmt;
1523238c84f7SMauro Carvalho Chehab
1524238c84f7SMauro Carvalho Chehab dbg("pad%d: code: 0x%x, %dx%d",
1525238c84f7SMauro Carvalho Chehab fmt->pad, mf->code, mf->width, mf->height);
1526238c84f7SMauro Carvalho Chehab
1527238c84f7SMauro Carvalho Chehab if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1528238c84f7SMauro Carvalho Chehab return -EBUSY;
1529238c84f7SMauro Carvalho Chehab
1530238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
1531238c84f7SMauro Carvalho Chehab ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1532238c84f7SMauro Carvalho Chehab &mf->code, NULL, fmt->pad);
1533238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1534238c84f7SMauro Carvalho Chehab mf->colorspace = V4L2_COLORSPACE_JPEG;
1535238c84f7SMauro Carvalho Chehab
1536238c84f7SMauro Carvalho Chehab if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1537238c84f7SMauro Carvalho Chehab mf = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
1538238c84f7SMauro Carvalho Chehab *mf = fmt->format;
1539238c84f7SMauro Carvalho Chehab return 0;
1540238c84f7SMauro Carvalho Chehab }
1541238c84f7SMauro Carvalho Chehab /* There must be a bug in the driver if this happens */
1542238c84f7SMauro Carvalho Chehab if (WARN_ON(ffmt == NULL))
1543238c84f7SMauro Carvalho Chehab return -EINVAL;
1544238c84f7SMauro Carvalho Chehab
1545238c84f7SMauro Carvalho Chehab /* Update RGB Alpha control state and value range */
1546238c84f7SMauro Carvalho Chehab fimc_alpha_ctrl_update(ctx);
1547238c84f7SMauro Carvalho Chehab
1548238c84f7SMauro Carvalho Chehab fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1549238c84f7SMauro Carvalho Chehab if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1550238c84f7SMauro Carvalho Chehab ff = &ctx->d_frame;
1551238c84f7SMauro Carvalho Chehab /* Sink pads crop rectangle size */
1552238c84f7SMauro Carvalho Chehab mf->width = ctx->s_frame.width;
1553238c84f7SMauro Carvalho Chehab mf->height = ctx->s_frame.height;
1554238c84f7SMauro Carvalho Chehab } else {
1555238c84f7SMauro Carvalho Chehab ff = &ctx->s_frame;
1556238c84f7SMauro Carvalho Chehab }
1557238c84f7SMauro Carvalho Chehab
1558238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
1559238c84f7SMauro Carvalho Chehab set_frame_bounds(ff, mf->width, mf->height);
1560238c84f7SMauro Carvalho Chehab
1561238c84f7SMauro Carvalho Chehab if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1562238c84f7SMauro Carvalho Chehab vc->wb_fmt = *mf;
1563238c84f7SMauro Carvalho Chehab else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1564238c84f7SMauro Carvalho Chehab vc->ci_fmt = *mf;
1565238c84f7SMauro Carvalho Chehab
1566238c84f7SMauro Carvalho Chehab ff->fmt = ffmt;
1567238c84f7SMauro Carvalho Chehab
1568238c84f7SMauro Carvalho Chehab /* Reset the crop rectangle if required. */
1569238c84f7SMauro Carvalho Chehab if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1570238c84f7SMauro Carvalho Chehab set_frame_crop(ff, 0, 0, mf->width, mf->height);
1571238c84f7SMauro Carvalho Chehab
1572238c84f7SMauro Carvalho Chehab if (fmt->pad != FIMC_SD_PAD_SOURCE)
1573238c84f7SMauro Carvalho Chehab ctx->state &= ~FIMC_COMPOSE;
1574238c84f7SMauro Carvalho Chehab
1575238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1576238c84f7SMauro Carvalho Chehab return 0;
1577238c84f7SMauro Carvalho Chehab }
1578238c84f7SMauro Carvalho Chehab
fimc_subdev_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1579238c84f7SMauro Carvalho Chehab static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1580238c84f7SMauro Carvalho Chehab struct v4l2_subdev_state *sd_state,
1581238c84f7SMauro Carvalho Chehab struct v4l2_subdev_selection *sel)
1582238c84f7SMauro Carvalho Chehab {
1583238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1584238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1585238c84f7SMauro Carvalho Chehab struct fimc_frame *f = &ctx->s_frame;
1586238c84f7SMauro Carvalho Chehab struct v4l2_rect *r = &sel->r;
1587238c84f7SMauro Carvalho Chehab struct v4l2_rect *try_sel;
1588238c84f7SMauro Carvalho Chehab
1589238c84f7SMauro Carvalho Chehab if (sel->pad == FIMC_SD_PAD_SOURCE)
1590238c84f7SMauro Carvalho Chehab return -EINVAL;
1591238c84f7SMauro Carvalho Chehab
1592238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
1593238c84f7SMauro Carvalho Chehab
1594238c84f7SMauro Carvalho Chehab switch (sel->target) {
1595238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1596238c84f7SMauro Carvalho Chehab f = &ctx->d_frame;
1597238c84f7SMauro Carvalho Chehab fallthrough;
1598238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_CROP_BOUNDS:
1599238c84f7SMauro Carvalho Chehab r->width = f->o_width;
1600238c84f7SMauro Carvalho Chehab r->height = f->o_height;
1601238c84f7SMauro Carvalho Chehab r->left = 0;
1602238c84f7SMauro Carvalho Chehab r->top = 0;
1603238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1604238c84f7SMauro Carvalho Chehab return 0;
1605238c84f7SMauro Carvalho Chehab
1606238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_CROP:
1607238c84f7SMauro Carvalho Chehab try_sel = v4l2_subdev_get_try_crop(sd, sd_state, sel->pad);
1608238c84f7SMauro Carvalho Chehab break;
1609238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_COMPOSE:
1610238c84f7SMauro Carvalho Chehab try_sel = v4l2_subdev_get_try_compose(sd, sd_state, sel->pad);
1611238c84f7SMauro Carvalho Chehab f = &ctx->d_frame;
1612238c84f7SMauro Carvalho Chehab break;
1613238c84f7SMauro Carvalho Chehab default:
1614238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1615238c84f7SMauro Carvalho Chehab return -EINVAL;
1616238c84f7SMauro Carvalho Chehab }
1617238c84f7SMauro Carvalho Chehab
1618238c84f7SMauro Carvalho Chehab if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1619238c84f7SMauro Carvalho Chehab sel->r = *try_sel;
1620238c84f7SMauro Carvalho Chehab } else {
1621238c84f7SMauro Carvalho Chehab r->left = f->offs_h;
1622238c84f7SMauro Carvalho Chehab r->top = f->offs_v;
1623238c84f7SMauro Carvalho Chehab r->width = f->width;
1624238c84f7SMauro Carvalho Chehab r->height = f->height;
1625238c84f7SMauro Carvalho Chehab }
1626238c84f7SMauro Carvalho Chehab
1627238c84f7SMauro Carvalho Chehab dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1628238c84f7SMauro Carvalho Chehab sel->pad, r->left, r->top, r->width, r->height,
1629238c84f7SMauro Carvalho Chehab f->f_width, f->f_height);
1630238c84f7SMauro Carvalho Chehab
1631238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1632238c84f7SMauro Carvalho Chehab return 0;
1633238c84f7SMauro Carvalho Chehab }
1634238c84f7SMauro Carvalho Chehab
fimc_subdev_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1635238c84f7SMauro Carvalho Chehab static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1636238c84f7SMauro Carvalho Chehab struct v4l2_subdev_state *sd_state,
1637238c84f7SMauro Carvalho Chehab struct v4l2_subdev_selection *sel)
1638238c84f7SMauro Carvalho Chehab {
1639238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1640238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1641238c84f7SMauro Carvalho Chehab struct fimc_frame *f = &ctx->s_frame;
1642238c84f7SMauro Carvalho Chehab struct v4l2_rect *r = &sel->r;
1643238c84f7SMauro Carvalho Chehab struct v4l2_rect *try_sel;
1644238c84f7SMauro Carvalho Chehab unsigned long flags;
1645238c84f7SMauro Carvalho Chehab
1646238c84f7SMauro Carvalho Chehab if (sel->pad == FIMC_SD_PAD_SOURCE)
1647238c84f7SMauro Carvalho Chehab return -EINVAL;
1648238c84f7SMauro Carvalho Chehab
1649238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
1650238c84f7SMauro Carvalho Chehab fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1651238c84f7SMauro Carvalho Chehab
1652238c84f7SMauro Carvalho Chehab switch (sel->target) {
1653238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_CROP:
1654238c84f7SMauro Carvalho Chehab try_sel = v4l2_subdev_get_try_crop(sd, sd_state, sel->pad);
1655238c84f7SMauro Carvalho Chehab break;
1656238c84f7SMauro Carvalho Chehab case V4L2_SEL_TGT_COMPOSE:
1657238c84f7SMauro Carvalho Chehab try_sel = v4l2_subdev_get_try_compose(sd, sd_state, sel->pad);
1658238c84f7SMauro Carvalho Chehab f = &ctx->d_frame;
1659238c84f7SMauro Carvalho Chehab break;
1660238c84f7SMauro Carvalho Chehab default:
1661238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1662238c84f7SMauro Carvalho Chehab return -EINVAL;
1663238c84f7SMauro Carvalho Chehab }
1664238c84f7SMauro Carvalho Chehab
1665238c84f7SMauro Carvalho Chehab if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1666238c84f7SMauro Carvalho Chehab *try_sel = sel->r;
1667238c84f7SMauro Carvalho Chehab } else {
1668238c84f7SMauro Carvalho Chehab spin_lock_irqsave(&fimc->slock, flags);
1669238c84f7SMauro Carvalho Chehab set_frame_crop(f, r->left, r->top, r->width, r->height);
1670238c84f7SMauro Carvalho Chehab set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1671238c84f7SMauro Carvalho Chehab if (sel->target == V4L2_SEL_TGT_COMPOSE)
1672238c84f7SMauro Carvalho Chehab ctx->state |= FIMC_COMPOSE;
1673238c84f7SMauro Carvalho Chehab spin_unlock_irqrestore(&fimc->slock, flags);
1674238c84f7SMauro Carvalho Chehab }
1675238c84f7SMauro Carvalho Chehab
1676238c84f7SMauro Carvalho Chehab dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1677238c84f7SMauro Carvalho Chehab r->width, r->height);
1678238c84f7SMauro Carvalho Chehab
1679238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1680238c84f7SMauro Carvalho Chehab return 0;
1681238c84f7SMauro Carvalho Chehab }
1682238c84f7SMauro Carvalho Chehab
1683238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1684238c84f7SMauro Carvalho Chehab .enum_mbus_code = fimc_subdev_enum_mbus_code,
1685238c84f7SMauro Carvalho Chehab .get_selection = fimc_subdev_get_selection,
1686238c84f7SMauro Carvalho Chehab .set_selection = fimc_subdev_set_selection,
1687238c84f7SMauro Carvalho Chehab .get_fmt = fimc_subdev_get_fmt,
1688238c84f7SMauro Carvalho Chehab .set_fmt = fimc_subdev_set_fmt,
1689238c84f7SMauro Carvalho Chehab };
1690238c84f7SMauro Carvalho Chehab
1691238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_ops fimc_subdev_ops = {
1692238c84f7SMauro Carvalho Chehab .pad = &fimc_subdev_pad_ops,
1693238c84f7SMauro Carvalho Chehab };
1694238c84f7SMauro Carvalho Chehab
1695238c84f7SMauro Carvalho Chehab /* Set default format at the sensor and host interface */
fimc_capture_set_default_format(struct fimc_dev * fimc)1696238c84f7SMauro Carvalho Chehab static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1697238c84f7SMauro Carvalho Chehab {
1698238c84f7SMauro Carvalho Chehab struct v4l2_format fmt = {
1699238c84f7SMauro Carvalho Chehab .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1700238c84f7SMauro Carvalho Chehab .fmt.pix_mp = {
1701238c84f7SMauro Carvalho Chehab .width = FIMC_DEFAULT_WIDTH,
1702238c84f7SMauro Carvalho Chehab .height = FIMC_DEFAULT_HEIGHT,
1703238c84f7SMauro Carvalho Chehab .pixelformat = V4L2_PIX_FMT_YUYV,
1704238c84f7SMauro Carvalho Chehab .field = V4L2_FIELD_NONE,
1705238c84f7SMauro Carvalho Chehab .colorspace = V4L2_COLORSPACE_JPEG,
1706238c84f7SMauro Carvalho Chehab },
1707238c84f7SMauro Carvalho Chehab };
1708238c84f7SMauro Carvalho Chehab
1709238c84f7SMauro Carvalho Chehab return __fimc_capture_set_format(fimc, &fmt);
1710238c84f7SMauro Carvalho Chehab }
1711238c84f7SMauro Carvalho Chehab
1712238c84f7SMauro Carvalho Chehab /* fimc->lock must be already initialized */
fimc_register_capture_device(struct fimc_dev * fimc,struct v4l2_device * v4l2_dev)1713238c84f7SMauro Carvalho Chehab static int fimc_register_capture_device(struct fimc_dev *fimc,
1714238c84f7SMauro Carvalho Chehab struct v4l2_device *v4l2_dev)
1715238c84f7SMauro Carvalho Chehab {
1716238c84f7SMauro Carvalho Chehab struct video_device *vfd = &fimc->vid_cap.ve.vdev;
1717238c84f7SMauro Carvalho Chehab struct vb2_queue *q = &fimc->vid_cap.vbq;
1718238c84f7SMauro Carvalho Chehab struct fimc_ctx *ctx;
1719238c84f7SMauro Carvalho Chehab struct fimc_vid_cap *vid_cap;
1720238c84f7SMauro Carvalho Chehab struct fimc_fmt *fmt;
1721238c84f7SMauro Carvalho Chehab int ret = -ENOMEM;
1722238c84f7SMauro Carvalho Chehab
1723238c84f7SMauro Carvalho Chehab ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1724238c84f7SMauro Carvalho Chehab if (!ctx)
1725238c84f7SMauro Carvalho Chehab return -ENOMEM;
1726238c84f7SMauro Carvalho Chehab
1727238c84f7SMauro Carvalho Chehab ctx->fimc_dev = fimc;
1728238c84f7SMauro Carvalho Chehab ctx->in_path = FIMC_IO_CAMERA;
1729238c84f7SMauro Carvalho Chehab ctx->out_path = FIMC_IO_DMA;
1730238c84f7SMauro Carvalho Chehab ctx->state = FIMC_CTX_CAP;
1731238c84f7SMauro Carvalho Chehab ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1732238c84f7SMauro Carvalho Chehab ctx->d_frame.fmt = ctx->s_frame.fmt;
1733238c84f7SMauro Carvalho Chehab
1734238c84f7SMauro Carvalho Chehab memset(vfd, 0, sizeof(*vfd));
1735238c84f7SMauro Carvalho Chehab snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1736238c84f7SMauro Carvalho Chehab
1737238c84f7SMauro Carvalho Chehab vfd->fops = &fimc_capture_fops;
1738238c84f7SMauro Carvalho Chehab vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1739238c84f7SMauro Carvalho Chehab vfd->v4l2_dev = v4l2_dev;
1740238c84f7SMauro Carvalho Chehab vfd->minor = -1;
1741238c84f7SMauro Carvalho Chehab vfd->release = video_device_release_empty;
1742238c84f7SMauro Carvalho Chehab vfd->queue = q;
1743238c84f7SMauro Carvalho Chehab vfd->lock = &fimc->lock;
1744238c84f7SMauro Carvalho Chehab vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1745238c84f7SMauro Carvalho Chehab
1746238c84f7SMauro Carvalho Chehab video_set_drvdata(vfd, fimc);
1747238c84f7SMauro Carvalho Chehab vid_cap = &fimc->vid_cap;
1748238c84f7SMauro Carvalho Chehab vid_cap->active_buf_cnt = 0;
1749238c84f7SMauro Carvalho Chehab vid_cap->reqbufs_count = 0;
1750238c84f7SMauro Carvalho Chehab vid_cap->ctx = ctx;
1751238c84f7SMauro Carvalho Chehab
1752238c84f7SMauro Carvalho Chehab INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1753238c84f7SMauro Carvalho Chehab INIT_LIST_HEAD(&vid_cap->active_buf_q);
1754238c84f7SMauro Carvalho Chehab
1755238c84f7SMauro Carvalho Chehab memset(q, 0, sizeof(*q));
1756238c84f7SMauro Carvalho Chehab q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1757238c84f7SMauro Carvalho Chehab q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1758238c84f7SMauro Carvalho Chehab q->drv_priv = ctx;
1759238c84f7SMauro Carvalho Chehab q->ops = &fimc_capture_qops;
1760238c84f7SMauro Carvalho Chehab q->mem_ops = &vb2_dma_contig_memops;
1761238c84f7SMauro Carvalho Chehab q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1762238c84f7SMauro Carvalho Chehab q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1763238c84f7SMauro Carvalho Chehab q->lock = &fimc->lock;
1764238c84f7SMauro Carvalho Chehab q->dev = &fimc->pdev->dev;
1765238c84f7SMauro Carvalho Chehab
1766238c84f7SMauro Carvalho Chehab ret = vb2_queue_init(q);
1767238c84f7SMauro Carvalho Chehab if (ret)
1768238c84f7SMauro Carvalho Chehab goto err_free_ctx;
1769238c84f7SMauro Carvalho Chehab
1770238c84f7SMauro Carvalho Chehab /* Default format configuration */
1771238c84f7SMauro Carvalho Chehab fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1772238c84f7SMauro Carvalho Chehab vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1773238c84f7SMauro Carvalho Chehab vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1774238c84f7SMauro Carvalho Chehab vid_cap->ci_fmt.code = fmt->mbus_code;
1775238c84f7SMauro Carvalho Chehab
1776238c84f7SMauro Carvalho Chehab ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1777238c84f7SMauro Carvalho Chehab ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1778238c84f7SMauro Carvalho Chehab ctx->s_frame.fmt = fmt;
1779238c84f7SMauro Carvalho Chehab
1780238c84f7SMauro Carvalho Chehab fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1781238c84f7SMauro Carvalho Chehab vid_cap->wb_fmt = vid_cap->ci_fmt;
1782238c84f7SMauro Carvalho Chehab vid_cap->wb_fmt.code = fmt->mbus_code;
1783238c84f7SMauro Carvalho Chehab
1784238c84f7SMauro Carvalho Chehab vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1785238c84f7SMauro Carvalho Chehab vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1786238c84f7SMauro Carvalho Chehab ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
1787238c84f7SMauro Carvalho Chehab if (ret)
1788238c84f7SMauro Carvalho Chehab goto err_free_ctx;
1789238c84f7SMauro Carvalho Chehab
1790238c84f7SMauro Carvalho Chehab ret = fimc_ctrls_create(ctx);
1791238c84f7SMauro Carvalho Chehab if (ret)
1792238c84f7SMauro Carvalho Chehab goto err_me_cleanup;
1793238c84f7SMauro Carvalho Chehab
1794238c84f7SMauro Carvalho Chehab ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
1795238c84f7SMauro Carvalho Chehab if (ret)
1796238c84f7SMauro Carvalho Chehab goto err_ctrl_free;
1797238c84f7SMauro Carvalho Chehab
1798238c84f7SMauro Carvalho Chehab v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1799238c84f7SMauro Carvalho Chehab vfd->name, video_device_node_name(vfd));
1800238c84f7SMauro Carvalho Chehab
1801238c84f7SMauro Carvalho Chehab vfd->ctrl_handler = &ctx->ctrls.handler;
1802238c84f7SMauro Carvalho Chehab return 0;
1803238c84f7SMauro Carvalho Chehab
1804238c84f7SMauro Carvalho Chehab err_ctrl_free:
1805238c84f7SMauro Carvalho Chehab fimc_ctrls_delete(ctx);
1806238c84f7SMauro Carvalho Chehab err_me_cleanup:
1807238c84f7SMauro Carvalho Chehab media_entity_cleanup(&vfd->entity);
1808238c84f7SMauro Carvalho Chehab err_free_ctx:
1809238c84f7SMauro Carvalho Chehab kfree(ctx);
1810238c84f7SMauro Carvalho Chehab return ret;
1811238c84f7SMauro Carvalho Chehab }
1812238c84f7SMauro Carvalho Chehab
fimc_capture_subdev_registered(struct v4l2_subdev * sd)1813238c84f7SMauro Carvalho Chehab static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1814238c84f7SMauro Carvalho Chehab {
1815238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1816238c84f7SMauro Carvalho Chehab int ret;
1817238c84f7SMauro Carvalho Chehab
1818238c84f7SMauro Carvalho Chehab if (fimc == NULL)
1819238c84f7SMauro Carvalho Chehab return -ENXIO;
1820238c84f7SMauro Carvalho Chehab
1821238c84f7SMauro Carvalho Chehab ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1822238c84f7SMauro Carvalho Chehab if (ret)
1823238c84f7SMauro Carvalho Chehab return ret;
1824238c84f7SMauro Carvalho Chehab
1825238c84f7SMauro Carvalho Chehab fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
1826238c84f7SMauro Carvalho Chehab
1827238c84f7SMauro Carvalho Chehab ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1828238c84f7SMauro Carvalho Chehab if (ret) {
1829238c84f7SMauro Carvalho Chehab fimc_unregister_m2m_device(fimc);
1830238c84f7SMauro Carvalho Chehab fimc->vid_cap.ve.pipe = NULL;
1831238c84f7SMauro Carvalho Chehab }
1832238c84f7SMauro Carvalho Chehab
1833238c84f7SMauro Carvalho Chehab return ret;
1834238c84f7SMauro Carvalho Chehab }
1835238c84f7SMauro Carvalho Chehab
fimc_capture_subdev_unregistered(struct v4l2_subdev * sd)1836238c84f7SMauro Carvalho Chehab static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1837238c84f7SMauro Carvalho Chehab {
1838238c84f7SMauro Carvalho Chehab struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1839238c84f7SMauro Carvalho Chehab struct video_device *vdev;
1840238c84f7SMauro Carvalho Chehab
1841238c84f7SMauro Carvalho Chehab if (fimc == NULL)
1842238c84f7SMauro Carvalho Chehab return;
1843238c84f7SMauro Carvalho Chehab
1844238c84f7SMauro Carvalho Chehab mutex_lock(&fimc->lock);
1845238c84f7SMauro Carvalho Chehab
1846238c84f7SMauro Carvalho Chehab fimc_unregister_m2m_device(fimc);
1847238c84f7SMauro Carvalho Chehab vdev = &fimc->vid_cap.ve.vdev;
1848238c84f7SMauro Carvalho Chehab
1849238c84f7SMauro Carvalho Chehab if (video_is_registered(vdev)) {
1850238c84f7SMauro Carvalho Chehab video_unregister_device(vdev);
1851238c84f7SMauro Carvalho Chehab media_entity_cleanup(&vdev->entity);
1852238c84f7SMauro Carvalho Chehab fimc_ctrls_delete(fimc->vid_cap.ctx);
1853238c84f7SMauro Carvalho Chehab fimc->vid_cap.ve.pipe = NULL;
1854238c84f7SMauro Carvalho Chehab }
1855238c84f7SMauro Carvalho Chehab kfree(fimc->vid_cap.ctx);
1856238c84f7SMauro Carvalho Chehab fimc->vid_cap.ctx = NULL;
1857238c84f7SMauro Carvalho Chehab
1858238c84f7SMauro Carvalho Chehab mutex_unlock(&fimc->lock);
1859238c84f7SMauro Carvalho Chehab }
1860238c84f7SMauro Carvalho Chehab
1861238c84f7SMauro Carvalho Chehab static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1862238c84f7SMauro Carvalho Chehab .registered = fimc_capture_subdev_registered,
1863238c84f7SMauro Carvalho Chehab .unregistered = fimc_capture_subdev_unregistered,
1864238c84f7SMauro Carvalho Chehab };
1865238c84f7SMauro Carvalho Chehab
fimc_initialize_capture_subdev(struct fimc_dev * fimc)1866238c84f7SMauro Carvalho Chehab int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1867238c84f7SMauro Carvalho Chehab {
1868238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1869238c84f7SMauro Carvalho Chehab int ret;
1870238c84f7SMauro Carvalho Chehab
1871238c84f7SMauro Carvalho Chehab v4l2_subdev_init(sd, &fimc_subdev_ops);
1872238c84f7SMauro Carvalho Chehab sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1873238c84f7SMauro Carvalho Chehab snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1874238c84f7SMauro Carvalho Chehab
1875238c84f7SMauro Carvalho Chehab fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1876238c84f7SMauro Carvalho Chehab fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1877238c84f7SMauro Carvalho Chehab fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1878238c84f7SMauro Carvalho Chehab ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
1879238c84f7SMauro Carvalho Chehab fimc->vid_cap.sd_pads);
1880238c84f7SMauro Carvalho Chehab if (ret)
1881238c84f7SMauro Carvalho Chehab return ret;
1882238c84f7SMauro Carvalho Chehab
1883238c84f7SMauro Carvalho Chehab sd->entity.ops = &fimc_sd_media_ops;
1884238c84f7SMauro Carvalho Chehab sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1885238c84f7SMauro Carvalho Chehab sd->internal_ops = &fimc_capture_sd_internal_ops;
1886238c84f7SMauro Carvalho Chehab v4l2_set_subdevdata(sd, fimc);
1887238c84f7SMauro Carvalho Chehab return 0;
1888238c84f7SMauro Carvalho Chehab }
1889238c84f7SMauro Carvalho Chehab
fimc_unregister_capture_subdev(struct fimc_dev * fimc)1890238c84f7SMauro Carvalho Chehab void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1891238c84f7SMauro Carvalho Chehab {
1892238c84f7SMauro Carvalho Chehab struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1893238c84f7SMauro Carvalho Chehab
1894238c84f7SMauro Carvalho Chehab v4l2_device_unregister_subdev(sd);
1895238c84f7SMauro Carvalho Chehab media_entity_cleanup(&sd->entity);
1896238c84f7SMauro Carvalho Chehab v4l2_set_subdevdata(sd, NULL);
1897238c84f7SMauro Carvalho Chehab }
1898