drm_dp_helper.c 35 KB

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