2 * Copyright © 2016 Intel Corporation
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/export.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
30 #include <drm/drm_dp_dual_mode_helper.h>
31 #include <drm/drm_print.h>
34 * DOC: dp dual mode helpers
36 * Helper functions to deal with DP dual mode (aka. DP++) adaptors.
39 * Adaptor registers (if any) and the sink DDC bus may be accessed via I2C.
42 * Adaptor registers and sink DDC bus can be accessed either via I2C or
43 * I2C-over-AUX. Source devices may choose to implement either of these
47 #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40
50 * drm_dp_dual_mode_read - Read from the DP dual mode adaptor register(s)
51 * @adapter: I2C adapter for the DDC bus
52 * @offset: register offset
53 * @buffer: buffer for return data
54 * @size: sizo of the buffer
56 * Reads @size bytes from the DP dual mode adaptor registers
57 * starting at @offset.
60 * 0 on success, negative error code on failure
62 ssize_t
drm_dp_dual_mode_read(struct i2c_adapter
*adapter
,
63 u8 offset
, void *buffer
, size_t size
)
65 struct i2c_msg msgs
[] = {
67 .addr
= DP_DUAL_MODE_SLAVE_ADDRESS
,
73 .addr
= DP_DUAL_MODE_SLAVE_ADDRESS
,
81 ret
= i2c_transfer(adapter
, msgs
, ARRAY_SIZE(msgs
));
84 if (ret
!= ARRAY_SIZE(msgs
))
89 EXPORT_SYMBOL(drm_dp_dual_mode_read
);
92 * drm_dp_dual_mode_write - Write to the DP dual mode adaptor register(s)
93 * @adapter: I2C adapter for the DDC bus
94 * @offset: register offset
95 * @buffer: buffer for write data
96 * @size: sizo of the buffer
98 * Writes @size bytes to the DP dual mode adaptor registers
99 * starting at @offset.
102 * 0 on success, negative error code on failure
104 ssize_t
drm_dp_dual_mode_write(struct i2c_adapter
*adapter
,
105 u8 offset
, const void *buffer
, size_t size
)
107 struct i2c_msg msg
= {
108 .addr
= DP_DUAL_MODE_SLAVE_ADDRESS
,
116 data
= kmalloc(msg
.len
, GFP_KERNEL
);
122 memcpy(data
, &offset
, 1);
123 memcpy(data
+ 1, buffer
, size
);
125 ret
= i2c_transfer(adapter
, &msg
, 1);
136 EXPORT_SYMBOL(drm_dp_dual_mode_write
);
138 static bool is_hdmi_adaptor(const char hdmi_id
[DP_DUAL_MODE_HDMI_ID_LEN
])
140 static const char dp_dual_mode_hdmi_id
[DP_DUAL_MODE_HDMI_ID_LEN
] =
141 "DP-HDMI ADAPTOR\x04";
143 return memcmp(hdmi_id
, dp_dual_mode_hdmi_id
,
144 sizeof(dp_dual_mode_hdmi_id
)) == 0;
147 static bool is_type1_adaptor(uint8_t adaptor_id
)
149 return adaptor_id
== 0 || adaptor_id
== 0xff;
152 static bool is_type2_adaptor(uint8_t adaptor_id
)
154 return adaptor_id
== (DP_DUAL_MODE_TYPE_TYPE2
|
155 DP_DUAL_MODE_REV_TYPE2
);
158 static bool is_lspcon_adaptor(const char hdmi_id
[DP_DUAL_MODE_HDMI_ID_LEN
],
159 const uint8_t adaptor_id
)
161 return is_hdmi_adaptor(hdmi_id
) &&
162 (adaptor_id
== (DP_DUAL_MODE_TYPE_TYPE2
|
163 DP_DUAL_MODE_TYPE_HAS_DPCD
));
167 * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor
168 * @adapter: I2C adapter for the DDC bus
170 * Attempt to identify the type of the DP dual mode adaptor used.
172 * Note that when the answer is @DRM_DP_DUAL_MODE_UNKNOWN it's not
173 * certain whether we're dealing with a native HDMI port or
174 * a type 1 DVI dual mode adaptor. The driver will have to use
175 * some other hardware/driver specific mechanism to make that
179 * The type of the DP dual mode adaptor used
181 enum drm_dp_dual_mode_type
drm_dp_dual_mode_detect(struct i2c_adapter
*adapter
)
183 char hdmi_id
[DP_DUAL_MODE_HDMI_ID_LEN
] = {};
184 uint8_t adaptor_id
= 0x00;
188 * Let's see if the adaptor is there the by reading the
191 * Note that type 1 DVI adaptors are not required to implemnt
192 * any registers, and that presents a problem for detection.
193 * If the i2c transfer is nacked, we may or may not be dealing
194 * with a type 1 DVI adaptor. Some other mechanism of detecting
195 * the presence of the adaptor is required. One way would be
196 * to check the state of the CONFIG1 pin, Another method would
197 * simply require the driver to know whether the port is a DP++
198 * port or a native HDMI port. Both of these methods are entirely
199 * hardware/driver specific so we can't deal with them here.
201 ret
= drm_dp_dual_mode_read(adapter
, DP_DUAL_MODE_HDMI_ID
,
202 hdmi_id
, sizeof(hdmi_id
));
203 DRM_DEBUG_KMS("DP dual mode HDMI ID: %*pE (err %zd)\n",
204 ret
? 0 : (int)sizeof(hdmi_id
), hdmi_id
, ret
);
206 return DRM_DP_DUAL_MODE_UNKNOWN
;
209 * Sigh. Some (maybe all?) type 1 adaptors are broken and ack
210 * the offset but ignore it, and instead they just always return
211 * data from the start of the HDMI ID buffer. So for a broken
212 * type 1 HDMI adaptor a single byte read will always give us
213 * 0x44, and for a type 1 DVI adaptor it should give 0x00
214 * (assuming it implements any registers). Fortunately neither
215 * of those values will match the type 2 signature of the
216 * DP_DUAL_MODE_ADAPTOR_ID register so we can proceed with
217 * the type 2 adaptor detection safely even in the presence
218 * of broken type 1 adaptors.
220 ret
= drm_dp_dual_mode_read(adapter
, DP_DUAL_MODE_ADAPTOR_ID
,
221 &adaptor_id
, sizeof(adaptor_id
));
222 DRM_DEBUG_KMS("DP dual mode adaptor ID: %02x (err %zd)\n",
225 if (is_lspcon_adaptor(hdmi_id
, adaptor_id
))
226 return DRM_DP_DUAL_MODE_LSPCON
;
227 if (is_type2_adaptor(adaptor_id
)) {
228 if (is_hdmi_adaptor(hdmi_id
))
229 return DRM_DP_DUAL_MODE_TYPE2_HDMI
;
231 return DRM_DP_DUAL_MODE_TYPE2_DVI
;
234 * If neither a proper type 1 ID nor a broken type 1 adaptor
235 * as described above, assume type 1, but let the user know
236 * that we may have misdetected the type.
238 if (!is_type1_adaptor(adaptor_id
) && adaptor_id
!= hdmi_id
[0])
239 DRM_ERROR("Unexpected DP dual mode adaptor ID %02x\n",
244 if (is_hdmi_adaptor(hdmi_id
))
245 return DRM_DP_DUAL_MODE_TYPE1_HDMI
;
247 return DRM_DP_DUAL_MODE_TYPE1_DVI
;
249 EXPORT_SYMBOL(drm_dp_dual_mode_detect
);
252 * drm_dp_dual_mode_max_tmds_clock - Max TMDS clock for DP dual mode adaptor
253 * @type: DP dual mode adaptor type
254 * @adapter: I2C adapter for the DDC bus
256 * Determine the max TMDS clock the adaptor supports based on the
257 * type of the dual mode adaptor and the DP_DUAL_MODE_MAX_TMDS_CLOCK
258 * register (on type2 adaptors). As some type 1 adaptors have
259 * problems with registers (see comments in drm_dp_dual_mode_detect())
260 * we don't read the register on those, instead we simply assume
261 * a 165 MHz limit based on the specification.
264 * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz.
266 int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type
,
267 struct i2c_adapter
*adapter
)
269 uint8_t max_tmds_clock
;
272 /* native HDMI so no limit */
273 if (type
== DRM_DP_DUAL_MODE_NONE
)
277 * Type 1 adaptors are limited to 165MHz
278 * Type 2 adaptors can tells us their limit
280 if (type
< DRM_DP_DUAL_MODE_TYPE2_DVI
)
283 ret
= drm_dp_dual_mode_read(adapter
, DP_DUAL_MODE_MAX_TMDS_CLOCK
,
284 &max_tmds_clock
, sizeof(max_tmds_clock
));
285 if (ret
|| max_tmds_clock
== 0x00 || max_tmds_clock
== 0xff) {
286 DRM_DEBUG_KMS("Failed to query max TMDS clock\n");
290 return max_tmds_clock
* 5000 / 2;
292 EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock
);
295 * drm_dp_dual_mode_get_tmds_output - Get the state of the TMDS output buffers in the DP dual mode adaptor
296 * @type: DP dual mode adaptor type
297 * @adapter: I2C adapter for the DDC bus
298 * @enabled: current state of the TMDS output buffers
300 * Get the state of the TMDS output buffers in the adaptor. For
301 * type2 adaptors this is queried from the DP_DUAL_MODE_TMDS_OEN
302 * register. As some type 1 adaptors have problems with registers
303 * (see comments in drm_dp_dual_mode_detect()) we don't read the
304 * register on those, instead we simply assume that the buffers
305 * are always enabled.
308 * 0 on success, negative error code on failure
310 int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type
,
311 struct i2c_adapter
*adapter
,
317 if (type
< DRM_DP_DUAL_MODE_TYPE2_DVI
) {
322 ret
= drm_dp_dual_mode_read(adapter
, DP_DUAL_MODE_TMDS_OEN
,
323 &tmds_oen
, sizeof(tmds_oen
));
325 DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n");
329 *enabled
= !(tmds_oen
& DP_DUAL_MODE_TMDS_DISABLE
);
333 EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output
);
336 * drm_dp_dual_mode_set_tmds_output - Enable/disable TMDS output buffers in the DP dual mode adaptor
337 * @type: DP dual mode adaptor type
338 * @adapter: I2C adapter for the DDC bus
339 * @enable: enable (as opposed to disable) the TMDS output buffers
341 * Set the state of the TMDS output buffers in the adaptor. For
342 * type2 this is set via the DP_DUAL_MODE_TMDS_OEN register. As
343 * some type 1 adaptors have problems with registers (see comments
344 * in drm_dp_dual_mode_detect()) we avoid touching the register,
345 * making this function a no-op on type 1 adaptors.
348 * 0 on success, negative error code on failure
350 int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type
,
351 struct i2c_adapter
*adapter
, bool enable
)
353 uint8_t tmds_oen
= enable
? 0 : DP_DUAL_MODE_TMDS_DISABLE
;
357 if (type
< DRM_DP_DUAL_MODE_TYPE2_DVI
)
361 * LSPCON adapters in low-power state may ignore the first write, so
362 * read back and verify the written value a few times.
364 for (retry
= 0; retry
< 3; retry
++) {
367 ret
= drm_dp_dual_mode_write(adapter
, DP_DUAL_MODE_TMDS_OEN
,
368 &tmds_oen
, sizeof(tmds_oen
));
370 DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
371 enable
? "enable" : "disable",
376 ret
= drm_dp_dual_mode_read(adapter
, DP_DUAL_MODE_TMDS_OEN
,
379 DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
380 enable
? "enabling" : "disabling",
389 DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
390 enable
? "enabling" : "disabling");
394 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output
);
397 * drm_dp_get_dual_mode_type_name - Get the name of the DP dual mode adaptor type as a string
398 * @type: DP dual mode adaptor type
401 * String representation of the DP dual mode adaptor type
403 const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type
)
406 case DRM_DP_DUAL_MODE_NONE
:
408 case DRM_DP_DUAL_MODE_TYPE1_DVI
:
410 case DRM_DP_DUAL_MODE_TYPE1_HDMI
:
411 return "type 1 HDMI";
412 case DRM_DP_DUAL_MODE_TYPE2_DVI
:
414 case DRM_DP_DUAL_MODE_TYPE2_HDMI
:
415 return "type 2 HDMI";
416 case DRM_DP_DUAL_MODE_LSPCON
:
419 WARN_ON(type
!= DRM_DP_DUAL_MODE_UNKNOWN
);
423 EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name
);
426 * drm_lspcon_get_mode: Get LSPCON's current mode of operation by
427 * reading offset (0x80, 0x41)
428 * @adapter: I2C-over-aux adapter
429 * @mode: current lspcon mode of operation output variable
432 * 0 on success, sets the current_mode value to appropriate mode
435 int drm_lspcon_get_mode(struct i2c_adapter
*adapter
,
436 enum drm_lspcon_mode
*mode
)
443 DRM_ERROR("NULL input\n");
447 /* Read Status: i2c over aux */
448 for (retry
= 0; retry
< 6; retry
++) {
450 usleep_range(500, 1000);
452 ret
= drm_dp_dual_mode_read(adapter
,
453 DP_DUAL_MODE_LSPCON_CURRENT_MODE
,
454 &data
, sizeof(data
));
460 DRM_DEBUG_KMS("LSPCON read(0x80, 0x41) failed\n");
464 if (data
& DP_DUAL_MODE_LSPCON_MODE_PCON
)
465 *mode
= DRM_LSPCON_MODE_PCON
;
467 *mode
= DRM_LSPCON_MODE_LS
;
470 EXPORT_SYMBOL(drm_lspcon_get_mode
);
473 * drm_lspcon_set_mode: Change LSPCON's mode of operation by
474 * writing offset (0x80, 0x40)
475 * @adapter: I2C-over-aux adapter
476 * @mode: required mode of operation
479 * 0 on success, -error on failure/timeout
481 int drm_lspcon_set_mode(struct i2c_adapter
*adapter
,
482 enum drm_lspcon_mode mode
)
487 enum drm_lspcon_mode current_mode
;
489 if (mode
== DRM_LSPCON_MODE_PCON
)
490 data
= DP_DUAL_MODE_LSPCON_MODE_PCON
;
493 ret
= drm_dp_dual_mode_write(adapter
, DP_DUAL_MODE_LSPCON_MODE_CHANGE
,
494 &data
, sizeof(data
));
496 DRM_ERROR("LSPCON mode change failed\n");
501 * Confirm mode change by reading the status bit.
502 * Sometimes, it takes a while to change the mode,
503 * so wait and retry until time out or done.
506 ret
= drm_lspcon_get_mode(adapter
, ¤t_mode
);
508 DRM_ERROR("can't confirm LSPCON mode change\n");
511 if (current_mode
!= mode
) {
515 DRM_DEBUG_KMS("LSPCON mode changed to %s\n",
516 mode
== DRM_LSPCON_MODE_LS
?
523 DRM_ERROR("LSPCON mode change timed out\n");
526 EXPORT_SYMBOL(drm_lspcon_set_mode
);