dp_mst_sim.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/platform_device.h>
  19. #include <drm/drm_edid.h>
  20. #include <drm/drm_modes.h>
  21. #include "dp_aux_bridge.h"
  22. #include "dp_debug.h"
  23. #include "dp_mst_sim_helper.h"
  24. struct dp_sim_device {
  25. struct dp_aux_bridge bridge;
  26. void *host_dev;
  27. int (*hpd_cb)(void *, bool, bool);
  28. };
  29. #define to_dp_sim_dev(x) container_of((x), struct dp_sim_device, bridge)
  30. static const struct dp_mst_sim_port output_port = {
  31. false, false, true, 3, false, 0x12,
  32. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  34. 0, 0, 2520, 2520, NULL, 0
  35. };
  36. static int dp_sim_register_hpd(struct dp_aux_bridge *bridge,
  37. int (*hpd_cb)(void *, bool, bool), void *dev)
  38. {
  39. struct dp_sim_device *sim_dev = to_dp_sim_dev(bridge);
  40. sim_dev->host_dev = dev;
  41. sim_dev->hpd_cb = hpd_cb;
  42. return 0;
  43. }
  44. static ssize_t dp_sim_transfer(struct dp_aux_bridge *bridge,
  45. struct drm_dp_aux *drm_aux,
  46. struct drm_dp_aux_msg *msg)
  47. {
  48. struct dp_sim_device *sim_dev = to_dp_sim_dev(bridge);
  49. int ret;
  50. ret = dp_mst_sim_transfer(sim_dev->bridge.mst_ctx, msg);
  51. if (ret < 0)
  52. ret = drm_aux->transfer(drm_aux, msg);
  53. else
  54. ret = msg->size;
  55. return ret;
  56. }
  57. static void dp_sim_host_hpd_irq(void *host_dev)
  58. {
  59. struct dp_sim_device *sim_dev = host_dev;
  60. if (sim_dev->hpd_cb)
  61. sim_dev->hpd_cb(sim_dev->host_dev, false, true);
  62. }
  63. static void dp_sim_update_dtd(struct edid *edid,
  64. struct drm_display_mode *mode)
  65. {
  66. struct detailed_timing *dtd = &edid->detailed_timings[0];
  67. struct detailed_pixel_timing *pd = &dtd->data.pixel_data;
  68. u32 h_blank = mode->htotal - mode->hdisplay;
  69. u32 v_blank = mode->vtotal - mode->vdisplay;
  70. u32 h_img = 0, v_img = 0;
  71. dtd->pixel_clock = cpu_to_le16(mode->clock / 10);
  72. pd->hactive_lo = mode->hdisplay & 0xFF;
  73. pd->hblank_lo = h_blank & 0xFF;
  74. pd->hactive_hblank_hi = ((h_blank >> 8) & 0xF) |
  75. ((mode->hdisplay >> 8) & 0xF) << 4;
  76. pd->vactive_lo = mode->vdisplay & 0xFF;
  77. pd->vblank_lo = v_blank & 0xFF;
  78. pd->vactive_vblank_hi = ((v_blank >> 8) & 0xF) |
  79. ((mode->vdisplay >> 8) & 0xF) << 4;
  80. pd->hsync_offset_lo =
  81. (mode->hsync_start - mode->hdisplay) & 0xFF;
  82. pd->hsync_pulse_width_lo =
  83. (mode->hsync_end - mode->hsync_start) & 0xFF;
  84. pd->vsync_offset_pulse_width_lo =
  85. (((mode->vsync_start - mode->vdisplay) & 0xF) << 4) |
  86. ((mode->vsync_end - mode->vsync_start) & 0xF);
  87. pd->hsync_vsync_offset_pulse_width_hi =
  88. ((((mode->hsync_start - mode->hdisplay) >> 8) & 0x3) << 6) |
  89. ((((mode->hsync_end - mode->hsync_start) >> 8) & 0x3) << 4) |
  90. ((((mode->vsync_start - mode->vdisplay) >> 4) & 0x3) << 2) |
  91. ((((mode->vsync_end - mode->vsync_start) >> 4) & 0x3) << 0);
  92. pd->width_mm_lo = h_img & 0xFF;
  93. pd->height_mm_lo = v_img & 0xFF;
  94. pd->width_height_mm_hi = (((h_img >> 8) & 0xF) << 4) |
  95. ((v_img >> 8) & 0xF);
  96. pd->hborder = 0;
  97. pd->vborder = 0;
  98. pd->misc = 0;
  99. }
  100. static void dp_sim_update_checksum(struct edid *edid)
  101. {
  102. u8 *data = (u8 *)edid;
  103. u32 i, sum = 0;
  104. for (i = 0; i < EDID_LENGTH - 1; i++)
  105. sum += data[i];
  106. edid->checksum = 0x100 - (sum & 0xFF);
  107. }
  108. static int dp_sim_parse(struct dp_sim_device *sim_dev)
  109. {
  110. struct device_node *of_node = sim_dev->bridge.of_node;
  111. struct device_node *node;
  112. struct dp_mst_sim_port *ports;
  113. struct drm_display_mode mode_buf, *mode = &mode_buf;
  114. u16 h_front_porch, h_pulse_width, h_back_porch;
  115. u16 v_front_porch, v_pulse_width, v_back_porch;
  116. bool h_active_high, v_active_high;
  117. u32 flags = 0;
  118. int rc, port_num, i;
  119. struct edid *edid;
  120. const u8 edid_buf[EDID_LENGTH] = {
  121. 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x44, 0x6D,
  122. 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1B, 0x10, 0x01, 0x03,
  123. 0x80, 0x50, 0x2D, 0x78, 0x0A, 0x0D, 0xC9, 0xA0, 0x57, 0x47,
  124. 0x98, 0x27, 0x12, 0x48, 0x4C, 0x00, 0x00, 0x00, 0x01, 0x01,
  125. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  126. 0x01, 0x01, 0x01, 0x01,
  127. };
  128. port_num = of_get_child_count(of_node);
  129. if (!port_num)
  130. return 0;
  131. if (port_num >= 15)
  132. return -EINVAL;
  133. ports = kcalloc(port_num, sizeof(*ports), GFP_KERNEL);
  134. if (!ports)
  135. return -ENOMEM;
  136. i = 0;
  137. for_each_child_of_node(of_node, node) {
  138. rc = of_property_read_u16(node, "qcom,mode-h-active",
  139. &mode->hdisplay);
  140. if (rc) {
  141. DP_ERR("failed to read h-active, rc=%d\n", rc);
  142. goto fail;
  143. }
  144. rc = of_property_read_u16(node, "qcom,mode-h-front-porch",
  145. &h_front_porch);
  146. if (rc) {
  147. DP_ERR("failed to read h-front-porch, rc=%d\n", rc);
  148. goto fail;
  149. }
  150. rc = of_property_read_u16(node, "qcom,mode-h-pulse-width",
  151. &h_pulse_width);
  152. if (rc) {
  153. DP_ERR("failed to read h-pulse-width, rc=%d\n", rc);
  154. goto fail;
  155. }
  156. rc = of_property_read_u16(node, "qcom,mode-h-back-porch",
  157. &h_back_porch);
  158. if (rc) {
  159. DP_ERR("failed to read h-back-porch, rc=%d\n", rc);
  160. goto fail;
  161. }
  162. h_active_high = of_property_read_bool(node,
  163. "qcom,mode-h-active-high");
  164. rc = of_property_read_u16(node, "qcom,mode-v-active",
  165. &mode->vdisplay);
  166. if (rc) {
  167. DP_ERR("failed to read v-active, rc=%d\n", rc);
  168. goto fail;
  169. }
  170. rc = of_property_read_u16(node, "qcom,mode-v-front-porch",
  171. &v_front_porch);
  172. if (rc) {
  173. DP_ERR("failed to read v-front-porch, rc=%d\n", rc);
  174. goto fail;
  175. }
  176. rc = of_property_read_u16(node, "qcom,mode-v-pulse-width",
  177. &v_pulse_width);
  178. if (rc) {
  179. DP_ERR("failed to read v-pulse-width, rc=%d\n", rc);
  180. goto fail;
  181. }
  182. rc = of_property_read_u16(node, "qcom,mode-v-back-porch",
  183. &v_back_porch);
  184. if (rc) {
  185. DP_ERR("failed to read v-back-porch, rc=%d\n", rc);
  186. goto fail;
  187. }
  188. v_active_high = of_property_read_bool(node,
  189. "qcom,mode-v-active-high");
  190. rc = of_property_read_u32(node, "qcom,mode-clock-in-khz",
  191. &mode->clock);
  192. if (rc) {
  193. DP_ERR("failed to read clock, rc=%d\n", rc);
  194. goto fail;
  195. }
  196. mode->hsync_start = mode->hdisplay + h_front_porch;
  197. mode->hsync_end = mode->hsync_start + h_pulse_width;
  198. mode->htotal = mode->hsync_end + h_back_porch;
  199. mode->vsync_start = mode->vdisplay + v_front_porch;
  200. mode->vsync_end = mode->vsync_start + v_pulse_width;
  201. mode->vtotal = mode->vsync_end + v_back_porch;
  202. if (h_active_high)
  203. flags |= DRM_MODE_FLAG_PHSYNC;
  204. else
  205. flags |= DRM_MODE_FLAG_NHSYNC;
  206. if (v_active_high)
  207. flags |= DRM_MODE_FLAG_PVSYNC;
  208. else
  209. flags |= DRM_MODE_FLAG_NVSYNC;
  210. mode->flags = flags;
  211. edid = kzalloc(sizeof(*edid), GFP_KERNEL);
  212. if (!edid) {
  213. rc = -ENOMEM;
  214. goto fail;
  215. }
  216. memcpy(edid, edid_buf, sizeof(edid_buf));
  217. dp_sim_update_dtd(edid, mode);
  218. dp_sim_update_checksum(edid);
  219. memcpy(&ports[i], &output_port, sizeof(*ports));
  220. ports[i].peer_guid[0] = i;
  221. ports[i].edid = (u8 *)edid;
  222. ports[i].edid_size = sizeof(*edid);
  223. i++;
  224. }
  225. rc = dp_mst_sim_update(sim_dev->bridge.mst_ctx, port_num, ports);
  226. fail:
  227. for (i = 0; i < port_num; i++)
  228. kfree(ports[i].edid);
  229. kfree(ports);
  230. return rc;
  231. }
  232. int dp_sim_probe(struct platform_device *pdev)
  233. {
  234. struct dp_sim_device *dp_sim_dev;
  235. struct dp_mst_sim_cfg cfg;
  236. int ret;
  237. dp_sim_dev = devm_kzalloc(&pdev->dev, sizeof(*dp_sim_dev), GFP_KERNEL);
  238. if (!dp_sim_dev)
  239. return -ENOMEM;
  240. dp_sim_dev->bridge.of_node = pdev->dev.of_node;
  241. dp_sim_dev->bridge.register_hpd = dp_sim_register_hpd;
  242. dp_sim_dev->bridge.transfer = dp_sim_transfer;
  243. dp_sim_dev->bridge.dev_priv = dp_sim_dev;
  244. dp_sim_dev->bridge.flag = DP_AUX_BRIDGE_MST;
  245. memset(&cfg, 0, sizeof(cfg));
  246. cfg.host_dev = dp_sim_dev;
  247. cfg.host_hpd_irq = dp_sim_host_hpd_irq;
  248. ret = dp_mst_sim_create(&cfg, &dp_sim_dev->bridge.mst_ctx);
  249. if (ret)
  250. return ret;
  251. ret = dp_sim_parse(dp_sim_dev);
  252. if (ret)
  253. goto fail;
  254. ret = dp_aux_add_bridge(&dp_sim_dev->bridge);
  255. if (ret)
  256. goto fail;
  257. platform_set_drvdata(pdev, dp_sim_dev);
  258. return 0;
  259. fail:
  260. dp_mst_sim_destroy(dp_sim_dev->bridge.mst_ctx);
  261. return ret;
  262. }
  263. int dp_sim_remove(struct platform_device *pdev)
  264. {
  265. struct dp_sim_device *dp_sim_dev;
  266. dp_sim_dev = platform_get_drvdata(pdev);
  267. if (!dp_sim_dev)
  268. return 0;
  269. dp_mst_sim_destroy(dp_sim_dev->bridge.mst_ctx);
  270. return 0;
  271. }
  272. #if 0
  273. static const struct of_device_id dt_match[] = {
  274. { .compatible = "qcom,dp-mst-sim"},
  275. {},
  276. };
  277. static struct platform_driver dp_sim_driver = {
  278. .probe = dp_sim_probe,
  279. .remove = dp_sim_remove,
  280. .driver = {
  281. .name = "dp_sim",
  282. .of_match_table = dt_match,
  283. .suppress_bind_attrs = true,
  284. },
  285. };
  286. static int __init dp_sim_register(void)
  287. {
  288. return platform_driver_register(&dp_sim_driver);
  289. }
  290. static void __exit dp_sim_unregister(void)
  291. {
  292. platform_driver_unregister(&dp_sim_driver);
  293. }
  294. module_init(dp_sim_register);
  295. module_exit(dp_sim_unregister);
  296. #endif