dp_mst_sim.c 37 KB

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