drm_dp_dual_mode_helper.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/export.h>
  25. #include <linux/i2c.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <drm/drm_dp_dual_mode_helper.h>
  29. #include <drm/drm_print.h>
  30. /**
  31. * DOC: dp dual mode helpers
  32. *
  33. * Helper functions to deal with DP dual mode (aka. DP++) adaptors.
  34. *
  35. * Type 1:
  36. * Adaptor registers (if any) and the sink DDC bus may be accessed via I2C.
  37. *
  38. * Type 2:
  39. * Adaptor registers and sink DDC bus can be accessed either via I2C or
  40. * I2C-over-AUX. Source devices may choose to implement either of these
  41. * access methods.
  42. */
  43. #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40
  44. /**
  45. * drm_dp_dual_mode_read - Read from the DP dual mode adaptor register(s)
  46. * @adapter: I2C adapter for the DDC bus
  47. * @offset: register offset
  48. * @buffer: buffer for return data
  49. * @size: sizo of the buffer
  50. *
  51. * Reads @size bytes from the DP dual mode adaptor registers
  52. * starting at @offset.
  53. *
  54. * Returns:
  55. * 0 on success, negative error code on failure
  56. */
  57. ssize_t drm_dp_dual_mode_read(struct i2c_adapter *adapter,
  58. u8 offset, void *buffer, size_t size)
  59. {
  60. struct i2c_msg msgs[] = {
  61. {
  62. .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
  63. .flags = 0,
  64. .len = 1,
  65. .buf = &offset,
  66. },
  67. {
  68. .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
  69. .flags = I2C_M_RD,
  70. .len = size,
  71. .buf = buffer,
  72. },
  73. };
  74. int ret;
  75. ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
  76. if (ret < 0)
  77. return ret;
  78. if (ret != ARRAY_SIZE(msgs))
  79. return -EPROTO;
  80. return 0;
  81. }
  82. EXPORT_SYMBOL(drm_dp_dual_mode_read);
  83. /**
  84. * drm_dp_dual_mode_write - Write to the DP dual mode adaptor register(s)
  85. * @adapter: I2C adapter for the DDC bus
  86. * @offset: register offset
  87. * @buffer: buffer for write data
  88. * @size: sizo of the buffer
  89. *
  90. * Writes @size bytes to the DP dual mode adaptor registers
  91. * starting at @offset.
  92. *
  93. * Returns:
  94. * 0 on success, negative error code on failure
  95. */
  96. ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter,
  97. u8 offset, const void *buffer, size_t size)
  98. {
  99. struct i2c_msg msg = {
  100. .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
  101. .flags = 0,
  102. .len = 1 + size,
  103. .buf = NULL,
  104. };
  105. void *data;
  106. int ret;
  107. data = kmalloc(msg.len, GFP_KERNEL);
  108. if (!data)
  109. return -ENOMEM;
  110. msg.buf = data;
  111. memcpy(data, &offset, 1);
  112. memcpy(data + 1, buffer, size);
  113. ret = i2c_transfer(adapter, &msg, 1);
  114. kfree(data);
  115. if (ret < 0)
  116. return ret;
  117. if (ret != 1)
  118. return -EPROTO;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(drm_dp_dual_mode_write);
  122. static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])
  123. {
  124. static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] =
  125. "DP-HDMI ADAPTOR\x04";
  126. return memcmp(hdmi_id, dp_dual_mode_hdmi_id,
  127. sizeof(dp_dual_mode_hdmi_id)) == 0;
  128. }
  129. static bool is_type1_adaptor(uint8_t adaptor_id)
  130. {
  131. return adaptor_id == 0 || adaptor_id == 0xff;
  132. }
  133. static bool is_type2_adaptor(uint8_t adaptor_id)
  134. {
  135. return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
  136. DP_DUAL_MODE_REV_TYPE2);
  137. }
  138. static bool is_lspcon_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN],
  139. const uint8_t adaptor_id)
  140. {
  141. return is_hdmi_adaptor(hdmi_id) &&
  142. (adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
  143. DP_DUAL_MODE_TYPE_HAS_DPCD));
  144. }
  145. /**
  146. * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor
  147. * @adapter: I2C adapter for the DDC bus
  148. *
  149. * Attempt to identify the type of the DP dual mode adaptor used.
  150. *
  151. * Note that when the answer is @DRM_DP_DUAL_MODE_UNKNOWN it's not
  152. * certain whether we're dealing with a native HDMI port or
  153. * a type 1 DVI dual mode adaptor. The driver will have to use
  154. * some other hardware/driver specific mechanism to make that
  155. * distinction.
  156. *
  157. * Returns:
  158. * The type of the DP dual mode adaptor used
  159. */
  160. enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter)
  161. {
  162. char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {};
  163. uint8_t adaptor_id = 0x00;
  164. ssize_t ret;
  165. /*
  166. * Let's see if the adaptor is there the by reading the
  167. * HDMI ID registers.
  168. *
  169. * Note that type 1 DVI adaptors are not required to implemnt
  170. * any registers, and that presents a problem for detection.
  171. * If the i2c transfer is nacked, we may or may not be dealing
  172. * with a type 1 DVI adaptor. Some other mechanism of detecting
  173. * the presence of the adaptor is required. One way would be
  174. * to check the state of the CONFIG1 pin, Another method would
  175. * simply require the driver to know whether the port is a DP++
  176. * port or a native HDMI port. Both of these methods are entirely
  177. * hardware/driver specific so we can't deal with them here.
  178. */
  179. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_HDMI_ID,
  180. hdmi_id, sizeof(hdmi_id));
  181. DRM_DEBUG_KMS("DP dual mode HDMI ID: %*pE (err %zd)\n",
  182. ret ? 0 : (int)sizeof(hdmi_id), hdmi_id, ret);
  183. if (ret)
  184. return DRM_DP_DUAL_MODE_UNKNOWN;
  185. /*
  186. * Sigh. Some (maybe all?) type 1 adaptors are broken and ack
  187. * the offset but ignore it, and instead they just always return
  188. * data from the start of the HDMI ID buffer. So for a broken
  189. * type 1 HDMI adaptor a single byte read will always give us
  190. * 0x44, and for a type 1 DVI adaptor it should give 0x00
  191. * (assuming it implements any registers). Fortunately neither
  192. * of those values will match the type 2 signature of the
  193. * DP_DUAL_MODE_ADAPTOR_ID register so we can proceed with
  194. * the type 2 adaptor detection safely even in the presence
  195. * of broken type 1 adaptors.
  196. */
  197. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_ADAPTOR_ID,
  198. &adaptor_id, sizeof(adaptor_id));
  199. DRM_DEBUG_KMS("DP dual mode adaptor ID: %02x (err %zd)\n",
  200. adaptor_id, ret);
  201. if (ret == 0) {
  202. if (is_lspcon_adaptor(hdmi_id, adaptor_id))
  203. return DRM_DP_DUAL_MODE_LSPCON;
  204. if (is_type2_adaptor(adaptor_id)) {
  205. if (is_hdmi_adaptor(hdmi_id))
  206. return DRM_DP_DUAL_MODE_TYPE2_HDMI;
  207. else
  208. return DRM_DP_DUAL_MODE_TYPE2_DVI;
  209. }
  210. /*
  211. * If neither a proper type 1 ID nor a broken type 1 adaptor
  212. * as described above, assume type 1, but let the user know
  213. * that we may have misdetected the type.
  214. */
  215. if (!is_type1_adaptor(adaptor_id) && adaptor_id != hdmi_id[0])
  216. DRM_ERROR("Unexpected DP dual mode adaptor ID %02x\n",
  217. adaptor_id);
  218. }
  219. if (is_hdmi_adaptor(hdmi_id))
  220. return DRM_DP_DUAL_MODE_TYPE1_HDMI;
  221. else
  222. return DRM_DP_DUAL_MODE_TYPE1_DVI;
  223. }
  224. EXPORT_SYMBOL(drm_dp_dual_mode_detect);
  225. /**
  226. * drm_dp_dual_mode_max_tmds_clock - Max TMDS clock for DP dual mode adaptor
  227. * @type: DP dual mode adaptor type
  228. * @adapter: I2C adapter for the DDC bus
  229. *
  230. * Determine the max TMDS clock the adaptor supports based on the
  231. * type of the dual mode adaptor and the DP_DUAL_MODE_MAX_TMDS_CLOCK
  232. * register (on type2 adaptors). As some type 1 adaptors have
  233. * problems with registers (see comments in drm_dp_dual_mode_detect())
  234. * we don't read the register on those, instead we simply assume
  235. * a 165 MHz limit based on the specification.
  236. *
  237. * Returns:
  238. * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz.
  239. */
  240. int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,
  241. struct i2c_adapter *adapter)
  242. {
  243. uint8_t max_tmds_clock;
  244. ssize_t ret;
  245. /* native HDMI so no limit */
  246. if (type == DRM_DP_DUAL_MODE_NONE)
  247. return 0;
  248. /*
  249. * Type 1 adaptors are limited to 165MHz
  250. * Type 2 adaptors can tells us their limit
  251. */
  252. if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
  253. return 165000;
  254. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_MAX_TMDS_CLOCK,
  255. &max_tmds_clock, sizeof(max_tmds_clock));
  256. if (ret || max_tmds_clock == 0x00 || max_tmds_clock == 0xff) {
  257. DRM_DEBUG_KMS("Failed to query max TMDS clock\n");
  258. return 165000;
  259. }
  260. return max_tmds_clock * 5000 / 2;
  261. }
  262. EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock);
  263. /**
  264. * drm_dp_dual_mode_get_tmds_output - Get the state of the TMDS output buffers in the DP dual mode adaptor
  265. * @type: DP dual mode adaptor type
  266. * @adapter: I2C adapter for the DDC bus
  267. * @enabled: current state of the TMDS output buffers
  268. *
  269. * Get the state of the TMDS output buffers in the adaptor. For
  270. * type2 adaptors this is queried from the DP_DUAL_MODE_TMDS_OEN
  271. * register. As some type 1 adaptors have problems with registers
  272. * (see comments in drm_dp_dual_mode_detect()) we don't read the
  273. * register on those, instead we simply assume that the buffers
  274. * are always enabled.
  275. *
  276. * Returns:
  277. * 0 on success, negative error code on failure
  278. */
  279. int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,
  280. struct i2c_adapter *adapter,
  281. bool *enabled)
  282. {
  283. uint8_t tmds_oen;
  284. ssize_t ret;
  285. if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) {
  286. *enabled = true;
  287. return 0;
  288. }
  289. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
  290. &tmds_oen, sizeof(tmds_oen));
  291. if (ret) {
  292. DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n");
  293. return ret;
  294. }
  295. *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output);
  299. /**
  300. * drm_dp_dual_mode_set_tmds_output - Enable/disable TMDS output buffers in the DP dual mode adaptor
  301. * @type: DP dual mode adaptor type
  302. * @adapter: I2C adapter for the DDC bus
  303. * @enable: enable (as opposed to disable) the TMDS output buffers
  304. *
  305. * Set the state of the TMDS output buffers in the adaptor. For
  306. * type2 this is set via the DP_DUAL_MODE_TMDS_OEN register. As
  307. * some type 1 adaptors have problems with registers (see comments
  308. * in drm_dp_dual_mode_detect()) we avoid touching the register,
  309. * making this function a no-op on type 1 adaptors.
  310. *
  311. * Returns:
  312. * 0 on success, negative error code on failure
  313. */
  314. int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,
  315. struct i2c_adapter *adapter, bool enable)
  316. {
  317. uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
  318. ssize_t ret;
  319. int retry;
  320. if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
  321. return 0;
  322. /*
  323. * LSPCON adapters in low-power state may ignore the first write, so
  324. * read back and verify the written value a few times.
  325. */
  326. for (retry = 0; retry < 3; retry++) {
  327. uint8_t tmp;
  328. ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
  329. &tmds_oen, sizeof(tmds_oen));
  330. if (ret) {
  331. DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
  332. enable ? "enable" : "disable",
  333. retry + 1);
  334. return ret;
  335. }
  336. ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
  337. &tmp, sizeof(tmp));
  338. if (ret) {
  339. DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
  340. enable ? "enabling" : "disabling",
  341. retry + 1);
  342. return ret;
  343. }
  344. if (tmp == tmds_oen)
  345. return 0;
  346. }
  347. DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
  348. enable ? "enabling" : "disabling");
  349. return -EIO;
  350. }
  351. EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
  352. /**
  353. * drm_dp_get_dual_mode_type_name - Get the name of the DP dual mode adaptor type as a string
  354. * @type: DP dual mode adaptor type
  355. *
  356. * Returns:
  357. * String representation of the DP dual mode adaptor type
  358. */
  359. const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)
  360. {
  361. switch (type) {
  362. case DRM_DP_DUAL_MODE_NONE:
  363. return "none";
  364. case DRM_DP_DUAL_MODE_TYPE1_DVI:
  365. return "type 1 DVI";
  366. case DRM_DP_DUAL_MODE_TYPE1_HDMI:
  367. return "type 1 HDMI";
  368. case DRM_DP_DUAL_MODE_TYPE2_DVI:
  369. return "type 2 DVI";
  370. case DRM_DP_DUAL_MODE_TYPE2_HDMI:
  371. return "type 2 HDMI";
  372. case DRM_DP_DUAL_MODE_LSPCON:
  373. return "lspcon";
  374. default:
  375. WARN_ON(type != DRM_DP_DUAL_MODE_UNKNOWN);
  376. return "unknown";
  377. }
  378. }
  379. EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name);
  380. /**
  381. * drm_lspcon_get_mode: Get LSPCON's current mode of operation by
  382. * reading offset (0x80, 0x41)
  383. * @adapter: I2C-over-aux adapter
  384. * @mode: current lspcon mode of operation output variable
  385. *
  386. * Returns:
  387. * 0 on success, sets the current_mode value to appropriate mode
  388. * -error on failure
  389. */
  390. int drm_lspcon_get_mode(struct i2c_adapter *adapter,
  391. enum drm_lspcon_mode *mode)
  392. {
  393. u8 data;
  394. int ret = 0;
  395. int retry;
  396. if (!mode) {
  397. DRM_ERROR("NULL input\n");
  398. return -EINVAL;
  399. }
  400. /* Read Status: i2c over aux */
  401. for (retry = 0; retry < 6; retry++) {
  402. if (retry)
  403. usleep_range(500, 1000);
  404. ret = drm_dp_dual_mode_read(adapter,
  405. DP_DUAL_MODE_LSPCON_CURRENT_MODE,
  406. &data, sizeof(data));
  407. if (!ret)
  408. break;
  409. }
  410. if (ret < 0) {
  411. DRM_DEBUG_KMS("LSPCON read(0x80, 0x41) failed\n");
  412. return -EFAULT;
  413. }
  414. if (data & DP_DUAL_MODE_LSPCON_MODE_PCON)
  415. *mode = DRM_LSPCON_MODE_PCON;
  416. else
  417. *mode = DRM_LSPCON_MODE_LS;
  418. return 0;
  419. }
  420. EXPORT_SYMBOL(drm_lspcon_get_mode);
  421. /**
  422. * drm_lspcon_set_mode: Change LSPCON's mode of operation by
  423. * writing offset (0x80, 0x40)
  424. * @adapter: I2C-over-aux adapter
  425. * @mode: required mode of operation
  426. *
  427. * Returns:
  428. * 0 on success, -error on failure/timeout
  429. */
  430. int drm_lspcon_set_mode(struct i2c_adapter *adapter,
  431. enum drm_lspcon_mode mode)
  432. {
  433. u8 data = 0;
  434. int ret;
  435. int time_out = 200;
  436. enum drm_lspcon_mode current_mode;
  437. if (mode == DRM_LSPCON_MODE_PCON)
  438. data = DP_DUAL_MODE_LSPCON_MODE_PCON;
  439. /* Change mode */
  440. ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_LSPCON_MODE_CHANGE,
  441. &data, sizeof(data));
  442. if (ret < 0) {
  443. DRM_ERROR("LSPCON mode change failed\n");
  444. return ret;
  445. }
  446. /*
  447. * Confirm mode change by reading the status bit.
  448. * Sometimes, it takes a while to change the mode,
  449. * so wait and retry until time out or done.
  450. */
  451. do {
  452. ret = drm_lspcon_get_mode(adapter, &current_mode);
  453. if (ret) {
  454. DRM_ERROR("can't confirm LSPCON mode change\n");
  455. return ret;
  456. } else {
  457. if (current_mode != mode) {
  458. msleep(10);
  459. time_out -= 10;
  460. } else {
  461. DRM_DEBUG_KMS("LSPCON mode changed to %s\n",
  462. mode == DRM_LSPCON_MODE_LS ?
  463. "LS" : "PCON");
  464. return 0;
  465. }
  466. }
  467. } while (time_out);
  468. DRM_ERROR("LSPCON mode change timed out\n");
  469. return -ETIMEDOUT;
  470. }
  471. EXPORT_SYMBOL(drm_lspcon_set_mode);