drm_dp_dual_mode_helper.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright © 2016 Intel Corporation
  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, sublicense,
  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 shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/export.h>
  24. #include <linux/i2c.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <drm/drm_dp_dual_mode_helper.h>
  28. #include <drm/drmP.h>
  29. /**
  30. * DOC: dp dual mode helpers
  31. *
  32. * Helper functions to deal with DP dual mode (aka. DP++) adaptors.
  33. *
  34. * Type 1:
  35. * Adaptor registers (if any) and the sink DDC bus may be accessed via I2C.
  36. *
  37. * Type 2:
  38. * Adaptor registers and sink DDC bus can be accessed either via I2C or
  39. * I2C-over-AUX. Source devices may choose to implement either of these
  40. * access methods.
  41. */
  42. #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40
  43. /**
  44. * drm_dp_dual_mode_read - Read from the DP dual mode adaptor register(s)
  45. * @adapter: I2C adapter for the DDC bus
  46. * @offset: register offset
  47. * @buffer: buffer for return data
  48. * @size: sizo of the buffer
  49. *
  50. * Reads @size bytes from the DP dual mode adaptor registers
  51. * starting at @offset.
  52. *
  53. * Returns:
  54. * 0 on success, negative error code on failure
  55. */
  56. ssize_t drm_dp_dual_mode_read(struct i2c_adapter *adapter,
  57. u8 offset, void *buffer, size_t size)
  58. {
  59. struct i2c_msg msgs[] = {
  60. {
  61. .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
  62. .flags = 0,
  63. .len = 1,
  64. .buf = &offset,
  65. },
  66. {
  67. .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
  68. .flags = I2C_M_RD,
  69. .len = size,
  70. .buf = buffer,
  71. },
  72. };
  73. int ret;
  74. ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
  75. if (ret < 0)
  76. return ret;
  77. if (ret != ARRAY_SIZE(msgs))
  78. return -EPROTO;
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(drm_dp_dual_mode_read);
  82. /**
  83. * drm_dp_dual_mode_write - Write to the DP dual mode adaptor register(s)
  84. * @adapter: I2C adapter for the DDC bus
  85. * @offset: register offset
  86. * @buffer: buffer for write data
  87. * @size: sizo of the buffer
  88. *
  89. * Writes @size bytes to the DP dual mode adaptor registers
  90. * starting at @offset.
  91. *
  92. * Returns:
  93. * 0 on success, negative error code on failure
  94. */
  95. ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter,
  96. u8 offset, const void *buffer, size_t size)
  97. {
  98. struct i2c_msg msg = {
  99. .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
  100. .flags = 0,
  101. .len = 1 + size,
  102. .buf = NULL,
  103. };
  104. void *data;
  105. int ret;
  106. data = kmalloc(msg.len, GFP_TEMPORARY);
  107. if (!data)
  108. return -ENOMEM;
  109. msg.buf = data;
  110. memcpy(data, &offset, 1);
  111. memcpy(data + 1, buffer, size);
  112. ret = i2c_transfer(adapter, &msg, 1);
  113. kfree(data);
  114. if (ret < 0)
  115. return ret;
  116. if (ret != 1)
  117. return -EPROTO;
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(drm_dp_dual_mode_write);
  121. static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])
  122. {
  123. static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] =
  124. "DP-HDMI ADAPTOR\x04";
  125. return memcmp(hdmi_id, dp_dual_mode_hdmi_id,
  126. sizeof(dp_dual_mode_hdmi_id)) == 0;
  127. }
  128. static bool is_type2_adaptor(uint8_t adaptor_id)
  129. {
  130. return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
  131. DP_DUAL_MODE_REV_TYPE2);
  132. }
  133. /**
  134. * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor
  135. * @adapter: I2C adapter for the DDC bus
  136. *
  137. * Attempt to identify the type of the DP dual mode adaptor used.
  138. *
  139. * Note that when the answer is @DRM_DP_DUAL_MODE_UNKNOWN it's not
  140. * certain whether we're dealing with a native HDMI port or
  141. * a type 1 DVI dual mode adaptor. The driver will have to use
  142. * some other hardware/driver specific mechanism to make that
  143. * distinction.
  144. *
  145. * Returns:
  146. * The type of the DP dual mode adaptor used
  147. */
  148. enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter)
  149. {
  150. char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {};
  151. uint8_t adaptor_id = 0x00;
  152. ssize_t ret;
  153. /*
  154. * Let's see if the adaptor is there the by reading the
  155. * HDMI ID registers.
  156. *
  157. * Note that type 1 DVI adaptors are not required to implemnt
  158. * any registers, and that presents a problem for detection.
  159. * If the i2c transfer is nacked, we may or may not be dealing
  160. * with a type 1 DVI adaptor. Some other mechanism of detecting
  161. * the presence of the adaptor is required. One way would be
  162. * to check the state of the CONFIG1 pin, Another method would
  163. * simply require the driver to know whether the port is a DP++
  164. * port or a native HDMI port. Both of these methods are entirely
  165. * hardware/driver specific so we can't deal with them here.
  166. */
  167. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_HDMI_ID,
  168. hdmi_id, sizeof(hdmi_id));
  169. if (ret)
  170. return DRM_DP_DUAL_MODE_UNKNOWN;
  171. /*
  172. * Sigh. Some (maybe all?) type 1 adaptors are broken and ack
  173. * the offset but ignore it, and instead they just always return
  174. * data from the start of the HDMI ID buffer. So for a broken
  175. * type 1 HDMI adaptor a single byte read will always give us
  176. * 0x44, and for a type 1 DVI adaptor it should give 0x00
  177. * (assuming it implements any registers). Fortunately neither
  178. * of those values will match the type 2 signature of the
  179. * DP_DUAL_MODE_ADAPTOR_ID register so we can proceed with
  180. * the type 2 adaptor detection safely even in the presence
  181. * of broken type 1 adaptors.
  182. */
  183. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_ADAPTOR_ID,
  184. &adaptor_id, sizeof(adaptor_id));
  185. if (ret == 0) {
  186. if (is_type2_adaptor(adaptor_id)) {
  187. if (is_hdmi_adaptor(hdmi_id))
  188. return DRM_DP_DUAL_MODE_TYPE2_HDMI;
  189. else
  190. return DRM_DP_DUAL_MODE_TYPE2_DVI;
  191. }
  192. }
  193. if (is_hdmi_adaptor(hdmi_id))
  194. return DRM_DP_DUAL_MODE_TYPE1_HDMI;
  195. else
  196. return DRM_DP_DUAL_MODE_TYPE1_DVI;
  197. }
  198. EXPORT_SYMBOL(drm_dp_dual_mode_detect);
  199. /**
  200. * drm_dp_dual_mode_max_tmds_clock - Max TMDS clock for DP dual mode adaptor
  201. * @type: DP dual mode adaptor type
  202. * @adapter: I2C adapter for the DDC bus
  203. *
  204. * Determine the max TMDS clock the adaptor supports based on the
  205. * type of the dual mode adaptor and the DP_DUAL_MODE_MAX_TMDS_CLOCK
  206. * register (on type2 adaptors). As some type 1 adaptors have
  207. * problems with registers (see comments in drm_dp_dual_mode_detect())
  208. * we don't read the register on those, instead we simply assume
  209. * a 165 MHz limit based on the specification.
  210. *
  211. * Returns:
  212. * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz.
  213. */
  214. int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,
  215. struct i2c_adapter *adapter)
  216. {
  217. uint8_t max_tmds_clock;
  218. ssize_t ret;
  219. /* native HDMI so no limit */
  220. if (type == DRM_DP_DUAL_MODE_NONE)
  221. return 0;
  222. /*
  223. * Type 1 adaptors are limited to 165MHz
  224. * Type 2 adaptors can tells us their limit
  225. */
  226. if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
  227. return 165000;
  228. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_MAX_TMDS_CLOCK,
  229. &max_tmds_clock, sizeof(max_tmds_clock));
  230. if (ret || max_tmds_clock == 0x00 || max_tmds_clock == 0xff) {
  231. DRM_DEBUG_KMS("Failed to query max TMDS clock\n");
  232. return 165000;
  233. }
  234. return max_tmds_clock * 5000 / 2;
  235. }
  236. EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock);
  237. /**
  238. * drm_dp_dual_mode_get_tmds_output - Get the state of the TMDS output buffers in the DP dual mode adaptor
  239. * @type: DP dual mode adaptor type
  240. * @adapter: I2C adapter for the DDC bus
  241. * @enabled: current state of the TMDS output buffers
  242. *
  243. * Get the state of the TMDS output buffers in the adaptor. For
  244. * type2 adaptors this is queried from the DP_DUAL_MODE_TMDS_OEN
  245. * register. As some type 1 adaptors have problems with registers
  246. * (see comments in drm_dp_dual_mode_detect()) we don't read the
  247. * register on those, instead we simply assume that the buffers
  248. * are always enabled.
  249. *
  250. * Returns:
  251. * 0 on success, negative error code on failure
  252. */
  253. int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,
  254. struct i2c_adapter *adapter,
  255. bool *enabled)
  256. {
  257. uint8_t tmds_oen;
  258. ssize_t ret;
  259. if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) {
  260. *enabled = true;
  261. return 0;
  262. }
  263. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
  264. &tmds_oen, sizeof(tmds_oen));
  265. if (ret) {
  266. DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n");
  267. return ret;
  268. }
  269. *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE);
  270. return 0;
  271. }
  272. EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output);
  273. /**
  274. * drm_dp_dual_mode_set_tmds_output - Enable/disable TMDS output buffers in the DP dual mode adaptor
  275. * @type: DP dual mode adaptor type
  276. * @adapter: I2C adapter for the DDC bus
  277. * @enable: enable (as opposed to disable) the TMDS output buffers
  278. *
  279. * Set the state of the TMDS output buffers in the adaptor. For
  280. * type2 this is set via the DP_DUAL_MODE_TMDS_OEN register. As
  281. * some type 1 adaptors have problems with registers (see comments
  282. * in drm_dp_dual_mode_detect()) we avoid touching the register,
  283. * making this function a no-op on type 1 adaptors.
  284. *
  285. * Returns:
  286. * 0 on success, negative error code on failure
  287. */
  288. int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,
  289. struct i2c_adapter *adapter, bool enable)
  290. {
  291. uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
  292. ssize_t ret;
  293. int retry;
  294. if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
  295. return 0;
  296. /*
  297. * LSPCON adapters in low-power state may ignore the first write, so
  298. * read back and verify the written value a few times.
  299. */
  300. for (retry = 0; retry < 3; retry++) {
  301. uint8_t tmp;
  302. ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
  303. &tmds_oen, sizeof(tmds_oen));
  304. if (ret) {
  305. DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
  306. enable ? "enable" : "disable",
  307. retry + 1);
  308. return ret;
  309. }
  310. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
  311. &tmp, sizeof(tmp));
  312. if (ret) {
  313. DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
  314. enable ? "enabling" : "disabling",
  315. retry + 1);
  316. return ret;
  317. }
  318. if (tmp == tmds_oen)
  319. return 0;
  320. }
  321. DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
  322. enable ? "enabling" : "disabling");
  323. return -EIO;
  324. }
  325. EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
  326. /**
  327. * drm_dp_get_dual_mode_type_name - Get the name of the DP dual mode adaptor type as a string
  328. * @type: DP dual mode adaptor type
  329. *
  330. * Returns:
  331. * String representation of the DP dual mode adaptor type
  332. */
  333. const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)
  334. {
  335. switch (type) {
  336. case DRM_DP_DUAL_MODE_NONE:
  337. return "none";
  338. case DRM_DP_DUAL_MODE_TYPE1_DVI:
  339. return "type 1 DVI";
  340. case DRM_DP_DUAL_MODE_TYPE1_HDMI:
  341. return "type 1 HDMI";
  342. case DRM_DP_DUAL_MODE_TYPE2_DVI:
  343. return "type 2 DVI";
  344. case DRM_DP_DUAL_MODE_TYPE2_HDMI:
  345. return "type 2 HDMI";
  346. default:
  347. WARN_ON(type != DRM_DP_DUAL_MODE_UNKNOWN);
  348. return "unknown";
  349. }
  350. }
  351. EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name);