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 <drm/drmP.h> 27 #include <linux/string.h> 28 #include <drm/drm_dp_dual_mode_helper.h> 29 30 /** 31 * DOC: dp dual mode helpers 32 * 33 * Helper functions to deal with DP dual mode (aka. DP++) adaptors. 34 * 35 * Type 1: 36 * Adaptor registers (if any) and the sink DDC bus may be accessed via I2C. 37 * 38 * Type 2: 39 * Adaptor registers and sink DDC bus can be accessed either via I2C or 40 * I2C-over-AUX. Source devices may choose to implement either of these 41 * access methods. 42 */ 43 44 #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40 45 46 /** 47 * drm_dp_dual_mode_read - Read from the DP dual mode adaptor register(s) 48 * @adapter: I2C adapter for the DDC bus 49 * @offset: register offset 50 * @buffer: buffer for return data 51 * @size: sizo of the buffer 52 * 53 * Reads @size bytes from the DP dual mode adaptor registers 54 * starting at @offset. 55 * 56 * Returns: 57 * 0 on success, negative error code on failure 58 */ 59 ssize_t drm_dp_dual_mode_read(struct i2c_adapter *adapter, 60 u8 offset, void *buffer, size_t size) 61 { 62 struct i2c_msg msgs[] = { 63 { 64 .addr = DP_DUAL_MODE_SLAVE_ADDRESS, 65 .flags = 0, 66 .len = 1, 67 .buf = &offset, 68 }, 69 { 70 .addr = DP_DUAL_MODE_SLAVE_ADDRESS, 71 .flags = I2C_M_RD, 72 .len = size, 73 .buf = buffer, 74 }, 75 }; 76 int ret; 77 78 ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs)); 79 if (ret < 0) 80 return ret; 81 if (ret != ARRAY_SIZE(msgs)) 82 return -EPROTO; 83 84 return 0; 85 } 86 EXPORT_SYMBOL(drm_dp_dual_mode_read); 87 88 /** 89 * drm_dp_dual_mode_write - Write to the DP dual mode adaptor register(s) 90 * @adapter: I2C adapter for the DDC bus 91 * @offset: register offset 92 * @buffer: buffer for write data 93 * @size: sizo of the buffer 94 * 95 * Writes @size bytes to the DP dual mode adaptor registers 96 * starting at @offset. 97 * 98 * Returns: 99 * 0 on success, negative error code on failure 100 */ 101 ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter, 102 u8 offset, const void *buffer, size_t size) 103 { 104 struct i2c_msg msg = { 105 .addr = DP_DUAL_MODE_SLAVE_ADDRESS, 106 .flags = 0, 107 .len = 1 + size, 108 .buf = NULL, 109 }; 110 void *data; 111 int ret; 112 113 data = kmalloc(msg.len, M_DRM, M_WAITOK); 114 if (!data) 115 return -ENOMEM; 116 117 msg.buf = data; 118 119 memcpy(data, &offset, 1); 120 memcpy((char *)data + 1, buffer, size); 121 122 ret = i2c_transfer(adapter, &msg, 1); 123 124 kfree(data); 125 126 if (ret < 0) 127 return ret; 128 if (ret != 1) 129 return -EPROTO; 130 131 return 0; 132 } 133 EXPORT_SYMBOL(drm_dp_dual_mode_write); 134 135 static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN]) 136 { 137 static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = 138 "DP-HDMI ADAPTOR\x04"; 139 140 return memcmp(hdmi_id, dp_dual_mode_hdmi_id, 141 sizeof(dp_dual_mode_hdmi_id)) == 0; 142 } 143 144 static bool is_type2_adaptor(uint8_t adaptor_id) 145 { 146 return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 | 147 DP_DUAL_MODE_REV_TYPE2); 148 } 149 150 /** 151 * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor 152 * @adapter: I2C adapter for the DDC bus 153 * 154 * Attempt to identify the type of the DP dual mode adaptor used. 155 * 156 * Note that when the answer is @DRM_DP_DUAL_MODE_UNKNOWN it's not 157 * certain whether we're dealing with a native HDMI port or 158 * a type 1 DVI dual mode adaptor. The driver will have to use 159 * some other hardware/driver specific mechanism to make that 160 * distinction. 161 * 162 * Returns: 163 * The type of the DP dual mode adaptor used 164 */ 165 enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter) 166 { 167 char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {}; 168 uint8_t adaptor_id = 0x00; 169 ssize_t ret; 170 171 /* 172 * Let's see if the adaptor is there the by reading the 173 * HDMI ID registers. 174 * 175 * Note that type 1 DVI adaptors are not required to implemnt 176 * any registers, and that presents a problem for detection. 177 * If the i2c transfer is nacked, we may or may not be dealing 178 * with a type 1 DVI adaptor. Some other mechanism of detecting 179 * the presence of the adaptor is required. One way would be 180 * to check the state of the CONFIG1 pin, Another method would 181 * simply require the driver to know whether the port is a DP++ 182 * port or a native HDMI port. Both of these methods are entirely 183 * hardware/driver specific so we can't deal with them here. 184 */ 185 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_HDMI_ID, 186 hdmi_id, sizeof(hdmi_id)); 187 if (ret) 188 return DRM_DP_DUAL_MODE_UNKNOWN; 189 190 /* 191 * Sigh. Some (maybe all?) type 1 adaptors are broken and ack 192 * the offset but ignore it, and instead they just always return 193 * data from the start of the HDMI ID buffer. So for a broken 194 * type 1 HDMI adaptor a single byte read will always give us 195 * 0x44, and for a type 1 DVI adaptor it should give 0x00 196 * (assuming it implements any registers). Fortunately neither 197 * of those values will match the type 2 signature of the 198 * DP_DUAL_MODE_ADAPTOR_ID register so we can proceed with 199 * the type 2 adaptor detection safely even in the presence 200 * of broken type 1 adaptors. 201 */ 202 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_ADAPTOR_ID, 203 &adaptor_id, sizeof(adaptor_id)); 204 if (ret == 0) { 205 if (is_type2_adaptor(adaptor_id)) { 206 if (is_hdmi_adaptor(hdmi_id)) 207 return DRM_DP_DUAL_MODE_TYPE2_HDMI; 208 else 209 return DRM_DP_DUAL_MODE_TYPE2_DVI; 210 } 211 } 212 213 if (is_hdmi_adaptor(hdmi_id)) 214 return DRM_DP_DUAL_MODE_TYPE1_HDMI; 215 else 216 return DRM_DP_DUAL_MODE_TYPE1_DVI; 217 } 218 EXPORT_SYMBOL(drm_dp_dual_mode_detect); 219 220 /** 221 * drm_dp_dual_mode_max_tmds_clock - Max TMDS clock for DP dual mode adaptor 222 * @type: DP dual mode adaptor type 223 * @adapter: I2C adapter for the DDC bus 224 * 225 * Determine the max TMDS clock the adaptor supports based on the 226 * type of the dual mode adaptor and the DP_DUAL_MODE_MAX_TMDS_CLOCK 227 * register (on type2 adaptors). As some type 1 adaptors have 228 * problems with registers (see comments in drm_dp_dual_mode_detect()) 229 * we don't read the register on those, instead we simply assume 230 * a 165 MHz limit based on the specification. 231 * 232 * Returns: 233 * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz. 234 */ 235 int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type, 236 struct i2c_adapter *adapter) 237 { 238 uint8_t max_tmds_clock; 239 ssize_t ret; 240 241 /* native HDMI so no limit */ 242 if (type == DRM_DP_DUAL_MODE_NONE) 243 return 0; 244 245 /* 246 * Type 1 adaptors are limited to 165MHz 247 * Type 2 adaptors can tells us their limit 248 */ 249 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) 250 return 165000; 251 252 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_MAX_TMDS_CLOCK, 253 &max_tmds_clock, sizeof(max_tmds_clock)); 254 if (ret || max_tmds_clock == 0x00 || max_tmds_clock == 0xff) { 255 DRM_DEBUG_KMS("Failed to query max TMDS clock\n"); 256 return 165000; 257 } 258 259 return max_tmds_clock * 5000 / 2; 260 } 261 EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock); 262 263 /** 264 * drm_dp_dual_mode_get_tmds_output - Get the state of the TMDS output buffers in the DP dual mode adaptor 265 * @type: DP dual mode adaptor type 266 * @adapter: I2C adapter for the DDC bus 267 * @enabled: current state of the TMDS output buffers 268 * 269 * Get the state of the TMDS output buffers in the adaptor. For 270 * type2 adaptors this is queried from the DP_DUAL_MODE_TMDS_OEN 271 * register. As some type 1 adaptors have problems with registers 272 * (see comments in drm_dp_dual_mode_detect()) we don't read the 273 * register on those, instead we simply assume that the buffers 274 * are always enabled. 275 * 276 * Returns: 277 * 0 on success, negative error code on failure 278 */ 279 int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type, 280 struct i2c_adapter *adapter, 281 bool *enabled) 282 { 283 uint8_t tmds_oen; 284 ssize_t ret; 285 286 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) { 287 *enabled = true; 288 return 0; 289 } 290 291 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN, 292 &tmds_oen, sizeof(tmds_oen)); 293 if (ret) { 294 DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n"); 295 return ret; 296 } 297 298 *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE); 299 300 return 0; 301 } 302 EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output); 303 304 /** 305 * drm_dp_dual_mode_set_tmds_output - Enable/disable TMDS output buffers in the DP dual mode adaptor 306 * @type: DP dual mode adaptor type 307 * @adapter: I2C adapter for the DDC bus 308 * @enable: enable (as opposed to disable) the TMDS output buffers 309 * 310 * Set the state of the TMDS output buffers in the adaptor. For 311 * type2 this is set via the DP_DUAL_MODE_TMDS_OEN register. As 312 * some type 1 adaptors have problems with registers (see comments 313 * in drm_dp_dual_mode_detect()) we avoid touching the register, 314 * making this function a no-op on type 1 adaptors. 315 * 316 * Returns: 317 * 0 on success, negative error code on failure 318 */ 319 int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type, 320 struct i2c_adapter *adapter, bool enable) 321 { 322 uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE; 323 ssize_t ret; 324 325 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) 326 return 0; 327 328 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN, 329 &tmds_oen, sizeof(tmds_oen)); 330 if (ret) { 331 DRM_DEBUG_KMS("Failed to %s TMDS output buffers\n", 332 enable ? "enable" : "disable"); 333 return ret; 334 } 335 336 return 0; 337 } 338 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output); 339 340 /** 341 * drm_dp_get_dual_mode_type_name - Get the name of the DP dual mode adaptor type as a string 342 * @type: DP dual mode adaptor type 343 * 344 * Returns: 345 * String representation of the DP dual mode adaptor type 346 */ 347 const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type) 348 { 349 switch (type) { 350 case DRM_DP_DUAL_MODE_NONE: 351 return "none"; 352 case DRM_DP_DUAL_MODE_TYPE1_DVI: 353 return "type 1 DVI"; 354 case DRM_DP_DUAL_MODE_TYPE1_HDMI: 355 return "type 1 HDMI"; 356 case DRM_DP_DUAL_MODE_TYPE2_DVI: 357 return "type 2 DVI"; 358 case DRM_DP_DUAL_MODE_TYPE2_HDMI: 359 return "type 2 HDMI"; 360 default: 361 WARN_ON(type != DRM_DP_DUAL_MODE_UNKNOWN); 362 return "unknown"; 363 } 364 } 365 EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name); 366