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/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>
29
30 #include <drm/drm_dp_dual_mode_helper.h>
31 #include <drm/drm_print.h>
32
33 /**
34 * DOC: dp dual mode helpers
35 *
36 * Helper functions to deal with DP dual mode (aka. DP++) adaptors.
37 *
38 * Type 1:
39 * Adaptor registers (if any) and the sink DDC bus may be accessed via I2C.
40 *
41 * Type 2:
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
44 * access methods.
45 */
46
47 #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40
48
49 /**
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
55 *
56 * Reads @size bytes from the DP dual mode adaptor registers
57 * starting at @offset.
58 *
59 * Returns:
60 * 0 on success, negative error code on failure
61 */
drm_dp_dual_mode_read(struct i2c_adapter * adapter,u8 offset,void * buffer,size_t size)62 ssize_t drm_dp_dual_mode_read(struct i2c_adapter *adapter,
63 u8 offset, void *buffer, size_t size)
64 {
65 u8 zero = 0;
66 char *tmpbuf = NULL;
67 /*
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.
73 */
74 struct i2c_msg msgs[] = {
75 {
76 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
77 .flags = 0,
78 .len = 1,
79 .buf = &zero,
80 },
81 {
82 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
83 .flags = I2C_M_RD,
84 .len = size + offset,
85 .buf = buffer,
86 },
87 };
88 int ret;
89
90 if (offset) {
91 tmpbuf = kmalloc(size + offset, GFP_KERNEL);
92 if (!tmpbuf)
93 return -ENOMEM;
94
95 msgs[1].buf = tmpbuf;
96 }
97
98 ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
99 if (tmpbuf)
100 memcpy(buffer, tmpbuf + offset, size);
101
102 kfree(tmpbuf);
103
104 if (ret < 0)
105 return ret;
106 if (ret != ARRAY_SIZE(msgs))
107 return -EPROTO;
108
109 return 0;
110 }
111 EXPORT_SYMBOL(drm_dp_dual_mode_read);
112
113 /**
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
119 *
120 * Writes @size bytes to the DP dual mode adaptor registers
121 * starting at @offset.
122 *
123 * Returns:
124 * 0 on success, negative error code on failure
125 */
drm_dp_dual_mode_write(struct i2c_adapter * adapter,u8 offset,const void * buffer,size_t size)126 ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter,
127 u8 offset, const void *buffer, size_t size)
128 {
129 struct i2c_msg msg = {
130 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
131 .flags = 0,
132 .len = 1 + size,
133 .buf = NULL,
134 };
135 void *data;
136 int ret;
137
138 data = kmalloc(msg.len, GFP_KERNEL);
139 if (!data)
140 return -ENOMEM;
141
142 msg.buf = data;
143
144 memcpy(data, &offset, 1);
145 memcpy(data + 1, buffer, size);
146
147 ret = i2c_transfer(adapter, &msg, 1);
148
149 kfree(data);
150
151 if (ret < 0)
152 return ret;
153 if (ret != 1)
154 return -EPROTO;
155
156 return 0;
157 }
158 EXPORT_SYMBOL(drm_dp_dual_mode_write);
159
is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])160 static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])
161 {
162 static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] =
163 "DP-HDMI ADAPTOR\x04";
164
165 return memcmp(hdmi_id, dp_dual_mode_hdmi_id,
166 sizeof(dp_dual_mode_hdmi_id)) == 0;
167 }
168
is_type1_adaptor(uint8_t adaptor_id)169 static bool is_type1_adaptor(uint8_t adaptor_id)
170 {
171 return adaptor_id == 0 || adaptor_id == 0xff;
172 }
173
is_type2_adaptor(uint8_t adaptor_id)174 static bool is_type2_adaptor(uint8_t adaptor_id)
175 {
176 return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
177 DP_DUAL_MODE_REV_TYPE2);
178 }
179
is_lspcon_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN],const uint8_t adaptor_id)180 static bool is_lspcon_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN],
181 const uint8_t adaptor_id)
182 {
183 return is_hdmi_adaptor(hdmi_id) &&
184 (adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
185 DP_DUAL_MODE_TYPE_HAS_DPCD));
186 }
187
188 /**
189 * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor
190 * @adapter: I2C adapter for the DDC bus
191 *
192 * Attempt to identify the type of the DP dual mode adaptor used.
193 *
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
198 * distinction.
199 *
200 * Returns:
201 * The type of the DP dual mode adaptor used
202 */
drm_dp_dual_mode_detect(struct i2c_adapter * adapter)203 enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter)
204 {
205 char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {};
206 uint8_t adaptor_id = 0x00;
207 ssize_t ret;
208
209 /*
210 * Let's see if the adaptor is there the by reading the
211 * HDMI ID registers.
212 *
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.
222 */
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);
227 if (ret)
228 return DRM_DP_DUAL_MODE_UNKNOWN;
229
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",
233 adaptor_id, ret);
234 if (ret == 0) {
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;
240 else
241 return DRM_DP_DUAL_MODE_TYPE2_DVI;
242 }
243 /*
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.
246 */
247 if (!is_type1_adaptor(adaptor_id))
248 DRM_ERROR("Unexpected DP dual mode adaptor ID %02x\n",
249 adaptor_id);
250
251 }
252
253 if (is_hdmi_adaptor(hdmi_id))
254 return DRM_DP_DUAL_MODE_TYPE1_HDMI;
255 else
256 return DRM_DP_DUAL_MODE_TYPE1_DVI;
257 }
258 EXPORT_SYMBOL(drm_dp_dual_mode_detect);
259
260 /**
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
264 *
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.
271 *
272 * Returns:
273 * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz.
274 */
drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,struct i2c_adapter * adapter)275 int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,
276 struct i2c_adapter *adapter)
277 {
278 uint8_t max_tmds_clock;
279 ssize_t ret;
280
281 /* native HDMI so no limit */
282 if (type == DRM_DP_DUAL_MODE_NONE)
283 return 0;
284
285 /*
286 * Type 1 adaptors are limited to 165MHz
287 * Type 2 adaptors can tells us their limit
288 */
289 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
290 return 165000;
291
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");
296 return 165000;
297 }
298
299 return max_tmds_clock * 5000 / 2;
300 }
301 EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock);
302
303 /**
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
308 *
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.
315 *
316 * Returns:
317 * 0 on success, negative error code on failure
318 */
drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,struct i2c_adapter * adapter,bool * enabled)319 int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,
320 struct i2c_adapter *adapter,
321 bool *enabled)
322 {
323 uint8_t tmds_oen;
324 ssize_t ret;
325
326 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) {
327 *enabled = true;
328 return 0;
329 }
330
331 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
332 &tmds_oen, sizeof(tmds_oen));
333 if (ret) {
334 DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n");
335 return ret;
336 }
337
338 *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE);
339
340 return 0;
341 }
342 EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output);
343
344 /**
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
349 *
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.
353 *
354 * Returns:
355 * 0 on success, negative error code on failure
356 */
drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,struct i2c_adapter * adapter,bool enable)357 int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,
358 struct i2c_adapter *adapter, bool enable)
359 {
360 uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
361 ssize_t ret;
362 int retry;
363
364 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
365 return 0;
366
367 /*
368 * LSPCON adapters in low-power state may ignore the first write, so
369 * read back and verify the written value a few times.
370 */
371 for (retry = 0; retry < 3; retry++) {
372 uint8_t tmp;
373
374 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
375 &tmds_oen, sizeof(tmds_oen));
376 if (ret) {
377 DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
378 enable ? "enable" : "disable",
379 retry + 1);
380 return ret;
381 }
382
383 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
384 &tmp, sizeof(tmp));
385 if (ret) {
386 DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
387 enable ? "enabling" : "disabling",
388 retry + 1);
389 return ret;
390 }
391
392 if (tmp == tmds_oen)
393 return 0;
394 }
395
396 DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
397 enable ? "enabling" : "disabling");
398
399 return -EIO;
400 }
401 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
402
403 /**
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
406 *
407 * Returns:
408 * String representation of the DP dual mode adaptor type
409 */
drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)410 const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)
411 {
412 switch (type) {
413 case DRM_DP_DUAL_MODE_NONE:
414 return "none";
415 case DRM_DP_DUAL_MODE_TYPE1_DVI:
416 return "type 1 DVI";
417 case DRM_DP_DUAL_MODE_TYPE1_HDMI:
418 return "type 1 HDMI";
419 case DRM_DP_DUAL_MODE_TYPE2_DVI:
420 return "type 2 DVI";
421 case DRM_DP_DUAL_MODE_TYPE2_HDMI:
422 return "type 2 HDMI";
423 case DRM_DP_DUAL_MODE_LSPCON:
424 return "lspcon";
425 default:
426 WARN_ON(type != DRM_DP_DUAL_MODE_UNKNOWN);
427 return "unknown";
428 }
429 }
430 EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name);
431
432 /**
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
437 *
438 * Returns:
439 * 0 on success, sets the current_mode value to appropriate mode
440 * -error on failure
441 */
drm_lspcon_get_mode(struct i2c_adapter * adapter,enum drm_lspcon_mode * mode)442 int drm_lspcon_get_mode(struct i2c_adapter *adapter,
443 enum drm_lspcon_mode *mode)
444 {
445 u8 data;
446 int ret = 0;
447 int retry;
448
449 if (!mode) {
450 DRM_ERROR("NULL input\n");
451 return -EINVAL;
452 }
453
454 /* Read Status: i2c over aux */
455 for (retry = 0; retry < 6; retry++) {
456 if (retry)
457 usleep_range(500, 1000);
458
459 ret = drm_dp_dual_mode_read(adapter,
460 DP_DUAL_MODE_LSPCON_CURRENT_MODE,
461 &data, sizeof(data));
462 if (!ret)
463 break;
464 }
465
466 if (ret < 0) {
467 DRM_DEBUG_KMS("LSPCON read(0x80, 0x41) failed\n");
468 return -EFAULT;
469 }
470
471 if (data & DP_DUAL_MODE_LSPCON_MODE_PCON)
472 *mode = DRM_LSPCON_MODE_PCON;
473 else
474 *mode = DRM_LSPCON_MODE_LS;
475 return 0;
476 }
477 EXPORT_SYMBOL(drm_lspcon_get_mode);
478
479 /**
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
484 *
485 * Returns:
486 * 0 on success, -error on failure/timeout
487 */
drm_lspcon_set_mode(struct i2c_adapter * adapter,enum drm_lspcon_mode mode)488 int drm_lspcon_set_mode(struct i2c_adapter *adapter,
489 enum drm_lspcon_mode mode)
490 {
491 u8 data = 0;
492 int ret;
493 int time_out = 200;
494 enum drm_lspcon_mode current_mode;
495
496 if (mode == DRM_LSPCON_MODE_PCON)
497 data = DP_DUAL_MODE_LSPCON_MODE_PCON;
498
499 /* Change mode */
500 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_LSPCON_MODE_CHANGE,
501 &data, sizeof(data));
502 if (ret < 0) {
503 DRM_ERROR("LSPCON mode change failed\n");
504 return ret;
505 }
506
507 /*
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.
511 */
512 do {
513 ret = drm_lspcon_get_mode(adapter, ¤t_mode);
514 if (ret) {
515 DRM_ERROR("can't confirm LSPCON mode change\n");
516 return ret;
517 } else {
518 if (current_mode != mode) {
519 msleep(10);
520 time_out -= 10;
521 } else {
522 DRM_DEBUG_KMS("LSPCON mode changed to %s\n",
523 mode == DRM_LSPCON_MODE_LS ?
524 "LS" : "PCON");
525 return 0;
526 }
527 }
528 } while (time_out);
529
530 DRM_ERROR("LSPCON mode change timed out\n");
531 return -ETIMEDOUT;
532 }
533 EXPORT_SYMBOL(drm_lspcon_set_mode);
534