Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  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 (including the next
 12 * paragraph) shall be included in all copies or substantial portions of the
 13 * Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 21 * DEALINGS IN THE SOFTWARE.
 22 *
 23 *
 24 */
 25#include <drm/drm_edid.h>
 26#include <drm/drm_atomic_helper.h>
 27#include <drm/drm_dp_dual_mode_helper.h>
 28#include "intel_drv.h"
 29
 30static struct intel_dp *lspcon_to_intel_dp(struct intel_lspcon *lspcon)
 31{
 32	struct intel_digital_port *dig_port =
 33		container_of(lspcon, struct intel_digital_port, lspcon);
 34
 35	return &dig_port->dp;
 36}
 37
 38static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
 39{
 40	enum drm_lspcon_mode current_mode = DRM_LSPCON_MODE_INVALID;
 41	struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
 42
 43	if (drm_lspcon_get_mode(adapter, &current_mode))
 44		DRM_ERROR("Error reading LSPCON mode\n");
 45	else
 46		DRM_DEBUG_KMS("Current LSPCON mode %s\n",
 47			current_mode == DRM_LSPCON_MODE_PCON ? "PCON" : "LS");
 48	return current_mode;
 49}
 50
 51static int lspcon_change_mode(struct intel_lspcon *lspcon,
 52	enum drm_lspcon_mode mode, bool force)
 53{
 54	int err;
 55	enum drm_lspcon_mode current_mode;
 56	struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
 57
 58	err = drm_lspcon_get_mode(adapter, &current_mode);
 59	if (err) {
 60		DRM_ERROR("Error reading LSPCON mode\n");
 61		return err;
 62	}
 63
 64	if (current_mode == mode) {
 65		DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
 66		return 0;
 67	}
 68
 69	err = drm_lspcon_set_mode(adapter, mode);
 70	if (err < 0) {
 71		DRM_ERROR("LSPCON mode change failed\n");
 72		return err;
 73	}
 74
 75	lspcon->mode = mode;
 76	DRM_DEBUG_KMS("LSPCON mode changed done\n");
 77	return 0;
 78}
 79
 80static bool lspcon_probe(struct intel_lspcon *lspcon)
 81{
 82	enum drm_dp_dual_mode_type adaptor_type;
 83	struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
 84
 85	/* Lets probe the adaptor and check its type */
 86	adaptor_type = drm_dp_dual_mode_detect(adapter);
 87	if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
 88		DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
 89			drm_dp_get_dual_mode_type_name(adaptor_type));
 90		return false;
 91	}
 92
 93	/* Yay ... got a LSPCON device */
 94	DRM_DEBUG_KMS("LSPCON detected\n");
 95	lspcon->mode = lspcon_get_current_mode(lspcon);
 96	lspcon->active = true;
 97	return true;
 98}
 99
100static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
101{
102	struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
103	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
104	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
105	unsigned long start = jiffies;
106
107	if (!lspcon->desc_valid)
108		return;
109
110	while (1) {
111		struct intel_dp_desc desc;
112
113		/*
114		 * The w/a only applies in PCON mode and we don't expect any
115		 * AUX errors.
116		 */
117		if (!__intel_dp_read_desc(intel_dp, &desc))
118			return;
119
120		if (intel_digital_port_connected(dev_priv, dig_port) &&
121		    !memcmp(&intel_dp->desc, &desc, sizeof(desc))) {
122			DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
123				      jiffies_to_msecs(jiffies - start));
124			return;
125		}
126
127		if (time_after(jiffies, start + msecs_to_jiffies(1000)))
128			break;
129
130		usleep_range(10000, 15000);
131	}
132
133	DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
134}
135
136void lspcon_resume(struct intel_lspcon *lspcon)
137{
138	lspcon_resume_in_pcon_wa(lspcon);
139
140	if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON, true))
141		DRM_ERROR("LSPCON resume failed\n");
142	else
143		DRM_DEBUG_KMS("LSPCON resume success\n");
144}
145
146bool lspcon_init(struct intel_digital_port *intel_dig_port)
147{
148	struct intel_dp *dp = &intel_dig_port->dp;
149	struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
150	struct drm_device *dev = intel_dig_port->base.base.dev;
151	struct drm_i915_private *dev_priv = to_i915(dev);
152
153	if (!IS_GEN9(dev_priv)) {
154		DRM_ERROR("LSPCON is supported on GEN9 only\n");
155		return false;
156	}
157
158	lspcon->active = false;
159	lspcon->mode = DRM_LSPCON_MODE_INVALID;
160
161	if (!lspcon_probe(lspcon)) {
162		DRM_ERROR("Failed to probe lspcon\n");
163		return false;
164	}
165
166	/*
167	* In the SW state machine, lets Put LSPCON in PCON mode only.
168	* In this way, it will work with both HDMI 1.4 sinks as well as HDMI
169	* 2.0 sinks.
170	*/
171	if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
172		if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON,
173			true) < 0) {
174			DRM_ERROR("LSPCON mode change to PCON failed\n");
175			return false;
176		}
177	}
178
179	if (!intel_dp_read_dpcd(dp)) {
180		DRM_ERROR("LSPCON DPCD read failed\n");
181		return false;
182	}
183
184	lspcon->desc_valid = intel_dp_read_desc(dp);
185
186	DRM_DEBUG_KMS("Success: LSPCON init\n");
187	return true;
188}