drm_dp_helper.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 <drm/drm_dp_helper.h>
  30. #include <drm/drmP.h>
  31. /**
  32. * DOC: dp helpers
  33. *
  34. * These functions contain some common logic and helpers at various abstraction
  35. * levels to deal with Display Port sink devices and related things like DP aux
  36. * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
  37. * blocks, ...
  38. */
  39. /* Helpers for DP link training */
  40. static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
  41. {
  42. return link_status[r - DP_LANE0_1_STATUS];
  43. }
  44. static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
  45. int lane)
  46. {
  47. int i = DP_LANE0_1_STATUS + (lane >> 1);
  48. int s = (lane & 1) * 4;
  49. u8 l = dp_link_status(link_status, i);
  50. return (l >> s) & 0xf;
  51. }
  52. bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  53. int lane_count)
  54. {
  55. u8 lane_align;
  56. u8 lane_status;
  57. int lane;
  58. lane_align = dp_link_status(link_status,
  59. DP_LANE_ALIGN_STATUS_UPDATED);
  60. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  61. return false;
  62. for (lane = 0; lane < lane_count; lane++) {
  63. lane_status = dp_get_lane_status(link_status, lane);
  64. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  65. return false;
  66. }
  67. return true;
  68. }
  69. EXPORT_SYMBOL(drm_dp_channel_eq_ok);
  70. bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  71. int lane_count)
  72. {
  73. int lane;
  74. u8 lane_status;
  75. for (lane = 0; lane < lane_count; lane++) {
  76. lane_status = dp_get_lane_status(link_status, lane);
  77. if ((lane_status & DP_LANE_CR_DONE) == 0)
  78. return false;
  79. }
  80. return true;
  81. }
  82. EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
  83. u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
  84. int lane)
  85. {
  86. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  87. int s = ((lane & 1) ?
  88. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  89. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  90. u8 l = dp_link_status(link_status, i);
  91. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  92. }
  93. EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
  94. u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
  95. int lane)
  96. {
  97. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  98. int s = ((lane & 1) ?
  99. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  100. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  101. u8 l = dp_link_status(link_status, i);
  102. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  103. }
  104. EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
  105. void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  106. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  107. udelay(100);
  108. else
  109. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  110. }
  111. EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
  112. void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  113. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  114. udelay(400);
  115. else
  116. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  117. }
  118. EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
  119. u8 drm_dp_link_rate_to_bw_code(int link_rate)
  120. {
  121. switch (link_rate) {
  122. case 162000:
  123. default:
  124. return DP_LINK_BW_1_62;
  125. case 270000:
  126. return DP_LINK_BW_2_7;
  127. case 540000:
  128. return DP_LINK_BW_5_4;
  129. }
  130. }
  131. EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
  132. int drm_dp_bw_code_to_link_rate(u8 link_bw)
  133. {
  134. switch (link_bw) {
  135. case DP_LINK_BW_1_62:
  136. default:
  137. return 162000;
  138. case DP_LINK_BW_2_7:
  139. return 270000;
  140. case DP_LINK_BW_5_4:
  141. return 540000;
  142. }
  143. }
  144. EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
  145. #define AUX_RETRY_INTERVAL 500 /* us */
  146. /**
  147. * DOC: dp helpers
  148. *
  149. * The DisplayPort AUX channel is an abstraction to allow generic, driver-
  150. * independent access to AUX functionality. Drivers can take advantage of
  151. * this by filling in the fields of the drm_dp_aux structure.
  152. *
  153. * Transactions are described using a hardware-independent drm_dp_aux_msg
  154. * structure, which is passed into a driver's .transfer() implementation.
  155. * Both native and I2C-over-AUX transactions are supported.
  156. */
  157. static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
  158. unsigned int offset, void *buffer, size_t size)
  159. {
  160. struct drm_dp_aux_msg msg;
  161. unsigned int retry;
  162. int err;
  163. memset(&msg, 0, sizeof(msg));
  164. msg.address = offset;
  165. msg.request = request;
  166. msg.buffer = buffer;
  167. msg.size = size;
  168. /*
  169. * The specification doesn't give any recommendation on how often to
  170. * retry native transactions. We used to retry 7 times like for
  171. * aux i2c transactions but real world devices this wasn't
  172. * sufficient, bump to 32 which makes Dell 4k monitors happier.
  173. */
  174. for (retry = 0; retry < 32; retry++) {
  175. mutex_lock(&aux->hw_mutex);
  176. err = aux->transfer(aux, &msg);
  177. mutex_unlock(&aux->hw_mutex);
  178. if (err < 0) {
  179. if (err == -EBUSY)
  180. continue;
  181. return err;
  182. }
  183. switch (msg.reply & DP_AUX_NATIVE_REPLY_MASK) {
  184. case DP_AUX_NATIVE_REPLY_ACK:
  185. if (err < size)
  186. return -EPROTO;
  187. return err;
  188. case DP_AUX_NATIVE_REPLY_NACK:
  189. return -EIO;
  190. case DP_AUX_NATIVE_REPLY_DEFER:
  191. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  192. break;
  193. }
  194. }
  195. DRM_DEBUG_KMS("too many retries, giving up\n");
  196. return -EIO;
  197. }
  198. /**
  199. * drm_dp_dpcd_read() - read a series of bytes from the DPCD
  200. * @aux: DisplayPort AUX channel
  201. * @offset: address of the (first) register to read
  202. * @buffer: buffer to store the register values
  203. * @size: number of bytes in @buffer
  204. *
  205. * Returns the number of bytes transferred on success, or a negative error
  206. * code on failure. -EIO is returned if the request was NAKed by the sink or
  207. * if the retry count was exceeded. If not all bytes were transferred, this
  208. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  209. * function, with the exception of -EBUSY (which causes the transaction to
  210. * be retried), are propagated to the caller.
  211. */
  212. ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
  213. void *buffer, size_t size)
  214. {
  215. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, buffer,
  216. size);
  217. }
  218. EXPORT_SYMBOL(drm_dp_dpcd_read);
  219. /**
  220. * drm_dp_dpcd_write() - write a series of bytes to the DPCD
  221. * @aux: DisplayPort AUX channel
  222. * @offset: address of the (first) register to write
  223. * @buffer: buffer containing the values to write
  224. * @size: number of bytes in @buffer
  225. *
  226. * Returns the number of bytes transferred on success, or a negative error
  227. * code on failure. -EIO is returned if the request was NAKed by the sink or
  228. * if the retry count was exceeded. If not all bytes were transferred, this
  229. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  230. * function, with the exception of -EBUSY (which causes the transaction to
  231. * be retried), are propagated to the caller.
  232. */
  233. ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
  234. void *buffer, size_t size)
  235. {
  236. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer,
  237. size);
  238. }
  239. EXPORT_SYMBOL(drm_dp_dpcd_write);
  240. /**
  241. * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
  242. * @aux: DisplayPort AUX channel
  243. * @status: buffer to store the link status in (must be at least 6 bytes)
  244. *
  245. * Returns the number of bytes transferred on success or a negative error
  246. * code on failure.
  247. */
  248. int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
  249. u8 status[DP_LINK_STATUS_SIZE])
  250. {
  251. return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
  252. DP_LINK_STATUS_SIZE);
  253. }
  254. EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
  255. /**
  256. * drm_dp_link_probe() - probe a DisplayPort link for capabilities
  257. * @aux: DisplayPort AUX channel
  258. * @link: pointer to structure in which to return link capabilities
  259. *
  260. * The structure filled in by this function can usually be passed directly
  261. * into drm_dp_link_power_up() and drm_dp_link_configure() to power up and
  262. * configure the link based on the link's capabilities.
  263. *
  264. * Returns 0 on success or a negative error code on failure.
  265. */
  266. int drm_dp_link_probe(struct drm_dp_aux *aux, struct drm_dp_link *link)
  267. {
  268. u8 values[3];
  269. int err;
  270. memset(link, 0, sizeof(*link));
  271. err = drm_dp_dpcd_read(aux, DP_DPCD_REV, values, sizeof(values));
  272. if (err < 0)
  273. return err;
  274. link->revision = values[0];
  275. link->rate = drm_dp_bw_code_to_link_rate(values[1]);
  276. link->num_lanes = values[2] & DP_MAX_LANE_COUNT_MASK;
  277. if (values[2] & DP_ENHANCED_FRAME_CAP)
  278. link->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(drm_dp_link_probe);
  282. /**
  283. * drm_dp_link_power_up() - power up a DisplayPort link
  284. * @aux: DisplayPort AUX channel
  285. * @link: pointer to a structure containing the link configuration
  286. *
  287. * Returns 0 on success or a negative error code on failure.
  288. */
  289. int drm_dp_link_power_up(struct drm_dp_aux *aux, struct drm_dp_link *link)
  290. {
  291. u8 value;
  292. int err;
  293. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  294. if (link->revision < 0x11)
  295. return 0;
  296. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  297. if (err < 0)
  298. return err;
  299. value &= ~DP_SET_POWER_MASK;
  300. value |= DP_SET_POWER_D0;
  301. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  302. if (err < 0)
  303. return err;
  304. /*
  305. * According to the DP 1.1 specification, a "Sink Device must exit the
  306. * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
  307. * Control Field" (register 0x600).
  308. */
  309. usleep_range(1000, 2000);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL(drm_dp_link_power_up);
  313. /**
  314. * drm_dp_link_power_down() - power down a DisplayPort link
  315. * @aux: DisplayPort AUX channel
  316. * @link: pointer to a structure containing the link configuration
  317. *
  318. * Returns 0 on success or a negative error code on failure.
  319. */
  320. int drm_dp_link_power_down(struct drm_dp_aux *aux, struct drm_dp_link *link)
  321. {
  322. u8 value;
  323. int err;
  324. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  325. if (link->revision < 0x11)
  326. return 0;
  327. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  328. if (err < 0)
  329. return err;
  330. value &= ~DP_SET_POWER_MASK;
  331. value |= DP_SET_POWER_D3;
  332. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  333. if (err < 0)
  334. return err;
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(drm_dp_link_power_down);
  338. /**
  339. * drm_dp_link_configure() - configure a DisplayPort link
  340. * @aux: DisplayPort AUX channel
  341. * @link: pointer to a structure containing the link configuration
  342. *
  343. * Returns 0 on success or a negative error code on failure.
  344. */
  345. int drm_dp_link_configure(struct drm_dp_aux *aux, struct drm_dp_link *link)
  346. {
  347. u8 values[2];
  348. int err;
  349. values[0] = drm_dp_link_rate_to_bw_code(link->rate);
  350. values[1] = link->num_lanes;
  351. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  352. values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  353. err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
  354. if (err < 0)
  355. return err;
  356. return 0;
  357. }
  358. EXPORT_SYMBOL(drm_dp_link_configure);
  359. /*
  360. * I2C-over-AUX implementation
  361. */
  362. static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
  363. {
  364. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  365. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  366. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  367. I2C_FUNC_10BIT_ADDR;
  368. }
  369. #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
  370. #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
  371. #define AUX_STOP_LEN 4
  372. #define AUX_CMD_LEN 4
  373. #define AUX_ADDRESS_LEN 20
  374. #define AUX_REPLY_PAD_LEN 4
  375. #define AUX_LENGTH_LEN 8
  376. /*
  377. * Calculate the duration of the AUX request/reply in usec. Gives the
  378. * "best" case estimate, ie. successful while as short as possible.
  379. */
  380. static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
  381. {
  382. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  383. AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
  384. if ((msg->request & DP_AUX_I2C_READ) == 0)
  385. len += msg->size * 8;
  386. return len;
  387. }
  388. static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
  389. {
  390. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  391. AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
  392. /*
  393. * For read we expect what was asked. For writes there will
  394. * be 0 or 1 data bytes. Assume 0 for the "best" case.
  395. */
  396. if (msg->request & DP_AUX_I2C_READ)
  397. len += msg->size * 8;
  398. return len;
  399. }
  400. #define I2C_START_LEN 1
  401. #define I2C_STOP_LEN 1
  402. #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
  403. #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
  404. /*
  405. * Calculate the length of the i2c transfer in usec, assuming
  406. * the i2c bus speed is as specified. Gives the the "worst"
  407. * case estimate, ie. successful while as long as possible.
  408. * Doesn't account the the "MOT" bit, and instead assumes each
  409. * message includes a START, ADDRESS and STOP. Neither does it
  410. * account for additional random variables such as clock stretching.
  411. */
  412. static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
  413. int i2c_speed_khz)
  414. {
  415. /* AUX bitrate is 1MHz, i2c bitrate as specified */
  416. return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
  417. msg->size * I2C_DATA_LEN +
  418. I2C_STOP_LEN) * 1000, i2c_speed_khz);
  419. }
  420. /*
  421. * Deterine how many retries should be attempted to successfully transfer
  422. * the specified message, based on the estimated durations of the
  423. * i2c and AUX transfers.
  424. */
  425. static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
  426. int i2c_speed_khz)
  427. {
  428. int aux_time_us = drm_dp_aux_req_duration(msg) +
  429. drm_dp_aux_reply_duration(msg);
  430. int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
  431. return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
  432. }
  433. /*
  434. * FIXME currently assumes 10 kHz as some real world devices seem
  435. * to require it. We should query/set the speed via DPCD if supported.
  436. */
  437. static int dp_aux_i2c_speed_khz __read_mostly = 10;
  438. module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
  439. MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
  440. "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
  441. /*
  442. * Transfer a single I2C-over-AUX message and handle various error conditions,
  443. * retrying the transaction as appropriate. It is assumed that the
  444. * aux->transfer function does not modify anything in the msg other than the
  445. * reply field.
  446. *
  447. * Returns bytes transferred on success, or a negative error code on failure.
  448. */
  449. static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  450. {
  451. unsigned int retry, defer_i2c;
  452. int ret;
  453. /*
  454. * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
  455. * is required to retry at least seven times upon receiving AUX_DEFER
  456. * before giving up the AUX transaction.
  457. *
  458. * We also try to account for the i2c bus speed.
  459. */
  460. int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
  461. for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
  462. mutex_lock(&aux->hw_mutex);
  463. ret = aux->transfer(aux, msg);
  464. mutex_unlock(&aux->hw_mutex);
  465. if (ret < 0) {
  466. if (ret == -EBUSY)
  467. continue;
  468. DRM_DEBUG_KMS("transaction failed: %d\n", ret);
  469. return ret;
  470. }
  471. switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
  472. case DP_AUX_NATIVE_REPLY_ACK:
  473. /*
  474. * For I2C-over-AUX transactions this isn't enough, we
  475. * need to check for the I2C ACK reply.
  476. */
  477. break;
  478. case DP_AUX_NATIVE_REPLY_NACK:
  479. DRM_DEBUG_KMS("native nack (result=%d, size=%zu)\n", ret, msg->size);
  480. return -EREMOTEIO;
  481. case DP_AUX_NATIVE_REPLY_DEFER:
  482. DRM_DEBUG_KMS("native defer\n");
  483. /*
  484. * We could check for I2C bit rate capabilities and if
  485. * available adjust this interval. We could also be
  486. * more careful with DP-to-legacy adapters where a
  487. * long legacy cable may force very low I2C bit rates.
  488. *
  489. * For now just defer for long enough to hopefully be
  490. * safe for all use-cases.
  491. */
  492. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  493. continue;
  494. default:
  495. DRM_ERROR("invalid native reply %#04x\n", msg->reply);
  496. return -EREMOTEIO;
  497. }
  498. switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
  499. case DP_AUX_I2C_REPLY_ACK:
  500. /*
  501. * Both native ACK and I2C ACK replies received. We
  502. * can assume the transfer was successful.
  503. */
  504. return ret;
  505. case DP_AUX_I2C_REPLY_NACK:
  506. DRM_DEBUG_KMS("I2C nack (result=%d, size=%zu\n", ret, msg->size);
  507. aux->i2c_nack_count++;
  508. return -EREMOTEIO;
  509. case DP_AUX_I2C_REPLY_DEFER:
  510. DRM_DEBUG_KMS("I2C defer\n");
  511. /* DP Compliance Test 4.2.2.5 Requirement:
  512. * Must have at least 7 retries for I2C defers on the
  513. * transaction to pass this test
  514. */
  515. aux->i2c_defer_count++;
  516. if (defer_i2c < 7)
  517. defer_i2c++;
  518. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  519. continue;
  520. default:
  521. DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
  522. return -EREMOTEIO;
  523. }
  524. }
  525. DRM_DEBUG_KMS("too many retries, giving up\n");
  526. return -EREMOTEIO;
  527. }
  528. /*
  529. * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
  530. *
  531. * Returns an error code on failure, or a recommended transfer size on success.
  532. */
  533. static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
  534. {
  535. int err, ret = orig_msg->size;
  536. struct drm_dp_aux_msg msg = *orig_msg;
  537. while (msg.size > 0) {
  538. err = drm_dp_i2c_do_msg(aux, &msg);
  539. if (err <= 0)
  540. return err == 0 ? -EPROTO : err;
  541. if (err < msg.size && err < ret) {
  542. DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
  543. msg.size, err);
  544. ret = err;
  545. }
  546. msg.size -= err;
  547. msg.buffer += err;
  548. }
  549. return ret;
  550. }
  551. /*
  552. * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
  553. * packets to be as large as possible. If not, the I2C transactions never
  554. * succeed. Hence the default is maximum.
  555. */
  556. static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
  557. module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
  558. MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
  559. "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
  560. static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  561. int num)
  562. {
  563. struct drm_dp_aux *aux = adapter->algo_data;
  564. unsigned int i, j;
  565. unsigned transfer_size;
  566. struct drm_dp_aux_msg msg;
  567. int err = 0;
  568. dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
  569. memset(&msg, 0, sizeof(msg));
  570. for (i = 0; i < num; i++) {
  571. msg.address = msgs[i].addr;
  572. msg.request = (msgs[i].flags & I2C_M_RD) ?
  573. DP_AUX_I2C_READ :
  574. DP_AUX_I2C_WRITE;
  575. msg.request |= DP_AUX_I2C_MOT;
  576. /* Send a bare address packet to start the transaction.
  577. * Zero sized messages specify an address only (bare
  578. * address) transaction.
  579. */
  580. msg.buffer = NULL;
  581. msg.size = 0;
  582. err = drm_dp_i2c_do_msg(aux, &msg);
  583. if (err < 0)
  584. break;
  585. /* We want each transaction to be as large as possible, but
  586. * we'll go to smaller sizes if the hardware gives us a
  587. * short reply.
  588. */
  589. transfer_size = dp_aux_i2c_transfer_size;
  590. for (j = 0; j < msgs[i].len; j += msg.size) {
  591. msg.buffer = msgs[i].buf + j;
  592. msg.size = min(transfer_size, msgs[i].len - j);
  593. err = drm_dp_i2c_drain_msg(aux, &msg);
  594. if (err < 0)
  595. break;
  596. transfer_size = err;
  597. }
  598. if (err < 0)
  599. break;
  600. }
  601. if (err >= 0)
  602. err = num;
  603. /* Send a bare address packet to close out the transaction.
  604. * Zero sized messages specify an address only (bare
  605. * address) transaction.
  606. */
  607. msg.request &= ~DP_AUX_I2C_MOT;
  608. msg.buffer = NULL;
  609. msg.size = 0;
  610. (void)drm_dp_i2c_do_msg(aux, &msg);
  611. return err;
  612. }
  613. static const struct i2c_algorithm drm_dp_i2c_algo = {
  614. .functionality = drm_dp_i2c_functionality,
  615. .master_xfer = drm_dp_i2c_xfer,
  616. };
  617. /**
  618. * drm_dp_aux_register() - initialise and register aux channel
  619. * @aux: DisplayPort AUX channel
  620. *
  621. * Returns 0 on success or a negative error code on failure.
  622. */
  623. int drm_dp_aux_register(struct drm_dp_aux *aux)
  624. {
  625. mutex_init(&aux->hw_mutex);
  626. aux->ddc.algo = &drm_dp_i2c_algo;
  627. aux->ddc.algo_data = aux;
  628. aux->ddc.retries = 3;
  629. aux->ddc.class = I2C_CLASS_DDC;
  630. aux->ddc.owner = THIS_MODULE;
  631. aux->ddc.dev.parent = aux->dev;
  632. aux->ddc.dev.of_node = aux->dev->of_node;
  633. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  634. sizeof(aux->ddc.name));
  635. return i2c_add_adapter(&aux->ddc);
  636. }
  637. EXPORT_SYMBOL(drm_dp_aux_register);
  638. /**
  639. * drm_dp_aux_unregister() - unregister an AUX adapter
  640. * @aux: DisplayPort AUX channel
  641. */
  642. void drm_dp_aux_unregister(struct drm_dp_aux *aux)
  643. {
  644. i2c_del_adapter(&aux->ddc);
  645. }
  646. EXPORT_SYMBOL(drm_dp_aux_unregister);