xref: /openbmc/linux/drivers/ps3/ps3av_cmd.c (revision 827634ad)
1 /*
2  * Copyright (C) 2006 Sony Computer Entertainment Inc.
3  * Copyright 2006, 2007 Sony Corporation
4  *
5  * AV backend support for PS3
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 
25 #include <asm/ps3av.h>
26 #include <asm/ps3.h>
27 #include <asm/ps3gpu.h>
28 
29 #include "vuart.h"
30 
31 static const struct video_fmt {
32 	u32 format;
33 	u32 order;
34 } ps3av_video_fmt_table[] = {
35 	{ PS3AV_CMD_VIDEO_FORMAT_ARGB_8BIT, PS3AV_CMD_VIDEO_ORDER_RGB },
36 	{ PS3AV_CMD_VIDEO_FORMAT_ARGB_8BIT, PS3AV_CMD_VIDEO_ORDER_BGR },
37 };
38 
39 static const struct {
40 	int cs;
41 	u32 av;
42 	u32 bl;
43 } ps3av_cs_video2av_table[] = {
44 	{
45 		.cs = PS3AV_CMD_VIDEO_CS_RGB_8,
46 		.av = PS3AV_CMD_AV_CS_RGB_8,
47 		.bl = PS3AV_CMD_AV_CS_8
48 	}, {
49 		.cs = PS3AV_CMD_VIDEO_CS_RGB_10,
50 		.av = PS3AV_CMD_AV_CS_RGB_8,
51 		.bl = PS3AV_CMD_AV_CS_8
52 	}, {
53 		.cs = PS3AV_CMD_VIDEO_CS_RGB_12,
54 		.av = PS3AV_CMD_AV_CS_RGB_8,
55 		.bl = PS3AV_CMD_AV_CS_8
56 	}, {
57 		.cs = PS3AV_CMD_VIDEO_CS_YUV444_8,
58 		.av = PS3AV_CMD_AV_CS_YUV444_8,
59 		.bl = PS3AV_CMD_AV_CS_8
60 	}, {
61 		.cs = PS3AV_CMD_VIDEO_CS_YUV444_10,
62 		.av = PS3AV_CMD_AV_CS_YUV444_8,
63 		.bl = PS3AV_CMD_AV_CS_10
64 	}, {
65 		.cs = PS3AV_CMD_VIDEO_CS_YUV444_12,
66 		.av = PS3AV_CMD_AV_CS_YUV444_8,
67 		.bl = PS3AV_CMD_AV_CS_10
68 	}, {
69 		.cs = PS3AV_CMD_VIDEO_CS_YUV422_8,
70 		.av = PS3AV_CMD_AV_CS_YUV422_8,
71 		.bl = PS3AV_CMD_AV_CS_10
72 	}, {
73 		.cs = PS3AV_CMD_VIDEO_CS_YUV422_10,
74 		.av = PS3AV_CMD_AV_CS_YUV422_8,
75 		.bl = PS3AV_CMD_AV_CS_10
76 	}, {
77 		.cs = PS3AV_CMD_VIDEO_CS_YUV422_12,
78 		.av = PS3AV_CMD_AV_CS_YUV422_8,
79 		.bl = PS3AV_CMD_AV_CS_12
80 	}, {
81 		.cs = PS3AV_CMD_VIDEO_CS_XVYCC_8,
82 		.av = PS3AV_CMD_AV_CS_XVYCC_8,
83 		.bl = PS3AV_CMD_AV_CS_12
84 	}, {
85 		.cs = PS3AV_CMD_VIDEO_CS_XVYCC_10,
86 		.av = PS3AV_CMD_AV_CS_XVYCC_8,
87 		.bl = PS3AV_CMD_AV_CS_12
88 	}, {
89 		.cs = PS3AV_CMD_VIDEO_CS_XVYCC_12,
90 		.av = PS3AV_CMD_AV_CS_XVYCC_8,
91 		.bl = PS3AV_CMD_AV_CS_12
92 	}
93 };
94 
95 static u32 ps3av_cs_video2av(int cs)
96 {
97 	unsigned int i;
98 
99 	for (i = 0; i < ARRAY_SIZE(ps3av_cs_video2av_table); i++)
100 		if (ps3av_cs_video2av_table[i].cs == cs)
101 			return ps3av_cs_video2av_table[i].av;
102 
103 	return PS3AV_CMD_AV_CS_RGB_8;
104 }
105 
106 static u32 ps3av_cs_video2av_bitlen(int cs)
107 {
108 	unsigned int i;
109 
110 	for (i = 0; i < ARRAY_SIZE(ps3av_cs_video2av_table); i++)
111 		if (ps3av_cs_video2av_table[i].cs == cs)
112 			return ps3av_cs_video2av_table[i].bl;
113 
114 	return PS3AV_CMD_AV_CS_8;
115 }
116 
117 static const struct {
118 	int vid;
119 	u32 av;
120 } ps3av_vid_video2av_table[] = {
121 	{ PS3AV_CMD_VIDEO_VID_480I, PS3AV_CMD_AV_VID_480I },
122 	{ PS3AV_CMD_VIDEO_VID_480P, PS3AV_CMD_AV_VID_480P },
123 	{ PS3AV_CMD_VIDEO_VID_576I, PS3AV_CMD_AV_VID_576I },
124 	{ PS3AV_CMD_VIDEO_VID_576P, PS3AV_CMD_AV_VID_576P },
125 	{ PS3AV_CMD_VIDEO_VID_1080I_60HZ, PS3AV_CMD_AV_VID_1080I_60HZ },
126 	{ PS3AV_CMD_VIDEO_VID_720P_60HZ, PS3AV_CMD_AV_VID_720P_60HZ },
127 	{ PS3AV_CMD_VIDEO_VID_1080P_60HZ, PS3AV_CMD_AV_VID_1080P_60HZ },
128 	{ PS3AV_CMD_VIDEO_VID_1080I_50HZ, PS3AV_CMD_AV_VID_1080I_50HZ },
129 	{ PS3AV_CMD_VIDEO_VID_720P_50HZ, PS3AV_CMD_AV_VID_720P_50HZ },
130 	{ PS3AV_CMD_VIDEO_VID_1080P_50HZ, PS3AV_CMD_AV_VID_1080P_50HZ },
131 	{ PS3AV_CMD_VIDEO_VID_WXGA, PS3AV_CMD_AV_VID_WXGA },
132 	{ PS3AV_CMD_VIDEO_VID_SXGA, PS3AV_CMD_AV_VID_SXGA },
133 	{ PS3AV_CMD_VIDEO_VID_WUXGA, PS3AV_CMD_AV_VID_WUXGA }
134 };
135 
136 static u32 ps3av_vid_video2av(int vid)
137 {
138 	unsigned int i;
139 
140 	for (i = 0; i < ARRAY_SIZE(ps3av_vid_video2av_table); i++)
141 		if (ps3av_vid_video2av_table[i].vid == vid)
142 			return ps3av_vid_video2av_table[i].av;
143 
144 	return PS3AV_CMD_AV_VID_480P;
145 }
146 
147 static int ps3av_hdmi_range(void)
148 {
149 	if (ps3_compare_firmware_version(1, 8, 0) < 0)
150 		return 0;
151 	else
152 		return 1; /* supported */
153 }
154 
155 int ps3av_cmd_init(void)
156 {
157 	int res;
158 	struct ps3av_pkt_av_init av_init;
159 	struct ps3av_pkt_video_init video_init;
160 	struct ps3av_pkt_audio_init audio_init;
161 
162 	/* video init */
163 	memset(&video_init, 0, sizeof(video_init));
164 
165 	res = ps3av_do_pkt(PS3AV_CID_VIDEO_INIT, sizeof(video_init.send_hdr),
166 			   sizeof(video_init), &video_init.send_hdr);
167 	if (res < 0)
168 		return res;
169 
170 	res = get_status(&video_init);
171 	if (res) {
172 		printk(KERN_ERR "PS3AV_CID_VIDEO_INIT: failed %x\n", res);
173 		return res;
174 	}
175 
176 	/* audio init */
177 	memset(&audio_init, 0, sizeof(audio_init));
178 
179 	res = ps3av_do_pkt(PS3AV_CID_AUDIO_INIT, sizeof(audio_init.send_hdr),
180 			   sizeof(audio_init), &audio_init.send_hdr);
181 	if (res < 0)
182 		return res;
183 
184 	res = get_status(&audio_init);
185 	if (res) {
186 		printk(KERN_ERR "PS3AV_CID_AUDIO_INIT: failed %x\n", res);
187 		return res;
188 	}
189 
190 	/* av init */
191 	memset(&av_init, 0, sizeof(av_init));
192 	av_init.event_bit = 0;
193 
194 	res = ps3av_do_pkt(PS3AV_CID_AV_INIT, sizeof(av_init), sizeof(av_init),
195 			   &av_init.send_hdr);
196 	if (res < 0)
197 		return res;
198 
199 	res = get_status(&av_init);
200 	if (res)
201 		printk(KERN_ERR "PS3AV_CID_AV_INIT: failed %x\n", res);
202 
203 	return res;
204 }
205 
206 int ps3av_cmd_fin(void)
207 {
208 	int res;
209 	struct ps3av_pkt_av_fin av_fin;
210 
211 	memset(&av_fin, 0, sizeof(av_fin));
212 
213 	res = ps3av_do_pkt(PS3AV_CID_AV_FIN, sizeof(av_fin.send_hdr),
214 			   sizeof(av_fin), &av_fin.send_hdr);
215 	if (res < 0)
216 		return res;
217 
218 	res = get_status(&av_fin);
219 	if (res)
220 		printk(KERN_ERR "PS3AV_CID_AV_FIN: failed %x\n", res);
221 
222 	return res;
223 }
224 
225 int ps3av_cmd_av_video_mute(int num_of_port, u32 *port, u32 mute)
226 {
227 	int i, send_len, res;
228 	struct ps3av_pkt_av_video_mute av_video_mute;
229 
230 	if (num_of_port > PS3AV_MUTE_PORT_MAX)
231 		return -EINVAL;
232 
233 	memset(&av_video_mute, 0, sizeof(av_video_mute));
234 	for (i = 0; i < num_of_port; i++) {
235 		av_video_mute.mute[i].avport = port[i];
236 		av_video_mute.mute[i].mute = mute;
237 	}
238 
239 	send_len = sizeof(av_video_mute.send_hdr) +
240 	    sizeof(struct ps3av_av_mute) * num_of_port;
241 	res = ps3av_do_pkt(PS3AV_CID_AV_VIDEO_MUTE, send_len,
242 			   sizeof(av_video_mute), &av_video_mute.send_hdr);
243 	if (res < 0)
244 		return res;
245 
246 	res = get_status(&av_video_mute);
247 	if (res)
248 		printk(KERN_ERR "PS3AV_CID_AV_VIDEO_MUTE: failed %x\n", res);
249 
250 	return res;
251 }
252 
253 int ps3av_cmd_av_video_disable_sig(u32 port)
254 {
255 	int res;
256 	struct ps3av_pkt_av_video_disable_sig av_video_sig;
257 
258 	memset(&av_video_sig, 0, sizeof(av_video_sig));
259 	av_video_sig.avport = port;
260 
261 	res = ps3av_do_pkt(PS3AV_CID_AV_VIDEO_DISABLE_SIG,
262 			   sizeof(av_video_sig), sizeof(av_video_sig),
263 			   &av_video_sig.send_hdr);
264 	if (res < 0)
265 		return res;
266 
267 	res = get_status(&av_video_sig);
268 	if (res)
269 		printk(KERN_ERR
270 		       "PS3AV_CID_AV_VIDEO_DISABLE_SIG: failed %x port:%x\n",
271 		       res, port);
272 
273 	return res;
274 }
275 
276 int ps3av_cmd_av_tv_mute(u32 avport, u32 mute)
277 {
278 	int res;
279 	struct ps3av_pkt_av_tv_mute tv_mute;
280 
281 	memset(&tv_mute, 0, sizeof(tv_mute));
282 	tv_mute.avport = avport;
283 	tv_mute.mute = mute;
284 
285 	res = ps3av_do_pkt(PS3AV_CID_AV_TV_MUTE, sizeof(tv_mute),
286 			   sizeof(tv_mute), &tv_mute.send_hdr);
287 	if (res < 0)
288 		return res;
289 
290 	res = get_status(&tv_mute);
291 	if (res)
292 		printk(KERN_ERR "PS3AV_CID_AV_TV_MUTE: failed %x port:%x\n",
293 		       res, avport);
294 
295 	return res;
296 }
297 
298 int ps3av_cmd_enable_event(void)
299 {
300 	int res;
301 	struct ps3av_pkt_av_event av_event;
302 
303 	memset(&av_event, 0, sizeof(av_event));
304 	av_event.event_bit = PS3AV_CMD_EVENT_BIT_UNPLUGGED |
305 	    PS3AV_CMD_EVENT_BIT_PLUGGED | PS3AV_CMD_EVENT_BIT_HDCP_DONE;
306 
307 	res = ps3av_do_pkt(PS3AV_CID_AV_ENABLE_EVENT, sizeof(av_event),
308 			   sizeof(av_event), &av_event.send_hdr);
309 	if (res < 0)
310 		return res;
311 
312 	res = get_status(&av_event);
313 	if (res)
314 		printk(KERN_ERR "PS3AV_CID_AV_ENABLE_EVENT: failed %x\n", res);
315 
316 	return res;
317 }
318 
319 int ps3av_cmd_av_hdmi_mode(u8 mode)
320 {
321 	int res;
322 	struct ps3av_pkt_av_hdmi_mode hdmi_mode;
323 
324 	memset(&hdmi_mode, 0, sizeof(hdmi_mode));
325 	hdmi_mode.mode = mode;
326 
327 	res = ps3av_do_pkt(PS3AV_CID_AV_HDMI_MODE, sizeof(hdmi_mode),
328 			   sizeof(hdmi_mode), &hdmi_mode.send_hdr);
329 	if (res < 0)
330 		return res;
331 
332 	res = get_status(&hdmi_mode);
333 	if (res && res != PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
334 		printk(KERN_ERR "PS3AV_CID_AV_HDMI_MODE: failed %x\n", res);
335 
336 	return res;
337 }
338 
339 u32 ps3av_cmd_set_av_video_cs(void *p, u32 avport, int video_vid, int cs_out,
340 			      int aspect, u32 id)
341 {
342 	struct ps3av_pkt_av_video_cs *av_video_cs;
343 
344 	av_video_cs = (struct ps3av_pkt_av_video_cs *)p;
345 	if (video_vid == -1)
346 		video_vid = PS3AV_CMD_VIDEO_VID_720P_60HZ;
347 	if (cs_out == -1)
348 		cs_out = PS3AV_CMD_VIDEO_CS_YUV444_8;
349 	if (aspect == -1)
350 		aspect = 0;
351 
352 	memset(av_video_cs, 0, sizeof(*av_video_cs));
353 	ps3av_set_hdr(PS3AV_CID_AV_VIDEO_CS, sizeof(*av_video_cs),
354 		      &av_video_cs->send_hdr);
355 	av_video_cs->avport = avport;
356 	/* should be same as video_mode.resolution */
357 	av_video_cs->av_vid = ps3av_vid_video2av(video_vid);
358 	av_video_cs->av_cs_out = ps3av_cs_video2av(cs_out);
359 	/* should be same as video_mode.video_cs_out */
360 	av_video_cs->av_cs_in = ps3av_cs_video2av(PS3AV_CMD_VIDEO_CS_RGB_8);
361 	av_video_cs->bitlen_out = ps3av_cs_video2av_bitlen(cs_out);
362 	if ((id & PS3AV_MODE_WHITE) && ps3av_hdmi_range())
363 		av_video_cs->super_white = PS3AV_CMD_AV_SUPER_WHITE_ON;
364 	else /* default off */
365 		av_video_cs->super_white = PS3AV_CMD_AV_SUPER_WHITE_OFF;
366 	av_video_cs->aspect = aspect;
367 	if (id & PS3AV_MODE_DITHER) {
368 		av_video_cs->dither = PS3AV_CMD_AV_DITHER_ON
369 		    | PS3AV_CMD_AV_DITHER_8BIT;
370 	} else {
371 		/* default off */
372 		av_video_cs->dither = PS3AV_CMD_AV_DITHER_OFF;
373 	}
374 
375 	return sizeof(*av_video_cs);
376 }
377 
378 u32 ps3av_cmd_set_video_mode(void *p, u32 head, int video_vid, int video_fmt,
379 			     u32 id)
380 {
381 	struct ps3av_pkt_video_mode *video_mode;
382 	u32 x, y;
383 
384 	video_mode = (struct ps3av_pkt_video_mode *)p;
385 	if (video_vid == -1)
386 		video_vid = PS3AV_CMD_VIDEO_VID_720P_60HZ;
387 	if (video_fmt == -1)
388 		video_fmt = PS3AV_CMD_VIDEO_FMT_X8R8G8B8;
389 
390 	if (ps3av_video_mode2res(id, &x, &y))
391 		return 0;
392 
393 	/* video mode */
394 	memset(video_mode, 0, sizeof(*video_mode));
395 	ps3av_set_hdr(PS3AV_CID_VIDEO_MODE, sizeof(*video_mode),
396 		      &video_mode->send_hdr);
397 	video_mode->video_head = head;
398 	if (video_vid == PS3AV_CMD_VIDEO_VID_480I
399 	    && head == PS3AV_CMD_VIDEO_HEAD_B)
400 		video_mode->video_vid = PS3AV_CMD_VIDEO_VID_480I_A;
401 	else
402 		video_mode->video_vid = video_vid;
403 	video_mode->width = (u16) x;
404 	video_mode->height = (u16) y;
405 	video_mode->pitch = video_mode->width * 4;	/* line_length */
406 	video_mode->video_out_format = PS3AV_CMD_VIDEO_OUT_FORMAT_RGB_12BIT;
407 	video_mode->video_format = ps3av_video_fmt_table[video_fmt].format;
408 	if ((id & PS3AV_MODE_COLOR) && ps3av_hdmi_range())
409 		video_mode->video_cl_cnv = PS3AV_CMD_VIDEO_CL_CNV_DISABLE_LUT;
410 	else /* default enable */
411 		video_mode->video_cl_cnv = PS3AV_CMD_VIDEO_CL_CNV_ENABLE_LUT;
412 	video_mode->video_order = ps3av_video_fmt_table[video_fmt].order;
413 
414 	pr_debug("%s: video_mode:vid:%x width:%d height:%d pitch:%d out_format:%d format:%x order:%x\n",
415 		__func__, video_vid, video_mode->width, video_mode->height,
416 		video_mode->pitch, video_mode->video_out_format,
417 		video_mode->video_format, video_mode->video_order);
418 	return sizeof(*video_mode);
419 }
420 
421 int ps3av_cmd_video_format_black(u32 head, u32 video_fmt, u32 mute)
422 {
423 	int res;
424 	struct ps3av_pkt_video_format video_format;
425 
426 	memset(&video_format, 0, sizeof(video_format));
427 	video_format.video_head = head;
428 	if (mute != PS3AV_CMD_MUTE_OFF)
429 		video_format.video_format = PS3AV_CMD_VIDEO_FORMAT_BLACK;
430 	else
431 		video_format.video_format =
432 		    ps3av_video_fmt_table[video_fmt].format;
433 	video_format.video_order = ps3av_video_fmt_table[video_fmt].order;
434 
435 	res = ps3av_do_pkt(PS3AV_CID_VIDEO_FORMAT, sizeof(video_format),
436 			   sizeof(video_format), &video_format.send_hdr);
437 	if (res < 0)
438 		return res;
439 
440 	res = get_status(&video_format);
441 	if (res)
442 		printk(KERN_ERR "PS3AV_CID_VIDEO_FORMAT: failed %x\n", res);
443 
444 	return res;
445 }
446 
447 
448 int ps3av_cmd_av_audio_mute(int num_of_port, u32 *port, u32 mute)
449 {
450 	int i, res;
451 	struct ps3av_pkt_av_audio_mute av_audio_mute;
452 
453 	if (num_of_port > PS3AV_MUTE_PORT_MAX)
454 		return -EINVAL;
455 
456 	/* audio mute */
457 	memset(&av_audio_mute, 0, sizeof(av_audio_mute));
458 	for (i = 0; i < num_of_port; i++) {
459 		av_audio_mute.mute[i].avport = port[i];
460 		av_audio_mute.mute[i].mute = mute;
461 	}
462 
463 	res = ps3av_do_pkt(PS3AV_CID_AV_AUDIO_MUTE,
464 			   sizeof(av_audio_mute.send_hdr) +
465 			   sizeof(struct ps3av_av_mute) * num_of_port,
466 			   sizeof(av_audio_mute), &av_audio_mute.send_hdr);
467 	if (res < 0)
468 		return res;
469 
470 	res = get_status(&av_audio_mute);
471 	if (res)
472 		printk(KERN_ERR "PS3AV_CID_AV_AUDIO_MUTE: failed %x\n", res);
473 
474 	return res;
475 }
476 
477 static const struct {
478 	u32 fs;
479 	u8 mclk;
480 } ps3av_cnv_mclk_table[] = {
481 	{ PS3AV_CMD_AUDIO_FS_44K, PS3AV_CMD_AV_MCLK_512 },
482 	{ PS3AV_CMD_AUDIO_FS_48K, PS3AV_CMD_AV_MCLK_512 },
483 	{ PS3AV_CMD_AUDIO_FS_88K, PS3AV_CMD_AV_MCLK_256 },
484 	{ PS3AV_CMD_AUDIO_FS_96K, PS3AV_CMD_AV_MCLK_256 },
485 	{ PS3AV_CMD_AUDIO_FS_176K, PS3AV_CMD_AV_MCLK_128 },
486 	{ PS3AV_CMD_AUDIO_FS_192K, PS3AV_CMD_AV_MCLK_128 }
487 };
488 
489 static u8 ps3av_cnv_mclk(u32 fs)
490 {
491 	unsigned int i;
492 
493 	for (i = 0; i < ARRAY_SIZE(ps3av_cnv_mclk_table); i++)
494 		if (ps3av_cnv_mclk_table[i].fs == fs)
495 			return ps3av_cnv_mclk_table[i].mclk;
496 
497 	printk(KERN_ERR "%s failed, fs:%x\n", __func__, fs);
498 	return 0;
499 }
500 
501 #define BASE	PS3AV_CMD_AUDIO_FS_44K
502 
503 static const u32 ps3av_ns_table[][5] = {
504 					/*   D1,    D2,    D3,    D4,    D5 */
505 	[PS3AV_CMD_AUDIO_FS_44K-BASE] =	{  6272,  6272, 17836, 17836,  8918 },
506 	[PS3AV_CMD_AUDIO_FS_48K-BASE] =	{  6144,  6144, 11648, 11648,  5824 },
507 	[PS3AV_CMD_AUDIO_FS_88K-BASE] =	{ 12544, 12544, 35672, 35672, 17836 },
508 	[PS3AV_CMD_AUDIO_FS_96K-BASE] =	{ 12288, 12288, 23296, 23296, 11648 },
509 	[PS3AV_CMD_AUDIO_FS_176K-BASE] =	{ 25088, 25088, 71344, 71344, 35672 },
510 	[PS3AV_CMD_AUDIO_FS_192K-BASE] =	{ 24576, 24576, 46592, 46592, 23296 }
511 };
512 
513 static void ps3av_cnv_ns(u8 *ns, u32 fs, u32 video_vid)
514 {
515 	u32 av_vid, ns_val;
516 	int d;
517 
518 	d = ns_val = 0;
519 	av_vid = ps3av_vid_video2av(video_vid);
520 	switch (av_vid) {
521 	case PS3AV_CMD_AV_VID_480I:
522 	case PS3AV_CMD_AV_VID_576I:
523 		d = 0;
524 		break;
525 	case PS3AV_CMD_AV_VID_480P:
526 	case PS3AV_CMD_AV_VID_576P:
527 		d = 1;
528 		break;
529 	case PS3AV_CMD_AV_VID_1080I_60HZ:
530 	case PS3AV_CMD_AV_VID_1080I_50HZ:
531 		d = 2;
532 		break;
533 	case PS3AV_CMD_AV_VID_720P_60HZ:
534 	case PS3AV_CMD_AV_VID_720P_50HZ:
535 		d = 3;
536 		break;
537 	case PS3AV_CMD_AV_VID_1080P_60HZ:
538 	case PS3AV_CMD_AV_VID_1080P_50HZ:
539 	case PS3AV_CMD_AV_VID_WXGA:
540 	case PS3AV_CMD_AV_VID_SXGA:
541 	case PS3AV_CMD_AV_VID_WUXGA:
542 		d = 4;
543 		break;
544 	default:
545 		printk(KERN_ERR "%s failed, vid:%x\n", __func__, video_vid);
546 		break;
547 	}
548 
549 	if (fs < PS3AV_CMD_AUDIO_FS_44K || fs > PS3AV_CMD_AUDIO_FS_192K)
550 		printk(KERN_ERR "%s failed, fs:%x\n", __func__, fs);
551 	else
552 		ns_val = ps3av_ns_table[PS3AV_CMD_AUDIO_FS_44K-BASE][d];
553 
554 	*ns++ = ns_val & 0x000000FF;
555 	*ns++ = (ns_val & 0x0000FF00) >> 8;
556 	*ns = (ns_val & 0x00FF0000) >> 16;
557 }
558 
559 #undef BASE
560 
561 static u8 ps3av_cnv_enable(u32 source, const u8 *enable)
562 {
563 	u8 ret = 0;
564 
565 	if (source == PS3AV_CMD_AUDIO_SOURCE_SPDIF) {
566 		ret = 0x03;
567 	} else if (source == PS3AV_CMD_AUDIO_SOURCE_SERIAL) {
568 		ret = ((enable[0] << 4) + (enable[1] << 5) + (enable[2] << 6) +
569 		       (enable[3] << 7)) | 0x01;
570 	} else
571 		printk(KERN_ERR "%s failed, source:%x\n", __func__, source);
572 	return ret;
573 }
574 
575 static u8 ps3av_cnv_fifomap(const u8 *map)
576 {
577 	u8 ret = 0;
578 
579 	ret = map[0] + (map[1] << 2) + (map[2] << 4) + (map[3] << 6);
580 	return ret;
581 }
582 
583 static u8 ps3av_cnv_inputlen(u32 word_bits)
584 {
585 	u8 ret = 0;
586 
587 	switch (word_bits) {
588 	case PS3AV_CMD_AUDIO_WORD_BITS_16:
589 		ret = PS3AV_CMD_AV_INPUTLEN_16;
590 		break;
591 	case PS3AV_CMD_AUDIO_WORD_BITS_20:
592 		ret = PS3AV_CMD_AV_INPUTLEN_20;
593 		break;
594 	case PS3AV_CMD_AUDIO_WORD_BITS_24:
595 		ret = PS3AV_CMD_AV_INPUTLEN_24;
596 		break;
597 	default:
598 		printk(KERN_ERR "%s failed, word_bits:%x\n", __func__,
599 		       word_bits);
600 		break;
601 	}
602 	return ret;
603 }
604 
605 static u8 ps3av_cnv_layout(u32 num_of_ch)
606 {
607 	if (num_of_ch > PS3AV_CMD_AUDIO_NUM_OF_CH_8) {
608 		printk(KERN_ERR "%s failed, num_of_ch:%x\n", __func__,
609 		       num_of_ch);
610 		return 0;
611 	}
612 
613 	return num_of_ch == PS3AV_CMD_AUDIO_NUM_OF_CH_2 ? 0x0 : 0x1;
614 }
615 
616 static void ps3av_cnv_info(struct ps3av_audio_info_frame *info,
617 			   const struct ps3av_pkt_audio_mode *mode)
618 {
619 	info->pb1.cc = mode->audio_num_of_ch + 1;	/* CH2:0x01 --- CH8:0x07 */
620 	info->pb1.ct = 0;
621 	info->pb2.sf = 0;
622 	info->pb2.ss = 0;
623 
624 	info->pb3 = 0;		/* check mode->audio_format ?? */
625 	info->pb4 = mode->audio_layout;
626 	info->pb5.dm = mode->audio_downmix;
627 	info->pb5.lsv = mode->audio_downmix_level;
628 }
629 
630 static void ps3av_cnv_chstat(u8 *chstat, const u8 *cs_info)
631 {
632 	memcpy(chstat, cs_info, 5);
633 }
634 
635 u32 ps3av_cmd_set_av_audio_param(void *p, u32 port,
636 				 const struct ps3av_pkt_audio_mode *audio_mode,
637 				 u32 video_vid)
638 {
639 	struct ps3av_pkt_av_audio_param *param;
640 
641 	param = (struct ps3av_pkt_av_audio_param *)p;
642 
643 	memset(param, 0, sizeof(*param));
644 	ps3av_set_hdr(PS3AV_CID_AV_AUDIO_PARAM, sizeof(*param),
645 		      &param->send_hdr);
646 
647 	param->avport = port;
648 	param->mclk = ps3av_cnv_mclk(audio_mode->audio_fs) | 0x80;
649 	ps3av_cnv_ns(param->ns, audio_mode->audio_fs, video_vid);
650 	param->enable = ps3av_cnv_enable(audio_mode->audio_source,
651 					 audio_mode->audio_enable);
652 	param->swaplr = 0x09;
653 	param->fifomap = ps3av_cnv_fifomap(audio_mode->audio_map);
654 	param->inputctrl = 0x49;
655 	param->inputlen = ps3av_cnv_inputlen(audio_mode->audio_word_bits);
656 	param->layout = ps3av_cnv_layout(audio_mode->audio_num_of_ch);
657 	ps3av_cnv_info(&param->info, audio_mode);
658 	ps3av_cnv_chstat(param->chstat, audio_mode->audio_cs_info);
659 
660 	return sizeof(*param);
661 }
662 
663 /* default cs val */
664 u8 ps3av_mode_cs_info[] = {
665 	0x00, 0x09, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00
666 };
667 EXPORT_SYMBOL_GPL(ps3av_mode_cs_info);
668 
669 #define CS_44	0x00
670 #define CS_48	0x02
671 #define CS_88	0x08
672 #define CS_96	0x0a
673 #define CS_176	0x0c
674 #define CS_192	0x0e
675 #define CS_MASK	0x0f
676 #define CS_BIT	0x40
677 
678 void ps3av_cmd_set_audio_mode(struct ps3av_pkt_audio_mode *audio, u32 avport,
679 			      u32 ch, u32 fs, u32 word_bits, u32 format,
680 			      u32 source)
681 {
682 	int spdif_through;
683 	int i;
684 
685 	if (!(ch | fs | format | word_bits | source)) {
686 		ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
687 		fs = PS3AV_CMD_AUDIO_FS_48K;
688 		word_bits = PS3AV_CMD_AUDIO_WORD_BITS_16;
689 		format = PS3AV_CMD_AUDIO_FORMAT_PCM;
690 		source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
691 	}
692 
693 	/* audio mode */
694 	memset(audio, 0, sizeof(*audio));
695 	ps3av_set_hdr(PS3AV_CID_AUDIO_MODE, sizeof(*audio), &audio->send_hdr);
696 
697 	audio->avport = (u8) avport;
698 	audio->mask = 0x0FFF;	/* XXX set all */
699 	audio->audio_num_of_ch = ch;
700 	audio->audio_fs = fs;
701 	audio->audio_word_bits = word_bits;
702 	audio->audio_format = format;
703 	audio->audio_source = source;
704 
705 	switch (ch) {
706 	case PS3AV_CMD_AUDIO_NUM_OF_CH_8:
707 		audio->audio_enable[3] = 1;
708 		/* fall through */
709 	case PS3AV_CMD_AUDIO_NUM_OF_CH_6:
710 		audio->audio_enable[2] = 1;
711 		audio->audio_enable[1] = 1;
712 		/* fall through */
713 	case PS3AV_CMD_AUDIO_NUM_OF_CH_2:
714 	default:
715 		audio->audio_enable[0] = 1;
716 	}
717 
718 	/* audio swap L/R */
719 	for (i = 0; i < 4; i++)
720 		audio->audio_swap[i] = PS3AV_CMD_AUDIO_SWAP_0;	/* no swap */
721 
722 	/* audio serial input mapping */
723 	audio->audio_map[0] = PS3AV_CMD_AUDIO_MAP_OUTPUT_0;
724 	audio->audio_map[1] = PS3AV_CMD_AUDIO_MAP_OUTPUT_1;
725 	audio->audio_map[2] = PS3AV_CMD_AUDIO_MAP_OUTPUT_2;
726 	audio->audio_map[3] = PS3AV_CMD_AUDIO_MAP_OUTPUT_3;
727 
728 	/* audio speaker layout */
729 	if (avport == PS3AV_CMD_AVPORT_HDMI_0 ||
730 	    avport == PS3AV_CMD_AVPORT_HDMI_1) {
731 		switch (ch) {
732 		case PS3AV_CMD_AUDIO_NUM_OF_CH_8:
733 			audio->audio_layout = PS3AV_CMD_AUDIO_LAYOUT_8CH;
734 			break;
735 		case PS3AV_CMD_AUDIO_NUM_OF_CH_6:
736 			audio->audio_layout = PS3AV_CMD_AUDIO_LAYOUT_6CH;
737 			break;
738 		case PS3AV_CMD_AUDIO_NUM_OF_CH_2:
739 		default:
740 			audio->audio_layout = PS3AV_CMD_AUDIO_LAYOUT_2CH;
741 			break;
742 		}
743 	} else {
744 		audio->audio_layout = PS3AV_CMD_AUDIO_LAYOUT_2CH;
745 	}
746 
747 	/* audio downmix permission */
748 	audio->audio_downmix = PS3AV_CMD_AUDIO_DOWNMIX_PERMITTED;
749 	/* audio downmix level shift (0:0dB to 15:15dB) */
750 	audio->audio_downmix_level = 0;	/* 0dB */
751 
752 	/* set ch status */
753 	for (i = 0; i < 8; i++)
754 		audio->audio_cs_info[i] = ps3av_mode_cs_info[i];
755 
756 	switch (fs) {
757 	case PS3AV_CMD_AUDIO_FS_44K:
758 		audio->audio_cs_info[3] &= ~CS_MASK;
759 		audio->audio_cs_info[3] |= CS_44;
760 		break;
761 	case PS3AV_CMD_AUDIO_FS_88K:
762 		audio->audio_cs_info[3] &= ~CS_MASK;
763 		audio->audio_cs_info[3] |= CS_88;
764 		break;
765 	case PS3AV_CMD_AUDIO_FS_96K:
766 		audio->audio_cs_info[3] &= ~CS_MASK;
767 		audio->audio_cs_info[3] |= CS_96;
768 		break;
769 	case PS3AV_CMD_AUDIO_FS_176K:
770 		audio->audio_cs_info[3] &= ~CS_MASK;
771 		audio->audio_cs_info[3] |= CS_176;
772 		break;
773 	case PS3AV_CMD_AUDIO_FS_192K:
774 		audio->audio_cs_info[3] &= ~CS_MASK;
775 		audio->audio_cs_info[3] |= CS_192;
776 		break;
777 	default:
778 		break;
779 	}
780 
781 	/* non-audio bit */
782 	spdif_through = audio->audio_cs_info[0] & 0x02;
783 
784 	/* pass through setting */
785 	if (spdif_through &&
786 	    (avport == PS3AV_CMD_AVPORT_SPDIF_0 ||
787 	     avport == PS3AV_CMD_AVPORT_SPDIF_1 ||
788 	     avport == PS3AV_CMD_AVPORT_HDMI_0 ||
789 	     avport == PS3AV_CMD_AVPORT_HDMI_1)) {
790 		audio->audio_word_bits = PS3AV_CMD_AUDIO_WORD_BITS_16;
791 		audio->audio_format = PS3AV_CMD_AUDIO_FORMAT_BITSTREAM;
792 	}
793 }
794 
795 int ps3av_cmd_audio_mode(struct ps3av_pkt_audio_mode *audio_mode)
796 {
797 	int res;
798 
799 	res = ps3av_do_pkt(PS3AV_CID_AUDIO_MODE, sizeof(*audio_mode),
800 			   sizeof(*audio_mode), &audio_mode->send_hdr);
801 	if (res < 0)
802 		return res;
803 
804 	res = get_status(audio_mode);
805 	if (res)
806 		printk(KERN_ERR "PS3AV_CID_AUDIO_MODE: failed %x\n", res);
807 
808 	return res;
809 }
810 
811 int ps3av_cmd_audio_mute(int num_of_port, u32 *port, u32 mute)
812 {
813 	int i, res;
814 	struct ps3av_pkt_audio_mute audio_mute;
815 
816 	if (num_of_port > PS3AV_OPT_PORT_MAX)
817 		return -EINVAL;
818 
819 	/* audio mute */
820 	memset(&audio_mute, 0, sizeof(audio_mute));
821 	for (i = 0; i < num_of_port; i++) {
822 		audio_mute.mute[i].avport = port[i];
823 		audio_mute.mute[i].mute = mute;
824 	}
825 
826 	res = ps3av_do_pkt(PS3AV_CID_AUDIO_MUTE,
827 			   sizeof(audio_mute.send_hdr) +
828 			   sizeof(struct ps3av_audio_mute) * num_of_port,
829 			   sizeof(audio_mute), &audio_mute.send_hdr);
830 	if (res < 0)
831 		return res;
832 
833 	res = get_status(&audio_mute);
834 	if (res)
835 		printk(KERN_ERR "PS3AV_CID_AUDIO_MUTE: failed %x\n", res);
836 
837 	return res;
838 }
839 
840 int ps3av_cmd_audio_active(int active, u32 port)
841 {
842 	int res;
843 	struct ps3av_pkt_audio_active audio_active;
844 	u32 cid;
845 
846 	/* audio active */
847 	memset(&audio_active, 0, sizeof(audio_active));
848 	audio_active.audio_port = port;
849 	cid = active ? PS3AV_CID_AUDIO_ACTIVE : PS3AV_CID_AUDIO_INACTIVE;
850 
851 	res = ps3av_do_pkt(cid, sizeof(audio_active), sizeof(audio_active),
852 			   &audio_active.send_hdr);
853 	if (res < 0)
854 		return res;
855 
856 	res = get_status(&audio_active);
857 	if (res)
858 		printk(KERN_ERR "PS3AV_CID_AUDIO_ACTIVE:%x failed %x\n", cid,
859 		       res);
860 
861 	return res;
862 }
863 
864 int ps3av_cmd_avb_param(struct ps3av_pkt_avb_param *avb, u32 send_len)
865 {
866 	int res;
867 
868 	mutex_lock(&ps3_gpu_mutex);
869 
870 	/* avb packet */
871 	res = ps3av_do_pkt(PS3AV_CID_AVB_PARAM, send_len, sizeof(*avb),
872 			   &avb->send_hdr);
873 	if (res < 0)
874 		goto out;
875 
876 	res = get_status(avb);
877 	if (res)
878 		pr_debug("%s: PS3AV_CID_AVB_PARAM: failed %x\n", __func__,
879 			 res);
880 
881       out:
882 	mutex_unlock(&ps3_gpu_mutex);
883 	return res;
884 }
885 
886 int ps3av_cmd_av_get_hw_conf(struct ps3av_pkt_av_get_hw_conf *hw_conf)
887 {
888 	int res;
889 
890 	memset(hw_conf, 0, sizeof(*hw_conf));
891 
892 	res = ps3av_do_pkt(PS3AV_CID_AV_GET_HW_CONF, sizeof(hw_conf->send_hdr),
893 			   sizeof(*hw_conf), &hw_conf->send_hdr);
894 	if (res < 0)
895 		return res;
896 
897 	res = get_status(hw_conf);
898 	if (res)
899 		printk(KERN_ERR "PS3AV_CID_AV_GET_HW_CONF: failed %x\n", res);
900 
901 	return res;
902 }
903 
904 int ps3av_cmd_video_get_monitor_info(struct ps3av_pkt_av_get_monitor_info *info,
905 				     u32 avport)
906 {
907 	int res;
908 
909 	memset(info, 0, sizeof(*info));
910 	info->avport = avport;
911 
912 	res = ps3av_do_pkt(PS3AV_CID_AV_GET_MONITOR_INFO,
913 			   sizeof(info->send_hdr) + sizeof(info->avport) +
914 			   sizeof(info->reserved),
915 			   sizeof(*info), &info->send_hdr);
916 	if (res < 0)
917 		return res;
918 
919 	res = get_status(info);
920 	if (res)
921 		printk(KERN_ERR "PS3AV_CID_AV_GET_MONITOR_INFO: failed %x\n",
922 		       res);
923 
924 	return res;
925 }
926 
927 #define PS3AV_AV_LAYOUT_0 (PS3AV_CMD_AV_LAYOUT_32 \
928 		| PS3AV_CMD_AV_LAYOUT_44 \
929 		| PS3AV_CMD_AV_LAYOUT_48)
930 
931 #define PS3AV_AV_LAYOUT_1 (PS3AV_AV_LAYOUT_0 \
932 		| PS3AV_CMD_AV_LAYOUT_88 \
933 		| PS3AV_CMD_AV_LAYOUT_96 \
934 		| PS3AV_CMD_AV_LAYOUT_176 \
935 		| PS3AV_CMD_AV_LAYOUT_192)
936 
937