drm_scdc_helper.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <drm/display/drm_scdc_helper.h>
  27. #include <drm/drm_connector.h>
  28. #include <drm/drm_device.h>
  29. #include <drm/drm_print.h>
  30. /**
  31. * DOC: scdc helpers
  32. *
  33. * Status and Control Data Channel (SCDC) is a mechanism introduced by the
  34. * HDMI 2.0 specification. It is a point-to-point protocol that allows the
  35. * HDMI source and HDMI sink to exchange data. The same I2C interface that
  36. * is used to access EDID serves as the transport mechanism for SCDC.
  37. *
  38. * Note: The SCDC status is going to be lost when the display is
  39. * disconnected. This can happen physically when the user disconnects
  40. * the cable, but also when a display is switched on (such as waking up
  41. * a TV).
  42. *
  43. * This is further complicated by the fact that, upon a disconnection /
  44. * reconnection, KMS won't change the mode on its own. This means that
  45. * one can't just rely on setting the SCDC status on enable, but also
  46. * has to track the connector status changes using interrupts and
  47. * restore the SCDC status. The typical solution for this is to trigger an
  48. * empty modeset in drm_connector_helper_funcs.detect_ctx(), like what vc4 does
  49. * in vc4_hdmi_reset_link().
  50. */
  51. #define SCDC_I2C_SLAVE_ADDRESS 0x54
  52. /**
  53. * drm_scdc_read - read a block of data from SCDC
  54. * @adapter: I2C controller
  55. * @offset: start offset of block to read
  56. * @buffer: return location for the block to read
  57. * @size: size of the block to read
  58. *
  59. * Reads a block of data from SCDC, starting at a given offset.
  60. *
  61. * Returns:
  62. * 0 on success, negative error code on failure.
  63. */
  64. ssize_t drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer,
  65. size_t size)
  66. {
  67. int ret;
  68. struct i2c_msg msgs[2] = {
  69. {
  70. .addr = SCDC_I2C_SLAVE_ADDRESS,
  71. .flags = 0,
  72. .len = 1,
  73. .buf = &offset,
  74. }, {
  75. .addr = SCDC_I2C_SLAVE_ADDRESS,
  76. .flags = I2C_M_RD,
  77. .len = size,
  78. .buf = buffer,
  79. }
  80. };
  81. ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
  82. if (ret < 0)
  83. return ret;
  84. if (ret != ARRAY_SIZE(msgs))
  85. return -EPROTO;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(drm_scdc_read);
  89. /**
  90. * drm_scdc_write - write a block of data to SCDC
  91. * @adapter: I2C controller
  92. * @offset: start offset of block to write
  93. * @buffer: block of data to write
  94. * @size: size of the block to write
  95. *
  96. * Writes a block of data to SCDC, starting at a given offset.
  97. *
  98. * Returns:
  99. * 0 on success, negative error code on failure.
  100. */
  101. ssize_t drm_scdc_write(struct i2c_adapter *adapter, u8 offset,
  102. const void *buffer, size_t size)
  103. {
  104. struct i2c_msg msg = {
  105. .addr = SCDC_I2C_SLAVE_ADDRESS,
  106. .flags = 0,
  107. .len = 1 + size,
  108. .buf = NULL,
  109. };
  110. void *data;
  111. int err;
  112. data = kmalloc(1 + size, GFP_KERNEL);
  113. if (!data)
  114. return -ENOMEM;
  115. msg.buf = data;
  116. memcpy(data, &offset, sizeof(offset));
  117. memcpy(data + 1, buffer, size);
  118. err = i2c_transfer(adapter, &msg, 1);
  119. kfree(data);
  120. if (err < 0)
  121. return err;
  122. if (err != 1)
  123. return -EPROTO;
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(drm_scdc_write);
  127. /**
  128. * drm_scdc_get_scrambling_status - what is status of scrambling?
  129. * @connector: connector
  130. *
  131. * Reads the scrambler status over SCDC, and checks the
  132. * scrambling status.
  133. *
  134. * Returns:
  135. * True if the scrambling is enabled, false otherwise.
  136. */
  137. bool drm_scdc_get_scrambling_status(struct drm_connector *connector)
  138. {
  139. u8 status;
  140. int ret;
  141. ret = drm_scdc_readb(connector->ddc, SCDC_SCRAMBLER_STATUS, &status);
  142. if (ret < 0) {
  143. drm_dbg_kms(connector->dev,
  144. "[CONNECTOR:%d:%s] Failed to read scrambling status: %d\n",
  145. connector->base.id, connector->name, ret);
  146. return false;
  147. }
  148. return status & SCDC_SCRAMBLING_STATUS;
  149. }
  150. EXPORT_SYMBOL(drm_scdc_get_scrambling_status);
  151. /**
  152. * drm_scdc_set_scrambling - enable scrambling
  153. * @connector: connector
  154. * @enable: bool to indicate if scrambling is to be enabled/disabled
  155. *
  156. * Writes the TMDS config register over SCDC channel, and:
  157. * enables scrambling when enable = 1
  158. * disables scrambling when enable = 0
  159. *
  160. * Returns:
  161. * True if scrambling is set/reset successfully, false otherwise.
  162. */
  163. bool drm_scdc_set_scrambling(struct drm_connector *connector,
  164. bool enable)
  165. {
  166. u8 config;
  167. int ret;
  168. ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
  169. if (ret < 0) {
  170. drm_dbg_kms(connector->dev,
  171. "[CONNECTOR:%d:%s] Failed to read TMDS config: %d\n",
  172. connector->base.id, connector->name, ret);
  173. return false;
  174. }
  175. if (enable)
  176. config |= SCDC_SCRAMBLING_ENABLE;
  177. else
  178. config &= ~SCDC_SCRAMBLING_ENABLE;
  179. ret = drm_scdc_writeb(connector->ddc, SCDC_TMDS_CONFIG, config);
  180. if (ret < 0) {
  181. drm_dbg_kms(connector->dev,
  182. "[CONNECTOR:%d:%s] Failed to enable scrambling: %d\n",
  183. connector->base.id, connector->name, ret);
  184. return false;
  185. }
  186. return true;
  187. }
  188. EXPORT_SYMBOL(drm_scdc_set_scrambling);
  189. /**
  190. * drm_scdc_set_high_tmds_clock_ratio - set TMDS clock ratio
  191. * @connector: connector
  192. * @set: ret or reset the high clock ratio
  193. *
  194. *
  195. * TMDS clock ratio calculations go like this:
  196. * TMDS character = 10 bit TMDS encoded value
  197. *
  198. * TMDS character rate = The rate at which TMDS characters are
  199. * transmitted (Mcsc)
  200. *
  201. * TMDS bit rate = 10x TMDS character rate
  202. *
  203. * As per the spec:
  204. * TMDS clock rate for pixel clock < 340 MHz = 1x the character
  205. * rate = 1/10 pixel clock rate
  206. *
  207. * TMDS clock rate for pixel clock > 340 MHz = 0.25x the character
  208. * rate = 1/40 pixel clock rate
  209. *
  210. * Writes to the TMDS config register over SCDC channel, and:
  211. * sets TMDS clock ratio to 1/40 when set = 1
  212. *
  213. * sets TMDS clock ratio to 1/10 when set = 0
  214. *
  215. * Returns:
  216. * True if write is successful, false otherwise.
  217. */
  218. bool drm_scdc_set_high_tmds_clock_ratio(struct drm_connector *connector,
  219. bool set)
  220. {
  221. u8 config;
  222. int ret;
  223. ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
  224. if (ret < 0) {
  225. drm_dbg_kms(connector->dev,
  226. "[CONNECTOR:%d:%s] Failed to read TMDS config: %d\n",
  227. connector->base.id, connector->name, ret);
  228. return false;
  229. }
  230. if (set)
  231. config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
  232. else
  233. config &= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
  234. ret = drm_scdc_writeb(connector->ddc, SCDC_TMDS_CONFIG, config);
  235. if (ret < 0) {
  236. drm_dbg_kms(connector->dev,
  237. "[CONNECTOR:%d:%s] Failed to set TMDS clock ratio: %d\n",
  238. connector->base.id, connector->name, ret);
  239. return false;
  240. }
  241. /*
  242. * The spec says that a source should wait minimum 1ms and maximum
  243. * 100ms after writing the TMDS config for clock ratio. Lets allow a
  244. * wait of up to 2ms here.
  245. */
  246. usleep_range(1000, 2000);
  247. return true;
  248. }
  249. EXPORT_SYMBOL(drm_scdc_set_high_tmds_clock_ratio);