drm_dp_helper.c 35 KB

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