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 = 502 V4L2_EVENT_SRC_CH_RESOLUTION, 503 }; 504 505 dev_dbg(ctx->mxc_jpeg->dev, "Notify app event SRC_CH_RESOLUTION"); 506 v4l2_event_queue_fh(&ctx->fh, &ev); 507 } 508 509 static int mxc_get_free_slot(struct mxc_jpeg_slot_data slot_data[], int n) 510 { 511 int free_slot = 0; 512 513 while (slot_data[free_slot].used && free_slot < n) 514 free_slot++; 515 516 return free_slot; /* >=n when there are no more free slots */ 517 } 518 519 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg, 520 unsigned int slot) 521 { 522 struct mxc_jpeg_desc *desc; 523 struct mxc_jpeg_desc *cfg_desc; 524 void *cfg_stm; 525 526 if (jpeg->slot_data[slot].desc) 527 goto skip_alloc; /* already allocated, reuse it */ 528 529 /* allocate descriptor for decoding/encoding phase */ 530 desc = dma_alloc_coherent(jpeg->dev, 531 sizeof(struct mxc_jpeg_desc), 532 &jpeg->slot_data[slot].desc_handle, 533 GFP_ATOMIC); 534 if (!desc) 535 goto err; 536 jpeg->slot_data[slot].desc = desc; 537 538 /* allocate descriptor for configuration phase (encoder only) */ 539 cfg_desc = dma_alloc_coherent(jpeg->dev, 540 sizeof(struct mxc_jpeg_desc), 541 &jpeg->slot_data[slot].cfg_desc_handle, 542 GFP_ATOMIC); 543 if (!cfg_desc) 544 goto err; 545 jpeg->slot_data[slot].cfg_desc = cfg_desc; 546 547 /* allocate configuration stream */ 548 cfg_stm = dma_alloc_coherent(jpeg->dev, 549 MXC_JPEG_MAX_CFG_STREAM, 550 &jpeg->slot_data[slot].cfg_stream_handle, 551 GFP_ATOMIC); 552 if (!cfg_stm) 553 goto err; 554 jpeg->slot_data[slot].cfg_stream_vaddr = cfg_stm; 555 556 skip_alloc: 557 jpeg->slot_data[slot].used = true; 558 559 return true; 560 err: 561 dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", slot); 562 563 return false; 564 } 565 566 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg, 567 unsigned int slot) 568 { 569 if (slot >= MXC_MAX_SLOTS) { 570 dev_err(jpeg->dev, "Invalid slot %d, nothing to free.", slot); 571 return; 572 } 573 574 /* free descriptor for decoding/encoding phase */ 575 dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc), 576 jpeg->slot_data[slot].desc, 577 jpeg->slot_data[slot].desc_handle); 578 579 /* free descriptor for encoder configuration phase / decoder DHT */ 580 dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc), 581 jpeg->slot_data[slot].cfg_desc, 582 jpeg->slot_data[slot].cfg_desc_handle); 583 584 /* free configuration stream */ 585 dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM, 586 jpeg->slot_data[slot].cfg_stream_vaddr, 587 jpeg->slot_data[slot].cfg_stream_handle); 588 589 jpeg->slot_data[slot].used = false; 590 } 591 592 static void mxc_jpeg_check_and_set_last_buffer(struct mxc_jpeg_ctx *ctx, 593 struct vb2_v4l2_buffer *src_buf, 594 struct vb2_v4l2_buffer *dst_buf) 595 { 596 if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) { 597 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 598 v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx); 599 notify_eos(ctx); 600 ctx->header_parsed = false; 601 } 602 } 603 604 static void mxc_jpeg_job_finish(struct mxc_jpeg_ctx *ctx, enum vb2_buffer_state state, bool reset) 605 { 606 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 607 void __iomem *reg = jpeg->base_reg; 608 struct vb2_v4l2_buffer *src_buf, *dst_buf; 609 610 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 611 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 612 mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf); 613 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 614 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 615 v4l2_m2m_buf_done(src_buf, state); 616 v4l2_m2m_buf_done(dst_buf, state); 617 618 mxc_jpeg_disable_irq(reg, ctx->slot); 619 ctx->mxc_jpeg->slot_data[ctx->slot].used = false; 620 if (reset) 621 mxc_jpeg_sw_reset(reg); 622 } 623 624 static u32 mxc_jpeg_get_plane_size(struct mxc_jpeg_q_data *q_data, u32 plane_no) 625 { 626 const struct mxc_jpeg_fmt *fmt = q_data->fmt; 627 u32 size; 628 int i; 629 630 if (plane_no >= fmt->mem_planes) 631 return 0; 632 633 if (fmt->mem_planes == fmt->comp_planes) 634 return q_data->sizeimage[plane_no]; 635 636 if (plane_no < fmt->mem_planes - 1) 637 return q_data->sizeimage[plane_no]; 638 639 size = q_data->sizeimage[fmt->mem_planes - 1]; 640 641 /* Should be impossible given mxc_formats. */ 642 if (WARN_ON_ONCE(fmt->comp_planes > ARRAY_SIZE(q_data->sizeimage))) 643 return size; 644 645 for (i = fmt->mem_planes; i < fmt->comp_planes; i++) 646 size += q_data->sizeimage[i]; 647 648 return size; 649 } 650 651 static irqreturn_t mxc_jpeg_dec_irq(int irq, void *priv) 652 { 653 struct mxc_jpeg_dev *jpeg = priv; 654 struct mxc_jpeg_ctx *ctx; 655 void __iomem *reg = jpeg->base_reg; 656 struct device *dev = jpeg->dev; 657 struct vb2_v4l2_buffer *src_buf, *dst_buf; 658 struct mxc_jpeg_src_buf *jpeg_src_buf; 659 enum vb2_buffer_state buf_state; 660 u32 dec_ret, com_status; 661 unsigned long payload; 662 struct mxc_jpeg_q_data *q_data; 663 enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 664 unsigned int slot; 665 666 spin_lock(&jpeg->hw_lock); 667 668 com_status = readl(reg + COM_STATUS); 669 slot = COM_STATUS_CUR_SLOT(com_status); 670 dev_dbg(dev, "Irq %d on slot %d.\n", irq, slot); 671 672 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); 673 if (WARN_ON(!ctx)) 674 goto job_unlock; 675 676 if (slot != ctx->slot) { 677 /* TODO investigate when adding multi-instance support */ 678 dev_warn(dev, "IRQ slot %d != context slot %d.\n", 679 slot, ctx->slot); 680 goto job_unlock; 681 } 682 683 if (!jpeg->slot_data[slot].used) 684 goto job_unlock; 685 686 dec_ret = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); 687 writel(dec_ret, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); /* w1c */ 688 689 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 690 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 691 if (!dst_buf || !src_buf) { 692 dev_err(dev, "No source or destination buffer.\n"); 693 goto job_unlock; 694 } 695 jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf); 696 697 if (dec_ret & SLOT_STATUS_ENC_CONFIG_ERR) { 698 u32 ret = readl(reg + CAST_STATUS12); 699 700 dev_err(dev, "Encoder/decoder error, status=0x%08x", ret); 701 mxc_jpeg_sw_reset(reg); 702 buf_state = VB2_BUF_STATE_ERROR; 703 goto buffers_done; 704 } 705 706 if (!(dec_ret & SLOT_STATUS_FRMDONE)) 707 goto job_unlock; 708 709 if (jpeg->mode == MXC_JPEG_ENCODE && 710 ctx->enc_state == MXC_JPEG_ENC_CONF) { 711 ctx->enc_state = MXC_JPEG_ENCODING; 712 dev_dbg(dev, "Encoder config finished. Start encoding...\n"); 713 mxc_jpeg_enc_set_quality(dev, reg, ctx->jpeg_quality); 714 mxc_jpeg_enc_mode_go(dev, reg); 715 goto job_unlock; 716 } 717 if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed) { 718 jpeg_src_buf->dht_needed = false; 719 dev_dbg(dev, "Decoder DHT cfg finished. Start decoding...\n"); 720 goto job_unlock; 721 } 722 723 if (jpeg->mode == MXC_JPEG_ENCODE) { 724 payload = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_BUF_PTR)); 725 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload); 726 dev_dbg(dev, "Encoding finished, payload size: %ld\n", 727 payload); 728 } else { 729 q_data = mxc_jpeg_get_q_data(ctx, cap_type); 730 payload = mxc_jpeg_get_plane_size(q_data, 0); 731 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload); 732 vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0); 733 if (q_data->fmt->mem_planes == 2) { 734 payload = mxc_jpeg_get_plane_size(q_data, 1); 735 vb2_set_plane_payload(&dst_buf->vb2_buf, 1, payload); 736 } 737 dev_dbg(dev, "Decoding finished, payload size: %ld + %ld\n", 738 vb2_get_plane_payload(&dst_buf->vb2_buf, 0), 739 vb2_get_plane_payload(&dst_buf->vb2_buf, 1)); 740 } 741 742 /* short preview of the results */ 743 dev_dbg(dev, "src_buf preview: "); 744 print_mxc_buf(jpeg, &src_buf->vb2_buf, 32); 745 dev_dbg(dev, "dst_buf preview: "); 746 print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32); 747 buf_state = VB2_BUF_STATE_DONE; 748 749 buffers_done: 750 mxc_jpeg_job_finish(ctx, buf_state, false); 751 spin_unlock(&jpeg->hw_lock); 752 cancel_delayed_work(&ctx->task_timer); 753 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 754 return IRQ_HANDLED; 755 job_unlock: 756 spin_unlock(&jpeg->hw_lock); 757 return IRQ_HANDLED; 758 } 759 760 static int mxc_jpeg_fixup_sof(struct mxc_jpeg_sof *sof, 761 u32 fourcc, 762 u16 w, u16 h) 763 { 764 int sof_length; 765 766 sof->precision = 8; /* TODO allow 8/12 bit precision*/ 767 sof->height = h; 768 _bswap16(&sof->height); 769 sof->width = w; 770 _bswap16(&sof->width); 771 772 switch (fourcc) { 773 case V4L2_PIX_FMT_NV12: 774 case V4L2_PIX_FMT_NV12M: 775 sof->components_no = 3; 776 sof->comp[0].v = 0x2; 777 sof->comp[0].h = 0x2; 778 break; 779 case V4L2_PIX_FMT_YUYV: 780 sof->components_no = 3; 781 sof->comp[0].v = 0x1; 782 sof->comp[0].h = 0x2; 783 break; 784 case V4L2_PIX_FMT_YUV24: 785 case V4L2_PIX_FMT_BGR24: 786 default: 787 sof->components_no = 3; 788 break; 789 case V4L2_PIX_FMT_ABGR32: 790 sof->components_no = 4; 791 break; 792 case V4L2_PIX_FMT_GREY: 793 sof->components_no = 1; 794 break; 795 } 796 sof_length = 8 + 3 * sof->components_no; 797 sof->length = sof_length; 798 _bswap16(&sof->length); 799 800 return sof_length; /* not swaped */ 801 } 802 803 static int mxc_jpeg_fixup_sos(struct mxc_jpeg_sos *sos, 804 u32 fourcc) 805 { 806 int sos_length; 807 u8 *sof_u8 = (u8 *)sos; 808 809 switch (fourcc) { 810 case V4L2_PIX_FMT_NV12: 811 case V4L2_PIX_FMT_NV12M: 812 sos->components_no = 3; 813 break; 814 case V4L2_PIX_FMT_YUYV: 815 sos->components_no = 3; 816 break; 817 case V4L2_PIX_FMT_YUV24: 818 case V4L2_PIX_FMT_BGR24: 819 default: 820 sos->components_no = 3; 821 break; 822 case V4L2_PIX_FMT_ABGR32: 823 sos->components_no = 4; 824 break; 825 case V4L2_PIX_FMT_GREY: 826 sos->components_no = 1; 827 break; 828 } 829 sos_length = 6 + 2 * sos->components_no; 830 sos->length = sos_length; 831 _bswap16(&sos->length); 832 833 /* SOS ignorable bytes, not so ignorable after all */ 834 sof_u8[sos_length - 1] = 0x0; 835 sof_u8[sos_length - 2] = 0x3f; 836 sof_u8[sos_length - 3] = 0x0; 837 838 return sos_length; /* not swaped */ 839 } 840 841 static unsigned int mxc_jpeg_setup_cfg_stream(void *cfg_stream_vaddr, 842 u32 fourcc, 843 u16 w, u16 h) 844 { 845 /* 846 * There is a hardware issue that first 128 bytes of configuration data 847 * can't be loaded correctly. 848 * To avoid this issue, we need to write the configuration from 849 * an offset which should be no less than 0x80 (128 bytes). 850 */ 851 unsigned int offset = 0x80; 852 u8 *cfg = (u8 *)cfg_stream_vaddr; 853 struct mxc_jpeg_sof *sof; 854 struct mxc_jpeg_sos *sos; 855 856 memcpy(cfg + offset, jpeg_soi, ARRAY_SIZE(jpeg_soi)); 857 offset += ARRAY_SIZE(jpeg_soi); 858 859 if (fourcc == V4L2_PIX_FMT_BGR24 || 860 fourcc == V4L2_PIX_FMT_ABGR32) { 861 memcpy(cfg + offset, jpeg_app14, sizeof(jpeg_app14)); 862 offset += sizeof(jpeg_app14); 863 } else { 864 memcpy(cfg + offset, jpeg_app0, sizeof(jpeg_app0)); 865 offset += sizeof(jpeg_app0); 866 } 867 868 memcpy(cfg + offset, jpeg_dqt, sizeof(jpeg_dqt)); 869 offset += sizeof(jpeg_dqt); 870 871 memcpy(cfg + offset, jpeg_sof_maximal, sizeof(jpeg_sof_maximal)); 872 offset += 2; /* skip marker ID */ 873 sof = (struct mxc_jpeg_sof *)(cfg + offset); 874 offset += mxc_jpeg_fixup_sof(sof, fourcc, w, h); 875 876 memcpy(cfg + offset, jpeg_dht, sizeof(jpeg_dht)); 877 offset += sizeof(jpeg_dht); 878 879 memcpy(cfg + offset, jpeg_dri, sizeof(jpeg_dri)); 880 offset += sizeof(jpeg_dri); 881 882 memcpy(cfg + offset, jpeg_sos_maximal, sizeof(jpeg_sos_maximal)); 883 offset += 2; /* skip marker ID */ 884 sos = (struct mxc_jpeg_sos *)(cfg + offset); 885 offset += mxc_jpeg_fixup_sos(sos, fourcc); 886 887 memcpy(cfg + offset, jpeg_image_red, sizeof(jpeg_image_red)); 888 offset += sizeof(jpeg_image_red); 889 890 memcpy(cfg + offset, jpeg_eoi, sizeof(jpeg_eoi)); 891 offset += sizeof(jpeg_eoi); 892 893 return offset; 894 } 895 896 static void mxc_jpeg_config_dec_desc(struct vb2_buffer *out_buf, 897 struct mxc_jpeg_ctx *ctx, 898 struct vb2_buffer *src_buf, 899 struct vb2_buffer *dst_buf) 900 { 901 enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 902 struct mxc_jpeg_q_data *q_data_cap; 903 enum mxc_jpeg_image_format img_fmt; 904 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 905 void __iomem *reg = jpeg->base_reg; 906 unsigned int slot = ctx->slot; 907 struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc; 908 struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc; 909 dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle; 910 dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle; 911 dma_addr_t cfg_stream_handle = jpeg->slot_data[slot].cfg_stream_handle; 912 unsigned int *cfg_size = &jpeg->slot_data[slot].cfg_stream_size; 913 void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr; 914 struct mxc_jpeg_src_buf *jpeg_src_buf; 915 916 jpeg_src_buf = vb2_to_mxc_buf(src_buf); 917 918 /* setup the decoding descriptor */ 919 desc->next_descpt_ptr = 0; /* end of chain */ 920 q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type); 921 desc->imgsize = q_data_cap->w_adjusted << 16 | q_data_cap->h_adjusted; 922 img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data_cap->fmt->fourcc); 923 desc->stm_ctrl &= ~STM_CTRL_IMAGE_FORMAT(0xF); /* clear image format */ 924 desc->stm_ctrl |= STM_CTRL_IMAGE_FORMAT(img_fmt); 925 desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 926 desc->line_pitch = q_data_cap->bytesperline[0]; 927 mxc_jpeg_addrs(desc, dst_buf, src_buf, 0); 928 mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(src_buf, 0), 1024)); 929 print_descriptor_info(jpeg->dev, desc); 930 931 if (!jpeg_src_buf->dht_needed) { 932 /* validate the decoding descriptor */ 933 mxc_jpeg_set_desc(desc_handle, reg, slot); 934 return; 935 } 936 937 /* 938 * if a default huffman table is needed, use the config descriptor to 939 * inject a DHT, by chaining it before the decoding descriptor 940 */ 941 *cfg_size = mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr, 942 V4L2_PIX_FMT_YUYV, 943 MXC_JPEG_MIN_WIDTH, 944 MXC_JPEG_MIN_HEIGHT); 945 cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN; 946 cfg_desc->buf_base0 = vb2_dma_contig_plane_dma_addr(dst_buf, 0); 947 cfg_desc->buf_base1 = 0; 948 cfg_desc->imgsize = MXC_JPEG_MIN_WIDTH << 16; 949 cfg_desc->imgsize |= MXC_JPEG_MIN_HEIGHT; 950 cfg_desc->line_pitch = MXC_JPEG_MIN_WIDTH * 2; 951 cfg_desc->stm_ctrl = STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV422); 952 cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 953 cfg_desc->stm_bufbase = cfg_stream_handle; 954 cfg_desc->stm_bufsize = ALIGN(*cfg_size, 1024); 955 print_descriptor_info(jpeg->dev, cfg_desc); 956 957 /* validate the configuration descriptor */ 958 mxc_jpeg_set_desc(cfg_desc_handle, reg, slot); 959 } 960 961 static void mxc_jpeg_config_enc_desc(struct vb2_buffer *out_buf, 962 struct mxc_jpeg_ctx *ctx, 963 struct vb2_buffer *src_buf, 964 struct vb2_buffer *dst_buf) 965 { 966 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 967 void __iomem *reg = jpeg->base_reg; 968 unsigned int slot = ctx->slot; 969 struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc; 970 struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc; 971 dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle; 972 dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle; 973 void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr; 974 struct mxc_jpeg_q_data *q_data; 975 enum mxc_jpeg_image_format img_fmt; 976 int w, h; 977 978 q_data = mxc_jpeg_get_q_data(ctx, src_buf->vb2_queue->type); 979 980 jpeg->slot_data[slot].cfg_stream_size = 981 mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr, 982 q_data->fmt->fourcc, 983 q_data->crop.width, 984 q_data->crop.height); 985 986 /* chain the config descriptor with the encoding descriptor */ 987 cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN; 988 989 cfg_desc->buf_base0 = jpeg->slot_data[slot].cfg_stream_handle; 990 cfg_desc->buf_base1 = 0; 991 cfg_desc->line_pitch = 0; 992 cfg_desc->stm_bufbase = 0; /* no output expected */ 993 cfg_desc->stm_bufsize = 0x0; 994 cfg_desc->imgsize = 0; 995 cfg_desc->stm_ctrl = STM_CTRL_CONFIG_MOD(1); 996 cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 997 998 desc->next_descpt_ptr = 0; /* end of chain */ 999 1000 /* use adjusted resolution for CAST IP job */ 1001 w = q_data->crop.width; 1002 h = q_data->crop.height; 1003 v4l_bound_align_image(&w, w, MXC_JPEG_MAX_WIDTH, q_data->fmt->h_align, 1004 &h, h, MXC_JPEG_MAX_HEIGHT, q_data->fmt->v_align, 0); 1005 mxc_jpeg_set_res(desc, w, h); 1006 mxc_jpeg_set_line_pitch(desc, q_data->bytesperline[0]); 1007 mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(dst_buf, 0), 1024)); 1008 img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data->fmt->fourcc); 1009 if (img_fmt == MXC_JPEG_INVALID) 1010 dev_err(jpeg->dev, "No valid image format detected\n"); 1011 desc->stm_ctrl = STM_CTRL_CONFIG_MOD(0) | 1012 STM_CTRL_IMAGE_FORMAT(img_fmt); 1013 desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1); 1014 mxc_jpeg_addrs(desc, src_buf, dst_buf, 0); 1015 dev_dbg(jpeg->dev, "cfg_desc:\n"); 1016 print_descriptor_info(jpeg->dev, cfg_desc); 1017 dev_dbg(jpeg->dev, "enc desc:\n"); 1018 print_descriptor_info(jpeg->dev, desc); 1019 print_wrapper_info(jpeg->dev, reg); 1020 print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE); 1021 1022 /* validate the configuration descriptor */ 1023 mxc_jpeg_set_desc(cfg_desc_handle, reg, slot); 1024 } 1025 1026 static const struct mxc_jpeg_fmt *mxc_jpeg_get_sibling_format(const struct mxc_jpeg_fmt *fmt) 1027 { 1028 int i; 1029 1030 for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) { 1031 if (mxc_formats[i].subsampling == fmt->subsampling && 1032 mxc_formats[i].nc == fmt->nc && 1033 mxc_formats[i].precision == fmt->precision && 1034 mxc_formats[i].is_rgb == fmt->is_rgb && 1035 mxc_formats[i].fourcc != fmt->fourcc) 1036 return &mxc_formats[i]; 1037 } 1038 1039 return NULL; 1040 } 1041 1042 static bool mxc_jpeg_compare_format(const struct mxc_jpeg_fmt *fmt1, 1043 const struct mxc_jpeg_fmt *fmt2) 1044 { 1045 if (fmt1 == fmt2) 1046 return true; 1047 if (mxc_jpeg_get_sibling_format(fmt1) == fmt2) 1048 return true; 1049 return false; 1050 } 1051 1052 static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx *ctx, 1053 struct mxc_jpeg_src_buf *jpeg_src_buf) 1054 { 1055 struct device *dev = ctx->mxc_jpeg->dev; 1056 struct mxc_jpeg_q_data *q_data_cap; 1057 1058 if (!jpeg_src_buf->fmt) 1059 return false; 1060 1061 q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1062 if (mxc_jpeg_compare_format(q_data_cap->fmt, jpeg_src_buf->fmt)) 1063 jpeg_src_buf->fmt = q_data_cap->fmt; 1064 if (q_data_cap->fmt != jpeg_src_buf->fmt || 1065 q_data_cap->w != jpeg_src_buf->w || 1066 q_data_cap->h != jpeg_src_buf->h) { 1067 dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n", 1068 q_data_cap->w, q_data_cap->h, 1069 jpeg_src_buf->w, jpeg_src_buf->h, 1070 (jpeg_src_buf->fmt->fourcc & 0xff), 1071 (jpeg_src_buf->fmt->fourcc >> 8) & 0xff, 1072 (jpeg_src_buf->fmt->fourcc >> 16) & 0xff, 1073 (jpeg_src_buf->fmt->fourcc >> 24) & 0xff); 1074 1075 /* 1076 * set-up the capture queue with the pixelformat and resolution 1077 * detected from the jpeg output stream 1078 */ 1079 q_data_cap->w = jpeg_src_buf->w; 1080 q_data_cap->h = jpeg_src_buf->h; 1081 q_data_cap->fmt = jpeg_src_buf->fmt; 1082 q_data_cap->w_adjusted = q_data_cap->w; 1083 q_data_cap->h_adjusted = q_data_cap->h; 1084 q_data_cap->crop.left = 0; 1085 q_data_cap->crop.top = 0; 1086 q_data_cap->crop.width = jpeg_src_buf->w; 1087 q_data_cap->crop.height = jpeg_src_buf->h; 1088 1089 /* 1090 * align up the resolution for CAST IP, 1091 * but leave the buffer resolution unchanged 1092 */ 1093 v4l_bound_align_image(&q_data_cap->w_adjusted, 1094 q_data_cap->w_adjusted, /* adjust up */ 1095 MXC_JPEG_MAX_WIDTH, 1096 q_data_cap->fmt->h_align, 1097 &q_data_cap->h_adjusted, 1098 q_data_cap->h_adjusted, /* adjust up */ 1099 MXC_JPEG_MAX_HEIGHT, 1100 q_data_cap->fmt->v_align, 1101 0); 1102 1103 /* setup bytesperline/sizeimage for capture queue */ 1104 mxc_jpeg_bytesperline(q_data_cap, jpeg_src_buf->fmt->precision); 1105 mxc_jpeg_sizeimage(q_data_cap); 1106 notify_src_chg(ctx); 1107 ctx->source_change = 1; 1108 } 1109 1110 return ctx->source_change ? true : false; 1111 } 1112 1113 static int mxc_jpeg_job_ready(void *priv) 1114 { 1115 struct mxc_jpeg_ctx *ctx = priv; 1116 1117 return ctx->source_change ? 0 : 1; 1118 } 1119 1120 static void mxc_jpeg_device_run_timeout(struct work_struct *work) 1121 { 1122 struct delayed_work *dwork = to_delayed_work(work); 1123 struct mxc_jpeg_ctx *ctx = container_of(dwork, struct mxc_jpeg_ctx, task_timer); 1124 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 1125 unsigned long flags; 1126 1127 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1128 if (ctx->slot < MXC_MAX_SLOTS && ctx->mxc_jpeg->slot_data[ctx->slot].used) { 1129 dev_warn(jpeg->dev, "%s timeout, cancel it\n", 1130 ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ? "decode" : "encode"); 1131 mxc_jpeg_job_finish(ctx, VB2_BUF_STATE_ERROR, true); 1132 v4l2_m2m_job_finish(ctx->mxc_jpeg->m2m_dev, ctx->fh.m2m_ctx); 1133 } 1134 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1135 } 1136 1137 static void mxc_jpeg_device_run(void *priv) 1138 { 1139 struct mxc_jpeg_ctx *ctx = priv; 1140 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 1141 void __iomem *reg = jpeg->base_reg; 1142 struct device *dev = jpeg->dev; 1143 struct vb2_v4l2_buffer *src_buf, *dst_buf; 1144 unsigned long flags; 1145 struct mxc_jpeg_q_data *q_data_cap, *q_data_out; 1146 struct mxc_jpeg_src_buf *jpeg_src_buf; 1147 1148 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1149 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 1150 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 1151 if (!src_buf || !dst_buf) { 1152 dev_err(dev, "Null src or dst buf\n"); 1153 goto end; 1154 } 1155 1156 q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1157 if (!q_data_cap) 1158 goto end; 1159 q_data_out = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 1160 if (!q_data_out) 1161 goto end; 1162 src_buf->sequence = q_data_out->sequence++; 1163 dst_buf->sequence = q_data_cap->sequence++; 1164 1165 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true); 1166 1167 jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf); 1168 if (q_data_cap->fmt->mem_planes != dst_buf->vb2_buf.num_planes) { 1169 dev_err(dev, "Capture format %s has %d planes, but capture buffer has %d planes\n", 1170 q_data_cap->fmt->name, q_data_cap->fmt->mem_planes, 1171 dst_buf->vb2_buf.num_planes); 1172 jpeg_src_buf->jpeg_parse_error = true; 1173 } 1174 if (jpeg_src_buf->jpeg_parse_error) { 1175 mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf); 1176 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1177 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1178 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); 1179 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); 1180 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1181 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1182 1183 return; 1184 } 1185 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) { 1186 if (ctx->source_change || mxc_jpeg_source_change(ctx, jpeg_src_buf)) { 1187 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1188 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1189 return; 1190 } 1191 } 1192 1193 mxc_jpeg_enable(reg); 1194 mxc_jpeg_set_l_endian(reg, 1); 1195 1196 ctx->slot = mxc_get_free_slot(jpeg->slot_data, MXC_MAX_SLOTS); 1197 if (ctx->slot >= MXC_MAX_SLOTS) { 1198 dev_err(dev, "No more free slots\n"); 1199 goto end; 1200 } 1201 if (!mxc_jpeg_alloc_slot_data(jpeg, ctx->slot)) { 1202 dev_err(dev, "Cannot allocate slot data\n"); 1203 goto end; 1204 } 1205 1206 mxc_jpeg_enable_slot(reg, ctx->slot); 1207 mxc_jpeg_enable_irq(reg, ctx->slot); 1208 1209 if (jpeg->mode == MXC_JPEG_ENCODE) { 1210 dev_dbg(dev, "Encoding on slot %d\n", ctx->slot); 1211 ctx->enc_state = MXC_JPEG_ENC_CONF; 1212 mxc_jpeg_config_enc_desc(&dst_buf->vb2_buf, ctx, 1213 &src_buf->vb2_buf, &dst_buf->vb2_buf); 1214 mxc_jpeg_enc_mode_conf(dev, reg); /* start config phase */ 1215 } else { 1216 dev_dbg(dev, "Decoding on slot %d\n", ctx->slot); 1217 print_mxc_buf(jpeg, &src_buf->vb2_buf, 0); 1218 mxc_jpeg_config_dec_desc(&dst_buf->vb2_buf, ctx, 1219 &src_buf->vb2_buf, &dst_buf->vb2_buf); 1220 mxc_jpeg_dec_mode_go(dev, reg); 1221 } 1222 schedule_delayed_work(&ctx->task_timer, msecs_to_jiffies(hw_timeout)); 1223 end: 1224 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1225 } 1226 1227 static int mxc_jpeg_decoder_cmd(struct file *file, void *priv, 1228 struct v4l2_decoder_cmd *cmd) 1229 { 1230 struct v4l2_fh *fh = file->private_data; 1231 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 1232 unsigned long flags; 1233 int ret; 1234 1235 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd); 1236 if (ret < 0) 1237 return ret; 1238 1239 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx))) 1240 return 0; 1241 1242 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1243 ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, cmd); 1244 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1245 if (ret < 0) 1246 return ret; 1247 1248 if (cmd->cmd == V4L2_DEC_CMD_STOP && 1249 v4l2_m2m_has_stopped(fh->m2m_ctx)) { 1250 notify_eos(ctx); 1251 ctx->header_parsed = false; 1252 } 1253 1254 if (cmd->cmd == V4L2_DEC_CMD_START && 1255 v4l2_m2m_has_stopped(fh->m2m_ctx)) 1256 vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q); 1257 return 0; 1258 } 1259 1260 static int mxc_jpeg_encoder_cmd(struct file *file, void *priv, 1261 struct v4l2_encoder_cmd *cmd) 1262 { 1263 struct v4l2_fh *fh = file->private_data; 1264 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 1265 unsigned long flags; 1266 int ret; 1267 1268 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd); 1269 if (ret < 0) 1270 return ret; 1271 1272 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)) || 1273 !vb2_is_streaming(v4l2_m2m_get_dst_vq(fh->m2m_ctx))) 1274 return 0; 1275 1276 spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags); 1277 ret = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd); 1278 spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags); 1279 if (ret < 0) 1280 return 0; 1281 1282 if (cmd->cmd == V4L2_ENC_CMD_STOP && 1283 v4l2_m2m_has_stopped(fh->m2m_ctx)) 1284 notify_eos(ctx); 1285 1286 if (cmd->cmd == V4L2_ENC_CMD_START && 1287 v4l2_m2m_has_stopped(fh->m2m_ctx)) 1288 vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q); 1289 1290 return 0; 1291 } 1292 1293 static int mxc_jpeg_queue_setup(struct vb2_queue *q, 1294 unsigned int *nbuffers, 1295 unsigned int *nplanes, 1296 unsigned int sizes[], 1297 struct device *alloc_ctxs[]) 1298 { 1299 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q); 1300 struct mxc_jpeg_q_data *q_data = NULL; 1301 int i; 1302 1303 q_data = mxc_jpeg_get_q_data(ctx, q->type); 1304 if (!q_data) 1305 return -EINVAL; 1306 1307 /* Handle CREATE_BUFS situation - *nplanes != 0 */ 1308 if (*nplanes) { 1309 if (*nplanes != q_data->fmt->mem_planes) 1310 return -EINVAL; 1311 for (i = 0; i < *nplanes; i++) { 1312 if (sizes[i] < mxc_jpeg_get_plane_size(q_data, i)) 1313 return -EINVAL; 1314 } 1315 return 0; 1316 } 1317 1318 /* Handle REQBUFS situation */ 1319 *nplanes = q_data->fmt->mem_planes; 1320 for (i = 0; i < *nplanes; i++) 1321 sizes[i] = mxc_jpeg_get_plane_size(q_data, i); 1322 1323 return 0; 1324 } 1325 1326 static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count) 1327 { 1328 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q); 1329 struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type); 1330 int ret; 1331 1332 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q); 1333 1334 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(q->type)) 1335 ctx->source_change = 0; 1336 dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx); 1337 q_data->sequence = 0; 1338 1339 ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev); 1340 if (ret < 0) { 1341 dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n"); 1342 return ret; 1343 } 1344 1345 return 0; 1346 } 1347 1348 static void mxc_jpeg_stop_streaming(struct vb2_queue *q) 1349 { 1350 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q); 1351 struct vb2_v4l2_buffer *vbuf; 1352 1353 dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx); 1354 1355 /* Release all active buffers */ 1356 for (;;) { 1357 if (V4L2_TYPE_IS_OUTPUT(q->type)) 1358 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1359 else 1360 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1361 if (!vbuf) 1362 break; 1363 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 1364 } 1365 1366 if (V4L2_TYPE_IS_OUTPUT(q->type) || !ctx->source_change) 1367 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); 1368 if (V4L2_TYPE_IS_OUTPUT(q->type) && 1369 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) { 1370 notify_eos(ctx); 1371 ctx->header_parsed = false; 1372 } 1373 1374 pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev); 1375 } 1376 1377 static int mxc_jpeg_valid_comp_id(struct device *dev, 1378 struct mxc_jpeg_sof *sof, 1379 struct mxc_jpeg_sos *sos) 1380 { 1381 int valid = 1; 1382 int i; 1383 1384 /* 1385 * there's a limitation in the IP that the component IDs must be 1386 * between 0..4, if they are not, let's patch them 1387 */ 1388 for (i = 0; i < sof->components_no; i++) 1389 if (sof->comp[i].id > MXC_JPEG_MAX_COMPONENTS) { 1390 valid = 0; 1391 dev_err(dev, "Component %d has invalid ID: %d", 1392 i, sof->comp[i].id); 1393 } 1394 if (!valid) 1395 /* patch all comp IDs if at least one is invalid */ 1396 for (i = 0; i < sof->components_no; i++) { 1397 dev_warn(dev, "Component %d ID patched to: %d", 1398 i, i + 1); 1399 sof->comp[i].id = i + 1; 1400 sos->comp[i].id = i + 1; 1401 } 1402 1403 return valid; 1404 } 1405 1406 static bool mxc_jpeg_match_image_format(const struct mxc_jpeg_fmt *fmt, 1407 const struct v4l2_jpeg_header *header) 1408 { 1409 if (fmt->subsampling != header->frame.subsampling || 1410 fmt->nc != header->frame.num_components || 1411 fmt->precision != header->frame.precision) 1412 return false; 1413 1414 /* 1415 * If the transform flag from APP14 marker is 0, images that are 1416 * encoded with 3 components have RGB colorspace, see Recommendation 1417 * ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding 1418 */ 1419 if (header->frame.subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) { 1420 u8 is_rgb = header->app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB ? 1 : 0; 1421 1422 if (is_rgb != fmt->is_rgb) 1423 return false; 1424 } 1425 return true; 1426 } 1427 1428 static u32 mxc_jpeg_get_image_format(struct device *dev, 1429 const struct v4l2_jpeg_header *header) 1430 { 1431 int i; 1432 u32 fourcc = 0; 1433 1434 for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) { 1435 if (mxc_jpeg_match_image_format(&mxc_formats[i], header)) { 1436 fourcc = mxc_formats[i].fourcc; 1437 break; 1438 } 1439 } 1440 if (fourcc == 0) { 1441 dev_err(dev, 1442 "Could not identify image format nc=%d, subsampling=%d, precision=%d\n", 1443 header->frame.num_components, 1444 header->frame.subsampling, 1445 header->frame.precision); 1446 return fourcc; 1447 } 1448 1449 return fourcc; 1450 } 1451 1452 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision) 1453 { 1454 /* Bytes distance between the leftmost pixels in two adjacent lines */ 1455 if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) { 1456 /* bytesperline unused for compressed formats */ 1457 q->bytesperline[0] = 0; 1458 q->bytesperline[1] = 0; 1459 } else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) { 1460 /* When the image format is planar the bytesperline value 1461 * applies to the first plane and is divided by the same factor 1462 * as the width field for the other planes 1463 */ 1464 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8); 1465 q->bytesperline[1] = q->bytesperline[0]; 1466 } else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) { 1467 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * 2; 1468 q->bytesperline[1] = 0; 1469 } else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) { 1470 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * q->fmt->nc; 1471 q->bytesperline[1] = 0; 1472 } else { 1473 /* grayscale */ 1474 q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8); 1475 q->bytesperline[1] = 0; 1476 } 1477 } 1478 1479 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q) 1480 { 1481 if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) { 1482 /* if no sizeimage from user, assume worst jpeg compression */ 1483 if (!q->sizeimage[0]) 1484 q->sizeimage[0] = 6 * q->w * q->h; 1485 q->sizeimage[1] = 0; 1486 1487 if (q->sizeimage[0] > MXC_JPEG_MAX_SIZEIMAGE) 1488 q->sizeimage[0] = MXC_JPEG_MAX_SIZEIMAGE; 1489 1490 /* jpeg stream size must be multiple of 1K */ 1491 q->sizeimage[0] = ALIGN(q->sizeimage[0], 1024); 1492 } else { 1493 q->sizeimage[0] = q->bytesperline[0] * q->h_adjusted; 1494 q->sizeimage[1] = 0; 1495 if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) 1496 q->sizeimage[1] = q->sizeimage[0] / 2; 1497 } 1498 } 1499 1500 static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, struct vb2_buffer *vb) 1501 { 1502 struct device *dev = ctx->mxc_jpeg->dev; 1503 struct mxc_jpeg_q_data *q_data_out; 1504 struct mxc_jpeg_q_data *q_data_cap; 1505 u32 fourcc; 1506 struct v4l2_jpeg_header header; 1507 struct mxc_jpeg_sof *psof = NULL; 1508 struct mxc_jpeg_sos *psos = NULL; 1509 struct mxc_jpeg_src_buf *jpeg_src_buf = vb2_to_mxc_buf(vb); 1510 u8 *src_addr = (u8 *)vb2_plane_vaddr(vb, 0); 1511 u32 size = vb2_get_plane_payload(vb, 0); 1512 int ret; 1513 1514 memset(&header, 0, sizeof(header)); 1515 ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header); 1516 if (ret < 0) { 1517 dev_err(dev, "Error parsing JPEG stream markers\n"); 1518 return ret; 1519 } 1520 1521 /* if DHT marker present, no need to inject default one */ 1522 jpeg_src_buf->dht_needed = (header.num_dht == 0); 1523 1524 q_data_out = mxc_jpeg_get_q_data(ctx, 1525 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 1526 if (q_data_out->w == 0 && q_data_out->h == 0) { 1527 dev_warn(dev, "Invalid user resolution 0x0"); 1528 dev_warn(dev, "Keeping resolution from JPEG: %dx%d", 1529 header.frame.width, header.frame.height); 1530 } else if (header.frame.width != q_data_out->w || 1531 header.frame.height != q_data_out->h) { 1532 dev_err(dev, 1533 "Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)", 1534 header.frame.width, header.frame.height, 1535 q_data_out->w, q_data_out->h); 1536 } 1537 q_data_out->w = header.frame.width; 1538 q_data_out->h = header.frame.height; 1539 if (header.frame.width > MXC_JPEG_MAX_WIDTH || 1540 header.frame.height > MXC_JPEG_MAX_HEIGHT) { 1541 dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n", 1542 header.frame.width, header.frame.height); 1543 return -EINVAL; 1544 } 1545 if (header.frame.width < MXC_JPEG_MIN_WIDTH || 1546 header.frame.height < MXC_JPEG_MIN_HEIGHT) { 1547 dev_err(dev, "JPEG width or height should be > 64: %dx%d\n", 1548 header.frame.width, header.frame.height); 1549 return -EINVAL; 1550 } 1551 if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) { 1552 dev_err(dev, "JPEG number of components should be <=%d", 1553 V4L2_JPEG_MAX_COMPONENTS); 1554 return -EINVAL; 1555 } 1556 /* check and, if necessary, patch component IDs*/ 1557 psof = (struct mxc_jpeg_sof *)header.sof.start; 1558 psos = (struct mxc_jpeg_sos *)header.sos.start; 1559 if (!mxc_jpeg_valid_comp_id(dev, psof, psos)) 1560 dev_warn(dev, "JPEG component ids should be 0-3 or 1-4"); 1561 1562 q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 1563 if (q_data_cap->fmt && mxc_jpeg_match_image_format(q_data_cap->fmt, &header)) 1564 fourcc = q_data_cap->fmt->fourcc; 1565 else 1566 fourcc = mxc_jpeg_get_image_format(dev, &header); 1567 if (fourcc == 0) 1568 return -EINVAL; 1569 1570 jpeg_src_buf->fmt = mxc_jpeg_find_format(ctx, fourcc); 1571 jpeg_src_buf->w = header.frame.width; 1572 jpeg_src_buf->h = header.frame.height; 1573 ctx->header_parsed = true; 1574 1575 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) 1576 mxc_jpeg_source_change(ctx, jpeg_src_buf); 1577 1578 return 0; 1579 } 1580 1581 static void mxc_jpeg_buf_queue(struct vb2_buffer *vb) 1582 { 1583 int ret; 1584 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1585 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1586 struct mxc_jpeg_src_buf *jpeg_src_buf; 1587 1588 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && 1589 vb2_is_streaming(vb->vb2_queue) && 1590 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) { 1591 struct mxc_jpeg_q_data *q_data; 1592 1593 q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type); 1594 vbuf->field = V4L2_FIELD_NONE; 1595 vbuf->sequence = q_data->sequence++; 1596 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf); 1597 notify_eos(ctx); 1598 ctx->header_parsed = false; 1599 return; 1600 } 1601 1602 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1603 goto end; 1604 1605 /* for V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE */ 1606 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE) 1607 goto end; 1608 1609 jpeg_src_buf = vb2_to_mxc_buf(vb); 1610 jpeg_src_buf->jpeg_parse_error = false; 1611 ret = mxc_jpeg_parse(ctx, vb); 1612 if (ret) 1613 jpeg_src_buf->jpeg_parse_error = true; 1614 1615 end: 1616 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1617 } 1618 1619 static int mxc_jpeg_buf_out_validate(struct vb2_buffer *vb) 1620 { 1621 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1622 1623 vbuf->field = V4L2_FIELD_NONE; 1624 1625 return 0; 1626 } 1627 1628 static int mxc_jpeg_buf_prepare(struct vb2_buffer *vb) 1629 { 1630 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1631 struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1632 struct mxc_jpeg_q_data *q_data = NULL; 1633 struct device *dev = ctx->mxc_jpeg->dev; 1634 unsigned long sizeimage; 1635 int i; 1636 1637 vbuf->field = V4L2_FIELD_NONE; 1638 1639 q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type); 1640 if (!q_data) 1641 return -EINVAL; 1642 for (i = 0; i < q_data->fmt->mem_planes; i++) { 1643 sizeimage = mxc_jpeg_get_plane_size(q_data, i); 1644 if (vb2_plane_size(vb, i) < sizeimage) { 1645 dev_err(dev, "plane %d too small (%lu < %lu)", 1646 i, vb2_plane_size(vb, i), sizeimage); 1647 return -EINVAL; 1648 } 1649 } 1650 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) { 1651 vb2_set_plane_payload(vb, 0, 0); 1652 vb2_set_plane_payload(vb, 1, 0); 1653 } 1654 return 0; 1655 } 1656 1657 static const struct vb2_ops mxc_jpeg_qops = { 1658 .queue_setup = mxc_jpeg_queue_setup, 1659 .wait_prepare = vb2_ops_wait_prepare, 1660 .wait_finish = vb2_ops_wait_finish, 1661 .buf_out_validate = mxc_jpeg_buf_out_validate, 1662 .buf_prepare = mxc_jpeg_buf_prepare, 1663 .start_streaming = mxc_jpeg_start_streaming, 1664 .stop_streaming = mxc_jpeg_stop_streaming, 1665 .buf_queue = mxc_jpeg_buf_queue, 1666 }; 1667 1668 static int mxc_jpeg_queue_init(void *priv, struct vb2_queue *src_vq, 1669 struct vb2_queue *dst_vq) 1670 { 1671 struct mxc_jpeg_ctx *ctx = priv; 1672 int ret; 1673 1674 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1675 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1676 src_vq->drv_priv = ctx; 1677 src_vq->buf_struct_size = sizeof(struct mxc_jpeg_src_buf); 1678 src_vq->ops = &mxc_jpeg_qops; 1679 src_vq->mem_ops = &vb2_dma_contig_memops; 1680 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1681 src_vq->lock = &ctx->mxc_jpeg->lock; 1682 src_vq->dev = ctx->mxc_jpeg->dev; 1683 1684 ret = vb2_queue_init(src_vq); 1685 if (ret) 1686 return ret; 1687 1688 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1689 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 1690 dst_vq->drv_priv = ctx; 1691 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1692 dst_vq->ops = &mxc_jpeg_qops; 1693 dst_vq->mem_ops = &vb2_dma_contig_memops; 1694 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1695 dst_vq->lock = &ctx->mxc_jpeg->lock; 1696 dst_vq->dev = ctx->mxc_jpeg->dev; 1697 1698 ret = vb2_queue_init(dst_vq); 1699 return ret; 1700 } 1701 1702 static void mxc_jpeg_set_default_params(struct mxc_jpeg_ctx *ctx) 1703 { 1704 struct mxc_jpeg_q_data *out_q = &ctx->out_q; 1705 struct mxc_jpeg_q_data *cap_q = &ctx->cap_q; 1706 struct mxc_jpeg_q_data *q[2] = {out_q, cap_q}; 1707 int i; 1708 1709 if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) { 1710 out_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT); 1711 cap_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG); 1712 } else { 1713 out_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG); 1714 cap_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT); 1715 } 1716 1717 for (i = 0; i < 2; i++) { 1718 q[i]->w = MXC_JPEG_DEFAULT_WIDTH; 1719 q[i]->h = MXC_JPEG_DEFAULT_HEIGHT; 1720 q[i]->w_adjusted = MXC_JPEG_DEFAULT_WIDTH; 1721 q[i]->h_adjusted = MXC_JPEG_DEFAULT_HEIGHT; 1722 q[i]->crop.left = 0; 1723 q[i]->crop.top = 0; 1724 q[i]->crop.width = MXC_JPEG_DEFAULT_WIDTH; 1725 q[i]->crop.height = MXC_JPEG_DEFAULT_HEIGHT; 1726 mxc_jpeg_bytesperline(q[i], q[i]->fmt->precision); 1727 mxc_jpeg_sizeimage(q[i]); 1728 } 1729 } 1730 1731 static int mxc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl) 1732 { 1733 struct mxc_jpeg_ctx *ctx = 1734 container_of(ctrl->handler, struct mxc_jpeg_ctx, ctrl_handler); 1735 1736 switch (ctrl->id) { 1737 case V4L2_CID_JPEG_COMPRESSION_QUALITY: 1738 ctx->jpeg_quality = ctrl->val; 1739 break; 1740 default: 1741 dev_err(ctx->mxc_jpeg->dev, "Invalid control, id = %d, val = %d\n", 1742 ctrl->id, ctrl->val); 1743 return -EINVAL; 1744 } 1745 1746 return 0; 1747 } 1748 1749 static const struct v4l2_ctrl_ops mxc_jpeg_ctrl_ops = { 1750 .s_ctrl = mxc_jpeg_s_ctrl, 1751 }; 1752 1753 static void mxc_jpeg_encode_ctrls(struct mxc_jpeg_ctx *ctx) 1754 { 1755 v4l2_ctrl_new_std(&ctx->ctrl_handler, &mxc_jpeg_ctrl_ops, 1756 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75); 1757 } 1758 1759 static int mxc_jpeg_ctrls_setup(struct mxc_jpeg_ctx *ctx) 1760 { 1761 int err; 1762 1763 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 2); 1764 1765 if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) 1766 mxc_jpeg_encode_ctrls(ctx); 1767 1768 if (ctx->ctrl_handler.error) { 1769 err = ctx->ctrl_handler.error; 1770 1771 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1772 return err; 1773 } 1774 1775 err = v4l2_ctrl_handler_setup(&ctx->ctrl_handler); 1776 if (err) 1777 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1778 return err; 1779 } 1780 1781 static int mxc_jpeg_open(struct file *file) 1782 { 1783 struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file); 1784 struct video_device *mxc_vfd = video_devdata(file); 1785 struct device *dev = mxc_jpeg->dev; 1786 struct mxc_jpeg_ctx *ctx; 1787 int ret = 0; 1788 1789 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1790 if (!ctx) 1791 return -ENOMEM; 1792 1793 if (mutex_lock_interruptible(&mxc_jpeg->lock)) { 1794 ret = -ERESTARTSYS; 1795 goto free; 1796 } 1797 1798 v4l2_fh_init(&ctx->fh, mxc_vfd); 1799 file->private_data = &ctx->fh; 1800 v4l2_fh_add(&ctx->fh); 1801 1802 ctx->mxc_jpeg = mxc_jpeg; 1803 1804 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(mxc_jpeg->m2m_dev, ctx, 1805 mxc_jpeg_queue_init); 1806 1807 if (IS_ERR(ctx->fh.m2m_ctx)) { 1808 ret = PTR_ERR(ctx->fh.m2m_ctx); 1809 goto error; 1810 } 1811 1812 ret = mxc_jpeg_ctrls_setup(ctx); 1813 if (ret) { 1814 dev_err(ctx->mxc_jpeg->dev, "failed to setup mxc jpeg controls\n"); 1815 goto err_ctrls_setup; 1816 } 1817 ctx->fh.ctrl_handler = &ctx->ctrl_handler; 1818 mxc_jpeg_set_default_params(ctx); 1819 ctx->slot = MXC_MAX_SLOTS; /* slot not allocated yet */ 1820 INIT_DELAYED_WORK(&ctx->task_timer, mxc_jpeg_device_run_timeout); 1821 1822 if (mxc_jpeg->mode == MXC_JPEG_DECODE) 1823 dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx); 1824 else 1825 dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx); 1826 mutex_unlock(&mxc_jpeg->lock); 1827 1828 return 0; 1829 1830 err_ctrls_setup: 1831 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 1832 error: 1833 v4l2_fh_del(&ctx->fh); 1834 v4l2_fh_exit(&ctx->fh); 1835 mutex_unlock(&mxc_jpeg->lock); 1836 free: 1837 kfree(ctx); 1838 return ret; 1839 } 1840 1841 static int mxc_jpeg_querycap(struct file *file, void *priv, 1842 struct v4l2_capability *cap) 1843 { 1844 strscpy(cap->driver, MXC_JPEG_NAME " codec", sizeof(cap->driver)); 1845 strscpy(cap->card, MXC_JPEG_NAME " codec", sizeof(cap->card)); 1846 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE; 1847 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 1848 1849 return 0; 1850 } 1851 1852 static int mxc_jpeg_enum_fmt_vid_cap(struct file *file, void *priv, 1853 struct v4l2_fmtdesc *f) 1854 { 1855 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 1856 struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type); 1857 1858 if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) { 1859 return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, 1860 MXC_JPEG_FMT_TYPE_ENC); 1861 } else if (!ctx->header_parsed) { 1862 return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, 1863 MXC_JPEG_FMT_TYPE_RAW); 1864 } else { 1865 /* For the decoder CAPTURE queue, only enumerate the raw formats 1866 * supported for the format currently active on OUTPUT 1867 * (more precisely what was propagated on capture queue 1868 * after jpeg parse on the output buffer) 1869 */ 1870 int ret = -EINVAL; 1871 const struct mxc_jpeg_fmt *sibling; 1872 1873 switch (f->index) { 1874 case 0: 1875 f->pixelformat = q_data->fmt->fourcc; 1876 ret = 0; 1877 break; 1878 case 1: 1879 sibling = mxc_jpeg_get_sibling_format(q_data->fmt); 1880 if (sibling) { 1881 f->pixelformat = sibling->fourcc; 1882 ret = 0; 1883 } 1884 break; 1885 default: 1886 break; 1887 } 1888 return ret; 1889 } 1890 } 1891 1892 static int mxc_jpeg_enum_fmt_vid_out(struct file *file, void *priv, 1893 struct v4l2_fmtdesc *f) 1894 { 1895 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 1896 u32 type = ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ? MXC_JPEG_FMT_TYPE_ENC : 1897 MXC_JPEG_FMT_TYPE_RAW; 1898 int ret; 1899 1900 ret = enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, type); 1901 if (ret) 1902 return ret; 1903 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 1904 f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION; 1905 return 0; 1906 } 1907 1908 static u32 mxc_jpeg_get_fmt_type(struct mxc_jpeg_ctx *ctx, u32 type) 1909 { 1910 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 1911 return V4L2_TYPE_IS_OUTPUT(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW; 1912 else 1913 return V4L2_TYPE_IS_CAPTURE(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW; 1914 } 1915 1916 static u32 mxc_jpeg_get_default_fourcc(struct mxc_jpeg_ctx *ctx, u32 type) 1917 { 1918 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 1919 return V4L2_TYPE_IS_OUTPUT(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT; 1920 else 1921 return V4L2_TYPE_IS_CAPTURE(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT; 1922 } 1923 1924 static u32 mxc_jpeg_try_fourcc(struct mxc_jpeg_ctx *ctx, u32 fourcc) 1925 { 1926 const struct mxc_jpeg_fmt *sibling; 1927 struct mxc_jpeg_q_data *q_data_cap; 1928 1929 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE) 1930 return fourcc; 1931 if (!ctx->header_parsed) 1932 return fourcc; 1933 1934 q_data_cap = &ctx->cap_q; 1935 if (q_data_cap->fmt->fourcc == fourcc) 1936 return fourcc; 1937 1938 sibling = mxc_jpeg_get_sibling_format(q_data_cap->fmt); 1939 if (sibling && sibling->fourcc == fourcc) 1940 return sibling->fourcc; 1941 1942 return q_data_cap->fmt->fourcc; 1943 } 1944 1945 static int mxc_jpeg_try_fmt(struct v4l2_format *f, 1946 struct mxc_jpeg_ctx *ctx, struct mxc_jpeg_q_data *q_data) 1947 { 1948 const struct mxc_jpeg_fmt *fmt; 1949 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 1950 struct v4l2_plane_pix_format *pfmt; 1951 u32 fourcc = f->fmt.pix_mp.pixelformat; 1952 u32 w = (pix_mp->width < MXC_JPEG_MAX_WIDTH) ? 1953 pix_mp->width : MXC_JPEG_MAX_WIDTH; 1954 u32 h = (pix_mp->height < MXC_JPEG_MAX_HEIGHT) ? 1955 pix_mp->height : MXC_JPEG_MAX_HEIGHT; 1956 int i; 1957 1958 fmt = mxc_jpeg_find_format(ctx, fourcc); 1959 if (!fmt || fmt->flags != mxc_jpeg_get_fmt_type(ctx, f->type)) { 1960 dev_warn(ctx->mxc_jpeg->dev, "Format not supported: %c%c%c%c, use the default.\n", 1961 (fourcc & 0xff), 1962 (fourcc >> 8) & 0xff, 1963 (fourcc >> 16) & 0xff, 1964 (fourcc >> 24) & 0xff); 1965 fourcc = mxc_jpeg_get_default_fourcc(ctx, f->type); 1966 fmt = mxc_jpeg_find_format(ctx, fourcc); 1967 if (!fmt) 1968 return -EINVAL; 1969 f->fmt.pix_mp.pixelformat = fourcc; 1970 } 1971 q_data->fmt = fmt; 1972 1973 memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved)); 1974 pix_mp->field = V4L2_FIELD_NONE; 1975 pix_mp->num_planes = fmt->mem_planes; 1976 pix_mp->pixelformat = fmt->fourcc; 1977 1978 q_data->w = w; 1979 q_data->h = h; 1980 q_data->w_adjusted = w; 1981 q_data->h_adjusted = h; 1982 v4l_bound_align_image(&q_data->w_adjusted, 1983 w, /* adjust upwards*/ 1984 MXC_JPEG_MAX_WIDTH, 1985 fmt->h_align, 1986 &q_data->h_adjusted, 1987 h, /* adjust upwards*/ 1988 MXC_JPEG_MAX_HEIGHT, 1989 fmt->v_align, 1990 0); 1991 for (i = 0; i < pix_mp->num_planes; i++) { 1992 pfmt = &pix_mp->plane_fmt[i]; 1993 q_data->bytesperline[i] = pfmt->bytesperline; 1994 q_data->sizeimage[i] = pfmt->sizeimage; 1995 } 1996 1997 /* calculate bytesperline & sizeimage */ 1998 mxc_jpeg_bytesperline(q_data, fmt->precision); 1999 mxc_jpeg_sizeimage(q_data); 2000 2001 /* adjust user format according to our calculations */ 2002 for (i = 0; i < pix_mp->num_planes; i++) { 2003 pfmt = &pix_mp->plane_fmt[i]; 2004 memset(pfmt->reserved, 0, sizeof(pfmt->reserved)); 2005 pfmt->bytesperline = q_data->bytesperline[i]; 2006 pfmt->sizeimage = mxc_jpeg_get_plane_size(q_data, i); 2007 } 2008 2009 /* fix colorspace information to sRGB for both output & capture */ 2010 pix_mp->colorspace = V4L2_COLORSPACE_SRGB; 2011 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601; 2012 pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB; 2013 /* 2014 * this hardware does not change the range of the samples 2015 * but since inside JPEG the YUV quantization is full-range, 2016 * this driver will always use full-range for the raw frames, too 2017 */ 2018 pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2019 2020 if (fmt->flags == MXC_JPEG_FMT_TYPE_RAW) { 2021 q_data->crop.left = 0; 2022 q_data->crop.top = 0; 2023 q_data->crop.width = q_data->w; 2024 q_data->crop.height = q_data->h; 2025 } 2026 2027 pix_mp->width = q_data->w_adjusted; 2028 pix_mp->height = q_data->h_adjusted; 2029 2030 return 0; 2031 } 2032 2033 static int mxc_jpeg_try_fmt_vid_cap(struct file *file, void *priv, 2034 struct v4l2_format *f) 2035 { 2036 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2037 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2038 struct device *dev = jpeg->dev; 2039 struct mxc_jpeg_q_data tmp_q; 2040 2041 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 2042 dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type); 2043 return -EINVAL; 2044 } 2045 2046 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(f->type)) 2047 f->fmt.pix_mp.pixelformat = mxc_jpeg_try_fourcc(ctx, f->fmt.pix_mp.pixelformat); 2048 2049 return mxc_jpeg_try_fmt(f, ctx, &tmp_q); 2050 } 2051 2052 static int mxc_jpeg_try_fmt_vid_out(struct file *file, void *priv, 2053 struct v4l2_format *f) 2054 { 2055 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2056 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2057 struct device *dev = jpeg->dev; 2058 struct mxc_jpeg_q_data tmp_q; 2059 2060 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 2061 dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type); 2062 return -EINVAL; 2063 } 2064 2065 return mxc_jpeg_try_fmt(f, ctx, &tmp_q); 2066 } 2067 2068 static void mxc_jpeg_s_parsed_fmt(struct mxc_jpeg_ctx *ctx, struct v4l2_format *f) 2069 { 2070 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 2071 struct mxc_jpeg_q_data *q_data_cap; 2072 2073 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE || !V4L2_TYPE_IS_CAPTURE(f->type)) 2074 return; 2075 if (!ctx->header_parsed) 2076 return; 2077 2078 q_data_cap = mxc_jpeg_get_q_data(ctx, f->type); 2079 pix_mp->pixelformat = mxc_jpeg_try_fourcc(ctx, pix_mp->pixelformat); 2080 pix_mp->width = q_data_cap->w; 2081 pix_mp->height = q_data_cap->h; 2082 } 2083 2084 static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx, 2085 struct v4l2_format *f) 2086 { 2087 struct vb2_queue *vq; 2088 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2089 2090 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 2091 if (!vq) 2092 return -EINVAL; 2093 2094 if (vb2_is_busy(vq)) { 2095 v4l2_err(&jpeg->v4l2_dev, "queue busy\n"); 2096 return -EBUSY; 2097 } 2098 2099 mxc_jpeg_s_parsed_fmt(ctx, f); 2100 2101 return mxc_jpeg_try_fmt(f, ctx, mxc_jpeg_get_q_data(ctx, f->type)); 2102 } 2103 2104 static int mxc_jpeg_s_fmt_vid_cap(struct file *file, void *priv, 2105 struct v4l2_format *f) 2106 { 2107 return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f); 2108 } 2109 2110 static int mxc_jpeg_s_fmt_vid_out(struct file *file, void *priv, 2111 struct v4l2_format *f) 2112 { 2113 int ret; 2114 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2115 struct vb2_queue *dst_vq; 2116 struct mxc_jpeg_q_data *q_data_cap; 2117 enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 2118 struct v4l2_format fc; 2119 2120 ret = mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f); 2121 if (ret) 2122 return ret; 2123 2124 if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE) 2125 return 0; 2126 2127 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, cap_type); 2128 if (!dst_vq) 2129 return -EINVAL; 2130 2131 if (vb2_is_busy(dst_vq)) 2132 return 0; 2133 2134 q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type); 2135 if (q_data_cap->w == f->fmt.pix_mp.width && q_data_cap->h == f->fmt.pix_mp.height) 2136 return 0; 2137 memset(&fc, 0, sizeof(fc)); 2138 fc.type = cap_type; 2139 fc.fmt.pix_mp.pixelformat = q_data_cap->fmt->fourcc; 2140 fc.fmt.pix_mp.width = f->fmt.pix_mp.width; 2141 fc.fmt.pix_mp.height = f->fmt.pix_mp.height; 2142 2143 return mxc_jpeg_s_fmt_vid_cap(file, priv, &fc); 2144 } 2145 2146 static int mxc_jpeg_g_fmt_vid(struct file *file, void *priv, 2147 struct v4l2_format *f) 2148 { 2149 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv); 2150 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; 2151 struct device *dev = jpeg->dev; 2152 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 2153 struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type); 2154 int i; 2155 2156 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 2157 dev_err(dev, "G_FMT with Invalid type: %d\n", f->type); 2158 return -EINVAL; 2159 } 2160 2161 pix_mp->pixelformat = q_data->fmt->fourcc; 2162 pix_mp->width = q_data->w; 2163 pix_mp->height = q_data->h; 2164 pix_mp->field = V4L2_FIELD_NONE; 2165 if (q_data->fmt->flags == MXC_JPEG_FMT_TYPE_RAW) { 2166 pix_mp->width = q_data->w_adjusted; 2167 pix_mp->height = q_data->h_adjusted; 2168 } 2169 2170 /* fix colorspace information to sRGB for both output & capture */ 2171 pix_mp->colorspace = V4L2_COLORSPACE_SRGB; 2172 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601; 2173 pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB; 2174 pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2175 2176 pix_mp->num_planes = q_data->fmt->mem_planes; 2177 for (i = 0; i < pix_mp->num_planes; i++) { 2178 pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i]; 2179 pix_mp->plane_fmt[i].sizeimage = mxc_jpeg_get_plane_size(q_data, i); 2180 } 2181 2182 return 0; 2183 } 2184 2185 static int mxc_jpeg_dec_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 2186 { 2187 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2188 struct mxc_jpeg_q_data *q_data_cap; 2189 2190 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 2191 return -EINVAL; 2192 2193 q_data_cap = mxc_jpeg_get_q_data(ctx, s->type); 2194 2195 switch (s->target) { 2196 case V4L2_SEL_TGT_COMPOSE: 2197 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 2198 s->r = q_data_cap->crop; 2199 break; 2200 case V4L2_SEL_TGT_COMPOSE_PADDED: 2201 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 2202 s->r.left = 0; 2203 s->r.top = 0; 2204 s->r.width = q_data_cap->w_adjusted; 2205 s->r.height = q_data_cap->h_adjusted; 2206 break; 2207 default: 2208 return -EINVAL; 2209 } 2210 2211 return 0; 2212 } 2213 2214 static int mxc_jpeg_enc_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 2215 { 2216 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2217 struct mxc_jpeg_q_data *q_data_out; 2218 2219 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 2220 return -EINVAL; 2221 2222 q_data_out = mxc_jpeg_get_q_data(ctx, s->type); 2223 2224 switch (s->target) { 2225 case V4L2_SEL_TGT_CROP_DEFAULT: 2226 case V4L2_SEL_TGT_CROP_BOUNDS: 2227 s->r.left = 0; 2228 s->r.top = 0; 2229 s->r.width = q_data_out->w; 2230 s->r.height = q_data_out->h; 2231 break; 2232 case V4L2_SEL_TGT_CROP: 2233 s->r = q_data_out->crop; 2234 break; 2235 default: 2236 return -EINVAL; 2237 } 2238 2239 return 0; 2240 } 2241 2242 static int mxc_jpeg_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 2243 { 2244 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2245 2246 if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) 2247 return mxc_jpeg_dec_g_selection(file, fh, s); 2248 else 2249 return mxc_jpeg_enc_g_selection(file, fh, s); 2250 } 2251 2252 static int mxc_jpeg_s_selection(struct file *file, void *fh, struct v4l2_selection *s) 2253 { 2254 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh); 2255 struct mxc_jpeg_q_data *q_data_out; 2256 2257 if (ctx->mxc_jpeg->mode != MXC_JPEG_ENCODE) 2258 return -ENOTTY; 2259 2260 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 2261 return -EINVAL; 2262 if (s->target != V4L2_SEL_TGT_CROP) 2263 return -EINVAL; 2264 2265 q_data_out = mxc_jpeg_get_q_data(ctx, s->type); 2266 if (s->r.left || s->r.top) 2267 return -EINVAL; 2268 if (s->r.width > q_data_out->w || s->r.height > q_data_out->h) 2269 return -EINVAL; 2270 2271 q_data_out->crop.left = 0; 2272 q_data_out->crop.top = 0; 2273 q_data_out->crop.width = s->r.width; 2274 q_data_out->crop.height = s->r.height; 2275 2276 return 0; 2277 } 2278 2279 static int mxc_jpeg_subscribe_event(struct v4l2_fh *fh, 2280 const struct v4l2_event_subscription *sub) 2281 { 2282 switch (sub->type) { 2283 case V4L2_EVENT_EOS: 2284 return v4l2_event_subscribe(fh, sub, 0, NULL); 2285 case V4L2_EVENT_SOURCE_CHANGE: 2286 return v4l2_src_change_event_subscribe(fh, sub); 2287 case V4L2_EVENT_CTRL: 2288 return v4l2_ctrl_subscribe_event(fh, sub); 2289 default: 2290 return -EINVAL; 2291 } 2292 } 2293 2294 static const struct v4l2_ioctl_ops mxc_jpeg_ioctl_ops = { 2295 .vidioc_querycap = mxc_jpeg_querycap, 2296 .vidioc_enum_fmt_vid_cap = mxc_jpeg_enum_fmt_vid_cap, 2297 .vidioc_enum_fmt_vid_out = mxc_jpeg_enum_fmt_vid_out, 2298 2299 .vidioc_try_fmt_vid_cap_mplane = mxc_jpeg_try_fmt_vid_cap, 2300 .vidioc_try_fmt_vid_out_mplane = mxc_jpeg_try_fmt_vid_out, 2301 2302 .vidioc_s_fmt_vid_cap_mplane = mxc_jpeg_s_fmt_vid_cap, 2303 .vidioc_s_fmt_vid_out_mplane = mxc_jpeg_s_fmt_vid_out, 2304 2305 .vidioc_g_fmt_vid_cap_mplane = mxc_jpeg_g_fmt_vid, 2306 .vidioc_g_fmt_vid_out_mplane = mxc_jpeg_g_fmt_vid, 2307 2308 .vidioc_g_selection = mxc_jpeg_g_selection, 2309 .vidioc_s_selection = mxc_jpeg_s_selection, 2310 2311 .vidioc_subscribe_event = mxc_jpeg_subscribe_event, 2312 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2313 2314 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, 2315 .vidioc_decoder_cmd = mxc_jpeg_decoder_cmd, 2316 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 2317 .vidioc_encoder_cmd = mxc_jpeg_encoder_cmd, 2318 2319 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 2320 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 2321 2322 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 2323 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 2324 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 2325 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 2326 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 2327 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 2328 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 2329 }; 2330 2331 static int mxc_jpeg_release(struct file *file) 2332 { 2333 struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file); 2334 struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(file->private_data); 2335 struct device *dev = mxc_jpeg->dev; 2336 2337 mutex_lock(&mxc_jpeg->lock); 2338 if (mxc_jpeg->mode == MXC_JPEG_DECODE) 2339 dev_dbg(dev, "Release JPEG decoder instance on slot %d.", 2340 ctx->slot); 2341 else 2342 dev_dbg(dev, "Release JPEG encoder instance on slot %d.", 2343 ctx->slot); 2344 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 2345 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 2346 v4l2_fh_del(&ctx->fh); 2347 v4l2_fh_exit(&ctx->fh); 2348 kfree(ctx); 2349 mutex_unlock(&mxc_jpeg->lock); 2350 2351 return 0; 2352 } 2353 2354 static const struct v4l2_file_operations mxc_jpeg_fops = { 2355 .owner = THIS_MODULE, 2356 .open = mxc_jpeg_open, 2357 .release = mxc_jpeg_release, 2358 .poll = v4l2_m2m_fop_poll, 2359 .unlocked_ioctl = video_ioctl2, 2360 .mmap = v4l2_m2m_fop_mmap, 2361 }; 2362 2363 static const struct v4l2_m2m_ops mxc_jpeg_m2m_ops = { 2364 .job_ready = mxc_jpeg_job_ready, 2365 .device_run = mxc_jpeg_device_run, 2366 }; 2367 2368 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg) 2369 { 2370 int i; 2371 2372 for (i = 0; i < jpeg->num_domains; i++) { 2373 if (jpeg->pd_link[i] && !IS_ERR(jpeg->pd_link[i])) 2374 device_link_del(jpeg->pd_link[i]); 2375 if (jpeg->pd_dev[i] && !IS_ERR(jpeg->pd_dev[i])) 2376 dev_pm_domain_detach(jpeg->pd_dev[i], true); 2377 jpeg->pd_dev[i] = NULL; 2378 jpeg->pd_link[i] = NULL; 2379 } 2380 } 2381 2382 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg) 2383 { 2384 struct device *dev = jpeg->dev; 2385 struct device_node *np = jpeg->pdev->dev.of_node; 2386 int i; 2387 int ret; 2388 2389 jpeg->num_domains = of_count_phandle_with_args(np, "power-domains", 2390 "#power-domain-cells"); 2391 if (jpeg->num_domains < 0) { 2392 dev_err(dev, "No power domains defined for jpeg node\n"); 2393 return jpeg->num_domains; 2394 } 2395 2396 jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains, 2397 sizeof(*jpeg->pd_dev), GFP_KERNEL); 2398 if (!jpeg->pd_dev) 2399 return -ENOMEM; 2400 2401 jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains, 2402 sizeof(*jpeg->pd_link), GFP_KERNEL); 2403 if (!jpeg->pd_link) 2404 return -ENOMEM; 2405 2406 for (i = 0; i < jpeg->num_domains; i++) { 2407 jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i); 2408 if (IS_ERR(jpeg->pd_dev[i])) { 2409 ret = PTR_ERR(jpeg->pd_dev[i]); 2410 goto fail; 2411 } 2412 2413 jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i], 2414 DL_FLAG_STATELESS | 2415 DL_FLAG_PM_RUNTIME); 2416 if (!jpeg->pd_link[i]) { 2417 ret = -EINVAL; 2418 goto fail; 2419 } 2420 } 2421 2422 return 0; 2423 fail: 2424 mxc_jpeg_detach_pm_domains(jpeg); 2425 return ret; 2426 } 2427 2428 static int mxc_jpeg_probe(struct platform_device *pdev) 2429 { 2430 struct mxc_jpeg_dev *jpeg; 2431 struct device *dev = &pdev->dev; 2432 int dec_irq; 2433 int ret; 2434 int mode; 2435 const struct of_device_id *of_id; 2436 unsigned int slot; 2437 2438 of_id = of_match_node(mxc_jpeg_match, dev->of_node); 2439 if (!of_id) 2440 return -ENODEV; 2441 mode = *(const int *)of_id->data; 2442 2443 jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL); 2444 if (!jpeg) 2445 return -ENOMEM; 2446 2447 mutex_init(&jpeg->lock); 2448 spin_lock_init(&jpeg->hw_lock); 2449 2450 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 2451 if (ret) { 2452 dev_err(&pdev->dev, "No suitable DMA available.\n"); 2453 goto err_irq; 2454 } 2455 2456 jpeg->base_reg = devm_platform_ioremap_resource(pdev, 0); 2457 if (IS_ERR(jpeg->base_reg)) 2458 return PTR_ERR(jpeg->base_reg); 2459 2460 for (slot = 0; slot < MXC_MAX_SLOTS; slot++) { 2461 dec_irq = platform_get_irq(pdev, slot); 2462 if (dec_irq < 0) { 2463 ret = dec_irq; 2464 goto err_irq; 2465 } 2466 ret = devm_request_irq(&pdev->dev, dec_irq, mxc_jpeg_dec_irq, 2467 0, pdev->name, jpeg); 2468 if (ret) { 2469 dev_err(&pdev->dev, "Failed to request irq %d (%d)\n", 2470 dec_irq, ret); 2471 goto err_irq; 2472 } 2473 } 2474 2475 jpeg->pdev = pdev; 2476 jpeg->dev = dev; 2477 jpeg->mode = mode; 2478 2479 /* Get clocks */ 2480 ret = devm_clk_bulk_get_all(&pdev->dev, &jpeg->clks); 2481 if (ret < 0) { 2482 dev_err(dev, "failed to get clock\n"); 2483 goto err_clk; 2484 } 2485 jpeg->num_clks = ret; 2486 2487 ret = mxc_jpeg_attach_pm_domains(jpeg); 2488 if (ret < 0) { 2489 dev_err(dev, "failed to attach power domains %d\n", ret); 2490 return ret; 2491 } 2492 2493 /* v4l2 */ 2494 ret = v4l2_device_register(dev, &jpeg->v4l2_dev); 2495 if (ret) { 2496 dev_err(dev, "failed to register v4l2 device\n"); 2497 goto err_register; 2498 } 2499 jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops); 2500 if (IS_ERR(jpeg->m2m_dev)) { 2501 dev_err(dev, "failed to register v4l2 device\n"); 2502 ret = PTR_ERR(jpeg->m2m_dev); 2503 goto err_m2m; 2504 } 2505 2506 jpeg->dec_vdev = video_device_alloc(); 2507 if (!jpeg->dec_vdev) { 2508 dev_err(dev, "failed to register v4l2 device\n"); 2509 ret = -ENOMEM; 2510 goto err_vdev_alloc; 2511 } 2512 if (mode == MXC_JPEG_ENCODE) 2513 snprintf(jpeg->dec_vdev->name, 2514 sizeof(jpeg->dec_vdev->name), 2515 "%s-enc", MXC_JPEG_NAME); 2516 else 2517 snprintf(jpeg->dec_vdev->name, 2518 sizeof(jpeg->dec_vdev->name), 2519 "%s-dec", MXC_JPEG_NAME); 2520 2521 jpeg->dec_vdev->fops = &mxc_jpeg_fops; 2522 jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops; 2523 jpeg->dec_vdev->minor = -1; 2524 jpeg->dec_vdev->release = video_device_release; 2525 jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */ 2526 jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev; 2527 jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M; 2528 jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING | 2529 V4L2_CAP_VIDEO_M2M_MPLANE; 2530 if (mode == MXC_JPEG_ENCODE) { 2531 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD); 2532 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD); 2533 } else { 2534 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD); 2535 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD); 2536 } 2537 ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1); 2538 if (ret) { 2539 dev_err(dev, "failed to register video device\n"); 2540 goto err_vdev_register; 2541 } 2542 video_set_drvdata(jpeg->dec_vdev, jpeg); 2543 if (mode == MXC_JPEG_ENCODE) 2544 v4l2_info(&jpeg->v4l2_dev, 2545 "encoder device registered as /dev/video%d (%d,%d)\n", 2546 jpeg->dec_vdev->num, VIDEO_MAJOR, 2547 jpeg->dec_vdev->minor); 2548 else 2549 v4l2_info(&jpeg->v4l2_dev, 2550 "decoder device registered as /dev/video%d (%d,%d)\n", 2551 jpeg->dec_vdev->num, VIDEO_MAJOR, 2552 jpeg->dec_vdev->minor); 2553 2554 platform_set_drvdata(pdev, jpeg); 2555 pm_runtime_enable(dev); 2556 2557 return 0; 2558 2559 err_vdev_register: 2560 video_device_release(jpeg->dec_vdev); 2561 2562 err_vdev_alloc: 2563 v4l2_m2m_release(jpeg->m2m_dev); 2564 2565 err_m2m: 2566 v4l2_device_unregister(&jpeg->v4l2_dev); 2567 2568 err_register: 2569 mxc_jpeg_detach_pm_domains(jpeg); 2570 2571 err_irq: 2572 err_clk: 2573 return ret; 2574 } 2575 2576 #ifdef CONFIG_PM 2577 static int mxc_jpeg_runtime_resume(struct device *dev) 2578 { 2579 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2580 int ret; 2581 2582 ret = clk_bulk_prepare_enable(jpeg->num_clks, jpeg->clks); 2583 if (ret < 0) { 2584 dev_err(dev, "failed to enable clock\n"); 2585 return ret; 2586 } 2587 2588 return 0; 2589 } 2590 2591 static int mxc_jpeg_runtime_suspend(struct device *dev) 2592 { 2593 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2594 2595 clk_bulk_disable_unprepare(jpeg->num_clks, jpeg->clks); 2596 2597 return 0; 2598 } 2599 #endif 2600 2601 #ifdef CONFIG_PM_SLEEP 2602 static int mxc_jpeg_suspend(struct device *dev) 2603 { 2604 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2605 2606 v4l2_m2m_suspend(jpeg->m2m_dev); 2607 return pm_runtime_force_suspend(dev); 2608 } 2609 2610 static int mxc_jpeg_resume(struct device *dev) 2611 { 2612 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); 2613 int ret; 2614 2615 ret = pm_runtime_force_resume(dev); 2616 if (ret < 0) 2617 return ret; 2618 2619 v4l2_m2m_resume(jpeg->m2m_dev); 2620 return ret; 2621 } 2622 #endif 2623 2624 static const struct dev_pm_ops mxc_jpeg_pm_ops = { 2625 SET_RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend, 2626 mxc_jpeg_runtime_resume, NULL) 2627 SET_SYSTEM_SLEEP_PM_OPS(mxc_jpeg_suspend, mxc_jpeg_resume) 2628 }; 2629 2630 static void mxc_jpeg_remove(struct platform_device *pdev) 2631 { 2632 unsigned int slot; 2633 struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev); 2634 2635 for (slot = 0; slot < MXC_MAX_SLOTS; slot++) 2636 mxc_jpeg_free_slot_data(jpeg, slot); 2637 2638 pm_runtime_disable(&pdev->dev); 2639 video_unregister_device(jpeg->dec_vdev); 2640 v4l2_m2m_release(jpeg->m2m_dev); 2641 v4l2_device_unregister(&jpeg->v4l2_dev); 2642 mxc_jpeg_detach_pm_domains(jpeg); 2643 } 2644 2645 MODULE_DEVICE_TABLE(of, mxc_jpeg_match); 2646 2647 static struct platform_driver mxc_jpeg_driver = { 2648 .probe = mxc_jpeg_probe, 2649 .remove_new = mxc_jpeg_remove, 2650 .driver = { 2651 .name = "mxc-jpeg", 2652 .of_match_table = mxc_jpeg_match, 2653 .pm = &mxc_jpeg_pm_ops, 2654 }, 2655 }; 2656 module_platform_driver(mxc_jpeg_driver); 2657 2658 MODULE_AUTHOR("Zhengyu Shen <zhengyu.shen_1@nxp.com>"); 2659 MODULE_AUTHOR("Mirela Rabulea <mirela.rabulea@nxp.com>"); 2660 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder"); 2661 MODULE_LICENSE("GPL v2"); 2662