1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * i.MX8QXP/i.MX8QM JPEG encoder/decoder v4l2 driver
4 *
5 * Copyright 2018-2019 NXP
6 */
7
8#include <media/v4l2-ctrls.h>
9#include <media/v4l2-device.h>
10#include <media/v4l2-fh.h>
11
12#ifndef _MXC_JPEG_CORE_H
13#define _MXC_JPEG_CORE_H
14
15#define MXC_JPEG_NAME			"mxc-jpeg"
16#define MXC_JPEG_FMT_TYPE_ENC		0
17#define MXC_JPEG_FMT_TYPE_RAW		1
18#define MXC_JPEG_DEFAULT_WIDTH		1280
19#define MXC_JPEG_DEFAULT_HEIGHT		720
20#define MXC_JPEG_DEFAULT_PFMT		V4L2_PIX_FMT_BGR24
21#define MXC_JPEG_MIN_WIDTH		64
22#define MXC_JPEG_MIN_HEIGHT		64
23#define MXC_JPEG_MAX_WIDTH		0x2000
24#define MXC_JPEG_MAX_HEIGHT		0x2000
25#define MXC_JPEG_MAX_CFG_STREAM		0x1000
26#define MXC_JPEG_H_ALIGN		3
27#define MXC_JPEG_W_ALIGN		3
28#define MXC_JPEG_MAX_SIZEIMAGE		0xFFFFFC00
29#define MXC_JPEG_MAX_PLANES		2
30
31enum mxc_jpeg_enc_state {
32	MXC_JPEG_ENCODING	= 0, /* jpeg encode phase */
33	MXC_JPEG_ENC_CONF	= 1, /* jpeg encoder config phase */
34};
35
36enum mxc_jpeg_mode {
37	MXC_JPEG_DECODE	= 0, /* jpeg decode mode */
38	MXC_JPEG_ENCODE	= 1, /* jpeg encode mode */
39};
40
41/**
42 * struct mxc_jpeg_fmt - driver's internal color format data
43 * @name:	format description
44 * @fourcc:	fourcc code, 0 if not applicable
45 * @subsampling: subsampling of jpeg components
46 * @nc:		number of color components
47 * @depth:	number of bits per pixel
48 * @mem_planes:	number of memory planes (1 for packed formats)
49 * @comp_planes:number of component planes, which includes the alpha plane (1 to 4).
50 * @h_align:	horizontal alignment order (align to 2^h_align)
51 * @v_align:	vertical alignment order (align to 2^v_align)
52 * @flags:	flags describing format applicability
53 * @precision:  jpeg sample precision
54 * @is_rgb:     is an RGB pixel format
55 */
56struct mxc_jpeg_fmt {
57	const char				*name;
58	u32					fourcc;
59	enum v4l2_jpeg_chroma_subsampling	subsampling;
60	int					nc;
61	int					depth;
62	int					mem_planes;
63	int					comp_planes;
64	int					h_align;
65	int					v_align;
66	u32					flags;
67	u8					precision;
68	u8					is_rgb;
69};
70
71struct mxc_jpeg_desc {
72	u32 next_descpt_ptr;
73	u32 buf_base0;
74	u32 buf_base1;
75	u32 line_pitch;
76	u32 stm_bufbase;
77	u32 stm_bufsize;
78	u32 imgsize;
79	u32 stm_ctrl;
80} __packed;
81
82struct mxc_jpeg_q_data {
83	const struct mxc_jpeg_fmt	*fmt;
84	u32				sizeimage[MXC_JPEG_MAX_PLANES];
85	u32				bytesperline[MXC_JPEG_MAX_PLANES];
86	int				w;
87	int				w_adjusted;
88	int				h;
89	int				h_adjusted;
90	unsigned int			sequence;
91	struct v4l2_rect		crop;
92};
93
94struct mxc_jpeg_ctx {
95	struct mxc_jpeg_dev		*mxc_jpeg;
96	struct mxc_jpeg_q_data		out_q;
97	struct mxc_jpeg_q_data		cap_q;
98	struct v4l2_fh			fh;
99	enum mxc_jpeg_enc_state		enc_state;
100	int				slot;
101	unsigned int			source_change;
102	bool				need_initial_source_change_evt;
103	bool				header_parsed;
104	struct v4l2_ctrl_handler	ctrl_handler;
105	u8				jpeg_quality;
106	struct delayed_work		task_timer;
107};
108
109struct mxc_jpeg_slot_data {
110	int slot;
111	bool used;
112	struct mxc_jpeg_desc *desc; // enc/dec descriptor
113	struct mxc_jpeg_desc *cfg_desc; // configuration descriptor
114	void *cfg_stream_vaddr; // configuration bitstream virtual address
115	unsigned int cfg_stream_size;
116	dma_addr_t desc_handle;
117	dma_addr_t cfg_desc_handle; // configuration descriptor dma address
118	dma_addr_t cfg_stream_handle; // configuration bitstream dma address
119};
120
121struct mxc_jpeg_dev {
122	spinlock_t			hw_lock; /* hardware access lock */
123	unsigned int			mode;
124	struct mutex			lock; /* v4l2 ioctls serialization */
125	struct clk_bulk_data		*clks;
126	int				num_clks;
127	struct platform_device		*pdev;
128	struct device			*dev;
129	void __iomem			*base_reg;
130	struct v4l2_device		v4l2_dev;
131	struct v4l2_m2m_dev		*m2m_dev;
132	struct video_device		*dec_vdev;
133	struct mxc_jpeg_slot_data	slot_data;
134	int				num_domains;
135	struct device			**pd_dev;
136	struct device_link		**pd_link;
137};
138
139/**
140 * struct mxc_jpeg_sof_comp - JPEG Start Of Frame component fields
141 * @id:				component id
142 * @v:				vertical sampling
143 * @h:				horizontal sampling
144 * @quantization_table_no:	id of quantization table
145 */
146struct mxc_jpeg_sof_comp {
147	u8 id;
148	u8 v :4;
149	u8 h :4;
150	u8 quantization_table_no;
151} __packed;
152
153#define MXC_JPEG_MAX_COMPONENTS 4
154/**
155 * struct mxc_jpeg_sof - JPEG Start Of Frame marker fields
156 * @length:		Start of Frame length
157 * @precision:		precision (bits per pixel per color component)
158 * @height:		image height
159 * @width:		image width
160 * @components_no:	number of color components
161 * @comp:		component fields for each color component
162 */
163struct mxc_jpeg_sof {
164	u16 length;
165	u8 precision;
166	u16 height, width;
167	u8 components_no;
168	struct mxc_jpeg_sof_comp comp[MXC_JPEG_MAX_COMPONENTS];
169} __packed;
170
171/**
172 * struct mxc_jpeg_sos_comp - JPEG Start Of Scan component fields
173 * @id:			component id
174 * @huffman_table_no:	id of the Huffman table
175 */
176struct mxc_jpeg_sos_comp {
177	u8 id; /*component id*/
178	u8 huffman_table_no;
179} __packed;
180
181/**
182 * struct mxc_jpeg_sos - JPEG Start Of Scan marker fields
183 * @length:		Start of Frame length
184 * @components_no:	number of color components
185 * @comp:		SOS component fields for each color component
186 * @ignorable_bytes:	ignorable bytes
187 */
188struct mxc_jpeg_sos {
189	u16 length;
190	u8 components_no;
191	struct mxc_jpeg_sos_comp comp[MXC_JPEG_MAX_COMPONENTS];
192	u8 ignorable_bytes[3];
193} __packed;
194
195#endif
196