dp_mst_sim.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * Copyright (c) 2021-2023 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. mutex_lock(&sim_dev->lock);
  282. sim_dev->sim_mode = sim_mode;
  283. mutex_unlock(&sim_dev->lock);
  284. return 0;
  285. }
  286. int dp_sim_update_port_num(struct dp_aux_bridge *bridge, u32 port_num)
  287. {
  288. struct dp_sim_device *sim_dev;
  289. struct dp_mst_sim_port *ports;
  290. u32 i, rc;
  291. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  292. return -EINVAL;
  293. sim_dev = to_dp_sim_dev(bridge);
  294. DP_INFO("Update port count from %d to %d\n", sim_dev->port_num, port_num);
  295. mutex_lock(&sim_dev->lock);
  296. if (port_num > sim_dev->port_num) {
  297. ports = devm_kzalloc(sim_dev->dev,
  298. port_num * sizeof(*ports), GFP_KERNEL);
  299. if (!ports) {
  300. rc = -ENOMEM;
  301. goto bail;
  302. }
  303. memcpy(ports, sim_dev->ports,
  304. sim_dev->port_num * sizeof(*ports));
  305. if (sim_dev->ports)
  306. devm_kfree(sim_dev->dev, sim_dev->ports);
  307. sim_dev->ports = ports;
  308. for (i = sim_dev->port_num; i < port_num; i++) {
  309. memcpy(&ports[i], &output_port, sizeof(*ports));
  310. ports[i].peer_guid[0] = i;
  311. }
  312. }
  313. sim_dev->port_num = port_num;
  314. rc = dp_mst_sim_update(sim_dev->bridge.mst_ctx,
  315. port_num, sim_dev->ports);
  316. if (rc)
  317. goto bail;
  318. sim_dev->current_port_num = port_num;
  319. bail:
  320. mutex_unlock(&sim_dev->lock);
  321. return rc;
  322. }
  323. int dp_sim_update_port_status(struct dp_aux_bridge *bridge,
  324. int port, enum drm_connector_status status)
  325. {
  326. struct dp_sim_device *sim_dev;
  327. int rc;
  328. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  329. return -EINVAL;
  330. sim_dev = to_dp_sim_dev(bridge);
  331. mutex_lock(&sim_dev->lock);
  332. if (port < 0 || port >= sim_dev->current_port_num) {
  333. rc = -EINVAL;
  334. goto bail;
  335. }
  336. sim_dev->ports[port].pdt = (status == connector_status_connected) ?
  337. DP_PEER_DEVICE_SST_SINK : DP_PEER_DEVICE_NONE;
  338. rc = dp_mst_sim_update(sim_dev->bridge.mst_ctx, sim_dev->current_port_num, sim_dev->ports);
  339. bail:
  340. mutex_unlock(&sim_dev->lock);
  341. return rc;
  342. }
  343. int dp_sim_update_port_edid(struct dp_aux_bridge *bridge,
  344. int port, const u8 *edid, u32 size)
  345. {
  346. struct dp_sim_device *sim_dev;
  347. struct dp_mst_sim_port *sim_port;
  348. int rc;
  349. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  350. return -EINVAL;
  351. sim_dev = to_dp_sim_dev(bridge);
  352. mutex_lock(&sim_dev->lock);
  353. if (port < 0 || port >= sim_dev->current_port_num) {
  354. rc = -EINVAL;
  355. goto bail;
  356. }
  357. sim_port = &sim_dev->ports[port];
  358. if (size != sim_port->edid_size) {
  359. if (sim_port->edid)
  360. devm_kfree(sim_dev->dev, (u8 *)sim_port->edid);
  361. sim_port->edid = devm_kzalloc(sim_dev->dev,
  362. size, GFP_KERNEL);
  363. if (!sim_port->edid)
  364. return -ENOMEM;
  365. sim_port->edid_size = size;
  366. }
  367. memcpy((u8 *)sim_port->edid, edid, size);
  368. rc = dp_mst_sim_update(sim_dev->bridge.mst_ctx, sim_dev->current_port_num, sim_dev->ports);
  369. bail:
  370. mutex_unlock(&sim_dev->lock);
  371. return rc;
  372. }
  373. int dp_sim_write_dpcd_reg(struct dp_aux_bridge *bridge,
  374. const u8 *dpcd, u32 size, u32 offset)
  375. {
  376. struct dp_sim_device *sim_dev;
  377. int i;
  378. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  379. return -EINVAL;
  380. sim_dev = to_dp_sim_dev(bridge);
  381. mutex_lock(&sim_dev->lock);
  382. for (i = 0; i < size; i++)
  383. dp_sim_write_dpcd(sim_dev, offset + i, dpcd[i]);
  384. mutex_unlock(&sim_dev->lock);
  385. return 0;
  386. }
  387. int dp_sim_read_dpcd_reg(struct dp_aux_bridge *bridge,
  388. u8 *dpcd, u32 size, u32 offset)
  389. {
  390. struct dp_sim_device *sim_dev;
  391. int rc;
  392. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  393. return -EINVAL;
  394. sim_dev = to_dp_sim_dev(bridge);
  395. mutex_lock(&sim_dev->lock);
  396. rc = dp_sim_read_dpcd_regs(sim_dev, dpcd, size, offset);
  397. mutex_unlock(&sim_dev->lock);
  398. return rc;
  399. }
  400. static void dp_sim_update_dtd(struct edid *edid,
  401. struct drm_display_mode *mode)
  402. {
  403. struct detailed_timing *dtd = &edid->detailed_timings[0];
  404. struct detailed_pixel_timing *pd = &dtd->data.pixel_data;
  405. u32 h_blank = mode->htotal - mode->hdisplay;
  406. u32 v_blank = mode->vtotal - mode->vdisplay;
  407. u32 h_img = 0, v_img = 0;
  408. dtd->pixel_clock = cpu_to_le16(mode->clock / 10);
  409. pd->hactive_lo = mode->hdisplay & 0xFF;
  410. pd->hblank_lo = h_blank & 0xFF;
  411. pd->hactive_hblank_hi = ((h_blank >> 8) & 0xF) |
  412. ((mode->hdisplay >> 8) & 0xF) << 4;
  413. pd->vactive_lo = mode->vdisplay & 0xFF;
  414. pd->vblank_lo = v_blank & 0xFF;
  415. pd->vactive_vblank_hi = ((v_blank >> 8) & 0xF) |
  416. ((mode->vdisplay >> 8) & 0xF) << 4;
  417. pd->hsync_offset_lo =
  418. (mode->hsync_start - mode->hdisplay) & 0xFF;
  419. pd->hsync_pulse_width_lo =
  420. (mode->hsync_end - mode->hsync_start) & 0xFF;
  421. pd->vsync_offset_pulse_width_lo =
  422. (((mode->vsync_start - mode->vdisplay) & 0xF) << 4) |
  423. ((mode->vsync_end - mode->vsync_start) & 0xF);
  424. pd->hsync_vsync_offset_pulse_width_hi =
  425. ((((mode->hsync_start - mode->hdisplay) >> 8) & 0x3) << 6) |
  426. ((((mode->hsync_end - mode->hsync_start) >> 8) & 0x3) << 4) |
  427. ((((mode->vsync_start - mode->vdisplay) >> 4) & 0x3) << 2) |
  428. ((((mode->vsync_end - mode->vsync_start) >> 4) & 0x3) << 0);
  429. pd->width_mm_lo = h_img & 0xFF;
  430. pd->height_mm_lo = v_img & 0xFF;
  431. pd->width_height_mm_hi = (((h_img >> 8) & 0xF) << 4) |
  432. ((v_img >> 8) & 0xF);
  433. pd->hborder = 0;
  434. pd->vborder = 0;
  435. pd->misc = 0;
  436. }
  437. static void dp_sim_update_checksum(struct edid *edid)
  438. {
  439. u8 *data = (u8 *)edid;
  440. u32 i, sum = 0;
  441. for (i = 0; i < EDID_LENGTH - 1; i++)
  442. sum += data[i];
  443. edid->checksum = 0x100 - (sum & 0xFF);
  444. }
  445. static int dp_sim_parse_edid_from_node(struct dp_sim_device *sim_dev,
  446. int index, struct device_node *node)
  447. {
  448. struct dp_mst_sim_port *port;
  449. struct drm_display_mode mode_buf, *mode = &mode_buf;
  450. u16 h_front_porch, h_pulse_width, h_back_porch;
  451. u16 v_front_porch, v_pulse_width, v_back_porch;
  452. bool h_active_high, v_active_high;
  453. u32 flags = 0;
  454. int rc;
  455. struct edid *edid;
  456. const u8 edid_buf[EDID_LENGTH] = {
  457. 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x44, 0x6D,
  458. 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1B, 0x10, 0x01, 0x03,
  459. 0x80, 0x50, 0x2D, 0x78, 0x0A, 0x0D, 0xC9, 0xA0, 0x57, 0x47,
  460. 0x98, 0x27, 0x12, 0x48, 0x4C, 0x00, 0x00, 0x00, 0x01, 0x01,
  461. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  462. 0x01, 0x01, 0x01, 0x01,
  463. };
  464. rc = of_property_read_u16(node, "qcom,mode-h-active",
  465. &mode->hdisplay);
  466. if (rc) {
  467. DP_ERR("failed to read h-active, rc=%d\n", rc);
  468. goto fail;
  469. }
  470. rc = of_property_read_u16(node, "qcom,mode-h-front-porch",
  471. &h_front_porch);
  472. if (rc) {
  473. DP_ERR("failed to read h-front-porch, rc=%d\n", rc);
  474. goto fail;
  475. }
  476. rc = of_property_read_u16(node, "qcom,mode-h-pulse-width",
  477. &h_pulse_width);
  478. if (rc) {
  479. DP_ERR("failed to read h-pulse-width, rc=%d\n", rc);
  480. goto fail;
  481. }
  482. rc = of_property_read_u16(node, "qcom,mode-h-back-porch",
  483. &h_back_porch);
  484. if (rc) {
  485. DP_ERR("failed to read h-back-porch, rc=%d\n", rc);
  486. goto fail;
  487. }
  488. h_active_high = of_property_read_bool(node,
  489. "qcom,mode-h-active-high");
  490. rc = of_property_read_u16(node, "qcom,mode-v-active",
  491. &mode->vdisplay);
  492. if (rc) {
  493. DP_ERR("failed to read v-active, rc=%d\n", rc);
  494. goto fail;
  495. }
  496. rc = of_property_read_u16(node, "qcom,mode-v-front-porch",
  497. &v_front_porch);
  498. if (rc) {
  499. DP_ERR("failed to read v-front-porch, rc=%d\n", rc);
  500. goto fail;
  501. }
  502. rc = of_property_read_u16(node, "qcom,mode-v-pulse-width",
  503. &v_pulse_width);
  504. if (rc) {
  505. DP_ERR("failed to read v-pulse-width, rc=%d\n", rc);
  506. goto fail;
  507. }
  508. rc = of_property_read_u16(node, "qcom,mode-v-back-porch",
  509. &v_back_porch);
  510. if (rc) {
  511. DP_ERR("failed to read v-back-porch, rc=%d\n", rc);
  512. goto fail;
  513. }
  514. v_active_high = of_property_read_bool(node,
  515. "qcom,mode-v-active-high");
  516. rc = of_property_read_u32(node, "qcom,mode-clock-in-khz",
  517. &mode->clock);
  518. if (rc) {
  519. DP_ERR("failed to read clock, rc=%d\n", rc);
  520. goto fail;
  521. }
  522. mode->hsync_start = mode->hdisplay + h_front_porch;
  523. mode->hsync_end = mode->hsync_start + h_pulse_width;
  524. mode->htotal = mode->hsync_end + h_back_porch;
  525. mode->vsync_start = mode->vdisplay + v_front_porch;
  526. mode->vsync_end = mode->vsync_start + v_pulse_width;
  527. mode->vtotal = mode->vsync_end + v_back_porch;
  528. if (h_active_high)
  529. flags |= DRM_MODE_FLAG_PHSYNC;
  530. else
  531. flags |= DRM_MODE_FLAG_NHSYNC;
  532. if (v_active_high)
  533. flags |= DRM_MODE_FLAG_PVSYNC;
  534. else
  535. flags |= DRM_MODE_FLAG_NVSYNC;
  536. mode->flags = flags;
  537. edid = devm_kzalloc(sim_dev->dev, sizeof(*edid), GFP_KERNEL);
  538. if (!edid) {
  539. rc = -ENOMEM;
  540. goto fail;
  541. }
  542. memcpy(edid, edid_buf, sizeof(edid_buf));
  543. dp_sim_update_dtd(edid, mode);
  544. dp_sim_update_checksum(edid);
  545. port = &sim_dev->ports[index];
  546. memcpy(port, &output_port, sizeof(*port));
  547. port->peer_guid[0] = index;
  548. if (port->edid)
  549. devm_kfree(sim_dev->dev, (u8 *)port->edid);
  550. port->edid = (u8 *)edid;
  551. port->edid_size = sizeof(*edid);
  552. fail:
  553. return rc;
  554. }
  555. static int dp_sim_parse_edid_from_data(struct dp_sim_device *sim_dev,
  556. int index, const char *data, int len)
  557. {
  558. struct dp_mst_sim_port *port;
  559. u8 *edid_data;
  560. edid_data = devm_kzalloc(sim_dev->dev, len, GFP_KERNEL);
  561. if (!edid_data)
  562. return -ENOMEM;
  563. memcpy(edid_data, data, len);
  564. port = &sim_dev->ports[index];
  565. memcpy(port, &output_port, sizeof(*port));
  566. port->peer_guid[0] = index;
  567. if (port->edid)
  568. devm_kfree(sim_dev->dev, (u8 *)port->edid);
  569. port->edid = edid_data;
  570. port->edid_size = len;
  571. return 0;
  572. }
  573. static int dp_sim_parse_edid(struct dp_sim_device *sim_dev)
  574. {
  575. struct dp_mst_sim_port *ports;
  576. struct device_node *of_node = sim_dev->bridge.of_node;
  577. struct device_node *node;
  578. const char *data;
  579. int rc, port_num, i, len;
  580. port_num = of_get_child_count(of_node);
  581. if (!port_num)
  582. port_num = 1;
  583. if (port_num >= 15)
  584. return -EINVAL;
  585. ports = devm_kzalloc(sim_dev->dev,
  586. port_num * sizeof(*ports), GFP_KERNEL);
  587. if (!ports)
  588. return -ENOMEM;
  589. sim_dev->ports = ports;
  590. sim_dev->port_num = port_num;
  591. sim_dev->current_port_num = port_num;
  592. i = 0;
  593. for_each_child_of_node(of_node, node) {
  594. data = of_get_property(node, "qcom,edid", &len);
  595. if (data)
  596. rc = dp_sim_parse_edid_from_data(sim_dev, i,
  597. data, len);
  598. else
  599. rc = dp_sim_parse_edid_from_node(sim_dev, i,
  600. node);
  601. if (rc)
  602. return rc;
  603. i++;
  604. }
  605. if (i == 0)
  606. memcpy(ports, &output_port, sizeof(*ports));
  607. return 0;
  608. }
  609. static int dp_sim_parse_dpcd(struct dp_sim_device *sim_dev)
  610. {
  611. struct device_node *node = sim_dev->bridge.of_node;
  612. u32 val, i;
  613. const __be32 *arr;
  614. int rc;
  615. rc = of_property_read_u32(node, "qcom,dpcd-max-rate", &val);
  616. if (!rc)
  617. sim_dev->dpcd_reg[DP_MAX_LINK_RATE] = val;
  618. rc = of_property_read_u32(node, "qcom,dpcd-max-lane", &val);
  619. if (!rc)
  620. sim_dev->dpcd_reg[DP_MAX_LANE_COUNT] = val;
  621. rc = of_property_read_u32(node, "qcom,dpcd-mst", &val);
  622. if (!rc)
  623. sim_dev->dpcd_reg[DP_MSTM_CAP] = val;
  624. arr = of_get_property(node, "qcom,dpcd-regs", &val);
  625. if (arr) {
  626. val /= sizeof(u32);
  627. val &= ~0x1;
  628. for (i = 0; i < val; i += 2)
  629. dp_sim_write_dpcd(sim_dev,
  630. be32_to_cpu(arr[i]),
  631. be32_to_cpu(arr[i+1]));
  632. }
  633. rc = of_property_read_u32(node, "qcom,voltage-swing", &val);
  634. if (!rc)
  635. for (i = 0; i < 4; i++) {
  636. sim_dev->dpcd_reg[DP_TRAINING_LANE0_SET + i] |=
  637. val;
  638. sim_dev->dpcd_reg[DP_ADJUST_REQUEST_LANE0_1 + (i/2)] |=
  639. (val & 0x3) << ((i & 0x1) << 2);
  640. }
  641. rc = of_property_read_u32(node, "qcom,pre-emphasis", &val);
  642. if (!rc)
  643. for (i = 0; i < 4; i++) {
  644. sim_dev->dpcd_reg[DP_TRAINING_LANE0_SET + i] |=
  645. val << 3;
  646. sim_dev->dpcd_reg[DP_ADJUST_REQUEST_LANE0_1 + (i/2)] |=
  647. (val & 0x3) << (((i & 0x1) << 2) + 2);
  648. }
  649. rc = of_property_read_u32(node, "qcom,link-training-cnt", &val);
  650. if (!rc)
  651. sim_dev->link_training_cnt = val;
  652. else
  653. sim_dev->link_training_cnt = 0;
  654. return 0;
  655. }
  656. static int dp_sim_parse_misc(struct dp_sim_device *sim_dev)
  657. {
  658. struct device_node *node = sim_dev->bridge.of_node;
  659. sim_dev->skip_edid = of_property_read_bool(node,
  660. "qcom,skip-edid");
  661. sim_dev->skip_dpcd = of_property_read_bool(node,
  662. "qcom,skip-dpcd-read");
  663. sim_dev->skip_link_training = of_property_read_bool(node,
  664. "qcom,skip-link-training");
  665. sim_dev->skip_config = of_property_read_bool(node,
  666. "qcom,skip-dpcd-write");
  667. sim_dev->skip_hpd = of_property_read_bool(node,
  668. "qcom,skip-hpd");
  669. sim_dev->skip_mst = of_property_read_bool(node,
  670. "qcom,skip-mst");
  671. DP_DEBUG("skip: edid=%d dpcd=%d LT=%d config=%d hpd=%d mst=%d\n",
  672. sim_dev->skip_edid,
  673. sim_dev->skip_dpcd,
  674. sim_dev->skip_link_training,
  675. sim_dev->skip_config,
  676. sim_dev->skip_hpd,
  677. sim_dev->skip_mst);
  678. return 0;
  679. }
  680. static ssize_t dp_sim_debug_write_edid(struct file *file,
  681. const char __user *user_buff, size_t count, loff_t *ppos)
  682. {
  683. struct dp_sim_debug_edid_entry *entry = file->private_data;
  684. struct dp_sim_device *debug;
  685. struct dp_mst_sim_port *port;
  686. u8 *buf = NULL, *buf_t = NULL;
  687. const int char_to_nib = 2;
  688. size_t edid_size = 0;
  689. size_t size = 0, edid_buf_index = 0;
  690. ssize_t rc = count;
  691. if (!entry)
  692. return -ENODEV;
  693. debug = entry->sim_dev;
  694. if (!debug || entry->index >= debug->port_num)
  695. return -EINVAL;
  696. port = &debug->ports[entry->index];
  697. mutex_lock(&debug->lock);
  698. if (*ppos)
  699. goto bail;
  700. size = min_t(size_t, count, SZ_1K);
  701. buf = kzalloc(size, GFP_KERNEL);
  702. if (ZERO_OR_NULL_PTR(buf)) {
  703. rc = -ENOMEM;
  704. goto bail;
  705. }
  706. if (copy_from_user(buf, user_buff, size))
  707. goto bail;
  708. edid_size = size / char_to_nib;
  709. buf_t = buf;
  710. if (edid_size != port->edid_size) {
  711. if (port->edid)
  712. devm_kfree(debug->dev, (u8 *)port->edid);
  713. port->edid = devm_kzalloc(debug->dev,
  714. edid_size, GFP_KERNEL);
  715. if (!port->edid) {
  716. rc = -ENOMEM;
  717. goto bail;
  718. }
  719. port->edid_size = edid_size;
  720. }
  721. while (edid_size--) {
  722. char t[3];
  723. int d;
  724. memcpy(t, buf_t, sizeof(char) * char_to_nib);
  725. t[char_to_nib] = '\0';
  726. if (kstrtoint(t, 16, &d)) {
  727. DP_ERR("kstrtoint error\n");
  728. goto bail;
  729. }
  730. if (port->edid && (edid_buf_index < port->edid_size))
  731. ((u8 *)port->edid)[edid_buf_index++] = d;
  732. buf_t += char_to_nib;
  733. }
  734. if (debug->skip_mst)
  735. dp_mst_sim_update(debug->bridge.mst_ctx,
  736. debug->port_num, debug->ports);
  737. debug->skip_edid = true;
  738. bail:
  739. kfree(buf);
  740. mutex_unlock(&debug->lock);
  741. return rc;
  742. }
  743. static ssize_t dp_sim_debug_write_dpcd(struct file *file,
  744. const char __user *user_buff, size_t count, loff_t *ppos)
  745. {
  746. struct dp_sim_device *debug = file->private_data;
  747. u8 *buf = NULL, *buf_t = NULL;
  748. const int char_to_nib = 2;
  749. size_t dpcd_size = 0;
  750. size_t size = 0, dpcd_buf_index = 0;
  751. ssize_t rc = count;
  752. char offset_ch[5];
  753. u32 offset, data_len;
  754. if (!debug)
  755. return -ENODEV;
  756. mutex_lock(&debug->lock);
  757. if (*ppos)
  758. goto bail;
  759. size = min_t(size_t, count, SZ_2K);
  760. if (size < 4)
  761. goto bail;
  762. buf = kzalloc(size, GFP_KERNEL);
  763. if (ZERO_OR_NULL_PTR(buf)) {
  764. rc = -ENOMEM;
  765. goto bail;
  766. }
  767. if (copy_from_user(buf, user_buff, size))
  768. goto bail;
  769. memcpy(offset_ch, buf, 4);
  770. offset_ch[4] = '\0';
  771. if (kstrtoint(offset_ch, 16, &offset)) {
  772. DP_ERR("offset kstrtoint error\n");
  773. goto bail;
  774. }
  775. if (offset == 0xFFFF) {
  776. DP_ERR("clearing dpcd\n");
  777. memset(debug->dpcd_reg, 0, sizeof(debug->dpcd_reg));
  778. goto bail;
  779. }
  780. size -= 4;
  781. if (size == 0)
  782. goto bail;
  783. dpcd_size = size / char_to_nib;
  784. data_len = dpcd_size;
  785. buf_t = buf + 4;
  786. dpcd_buf_index = offset;
  787. while (dpcd_size--) {
  788. char t[3];
  789. int d;
  790. memcpy(t, buf_t, sizeof(char) * char_to_nib);
  791. t[char_to_nib] = '\0';
  792. if (kstrtoint(t, 16, &d)) {
  793. DP_ERR("kstrtoint error\n");
  794. goto bail;
  795. }
  796. dp_sim_write_dpcd(debug, dpcd_buf_index, d);
  797. dpcd_buf_index++;
  798. buf_t += char_to_nib;
  799. }
  800. debug->skip_dpcd = true;
  801. debug->skip_config = true;
  802. bail:
  803. kfree(buf);
  804. mutex_unlock(&debug->lock);
  805. return rc;
  806. }
  807. static ssize_t dp_sim_debug_read_dpcd(struct file *file,
  808. char __user *user_buff, size_t count, loff_t *ppos)
  809. {
  810. struct dp_sim_device *debug = file->private_data;
  811. char *buf;
  812. int const buf_size = SZ_4K;
  813. u32 offset = 0;
  814. u32 len = 0;
  815. if (!debug)
  816. return -ENODEV;
  817. if (*ppos)
  818. return 0;
  819. buf = kzalloc(buf_size, GFP_KERNEL);
  820. if (!buf)
  821. return -ENOMEM;
  822. mutex_lock(&debug->lock);
  823. len += snprintf(buf, buf_size, "0x%x", debug->dpcd_write_addr);
  824. while (1) {
  825. if (debug->dpcd_write_addr + offset >= buf_size ||
  826. offset >= debug->dpcd_write_size)
  827. break;
  828. len += snprintf(buf + len, buf_size - len, "0x%x",
  829. debug->dpcd_reg[debug->dpcd_write_addr + offset++]);
  830. }
  831. mutex_unlock(&debug->lock);
  832. len = min_t(size_t, count, len);
  833. if (!copy_to_user(user_buff, buf, len))
  834. *ppos += len;
  835. kfree(buf);
  836. return len;
  837. }
  838. static ssize_t dp_sim_debug_write_hpd(struct file *file,
  839. const char __user *user_buff, size_t count, loff_t *ppos)
  840. {
  841. struct dp_sim_device *debug = file->private_data;
  842. char buf[SZ_8];
  843. size_t len = 0;
  844. int hpd = 0;
  845. if (!debug)
  846. return -ENODEV;
  847. if (*ppos)
  848. return 0;
  849. len = min_t(size_t, count, SZ_8 - 1);
  850. if (copy_from_user(buf, user_buff, len))
  851. goto end;
  852. buf[len] = '\0';
  853. if (kstrtoint(buf, 10, &hpd) != 0)
  854. goto end;
  855. mutex_lock(&debug->lock);
  856. if (debug->hpd_cb)
  857. debug->hpd_cb(debug->host_dev, !!hpd, false);
  858. mutex_unlock(&debug->lock);
  859. end:
  860. return len;
  861. }
  862. static ssize_t dp_sim_debug_write_skip_link_training(struct file *file,
  863. const char __user *user_buff, size_t count, loff_t *ppos)
  864. {
  865. struct dp_sim_device *debug = file->private_data;
  866. char buf[SZ_8];
  867. size_t len = 0;
  868. int skip_lk, lk_cnt;
  869. if (!debug)
  870. return -ENODEV;
  871. if (*ppos)
  872. return 0;
  873. len = min_t(size_t, count, SZ_8 - 1);
  874. if (copy_from_user(buf, user_buff, len))
  875. goto end;
  876. buf[len] = '\0';
  877. if (sscanf(buf, "%d %u", &skip_lk, &lk_cnt) != 2) {
  878. DP_ERR("invalid input\n");
  879. return -EINVAL;
  880. }
  881. mutex_lock(&debug->lock);
  882. debug->skip_link_training = !!skip_lk;
  883. debug->link_training_cnt = lk_cnt;
  884. mutex_unlock(&debug->lock);
  885. end:
  886. return len;
  887. }
  888. static ssize_t dp_sim_debug_write_skip_edid(struct file *file,
  889. const char __user *user_buff, size_t count, loff_t *ppos)
  890. {
  891. struct dp_sim_device *debug = file->private_data;
  892. char buf[SZ_8];
  893. size_t len = 0;
  894. int val = 0;
  895. if (!debug)
  896. return -ENODEV;
  897. if (*ppos)
  898. return 0;
  899. len = min_t(size_t, count, SZ_8 - 1);
  900. if (copy_from_user(buf, user_buff, len))
  901. goto end;
  902. buf[len] = '\0';
  903. if (kstrtoint(buf, 10, &val) != 0)
  904. goto end;
  905. mutex_lock(&debug->lock);
  906. debug->skip_edid = !!val;
  907. mutex_unlock(&debug->lock);
  908. end:
  909. return len;
  910. }
  911. static ssize_t dp_sim_debug_write_skip_dpcd(struct file *file,
  912. const char __user *user_buff, size_t count, loff_t *ppos)
  913. {
  914. struct dp_sim_device *debug = file->private_data;
  915. char buf[SZ_8];
  916. size_t len = 0;
  917. int val = 0;
  918. if (!debug)
  919. return -ENODEV;
  920. if (*ppos)
  921. return 0;
  922. len = min_t(size_t, count, SZ_8 - 1);
  923. if (copy_from_user(buf, user_buff, len))
  924. goto end;
  925. buf[len] = '\0';
  926. if (kstrtoint(buf, 10, &val) != 0)
  927. goto end;
  928. mutex_lock(&debug->lock);
  929. debug->skip_dpcd = !!val;
  930. mutex_unlock(&debug->lock);
  931. end:
  932. return len;
  933. }
  934. static ssize_t dp_sim_debug_write_skip_config(struct file *file,
  935. const char __user *user_buff, size_t count, loff_t *ppos)
  936. {
  937. struct dp_sim_device *debug = file->private_data;
  938. char buf[SZ_8];
  939. size_t len = 0;
  940. int val = 0;
  941. if (!debug)
  942. return -ENODEV;
  943. if (*ppos)
  944. return 0;
  945. len = min_t(size_t, count, SZ_8 - 1);
  946. if (copy_from_user(buf, user_buff, len))
  947. goto end;
  948. buf[len] = '\0';
  949. if (kstrtoint(buf, 10, &val) != 0)
  950. goto end;
  951. mutex_lock(&debug->lock);
  952. debug->skip_config = !!val;
  953. mutex_unlock(&debug->lock);
  954. end:
  955. return len;
  956. }
  957. static ssize_t dp_sim_debug_write_mst_hpd(struct file *file,
  958. const char __user *user_buff, size_t count, loff_t *ppos)
  959. {
  960. struct dp_sim_debug_edid_entry *entry = file->private_data;
  961. struct dp_sim_device *debug;
  962. char buf[SZ_8];
  963. size_t len = 0;
  964. int hpd = 0;
  965. if (!entry)
  966. return -ENODEV;
  967. debug = entry->sim_dev;
  968. if (!debug || entry->index >= debug->port_num)
  969. return -EINVAL;
  970. if (*ppos)
  971. return 0;
  972. len = min_t(size_t, count, SZ_8 - 1);
  973. if (copy_from_user(buf, user_buff, len))
  974. goto end;
  975. buf[len] = '\0';
  976. if (kstrtoint(buf, 10, &hpd) != 0)
  977. goto end;
  978. mutex_lock(&debug->lock);
  979. dp_sim_update_port_status(&debug->bridge,
  980. entry->index, hpd ?
  981. connector_status_connected :
  982. connector_status_disconnected);
  983. mutex_unlock(&debug->lock);
  984. end:
  985. return len;
  986. }
  987. static const struct file_operations sim_edid_fops = {
  988. .open = simple_open,
  989. .write = dp_sim_debug_write_edid,
  990. };
  991. static const struct file_operations sim_mst_hpd_fops = {
  992. .open = simple_open,
  993. .write = dp_sim_debug_write_mst_hpd,
  994. };
  995. static ssize_t dp_sim_debug_write_mst_mode(struct file *file,
  996. const char __user *user_buff, size_t count, loff_t *ppos)
  997. {
  998. struct dp_sim_device *debug = file->private_data;
  999. char buf[SZ_16];
  1000. size_t len = 0;
  1001. int mst_sideband_mode = 0;
  1002. u32 mst_port_cnt = 0;
  1003. u32 mst_old_port_cnt;
  1004. struct dp_sim_debug_edid_entry *edid_entry;
  1005. u8 *edid;
  1006. u32 i, rc;
  1007. if (!debug)
  1008. return -ENODEV;
  1009. /* Leave room for termination char */
  1010. len = min_t(size_t, count, SZ_8 - 1);
  1011. if (copy_from_user(buf, user_buff, len))
  1012. return -EFAULT;
  1013. buf[len] = '\0';
  1014. if (sscanf(buf, "%d %u", &mst_sideband_mode, &mst_port_cnt) != 2) {
  1015. DP_ERR("invalid input\n");
  1016. return -EINVAL;
  1017. }
  1018. if (mst_port_cnt >= MAX_MST_PORT) {
  1019. DP_ERR("port cnt:%d exceeding max:%d\n", mst_port_cnt,
  1020. MAX_MST_PORT);
  1021. return -EINVAL;
  1022. }
  1023. mutex_lock(&debug->lock);
  1024. if (!mst_port_cnt)
  1025. mst_port_cnt = 1;
  1026. debug->skip_mst = !mst_sideband_mode;
  1027. DP_DEBUG("mst_sideband_mode: %d port_cnt:%d\n",
  1028. mst_sideband_mode, mst_port_cnt);
  1029. mst_old_port_cnt = debug->port_num;
  1030. rc = dp_sim_update_port_num(&debug->bridge, mst_port_cnt);
  1031. if (rc)
  1032. goto bail;
  1033. /* write mst */
  1034. dp_sim_write_dpcd(debug, DP_MSTM_CAP, debug->skip_mst);
  1035. /* create default edid nodes */
  1036. for (i = mst_old_port_cnt; i < mst_port_cnt; i++) {
  1037. edid_entry = devm_kzalloc(debug->dev,
  1038. sizeof(*edid_entry), GFP_KERNEL);
  1039. if (!edid_entry)
  1040. continue;
  1041. edid_entry->index = i;
  1042. edid_entry->sim_dev = debug;
  1043. scnprintf(buf, sizeof(buf), "edid-%d", i);
  1044. debugfs_create_file(buf,
  1045. 0444,
  1046. debug->debugfs_edid_dir,
  1047. edid_entry,
  1048. &sim_edid_fops);
  1049. scnprintf(buf, sizeof(buf), "hpd-%d", i);
  1050. debugfs_create_file(buf,
  1051. 0444,
  1052. debug->debugfs_edid_dir,
  1053. edid_entry,
  1054. &sim_mst_hpd_fops);
  1055. if (!debug->ports[0].edid_size)
  1056. continue;
  1057. edid = devm_kzalloc(debug->dev,
  1058. debug->ports[0].edid_size, GFP_KERNEL);
  1059. if (!edid) {
  1060. rc = -ENOMEM;
  1061. goto bail;
  1062. }
  1063. memcpy(edid, debug->ports[0].edid, debug->ports[0].edid_size);
  1064. debug->ports[i].edid = edid;
  1065. debug->ports[i].edid_size = debug->ports[0].edid_size;
  1066. }
  1067. rc = count;
  1068. bail:
  1069. mutex_unlock(&debug->lock);
  1070. return rc;
  1071. }
  1072. static const struct file_operations sim_dpcd_fops = {
  1073. .open = simple_open,
  1074. .write = dp_sim_debug_write_dpcd,
  1075. .read = dp_sim_debug_read_dpcd,
  1076. };
  1077. static const struct file_operations sim_hpd_fops = {
  1078. .open = simple_open,
  1079. .write = dp_sim_debug_write_hpd,
  1080. };
  1081. static const struct file_operations sim_skip_link_training_fops = {
  1082. .open = simple_open,
  1083. .write = dp_sim_debug_write_skip_link_training,
  1084. };
  1085. static const struct file_operations sim_skip_edid_fops = {
  1086. .open = simple_open,
  1087. .write = dp_sim_debug_write_skip_edid,
  1088. };
  1089. static const struct file_operations sim_skip_dpcd_fops = {
  1090. .open = simple_open,
  1091. .write = dp_sim_debug_write_skip_dpcd,
  1092. };
  1093. static const struct file_operations sim_skip_config_fops = {
  1094. .open = simple_open,
  1095. .write = dp_sim_debug_write_skip_config,
  1096. };
  1097. static const struct file_operations sim_mst_mode_fops = {
  1098. .open = simple_open,
  1099. .write = dp_sim_debug_write_mst_mode,
  1100. };
  1101. static int dp_sim_debug_init(struct dp_sim_device *sim_dev)
  1102. {
  1103. struct dp_sim_debug_edid_entry *edid_entry;
  1104. struct dentry *dir, *file, *edid_dir;
  1105. char name[SZ_16];
  1106. int rc = 0, i;
  1107. if (!sim_dev->label)
  1108. return 0;
  1109. dir = debugfs_create_dir(sim_dev->label, NULL);
  1110. if (IS_ERR_OR_NULL(dir)) {
  1111. rc = PTR_ERR(dir);
  1112. DP_ERR("[%s] debugfs create dir failed, rc = %d\n",
  1113. sim_dev->label, rc);
  1114. goto error;
  1115. }
  1116. edid_dir = debugfs_create_dir("mst_edid", dir);
  1117. if (IS_ERR_OR_NULL(edid_dir)) {
  1118. rc = PTR_ERR(edid_dir);
  1119. DP_ERR("[%s] debugfs create dir failed, rc = %d\n",
  1120. sim_dev->label, rc);
  1121. goto error_remove_dir;
  1122. }
  1123. for (i = 0; i < sim_dev->port_num; i++) {
  1124. edid_entry = devm_kzalloc(sim_dev->dev,
  1125. sizeof(*edid_entry), GFP_KERNEL);
  1126. edid_entry->index = i;
  1127. edid_entry->sim_dev = sim_dev;
  1128. scnprintf(name, sizeof(name), "edid-%d", i);
  1129. file = debugfs_create_file(name,
  1130. 0444,
  1131. edid_dir,
  1132. edid_entry,
  1133. &sim_edid_fops);
  1134. if (IS_ERR_OR_NULL(file)) {
  1135. rc = PTR_ERR(file);
  1136. DP_ERR("[%s] debugfs create edid failed, rc=%d\n",
  1137. sim_dev->label, rc);
  1138. goto error_remove_dir;
  1139. }
  1140. scnprintf(name, sizeof(name), "hpd-%d", i);
  1141. file = debugfs_create_file(name,
  1142. 0444,
  1143. edid_dir,
  1144. edid_entry,
  1145. &sim_mst_hpd_fops);
  1146. if (IS_ERR_OR_NULL(file)) {
  1147. rc = PTR_ERR(file);
  1148. DP_ERR("[%s] debugfs create hpd failed, rc=%d\n",
  1149. sim_dev->label, rc);
  1150. goto error_remove_dir;
  1151. }
  1152. }
  1153. file = debugfs_create_symlink("edid", dir, "./mst_edid/edid-0");
  1154. if (IS_ERR_OR_NULL(file)) {
  1155. rc = PTR_ERR(file);
  1156. DP_ERR("[%s] debugfs create edid link failed, rc=%d\n",
  1157. sim_dev->label, rc);
  1158. goto error_remove_dir;
  1159. }
  1160. file = debugfs_create_file("dpcd",
  1161. 0444,
  1162. dir,
  1163. sim_dev,
  1164. &sim_dpcd_fops);
  1165. if (IS_ERR_OR_NULL(file)) {
  1166. rc = PTR_ERR(file);
  1167. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1168. sim_dev->label, rc);
  1169. goto error_remove_dir;
  1170. }
  1171. file = debugfs_create_file("hpd",
  1172. 0444,
  1173. dir,
  1174. sim_dev,
  1175. &sim_hpd_fops);
  1176. if (IS_ERR_OR_NULL(file)) {
  1177. rc = PTR_ERR(file);
  1178. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1179. sim_dev->label, rc);
  1180. goto error_remove_dir;
  1181. }
  1182. file = debugfs_create_file("skip_link_training",
  1183. 0444,
  1184. dir,
  1185. sim_dev,
  1186. &sim_skip_link_training_fops);
  1187. if (IS_ERR_OR_NULL(file)) {
  1188. rc = PTR_ERR(file);
  1189. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1190. sim_dev->label, rc);
  1191. goto error_remove_dir;
  1192. }
  1193. file = debugfs_create_file("skip_edid",
  1194. 0444,
  1195. dir,
  1196. sim_dev,
  1197. &sim_skip_edid_fops);
  1198. if (IS_ERR_OR_NULL(file)) {
  1199. rc = PTR_ERR(file);
  1200. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1201. sim_dev->label, rc);
  1202. goto error_remove_dir;
  1203. }
  1204. file = debugfs_create_file("skip_dpcd_read",
  1205. 0444,
  1206. dir,
  1207. sim_dev,
  1208. &sim_skip_dpcd_fops);
  1209. if (IS_ERR_OR_NULL(file)) {
  1210. rc = PTR_ERR(file);
  1211. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1212. sim_dev->label, rc);
  1213. goto error_remove_dir;
  1214. }
  1215. file = debugfs_create_file("skip_dpcd_write",
  1216. 0444,
  1217. dir,
  1218. sim_dev,
  1219. &sim_skip_config_fops);
  1220. if (IS_ERR_OR_NULL(file)) {
  1221. rc = PTR_ERR(file);
  1222. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1223. sim_dev->label, rc);
  1224. goto error_remove_dir;
  1225. }
  1226. file = debugfs_create_file("mst_sideband_mode",
  1227. 0444,
  1228. dir,
  1229. sim_dev,
  1230. &sim_mst_mode_fops);
  1231. if (IS_ERR_OR_NULL(file)) {
  1232. rc = PTR_ERR(file);
  1233. DP_ERR("[%s] debugfs create failed, rc=%d\n",
  1234. sim_dev->label, rc);
  1235. goto error_remove_dir;
  1236. }
  1237. sim_dev->debugfs_dir = dir;
  1238. sim_dev->debugfs_edid_dir = edid_dir;
  1239. return 0;
  1240. error_remove_dir:
  1241. debugfs_remove_recursive(dir);
  1242. error:
  1243. return rc;
  1244. }
  1245. static int dp_sim_parse(struct dp_sim_device *sim_dev)
  1246. {
  1247. int rc;
  1248. sim_dev->label = of_get_property(sim_dev->bridge.of_node,
  1249. "label", NULL);
  1250. rc = dp_sim_parse_dpcd(sim_dev);
  1251. if (rc) {
  1252. DP_ERR("failed to parse DPCD nodes\n");
  1253. return rc;
  1254. }
  1255. rc = dp_sim_parse_edid(sim_dev);
  1256. if (rc) {
  1257. DP_ERR("failed to parse EDID nodes\n");
  1258. return rc;
  1259. }
  1260. rc = dp_sim_parse_misc(sim_dev);
  1261. if (rc) {
  1262. DP_ERR("failed to parse misc nodes\n");
  1263. return rc;
  1264. }
  1265. return 0;
  1266. }
  1267. int dp_sim_create_bridge(struct device *dev, struct dp_aux_bridge **bridge)
  1268. {
  1269. struct dp_sim_device *dp_sim_dev;
  1270. struct dp_mst_sim_cfg cfg;
  1271. int ret;
  1272. dp_sim_dev = devm_kzalloc(dev, sizeof(*dp_sim_dev), GFP_KERNEL);
  1273. if (!dp_sim_dev)
  1274. return -ENOMEM;
  1275. dp_sim_dev->dev = dev;
  1276. dp_sim_dev->bridge.of_node = dev->of_node;
  1277. dp_sim_dev->bridge.register_hpd = dp_sim_register_hpd;
  1278. dp_sim_dev->bridge.transfer = dp_sim_transfer;
  1279. dp_sim_dev->bridge.dev_priv = dp_sim_dev;
  1280. dp_sim_dev->bridge.flag = DP_AUX_BRIDGE_MST | DP_SIM_BRIDGE_PRIV_FLAG;
  1281. INIT_LIST_HEAD(&dp_sim_dev->dpcd_reg_list);
  1282. mutex_init(&dp_sim_dev->lock);
  1283. memset(&cfg, 0, sizeof(cfg));
  1284. cfg.host_dev = dp_sim_dev;
  1285. cfg.host_hpd_irq = dp_sim_host_hpd_irq;
  1286. ret = dp_mst_sim_create(&cfg, &dp_sim_dev->bridge.mst_ctx);
  1287. if (ret) {
  1288. devm_kfree(dev, dp_sim_dev);
  1289. return ret;
  1290. }
  1291. /* default dpcd reg value */
  1292. dp_sim_dev->dpcd_reg[DP_DPCD_REV] = 0x12;
  1293. dp_sim_dev->dpcd_reg[DP_MAX_LINK_RATE] = 0x14;
  1294. dp_sim_dev->dpcd_reg[DP_MAX_LANE_COUNT] = 0xc4;
  1295. dp_sim_dev->dpcd_reg[DP_SINK_COUNT] = 0x1;
  1296. dp_sim_dev->dpcd_reg[DP_LANE0_1_STATUS] = 0x77;
  1297. dp_sim_dev->dpcd_reg[DP_LANE2_3_STATUS] = 0x77;
  1298. dp_sim_dev->dpcd_reg[DP_LANE_ALIGN_STATUS_UPDATED] = 0x1;
  1299. dp_sim_dev->dpcd_reg[DP_SINK_STATUS] = 0x3;
  1300. dp_sim_dev->dpcd_reg[DP_PAYLOAD_TABLE_UPDATE_STATUS] = 0x3;
  1301. /* enable link training by default */
  1302. dp_sim_dev->skip_link_training = true;
  1303. dp_sim_dev->link_training_cnt = (u32)-1;
  1304. *bridge = &dp_sim_dev->bridge;
  1305. return 0;
  1306. }
  1307. int dp_sim_destroy_bridge(struct dp_aux_bridge *bridge)
  1308. {
  1309. struct dp_sim_device *dp_sim_dev;
  1310. struct dp_sim_dpcd_reg *reg, *p;
  1311. if (!bridge || !(bridge->flag & DP_SIM_BRIDGE_PRIV_FLAG))
  1312. return -EINVAL;
  1313. dp_sim_dev = to_dp_sim_dev(bridge);
  1314. dp_mst_sim_destroy(dp_sim_dev->bridge.mst_ctx);
  1315. list_for_each_entry_safe(reg, p, &dp_sim_dev->dpcd_reg_list, head) {
  1316. list_del(&reg->head);
  1317. devm_kfree(dp_sim_dev->dev, reg);
  1318. }
  1319. if (dp_sim_dev->ports)
  1320. devm_kfree(dp_sim_dev->dev, dp_sim_dev->ports);
  1321. devm_kfree(dp_sim_dev->dev, dp_sim_dev);
  1322. return 0;
  1323. }
  1324. int dp_sim_probe(struct platform_device *pdev)
  1325. {
  1326. struct dp_sim_device *dp_sim_dev;
  1327. struct dp_aux_bridge *bridge;
  1328. int ret;
  1329. ret = dp_sim_create_bridge(&pdev->dev, &bridge);
  1330. if (ret)
  1331. return ret;
  1332. dp_sim_dev = to_dp_sim_dev(bridge);
  1333. ret = dp_sim_parse(dp_sim_dev);
  1334. if (ret)
  1335. goto fail;
  1336. if (dp_sim_dev->skip_hpd)
  1337. dp_sim_dev->bridge.flag |= DP_AUX_BRIDGE_HPD;
  1338. ret = dp_mst_sim_update(dp_sim_dev->bridge.mst_ctx,
  1339. dp_sim_dev->port_num, dp_sim_dev->ports);
  1340. if (ret)
  1341. goto fail;
  1342. ret = dp_sim_debug_init(dp_sim_dev);
  1343. if (ret)
  1344. goto fail;
  1345. ret = dp_aux_add_bridge(&dp_sim_dev->bridge);
  1346. if (ret)
  1347. goto fail;
  1348. platform_set_drvdata(pdev, dp_sim_dev);
  1349. return 0;
  1350. fail:
  1351. dp_sim_destroy_bridge(bridge);
  1352. return ret;
  1353. }
  1354. int dp_sim_remove(struct platform_device *pdev)
  1355. {
  1356. struct dp_sim_device *dp_sim_dev;
  1357. dp_sim_dev = platform_get_drvdata(pdev);
  1358. if (!dp_sim_dev)
  1359. return 0;
  1360. debugfs_remove_recursive(dp_sim_dev->debugfs_dir);
  1361. dp_sim_destroy_bridge(&dp_sim_dev->bridge);
  1362. return 0;
  1363. }