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