blob: 4492a3535df5bdbf8b1dfa96591e56b63476c0f6 [file] [log] [blame]
Mauro Carvalho Chehab2c3fb082012-08-14 17:31:16 -03001/* linux/drivers/media/platform/s5p-jpeg/jpeg-core.h
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -03002 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
6 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef JPEG_CORE_H_
14#define JPEG_CORE_H_
15
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030016#include <linux/interrupt.h>
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030017#include <media/v4l2-device.h>
Sylwester Nawrocki275de242012-02-17 11:38:09 -030018#include <media/v4l2-fh.h>
Sylwester Nawrocki15f4bc32012-02-17 11:39:36 -030019#include <media/v4l2-ctrls.h>
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030020
21#define S5P_JPEG_M2M_NAME "s5p-jpeg"
22
Marek Szyprowskib95a24d2015-09-18 11:20:57 -030023#define JPEG_MAX_CLOCKS 4
24
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030025/* JPEG compression quality setting */
26#define S5P_JPEG_COMPR_QUAL_BEST 0
27#define S5P_JPEG_COMPR_QUAL_WORST 3
28
29/* JPEG RGB to YCbCr conversion matrix coefficients */
30#define S5P_JPEG_COEF11 0x4d
31#define S5P_JPEG_COEF12 0x97
32#define S5P_JPEG_COEF13 0x1e
33#define S5P_JPEG_COEF21 0x2c
34#define S5P_JPEG_COEF22 0x57
35#define S5P_JPEG_COEF23 0x83
36#define S5P_JPEG_COEF31 0x83
37#define S5P_JPEG_COEF32 0x6e
38#define S5P_JPEG_COEF33 0x13
39
Jacek Anaszewski3246fda2014-07-11 12:19:42 -030040#define EXYNOS3250_IRQ_TIMEOUT 0x10000000
41
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030042/* a selection of JPEG markers */
43#define TEM 0x01
44#define SOF0 0xc0
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -030045#define DHT 0xc4
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030046#define RST 0xd0
47#define SOI 0xd8
48#define EOI 0xd9
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -030049#define SOS 0xda
50#define DQT 0xdb
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030051#define DHP 0xde
52
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030053/* Flags that indicate a format can be used for capture/output */
54#define SJPEG_FMT_FLAG_ENC_CAPTURE (1 << 0)
55#define SJPEG_FMT_FLAG_ENC_OUTPUT (1 << 1)
56#define SJPEG_FMT_FLAG_DEC_CAPTURE (1 << 2)
57#define SJPEG_FMT_FLAG_DEC_OUTPUT (1 << 3)
58#define SJPEG_FMT_FLAG_S5P (1 << 4)
Jacek Anaszewski3246fda2014-07-11 12:19:42 -030059#define SJPEG_FMT_FLAG_EXYNOS3250 (1 << 5)
60#define SJPEG_FMT_FLAG_EXYNOS4 (1 << 6)
61#define SJPEG_FMT_RGB (1 << 7)
62#define SJPEG_FMT_NON_RGB (1 << 8)
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030063
Jacek Anaszewski9f7b62d2013-12-18 09:32:50 -030064#define S5P_JPEG_ENCODE 0
65#define S5P_JPEG_DECODE 1
66
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030067#define FMT_TYPE_OUTPUT 0
68#define FMT_TYPE_CAPTURE 1
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -030069
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030070#define SJPEG_SUBSAMPLING_444 0x11
71#define SJPEG_SUBSAMPLING_422 0x21
72#define SJPEG_SUBSAMPLING_420 0x22
Jacek Anaszewski9f7b62d2013-12-18 09:32:50 -030073
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -030074#define S5P_JPEG_MAX_MARKER 4
75
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030076/* Version numbers */
Andrzej Pietrasiewicz7c15fd42015-03-09 09:32:46 -030077enum sjpeg_version {
78 SJPEG_S5P,
79 SJPEG_EXYNOS3250,
80 SJPEG_EXYNOS4,
81 SJPEG_EXYNOS5420,
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -030082 SJPEG_EXYNOS5433,
Andrzej Pietrasiewicz7c15fd42015-03-09 09:32:46 -030083};
Jacek Anaszewski80529ae2013-12-18 11:04:44 -030084
85enum exynos4_jpeg_result {
86 OK_ENC_OR_DEC,
87 ERR_PROT,
88 ERR_DEC_INVALID_FORMAT,
89 ERR_MULTI_SCAN,
90 ERR_FRAME,
91 ERR_UNKNOWN,
92};
93
94enum exynos4_jpeg_img_quality_level {
95 QUALITY_LEVEL_1 = 0, /* high */
96 QUALITY_LEVEL_2,
97 QUALITY_LEVEL_3,
98 QUALITY_LEVEL_4, /* low */
99};
Jacek Anaszewski9f7b62d2013-12-18 09:32:50 -0300100
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300101/**
102 * struct s5p_jpeg - JPEG IP abstraction
103 * @lock: the mutex protecting this structure
Sylwester Nawrocki15f4bc32012-02-17 11:39:36 -0300104 * @slock: spinlock protecting the device contexts
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300105 * @v4l2_dev: v4l2 device for mem2mem mode
106 * @vfd_encoder: video device node for encoder mem2mem mode
107 * @vfd_decoder: video device node for decoder mem2mem mode
108 * @m2m_dev: v4l2 mem2mem device data
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300109 * @regs: JPEG IP registers mapping
110 * @irq: JPEG IP irq
Marek Szyprowskib95a24d2015-09-18 11:20:57 -0300111 * @clocks: JPEG IP clock(s)
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300112 * @dev: JPEG IP struct device
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300113 * @variant: driver variant to be used
114 * @irq_status interrupt flags set during single encode/decode
115 operation
116
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300117 */
118struct s5p_jpeg {
119 struct mutex lock;
Luis R. Rodrigueza75831f2012-11-29 16:45:08 -0300120 spinlock_t slock;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300121
122 struct v4l2_device v4l2_dev;
123 struct video_device *vfd_encoder;
124 struct video_device *vfd_decoder;
125 struct v4l2_m2m_dev *m2m_dev;
126
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300127 void __iomem *regs;
128 unsigned int irq;
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300129 enum exynos4_jpeg_result irq_ret;
Marek Szyprowskib95a24d2015-09-18 11:20:57 -0300130 struct clk *clocks[JPEG_MAX_CLOCKS];
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300131 struct device *dev;
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300132 struct s5p_jpeg_variant *variant;
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300133 u32 irq_status;
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300134};
135
136struct s5p_jpeg_variant {
Jacek Anaszewskibf689bb2014-04-10 04:32:13 -0300137 unsigned int version;
138 unsigned int fmt_ver_flag;
Andrzej Pietrasiewicz7c15fd42015-03-09 09:32:46 -0300139 unsigned int hw3250_compat:1;
140 unsigned int htbl_reinit:1;
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -0300141 unsigned int hw_ex4_compat:1;
Jacek Anaszewskibf689bb2014-04-10 04:32:13 -0300142 struct v4l2_m2m_ops *m2m_ops;
143 irqreturn_t (*jpeg_irq)(int irq, void *priv);
Marek Szyprowskib95a24d2015-09-18 11:20:57 -0300144 const char *clk_names[JPEG_MAX_CLOCKS];
145 int num_clocks;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300146};
147
148/**
149 * struct jpeg_fmt - driver's internal color format data
150 * @name: format descritpion
151 * @fourcc: the fourcc code, 0 if not applicable
152 * @depth: number of bits per pixel
153 * @colplanes: number of color planes (1 for packed formats)
154 * @h_align: horizontal alignment order (align to 2^h_align)
155 * @v_align: vertical alignment order (align to 2^v_align)
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300156 * @flags: flags describing format applicability
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300157 */
158struct s5p_jpeg_fmt {
159 char *name;
160 u32 fourcc;
161 int depth;
162 int colplanes;
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300163 int memplanes;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300164 int h_align;
165 int v_align;
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300166 int subsampling;
167 u32 flags;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300168};
169
170/**
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -0300171 * s5p_jpeg_marker - collection of markers from jpeg header
172 * @marker: markers' positions relative to the buffer beginning
173 * @len: markers' payload lengths (without length field)
174 * @n: number of markers in collection
175 */
176struct s5p_jpeg_marker {
177 u32 marker[S5P_JPEG_MAX_MARKER];
178 u32 len[S5P_JPEG_MAX_MARKER];
179 u32 n;
180};
181
182/**
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300183 * s5p_jpeg_q_data - parameters of one queue
184 * @fmt: driver-specific format of this queue
185 * @w: image width
186 * @h: image height
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -0300187 * @sos: SOS marker's position relative to the buffer beginning
188 * @dht: DHT markers' positions relative to the buffer beginning
189 * @dqt: DQT markers' positions relative to the buffer beginning
190 * @sof: SOF0 marker's postition relative to the buffer beginning
191 * @sof_len: SOF0 marker's payload length (without length field itself)
192 * @components: number of image components
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300193 * @size: image buffer size in bytes
194 */
195struct s5p_jpeg_q_data {
196 struct s5p_jpeg_fmt *fmt;
197 u32 w;
198 u32 h;
Andrzej Pietrasiewicz6c96dbb2015-09-18 11:20:58 -0300199 u32 sos;
200 struct s5p_jpeg_marker dht;
201 struct s5p_jpeg_marker dqt;
202 u32 sof;
203 u32 sof_len;
204 u32 components;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300205 u32 size;
206};
207
208/**
209 * s5p_jpeg_ctx - the device context data
210 * @jpeg: JPEG IP device for this context
211 * @mode: compression (encode) operation or decompression (decode)
212 * @compr_quality: destination image quality in compression (encode) mode
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300213 * @restart_interval: JPEG restart interval for JPEG encoding
214 * @subsampling: subsampling of a raw format or a JPEG
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300215 * @out_q: source (output) queue information
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300216 * @cap_q: destination (capture) queue queue information
217 * @scale_factor: scale factor for JPEG decoding
218 * @crop_rect: a rectangle representing crop area of the output buffer
219 * @fh: V4L2 file handle
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300220 * @hdr_parsed: set if header has been parsed during decompression
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300221 * @crop_altered: set if crop rectangle has been altered by the user space
Sylwester Nawrocki15f4bc32012-02-17 11:39:36 -0300222 * @ctrl_handler: controls handler
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300223 */
224struct s5p_jpeg_ctx {
225 struct s5p_jpeg *jpeg;
226 unsigned int mode;
Sylwester Nawrocki15f4bc32012-02-17 11:39:36 -0300227 unsigned short compr_quality;
228 unsigned short restart_interval;
229 unsigned short subsampling;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300230 struct s5p_jpeg_q_data out_q;
231 struct s5p_jpeg_q_data cap_q;
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300232 unsigned int scale_factor;
233 struct v4l2_rect crop_rect;
Sylwester Nawrocki275de242012-02-17 11:38:09 -0300234 struct v4l2_fh fh;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300235 bool hdr_parsed;
Jacek Anaszewski3246fda2014-07-11 12:19:42 -0300236 bool crop_altered;
Sylwester Nawrocki15f4bc32012-02-17 11:39:36 -0300237 struct v4l2_ctrl_handler ctrl_handler;
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300238};
239
240/**
241 * s5p_jpeg_buffer - description of memory containing input JPEG data
242 * @size: buffer size
243 * @curr: current position in the buffer
244 * @data: pointer to the data
245 */
246struct s5p_jpeg_buffer {
247 unsigned long size;
248 unsigned long curr;
249 unsigned long data;
250};
251
Jacek Anaszewski80529ae2013-12-18 11:04:44 -0300252/**
253 * struct s5p_jpeg_addr - JPEG converter physical address set for DMA
254 * @y: luminance plane physical address
255 * @cb: Cb plane physical address
256 * @cr: Cr plane physical address
257 */
258struct s5p_jpeg_addr {
259 u32 y;
260 u32 cb;
261 u32 cr;
262};
263
Andrzej Pietrasiewiczbb677f32011-11-24 11:15:23 -0300264#endif /* JPEG_CORE_H */