ps3av.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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(const struct ps3av_pkt_av_get_monitor_info *monitor_info)
  573. {
  574. const struct ps3av_info_monitor *info = &monitor_info->info;
  575. const struct ps3av_info_audio *audio = info->audio;
  576. char id[sizeof(info->monitor_id)*3+1];
  577. int i;
  578. pr_debug("Monitor Info: size %u\n", monitor_info->send_hdr.size);
  579. pr_debug("avport: %02x\n", info->avport);
  580. for (i = 0; i < sizeof(info->monitor_id); i++)
  581. sprintf(&id[i*3], " %02x", info->monitor_id[i]);
  582. pr_debug("monitor_id: %s\n", id);
  583. pr_debug("monitor_type: %02x\n", info->monitor_type);
  584. pr_debug("monitor_name: %.*s\n", (int)sizeof(info->monitor_name),
  585. info->monitor_name);
  586. /* resolution */
  587. pr_debug("resolution_60: bits: %08x native: %08x\n",
  588. info->res_60.res_bits, info->res_60.native);
  589. pr_debug("resolution_50: bits: %08x native: %08x\n",
  590. info->res_50.res_bits, info->res_50.native);
  591. pr_debug("resolution_other: bits: %08x native: %08x\n",
  592. info->res_other.res_bits, info->res_other.native);
  593. pr_debug("resolution_vesa: bits: %08x native: %08x\n",
  594. info->res_vesa.res_bits, info->res_vesa.native);
  595. /* color space */
  596. pr_debug("color space rgb: %02x\n", info->cs.rgb);
  597. pr_debug("color space yuv444: %02x\n", info->cs.yuv444);
  598. pr_debug("color space yuv422: %02x\n", info->cs.yuv422);
  599. /* color info */
  600. pr_debug("color info red: X %04x Y %04x\n", info->color.red_x,
  601. info->color.red_y);
  602. pr_debug("color info green: X %04x Y %04x\n", info->color.green_x,
  603. info->color.green_y);
  604. pr_debug("color info blue: X %04x Y %04x\n", info->color.blue_x,
  605. info->color.blue_y);
  606. pr_debug("color info white: X %04x Y %04x\n", info->color.white_x,
  607. info->color.white_y);
  608. pr_debug("color info gamma: %08x\n", info->color.gamma);
  609. /* other info */
  610. pr_debug("supported_AI: %02x\n", info->supported_ai);
  611. pr_debug("speaker_info: %02x\n", info->speaker_info);
  612. pr_debug("num of audio: %02x\n", info->num_of_audio_block);
  613. /* audio block */
  614. for (i = 0; i < info->num_of_audio_block; i++) {
  615. pr_debug("audio[%d] type: %02x max_ch: %02x fs: %02x sbit: "
  616. "%02x\n",
  617. i, audio->type, audio->max_num_of_ch, audio->fs,
  618. audio->sbit);
  619. audio++;
  620. }
  621. }
  622. static const struct ps3av_monitor_quirk {
  623. const char *monitor_name;
  624. u32 clear_60;
  625. } ps3av_monitor_quirks[] = {
  626. {
  627. .monitor_name = "DELL 2007WFP",
  628. .clear_60 = PS3AV_RESBIT_1920x1080I
  629. }, {
  630. .monitor_name = "L226WTQ",
  631. .clear_60 = PS3AV_RESBIT_1920x1080I |
  632. PS3AV_RESBIT_1920x1080P
  633. }, {
  634. .monitor_name = "SyncMaster",
  635. .clear_60 = PS3AV_RESBIT_1920x1080I
  636. }
  637. };
  638. static void ps3av_fixup_monitor_info(struct ps3av_info_monitor *info)
  639. {
  640. unsigned int i;
  641. const struct ps3av_monitor_quirk *quirk;
  642. for (i = 0; i < ARRAY_SIZE(ps3av_monitor_quirks); i++) {
  643. quirk = &ps3av_monitor_quirks[i];
  644. if (!strncmp(info->monitor_name, quirk->monitor_name,
  645. sizeof(info->monitor_name))) {
  646. pr_info("%s: Applying quirk for %s\n", __func__,
  647. quirk->monitor_name);
  648. info->res_60.res_bits &= ~quirk->clear_60;
  649. info->res_60.native &= ~quirk->clear_60;
  650. break;
  651. }
  652. }
  653. }
  654. static int ps3av_auto_videomode(struct ps3av_pkt_av_get_hw_conf *av_hw_conf)
  655. {
  656. int i, res, id = 0, dvi = 0, rgb = 0;
  657. struct ps3av_pkt_av_get_monitor_info monitor_info;
  658. struct ps3av_info_monitor *info;
  659. /* get mode id for hdmi */
  660. for (i = 0; i < av_hw_conf->num_of_hdmi && !id; i++) {
  661. res = ps3av_cmd_video_get_monitor_info(&monitor_info,
  662. PS3AV_CMD_AVPORT_HDMI_0 +
  663. i);
  664. if (res < 0)
  665. return -1;
  666. ps3av_monitor_info_dump(&monitor_info);
  667. info = &monitor_info.info;
  668. ps3av_fixup_monitor_info(info);
  669. switch (info->monitor_type) {
  670. case PS3AV_MONITOR_TYPE_DVI:
  671. dvi = PS3AV_MODE_DVI;
  672. /* fall through */
  673. case PS3AV_MONITOR_TYPE_HDMI:
  674. id = ps3av_hdmi_get_id(info);
  675. break;
  676. }
  677. }
  678. if (!id) {
  679. /* no HDMI interface or HDMI is off */
  680. if (ps3av->region & PS3AV_REGION_60)
  681. id = PS3AV_DEFAULT_AVMULTI_MODE_ID_REG_60;
  682. else
  683. id = PS3AV_DEFAULT_AVMULTI_MODE_ID_REG_50;
  684. if (ps3av->region & PS3AV_REGION_RGB)
  685. rgb = PS3AV_MODE_RGB;
  686. pr_debug("%s: Using avmulti mode %d\n", __func__, id);
  687. }
  688. return id | dvi | rgb;
  689. }
  690. static int ps3av_get_hw_conf(struct ps3av *ps3av)
  691. {
  692. int i, j, k, res;
  693. const struct ps3av_pkt_av_get_hw_conf *hw_conf;
  694. /* get av_hw_conf */
  695. res = ps3av_cmd_av_get_hw_conf(&ps3av->av_hw_conf);
  696. if (res < 0)
  697. return -1;
  698. hw_conf = &ps3av->av_hw_conf;
  699. pr_debug("av_h_conf: num of hdmi: %u\n", hw_conf->num_of_hdmi);
  700. pr_debug("av_h_conf: num of avmulti: %u\n", hw_conf->num_of_avmulti);
  701. pr_debug("av_h_conf: num of spdif: %u\n", hw_conf->num_of_spdif);
  702. for (i = 0; i < PS3AV_HEAD_MAX; i++)
  703. ps3av->head[i] = PS3AV_CMD_VIDEO_HEAD_A + i;
  704. for (i = 0; i < PS3AV_OPT_PORT_MAX; i++)
  705. ps3av->opt_port[i] = PS3AV_CMD_AVPORT_SPDIF_0 + i;
  706. for (i = 0; i < hw_conf->num_of_hdmi; i++)
  707. ps3av->av_port[i] = PS3AV_CMD_AVPORT_HDMI_0 + i;
  708. for (j = 0; j < hw_conf->num_of_avmulti; j++)
  709. ps3av->av_port[i + j] = PS3AV_CMD_AVPORT_AVMULTI_0 + j;
  710. for (k = 0; k < hw_conf->num_of_spdif; k++)
  711. ps3av->av_port[i + j + k] = PS3AV_CMD_AVPORT_SPDIF_0 + k;
  712. /* set all audio port */
  713. ps3av->audio_port = PS3AV_CMD_AUDIO_PORT_HDMI_0
  714. | PS3AV_CMD_AUDIO_PORT_HDMI_1
  715. | PS3AV_CMD_AUDIO_PORT_AVMULTI_0
  716. | PS3AV_CMD_AUDIO_PORT_SPDIF_0 | PS3AV_CMD_AUDIO_PORT_SPDIF_1;
  717. return 0;
  718. }
  719. /* set mode using id */
  720. int ps3av_set_video_mode(int id)
  721. {
  722. int size;
  723. u32 option;
  724. size = ARRAY_SIZE(video_mode_table);
  725. if ((id & PS3AV_MODE_MASK) > size - 1 || id < 0) {
  726. dev_dbg(&ps3av->dev->core, "%s: error id :%d\n", __func__, id);
  727. return -EINVAL;
  728. }
  729. /* auto mode */
  730. option = id & ~PS3AV_MODE_MASK;
  731. if ((id & PS3AV_MODE_MASK) == PS3AV_MODE_AUTO) {
  732. id = ps3av_auto_videomode(&ps3av->av_hw_conf);
  733. if (id < 1) {
  734. printk(KERN_ERR "%s: invalid id :%d\n", __func__, id);
  735. return -EINVAL;
  736. }
  737. id |= option;
  738. }
  739. /* set videomode */
  740. wait_for_completion(&ps3av->done);
  741. ps3av->ps3av_mode_old = ps3av->ps3av_mode;
  742. ps3av->ps3av_mode = id;
  743. if (ps3av_set_videomode())
  744. ps3av->ps3av_mode = ps3av->ps3av_mode_old;
  745. return 0;
  746. }
  747. EXPORT_SYMBOL_GPL(ps3av_set_video_mode);
  748. int ps3av_get_auto_mode(void)
  749. {
  750. return ps3av_auto_videomode(&ps3av->av_hw_conf);
  751. }
  752. EXPORT_SYMBOL_GPL(ps3av_get_auto_mode);
  753. int ps3av_get_mode(void)
  754. {
  755. return ps3av ? ps3av->ps3av_mode : 0;
  756. }
  757. EXPORT_SYMBOL_GPL(ps3av_get_mode);
  758. /* get resolution by video_mode */
  759. int ps3av_video_mode2res(u32 id, u32 *xres, u32 *yres)
  760. {
  761. int size;
  762. id = id & PS3AV_MODE_MASK;
  763. size = ARRAY_SIZE(video_mode_table);
  764. if (id > size - 1 || id < 0) {
  765. printk(KERN_ERR "%s: invalid mode %d\n", __func__, id);
  766. return -EINVAL;
  767. }
  768. *xres = video_mode_table[id].x;
  769. *yres = video_mode_table[id].y;
  770. return 0;
  771. }
  772. EXPORT_SYMBOL_GPL(ps3av_video_mode2res);
  773. /* mute */
  774. int ps3av_video_mute(int mute)
  775. {
  776. return ps3av_set_av_video_mute(mute ? PS3AV_CMD_MUTE_ON
  777. : PS3AV_CMD_MUTE_OFF);
  778. }
  779. EXPORT_SYMBOL_GPL(ps3av_video_mute);
  780. /* mute analog output only */
  781. int ps3av_audio_mute_analog(int mute)
  782. {
  783. int i, res;
  784. for (i = 0; i < ps3av->av_hw_conf.num_of_avmulti; i++) {
  785. res = ps3av_cmd_av_audio_mute(1,
  786. &ps3av->av_port[i + ps3av->av_hw_conf.num_of_hdmi],
  787. mute);
  788. if (res < 0)
  789. return -1;
  790. }
  791. return 0;
  792. }
  793. EXPORT_SYMBOL_GPL(ps3av_audio_mute_analog);
  794. int ps3av_audio_mute(int mute)
  795. {
  796. return ps3av_set_audio_mute(mute ? PS3AV_CMD_MUTE_ON
  797. : PS3AV_CMD_MUTE_OFF);
  798. }
  799. EXPORT_SYMBOL_GPL(ps3av_audio_mute);
  800. static int __devinit ps3av_probe(struct ps3_system_bus_device *dev)
  801. {
  802. int res;
  803. int id;
  804. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  805. dev_dbg(&dev->core, " timeout=%d\n", timeout);
  806. if (ps3av) {
  807. dev_err(&dev->core, "Only one ps3av device is supported\n");
  808. return -EBUSY;
  809. }
  810. ps3av = kzalloc(sizeof(*ps3av), GFP_KERNEL);
  811. if (!ps3av)
  812. return -ENOMEM;
  813. mutex_init(&ps3av->mutex);
  814. ps3av->ps3av_mode = PS3AV_MODE_AUTO;
  815. ps3av->dev = dev;
  816. INIT_WORK(&ps3av->work, ps3avd);
  817. init_completion(&ps3av->done);
  818. complete(&ps3av->done);
  819. ps3av->wq = create_singlethread_workqueue("ps3avd");
  820. if (!ps3av->wq) {
  821. res = -ENOMEM;
  822. goto fail;
  823. }
  824. switch (ps3_os_area_get_av_multi_out()) {
  825. case PS3_PARAM_AV_MULTI_OUT_NTSC:
  826. ps3av->region = PS3AV_REGION_60;
  827. break;
  828. case PS3_PARAM_AV_MULTI_OUT_PAL_YCBCR:
  829. case PS3_PARAM_AV_MULTI_OUT_SECAM:
  830. ps3av->region = PS3AV_REGION_50;
  831. break;
  832. case PS3_PARAM_AV_MULTI_OUT_PAL_RGB:
  833. ps3av->region = PS3AV_REGION_50 | PS3AV_REGION_RGB;
  834. break;
  835. default:
  836. ps3av->region = PS3AV_REGION_60;
  837. break;
  838. }
  839. /* init avsetting modules */
  840. res = ps3av_cmd_init();
  841. if (res < 0)
  842. printk(KERN_ERR "%s: ps3av_cmd_init failed %d\n", __func__,
  843. res);
  844. ps3av_get_hw_conf(ps3av);
  845. #ifdef CONFIG_FB
  846. if (fb_mode_option && !strcmp(fb_mode_option, "safe"))
  847. safe_mode = 1;
  848. #endif /* CONFIG_FB */
  849. id = ps3av_auto_videomode(&ps3av->av_hw_conf);
  850. if (id < 0) {
  851. printk(KERN_ERR "%s: invalid id :%d\n", __func__, id);
  852. res = -EINVAL;
  853. goto fail;
  854. }
  855. safe_mode = 0;
  856. mutex_lock(&ps3av->mutex);
  857. ps3av->ps3av_mode = id;
  858. mutex_unlock(&ps3av->mutex);
  859. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  860. return 0;
  861. fail:
  862. kfree(ps3av);
  863. ps3av = NULL;
  864. return res;
  865. }
  866. static int ps3av_remove(struct ps3_system_bus_device *dev)
  867. {
  868. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  869. if (ps3av) {
  870. ps3av_cmd_fin();
  871. if (ps3av->wq)
  872. destroy_workqueue(ps3av->wq);
  873. kfree(ps3av);
  874. ps3av = NULL;
  875. }
  876. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  877. return 0;
  878. }
  879. static void ps3av_shutdown(struct ps3_system_bus_device *dev)
  880. {
  881. dev_dbg(&dev->core, " -> %s:%d\n", __func__, __LINE__);
  882. ps3av_remove(dev);
  883. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  884. }
  885. static struct ps3_vuart_port_driver ps3av_driver = {
  886. .core.match_id = PS3_MATCH_ID_AV_SETTINGS,
  887. .core.core.name = "ps3_av",
  888. .probe = ps3av_probe,
  889. .remove = ps3av_remove,
  890. .shutdown = ps3av_shutdown,
  891. };
  892. static int __init ps3av_module_init(void)
  893. {
  894. int error;
  895. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  896. return -ENODEV;
  897. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  898. error = ps3_vuart_port_driver_register(&ps3av_driver);
  899. if (error) {
  900. printk(KERN_ERR
  901. "%s: ps3_vuart_port_driver_register failed %d\n",
  902. __func__, error);
  903. return error;
  904. }
  905. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  906. return error;
  907. }
  908. static void __exit ps3av_module_exit(void)
  909. {
  910. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  911. ps3_vuart_port_driver_unregister(&ps3av_driver);
  912. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  913. }
  914. subsys_initcall(ps3av_module_init);
  915. module_exit(ps3av_module_exit);
  916. MODULE_LICENSE("GPL v2");
  917. MODULE_DESCRIPTION("PS3 AV Settings Driver");
  918. MODULE_AUTHOR("Sony Computer Entertainment Inc.");
  919. MODULE_ALIAS(PS3_MODULE_ALIAS_AV_SETTINGS);