Merge branch 'for-rmk' of git://git.pengutronix.de/git/imx/linux-2.6
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nv50_sor.c
1 /*
2  * Copyright (C) 2008 Maarten Maathuis.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include "drmP.h"
28 #include "drm_crtc_helper.h"
29
30 #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
31 #include "nouveau_reg.h"
32 #include "nouveau_drv.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_encoder.h"
35 #include "nouveau_connector.h"
36 #include "nouveau_crtc.h"
37 #include "nv50_display.h"
38
39 static void
40 nv50_sor_disconnect(struct nouveau_encoder *nv_encoder)
41 {
42         struct drm_device *dev = to_drm_encoder(nv_encoder)->dev;
43         struct drm_nouveau_private *dev_priv = dev->dev_private;
44         struct nouveau_channel *evo = dev_priv->evo;
45         int ret;
46
47         NV_DEBUG_KMS(dev, "Disconnecting SOR %d\n", nv_encoder->or);
48
49         ret = RING_SPACE(evo, 2);
50         if (ret) {
51                 NV_ERROR(dev, "no space while disconnecting SOR\n");
52                 return;
53         }
54         BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
55         OUT_RING(evo, 0);
56 }
57
58 static void
59 nv50_sor_dp_link_train(struct drm_encoder *encoder)
60 {
61         struct drm_device *dev = encoder->dev;
62         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
63         struct bit_displayport_encoder_table *dpe;
64         int dpe_headerlen;
65
66         dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
67         if (!dpe) {
68                 NV_ERROR(dev, "SOR-%d: no DP encoder table!\n", nv_encoder->or);
69                 return;
70         }
71
72         if (dpe->script0) {
73                 NV_DEBUG_KMS(dev, "SOR-%d: running DP script 0\n", nv_encoder->or);
74                 nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script0),
75                                             nv_encoder->dcb);
76         }
77
78         if (!nouveau_dp_link_train(encoder))
79                 NV_ERROR(dev, "SOR-%d: link training failed\n", nv_encoder->or);
80
81         if (dpe->script1) {
82                 NV_DEBUG_KMS(dev, "SOR-%d: running DP script 1\n", nv_encoder->or);
83                 nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script1),
84                                             nv_encoder->dcb);
85         }
86 }
87
88 static void
89 nv50_sor_dpms(struct drm_encoder *encoder, int mode)
90 {
91         struct drm_device *dev = encoder->dev;
92         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
93         struct drm_encoder *enc;
94         uint32_t val;
95         int or = nv_encoder->or;
96
97         NV_DEBUG_KMS(dev, "or %d mode %d\n", or, mode);
98
99         nv_encoder->last_dpms = mode;
100         list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
101                 struct nouveau_encoder *nvenc = nouveau_encoder(enc);
102
103                 if (nvenc == nv_encoder ||
104                     nvenc->dcb->or != nv_encoder->dcb->or)
105                         continue;
106
107                 if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
108                         return;
109         }
110
111         /* wait for it to be done */
112         if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_CTRL(or),
113                      NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING, 0)) {
114                 NV_ERROR(dev, "timeout: SOR_DPMS_CTRL_PENDING(%d) == 0\n", or);
115                 NV_ERROR(dev, "SOR_DPMS_CTRL(%d) = 0x%08x\n", or,
116                          nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or)));
117         }
118
119         val = nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or));
120
121         if (mode == DRM_MODE_DPMS_ON)
122                 val |= NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
123         else
124                 val &= ~NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
125
126         nv_wr32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or), val |
127                 NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING);
128         if (!nv_wait(NV50_PDISPLAY_SOR_DPMS_STATE(or),
129                      NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
130                 NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", or);
131                 NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", or,
132                          nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(or)));
133         }
134
135         if (nv_encoder->dcb->type == OUTPUT_DP && mode == DRM_MODE_DPMS_ON)
136                 nv50_sor_dp_link_train(encoder);
137 }
138
139 static void
140 nv50_sor_save(struct drm_encoder *encoder)
141 {
142         NV_ERROR(encoder->dev, "!!\n");
143 }
144
145 static void
146 nv50_sor_restore(struct drm_encoder *encoder)
147 {
148         NV_ERROR(encoder->dev, "!!\n");
149 }
150
151 static bool
152 nv50_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
153                     struct drm_display_mode *adjusted_mode)
154 {
155         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
156         struct nouveau_connector *connector;
157
158         NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
159
160         connector = nouveau_encoder_connector_get(nv_encoder);
161         if (!connector) {
162                 NV_ERROR(encoder->dev, "Encoder has no connector\n");
163                 return false;
164         }
165
166         if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
167              connector->native_mode) {
168                 int id = adjusted_mode->base.id;
169                 *adjusted_mode = *connector->native_mode;
170                 adjusted_mode->base.id = id;
171         }
172
173         return true;
174 }
175
176 static void
177 nv50_sor_prepare(struct drm_encoder *encoder)
178 {
179 }
180
181 static void
182 nv50_sor_commit(struct drm_encoder *encoder)
183 {
184 }
185
186 static void
187 nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
188                   struct drm_display_mode *adjusted_mode)
189 {
190         struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
191         struct nouveau_channel *evo = dev_priv->evo;
192         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
193         struct drm_device *dev = encoder->dev;
194         struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
195         uint32_t mode_ctl = 0;
196         int ret;
197
198         NV_DEBUG_KMS(dev, "or %d\n", nv_encoder->or);
199
200         nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
201
202         switch (nv_encoder->dcb->type) {
203         case OUTPUT_TMDS:
204                 if (nv_encoder->dcb->sorconf.link & 1) {
205                         if (adjusted_mode->clock < 165000)
206                                 mode_ctl = 0x0100;
207                         else
208                                 mode_ctl = 0x0500;
209                 } else
210                         mode_ctl = 0x0200;
211                 break;
212         case OUTPUT_DP:
213                 mode_ctl |= 0x00050000;
214                 if (nv_encoder->dcb->sorconf.link & 1)
215                         mode_ctl |= 0x00000800;
216                 else
217                         mode_ctl |= 0x00000900;
218                 break;
219         default:
220                 break;
221         }
222
223         if (crtc->index == 1)
224                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
225         else
226                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
227
228         if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
229                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
230
231         if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
232                 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
233
234         ret = RING_SPACE(evo, 2);
235         if (ret) {
236                 NV_ERROR(dev, "no space while connecting SOR\n");
237                 return;
238         }
239         BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
240         OUT_RING(evo, mode_ctl);
241 }
242
243 static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
244         .dpms = nv50_sor_dpms,
245         .save = nv50_sor_save,
246         .restore = nv50_sor_restore,
247         .mode_fixup = nv50_sor_mode_fixup,
248         .prepare = nv50_sor_prepare,
249         .commit = nv50_sor_commit,
250         .mode_set = nv50_sor_mode_set,
251         .detect = NULL
252 };
253
254 static void
255 nv50_sor_destroy(struct drm_encoder *encoder)
256 {
257         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
258
259         if (!encoder)
260                 return;
261
262         NV_DEBUG_KMS(encoder->dev, "\n");
263
264         drm_encoder_cleanup(encoder);
265
266         kfree(nv_encoder);
267 }
268
269 static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
270         .destroy = nv50_sor_destroy,
271 };
272
273 int
274 nv50_sor_create(struct drm_device *dev, struct dcb_entry *entry)
275 {
276         struct nouveau_encoder *nv_encoder = NULL;
277         struct drm_encoder *encoder;
278         bool dum;
279         int type;
280
281         NV_DEBUG_KMS(dev, "\n");
282
283         switch (entry->type) {
284         case OUTPUT_TMDS:
285                 NV_INFO(dev, "Detected a TMDS output\n");
286                 type = DRM_MODE_ENCODER_TMDS;
287                 break;
288         case OUTPUT_LVDS:
289                 NV_INFO(dev, "Detected a LVDS output\n");
290                 type = DRM_MODE_ENCODER_LVDS;
291
292                 if (nouveau_bios_parse_lvds_table(dev, 0, &dum, &dum)) {
293                         NV_ERROR(dev, "Failed parsing LVDS table\n");
294                         return -EINVAL;
295                 }
296                 break;
297         case OUTPUT_DP:
298                 NV_INFO(dev, "Detected a DP output\n");
299                 type = DRM_MODE_ENCODER_TMDS;
300                 break;
301         default:
302                 return -EINVAL;
303         }
304
305         nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
306         if (!nv_encoder)
307                 return -ENOMEM;
308         encoder = to_drm_encoder(nv_encoder);
309
310         nv_encoder->dcb = entry;
311         nv_encoder->or = ffs(entry->or) - 1;
312
313         nv_encoder->disconnect = nv50_sor_disconnect;
314
315         drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
316         drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
317
318         encoder->possible_crtcs = entry->heads;
319         encoder->possible_clones = 0;
320
321         return 0;
322 }