drm_dp_helper.c 36 KB

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