59be357b17e00b60a7cbc7accba0b9833e7f4e79
[linux-2.6-microblaze.git] / drivers / gpu / drm / nouveau / nouveau_dp.c
1 /*
2  * Copyright 2009 Red Hat Inc.
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  * Authors: Ben Skeggs
23  */
24
25 #include <drm/drm_dp_helper.h>
26
27 #include "nouveau_drv.h"
28 #include "nouveau_connector.h"
29 #include "nouveau_encoder.h"
30 #include "nouveau_crtc.h"
31
32 #include <nvif/class.h>
33 #include <nvif/cl5070.h>
34
35 MODULE_PARM_DESC(mst, "Enable DisplayPort multi-stream (default: enabled)");
36 static int nouveau_mst = 1;
37 module_param_named(mst, nouveau_mst, int, 0400);
38
39 static bool
40 nouveau_dp_has_sink_count(struct drm_connector *connector,
41                           struct nouveau_encoder *outp)
42 {
43         return drm_dp_read_sink_count_cap(connector, outp->dp.dpcd, &outp->dp.desc);
44 }
45
46 static enum drm_connector_status
47 nouveau_dp_probe_dpcd(struct nouveau_connector *nv_connector,
48                       struct nouveau_encoder *outp)
49 {
50         struct drm_connector *connector = &nv_connector->base;
51         struct drm_dp_aux *aux = &nv_connector->aux;
52         struct nv50_mstm *mstm = NULL;
53         enum drm_connector_status status = connector_status_disconnected;
54         int ret;
55         u8 *dpcd = outp->dp.dpcd;
56
57         ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
58         if (ret == DP_RECEIVER_CAP_SIZE && dpcd[DP_DPCD_REV]) {
59                 ret = drm_dp_read_desc(aux, &outp->dp.desc,
60                                        drm_dp_is_branch(dpcd));
61                 if (ret < 0)
62                         goto out;
63         } else {
64                 goto out;
65         }
66
67         if (nouveau_mst) {
68                 mstm = outp->dp.mstm;
69                 if (mstm)
70                         mstm->can_mst = drm_dp_read_mst_cap(aux, dpcd);
71         }
72
73         if (nouveau_dp_has_sink_count(connector, outp)) {
74                 ret = drm_dp_read_sink_count(aux);
75                 if (ret < 0)
76                         goto out;
77
78                 outp->dp.sink_count = ret;
79
80                 /*
81                  * Dongle connected, but no display. Don't bother reading
82                  * downstream port info
83                  */
84                 if (!outp->dp.sink_count)
85                         return connector_status_disconnected;
86         }
87
88         ret = drm_dp_read_downstream_info(aux, dpcd,
89                                           outp->dp.downstream_ports);
90         if (ret < 0)
91                 goto out;
92
93         status = connector_status_connected;
94 out:
95         if (status != connector_status_connected) {
96                 /* Clear any cached info */
97                 outp->dp.sink_count = 0;
98         }
99         return status;
100 }
101
102 int
103 nouveau_dp_detect(struct nouveau_connector *nv_connector,
104                   struct nouveau_encoder *nv_encoder)
105 {
106         struct drm_device *dev = nv_encoder->base.base.dev;
107         struct nouveau_drm *drm = nouveau_drm(dev);
108         struct drm_connector *connector = &nv_connector->base;
109         struct nv50_mstm *mstm = nv_encoder->dp.mstm;
110         enum drm_connector_status status;
111         u8 *dpcd = nv_encoder->dp.dpcd;
112         int ret = NOUVEAU_DP_NONE;
113
114         /* If we've already read the DPCD on an eDP device, we don't need to
115          * reread it as it won't change
116          */
117         if (connector->connector_type == DRM_MODE_CONNECTOR_eDP &&
118             dpcd[DP_DPCD_REV] != 0)
119                 return NOUVEAU_DP_SST;
120
121         mutex_lock(&nv_encoder->dp.hpd_irq_lock);
122         if (mstm) {
123                 /* If we're not ready to handle MST state changes yet, just
124                  * report the last status of the connector. We'll reprobe it
125                  * once we've resumed.
126                  */
127                 if (mstm->suspended) {
128                         if (mstm->is_mst)
129                                 ret = NOUVEAU_DP_MST;
130                         else if (connector->status ==
131                                  connector_status_connected)
132                                 ret = NOUVEAU_DP_SST;
133
134                         goto out;
135                 }
136         }
137
138         status = nouveau_dp_probe_dpcd(nv_connector, nv_encoder);
139         if (status == connector_status_disconnected)
140                 goto out;
141
142         /* If we're in MST mode, we're done here */
143         if (mstm && mstm->can_mst && mstm->is_mst) {
144                 ret = NOUVEAU_DP_MST;
145                 goto out;
146         }
147
148         nv_encoder->dp.link_bw = 27000 * dpcd[DP_MAX_LINK_RATE];
149         nv_encoder->dp.link_nr =
150                 dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
151
152         NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
153                  nv_encoder->dp.link_nr, nv_encoder->dp.link_bw,
154                  dpcd[DP_DPCD_REV]);
155         NV_DEBUG(drm, "encoder: %dx%d\n",
156                  nv_encoder->dcb->dpconf.link_nr,
157                  nv_encoder->dcb->dpconf.link_bw);
158
159         if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
160                 nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
161         if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
162                 nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
163
164         NV_DEBUG(drm, "maximum: %dx%d\n",
165                  nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
166
167         if (mstm && mstm->can_mst) {
168                 ret = nv50_mstm_detect(nv_encoder);
169                 if (ret == 1) {
170                         ret = NOUVEAU_DP_MST;
171                         goto out;
172                 } else if (ret != 0) {
173                         goto out;
174                 }
175         }
176         ret = NOUVEAU_DP_SST;
177
178 out:
179         if (mstm && !mstm->suspended && ret != NOUVEAU_DP_MST)
180                 nv50_mstm_remove(mstm);
181
182         mutex_unlock(&nv_encoder->dp.hpd_irq_lock);
183         return ret;
184 }
185
186 void nouveau_dp_irq(struct nouveau_drm *drm,
187                     struct nouveau_connector *nv_connector)
188 {
189         struct drm_connector *connector = &nv_connector->base;
190         struct nouveau_encoder *outp = find_encoder(connector, DCB_OUTPUT_DP);
191         struct nv50_mstm *mstm;
192         int ret;
193         bool send_hpd = false;
194
195         if (!outp)
196                 return;
197
198         mstm = outp->dp.mstm;
199         NV_DEBUG(drm, "service %s\n", connector->name);
200
201         mutex_lock(&outp->dp.hpd_irq_lock);
202
203         if (mstm && mstm->is_mst) {
204                 if (!nv50_mstm_service(drm, nv_connector, mstm))
205                         send_hpd = true;
206         } else {
207                 drm_dp_cec_irq(&nv_connector->aux);
208
209                 if (nouveau_dp_has_sink_count(connector, outp)) {
210                         ret = drm_dp_read_sink_count(&nv_connector->aux);
211                         if (ret != outp->dp.sink_count)
212                                 send_hpd = true;
213                         if (ret >= 0)
214                                 outp->dp.sink_count = ret;
215                 }
216         }
217
218         mutex_unlock(&outp->dp.hpd_irq_lock);
219
220         if (send_hpd)
221                 nouveau_connector_hpd(connector);
222 }
223
224 /* TODO:
225  * - Use the minimum possible BPC here, once we add support for the max bpc
226  *   property.
227  * - Validate against the DP caps advertised by the GPU (we don't check these
228  *   yet)
229  */
230 enum drm_mode_status
231 nv50_dp_mode_valid(struct drm_connector *connector,
232                    struct nouveau_encoder *outp,
233                    const struct drm_display_mode *mode,
234                    unsigned *out_clock)
235 {
236         const unsigned min_clock = 25000;
237         unsigned max_clock, ds_clock, clock;
238         enum drm_mode_status ret;
239
240         if (mode->flags & DRM_MODE_FLAG_INTERLACE && !outp->caps.dp_interlace)
241                 return MODE_NO_INTERLACE;
242
243         max_clock = outp->dp.link_nr * outp->dp.link_bw;
244         ds_clock = drm_dp_downstream_max_clock(outp->dp.dpcd,
245                                                outp->dp.downstream_ports);
246         if (ds_clock)
247                 max_clock = min(max_clock, ds_clock);
248
249         clock = mode->clock * (connector->display_info.bpc * 3) / 10;
250         ret = nouveau_conn_mode_clock_valid(mode, min_clock, max_clock,
251                                             &clock);
252         if (out_clock)
253                 *out_clock = clock;
254         return ret;
255 }