1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * V4L2 driver for the JPEG encoder/decoder from i.MX8QXP/i.MX8QM application 4 * processors. 5 * 6 * The multi-planar buffers API is used. 7 * 8 * Baseline and extended sequential jpeg decoding is supported. 9 * Progressive jpeg decoding is not supported by the IP. 10 * Supports encode and decode of various formats: 11 * YUV444, YUV422, YUV420, BGR, ABGR, Gray 12 * YUV420 is the only multi-planar format supported. 13 * Minimum resolution is 64 x 64, maximum 8192 x 8192. 14 * To achieve 8192 x 8192, modify in defconfig: CONFIG_CMA_SIZE_MBYTES=320 15 * The alignment requirements for the resolution depend on the format, 16 * multiple of 16 resolutions should work for all formats. 17 * Special workarounds are made in the driver to support NV12 1080p. 18 * When decoding, the driver detects image resolution and pixel format 19 * from the jpeg stream, by parsing the jpeg markers. 20 * 21 * The IP has 4 slots available for context switching, but only slot 0 22 * was fully tested to work. Context switching is not used by the driver. 23 * Each driver instance (context) allocates a slot for itself, but this 24 * is postponed until device_run, to allow unlimited opens. 25 * 26 * The driver submits jobs to the IP by setting up a descriptor for the 27 * used slot, and then validating it. The encoder has an additional descriptor 28 * for the configuration phase. The driver expects FRM_DONE interrupt from 29 * IP to mark the job as finished. 30 * 31 * The decoder IP has some limitations regarding the component ID's, 32 * but the driver works around this by replacing them in the jpeg stream. 33 * 34 * A module parameter is available for debug purpose (jpeg_tracing), to enable 35 * it, enable dynamic debug for this module and: 36 * echo 1 > /sys/module/mxc_jpeg_encdec/parameters/jpeg_tracing 37 * 38 * This is inspired by the drivers/media/platform/samsung/s5p-jpeg driver 39 * 40 * Copyright 2018-2019 NXP 41 */ 42 43 #include <linux/kernel.h> 44 #include <linux/module.h> 45 #include <linux/io.h> 46 #include <linux/clk.h> 47 #include <linux/of_platform.h> 48 #include <linux/platform_device.h> 49 #include <linux/slab.h> 50 #include <linux/irqreturn.h> 51 #include <linux/interrupt.h> 52 #include <linux/pm_runtime.h> 53 #include <linux/pm_domain.h> 54 #include <linux/string.h> 55 56 #include <media/v4l2-jpeg.h> 57 #include <media/v4l2-mem2mem.h> 58 #include <media/v4l2-ioctl.h> 59 #include <media/v4l2-common.h> 60 #include <media/v4l2-event.h> 61 #include <media/videobuf2-dma-contig.h> 62 63 #include "mxc-jpeg-hw.h" 64 #include "mxc-jpeg.h" 65 66 static const struct mxc_jpeg_fmt mxc_formats[] = { 67 { 68 .name = "JPEG", 69 .fourcc = V4L2_PIX_FMT_JPEG, 70 .subsampling = -1, 71 .nc = -1, 72 .mem_planes = 1, 73 .comp_planes = 1, 74 .flags = MXC_JPEG_FMT_TYPE_ENC, 75 }, 76 { 77 .name = "BGR", /*BGR packed format*/ 78 .fourcc = V4L2_PIX_FMT_BGR24, 79 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444, 80 .nc = 3, 81 .depth = 24, 82 .mem_planes = 1, 83 .comp_planes = 1, 84 .h_align = 3, 85 .v_align = 3, 86 .flags = MXC_JPEG_FMT_TYPE_RAW, 87 .precision = 8, 88 .is_rgb = 1, 89 }, 90 { 91 .name = "ABGR", /* ABGR packed format */ 92 .fourcc = V4L2_PIX_FMT_ABGR32, 93 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444, 94 .nc = 4, 95 .depth = 32, 96 .mem_planes = 1, 97 .comp_planes = 1, 98 .h_align = 3, 99 .v_align = 3, 100 .flags = MXC_JPEG_FMT_TYPE_RAW, 101 .precision = 8, 102 .is_rgb = 1, 103 }, 104 { 105 .name = "YUV420", /* 1st plane = Y, 2nd plane = UV */ 106 .fourcc = V4L2_PIX_FMT_NV12M, 107 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420, 108 .nc = 3, 109 .depth = 12, /* 6 bytes (4Y + UV) for 4 pixels */ 110 .mem_planes = 2, 111 .comp_planes = 2, /* 1 plane Y, 1 plane UV interleaved */ 112 .h_align = 4, 113 .v_align = 4, 114 .flags = MXC_JPEG_FMT_TYPE_RAW, 115 .precision = 8, 116 }, 117 { 118 .name = "YUV420", /* 1st plane = Y, 2nd plane = UV */ 119 .fourcc = V4L2_PIX_FMT_NV12, 120 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420, 121 .nc = 3, 122 .depth = 12, /* 6 bytes (4Y + UV) for 4 pixels */ 123 .mem_planes = 1, 124 .comp_planes = 2, /* 1 plane Y, 1 plane UV interleaved */ 125 .h_align = 4, 126 .v_align = 4, 127 .flags = MXC_JPEG_FMT_TYPE_RAW, 128 .precision = 8, 129 }, 130 { 131 .name = "YUV422", /* YUYV */ 132 .fourcc = V4L2_PIX_FMT_YUYV, 133 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422, 134 .nc = 3, 135 .depth = 16, 136 .mem_planes = 1, 137 .comp_planes = 1, 138 .h_align = 4, 139 .v_align = 3, 140 .flags = MXC_JPEG_FMT_TYPE_RAW, 141 .precision = 8, 142 }, 143 { 144 .name = "YUV444", /* YUVYUV */ 145 .fourcc = V4L2_PIX_FMT_YUV24, 146 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444, 147 .nc = 3, 148 .depth = 24, 149 .mem_planes = 1, 150 .comp_planes = 1, 151 .h_align = 3, 152 .v_align = 3, 153 .flags = MXC_JPEG_FMT_TYPE_RAW, 154 .precision = 8, 155 }, 156 { 157 .name = "Gray", /* Gray (Y8/Y12) or Single Comp */ 158 .fourcc = V4L2_PIX_FMT_GREY, 159 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY, 160 .nc = 1, 161 .depth = 8, 162 .mem_planes = 1, 163 .comp_planes = 1, 164 .h_align = 3, 165 .v_align = 3, 166 .flags = MXC_JPEG_FMT_TYPE_RAW, 167 .precision = 8, 168 }, 169 }; 170 171 #define MXC_JPEG_NUM_FORMATS ARRAY_SIZE(mxc_formats) 172 173 static const int mxc_decode_mode = MXC_JPEG_DECODE; 174 static const int mxc_encode_mode = MXC_JPEG_ENCODE; 175 176 static const struct of_device_id mxc_jpeg_match[] = { 177 { 178 .compatible = "nxp,imx8qxp-jpgdec", 179 .data = &mxc_decode_mode, 180 }, 181 { 182 .compatible = "nxp,imx8qxp-jpgenc", 183 .data = &mxc_encode_mode, 184 }, 185 { }, 186 }; 187 188 /* 189 * default configuration stream, 64x64 yuv422 190 * split by JPEG marker, so it's easier to modify & use 191 */ 192 static const unsigned char jpeg_soi[] = { 193 0xFF, 0xD8 194 }; 195 196 static const unsigned char jpeg_app0[] = { 197 0xFF, 0xE0, 198 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 199 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 200 0x00, 0x00 201 }; 202 203 static const unsigned char jpeg_app14[] = { 204 0xFF, 0xEE, 205 0x00, 0x0E, 0x41, 0x64, 0x6F, 0x62, 0x65, 206 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00 207 }; 208 209 static const unsigned char jpeg_dqt[] = { 210 0xFF, 0xDB, 211 0x00, 0x84, 0x00, 0x10, 0x0B, 0x0C, 0x0E, 212 0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12, 213 0x11, 0x10, 0x13, 0x18, 0x28, 0x1A, 0x18, 214 0x16, 0x16, 0x18, 0x31, 0x23, 0x25, 0x1D, 215 0x28, 0x3A, 0x33, 0x3D, 0x3C, 0x39, 0x33, 216 0x38, 0x37, 0x40, 0x48, 0x5C, 0x4E, 0x40, 217 0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6D, 218 0x51, 0x57, 0x5F, 0x62, 0x67, 0x68, 0x67, 219 0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64, 0x78, 220 0x5C, 0x65, 0x67, 0x63, 0x01, 0x11, 0x12, 221 0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A, 222 0x2F, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63, 223 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 224 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 225 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 226 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 227 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 228 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 229 0x63, 0x63, 0x63, 0x63, 0x63, 0x63 230 }; 231 232 static const unsigned char jpeg_sof_maximal[] = { 233 0xFF, 0xC0, 234 0x00, 0x14, 0x08, 0x00, 0x40, 0x00, 0x40, 235 0x04, 0x01, 0x11, 0x00, 0x02, 0x11, 0x01, 236 0x03, 0x11, 0x01, 0x04, 0x11, 0x01 237 }; 238 239 static const unsigned char jpeg_dht[] = { 240 0xFF, 0xC4, 241 0x01, 0xA2, 0x00, 0x00, 0x01, 0x05, 0x01, 242 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 244 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 245 0x09, 0x0A, 0x0B, 0x10, 0x00, 0x02, 0x01, 246 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 247 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01, 248 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 249 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 250 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 251 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 252 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 253 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 254 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 255 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 256 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 257 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 258 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 259 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 260 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 261 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 262 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 263 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 264 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 265 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 266 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 267 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 268 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 269 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 270 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 271 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 272 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 273 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 274 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 275 0x0B, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 276 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 277 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 278 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 279 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 280 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 281 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 282 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 283 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 284 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A, 285 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 286 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 287 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 288 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 289 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 290 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 291 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 292 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 293 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 294 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 295 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 296 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 297 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 298 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 299 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 300 0xF6, 0xF7, 0xF8, 0xF9, 0xFA 301 }; 302 303 static const unsigned char jpeg_dri[] = { 304 0xFF, 0xDD, 305 0x00, 0x04, 0x00, 0x20 306 }; 307 308 static const unsigned char jpeg_sos_maximal[] = { 309 0xFF, 0xDA, 310 0x00, 0x0C, 0x04, 0x01, 0x00, 0x02, 0x11, 0x03, 311 0x11, 0x04, 0x11, 0x00, 0x3F, 0x00 312 }; 313 314 static const unsigned char jpeg_image_red[] = { 315 0xFC, 0x5F, 0xA2, 0xBF, 0xCA, 0x73, 0xFE, 0xFE, 316 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 317 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 318 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 319 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 320 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 321 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 322 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 323 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 324 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 325 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00 326 }; 327 328 static const unsigned char jpeg_eoi[] = { 329 0xFF, 0xD9 330 }; 331 332 struct mxc_jpeg_src_buf { 333 /* common v4l buffer stuff -- must be first */ 334 struct vb2_v4l2_buffer b; 335 struct list_head list; 336 337 /* mxc-jpeg specific */ 338 bool dht_needed; 339 bool jpeg_parse_error; 340 const struct mxc_jpeg_fmt *fmt; 341 int w; 342 int h; 343 }; 344 345 static inline struct mxc_jpeg_src_buf *vb2_to_mxc_buf(struct vb2_buffer *vb) 346 { 347 return container_of(to_vb2_v4l2_buffer(vb), 348 struct mxc_jpeg_src_buf, b); 349 } 350 351 static unsigned int debug; 352 module_param(debug, int, 0644); 353 MODULE_PARM_DESC(debug, "Debug level (0-3)"); 354 355 static unsigned int hw_timeout = 2000; 356 module_param(hw_timeout, int, 0644); 357 MODULE_PARM_DESC(hw_timeout, "MXC JPEG hw timeout, the number of milliseconds"); 358 359 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision); 360 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q); 361 362 static void _bswap16(u16 *a) 363 { 364 *a = ((*a & 0x00FF) << 8) | ((*a & 0xFF00) >> 8); 365 } 366 367 static void print_mxc_buf(struct mxc_jpeg_dev *jpeg, struct vb2_buffer *buf, 368 unsigned long len) 369 { 370 unsigned int plane_no; 371 u32 dma_addr; 372 void *vaddr; 373 unsigned long payload; 374 375 if (debug < 3) 376 return; 377 378 for (plane_no = 0; plane_no < buf->num_planes; plane_no++) { 379 payload = vb2_get_plane_payload(buf, plane_no); 380 if (len == 0) 381 len = payload; 382 dma_addr = vb2_dma_contig_plane_dma_addr(buf, plane_no); 383 vaddr = vb2_plane_vaddr(buf, plane_no); 384 v4l2_dbg(3, debug, &jpeg->v4l2_dev, 385 "plane %d (vaddr=%p dma_addr=%x payload=%ld):", 386 plane_no, vaddr, dma_addr, payload); 387 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 388 vaddr, len, false); 389 } 390 } 391 392 static inline struct mxc_jpeg_ctx *mxc_jpeg_fh_to_ctx(struct v4l2_fh *fh) 393 { 394 return container_of(fh, struct mxc_jpeg_ctx, fh); 395 } 396 397 static int enum_fmt(const struct mxc_jpeg_fmt *mxc_formats, int n, 398 struct v4l2_fmtdesc *f, u32 type) 399 { 400 int i, num = 0; 401 402 for (i = 0; i < n; ++i) { 403 if (mxc_formats[i].flags == type) { 404 /* index-th format of searched type found ? */ 405 if (num == f->index) 406 break; 407 /* Correct type but haven't reached our index yet, 408 * just increment per-type index 409 */ 410 ++num; 411 } 412 } 413 414 /* Format not found */ 415 if (i >= n) 416 return -EINVAL; 417 418 f->pixelformat = mxc_formats[i].fourcc; 419 420 return 0; 421 } 422 423 static const struct mxc_jpeg_fmt *mxc_jpeg_find_format(struct mxc_jpeg_ctx *ctx, 424 u32 pixelformat) 425 { 426 unsigned int k; 427 428 for (k = 0; k < MXC_JPEG_NUM_FORMATS; k++) { 429 const struct mxc_jpeg_fmt *fmt = &mxc_formats[k]; 430 431 if (fmt->fourcc == pixelformat) 432 return fmt; 433 } 434 return NULL; 435 } 436 437 static enum mxc_jpeg_image_format mxc_jpeg_fourcc_to_imgfmt(u32 fourcc) 438 { 439 switch (fourcc) { 440 case V4L2_PIX_FMT_GREY: 441 return MXC_JPEG_GRAY; 442 case V4L2_PIX_FMT_YUYV: 443 return MXC_JPEG_YUV422; 444 case V4L2_PIX_FMT_NV12: 445 case V4L2_PIX_FMT_NV12M: 446 return MXC_JPEG_YUV420; 447 case V4L2_PIX_FMT_YUV24: 448 return MXC_JPEG_YUV444; 449 case V4L2_PIX_FMT_BGR24: 450 return MXC_JPEG_BGR; 451 case V4L2_PIX_FMT_ABGR32: 452 return MXC_JPEG_ABGR; 453 default: 454 return MXC_JPEG_INVALID; 455 } 456 } 457 458 static struct mxc_jpeg_q_data *mxc_jpeg_get_q_data(struct mxc_jpeg_ctx *ctx, 459 enum v4l2_buf_type type) 460 { 461 if (V4L2_TYPE_IS_OUTPUT(type)) 462 return &ctx->out_q; 463 return &ctx->cap_q; 464 } 465 466 static void mxc_jpeg_addrs(struct mxc_jpeg_desc *desc, 467 struct vb2_buffer *raw_buf, 468 struct vb2_buffer *jpeg_buf, int offset) 469 { 470 int img_fmt = desc->stm_ctrl & STM_CTRL_IMAGE_FORMAT_MASK; 471 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(raw_buf->vb2_queue); 472 struct mxc_jpeg_q_data *q_data; 473 474 q_data = mxc_jpeg_get_q_data(ctx, raw_buf->type); 475 desc->buf_base0 = vb2_dma_contig_plane_dma_addr(raw_buf, 0); 476 desc->buf_base1 = 0; 477 if (img_fmt == STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV420)) { 478 if (raw_buf->num_planes == 2) 479 desc->buf_base1 = vb2_dma_contig_plane_dma_addr(raw_buf, 1); 480 else 481 desc->buf_base1 = desc->buf_base0 + q_data->sizeimage[0]; 482 } 483 desc->stm_bufbase = vb2_dma_contig_plane_dma_addr(jpeg_buf, 0) + 484 offset; 485 } 486 487 static void notify_eos(struct mxc_jpeg_ctx *ctx) 488 { 489 const struct v4l2_event ev = { 490 .type = V4L2_EVENT_EOS 491 }; 492 493 dev_dbg(ctx->mxc_jpeg->dev, "Notify app event EOS reached"); 494 v4l2_event_queue_fh(&ctx->fh, &ev); 495 } 496 497 static void notify_src_chg(struct mxc_jpeg_ctx *ctx) 498 { 499 const struct v4l2_event ev = { 500 .type = V4L2_EVENT_SOURCE_CHANGE, 501 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, 502 }; 503 504 dev_dbg(ctx->mxc_jpeg->dev, "Notify app event SRC_CH_RESOLUTION"); 505 v4l2_event_queue_fh(&ctx->fh, &ev); 506 } 507 508 static int mxc_get_free_slot(struct mxc_jpeg_slot_data slot_data[], int n) 509 { 510 int free_slot = 0; 511 512 while (slot_data[free_slot].used && free_slot < n) 513 free_slot++; 514 515 return free_slot; /* >=n when there are no more free slots */ 516 } 517 518 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg, 519 unsigned int slot) 520 { 521 struct mxc_jpeg_desc *desc; 522 struct mxc_jpeg_desc *cfg_desc; 523 void *cfg_stm; 524 525 if (jpeg->slot_data[slot].desc) 526 goto skip_alloc; /* already allocated, reuse it */ 527 528 /* allocate descriptor for decoding/encoding phase */ 529 desc = dma_alloc_coherent(jpeg->dev, 530 sizeof(struct mxc_jpeg_desc), 531 &jpeg->slot_data[slot].desc_handle, 532 GFP_ATOMIC); 533 if (!desc) 534 goto err; 535 jpeg->slot_data[slot].desc = desc; 536 537 /* allocate descriptor for configuration phase (encoder only) */ 538 cfg_desc = dma_alloc_coherent(jpeg->dev, 539 sizeof(struct mxc_jpeg_desc), 540 &jpeg->slot_data[slot].cfg_desc_handle, 541 GFP_ATOMIC); 542 if (!cfg_desc) 543 goto err; 544 jpeg->slot_data[slot].cfg_desc = cfg_desc; 545 546 /* allocate configuration stream */ 547 cfg_stm = dma_alloc_coherent(jpeg->dev, 548 MXC_JPEG_MAX_CFG_STREAM, 549 &jpeg->slot_data[slot].cfg_stream_handle, 550 GFP_ATOMIC); 551 if (!cfg_stm) 552 goto err; 553 jpeg->slot_data[slot].cfg_stream_vaddr = cfg_stm; 554 555 skip_alloc: 556 jpeg->slot_data[slot].used = true; 557 558 return true; 559 err: 560 dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", slot); 561 562 return false; 563 } 564 565 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg, 566 unsigned int slot) 567 { 568 if (slot >= MXC_MAX_SLOTS) { 569 dev_err(jpeg->dev, "Invalid slot %d, nothing to free.", slot); 570 return; 571 } 572 573 /* free descriptor for decoding/encoding phase */ 574 dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc), 575 jpeg->slot_data[slot].desc, 576 jpeg->slot_data[slot].desc_handle); 577 578 /* free descriptor for encoder configuration phase / decoder DHT */ 579 dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc), 580 jpeg->slot_data[slot].cfg_desc, 581 jpeg->slot_data[slot].cfg_desc_handle); 582 583 /* free configuration stream */ 584 dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM, 585 jpeg->slot_data[slot].cfg_stream_vaddr, 586 jpeg->slot_data[slot].cfg_stream_handle); 587 588 jpeg->slot_data[slot].used = false; 589 } 590 591 static void mxc_jpeg_check_and_set_last_buffer(struct mxc_jpeg_ctx *ctx, 592 struct vb2_v4l2_buffer *src_buf, 593 struct vb2_v4l2_buffer *dst_buf) 594 { 595 if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) { 596 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 597 v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx); 598 notify_eos(ctx); 599 ctx->header_parsed = false; 600 } 601 } 602 603 static void mxc_jpeg_job_finish(struct mxc_jpeg_ctx *ctx, enum vb2_buffer_state state, bool reset) 604 { 605 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 606 void __iomem *reg = jpeg->base_reg; 607 struct vb2_v4l2_buffer *src_buf, *dst_buf; 608 609 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 610 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 611 mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf); 612 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 613 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 614 v4l2_m2m_buf_done(src_buf, state); 615 v4l2_m2m_buf_done(dst_buf, state); 616 617 mxc_jpeg_disable_irq(reg, ctx->slot); 618 ctx->mxc_jpeg->slot_data[ctx->slot].used = false; 619 if (reset) 620 mxc_jpeg_sw_reset(reg); 621 } 622 623 static u32 mxc_jpeg_get_plane_size(struct mxc_jpeg_q_data *q_data, u32 plane_no) 624 { 625 const struct mxc_jpeg_fmt *fmt = q_data->fmt; 626 u32 size; 627 int i; 628 629 if (plane_no >= fmt->mem_planes) 630 return 0; 631 632 if (fmt->mem_planes == fmt->comp_planes) 633 return q_data->sizeimage[plane_no]; 634 635 if (plane_no < fmt->mem_planes - 1) 636 return q_data->sizeimage[plane_no]; 637 638 size = q_data->sizeimage[fmt->mem_planes - 1]; 639 640 /* Should be impossible given mxc_formats. */ 641 if (WARN_ON_ONCE(fmt->comp_planes > ARRAY_SIZE(q_data->sizeimage))) 642 return size; 643 644 for (i = fmt->mem_planes; i < fmt->comp_planes; i++) 645 size += q_data->sizeimage[i]; 646 647 return size; 648 } 649 650 static irqreturn_t mxc_jpeg_dec_irq(int irq, void *priv) 651 { 652 struct mxc_jpeg_dev *jpeg = priv; 653 struct mxc_jpeg_ctx *ctx; 654 void __iomem *reg = jpeg->base_reg; 655 struct device *dev = jpeg->dev; 656 struct vb2_v4l2_buffer *src_buf, *dst_buf; 657 struct mxc_jpeg_src_buf *jpeg_src_buf; 658 enum vb2_buffer_state buf_state; 659 u32 dec_ret, com_status; 660 unsigned long payload; 661 struct mxc_jpeg_q_data *q_data; 662 enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 663 unsigned int slot; 664 665 spin_lock(&jpeg->hw_lock); 666 667 com_status = readl(reg + COM_STATUS); 668 slot = COM_STATUS_CUR_SLOT(com_status); 669 dev_dbg(dev, "Irq %d on slot %d.\n", irq, slot); 670 671 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); 672 if (WARN_ON(!ctx)) 673 goto job_unlock; 674 675 if (slot != ctx->slot) { 676 /* TODO investigate when adding multi-instance support */ 677 dev_warn(dev, "IRQ slot %d != context slot %d.\n", 678 slot, ctx->slot); 679 goto job_unlock; 680 } 681 682 if (!jpeg->slot_data[slot].used) 683 goto job_unlock; 684 685 dec_ret = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); 686 writel(dec_ret, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); /* w1c */ 687 688 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 689 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 690 if (!dst_buf || !src_buf) { 691 dev_err(dev, "No source or destination buffer.\n"); 692 goto job_unlock; 693 } 694 jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf); 695 696 if (dec_ret & SLOT_STATUS_ENC_CONFIG_ERR) { 697 u32 ret = readl(reg + CAST_STATUS12); 698 699 dev_err(dev, "Encoder/decoder error, status=0x%08x", ret); 700 mxc_jpeg_sw_reset(reg); 701 buf_state = VB2_BUF_STATE_ERROR; 702 goto buffers_done; 703 } 704 705 if (!(dec_ret & SLOT_STATUS_FRMDONE)) 706 goto job_unlock; 707 708 if (jpeg->mode == MXC_JPEG_ENCODE && 709 ctx->enc_state == MXC_JPEG_ENC_CONF) { 710 ctx->enc_state = MXC_JPEG_ENCODING; 711 dev_dbg(dev, "Encoder config finished. Start encoding...\n"); 712 mxc_jpeg_enc_set_quality(dev, reg, ctx->jpeg_quality); 713 mxc_jpeg_enc_mode_go(dev, reg); 714 goto job_unlock; 715 } 716 if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed) { 717 jpeg_src_buf->dht_needed = false; 718 dev_dbg(dev, "Decoder DHT cfg finished. Start decoding...\n"); 719 goto job_unlock; 720 } 721 722 if (jpeg->mode == MXC_JPEG_ENCODE) { 723 payload = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_BUF_PTR)); 724 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload); 725 dev_dbg(dev, "Encoding finished, payload size: %ld\n", 726 payload); 727 } else { 728 q_data = mxc_jpeg_get_q_data(ctx, cap_type); 729 payload = mxc_jpeg_get_plane_size(q_data, 0); 730 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload); 731 vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0); 732 if (q_data->fmt->mem_planes == 2) { 733 payload = mxc_jpeg_get_plane_size(q_data, 1); 734 vb2_set_plane_payload(&dst_buf->vb2_buf, 1, payload); 735 } 736 dev_dbg(dev, "Decoding finished, payload size: %ld + %ld\n", 737 vb2_get_plane_payload(&dst_buf->vb2_buf, 0), 738 vb2_get_plane_payload(&dst_buf->vb2_buf, 1)); 739 } 740 741 /* short preview of the results */ 742 dev_dbg(dev, "src_buf preview: "); 743 print_mxc_buf(jpeg, &src_buf->vb2_buf, 32); 744 dev_dbg(dev, "dst_buf preview: "); 745 print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32); 746 buf_state = VB2_BUF_STATE_DONE; 747 748 buffers_done: 749 mxc_jpeg_job_finish(ctx, buf_state, false); 750 spin_unlock(&jpeg->hw_lock); 751 cancel_delayed_work(&ctx->task_timer); 752 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 753 return IRQ_HANDLED; 754 job_unlock: 755 spin_unlock(&jpeg->hw_lock); 756 return IRQ_HANDLED; 757 } 758 759 static int mxc_jpeg_fixup_sof(struct mxc_jpeg_sof *sof, 760 u32 fourcc, 761 u16 w, u16 h) 762 { 763 int sof_length; 764 765 sof->precision = 8; /* TODO allow 8/12 bit precision*/ 766 sof->height = h; 767 _bswap16(&sof->height); 768 sof->width = w; 769 _bswap16(&sof->width); 770 771 switch (fourcc) { 772 case V4L2_PIX_FMT_NV12: 773 case V4L2_PIX_FMT_NV12M: 774 sof->components_no = 3; 775 sof->comp[0].v = 0x2; 776 sof->comp[0].h = 0x2; 777 break; 778 case V4L2_PIX_FMT_YUYV: 779 sof->components_no = 3; 780 sof->comp[0].v = 0x1; 781 sof->comp[0].h = 0x2; 782 break; 783 case V4L2_PIX_FMT_YUV24: 784 case V4L2_PIX_FMT_BGR24: 785 default: 786 sof->components_no = 3; 787 break; 788 case V4L2_PIX_FMT_ABGR32: 789 sof->components_no = 4; 790 break; 791 case V4L2_PIX_FMT_GREY: 792 sof->components_no = 1; 793 break; 794 } 795 sof_length = 8 + 3 * sof->components_no; 796 sof->length = sof_length; 797 _bswap16(&sof->length); 798 799 return sof_length; /* not swaped */ 800 } 801 802 static int mxc_jpeg_fixup_sos(struct mxc_jpeg_sos *sos, 803 u32 fourcc) 804 { 805 int sos_length; 806 u8 *sof_u8 = (u8 *)sos; 807 808 switch (fourcc) { 809 case V4L2_PIX_FMT_NV12: 810 case V4L2_PIX_FMT_NV12M: 811 sos->components_no = 3; 812 break; 813 case V4L2_PIX_FMT_YUYV: 814 sos->components_no = 3; 815 break; 816 case V4L2_PIX_FMT_YUV24: 817 case V4L2_PIX_FMT_BGR24: 818 default: 819 sos->components_no = 3; 820 break; 821 case V4L2_PIX_FMT_ABGR32: 822 sos->components_no = 4; 823 break; 824 case V4L2_PIX_FMT_GREY: 825 sos->components_no = 1; 826 break; 827 } 828 sos_length = 6 + 2 * sos->components_no; 829 sos->length = sos_length; 830 _bswap16(&sos->length); 831 832 /* SOS ignorable bytes, not so ignorable after all */ 833 sof_u8[sos_length - 1] = 0x0; 834 sof_u8[sos_length - 2] = 0x3f; 835 sof_u8[sos_length - 3] = 0x0; 836 837 return sos_length; /* not swaped */ 838 } 839 840 static unsigned int mxc_jpeg_setup_cfg_stream(void *cfg_stream_vaddr, 841 u32 fourcc, 842 u16 w, u16 h) 843 { 844 /* 845 * There is a hardware issue that first 128 bytes of configuration data 846 * can't be loaded correctly. 847 * To avoid this issue, we need to write the configuration from 848 * an offset which should be no less than 0x80 (128 bytes). 849 */ 850 unsigned int offset = 0x80; 851 u8 *cfg = (u8 *)cfg_stream_vaddr; 852 struct mxc_jpeg_sof *sof; 853 struct mxc_jpeg_sos *sos; 854 855 memcpy(cfg + offset, jpeg_soi, ARRAY_SIZE(jpeg_soi)); 856 offset += ARRAY_SIZE(jpeg_soi); 857 858 if (fourcc == V4L2_PIX_FMT_BGR24 || 859 fourcc == V4L2_PIX_FMT_ABGR32) { 860 memcpy(cfg + offset, jpeg_app14, sizeof(jpeg_app14)); 861 offset += sizeof(jpeg_app14); 862 } else { 863 memcpy(cfg + offset, jpeg_app0, sizeof(jpeg_app0)); 864 offset += sizeof(jpeg_app0); 865 } 866 867 memcpy(cfg + offset, jpeg_dqt, sizeof(jpeg_dqt)); 868 offset += sizeof(jpeg_dqt); 869 870 memcpy(cfg + offset, jpeg_sof_maximal, sizeof(jpeg_sof_maximal)); 871 offset += 2; /* skip marker ID */ 872 sof = (struct mxc_jpeg_sof *)(cfg + offset); 873 offset += mxc_jpeg_fixup_sof(sof, fourcc, w, h); 874 875 memcpy(cfg + offset, jpeg_dht, sizeof(jpeg_dht)); 876 offset += sizeof(jpeg_dht); 877 878 memcpy(cfg + offset, jpeg_dri, sizeof(jpeg_dri)); 879 offset += sizeof(jpeg_dri); 880 881 memcpy(cfg + offset, jpeg_sos_maximal, sizeof(jpeg_sos_maximal)); 882 offset += 2; /* skip marker ID */ 883 sos = (struct mxc_jpeg_sos *)(cfg + offset); 884 offset += mxc_jpeg_fixup_sos(sos, fourcc); 885 886 memcpy(cfg + offset, jpeg_image_red, sizeof(jpeg_image_red)); 887 offset += sizeof(jpeg_image_red); 888 889 memcpy(cfg + offset, jpeg_eoi, sizeof(jpeg_eoi)); 890 offset += sizeof(jpeg_eoi); 891 892 return offset; 893 } 894 895 static void mxc_jpeg_config_dec_desc(struct vb2_buffer *out_buf, 896 struct mxc_jpeg_ctx *ctx, 897 struct vb2_buffer *src_buf, 898 struct vb2_buffer *dst_buf) 899 { 900 enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 901 struct mxc_jpeg_q_data *q_data_cap; 902 enum mxc_jpeg_image_format img_fmt; 903 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 904 void __iomem *reg = jpeg->base_reg; 905 unsigned int slot = ctx->slot; 906 struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc; 907 struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc; 908 dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle; 909 dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle; 910 dma_addr_t cfg_stream_handle = jpeg->slot_data[slot].cfg_stream_handle; 911 unsigned int *cfg_size = &jpeg->slot_data[slot].cfg_stream_size; 912 void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr; 913 struct mxc_jpeg_src_buf *jpeg_src_buf; 914 915 jpeg_src_buf = vb2_to_mxc_buf(src_buf); 916 917 /* setup the decoding descriptor */ 918 desc->next_descpt_ptr = 0; /* end of chain */ 919 q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type); 920 desc->imgsize = q_data_cap->w_adjusted << 16 | q_data_cap->h_adjusted; 921 img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data_cap->fmt->fourcc); 922 desc->stm_ctrl &= ~STM_CTRL_IMAGE_FORMAT(0xF); /* clear image format */ 923 desc->stm_ctrl |= STM_CTRL_IMAGE_FORMAT(img_fmt); 924 desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 925 desc->line_pitch = q_data_cap->bytesperline[0]; 926 mxc_jpeg_addrs(desc, dst_buf, src_buf, 0); 927 mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(src_buf, 0), 1024)); 928 print_descriptor_info(jpeg->dev, desc); 929 930 if (!jpeg_src_buf->dht_needed) { 931 /* validate the decoding descriptor */ 932 mxc_jpeg_set_desc(desc_handle, reg, slot); 933 return; 934 } 935 936 /* 937 * if a default huffman table is needed, use the config descriptor to 938 * inject a DHT, by chaining it before the decoding descriptor 939 */ 940 *cfg_size = mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr, 941 V4L2_PIX_FMT_YUYV, 942 MXC_JPEG_MIN_WIDTH, 943 MXC_JPEG_MIN_HEIGHT); 944 cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN; 945 cfg_desc->buf_base0 = vb2_dma_contig_plane_dma_addr(dst_buf, 0); 946 cfg_desc->buf_base1 = 0; 947 cfg_desc->imgsize = MXC_JPEG_MIN_WIDTH << 16; 948 cfg_desc->imgsize |= MXC_JPEG_MIN_HEIGHT; 949 cfg_desc->line_pitch = MXC_JPEG_MIN_WIDTH * 2; 950 cfg_desc->stm_ctrl = STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV422); 951 cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 952 cfg_desc->stm_bufbase = cfg_stream_handle; 953 cfg_desc->stm_bufsize = ALIGN(*cfg_size, 1024); 954 print_descriptor_info(jpeg->dev, cfg_desc); 955 956 /* validate the configuration descriptor */ 957 mxc_jpeg_set_desc(cfg_desc_handle, reg, slot); 958 } 959 960 static void mxc_jpeg_config_enc_desc(struct vb2_buffer *out_buf, 961 struct mxc_jpeg_ctx *ctx, 962 struct vb2_buffer *src_buf, 963 struct vb2_buffer *dst_buf) 964 { 965 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 966 void __iomem *reg = jpeg->base_reg; 967 unsigned int slot = ctx->slot; 968 struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc; 969 struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc; 970 dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle; 971 dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle; 972 void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr; 973 struct mxc_jpeg_q_data *q_data; 974 enum mxc_jpeg_image_format img_fmt; 975 int w, h; 976 977 q_data = mxc_jpeg_get_q_data(ctx, src_buf->vb2_queue->type); 978 979 jpeg->slot_data[slot].cfg_stream_size = 980 mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr, 981 q_data->fmt->fourcc, 982 q_data->crop.width, 983 q_data->crop.height); 984 985 /* chain the config descriptor with the encoding descriptor */ 986 cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN; 987 988 cfg_desc->buf_base0 = jpeg->slot_data[slot].cfg_stream_handle; 989 cfg_desc->buf_base1 = 0; 990 cfg_desc->line_pitch = 0; 991 cfg_desc->stm_bufbase = 0; /* no output expected */ 992 cfg_desc->stm_bufsize = 0x0; 993 cfg_desc->imgsize = 0; 994 cfg_desc->stm_ctrl = STM_CTRL_CONFIG_MOD(1); 995 cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 996 997 desc->next_descpt_ptr = 0; /* end of chain */ 998 999 /* use adjusted resolution for CAST IP job */ 1000 w = q_data->crop.width; 1001 h = q_data->crop.height; 1002 v4l_bound_align_image(&w, w, MXC_JPEG_MAX_WIDTH, q_data->fmt->h_align, 1003 &h, h, MXC_JPEG_MAX_HEIGHT, q_data->fmt->v_align, 0); 1004 mxc_jpeg_set_res(desc, w, h); 1005 mxc_jpeg_set_line_pitch(desc, q_data->bytesperline[0]); 1006 mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(dst_buf, 0), 1024)); 1007 img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data->fmt->fourcc); 1008 if (img_fmt == MXC_JPEG_INVALID) 1009 dev_err(jpeg->dev, "No valid image format detected\n"); 1010 desc->stm_ctrl = STM_CTRL_CONFIG_MOD(0) | 1011 STM_CTRL_IMAGE_FORMAT(img_fmt); 1012 desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 1013 mxc_jpeg_addrs(desc, src_buf, dst_buf, 0); 1014 dev_dbg(jpeg->dev, "cfg_desc:\n"); 1015 print_descriptor_info(jpeg->dev, cfg_desc); 1016 dev_dbg(jpeg->dev, "enc desc:\n"); 1017 print_descriptor_info(jpeg->dev, desc); 1018 print_wrapper_info(jpeg->dev, reg); 1019 print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE); 1020 1021 /* validate the configuration descriptor */ 1022 mxc_jpeg_set_desc(cfg_desc_handle, reg, slot); 1023 } 1024 1025 static const struct mxc_jpeg_fmt *mxc_jpeg_get_sibling_format(const struct mxc_jpeg_fmt *fmt) 1026 { 1027 int i; 1028 1029 for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) { 1030 if (mxc_formats[i].subsampling == fmt->subsampling && 1031 mxc_formats[i].nc == fmt->nc && 1032 mxc_formats[i].precision == fmt->precision && 1033 mxc_formats[i].is_rgb == fmt->is_rgb && 1034 mxc_formats[i].fourcc != fmt->fourcc) 1035 return &mxc_formats[i]; 1036 } 1037 1038 return NULL; 1039 } 1040 1041 static bool mxc_jpeg_compare_format(const struct mxc_jpeg_fmt *fmt1, 1042 const struct mxc_jpeg_fmt *fmt2) 1043 { 1044 if (fmt1 == fmt2) 1045 return true; 1046 if (mxc_jpeg_get_sibling_format(fmt1) == fmt2) 1047 return true; 1048 return false; 1049 } 1050 1051 static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx *ctx, 1052 struct mxc_jpeg_src_buf *jpeg_src_buf) 1053 { 1054 struct device *dev = ctx->mxc_jpeg->dev; 1055 struct mxc_jpeg_q_data *q_data_cap; 1056 1057 if (!jpeg_src_buf->fmt) 1058 return false; 1059 1060 q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1061 if (mxc_jpeg_compare_format(q_data_cap->fmt, jpeg_src_buf->fmt)) 1062 jpeg_src_buf->fmt = q_data_cap->fmt; 1063 if (q_data_cap->fmt != jpeg_src_buf->fmt || 1064 q_data_cap->w != jpeg_src_buf->w || 1065 q_data_cap->h != jpeg_src_buf->h) { 1066 dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n", 1067 q_data_cap->w, q_data_cap->h, 1068 jpeg_src_buf->w, jpeg_src_buf->h, 1069 (jpeg_src_buf->fmt->fourcc & 0xff), 1070 (jpeg_src_buf->fmt->fourcc >> 8) & 0xff, 1071 (jpeg_src_buf->fmt->fourcc >> 16) & 0xff, 1072 (jpeg_src_buf->fmt->fourcc >> 24) & 0xff); 1073 1074 /* 1075 * set-up the capture queue with the pixelformat and resolution 1076 * detected from the jpeg output stream 1077 */ 1078 q_data_cap->w = jpeg_src_buf->w; 1079 q_data_cap->h = jpeg_src_buf->h; 1080 q_data_cap->fmt = jpeg_src_buf->fmt; 1081 q_data_cap->w_adjusted = q_data_cap->w; 1082 q_data_cap->h_adjusted = q_data_cap->h; 1083 q_data_cap->crop.left = 0; 1084 q_data_cap->crop.top = 0; 1085 q_data_cap->crop.width = jpeg_src_buf->w; 1086 q_data_cap->crop.height = jpeg_src_buf->h; 1087 1088 /* 1089 * align up the resolution for CAST IP, 1090 * but leave the buffer resolution unchanged 1091 */ 1092 v4l_bound_align_image(&q_data_cap->w_adjusted, 1093 q_data_cap->w_adjusted, /* adjust up */ 1094 MXC_JPEG_MAX_WIDTH, 1095 q_data_cap->fmt->h_align, 1096 &q_data_cap->h_adjusted, 1097 q_data_cap->h_adjusted, /* adjust up */ 1098 MXC_JPEG_MAX_HEIGHT, 1099 q_data_cap->fmt->v_align, 1100 0); 1101 1102 /* setup bytesperline/sizeimage for capture queue */ 1103 mxc_jpeg_bytesperline(q_data_cap, jpeg_src_buf->fmt->precision); 1104 mxc_jpeg_sizeimage(q_data_cap); 1105 notify_src_chg(ctx); 1106 ctx->source_change = 1; 1107 } 1108 1109 return ctx->source_change ? true : false; 1110 } 1111 1112 static int mxc_jpeg_job_ready(void *priv) 1113 { 1114 struct mxc_jpeg_ctx *ctx = priv; 1115 1116 return ctx->source_change ? 0 : 1; 1117 } 1118 1119 static void mxc_jpeg_device_run_timeout(struct work_struct *work) 1120 { 1121 struct delayed_work *dwork = to_delayed_work(work); 1122 struct mxc_jpeg_ctx *ctx = container_of(dwork, struct mxc_jpeg_ctx, task_timer); 1123 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 1124 unsigned long flags; 1125 1126 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1127 if (ctx->slot < MXC_MAX_SLOTS && ctx->mxc_jpeg->slot_data[ctx->slot].used) { 1128 dev_warn(jpeg->dev, "%s timeout, cancel it\n", 1129 ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ? "decode" : "encode"); 1130 mxc_jpeg_job_finish(ctx, VB2_BUF_STATE_ERROR, true); 1131 v4l2_m2m_job_finish(ctx->mxc_jpeg->m2m_dev, ctx->fh.m2m_ctx); 1132 } 1133 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1134 } 1135 1136 static void mxc_jpeg_device_run(void *priv) 1137 { 1138 struct mxc_jpeg_ctx *ctx = priv; 1139 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 1140 void __iomem *reg = jpeg->base_reg; 1141 struct device *dev = jpeg->dev; 1142 struct vb2_v4l2_buffer *src_buf, *dst_buf; 1143 unsigned long flags; 1144 struct mxc_jpeg_q_data *q_data_cap, *q_data_out; 1145 struct mxc_jpeg_src_buf *jpeg_src_buf; 1146 1147 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1148 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 1149 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 1150 if (!src_buf || !dst_buf) { 1151 dev_err(dev, "Null src or dst buf\n"); 1152 goto end; 1153 } 1154 1155 q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1156 if (!q_data_cap) 1157 goto end; 1158 q_data_out = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 1159 if (!q_data_out) 1160 goto end; 1161 src_buf->sequence = q_data_out->sequence++; 1162 dst_buf->sequence = q_data_cap->sequence++; 1163 1164 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true); 1165 1166 jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf); 1167 if (q_data_cap->fmt->mem_planes != dst_buf->vb2_buf.num_planes) { 1168 dev_err(dev, "Capture format %s has %d planes, but capture buffer has %d planes\n", 1169 q_data_cap->fmt->name, q_data_cap->fmt->mem_planes, 1170 dst_buf->vb2_buf.num_planes); 1171 jpeg_src_buf->jpeg_parse_error = true; 1172 } 1173 if (jpeg_src_buf->jpeg_parse_error) { 1174 mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf); 1175 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1176 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1177 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); 1178 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); 1179 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1180 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1181 1182 return; 1183 } 1184 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) { 1185 if (ctx->source_change || mxc_jpeg_source_change(ctx, jpeg_src_buf)) { 1186 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1187 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1188 return; 1189 } 1190 } 1191 1192 mxc_jpeg_enable(reg); 1193 mxc_jpeg_set_l_endian(reg, 1); 1194 1195 ctx->slot = mxc_get_free_slot(jpeg->slot_data, MXC_MAX_SLOTS); 1196 if (ctx->slot >= MXC_MAX_SLOTS) { 1197 dev_err(dev, "No more free slots\n"); 1198 goto end; 1199 } 1200 if (!mxc_jpeg_alloc_slot_data(jpeg, ctx->slot)) { 1201 dev_err(dev, "Cannot allocate slot data\n"); 1202 goto end; 1203 } 1204 1205 mxc_jpeg_enable_slot(reg, ctx->slot); 1206 mxc_jpeg_enable_irq(reg, ctx->slot); 1207 1208 if (jpeg->mode == MXC_JPEG_ENCODE) { 1209 dev_dbg(dev, "Encoding on slot %d\n", ctx->slot); 1210 ctx->enc_state = MXC_JPEG_ENC_CONF; 1211 mxc_jpeg_config_enc_desc(&dst_buf->vb2_buf, ctx, 1212 &src_buf->vb2_buf, &dst_buf->vb2_buf); 1213 mxc_jpeg_enc_mode_conf(dev, reg); /* start config phase */ 1214 } else { 1215 dev_dbg(dev, "Decoding on slot %d\n", ctx->slot); 1216 print_mxc_buf(jpeg, &src_buf->vb2_buf, 0); 1217 mxc_jpeg_config_dec_desc(&dst_buf->vb2_buf, ctx, 1218 &src_buf->vb2_buf, &dst_buf->vb2_buf); 1219 mxc_jpeg_dec_mode_go(dev, reg); 1220 } 1221 schedule_delayed_work(&ctx->task_timer, msecs_to_jiffies(hw_timeout)); 1222 end: 1223 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1224 } 1225 1226 static int mxc_jpeg_decoder_cmd(struct file *file, void *priv, 1227 struct v4l2_decoder_cmd *cmd) 1228 { 1229 struct v4l2_fh *fh = file->private_data; 1230 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 1231 unsigned long flags; 1232 int ret; 1233 1234 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd); 1235 if (ret < 0) 1236 return ret; 1237 1238 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx))) 1239 return 0; 1240 1241 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1242 ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, cmd); 1243 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1244 if (ret < 0) 1245 return ret; 1246 1247 if (cmd->cmd == V4L2_DEC_CMD_STOP && 1248 v4l2_m2m_has_stopped(fh->m2m_ctx)) { 1249 notify_eos(ctx); 1250 ctx->header_parsed = false; 1251 } 1252 1253 if (cmd->cmd == V4L2_DEC_CMD_START && 1254 v4l2_m2m_has_stopped(fh->m2m_ctx)) 1255 vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q); 1256 return 0; 1257 } 1258 1259 static int mxc_jpeg_encoder_cmd(struct file *file, void *priv, 1260 struct v4l2_encoder_cmd *cmd) 1261 { 1262 struct v4l2_fh *fh = file->private_data; 1263 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 1264 unsigned long flags; 1265 int ret; 1266 1267 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd); 1268 if (ret < 0) 1269 return ret; 1270 1271 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)) || 1272 !vb2_is_streaming(v4l2_m2m_get_dst_vq(fh->m2m_ctx))) 1273 return 0; 1274 1275 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1276 ret = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd); 1277 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1278 if (ret < 0) 1279 return 0; 1280 1281 if (cmd->cmd == V4L2_ENC_CMD_STOP && 1282 v4l2_m2m_has_stopped(fh->m2m_ctx)) 1283 notify_eos(ctx); 1284 1285 if (cmd->cmd == V4L2_ENC_CMD_START && 1286 v4l2_m2m_has_stopped(fh->m2m_ctx)) 1287 vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q); 1288 1289 return 0; 1290 } 1291 1292 static int mxc_jpeg_queue_setup(struct vb2_queue *q, 1293 unsigned int *nbuffers, 1294 unsigned int *nplanes, 1295 unsigned int sizes[], 1296 struct device *alloc_ctxs[]) 1297 { 1298 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q); 1299 struct mxc_jpeg_q_data *q_data = NULL; 1300 int i; 1301 1302 q_data = mxc_jpeg_get_q_data(ctx, q->type); 1303 if (!q_data) 1304 return -EINVAL; 1305 1306 /* Handle CREATE_BUFS situation - *nplanes != 0 */ 1307 if (*nplanes) { 1308 if (*nplanes != q_data->fmt->mem_planes) 1309 return -EINVAL; 1310 for (i = 0; i < *nplanes; i++) { 1311 if (sizes[i] < mxc_jpeg_get_plane_size(q_data, i)) 1312 return -EINVAL; 1313 } 1314 return 0; 1315 } 1316 1317 /* Handle REQBUFS situation */ 1318 *nplanes = q_data->fmt->mem_planes; 1319 for (i = 0; i < *nplanes; i++) 1320 sizes[i] = mxc_jpeg_get_plane_size(q_data, i); 1321 1322 return 0; 1323 } 1324 1325 static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count) 1326 { 1327 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q); 1328 struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type); 1329 int ret; 1330 1331 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q); 1332 1333 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(q->type)) 1334 ctx->source_change = 0; 1335 dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx); 1336 q_data->sequence = 0; 1337 1338 ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev); 1339 if (ret < 0) { 1340 dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n"); 1341 return ret; 1342 } 1343 1344 return 0; 1345 } 1346 1347 static void mxc_jpeg_stop_streaming(struct vb2_queue *q) 1348 { 1349 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q); 1350 struct vb2_v4l2_buffer *vbuf; 1351 1352 dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx); 1353 1354 /* Release all active buffers */ 1355 for (;;) { 1356 if (V4L2_TYPE_IS_OUTPUT(q->type)) 1357 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1358 else 1359 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1360 if (!vbuf) 1361 break; 1362 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 1363 } 1364 1365 if (V4L2_TYPE_IS_OUTPUT(q->type) || !ctx->source_change) 1366 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); 1367 if (V4L2_TYPE_IS_OUTPUT(q->type) && 1368 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) { 1369 notify_eos(ctx); 1370 ctx->header_parsed = false; 1371 } 1372 1373 pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev); 1374 } 1375 1376 static int mxc_jpeg_valid_comp_id(struct device *dev, 1377 struct mxc_jpeg_sof *sof, 1378 struct mxc_jpeg_sos *sos) 1379 { 1380 int valid = 1; 1381 int i; 1382 1383 /* 1384 * there's a limitation in the IP that the component IDs must be 1385 * between 0..4, if they are not, let's patch them 1386 */ 1387 for (i = 0; i < sof->components_no; i++) 1388 if (sof->comp[i].id > MXC_JPEG_MAX_COMPONENTS) { 1389 valid = 0; 1390 dev_err(dev, "Component %d has invalid ID: %d", 1391 i, sof->comp[i].id); 1392 } 1393 if (!valid) 1394 /* patch all comp IDs if at least one is invalid */ 1395 for (i = 0; i < sof->components_no; i++) { 1396 dev_warn(dev, "Component %d ID patched to: %d", 1397 i, i + 1); 1398 sof->comp[i].id = i + 1; 1399 sos->comp[i].id = i + 1; 1400 } 1401 1402 return valid; 1403 } 1404 1405 static bool mxc_jpeg_match_image_format(const struct mxc_jpeg_fmt *fmt, 1406 const struct v4l2_jpeg_header *header) 1407 { 1408 if (fmt->subsampling != header->frame.subsampling || 1409 fmt->nc != header->frame.num_components || 1410 fmt->precision != header->frame.precision) 1411 return false; 1412 1413 /* 1414 * If the transform flag from APP14 marker is 0, images that are 1415 * encoded with 3 components have RGB colorspace, see Recommendation 1416 * ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding 1417 */ 1418 if (header->frame.subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) { 1419 u8 is_rgb = header->app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB ? 1 : 0; 1420 1421 if (is_rgb != fmt->is_rgb) 1422 return false; 1423 } 1424 return true; 1425 } 1426 1427 static u32 mxc_jpeg_get_image_format(struct device *dev, 1428 const struct v4l2_jpeg_header *header) 1429 { 1430 int i; 1431 u32 fourcc = 0; 1432 1433 for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) { 1434 if (mxc_jpeg_match_image_format(&mxc_formats[i], header)) { 1435 fourcc = mxc_formats[i].fourcc; 1436 break; 1437 } 1438 } 1439 if (fourcc == 0) { 1440 dev_err(dev, 1441 "Could not identify image format nc=%d, subsampling=%d, precision=%d\n", 1442 header->frame.num_components, 1443 header->frame.subsampling, 1444 header->frame.precision); 1445 return fourcc; 1446 } 1447 1448 return fourcc; 1449 } 1450 1451 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision) 1452 { 1453 /* Bytes distance between the leftmost pixels in two adjacent lines */ 1454 if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) { 1455 /* bytesperline unused for compressed formats */ 1456 q->bytesperline[0] = 0; 1457 q->bytesperline[1] = 0; 1458 } else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) { 1459 /* When the image format is planar the bytesperline value 1460 * applies to the first plane and is divided by the same factor 1461 * as the width field for the other planes 1462 */ 1463 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8); 1464 q->bytesperline[1] = q->bytesperline[0]; 1465 } else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) { 1466 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * 2; 1467 q->bytesperline[1] = 0; 1468 } else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) { 1469 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * q->fmt->nc; 1470 q->bytesperline[1] = 0; 1471 } else { 1472 /* grayscale */ 1473 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8); 1474 q->bytesperline[1] = 0; 1475 } 1476 } 1477 1478 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q) 1479 { 1480 if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) { 1481 /* if no sizeimage from user, assume worst jpeg compression */ 1482 if (!q->sizeimage[0]) 1483 q->sizeimage[0] = 6 * q->w * q->h; 1484 q->sizeimage[1] = 0; 1485 1486 if (q->sizeimage[0] > MXC_JPEG_MAX_SIZEIMAGE) 1487 q->sizeimage[0] = MXC_JPEG_MAX_SIZEIMAGE; 1488 1489 /* jpeg stream size must be multiple of 1K */ 1490 q->sizeimage[0] = ALIGN(q->sizeimage[0], 1024); 1491 } else { 1492 q->sizeimage[0] = q->bytesperline[0] * q->h_adjusted; 1493 q->sizeimage[1] = 0; 1494 if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) 1495 q->sizeimage[1] = q->sizeimage[0] / 2; 1496 } 1497 } 1498 1499 static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, struct vb2_buffer *vb) 1500 { 1501 struct device *dev = ctx->mxc_jpeg->dev; 1502 struct mxc_jpeg_q_data *q_data_out; 1503 struct mxc_jpeg_q_data *q_data_cap; 1504 u32 fourcc; 1505 struct v4l2_jpeg_header header; 1506 struct mxc_jpeg_sof *psof = NULL; 1507 struct mxc_jpeg_sos *psos = NULL; 1508 struct mxc_jpeg_src_buf *jpeg_src_buf = vb2_to_mxc_buf(vb); 1509 u8 *src_addr = (u8 *)vb2_plane_vaddr(vb, 0); 1510 u32 size = vb2_get_plane_payload(vb, 0); 1511 int ret; 1512 1513 memset(&header, 0, sizeof(header)); 1514 ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header); 1515 if (ret < 0) { 1516 dev_err(dev, "Error parsing JPEG stream markers\n"); 1517 return ret; 1518 } 1519 1520 /* if DHT marker present, no need to inject default one */ 1521 jpeg_src_buf->dht_needed = (header.num_dht == 0); 1522 1523 q_data_out = mxc_jpeg_get_q_data(ctx, 1524 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 1525 if (q_data_out->w == 0 && q_data_out->h == 0) { 1526 dev_warn(dev, "Invalid user resolution 0x0"); 1527 dev_warn(dev, "Keeping resolution from JPEG: %dx%d", 1528 header.frame.width, header.frame.height); 1529 } else if (header.frame.width != q_data_out->w || 1530 header.frame.height != q_data_out->h) { 1531 dev_err(dev, 1532 "Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)", 1533 header.frame.width, header.frame.height, 1534 q_data_out->w, q_data_out->h); 1535 } 1536 q_data_out->w = header.frame.width; 1537 q_data_out->h = header.frame.height; 1538 if (header.frame.width > MXC_JPEG_MAX_WIDTH || 1539 header.frame.height > MXC_JPEG_MAX_HEIGHT) { 1540 dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n", 1541 header.frame.width, header.frame.height); 1542 return -EINVAL; 1543 } 1544 if (header.frame.width < MXC_JPEG_MIN_WIDTH || 1545 header.frame.height < MXC_JPEG_MIN_HEIGHT) { 1546 dev_err(dev, "JPEG width or height should be > 64: %dx%d\n", 1547 header.frame.width, header.frame.height); 1548 return -EINVAL; 1549 } 1550 if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) { 1551 dev_err(dev, "JPEG number of components should be <=%d", 1552 V4L2_JPEG_MAX_COMPONENTS); 1553 return -EINVAL; 1554 } 1555 /* check and, if necessary, patch component IDs*/ 1556 psof = (struct mxc_jpeg_sof *)header.sof.start; 1557 psos = (struct mxc_jpeg_sos *)header.sos.start; 1558 if (!mxc_jpeg_valid_comp_id(dev, psof, psos)) 1559 dev_warn(dev, "JPEG component ids should be 0-3 or 1-4"); 1560 1561 q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 1562 if (q_data_cap->fmt && mxc_jpeg_match_image_format(q_data_cap->fmt, &header)) 1563 fourcc = q_data_cap->fmt->fourcc; 1564 else 1565 fourcc = mxc_jpeg_get_image_format(dev, &header); 1566 if (fourcc == 0) 1567 return -EINVAL; 1568 1569 jpeg_src_buf->fmt = mxc_jpeg_find_format(ctx, fourcc); 1570 jpeg_src_buf->w = header.frame.width; 1571 jpeg_src_buf->h = header.frame.height; 1572 ctx->header_parsed = true; 1573 1574 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) 1575 mxc_jpeg_source_change(ctx, jpeg_src_buf); 1576 1577 return 0; 1578 } 1579 1580 static void mxc_jpeg_buf_queue(struct vb2_buffer *vb) 1581 { 1582 int ret; 1583 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1584 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1585 struct mxc_jpeg_src_buf *jpeg_src_buf; 1586 1587 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && 1588 vb2_is_streaming(vb->vb2_queue) && 1589 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) { 1590 struct mxc_jpeg_q_data *q_data; 1591 1592 q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type); 1593 vbuf->field = V4L2_FIELD_NONE; 1594 vbuf->sequence = q_data->sequence++; 1595 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf); 1596 notify_eos(ctx); 1597 ctx->header_parsed = false; 1598 return; 1599 } 1600 1601 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1602 goto end; 1603 1604 /* for V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE */ 1605 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE) 1606 goto end; 1607 1608 jpeg_src_buf = vb2_to_mxc_buf(vb); 1609 jpeg_src_buf->jpeg_parse_error = false; 1610 ret = mxc_jpeg_parse(ctx, vb); 1611 if (ret) 1612 jpeg_src_buf->jpeg_parse_error = true; 1613 1614 end: 1615 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1616 } 1617 1618 static int mxc_jpeg_buf_out_validate(struct vb2_buffer *vb) 1619 { 1620 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1621 1622 vbuf->field = V4L2_FIELD_NONE; 1623 1624 return 0; 1625 } 1626 1627 static int mxc_jpeg_buf_prepare(struct vb2_buffer *vb) 1628 { 1629 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1630 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1631 struct mxc_jpeg_q_data *q_data = NULL; 1632 struct device *dev = ctx->mxc_jpeg->dev; 1633 unsigned long sizeimage; 1634 int i; 1635 1636 vbuf->field = V4L2_FIELD_NONE; 1637 1638 q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type); 1639 if (!q_data) 1640 return -EINVAL; 1641 for (i = 0; i < q_data->fmt->mem_planes; i++) { 1642 sizeimage = mxc_jpeg_get_plane_size(q_data, i); 1643 if (vb2_plane_size(vb, i) < sizeimage) { 1644 dev_err(dev, "plane %d too small (%lu < %lu)", 1645 i, vb2_plane_size(vb, i), sizeimage); 1646 return -EINVAL; 1647 } 1648 } 1649 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) { 1650 vb2_set_plane_payload(vb, 0, 0); 1651 vb2_set_plane_payload(vb, 1, 0); 1652 } 1653 return 0; 1654 } 1655 1656 static const struct vb2_ops mxc_jpeg_qops = { 1657 .queue_setup = mxc_jpeg_queue_setup, 1658 .wait_prepare = vb2_ops_wait_prepare, 1659 .wait_finish = vb2_ops_wait_finish, 1660 .buf_out_validate = mxc_jpeg_buf_out_validate, 1661 .buf_prepare = mxc_jpeg_buf_prepare, 1662 .start_streaming = mxc_jpeg_start_streaming, 1663 .stop_streaming = mxc_jpeg_stop_streaming, 1664 .buf_queue = mxc_jpeg_buf_queue, 1665 }; 1666 1667 static int mxc_jpeg_queue_init(void *priv, struct vb2_queue *src_vq, 1668 struct vb2_queue *dst_vq) 1669 { 1670 struct mxc_jpeg_ctx *ctx = priv; 1671 int ret; 1672 1673 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1674 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1675 src_vq->drv_priv = ctx; 1676 src_vq->buf_struct_size = sizeof(struct mxc_jpeg_src_buf); 1677 src_vq->ops = &mxc_jpeg_qops; 1678 src_vq->mem_ops = &vb2_dma_contig_memops; 1679 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1680 src_vq->lock = &ctx->mxc_jpeg->lock; 1681 src_vq->dev = ctx->mxc_jpeg->dev; 1682 1683 ret = vb2_queue_init(src_vq); 1684 if (ret) 1685 return ret; 1686 1687 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1688 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1689 dst_vq->drv_priv = ctx; 1690 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1691 dst_vq->ops = &mxc_jpeg_qops; 1692 dst_vq->mem_ops = &vb2_dma_contig_memops; 1693 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1694 dst_vq->lock = &ctx->mxc_jpeg->lock; 1695 dst_vq->dev = ctx->mxc_jpeg->dev; 1696 1697 ret = vb2_queue_init(dst_vq); 1698 return ret; 1699 } 1700 1701 static void mxc_jpeg_set_default_params(struct mxc_jpeg_ctx *ctx) 1702 { 1703 struct mxc_jpeg_q_data *out_q = &ctx->out_q; 1704 struct mxc_jpeg_q_data *cap_q = &ctx->cap_q; 1705 struct mxc_jpeg_q_data *q[2] = {out_q, cap_q}; 1706 int i; 1707 1708 if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) { 1709 out_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT); 1710 cap_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG); 1711 } else { 1712 out_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG); 1713 cap_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT); 1714 } 1715 1716 for (i = 0; i < 2; i++) { 1717 q[i]->w = MXC_JPEG_DEFAULT_WIDTH; 1718 q[i]->h = MXC_JPEG_DEFAULT_HEIGHT; 1719 q[i]->w_adjusted = MXC_JPEG_DEFAULT_WIDTH; 1720 q[i]->h_adjusted = MXC_JPEG_DEFAULT_HEIGHT; 1721 q[i]->crop.left = 0; 1722 q[i]->crop.top = 0; 1723 q[i]->crop.width = MXC_JPEG_DEFAULT_WIDTH; 1724 q[i]->crop.height = MXC_JPEG_DEFAULT_HEIGHT; 1725 mxc_jpeg_bytesperline(q[i], q[i]->fmt->precision); 1726 mxc_jpeg_sizeimage(q[i]); 1727 } 1728 } 1729 1730 static int mxc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl) 1731 { 1732 struct mxc_jpeg_ctx *ctx = 1733 container_of(ctrl->handler, struct mxc_jpeg_ctx, ctrl_handler); 1734 1735 switch (ctrl->id) { 1736 case V4L2_CID_JPEG_COMPRESSION_QUALITY: 1737 ctx->jpeg_quality = ctrl->val; 1738 break; 1739 default: 1740 dev_err(ctx->mxc_jpeg->dev, "Invalid control, id = %d, val = %d\n", 1741 ctrl->id, ctrl->val); 1742 return -EINVAL; 1743 } 1744 1745 return 0; 1746 } 1747 1748 static const struct v4l2_ctrl_ops mxc_jpeg_ctrl_ops = { 1749 .s_ctrl = mxc_jpeg_s_ctrl, 1750 }; 1751 1752 static void mxc_jpeg_encode_ctrls(struct mxc_jpeg_ctx *ctx) 1753 { 1754 v4l2_ctrl_new_std(&ctx->ctrl_handler, &mxc_jpeg_ctrl_ops, 1755 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75); 1756 } 1757 1758 static int mxc_jpeg_ctrls_setup(struct mxc_jpeg_ctx *ctx) 1759 { 1760 int err; 1761 1762 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 2); 1763 1764 if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) 1765 mxc_jpeg_encode_ctrls(ctx); 1766 1767 if (ctx->ctrl_handler.error) { 1768 err = ctx->ctrl_handler.error; 1769 1770 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1771 return err; 1772 } 1773 1774 err = v4l2_ctrl_handler_setup(&ctx->ctrl_handler); 1775 if (err) 1776 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1777 return err; 1778 } 1779 1780 static int mxc_jpeg_open(struct file *file) 1781 { 1782 struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file); 1783 struct video_device *mxc_vfd = video_devdata(file); 1784 struct device *dev = mxc_jpeg->dev; 1785 struct mxc_jpeg_ctx *ctx; 1786 int ret = 0; 1787 1788 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1789 if (!ctx) 1790 return -ENOMEM; 1791 1792 if (mutex_lock_interruptible(&mxc_jpeg->lock)) { 1793 ret = -ERESTARTSYS; 1794 goto free; 1795 } 1796 1797 v4l2_fh_init(&ctx->fh, mxc_vfd); 1798 file->private_data = &ctx->fh; 1799 v4l2_fh_add(&ctx->fh); 1800 1801 ctx->mxc_jpeg = mxc_jpeg; 1802 1803 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(mxc_jpeg->m2m_dev, ctx, 1804 mxc_jpeg_queue_init); 1805 1806 if (IS_ERR(ctx->fh.m2m_ctx)) { 1807 ret = PTR_ERR(ctx->fh.m2m_ctx); 1808 goto error; 1809 } 1810 1811 ret = mxc_jpeg_ctrls_setup(ctx); 1812 if (ret) { 1813 dev_err(ctx->mxc_jpeg->dev, "failed to setup mxc jpeg controls\n"); 1814 goto err_ctrls_setup; 1815 } 1816 ctx->fh.ctrl_handler = &ctx->ctrl_handler; 1817 mxc_jpeg_set_default_params(ctx); 1818 ctx->slot = MXC_MAX_SLOTS; /* slot not allocated yet */ 1819 INIT_DELAYED_WORK(&ctx->task_timer, mxc_jpeg_device_run_timeout); 1820 1821 if (mxc_jpeg->mode == MXC_JPEG_DECODE) 1822 dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx); 1823 else 1824 dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx); 1825 mutex_unlock(&mxc_jpeg->lock); 1826 1827 return 0; 1828 1829 err_ctrls_setup: 1830 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 1831 error: 1832 v4l2_fh_del(&ctx->fh); 1833 v4l2_fh_exit(&ctx->fh); 1834 mutex_unlock(&mxc_jpeg->lock); 1835 free: 1836 kfree(ctx); 1837 return ret; 1838 } 1839 1840 static int mxc_jpeg_querycap(struct file *file, void *priv, 1841 struct v4l2_capability *cap) 1842 { 1843 strscpy(cap->driver, MXC_JPEG_NAME " codec", sizeof(cap->driver)); 1844 strscpy(cap->card, MXC_JPEG_NAME " codec", sizeof(cap->card)); 1845 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE; 1846 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 1847 1848 return 0; 1849 } 1850 1851 static int mxc_jpeg_enum_fmt_vid_cap(struct file *file, void *priv, 1852 struct v4l2_fmtdesc *f) 1853 { 1854 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 1855 struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type); 1856 1857 if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) { 1858 return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, 1859 MXC_JPEG_FMT_TYPE_ENC); 1860 } else if (!ctx->header_parsed) { 1861 return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, 1862 MXC_JPEG_FMT_TYPE_RAW); 1863 } else { 1864 /* For the decoder CAPTURE queue, only enumerate the raw formats 1865 * supported for the format currently active on OUTPUT 1866 * (more precisely what was propagated on capture queue 1867 * after jpeg parse on the output buffer) 1868 */ 1869 int ret = -EINVAL; 1870 const struct mxc_jpeg_fmt *sibling; 1871 1872 switch (f->index) { 1873 case 0: 1874 f->pixelformat = q_data->fmt->fourcc; 1875 ret = 0; 1876 break; 1877 case 1: 1878 sibling = mxc_jpeg_get_sibling_format(q_data->fmt); 1879 if (sibling) { 1880 f->pixelformat = sibling->fourcc; 1881 ret = 0; 1882 } 1883 break; 1884 default: 1885 break; 1886 } 1887 return ret; 1888 } 1889 } 1890 1891 static int mxc_jpeg_enum_fmt_vid_out(struct file *file, void *priv, 1892 struct v4l2_fmtdesc *f) 1893 { 1894 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 1895 u32 type = ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ? MXC_JPEG_FMT_TYPE_ENC : 1896 MXC_JPEG_FMT_TYPE_RAW; 1897 int ret; 1898 1899 ret = enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, type); 1900 if (ret) 1901 return ret; 1902 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 1903 f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION; 1904 return 0; 1905 } 1906 1907 static u32 mxc_jpeg_get_fmt_type(struct mxc_jpeg_ctx *ctx, u32 type) 1908 { 1909 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 1910 return V4L2_TYPE_IS_OUTPUT(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW; 1911 else 1912 return V4L2_TYPE_IS_CAPTURE(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW; 1913 } 1914 1915 static u32 mxc_jpeg_get_default_fourcc(struct mxc_jpeg_ctx *ctx, u32 type) 1916 { 1917 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 1918 return V4L2_TYPE_IS_OUTPUT(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT; 1919 else 1920 return V4L2_TYPE_IS_CAPTURE(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT; 1921 } 1922 1923 static u32 mxc_jpeg_try_fourcc(struct mxc_jpeg_ctx *ctx, u32 fourcc) 1924 { 1925 const struct mxc_jpeg_fmt *sibling; 1926 struct mxc_jpeg_q_data *q_data_cap; 1927 1928 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE) 1929 return fourcc; 1930 if (!ctx->header_parsed) 1931 return fourcc; 1932 1933 q_data_cap = &ctx->cap_q; 1934 if (q_data_cap->fmt->fourcc == fourcc) 1935 return fourcc; 1936 1937 sibling = mxc_jpeg_get_sibling_format(q_data_cap->fmt); 1938 if (sibling && sibling->fourcc == fourcc) 1939 return sibling->fourcc; 1940 1941 return q_data_cap->fmt->fourcc; 1942 } 1943 1944 static int mxc_jpeg_try_fmt(struct v4l2_format *f, 1945 struct mxc_jpeg_ctx *ctx, struct mxc_jpeg_q_data *q_data) 1946 { 1947 const struct mxc_jpeg_fmt *fmt; 1948 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 1949 struct v4l2_plane_pix_format *pfmt; 1950 u32 fourcc = f->fmt.pix_mp.pixelformat; 1951 u32 w = (pix_mp->width < MXC_JPEG_MAX_WIDTH) ? 1952 pix_mp->width : MXC_JPEG_MAX_WIDTH; 1953 u32 h = (pix_mp->height < MXC_JPEG_MAX_HEIGHT) ? 1954 pix_mp->height : MXC_JPEG_MAX_HEIGHT; 1955 int i; 1956 1957 fmt = mxc_jpeg_find_format(ctx, fourcc); 1958 if (!fmt || fmt->flags != mxc_jpeg_get_fmt_type(ctx, f->type)) { 1959 dev_warn(ctx->mxc_jpeg->dev, "Format not supported: %c%c%c%c, use the default.\n", 1960 (fourcc & 0xff), 1961 (fourcc >> 8) & 0xff, 1962 (fourcc >> 16) & 0xff, 1963 (fourcc >> 24) & 0xff); 1964 fourcc = mxc_jpeg_get_default_fourcc(ctx, f->type); 1965 fmt = mxc_jpeg_find_format(ctx, fourcc); 1966 if (!fmt) 1967 return -EINVAL; 1968 f->fmt.pix_mp.pixelformat = fourcc; 1969 } 1970 q_data->fmt = fmt; 1971 1972 memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved)); 1973 pix_mp->field = V4L2_FIELD_NONE; 1974 pix_mp->num_planes = fmt->mem_planes; 1975 pix_mp->pixelformat = fmt->fourcc; 1976 1977 q_data->w = w; 1978 q_data->h = h; 1979 q_data->w_adjusted = w; 1980 q_data->h_adjusted = h; 1981 v4l_bound_align_image(&q_data->w_adjusted, 1982 w, /* adjust upwards*/ 1983 MXC_JPEG_MAX_WIDTH, 1984 fmt->h_align, 1985 &q_data->h_adjusted, 1986 h, /* adjust upwards*/ 1987 MXC_JPEG_MAX_HEIGHT, 1988 fmt->v_align, 1989 0); 1990 for (i = 0; i < pix_mp->num_planes; i++) { 1991 pfmt = &pix_mp->plane_fmt[i]; 1992 q_data->bytesperline[i] = pfmt->bytesperline; 1993 q_data->sizeimage[i] = pfmt->sizeimage; 1994 } 1995 1996 /* calculate bytesperline & sizeimage */ 1997 mxc_jpeg_bytesperline(q_data, fmt->precision); 1998 mxc_jpeg_sizeimage(q_data); 1999 2000 /* adjust user format according to our calculations */ 2001 for (i = 0; i < pix_mp->num_planes; i++) { 2002 pfmt = &pix_mp->plane_fmt[i]; 2003 memset(pfmt->reserved, 0, sizeof(pfmt->reserved)); 2004 pfmt->bytesperline = q_data->bytesperline[i]; 2005 pfmt->sizeimage = mxc_jpeg_get_plane_size(q_data, i); 2006 } 2007 2008 /* fix colorspace information to sRGB for both output & capture */ 2009 pix_mp->colorspace = V4L2_COLORSPACE_SRGB; 2010 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601; 2011 pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB; 2012 /* 2013 * this hardware does not change the range of the samples 2014 * but since inside JPEG the YUV quantization is full-range, 2015 * this driver will always use full-range for the raw frames, too 2016 */ 2017 pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2018 2019 if (fmt->flags == MXC_JPEG_FMT_TYPE_RAW) { 2020 q_data->crop.left = 0; 2021 q_data->crop.top = 0; 2022 q_data->crop.width = q_data->w; 2023 q_data->crop.height = q_data->h; 2024 } 2025 2026 pix_mp->width = q_data->w_adjusted; 2027 pix_mp->height = q_data->h_adjusted; 2028 2029 return 0; 2030 } 2031 2032 static int mxc_jpeg_try_fmt_vid_cap(struct file *file, void *priv, 2033 struct v4l2_format *f) 2034 { 2035 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2036 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2037 struct device *dev = jpeg->dev; 2038 struct mxc_jpeg_q_data tmp_q; 2039 2040 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 2041 dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type); 2042 return -EINVAL; 2043 } 2044 2045 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(f->type)) 2046 f->fmt.pix_mp.pixelformat = mxc_jpeg_try_fourcc(ctx, f->fmt.pix_mp.pixelformat); 2047 2048 return mxc_jpeg_try_fmt(f, ctx, &tmp_q); 2049 } 2050 2051 static int mxc_jpeg_try_fmt_vid_out(struct file *file, void *priv, 2052 struct v4l2_format *f) 2053 { 2054 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2055 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2056 struct device *dev = jpeg->dev; 2057 struct mxc_jpeg_q_data tmp_q; 2058 2059 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 2060 dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type); 2061 return -EINVAL; 2062 } 2063 2064 return mxc_jpeg_try_fmt(f, ctx, &tmp_q); 2065 } 2066 2067 static void mxc_jpeg_s_parsed_fmt(struct mxc_jpeg_ctx *ctx, struct v4l2_format *f) 2068 { 2069 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 2070 struct mxc_jpeg_q_data *q_data_cap; 2071 2072 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE || !V4L2_TYPE_IS_CAPTURE(f->type)) 2073 return; 2074 if (!ctx->header_parsed) 2075 return; 2076 2077 q_data_cap = mxc_jpeg_get_q_data(ctx, f->type); 2078 pix_mp->pixelformat = mxc_jpeg_try_fourcc(ctx, pix_mp->pixelformat); 2079 pix_mp->width = q_data_cap->w; 2080 pix_mp->height = q_data_cap->h; 2081 } 2082 2083 static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx, 2084 struct v4l2_format *f) 2085 { 2086 struct vb2_queue *vq; 2087 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2088 2089 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 2090 if (!vq) 2091 return -EINVAL; 2092 2093 if (vb2_is_busy(vq)) { 2094 v4l2_err(&jpeg->v4l2_dev, "queue busy\n"); 2095 return -EBUSY; 2096 } 2097 2098 mxc_jpeg_s_parsed_fmt(ctx, f); 2099 2100 return mxc_jpeg_try_fmt(f, ctx, mxc_jpeg_get_q_data(ctx, f->type)); 2101 } 2102 2103 static int mxc_jpeg_s_fmt_vid_cap(struct file *file, void *priv, 2104 struct v4l2_format *f) 2105 { 2106 return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f); 2107 } 2108 2109 static int mxc_jpeg_s_fmt_vid_out(struct file *file, void *priv, 2110 struct v4l2_format *f) 2111 { 2112 int ret; 2113 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2114 struct vb2_queue *dst_vq; 2115 struct mxc_jpeg_q_data *q_data_cap; 2116 enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 2117 struct v4l2_format fc; 2118 2119 ret = mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f); 2120 if (ret) 2121 return ret; 2122 2123 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE) 2124 return 0; 2125 2126 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, cap_type); 2127 if (!dst_vq) 2128 return -EINVAL; 2129 2130 if (vb2_is_busy(dst_vq)) 2131 return 0; 2132 2133 q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type); 2134 if (q_data_cap->w == f->fmt.pix_mp.width && q_data_cap->h == f->fmt.pix_mp.height) 2135 return 0; 2136 memset(&fc, 0, sizeof(fc)); 2137 fc.type = cap_type; 2138 fc.fmt.pix_mp.pixelformat = q_data_cap->fmt->fourcc; 2139 fc.fmt.pix_mp.width = f->fmt.pix_mp.width; 2140 fc.fmt.pix_mp.height = f->fmt.pix_mp.height; 2141 2142 return mxc_jpeg_s_fmt_vid_cap(file, priv, &fc); 2143 } 2144 2145 static int mxc_jpeg_g_fmt_vid(struct file *file, void *priv, 2146 struct v4l2_format *f) 2147 { 2148 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2149 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2150 struct device *dev = jpeg->dev; 2151 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 2152 struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type); 2153 int i; 2154 2155 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 2156 dev_err(dev, "G_FMT with Invalid type: %d\n", f->type); 2157 return -EINVAL; 2158 } 2159 2160 pix_mp->pixelformat = q_data->fmt->fourcc; 2161 pix_mp->width = q_data->w; 2162 pix_mp->height = q_data->h; 2163 pix_mp->field = V4L2_FIELD_NONE; 2164 if (q_data->fmt->flags == MXC_JPEG_FMT_TYPE_RAW) { 2165 pix_mp->width = q_data->w_adjusted; 2166 pix_mp->height = q_data->h_adjusted; 2167 } 2168 2169 /* fix colorspace information to sRGB for both output & capture */ 2170 pix_mp->colorspace = V4L2_COLORSPACE_SRGB; 2171 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601; 2172 pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB; 2173 pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2174 2175 pix_mp->num_planes = q_data->fmt->mem_planes; 2176 for (i = 0; i < pix_mp->num_planes; i++) { 2177 pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i]; 2178 pix_mp->plane_fmt[i].sizeimage = mxc_jpeg_get_plane_size(q_data, i); 2179 } 2180 2181 return 0; 2182 } 2183 2184 static int mxc_jpeg_dec_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 2185 { 2186 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2187 struct mxc_jpeg_q_data *q_data_cap; 2188 2189 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 2190 return -EINVAL; 2191 2192 q_data_cap = mxc_jpeg_get_q_data(ctx, s->type); 2193 2194 switch (s->target) { 2195 case V4L2_SEL_TGT_COMPOSE: 2196 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 2197 s->r = q_data_cap->crop; 2198 break; 2199 case V4L2_SEL_TGT_COMPOSE_PADDED: 2200 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 2201 s->r.left = 0; 2202 s->r.top = 0; 2203 s->r.width = q_data_cap->w_adjusted; 2204 s->r.height = q_data_cap->h_adjusted; 2205 break; 2206 default: 2207 return -EINVAL; 2208 } 2209 2210 return 0; 2211 } 2212 2213 static int mxc_jpeg_enc_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 2214 { 2215 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2216 struct mxc_jpeg_q_data *q_data_out; 2217 2218 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 2219 return -EINVAL; 2220 2221 q_data_out = mxc_jpeg_get_q_data(ctx, s->type); 2222 2223 switch (s->target) { 2224 case V4L2_SEL_TGT_CROP_DEFAULT: 2225 case V4L2_SEL_TGT_CROP_BOUNDS: 2226 s->r.left = 0; 2227 s->r.top = 0; 2228 s->r.width = q_data_out->w; 2229 s->r.height = q_data_out->h; 2230 break; 2231 case V4L2_SEL_TGT_CROP: 2232 s->r = q_data_out->crop; 2233 break; 2234 default: 2235 return -EINVAL; 2236 } 2237 2238 return 0; 2239 } 2240 2241 static int mxc_jpeg_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 2242 { 2243 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2244 2245 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 2246 return mxc_jpeg_dec_g_selection(file, fh, s); 2247 else 2248 return mxc_jpeg_enc_g_selection(file, fh, s); 2249 } 2250 2251 static int mxc_jpeg_s_selection(struct file *file, void *fh, struct v4l2_selection *s) 2252 { 2253 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2254 struct mxc_jpeg_q_data *q_data_out; 2255 2256 if (ctx->mxc_jpeg->mode != MXC_JPEG_ENCODE) 2257 return -ENOTTY; 2258 2259 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 2260 return -EINVAL; 2261 if (s->target != V4L2_SEL_TGT_CROP) 2262 return -EINVAL; 2263 2264 q_data_out = mxc_jpeg_get_q_data(ctx, s->type); 2265 if (s->r.left || s->r.top) 2266 return -EINVAL; 2267 if (s->r.width > q_data_out->w || s->r.height > q_data_out->h) 2268 return -EINVAL; 2269 2270 q_data_out->crop.left = 0; 2271 q_data_out->crop.top = 0; 2272 q_data_out->crop.width = s->r.width; 2273 q_data_out->crop.height = s->r.height; 2274 2275 return 0; 2276 } 2277 2278 static int mxc_jpeg_subscribe_event(struct v4l2_fh *fh, 2279 const struct v4l2_event_subscription *sub) 2280 { 2281 switch (sub->type) { 2282 case V4L2_EVENT_EOS: 2283 return v4l2_event_subscribe(fh, sub, 0, NULL); 2284 case V4L2_EVENT_SOURCE_CHANGE: 2285 return v4l2_src_change_event_subscribe(fh, sub); 2286 case V4L2_EVENT_CTRL: 2287 return v4l2_ctrl_subscribe_event(fh, sub); 2288 default: 2289 return -EINVAL; 2290 } 2291 } 2292 2293 static const struct v4l2_ioctl_ops mxc_jpeg_ioctl_ops = { 2294 .vidioc_querycap = mxc_jpeg_querycap, 2295 .vidioc_enum_fmt_vid_cap = mxc_jpeg_enum_fmt_vid_cap, 2296 .vidioc_enum_fmt_vid_out = mxc_jpeg_enum_fmt_vid_out, 2297 2298 .vidioc_try_fmt_vid_cap_mplane = mxc_jpeg_try_fmt_vid_cap, 2299 .vidioc_try_fmt_vid_out_mplane = mxc_jpeg_try_fmt_vid_out, 2300 2301 .vidioc_s_fmt_vid_cap_mplane = mxc_jpeg_s_fmt_vid_cap, 2302 .vidioc_s_fmt_vid_out_mplane = mxc_jpeg_s_fmt_vid_out, 2303 2304 .vidioc_g_fmt_vid_cap_mplane = mxc_jpeg_g_fmt_vid, 2305 .vidioc_g_fmt_vid_out_mplane = mxc_jpeg_g_fmt_vid, 2306 2307 .vidioc_g_selection = mxc_jpeg_g_selection, 2308 .vidioc_s_selection = mxc_jpeg_s_selection, 2309 2310 .vidioc_subscribe_event = mxc_jpeg_subscribe_event, 2311 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2312 2313 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, 2314 .vidioc_decoder_cmd = mxc_jpeg_decoder_cmd, 2315 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 2316 .vidioc_encoder_cmd = mxc_jpeg_encoder_cmd, 2317 2318 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 2319 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 2320 2321 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 2322 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 2323 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 2324 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 2325 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 2326 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 2327 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 2328 }; 2329 2330 static int mxc_jpeg_release(struct file *file) 2331 { 2332 struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file); 2333 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(file->private_data); 2334 struct device *dev = mxc_jpeg->dev; 2335 2336 mutex_lock(&mxc_jpeg->lock); 2337 if (mxc_jpeg->mode == MXC_JPEG_DECODE) 2338 dev_dbg(dev, "Release JPEG decoder instance on slot %d.", 2339 ctx->slot); 2340 else 2341 dev_dbg(dev, "Release JPEG encoder instance on slot %d.", 2342 ctx->slot); 2343 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 2344 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 2345 v4l2_fh_del(&ctx->fh); 2346 v4l2_fh_exit(&ctx->fh); 2347 kfree(ctx); 2348 mutex_unlock(&mxc_jpeg->lock); 2349 2350 return 0; 2351 } 2352 2353 static const struct v4l2_file_operations mxc_jpeg_fops = { 2354 .owner = THIS_MODULE, 2355 .open = mxc_jpeg_open, 2356 .release = mxc_jpeg_release, 2357 .poll = v4l2_m2m_fop_poll, 2358 .unlocked_ioctl = video_ioctl2, 2359 .mmap = v4l2_m2m_fop_mmap, 2360 }; 2361 2362 static const struct v4l2_m2m_ops mxc_jpeg_m2m_ops = { 2363 .job_ready = mxc_jpeg_job_ready, 2364 .device_run = mxc_jpeg_device_run, 2365 }; 2366 2367 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg) 2368 { 2369 int i; 2370 2371 for (i = 0; i < jpeg->num_domains; i++) { 2372 if (jpeg->pd_link[i] && !IS_ERR(jpeg->pd_link[i])) 2373 device_link_del(jpeg->pd_link[i]); 2374 if (jpeg->pd_dev[i] && !IS_ERR(jpeg->pd_dev[i])) 2375 dev_pm_domain_detach(jpeg->pd_dev[i], true); 2376 jpeg->pd_dev[i] = NULL; 2377 jpeg->pd_link[i] = NULL; 2378 } 2379 } 2380 2381 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg) 2382 { 2383 struct device *dev = jpeg->dev; 2384 struct device_node *np = jpeg->pdev->dev.of_node; 2385 int i; 2386 int ret; 2387 2388 jpeg->num_domains = of_count_phandle_with_args(np, "power-domains", 2389 "#power-domain-cells"); 2390 if (jpeg->num_domains < 0) { 2391 dev_err(dev, "No power domains defined for jpeg node\n"); 2392 return jpeg->num_domains; 2393 } 2394 2395 jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains, 2396 sizeof(*jpeg->pd_dev), GFP_KERNEL); 2397 if (!jpeg->pd_dev) 2398 return -ENOMEM; 2399 2400 jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains, 2401 sizeof(*jpeg->pd_link), GFP_KERNEL); 2402 if (!jpeg->pd_link) 2403 return -ENOMEM; 2404 2405 for (i = 0; i < jpeg->num_domains; i++) { 2406 jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i); 2407 if (IS_ERR(jpeg->pd_dev[i])) { 2408 ret = PTR_ERR(jpeg->pd_dev[i]); 2409 goto fail; 2410 } 2411 2412 jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i], 2413 DL_FLAG_STATELESS | 2414 DL_FLAG_PM_RUNTIME); 2415 if (!jpeg->pd_link[i]) { 2416 ret = -EINVAL; 2417 goto fail; 2418 } 2419 } 2420 2421 return 0; 2422 fail: 2423 mxc_jpeg_detach_pm_domains(jpeg); 2424 return ret; 2425 } 2426 2427 static int mxc_jpeg_probe(struct platform_device *pdev) 2428 { 2429 struct mxc_jpeg_dev *jpeg; 2430 struct device *dev = &pdev->dev; 2431 int dec_irq; 2432 int ret; 2433 int mode; 2434 const struct of_device_id *of_id; 2435 unsigned int slot; 2436 2437 of_id = of_match_node(mxc_jpeg_match, dev->of_node); 2438 if (!of_id) 2439 return -ENODEV; 2440 mode = *(const int *)of_id->data; 2441 2442 jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL); 2443 if (!jpeg) 2444 return -ENOMEM; 2445 2446 mutex_init(&jpeg->lock); 2447 spin_lock_init(&jpeg->hw_lock); 2448 2449 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 2450 if (ret) { 2451 dev_err(&pdev->dev, "No suitable DMA available.\n"); 2452 goto err_irq; 2453 } 2454 2455 jpeg->base_reg = devm_platform_ioremap_resource(pdev, 0); 2456 if (IS_ERR(jpeg->base_reg)) 2457 return PTR_ERR(jpeg->base_reg); 2458 2459 for (slot = 0; slot < MXC_MAX_SLOTS; slot++) { 2460 dec_irq = platform_get_irq(pdev, slot); 2461 if (dec_irq < 0) { 2462 ret = dec_irq; 2463 goto err_irq; 2464 } 2465 ret = devm_request_irq(&pdev->dev, dec_irq, mxc_jpeg_dec_irq, 2466 0, pdev->name, jpeg); 2467 if (ret) { 2468 dev_err(&pdev->dev, "Failed to request irq %d (%d)\n", 2469 dec_irq, ret); 2470 goto err_irq; 2471 } 2472 } 2473 2474 jpeg->pdev = pdev; 2475 jpeg->dev = dev; 2476 jpeg->mode = mode; 2477 2478 /* Get clocks */ 2479 ret = devm_clk_bulk_get_all(&pdev->dev, &jpeg->clks); 2480 if (ret < 0) { 2481 dev_err(dev, "failed to get clock\n"); 2482 goto err_clk; 2483 } 2484 jpeg->num_clks = ret; 2485 2486 ret = mxc_jpeg_attach_pm_domains(jpeg); 2487 if (ret < 0) { 2488 dev_err(dev, "failed to attach power domains %d\n", ret); 2489 return ret; 2490 } 2491 2492 /* v4l2 */ 2493 ret = v4l2_device_register(dev, &jpeg->v4l2_dev); 2494 if (ret) { 2495 dev_err(dev, "failed to register v4l2 device\n"); 2496 goto err_register; 2497 } 2498 jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops); 2499 if (IS_ERR(jpeg->m2m_dev)) { 2500 dev_err(dev, "failed to register v4l2 device\n"); 2501 ret = PTR_ERR(jpeg->m2m_dev); 2502 goto err_m2m; 2503 } 2504 2505 jpeg->dec_vdev = video_device_alloc(); 2506 if (!jpeg->dec_vdev) { 2507 dev_err(dev, "failed to register v4l2 device\n"); 2508 ret = -ENOMEM; 2509 goto err_vdev_alloc; 2510 } 2511 if (mode == MXC_JPEG_ENCODE) 2512 snprintf(jpeg->dec_vdev->name, 2513 sizeof(jpeg->dec_vdev->name), 2514 "%s-enc", MXC_JPEG_NAME); 2515 else 2516 snprintf(jpeg->dec_vdev->name, 2517 sizeof(jpeg->dec_vdev->name), 2518 "%s-dec", MXC_JPEG_NAME); 2519 2520 jpeg->dec_vdev->fops = &mxc_jpeg_fops; 2521 jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops; 2522 jpeg->dec_vdev->minor = -1; 2523 jpeg->dec_vdev->release = video_device_release; 2524 jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */ 2525 jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev; 2526 jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M; 2527 jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING | 2528 V4L2_CAP_VIDEO_M2M_MPLANE; 2529 if (mode == MXC_JPEG_ENCODE) { 2530 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD); 2531 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD); 2532 } else { 2533 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD); 2534 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD); 2535 } 2536 ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1); 2537 if (ret) { 2538 dev_err(dev, "failed to register video device\n"); 2539 goto err_vdev_register; 2540 } 2541 video_set_drvdata(jpeg->dec_vdev, jpeg); 2542 if (mode == MXC_JPEG_ENCODE) 2543 v4l2_info(&jpeg->v4l2_dev, 2544 "encoder device registered as /dev/video%d (%d,%d)\n", 2545 jpeg->dec_vdev->num, VIDEO_MAJOR, 2546 jpeg->dec_vdev->minor); 2547 else 2548 v4l2_info(&jpeg->v4l2_dev, 2549 "decoder device registered as /dev/video%d (%d,%d)\n", 2550 jpeg->dec_vdev->num, VIDEO_MAJOR, 2551 jpeg->dec_vdev->minor); 2552 2553 platform_set_drvdata(pdev, jpeg); 2554 pm_runtime_enable(dev); 2555 2556 return 0; 2557 2558 err_vdev_register: 2559 video_device_release(jpeg->dec_vdev); 2560 2561 err_vdev_alloc: 2562 v4l2_m2m_release(jpeg->m2m_dev); 2563 2564 err_m2m: 2565 v4l2_device_unregister(&jpeg->v4l2_dev); 2566 2567 err_register: 2568 mxc_jpeg_detach_pm_domains(jpeg); 2569 2570 err_irq: 2571 err_clk: 2572 return ret; 2573 } 2574 2575 #ifdef CONFIG_PM 2576 static int mxc_jpeg_runtime_resume(struct device *dev) 2577 { 2578 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2579 int ret; 2580 2581 ret = clk_bulk_prepare_enable(jpeg->num_clks, jpeg->clks); 2582 if (ret < 0) { 2583 dev_err(dev, "failed to enable clock\n"); 2584 return ret; 2585 } 2586 2587 return 0; 2588 } 2589 2590 static int mxc_jpeg_runtime_suspend(struct device *dev) 2591 { 2592 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2593 2594 clk_bulk_disable_unprepare(jpeg->num_clks, jpeg->clks); 2595 2596 return 0; 2597 } 2598 #endif 2599 2600 #ifdef CONFIG_PM_SLEEP 2601 static int mxc_jpeg_suspend(struct device *dev) 2602 { 2603 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2604 2605 v4l2_m2m_suspend(jpeg->m2m_dev); 2606 return pm_runtime_force_suspend(dev); 2607 } 2608 2609 static int mxc_jpeg_resume(struct device *dev) 2610 { 2611 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2612 int ret; 2613 2614 ret = pm_runtime_force_resume(dev); 2615 if (ret < 0) 2616 return ret; 2617 2618 v4l2_m2m_resume(jpeg->m2m_dev); 2619 return ret; 2620 } 2621 #endif 2622 2623 static const struct dev_pm_ops mxc_jpeg_pm_ops = { 2624 SET_RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend, 2625 mxc_jpeg_runtime_resume, NULL) 2626 SET_SYSTEM_SLEEP_PM_OPS(mxc_jpeg_suspend, mxc_jpeg_resume) 2627 }; 2628 2629 static void mxc_jpeg_remove(struct platform_device *pdev) 2630 { 2631 unsigned int slot; 2632 struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev); 2633 2634 for (slot = 0; slot < MXC_MAX_SLOTS; slot++) 2635 mxc_jpeg_free_slot_data(jpeg, slot); 2636 2637 pm_runtime_disable(&pdev->dev); 2638 video_unregister_device(jpeg->dec_vdev); 2639 v4l2_m2m_release(jpeg->m2m_dev); 2640 v4l2_device_unregister(&jpeg->v4l2_dev); 2641 mxc_jpeg_detach_pm_domains(jpeg); 2642 } 2643 2644 MODULE_DEVICE_TABLE(of, mxc_jpeg_match); 2645 2646 static struct platform_driver mxc_jpeg_driver = { 2647 .probe = mxc_jpeg_probe, 2648 .remove_new = mxc_jpeg_remove, 2649 .driver = { 2650 .name = "mxc-jpeg", 2651 .of_match_table = mxc_jpeg_match, 2652 .pm = &mxc_jpeg_pm_ops, 2653 }, 2654 }; 2655 module_platform_driver(mxc_jpeg_driver); 2656 2657 MODULE_AUTHOR("Zhengyu Shen <zhengyu.shen_1@nxp.com>"); 2658 MODULE_AUTHOR("Mirela Rabulea <mirela.rabulea@nxp.com>"); 2659 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder"); 2660 MODULE_LICENSE("GPL v2"); 2661