drm_dp_helper.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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", ret, msg->size);
  743. aux->i2c_nack_count++;
  744. return -EREMOTEIO;
  745. case DP_AUX_I2C_REPLY_DEFER:
  746. DRM_DEBUG_KMS("I2C defer\n");
  747. /* DP Compliance Test 4.2.2.5 Requirement:
  748. * Must have at least 7 retries for I2C defers on the
  749. * transaction to pass this test
  750. */
  751. aux->i2c_defer_count++;
  752. if (defer_i2c < 7)
  753. defer_i2c++;
  754. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  755. drm_dp_i2c_msg_write_status_update(msg);
  756. continue;
  757. default:
  758. DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
  759. return -EREMOTEIO;
  760. }
  761. }
  762. DRM_DEBUG_KMS("too many retries, giving up\n");
  763. return -EREMOTEIO;
  764. }
  765. static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
  766. const struct i2c_msg *i2c_msg)
  767. {
  768. msg->request = (i2c_msg->flags & I2C_M_RD) ?
  769. DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
  770. msg->request |= DP_AUX_I2C_MOT;
  771. }
  772. /*
  773. * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
  774. *
  775. * Returns an error code on failure, or a recommended transfer size on success.
  776. */
  777. static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
  778. {
  779. int err, ret = orig_msg->size;
  780. struct drm_dp_aux_msg msg = *orig_msg;
  781. while (msg.size > 0) {
  782. err = drm_dp_i2c_do_msg(aux, &msg);
  783. if (err <= 0)
  784. return err == 0 ? -EPROTO : err;
  785. if (err < msg.size && err < ret) {
  786. DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
  787. msg.size, err);
  788. ret = err;
  789. }
  790. msg.size -= err;
  791. msg.buffer += err;
  792. }
  793. return ret;
  794. }
  795. /*
  796. * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
  797. * packets to be as large as possible. If not, the I2C transactions never
  798. * succeed. Hence the default is maximum.
  799. */
  800. static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
  801. module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
  802. MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
  803. "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
  804. static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  805. int num)
  806. {
  807. struct drm_dp_aux *aux = adapter->algo_data;
  808. unsigned int i, j;
  809. unsigned transfer_size;
  810. struct drm_dp_aux_msg msg;
  811. int err = 0;
  812. dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
  813. memset(&msg, 0, sizeof(msg));
  814. for (i = 0; i < num; i++) {
  815. msg.address = msgs[i].addr;
  816. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  817. /* Send a bare address packet to start the transaction.
  818. * Zero sized messages specify an address only (bare
  819. * address) transaction.
  820. */
  821. msg.buffer = NULL;
  822. msg.size = 0;
  823. err = drm_dp_i2c_do_msg(aux, &msg);
  824. /*
  825. * Reset msg.request in case in case it got
  826. * changed into a WRITE_STATUS_UPDATE.
  827. */
  828. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  829. if (err < 0)
  830. break;
  831. /* We want each transaction to be as large as possible, but
  832. * we'll go to smaller sizes if the hardware gives us a
  833. * short reply.
  834. */
  835. transfer_size = dp_aux_i2c_transfer_size;
  836. for (j = 0; j < msgs[i].len; j += msg.size) {
  837. msg.buffer = msgs[i].buf + j;
  838. msg.size = min(transfer_size, msgs[i].len - j);
  839. err = drm_dp_i2c_drain_msg(aux, &msg);
  840. /*
  841. * Reset msg.request in case in case it got
  842. * changed into a WRITE_STATUS_UPDATE.
  843. */
  844. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  845. if (err < 0)
  846. break;
  847. transfer_size = err;
  848. }
  849. if (err < 0)
  850. break;
  851. }
  852. if (err >= 0)
  853. err = num;
  854. /* Send a bare address packet to close out the transaction.
  855. * Zero sized messages specify an address only (bare
  856. * address) transaction.
  857. */
  858. msg.request &= ~DP_AUX_I2C_MOT;
  859. msg.buffer = NULL;
  860. msg.size = 0;
  861. (void)drm_dp_i2c_do_msg(aux, &msg);
  862. return err;
  863. }
  864. static const struct i2c_algorithm drm_dp_i2c_algo = {
  865. .functionality = drm_dp_i2c_functionality,
  866. .master_xfer = drm_dp_i2c_xfer,
  867. };
  868. static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
  869. {
  870. return container_of(i2c, struct drm_dp_aux, ddc);
  871. }
  872. static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
  873. {
  874. mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
  875. }
  876. static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
  877. {
  878. return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
  879. }
  880. static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
  881. {
  882. mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
  883. }
  884. static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
  885. .lock_bus = lock_bus,
  886. .trylock_bus = trylock_bus,
  887. .unlock_bus = unlock_bus,
  888. };
  889. static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
  890. {
  891. u8 buf, count;
  892. int ret;
  893. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  894. if (ret < 0)
  895. return ret;
  896. WARN_ON(!(buf & DP_TEST_SINK_START));
  897. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
  898. if (ret < 0)
  899. return ret;
  900. count = buf & DP_TEST_COUNT_MASK;
  901. if (count == aux->crc_count)
  902. return -EAGAIN; /* No CRC yet */
  903. aux->crc_count = count;
  904. /*
  905. * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
  906. * per component (RGB or CrYCb).
  907. */
  908. ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
  909. if (ret < 0)
  910. return ret;
  911. return 0;
  912. }
  913. static void drm_dp_aux_crc_work(struct work_struct *work)
  914. {
  915. struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
  916. crc_work);
  917. struct drm_crtc *crtc;
  918. u8 crc_bytes[6];
  919. uint32_t crcs[3];
  920. int ret;
  921. if (WARN_ON(!aux->crtc))
  922. return;
  923. crtc = aux->crtc;
  924. while (crtc->crc.opened) {
  925. drm_crtc_wait_one_vblank(crtc);
  926. if (!crtc->crc.opened)
  927. break;
  928. ret = drm_dp_aux_get_crc(aux, crc_bytes);
  929. if (ret == -EAGAIN) {
  930. usleep_range(1000, 2000);
  931. ret = drm_dp_aux_get_crc(aux, crc_bytes);
  932. }
  933. if (ret == -EAGAIN) {
  934. DRM_DEBUG_KMS("Get CRC failed after retrying: %d\n",
  935. ret);
  936. continue;
  937. } else if (ret) {
  938. DRM_DEBUG_KMS("Failed to get a CRC: %d\n", ret);
  939. continue;
  940. }
  941. crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
  942. crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
  943. crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
  944. drm_crtc_add_crc_entry(crtc, false, 0, crcs);
  945. }
  946. }
  947. /**
  948. * drm_dp_aux_init() - minimally initialise an aux channel
  949. * @aux: DisplayPort AUX channel
  950. *
  951. * If you need to use the drm_dp_aux's i2c adapter prior to registering it
  952. * with the outside world, call drm_dp_aux_init() first. You must still
  953. * call drm_dp_aux_register() once the connector has been registered to
  954. * allow userspace access to the auxiliary DP channel.
  955. */
  956. void drm_dp_aux_init(struct drm_dp_aux *aux)
  957. {
  958. mutex_init(&aux->hw_mutex);
  959. mutex_init(&aux->cec.lock);
  960. INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
  961. aux->ddc.algo = &drm_dp_i2c_algo;
  962. aux->ddc.algo_data = aux;
  963. aux->ddc.retries = 3;
  964. aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
  965. }
  966. EXPORT_SYMBOL(drm_dp_aux_init);
  967. /**
  968. * drm_dp_aux_register() - initialise and register aux channel
  969. * @aux: DisplayPort AUX channel
  970. *
  971. * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
  972. *
  973. * Returns 0 on success or a negative error code on failure.
  974. */
  975. int drm_dp_aux_register(struct drm_dp_aux *aux)
  976. {
  977. int ret;
  978. if (!aux->ddc.algo)
  979. drm_dp_aux_init(aux);
  980. aux->ddc.class = I2C_CLASS_DDC;
  981. aux->ddc.owner = THIS_MODULE;
  982. aux->ddc.dev.parent = aux->dev;
  983. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  984. sizeof(aux->ddc.name));
  985. ret = drm_dp_aux_register_devnode(aux);
  986. if (ret)
  987. return ret;
  988. ret = i2c_add_adapter(&aux->ddc);
  989. if (ret) {
  990. drm_dp_aux_unregister_devnode(aux);
  991. return ret;
  992. }
  993. return 0;
  994. }
  995. EXPORT_SYMBOL(drm_dp_aux_register);
  996. /**
  997. * drm_dp_aux_unregister() - unregister an AUX adapter
  998. * @aux: DisplayPort AUX channel
  999. */
  1000. void drm_dp_aux_unregister(struct drm_dp_aux *aux)
  1001. {
  1002. drm_dp_aux_unregister_devnode(aux);
  1003. i2c_del_adapter(&aux->ddc);
  1004. }
  1005. EXPORT_SYMBOL(drm_dp_aux_unregister);
  1006. #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
  1007. /**
  1008. * drm_dp_psr_setup_time() - PSR setup in time usec
  1009. * @psr_cap: PSR capabilities from DPCD
  1010. *
  1011. * Returns:
  1012. * PSR setup time for the panel in microseconds, negative
  1013. * error code on failure.
  1014. */
  1015. int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
  1016. {
  1017. static const u16 psr_setup_time_us[] = {
  1018. PSR_SETUP_TIME(330),
  1019. PSR_SETUP_TIME(275),
  1020. PSR_SETUP_TIME(220),
  1021. PSR_SETUP_TIME(165),
  1022. PSR_SETUP_TIME(110),
  1023. PSR_SETUP_TIME(55),
  1024. PSR_SETUP_TIME(0),
  1025. };
  1026. int i;
  1027. i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
  1028. if (i >= ARRAY_SIZE(psr_setup_time_us))
  1029. return -EINVAL;
  1030. return psr_setup_time_us[i];
  1031. }
  1032. EXPORT_SYMBOL(drm_dp_psr_setup_time);
  1033. #undef PSR_SETUP_TIME
  1034. /**
  1035. * drm_dp_start_crc() - start capture of frame CRCs
  1036. * @aux: DisplayPort AUX channel
  1037. * @crtc: CRTC displaying the frames whose CRCs are to be captured
  1038. *
  1039. * Returns 0 on success or a negative error code on failure.
  1040. */
  1041. int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
  1042. {
  1043. u8 buf;
  1044. int ret;
  1045. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1046. if (ret < 0)
  1047. return ret;
  1048. ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
  1049. if (ret < 0)
  1050. return ret;
  1051. aux->crc_count = 0;
  1052. aux->crtc = crtc;
  1053. schedule_work(&aux->crc_work);
  1054. return 0;
  1055. }
  1056. EXPORT_SYMBOL(drm_dp_start_crc);
  1057. /**
  1058. * drm_dp_stop_crc() - stop capture of frame CRCs
  1059. * @aux: DisplayPort AUX channel
  1060. *
  1061. * Returns 0 on success or a negative error code on failure.
  1062. */
  1063. int drm_dp_stop_crc(struct drm_dp_aux *aux)
  1064. {
  1065. u8 buf;
  1066. int ret;
  1067. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1068. if (ret < 0)
  1069. return ret;
  1070. ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
  1071. if (ret < 0)
  1072. return ret;
  1073. flush_work(&aux->crc_work);
  1074. aux->crtc = NULL;
  1075. return 0;
  1076. }
  1077. EXPORT_SYMBOL(drm_dp_stop_crc);
  1078. struct dpcd_quirk {
  1079. u8 oui[3];
  1080. bool is_branch;
  1081. u32 quirks;
  1082. };
  1083. #define OUI(first, second, third) { (first), (second), (third) }
  1084. static const struct dpcd_quirk dpcd_quirk_list[] = {
  1085. /* Analogix 7737 needs reduced M and N at HBR2 link rates */
  1086. { OUI(0x00, 0x22, 0xb9), true, BIT(DP_DPCD_QUIRK_LIMITED_M_N) },
  1087. };
  1088. #undef OUI
  1089. /*
  1090. * Get a bit mask of DPCD quirks for the sink/branch device identified by
  1091. * ident. The quirk data is shared but it's up to the drivers to act on the
  1092. * data.
  1093. *
  1094. * For now, only the OUI (first three bytes) is used, but this may be extended
  1095. * to device identification string and hardware/firmware revisions later.
  1096. */
  1097. static u32
  1098. drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
  1099. {
  1100. const struct dpcd_quirk *quirk;
  1101. u32 quirks = 0;
  1102. int i;
  1103. for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
  1104. quirk = &dpcd_quirk_list[i];
  1105. if (quirk->is_branch != is_branch)
  1106. continue;
  1107. if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
  1108. continue;
  1109. quirks |= quirk->quirks;
  1110. }
  1111. return quirks;
  1112. }
  1113. /**
  1114. * drm_dp_read_desc - read sink/branch descriptor from DPCD
  1115. * @aux: DisplayPort AUX channel
  1116. * @desc: Device decriptor to fill from DPCD
  1117. * @is_branch: true for branch devices, false for sink devices
  1118. *
  1119. * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
  1120. * identification.
  1121. *
  1122. * Returns 0 on success or a negative error code on failure.
  1123. */
  1124. int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
  1125. bool is_branch)
  1126. {
  1127. struct drm_dp_dpcd_ident *ident = &desc->ident;
  1128. unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
  1129. int ret, dev_id_len;
  1130. ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
  1131. if (ret < 0)
  1132. return ret;
  1133. desc->quirks = drm_dp_get_quirks(ident, is_branch);
  1134. dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
  1135. DRM_DEBUG_KMS("DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
  1136. is_branch ? "branch" : "sink",
  1137. (int)sizeof(ident->oui), ident->oui,
  1138. dev_id_len, ident->device_id,
  1139. ident->hw_rev >> 4, ident->hw_rev & 0xf,
  1140. ident->sw_major_rev, ident->sw_minor_rev,
  1141. desc->quirks);
  1142. return 0;
  1143. }
  1144. EXPORT_SYMBOL(drm_dp_read_desc);