drm_dp_helper.c 8.2 KB

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