dp_mst_sim.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /*
  2. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/version.h>
  20. #include <linux/platform_device.h>
  21. #include <drm/drm_edid.h>
  22. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  23. #include <drm/display/drm_dp_helper.h>
  24. #else
  25. #include <drm/drm_dp_helper.h>
  26. #endif
  27. #include "dp_debug.h"
  28. #include "dp_mst_sim.h"
  29. struct dp_sim_dpcd_reg {
  30. struct list_head head;
  31. u32 addr;
  32. u8 val;
  33. };
  34. #define DP_SIM_BRIDGE_PRIV_FLAG (1 << 31)
  35. #define MAX_BUILTIN_DPCD_ADDR SZ_2K
  36. #define MAX_MST_PORT 8
  37. struct dp_sim_device {
  38. struct device *dev;
  39. struct dp_aux_bridge bridge;
  40. void *host_dev;
  41. int (*hpd_cb)(void *, bool, bool);
  42. struct mutex lock;
  43. const char *label;
  44. struct dentry *debugfs_dir;
  45. struct dentry *debugfs_edid_dir;
  46. u8 dpcd_reg[MAX_BUILTIN_DPCD_ADDR];
  47. struct list_head dpcd_reg_list;
  48. u32 dpcd_write_addr;
  49. u32 dpcd_write_size;
  50. u32 link_training_cnt;
  51. u32 link_training_remain;
  52. u32 link_training_lane_cnt;
  53. bool link_training_mismatch;
  54. struct dp_mst_sim_port *ports;
  55. u32 port_num;
  56. u32 current_port_num;
  57. u32 sim_mode;
  58. u32 edid_seg;
  59. u32 edid_seg_int;
  60. u32 edid_addr;
  61. bool skip_edid;
  62. bool skip_dpcd;
  63. bool skip_link_training;
  64. bool skip_config;
  65. bool skip_hpd;
  66. bool skip_mst;
  67. };
  68. struct dp_sim_debug_edid_entry {
  69. struct dp_sim_device *sim_dev;
  70. u32 index;
  71. };
  72. #define to_dp_sim_dev(x) container_of((x), struct dp_sim_device, bridge)
  73. static const struct dp_mst_sim_port output_port = {
  74. false, false, true, 3, false, 0x12,
  75. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  77. 0, 0, 2520, 2520, NULL, 0
  78. };
  79. static int dp_sim_register_hpd(struct dp_aux_bridge *bridge,
  80. int (*hpd_cb)(void *, bool, bool), void *dev)
  81. {
  82. struct dp_sim_device *sim_dev = to_dp_sim_dev(bridge);
  83. sim_dev->host_dev = dev;
  84. sim_dev->hpd_cb = hpd_cb;
  85. if (sim_dev->skip_hpd)
  86. hpd_cb(dev, true, false);
  87. return 0;
  88. }
  89. static u8 dp_sim_read_dpcd(struct dp_sim_device *sim_dev,
  90. u32 addr)
  91. {
  92. struct dp_sim_dpcd_reg *reg;
  93. if (addr < MAX_BUILTIN_DPCD_ADDR) {
  94. return sim_dev->dpcd_reg[addr];
  95. } else {
  96. list_for_each_entry(reg, &sim_dev->dpcd_reg_list, head) {
  97. if (reg->addr == addr)
  98. return reg->val;
  99. }
  100. }
  101. return 0;
  102. }
  103. static void dp_sim_write_dpcd(struct dp_sim_device *sim_dev,
  104. u32 addr, u8 val)
  105. {
  106. struct dp_sim_dpcd_reg *dpcd_reg;
  107. if (addr < MAX_BUILTIN_DPCD_ADDR) {
  108. sim_dev->dpcd_reg[addr] = val;
  109. } else {
  110. list_for_each_entry(dpcd_reg, &sim_dev->dpcd_reg_list, head) {
  111. if (dpcd_reg->addr == addr) {
  112. dpcd_reg->val = val;
  113. return;
  114. }
  115. }
  116. dpcd_reg = devm_kzalloc(sim_dev->dev,
  117. sizeof(*dpcd_reg), GFP_KERNEL);
  118. if (!dpcd_reg)
  119. return;
  120. dpcd_reg->addr = addr;
  121. dpcd_reg->val = val;
  122. list_add_tail(&dpcd_reg->head, &sim_dev->dpcd_reg_list);
  123. }
  124. }
  125. static int dp_sim_read_dpcd_regs(struct dp_sim_device *sim_dev,
  126. u8 *buf, u32 size, u32 offset)
  127. {
  128. u32 i;
  129. if (offset + size <= MAX_BUILTIN_DPCD_ADDR) {
  130. memcpy(buf, &sim_dev->dpcd_reg[offset], size);
  131. } else {
  132. for (i = 0; i < size; i++)
  133. buf[i] = dp_sim_read_dpcd(sim_dev, offset + i);
  134. }
  135. return size;
  136. }
  137. static int dp_sim_read_edid(struct dp_sim_device *sim_dev,
  138. struct drm_dp_aux_msg *msg)
  139. {
  140. u8 *buf = (u8 *)msg->buffer;
  141. u32 addr;
  142. if (!sim_dev->port_num || !msg->size)
  143. return 0;
  144. if (msg->request & DP_AUX_I2C_READ) {
  145. addr = (sim_dev->edid_seg_int << 8) + sim_dev->edid_addr;
  146. if (addr + msg->size <= sim_dev->ports[0].edid_size) {
  147. memcpy(msg->buffer, &sim_dev->ports[0].edid[addr],
  148. msg->size);
  149. } else if (addr < sim_dev->ports[0].edid_size) {
  150. memcpy(msg->buffer, &sim_dev->ports[0].edid[addr],
  151. sim_dev->ports[0].edid_size - addr);
  152. }
  153. sim_dev->edid_addr += msg->size;
  154. sim_dev->edid_addr &= 0xFF;
  155. } else {
  156. if (msg->address == 0x30) {
  157. sim_dev->edid_seg = buf[0];
  158. } else if (msg->address == 0x50) {
  159. sim_dev->edid_seg_int = sim_dev->edid_seg;
  160. sim_dev->edid_addr = buf[0];
  161. sim_dev->edid_seg = 0;
  162. }
  163. }
  164. return msg->size;
  165. }
  166. static int dp_sim_link_training(struct dp_sim_device *sim_dev,
  167. struct drm_dp_aux *drm_aux,
  168. struct drm_dp_aux_msg *msg)
  169. {
  170. u8 *link_status = msg->buffer;
  171. int ret, i;
  172. if (msg->request == DP_AUX_NATIVE_READ &&
  173. msg->address == DP_LANE0_1_STATUS) {
  174. /*
  175. * remain is an option to allow limited actual
  176. * link training. this is needed for some device
  177. * when actual read is needed.
  178. */
  179. if (sim_dev->link_training_remain) {
  180. sim_dev->link_training_remain--;
  181. ret = drm_aux->transfer(drm_aux, msg);
  182. if (ret >= 0)
  183. link_status[2] &= ~DP_LINK_STATUS_UPDATED;
  184. return ret;
  185. }
  186. memcpy(msg->buffer, &sim_dev->dpcd_reg[msg->address],
  187. msg->size);
  188. /*
  189. * when mismatch happens, clear status and fail the link
  190. * training.
  191. */
  192. if (sim_dev->link_training_mismatch) {
  193. link_status[0] = 0;
  194. link_status[1] = 0;
  195. }
  196. return msg->size;
  197. }
  198. if (msg->request == DP_AUX_NATIVE_WRITE) {
  199. if (msg->address == DP_TRAINING_LANE0_SET) {
  200. const u8 mask = DP_TRAIN_VOLTAGE_SWING_MASK |
  201. DP_TRAIN_PRE_EMPHASIS_MASK;
  202. /*
  203. * when link training is set, only pre-set vx/px is
  204. * going through. here we will fail the initial
  205. * vx/px and correct them automatically.
  206. */
  207. sim_dev->link_training_mismatch = false;
  208. for (i = 0; i < sim_dev->link_training_lane_cnt; i++) {
  209. if ((link_status[i] & mask) !=
  210. (sim_dev->dpcd_reg[
  211. DP_TRAINING_LANE0_SET + i] & mask)) {
  212. sim_dev->link_training_mismatch = true;
  213. break;
  214. }
  215. }
  216. } else if (msg->address == DP_TRAINING_PATTERN_SET) {
  217. sim_dev->link_training_remain =
  218. sim_dev->link_training_cnt;
  219. } else if (msg->address == DP_LINK_BW_SET) {
  220. sim_dev->link_training_lane_cnt =
  221. link_status[1] & 0x1F;
  222. }
  223. }
  224. return 0;
  225. }
  226. static ssize_t dp_sim_transfer(struct dp_aux_bridge *bridge,
  227. struct drm_dp_aux *drm_aux,
  228. struct drm_dp_aux_msg *msg)
  229. {
  230. struct dp_sim_device *sim_dev = to_dp_sim_dev(bridge);
  231. int ret;
  232. mutex_lock(&sim_dev->lock);
  233. if (sim_dev->skip_link_training &&
  234. !(sim_dev->sim_mode & DP_SIM_MODE_LINK_TRAIN)) {
  235. ret = dp_sim_link_training(sim_dev, drm_aux, msg);
  236. if (ret)
  237. goto end;
  238. }
  239. if ((sim_dev->sim_mode & DP_SIM_MODE_MST) || sim_dev->skip_mst) {
  240. ret = dp_mst_sim_transfer(sim_dev->bridge.mst_ctx, msg);
  241. if (ret >= 0) {
  242. ret = msg->size;
  243. goto end;
  244. }
  245. }
  246. if (msg->request == DP_AUX_NATIVE_WRITE) {
  247. sim_dev->dpcd_write_addr = msg->address;
  248. sim_dev->dpcd_write_size = msg->size;
  249. }
  250. if (((sim_dev->sim_mode & DP_SIM_MODE_EDID) ||
  251. sim_dev->skip_edid) &&
  252. (msg->request & DP_AUX_I2C_MOT))
  253. ret = dp_sim_read_edid(sim_dev, msg);
  254. else if (((sim_dev->sim_mode & DP_SIM_MODE_DPCD_READ) ||
  255. sim_dev->skip_dpcd) &&
  256. msg->request == DP_AUX_NATIVE_READ)
  257. ret = dp_sim_read_dpcd_regs(sim_dev, msg->buffer,
  258. msg->size, msg->address);
  259. else if (((sim_dev->sim_mode & DP_SIM_MODE_DPCD_WRITE) ||
  260. sim_dev->skip_config) &&
  261. msg->request == DP_AUX_NATIVE_WRITE)
  262. ret = msg->size;
  263. else
  264. ret = drm_aux->transfer(drm_aux, msg);
  265. end:
  266. mutex_unlock(&sim_dev->lock);
  267. return ret;
  268. }
  269. static void dp_sim_host_hpd_irq(void *host_dev)
  270. {
  271. struct dp_sim_device *sim_dev = host_dev;
  272. if (sim_dev->hpd_cb)
  273. sim_dev->hpd_cb(sim_dev->host_dev, true, true);
  274. }
  275. int dp_sim_set_sim_mode(struct dp_aux_bridge *bridge, u32 sim_mode)
  276. {
  277. struct dp_sim_device *sim_dev;
  278. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  279. return -EINVAL;
  280. sim_dev = to_dp_sim_dev(bridge);
  281. sim_dev->sim_mode = sim_mode;
  282. return 0;
  283. }
  284. int dp_sim_update_port_num(struct dp_aux_bridge *bridge, u32 port_num)
  285. {
  286. struct dp_sim_device *sim_dev;
  287. struct dp_mst_sim_port *ports;
  288. u32 i, rc;
  289. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  290. return -EINVAL;
  291. sim_dev = to_dp_sim_dev(bridge);
  292. DP_INFO("Update port count from %d to %d\n", sim_dev->port_num, port_num);
  293. if (port_num > sim_dev->port_num) {
  294. ports = devm_kzalloc(sim_dev->dev,
  295. port_num * sizeof(*ports), GFP_KERNEL);
  296. if (!ports)
  297. return -ENOMEM;
  298. memcpy(ports, sim_dev->ports,
  299. sim_dev->port_num * sizeof(*ports));
  300. if (sim_dev->ports)
  301. devm_kfree(sim_dev->dev, sim_dev->ports);
  302. sim_dev->ports = ports;
  303. for (i = sim_dev->port_num; i < port_num; i++) {
  304. memcpy(&ports[i], &output_port, sizeof(*ports));
  305. ports[i].peer_guid[0] = i;
  306. }
  307. }
  308. sim_dev->port_num = port_num;
  309. rc = dp_mst_sim_update(sim_dev->bridge.mst_ctx,
  310. port_num, sim_dev->ports);
  311. if (rc)
  312. return rc;
  313. sim_dev->current_port_num = port_num;
  314. return rc;
  315. }
  316. int dp_sim_update_port_status(struct dp_aux_bridge *bridge,
  317. int port, enum drm_connector_status status)
  318. {
  319. struct dp_sim_device *sim_dev;
  320. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  321. return -EINVAL;
  322. sim_dev = to_dp_sim_dev(bridge);
  323. if (port < 0 || port >= sim_dev->current_port_num)
  324. return -EINVAL;
  325. sim_dev->ports[port].pdt = (status == connector_status_connected) ?
  326. DP_PEER_DEVICE_SST_SINK : DP_PEER_DEVICE_NONE;
  327. return dp_mst_sim_update(sim_dev->bridge.mst_ctx,
  328. sim_dev->current_port_num, sim_dev->ports);
  329. }
  330. int dp_sim_update_port_edid(struct dp_aux_bridge *bridge,
  331. int port, const u8 *edid, u32 size)
  332. {
  333. struct dp_sim_device *sim_dev;
  334. struct dp_mst_sim_port *sim_port;
  335. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  336. return -EINVAL;
  337. sim_dev = to_dp_sim_dev(bridge);
  338. if (port < 0 || port >= sim_dev->current_port_num)
  339. return -EINVAL;
  340. sim_port = &sim_dev->ports[port];
  341. if (size != sim_port->edid_size) {
  342. if (sim_port->edid)
  343. devm_kfree(sim_dev->dev, (u8 *)sim_port->edid);
  344. sim_port->edid = devm_kzalloc(sim_dev->dev,
  345. size, GFP_KERNEL);
  346. if (!sim_port->edid)
  347. return -ENOMEM;
  348. sim_port->edid_size = size;
  349. }
  350. memcpy((u8 *)sim_port->edid, edid, size);
  351. return dp_mst_sim_update(sim_dev->bridge.mst_ctx,
  352. sim_dev->current_port_num, sim_dev->ports);
  353. }
  354. int dp_sim_write_dpcd_reg(struct dp_aux_bridge *bridge,
  355. const u8 *dpcd, u32 size, u32 offset)
  356. {
  357. struct dp_sim_device *sim_dev;
  358. int i;
  359. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  360. return -EINVAL;
  361. sim_dev = to_dp_sim_dev(bridge);
  362. for (i = 0; i < size; i++)
  363. dp_sim_write_dpcd(sim_dev, offset + i, dpcd[i]);
  364. return 0;
  365. }
  366. int dp_sim_read_dpcd_reg(struct dp_aux_bridge *bridge,
  367. u8 *dpcd, u32 size, u32 offset)
  368. {
  369. struct dp_sim_device *sim_dev;
  370. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  371. return -EINVAL;
  372. sim_dev = to_dp_sim_dev(bridge);
  373. return dp_sim_read_dpcd_regs(sim_dev, dpcd, size, offset);
  374. }
  375. static void dp_sim_update_dtd(struct edid *edid,
  376. struct drm_display_mode *mode)
  377. {
  378. struct detailed_timing *dtd = &edid->detailed_timings[0];
  379. struct detailed_pixel_timing *pd = &dtd->data.pixel_data;
  380. u32 h_blank = mode->htotal - mode->hdisplay;
  381. u32 v_blank = mode->vtotal - mode->vdisplay;
  382. u32 h_img = 0, v_img = 0;
  383. dtd->pixel_clock = cpu_to_le16(mode->clock / 10);
  384. pd->hactive_lo = mode->hdisplay & 0xFF;
  385. pd->hblank_lo = h_blank & 0xFF;
  386. pd->hactive_hblank_hi = ((h_blank >> 8) & 0xF) |
  387. ((mode->hdisplay >> 8) & 0xF) << 4;
  388. pd->vactive_lo = mode->vdisplay & 0xFF;
  389. pd->vblank_lo = v_blank & 0xFF;
  390. pd->vactive_vblank_hi = ((v_blank >> 8) & 0xF) |
  391. ((mode->vdisplay >> 8) & 0xF) << 4;
  392. pd->hsync_offset_lo =
  393. (mode->hsync_start - mode->hdisplay) & 0xFF;
  394. pd->hsync_pulse_width_lo =
  395. (mode->hsync_end - mode->hsync_start) & 0xFF;
  396. pd->vsync_offset_pulse_width_lo =
  397. (((mode->vsync_start - mode->vdisplay) & 0xF) << 4) |
  398. ((mode->vsync_end - mode->vsync_start) & 0xF);
  399. pd->hsync_vsync_offset_pulse_width_hi =
  400. ((((mode->hsync_start - mode->hdisplay) >> 8) & 0x3) << 6) |
  401. ((((mode->hsync_end - mode->hsync_start) >> 8) & 0x3) << 4) |
  402. ((((mode->vsync_start - mode->vdisplay) >> 4) & 0x3) << 2) |
  403. ((((mode->vsync_end - mode->vsync_start) >> 4) & 0x3) << 0);
  404. pd->width_mm_lo = h_img & 0xFF;
  405. pd->height_mm_lo = v_img & 0xFF;
  406. pd->width_height_mm_hi = (((h_img >> 8) & 0xF) << 4) |
  407. ((v_img >> 8) & 0xF);
  408. pd->hborder = 0;
  409. pd->vborder = 0;
  410. pd->misc = 0;
  411. }
  412. static void dp_sim_update_checksum(struct edid *edid)
  413. {
  414. u8 *data = (u8 *)edid;
  415. u32 i, sum = 0;
  416. for (i = 0; i < EDID_LENGTH - 1; i++)
  417. sum += data[i];
  418. edid->checksum = 0x100 - (sum & 0xFF);
  419. }
  420. static int dp_sim_parse_edid_from_node(struct dp_sim_device *sim_dev,
  421. int index, struct device_node *node)
  422. {
  423. struct dp_mst_sim_port *port;
  424. struct drm_display_mode mode_buf, *mode = &mode_buf;
  425. u16 h_front_porch, h_pulse_width, h_back_porch;
  426. u16 v_front_porch, v_pulse_width, v_back_porch;
  427. bool h_active_high, v_active_high;
  428. u32 flags = 0;
  429. int rc;
  430. struct edid *edid;
  431. const u8 edid_buf[EDID_LENGTH] = {
  432. 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x44, 0x6D,
  433. 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1B, 0x10, 0x01, 0x03,
  434. 0x80, 0x50, 0x2D, 0x78, 0x0A, 0x0D, 0xC9, 0xA0, 0x57, 0x47,
  435. 0x98, 0x27, 0x12, 0x48, 0x4C, 0x00, 0x00, 0x00, 0x01, 0x01,
  436. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  437. 0x01, 0x01, 0x01, 0x01,
  438. };
  439. rc = of_property_read_u16(node, "qcom,mode-h-active",
  440. &mode->hdisplay);
  441. if (rc) {
  442. DP_ERR("failed to read h-active, rc=%d\n", rc);
  443. goto fail;
  444. }
  445. rc = of_property_read_u16(node, "qcom,mode-h-front-porch",
  446. &h_front_porch);
  447. if (rc) {
  448. DP_ERR("failed to read h-front-porch, rc=%d\n", rc);
  449. goto fail;
  450. }
  451. rc = of_property_read_u16(node, "qcom,mode-h-pulse-width",
  452. &h_pulse_width);
  453. if (rc) {
  454. DP_ERR("failed to read h-pulse-width, rc=%d\n", rc);
  455. goto fail;
  456. }
  457. rc = of_property_read_u16(node, "qcom,mode-h-back-porch",
  458. &h_back_porch);
  459. if (rc) {
  460. DP_ERR("failed to read h-back-porch, rc=%d\n", rc);
  461. goto fail;
  462. }
  463. h_active_high = of_property_read_bool(node,
  464. "qcom,mode-h-active-high");
  465. rc = of_property_read_u16(node, "qcom,mode-v-active",
  466. &mode->vdisplay);
  467. if (rc) {
  468. DP_ERR("failed to read v-active, rc=%d\n", rc);
  469. goto fail;
  470. }
  471. rc = of_property_read_u16(node, "qcom,mode-v-front-porch",
  472. &v_front_porch);
  473. if (rc) {
  474. DP_ERR("failed to read v-front-porch, rc=%d\n", rc);
  475. goto fail;
  476. }
  477. rc = of_property_read_u16(node, "qcom,mode-v-pulse-width",
  478. &v_pulse_width);
  479. if (rc) {
  480. DP_ERR("failed to read v-pulse-width, rc=%d\n", rc);
  481. goto fail;
  482. }
  483. rc = of_property_read_u16(node, "qcom,mode-v-back-porch",
  484. &v_back_porch);
  485. if (rc) {
  486. DP_ERR("failed to read v-back-porch, rc=%d\n", rc);
  487. goto fail;
  488. }
  489. v_active_high = of_property_read_bool(node,
  490. "qcom,mode-v-active-high");
  491. rc = of_property_read_u32(node, "qcom,mode-clock-in-khz",
  492. &mode->clock);
  493. if (rc) {
  494. DP_ERR("failed to read clock, rc=%d\n", rc);
  495. goto fail;
  496. }
  497. mode->hsync_start = mode->hdisplay + h_front_porch;
  498. mode->hsync_end = mode->hsync_start + h_pulse_width;
  499. mode->htotal = mode->hsync_end + h_back_porch;
  500. mode->vsync_start = mode->vdisplay + v_front_porch;
  501. mode->vsync_end = mode->vsync_start + v_pulse_width;
  502. mode->vtotal = mode->vsync_end + v_back_porch;
  503. if (h_active_high)
  504. flags |= DRM_MODE_FLAG_PHSYNC;
  505. else
  506. flags |= DRM_MODE_FLAG_NHSYNC;
  507. if (v_active_high)
  508. flags |= DRM_MODE_FLAG_PVSYNC;
  509. else
  510. flags |= DRM_MODE_FLAG_NVSYNC;
  511. mode->flags = flags;
  512. edid = devm_kzalloc(sim_dev->dev, sizeof(*edid), GFP_KERNEL);
  513. if (!edid) {
  514. rc = -ENOMEM;
  515. goto fail;
  516. }
  517. memcpy(edid, edid_buf, sizeof(edid_buf));
  518. dp_sim_update_dtd(edid, mode);
  519. dp_sim_update_checksum(edid);
  520. port = &sim_dev->ports[index];
  521. memcpy(port, &output_port, sizeof(*port));
  522. port->peer_guid[0] = index;
  523. if (port->edid)
  524. devm_kfree(sim_dev->dev, (u8 *)port->edid);
  525. port->edid = (u8 *)edid;
  526. port->edid_size = sizeof(*edid);
  527. fail:
  528. return rc;
  529. }
  530. static int dp_sim_parse_edid_from_data(struct dp_sim_device *sim_dev,
  531. int index, const char *data, int len)
  532. {
  533. struct dp_mst_sim_port *port;
  534. u8 *edid_data;
  535. edid_data = devm_kzalloc(sim_dev->dev, len, GFP_KERNEL);
  536. if (!edid_data)
  537. return -ENOMEM;
  538. memcpy(edid_data, data, len);
  539. port = &sim_dev->ports[index];
  540. memcpy(port, &output_port, sizeof(*port));
  541. port->peer_guid[0] = index;
  542. if (port->edid)
  543. devm_kfree(sim_dev->dev, (u8 *)port->edid);
  544. port->edid = edid_data;
  545. port->edid_size = len;
  546. return 0;
  547. }
  548. static int dp_sim_parse_edid(struct dp_sim_device *sim_dev)
  549. {
  550. struct dp_mst_sim_port *ports;
  551. struct device_node *of_node = sim_dev->bridge.of_node;
  552. struct device_node *node;
  553. const char *data;
  554. int rc, port_num, i, len;
  555. port_num = of_get_child_count(of_node);
  556. if (!port_num)
  557. port_num = 1;
  558. if (port_num >= 15)
  559. return -EINVAL;
  560. ports = devm_kzalloc(sim_dev->dev,
  561. port_num * sizeof(*ports), GFP_KERNEL);
  562. if (!ports)
  563. return -ENOMEM;
  564. sim_dev->ports = ports;
  565. sim_dev->port_num = port_num;
  566. sim_dev->current_port_num = port_num;
  567. i = 0;
  568. for_each_child_of_node(of_node, node) {
  569. data = of_get_property(node, "qcom,edid", &len);
  570. if (data)
  571. rc = dp_sim_parse_edid_from_data(sim_dev, i,
  572. data, len);
  573. else
  574. rc = dp_sim_parse_edid_from_node(sim_dev, i,
  575. node);
  576. if (rc)
  577. return rc;
  578. i++;
  579. }
  580. if (i == 0)
  581. memcpy(ports, &output_port, sizeof(*ports));
  582. return 0;
  583. }
  584. static int dp_sim_parse_dpcd(struct dp_sim_device *sim_dev)
  585. {
  586. struct device_node *node = sim_dev->bridge.of_node;
  587. u32 val, i;
  588. const __be32 *arr;
  589. int rc;
  590. rc = of_property_read_u32(node, "qcom,dpcd-max-rate", &val);
  591. if (!rc)
  592. sim_dev->dpcd_reg[DP_MAX_LINK_RATE] = val;
  593. rc = of_property_read_u32(node, "qcom,dpcd-max-lane", &val);
  594. if (!rc)
  595. sim_dev->dpcd_reg[DP_MAX_LANE_COUNT] = val;
  596. rc = of_property_read_u32(node, "qcom,dpcd-mst", &val);
  597. if (!rc)
  598. sim_dev->dpcd_reg[DP_MSTM_CAP] = val;
  599. arr = of_get_property(node, "qcom,dpcd-regs", &val);
  600. if (arr) {
  601. val /= sizeof(u32);
  602. val &= ~0x1;
  603. for (i = 0; i < val; i += 2)
  604. dp_sim_write_dpcd(sim_dev,
  605. be32_to_cpu(arr[i]),
  606. be32_to_cpu(arr[i+1]));
  607. }
  608. rc = of_property_read_u32(node, "qcom,voltage-swing", &val);
  609. if (!rc)
  610. for (i = 0; i < 4; i++) {
  611. sim_dev->dpcd_reg[DP_TRAINING_LANE0_SET + i] |=
  612. val;
  613. sim_dev->dpcd_reg[DP_ADJUST_REQUEST_LANE0_1 + (i/2)] |=
  614. (val & 0x3) << ((i & 0x1) << 2);
  615. }
  616. rc = of_property_read_u32(node, "qcom,pre-emphasis", &val);
  617. if (!rc)
  618. for (i = 0; i < 4; i++) {
  619. sim_dev->dpcd_reg[DP_TRAINING_LANE0_SET + i] |=
  620. val << 3;
  621. sim_dev->dpcd_reg[DP_ADJUST_REQUEST_LANE0_1 + (i/2)] |=
  622. (val & 0x3) << (((i & 0x1) << 2) + 2);
  623. }
  624. rc = of_property_read_u32(node, "qcom,link-training-cnt", &val);
  625. if (!rc)
  626. sim_dev->link_training_cnt = val;
  627. else
  628. sim_dev->link_training_cnt = 0;
  629. return 0;
  630. }
  631. static int dp_sim_parse_misc(struct dp_sim_device *sim_dev)
  632. {
  633. struct device_node *node = sim_dev->bridge.of_node;
  634. sim_dev->skip_edid = of_property_read_bool(node,
  635. "qcom,skip-edid");
  636. sim_dev->skip_dpcd = of_property_read_bool(node,
  637. "qcom,skip-dpcd-read");
  638. sim_dev->skip_link_training = of_property_read_bool(node,
  639. "qcom,skip-link-training");
  640. sim_dev->skip_config = of_property_read_bool(node,
  641. "qcom,skip-dpcd-write");
  642. sim_dev->skip_hpd = of_property_read_bool(node,
  643. "qcom,skip-hpd");
  644. sim_dev->skip_mst = of_property_read_bool(node,
  645. "qcom,skip-mst");
  646. DP_DEBUG("skip: edid=%d dpcd=%d LT=%d config=%d hpd=%d mst=%d\n",
  647. sim_dev->skip_edid,
  648. sim_dev->skip_dpcd,
  649. sim_dev->skip_link_training,
  650. sim_dev->skip_config,
  651. sim_dev->skip_hpd,
  652. sim_dev->skip_mst);
  653. return 0;
  654. }
  655. static ssize_t dp_sim_debug_write_edid(struct file *file,
  656. const char __user *user_buff, size_t count, loff_t *ppos)
  657. {
  658. struct dp_sim_debug_edid_entry *entry = file->private_data;
  659. struct dp_sim_device *debug;
  660. struct dp_mst_sim_port *port;
  661. u8 *buf = NULL, *buf_t = NULL;
  662. const int char_to_nib = 2;
  663. size_t edid_size = 0;
  664. size_t size = 0, edid_buf_index = 0;
  665. ssize_t rc = count;
  666. if (!entry)
  667. return -ENODEV;
  668. debug = entry->sim_dev;
  669. if (!debug || entry->index >= debug->port_num)
  670. return -EINVAL;
  671. port = &debug->ports[entry->index];
  672. mutex_lock(&debug->lock);
  673. if (*ppos)
  674. goto bail;
  675. size = min_t(size_t, count, SZ_1K);
  676. buf = kzalloc(size, GFP_KERNEL);
  677. if (ZERO_OR_NULL_PTR(buf)) {
  678. rc = -ENOMEM;
  679. goto bail;
  680. }
  681. if (copy_from_user(buf, user_buff, size))
  682. goto bail;
  683. edid_size = size / char_to_nib;
  684. buf_t = buf;
  685. if (edid_size != port->edid_size) {
  686. if (port->edid)
  687. devm_kfree(debug->dev, (u8 *)port->edid);
  688. port->edid = devm_kzalloc(debug->dev,
  689. edid_size, GFP_KERNEL);
  690. if (!port->edid) {
  691. rc = -ENOMEM;
  692. goto bail;
  693. }
  694. port->edid_size = edid_size;
  695. }
  696. while (edid_size--) {
  697. char t[3];
  698. int d;
  699. memcpy(t, buf_t, sizeof(char) * char_to_nib);
  700. t[char_to_nib] = '\0';
  701. if (kstrtoint(t, 16, &d)) {
  702. DP_ERR("kstrtoint error\n");
  703. goto bail;
  704. }
  705. if (port->edid && (edid_buf_index < port->edid_size))
  706. ((u8 *)port->edid)[edid_buf_index++] = d;
  707. buf_t += char_to_nib;
  708. }
  709. if (debug->skip_mst)
  710. dp_mst_sim_update(debug->bridge.mst_ctx,
  711. debug->port_num, debug->ports);
  712. debug->skip_edid = true;
  713. bail:
  714. kfree(buf);
  715. mutex_unlock(&debug->lock);
  716. return rc;
  717. }
  718. static ssize_t dp_sim_debug_write_dpcd(struct file *file,
  719. const char __user *user_buff, size_t count, loff_t *ppos)
  720. {
  721. struct dp_sim_device *debug = file->private_data;
  722. u8 *buf = NULL, *buf_t = NULL;
  723. const int char_to_nib = 2;
  724. size_t dpcd_size = 0;
  725. size_t size = 0, dpcd_buf_index = 0;
  726. ssize_t rc = count;
  727. char offset_ch[5];
  728. u32 offset, data_len;
  729. if (!debug)
  730. return -ENODEV;
  731. mutex_lock(&debug->lock);
  732. if (*ppos)
  733. goto bail;
  734. size = min_t(size_t, count, SZ_2K);
  735. if (size < 4)
  736. goto bail;
  737. buf = kzalloc(size, GFP_KERNEL);
  738. if (ZERO_OR_NULL_PTR(buf)) {
  739. rc = -ENOMEM;
  740. goto bail;
  741. }
  742. if (copy_from_user(buf, user_buff, size))
  743. goto bail;
  744. memcpy(offset_ch, buf, 4);
  745. offset_ch[4] = '\0';
  746. if (kstrtoint(offset_ch, 16, &offset)) {
  747. DP_ERR("offset kstrtoint error\n");
  748. goto bail;
  749. }
  750. if (offset == 0xFFFF) {
  751. DP_ERR("clearing dpcd\n");
  752. memset(debug->dpcd_reg, 0, sizeof(debug->dpcd_reg));
  753. goto bail;
  754. }
  755. size -= 4;
  756. if (size == 0)
  757. goto bail;
  758. dpcd_size = size / char_to_nib;
  759. data_len = dpcd_size;
  760. buf_t = buf + 4;
  761. dpcd_buf_index = offset;
  762. while (dpcd_size--) {
  763. char t[3];
  764. int d;
  765. memcpy(t, buf_t, sizeof(char) * char_to_nib);
  766. t[char_to_nib] = '\0';
  767. if (kstrtoint(t, 16, &d)) {
  768. DP_ERR("kstrtoint error\n");
  769. goto bail;
  770. }
  771. dp_sim_write_dpcd(debug, dpcd_buf_index, d);
  772. dpcd_buf_index++;
  773. buf_t += char_to_nib;
  774. }
  775. debug->skip_dpcd = true;
  776. debug->skip_config = true;
  777. bail:
  778. kfree(buf);
  779. mutex_unlock(&debug->lock);
  780. return rc;
  781. }
  782. static ssize_t dp_sim_debug_read_dpcd(struct file *file,
  783. char __user *user_buff, size_t count, loff_t *ppos)
  784. {
  785. struct dp_sim_device *debug = file->private_data;
  786. char *buf;
  787. int const buf_size = SZ_4K;
  788. u32 offset = 0;
  789. u32 len = 0;
  790. if (!debug)
  791. return -ENODEV;
  792. if (*ppos)
  793. return 0;
  794. buf = kzalloc(buf_size, GFP_KERNEL);
  795. if (!buf)
  796. return -ENOMEM;
  797. len += snprintf(buf, buf_size, "0x%x", debug->dpcd_write_addr);
  798. while (1) {
  799. if (debug->dpcd_write_addr + offset >= buf_size ||
  800. offset >= debug->dpcd_write_size)
  801. break;
  802. len += snprintf(buf + len, buf_size - len, "0x%x",
  803. debug->dpcd_reg[debug->dpcd_write_addr + offset++]);
  804. }
  805. len = min_t(size_t, count, len);
  806. if (!copy_to_user(user_buff, buf, len))
  807. *ppos += len;
  808. kfree(buf);
  809. return len;
  810. }
  811. static ssize_t dp_sim_debug_write_hpd(struct file *file,
  812. const char __user *user_buff, size_t count, loff_t *ppos)
  813. {
  814. struct dp_sim_device *debug = file->private_data;
  815. char buf[SZ_8];
  816. size_t len = 0;
  817. int hpd = 0;
  818. if (!debug)
  819. return -ENODEV;
  820. if (*ppos)
  821. return 0;
  822. len = min_t(size_t, count, SZ_8 - 1);
  823. if (copy_from_user(buf, user_buff, len))
  824. goto end;
  825. buf[len] = '\0';
  826. if (kstrtoint(buf, 10, &hpd) != 0)
  827. goto end;
  828. if (debug->hpd_cb)
  829. debug->hpd_cb(debug->host_dev, !!hpd, false);
  830. end:
  831. return len;
  832. }
  833. static ssize_t dp_sim_debug_write_skip_link_training(struct file *file,
  834. const char __user *user_buff, size_t count, loff_t *ppos)
  835. {
  836. struct dp_sim_device *debug = file->private_data;
  837. char buf[SZ_8];
  838. size_t len = 0;
  839. int skip_lk, lk_cnt;
  840. if (!debug)
  841. return -ENODEV;
  842. if (*ppos)
  843. return 0;
  844. len = min_t(size_t, count, SZ_8 - 1);
  845. if (copy_from_user(buf, user_buff, len))
  846. goto end;
  847. buf[len] = '\0';
  848. if (sscanf(buf, "%d %u", &skip_lk, &lk_cnt) != 2) {
  849. DP_ERR("invalid input\n");
  850. return -EINVAL;
  851. }
  852. mutex_lock(&debug->lock);
  853. debug->skip_link_training = !!skip_lk;
  854. debug->link_training_cnt = lk_cnt;
  855. mutex_unlock(&debug->lock);
  856. end:
  857. return len;
  858. }
  859. static ssize_t dp_sim_debug_write_skip_edid(struct file *file,
  860. const char __user *user_buff, size_t count, loff_t *ppos)
  861. {
  862. struct dp_sim_device *debug = file->private_data;
  863. char buf[SZ_8];
  864. size_t len = 0;
  865. int val = 0;
  866. if (!debug)
  867. return -ENODEV;
  868. if (*ppos)
  869. return 0;
  870. len = min_t(size_t, count, SZ_8 - 1);
  871. if (copy_from_user(buf, user_buff, len))
  872. goto end;
  873. buf[len] = '\0';
  874. if (kstrtoint(buf, 10, &val) != 0)
  875. goto end;
  876. mutex_lock(&debug->lock);
  877. debug->skip_edid = !!val;
  878. mutex_unlock(&debug->lock);
  879. end:
  880. return len;
  881. }
  882. static ssize_t dp_sim_debug_write_skip_dpcd(struct file *file,
  883. const char __user *user_buff, size_t count, loff_t *ppos)
  884. {
  885. struct dp_sim_device *debug = file->private_data;
  886. char buf[SZ_8];
  887. size_t len = 0;
  888. int val = 0;
  889. if (!debug)
  890. return -ENODEV;
  891. if (*ppos)
  892. return 0;
  893. len = min_t(size_t, count, SZ_8 - 1);
  894. if (copy_from_user(buf, user_buff, len))
  895. goto end;
  896. buf[len] = '\0';
  897. if (kstrtoint(buf, 10, &val) != 0)
  898. goto end;
  899. mutex_lock(&debug->lock);
  900. debug->skip_dpcd = !!val;
  901. mutex_unlock(&debug->lock);
  902. end:
  903. return len;
  904. }
  905. static ssize_t dp_sim_debug_write_skip_config(struct file *file,
  906. const char __user *user_buff, size_t count, loff_t *ppos)
  907. {
  908. struct dp_sim_device *debug = file->private_data;
  909. char buf[SZ_8];
  910. size_t len = 0;
  911. int val = 0;
  912. if (!debug)
  913. return -ENODEV;
  914. if (*ppos)
  915. return 0;
  916. len = min_t(size_t, count, SZ_8 - 1);
  917. if (copy_from_user(buf, user_buff, len))
  918. goto end;
  919. buf[len] = '\0';
  920. if (kstrtoint(buf, 10, &val) != 0)
  921. goto end;
  922. mutex_lock(&debug->lock);
  923. debug->skip_config = !!val;
  924. mutex_unlock(&debug->lock);
  925. end:
  926. return len;
  927. }
  928. static ssize_t dp_sim_debug_write_mst_hpd(struct file *file,
  929. const char __user *user_buff, size_t count, loff_t *ppos)
  930. {
  931. struct dp_sim_debug_edid_entry *entry = file->private_data;
  932. struct dp_sim_device *debug;
  933. char buf[SZ_8];
  934. size_t len = 0;
  935. int hpd = 0;
  936. if (!entry)
  937. return -ENODEV;
  938. debug = entry->sim_dev;
  939. if (!debug || entry->index >= debug->port_num)
  940. return -EINVAL;
  941. if (*ppos)
  942. return 0;
  943. len = min_t(size_t, count, SZ_8 - 1);
  944. if (copy_from_user(buf, user_buff, len))
  945. goto end;
  946. buf[len] = '\0';
  947. if (kstrtoint(buf, 10, &hpd) != 0)
  948. goto end;
  949. dp_sim_update_port_status(&debug->bridge,
  950. entry->index, hpd ?
  951. connector_status_connected :
  952. connector_status_disconnected);
  953. end:
  954. return len;
  955. }
  956. static const struct file_operations sim_edid_fops = {
  957. .open = simple_open,
  958. .write = dp_sim_debug_write_edid,
  959. };
  960. static const struct file_operations sim_mst_hpd_fops = {
  961. .open = simple_open,
  962. .write = dp_sim_debug_write_mst_hpd,
  963. };
  964. static ssize_t dp_sim_debug_write_mst_mode(struct file *file,
  965. const char __user *user_buff, size_t count, loff_t *ppos)
  966. {
  967. struct dp_sim_device *debug = file->private_data;
  968. char buf[SZ_16];
  969. size_t len = 0;
  970. int mst_sideband_mode = 0;
  971. u32 mst_port_cnt = 0;
  972. u32 mst_old_port_cnt;
  973. struct dp_sim_debug_edid_entry *edid_entry;
  974. u8 *edid;
  975. u32 i, rc;
  976. if (!debug)
  977. return -ENODEV;
  978. /* Leave room for termination char */
  979. len = min_t(size_t, count, SZ_8 - 1);
  980. if (copy_from_user(buf, user_buff, len))
  981. return -EFAULT;
  982. buf[len] = '\0';
  983. if (sscanf(buf, "%d %u", &mst_sideband_mode, &mst_port_cnt) != 2) {
  984. DP_ERR("invalid input\n");
  985. return -EINVAL;
  986. }
  987. if (mst_port_cnt >= MAX_MST_PORT) {
  988. DP_ERR("port cnt:%d exceeding max:%d\n", mst_port_cnt,
  989. MAX_MST_PORT);
  990. return -EINVAL;
  991. }
  992. if (!mst_port_cnt)
  993. mst_port_cnt = 1;
  994. debug->skip_mst = !mst_sideband_mode;
  995. DP_DEBUG("mst_sideband_mode: %d port_cnt:%d\n",
  996. mst_sideband_mode, mst_port_cnt);
  997. mst_old_port_cnt = debug->port_num;
  998. rc = dp_sim_update_port_num(&debug->bridge, mst_port_cnt);
  999. if (rc)
  1000. return rc;
  1001. /* write mst */
  1002. dp_sim_write_dpcd(debug, DP_MSTM_CAP, debug->skip_mst);
  1003. /* create default edid nodes */
  1004. for (i = mst_old_port_cnt; i < mst_port_cnt; i++) {
  1005. edid_entry = devm_kzalloc(debug->dev,
  1006. sizeof(*edid_entry), GFP_KERNEL);
  1007. if (!edid_entry)
  1008. continue;
  1009. edid_entry->index = i;
  1010. edid_entry->sim_dev = debug;
  1011. scnprintf(buf, sizeof(buf), "edid-%d", i);
  1012. debugfs_create_file(buf,
  1013. 0444,
  1014. debug->debugfs_edid_dir,
  1015. edid_entry,
  1016. &sim_edid_fops);
  1017. scnprintf(buf, sizeof(buf), "hpd-%d", i);
  1018. debugfs_create_file(buf,
  1019. 0444,
  1020. debug->debugfs_edid_dir,
  1021. edid_entry,
  1022. &sim_mst_hpd_fops);
  1023. if (!debug->ports[0].edid_size)
  1024. continue;
  1025. edid = devm_kzalloc(debug->dev,
  1026. debug->ports[0].edid_size, GFP_KERNEL);
  1027. if (!edid)
  1028. return -ENOMEM;
  1029. memcpy(edid, debug->ports[0].edid, debug->ports[0].edid_size);
  1030. debug->ports[i].edid = edid;
  1031. debug->ports[i].edid_size = debug->ports[0].edid_size;
  1032. }
  1033. return count;
  1034. }
  1035. static const struct file_operations sim_dpcd_fops = {
  1036. .open = simple_open,
  1037. .write = dp_sim_debug_write_dpcd,
  1038. .read = dp_sim_debug_read_dpcd,
  1039. };
  1040. static const struct file_operations sim_hpd_fops = {
  1041. .open = simple_open,
  1042. .write = dp_sim_debug_write_hpd,
  1043. };
  1044. static const struct file_operations sim_skip_link_training_fops = {
  1045. .open = simple_open,
  1046. .write = dp_sim_debug_write_skip_link_training,
  1047. };
  1048. static const struct file_operations sim_skip_edid_fops = {
  1049. .open = simple_open,
  1050. .write = dp_sim_debug_write_skip_edid,
  1051. };
  1052. static const struct file_operations sim_skip_dpcd_fops = {
  1053. .open = simple_open,
  1054. .write = dp_sim_debug_write_skip_dpcd,
  1055. };
  1056. static const struct file_operations sim_skip_config_fops = {
  1057. .open = simple_open,
  1058. .write = dp_sim_debug_write_skip_config,
  1059. };
  1060. static const struct file_operations sim_mst_mode_fops = {
  1061. .open = simple_open,
  1062. .write = dp_sim_debug_write_mst_mode,
  1063. };
  1064. static int dp_sim_debug_init(struct dp_sim_device *sim_dev)
  1065. {
  1066. struct dp_sim_debug_edid_entry *edid_entry;
  1067. struct dentry *dir, *file, *edid_dir;
  1068. char name[SZ_16];
  1069. int rc = 0, i;
  1070. if (!sim_dev->label)
  1071. return 0;
  1072. dir = debugfs_create_dir(sim_dev->label, NULL);
  1073. if (IS_ERR_OR_NULL(dir)) {
  1074. rc = PTR_ERR(dir);
  1075. DP_ERR("[%s] debugfs create dir failed, rc = %d\n",
  1076. sim_dev->label, rc);
  1077. goto error;
  1078. }
  1079. edid_dir = debugfs_create_dir("mst_edid", dir);
  1080. if (IS_ERR_OR_NULL(edid_dir)) {
  1081. rc = PTR_ERR(edid_dir);
  1082. DP_ERR("[%s] debugfs create dir failed, rc = %d\n",
  1083. sim_dev->label, rc);
  1084. goto error_remove_dir;
  1085. }
  1086. for (i = 0; i < sim_dev->port_num; i++) {
  1087. edid_entry = devm_kzalloc(sim_dev->dev,
  1088. sizeof(*edid_entry), GFP_KERNEL);
  1089. edid_entry->index = i;
  1090. edid_entry->sim_dev = sim_dev;
  1091. scnprintf(name, sizeof(name), "edid-%d", i);
  1092. file = debugfs_create_file(name,
  1093. 0444,
  1094. edid_dir,
  1095. edid_entry,
  1096. &sim_edid_fops);
  1097. if (IS_ERR_OR_NULL(file)) {
  1098. rc = PTR_ERR(file);
  1099. DP_ERR("[%s] debugfs create edid failed, rc=%d\n",
  1100. sim_dev->label, rc);
  1101. goto error_remove_dir;
  1102. }
  1103. scnprintf(name, sizeof(name), "hpd-%d", i);
  1104. file = debugfs_create_file(name,
  1105. 0444,
  1106. edid_dir,
  1107. edid_entry,
  1108. &sim_mst_hpd_fops);
  1109. if (IS_ERR_OR_NULL(file)) {
  1110. rc = PTR_ERR(file);
  1111. DP_ERR("[%s] debugfs create hpd failed, rc=%d\n",
  1112. sim_dev->label, rc);
  1113. goto error_remove_dir;
  1114. }
  1115. }
  1116. file = debugfs_create_symlink("edid", dir, "./mst_edid/edid-0");
  1117. if (IS_ERR_OR_NULL(file)) {
  1118. rc = PTR_ERR(file);
  1119. DP_ERR("[%s] debugfs create edid link failed, rc=%d\n",
  1120. sim_dev->label, rc);
  1121. goto error_remove_dir;
  1122. }
  1123. file = debugfs_create_file("dpcd",
  1124. 0444,
  1125. dir,
  1126. sim_dev,
  1127. &sim_dpcd_fops);
  1128. if (IS_ERR_OR_NULL(file)) {
  1129. rc = PTR_ERR(file);
  1130. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1131. sim_dev->label, rc);
  1132. goto error_remove_dir;
  1133. }
  1134. file = debugfs_create_file("hpd",
  1135. 0444,
  1136. dir,
  1137. sim_dev,
  1138. &sim_hpd_fops);
  1139. if (IS_ERR_OR_NULL(file)) {
  1140. rc = PTR_ERR(file);
  1141. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1142. sim_dev->label, rc);
  1143. goto error_remove_dir;
  1144. }
  1145. file = debugfs_create_file("skip_link_training",
  1146. 0444,
  1147. dir,
  1148. sim_dev,
  1149. &sim_skip_link_training_fops);
  1150. if (IS_ERR_OR_NULL(file)) {
  1151. rc = PTR_ERR(file);
  1152. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1153. sim_dev->label, rc);
  1154. goto error_remove_dir;
  1155. }
  1156. file = debugfs_create_file("skip_edid",
  1157. 0444,
  1158. dir,
  1159. sim_dev,
  1160. &sim_skip_edid_fops);
  1161. if (IS_ERR_OR_NULL(file)) {
  1162. rc = PTR_ERR(file);
  1163. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1164. sim_dev->label, rc);
  1165. goto error_remove_dir;
  1166. }
  1167. file = debugfs_create_file("skip_dpcd_read",
  1168. 0444,
  1169. dir,
  1170. sim_dev,
  1171. &sim_skip_dpcd_fops);
  1172. if (IS_ERR_OR_NULL(file)) {
  1173. rc = PTR_ERR(file);
  1174. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1175. sim_dev->label, rc);
  1176. goto error_remove_dir;
  1177. }
  1178. file = debugfs_create_file("skip_dpcd_write",
  1179. 0444,
  1180. dir,
  1181. sim_dev,
  1182. &sim_skip_config_fops);
  1183. if (IS_ERR_OR_NULL(file)) {
  1184. rc = PTR_ERR(file);
  1185. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1186. sim_dev->label, rc);
  1187. goto error_remove_dir;
  1188. }
  1189. file = debugfs_create_file("mst_sideband_mode",
  1190. 0444,
  1191. dir,
  1192. sim_dev,
  1193. &sim_mst_mode_fops);
  1194. if (IS_ERR_OR_NULL(file)) {
  1195. rc = PTR_ERR(file);
  1196. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1197. sim_dev->label, rc);
  1198. goto error_remove_dir;
  1199. }
  1200. sim_dev->debugfs_dir = dir;
  1201. sim_dev->debugfs_edid_dir = edid_dir;
  1202. return 0;
  1203. error_remove_dir:
  1204. debugfs_remove_recursive(dir);
  1205. error:
  1206. return rc;
  1207. }
  1208. static int dp_sim_parse(struct dp_sim_device *sim_dev)
  1209. {
  1210. int rc;
  1211. sim_dev->label = of_get_property(sim_dev->bridge.of_node,
  1212. "label", NULL);
  1213. rc = dp_sim_parse_dpcd(sim_dev);
  1214. if (rc) {
  1215. DP_ERR("failed to parse DPCD nodes\n");
  1216. return rc;
  1217. }
  1218. rc = dp_sim_parse_edid(sim_dev);
  1219. if (rc) {
  1220. DP_ERR("failed to parse EDID nodes\n");
  1221. return rc;
  1222. }
  1223. rc = dp_sim_parse_misc(sim_dev);
  1224. if (rc) {
  1225. DP_ERR("failed to parse misc nodes\n");
  1226. return rc;
  1227. }
  1228. return 0;
  1229. }
  1230. int dp_sim_create_bridge(struct device *dev, struct dp_aux_bridge **bridge)
  1231. {
  1232. struct dp_sim_device *dp_sim_dev;
  1233. struct dp_mst_sim_cfg cfg;
  1234. int ret;
  1235. dp_sim_dev = devm_kzalloc(dev, sizeof(*dp_sim_dev), GFP_KERNEL);
  1236. if (!dp_sim_dev)
  1237. return -ENOMEM;
  1238. dp_sim_dev->dev = dev;
  1239. dp_sim_dev->bridge.of_node = dev->of_node;
  1240. dp_sim_dev->bridge.register_hpd = dp_sim_register_hpd;
  1241. dp_sim_dev->bridge.transfer = dp_sim_transfer;
  1242. dp_sim_dev->bridge.dev_priv = dp_sim_dev;
  1243. dp_sim_dev->bridge.flag = DP_AUX_BRIDGE_MST | DP_SIM_BRIDGE_PRIV_FLAG;
  1244. INIT_LIST_HEAD(&dp_sim_dev->dpcd_reg_list);
  1245. mutex_init(&dp_sim_dev->lock);
  1246. memset(&cfg, 0, sizeof(cfg));
  1247. cfg.host_dev = dp_sim_dev;
  1248. cfg.host_hpd_irq = dp_sim_host_hpd_irq;
  1249. ret = dp_mst_sim_create(&cfg, &dp_sim_dev->bridge.mst_ctx);
  1250. if (ret) {
  1251. devm_kfree(dev, dp_sim_dev);
  1252. return ret;
  1253. }
  1254. /* default dpcd reg value */
  1255. dp_sim_dev->dpcd_reg[DP_DPCD_REV] = 0x12;
  1256. dp_sim_dev->dpcd_reg[DP_MAX_LINK_RATE] = 0x14;
  1257. dp_sim_dev->dpcd_reg[DP_MAX_LANE_COUNT] = 0xc4;
  1258. dp_sim_dev->dpcd_reg[DP_SINK_COUNT] = 0x1;
  1259. dp_sim_dev->dpcd_reg[DP_LANE0_1_STATUS] = 0x77;
  1260. dp_sim_dev->dpcd_reg[DP_LANE2_3_STATUS] = 0x77;
  1261. dp_sim_dev->dpcd_reg[DP_LANE_ALIGN_STATUS_UPDATED] = 0x1;
  1262. dp_sim_dev->dpcd_reg[DP_SINK_STATUS] = 0x3;
  1263. dp_sim_dev->dpcd_reg[DP_PAYLOAD_TABLE_UPDATE_STATUS] = 0x3;
  1264. /* enable link training by default */
  1265. dp_sim_dev->skip_link_training = true;
  1266. dp_sim_dev->link_training_cnt = (u32)-1;
  1267. *bridge = &dp_sim_dev->bridge;
  1268. return 0;
  1269. }
  1270. int dp_sim_destroy_bridge(struct dp_aux_bridge *bridge)
  1271. {
  1272. struct dp_sim_device *dp_sim_dev;
  1273. struct dp_sim_dpcd_reg *reg, *p;
  1274. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  1275. return -EINVAL;
  1276. dp_sim_dev = to_dp_sim_dev(bridge);
  1277. dp_mst_sim_destroy(dp_sim_dev->bridge.mst_ctx);
  1278. list_for_each_entry_safe(reg, p, &dp_sim_dev->dpcd_reg_list, head) {
  1279. list_del(&reg->head);
  1280. devm_kfree(dp_sim_dev->dev, reg);
  1281. }
  1282. if (dp_sim_dev->ports)
  1283. devm_kfree(dp_sim_dev->dev, dp_sim_dev->ports);
  1284. devm_kfree(dp_sim_dev->dev, dp_sim_dev);
  1285. return 0;
  1286. }
  1287. int dp_sim_probe(struct platform_device *pdev)
  1288. {
  1289. struct dp_sim_device *dp_sim_dev;
  1290. struct dp_aux_bridge *bridge;
  1291. int ret;
  1292. ret = dp_sim_create_bridge(&pdev->dev, &bridge);
  1293. if (ret)
  1294. return ret;
  1295. dp_sim_dev = to_dp_sim_dev(bridge);
  1296. ret = dp_sim_parse(dp_sim_dev);
  1297. if (ret)
  1298. goto fail;
  1299. if (dp_sim_dev->skip_hpd)
  1300. dp_sim_dev->bridge.flag |= DP_AUX_BRIDGE_HPD;
  1301. ret = dp_mst_sim_update(dp_sim_dev->bridge.mst_ctx,
  1302. dp_sim_dev->port_num, dp_sim_dev->ports);
  1303. if (ret)
  1304. goto fail;
  1305. ret = dp_sim_debug_init(dp_sim_dev);
  1306. if (ret)
  1307. goto fail;
  1308. ret = dp_aux_add_bridge(&dp_sim_dev->bridge);
  1309. if (ret)
  1310. goto fail;
  1311. platform_set_drvdata(pdev, dp_sim_dev);
  1312. return 0;
  1313. fail:
  1314. dp_sim_destroy_bridge(bridge);
  1315. return ret;
  1316. }
  1317. int dp_sim_remove(struct platform_device *pdev)
  1318. {
  1319. struct dp_sim_device *dp_sim_dev;
  1320. dp_sim_dev = platform_get_drvdata(pdev);
  1321. if (!dp_sim_dev)
  1322. return 0;
  1323. debugfs_remove_recursive(dp_sim_dev->debugfs_dir);
  1324. dp_sim_destroy_bridge(&dp_sim_dev->bridge);
  1325. return 0;
  1326. }