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