drm_dp_dual_mode_helper.c 15 KB

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