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