dp_mst_sim.c 36 KB

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