drm_dp_helper.c 22 KB

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