dp_mst_sim.c 37 KB

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