ast_dp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2021, ASPEED Technology Inc.
  3. // Authors: KuoHsiang Chou <kuohsiang_chou@aspeedtech.com>
  4. #include <linux/firmware.h>
  5. #include <linux/delay.h>
  6. #include <drm/drm_atomic_state_helper.h>
  7. #include <drm/drm_edid.h>
  8. #include <drm/drm_modeset_helper_vtables.h>
  9. #include <drm/drm_print.h>
  10. #include <drm/drm_probe_helper.h>
  11. #include "ast_drv.h"
  12. static bool ast_astdp_is_connected(struct ast_device *ast)
  13. {
  14. if (!ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDF, AST_IO_VGACRDF_HPD))
  15. return false;
  16. /*
  17. * HPD might be set even if no monitor is connected, so also check that
  18. * the link training was successful.
  19. */
  20. if (!ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDC, AST_IO_VGACRDC_LINK_SUCCESS))
  21. return false;
  22. return true;
  23. }
  24. static int ast_astdp_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
  25. {
  26. struct ast_device *ast = data;
  27. size_t rdlen = round_up(len, 4);
  28. int ret = 0;
  29. unsigned int i;
  30. if (block > 0)
  31. return -EIO; /* extension headers not supported */
  32. /*
  33. * Protect access to I/O registers from concurrent modesetting
  34. * by acquiring the I/O-register lock.
  35. */
  36. mutex_lock(&ast->modeset_lock);
  37. /* Start reading EDID data */
  38. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5, (u8)~AST_IO_VGACRE5_EDID_READ_DONE, 0x00);
  39. for (i = 0; i < rdlen; i += 4) {
  40. unsigned int offset;
  41. unsigned int j;
  42. u8 ediddata[4];
  43. u8 vgacre4;
  44. offset = (i + block * EDID_LENGTH) / 4;
  45. if (offset >= 64) {
  46. ret = -EIO;
  47. goto out;
  48. }
  49. vgacre4 = offset;
  50. /*
  51. * CRE4[7:0]: Read-Pointer for EDID (Unit: 4bytes); valid range: 0~64
  52. */
  53. ast_set_index_reg(ast, AST_IO_VGACRI, 0xe4, vgacre4);
  54. /*
  55. * CRD7[b0]: valid flag for EDID
  56. * CRD6[b0]: mirror read pointer for EDID
  57. */
  58. for (j = 0; j < 200; ++j) {
  59. u8 vgacrd7, vgacrd6;
  60. /*
  61. * Delay are getting longer with each retry.
  62. *
  63. * 1. No delay on first try
  64. * 2. The Delays are often 2 loops when users request "Display Settings"
  65. * of right-click of mouse.
  66. * 3. The Delays are often longer a lot when system resume from S3/S4.
  67. */
  68. if (j)
  69. msleep(j + 1);
  70. /* Wait for EDID offset to show up in mirror register */
  71. vgacrd7 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd7);
  72. if (vgacrd7 & AST_IO_VGACRD7_EDID_VALID_FLAG) {
  73. vgacrd6 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd6);
  74. if (vgacrd6 == offset)
  75. break;
  76. }
  77. }
  78. if (j == 200) {
  79. ret = -EBUSY;
  80. goto out;
  81. }
  82. ediddata[0] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd8);
  83. ediddata[1] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd9);
  84. ediddata[2] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xda);
  85. ediddata[3] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdb);
  86. if (i == 31) {
  87. /*
  88. * For 128-bytes EDID_1.3,
  89. * 1. Add the value of Bytes-126 to Bytes-127.
  90. * The Bytes-127 is Checksum. Sum of all 128bytes should
  91. * equal 0 (mod 256).
  92. * 2. Modify Bytes-126 to be 0.
  93. * The Bytes-126 indicates the Number of extensions to
  94. * follow. 0 represents noextensions.
  95. */
  96. ediddata[3] = ediddata[3] + ediddata[2];
  97. ediddata[2] = 0;
  98. }
  99. memcpy(buf, ediddata, min((len - i), 4));
  100. buf += 4;
  101. }
  102. out:
  103. /* Signal end of reading */
  104. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5, (u8)~AST_IO_VGACRE5_EDID_READ_DONE,
  105. AST_IO_VGACRE5_EDID_READ_DONE);
  106. mutex_unlock(&ast->modeset_lock);
  107. return ret;
  108. }
  109. /*
  110. * Launch Aspeed DP
  111. */
  112. int ast_dp_launch(struct ast_device *ast)
  113. {
  114. struct drm_device *dev = &ast->base;
  115. unsigned int i = 10;
  116. while (i) {
  117. u8 vgacrd1 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd1);
  118. if (vgacrd1 & AST_IO_VGACRD1_MCU_FW_EXECUTING)
  119. break;
  120. --i;
  121. msleep(100);
  122. }
  123. if (!i) {
  124. drm_err(dev, "Wait DPMCU executing timeout\n");
  125. return -ENODEV;
  126. }
  127. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5,
  128. (u8) ~AST_IO_VGACRE5_EDID_READ_DONE,
  129. AST_IO_VGACRE5_EDID_READ_DONE);
  130. return 0;
  131. }
  132. static bool ast_dp_power_is_on(struct ast_device *ast)
  133. {
  134. u8 vgacre3;
  135. vgacre3 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xe3);
  136. return !(vgacre3 & AST_DP_PHY_SLEEP);
  137. }
  138. static void ast_dp_power_on_off(struct drm_device *dev, bool on)
  139. {
  140. struct ast_device *ast = to_ast_device(dev);
  141. // Read and Turn off DP PHY sleep
  142. u8 bE3 = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xE3, AST_DP_VIDEO_ENABLE);
  143. // Turn on DP PHY sleep
  144. if (!on)
  145. bE3 |= AST_DP_PHY_SLEEP;
  146. // DP Power on/off
  147. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE3, (u8) ~AST_DP_PHY_SLEEP, bE3);
  148. msleep(50);
  149. }
  150. static void ast_dp_link_training(struct ast_device *ast)
  151. {
  152. struct drm_device *dev = &ast->base;
  153. int i;
  154. for (i = 0; i < 10; i++) {
  155. u8 vgacrdc;
  156. if (i)
  157. msleep(100);
  158. vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
  159. if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
  160. return;
  161. }
  162. drm_err(dev, "Link training failed\n");
  163. }
  164. static void ast_dp_set_on_off(struct drm_device *dev, bool on)
  165. {
  166. struct ast_device *ast = to_ast_device(dev);
  167. u8 video_on_off = on;
  168. u32 i = 0;
  169. // Video On/Off
  170. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE3, (u8) ~AST_DP_VIDEO_ENABLE, on);
  171. video_on_off <<= 4;
  172. while (ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDF,
  173. ASTDP_MIRROR_VIDEO_ENABLE) != video_on_off) {
  174. // wait 1 ms
  175. mdelay(1);
  176. if (++i > 200)
  177. break;
  178. }
  179. }
  180. static void ast_dp_set_mode(struct drm_crtc *crtc, struct ast_vbios_mode_info *vbios_mode)
  181. {
  182. struct ast_device *ast = to_ast_device(crtc->dev);
  183. u32 ulRefreshRateIndex;
  184. u8 ModeIdx;
  185. ulRefreshRateIndex = vbios_mode->enh_table->refresh_rate_index - 1;
  186. switch (crtc->mode.crtc_hdisplay) {
  187. case 320:
  188. ModeIdx = ASTDP_320x240_60;
  189. break;
  190. case 400:
  191. ModeIdx = ASTDP_400x300_60;
  192. break;
  193. case 512:
  194. ModeIdx = ASTDP_512x384_60;
  195. break;
  196. case 640:
  197. ModeIdx = (ASTDP_640x480_60 + (u8) ulRefreshRateIndex);
  198. break;
  199. case 800:
  200. ModeIdx = (ASTDP_800x600_56 + (u8) ulRefreshRateIndex);
  201. break;
  202. case 1024:
  203. ModeIdx = (ASTDP_1024x768_60 + (u8) ulRefreshRateIndex);
  204. break;
  205. case 1152:
  206. ModeIdx = ASTDP_1152x864_75;
  207. break;
  208. case 1280:
  209. if (crtc->mode.crtc_vdisplay == 800)
  210. ModeIdx = (ASTDP_1280x800_60_RB - (u8) ulRefreshRateIndex);
  211. else // 1024
  212. ModeIdx = (ASTDP_1280x1024_60 + (u8) ulRefreshRateIndex);
  213. break;
  214. case 1360:
  215. case 1366:
  216. ModeIdx = ASTDP_1366x768_60;
  217. break;
  218. case 1440:
  219. ModeIdx = (ASTDP_1440x900_60_RB - (u8) ulRefreshRateIndex);
  220. break;
  221. case 1600:
  222. if (crtc->mode.crtc_vdisplay == 900)
  223. ModeIdx = (ASTDP_1600x900_60_RB - (u8) ulRefreshRateIndex);
  224. else //1200
  225. ModeIdx = ASTDP_1600x1200_60;
  226. break;
  227. case 1680:
  228. ModeIdx = (ASTDP_1680x1050_60_RB - (u8) ulRefreshRateIndex);
  229. break;
  230. case 1920:
  231. if (crtc->mode.crtc_vdisplay == 1080)
  232. ModeIdx = ASTDP_1920x1080_60;
  233. else //1200
  234. ModeIdx = ASTDP_1920x1200_60;
  235. break;
  236. default:
  237. return;
  238. }
  239. /*
  240. * CRE0[7:0]: MISC0 ((0x00: 18-bpp) or (0x20: 24-bpp)
  241. * CRE1[7:0]: MISC1 (default: 0x00)
  242. * CRE2[7:0]: video format index (0x00 ~ 0x20 or 0x40 ~ 0x50)
  243. */
  244. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE0, ASTDP_AND_CLEAR_MASK,
  245. ASTDP_MISC0_24bpp);
  246. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE1, ASTDP_AND_CLEAR_MASK, ASTDP_MISC1);
  247. ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xE2, ASTDP_AND_CLEAR_MASK, ModeIdx);
  248. }
  249. static void ast_wait_for_vretrace(struct ast_device *ast)
  250. {
  251. unsigned long timeout = jiffies + HZ;
  252. u8 vgair1;
  253. do {
  254. vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R);
  255. } while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
  256. }
  257. /*
  258. * Encoder
  259. */
  260. static const struct drm_encoder_funcs ast_astdp_encoder_funcs = {
  261. .destroy = drm_encoder_cleanup,
  262. };
  263. static void ast_astdp_encoder_helper_atomic_mode_set(struct drm_encoder *encoder,
  264. struct drm_crtc_state *crtc_state,
  265. struct drm_connector_state *conn_state)
  266. {
  267. struct drm_crtc *crtc = crtc_state->crtc;
  268. struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
  269. struct ast_vbios_mode_info *vbios_mode_info = &ast_crtc_state->vbios_mode_info;
  270. ast_dp_set_mode(crtc, vbios_mode_info);
  271. }
  272. static void ast_astdp_encoder_helper_atomic_enable(struct drm_encoder *encoder,
  273. struct drm_atomic_state *state)
  274. {
  275. struct drm_device *dev = encoder->dev;
  276. struct ast_device *ast = to_ast_device(dev);
  277. struct ast_connector *ast_connector = &ast->output.astdp.connector;
  278. if (ast_connector->physical_status == connector_status_connected) {
  279. ast_dp_power_on_off(dev, AST_DP_POWER_ON);
  280. ast_dp_link_training(ast);
  281. ast_wait_for_vretrace(ast);
  282. ast_dp_set_on_off(dev, 1);
  283. }
  284. }
  285. static void ast_astdp_encoder_helper_atomic_disable(struct drm_encoder *encoder,
  286. struct drm_atomic_state *state)
  287. {
  288. struct drm_device *dev = encoder->dev;
  289. ast_dp_set_on_off(dev, 0);
  290. ast_dp_power_on_off(dev, AST_DP_POWER_OFF);
  291. }
  292. static const struct drm_encoder_helper_funcs ast_astdp_encoder_helper_funcs = {
  293. .atomic_mode_set = ast_astdp_encoder_helper_atomic_mode_set,
  294. .atomic_enable = ast_astdp_encoder_helper_atomic_enable,
  295. .atomic_disable = ast_astdp_encoder_helper_atomic_disable,
  296. };
  297. /*
  298. * Connector
  299. */
  300. static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
  301. {
  302. struct ast_connector *ast_connector = to_ast_connector(connector);
  303. int count;
  304. if (ast_connector->physical_status == connector_status_connected) {
  305. struct ast_device *ast = to_ast_device(connector->dev);
  306. const struct drm_edid *drm_edid;
  307. drm_edid = drm_edid_read_custom(connector, ast_astdp_read_edid_block, ast);
  308. drm_edid_connector_update(connector, drm_edid);
  309. count = drm_edid_connector_add_modes(connector);
  310. drm_edid_free(drm_edid);
  311. } else {
  312. drm_edid_connector_update(connector, NULL);
  313. /*
  314. * There's no EDID data without a connected monitor. Set BMC-
  315. * compatible modes in this case. The XGA default resolution
  316. * should work well for all BMCs.
  317. */
  318. count = drm_add_modes_noedid(connector, 4096, 4096);
  319. if (count)
  320. drm_set_preferred_mode(connector, 1024, 768);
  321. }
  322. return count;
  323. }
  324. static int ast_astdp_connector_helper_detect_ctx(struct drm_connector *connector,
  325. struct drm_modeset_acquire_ctx *ctx,
  326. bool force)
  327. {
  328. struct ast_connector *ast_connector = to_ast_connector(connector);
  329. struct drm_device *dev = connector->dev;
  330. struct ast_device *ast = to_ast_device(connector->dev);
  331. enum drm_connector_status status = connector_status_disconnected;
  332. bool power_is_on;
  333. mutex_lock(&ast->modeset_lock);
  334. power_is_on = ast_dp_power_is_on(ast);
  335. if (!power_is_on)
  336. ast_dp_power_on_off(dev, true);
  337. if (ast_astdp_is_connected(ast))
  338. status = connector_status_connected;
  339. if (!power_is_on && status == connector_status_disconnected)
  340. ast_dp_power_on_off(dev, false);
  341. mutex_unlock(&ast->modeset_lock);
  342. if (status != ast_connector->physical_status)
  343. ++connector->epoch_counter;
  344. ast_connector->physical_status = status;
  345. return connector_status_connected;
  346. }
  347. static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
  348. .get_modes = ast_astdp_connector_helper_get_modes,
  349. .detect_ctx = ast_astdp_connector_helper_detect_ctx,
  350. };
  351. static const struct drm_connector_funcs ast_astdp_connector_funcs = {
  352. .reset = drm_atomic_helper_connector_reset,
  353. .fill_modes = drm_helper_probe_single_connector_modes,
  354. .destroy = drm_connector_cleanup,
  355. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  356. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  357. };
  358. static int ast_astdp_connector_init(struct drm_device *dev, struct drm_connector *connector)
  359. {
  360. int ret;
  361. ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
  362. DRM_MODE_CONNECTOR_DisplayPort);
  363. if (ret)
  364. return ret;
  365. drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
  366. connector->interlace_allowed = 0;
  367. connector->doublescan_allowed = 0;
  368. connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  369. return 0;
  370. }
  371. int ast_astdp_output_init(struct ast_device *ast)
  372. {
  373. struct drm_device *dev = &ast->base;
  374. struct drm_crtc *crtc = &ast->crtc;
  375. struct drm_encoder *encoder = &ast->output.astdp.encoder;
  376. struct ast_connector *ast_connector = &ast->output.astdp.connector;
  377. struct drm_connector *connector = &ast_connector->base;
  378. int ret;
  379. ret = drm_encoder_init(dev, encoder, &ast_astdp_encoder_funcs,
  380. DRM_MODE_ENCODER_TMDS, NULL);
  381. if (ret)
  382. return ret;
  383. drm_encoder_helper_add(encoder, &ast_astdp_encoder_helper_funcs);
  384. encoder->possible_crtcs = drm_crtc_mask(crtc);
  385. ret = ast_astdp_connector_init(dev, connector);
  386. if (ret)
  387. return ret;
  388. ast_connector->physical_status = connector->status;
  389. ret = drm_connector_attach_encoder(connector, encoder);
  390. if (ret)
  391. return ret;
  392. return 0;
  393. }