drm_dp_helper.c 35 KB

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