drm_dp_helper.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /* Run a single AUX_CH I2C transaction, writing/reading data as necessary */
  40. static int
  41. i2c_algo_dp_aux_transaction(struct i2c_adapter *adapter, int mode,
  42. uint8_t write_byte, uint8_t *read_byte)
  43. {
  44. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  45. int ret;
  46. ret = (*algo_data->aux_ch)(adapter, mode,
  47. write_byte, read_byte);
  48. return ret;
  49. }
  50. /*
  51. * I2C over AUX CH
  52. */
  53. /*
  54. * Send the address. If the I2C link is running, this 'restarts'
  55. * the connection with the new address, this is used for doing
  56. * a write followed by a read (as needed for DDC)
  57. */
  58. static int
  59. i2c_algo_dp_aux_address(struct i2c_adapter *adapter, u16 address, bool reading)
  60. {
  61. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  62. int mode = MODE_I2C_START;
  63. int ret;
  64. if (reading)
  65. mode |= MODE_I2C_READ;
  66. else
  67. mode |= MODE_I2C_WRITE;
  68. algo_data->address = address;
  69. algo_data->running = true;
  70. ret = i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  71. return ret;
  72. }
  73. /*
  74. * Stop the I2C transaction. This closes out the link, sending
  75. * a bare address packet with the MOT bit turned off
  76. */
  77. static void
  78. i2c_algo_dp_aux_stop(struct i2c_adapter *adapter, bool reading)
  79. {
  80. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  81. int mode = MODE_I2C_STOP;
  82. if (reading)
  83. mode |= MODE_I2C_READ;
  84. else
  85. mode |= MODE_I2C_WRITE;
  86. if (algo_data->running) {
  87. (void) i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
  88. algo_data->running = false;
  89. }
  90. }
  91. /*
  92. * Write a single byte to the current I2C address, the
  93. * the I2C link must be running or this returns -EIO
  94. */
  95. static int
  96. i2c_algo_dp_aux_put_byte(struct i2c_adapter *adapter, u8 byte)
  97. {
  98. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  99. int ret;
  100. if (!algo_data->running)
  101. return -EIO;
  102. ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_WRITE, byte, NULL);
  103. return ret;
  104. }
  105. /*
  106. * Read a single byte from the current I2C address, the
  107. * I2C link must be running or this returns -EIO
  108. */
  109. static int
  110. i2c_algo_dp_aux_get_byte(struct i2c_adapter *adapter, u8 *byte_ret)
  111. {
  112. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  113. int ret;
  114. if (!algo_data->running)
  115. return -EIO;
  116. ret = i2c_algo_dp_aux_transaction(adapter, MODE_I2C_READ, 0, byte_ret);
  117. return ret;
  118. }
  119. static int
  120. i2c_algo_dp_aux_xfer(struct i2c_adapter *adapter,
  121. struct i2c_msg *msgs,
  122. int num)
  123. {
  124. int ret = 0;
  125. bool reading = false;
  126. int m;
  127. int b;
  128. for (m = 0; m < num; m++) {
  129. u16 len = msgs[m].len;
  130. u8 *buf = msgs[m].buf;
  131. reading = (msgs[m].flags & I2C_M_RD) != 0;
  132. ret = i2c_algo_dp_aux_address(adapter, msgs[m].addr, reading);
  133. if (ret < 0)
  134. break;
  135. if (reading) {
  136. for (b = 0; b < len; b++) {
  137. ret = i2c_algo_dp_aux_get_byte(adapter, &buf[b]);
  138. if (ret < 0)
  139. break;
  140. }
  141. } else {
  142. for (b = 0; b < len; b++) {
  143. ret = i2c_algo_dp_aux_put_byte(adapter, buf[b]);
  144. if (ret < 0)
  145. break;
  146. }
  147. }
  148. if (ret < 0)
  149. break;
  150. }
  151. if (ret >= 0)
  152. ret = num;
  153. i2c_algo_dp_aux_stop(adapter, reading);
  154. DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
  155. return ret;
  156. }
  157. static u32
  158. i2c_algo_dp_aux_functionality(struct i2c_adapter *adapter)
  159. {
  160. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  161. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  162. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  163. I2C_FUNC_10BIT_ADDR;
  164. }
  165. static const struct i2c_algorithm i2c_dp_aux_algo = {
  166. .master_xfer = i2c_algo_dp_aux_xfer,
  167. .functionality = i2c_algo_dp_aux_functionality,
  168. };
  169. static void
  170. i2c_dp_aux_reset_bus(struct i2c_adapter *adapter)
  171. {
  172. (void) i2c_algo_dp_aux_address(adapter, 0, false);
  173. (void) i2c_algo_dp_aux_stop(adapter, false);
  174. }
  175. static int
  176. i2c_dp_aux_prepare_bus(struct i2c_adapter *adapter)
  177. {
  178. adapter->algo = &i2c_dp_aux_algo;
  179. adapter->retries = 3;
  180. i2c_dp_aux_reset_bus(adapter);
  181. return 0;
  182. }
  183. /**
  184. * i2c_dp_aux_add_bus() - register an i2c adapter using the aux ch helper
  185. * @adapter: i2c adapter to register
  186. *
  187. * This registers an i2c adapater that uses dp aux channel as it's underlaying
  188. * transport. The driver needs to fill out the &i2c_algo_dp_aux_data structure
  189. * and store it in the algo_data member of the @adapter argument. This will be
  190. * used by the i2c over dp aux algorithm to drive the hardware.
  191. *
  192. * RETURNS:
  193. * 0 on success, -ERRNO on failure.
  194. */
  195. int
  196. i2c_dp_aux_add_bus(struct i2c_adapter *adapter)
  197. {
  198. int error;
  199. error = i2c_dp_aux_prepare_bus(adapter);
  200. if (error)
  201. return error;
  202. error = i2c_add_adapter(adapter);
  203. return error;
  204. }
  205. EXPORT_SYMBOL(i2c_dp_aux_add_bus);
  206. /* Helpers for DP link training */
  207. static u8 dp_link_status(u8 link_status[DP_LINK_STATUS_SIZE], int r)
  208. {
  209. return link_status[r - DP_LANE0_1_STATUS];
  210. }
  211. static u8 dp_get_lane_status(u8 link_status[DP_LINK_STATUS_SIZE],
  212. int lane)
  213. {
  214. int i = DP_LANE0_1_STATUS + (lane >> 1);
  215. int s = (lane & 1) * 4;
  216. u8 l = dp_link_status(link_status, i);
  217. return (l >> s) & 0xf;
  218. }
  219. bool drm_dp_channel_eq_ok(u8 link_status[DP_LINK_STATUS_SIZE],
  220. int lane_count)
  221. {
  222. u8 lane_align;
  223. u8 lane_status;
  224. int lane;
  225. lane_align = dp_link_status(link_status,
  226. DP_LANE_ALIGN_STATUS_UPDATED);
  227. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  228. return false;
  229. for (lane = 0; lane < lane_count; lane++) {
  230. lane_status = dp_get_lane_status(link_status, lane);
  231. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  232. return false;
  233. }
  234. return true;
  235. }
  236. EXPORT_SYMBOL(drm_dp_channel_eq_ok);
  237. bool drm_dp_clock_recovery_ok(u8 link_status[DP_LINK_STATUS_SIZE],
  238. int lane_count)
  239. {
  240. int lane;
  241. u8 lane_status;
  242. for (lane = 0; lane < lane_count; lane++) {
  243. lane_status = dp_get_lane_status(link_status, lane);
  244. if ((lane_status & DP_LANE_CR_DONE) == 0)
  245. return false;
  246. }
  247. return true;
  248. }
  249. EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
  250. u8 drm_dp_get_adjust_request_voltage(u8 link_status[DP_LINK_STATUS_SIZE],
  251. int lane)
  252. {
  253. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  254. int s = ((lane & 1) ?
  255. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  256. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  257. u8 l = dp_link_status(link_status, i);
  258. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  259. }
  260. EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
  261. u8 drm_dp_get_adjust_request_pre_emphasis(u8 link_status[DP_LINK_STATUS_SIZE],
  262. int lane)
  263. {
  264. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  265. int s = ((lane & 1) ?
  266. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  267. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  268. u8 l = dp_link_status(link_status, i);
  269. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  270. }
  271. EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
  272. void drm_dp_link_train_clock_recovery_delay(u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  273. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  274. udelay(100);
  275. else
  276. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  277. }
  278. EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
  279. void drm_dp_link_train_channel_eq_delay(u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
  280. if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
  281. udelay(400);
  282. else
  283. mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
  284. }
  285. EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
  286. u8 drm_dp_link_rate_to_bw_code(int link_rate)
  287. {
  288. switch (link_rate) {
  289. case 162000:
  290. default:
  291. return DP_LINK_BW_1_62;
  292. case 270000:
  293. return DP_LINK_BW_2_7;
  294. case 540000:
  295. return DP_LINK_BW_5_4;
  296. }
  297. }
  298. EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
  299. int drm_dp_bw_code_to_link_rate(u8 link_bw)
  300. {
  301. switch (link_bw) {
  302. case DP_LINK_BW_1_62:
  303. default:
  304. return 162000;
  305. case DP_LINK_BW_2_7:
  306. return 270000;
  307. case DP_LINK_BW_5_4:
  308. return 540000;
  309. }
  310. }
  311. EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);