kernel/mrsas: Fix a double assignment.
[dragonfly.git] / sys / dev / drm / drm_dp_dual_mode_helper.c
blobc3ac9317991dfd429cb291048e3f62a2e383be89
1 /*
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/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>
30 /**
31 * DOC: dp dual mode helpers
33 * Helper functions to deal with DP dual mode (aka. DP++) adaptors.
35 * Type 1:
36 * Adaptor registers (if any) and the sink DDC bus may be accessed via I2C.
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.
44 #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40
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
53 * Reads @size bytes from the DP dual mode adaptor registers
54 * starting at @offset.
56 * Returns:
57 * 0 on success, negative error code on failure
59 ssize_t drm_dp_dual_mode_read(struct i2c_adapter *adapter,
60 u8 offset, void *buffer, size_t size)
62 struct i2c_msg msgs[] = {
64 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
65 .flags = 0,
66 .len = 1,
67 .buf = &offset,
70 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
71 .flags = I2C_M_RD,
72 .len = size,
73 .buf = buffer,
76 int ret;
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;
84 return 0;
86 EXPORT_SYMBOL(drm_dp_dual_mode_read);
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
95 * Writes @size bytes to the DP dual mode adaptor registers
96 * starting at @offset.
98 * Returns:
99 * 0 on success, negative error code on failure
101 ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter,
102 u8 offset, const void *buffer, size_t size)
104 struct i2c_msg msg = {
105 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
106 .flags = 0,
107 .len = 1 + size,
108 .buf = NULL,
110 void *data;
111 int ret;
113 data = kmalloc(msg.len, M_DRM, M_WAITOK);
114 if (!data)
115 return -ENOMEM;
117 msg.buf = data;
119 memcpy(data, &offset, 1);
120 memcpy((char *)data + 1, buffer, size);
122 ret = i2c_transfer(adapter, &msg, 1);
124 kfree(data);
126 if (ret < 0)
127 return ret;
128 if (ret != 1)
129 return -EPROTO;
131 return 0;
133 EXPORT_SYMBOL(drm_dp_dual_mode_write);
135 static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])
137 static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] =
138 "DP-HDMI ADAPTOR\x04";
140 return memcmp(hdmi_id, dp_dual_mode_hdmi_id,
141 sizeof(dp_dual_mode_hdmi_id)) == 0;
144 static bool is_type2_adaptor(uint8_t adaptor_id)
146 return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
147 DP_DUAL_MODE_REV_TYPE2);
151 * drm_dp_dual_mode_detect - Identify the DP dual mode adaptor
152 * @adapter: I2C adapter for the DDC bus
154 * Attempt to identify the type of the DP dual mode adaptor used.
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.
162 * Returns:
163 * The type of the DP dual mode adaptor used
165 enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter)
167 char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {};
168 uint8_t adaptor_id = 0x00;
169 ssize_t ret;
172 * Let's see if the adaptor is there the by reading the
173 * HDMI ID registers.
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.
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;
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.
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;
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;
218 EXPORT_SYMBOL(drm_dp_dual_mode_detect);
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
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.
232 * Returns:
233 * Maximum supported TMDS clock rate for the DP dual mode adaptor in kHz.
235 int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,
236 struct i2c_adapter *adapter)
238 uint8_t max_tmds_clock;
239 ssize_t ret;
241 /* native HDMI so no limit */
242 if (type == DRM_DP_DUAL_MODE_NONE)
243 return 0;
246 * Type 1 adaptors are limited to 165MHz
247 * Type 2 adaptors can tells us their limit
249 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
250 return 165000;
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;
259 return max_tmds_clock * 5000 / 2;
261 EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock);
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
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.
276 * Returns:
277 * 0 on success, negative error code on failure
279 int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,
280 struct i2c_adapter *adapter,
281 bool *enabled)
283 uint8_t tmds_oen;
284 ssize_t ret;
286 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) {
287 *enabled = true;
288 return 0;
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;
298 *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE);
300 return 0;
302 EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output);
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
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.
316 * Returns:
317 * 0 on success, negative error code on failure
319 int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,
320 struct i2c_adapter *adapter, bool enable)
322 uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
323 ssize_t ret;
325 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
326 return 0;
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;
336 return 0;
338 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
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
344 * Returns:
345 * String representation of the DP dual mode adaptor type
347 const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)
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";
365 EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name);