ps3av.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/notifier.h>
  23. #include <linux/reboot.h>
  24. #include <linux/kernel.h>
  25. #include <linux/ioctl.h>
  26. #include <asm/lv1call.h>
  27. #include <asm/ps3av.h>
  28. #include <asm/ps3.h>
  29. #include "vuart.h"
  30. #define BUFSIZE 4096 /* vuart buf size */
  31. #define PS3AV_BUF_SIZE 512 /* max packet size */
  32. static int timeout = 5000; /* in msec ( 5 sec ) */
  33. module_param(timeout, int, 0644);
  34. static struct ps3av ps3av;
  35. static struct ps3_vuart_port_device ps3av_dev = {
  36. .match_id = PS3_MATCH_ID_AV_SETTINGS
  37. };
  38. /* color space */
  39. #define YUV444 PS3AV_CMD_VIDEO_CS_YUV444_8
  40. #define RGB8 PS3AV_CMD_VIDEO_CS_RGB_8
  41. /* format */
  42. #define XRGB PS3AV_CMD_VIDEO_FMT_X8R8G8B8
  43. /* aspect */
  44. #define A_N PS3AV_CMD_AV_ASPECT_4_3
  45. #define A_W PS3AV_CMD_AV_ASPECT_16_9
  46. static const struct avset_video_mode {
  47. u32 cs;
  48. u32 fmt;
  49. u32 vid;
  50. u32 aspect;
  51. u32 x;
  52. u32 y;
  53. u32 interlace;
  54. u32 freq;
  55. } video_mode_table[] = {
  56. { 0, }, /* auto */
  57. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480I, A_N, 720, 480, 1, 60},
  58. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480P, A_N, 720, 480, 0, 60},
  59. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_60HZ, A_N, 1280, 720, 0, 60},
  60. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_60HZ, A_W, 1920, 1080, 1, 60},
  61. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_60HZ, A_W, 1920, 1080, 0, 60},
  62. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576I, A_N, 720, 576, 1, 50},
  63. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576P, A_N, 720, 576, 0, 50},
  64. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_50HZ, A_N, 1280, 720, 0, 50},
  65. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_50HZ, A_W, 1920, 1080, 1, 50},
  66. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_50HZ, A_W, 1920, 1080, 0, 50},
  67. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WXGA, A_W, 1280, 768, 0, 60},
  68. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_SXGA, A_N, 1280, 1024, 0, 60},
  69. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WUXGA, A_W, 1920, 1200, 0, 60},
  70. };
  71. /* supported CIDs */
  72. static u32 cmd_table[] = {
  73. /* init */
  74. PS3AV_CID_AV_INIT,
  75. PS3AV_CID_AV_FIN,
  76. PS3AV_CID_VIDEO_INIT,
  77. PS3AV_CID_AUDIO_INIT,
  78. /* set */
  79. PS3AV_CID_AV_ENABLE_EVENT,
  80. PS3AV_CID_AV_DISABLE_EVENT,
  81. PS3AV_CID_AV_VIDEO_CS,
  82. PS3AV_CID_AV_VIDEO_MUTE,
  83. PS3AV_CID_AV_VIDEO_DISABLE_SIG,
  84. PS3AV_CID_AV_AUDIO_PARAM,
  85. PS3AV_CID_AV_AUDIO_MUTE,
  86. PS3AV_CID_AV_HDMI_MODE,
  87. PS3AV_CID_AV_TV_MUTE,
  88. PS3AV_CID_VIDEO_MODE,
  89. PS3AV_CID_VIDEO_FORMAT,
  90. PS3AV_CID_VIDEO_PITCH,
  91. PS3AV_CID_AUDIO_MODE,
  92. PS3AV_CID_AUDIO_MUTE,
  93. PS3AV_CID_AUDIO_ACTIVE,
  94. PS3AV_CID_AUDIO_INACTIVE,
  95. PS3AV_CID_AVB_PARAM,
  96. /* get */
  97. PS3AV_CID_AV_GET_HW_CONF,
  98. PS3AV_CID_AV_GET_MONITOR_INFO,
  99. /* event */
  100. PS3AV_CID_EVENT_UNPLUGGED,
  101. PS3AV_CID_EVENT_PLUGGED,
  102. PS3AV_CID_EVENT_HDCP_DONE,
  103. PS3AV_CID_EVENT_HDCP_FAIL,
  104. PS3AV_CID_EVENT_HDCP_AUTH,
  105. PS3AV_CID_EVENT_HDCP_ERROR,
  106. 0
  107. };
  108. #define PS3AV_EVENT_CMD_MASK 0x10000000
  109. #define PS3AV_EVENT_ID_MASK 0x0000ffff
  110. #define PS3AV_CID_MASK 0xffffffff
  111. #define PS3AV_REPLY_BIT 0x80000000
  112. #define ps3av_event_get_port_id(cid) ((cid >> 16) & 0xff)
  113. static u32 *ps3av_search_cmd_table(u32 cid, u32 mask)
  114. {
  115. u32 *table;
  116. int i;
  117. table = cmd_table;
  118. for (i = 0;; table++, i++) {
  119. if ((*table & mask) == (cid & mask))
  120. break;
  121. if (*table == 0)
  122. return NULL;
  123. }
  124. return table;
  125. }
  126. static int ps3av_parse_event_packet(const struct ps3av_reply_hdr *hdr)
  127. {
  128. u32 *table;
  129. if (hdr->cid & PS3AV_EVENT_CMD_MASK) {
  130. table = ps3av_search_cmd_table(hdr->cid, PS3AV_EVENT_CMD_MASK);
  131. if (table)
  132. dev_dbg(&ps3av_dev.core,
  133. "recv event packet cid:%08x port:0x%x size:%d\n",
  134. hdr->cid, ps3av_event_get_port_id(hdr->cid),
  135. hdr->size);
  136. else
  137. printk(KERN_ERR
  138. "%s: failed event packet, cid:%08x size:%d\n",
  139. __FUNCTION__, hdr->cid, hdr->size);
  140. return 1; /* receive event packet */
  141. }
  142. return 0;
  143. }
  144. static int ps3av_send_cmd_pkt(const struct ps3av_send_hdr *send_buf,
  145. struct ps3av_reply_hdr *recv_buf, int write_len,
  146. int read_len)
  147. {
  148. int res;
  149. u32 cmd;
  150. int event;
  151. if (!ps3av.available)
  152. return -ENODEV;
  153. /* send pkt */
  154. res = ps3av_vuart_write(ps3av.dev, send_buf, write_len);
  155. if (res < 0) {
  156. dev_dbg(&ps3av_dev.core,
  157. "%s: ps3av_vuart_write() failed (result=%d)\n",
  158. __FUNCTION__, res);
  159. return res;
  160. }
  161. /* recv pkt */
  162. cmd = send_buf->cid;
  163. do {
  164. /* read header */
  165. res = ps3av_vuart_read(ps3av.dev, recv_buf, PS3AV_HDR_SIZE,
  166. timeout);
  167. if (res != PS3AV_HDR_SIZE) {
  168. dev_dbg(&ps3av_dev.core,
  169. "%s: ps3av_vuart_read() failed (result=%d)\n",
  170. __FUNCTION__, res);
  171. return res;
  172. }
  173. /* read body */
  174. res = ps3av_vuart_read(ps3av.dev, &recv_buf->cid,
  175. recv_buf->size, timeout);
  176. if (res < 0) {
  177. dev_dbg(&ps3av_dev.core,
  178. "%s: ps3av_vuart_read() failed (result=%d)\n",
  179. __FUNCTION__, res);
  180. return res;
  181. }
  182. res += PS3AV_HDR_SIZE; /* total len */
  183. event = ps3av_parse_event_packet(recv_buf);
  184. /* ret > 0 event packet */
  185. } while (event);
  186. if ((cmd | PS3AV_REPLY_BIT) != recv_buf->cid) {
  187. dev_dbg(&ps3av_dev.core, "%s: reply err (result=%x)\n",
  188. __FUNCTION__, recv_buf->cid);
  189. return -EINVAL;
  190. }
  191. return 0;
  192. }
  193. static int ps3av_process_reply_packet(struct ps3av_send_hdr *cmd_buf,
  194. const struct ps3av_reply_hdr *recv_buf,
  195. int user_buf_size)
  196. {
  197. int return_len;
  198. if (recv_buf->version != PS3AV_VERSION) {
  199. dev_dbg(&ps3av_dev.core, "reply_packet invalid version:%x\n",
  200. recv_buf->version);
  201. return -EFAULT;
  202. }
  203. return_len = recv_buf->size + PS3AV_HDR_SIZE;
  204. if (return_len > user_buf_size)
  205. return_len = user_buf_size;
  206. memcpy(cmd_buf, recv_buf, return_len);
  207. return 0; /* success */
  208. }
  209. void ps3av_set_hdr(u32 cid, u16 size, struct ps3av_send_hdr *hdr)
  210. {
  211. hdr->version = PS3AV_VERSION;
  212. hdr->size = size - PS3AV_HDR_SIZE;
  213. hdr->cid = cid;
  214. }
  215. int ps3av_do_pkt(u32 cid, u16 send_len, size_t usr_buf_size,
  216. struct ps3av_send_hdr *buf)
  217. {
  218. int res = 0;
  219. union {
  220. struct ps3av_reply_hdr reply_hdr;
  221. u8 raw[PS3AV_BUF_SIZE];
  222. } recv_buf;
  223. u32 *table;
  224. BUG_ON(!ps3av.available);
  225. if (down_interruptible(&ps3av.sem))
  226. return -ERESTARTSYS;
  227. table = ps3av_search_cmd_table(cid, PS3AV_CID_MASK);
  228. BUG_ON(!table);
  229. BUG_ON(send_len < PS3AV_HDR_SIZE);
  230. BUG_ON(usr_buf_size < send_len);
  231. BUG_ON(usr_buf_size > PS3AV_BUF_SIZE);
  232. /* create header */
  233. ps3av_set_hdr(cid, send_len, buf);
  234. /* send packet via vuart */
  235. res = ps3av_send_cmd_pkt(buf, &recv_buf.reply_hdr, send_len,
  236. usr_buf_size);
  237. if (res < 0) {
  238. printk(KERN_ERR
  239. "%s: ps3av_send_cmd_pkt() failed (result=%d)\n",
  240. __FUNCTION__, res);
  241. goto err;
  242. }
  243. /* process reply packet */
  244. res = ps3av_process_reply_packet(buf, &recv_buf.reply_hdr,
  245. usr_buf_size);
  246. if (res < 0) {
  247. printk(KERN_ERR "%s: put_return_status() failed (result=%d)\n",
  248. __FUNCTION__, res);
  249. goto err;
  250. }
  251. up(&ps3av.sem);
  252. return 0;
  253. err:
  254. up(&ps3av.sem);
  255. printk(KERN_ERR "%s: failed cid:%x res:%d\n", __FUNCTION__, cid, res);
  256. return res;
  257. }
  258. static int ps3av_set_av_video_mute(u32 mute)
  259. {
  260. int i, num_of_av_port, res;
  261. num_of_av_port = ps3av.av_hw_conf.num_of_hdmi +
  262. ps3av.av_hw_conf.num_of_avmulti;
  263. /* video mute on */
  264. for (i = 0; i < num_of_av_port; i++) {
  265. res = ps3av_cmd_av_video_mute(1, &ps3av.av_port[i], mute);
  266. if (res < 0)
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. static int ps3av_set_video_disable_sig(void)
  272. {
  273. int i, num_of_hdmi_port, num_of_av_port, res;
  274. num_of_hdmi_port = ps3av.av_hw_conf.num_of_hdmi;
  275. num_of_av_port = ps3av.av_hw_conf.num_of_hdmi +
  276. ps3av.av_hw_conf.num_of_avmulti;
  277. /* tv mute */
  278. for (i = 0; i < num_of_hdmi_port; i++) {
  279. res = ps3av_cmd_av_tv_mute(ps3av.av_port[i],
  280. PS3AV_CMD_MUTE_ON);
  281. if (res < 0)
  282. return -1;
  283. }
  284. msleep(100);
  285. /* video mute on */
  286. for (i = 0; i < num_of_av_port; i++) {
  287. res = ps3av_cmd_av_video_disable_sig(ps3av.av_port[i]);
  288. if (res < 0)
  289. return -1;
  290. if (i < num_of_hdmi_port) {
  291. res = ps3av_cmd_av_tv_mute(ps3av.av_port[i],
  292. PS3AV_CMD_MUTE_OFF);
  293. if (res < 0)
  294. return -1;
  295. }
  296. }
  297. msleep(300);
  298. return 0;
  299. }
  300. static int ps3av_set_audio_mute(u32 mute)
  301. {
  302. int i, num_of_av_port, num_of_opt_port, res;
  303. num_of_av_port = ps3av.av_hw_conf.num_of_hdmi +
  304. ps3av.av_hw_conf.num_of_avmulti;
  305. num_of_opt_port = ps3av.av_hw_conf.num_of_spdif;
  306. for (i = 0; i < num_of_av_port; i++) {
  307. res = ps3av_cmd_av_audio_mute(1, &ps3av.av_port[i], mute);
  308. if (res < 0)
  309. return -1;
  310. }
  311. for (i = 0; i < num_of_opt_port; i++) {
  312. res = ps3av_cmd_audio_mute(1, &ps3av.opt_port[i], mute);
  313. if (res < 0)
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. int ps3av_set_audio_mode(u32 ch, u32 fs, u32 word_bits, u32 format, u32 source)
  319. {
  320. struct ps3av_pkt_avb_param avb_param;
  321. int i, num_of_audio, vid, res;
  322. struct ps3av_pkt_audio_mode audio_mode;
  323. u32 len = 0;
  324. num_of_audio = ps3av.av_hw_conf.num_of_hdmi +
  325. ps3av.av_hw_conf.num_of_avmulti +
  326. ps3av.av_hw_conf.num_of_spdif;
  327. avb_param.num_of_video_pkt = 0;
  328. avb_param.num_of_audio_pkt = PS3AV_AVB_NUM_AUDIO; /* always 0 */
  329. avb_param.num_of_av_video_pkt = 0;
  330. avb_param.num_of_av_audio_pkt = ps3av.av_hw_conf.num_of_hdmi;
  331. vid = video_mode_table[ps3av.ps3av_mode].vid;
  332. /* audio mute */
  333. ps3av_set_audio_mute(PS3AV_CMD_MUTE_ON);
  334. /* audio inactive */
  335. res = ps3av_cmd_audio_active(0, ps3av.audio_port);
  336. if (res < 0)
  337. dev_dbg(&ps3av_dev.core,
  338. "ps3av_cmd_audio_active OFF failed\n");
  339. /* audio_pkt */
  340. for (i = 0; i < num_of_audio; i++) {
  341. ps3av_cmd_set_audio_mode(&audio_mode, ps3av.av_port[i], ch, fs,
  342. word_bits, format, source);
  343. if (i < ps3av.av_hw_conf.num_of_hdmi) {
  344. /* hdmi only */
  345. len += ps3av_cmd_set_av_audio_param(&avb_param.buf[len],
  346. ps3av.av_port[i],
  347. &audio_mode, vid);
  348. }
  349. /* audio_mode pkt should be sent separately */
  350. res = ps3av_cmd_audio_mode(&audio_mode);
  351. if (res < 0)
  352. dev_dbg(&ps3av_dev.core,
  353. "ps3av_cmd_audio_mode failed, port:%x\n", i);
  354. }
  355. /* send command using avb pkt */
  356. len += offsetof(struct ps3av_pkt_avb_param, buf);
  357. res = ps3av_cmd_avb_param(&avb_param, len);
  358. if (res < 0)
  359. dev_dbg(&ps3av_dev.core, "ps3av_cmd_avb_param failed\n");
  360. /* audio mute */
  361. ps3av_set_audio_mute(PS3AV_CMD_MUTE_OFF);
  362. /* audio active */
  363. res = ps3av_cmd_audio_active(1, ps3av.audio_port);
  364. if (res < 0)
  365. dev_dbg(&ps3av_dev.core, "ps3av_cmd_audio_active ON failed\n");
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(ps3av_set_audio_mode);
  369. static int ps3av_set_videomode(void)
  370. {
  371. /* av video mute */
  372. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_ON);
  373. /* wake up ps3avd to do the actual video mode setting */
  374. up(&ps3av.ping);
  375. return 0;
  376. }
  377. static void ps3av_set_videomode_cont(u32 id, u32 old_id)
  378. {
  379. struct ps3av_pkt_avb_param avb_param;
  380. int i;
  381. u32 len = 0, av_video_cs;
  382. const struct avset_video_mode *video_mode;
  383. int res;
  384. video_mode = &video_mode_table[id & PS3AV_MODE_MASK];
  385. avb_param.num_of_video_pkt = PS3AV_AVB_NUM_VIDEO; /* num of head */
  386. avb_param.num_of_audio_pkt = 0;
  387. avb_param.num_of_av_video_pkt = ps3av.av_hw_conf.num_of_hdmi +
  388. ps3av.av_hw_conf.num_of_avmulti;
  389. avb_param.num_of_av_audio_pkt = 0;
  390. /* video signal off */
  391. ps3av_set_video_disable_sig();
  392. /* Retail PS3 product doesn't support this */
  393. if (id & PS3AV_MODE_HDCP_OFF) {
  394. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_HDCP_OFF);
  395. if (res == PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  396. dev_dbg(&ps3av_dev.core, "Not supported\n");
  397. else if (res)
  398. dev_dbg(&ps3av_dev.core,
  399. "ps3av_cmd_av_hdmi_mode failed\n");
  400. } else if (old_id & PS3AV_MODE_HDCP_OFF) {
  401. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_MODE_NORMAL);
  402. if (res < 0 && res != PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  403. dev_dbg(&ps3av_dev.core,
  404. "ps3av_cmd_av_hdmi_mode failed\n");
  405. }
  406. /* video_pkt */
  407. for (i = 0; i < avb_param.num_of_video_pkt; i++)
  408. len += ps3av_cmd_set_video_mode(&avb_param.buf[len],
  409. ps3av.head[i], video_mode->vid,
  410. video_mode->fmt, id);
  411. /* av_video_pkt */
  412. for (i = 0; i < avb_param.num_of_av_video_pkt; i++) {
  413. if (id & PS3AV_MODE_DVI || id & PS3AV_MODE_RGB)
  414. av_video_cs = RGB8;
  415. else
  416. av_video_cs = video_mode->cs;
  417. #ifndef PS3AV_HDMI_YUV
  418. if (ps3av.av_port[i] == PS3AV_CMD_AVPORT_HDMI_0 ||
  419. ps3av.av_port[i] == PS3AV_CMD_AVPORT_HDMI_1)
  420. av_video_cs = RGB8; /* use RGB for HDMI */
  421. #endif
  422. len += ps3av_cmd_set_av_video_cs(&avb_param.buf[len],
  423. ps3av.av_port[i],
  424. video_mode->vid, av_video_cs,
  425. video_mode->aspect, id);
  426. }
  427. /* send command using avb pkt */
  428. len += offsetof(struct ps3av_pkt_avb_param, buf);
  429. res = ps3av_cmd_avb_param(&avb_param, len);
  430. if (res == PS3AV_STATUS_NO_SYNC_HEAD)
  431. printk(KERN_WARNING
  432. "%s: Command failed. Please try your request again. \n",
  433. __FUNCTION__);
  434. else if (res)
  435. dev_dbg(&ps3av_dev.core, "ps3av_cmd_avb_param failed\n");
  436. msleep(1500);
  437. /* av video mute */
  438. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_OFF);
  439. }
  440. static int ps3avd(void *p)
  441. {
  442. struct ps3av *info = p;
  443. daemonize("ps3avd");
  444. while (1) {
  445. down(&info->ping);
  446. ps3av_set_videomode_cont(info->ps3av_mode,
  447. info->ps3av_mode_old);
  448. up(&info->pong);
  449. }
  450. return 0;
  451. }
  452. static int ps3av_vid2table_id(int vid)
  453. {
  454. int i;
  455. for (i = 1; i < ARRAY_SIZE(video_mode_table); i++)
  456. if (video_mode_table[i].vid == vid)
  457. return i;
  458. return -1;
  459. }
  460. static int ps3av_resbit2vid(u32 res_50, u32 res_60)
  461. {
  462. int vid = -1;
  463. if (res_50 > res_60) { /* if res_50 == res_60, res_60 will be used */
  464. if (res_50 & PS3AV_RESBIT_1920x1080P)
  465. vid = PS3AV_CMD_VIDEO_VID_1080P_50HZ;
  466. else if (res_50 & PS3AV_RESBIT_1920x1080I)
  467. vid = PS3AV_CMD_VIDEO_VID_1080I_50HZ;
  468. else if (res_50 & PS3AV_RESBIT_1280x720P)
  469. vid = PS3AV_CMD_VIDEO_VID_720P_50HZ;
  470. else if (res_50 & PS3AV_RESBIT_720x576P)
  471. vid = PS3AV_CMD_VIDEO_VID_576P;
  472. else
  473. vid = -1;
  474. } else {
  475. if (res_60 & PS3AV_RESBIT_1920x1080P)
  476. vid = PS3AV_CMD_VIDEO_VID_1080P_60HZ;
  477. else if (res_60 & PS3AV_RESBIT_1920x1080I)
  478. vid = PS3AV_CMD_VIDEO_VID_1080I_60HZ;
  479. else if (res_60 & PS3AV_RESBIT_1280x720P)
  480. vid = PS3AV_CMD_VIDEO_VID_720P_60HZ;
  481. else if (res_60 & PS3AV_RESBIT_720x480P)
  482. vid = PS3AV_CMD_VIDEO_VID_480P;
  483. else
  484. vid = -1;
  485. }
  486. return vid;
  487. }
  488. static int ps3av_hdmi_get_vid(struct ps3av_info_monitor *info)
  489. {
  490. u32 res_50, res_60;
  491. int vid = -1;
  492. if (info->monitor_type != PS3AV_MONITOR_TYPE_HDMI)
  493. return -1;
  494. /* check native resolution */
  495. res_50 = info->res_50.native & PS3AV_RES_MASK_50;
  496. res_60 = info->res_60.native & PS3AV_RES_MASK_60;
  497. if (res_50 || res_60) {
  498. vid = ps3av_resbit2vid(res_50, res_60);
  499. return vid;
  500. }
  501. /* check resolution */
  502. res_50 = info->res_50.res_bits & PS3AV_RES_MASK_50;
  503. res_60 = info->res_60.res_bits & PS3AV_RES_MASK_60;
  504. if (res_50 || res_60) {
  505. vid = ps3av_resbit2vid(res_50, res_60);
  506. return vid;
  507. }
  508. if (ps3av.region & PS3AV_REGION_60)
  509. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  510. else
  511. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  512. return vid;
  513. }
  514. static int ps3av_auto_videomode(struct ps3av_pkt_av_get_hw_conf *av_hw_conf,
  515. int boot)
  516. {
  517. int i, res, vid = -1, dvi = 0, rgb = 0;
  518. struct ps3av_pkt_av_get_monitor_info monitor_info;
  519. struct ps3av_info_monitor *info;
  520. /* get vid for hdmi */
  521. for (i = 0; i < av_hw_conf->num_of_hdmi; i++) {
  522. res = ps3av_cmd_video_get_monitor_info(&monitor_info,
  523. PS3AV_CMD_AVPORT_HDMI_0 +
  524. i);
  525. if (res < 0)
  526. return -1;
  527. ps3av_cmd_av_monitor_info_dump(&monitor_info);
  528. info = &monitor_info.info;
  529. /* check DVI */
  530. if (info->monitor_type == PS3AV_MONITOR_TYPE_DVI) {
  531. dvi = PS3AV_MODE_DVI;
  532. break;
  533. }
  534. /* check HDMI */
  535. vid = ps3av_hdmi_get_vid(info);
  536. if (vid != -1) {
  537. /* got valid vid */
  538. break;
  539. }
  540. }
  541. if (dvi) {
  542. /* DVI mode */
  543. vid = PS3AV_DEFAULT_DVI_VID;
  544. } else if (vid == -1) {
  545. /* no HDMI interface or HDMI is off */
  546. if (ps3av.region & PS3AV_REGION_60)
  547. vid = PS3AV_DEFAULT_AVMULTI_VID_REG_60;
  548. else
  549. vid = PS3AV_DEFAULT_AVMULTI_VID_REG_50;
  550. if (ps3av.region & PS3AV_REGION_RGB)
  551. rgb = PS3AV_MODE_RGB;
  552. } else if (boot) {
  553. /* HDMI: using DEFAULT HDMI_VID while booting up */
  554. info = &monitor_info.info;
  555. if (ps3av.region & PS3AV_REGION_60) {
  556. if (info->res_60.res_bits & PS3AV_RESBIT_720x480P)
  557. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  558. else if (info->res_50.res_bits & PS3AV_RESBIT_720x576P)
  559. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  560. else {
  561. /* default */
  562. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  563. }
  564. } else {
  565. if (info->res_50.res_bits & PS3AV_RESBIT_720x576P)
  566. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  567. else if (info->res_60.res_bits & PS3AV_RESBIT_720x480P)
  568. vid = PS3AV_DEFAULT_HDMI_VID_REG_60;
  569. else {
  570. /* default */
  571. vid = PS3AV_DEFAULT_HDMI_VID_REG_50;
  572. }
  573. }
  574. }
  575. return (ps3av_vid2table_id(vid) | dvi | rgb);
  576. }
  577. static int ps3av_get_hw_conf(struct ps3av *ps3av)
  578. {
  579. int i, j, k, res;
  580. /* get av_hw_conf */
  581. res = ps3av_cmd_av_get_hw_conf(&ps3av->av_hw_conf);
  582. if (res < 0)
  583. return -1;
  584. ps3av_cmd_av_hw_conf_dump(&ps3av->av_hw_conf);
  585. for (i = 0; i < PS3AV_HEAD_MAX; i++)
  586. ps3av->head[i] = PS3AV_CMD_VIDEO_HEAD_A + i;
  587. for (i = 0; i < PS3AV_OPT_PORT_MAX; i++)
  588. ps3av->opt_port[i] = PS3AV_CMD_AVPORT_SPDIF_0 + i;
  589. for (i = 0; i < ps3av->av_hw_conf.num_of_hdmi; i++)
  590. ps3av->av_port[i] = PS3AV_CMD_AVPORT_HDMI_0 + i;
  591. for (j = 0; j < ps3av->av_hw_conf.num_of_avmulti; j++)
  592. ps3av->av_port[i + j] = PS3AV_CMD_AVPORT_AVMULTI_0 + j;
  593. for (k = 0; k < ps3av->av_hw_conf.num_of_spdif; k++)
  594. ps3av->av_port[i + j + k] = PS3AV_CMD_AVPORT_SPDIF_0 + k;
  595. /* set all audio port */
  596. ps3av->audio_port = PS3AV_CMD_AUDIO_PORT_HDMI_0
  597. | PS3AV_CMD_AUDIO_PORT_HDMI_1
  598. | PS3AV_CMD_AUDIO_PORT_AVMULTI_0
  599. | PS3AV_CMD_AUDIO_PORT_SPDIF_0 | PS3AV_CMD_AUDIO_PORT_SPDIF_1;
  600. return 0;
  601. }
  602. /* set mode using id */
  603. int ps3av_set_video_mode(u32 id, int boot)
  604. {
  605. int size;
  606. u32 option;
  607. size = ARRAY_SIZE(video_mode_table);
  608. if ((id & PS3AV_MODE_MASK) > size - 1 || id < 0) {
  609. dev_dbg(&ps3av_dev.core, "%s: error id :%d\n", __FUNCTION__,
  610. id);
  611. return -EINVAL;
  612. }
  613. /* auto mode */
  614. option = id & ~PS3AV_MODE_MASK;
  615. if ((id & PS3AV_MODE_MASK) == 0) {
  616. id = ps3av_auto_videomode(&ps3av.av_hw_conf, boot);
  617. if (id < 1) {
  618. printk(KERN_ERR "%s: invalid id :%d\n", __FUNCTION__,
  619. id);
  620. return -EINVAL;
  621. }
  622. id |= option;
  623. }
  624. /* set videomode */
  625. down(&ps3av.pong);
  626. ps3av.ps3av_mode_old = ps3av.ps3av_mode;
  627. ps3av.ps3av_mode = id;
  628. if (ps3av_set_videomode())
  629. ps3av.ps3av_mode = ps3av.ps3av_mode_old;
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(ps3av_set_video_mode);
  633. int ps3av_set_mode(u32 id, int boot)
  634. {
  635. int res;
  636. res = ps3av_set_video_mode(id, boot);
  637. if (res)
  638. return res;
  639. res = ps3av_set_audio_mode(PS3AV_CMD_AUDIO_NUM_OF_CH_2,
  640. PS3AV_CMD_AUDIO_FS_48K,
  641. PS3AV_CMD_AUDIO_WORD_BITS_16,
  642. PS3AV_CMD_AUDIO_FORMAT_PCM,
  643. PS3AV_CMD_AUDIO_SOURCE_SERIAL);
  644. if (res)
  645. return res;
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(ps3av_set_mode);
  649. int ps3av_get_mode(void)
  650. {
  651. return ps3av.ps3av_mode;
  652. }
  653. EXPORT_SYMBOL_GPL(ps3av_get_mode);
  654. int ps3av_get_scanmode(int id)
  655. {
  656. int size;
  657. id = id & PS3AV_MODE_MASK;
  658. size = ARRAY_SIZE(video_mode_table);
  659. if (id > size - 1 || id < 0) {
  660. printk(KERN_ERR "%s: invalid mode %d\n", __FUNCTION__, id);
  661. return -EINVAL;
  662. }
  663. return video_mode_table[id].interlace;
  664. }
  665. EXPORT_SYMBOL_GPL(ps3av_get_scanmode);
  666. int ps3av_get_refresh_rate(int id)
  667. {
  668. int size;
  669. id = id & PS3AV_MODE_MASK;
  670. size = ARRAY_SIZE(video_mode_table);
  671. if (id > size - 1 || id < 0) {
  672. printk(KERN_ERR "%s: invalid mode %d\n", __FUNCTION__, id);
  673. return -EINVAL;
  674. }
  675. return video_mode_table[id].freq;
  676. }
  677. EXPORT_SYMBOL_GPL(ps3av_get_refresh_rate);
  678. /* get resolution by video_mode */
  679. int ps3av_video_mode2res(u32 id, u32 *xres, u32 *yres)
  680. {
  681. int size;
  682. id = id & PS3AV_MODE_MASK;
  683. size = ARRAY_SIZE(video_mode_table);
  684. if (id > size - 1 || id < 0) {
  685. printk(KERN_ERR "%s: invalid mode %d\n", __FUNCTION__, id);
  686. return -EINVAL;
  687. }
  688. *xres = video_mode_table[id].x;
  689. *yres = video_mode_table[id].y;
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(ps3av_video_mode2res);
  693. /* mute */
  694. int ps3av_video_mute(int mute)
  695. {
  696. return ps3av_set_av_video_mute(mute ? PS3AV_CMD_MUTE_ON
  697. : PS3AV_CMD_MUTE_OFF);
  698. }
  699. EXPORT_SYMBOL_GPL(ps3av_video_mute);
  700. int ps3av_audio_mute(int mute)
  701. {
  702. return ps3av_set_audio_mute(mute ? PS3AV_CMD_MUTE_ON
  703. : PS3AV_CMD_MUTE_OFF);
  704. }
  705. EXPORT_SYMBOL_GPL(ps3av_audio_mute);
  706. int ps3av_dev_open(void)
  707. {
  708. int status = 0;
  709. mutex_lock(&ps3av.mutex);
  710. if (!ps3av.open_count++) {
  711. status = lv1_gpu_open(0);
  712. if (status) {
  713. printk(KERN_ERR "%s: lv1_gpu_open failed %d\n",
  714. __FUNCTION__, status);
  715. ps3av.open_count--;
  716. }
  717. }
  718. mutex_unlock(&ps3av.mutex);
  719. return status;
  720. }
  721. EXPORT_SYMBOL_GPL(ps3av_dev_open);
  722. int ps3av_dev_close(void)
  723. {
  724. int status = 0;
  725. mutex_lock(&ps3av.mutex);
  726. if (ps3av.open_count <= 0) {
  727. printk(KERN_ERR "%s: GPU already closed\n", __FUNCTION__);
  728. status = -1;
  729. } else if (!--ps3av.open_count) {
  730. status = lv1_gpu_close();
  731. if (status)
  732. printk(KERN_WARNING "%s: lv1_gpu_close failed %d\n",
  733. __FUNCTION__, status);
  734. }
  735. mutex_unlock(&ps3av.mutex);
  736. return status;
  737. }
  738. EXPORT_SYMBOL_GPL(ps3av_dev_close);
  739. static int ps3av_probe(struct ps3_vuart_port_device *dev)
  740. {
  741. int res;
  742. u32 id;
  743. dev_dbg(&ps3av_dev.core, "init ...\n");
  744. dev_dbg(&ps3av_dev.core, " timeout=%d\n", timeout);
  745. memset(&ps3av, 0, sizeof(ps3av));
  746. init_MUTEX(&ps3av.sem);
  747. init_MUTEX_LOCKED(&ps3av.ping);
  748. init_MUTEX(&ps3av.pong);
  749. mutex_init(&ps3av.mutex);
  750. ps3av.ps3av_mode = 0;
  751. ps3av.dev = dev;
  752. kernel_thread(ps3avd, &ps3av, CLONE_KERNEL);
  753. ps3av.available = 1;
  754. switch (ps3_os_area_get_av_multi_out()) {
  755. case PS3_PARAM_AV_MULTI_OUT_NTSC:
  756. ps3av.region = PS3AV_REGION_60;
  757. break;
  758. case PS3_PARAM_AV_MULTI_OUT_PAL_YCBCR:
  759. case PS3_PARAM_AV_MULTI_OUT_SECAM:
  760. ps3av.region = PS3AV_REGION_50;
  761. break;
  762. case PS3_PARAM_AV_MULTI_OUT_PAL_RGB:
  763. ps3av.region = PS3AV_REGION_50 | PS3AV_REGION_RGB;
  764. break;
  765. default:
  766. ps3av.region = PS3AV_REGION_60;
  767. break;
  768. }
  769. /* init avsetting modules */
  770. res = ps3av_cmd_init();
  771. if (res < 0)
  772. printk(KERN_ERR "%s: ps3av_cmd_init failed %d\n", __FUNCTION__,
  773. res);
  774. ps3av_get_hw_conf(&ps3av);
  775. id = ps3av_auto_videomode(&ps3av.av_hw_conf, 1);
  776. mutex_lock(&ps3av.mutex);
  777. ps3av.ps3av_mode = id;
  778. mutex_unlock(&ps3av.mutex);
  779. dev_dbg(&ps3av_dev.core, "init...done\n");
  780. return 0;
  781. }
  782. static int ps3av_remove(struct ps3_vuart_port_device *dev)
  783. {
  784. if (ps3av.available) {
  785. ps3av_cmd_fin();
  786. ps3av.available = 0;
  787. }
  788. return 0;
  789. }
  790. static void ps3av_shutdown(struct ps3_vuart_port_device *dev)
  791. {
  792. ps3av_remove(dev);
  793. }
  794. static struct ps3_vuart_port_driver ps3av_driver = {
  795. .match_id = PS3_MATCH_ID_AV_SETTINGS,
  796. .core = {
  797. .name = "ps3_av",
  798. },
  799. .probe = ps3av_probe,
  800. .remove = ps3av_remove,
  801. .shutdown = ps3av_shutdown,
  802. };
  803. static int ps3av_module_init(void)
  804. {
  805. int error = ps3_vuart_port_driver_register(&ps3av_driver);
  806. if (error) {
  807. printk(KERN_ERR
  808. "%s: ps3_vuart_port_driver_register failed %d\n",
  809. __FUNCTION__, error);
  810. return error;
  811. }
  812. error = ps3_vuart_port_device_register(&ps3av_dev);
  813. if (error)
  814. printk(KERN_ERR
  815. "%s: ps3_vuart_port_device_register failed %d\n",
  816. __FUNCTION__, error);
  817. return error;
  818. }
  819. static void __exit ps3av_module_exit(void)
  820. {
  821. device_unregister(&ps3av_dev.core);
  822. ps3_vuart_port_driver_unregister(&ps3av_driver);
  823. }
  824. subsys_initcall(ps3av_module_init);
  825. module_exit(ps3av_module_exit);