blob: 50131249d0bf835f4a6bd367504faceca87983f2 [file] [log] [blame]
Shashank Sharmadbe9e612016-10-14 19:56:49 +05301/*
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
Imre Deaka5d94b832016-10-24 19:33:30 +030030static 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
Jani Nikula1dc16aa2016-10-18 14:21:51 +030038static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
Shashank Sharmadbe9e612016-10-14 19:56:49 +053039{
40 enum drm_lspcon_mode current_mode = DRM_LSPCON_MODE_INVALID;
Imre Deaka5d94b832016-10-24 19:33:30 +030041 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Shashank Sharmadbe9e612016-10-14 19:56:49 +053042
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;
Imre Deaka5d94b832016-10-24 19:33:30 +030056 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Shashank Sharmadbe9e612016-10-14 19:56:49 +053057
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
Imre Deakf2b667b2016-11-21 21:15:04 +020080static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
81{
82 uint8_t rev;
83
84 if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
85 &rev) != 1) {
86 DRM_DEBUG_KMS("Native AUX CH down\n");
87 return false;
88 }
89
90 DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
91 rev >> 4, rev & 0xf);
92
93 return true;
94}
95
Shashank Sharmadbe9e612016-10-14 19:56:49 +053096static bool lspcon_probe(struct intel_lspcon *lspcon)
97{
98 enum drm_dp_dual_mode_type adaptor_type;
Imre Deaka5d94b832016-10-24 19:33:30 +030099 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530100
Imre Deakf2b667b2016-11-21 21:15:04 +0200101 lspcon_wake_native_aux_ch(lspcon);
102
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530103 /* Lets probe the adaptor and check its type */
104 adaptor_type = drm_dp_dual_mode_detect(adapter);
105 if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
106 DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
107 drm_dp_get_dual_mode_type_name(adaptor_type));
108 return false;
109 }
110
111 /* Yay ... got a LSPCON device */
112 DRM_DEBUG_KMS("LSPCON detected\n");
113 lspcon->mode = lspcon_get_current_mode(lspcon);
114 lspcon->active = true;
115 return true;
116}
117
Imre Deak489375c2016-10-24 19:33:31 +0300118static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
119{
120 struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
121 unsigned long start = jiffies;
122
123 if (!lspcon->desc_valid)
124 return;
125
126 while (1) {
127 struct intel_dp_desc desc;
128
129 /*
130 * The w/a only applies in PCON mode and we don't expect any
131 * AUX errors.
132 */
133 if (!__intel_dp_read_desc(intel_dp, &desc))
134 return;
135
136 if (!memcmp(&intel_dp->desc, &desc, sizeof(desc))) {
137 DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
138 jiffies_to_msecs(jiffies - start));
139 return;
140 }
141
142 if (time_after(jiffies, start + msecs_to_jiffies(1000)))
143 break;
144
145 usleep_range(10000, 15000);
146 }
147
148 DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
149}
150
Shashank Sharma910530c2016-10-14 19:56:52 +0530151void lspcon_resume(struct intel_lspcon *lspcon)
152{
Imre Deakf2b667b2016-11-21 21:15:04 +0200153 if (lspcon_wake_native_aux_ch(lspcon))
154 lspcon_resume_in_pcon_wa(lspcon);
Imre Deak489375c2016-10-24 19:33:31 +0300155
Shashank Sharma910530c2016-10-14 19:56:52 +0530156 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON, true))
157 DRM_ERROR("LSPCON resume failed\n");
158 else
159 DRM_DEBUG_KMS("LSPCON resume success\n");
160}
161
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530162bool lspcon_init(struct intel_digital_port *intel_dig_port)
163{
164 struct intel_dp *dp = &intel_dig_port->dp;
165 struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
166 struct drm_device *dev = intel_dig_port->base.base.dev;
167 struct drm_i915_private *dev_priv = to_i915(dev);
168
169 if (!IS_GEN9(dev_priv)) {
170 DRM_ERROR("LSPCON is supported on GEN9 only\n");
171 return false;
172 }
173
174 lspcon->active = false;
175 lspcon->mode = DRM_LSPCON_MODE_INVALID;
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530176
177 if (!lspcon_probe(lspcon)) {
178 DRM_ERROR("Failed to probe lspcon\n");
179 return false;
180 }
181
182 /*
183 * In the SW state machine, lets Put LSPCON in PCON mode only.
184 * In this way, it will work with both HDMI 1.4 sinks as well as HDMI
185 * 2.0 sinks.
186 */
187 if (lspcon->active && lspcon->mode != DRM_LSPCON_MODE_PCON) {
188 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON,
189 true) < 0) {
190 DRM_ERROR("LSPCON mode change to PCON failed\n");
191 return false;
192 }
193 }
194
Imre Deak24e807e2016-10-24 19:33:28 +0300195 if (!intel_dp_read_dpcd(dp)) {
196 DRM_ERROR("LSPCON DPCD read failed\n");
197 return false;
198 }
199
Imre Deak489375c2016-10-24 19:33:31 +0300200 lspcon->desc_valid = intel_dp_read_desc(dp);
Imre Deak12a47a422016-10-24 19:33:29 +0300201
Shashank Sharmadbe9e612016-10-14 19:56:49 +0530202 DRM_DEBUG_KMS("Success: LSPCON init\n");
203 return true;
204}