ps3av.c 29 KB

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