xref: /linux/drivers/media/platform/imagination/e5010-jpeg-enc.h (revision 68a052239fc4b351e961f698b824f7654a346091)
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 static inline struct e5010_context *to_e5010_context(struct file *filp)
124 {
125 	return container_of(file_to_v4l2_fh(filp), struct e5010_context, fh);
126 }
127 
128 /*
129  * Buffer structure
130  * Contains info for all buffers
131  */
132 struct e5010_buffer {
133 	struct v4l2_m2m_buffer buffer;
134 };
135 
136 enum {
137 	CHROMA_ORDER_CB_CR = 0, //UV ordering
138 	CHROMA_ORDER_CR_CB = 1, //VU ordering
139 };
140 
141 enum {
142 	SUBSAMPLING_420 = 1,
143 	SUBSAMPLING_422 = 2,
144 };
145 
146 /*
147  * e5010 format structure
148  * contains format information
149  */
150 struct e5010_fmt {
151 	u32					fourcc;
152 	unsigned int				num_planes;
153 	unsigned int				type;
154 	u32					subsampling;
155 	u32					chroma_order;
156 	const struct v4l2_frmsize_stepwise	frmsize;
157 };
158 
159 /*
160  * struct e5010_ctrl - contains info for each supported v4l2 control
161  */
162 struct e5010_ctrl {
163 	unsigned int		cid;
164 	enum v4l2_ctrl_type	type;
165 	unsigned char		name[32];
166 	int			minimum;
167 	int			maximum;
168 	int			step;
169 	int			default_value;
170 	unsigned char		compound;
171 };
172 
173 #endif
174