xref: /linux/drivers/media/platform/imagination/e5010-jpeg-enc.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Imagination E5010 JPEG Encoder driver.
4  *
5  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
6  *
7  * Author: David Huang <d-huang@ti.com>
8  * Author: Devarsh Thakkar <devarsht@ti.com>
9  */
10 
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-fh.h>
14 
15 #ifndef _E5010_JPEG_ENC_H
16 #define _E5010_JPEG_ENC_H
17 
18 #define MAX_PLANES			2
19 #define HEADER_SIZE			0x025D
20 #define MIN_DIMENSION			64
21 #define MAX_DIMENSION			8192
22 #define DEFAULT_WIDTH			640
23 #define DEFAULT_HEIGHT			480
24 #define E5010_MODULE_NAME		"e5010"
25 #define JPEG_MAX_BYTES_PER_PIXEL	2
26 
27 /* JPEG marker definitions */
28 #define START_OF_IMAGE			0xFFD8
29 #define SOF_BASELINE_DCT		0xFFC0
30 #define END_OF_IMAGE			0xFFD9
31 #define START_OF_SCAN			0xFFDA
32 
33 /* Definitions for the huffman table specification in the Marker segment */
34 #define DHT_MARKER			0xFFC4
35 #define LH_DC				0x001F
36 #define LH_AC				0x00B5
37 
38 /* Definitions for the quantization table specification in the Marker segment */
39 #define DQT_MARKER			0xFFDB
40 #define ACMAX				0x03FF
41 #define DCMAX				0x07FF
42 
43 /* Length and precision of the quantization table parameters */
44 #define LQPQ				0x00430
45 #define QMAX				255
46 
47 /* Misc JPEG header definitions */
48 #define UC_NUM_COMP			3
49 #define PRECISION			8
50 #define HORZ_SAMPLING_FACTOR		(2 << 4)
51 #define VERT_SAMPLING_FACTOR_422	1
52 #define VERT_SAMPLING_FACTOR_420	2
53 #define COMPONENTS_IN_SCAN		3
54 #define PELS_IN_BLOCK			64
55 
56 /* Used for Qp table generation */
57 #define LUMINOSITY			10
58 #define CONTRAST			1
59 #define INCREASE			2
60 #define QP_TABLE_SIZE			(8 * 8)
61 #define QP_TABLE_FIELD_OFFSET		0x04
62 
63 /*
64  * vb2 queue structure
65  * contains queue data information
66  *
67  * @fmt: format info
68  * @width: frame width
69  * @height: frame height
70  * @bytesperline: bytes per line in memory
71  * @size_image: image size in memory
72  */
73 struct e5010_q_data {
74 	struct e5010_fmt	*fmt;
75 	u32			width;
76 	u32			height;
77 	u32			width_adjusted;
78 	u32			height_adjusted;
79 	u32			sizeimage[MAX_PLANES];
80 	u32			bytesperline[MAX_PLANES];
81 	u32			sequence;
82 	struct v4l2_rect	crop;
83 	bool			crop_set;
84 };
85 
86 /*
87  * Driver device structure
88  * Holds all memory handles and global parameters
89  * Shared by all instances
90  */
91 struct e5010_dev {
92 	struct device *dev;
93 	struct v4l2_device	v4l2_dev;
94 	struct v4l2_m2m_dev	*m2m_dev;
95 	struct video_device	*vdev;
96 	void __iomem		*core_base;
97 	void __iomem		*mmu_base;
98 	struct clk		*clk;
99 	struct e5010_context	*last_context_run;
100 	/* Protect access to device data */
101 	struct mutex		mutex;
102 	/* Protect access to hardware*/
103 	spinlock_t		hw_lock;
104 };
105 
106 /*
107  * Driver context structure
108  * One of these exists for every m2m context
109  * Holds context specific data
110  */
111 struct e5010_context {
112 	struct v4l2_fh			fh;
113 	struct e5010_dev		*e5010;
114 	struct e5010_q_data		out_queue;
115 	struct e5010_q_data		cap_queue;
116 	int				quality;
117 	bool				update_qp;
118 	struct v4l2_ctrl_handler	ctrl_handler;
119 	u8				luma_qp[QP_TABLE_SIZE];
120 	u8				chroma_qp[QP_TABLE_SIZE];
121 };
122 
123 /*
124  * Buffer structure
125  * Contains info for all buffers
126  */
127 struct e5010_buffer {
128 	struct v4l2_m2m_buffer buffer;
129 };
130 
131 enum {
132 	CHROMA_ORDER_CB_CR = 0, //UV ordering
133 	CHROMA_ORDER_CR_CB = 1, //VU ordering
134 };
135 
136 enum {
137 	SUBSAMPLING_420 = 1,
138 	SUBSAMPLING_422 = 2,
139 };
140 
141 /*
142  * e5010 format structure
143  * contains format information
144  */
145 struct e5010_fmt {
146 	u32					fourcc;
147 	unsigned int				num_planes;
148 	unsigned int				type;
149 	u32					subsampling;
150 	u32					chroma_order;
151 	const struct v4l2_frmsize_stepwise	frmsize;
152 };
153 
154 /*
155  * struct e5010_ctrl - contains info for each supported v4l2 control
156  */
157 struct e5010_ctrl {
158 	unsigned int		cid;
159 	enum v4l2_ctrl_type	type;
160 	unsigned char		name[32];
161 	int			minimum;
162 	int			maximum;
163 	int			step;
164 	int			default_value;
165 	unsigned char		compound;
166 };
167 
168 #endif
169