ps3av.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * PS3 AV backend support.
  3. *
  4. * Copyright (C) 2007 Sony Computer Entertainment Inc.
  5. * Copyright 2007 Sony Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/notifier.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/fb.h>
  26. #include <asm/firmware.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 safe_mode;
  33. static int timeout = 5000; /* in msec ( 5 sec ) */
  34. module_param(timeout, int, 0644);
  35. static struct ps3av {
  36. struct mutex mutex;
  37. struct work_struct work;
  38. struct completion done;
  39. struct workqueue_struct *wq;
  40. int open_count;
  41. struct ps3_system_bus_device *dev;
  42. int region;
  43. struct ps3av_pkt_av_get_hw_conf av_hw_conf;
  44. u32 av_port[PS3AV_AV_PORT_MAX + PS3AV_OPT_PORT_MAX];
  45. u32 opt_port[PS3AV_OPT_PORT_MAX];
  46. u32 head[PS3AV_HEAD_MAX];
  47. u32 audio_port;
  48. int ps3av_mode;
  49. int ps3av_mode_old;
  50. union {
  51. struct ps3av_reply_hdr reply_hdr;
  52. u8 raw[PS3AV_BUF_SIZE];
  53. } recv_buf;
  54. void (*flip_ctl)(int on, void *data);
  55. void *flip_data;
  56. } *ps3av;
  57. /* color space */
  58. #define YUV444 PS3AV_CMD_VIDEO_CS_YUV444_8
  59. #define RGB8 PS3AV_CMD_VIDEO_CS_RGB_8
  60. /* format */
  61. #define XRGB PS3AV_CMD_VIDEO_FMT_X8R8G8B8
  62. /* aspect */
  63. #define A_N PS3AV_CMD_AV_ASPECT_4_3
  64. #define A_W PS3AV_CMD_AV_ASPECT_16_9
  65. static const struct avset_video_mode {
  66. u32 cs;
  67. u32 fmt;
  68. u32 vid;
  69. u32 aspect;
  70. u32 x;
  71. u32 y;
  72. } video_mode_table[] = {
  73. { 0, }, /* auto */
  74. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480I, A_N, 720, 480},
  75. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480P, A_N, 720, 480},
  76. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_60HZ, A_N, 1280, 720},
  77. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_60HZ, A_W, 1920, 1080},
  78. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_60HZ, A_W, 1920, 1080},
  79. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576I, A_N, 720, 576},
  80. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576P, A_N, 720, 576},
  81. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_50HZ, A_N, 1280, 720},
  82. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_50HZ, A_W, 1920, 1080},
  83. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_50HZ, A_W, 1920, 1080},
  84. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WXGA, A_W, 1280, 768},
  85. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_SXGA, A_N, 1280, 1024},
  86. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WUXGA, A_W, 1920, 1200},
  87. };
  88. /* supported CIDs */
  89. static u32 cmd_table[] = {
  90. /* init */
  91. PS3AV_CID_AV_INIT,
  92. PS3AV_CID_AV_FIN,
  93. PS3AV_CID_VIDEO_INIT,
  94. PS3AV_CID_AUDIO_INIT,
  95. /* set */
  96. PS3AV_CID_AV_ENABLE_EVENT,
  97. PS3AV_CID_AV_DISABLE_EVENT,
  98. PS3AV_CID_AV_VIDEO_CS,
  99. PS3AV_CID_AV_VIDEO_MUTE,
  100. PS3AV_CID_AV_VIDEO_DISABLE_SIG,
  101. PS3AV_CID_AV_AUDIO_PARAM,
  102. PS3AV_CID_AV_AUDIO_MUTE,
  103. PS3AV_CID_AV_HDMI_MODE,
  104. PS3AV_CID_AV_TV_MUTE,
  105. PS3AV_CID_VIDEO_MODE,
  106. PS3AV_CID_VIDEO_FORMAT,
  107. PS3AV_CID_VIDEO_PITCH,
  108. PS3AV_CID_AUDIO_MODE,
  109. PS3AV_CID_AUDIO_MUTE,
  110. PS3AV_CID_AUDIO_ACTIVE,
  111. PS3AV_CID_AUDIO_INACTIVE,
  112. PS3AV_CID_AVB_PARAM,
  113. /* get */
  114. PS3AV_CID_AV_GET_HW_CONF,
  115. PS3AV_CID_AV_GET_MONITOR_INFO,
  116. /* event */
  117. PS3AV_CID_EVENT_UNPLUGGED,
  118. PS3AV_CID_EVENT_PLUGGED,
  119. PS3AV_CID_EVENT_HDCP_DONE,
  120. PS3AV_CID_EVENT_HDCP_FAIL,
  121. PS3AV_CID_EVENT_HDCP_AUTH,
  122. PS3AV_CID_EVENT_HDCP_ERROR,
  123. 0
  124. };
  125. #define PS3AV_EVENT_CMD_MASK 0x10000000
  126. #define PS3AV_EVENT_ID_MASK 0x0000ffff
  127. #define PS3AV_CID_MASK 0xffffffff
  128. #define PS3AV_REPLY_BIT 0x80000000
  129. #define ps3av_event_get_port_id(cid) ((cid >> 16) & 0xff)
  130. static u32 *ps3av_search_cmd_table(u32 cid, u32 mask)
  131. {
  132. u32 *table;
  133. int i;
  134. table = cmd_table;
  135. for (i = 0;; table++, i++) {
  136. if ((*table & mask) == (cid & mask))
  137. break;
  138. if (*table == 0)
  139. return NULL;
  140. }
  141. return table;
  142. }
  143. static int ps3av_parse_event_packet(const struct ps3av_reply_hdr *hdr)
  144. {
  145. u32 *table;
  146. if (hdr->cid & PS3AV_EVENT_CMD_MASK) {
  147. table = ps3av_search_cmd_table(hdr->cid, PS3AV_EVENT_CMD_MASK);
  148. if (table)
  149. dev_dbg(&ps3av->dev->core,
  150. "recv event packet cid:%08x port:0x%x size:%d\n",
  151. hdr->cid, ps3av_event_get_port_id(hdr->cid),
  152. hdr->size);
  153. else
  154. printk(KERN_ERR
  155. "%s: failed event packet, cid:%08x size:%d\n",
  156. __func__, hdr->cid, hdr->size);
  157. return 1; /* receive event packet */
  158. }
  159. return 0;
  160. }
  161. #define POLLING_INTERVAL 25 /* in msec */
  162. static int ps3av_vuart_write(struct ps3_system_bus_device *dev,
  163. const void *buf, unsigned long size)
  164. {
  165. int error;
  166. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  167. error = ps3_vuart_write(dev, buf, size);
  168. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  169. return error ? error : size;
  170. }
  171. static int ps3av_vuart_read(struct ps3_system_bus_device *dev, void *buf,
  172. unsigned long size, int timeout)
  173. {
  174. int error;
  175. int loopcnt = 0;
  176. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  177. timeout = (timeout + POLLING_INTERVAL - 1) / POLLING_INTERVAL;
  178. while (loopcnt++ <= timeout) {
  179. error = ps3_vuart_read(dev, buf, size);
  180. if (!error)
  181. return size;
  182. if (error != -EAGAIN) {
  183. printk(KERN_ERR "%s: ps3_vuart_read failed %d\n",
  184. __func__, error);
  185. return error;
  186. }
  187. msleep(POLLING_INTERVAL);
  188. }
  189. return -EWOULDBLOCK;
  190. }
  191. static int ps3av_send_cmd_pkt(const struct ps3av_send_hdr *send_buf,
  192. struct ps3av_reply_hdr *recv_buf, int write_len,
  193. int read_len)
  194. {
  195. int res;
  196. u32 cmd;
  197. int event;
  198. if (!ps3av)
  199. return -ENODEV;
  200. /* send pkt */
  201. res = ps3av_vuart_write(ps3av->dev, send_buf, write_len);
  202. if (res < 0) {
  203. dev_dbg(&ps3av->dev->core,
  204. "%s: ps3av_vuart_write() failed (result=%d)\n",
  205. __func__, res);
  206. return res;
  207. }
  208. /* recv pkt */
  209. cmd = send_buf->cid;
  210. do {
  211. /* read header */
  212. res = ps3av_vuart_read(ps3av->dev, recv_buf, PS3AV_HDR_SIZE,
  213. timeout);
  214. if (res != PS3AV_HDR_SIZE) {
  215. dev_dbg(&ps3av->dev->core,
  216. "%s: ps3av_vuart_read() failed (result=%d)\n",
  217. __func__, res);
  218. return res;
  219. }
  220. /* read body */
  221. res = ps3av_vuart_read(ps3av->dev, &recv_buf->cid,
  222. recv_buf->size, timeout);
  223. if (res < 0) {
  224. dev_dbg(&ps3av->dev->core,
  225. "%s: ps3av_vuart_read() failed (result=%d)\n",
  226. __func__, res);
  227. return res;
  228. }
  229. res += PS3AV_HDR_SIZE; /* total len */
  230. event = ps3av_parse_event_packet(recv_buf);
  231. /* ret > 0 event packet */
  232. } while (event);
  233. if ((cmd | PS3AV_REPLY_BIT) != recv_buf->cid) {
  234. dev_dbg(&ps3av->dev->core, "%s: reply err (result=%x)\n",
  235. __func__, recv_buf->cid);
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. static int ps3av_process_reply_packet(struct ps3av_send_hdr *cmd_buf,
  241. const struct ps3av_reply_hdr *recv_buf,
  242. int user_buf_size)
  243. {
  244. int return_len;
  245. if (recv_buf->version != PS3AV_VERSION) {
  246. dev_dbg(&ps3av->dev->core, "reply_packet invalid version:%x\n",
  247. recv_buf->version);
  248. return -EFAULT;
  249. }
  250. return_len = recv_buf->size + PS3AV_HDR_SIZE;
  251. if (return_len > user_buf_size)
  252. return_len = user_buf_size;
  253. memcpy(cmd_buf, recv_buf, return_len);
  254. return 0; /* success */
  255. }
  256. void ps3av_set_hdr(u32 cid, u16 size, struct ps3av_send_hdr *hdr)
  257. {
  258. hdr->version = PS3AV_VERSION;
  259. hdr->size = size - PS3AV_HDR_SIZE;
  260. hdr->cid = cid;
  261. }
  262. int ps3av_do_pkt(u32 cid, u16 send_len, size_t usr_buf_size,
  263. struct ps3av_send_hdr *buf)
  264. {
  265. int res = 0;
  266. u32 *table;
  267. BUG_ON(!ps3av);
  268. mutex_lock(&ps3av->mutex);
  269. table = ps3av_search_cmd_table(cid, PS3AV_CID_MASK);
  270. BUG_ON(!table);
  271. BUG_ON(send_len < PS3AV_HDR_SIZE);
  272. BUG_ON(usr_buf_size < send_len);
  273. BUG_ON(usr_buf_size > PS3AV_BUF_SIZE);
  274. /* create header */
  275. ps3av_set_hdr(cid, send_len, buf);
  276. /* send packet via vuart */
  277. res = ps3av_send_cmd_pkt(buf, &ps3av->recv_buf.reply_hdr, send_len,
  278. usr_buf_size);
  279. if (res < 0) {
  280. printk(KERN_ERR
  281. "%s: ps3av_send_cmd_pkt() failed (result=%d)\n",
  282. __func__, res);
  283. goto err;
  284. }
  285. /* process reply packet */
  286. res = ps3av_process_reply_packet(buf, &ps3av->recv_buf.reply_hdr,
  287. usr_buf_size);
  288. if (res < 0) {
  289. printk(KERN_ERR "%s: put_return_status() failed (result=%d)\n",
  290. __func__, res);
  291. goto err;
  292. }
  293. mutex_unlock(&ps3av->mutex);
  294. return 0;
  295. err:
  296. mutex_unlock(&ps3av->mutex);
  297. printk(KERN_ERR "%s: failed cid:%x res:%d\n", __func__, cid, res);
  298. return res;
  299. }
  300. static int ps3av_set_av_video_mute(u32 mute)
  301. {
  302. int i, num_of_av_port, res;
  303. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  304. ps3av->av_hw_conf.num_of_avmulti;
  305. /* video mute on */
  306. for (i = 0; i < num_of_av_port; i++) {
  307. res = ps3av_cmd_av_video_mute(1, &ps3av->av_port[i], mute);
  308. if (res < 0)
  309. return -1;
  310. }
  311. return 0;
  312. }
  313. static int ps3av_set_video_disable_sig(void)
  314. {
  315. int i, num_of_hdmi_port, num_of_av_port, res;
  316. num_of_hdmi_port = ps3av->av_hw_conf.num_of_hdmi;
  317. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  318. ps3av->av_hw_conf.num_of_avmulti;
  319. /* tv mute */
  320. for (i = 0; i < num_of_hdmi_port; i++) {
  321. res = ps3av_cmd_av_tv_mute(ps3av->av_port[i],
  322. PS3AV_CMD_MUTE_ON);
  323. if (res < 0)
  324. return -1;
  325. }
  326. msleep(100);
  327. /* video mute on */
  328. for (i = 0; i < num_of_av_port; i++) {
  329. res = ps3av_cmd_av_video_disable_sig(ps3av->av_port[i]);
  330. if (res < 0)
  331. return -1;
  332. if (i < num_of_hdmi_port) {
  333. res = ps3av_cmd_av_tv_mute(ps3av->av_port[i],
  334. PS3AV_CMD_MUTE_OFF);
  335. if (res < 0)
  336. return -1;
  337. }
  338. }
  339. msleep(300);
  340. return 0;
  341. }
  342. static int ps3av_set_audio_mute(u32 mute)
  343. {
  344. int i, num_of_av_port, num_of_opt_port, res;
  345. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  346. ps3av->av_hw_conf.num_of_avmulti;
  347. num_of_opt_port = ps3av->av_hw_conf.num_of_spdif;
  348. for (i = 0; i < num_of_av_port; i++) {
  349. res = ps3av_cmd_av_audio_mute(1, &ps3av->av_port[i], mute);
  350. if (res < 0)
  351. return -1;
  352. }
  353. for (i = 0; i < num_of_opt_port; i++) {
  354. res = ps3av_cmd_audio_mute(1, &ps3av->opt_port[i], mute);
  355. if (res < 0)
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. int ps3av_set_audio_mode(u32 ch, u32 fs, u32 word_bits, u32 format, u32 source)
  361. {
  362. struct ps3av_pkt_avb_param avb_param;
  363. int i, num_of_audio, vid, res;
  364. struct ps3av_pkt_audio_mode audio_mode;
  365. u32 len = 0;
  366. num_of_audio = ps3av->av_hw_conf.num_of_hdmi +
  367. ps3av->av_hw_conf.num_of_avmulti +
  368. ps3av->av_hw_conf.num_of_spdif;
  369. avb_param.num_of_video_pkt = 0;
  370. avb_param.num_of_audio_pkt = PS3AV_AVB_NUM_AUDIO; /* always 0 */
  371. avb_param.num_of_av_video_pkt = 0;
  372. avb_param.num_of_av_audio_pkt = ps3av->av_hw_conf.num_of_hdmi;
  373. vid = video_mode_table[ps3av->ps3av_mode].vid;
  374. /* audio mute */
  375. ps3av_set_audio_mute(PS3AV_CMD_MUTE_ON);
  376. /* audio inactive */
  377. res = ps3av_cmd_audio_active(0, ps3av->audio_port);
  378. if (res < 0)
  379. dev_dbg(&ps3av->dev->core,
  380. "ps3av_cmd_audio_active OFF failed\n");
  381. /* audio_pkt */
  382. for (i = 0; i < num_of_audio; i++) {
  383. ps3av_cmd_set_audio_mode(&audio_mode, ps3av->av_port[i], ch,
  384. fs, word_bits, format, source);
  385. if (i < ps3av->av_hw_conf.num_of_hdmi) {
  386. /* hdmi only */
  387. len += ps3av_cmd_set_av_audio_param(&avb_param.buf[len],
  388. ps3av->av_port[i],
  389. &audio_mode, vid);
  390. }
  391. /* audio_mode pkt should be sent separately */
  392. res = ps3av_cmd_audio_mode(&audio_mode);
  393. if (res < 0)
  394. dev_dbg(&ps3av->dev->core,
  395. "ps3av_cmd_audio_mode failed, port:%x\n", i);
  396. }
  397. /* send command using avb pkt */
  398. len += offsetof(struct ps3av_pkt_avb_param, buf);
  399. res = ps3av_cmd_avb_param(&avb_param, len);
  400. if (res < 0)
  401. dev_dbg(&ps3av->dev->core, "ps3av_cmd_avb_param failed\n");
  402. /* audio mute */
  403. ps3av_set_audio_mute(PS3AV_CMD_MUTE_OFF);
  404. /* audio active */
  405. res = ps3av_cmd_audio_active(1, ps3av->audio_port);
  406. if (res < 0)
  407. dev_dbg(&ps3av->dev->core,
  408. "ps3av_cmd_audio_active ON failed\n");
  409. return 0;
  410. }
  411. EXPORT_SYMBOL_GPL(ps3av_set_audio_mode);
  412. static int ps3av_set_videomode(void)
  413. {
  414. /* av video mute */
  415. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_ON);
  416. /* wake up ps3avd to do the actual video mode setting */
  417. queue_work(ps3av->wq, &ps3av->work);
  418. return 0;
  419. }
  420. static void ps3av_set_videomode_packet(u32 id)
  421. {
  422. struct ps3av_pkt_avb_param avb_param;
  423. unsigned int i;
  424. u32 len = 0, av_video_cs;
  425. const struct avset_video_mode *video_mode;
  426. int res;
  427. video_mode = &video_mode_table[id & PS3AV_MODE_MASK];
  428. avb_param.num_of_video_pkt = PS3AV_AVB_NUM_VIDEO; /* num of head */
  429. avb_param.num_of_audio_pkt = 0;
  430. avb_param.num_of_av_video_pkt = ps3av->av_hw_conf.num_of_hdmi +
  431. ps3av->av_hw_conf.num_of_avmulti;
  432. avb_param.num_of_av_audio_pkt = 0;
  433. /* video_pkt */
  434. for (i = 0; i < avb_param.num_of_video_pkt; i++)
  435. len += ps3av_cmd_set_video_mode(&avb_param.buf[len],
  436. ps3av->head[i], video_mode->vid,
  437. video_mode->fmt, id);
  438. /* av_video_pkt */
  439. for (i = 0; i < avb_param.num_of_av_video_pkt; i++) {
  440. if (id & PS3AV_MODE_DVI || id & PS3AV_MODE_RGB)
  441. av_video_cs = RGB8;
  442. else
  443. av_video_cs = video_mode->cs;
  444. #ifndef PS3AV_HDMI_YUV
  445. if (ps3av->av_port[i] == PS3AV_CMD_AVPORT_HDMI_0 ||
  446. ps3av->av_port[i] == PS3AV_CMD_AVPORT_HDMI_1)
  447. av_video_cs = RGB8; /* use RGB for HDMI */
  448. #endif
  449. len += ps3av_cmd_set_av_video_cs(&avb_param.buf[len],
  450. ps3av->av_port[i],
  451. video_mode->vid, av_video_cs,
  452. video_mode->aspect, id);
  453. }
  454. /* send command using avb pkt */
  455. len += offsetof(struct ps3av_pkt_avb_param, buf);
  456. res = ps3av_cmd_avb_param(&avb_param, len);
  457. if (res == PS3AV_STATUS_NO_SYNC_HEAD)
  458. printk(KERN_WARNING
  459. "%s: Command failed. Please try your request again. \n",
  460. __func__);
  461. else if (res)
  462. dev_dbg(&ps3av->dev->core, "ps3av_cmd_avb_param failed\n");
  463. }
  464. static void ps3av_set_videomode_cont(u32 id, u32 old_id)
  465. {
  466. static int vesa;
  467. int res;
  468. /* video signal off */
  469. ps3av_set_video_disable_sig();
  470. /*
  471. * AV backend needs non-VESA mode setting at least one time
  472. * when VESA mode is used.
  473. */
  474. if (vesa == 0 && (id & PS3AV_MODE_MASK) >= PS3AV_MODE_WXGA) {
  475. /* vesa mode */
  476. ps3av_set_videomode_packet(PS3AV_MODE_480P);
  477. }
  478. vesa = 1;
  479. /* Retail PS3 product doesn't support this */
  480. if (id & PS3AV_MODE_HDCP_OFF) {
  481. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_HDCP_OFF);
  482. if (res == PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  483. dev_dbg(&ps3av->dev->core, "Not supported\n");
  484. else if (res)
  485. dev_dbg(&ps3av->dev->core,
  486. "ps3av_cmd_av_hdmi_mode failed\n");
  487. } else if (old_id & PS3AV_MODE_HDCP_OFF) {
  488. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_MODE_NORMAL);
  489. if (res < 0 && res != PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  490. dev_dbg(&ps3av->dev->core,
  491. "ps3av_cmd_av_hdmi_mode failed\n");
  492. }
  493. ps3av_set_videomode_packet(id);
  494. msleep(1500);
  495. /* av video mute */
  496. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_OFF);
  497. }
  498. static void ps3avd(struct work_struct *work)
  499. {
  500. ps3av_set_videomode_cont(ps3av->ps3av_mode, ps3av->ps3av_mode_old);
  501. complete(&ps3av->done);
  502. }
  503. #define SHIFT_50 0
  504. #define SHIFT_60 4
  505. #define SHIFT_VESA 8
  506. static const struct {
  507. unsigned mask : 19;
  508. unsigned id : 4;
  509. } ps3av_preferred_modes[] = {
  510. { PS3AV_RESBIT_WUXGA << SHIFT_VESA, PS3AV_MODE_WUXGA },
  511. { PS3AV_RESBIT_1920x1080P << SHIFT_60, PS3AV_MODE_1080P60 },
  512. { PS3AV_RESBIT_1920x1080P << SHIFT_50, PS3AV_MODE_1080P50 },
  513. { PS3AV_RESBIT_1920x1080I << SHIFT_60, PS3AV_MODE_1080I60 },
  514. { PS3AV_RESBIT_1920x1080I << SHIFT_50, PS3AV_MODE_1080I50 },
  515. { PS3AV_RESBIT_SXGA << SHIFT_VESA, PS3AV_MODE_SXGA },
  516. { PS3AV_RESBIT_WXGA << SHIFT_VESA, PS3AV_MODE_WXGA },
  517. { PS3AV_RESBIT_1280x720P << SHIFT_60, PS3AV_MODE_720P60 },
  518. { PS3AV_RESBIT_1280x720P << SHIFT_50, PS3AV_MODE_720P50 },
  519. { PS3AV_RESBIT_720x480P << SHIFT_60, PS3AV_MODE_480P },
  520. { PS3AV_RESBIT_720x576P << SHIFT_50, PS3AV_MODE_576P },
  521. };
  522. static enum ps3av_mode_num ps3av_resbit2id(u32 res_50, u32 res_60,
  523. u32 res_vesa)
  524. {
  525. unsigned int i;
  526. u32 res_all;
  527. /*
  528. * We mask off the resolution bits we care about and combine the
  529. * results in one bitfield, so make sure there's no overlap
  530. */
  531. BUILD_BUG_ON(PS3AV_RES_MASK_50 << SHIFT_50 &
  532. PS3AV_RES_MASK_60 << SHIFT_60);
  533. BUILD_BUG_ON(PS3AV_RES_MASK_50 << SHIFT_50 &
  534. PS3AV_RES_MASK_VESA << SHIFT_VESA);
  535. BUILD_BUG_ON(PS3AV_RES_MASK_60 << SHIFT_60 &
  536. PS3AV_RES_MASK_VESA << SHIFT_VESA);
  537. res_all = (res_50 & PS3AV_RES_MASK_50) << SHIFT_50 |
  538. (res_60 & PS3AV_RES_MASK_60) << SHIFT_60 |
  539. (res_vesa & PS3AV_RES_MASK_VESA) << SHIFT_VESA;
  540. if (!res_all)
  541. return 0;
  542. for (i = 0; i < ARRAY_SIZE(ps3av_preferred_modes); i++)
  543. if (res_all & ps3av_preferred_modes[i].mask)
  544. return ps3av_preferred_modes[i].id;
  545. return 0;
  546. }
  547. static enum ps3av_mode_num ps3av_hdmi_get_id(struct ps3av_info_monitor *info)
  548. {
  549. enum ps3av_mode_num id;
  550. if (safe_mode)
  551. return PS3AV_DEFAULT_HDMI_MODE_ID_REG_60;
  552. /* check native resolution */
  553. id = ps3av_resbit2id(info->res_50.native, info->res_60.native,
  554. info->res_vesa.native);
  555. if (id) {
  556. pr_debug("%s: Using native mode %d\n", __func__, id);
  557. return id;
  558. }
  559. /* check supported resolutions */
  560. id = ps3av_resbit2id(info->res_50.res_bits, info->res_60.res_bits,
  561. info->res_vesa.res_bits);
  562. if (id) {
  563. pr_debug("%s: Using supported mode %d\n", __func__, id);
  564. return id;
  565. }
  566. if (ps3av->region & PS3AV_REGION_60)
  567. id = PS3AV_DEFAULT_HDMI_MODE_ID_REG_60;
  568. else
  569. id = PS3AV_DEFAULT_HDMI_MODE_ID_REG_50;
  570. pr_debug("%s: Using default mode %d\n", __func__, id);
  571. return id;
  572. }
  573. static void ps3av_monitor_info_dump(const struct ps3av_pkt_av_get_monitor_info *monitor_info)
  574. {
  575. const struct ps3av_info_monitor *info = &monitor_info->info;
  576. const struct ps3av_info_audio *audio = info->audio;
  577. char id[sizeof(info->monitor_id)*3+1];
  578. int i;
  579. pr_debug("Monitor Info: size %u\n", monitor_info->send_hdr.size);
  580. pr_debug("avport: %02x\n", info->avport);
  581. for (i = 0; i < sizeof(info->monitor_id); i++)
  582. sprintf(&id[i*3], " %02x", info->monitor_id[i]);
  583. pr_debug("monitor_id: %s\n", id);
  584. pr_debug("monitor_type: %02x\n", info->monitor_type);
  585. pr_debug("monitor_name: %.*s\n", (int)sizeof(info->monitor_name),
  586. info->monitor_name);
  587. /* resolution */
  588. pr_debug("resolution_60: bits: %08x native: %08x\n",
  589. info->res_60.res_bits, info->res_60.native);
  590. pr_debug("resolution_50: bits: %08x native: %08x\n",
  591. info->res_50.res_bits, info->res_50.native);
  592. pr_debug("resolution_other: bits: %08x native: %08x\n",
  593. info->res_other.res_bits, info->res_other.native);
  594. pr_debug("resolution_vesa: bits: %08x native: %08x\n",
  595. info->res_vesa.res_bits, info->res_vesa.native);
  596. /* color space */
  597. pr_debug("color space rgb: %02x\n", info->cs.rgb);
  598. pr_debug("color space yuv444: %02x\n", info->cs.yuv444);
  599. pr_debug("color space yuv422: %02x\n", info->cs.yuv422);
  600. /* color info */
  601. pr_debug("color info red: X %04x Y %04x\n", info->color.red_x,
  602. info->color.red_y);
  603. pr_debug("color info green: X %04x Y %04x\n", info->color.green_x,
  604. info->color.green_y);
  605. pr_debug("color info blue: X %04x Y %04x\n", info->color.blue_x,
  606. info->color.blue_y);
  607. pr_debug("color info white: X %04x Y %04x\n", info->color.white_x,
  608. info->color.white_y);
  609. pr_debug("color info gamma: %08x\n", info->color.gamma);
  610. /* other info */
  611. pr_debug("supported_AI: %02x\n", info->supported_ai);
  612. pr_debug("speaker_info: %02x\n", info->speaker_info);
  613. pr_debug("num of audio: %02x\n", info->num_of_audio_block);
  614. /* audio block */
  615. for (i = 0; i < info->num_of_audio_block; i++) {
  616. pr_debug("audio[%d] type: %02x max_ch: %02x fs: %02x sbit: "
  617. "%02x\n",
  618. i, audio->type, audio->max_num_of_ch, audio->fs,
  619. audio->sbit);
  620. audio++;
  621. }
  622. }
  623. static const struct ps3av_monitor_quirk {
  624. const char *monitor_name;
  625. u32 clear_60;
  626. } ps3av_monitor_quirks[] = {
  627. {
  628. .monitor_name = "DELL 2007WFP",
  629. .clear_60 = PS3AV_RESBIT_1920x1080I
  630. }, {
  631. .monitor_name = "L226WTQ",
  632. .clear_60 = PS3AV_RESBIT_1920x1080I |
  633. PS3AV_RESBIT_1920x1080P
  634. }, {
  635. .monitor_name = "SyncMaster",
  636. .clear_60 = PS3AV_RESBIT_1920x1080I
  637. }
  638. };
  639. static void ps3av_fixup_monitor_info(struct ps3av_info_monitor *info)
  640. {
  641. unsigned int i;
  642. const struct ps3av_monitor_quirk *quirk;
  643. for (i = 0; i < ARRAY_SIZE(ps3av_monitor_quirks); i++) {
  644. quirk = &ps3av_monitor_quirks[i];
  645. if (!strncmp(info->monitor_name, quirk->monitor_name,
  646. sizeof(info->monitor_name))) {
  647. pr_info("%s: Applying quirk for %s\n", __func__,
  648. quirk->monitor_name);
  649. info->res_60.res_bits &= ~quirk->clear_60;
  650. info->res_60.native &= ~quirk->clear_60;
  651. break;
  652. }
  653. }
  654. }
  655. static int ps3av_auto_videomode(struct ps3av_pkt_av_get_hw_conf *av_hw_conf)
  656. {
  657. int i, res, id = 0, dvi = 0, rgb = 0;
  658. struct ps3av_pkt_av_get_monitor_info monitor_info;
  659. struct ps3av_info_monitor *info;
  660. /* get mode id for hdmi */
  661. for (i = 0; i < av_hw_conf->num_of_hdmi && !id; i++) {
  662. res = ps3av_cmd_video_get_monitor_info(&monitor_info,
  663. PS3AV_CMD_AVPORT_HDMI_0 +
  664. i);
  665. if (res < 0)
  666. return -1;
  667. ps3av_monitor_info_dump(&monitor_info);
  668. info = &monitor_info.info;
  669. ps3av_fixup_monitor_info(info);
  670. switch (info->monitor_type) {
  671. case PS3AV_MONITOR_TYPE_DVI:
  672. dvi = PS3AV_MODE_DVI;
  673. /* fall through */
  674. case PS3AV_MONITOR_TYPE_HDMI:
  675. id = ps3av_hdmi_get_id(info);
  676. break;
  677. }
  678. }
  679. if (!id) {
  680. /* no HDMI interface or HDMI is off */
  681. if (ps3av->region & PS3AV_REGION_60)
  682. id = PS3AV_DEFAULT_AVMULTI_MODE_ID_REG_60;
  683. else
  684. id = PS3AV_DEFAULT_AVMULTI_MODE_ID_REG_50;
  685. if (ps3av->region & PS3AV_REGION_RGB)
  686. rgb = PS3AV_MODE_RGB;
  687. pr_debug("%s: Using avmulti mode %d\n", __func__, id);
  688. }
  689. return id | dvi | rgb;
  690. }
  691. static int ps3av_get_hw_conf(struct ps3av *ps3av)
  692. {
  693. int i, j, k, res;
  694. const struct ps3av_pkt_av_get_hw_conf *hw_conf;
  695. /* get av_hw_conf */
  696. res = ps3av_cmd_av_get_hw_conf(&ps3av->av_hw_conf);
  697. if (res < 0)
  698. return -1;
  699. hw_conf = &ps3av->av_hw_conf;
  700. pr_debug("av_h_conf: num of hdmi: %u\n", hw_conf->num_of_hdmi);
  701. pr_debug("av_h_conf: num of avmulti: %u\n", hw_conf->num_of_avmulti);
  702. pr_debug("av_h_conf: num of spdif: %u\n", hw_conf->num_of_spdif);
  703. for (i = 0; i < PS3AV_HEAD_MAX; i++)
  704. ps3av->head[i] = PS3AV_CMD_VIDEO_HEAD_A + i;
  705. for (i = 0; i < PS3AV_OPT_PORT_MAX; i++)
  706. ps3av->opt_port[i] = PS3AV_CMD_AVPORT_SPDIF_0 + i;
  707. for (i = 0; i < hw_conf->num_of_hdmi; i++)
  708. ps3av->av_port[i] = PS3AV_CMD_AVPORT_HDMI_0 + i;
  709. for (j = 0; j < hw_conf->num_of_avmulti; j++)
  710. ps3av->av_port[i + j] = PS3AV_CMD_AVPORT_AVMULTI_0 + j;
  711. for (k = 0; k < hw_conf->num_of_spdif; k++)
  712. ps3av->av_port[i + j + k] = PS3AV_CMD_AVPORT_SPDIF_0 + k;
  713. /* set all audio port */
  714. ps3av->audio_port = PS3AV_CMD_AUDIO_PORT_HDMI_0
  715. | PS3AV_CMD_AUDIO_PORT_HDMI_1
  716. | PS3AV_CMD_AUDIO_PORT_AVMULTI_0
  717. | PS3AV_CMD_AUDIO_PORT_SPDIF_0 | PS3AV_CMD_AUDIO_PORT_SPDIF_1;
  718. return 0;
  719. }
  720. /* set mode using id */
  721. int ps3av_set_video_mode(u32 id)
  722. {
  723. int size;
  724. u32 option;
  725. size = ARRAY_SIZE(video_mode_table);
  726. if ((id & PS3AV_MODE_MASK) > size - 1 || id < 0) {
  727. dev_dbg(&ps3av->dev->core, "%s: error id :%d\n", __func__, id);
  728. return -EINVAL;
  729. }
  730. /* auto mode */
  731. option = id & ~PS3AV_MODE_MASK;
  732. if ((id & PS3AV_MODE_MASK) == PS3AV_MODE_AUTO) {
  733. id = ps3av_auto_videomode(&ps3av->av_hw_conf);
  734. if (id < 1) {
  735. printk(KERN_ERR "%s: invalid id :%d\n", __func__, id);
  736. return -EINVAL;
  737. }
  738. id |= option;
  739. }
  740. /* set videomode */
  741. wait_for_completion(&ps3av->done);
  742. ps3av->ps3av_mode_old = ps3av->ps3av_mode;
  743. ps3av->ps3av_mode = id;
  744. if (ps3av_set_videomode())
  745. ps3av->ps3av_mode = ps3av->ps3av_mode_old;
  746. return 0;
  747. }
  748. EXPORT_SYMBOL_GPL(ps3av_set_video_mode);
  749. int ps3av_get_auto_mode(void)
  750. {
  751. return ps3av_auto_videomode(&ps3av->av_hw_conf);
  752. }
  753. EXPORT_SYMBOL_GPL(ps3av_get_auto_mode);
  754. int ps3av_get_mode(void)
  755. {
  756. return ps3av ? ps3av->ps3av_mode : 0;
  757. }
  758. EXPORT_SYMBOL_GPL(ps3av_get_mode);
  759. /* get resolution by video_mode */
  760. int ps3av_video_mode2res(u32 id, u32 *xres, u32 *yres)
  761. {
  762. int size;
  763. id = id & PS3AV_MODE_MASK;
  764. size = ARRAY_SIZE(video_mode_table);
  765. if (id > size - 1 || id < 0) {
  766. printk(KERN_ERR "%s: invalid mode %d\n", __func__, id);
  767. return -EINVAL;
  768. }
  769. *xres = video_mode_table[id].x;
  770. *yres = video_mode_table[id].y;
  771. return 0;
  772. }
  773. EXPORT_SYMBOL_GPL(ps3av_video_mode2res);
  774. /* mute */
  775. int ps3av_video_mute(int mute)
  776. {
  777. return ps3av_set_av_video_mute(mute ? PS3AV_CMD_MUTE_ON
  778. : PS3AV_CMD_MUTE_OFF);
  779. }
  780. EXPORT_SYMBOL_GPL(ps3av_video_mute);
  781. int ps3av_audio_mute(int mute)
  782. {
  783. return ps3av_set_audio_mute(mute ? PS3AV_CMD_MUTE_ON
  784. : PS3AV_CMD_MUTE_OFF);
  785. }
  786. EXPORT_SYMBOL_GPL(ps3av_audio_mute);
  787. void ps3av_register_flip_ctl(void (*flip_ctl)(int on, void *data),
  788. void *flip_data)
  789. {
  790. mutex_lock(&ps3av->mutex);
  791. ps3av->flip_ctl = flip_ctl;
  792. ps3av->flip_data = flip_data;
  793. mutex_unlock(&ps3av->mutex);
  794. }
  795. EXPORT_SYMBOL_GPL(ps3av_register_flip_ctl);
  796. void ps3av_flip_ctl(int on)
  797. {
  798. mutex_lock(&ps3av->mutex);
  799. if (ps3av->flip_ctl)
  800. ps3av->flip_ctl(on, ps3av->flip_data);
  801. mutex_unlock(&ps3av->mutex);
  802. }
  803. static int ps3av_probe(struct ps3_system_bus_device *dev)
  804. {
  805. int res;
  806. u32 id;
  807. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  808. dev_dbg(&dev->core, " timeout=%d\n", timeout);
  809. if (ps3av) {
  810. dev_err(&dev->core, "Only one ps3av device is supported\n");
  811. return -EBUSY;
  812. }
  813. ps3av = kzalloc(sizeof(*ps3av), GFP_KERNEL);
  814. if (!ps3av)
  815. return -ENOMEM;
  816. mutex_init(&ps3av->mutex);
  817. ps3av->ps3av_mode = PS3AV_MODE_AUTO;
  818. ps3av->dev = dev;
  819. INIT_WORK(&ps3av->work, ps3avd);
  820. init_completion(&ps3av->done);
  821. complete(&ps3av->done);
  822. ps3av->wq = create_singlethread_workqueue("ps3avd");
  823. if (!ps3av->wq)
  824. goto fail;
  825. switch (ps3_os_area_get_av_multi_out()) {
  826. case PS3_PARAM_AV_MULTI_OUT_NTSC:
  827. ps3av->region = PS3AV_REGION_60;
  828. break;
  829. case PS3_PARAM_AV_MULTI_OUT_PAL_YCBCR:
  830. case PS3_PARAM_AV_MULTI_OUT_SECAM:
  831. ps3av->region = PS3AV_REGION_50;
  832. break;
  833. case PS3_PARAM_AV_MULTI_OUT_PAL_RGB:
  834. ps3av->region = PS3AV_REGION_50 | PS3AV_REGION_RGB;
  835. break;
  836. default:
  837. ps3av->region = PS3AV_REGION_60;
  838. break;
  839. }
  840. /* init avsetting modules */
  841. res = ps3av_cmd_init();
  842. if (res < 0)
  843. printk(KERN_ERR "%s: ps3av_cmd_init failed %d\n", __func__,
  844. res);
  845. ps3av_get_hw_conf(ps3av);
  846. #ifdef CONFIG_FB
  847. if (fb_mode_option && !strcmp(fb_mode_option, "safe"))
  848. safe_mode = 1;
  849. #endif /* CONFIG_FB */
  850. id = ps3av_auto_videomode(&ps3av->av_hw_conf);
  851. safe_mode = 0;
  852. mutex_lock(&ps3av->mutex);
  853. ps3av->ps3av_mode = id;
  854. mutex_unlock(&ps3av->mutex);
  855. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  856. return 0;
  857. fail:
  858. kfree(ps3av);
  859. ps3av = NULL;
  860. return -ENOMEM;
  861. }
  862. static int ps3av_remove(struct ps3_system_bus_device *dev)
  863. {
  864. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  865. if (ps3av) {
  866. ps3av_cmd_fin();
  867. if (ps3av->wq)
  868. destroy_workqueue(ps3av->wq);
  869. kfree(ps3av);
  870. ps3av = NULL;
  871. }
  872. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  873. return 0;
  874. }
  875. static void ps3av_shutdown(struct ps3_system_bus_device *dev)
  876. {
  877. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  878. ps3av_remove(dev);
  879. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  880. }
  881. static struct ps3_vuart_port_driver ps3av_driver = {
  882. .core.match_id = PS3_MATCH_ID_AV_SETTINGS,
  883. .core.core.name = "ps3_av",
  884. .probe = ps3av_probe,
  885. .remove = ps3av_remove,
  886. .shutdown = ps3av_shutdown,
  887. };
  888. static int ps3av_module_init(void)
  889. {
  890. int error;
  891. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  892. return -ENODEV;
  893. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  894. error = ps3_vuart_port_driver_register(&ps3av_driver);
  895. if (error) {
  896. printk(KERN_ERR
  897. "%s: ps3_vuart_port_driver_register failed %d\n",
  898. __func__, error);
  899. return error;
  900. }
  901. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  902. return error;
  903. }
  904. static void __exit ps3av_module_exit(void)
  905. {
  906. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  907. ps3_vuart_port_driver_unregister(&ps3av_driver);
  908. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  909. }
  910. subsys_initcall(ps3av_module_init);
  911. module_exit(ps3av_module_exit);
  912. MODULE_LICENSE("GPL v2");
  913. MODULE_DESCRIPTION("PS3 AV Settings Driver");
  914. MODULE_AUTHOR("Sony Computer Entertainment Inc.");
  915. MODULE_ALIAS(PS3_MODULE_ALIAS_AV_SETTINGS);