drm_dp_helper.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * Copyright © 2009 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/errno.h>
  27. #include <linux/sched.h>
  28. #include <linux/i2c.h>
  29. #include <linux/seq_file.h>
  30. #include <drm/drm_dp_helper.h>
  31. #include <drm/drmP.h>
  32. #include "drm_crtc_helper_internal.h"
  33. /**
  34. * DOC: dp helpers
  35. *
  36. * These functions contain some common logic and helpers at various abstraction
  37. * levels to deal with Display Port sink devices and related things like DP aux
  38. * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
  39. * blocks, ...
  40. */
  41. /* Helpers for DP link training */
  42. static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
  43. {
  44. return link_status[r - DP_LANE0_1_STATUS];
  45. }
  46. static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
  47. int lane)
  48. {
  49. int i = DP_LANE0_1_STATUS + (lane >> 1);
  50. int s = (lane & 1) * 4;
  51. u8 l = dp_link_status(link_status, i);
  52. return (l >> s) & 0xf;
  53. }
  54. bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  55. int lane_count)
  56. {
  57. u8 lane_align;
  58. u8 lane_status;
  59. int lane;
  60. lane_align = dp_link_status(link_status,
  61. DP_LANE_ALIGN_STATUS_UPDATED);
  62. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  63. return false;
  64. for (lane = 0; lane < lane_count; lane++) {
  65. lane_status = dp_get_lane_status(link_status, lane);
  66. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  67. return false;
  68. }
  69. return true;
  70. }
  71. EXPORT_SYMBOL(drm_dp_channel_eq_ok);
  72. bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  73. int lane_count)
  74. {
  75. int lane;
  76. u8 lane_status;
  77. for (lane = 0; lane < lane_count; lane++) {
  78. lane_status = dp_get_lane_status(link_status, lane);
  79. if ((lane_status & DP_LANE_CR_DONE) == 0)
  80. return false;
  81. }
  82. return true;
  83. }
  84. EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
  85. u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
  86. int lane)
  87. {
  88. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  89. int s = ((lane & 1) ?
  90. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  91. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  92. u8 l = dp_link_status(link_status, i);
  93. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  94. }
  95. EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
  96. u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
  97. int lane)
  98. {
  99. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  100. int s = ((lane & 1) ?
  101. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  102. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  103. u8 l = dp_link_status(link_status, i);
  104. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  105. }
  106. EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
  107. void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  108. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  109. udelay(100);
  110. else
  111. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  112. }
  113. EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
  114. void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  115. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  116. udelay(400);
  117. else
  118. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  119. }
  120. EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
  121. u8 drm_dp_link_rate_to_bw_code(int link_rate)
  122. {
  123. switch (link_rate) {
  124. case 162000:
  125. default:
  126. return DP_LINK_BW_1_62;
  127. case 270000:
  128. return DP_LINK_BW_2_7;
  129. case 540000:
  130. return DP_LINK_BW_5_4;
  131. }
  132. }
  133. EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
  134. int drm_dp_bw_code_to_link_rate(u8 link_bw)
  135. {
  136. switch (link_bw) {
  137. case DP_LINK_BW_1_62:
  138. default:
  139. return 162000;
  140. case DP_LINK_BW_2_7:
  141. return 270000;
  142. case DP_LINK_BW_5_4:
  143. return 540000;
  144. }
  145. }
  146. EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
  147. #define AUX_RETRY_INTERVAL 500 /* us */
  148. /**
  149. * DOC: dp helpers
  150. *
  151. * The DisplayPort AUX channel is an abstraction to allow generic, driver-
  152. * independent access to AUX functionality. Drivers can take advantage of
  153. * this by filling in the fields of the drm_dp_aux structure.
  154. *
  155. * Transactions are described using a hardware-independent drm_dp_aux_msg
  156. * structure, which is passed into a driver's .transfer() implementation.
  157. * Both native and I2C-over-AUX transactions are supported.
  158. */
  159. static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
  160. unsigned int offset, void *buffer, size_t size)
  161. {
  162. struct drm_dp_aux_msg msg;
  163. unsigned int retry, native_reply;
  164. int err = 0, ret = 0;
  165. memset(&msg, 0, sizeof(msg));
  166. msg.address = offset;
  167. msg.request = request;
  168. msg.buffer = buffer;
  169. msg.size = size;
  170. mutex_lock(&aux->hw_mutex);
  171. /*
  172. * The specification doesn't give any recommendation on how often to
  173. * retry native transactions. We used to retry 7 times like for
  174. * aux i2c transactions but real world devices this wasn't
  175. * sufficient, bump to 32 which makes Dell 4k monitors happier.
  176. */
  177. for (retry = 0; retry < 32; retry++) {
  178. if (ret != 0 && ret != -ETIMEDOUT) {
  179. usleep_range(AUX_RETRY_INTERVAL,
  180. AUX_RETRY_INTERVAL + 100);
  181. }
  182. ret = aux->transfer(aux, &msg);
  183. if (ret >= 0) {
  184. native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
  185. if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
  186. if (ret == size)
  187. goto unlock;
  188. ret = -EPROTO;
  189. } else
  190. ret = -EIO;
  191. }
  192. /*
  193. * We want the error we return to be the error we received on
  194. * the first transaction, since we may get a different error the
  195. * next time we retry
  196. */
  197. if (!err)
  198. err = ret;
  199. }
  200. DRM_DEBUG_KMS("Too many retries, giving up. First error: %d\n", err);
  201. ret = err;
  202. unlock:
  203. mutex_unlock(&aux->hw_mutex);
  204. return ret;
  205. }
  206. /**
  207. * drm_dp_dpcd_read() - read a series of bytes from the DPCD
  208. * @aux: DisplayPort AUX channel
  209. * @offset: address of the (first) register to read
  210. * @buffer: buffer to store the register values
  211. * @size: number of bytes in @buffer
  212. *
  213. * Returns the number of bytes transferred on success, or a negative error
  214. * code on failure. -EIO is returned if the request was NAKed by the sink or
  215. * if the retry count was exceeded. If not all bytes were transferred, this
  216. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  217. * function, with the exception of -EBUSY (which causes the transaction to
  218. * be retried), are propagated to the caller.
  219. */
  220. ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
  221. void *buffer, size_t size)
  222. {
  223. int ret;
  224. /*
  225. * HP ZR24w corrupts the first DPCD access after entering power save
  226. * mode. Eg. on a read, the entire buffer will be filled with the same
  227. * byte. Do a throw away read to avoid corrupting anything we care
  228. * about. Afterwards things will work correctly until the monitor
  229. * gets woken up and subsequently re-enters power save mode.
  230. *
  231. * The user pressing any button on the monitor is enough to wake it
  232. * up, so there is no particularly good place to do the workaround.
  233. * We just have to do it before any DPCD access and hope that the
  234. * monitor doesn't power down exactly after the throw away read.
  235. */
  236. ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV, buffer,
  237. 1);
  238. if (ret != 1)
  239. return ret;
  240. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, buffer,
  241. size);
  242. }
  243. EXPORT_SYMBOL(drm_dp_dpcd_read);
  244. /**
  245. * drm_dp_dpcd_write() - write a series of bytes to the DPCD
  246. * @aux: DisplayPort AUX channel
  247. * @offset: address of the (first) register to write
  248. * @buffer: buffer containing the values to write
  249. * @size: number of bytes in @buffer
  250. *
  251. * Returns the number of bytes transferred on success, or a negative error
  252. * code on failure. -EIO is returned if the request was NAKed by the sink or
  253. * if the retry count was exceeded. If not all bytes were transferred, this
  254. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  255. * function, with the exception of -EBUSY (which causes the transaction to
  256. * be retried), are propagated to the caller.
  257. */
  258. ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
  259. void *buffer, size_t size)
  260. {
  261. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer,
  262. size);
  263. }
  264. EXPORT_SYMBOL(drm_dp_dpcd_write);
  265. /**
  266. * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
  267. * @aux: DisplayPort AUX channel
  268. * @status: buffer to store the link status in (must be at least 6 bytes)
  269. *
  270. * Returns the number of bytes transferred on success or a negative error
  271. * code on failure.
  272. */
  273. int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
  274. u8 status[DP_LINK_STATUS_SIZE])
  275. {
  276. return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
  277. DP_LINK_STATUS_SIZE);
  278. }
  279. EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
  280. /**
  281. * drm_dp_link_probe() - probe a DisplayPort link for capabilities
  282. * @aux: DisplayPort AUX channel
  283. * @link: pointer to structure in which to return link capabilities
  284. *
  285. * The structure filled in by this function can usually be passed directly
  286. * into drm_dp_link_power_up() and drm_dp_link_configure() to power up and
  287. * configure the link based on the link's capabilities.
  288. *
  289. * Returns 0 on success or a negative error code on failure.
  290. */
  291. int drm_dp_link_probe(struct drm_dp_aux *aux, struct drm_dp_link *link)
  292. {
  293. u8 values[3];
  294. int err;
  295. memset(link, 0, sizeof(*link));
  296. err = drm_dp_dpcd_read(aux, DP_DPCD_REV, values, sizeof(values));
  297. if (err < 0)
  298. return err;
  299. link->revision = values[0];
  300. link->rate = drm_dp_bw_code_to_link_rate(values[1]);
  301. link->num_lanes = values[2] & DP_MAX_LANE_COUNT_MASK;
  302. if (values[2] & DP_ENHANCED_FRAME_CAP)
  303. link->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
  304. return 0;
  305. }
  306. EXPORT_SYMBOL(drm_dp_link_probe);
  307. /**
  308. * drm_dp_link_power_up() - power up a DisplayPort link
  309. * @aux: DisplayPort AUX channel
  310. * @link: pointer to a structure containing the link configuration
  311. *
  312. * Returns 0 on success or a negative error code on failure.
  313. */
  314. int drm_dp_link_power_up(struct drm_dp_aux *aux, struct drm_dp_link *link)
  315. {
  316. u8 value;
  317. int err;
  318. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  319. if (link->revision < 0x11)
  320. return 0;
  321. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  322. if (err < 0)
  323. return err;
  324. value &= ~DP_SET_POWER_MASK;
  325. value |= DP_SET_POWER_D0;
  326. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  327. if (err < 0)
  328. return err;
  329. /*
  330. * According to the DP 1.1 specification, a "Sink Device must exit the
  331. * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
  332. * Control Field" (register 0x600).
  333. */
  334. usleep_range(1000, 2000);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(drm_dp_link_power_up);
  338. /**
  339. * drm_dp_link_power_down() - power down a DisplayPort link
  340. * @aux: DisplayPort AUX channel
  341. * @link: pointer to a structure containing the link configuration
  342. *
  343. * Returns 0 on success or a negative error code on failure.
  344. */
  345. int drm_dp_link_power_down(struct drm_dp_aux *aux, struct drm_dp_link *link)
  346. {
  347. u8 value;
  348. int err;
  349. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  350. if (link->revision < 0x11)
  351. return 0;
  352. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  353. if (err < 0)
  354. return err;
  355. value &= ~DP_SET_POWER_MASK;
  356. value |= DP_SET_POWER_D3;
  357. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  358. if (err < 0)
  359. return err;
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(drm_dp_link_power_down);
  363. /**
  364. * drm_dp_link_configure() - configure a DisplayPort link
  365. * @aux: DisplayPort AUX channel
  366. * @link: pointer to a structure containing the link configuration
  367. *
  368. * Returns 0 on success or a negative error code on failure.
  369. */
  370. int drm_dp_link_configure(struct drm_dp_aux *aux, struct drm_dp_link *link)
  371. {
  372. u8 values[2];
  373. int err;
  374. values[0] = drm_dp_link_rate_to_bw_code(link->rate);
  375. values[1] = link->num_lanes;
  376. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  377. values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  378. err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
  379. if (err < 0)
  380. return err;
  381. return 0;
  382. }
  383. EXPORT_SYMBOL(drm_dp_link_configure);
  384. /**
  385. * drm_dp_downstream_max_clock() - extract branch device max
  386. * pixel rate for legacy VGA
  387. * converter or max TMDS clock
  388. * rate for others
  389. * @dpcd: DisplayPort configuration data
  390. * @port_cap: port capabilities
  391. *
  392. * Returns max clock in kHz on success or 0 if max clock not defined
  393. */
  394. int drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  395. const u8 port_cap[4])
  396. {
  397. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  398. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  399. DP_DETAILED_CAP_INFO_AVAILABLE;
  400. if (!detailed_cap_info)
  401. return 0;
  402. switch (type) {
  403. case DP_DS_PORT_TYPE_VGA:
  404. return port_cap[1] * 8 * 1000;
  405. case DP_DS_PORT_TYPE_DVI:
  406. case DP_DS_PORT_TYPE_HDMI:
  407. case DP_DS_PORT_TYPE_DP_DUALMODE:
  408. return port_cap[1] * 2500;
  409. default:
  410. return 0;
  411. }
  412. }
  413. EXPORT_SYMBOL(drm_dp_downstream_max_clock);
  414. /**
  415. * drm_dp_downstream_max_bpc() - extract branch device max
  416. * bits per component
  417. * @dpcd: DisplayPort configuration data
  418. * @port_cap: port capabilities
  419. *
  420. * Returns max bpc on success or 0 if max bpc not defined
  421. */
  422. int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  423. const u8 port_cap[4])
  424. {
  425. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  426. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  427. DP_DETAILED_CAP_INFO_AVAILABLE;
  428. int bpc;
  429. if (!detailed_cap_info)
  430. return 0;
  431. switch (type) {
  432. case DP_DS_PORT_TYPE_VGA:
  433. case DP_DS_PORT_TYPE_DVI:
  434. case DP_DS_PORT_TYPE_HDMI:
  435. case DP_DS_PORT_TYPE_DP_DUALMODE:
  436. bpc = port_cap[2] & DP_DS_MAX_BPC_MASK;
  437. switch (bpc) {
  438. case DP_DS_8BPC:
  439. return 8;
  440. case DP_DS_10BPC:
  441. return 10;
  442. case DP_DS_12BPC:
  443. return 12;
  444. case DP_DS_16BPC:
  445. return 16;
  446. }
  447. default:
  448. return 0;
  449. }
  450. }
  451. EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
  452. /**
  453. * drm_dp_downstream_id() - identify branch device
  454. * @aux: DisplayPort AUX channel
  455. * @id: DisplayPort branch device id
  456. *
  457. * Returns branch device id on success or NULL on failure
  458. */
  459. int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
  460. {
  461. return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
  462. }
  463. EXPORT_SYMBOL(drm_dp_downstream_id);
  464. /**
  465. * drm_dp_downstream_debug() - debug DP branch devices
  466. * @m: pointer for debugfs file
  467. * @dpcd: DisplayPort configuration data
  468. * @port_cap: port capabilities
  469. * @aux: DisplayPort AUX channel
  470. *
  471. */
  472. void drm_dp_downstream_debug(struct seq_file *m,
  473. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  474. const u8 port_cap[4], struct drm_dp_aux *aux)
  475. {
  476. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  477. DP_DETAILED_CAP_INFO_AVAILABLE;
  478. int clk;
  479. int bpc;
  480. char id[7];
  481. int len;
  482. uint8_t rev[2];
  483. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  484. bool branch_device = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  485. DP_DWN_STRM_PORT_PRESENT;
  486. seq_printf(m, "\tDP branch device present: %s\n",
  487. branch_device ? "yes" : "no");
  488. if (!branch_device)
  489. return;
  490. switch (type) {
  491. case DP_DS_PORT_TYPE_DP:
  492. seq_puts(m, "\t\tType: DisplayPort\n");
  493. break;
  494. case DP_DS_PORT_TYPE_VGA:
  495. seq_puts(m, "\t\tType: VGA\n");
  496. break;
  497. case DP_DS_PORT_TYPE_DVI:
  498. seq_puts(m, "\t\tType: DVI\n");
  499. break;
  500. case DP_DS_PORT_TYPE_HDMI:
  501. seq_puts(m, "\t\tType: HDMI\n");
  502. break;
  503. case DP_DS_PORT_TYPE_NON_EDID:
  504. seq_puts(m, "\t\tType: others without EDID support\n");
  505. break;
  506. case DP_DS_PORT_TYPE_DP_DUALMODE:
  507. seq_puts(m, "\t\tType: DP++\n");
  508. break;
  509. case DP_DS_PORT_TYPE_WIRELESS:
  510. seq_puts(m, "\t\tType: Wireless\n");
  511. break;
  512. default:
  513. seq_puts(m, "\t\tType: N/A\n");
  514. }
  515. memset(id, 0, sizeof(id));
  516. drm_dp_downstream_id(aux, id);
  517. seq_printf(m, "\t\tID: %s\n", id);
  518. len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
  519. if (len > 0)
  520. seq_printf(m, "\t\tHW: %d.%d\n",
  521. (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
  522. len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
  523. if (len > 0)
  524. seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
  525. if (detailed_cap_info) {
  526. clk = drm_dp_downstream_max_clock(dpcd, port_cap);
  527. if (clk > 0) {
  528. if (type == DP_DS_PORT_TYPE_VGA)
  529. seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
  530. else
  531. seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
  532. }
  533. bpc = drm_dp_downstream_max_bpc(dpcd, port_cap);
  534. if (bpc > 0)
  535. seq_printf(m, "\t\tMax bpc: %d\n", bpc);
  536. }
  537. }
  538. EXPORT_SYMBOL(drm_dp_downstream_debug);
  539. /*
  540. * I2C-over-AUX implementation
  541. */
  542. static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
  543. {
  544. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  545. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  546. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  547. I2C_FUNC_10BIT_ADDR;
  548. }
  549. static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
  550. {
  551. /*
  552. * In case of i2c defer or short i2c ack reply to a write,
  553. * we need to switch to WRITE_STATUS_UPDATE to drain the
  554. * rest of the message
  555. */
  556. if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
  557. msg->request &= DP_AUX_I2C_MOT;
  558. msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
  559. }
  560. }
  561. #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
  562. #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
  563. #define AUX_STOP_LEN 4
  564. #define AUX_CMD_LEN 4
  565. #define AUX_ADDRESS_LEN 20
  566. #define AUX_REPLY_PAD_LEN 4
  567. #define AUX_LENGTH_LEN 8
  568. /*
  569. * Calculate the duration of the AUX request/reply in usec. Gives the
  570. * "best" case estimate, ie. successful while as short as possible.
  571. */
  572. static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
  573. {
  574. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  575. AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
  576. if ((msg->request & DP_AUX_I2C_READ) == 0)
  577. len += msg->size * 8;
  578. return len;
  579. }
  580. static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
  581. {
  582. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  583. AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
  584. /*
  585. * For read we expect what was asked. For writes there will
  586. * be 0 or 1 data bytes. Assume 0 for the "best" case.
  587. */
  588. if (msg->request & DP_AUX_I2C_READ)
  589. len += msg->size * 8;
  590. return len;
  591. }
  592. #define I2C_START_LEN 1
  593. #define I2C_STOP_LEN 1
  594. #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
  595. #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
  596. /*
  597. * Calculate the length of the i2c transfer in usec, assuming
  598. * the i2c bus speed is as specified. Gives the the "worst"
  599. * case estimate, ie. successful while as long as possible.
  600. * Doesn't account the the "MOT" bit, and instead assumes each
  601. * message includes a START, ADDRESS and STOP. Neither does it
  602. * account for additional random variables such as clock stretching.
  603. */
  604. static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
  605. int i2c_speed_khz)
  606. {
  607. /* AUX bitrate is 1MHz, i2c bitrate as specified */
  608. return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
  609. msg->size * I2C_DATA_LEN +
  610. I2C_STOP_LEN) * 1000, i2c_speed_khz);
  611. }
  612. /*
  613. * Deterine how many retries should be attempted to successfully transfer
  614. * the specified message, based on the estimated durations of the
  615. * i2c and AUX transfers.
  616. */
  617. static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
  618. int i2c_speed_khz)
  619. {
  620. int aux_time_us = drm_dp_aux_req_duration(msg) +
  621. drm_dp_aux_reply_duration(msg);
  622. int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
  623. return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
  624. }
  625. /*
  626. * FIXME currently assumes 10 kHz as some real world devices seem
  627. * to require it. We should query/set the speed via DPCD if supported.
  628. */
  629. static int dp_aux_i2c_speed_khz __read_mostly = 10;
  630. module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
  631. MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
  632. "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
  633. /*
  634. * Transfer a single I2C-over-AUX message and handle various error conditions,
  635. * retrying the transaction as appropriate. It is assumed that the
  636. * &drm_dp_aux.transfer function does not modify anything in the msg other than the
  637. * reply field.
  638. *
  639. * Returns bytes transferred on success, or a negative error code on failure.
  640. */
  641. static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  642. {
  643. unsigned int retry, defer_i2c;
  644. int ret;
  645. /*
  646. * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
  647. * is required to retry at least seven times upon receiving AUX_DEFER
  648. * before giving up the AUX transaction.
  649. *
  650. * We also try to account for the i2c bus speed.
  651. */
  652. int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
  653. for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
  654. ret = aux->transfer(aux, msg);
  655. if (ret < 0) {
  656. if (ret == -EBUSY)
  657. continue;
  658. /*
  659. * While timeouts can be errors, they're usually normal
  660. * behavior (for instance, when a driver tries to
  661. * communicate with a non-existant DisplayPort device).
  662. * Avoid spamming the kernel log with timeout errors.
  663. */
  664. if (ret == -ETIMEDOUT)
  665. DRM_DEBUG_KMS_RATELIMITED("transaction timed out\n");
  666. else
  667. DRM_DEBUG_KMS("transaction failed: %d\n", ret);
  668. return ret;
  669. }
  670. switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
  671. case DP_AUX_NATIVE_REPLY_ACK:
  672. /*
  673. * For I2C-over-AUX transactions this isn't enough, we
  674. * need to check for the I2C ACK reply.
  675. */
  676. break;
  677. case DP_AUX_NATIVE_REPLY_NACK:
  678. DRM_DEBUG_KMS("native nack (result=%d, size=%zu)\n", ret, msg->size);
  679. return -EREMOTEIO;
  680. case DP_AUX_NATIVE_REPLY_DEFER:
  681. DRM_DEBUG_KMS("native defer\n");
  682. /*
  683. * We could check for I2C bit rate capabilities and if
  684. * available adjust this interval. We could also be
  685. * more careful with DP-to-legacy adapters where a
  686. * long legacy cable may force very low I2C bit rates.
  687. *
  688. * For now just defer for long enough to hopefully be
  689. * safe for all use-cases.
  690. */
  691. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  692. continue;
  693. default:
  694. DRM_ERROR("invalid native reply %#04x\n", msg->reply);
  695. return -EREMOTEIO;
  696. }
  697. switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
  698. case DP_AUX_I2C_REPLY_ACK:
  699. /*
  700. * Both native ACK and I2C ACK replies received. We
  701. * can assume the transfer was successful.
  702. */
  703. if (ret != msg->size)
  704. drm_dp_i2c_msg_write_status_update(msg);
  705. return ret;
  706. case DP_AUX_I2C_REPLY_NACK:
  707. DRM_DEBUG_KMS("I2C nack (result=%d, size=%zu\n", ret, msg->size);
  708. aux->i2c_nack_count++;
  709. return -EREMOTEIO;
  710. case DP_AUX_I2C_REPLY_DEFER:
  711. DRM_DEBUG_KMS("I2C defer\n");
  712. /* DP Compliance Test 4.2.2.5 Requirement:
  713. * Must have at least 7 retries for I2C defers on the
  714. * transaction to pass this test
  715. */
  716. aux->i2c_defer_count++;
  717. if (defer_i2c < 7)
  718. defer_i2c++;
  719. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  720. drm_dp_i2c_msg_write_status_update(msg);
  721. continue;
  722. default:
  723. DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
  724. return -EREMOTEIO;
  725. }
  726. }
  727. DRM_DEBUG_KMS("too many retries, giving up\n");
  728. return -EREMOTEIO;
  729. }
  730. static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
  731. const struct i2c_msg *i2c_msg)
  732. {
  733. msg->request = (i2c_msg->flags & I2C_M_RD) ?
  734. DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
  735. msg->request |= DP_AUX_I2C_MOT;
  736. }
  737. /*
  738. * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
  739. *
  740. * Returns an error code on failure, or a recommended transfer size on success.
  741. */
  742. static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
  743. {
  744. int err, ret = orig_msg->size;
  745. struct drm_dp_aux_msg msg = *orig_msg;
  746. while (msg.size > 0) {
  747. err = drm_dp_i2c_do_msg(aux, &msg);
  748. if (err <= 0)
  749. return err == 0 ? -EPROTO : err;
  750. if (err < msg.size && err < ret) {
  751. DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
  752. msg.size, err);
  753. ret = err;
  754. }
  755. msg.size -= err;
  756. msg.buffer += err;
  757. }
  758. return ret;
  759. }
  760. /*
  761. * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
  762. * packets to be as large as possible. If not, the I2C transactions never
  763. * succeed. Hence the default is maximum.
  764. */
  765. static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
  766. module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
  767. MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
  768. "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
  769. static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  770. int num)
  771. {
  772. struct drm_dp_aux *aux = adapter->algo_data;
  773. unsigned int i, j;
  774. unsigned transfer_size;
  775. struct drm_dp_aux_msg msg;
  776. int err = 0;
  777. dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
  778. memset(&msg, 0, sizeof(msg));
  779. for (i = 0; i < num; i++) {
  780. msg.address = msgs[i].addr;
  781. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  782. /* Send a bare address packet to start the transaction.
  783. * Zero sized messages specify an address only (bare
  784. * address) transaction.
  785. */
  786. msg.buffer = NULL;
  787. msg.size = 0;
  788. err = drm_dp_i2c_do_msg(aux, &msg);
  789. /*
  790. * Reset msg.request in case in case it got
  791. * changed into a WRITE_STATUS_UPDATE.
  792. */
  793. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  794. if (err < 0)
  795. break;
  796. /* We want each transaction to be as large as possible, but
  797. * we'll go to smaller sizes if the hardware gives us a
  798. * short reply.
  799. */
  800. transfer_size = dp_aux_i2c_transfer_size;
  801. for (j = 0; j < msgs[i].len; j += msg.size) {
  802. msg.buffer = msgs[i].buf + j;
  803. msg.size = min(transfer_size, msgs[i].len - j);
  804. err = drm_dp_i2c_drain_msg(aux, &msg);
  805. /*
  806. * Reset msg.request in case in case it got
  807. * changed into a WRITE_STATUS_UPDATE.
  808. */
  809. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  810. if (err < 0)
  811. break;
  812. transfer_size = err;
  813. }
  814. if (err < 0)
  815. break;
  816. }
  817. if (err >= 0)
  818. err = num;
  819. /* Send a bare address packet to close out the transaction.
  820. * Zero sized messages specify an address only (bare
  821. * address) transaction.
  822. */
  823. msg.request &= ~DP_AUX_I2C_MOT;
  824. msg.buffer = NULL;
  825. msg.size = 0;
  826. (void)drm_dp_i2c_do_msg(aux, &msg);
  827. return err;
  828. }
  829. static const struct i2c_algorithm drm_dp_i2c_algo = {
  830. .functionality = drm_dp_i2c_functionality,
  831. .master_xfer = drm_dp_i2c_xfer,
  832. };
  833. static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
  834. {
  835. return container_of(i2c, struct drm_dp_aux, ddc);
  836. }
  837. static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
  838. {
  839. mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
  840. }
  841. static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
  842. {
  843. return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
  844. }
  845. static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
  846. {
  847. mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
  848. }
  849. static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
  850. .lock_bus = lock_bus,
  851. .trylock_bus = trylock_bus,
  852. .unlock_bus = unlock_bus,
  853. };
  854. static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
  855. {
  856. u8 buf, count;
  857. int ret;
  858. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  859. if (ret < 0)
  860. return ret;
  861. WARN_ON(!(buf & DP_TEST_SINK_START));
  862. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
  863. if (ret < 0)
  864. return ret;
  865. count = buf & DP_TEST_COUNT_MASK;
  866. if (count == aux->crc_count)
  867. return -EAGAIN; /* No CRC yet */
  868. aux->crc_count = count;
  869. /*
  870. * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
  871. * per component (RGB or CrYCb).
  872. */
  873. ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
  874. if (ret < 0)
  875. return ret;
  876. return 0;
  877. }
  878. static void drm_dp_aux_crc_work(struct work_struct *work)
  879. {
  880. struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
  881. crc_work);
  882. struct drm_crtc *crtc;
  883. u8 crc_bytes[6];
  884. uint32_t crcs[3];
  885. int ret;
  886. if (WARN_ON(!aux->crtc))
  887. return;
  888. crtc = aux->crtc;
  889. while (crtc->crc.opened) {
  890. drm_crtc_wait_one_vblank(crtc);
  891. if (!crtc->crc.opened)
  892. break;
  893. ret = drm_dp_aux_get_crc(aux, crc_bytes);
  894. if (ret == -EAGAIN) {
  895. usleep_range(1000, 2000);
  896. ret = drm_dp_aux_get_crc(aux, crc_bytes);
  897. }
  898. if (ret == -EAGAIN) {
  899. DRM_DEBUG_KMS("Get CRC failed after retrying: %d\n",
  900. ret);
  901. continue;
  902. } else if (ret) {
  903. DRM_DEBUG_KMS("Failed to get a CRC: %d\n", ret);
  904. continue;
  905. }
  906. crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
  907. crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
  908. crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
  909. drm_crtc_add_crc_entry(crtc, false, 0, crcs);
  910. }
  911. }
  912. /**
  913. * drm_dp_aux_init() - minimally initialise an aux channel
  914. * @aux: DisplayPort AUX channel
  915. *
  916. * If you need to use the drm_dp_aux's i2c adapter prior to registering it
  917. * with the outside world, call drm_dp_aux_init() first. You must still
  918. * call drm_dp_aux_register() once the connector has been registered to
  919. * allow userspace access to the auxiliary DP channel.
  920. */
  921. void drm_dp_aux_init(struct drm_dp_aux *aux)
  922. {
  923. mutex_init(&aux->hw_mutex);
  924. INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
  925. aux->ddc.algo = &drm_dp_i2c_algo;
  926. aux->ddc.algo_data = aux;
  927. aux->ddc.retries = 3;
  928. aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
  929. }
  930. EXPORT_SYMBOL(drm_dp_aux_init);
  931. /**
  932. * drm_dp_aux_register() - initialise and register aux channel
  933. * @aux: DisplayPort AUX channel
  934. *
  935. * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
  936. *
  937. * Returns 0 on success or a negative error code on failure.
  938. */
  939. int drm_dp_aux_register(struct drm_dp_aux *aux)
  940. {
  941. int ret;
  942. if (!aux->ddc.algo)
  943. drm_dp_aux_init(aux);
  944. aux->ddc.class = I2C_CLASS_DDC;
  945. aux->ddc.owner = THIS_MODULE;
  946. aux->ddc.dev.parent = aux->dev;
  947. aux->ddc.dev.of_node = aux->dev->of_node;
  948. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  949. sizeof(aux->ddc.name));
  950. ret = drm_dp_aux_register_devnode(aux);
  951. if (ret)
  952. return ret;
  953. ret = i2c_add_adapter(&aux->ddc);
  954. if (ret) {
  955. drm_dp_aux_unregister_devnode(aux);
  956. return ret;
  957. }
  958. return 0;
  959. }
  960. EXPORT_SYMBOL(drm_dp_aux_register);
  961. /**
  962. * drm_dp_aux_unregister() - unregister an AUX adapter
  963. * @aux: DisplayPort AUX channel
  964. */
  965. void drm_dp_aux_unregister(struct drm_dp_aux *aux)
  966. {
  967. drm_dp_aux_unregister_devnode(aux);
  968. i2c_del_adapter(&aux->ddc);
  969. }
  970. EXPORT_SYMBOL(drm_dp_aux_unregister);
  971. #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
  972. /**
  973. * drm_dp_psr_setup_time() - PSR setup in time usec
  974. * @psr_cap: PSR capabilities from DPCD
  975. *
  976. * Returns:
  977. * PSR setup time for the panel in microseconds, negative
  978. * error code on failure.
  979. */
  980. int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
  981. {
  982. static const u16 psr_setup_time_us[] = {
  983. PSR_SETUP_TIME(330),
  984. PSR_SETUP_TIME(275),
  985. PSR_SETUP_TIME(165),
  986. PSR_SETUP_TIME(110),
  987. PSR_SETUP_TIME(55),
  988. PSR_SETUP_TIME(0),
  989. };
  990. int i;
  991. i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
  992. if (i >= ARRAY_SIZE(psr_setup_time_us))
  993. return -EINVAL;
  994. return psr_setup_time_us[i];
  995. }
  996. EXPORT_SYMBOL(drm_dp_psr_setup_time);
  997. #undef PSR_SETUP_TIME
  998. /**
  999. * drm_dp_start_crc() - start capture of frame CRCs
  1000. * @aux: DisplayPort AUX channel
  1001. * @crtc: CRTC displaying the frames whose CRCs are to be captured
  1002. *
  1003. * Returns 0 on success or a negative error code on failure.
  1004. */
  1005. int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
  1006. {
  1007. u8 buf;
  1008. int ret;
  1009. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1010. if (ret < 0)
  1011. return ret;
  1012. ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
  1013. if (ret < 0)
  1014. return ret;
  1015. aux->crc_count = 0;
  1016. aux->crtc = crtc;
  1017. schedule_work(&aux->crc_work);
  1018. return 0;
  1019. }
  1020. EXPORT_SYMBOL(drm_dp_start_crc);
  1021. /**
  1022. * drm_dp_stop_crc() - stop capture of frame CRCs
  1023. * @aux: DisplayPort AUX channel
  1024. *
  1025. * Returns 0 on success or a negative error code on failure.
  1026. */
  1027. int drm_dp_stop_crc(struct drm_dp_aux *aux)
  1028. {
  1029. u8 buf;
  1030. int ret;
  1031. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1032. if (ret < 0)
  1033. return ret;
  1034. ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
  1035. if (ret < 0)
  1036. return ret;
  1037. flush_work(&aux->crc_work);
  1038. aux->crtc = NULL;
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(drm_dp_stop_crc);
  1042. struct dpcd_quirk {
  1043. u8 oui[3];
  1044. bool is_branch;
  1045. u32 quirks;
  1046. };
  1047. #define OUI(first, second, third) { (first), (second), (third) }
  1048. static const struct dpcd_quirk dpcd_quirk_list[] = {
  1049. /* Analogix 7737 needs reduced M and N at HBR2 link rates */
  1050. { OUI(0x00, 0x22, 0xb9), true, BIT(DP_DPCD_QUIRK_LIMITED_M_N) },
  1051. };
  1052. #undef OUI
  1053. /*
  1054. * Get a bit mask of DPCD quirks for the sink/branch device identified by
  1055. * ident. The quirk data is shared but it's up to the drivers to act on the
  1056. * data.
  1057. *
  1058. * For now, only the OUI (first three bytes) is used, but this may be extended
  1059. * to device identification string and hardware/firmware revisions later.
  1060. */
  1061. static u32
  1062. drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
  1063. {
  1064. const struct dpcd_quirk *quirk;
  1065. u32 quirks = 0;
  1066. int i;
  1067. for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
  1068. quirk = &dpcd_quirk_list[i];
  1069. if (quirk->is_branch != is_branch)
  1070. continue;
  1071. if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
  1072. continue;
  1073. quirks |= quirk->quirks;
  1074. }
  1075. return quirks;
  1076. }
  1077. /**
  1078. * drm_dp_read_desc - read sink/branch descriptor from DPCD
  1079. * @aux: DisplayPort AUX channel
  1080. * @desc: Device decriptor to fill from DPCD
  1081. * @is_branch: true for branch devices, false for sink devices
  1082. *
  1083. * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
  1084. * identification.
  1085. *
  1086. * Returns 0 on success or a negative error code on failure.
  1087. */
  1088. int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
  1089. bool is_branch)
  1090. {
  1091. struct drm_dp_dpcd_ident *ident = &desc->ident;
  1092. unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
  1093. int ret, dev_id_len;
  1094. ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
  1095. if (ret < 0)
  1096. return ret;
  1097. desc->quirks = drm_dp_get_quirks(ident, is_branch);
  1098. dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
  1099. DRM_DEBUG_KMS("DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
  1100. is_branch ? "branch" : "sink",
  1101. (int)sizeof(ident->oui), ident->oui,
  1102. dev_id_len, ident->device_id,
  1103. ident->hw_rev >> 4, ident->hw_rev & 0xf,
  1104. ident->sw_major_rev, ident->sw_minor_rev,
  1105. desc->quirks);
  1106. return 0;
  1107. }
  1108. EXPORT_SYMBOL(drm_dp_read_desc);