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)
68 * As sub-addressing is not supported by all adaptors,
69 * always explicitly read from the start and discard
70 * any bytes that come before the requested offset.
71 * This way, no matter whether the adaptor supports it
72 * or not, we'll end up reading the proper data.
74 struct i2c_msg msgs[] = {
76 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
82 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
91 tmpbuf = kmalloc(size + offset, GFP_KERNEL);
98 ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
100 memcpy(buffer, tmpbuf + offset, size);
106 if (ret != ARRAY_SIZE(msgs))
111 EXPORT_SYMBOL(drm_dp_dual_mode_read);
114 * drm_dp_dual_mode_write - Write to the DP dual mode adaptor register(s)
115 * @adapter: I2C adapter for the DDC bus
116 * @offset: register offset
117 * @buffer: buffer for write data
118 * @size: sizo of the buffer
120 * Writes @size bytes to the DP dual mode adaptor registers
121 * starting at @offset.
124 * 0 on success, negative error code on failure
126 ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter,
127 u8 offset, const void *buffer, size_t size)
129 struct i2c_msg msg = {
130 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
138 data = kmalloc(msg.len, GFP_KERNEL);
144 memcpy(data, &offset, 1);
145 memcpy(data + 1, buffer, size);
147 ret = i2c_transfer(adapter, &msg, 1);
158 EXPORT_SYMBOL(drm_dp_dual_mode_write);
160 static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])
162 static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] =
163 "DP-HDMI ADAPTOR\x04";
165 return memcmp(hdmi_id, dp_dual_mode_hdmi_id,
166 sizeof(dp_dual_mode_hdmi_id)) == 0;
169 static bool is_type1_adaptor(uint8_t adaptor_id)
171 return adaptor_id == 0 || adaptor_id == 0xff;
174 static bool is_type2_adaptor(uint8_t adaptor_id)
176 return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
177 DP_DUAL_MODE_REV_TYPE2);
180 static bool is_lspcon_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN],
181 const uint8_t adaptor_id)
183 return is_hdmi_adaptor(hdmi_id) &&
184 (adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
185 DP_DUAL_MODE_TYPE_HAS_DPCD));
189 * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor
190 * @adapter: I2C adapter for the DDC bus
192 * Attempt to identify the type of the DP dual mode adaptor used.
194 * Note that when the answer is @DRM_DP_DUAL_MODE_UNKNOWN it's not
195 * certain whether we're dealing with a native HDMI port or
196 * a type 1 DVI dual mode adaptor. The driver will have to use
197 * some other hardware/driver specific mechanism to make that
201 * The type of the DP dual mode adaptor used
203 enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter)
205 char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {};
206 uint8_t adaptor_id = 0x00;
210 * Let's see if the adaptor is there the by reading the
213 * Note that type 1 DVI adaptors are not required to implemnt
214 * any registers, and that presents a problem for detection.
215 * If the i2c transfer is nacked, we may or may not be dealing
216 * with a type 1 DVI adaptor. Some other mechanism of detecting
217 * the presence of the adaptor is required. One way would be
218 * to check the state of the CONFIG1 pin, Another method would
219 * simply require the driver to know whether the port is a DP++
220 * port or a native HDMI port. Both of these methods are entirely
221 * hardware/driver specific so we can't deal with them here.
223 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_HDMI_ID,
224 hdmi_id, sizeof(hdmi_id));
225 DRM_DEBUG_KMS("DP dual mode HDMI ID: %*pE (err %zd)\n",
226 ret ? 0 : (int)sizeof(hdmi_id), hdmi_id, ret);
228 return DRM_DP_DUAL_MODE_UNKNOWN;
230 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_ADAPTOR_ID,
231 &adaptor_id, sizeof(adaptor_id));
232 DRM_DEBUG_KMS("DP dual mode adaptor ID: %02x (err %zd)\n",
235 if (is_lspcon_adaptor(hdmi_id, adaptor_id))
236 return DRM_DP_DUAL_MODE_LSPCON;
237 if (is_type2_adaptor(adaptor_id)) {
238 if (is_hdmi_adaptor(hdmi_id))
239 return DRM_DP_DUAL_MODE_TYPE2_HDMI;
241 return DRM_DP_DUAL_MODE_TYPE2_DVI;
244 * If not a proper type 1 ID, still assume type 1, but let
245 * the user know that we may have misdetected the type.
247 if (!is_type1_adaptor(adaptor_id))
248 DRM_ERROR("Unexpected DP dual mode adaptor ID %02x\n",
253 if (is_hdmi_adaptor(hdmi_id))
254 return DRM_DP_DUAL_MODE_TYPE1_HDMI;
256 return DRM_DP_DUAL_MODE_TYPE1_DVI;
258 EXPORT_SYMBOL(drm_dp_dual_mode_detect);
261 * drm_dp_dual_mode_max_tmds_clock - Max TMDS clock for DP dual mode adaptor
262 * @type: DP dual mode adaptor type
263 * @adapter: I2C adapter for the DDC bus
265 * Determine the max TMDS clock the adaptor supports based on the
266 * type of the dual mode adaptor and the DP_DUAL_MODE_MAX_TMDS_CLOCK
267 * register (on type2 adaptors). As some type 1 adaptors have
268 * problems with registers (see comments in drm_dp_dual_mode_detect())
269 * we don't read the register on those, instead we simply assume
270 * a 165 MHz limit based on the specification.
273 * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz.
275 int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,
276 struct i2c_adapter *adapter)
278 uint8_t max_tmds_clock;
281 /* native HDMI so no limit */
282 if (type == DRM_DP_DUAL_MODE_NONE)
286 * Type 1 adaptors are limited to 165MHz
287 * Type 2 adaptors can tells us their limit
289 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
292 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_MAX_TMDS_CLOCK,
293 &max_tmds_clock, sizeof(max_tmds_clock));
294 if (ret || max_tmds_clock == 0x00 || max_tmds_clock == 0xff) {
295 DRM_DEBUG_KMS("Failed to query max TMDS clock\n");
299 return max_tmds_clock * 5000 / 2;
301 EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock);
304 * drm_dp_dual_mode_get_tmds_output - Get the state of the TMDS output buffers in the DP dual mode adaptor
305 * @type: DP dual mode adaptor type
306 * @adapter: I2C adapter for the DDC bus
307 * @enabled: current state of the TMDS output buffers
309 * Get the state of the TMDS output buffers in the adaptor. For
310 * type2 adaptors this is queried from the DP_DUAL_MODE_TMDS_OEN
311 * register. As some type 1 adaptors have problems with registers
312 * (see comments in drm_dp_dual_mode_detect()) we don't read the
313 * register on those, instead we simply assume that the buffers
314 * are always enabled.
317 * 0 on success, negative error code on failure
319 int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,
320 struct i2c_adapter *adapter,
326 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) {
331 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
332 &tmds_oen, sizeof(tmds_oen));
334 DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n");
338 *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE);
342 EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output);
345 * drm_dp_dual_mode_set_tmds_output - Enable/disable TMDS output buffers in the DP dual mode adaptor
346 * @type: DP dual mode adaptor type
347 * @adapter: I2C adapter for the DDC bus
348 * @enable: enable (as opposed to disable) the TMDS output buffers
350 * Set the state of the TMDS output buffers in the adaptor. For
351 * type2 this is set via the DP_DUAL_MODE_TMDS_OEN register.
352 * Type1 adaptors do not support any register writes.
355 * 0 on success, negative error code on failure
357 int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,
358 struct i2c_adapter *adapter, bool enable)
360 uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
364 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
368 * LSPCON adapters in low-power state may ignore the first write, so
369 * read back and verify the written value a few times.
371 for (retry = 0; retry < 3; retry++) {
374 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
375 &tmds_oen, sizeof(tmds_oen));
377 DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
378 enable ? "enable" : "disable",
383 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
386 DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
387 enable ? "enabling" : "disabling",
396 DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
397 enable ? "enabling" : "disabling");
401 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
404 * drm_dp_get_dual_mode_type_name - Get the name of the DP dual mode adaptor type as a string
405 * @type: DP dual mode adaptor type
408 * String representation of the DP dual mode adaptor type
410 const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)
413 case DRM_DP_DUAL_MODE_NONE:
415 case DRM_DP_DUAL_MODE_TYPE1_DVI:
417 case DRM_DP_DUAL_MODE_TYPE1_HDMI:
418 return "type 1 HDMI";
419 case DRM_DP_DUAL_MODE_TYPE2_DVI:
421 case DRM_DP_DUAL_MODE_TYPE2_HDMI:
422 return "type 2 HDMI";
423 case DRM_DP_DUAL_MODE_LSPCON:
426 WARN_ON(type != DRM_DP_DUAL_MODE_UNKNOWN);
430 EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name);
433 * drm_lspcon_get_mode: Get LSPCON's current mode of operation by
434 * reading offset (0x80, 0x41)
435 * @adapter: I2C-over-aux adapter
436 * @mode: current lspcon mode of operation output variable
439 * 0 on success, sets the current_mode value to appropriate mode
442 int drm_lspcon_get_mode(struct i2c_adapter *adapter,
443 enum drm_lspcon_mode *mode)
450 DRM_ERROR("NULL input\n");
454 /* Read Status: i2c over aux */
455 for (retry = 0; retry < 6; retry++) {
457 usleep_range(500, 1000);
459 ret = drm_dp_dual_mode_read(adapter,
460 DP_DUAL_MODE_LSPCON_CURRENT_MODE,
461 &data, sizeof(data));
467 DRM_DEBUG_KMS("LSPCON read(0x80, 0x41) failed\n");
471 if (data & DP_DUAL_MODE_LSPCON_MODE_PCON)
472 *mode = DRM_LSPCON_MODE_PCON;
474 *mode = DRM_LSPCON_MODE_LS;
477 EXPORT_SYMBOL(drm_lspcon_get_mode);
480 * drm_lspcon_set_mode: Change LSPCON's mode of operation by
481 * writing offset (0x80, 0x40)
482 * @adapter: I2C-over-aux adapter
483 * @mode: required mode of operation
486 * 0 on success, -error on failure/timeout
488 int drm_lspcon_set_mode(struct i2c_adapter *adapter,
489 enum drm_lspcon_mode mode)
494 enum drm_lspcon_mode current_mode;
496 if (mode == DRM_LSPCON_MODE_PCON)
497 data = DP_DUAL_MODE_LSPCON_MODE_PCON;
500 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_LSPCON_MODE_CHANGE,
501 &data, sizeof(data));
503 DRM_ERROR("LSPCON mode change failed\n");
508 * Confirm mode change by reading the status bit.
509 * Sometimes, it takes a while to change the mode,
510 * so wait and retry until time out or done.
513 ret = drm_lspcon_get_mode(adapter, ¤t_mode);
515 DRM_ERROR("can't confirm LSPCON mode change\n");
518 if (current_mode != mode) {
522 DRM_DEBUG_KMS("LSPCON mode changed to %s\n",
523 mode == DRM_LSPCON_MODE_LS ?
530 DRM_ERROR("LSPCON mode change timed out\n");
533 EXPORT_SYMBOL(drm_lspcon_set_mode);