blob: 9dbf0185a86d30ef9706ea779f7e47089f358d52 [file] [log] [blame]
Sergio Aguirre69c536b2011-01-24 15:48:19 -03001/*
2 * TI OMAP4 ISS V4L2 Driver - ISP RESIZER module
3 *
4 * Copyright (C) 2012 Texas Instruments, Inc.
5 *
6 * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/module.h>
15#include <linux/uaccess.h>
16#include <linux/delay.h>
17#include <linux/device.h>
18#include <linux/dma-mapping.h>
19#include <linux/mm.h>
20#include <linux/sched.h>
21
22#include "iss.h"
23#include "iss_regs.h"
24#include "iss_resizer.h"
25
Sergio Aguirre69c536b2011-01-24 15:48:19 -030026static const unsigned int resizer_fmts[] = {
27 V4L2_MBUS_FMT_UYVY8_1X16,
28 V4L2_MBUS_FMT_YUYV8_1X16,
29};
30
31/*
32 * resizer_print_status - Print current RESIZER Module register values.
33 * @resizer: Pointer to ISS ISP RESIZER device.
34 *
35 * Also prints other debug information stored in the RESIZER module.
36 */
37#define RSZ_PRINT_REGISTER(iss, name)\
38 dev_dbg(iss->dev, "###RSZ " #name "=0x%08x\n", \
Laurent Pinchart11abbfd2013-08-30 22:23:17 -030039 iss_reg_read(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_##name))
Sergio Aguirre69c536b2011-01-24 15:48:19 -030040
41#define RZA_PRINT_REGISTER(iss, name)\
42 dev_dbg(iss->dev, "###RZA " #name "=0x%08x\n", \
Laurent Pinchart11abbfd2013-08-30 22:23:17 -030043 iss_reg_read(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_##name))
Sergio Aguirre69c536b2011-01-24 15:48:19 -030044
45static void resizer_print_status(struct iss_resizer_device *resizer)
46{
47 struct iss_device *iss = to_iss_device(resizer);
48
49 dev_dbg(iss->dev, "-------------RESIZER Register dump-------------\n");
50
51 RSZ_PRINT_REGISTER(iss, SYSCONFIG);
52 RSZ_PRINT_REGISTER(iss, IN_FIFO_CTRL);
53 RSZ_PRINT_REGISTER(iss, FRACDIV);
54 RSZ_PRINT_REGISTER(iss, SRC_EN);
55 RSZ_PRINT_REGISTER(iss, SRC_MODE);
56 RSZ_PRINT_REGISTER(iss, SRC_FMT0);
57 RSZ_PRINT_REGISTER(iss, SRC_FMT1);
58 RSZ_PRINT_REGISTER(iss, SRC_VPS);
59 RSZ_PRINT_REGISTER(iss, SRC_VSZ);
60 RSZ_PRINT_REGISTER(iss, SRC_HPS);
61 RSZ_PRINT_REGISTER(iss, SRC_HSZ);
62 RSZ_PRINT_REGISTER(iss, DMA_RZA);
63 RSZ_PRINT_REGISTER(iss, DMA_RZB);
64 RSZ_PRINT_REGISTER(iss, DMA_STA);
65 RSZ_PRINT_REGISTER(iss, GCK_MMR);
66 RSZ_PRINT_REGISTER(iss, GCK_SDR);
67 RSZ_PRINT_REGISTER(iss, IRQ_RZA);
68 RSZ_PRINT_REGISTER(iss, IRQ_RZB);
69 RSZ_PRINT_REGISTER(iss, YUV_Y_MIN);
70 RSZ_PRINT_REGISTER(iss, YUV_Y_MAX);
71 RSZ_PRINT_REGISTER(iss, YUV_C_MIN);
72 RSZ_PRINT_REGISTER(iss, YUV_C_MAX);
73 RSZ_PRINT_REGISTER(iss, SEQ);
74
75 RZA_PRINT_REGISTER(iss, EN);
76 RZA_PRINT_REGISTER(iss, MODE);
77 RZA_PRINT_REGISTER(iss, 420);
78 RZA_PRINT_REGISTER(iss, I_VPS);
79 RZA_PRINT_REGISTER(iss, I_HPS);
80 RZA_PRINT_REGISTER(iss, O_VSZ);
81 RZA_PRINT_REGISTER(iss, O_HSZ);
82 RZA_PRINT_REGISTER(iss, V_PHS_Y);
83 RZA_PRINT_REGISTER(iss, V_PHS_C);
84 RZA_PRINT_REGISTER(iss, V_DIF);
85 RZA_PRINT_REGISTER(iss, V_TYP);
86 RZA_PRINT_REGISTER(iss, V_LPF);
87 RZA_PRINT_REGISTER(iss, H_PHS);
88 RZA_PRINT_REGISTER(iss, H_DIF);
89 RZA_PRINT_REGISTER(iss, H_TYP);
90 RZA_PRINT_REGISTER(iss, H_LPF);
91 RZA_PRINT_REGISTER(iss, DWN_EN);
92 RZA_PRINT_REGISTER(iss, SDR_Y_BAD_H);
93 RZA_PRINT_REGISTER(iss, SDR_Y_BAD_L);
94 RZA_PRINT_REGISTER(iss, SDR_Y_SAD_H);
95 RZA_PRINT_REGISTER(iss, SDR_Y_SAD_L);
96 RZA_PRINT_REGISTER(iss, SDR_Y_OFT);
97 RZA_PRINT_REGISTER(iss, SDR_Y_PTR_S);
98 RZA_PRINT_REGISTER(iss, SDR_Y_PTR_E);
99 RZA_PRINT_REGISTER(iss, SDR_C_BAD_H);
100 RZA_PRINT_REGISTER(iss, SDR_C_BAD_L);
101 RZA_PRINT_REGISTER(iss, SDR_C_SAD_H);
102 RZA_PRINT_REGISTER(iss, SDR_C_SAD_L);
103 RZA_PRINT_REGISTER(iss, SDR_C_OFT);
104 RZA_PRINT_REGISTER(iss, SDR_C_PTR_S);
105 RZA_PRINT_REGISTER(iss, SDR_C_PTR_E);
106
107 dev_dbg(iss->dev, "-----------------------------------------------\n");
108}
109
110/*
111 * resizer_enable - Enable/Disable RESIZER.
112 * @enable: enable flag
113 *
114 */
115static void resizer_enable(struct iss_resizer_device *resizer, u8 enable)
116{
117 struct iss_device *iss = to_iss_device(resizer);
118
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300119 iss_reg_update(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_EN,
120 RSZ_SRC_EN_SRC_EN, enable ? RSZ_SRC_EN_SRC_EN : 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300121
122 /* TODO: Enable RSZB */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300123 iss_reg_update(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_EN, RSZ_EN_EN,
124 enable ? RSZ_EN_EN : 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300125}
126
127/* -----------------------------------------------------------------------------
128 * Format- and pipeline-related configuration helpers
129 */
130
131/*
132 * resizer_set_outaddr - Set memory address to save output image
133 * @resizer: Pointer to ISP RESIZER device.
134 * @addr: 32-bit memory address aligned on 32 byte boundary.
135 *
136 * Sets the memory address where the output will be saved.
137 */
138static void resizer_set_outaddr(struct iss_resizer_device *resizer, u32 addr)
139{
140 struct iss_device *iss = to_iss_device(resizer);
141 struct v4l2_mbus_framefmt *informat, *outformat;
142
143 informat = &resizer->formats[RESIZER_PAD_SINK];
144 outformat = &resizer->formats[RESIZER_PAD_SOURCE_MEM];
145
146 /* Save address splitted in Base Address H & L */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300147 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_BAD_H,
148 (addr >> 16) & 0xffff);
149 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_BAD_L,
150 addr & 0xffff);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300151
152 /* SAD = BAD */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300153 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_SAD_H,
154 (addr >> 16) & 0xffff);
155 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_SAD_L,
156 addr & 0xffff);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300157
158 /* Program UV buffer address... Hardcoded to be contiguous! */
159 if ((informat->code == V4L2_MBUS_FMT_UYVY8_1X16) &&
160 (outformat->code == V4L2_MBUS_FMT_YUYV8_1_5X8)) {
161 u32 c_addr = addr + (resizer->video_out.bpl_value *
162 (outformat->height - 1));
163
164 /* Ensure Y_BAD_L[6:0] = C_BAD_L[6:0]*/
165 if ((c_addr ^ addr) & 0x7f) {
166 c_addr &= ~0x7f;
167 c_addr += 0x80;
168 c_addr |= addr & 0x7f;
169 }
170
171 /* Save address splitted in Base Address H & L */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300172 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_BAD_H,
173 (c_addr >> 16) & 0xffff);
174 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_BAD_L,
175 c_addr & 0xffff);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300176
177 /* SAD = BAD */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300178 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_SAD_H,
179 (c_addr >> 16) & 0xffff);
180 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_SAD_L,
181 c_addr & 0xffff);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300182 }
183}
184
185static void resizer_configure(struct iss_resizer_device *resizer)
186{
187 struct iss_device *iss = to_iss_device(resizer);
188 struct v4l2_mbus_framefmt *informat, *outformat;
189
190 informat = &resizer->formats[RESIZER_PAD_SINK];
191 outformat = &resizer->formats[RESIZER_PAD_SOURCE_MEM];
192
193 /* Make sure we don't bypass the resizer */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300194 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_FMT0,
195 RSZ_SRC_FMT0_BYPASS);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300196
197 /* Select RSZ input */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300198 iss_reg_update(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_FMT0,
199 RSZ_SRC_FMT0_SEL,
200 resizer->input == RESIZER_INPUT_IPIPEIF ?
201 RSZ_SRC_FMT0_SEL : 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300202
203 /* RSZ ignores WEN signal from IPIPE/IPIPEIF */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300204 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_MODE,
205 RSZ_SRC_MODE_WRT);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300206
207 /* Set Resizer in free-running mode */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300208 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_MODE,
209 RSZ_SRC_MODE_OST);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300210
211 /* Init Resizer A */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300212 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_MODE,
213 RZA_MODE_ONE_SHOT);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300214
215 /* Set size related things now */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300216 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_VPS, 0);
217 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_HPS, 0);
218 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_VSZ,
219 informat->height - 2);
220 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SRC_HSZ,
221 informat->width - 1);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300222
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300223 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_I_VPS, 0);
224 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_I_HPS, 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300225
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300226 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_O_VSZ,
227 outformat->height - 2);
228 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_O_HSZ,
229 outformat->width - 1);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300230
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300231 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_V_DIF, 0x100);
232 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_H_DIF, 0x100);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300233
234 /* Buffer output settings */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300235 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_PTR_S, 0);
236 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_PTR_E,
237 outformat->height - 1);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300238
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300239 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_Y_OFT,
240 resizer->video_out.bpl_value);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300241
242 /* UYVY -> NV12 conversion */
243 if ((informat->code == V4L2_MBUS_FMT_UYVY8_1X16) &&
244 (outformat->code == V4L2_MBUS_FMT_YUYV8_1_5X8)) {
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300245 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_420,
246 RSZ_420_CEN | RSZ_420_YEN);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300247
248 /* UV Buffer output settings */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300249 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_PTR_S,
250 0);
251 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_PTR_E,
252 outformat->height - 1);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300253
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300254 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_SDR_C_OFT,
255 resizer->video_out.bpl_value);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300256 } else {
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300257 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_RESIZER, RZA_420, 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300258 }
259
260 omap4iss_isp_enable_interrupts(iss);
261}
262
263/* -----------------------------------------------------------------------------
264 * Interrupt handling
265 */
266
267static void resizer_isr_buffer(struct iss_resizer_device *resizer)
268{
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300269 struct iss_buffer *buffer;
270
Laurent Pinchart82043ff2013-09-04 09:48:20 -0300271 /* The whole resizer needs to be stopped. Disabling RZA only produces
272 * input FIFO overflows, most probably when the next frame is received.
273 */
274 resizer_enable(resizer, 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300275
276 buffer = omap4iss_video_buffer_next(&resizer->video_out);
277 if (buffer == NULL)
278 return;
279
280 resizer_set_outaddr(resizer, buffer->iss_addr);
281
Laurent Pinchart82043ff2013-09-04 09:48:20 -0300282 resizer_enable(resizer, 1);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300283}
284
285/*
286 * resizer_isif0_isr - Handle ISIF0 event
287 * @resizer: Pointer to ISP RESIZER device.
288 *
289 * Executes LSC deferred enablement before next frame starts.
290 */
291static void resizer_int_dma_isr(struct iss_resizer_device *resizer)
292{
293 struct iss_pipeline *pipe =
294 to_iss_pipeline(&resizer->subdev.entity);
295 if (pipe->do_propagation)
296 atomic_inc(&pipe->frame_number);
297
298 resizer_isr_buffer(resizer);
299}
300
301/*
302 * omap4iss_resizer_isr - Configure resizer during interframe time.
303 * @resizer: Pointer to ISP RESIZER device.
304 * @events: RESIZER events
305 */
306void omap4iss_resizer_isr(struct iss_resizer_device *resizer, u32 events)
307{
308 struct iss_device *iss = to_iss_device(resizer);
309 struct iss_pipeline *pipe =
310 to_iss_pipeline(&resizer->subdev.entity);
311
Laurent Pinchartade1ec32013-08-28 12:03:50 -0300312 if (events & (ISP5_IRQ_RSZ_FIFO_IN_BLK_ERR |
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300313 ISP5_IRQ_RSZ_FIFO_OVF)) {
Laurent Pinchart499226f2013-12-03 21:26:37 -0300314 dev_dbg(iss->dev, "RSZ Err: FIFO_IN_BLK:%d, FIFO_OVF:%d\n",
Laurent Pinchartcd782f92013-08-28 13:40:57 -0300315 events & ISP5_IRQ_RSZ_FIFO_IN_BLK_ERR ? 1 : 0,
316 events & ISP5_IRQ_RSZ_FIFO_OVF ? 1 : 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300317 pipe->error = true;
318 }
319
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300320 if (omap4iss_module_sync_is_stopping(&resizer->wait,
321 &resizer->stopping))
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300322 return;
323
324 if (events & ISP5_IRQ_RSZ_INT_DMA)
325 resizer_int_dma_isr(resizer);
326}
327
328/* -----------------------------------------------------------------------------
329 * ISS video operations
330 */
331
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300332static int resizer_video_queue(struct iss_video *video,
333 struct iss_buffer *buffer)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300334{
335 struct iss_resizer_device *resizer = container_of(video,
336 struct iss_resizer_device, video_out);
337
338 if (!(resizer->output & RESIZER_OUTPUT_MEMORY))
339 return -ENODEV;
340
341 resizer_set_outaddr(resizer, buffer->iss_addr);
342
343 /*
344 * If streaming was enabled before there was a buffer queued
345 * or underrun happened in the ISR, the hardware was not enabled
346 * and DMA queue flag ISS_VIDEO_DMAQUEUE_UNDERRUN is still set.
347 * Enable it now.
348 */
349 if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
350 resizer_enable(resizer, 1);
351 iss_video_dmaqueue_flags_clr(video);
352 }
353
354 return 0;
355}
356
357static const struct iss_video_operations resizer_video_ops = {
358 .queue = resizer_video_queue,
359};
360
361/* -----------------------------------------------------------------------------
362 * V4L2 subdev operations
363 */
364
365/*
366 * resizer_set_stream - Enable/Disable streaming on the RESIZER module
367 * @sd: ISP RESIZER V4L2 subdevice
368 * @enable: Enable/disable stream
369 */
370static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
371{
372 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
373 struct iss_device *iss = to_iss_device(resizer);
374 struct iss_video *video_out = &resizer->video_out;
375 int ret = 0;
376
377 if (resizer->state == ISS_PIPELINE_STREAM_STOPPED) {
378 if (enable == ISS_PIPELINE_STREAM_STOPPED)
379 return 0;
380
381 omap4iss_isp_subclk_enable(iss, OMAP4_ISS_ISP_SUBCLK_RSZ);
382
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300383 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_MMR,
384 RSZ_GCK_MMR_MMR);
385 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_SDR,
386 RSZ_GCK_SDR_CORE);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300387
388 /* FIXME: Enable RSZB also */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300389 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SYSCONFIG,
390 RSZ_SYSCONFIG_RSZA_CLK_EN);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300391 }
392
393 switch (enable) {
394 case ISS_PIPELINE_STREAM_CONTINUOUS:
395
396 resizer_configure(resizer);
397 resizer_print_status(resizer);
398
399 /*
400 * When outputting to memory with no buffer available, let the
401 * buffer queue handler start the hardware. A DMA queue flag
402 * ISS_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
403 * a buffer available.
404 */
405 if (resizer->output & RESIZER_OUTPUT_MEMORY &&
406 !(video_out->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_QUEUED))
407 break;
408
409 atomic_set(&resizer->stopping, 0);
410 resizer_enable(resizer, 1);
411 iss_video_dmaqueue_flags_clr(video_out);
412 break;
413
414 case ISS_PIPELINE_STREAM_STOPPED:
415 if (resizer->state == ISS_PIPELINE_STREAM_STOPPED)
416 return 0;
417 if (omap4iss_module_sync_idle(&sd->entity, &resizer->wait,
418 &resizer->stopping))
Laurent Pinchart60164982013-10-09 11:52:45 -0300419 ret = -ETIMEDOUT;
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300420
421 resizer_enable(resizer, 0);
422 omap4iss_isp_disable_interrupts(iss);
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300423 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SYSCONFIG,
424 RSZ_SYSCONFIG_RSZA_CLK_EN);
425 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_SDR,
426 RSZ_GCK_SDR_CORE);
427 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_MMR,
428 RSZ_GCK_MMR_MMR);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300429 omap4iss_isp_subclk_disable(iss, OMAP4_ISS_ISP_SUBCLK_RSZ);
430 iss_video_dmaqueue_flags_clr(video_out);
431 break;
432 }
433
434 resizer->state = enable;
435 return ret;
436}
437
438static struct v4l2_mbus_framefmt *
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300439__resizer_get_format(struct iss_resizer_device *resizer,
440 struct v4l2_subdev_fh *fh, unsigned int pad,
441 enum v4l2_subdev_format_whence which)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300442{
443 if (which == V4L2_SUBDEV_FORMAT_TRY)
444 return v4l2_subdev_get_try_format(fh, pad);
445 else
446 return &resizer->formats[pad];
447}
448
449/*
450 * resizer_try_format - Try video format on a pad
451 * @resizer: ISS RESIZER device
452 * @fh : V4L2 subdev file handle
453 * @pad: Pad number
454 * @fmt: Format
455 */
456static void
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300457resizer_try_format(struct iss_resizer_device *resizer,
458 struct v4l2_subdev_fh *fh, unsigned int pad,
459 struct v4l2_mbus_framefmt *fmt,
460 enum v4l2_subdev_format_whence which)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300461{
462 enum v4l2_mbus_pixelcode pixelcode;
463 struct v4l2_mbus_framefmt *format;
464 unsigned int width = fmt->width;
465 unsigned int height = fmt->height;
466 unsigned int i;
467
468 switch (pad) {
469 case RESIZER_PAD_SINK:
470 for (i = 0; i < ARRAY_SIZE(resizer_fmts); i++) {
471 if (fmt->code == resizer_fmts[i])
472 break;
473 }
474
475 /* If not found, use UYVY as default */
476 if (i >= ARRAY_SIZE(resizer_fmts))
477 fmt->code = V4L2_MBUS_FMT_UYVY8_1X16;
478
479 /* Clamp the input size. */
480 fmt->width = clamp_t(u32, width, 1, 8192);
481 fmt->height = clamp_t(u32, height, 1, 8192);
482 break;
483
484 case RESIZER_PAD_SOURCE_MEM:
485 pixelcode = fmt->code;
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300486 format = __resizer_get_format(resizer, fh, RESIZER_PAD_SINK,
487 which);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300488 memcpy(fmt, format, sizeof(*fmt));
489
490 if ((pixelcode == V4L2_MBUS_FMT_YUYV8_1_5X8) &&
491 (fmt->code == V4L2_MBUS_FMT_UYVY8_1X16))
492 fmt->code = pixelcode;
493
494 /* The data formatter truncates the number of horizontal output
495 * pixels to a multiple of 16. To avoid clipping data, allow
496 * callers to request an output size bigger than the input size
497 * up to the nearest multiple of 16.
498 */
499 fmt->width = clamp_t(u32, width, 32, (fmt->width + 15) & ~15);
500 fmt->width &= ~15;
501 fmt->height = clamp_t(u32, height, 32, fmt->height);
502 break;
503
504 }
505
506 fmt->colorspace = V4L2_COLORSPACE_JPEG;
507 fmt->field = V4L2_FIELD_NONE;
508}
509
510/*
511 * resizer_enum_mbus_code - Handle pixel format enumeration
512 * @sd : pointer to v4l2 subdev structure
513 * @fh : V4L2 subdev file handle
514 * @code : pointer to v4l2_subdev_mbus_code_enum structure
515 * return -EINVAL or zero on success
516 */
517static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
518 struct v4l2_subdev_fh *fh,
519 struct v4l2_subdev_mbus_code_enum *code)
520{
521 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
522 struct v4l2_mbus_framefmt *format;
523
524 switch (code->pad) {
525 case RESIZER_PAD_SINK:
526 if (code->index >= ARRAY_SIZE(resizer_fmts))
527 return -EINVAL;
528
529 code->code = resizer_fmts[code->index];
530 break;
531
532 case RESIZER_PAD_SOURCE_MEM:
533 format = __resizer_get_format(resizer, fh, RESIZER_PAD_SINK,
534 V4L2_SUBDEV_FORMAT_TRY);
535
536 if (code->index == 0) {
537 code->code = format->code;
538 break;
539 }
540
541 switch (format->code) {
542 case V4L2_MBUS_FMT_UYVY8_1X16:
543 if (code->index == 1)
544 code->code = V4L2_MBUS_FMT_YUYV8_1_5X8;
545 else
546 return -EINVAL;
547 break;
548 default:
549 if (code->index != 0)
550 return -EINVAL;
551 }
552
553 break;
554
555 default:
556 return -EINVAL;
557 }
558
559 return 0;
560}
561
562static int resizer_enum_frame_size(struct v4l2_subdev *sd,
563 struct v4l2_subdev_fh *fh,
564 struct v4l2_subdev_frame_size_enum *fse)
565{
566 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
567 struct v4l2_mbus_framefmt format;
568
569 if (fse->index != 0)
570 return -EINVAL;
571
572 format.code = fse->code;
573 format.width = 1;
574 format.height = 1;
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300575 resizer_try_format(resizer, fh, fse->pad, &format,
576 V4L2_SUBDEV_FORMAT_TRY);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300577 fse->min_width = format.width;
578 fse->min_height = format.height;
579
580 if (format.code != fse->code)
581 return -EINVAL;
582
583 format.code = fse->code;
584 format.width = -1;
585 format.height = -1;
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300586 resizer_try_format(resizer, fh, fse->pad, &format,
587 V4L2_SUBDEV_FORMAT_TRY);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300588 fse->max_width = format.width;
589 fse->max_height = format.height;
590
591 return 0;
592}
593
594/*
595 * resizer_get_format - Retrieve the video format on a pad
596 * @sd : ISP RESIZER V4L2 subdevice
597 * @fh : V4L2 subdev file handle
598 * @fmt: Format
599 *
600 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
601 * to the format type.
602 */
603static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
604 struct v4l2_subdev_format *fmt)
605{
606 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
607 struct v4l2_mbus_framefmt *format;
608
609 format = __resizer_get_format(resizer, fh, fmt->pad, fmt->which);
610 if (format == NULL)
611 return -EINVAL;
612
613 fmt->format = *format;
614 return 0;
615}
616
617/*
618 * resizer_set_format - Set the video format on a pad
619 * @sd : ISP RESIZER V4L2 subdevice
620 * @fh : V4L2 subdev file handle
621 * @fmt: Format
622 *
623 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
624 * to the format type.
625 */
626static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
627 struct v4l2_subdev_format *fmt)
628{
629 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
630 struct v4l2_mbus_framefmt *format;
631
632 format = __resizer_get_format(resizer, fh, fmt->pad, fmt->which);
633 if (format == NULL)
634 return -EINVAL;
635
636 resizer_try_format(resizer, fh, fmt->pad, &fmt->format, fmt->which);
637 *format = fmt->format;
638
639 /* Propagate the format from sink to source */
640 if (fmt->pad == RESIZER_PAD_SINK) {
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300641 format = __resizer_get_format(resizer, fh,
642 RESIZER_PAD_SOURCE_MEM,
643 fmt->which);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300644 *format = fmt->format;
645 resizer_try_format(resizer, fh, RESIZER_PAD_SOURCE_MEM, format,
646 fmt->which);
647 }
648
649 return 0;
650}
651
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300652static int resizer_link_validate(struct v4l2_subdev *sd,
653 struct media_link *link,
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300654 struct v4l2_subdev_format *source_fmt,
655 struct v4l2_subdev_format *sink_fmt)
656{
657 /* Check if the two ends match */
658 if (source_fmt->format.width != sink_fmt->format.width ||
659 source_fmt->format.height != sink_fmt->format.height)
660 return -EPIPE;
661
662 if (source_fmt->format.code != sink_fmt->format.code)
663 return -EPIPE;
664
665 return 0;
666}
667
668/*
669 * resizer_init_formats - Initialize formats on all pads
670 * @sd: ISP RESIZER V4L2 subdevice
671 * @fh: V4L2 subdev file handle
672 *
673 * Initialize all pad formats with default values. If fh is not NULL, try
674 * formats are initialized on the file handle. Otherwise active formats are
675 * initialized on the device.
676 */
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300677static int resizer_init_formats(struct v4l2_subdev *sd,
678 struct v4l2_subdev_fh *fh)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300679{
680 struct v4l2_subdev_format format;
681
682 memset(&format, 0, sizeof(format));
683 format.pad = RESIZER_PAD_SINK;
684 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
685 format.format.code = V4L2_MBUS_FMT_UYVY8_1X16;
686 format.format.width = 4096;
687 format.format.height = 4096;
688 resizer_set_format(sd, fh, &format);
689
690 return 0;
691}
692
693/* V4L2 subdev video operations */
694static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
695 .s_stream = resizer_set_stream,
696};
697
698/* V4L2 subdev pad operations */
699static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
700 .enum_mbus_code = resizer_enum_mbus_code,
701 .enum_frame_size = resizer_enum_frame_size,
702 .get_fmt = resizer_get_format,
703 .set_fmt = resizer_set_format,
704 .link_validate = resizer_link_validate,
705};
706
707/* V4L2 subdev operations */
708static const struct v4l2_subdev_ops resizer_v4l2_ops = {
709 .video = &resizer_v4l2_video_ops,
710 .pad = &resizer_v4l2_pad_ops,
711};
712
713/* V4L2 subdev internal operations */
714static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
715 .open = resizer_init_formats,
716};
717
718/* -----------------------------------------------------------------------------
719 * Media entity operations
720 */
721
722/*
723 * resizer_link_setup - Setup RESIZER connections
724 * @entity: RESIZER media entity
725 * @local: Pad at the local end of the link
726 * @remote: Pad at the remote end of the link
727 * @flags: Link flags
728 *
729 * return -EINVAL or zero on success
730 */
731static int resizer_link_setup(struct media_entity *entity,
732 const struct media_pad *local,
733 const struct media_pad *remote, u32 flags)
734{
735 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
736 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
737 struct iss_device *iss = to_iss_device(resizer);
738
739 switch (local->index | media_entity_type(remote->entity)) {
740 case RESIZER_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
741 /* Read from IPIPE or IPIPEIF. */
742 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
743 resizer->input = RESIZER_INPUT_NONE;
744 break;
745 }
746
747 if (resizer->input != RESIZER_INPUT_NONE)
748 return -EBUSY;
749
750 if (remote->entity == &iss->ipipeif.subdev.entity)
751 resizer->input = RESIZER_INPUT_IPIPEIF;
752 else if (remote->entity == &iss->ipipe.subdev.entity)
753 resizer->input = RESIZER_INPUT_IPIPE;
754
755
756 break;
757
758 case RESIZER_PAD_SOURCE_MEM | MEDIA_ENT_T_DEVNODE:
759 /* Write to memory */
760 if (flags & MEDIA_LNK_FL_ENABLED) {
761 if (resizer->output & ~RESIZER_OUTPUT_MEMORY)
762 return -EBUSY;
763 resizer->output |= RESIZER_OUTPUT_MEMORY;
764 } else {
765 resizer->output &= ~RESIZER_OUTPUT_MEMORY;
766 }
767 break;
768
769 default:
770 return -EINVAL;
771 }
772
773 return 0;
774}
775
776/* media operations */
777static const struct media_entity_operations resizer_media_ops = {
778 .link_setup = resizer_link_setup,
779 .link_validate = v4l2_subdev_link_validate,
780};
781
782/*
783 * resizer_init_entities - Initialize V4L2 subdev and media entity
784 * @resizer: ISS ISP RESIZER module
785 *
786 * Return 0 on success and a negative error code on failure.
787 */
788static int resizer_init_entities(struct iss_resizer_device *resizer)
789{
790 struct v4l2_subdev *sd = &resizer->subdev;
791 struct media_pad *pads = resizer->pads;
792 struct media_entity *me = &sd->entity;
793 int ret;
794
795 resizer->input = RESIZER_INPUT_NONE;
796
797 v4l2_subdev_init(sd, &resizer_v4l2_ops);
798 sd->internal_ops = &resizer_v4l2_internal_ops;
799 strlcpy(sd->name, "OMAP4 ISS ISP resizer", sizeof(sd->name));
800 sd->grp_id = 1 << 16; /* group ID for iss subdevs */
801 v4l2_set_subdevdata(sd, resizer);
802 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
803
804 pads[RESIZER_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
805 pads[RESIZER_PAD_SOURCE_MEM].flags = MEDIA_PAD_FL_SOURCE;
806
807 me->ops = &resizer_media_ops;
808 ret = media_entity_init(me, RESIZER_PADS_NUM, pads, 0);
809 if (ret < 0)
810 return ret;
811
812 resizer_init_formats(sd, NULL);
813
814 resizer->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
815 resizer->video_out.ops = &resizer_video_ops;
816 resizer->video_out.iss = to_iss_device(resizer);
817 resizer->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
818 resizer->video_out.bpl_alignment = 32;
819 resizer->video_out.bpl_zero_padding = 1;
820 resizer->video_out.bpl_max = 0x1ffe0;
821
822 ret = omap4iss_video_init(&resizer->video_out, "ISP resizer a");
823 if (ret < 0)
824 return ret;
825
826 /* Connect the RESIZER subdev to the video node. */
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300827 ret = media_entity_create_link(&resizer->subdev.entity,
828 RESIZER_PAD_SOURCE_MEM,
829 &resizer->video_out.video.entity, 0, 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300830 if (ret < 0)
831 return ret;
832
833 return 0;
834}
835
836void omap4iss_resizer_unregister_entities(struct iss_resizer_device *resizer)
837{
838 media_entity_cleanup(&resizer->subdev.entity);
839
840 v4l2_device_unregister_subdev(&resizer->subdev);
841 omap4iss_video_unregister(&resizer->video_out);
842}
843
844int omap4iss_resizer_register_entities(struct iss_resizer_device *resizer,
845 struct v4l2_device *vdev)
846{
847 int ret;
848
849 /* Register the subdev and video node. */
850 ret = v4l2_device_register_subdev(vdev, &resizer->subdev);
851 if (ret < 0)
852 goto error;
853
854 ret = omap4iss_video_register(&resizer->video_out, vdev);
855 if (ret < 0)
856 goto error;
857
858 return 0;
859
860error:
861 omap4iss_resizer_unregister_entities(resizer);
862 return ret;
863}
864
865/* -----------------------------------------------------------------------------
866 * ISP RESIZER initialisation and cleanup
867 */
868
869/*
870 * omap4iss_resizer_init - RESIZER module initialization.
871 * @iss: Device pointer specific to the OMAP4 ISS.
872 *
873 * TODO: Get the initialisation values from platform data.
874 *
875 * Return 0 on success or a negative error code otherwise.
876 */
877int omap4iss_resizer_init(struct iss_device *iss)
878{
879 struct iss_resizer_device *resizer = &iss->resizer;
880
881 resizer->state = ISS_PIPELINE_STREAM_STOPPED;
882 init_waitqueue_head(&resizer->wait);
883
884 return resizer_init_entities(resizer);
885}
886
887/*
888 * omap4iss_resizer_cleanup - RESIZER module cleanup.
889 * @iss: Device pointer specific to the OMAP4 ISS.
890 */
891void omap4iss_resizer_cleanup(struct iss_device *iss)
892{
893 /* FIXME: are you sure there's nothing to do? */
894}