blob: 7ccc118fefedacda1b8805f17fa7a007ad01a660 [file] [log] [blame]
Thierry Redingf142d3b2012-11-21 15:29:29 +01001/*
2 * Copyright (C) 2012 Avionic Design GmbH
3 *
Thierry Reding96d672e02013-04-08 12:48:19 +00004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Thierry Redingf142d3b2012-11-21 15:29:29 +010022 */
23
24#include <linux/bitops.h>
Damien Lespiau72b09892013-08-06 20:32:14 +010025#include <linux/bug.h>
Thierry Redingf142d3b2012-11-21 15:29:29 +010026#include <linux/errno.h>
27#include <linux/export.h>
28#include <linux/hdmi.h>
29#include <linux/string.h>
30
31static void hdmi_infoframe_checksum(void *buffer, size_t size)
32{
33 u8 *ptr = buffer;
34 u8 csum = 0;
35 size_t i;
36
37 /* compute checksum */
38 for (i = 0; i < size; i++)
39 csum += ptr[i];
40
41 ptr[3] = 256 - csum;
42}
43
44/**
45 * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
46 * @frame: HDMI AVI infoframe
47 *
48 * Returns 0 on success or a negative error code on failure.
49 */
50int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
51{
52 memset(frame, 0, sizeof(*frame));
53
54 frame->type = HDMI_INFOFRAME_TYPE_AVI;
55 frame->version = 2;
Damien Lespiau3c6b0542013-08-06 20:32:13 +010056 frame->length = HDMI_AVI_INFOFRAME_SIZE;
Thierry Redingf142d3b2012-11-21 15:29:29 +010057
58 return 0;
59}
60EXPORT_SYMBOL(hdmi_avi_infoframe_init);
61
62/**
63 * hdmi_avi_infoframe_pack() - write HDMI AVI infoframe to binary buffer
64 * @frame: HDMI AVI infoframe
65 * @buffer: destination buffer
66 * @size: size of buffer
67 *
68 * Packs the information contained in the @frame structure into a binary
69 * representation that can be written into the corresponding controller
70 * registers. Also computes the checksum as required by section 5.3.5 of
71 * the HDMI 1.4 specification.
72 *
73 * Returns the number of bytes packed into the binary buffer or a negative
74 * error code on failure.
75 */
76ssize_t hdmi_avi_infoframe_pack(struct hdmi_avi_infoframe *frame, void *buffer,
77 size_t size)
78{
79 u8 *ptr = buffer;
80 size_t length;
81
82 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
83
84 if (size < length)
85 return -ENOSPC;
86
Damien Lespiau3b390f62013-08-06 20:32:16 +010087 memset(buffer, 0, size);
Thierry Redingf142d3b2012-11-21 15:29:29 +010088
89 ptr[0] = frame->type;
90 ptr[1] = frame->version;
91 ptr[2] = frame->length;
92 ptr[3] = 0; /* checksum */
93
94 /* start infoframe payload */
95 ptr += HDMI_INFOFRAME_HEADER_SIZE;
96
97 ptr[0] = ((frame->colorspace & 0x3) << 5) | (frame->scan_mode & 0x3);
98
Lespiau, Damiena5ad3dc2013-08-19 16:58:56 +010099 /*
100 * Data byte 1, bit 4 has to be set if we provide the active format
101 * aspect ratio
102 */
103 if (frame->active_aspect & 0xf)
Thierry Redingf142d3b2012-11-21 15:29:29 +0100104 ptr[0] |= BIT(4);
105
106 if (frame->horizontal_bar_valid)
107 ptr[0] |= BIT(3);
108
109 if (frame->vertical_bar_valid)
110 ptr[0] |= BIT(2);
111
112 ptr[1] = ((frame->colorimetry & 0x3) << 6) |
113 ((frame->picture_aspect & 0x3) << 4) |
114 (frame->active_aspect & 0xf);
115
116 ptr[2] = ((frame->extended_colorimetry & 0x7) << 4) |
117 ((frame->quantization_range & 0x3) << 2) |
118 (frame->nups & 0x3);
119
120 if (frame->itc)
121 ptr[2] |= BIT(7);
122
123 ptr[3] = frame->video_code & 0x7f;
124
125 ptr[4] = ((frame->ycc_quantization_range & 0x3) << 6) |
126 ((frame->content_type & 0x3) << 4) |
127 (frame->pixel_repeat & 0xf);
128
129 ptr[5] = frame->top_bar & 0xff;
130 ptr[6] = (frame->top_bar >> 8) & 0xff;
131 ptr[7] = frame->bottom_bar & 0xff;
132 ptr[8] = (frame->bottom_bar >> 8) & 0xff;
133 ptr[9] = frame->left_bar & 0xff;
134 ptr[10] = (frame->left_bar >> 8) & 0xff;
135 ptr[11] = frame->right_bar & 0xff;
136 ptr[12] = (frame->right_bar >> 8) & 0xff;
137
138 hdmi_infoframe_checksum(buffer, length);
139
140 return length;
141}
142EXPORT_SYMBOL(hdmi_avi_infoframe_pack);
143
144/**
145 * hdmi_spd_infoframe_init() - initialize an HDMI SPD infoframe
146 * @frame: HDMI SPD infoframe
147 * @vendor: vendor string
148 * @product: product string
149 *
150 * Returns 0 on success or a negative error code on failure.
151 */
152int hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame,
153 const char *vendor, const char *product)
154{
155 memset(frame, 0, sizeof(*frame));
156
157 frame->type = HDMI_INFOFRAME_TYPE_SPD;
158 frame->version = 1;
Damien Lespiau3c6b0542013-08-06 20:32:13 +0100159 frame->length = HDMI_SPD_INFOFRAME_SIZE;
Thierry Redingf142d3b2012-11-21 15:29:29 +0100160
161 strncpy(frame->vendor, vendor, sizeof(frame->vendor));
162 strncpy(frame->product, product, sizeof(frame->product));
163
164 return 0;
165}
166EXPORT_SYMBOL(hdmi_spd_infoframe_init);
167
168/**
169 * hdmi_spd_infoframe_pack() - write HDMI SPD infoframe to binary buffer
170 * @frame: HDMI SPD infoframe
171 * @buffer: destination buffer
172 * @size: size of buffer
173 *
174 * Packs the information contained in the @frame structure into a binary
175 * representation that can be written into the corresponding controller
176 * registers. Also computes the checksum as required by section 5.3.5 of
177 * the HDMI 1.4 specification.
178 *
179 * Returns the number of bytes packed into the binary buffer or a negative
180 * error code on failure.
181 */
182ssize_t hdmi_spd_infoframe_pack(struct hdmi_spd_infoframe *frame, void *buffer,
183 size_t size)
184{
185 u8 *ptr = buffer;
186 size_t length;
187
188 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
189
190 if (size < length)
191 return -ENOSPC;
192
Damien Lespiau3b390f62013-08-06 20:32:16 +0100193 memset(buffer, 0, size);
Thierry Redingf142d3b2012-11-21 15:29:29 +0100194
195 ptr[0] = frame->type;
196 ptr[1] = frame->version;
197 ptr[2] = frame->length;
198 ptr[3] = 0; /* checksum */
199
200 /* start infoframe payload */
201 ptr += HDMI_INFOFRAME_HEADER_SIZE;
202
203 memcpy(ptr, frame->vendor, sizeof(frame->vendor));
204 memcpy(ptr + 8, frame->product, sizeof(frame->product));
205
206 ptr[24] = frame->sdi;
207
208 hdmi_infoframe_checksum(buffer, length);
209
210 return length;
211}
212EXPORT_SYMBOL(hdmi_spd_infoframe_pack);
213
214/**
215 * hdmi_audio_infoframe_init() - initialize an HDMI audio infoframe
216 * @frame: HDMI audio infoframe
217 *
218 * Returns 0 on success or a negative error code on failure.
219 */
220int hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
221{
222 memset(frame, 0, sizeof(*frame));
223
224 frame->type = HDMI_INFOFRAME_TYPE_AUDIO;
225 frame->version = 1;
Damien Lespiau3c6b0542013-08-06 20:32:13 +0100226 frame->length = HDMI_AUDIO_INFOFRAME_SIZE;
Thierry Redingf142d3b2012-11-21 15:29:29 +0100227
228 return 0;
229}
230EXPORT_SYMBOL(hdmi_audio_infoframe_init);
231
232/**
233 * hdmi_audio_infoframe_pack() - write HDMI audio infoframe to binary buffer
234 * @frame: HDMI audio infoframe
235 * @buffer: destination buffer
236 * @size: size of buffer
237 *
238 * Packs the information contained in the @frame structure into a binary
239 * representation that can be written into the corresponding controller
240 * registers. Also computes the checksum as required by section 5.3.5 of
241 * the HDMI 1.4 specification.
242 *
243 * Returns the number of bytes packed into the binary buffer or a negative
244 * error code on failure.
245 */
246ssize_t hdmi_audio_infoframe_pack(struct hdmi_audio_infoframe *frame,
247 void *buffer, size_t size)
248{
249 unsigned char channels;
250 u8 *ptr = buffer;
251 size_t length;
252
253 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
254
255 if (size < length)
256 return -ENOSPC;
257
Damien Lespiau3b390f62013-08-06 20:32:16 +0100258 memset(buffer, 0, size);
Thierry Redingf142d3b2012-11-21 15:29:29 +0100259
260 if (frame->channels >= 2)
261 channels = frame->channels - 1;
262 else
263 channels = 0;
264
265 ptr[0] = frame->type;
266 ptr[1] = frame->version;
267 ptr[2] = frame->length;
268 ptr[3] = 0; /* checksum */
269
270 /* start infoframe payload */
271 ptr += HDMI_INFOFRAME_HEADER_SIZE;
272
273 ptr[0] = ((frame->coding_type & 0xf) << 4) | (channels & 0x7);
274 ptr[1] = ((frame->sample_frequency & 0x7) << 2) |
275 (frame->sample_size & 0x3);
276 ptr[2] = frame->coding_type_ext & 0x1f;
277 ptr[3] = frame->channel_allocation;
278 ptr[4] = (frame->level_shift_value & 0xf) << 3;
279
280 if (frame->downmix_inhibit)
281 ptr[4] |= BIT(7);
282
283 hdmi_infoframe_checksum(buffer, length);
284
285 return length;
286}
287EXPORT_SYMBOL(hdmi_audio_infoframe_pack);
288
289/**
290 * hdmi_vendor_infoframe_pack() - write a HDMI vendor infoframe to binary
291 * buffer
292 * @frame: HDMI vendor infoframe
293 * @buffer: destination buffer
294 * @size: size of buffer
295 *
296 * Packs the information contained in the @frame structure into a binary
297 * representation that can be written into the corresponding controller
298 * registers. Also computes the checksum as required by section 5.3.5 of
299 * the HDMI 1.4 specification.
300 *
301 * Returns the number of bytes packed into the binary buffer or a negative
302 * error code on failure.
303 */
304ssize_t hdmi_vendor_infoframe_pack(struct hdmi_vendor_infoframe *frame,
305 void *buffer, size_t size)
306{
307 u8 *ptr = buffer;
308 size_t length;
309
310 length = HDMI_INFOFRAME_HEADER_SIZE + frame->length;
311
312 if (size < length)
313 return -ENOSPC;
314
Damien Lespiau3b390f62013-08-06 20:32:16 +0100315 memset(buffer, 0, size);
Thierry Redingf142d3b2012-11-21 15:29:29 +0100316
317 ptr[0] = frame->type;
318 ptr[1] = frame->version;
319 ptr[2] = frame->length;
320 ptr[3] = 0; /* checksum */
321
322 memcpy(&ptr[HDMI_INFOFRAME_HEADER_SIZE], frame->data, frame->length);
323
324 hdmi_infoframe_checksum(buffer, length);
325
326 return length;
327}
328EXPORT_SYMBOL(hdmi_vendor_infoframe_pack);
Damien Lespiau72b09892013-08-06 20:32:14 +0100329
330/**
331 * hdmi_infoframe_pack() - write a HDMI infoframe to binary buffer
332 * @frame: HDMI infoframe
333 * @buffer: destination buffer
334 * @size: size of buffer
335 *
336 * Packs the information contained in the @frame structure into a binary
337 * representation that can be written into the corresponding controller
338 * registers. Also computes the checksum as required by section 5.3.5 of
339 * the HDMI 1.4 specification.
340 *
341 * Returns the number of bytes packed into the binary buffer or a negative
342 * error code on failure.
343 */
344ssize_t
345hdmi_infoframe_pack(union hdmi_infoframe *frame, void *buffer, size_t size)
346{
347 ssize_t length;
348
349 switch (frame->any.type) {
350 case HDMI_INFOFRAME_TYPE_AVI:
351 length = hdmi_avi_infoframe_pack(&frame->avi, buffer, size);
352 break;
353 case HDMI_INFOFRAME_TYPE_SPD:
354 length = hdmi_spd_infoframe_pack(&frame->spd, buffer, size);
355 break;
356 case HDMI_INFOFRAME_TYPE_AUDIO:
357 length = hdmi_audio_infoframe_pack(&frame->audio, buffer, size);
358 break;
359 case HDMI_INFOFRAME_TYPE_VENDOR:
360 length = hdmi_vendor_infoframe_pack(&frame->vendor,
361 buffer, size);
362 break;
363 default:
364 WARN(1, "Bad infoframe type %d\n", frame->any.type);
365 length = -EINVAL;
366 }
367
368 return length;
369}
370EXPORT_SYMBOL(hdmi_infoframe_pack);