cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

mxc-jpeg.c (68477B)


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