dp_mst_sim.c 38 KB

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