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 <linux/slab.h>
  27. #include <asm/firmware.h>
  28. #include <asm/ps3av.h>
  29. #include <asm/ps3.h>
  30. #include "vuart.h"
  31. #define BUFSIZE 4096 /* vuart buf size */
  32. #define PS3AV_BUF_SIZE 512 /* max packet size */
  33. static int safe_mode;
  34. static int timeout = 5000; /* in msec ( 5 sec ) */
  35. module_param(timeout, int, 0644);
  36. static struct ps3av {
  37. struct mutex mutex;
  38. struct work_struct work;
  39. struct completion done;
  40. struct workqueue_struct *wq;
  41. int open_count;
  42. struct ps3_system_bus_device *dev;
  43. int region;
  44. struct ps3av_pkt_av_get_hw_conf av_hw_conf;
  45. u32 av_port[PS3AV_AV_PORT_MAX + PS3AV_OPT_PORT_MAX];
  46. u32 opt_port[PS3AV_OPT_PORT_MAX];
  47. u32 head[PS3AV_HEAD_MAX];
  48. u32 audio_port;
  49. int ps3av_mode;
  50. int ps3av_mode_old;
  51. union {
  52. struct ps3av_reply_hdr reply_hdr;
  53. u8 raw[PS3AV_BUF_SIZE];
  54. } recv_buf;
  55. } *ps3av;
  56. /* color space */
  57. #define YUV444 PS3AV_CMD_VIDEO_CS_YUV444_8
  58. #define RGB8 PS3AV_CMD_VIDEO_CS_RGB_8
  59. /* format */
  60. #define XRGB PS3AV_CMD_VIDEO_FMT_X8R8G8B8
  61. /* aspect */
  62. #define A_N PS3AV_CMD_AV_ASPECT_4_3
  63. #define A_W PS3AV_CMD_AV_ASPECT_16_9
  64. static const struct avset_video_mode {
  65. u32 cs;
  66. u32 fmt;
  67. u32 vid;
  68. u32 aspect;
  69. u32 x;
  70. u32 y;
  71. } video_mode_table[] = {
  72. { 0, }, /* auto */
  73. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480I, A_N, 720, 480},
  74. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_480P, A_N, 720, 480},
  75. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_60HZ, A_W, 1280, 720},
  76. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_60HZ, A_W, 1920, 1080},
  77. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_60HZ, A_W, 1920, 1080},
  78. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576I, A_N, 720, 576},
  79. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_576P, A_N, 720, 576},
  80. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_720P_50HZ, A_W, 1280, 720},
  81. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080I_50HZ, A_W, 1920, 1080},
  82. {YUV444, XRGB, PS3AV_CMD_VIDEO_VID_1080P_50HZ, A_W, 1920, 1080},
  83. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WXGA, A_W, 1280, 768},
  84. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_SXGA, A_N, 1280, 1024},
  85. { RGB8, XRGB, PS3AV_CMD_VIDEO_VID_WUXGA, A_W, 1920, 1200},
  86. };
  87. /* supported CIDs */
  88. static u32 cmd_table[] = {
  89. /* init */
  90. PS3AV_CID_AV_INIT,
  91. PS3AV_CID_AV_FIN,
  92. PS3AV_CID_VIDEO_INIT,
  93. PS3AV_CID_AUDIO_INIT,
  94. /* set */
  95. PS3AV_CID_AV_ENABLE_EVENT,
  96. PS3AV_CID_AV_DISABLE_EVENT,
  97. PS3AV_CID_AV_VIDEO_CS,
  98. PS3AV_CID_AV_VIDEO_MUTE,
  99. PS3AV_CID_AV_VIDEO_DISABLE_SIG,
  100. PS3AV_CID_AV_AUDIO_PARAM,
  101. PS3AV_CID_AV_AUDIO_MUTE,
  102. PS3AV_CID_AV_HDMI_MODE,
  103. PS3AV_CID_AV_TV_MUTE,
  104. PS3AV_CID_VIDEO_MODE,
  105. PS3AV_CID_VIDEO_FORMAT,
  106. PS3AV_CID_VIDEO_PITCH,
  107. PS3AV_CID_AUDIO_MODE,
  108. PS3AV_CID_AUDIO_MUTE,
  109. PS3AV_CID_AUDIO_ACTIVE,
  110. PS3AV_CID_AUDIO_INACTIVE,
  111. PS3AV_CID_AVB_PARAM,
  112. /* get */
  113. PS3AV_CID_AV_GET_HW_CONF,
  114. PS3AV_CID_AV_GET_MONITOR_INFO,
  115. /* event */
  116. PS3AV_CID_EVENT_UNPLUGGED,
  117. PS3AV_CID_EVENT_PLUGGED,
  118. PS3AV_CID_EVENT_HDCP_DONE,
  119. PS3AV_CID_EVENT_HDCP_FAIL,
  120. PS3AV_CID_EVENT_HDCP_AUTH,
  121. PS3AV_CID_EVENT_HDCP_ERROR,
  122. 0
  123. };
  124. #define PS3AV_EVENT_CMD_MASK 0x10000000
  125. #define PS3AV_EVENT_ID_MASK 0x0000ffff
  126. #define PS3AV_CID_MASK 0xffffffff
  127. #define PS3AV_REPLY_BIT 0x80000000
  128. #define ps3av_event_get_port_id(cid) ((cid >> 16) & 0xff)
  129. static u32 *ps3av_search_cmd_table(u32 cid, u32 mask)
  130. {
  131. u32 *table;
  132. int i;
  133. table = cmd_table;
  134. for (i = 0;; table++, i++) {
  135. if ((*table & mask) == (cid & mask))
  136. break;
  137. if (*table == 0)
  138. return NULL;
  139. }
  140. return table;
  141. }
  142. static int ps3av_parse_event_packet(const struct ps3av_reply_hdr *hdr)
  143. {
  144. u32 *table;
  145. if (hdr->cid & PS3AV_EVENT_CMD_MASK) {
  146. table = ps3av_search_cmd_table(hdr->cid, PS3AV_EVENT_CMD_MASK);
  147. if (table)
  148. dev_dbg(&ps3av->dev->core,
  149. "recv event packet cid:%08x port:0x%x size:%d\n",
  150. hdr->cid, ps3av_event_get_port_id(hdr->cid),
  151. hdr->size);
  152. else
  153. printk(KERN_ERR
  154. "%s: failed event packet, cid:%08x size:%d\n",
  155. __func__, hdr->cid, hdr->size);
  156. return 1; /* receive event packet */
  157. }
  158. return 0;
  159. }
  160. #define POLLING_INTERVAL 25 /* in msec */
  161. static int ps3av_vuart_write(struct ps3_system_bus_device *dev,
  162. const void *buf, unsigned long size)
  163. {
  164. int error;
  165. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  166. error = ps3_vuart_write(dev, buf, size);
  167. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  168. return error ? error : size;
  169. }
  170. static int ps3av_vuart_read(struct ps3_system_bus_device *dev, void *buf,
  171. unsigned long size, int timeout)
  172. {
  173. int error;
  174. int loopcnt = 0;
  175. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  176. timeout = (timeout + POLLING_INTERVAL - 1) / POLLING_INTERVAL;
  177. while (loopcnt++ <= timeout) {
  178. error = ps3_vuart_read(dev, buf, size);
  179. if (!error)
  180. return size;
  181. if (error != -EAGAIN) {
  182. printk(KERN_ERR "%s: ps3_vuart_read failed %d\n",
  183. __func__, error);
  184. return error;
  185. }
  186. msleep(POLLING_INTERVAL);
  187. }
  188. return -EWOULDBLOCK;
  189. }
  190. static int ps3av_send_cmd_pkt(const struct ps3av_send_hdr *send_buf,
  191. struct ps3av_reply_hdr *recv_buf, int write_len,
  192. int read_len)
  193. {
  194. int res;
  195. u32 cmd;
  196. int event;
  197. if (!ps3av)
  198. return -ENODEV;
  199. /* send pkt */
  200. res = ps3av_vuart_write(ps3av->dev, send_buf, write_len);
  201. if (res < 0) {
  202. dev_dbg(&ps3av->dev->core,
  203. "%s: ps3av_vuart_write() failed (result=%d)\n",
  204. __func__, res);
  205. return res;
  206. }
  207. /* recv pkt */
  208. cmd = send_buf->cid;
  209. do {
  210. /* read header */
  211. res = ps3av_vuart_read(ps3av->dev, recv_buf, PS3AV_HDR_SIZE,
  212. timeout);
  213. if (res != PS3AV_HDR_SIZE) {
  214. dev_dbg(&ps3av->dev->core,
  215. "%s: ps3av_vuart_read() failed (result=%d)\n",
  216. __func__, res);
  217. return res;
  218. }
  219. /* read body */
  220. res = ps3av_vuart_read(ps3av->dev, &recv_buf->cid,
  221. recv_buf->size, timeout);
  222. if (res < 0) {
  223. dev_dbg(&ps3av->dev->core,
  224. "%s: ps3av_vuart_read() failed (result=%d)\n",
  225. __func__, res);
  226. return res;
  227. }
  228. res += PS3AV_HDR_SIZE; /* total len */
  229. event = ps3av_parse_event_packet(recv_buf);
  230. /* ret > 0 event packet */
  231. } while (event);
  232. if ((cmd | PS3AV_REPLY_BIT) != recv_buf->cid) {
  233. dev_dbg(&ps3av->dev->core, "%s: reply err (result=%x)\n",
  234. __func__, recv_buf->cid);
  235. return -EINVAL;
  236. }
  237. return 0;
  238. }
  239. static int ps3av_process_reply_packet(struct ps3av_send_hdr *cmd_buf,
  240. const struct ps3av_reply_hdr *recv_buf,
  241. int user_buf_size)
  242. {
  243. int return_len;
  244. if (recv_buf->version != PS3AV_VERSION) {
  245. dev_dbg(&ps3av->dev->core, "reply_packet invalid version:%x\n",
  246. recv_buf->version);
  247. return -EFAULT;
  248. }
  249. return_len = recv_buf->size + PS3AV_HDR_SIZE;
  250. if (return_len > user_buf_size)
  251. return_len = user_buf_size;
  252. memcpy(cmd_buf, recv_buf, return_len);
  253. return 0; /* success */
  254. }
  255. void ps3av_set_hdr(u32 cid, u16 size, struct ps3av_send_hdr *hdr)
  256. {
  257. hdr->version = PS3AV_VERSION;
  258. hdr->size = size - PS3AV_HDR_SIZE;
  259. hdr->cid = cid;
  260. }
  261. int ps3av_do_pkt(u32 cid, u16 send_len, size_t usr_buf_size,
  262. struct ps3av_send_hdr *buf)
  263. {
  264. int res = 0;
  265. u32 *table;
  266. BUG_ON(!ps3av);
  267. mutex_lock(&ps3av->mutex);
  268. table = ps3av_search_cmd_table(cid, PS3AV_CID_MASK);
  269. BUG_ON(!table);
  270. BUG_ON(send_len < PS3AV_HDR_SIZE);
  271. BUG_ON(usr_buf_size < send_len);
  272. BUG_ON(usr_buf_size > PS3AV_BUF_SIZE);
  273. /* create header */
  274. ps3av_set_hdr(cid, send_len, buf);
  275. /* send packet via vuart */
  276. res = ps3av_send_cmd_pkt(buf, &ps3av->recv_buf.reply_hdr, send_len,
  277. usr_buf_size);
  278. if (res < 0) {
  279. printk(KERN_ERR
  280. "%s: ps3av_send_cmd_pkt() failed (result=%d)\n",
  281. __func__, res);
  282. goto err;
  283. }
  284. /* process reply packet */
  285. res = ps3av_process_reply_packet(buf, &ps3av->recv_buf.reply_hdr,
  286. usr_buf_size);
  287. if (res < 0) {
  288. printk(KERN_ERR "%s: put_return_status() failed (result=%d)\n",
  289. __func__, res);
  290. goto err;
  291. }
  292. mutex_unlock(&ps3av->mutex);
  293. return 0;
  294. err:
  295. mutex_unlock(&ps3av->mutex);
  296. printk(KERN_ERR "%s: failed cid:%x res:%d\n", __func__, cid, res);
  297. return res;
  298. }
  299. static int ps3av_set_av_video_mute(u32 mute)
  300. {
  301. int i, num_of_av_port, res;
  302. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  303. ps3av->av_hw_conf.num_of_avmulti;
  304. /* video mute on */
  305. for (i = 0; i < num_of_av_port; i++) {
  306. res = ps3av_cmd_av_video_mute(1, &ps3av->av_port[i], mute);
  307. if (res < 0)
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. static int ps3av_set_video_disable_sig(void)
  313. {
  314. int i, num_of_hdmi_port, num_of_av_port, res;
  315. num_of_hdmi_port = ps3av->av_hw_conf.num_of_hdmi;
  316. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  317. ps3av->av_hw_conf.num_of_avmulti;
  318. /* tv mute */
  319. for (i = 0; i < num_of_hdmi_port; i++) {
  320. res = ps3av_cmd_av_tv_mute(ps3av->av_port[i],
  321. PS3AV_CMD_MUTE_ON);
  322. if (res < 0)
  323. return -1;
  324. }
  325. msleep(100);
  326. /* video mute on */
  327. for (i = 0; i < num_of_av_port; i++) {
  328. res = ps3av_cmd_av_video_disable_sig(ps3av->av_port[i]);
  329. if (res < 0)
  330. return -1;
  331. if (i < num_of_hdmi_port) {
  332. res = ps3av_cmd_av_tv_mute(ps3av->av_port[i],
  333. PS3AV_CMD_MUTE_OFF);
  334. if (res < 0)
  335. return -1;
  336. }
  337. }
  338. msleep(300);
  339. return 0;
  340. }
  341. static int ps3av_set_audio_mute(u32 mute)
  342. {
  343. int i, num_of_av_port, num_of_opt_port, res;
  344. num_of_av_port = ps3av->av_hw_conf.num_of_hdmi +
  345. ps3av->av_hw_conf.num_of_avmulti;
  346. num_of_opt_port = ps3av->av_hw_conf.num_of_spdif;
  347. for (i = 0; i < num_of_av_port; i++) {
  348. res = ps3av_cmd_av_audio_mute(1, &ps3av->av_port[i], mute);
  349. if (res < 0)
  350. return -1;
  351. }
  352. for (i = 0; i < num_of_opt_port; i++) {
  353. res = ps3av_cmd_audio_mute(1, &ps3av->opt_port[i], mute);
  354. if (res < 0)
  355. return -1;
  356. }
  357. return 0;
  358. }
  359. int ps3av_set_audio_mode(u32 ch, u32 fs, u32 word_bits, u32 format, u32 source)
  360. {
  361. struct ps3av_pkt_avb_param avb_param;
  362. int i, num_of_audio, vid, res;
  363. struct ps3av_pkt_audio_mode audio_mode;
  364. u32 len = 0;
  365. num_of_audio = ps3av->av_hw_conf.num_of_hdmi +
  366. ps3av->av_hw_conf.num_of_avmulti +
  367. ps3av->av_hw_conf.num_of_spdif;
  368. avb_param.num_of_video_pkt = 0;
  369. avb_param.num_of_audio_pkt = PS3AV_AVB_NUM_AUDIO; /* always 0 */
  370. avb_param.num_of_av_video_pkt = 0;
  371. avb_param.num_of_av_audio_pkt = ps3av->av_hw_conf.num_of_hdmi;
  372. vid = video_mode_table[ps3av->ps3av_mode].vid;
  373. /* audio mute */
  374. ps3av_set_audio_mute(PS3AV_CMD_MUTE_ON);
  375. /* audio inactive */
  376. res = ps3av_cmd_audio_active(0, ps3av->audio_port);
  377. if (res < 0)
  378. dev_dbg(&ps3av->dev->core,
  379. "ps3av_cmd_audio_active OFF failed\n");
  380. /* audio_pkt */
  381. for (i = 0; i < num_of_audio; i++) {
  382. ps3av_cmd_set_audio_mode(&audio_mode, ps3av->av_port[i], ch,
  383. fs, word_bits, format, source);
  384. if (i < ps3av->av_hw_conf.num_of_hdmi) {
  385. /* hdmi only */
  386. len += ps3av_cmd_set_av_audio_param(&avb_param.buf[len],
  387. ps3av->av_port[i],
  388. &audio_mode, vid);
  389. }
  390. /* audio_mode pkt should be sent separately */
  391. res = ps3av_cmd_audio_mode(&audio_mode);
  392. if (res < 0)
  393. dev_dbg(&ps3av->dev->core,
  394. "ps3av_cmd_audio_mode failed, port:%x\n", i);
  395. }
  396. /* send command using avb pkt */
  397. len += offsetof(struct ps3av_pkt_avb_param, buf);
  398. res = ps3av_cmd_avb_param(&avb_param, len);
  399. if (res < 0)
  400. dev_dbg(&ps3av->dev->core, "ps3av_cmd_avb_param failed\n");
  401. /* audio mute */
  402. ps3av_set_audio_mute(PS3AV_CMD_MUTE_OFF);
  403. /* audio active */
  404. res = ps3av_cmd_audio_active(1, ps3av->audio_port);
  405. if (res < 0)
  406. dev_dbg(&ps3av->dev->core,
  407. "ps3av_cmd_audio_active ON failed\n");
  408. return 0;
  409. }
  410. EXPORT_SYMBOL_GPL(ps3av_set_audio_mode);
  411. static int ps3av_set_videomode(void)
  412. {
  413. /* av video mute */
  414. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_ON);
  415. /* wake up ps3avd to do the actual video mode setting */
  416. queue_work(ps3av->wq, &ps3av->work);
  417. return 0;
  418. }
  419. static void ps3av_set_videomode_packet(u32 id)
  420. {
  421. struct ps3av_pkt_avb_param avb_param;
  422. unsigned int i;
  423. u32 len = 0, av_video_cs;
  424. const struct avset_video_mode *video_mode;
  425. int res;
  426. video_mode = &video_mode_table[id & PS3AV_MODE_MASK];
  427. avb_param.num_of_video_pkt = PS3AV_AVB_NUM_VIDEO; /* num of head */
  428. avb_param.num_of_audio_pkt = 0;
  429. avb_param.num_of_av_video_pkt = ps3av->av_hw_conf.num_of_hdmi +
  430. ps3av->av_hw_conf.num_of_avmulti;
  431. avb_param.num_of_av_audio_pkt = 0;
  432. /* video_pkt */
  433. for (i = 0; i < avb_param.num_of_video_pkt; i++)
  434. len += ps3av_cmd_set_video_mode(&avb_param.buf[len],
  435. ps3av->head[i], video_mode->vid,
  436. video_mode->fmt, id);
  437. /* av_video_pkt */
  438. for (i = 0; i < avb_param.num_of_av_video_pkt; i++) {
  439. if (id & PS3AV_MODE_DVI || id & PS3AV_MODE_RGB)
  440. av_video_cs = RGB8;
  441. else
  442. av_video_cs = video_mode->cs;
  443. #ifndef PS3AV_HDMI_YUV
  444. if (ps3av->av_port[i] == PS3AV_CMD_AVPORT_HDMI_0 ||
  445. ps3av->av_port[i] == PS3AV_CMD_AVPORT_HDMI_1)
  446. av_video_cs = RGB8; /* use RGB for HDMI */
  447. #endif
  448. len += ps3av_cmd_set_av_video_cs(&avb_param.buf[len],
  449. ps3av->av_port[i],
  450. video_mode->vid, av_video_cs,
  451. video_mode->aspect, id);
  452. }
  453. /* send command using avb pkt */
  454. len += offsetof(struct ps3av_pkt_avb_param, buf);
  455. res = ps3av_cmd_avb_param(&avb_param, len);
  456. if (res == PS3AV_STATUS_NO_SYNC_HEAD)
  457. printk(KERN_WARNING
  458. "%s: Command failed. Please try your request again.\n",
  459. __func__);
  460. else if (res)
  461. dev_dbg(&ps3av->dev->core, "ps3av_cmd_avb_param failed\n");
  462. }
  463. static void ps3av_set_videomode_cont(u32 id, u32 old_id)
  464. {
  465. static int vesa;
  466. int res;
  467. /* video signal off */
  468. ps3av_set_video_disable_sig();
  469. /*
  470. * AV backend needs non-VESA mode setting at least one time
  471. * when VESA mode is used.
  472. */
  473. if (vesa == 0 && (id & PS3AV_MODE_MASK) >= PS3AV_MODE_WXGA) {
  474. /* vesa mode */
  475. ps3av_set_videomode_packet(PS3AV_MODE_480P);
  476. }
  477. vesa = 1;
  478. /* Retail PS3 product doesn't support this */
  479. if (id & PS3AV_MODE_HDCP_OFF) {
  480. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_HDCP_OFF);
  481. if (res == PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  482. dev_dbg(&ps3av->dev->core, "Not supported\n");
  483. else if (res)
  484. dev_dbg(&ps3av->dev->core,
  485. "ps3av_cmd_av_hdmi_mode failed\n");
  486. } else if (old_id & PS3AV_MODE_HDCP_OFF) {
  487. res = ps3av_cmd_av_hdmi_mode(PS3AV_CMD_AV_HDMI_MODE_NORMAL);
  488. if (res < 0 && res != PS3AV_STATUS_UNSUPPORTED_HDMI_MODE)
  489. dev_dbg(&ps3av->dev->core,
  490. "ps3av_cmd_av_hdmi_mode failed\n");
  491. }
  492. ps3av_set_videomode_packet(id);
  493. msleep(1500);
  494. /* av video mute */
  495. ps3av_set_av_video_mute(PS3AV_CMD_MUTE_OFF);
  496. }
  497. static void ps3avd(struct work_struct *work)
  498. {
  499. ps3av_set_videomode_cont(ps3av->ps3av_mode, ps3av->ps3av_mode_old);
  500. complete(&ps3av->done);
  501. }
  502. #define SHIFT_50 0
  503. #define SHIFT_60 4
  504. #define SHIFT_VESA 8
  505. static const struct {
  506. unsigned mask:19;
  507. unsigned id:4;
  508. } ps3av_preferred_modes[] = {
  509. { PS3AV_RESBIT_WUXGA << SHIFT_VESA, PS3AV_MODE_WUXGA },
  510. { PS3AV_RESBIT_1920x1080P << SHIFT_60, PS3AV_MODE_1080P60 },
  511. { PS3AV_RESBIT_1920x1080P << SHIFT_50, PS3AV_MODE_1080P50 },
  512. { PS3AV_RESBIT_1920x1080I << SHIFT_60, PS3AV_MODE_1080I60 },
  513. { PS3AV_RESBIT_1920x1080I << SHIFT_50, PS3AV_MODE_1080I50 },
  514. { PS3AV_RESBIT_SXGA << SHIFT_VESA, PS3AV_MODE_SXGA },
  515. { PS3AV_RESBIT_WXGA << SHIFT_VESA, PS3AV_MODE_WXGA },
  516. { PS3AV_RESBIT_1280x720P << SHIFT_60, PS3AV_MODE_720P60 },
  517. { PS3AV_RESBIT_1280x720P << SHIFT_50, PS3AV_MODE_720P50 },
  518. { PS3AV_RESBIT_720x480P << SHIFT_60, PS3AV_MODE_480P },
  519. { PS3AV_RESBIT_720x576P << SHIFT_50, PS3AV_MODE_576P },
  520. };
  521. static enum ps3av_mode_num ps3av_resbit2id(u32 res_50, u32 res_60,
  522. u32 res_vesa)
  523. {
  524. unsigned int i;
  525. u32 res_all;
  526. /*
  527. * We mask off the resolution bits we care about and combine the
  528. * results in one bitfield, so make sure there's no overlap
  529. */
  530. BUILD_BUG_ON(PS3AV_RES_MASK_50 << SHIFT_50 &
  531. PS3AV_RES_MASK_60 << SHIFT_60);
  532. BUILD_BUG_ON(PS3AV_RES_MASK_50 << SHIFT_50 &
  533. PS3AV_RES_MASK_VESA << SHIFT_VESA);
  534. BUILD_BUG_ON(PS3AV_RES_MASK_60 << SHIFT_60 &
  535. PS3AV_RES_MASK_VESA << SHIFT_VESA);
  536. res_all = (res_50 & PS3AV_RES_MASK_50) << SHIFT_50 |
  537. (res_60 & PS3AV_RES_MASK_60) << SHIFT_60 |
  538. (res_vesa & PS3AV_RES_MASK_VESA) << SHIFT_VESA;
  539. if (!res_all)
  540. return 0;
  541. for (i = 0; i < ARRAY_SIZE(ps3av_preferred_modes); i++)
  542. if (res_all & ps3av_preferred_modes[i].mask)
  543. return ps3av_preferred_modes[i].id;
  544. return 0;
  545. }
  546. static enum ps3av_mode_num ps3av_hdmi_get_id(struct ps3av_info_monitor *info)
  547. {
  548. enum ps3av_mode_num id;
  549. if (safe_mode)
  550. return PS3AV_DEFAULT_HDMI_MODE_ID_REG_60;
  551. /* check native resolution */
  552. id = ps3av_resbit2id(info->res_50.native, info->res_60.native,
  553. info->res_vesa.native);
  554. if (id) {
  555. pr_debug("%s: Using native mode %d\n", __func__, id);
  556. return id;
  557. }
  558. /* check supported resolutions */
  559. id = ps3av_resbit2id(info->res_50.res_bits, info->res_60.res_bits,
  560. info->res_vesa.res_bits);
  561. if (id) {
  562. pr_debug("%s: Using supported mode %d\n", __func__, id);
  563. return id;
  564. }
  565. if (ps3av->region & PS3AV_REGION_60)
  566. id = PS3AV_DEFAULT_HDMI_MODE_ID_REG_60;
  567. else
  568. id = PS3AV_DEFAULT_HDMI_MODE_ID_REG_50;
  569. pr_debug("%s: Using default mode %d\n", __func__, id);
  570. return id;
  571. }
  572. static void ps3av_monitor_info_dump(
  573. 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(
  617. "audio[%d] type: %02x max_ch: %02x fs: %02x sbit: %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(int 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. /* mute analog output only */
  782. int ps3av_audio_mute_analog(int mute)
  783. {
  784. int i, res;
  785. for (i = 0; i < ps3av->av_hw_conf.num_of_avmulti; i++) {
  786. res = ps3av_cmd_av_audio_mute(1,
  787. &ps3av->av_port[i + ps3av->av_hw_conf.num_of_hdmi],
  788. mute);
  789. if (res < 0)
  790. return -1;
  791. }
  792. return 0;
  793. }
  794. EXPORT_SYMBOL_GPL(ps3av_audio_mute_analog);
  795. int ps3av_audio_mute(int mute)
  796. {
  797. return ps3av_set_audio_mute(mute ? PS3AV_CMD_MUTE_ON
  798. : PS3AV_CMD_MUTE_OFF);
  799. }
  800. EXPORT_SYMBOL_GPL(ps3av_audio_mute);
  801. static int ps3av_probe(struct ps3_system_bus_device *dev)
  802. {
  803. int res;
  804. int id;
  805. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  806. dev_dbg(&dev->core, " timeout=%d\n", timeout);
  807. if (ps3av) {
  808. dev_err(&dev->core, "Only one ps3av device is supported\n");
  809. return -EBUSY;
  810. }
  811. ps3av = kzalloc(sizeof(*ps3av), GFP_KERNEL);
  812. if (!ps3av)
  813. return -ENOMEM;
  814. mutex_init(&ps3av->mutex);
  815. ps3av->ps3av_mode = PS3AV_MODE_AUTO;
  816. ps3av->dev = dev;
  817. INIT_WORK(&ps3av->work, ps3avd);
  818. init_completion(&ps3av->done);
  819. complete(&ps3av->done);
  820. ps3av->wq = create_singlethread_workqueue("ps3avd");
  821. if (!ps3av->wq) {
  822. res = -ENOMEM;
  823. goto fail;
  824. }
  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. if (id < 0) {
  852. printk(KERN_ERR "%s: invalid id :%d\n", __func__, id);
  853. res = -EINVAL;
  854. goto fail;
  855. }
  856. safe_mode = 0;
  857. mutex_lock(&ps3av->mutex);
  858. ps3av->ps3av_mode = id;
  859. mutex_unlock(&ps3av->mutex);
  860. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  861. return 0;
  862. fail:
  863. kfree(ps3av);
  864. ps3av = NULL;
  865. return res;
  866. }
  867. static int ps3av_remove(struct ps3_system_bus_device *dev)
  868. {
  869. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  870. if (ps3av) {
  871. ps3av_cmd_fin();
  872. if (ps3av->wq)
  873. destroy_workqueue(ps3av->wq);
  874. kfree(ps3av);
  875. ps3av = NULL;
  876. }
  877. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  878. return 0;
  879. }
  880. static void ps3av_shutdown(struct ps3_system_bus_device *dev)
  881. {
  882. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  883. ps3av_remove(dev);
  884. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  885. }
  886. static struct ps3_vuart_port_driver ps3av_driver = {
  887. .core.match_id = PS3_MATCH_ID_AV_SETTINGS,
  888. .core.core.name = "ps3_av",
  889. .probe = ps3av_probe,
  890. .remove = ps3av_remove,
  891. .shutdown = ps3av_shutdown,
  892. };
  893. static int __init ps3av_module_init(void)
  894. {
  895. int error;
  896. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  897. return -ENODEV;
  898. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  899. error = ps3_vuart_port_driver_register(&ps3av_driver);
  900. if (error) {
  901. printk(KERN_ERR
  902. "%s: ps3_vuart_port_driver_register failed %d\n",
  903. __func__, error);
  904. return error;
  905. }
  906. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  907. return error;
  908. }
  909. static void __exit ps3av_module_exit(void)
  910. {
  911. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  912. ps3_vuart_port_driver_unregister(&ps3av_driver);
  913. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  914. }
  915. subsys_initcall(ps3av_module_init);
  916. module_exit(ps3av_module_exit);
  917. MODULE_LICENSE("GPL v2");
  918. MODULE_DESCRIPTION("PS3 AV Settings Driver");
  919. MODULE_AUTHOR("Sony Computer Entertainment Inc.");
  920. MODULE_ALIAS(PS3_MODULE_ALIAS_AV_SETTINGS);