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