drm_dp_helper.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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, native_reply;
  163. int err = 0, ret = 0;
  164. memset(&msg, 0, sizeof(msg));
  165. msg.address = offset;
  166. msg.request = request;
  167. msg.buffer = buffer;
  168. msg.size = size;
  169. mutex_lock(&aux->hw_mutex);
  170. /*
  171. * The specification doesn't give any recommendation on how often to
  172. * retry native transactions. We used to retry 7 times like for
  173. * aux i2c transactions but real world devices this wasn't
  174. * sufficient, bump to 32 which makes Dell 4k monitors happier.
  175. */
  176. for (retry = 0; retry < 32; retry++) {
  177. if (ret != 0 && ret != -ETIMEDOUT) {
  178. usleep_range(AUX_RETRY_INTERVAL,
  179. AUX_RETRY_INTERVAL + 100);
  180. }
  181. ret = aux->transfer(aux, &msg);
  182. if (ret > 0) {
  183. native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
  184. if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
  185. if (ret == size)
  186. goto unlock;
  187. ret = -EPROTO;
  188. } else
  189. ret = -EIO;
  190. }
  191. /*
  192. * We want the error we return to be the error we received on
  193. * the first transaction, since we may get a different error the
  194. * next time we retry
  195. */
  196. if (!err)
  197. err = ret;
  198. }
  199. DRM_DEBUG_KMS("too many retries, giving up\n");
  200. ret = err;
  201. unlock:
  202. mutex_unlock(&aux->hw_mutex);
  203. return ret;
  204. }
  205. /**
  206. * drm_dp_dpcd_read() - read a series of bytes from the DPCD
  207. * @aux: DisplayPort AUX channel
  208. * @offset: address of the (first) register to read
  209. * @buffer: buffer to store the register values
  210. * @size: number of bytes in @buffer
  211. *
  212. * Returns the number of bytes transferred on success, or a negative error
  213. * code on failure. -EIO is returned if the request was NAKed by the sink or
  214. * if the retry count was exceeded. If not all bytes were transferred, this
  215. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  216. * function, with the exception of -EBUSY (which causes the transaction to
  217. * be retried), are propagated to the caller.
  218. */
  219. ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
  220. void *buffer, size_t size)
  221. {
  222. int ret;
  223. /*
  224. * HP ZR24w corrupts the first DPCD access after entering power save
  225. * mode. Eg. on a read, the entire buffer will be filled with the same
  226. * byte. Do a throw away read to avoid corrupting anything we care
  227. * about. Afterwards things will work correctly until the monitor
  228. * gets woken up and subsequently re-enters power save mode.
  229. *
  230. * The user pressing any button on the monitor is enough to wake it
  231. * up, so there is no particularly good place to do the workaround.
  232. * We just have to do it before any DPCD access and hope that the
  233. * monitor doesn't power down exactly after the throw away read.
  234. */
  235. ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV, buffer,
  236. 1);
  237. if (ret != 1)
  238. return ret;
  239. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, buffer,
  240. size);
  241. }
  242. EXPORT_SYMBOL(drm_dp_dpcd_read);
  243. /**
  244. * drm_dp_dpcd_write() - write a series of bytes to the DPCD
  245. * @aux: DisplayPort AUX channel
  246. * @offset: address of the (first) register to write
  247. * @buffer: buffer containing the values to write
  248. * @size: number of bytes in @buffer
  249. *
  250. * Returns the number of bytes transferred on success, or a negative error
  251. * code on failure. -EIO is returned if the request was NAKed by the sink or
  252. * if the retry count was exceeded. If not all bytes were transferred, this
  253. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  254. * function, with the exception of -EBUSY (which causes the transaction to
  255. * be retried), are propagated to the caller.
  256. */
  257. ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
  258. void *buffer, size_t size)
  259. {
  260. return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer,
  261. size);
  262. }
  263. EXPORT_SYMBOL(drm_dp_dpcd_write);
  264. /**
  265. * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
  266. * @aux: DisplayPort AUX channel
  267. * @status: buffer to store the link status in (must be at least 6 bytes)
  268. *
  269. * Returns the number of bytes transferred on success or a negative error
  270. * code on failure.
  271. */
  272. int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
  273. u8 status[DP_LINK_STATUS_SIZE])
  274. {
  275. return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
  276. DP_LINK_STATUS_SIZE);
  277. }
  278. EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
  279. /**
  280. * drm_dp_link_probe() - probe a DisplayPort link for capabilities
  281. * @aux: DisplayPort AUX channel
  282. * @link: pointer to structure in which to return link capabilities
  283. *
  284. * The structure filled in by this function can usually be passed directly
  285. * into drm_dp_link_power_up() and drm_dp_link_configure() to power up and
  286. * configure the link based on the link's capabilities.
  287. *
  288. * Returns 0 on success or a negative error code on failure.
  289. */
  290. int drm_dp_link_probe(struct drm_dp_aux *aux, struct drm_dp_link *link)
  291. {
  292. u8 values[3];
  293. int err;
  294. memset(link, 0, sizeof(*link));
  295. err = drm_dp_dpcd_read(aux, DP_DPCD_REV, values, sizeof(values));
  296. if (err < 0)
  297. return err;
  298. link->revision = values[0];
  299. link->rate = drm_dp_bw_code_to_link_rate(values[1]);
  300. link->num_lanes = values[2] & DP_MAX_LANE_COUNT_MASK;
  301. if (values[2] & DP_ENHANCED_FRAME_CAP)
  302. link->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(drm_dp_link_probe);
  306. /**
  307. * drm_dp_link_power_up() - power up a DisplayPort link
  308. * @aux: DisplayPort AUX channel
  309. * @link: pointer to a structure containing the link configuration
  310. *
  311. * Returns 0 on success or a negative error code on failure.
  312. */
  313. int drm_dp_link_power_up(struct drm_dp_aux *aux, struct drm_dp_link *link)
  314. {
  315. u8 value;
  316. int err;
  317. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  318. if (link->revision < 0x11)
  319. return 0;
  320. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  321. if (err < 0)
  322. return err;
  323. value &= ~DP_SET_POWER_MASK;
  324. value |= DP_SET_POWER_D0;
  325. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  326. if (err < 0)
  327. return err;
  328. /*
  329. * According to the DP 1.1 specification, a "Sink Device must exit the
  330. * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
  331. * Control Field" (register 0x600).
  332. */
  333. usleep_range(1000, 2000);
  334. return 0;
  335. }
  336. EXPORT_SYMBOL(drm_dp_link_power_up);
  337. /**
  338. * drm_dp_link_power_down() - power down a DisplayPort link
  339. * @aux: DisplayPort AUX channel
  340. * @link: pointer to a structure containing the link configuration
  341. *
  342. * Returns 0 on success or a negative error code on failure.
  343. */
  344. int drm_dp_link_power_down(struct drm_dp_aux *aux, struct drm_dp_link *link)
  345. {
  346. u8 value;
  347. int err;
  348. /* DP_SET_POWER register is only available on DPCD v1.1 and later */
  349. if (link->revision < 0x11)
  350. return 0;
  351. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  352. if (err < 0)
  353. return err;
  354. value &= ~DP_SET_POWER_MASK;
  355. value |= DP_SET_POWER_D3;
  356. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  357. if (err < 0)
  358. return err;
  359. return 0;
  360. }
  361. EXPORT_SYMBOL(drm_dp_link_power_down);
  362. /**
  363. * drm_dp_link_configure() - configure a DisplayPort link
  364. * @aux: DisplayPort AUX channel
  365. * @link: pointer to a structure containing the link configuration
  366. *
  367. * Returns 0 on success or a negative error code on failure.
  368. */
  369. int drm_dp_link_configure(struct drm_dp_aux *aux, struct drm_dp_link *link)
  370. {
  371. u8 values[2];
  372. int err;
  373. values[0] = drm_dp_link_rate_to_bw_code(link->rate);
  374. values[1] = link->num_lanes;
  375. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  376. values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  377. err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
  378. if (err < 0)
  379. return err;
  380. return 0;
  381. }
  382. EXPORT_SYMBOL(drm_dp_link_configure);
  383. /*
  384. * I2C-over-AUX implementation
  385. */
  386. static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
  387. {
  388. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  389. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  390. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  391. I2C_FUNC_10BIT_ADDR;
  392. }
  393. static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
  394. {
  395. /*
  396. * In case of i2c defer or short i2c ack reply to a write,
  397. * we need to switch to WRITE_STATUS_UPDATE to drain the
  398. * rest of the message
  399. */
  400. if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
  401. msg->request &= DP_AUX_I2C_MOT;
  402. msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
  403. }
  404. }
  405. #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
  406. #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
  407. #define AUX_STOP_LEN 4
  408. #define AUX_CMD_LEN 4
  409. #define AUX_ADDRESS_LEN 20
  410. #define AUX_REPLY_PAD_LEN 4
  411. #define AUX_LENGTH_LEN 8
  412. /*
  413. * Calculate the duration of the AUX request/reply in usec. Gives the
  414. * "best" case estimate, ie. successful while as short as possible.
  415. */
  416. static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
  417. {
  418. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  419. AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
  420. if ((msg->request & DP_AUX_I2C_READ) == 0)
  421. len += msg->size * 8;
  422. return len;
  423. }
  424. static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
  425. {
  426. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  427. AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
  428. /*
  429. * For read we expect what was asked. For writes there will
  430. * be 0 or 1 data bytes. Assume 0 for the "best" case.
  431. */
  432. if (msg->request & DP_AUX_I2C_READ)
  433. len += msg->size * 8;
  434. return len;
  435. }
  436. #define I2C_START_LEN 1
  437. #define I2C_STOP_LEN 1
  438. #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
  439. #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
  440. /*
  441. * Calculate the length of the i2c transfer in usec, assuming
  442. * the i2c bus speed is as specified. Gives the the "worst"
  443. * case estimate, ie. successful while as long as possible.
  444. * Doesn't account the the "MOT" bit, and instead assumes each
  445. * message includes a START, ADDRESS and STOP. Neither does it
  446. * account for additional random variables such as clock stretching.
  447. */
  448. static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
  449. int i2c_speed_khz)
  450. {
  451. /* AUX bitrate is 1MHz, i2c bitrate as specified */
  452. return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
  453. msg->size * I2C_DATA_LEN +
  454. I2C_STOP_LEN) * 1000, i2c_speed_khz);
  455. }
  456. /*
  457. * Deterine how many retries should be attempted to successfully transfer
  458. * the specified message, based on the estimated durations of the
  459. * i2c and AUX transfers.
  460. */
  461. static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
  462. int i2c_speed_khz)
  463. {
  464. int aux_time_us = drm_dp_aux_req_duration(msg) +
  465. drm_dp_aux_reply_duration(msg);
  466. int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
  467. return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
  468. }
  469. /*
  470. * FIXME currently assumes 10 kHz as some real world devices seem
  471. * to require it. We should query/set the speed via DPCD if supported.
  472. */
  473. static int dp_aux_i2c_speed_khz __read_mostly = 10;
  474. module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
  475. MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
  476. "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
  477. /*
  478. * Transfer a single I2C-over-AUX message and handle various error conditions,
  479. * retrying the transaction as appropriate. It is assumed that the
  480. * aux->transfer function does not modify anything in the msg other than the
  481. * reply field.
  482. *
  483. * Returns bytes transferred on success, or a negative error code on failure.
  484. */
  485. static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  486. {
  487. unsigned int retry, defer_i2c;
  488. int ret;
  489. /*
  490. * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
  491. * is required to retry at least seven times upon receiving AUX_DEFER
  492. * before giving up the AUX transaction.
  493. *
  494. * We also try to account for the i2c bus speed.
  495. */
  496. int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
  497. for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
  498. ret = aux->transfer(aux, msg);
  499. if (ret < 0) {
  500. if (ret == -EBUSY)
  501. continue;
  502. DRM_DEBUG_KMS("transaction failed: %d\n", ret);
  503. return ret;
  504. }
  505. switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
  506. case DP_AUX_NATIVE_REPLY_ACK:
  507. /*
  508. * For I2C-over-AUX transactions this isn't enough, we
  509. * need to check for the I2C ACK reply.
  510. */
  511. break;
  512. case DP_AUX_NATIVE_REPLY_NACK:
  513. DRM_DEBUG_KMS("native nack (result=%d, size=%zu)\n", ret, msg->size);
  514. return -EREMOTEIO;
  515. case DP_AUX_NATIVE_REPLY_DEFER:
  516. DRM_DEBUG_KMS("native defer\n");
  517. /*
  518. * We could check for I2C bit rate capabilities and if
  519. * available adjust this interval. We could also be
  520. * more careful with DP-to-legacy adapters where a
  521. * long legacy cable may force very low I2C bit rates.
  522. *
  523. * For now just defer for long enough to hopefully be
  524. * safe for all use-cases.
  525. */
  526. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  527. continue;
  528. default:
  529. DRM_ERROR("invalid native reply %#04x\n", msg->reply);
  530. return -EREMOTEIO;
  531. }
  532. switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
  533. case DP_AUX_I2C_REPLY_ACK:
  534. /*
  535. * Both native ACK and I2C ACK replies received. We
  536. * can assume the transfer was successful.
  537. */
  538. if (ret != msg->size)
  539. drm_dp_i2c_msg_write_status_update(msg);
  540. return ret;
  541. case DP_AUX_I2C_REPLY_NACK:
  542. DRM_DEBUG_KMS("I2C nack (result=%d, size=%zu\n", ret, msg->size);
  543. aux->i2c_nack_count++;
  544. return -EREMOTEIO;
  545. case DP_AUX_I2C_REPLY_DEFER:
  546. DRM_DEBUG_KMS("I2C defer\n");
  547. /* DP Compliance Test 4.2.2.5 Requirement:
  548. * Must have at least 7 retries for I2C defers on the
  549. * transaction to pass this test
  550. */
  551. aux->i2c_defer_count++;
  552. if (defer_i2c < 7)
  553. defer_i2c++;
  554. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  555. drm_dp_i2c_msg_write_status_update(msg);
  556. continue;
  557. default:
  558. DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
  559. return -EREMOTEIO;
  560. }
  561. }
  562. DRM_DEBUG_KMS("too many retries, giving up\n");
  563. return -EREMOTEIO;
  564. }
  565. static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
  566. const struct i2c_msg *i2c_msg)
  567. {
  568. msg->request = (i2c_msg->flags & I2C_M_RD) ?
  569. DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
  570. msg->request |= DP_AUX_I2C_MOT;
  571. }
  572. /*
  573. * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
  574. *
  575. * Returns an error code on failure, or a recommended transfer size on success.
  576. */
  577. static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
  578. {
  579. int err, ret = orig_msg->size;
  580. struct drm_dp_aux_msg msg = *orig_msg;
  581. while (msg.size > 0) {
  582. err = drm_dp_i2c_do_msg(aux, &msg);
  583. if (err <= 0)
  584. return err == 0 ? -EPROTO : err;
  585. if (err < msg.size && err < ret) {
  586. DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
  587. msg.size, err);
  588. ret = err;
  589. }
  590. msg.size -= err;
  591. msg.buffer += err;
  592. }
  593. return ret;
  594. }
  595. /*
  596. * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
  597. * packets to be as large as possible. If not, the I2C transactions never
  598. * succeed. Hence the default is maximum.
  599. */
  600. static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
  601. module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
  602. MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
  603. "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
  604. static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  605. int num)
  606. {
  607. struct drm_dp_aux *aux = adapter->algo_data;
  608. unsigned int i, j;
  609. unsigned transfer_size;
  610. struct drm_dp_aux_msg msg;
  611. int err = 0;
  612. dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
  613. memset(&msg, 0, sizeof(msg));
  614. mutex_lock(&aux->hw_mutex);
  615. for (i = 0; i < num; i++) {
  616. msg.address = msgs[i].addr;
  617. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  618. /* Send a bare address packet to start the transaction.
  619. * Zero sized messages specify an address only (bare
  620. * address) transaction.
  621. */
  622. msg.buffer = NULL;
  623. msg.size = 0;
  624. err = drm_dp_i2c_do_msg(aux, &msg);
  625. /*
  626. * Reset msg.request in case in case it got
  627. * changed into a WRITE_STATUS_UPDATE.
  628. */
  629. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  630. if (err < 0)
  631. break;
  632. /* We want each transaction to be as large as possible, but
  633. * we'll go to smaller sizes if the hardware gives us a
  634. * short reply.
  635. */
  636. transfer_size = dp_aux_i2c_transfer_size;
  637. for (j = 0; j < msgs[i].len; j += msg.size) {
  638. msg.buffer = msgs[i].buf + j;
  639. msg.size = min(transfer_size, msgs[i].len - j);
  640. err = drm_dp_i2c_drain_msg(aux, &msg);
  641. /*
  642. * Reset msg.request in case in case it got
  643. * changed into a WRITE_STATUS_UPDATE.
  644. */
  645. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  646. if (err < 0)
  647. break;
  648. transfer_size = err;
  649. }
  650. if (err < 0)
  651. break;
  652. }
  653. if (err >= 0)
  654. err = num;
  655. /* Send a bare address packet to close out the transaction.
  656. * Zero sized messages specify an address only (bare
  657. * address) transaction.
  658. */
  659. msg.request &= ~DP_AUX_I2C_MOT;
  660. msg.buffer = NULL;
  661. msg.size = 0;
  662. (void)drm_dp_i2c_do_msg(aux, &msg);
  663. mutex_unlock(&aux->hw_mutex);
  664. return err;
  665. }
  666. static const struct i2c_algorithm drm_dp_i2c_algo = {
  667. .functionality = drm_dp_i2c_functionality,
  668. .master_xfer = drm_dp_i2c_xfer,
  669. };
  670. /**
  671. * drm_dp_aux_register() - initialise and register aux channel
  672. * @aux: DisplayPort AUX channel
  673. *
  674. * Returns 0 on success or a negative error code on failure.
  675. */
  676. int drm_dp_aux_register(struct drm_dp_aux *aux)
  677. {
  678. int ret;
  679. mutex_init(&aux->hw_mutex);
  680. aux->ddc.algo = &drm_dp_i2c_algo;
  681. aux->ddc.algo_data = aux;
  682. aux->ddc.retries = 3;
  683. aux->ddc.class = I2C_CLASS_DDC;
  684. aux->ddc.owner = THIS_MODULE;
  685. aux->ddc.dev.parent = aux->dev;
  686. aux->ddc.dev.of_node = aux->dev->of_node;
  687. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  688. sizeof(aux->ddc.name));
  689. ret = drm_dp_aux_register_devnode(aux);
  690. if (ret)
  691. return ret;
  692. ret = i2c_add_adapter(&aux->ddc);
  693. if (ret) {
  694. drm_dp_aux_unregister_devnode(aux);
  695. return ret;
  696. }
  697. return 0;
  698. }
  699. EXPORT_SYMBOL(drm_dp_aux_register);
  700. /**
  701. * drm_dp_aux_unregister() - unregister an AUX adapter
  702. * @aux: DisplayPort AUX channel
  703. */
  704. void drm_dp_aux_unregister(struct drm_dp_aux *aux)
  705. {
  706. drm_dp_aux_unregister_devnode(aux);
  707. i2c_del_adapter(&aux->ddc);
  708. }
  709. EXPORT_SYMBOL(drm_dp_aux_unregister);