blob: c6225d805d1a2aff7e30138cd45abd9580e7788f [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 }
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300259}
260
261/* -----------------------------------------------------------------------------
262 * Interrupt handling
263 */
264
265static void resizer_isr_buffer(struct iss_resizer_device *resizer)
266{
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300267 struct iss_buffer *buffer;
268
Laurent Pinchart82043ff2013-09-04 09:48:20 -0300269 /* The whole resizer needs to be stopped. Disabling RZA only produces
270 * input FIFO overflows, most probably when the next frame is received.
271 */
272 resizer_enable(resizer, 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300273
274 buffer = omap4iss_video_buffer_next(&resizer->video_out);
275 if (buffer == NULL)
276 return;
277
278 resizer_set_outaddr(resizer, buffer->iss_addr);
279
Laurent Pinchart82043ff2013-09-04 09:48:20 -0300280 resizer_enable(resizer, 1);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300281}
282
283/*
284 * resizer_isif0_isr - Handle ISIF0 event
285 * @resizer: Pointer to ISP RESIZER device.
286 *
287 * Executes LSC deferred enablement before next frame starts.
288 */
289static void resizer_int_dma_isr(struct iss_resizer_device *resizer)
290{
291 struct iss_pipeline *pipe =
292 to_iss_pipeline(&resizer->subdev.entity);
293 if (pipe->do_propagation)
294 atomic_inc(&pipe->frame_number);
295
296 resizer_isr_buffer(resizer);
297}
298
299/*
300 * omap4iss_resizer_isr - Configure resizer during interframe time.
301 * @resizer: Pointer to ISP RESIZER device.
302 * @events: RESIZER events
303 */
304void omap4iss_resizer_isr(struct iss_resizer_device *resizer, u32 events)
305{
306 struct iss_device *iss = to_iss_device(resizer);
307 struct iss_pipeline *pipe =
308 to_iss_pipeline(&resizer->subdev.entity);
309
Laurent Pinchartade1ec32013-08-28 12:03:50 -0300310 if (events & (ISP5_IRQ_RSZ_FIFO_IN_BLK_ERR |
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300311 ISP5_IRQ_RSZ_FIFO_OVF)) {
Laurent Pinchart499226f2013-12-03 21:26:37 -0300312 dev_dbg(iss->dev, "RSZ Err: FIFO_IN_BLK:%d, FIFO_OVF:%d\n",
Laurent Pinchartcd782f92013-08-28 13:40:57 -0300313 events & ISP5_IRQ_RSZ_FIFO_IN_BLK_ERR ? 1 : 0,
314 events & ISP5_IRQ_RSZ_FIFO_OVF ? 1 : 0);
Laurent Pinchart112da082013-11-05 12:32:05 -0300315 omap4iss_pipeline_cancel_stream(pipe);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300316 }
317
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300318 if (omap4iss_module_sync_is_stopping(&resizer->wait,
319 &resizer->stopping))
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300320 return;
321
322 if (events & ISP5_IRQ_RSZ_INT_DMA)
323 resizer_int_dma_isr(resizer);
324}
325
326/* -----------------------------------------------------------------------------
327 * ISS video operations
328 */
329
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300330static int resizer_video_queue(struct iss_video *video,
331 struct iss_buffer *buffer)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300332{
333 struct iss_resizer_device *resizer = container_of(video,
334 struct iss_resizer_device, video_out);
335
336 if (!(resizer->output & RESIZER_OUTPUT_MEMORY))
337 return -ENODEV;
338
339 resizer_set_outaddr(resizer, buffer->iss_addr);
340
341 /*
342 * If streaming was enabled before there was a buffer queued
343 * or underrun happened in the ISR, the hardware was not enabled
344 * and DMA queue flag ISS_VIDEO_DMAQUEUE_UNDERRUN is still set.
345 * Enable it now.
346 */
347 if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
348 resizer_enable(resizer, 1);
349 iss_video_dmaqueue_flags_clr(video);
350 }
351
352 return 0;
353}
354
355static const struct iss_video_operations resizer_video_ops = {
356 .queue = resizer_video_queue,
357};
358
359/* -----------------------------------------------------------------------------
360 * V4L2 subdev operations
361 */
362
363/*
364 * resizer_set_stream - Enable/Disable streaming on the RESIZER module
365 * @sd: ISP RESIZER V4L2 subdevice
366 * @enable: Enable/disable stream
367 */
368static int resizer_set_stream(struct v4l2_subdev *sd, int enable)
369{
370 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
371 struct iss_device *iss = to_iss_device(resizer);
372 struct iss_video *video_out = &resizer->video_out;
373 int ret = 0;
374
375 if (resizer->state == ISS_PIPELINE_STREAM_STOPPED) {
376 if (enable == ISS_PIPELINE_STREAM_STOPPED)
377 return 0;
378
379 omap4iss_isp_subclk_enable(iss, OMAP4_ISS_ISP_SUBCLK_RSZ);
380
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300381 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_MMR,
382 RSZ_GCK_MMR_MMR);
383 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_SDR,
384 RSZ_GCK_SDR_CORE);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300385
386 /* FIXME: Enable RSZB also */
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300387 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SYSCONFIG,
388 RSZ_SYSCONFIG_RSZA_CLK_EN);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300389 }
390
391 switch (enable) {
392 case ISS_PIPELINE_STREAM_CONTINUOUS:
393
394 resizer_configure(resizer);
395 resizer_print_status(resizer);
396
397 /*
398 * When outputting to memory with no buffer available, let the
399 * buffer queue handler start the hardware. A DMA queue flag
400 * ISS_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
401 * a buffer available.
402 */
403 if (resizer->output & RESIZER_OUTPUT_MEMORY &&
404 !(video_out->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_QUEUED))
405 break;
406
407 atomic_set(&resizer->stopping, 0);
408 resizer_enable(resizer, 1);
409 iss_video_dmaqueue_flags_clr(video_out);
410 break;
411
412 case ISS_PIPELINE_STREAM_STOPPED:
413 if (resizer->state == ISS_PIPELINE_STREAM_STOPPED)
414 return 0;
415 if (omap4iss_module_sync_idle(&sd->entity, &resizer->wait,
416 &resizer->stopping))
Laurent Pinchart60164982013-10-09 11:52:45 -0300417 ret = -ETIMEDOUT;
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300418
419 resizer_enable(resizer, 0);
Laurent Pinchart11abbfd2013-08-30 22:23:17 -0300420 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_SYSCONFIG,
421 RSZ_SYSCONFIG_RSZA_CLK_EN);
422 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_SDR,
423 RSZ_GCK_SDR_CORE);
424 iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_RESIZER, RSZ_GCK_MMR,
425 RSZ_GCK_MMR_MMR);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300426 omap4iss_isp_subclk_disable(iss, OMAP4_ISS_ISP_SUBCLK_RSZ);
427 iss_video_dmaqueue_flags_clr(video_out);
428 break;
429 }
430
431 resizer->state = enable;
432 return ret;
433}
434
435static struct v4l2_mbus_framefmt *
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300436__resizer_get_format(struct iss_resizer_device *resizer,
437 struct v4l2_subdev_fh *fh, unsigned int pad,
438 enum v4l2_subdev_format_whence which)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300439{
440 if (which == V4L2_SUBDEV_FORMAT_TRY)
441 return v4l2_subdev_get_try_format(fh, pad);
442 else
443 return &resizer->formats[pad];
444}
445
446/*
447 * resizer_try_format - Try video format on a pad
448 * @resizer: ISS RESIZER device
449 * @fh : V4L2 subdev file handle
450 * @pad: Pad number
451 * @fmt: Format
452 */
453static void
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300454resizer_try_format(struct iss_resizer_device *resizer,
455 struct v4l2_subdev_fh *fh, unsigned int pad,
456 struct v4l2_mbus_framefmt *fmt,
457 enum v4l2_subdev_format_whence which)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300458{
459 enum v4l2_mbus_pixelcode pixelcode;
460 struct v4l2_mbus_framefmt *format;
461 unsigned int width = fmt->width;
462 unsigned int height = fmt->height;
463 unsigned int i;
464
465 switch (pad) {
466 case RESIZER_PAD_SINK:
467 for (i = 0; i < ARRAY_SIZE(resizer_fmts); i++) {
468 if (fmt->code == resizer_fmts[i])
469 break;
470 }
471
472 /* If not found, use UYVY as default */
473 if (i >= ARRAY_SIZE(resizer_fmts))
474 fmt->code = V4L2_MBUS_FMT_UYVY8_1X16;
475
476 /* Clamp the input size. */
477 fmt->width = clamp_t(u32, width, 1, 8192);
478 fmt->height = clamp_t(u32, height, 1, 8192);
479 break;
480
481 case RESIZER_PAD_SOURCE_MEM:
482 pixelcode = fmt->code;
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300483 format = __resizer_get_format(resizer, fh, RESIZER_PAD_SINK,
484 which);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300485 memcpy(fmt, format, sizeof(*fmt));
486
487 if ((pixelcode == V4L2_MBUS_FMT_YUYV8_1_5X8) &&
488 (fmt->code == V4L2_MBUS_FMT_UYVY8_1X16))
489 fmt->code = pixelcode;
490
491 /* The data formatter truncates the number of horizontal output
492 * pixels to a multiple of 16. To avoid clipping data, allow
493 * callers to request an output size bigger than the input size
494 * up to the nearest multiple of 16.
495 */
496 fmt->width = clamp_t(u32, width, 32, (fmt->width + 15) & ~15);
497 fmt->width &= ~15;
498 fmt->height = clamp_t(u32, height, 32, fmt->height);
499 break;
500
501 }
502
503 fmt->colorspace = V4L2_COLORSPACE_JPEG;
504 fmt->field = V4L2_FIELD_NONE;
505}
506
507/*
508 * resizer_enum_mbus_code - Handle pixel format enumeration
509 * @sd : pointer to v4l2 subdev structure
510 * @fh : V4L2 subdev file handle
511 * @code : pointer to v4l2_subdev_mbus_code_enum structure
512 * return -EINVAL or zero on success
513 */
514static int resizer_enum_mbus_code(struct v4l2_subdev *sd,
515 struct v4l2_subdev_fh *fh,
516 struct v4l2_subdev_mbus_code_enum *code)
517{
518 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
519 struct v4l2_mbus_framefmt *format;
520
521 switch (code->pad) {
522 case RESIZER_PAD_SINK:
523 if (code->index >= ARRAY_SIZE(resizer_fmts))
524 return -EINVAL;
525
526 code->code = resizer_fmts[code->index];
527 break;
528
529 case RESIZER_PAD_SOURCE_MEM:
530 format = __resizer_get_format(resizer, fh, RESIZER_PAD_SINK,
531 V4L2_SUBDEV_FORMAT_TRY);
532
533 if (code->index == 0) {
534 code->code = format->code;
535 break;
536 }
537
538 switch (format->code) {
539 case V4L2_MBUS_FMT_UYVY8_1X16:
540 if (code->index == 1)
541 code->code = V4L2_MBUS_FMT_YUYV8_1_5X8;
542 else
543 return -EINVAL;
544 break;
545 default:
546 if (code->index != 0)
547 return -EINVAL;
548 }
549
550 break;
551
552 default:
553 return -EINVAL;
554 }
555
556 return 0;
557}
558
559static int resizer_enum_frame_size(struct v4l2_subdev *sd,
560 struct v4l2_subdev_fh *fh,
561 struct v4l2_subdev_frame_size_enum *fse)
562{
563 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
564 struct v4l2_mbus_framefmt format;
565
566 if (fse->index != 0)
567 return -EINVAL;
568
569 format.code = fse->code;
570 format.width = 1;
571 format.height = 1;
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300572 resizer_try_format(resizer, fh, fse->pad, &format,
573 V4L2_SUBDEV_FORMAT_TRY);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300574 fse->min_width = format.width;
575 fse->min_height = format.height;
576
577 if (format.code != fse->code)
578 return -EINVAL;
579
580 format.code = fse->code;
581 format.width = -1;
582 format.height = -1;
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300583 resizer_try_format(resizer, fh, fse->pad, &format,
584 V4L2_SUBDEV_FORMAT_TRY);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300585 fse->max_width = format.width;
586 fse->max_height = format.height;
587
588 return 0;
589}
590
591/*
592 * resizer_get_format - Retrieve the video format on a pad
593 * @sd : ISP RESIZER V4L2 subdevice
594 * @fh : V4L2 subdev file handle
595 * @fmt: Format
596 *
597 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
598 * to the format type.
599 */
600static int resizer_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
601 struct v4l2_subdev_format *fmt)
602{
603 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
604 struct v4l2_mbus_framefmt *format;
605
606 format = __resizer_get_format(resizer, fh, fmt->pad, fmt->which);
607 if (format == NULL)
608 return -EINVAL;
609
610 fmt->format = *format;
611 return 0;
612}
613
614/*
615 * resizer_set_format - Set the video format on a pad
616 * @sd : ISP RESIZER V4L2 subdevice
617 * @fh : V4L2 subdev file handle
618 * @fmt: Format
619 *
620 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
621 * to the format type.
622 */
623static int resizer_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
624 struct v4l2_subdev_format *fmt)
625{
626 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
627 struct v4l2_mbus_framefmt *format;
628
629 format = __resizer_get_format(resizer, fh, fmt->pad, fmt->which);
630 if (format == NULL)
631 return -EINVAL;
632
633 resizer_try_format(resizer, fh, fmt->pad, &fmt->format, fmt->which);
634 *format = fmt->format;
635
636 /* Propagate the format from sink to source */
637 if (fmt->pad == RESIZER_PAD_SINK) {
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300638 format = __resizer_get_format(resizer, fh,
639 RESIZER_PAD_SOURCE_MEM,
640 fmt->which);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300641 *format = fmt->format;
642 resizer_try_format(resizer, fh, RESIZER_PAD_SOURCE_MEM, format,
643 fmt->which);
644 }
645
646 return 0;
647}
648
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300649static int resizer_link_validate(struct v4l2_subdev *sd,
650 struct media_link *link,
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300651 struct v4l2_subdev_format *source_fmt,
652 struct v4l2_subdev_format *sink_fmt)
653{
654 /* Check if the two ends match */
655 if (source_fmt->format.width != sink_fmt->format.width ||
656 source_fmt->format.height != sink_fmt->format.height)
657 return -EPIPE;
658
659 if (source_fmt->format.code != sink_fmt->format.code)
660 return -EPIPE;
661
662 return 0;
663}
664
665/*
666 * resizer_init_formats - Initialize formats on all pads
667 * @sd: ISP RESIZER V4L2 subdevice
668 * @fh: V4L2 subdev file handle
669 *
670 * Initialize all pad formats with default values. If fh is not NULL, try
671 * formats are initialized on the file handle. Otherwise active formats are
672 * initialized on the device.
673 */
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300674static int resizer_init_formats(struct v4l2_subdev *sd,
675 struct v4l2_subdev_fh *fh)
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300676{
677 struct v4l2_subdev_format format;
678
679 memset(&format, 0, sizeof(format));
680 format.pad = RESIZER_PAD_SINK;
681 format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
682 format.format.code = V4L2_MBUS_FMT_UYVY8_1X16;
683 format.format.width = 4096;
684 format.format.height = 4096;
685 resizer_set_format(sd, fh, &format);
686
687 return 0;
688}
689
690/* V4L2 subdev video operations */
691static const struct v4l2_subdev_video_ops resizer_v4l2_video_ops = {
692 .s_stream = resizer_set_stream,
693};
694
695/* V4L2 subdev pad operations */
696static const struct v4l2_subdev_pad_ops resizer_v4l2_pad_ops = {
697 .enum_mbus_code = resizer_enum_mbus_code,
698 .enum_frame_size = resizer_enum_frame_size,
699 .get_fmt = resizer_get_format,
700 .set_fmt = resizer_set_format,
701 .link_validate = resizer_link_validate,
702};
703
704/* V4L2 subdev operations */
705static const struct v4l2_subdev_ops resizer_v4l2_ops = {
706 .video = &resizer_v4l2_video_ops,
707 .pad = &resizer_v4l2_pad_ops,
708};
709
710/* V4L2 subdev internal operations */
711static const struct v4l2_subdev_internal_ops resizer_v4l2_internal_ops = {
712 .open = resizer_init_formats,
713};
714
715/* -----------------------------------------------------------------------------
716 * Media entity operations
717 */
718
719/*
720 * resizer_link_setup - Setup RESIZER connections
721 * @entity: RESIZER media entity
722 * @local: Pad at the local end of the link
723 * @remote: Pad at the remote end of the link
724 * @flags: Link flags
725 *
726 * return -EINVAL or zero on success
727 */
728static int resizer_link_setup(struct media_entity *entity,
729 const struct media_pad *local,
730 const struct media_pad *remote, u32 flags)
731{
732 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
733 struct iss_resizer_device *resizer = v4l2_get_subdevdata(sd);
734 struct iss_device *iss = to_iss_device(resizer);
735
736 switch (local->index | media_entity_type(remote->entity)) {
737 case RESIZER_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
738 /* Read from IPIPE or IPIPEIF. */
739 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
740 resizer->input = RESIZER_INPUT_NONE;
741 break;
742 }
743
744 if (resizer->input != RESIZER_INPUT_NONE)
745 return -EBUSY;
746
747 if (remote->entity == &iss->ipipeif.subdev.entity)
748 resizer->input = RESIZER_INPUT_IPIPEIF;
749 else if (remote->entity == &iss->ipipe.subdev.entity)
750 resizer->input = RESIZER_INPUT_IPIPE;
751
752
753 break;
754
755 case RESIZER_PAD_SOURCE_MEM | MEDIA_ENT_T_DEVNODE:
756 /* Write to memory */
757 if (flags & MEDIA_LNK_FL_ENABLED) {
758 if (resizer->output & ~RESIZER_OUTPUT_MEMORY)
759 return -EBUSY;
760 resizer->output |= RESIZER_OUTPUT_MEMORY;
761 } else {
762 resizer->output &= ~RESIZER_OUTPUT_MEMORY;
763 }
764 break;
765
766 default:
767 return -EINVAL;
768 }
769
770 return 0;
771}
772
773/* media operations */
774static const struct media_entity_operations resizer_media_ops = {
775 .link_setup = resizer_link_setup,
776 .link_validate = v4l2_subdev_link_validate,
777};
778
779/*
780 * resizer_init_entities - Initialize V4L2 subdev and media entity
781 * @resizer: ISS ISP RESIZER module
782 *
783 * Return 0 on success and a negative error code on failure.
784 */
785static int resizer_init_entities(struct iss_resizer_device *resizer)
786{
787 struct v4l2_subdev *sd = &resizer->subdev;
788 struct media_pad *pads = resizer->pads;
789 struct media_entity *me = &sd->entity;
790 int ret;
791
792 resizer->input = RESIZER_INPUT_NONE;
793
794 v4l2_subdev_init(sd, &resizer_v4l2_ops);
795 sd->internal_ops = &resizer_v4l2_internal_ops;
796 strlcpy(sd->name, "OMAP4 ISS ISP resizer", sizeof(sd->name));
797 sd->grp_id = 1 << 16; /* group ID for iss subdevs */
798 v4l2_set_subdevdata(sd, resizer);
799 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
800
801 pads[RESIZER_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
802 pads[RESIZER_PAD_SOURCE_MEM].flags = MEDIA_PAD_FL_SOURCE;
803
804 me->ops = &resizer_media_ops;
805 ret = media_entity_init(me, RESIZER_PADS_NUM, pads, 0);
806 if (ret < 0)
807 return ret;
808
809 resizer_init_formats(sd, NULL);
810
811 resizer->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
812 resizer->video_out.ops = &resizer_video_ops;
813 resizer->video_out.iss = to_iss_device(resizer);
814 resizer->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
815 resizer->video_out.bpl_alignment = 32;
816 resizer->video_out.bpl_zero_padding = 1;
817 resizer->video_out.bpl_max = 0x1ffe0;
818
819 ret = omap4iss_video_init(&resizer->video_out, "ISP resizer a");
820 if (ret < 0)
821 return ret;
822
823 /* Connect the RESIZER subdev to the video node. */
Laurent Pincharta0fe0292013-12-03 21:28:37 -0300824 ret = media_entity_create_link(&resizer->subdev.entity,
825 RESIZER_PAD_SOURCE_MEM,
826 &resizer->video_out.video.entity, 0, 0);
Sergio Aguirre69c536b2011-01-24 15:48:19 -0300827 if (ret < 0)
828 return ret;
829
830 return 0;
831}
832
833void omap4iss_resizer_unregister_entities(struct iss_resizer_device *resizer)
834{
835 media_entity_cleanup(&resizer->subdev.entity);
836
837 v4l2_device_unregister_subdev(&resizer->subdev);
838 omap4iss_video_unregister(&resizer->video_out);
839}
840
841int omap4iss_resizer_register_entities(struct iss_resizer_device *resizer,
842 struct v4l2_device *vdev)
843{
844 int ret;
845
846 /* Register the subdev and video node. */
847 ret = v4l2_device_register_subdev(vdev, &resizer->subdev);
848 if (ret < 0)
849 goto error;
850
851 ret = omap4iss_video_register(&resizer->video_out, vdev);
852 if (ret < 0)
853 goto error;
854
855 return 0;
856
857error:
858 omap4iss_resizer_unregister_entities(resizer);
859 return ret;
860}
861
862/* -----------------------------------------------------------------------------
863 * ISP RESIZER initialisation and cleanup
864 */
865
866/*
867 * omap4iss_resizer_init - RESIZER module initialization.
868 * @iss: Device pointer specific to the OMAP4 ISS.
869 *
870 * TODO: Get the initialisation values from platform data.
871 *
872 * Return 0 on success or a negative error code otherwise.
873 */
874int omap4iss_resizer_init(struct iss_device *iss)
875{
876 struct iss_resizer_device *resizer = &iss->resizer;
877
878 resizer->state = ISS_PIPELINE_STREAM_STOPPED;
879 init_waitqueue_head(&resizer->wait);
880
881 return resizer_init_entities(resizer);
882}
883
884/*
885 * omap4iss_resizer_cleanup - RESIZER module cleanup.
886 * @iss: Device pointer specific to the OMAP4 ISS.
887 */
888void omap4iss_resizer_cleanup(struct iss_device *iss)
889{
890 /* FIXME: are you sure there's nothing to do? */
891}