xref: /openbmc/linux/drivers/media/platform/st/sti/delta/delta-mjpeg-dec.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*e7b8153eSMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0
2*e7b8153eSMauro Carvalho Chehab /*
3*e7b8153eSMauro Carvalho Chehab  * Copyright (C) STMicroelectronics SA 2013
4*e7b8153eSMauro Carvalho Chehab  * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
5*e7b8153eSMauro Carvalho Chehab  */
6*e7b8153eSMauro Carvalho Chehab 
7*e7b8153eSMauro Carvalho Chehab #include <linux/slab.h>
8*e7b8153eSMauro Carvalho Chehab 
9*e7b8153eSMauro Carvalho Chehab #include "delta.h"
10*e7b8153eSMauro Carvalho Chehab #include "delta-ipc.h"
11*e7b8153eSMauro Carvalho Chehab #include "delta-mjpeg.h"
12*e7b8153eSMauro Carvalho Chehab #include "delta-mjpeg-fw.h"
13*e7b8153eSMauro Carvalho Chehab 
14*e7b8153eSMauro Carvalho Chehab #define DELTA_MJPEG_MAX_RESO DELTA_MAX_RESO
15*e7b8153eSMauro Carvalho Chehab 
16*e7b8153eSMauro Carvalho Chehab struct delta_mjpeg_ctx {
17*e7b8153eSMauro Carvalho Chehab 	/* jpeg header */
18*e7b8153eSMauro Carvalho Chehab 	struct mjpeg_header header_struct;
19*e7b8153eSMauro Carvalho Chehab 	struct mjpeg_header *header;
20*e7b8153eSMauro Carvalho Chehab 
21*e7b8153eSMauro Carvalho Chehab 	/* ipc */
22*e7b8153eSMauro Carvalho Chehab 	void *ipc_hdl;
23*e7b8153eSMauro Carvalho Chehab 	struct delta_buf *ipc_buf;
24*e7b8153eSMauro Carvalho Chehab 
25*e7b8153eSMauro Carvalho Chehab 	/* decoded output frame */
26*e7b8153eSMauro Carvalho Chehab 	struct delta_frame *out_frame;
27*e7b8153eSMauro Carvalho Chehab 
28*e7b8153eSMauro Carvalho Chehab 	unsigned char str[3000];
29*e7b8153eSMauro Carvalho Chehab };
30*e7b8153eSMauro Carvalho Chehab 
31*e7b8153eSMauro Carvalho Chehab #define to_ctx(ctx) ((struct delta_mjpeg_ctx *)(ctx)->priv)
32*e7b8153eSMauro Carvalho Chehab 
ipc_open_param_str(struct jpeg_video_decode_init_params_t * p,char * str,unsigned int len)33*e7b8153eSMauro Carvalho Chehab static char *ipc_open_param_str(struct jpeg_video_decode_init_params_t *p,
34*e7b8153eSMauro Carvalho Chehab 				char *str, unsigned int len)
35*e7b8153eSMauro Carvalho Chehab {
36*e7b8153eSMauro Carvalho Chehab 	char *b = str;
37*e7b8153eSMauro Carvalho Chehab 
38*e7b8153eSMauro Carvalho Chehab 	if (!p)
39*e7b8153eSMauro Carvalho Chehab 		return "";
40*e7b8153eSMauro Carvalho Chehab 
41*e7b8153eSMauro Carvalho Chehab 	b += snprintf(b, len,
42*e7b8153eSMauro Carvalho Chehab 		      "jpeg_video_decode_init_params_t\n"
43*e7b8153eSMauro Carvalho Chehab 		      "circular_buffer_begin_addr_p 0x%x\n"
44*e7b8153eSMauro Carvalho Chehab 		      "circular_buffer_end_addr_p   0x%x\n",
45*e7b8153eSMauro Carvalho Chehab 		      p->circular_buffer_begin_addr_p,
46*e7b8153eSMauro Carvalho Chehab 		      p->circular_buffer_end_addr_p);
47*e7b8153eSMauro Carvalho Chehab 
48*e7b8153eSMauro Carvalho Chehab 	return str;
49*e7b8153eSMauro Carvalho Chehab }
50*e7b8153eSMauro Carvalho Chehab 
ipc_decode_param_str(struct jpeg_decode_params_t * p,char * str,unsigned int len)51*e7b8153eSMauro Carvalho Chehab static char *ipc_decode_param_str(struct jpeg_decode_params_t *p,
52*e7b8153eSMauro Carvalho Chehab 				  char *str, unsigned int len)
53*e7b8153eSMauro Carvalho Chehab {
54*e7b8153eSMauro Carvalho Chehab 	char *b = str;
55*e7b8153eSMauro Carvalho Chehab 
56*e7b8153eSMauro Carvalho Chehab 	if (!p)
57*e7b8153eSMauro Carvalho Chehab 		return "";
58*e7b8153eSMauro Carvalho Chehab 
59*e7b8153eSMauro Carvalho Chehab 	b += snprintf(b, len,
60*e7b8153eSMauro Carvalho Chehab 		      "jpeg_decode_params_t\n"
61*e7b8153eSMauro Carvalho Chehab 		      "picture_start_addr_p                  0x%x\n"
62*e7b8153eSMauro Carvalho Chehab 		      "picture_end_addr_p                    0x%x\n"
63*e7b8153eSMauro Carvalho Chehab 		      "decoding_mode                        %d\n"
64*e7b8153eSMauro Carvalho Chehab 		      "display_buffer_addr.display_decimated_luma_p   0x%x\n"
65*e7b8153eSMauro Carvalho Chehab 		      "display_buffer_addr.display_decimated_chroma_p 0x%x\n"
66*e7b8153eSMauro Carvalho Chehab 		      "main_aux_enable                       %d\n"
67*e7b8153eSMauro Carvalho Chehab 		      "additional_flags                     0x%x\n"
68*e7b8153eSMauro Carvalho Chehab 		      "field_flag                           %x\n"
69*e7b8153eSMauro Carvalho Chehab 		      "is_jpeg_image                        %x\n",
70*e7b8153eSMauro Carvalho Chehab 		      p->picture_start_addr_p,
71*e7b8153eSMauro Carvalho Chehab 		      p->picture_end_addr_p,
72*e7b8153eSMauro Carvalho Chehab 		      p->decoding_mode,
73*e7b8153eSMauro Carvalho Chehab 		      p->display_buffer_addr.display_decimated_luma_p,
74*e7b8153eSMauro Carvalho Chehab 		      p->display_buffer_addr.display_decimated_chroma_p,
75*e7b8153eSMauro Carvalho Chehab 		      p->main_aux_enable, p->additional_flags,
76*e7b8153eSMauro Carvalho Chehab 		      p->field_flag,
77*e7b8153eSMauro Carvalho Chehab 		      p->is_jpeg_image);
78*e7b8153eSMauro Carvalho Chehab 
79*e7b8153eSMauro Carvalho Chehab 	return str;
80*e7b8153eSMauro Carvalho Chehab }
81*e7b8153eSMauro Carvalho Chehab 
is_stream_error(enum jpeg_decoding_error_t err)82*e7b8153eSMauro Carvalho Chehab static inline bool is_stream_error(enum jpeg_decoding_error_t err)
83*e7b8153eSMauro Carvalho Chehab {
84*e7b8153eSMauro Carvalho Chehab 	switch (err) {
85*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNDEFINED_HUFF_TABLE:
86*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_RESTART_MARKER:
87*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_SOS_SPECTRAL:
88*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_SOS_SUCCESSIVE:
89*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_HEADER_LENGTH:
90*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_COUNT_VALUE:
91*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_DHT_MARKER:
92*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_INDEX_VALUE:
93*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_NUMBER_HUFFMAN_TABLES:
94*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_QUANT_TABLE_LENGTH:
95*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_NUMBER_QUANT_TABLES:
96*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_COMPONENT_COUNT:
97*e7b8153eSMauro Carvalho Chehab 		return true;
98*e7b8153eSMauro Carvalho Chehab 	default:
99*e7b8153eSMauro Carvalho Chehab 		return false;
100*e7b8153eSMauro Carvalho Chehab 	}
101*e7b8153eSMauro Carvalho Chehab }
102*e7b8153eSMauro Carvalho Chehab 
err_str(enum jpeg_decoding_error_t err)103*e7b8153eSMauro Carvalho Chehab static inline const char *err_str(enum jpeg_decoding_error_t err)
104*e7b8153eSMauro Carvalho Chehab {
105*e7b8153eSMauro Carvalho Chehab 	switch (err) {
106*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_NO_ERROR:
107*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_NO_ERROR";
108*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNDEFINED_HUFF_TABLE:
109*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_UNDEFINED_HUFF_TABLE";
110*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNSUPPORTED_MARKER:
111*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_UNSUPPORTED_MARKER";
112*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNABLE_ALLOCATE_MEMORY:
113*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_UNABLE_ALLOCATE_MEMORY";
114*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_NON_SUPPORTED_SAMP_FACTORS:
115*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_NON_SUPPORTED_SAMP_FACTORS";
116*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_PARAMETER:
117*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_PARAMETER";
118*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_DECODE_ERROR:
119*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_DECODE_ERROR";
120*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_RESTART_MARKER:
121*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_RESTART_MARKER";
122*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNSUPPORTED_COLORSPACE:
123*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_UNSUPPORTED_COLORSPACE";
124*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_SOS_SPECTRAL:
125*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_SOS_SPECTRAL";
126*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_SOS_SUCCESSIVE:
127*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_SOS_SUCCESSIVE";
128*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_HEADER_LENGTH:
129*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_HEADER_LENGTH";
130*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_COUNT_VALUE:
131*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_COUNT_VALUE";
132*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_DHT_MARKER:
133*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_DHT_MARKER";
134*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_INDEX_VALUE:
135*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_INDEX_VALUE";
136*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_NUMBER_HUFFMAN_TABLES:
137*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_NUMBER_HUFFMAN_TABLES";
138*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_QUANT_TABLE_LENGTH:
139*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_QUANT_TABLE_LENGTH";
140*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_NUMBER_QUANT_TABLES:
141*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_NUMBER_QUANT_TABLES";
142*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_COMPONENT_COUNT:
143*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_COMPONENT_COUNT";
144*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_DIVIDE_BY_ZERO_ERROR:
145*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_DIVIDE_BY_ZERO_ERROR";
146*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_NOT_JPG_IMAGE:
147*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_NOT_JPG_IMAGE";
148*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNSUPPORTED_ROTATION_ANGLE:
149*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_UNSUPPORTED_ROTATION_ANGLE";
150*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_UNSUPPORTED_SCALING:
151*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_UNSUPPORTED_SCALING";
152*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_INSUFFICIENT_OUTPUTBUFFER_SIZE:
153*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_INSUFFICIENT_OUTPUTBUFFER_SIZE";
154*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_HWCFG_GP_VERSION_VALUE:
155*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_HWCFG_GP_VERSION_VALUE";
156*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_VALUE_FROM_RED:
157*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_VALUE_FROM_RED";
158*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_BAD_SUBREGION_PARAMETERS:
159*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_BAD_SUBREGION_PARAMETERS";
160*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_PROGRESSIVE_DECODE_NOT_SUPPORTED:
161*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_PROGRESSIVE_DECODE_NOT_SUPPORTED";
162*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_ERROR_TASK_TIMEOUT:
163*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_ERROR_TASK_TIMEOUT";
164*e7b8153eSMauro Carvalho Chehab 	case JPEG_DECODER_ERROR_FEATURE_NOT_SUPPORTED:
165*e7b8153eSMauro Carvalho Chehab 		return "JPEG_DECODER_ERROR_FEATURE_NOT_SUPPORTED";
166*e7b8153eSMauro Carvalho Chehab 	default:
167*e7b8153eSMauro Carvalho Chehab 		return "!unknown MJPEG error!";
168*e7b8153eSMauro Carvalho Chehab 	}
169*e7b8153eSMauro Carvalho Chehab }
170*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_check_status(struct delta_ctx * pctx,struct jpeg_decode_return_params_t * status)171*e7b8153eSMauro Carvalho Chehab static bool delta_mjpeg_check_status(struct delta_ctx *pctx,
172*e7b8153eSMauro Carvalho Chehab 				     struct jpeg_decode_return_params_t *status)
173*e7b8153eSMauro Carvalho Chehab {
174*e7b8153eSMauro Carvalho Chehab 	struct delta_dev *delta = pctx->dev;
175*e7b8153eSMauro Carvalho Chehab 	bool dump = false;
176*e7b8153eSMauro Carvalho Chehab 
177*e7b8153eSMauro Carvalho Chehab 	if (status->error_code == JPEG_DECODER_NO_ERROR)
178*e7b8153eSMauro Carvalho Chehab 		goto out;
179*e7b8153eSMauro Carvalho Chehab 
180*e7b8153eSMauro Carvalho Chehab 	if (is_stream_error(status->error_code)) {
181*e7b8153eSMauro Carvalho Chehab 		dev_warn_ratelimited(delta->dev,
182*e7b8153eSMauro Carvalho Chehab 				     "%s  firmware: stream error @ frame %d (%s)\n",
183*e7b8153eSMauro Carvalho Chehab 				     pctx->name, pctx->decoded_frames,
184*e7b8153eSMauro Carvalho Chehab 				     err_str(status->error_code));
185*e7b8153eSMauro Carvalho Chehab 		pctx->stream_errors++;
186*e7b8153eSMauro Carvalho Chehab 	} else {
187*e7b8153eSMauro Carvalho Chehab 		dev_warn_ratelimited(delta->dev,
188*e7b8153eSMauro Carvalho Chehab 				     "%s  firmware: decode error @ frame %d (%s)\n",
189*e7b8153eSMauro Carvalho Chehab 				     pctx->name, pctx->decoded_frames,
190*e7b8153eSMauro Carvalho Chehab 				     err_str(status->error_code));
191*e7b8153eSMauro Carvalho Chehab 		pctx->decode_errors++;
192*e7b8153eSMauro Carvalho Chehab 		dump = true;
193*e7b8153eSMauro Carvalho Chehab 	}
194*e7b8153eSMauro Carvalho Chehab 
195*e7b8153eSMauro Carvalho Chehab out:
196*e7b8153eSMauro Carvalho Chehab 	dev_dbg(delta->dev,
197*e7b8153eSMauro Carvalho Chehab 		"%s  firmware: decoding time(us)=%d\n", pctx->name,
198*e7b8153eSMauro Carvalho Chehab 		status->decode_time_in_us);
199*e7b8153eSMauro Carvalho Chehab 
200*e7b8153eSMauro Carvalho Chehab 	return dump;
201*e7b8153eSMauro Carvalho Chehab }
202*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_ipc_open(struct delta_ctx * pctx)203*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_ipc_open(struct delta_ctx *pctx)
204*e7b8153eSMauro Carvalho Chehab {
205*e7b8153eSMauro Carvalho Chehab 	struct delta_dev *delta = pctx->dev;
206*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx = to_ctx(pctx);
207*e7b8153eSMauro Carvalho Chehab 	int ret = 0;
208*e7b8153eSMauro Carvalho Chehab 	struct jpeg_video_decode_init_params_t params_struct;
209*e7b8153eSMauro Carvalho Chehab 	struct jpeg_video_decode_init_params_t *params = &params_struct;
210*e7b8153eSMauro Carvalho Chehab 	struct delta_buf *ipc_buf;
211*e7b8153eSMauro Carvalho Chehab 	u32 ipc_buf_size;
212*e7b8153eSMauro Carvalho Chehab 	struct delta_ipc_param ipc_param;
213*e7b8153eSMauro Carvalho Chehab 	void *hdl;
214*e7b8153eSMauro Carvalho Chehab 
215*e7b8153eSMauro Carvalho Chehab 	memset(params, 0, sizeof(*params));
216*e7b8153eSMauro Carvalho Chehab 	params->circular_buffer_begin_addr_p = 0x00000000;
217*e7b8153eSMauro Carvalho Chehab 	params->circular_buffer_end_addr_p = 0xffffffff;
218*e7b8153eSMauro Carvalho Chehab 
219*e7b8153eSMauro Carvalho Chehab 	dev_vdbg(delta->dev,
220*e7b8153eSMauro Carvalho Chehab 		 "%s  %s\n", pctx->name,
221*e7b8153eSMauro Carvalho Chehab 		 ipc_open_param_str(params, ctx->str, sizeof(ctx->str)));
222*e7b8153eSMauro Carvalho Chehab 
223*e7b8153eSMauro Carvalho Chehab 	ipc_param.size = sizeof(*params);
224*e7b8153eSMauro Carvalho Chehab 	ipc_param.data = params;
225*e7b8153eSMauro Carvalho Chehab 	ipc_buf_size = sizeof(struct jpeg_decode_params_t) +
226*e7b8153eSMauro Carvalho Chehab 	    sizeof(struct jpeg_decode_return_params_t);
227*e7b8153eSMauro Carvalho Chehab 	ret = delta_ipc_open(pctx, "JPEG_DECODER_HW0", &ipc_param,
228*e7b8153eSMauro Carvalho Chehab 			     ipc_buf_size, &ipc_buf, &hdl);
229*e7b8153eSMauro Carvalho Chehab 	if (ret) {
230*e7b8153eSMauro Carvalho Chehab 		dev_err(delta->dev,
231*e7b8153eSMauro Carvalho Chehab 			"%s  dumping command %s\n", pctx->name,
232*e7b8153eSMauro Carvalho Chehab 			ipc_open_param_str(params, ctx->str, sizeof(ctx->str)));
233*e7b8153eSMauro Carvalho Chehab 		return ret;
234*e7b8153eSMauro Carvalho Chehab 	}
235*e7b8153eSMauro Carvalho Chehab 
236*e7b8153eSMauro Carvalho Chehab 	ctx->ipc_buf = ipc_buf;
237*e7b8153eSMauro Carvalho Chehab 	ctx->ipc_hdl = hdl;
238*e7b8153eSMauro Carvalho Chehab 
239*e7b8153eSMauro Carvalho Chehab 	return 0;
240*e7b8153eSMauro Carvalho Chehab }
241*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_ipc_decode(struct delta_ctx * pctx,struct delta_au * au)242*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_ipc_decode(struct delta_ctx *pctx, struct delta_au *au)
243*e7b8153eSMauro Carvalho Chehab {
244*e7b8153eSMauro Carvalho Chehab 	struct delta_dev *delta = pctx->dev;
245*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx = to_ctx(pctx);
246*e7b8153eSMauro Carvalho Chehab 	int ret = 0;
247*e7b8153eSMauro Carvalho Chehab 	struct jpeg_decode_params_t *params = ctx->ipc_buf->vaddr;
248*e7b8153eSMauro Carvalho Chehab 	struct jpeg_decode_return_params_t *status =
249*e7b8153eSMauro Carvalho Chehab 	    ctx->ipc_buf->vaddr + sizeof(*params);
250*e7b8153eSMauro Carvalho Chehab 	struct delta_frame *frame;
251*e7b8153eSMauro Carvalho Chehab 	struct delta_ipc_param ipc_param, ipc_status;
252*e7b8153eSMauro Carvalho Chehab 
253*e7b8153eSMauro Carvalho Chehab 	ret = delta_get_free_frame(pctx, &frame);
254*e7b8153eSMauro Carvalho Chehab 	if (ret)
255*e7b8153eSMauro Carvalho Chehab 		return ret;
256*e7b8153eSMauro Carvalho Chehab 
257*e7b8153eSMauro Carvalho Chehab 	memset(params, 0, sizeof(*params));
258*e7b8153eSMauro Carvalho Chehab 
259*e7b8153eSMauro Carvalho Chehab 	params->picture_start_addr_p = (u32)(au->paddr);
260*e7b8153eSMauro Carvalho Chehab 	params->picture_end_addr_p = (u32)(au->paddr + au->size - 1);
261*e7b8153eSMauro Carvalho Chehab 
262*e7b8153eSMauro Carvalho Chehab 	/*
263*e7b8153eSMauro Carvalho Chehab 	 * !WARNING!
264*e7b8153eSMauro Carvalho Chehab 	 * the NV12 decoded frame is only available
265*e7b8153eSMauro Carvalho Chehab 	 * on decimated output when enabling flag
266*e7b8153eSMauro Carvalho Chehab 	 * "JPEG_ADDITIONAL_FLAG_420MB"...
267*e7b8153eSMauro Carvalho Chehab 	 * the non decimated output gives YUV422SP
268*e7b8153eSMauro Carvalho Chehab 	 */
269*e7b8153eSMauro Carvalho Chehab 	params->main_aux_enable = JPEG_DISP_AUX_EN;
270*e7b8153eSMauro Carvalho Chehab 	params->additional_flags = JPEG_ADDITIONAL_FLAG_420MB;
271*e7b8153eSMauro Carvalho Chehab 	params->horizontal_decimation_factor = JPEG_HDEC_1;
272*e7b8153eSMauro Carvalho Chehab 	params->vertical_decimation_factor = JPEG_VDEC_1;
273*e7b8153eSMauro Carvalho Chehab 	params->decoding_mode = JPEG_NORMAL_DECODE;
274*e7b8153eSMauro Carvalho Chehab 
275*e7b8153eSMauro Carvalho Chehab 	params->display_buffer_addr.struct_size =
276*e7b8153eSMauro Carvalho Chehab 	    sizeof(struct jpeg_display_buffer_address_t);
277*e7b8153eSMauro Carvalho Chehab 	params->display_buffer_addr.display_decimated_luma_p =
278*e7b8153eSMauro Carvalho Chehab 	    (u32)frame->paddr;
279*e7b8153eSMauro Carvalho Chehab 	params->display_buffer_addr.display_decimated_chroma_p =
280*e7b8153eSMauro Carvalho Chehab 	    (u32)(frame->paddr
281*e7b8153eSMauro Carvalho Chehab 		  + frame->info.aligned_width * frame->info.aligned_height);
282*e7b8153eSMauro Carvalho Chehab 
283*e7b8153eSMauro Carvalho Chehab 	dev_vdbg(delta->dev,
284*e7b8153eSMauro Carvalho Chehab 		 "%s  %s\n", pctx->name,
285*e7b8153eSMauro Carvalho Chehab 		 ipc_decode_param_str(params, ctx->str, sizeof(ctx->str)));
286*e7b8153eSMauro Carvalho Chehab 
287*e7b8153eSMauro Carvalho Chehab 	/* status */
288*e7b8153eSMauro Carvalho Chehab 	memset(status, 0, sizeof(*status));
289*e7b8153eSMauro Carvalho Chehab 	status->error_code = JPEG_DECODER_NO_ERROR;
290*e7b8153eSMauro Carvalho Chehab 
291*e7b8153eSMauro Carvalho Chehab 	ipc_param.size = sizeof(*params);
292*e7b8153eSMauro Carvalho Chehab 	ipc_param.data = params;
293*e7b8153eSMauro Carvalho Chehab 	ipc_status.size = sizeof(*status);
294*e7b8153eSMauro Carvalho Chehab 	ipc_status.data = status;
295*e7b8153eSMauro Carvalho Chehab 	ret = delta_ipc_decode(ctx->ipc_hdl, &ipc_param, &ipc_status);
296*e7b8153eSMauro Carvalho Chehab 	if (ret) {
297*e7b8153eSMauro Carvalho Chehab 		dev_err(delta->dev,
298*e7b8153eSMauro Carvalho Chehab 			"%s  dumping command %s\n", pctx->name,
299*e7b8153eSMauro Carvalho Chehab 			ipc_decode_param_str(params, ctx->str,
300*e7b8153eSMauro Carvalho Chehab 					     sizeof(ctx->str)));
301*e7b8153eSMauro Carvalho Chehab 		return ret;
302*e7b8153eSMauro Carvalho Chehab 	}
303*e7b8153eSMauro Carvalho Chehab 
304*e7b8153eSMauro Carvalho Chehab 	pctx->decoded_frames++;
305*e7b8153eSMauro Carvalho Chehab 
306*e7b8153eSMauro Carvalho Chehab 	/* check firmware decoding status */
307*e7b8153eSMauro Carvalho Chehab 	if (delta_mjpeg_check_status(pctx, status)) {
308*e7b8153eSMauro Carvalho Chehab 		dev_err(delta->dev,
309*e7b8153eSMauro Carvalho Chehab 			"%s  dumping command %s\n", pctx->name,
310*e7b8153eSMauro Carvalho Chehab 			ipc_decode_param_str(params, ctx->str,
311*e7b8153eSMauro Carvalho Chehab 					     sizeof(ctx->str)));
312*e7b8153eSMauro Carvalho Chehab 	}
313*e7b8153eSMauro Carvalho Chehab 
314*e7b8153eSMauro Carvalho Chehab 	frame->field = V4L2_FIELD_NONE;
315*e7b8153eSMauro Carvalho Chehab 	frame->flags = V4L2_BUF_FLAG_KEYFRAME;
316*e7b8153eSMauro Carvalho Chehab 	frame->state |= DELTA_FRAME_DEC;
317*e7b8153eSMauro Carvalho Chehab 
318*e7b8153eSMauro Carvalho Chehab 	ctx->out_frame = frame;
319*e7b8153eSMauro Carvalho Chehab 
320*e7b8153eSMauro Carvalho Chehab 	return 0;
321*e7b8153eSMauro Carvalho Chehab }
322*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_open(struct delta_ctx * pctx)323*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_open(struct delta_ctx *pctx)
324*e7b8153eSMauro Carvalho Chehab {
325*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx;
326*e7b8153eSMauro Carvalho Chehab 
327*e7b8153eSMauro Carvalho Chehab 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
328*e7b8153eSMauro Carvalho Chehab 	if (!ctx)
329*e7b8153eSMauro Carvalho Chehab 		return -ENOMEM;
330*e7b8153eSMauro Carvalho Chehab 	pctx->priv = ctx;
331*e7b8153eSMauro Carvalho Chehab 
332*e7b8153eSMauro Carvalho Chehab 	return 0;
333*e7b8153eSMauro Carvalho Chehab }
334*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_close(struct delta_ctx * pctx)335*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_close(struct delta_ctx *pctx)
336*e7b8153eSMauro Carvalho Chehab {
337*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx = to_ctx(pctx);
338*e7b8153eSMauro Carvalho Chehab 
339*e7b8153eSMauro Carvalho Chehab 	if (ctx->ipc_hdl) {
340*e7b8153eSMauro Carvalho Chehab 		delta_ipc_close(ctx->ipc_hdl);
341*e7b8153eSMauro Carvalho Chehab 		ctx->ipc_hdl = NULL;
342*e7b8153eSMauro Carvalho Chehab 	}
343*e7b8153eSMauro Carvalho Chehab 
344*e7b8153eSMauro Carvalho Chehab 	kfree(ctx);
345*e7b8153eSMauro Carvalho Chehab 
346*e7b8153eSMauro Carvalho Chehab 	return 0;
347*e7b8153eSMauro Carvalho Chehab }
348*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_get_streaminfo(struct delta_ctx * pctx,struct delta_streaminfo * streaminfo)349*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_get_streaminfo(struct delta_ctx *pctx,
350*e7b8153eSMauro Carvalho Chehab 				      struct delta_streaminfo *streaminfo)
351*e7b8153eSMauro Carvalho Chehab {
352*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx = to_ctx(pctx);
353*e7b8153eSMauro Carvalho Chehab 
354*e7b8153eSMauro Carvalho Chehab 	if (!ctx->header)
355*e7b8153eSMauro Carvalho Chehab 		goto nodata;
356*e7b8153eSMauro Carvalho Chehab 
357*e7b8153eSMauro Carvalho Chehab 	streaminfo->streamformat = V4L2_PIX_FMT_MJPEG;
358*e7b8153eSMauro Carvalho Chehab 	streaminfo->width = ctx->header->frame_width;
359*e7b8153eSMauro Carvalho Chehab 	streaminfo->height = ctx->header->frame_height;
360*e7b8153eSMauro Carvalho Chehab 
361*e7b8153eSMauro Carvalho Chehab 	/* progressive stream */
362*e7b8153eSMauro Carvalho Chehab 	streaminfo->field = V4L2_FIELD_NONE;
363*e7b8153eSMauro Carvalho Chehab 
364*e7b8153eSMauro Carvalho Chehab 	streaminfo->dpb = 1;
365*e7b8153eSMauro Carvalho Chehab 
366*e7b8153eSMauro Carvalho Chehab 	return 0;
367*e7b8153eSMauro Carvalho Chehab 
368*e7b8153eSMauro Carvalho Chehab nodata:
369*e7b8153eSMauro Carvalho Chehab 	return -ENODATA;
370*e7b8153eSMauro Carvalho Chehab }
371*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_decode(struct delta_ctx * pctx,struct delta_au * pau)372*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_decode(struct delta_ctx *pctx, struct delta_au *pau)
373*e7b8153eSMauro Carvalho Chehab {
374*e7b8153eSMauro Carvalho Chehab 	struct delta_dev *delta = pctx->dev;
375*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx = to_ctx(pctx);
376*e7b8153eSMauro Carvalho Chehab 	int ret;
377*e7b8153eSMauro Carvalho Chehab 	struct delta_au au = *pau;
378*e7b8153eSMauro Carvalho Chehab 	unsigned int data_offset = 0;
379*e7b8153eSMauro Carvalho Chehab 	struct mjpeg_header *header = &ctx->header_struct;
380*e7b8153eSMauro Carvalho Chehab 
381*e7b8153eSMauro Carvalho Chehab 	if (!ctx->header) {
382*e7b8153eSMauro Carvalho Chehab 		ret = delta_mjpeg_read_header(pctx, au.vaddr, au.size,
383*e7b8153eSMauro Carvalho Chehab 					      header, &data_offset);
384*e7b8153eSMauro Carvalho Chehab 		if (ret) {
385*e7b8153eSMauro Carvalho Chehab 			pctx->stream_errors++;
386*e7b8153eSMauro Carvalho Chehab 			goto err;
387*e7b8153eSMauro Carvalho Chehab 		}
388*e7b8153eSMauro Carvalho Chehab 		if (header->frame_width * header->frame_height >
389*e7b8153eSMauro Carvalho Chehab 		    DELTA_MJPEG_MAX_RESO) {
390*e7b8153eSMauro Carvalho Chehab 			dev_err(delta->dev,
391*e7b8153eSMauro Carvalho Chehab 				"%s  stream resolution too large: %dx%d > %d pixels budget\n",
392*e7b8153eSMauro Carvalho Chehab 				pctx->name,
393*e7b8153eSMauro Carvalho Chehab 				header->frame_width,
394*e7b8153eSMauro Carvalho Chehab 				header->frame_height, DELTA_MJPEG_MAX_RESO);
395*e7b8153eSMauro Carvalho Chehab 			ret = -EINVAL;
396*e7b8153eSMauro Carvalho Chehab 			goto err;
397*e7b8153eSMauro Carvalho Chehab 		}
398*e7b8153eSMauro Carvalho Chehab 		ctx->header = header;
399*e7b8153eSMauro Carvalho Chehab 		goto out;
400*e7b8153eSMauro Carvalho Chehab 	}
401*e7b8153eSMauro Carvalho Chehab 
402*e7b8153eSMauro Carvalho Chehab 	if (!ctx->ipc_hdl) {
403*e7b8153eSMauro Carvalho Chehab 		ret = delta_mjpeg_ipc_open(pctx);
404*e7b8153eSMauro Carvalho Chehab 		if (ret)
405*e7b8153eSMauro Carvalho Chehab 			goto err;
406*e7b8153eSMauro Carvalho Chehab 	}
407*e7b8153eSMauro Carvalho Chehab 
408*e7b8153eSMauro Carvalho Chehab 	ret = delta_mjpeg_read_header(pctx, au.vaddr, au.size,
409*e7b8153eSMauro Carvalho Chehab 				      ctx->header, &data_offset);
410*e7b8153eSMauro Carvalho Chehab 	if (ret) {
411*e7b8153eSMauro Carvalho Chehab 		pctx->stream_errors++;
412*e7b8153eSMauro Carvalho Chehab 		goto err;
413*e7b8153eSMauro Carvalho Chehab 	}
414*e7b8153eSMauro Carvalho Chehab 
415*e7b8153eSMauro Carvalho Chehab 	au.paddr += data_offset;
416*e7b8153eSMauro Carvalho Chehab 	au.vaddr += data_offset;
417*e7b8153eSMauro Carvalho Chehab 
418*e7b8153eSMauro Carvalho Chehab 	ret = delta_mjpeg_ipc_decode(pctx, &au);
419*e7b8153eSMauro Carvalho Chehab 	if (ret)
420*e7b8153eSMauro Carvalho Chehab 		goto err;
421*e7b8153eSMauro Carvalho Chehab 
422*e7b8153eSMauro Carvalho Chehab out:
423*e7b8153eSMauro Carvalho Chehab 	return 0;
424*e7b8153eSMauro Carvalho Chehab 
425*e7b8153eSMauro Carvalho Chehab err:
426*e7b8153eSMauro Carvalho Chehab 	return ret;
427*e7b8153eSMauro Carvalho Chehab }
428*e7b8153eSMauro Carvalho Chehab 
delta_mjpeg_get_frame(struct delta_ctx * pctx,struct delta_frame ** frame)429*e7b8153eSMauro Carvalho Chehab static int delta_mjpeg_get_frame(struct delta_ctx *pctx,
430*e7b8153eSMauro Carvalho Chehab 				 struct delta_frame **frame)
431*e7b8153eSMauro Carvalho Chehab {
432*e7b8153eSMauro Carvalho Chehab 	struct delta_mjpeg_ctx *ctx = to_ctx(pctx);
433*e7b8153eSMauro Carvalho Chehab 
434*e7b8153eSMauro Carvalho Chehab 	if (!ctx->out_frame)
435*e7b8153eSMauro Carvalho Chehab 		return -ENODATA;
436*e7b8153eSMauro Carvalho Chehab 
437*e7b8153eSMauro Carvalho Chehab 	*frame = ctx->out_frame;
438*e7b8153eSMauro Carvalho Chehab 
439*e7b8153eSMauro Carvalho Chehab 	ctx->out_frame = NULL;
440*e7b8153eSMauro Carvalho Chehab 
441*e7b8153eSMauro Carvalho Chehab 	return 0;
442*e7b8153eSMauro Carvalho Chehab }
443*e7b8153eSMauro Carvalho Chehab 
444*e7b8153eSMauro Carvalho Chehab const struct delta_dec mjpegdec = {
445*e7b8153eSMauro Carvalho Chehab 	.name = "MJPEG",
446*e7b8153eSMauro Carvalho Chehab 	.streamformat = V4L2_PIX_FMT_MJPEG,
447*e7b8153eSMauro Carvalho Chehab 	.pixelformat = V4L2_PIX_FMT_NV12,
448*e7b8153eSMauro Carvalho Chehab 	.open = delta_mjpeg_open,
449*e7b8153eSMauro Carvalho Chehab 	.close = delta_mjpeg_close,
450*e7b8153eSMauro Carvalho Chehab 	.get_streaminfo = delta_mjpeg_get_streaminfo,
451*e7b8153eSMauro Carvalho Chehab 	.get_frameinfo = delta_get_frameinfo_default,
452*e7b8153eSMauro Carvalho Chehab 	.decode = delta_mjpeg_decode,
453*e7b8153eSMauro Carvalho Chehab 	.get_frame = delta_mjpeg_get_frame,
454*e7b8153eSMauro Carvalho Chehab 	.recycle = delta_recycle_default,
455*e7b8153eSMauro Carvalho Chehab };
456