ps3av.c 28 KB

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