Merge commit 'v2.6.36' into kbuild/misc
[pandora-kernel.git] / drivers / gpu / drm / nouveau / nv50_dac.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_dac_disconnect(struct drm_encoder *encoder)
41 {
42         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
43         struct drm_device *dev = encoder->dev;
44         struct drm_nouveau_private *dev_priv = dev->dev_private;
45         struct nouveau_channel *evo = dev_priv->evo;
46         int ret;
47
48         if (!nv_encoder->crtc)
49                 return;
50         nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
51
52         NV_DEBUG_KMS(dev, "Disconnecting DAC %d\n", nv_encoder->or);
53
54         ret = RING_SPACE(evo, 4);
55         if (ret) {
56                 NV_ERROR(dev, "no space while disconnecting DAC\n");
57                 return;
58         }
59         BEGIN_RING(evo, 0, NV50_EVO_DAC(nv_encoder->or, MODE_CTRL), 1);
60         OUT_RING  (evo, 0);
61         BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
62         OUT_RING  (evo, 0);
63
64         nv_encoder->crtc = NULL;
65 }
66
67 static enum drm_connector_status
68 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
69 {
70         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
71         struct drm_device *dev = encoder->dev;
72         struct drm_nouveau_private *dev_priv = dev->dev_private;
73         enum drm_connector_status status = connector_status_disconnected;
74         uint32_t dpms_state, load_pattern, load_state;
75         int or = nv_encoder->or;
76
77         nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(or), 0x00000001);
78         dpms_state = nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or));
79
80         nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or),
81                 0x00150000 | NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
82         if (!nv_wait(NV50_PDISPLAY_DAC_DPMS_CTRL(or),
83                      NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING, 0)) {
84                 NV_ERROR(dev, "timeout: DAC_DPMS_CTRL_PENDING(%d) == 0\n", or);
85                 NV_ERROR(dev, "DAC_DPMS_CTRL(%d) = 0x%08x\n", or,
86                           nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)));
87                 return status;
88         }
89
90         /* Use bios provided value if possible. */
91         if (dev_priv->vbios.dactestval) {
92                 load_pattern = dev_priv->vbios.dactestval;
93                 NV_DEBUG_KMS(dev, "Using bios provided load_pattern of %d\n",
94                           load_pattern);
95         } else {
96                 load_pattern = 340;
97                 NV_DEBUG_KMS(dev, "Using default load_pattern of %d\n",
98                          load_pattern);
99         }
100
101         nv_wr32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or),
102                 NV50_PDISPLAY_DAC_LOAD_CTRL_ACTIVE | load_pattern);
103         mdelay(45); /* give it some time to process */
104         load_state = nv_rd32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or));
105
106         nv_wr32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or), 0);
107         nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or), dpms_state |
108                 NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
109
110         if ((load_state & NV50_PDISPLAY_DAC_LOAD_CTRL_PRESENT) ==
111                           NV50_PDISPLAY_DAC_LOAD_CTRL_PRESENT)
112                 status = connector_status_connected;
113
114         if (status == connector_status_connected)
115                 NV_DEBUG_KMS(dev, "Load was detected on output with or %d\n", or);
116         else
117                 NV_DEBUG_KMS(dev, "Load was not detected on output with or %d\n", or);
118
119         return status;
120 }
121
122 static void
123 nv50_dac_dpms(struct drm_encoder *encoder, int mode)
124 {
125         struct drm_device *dev = encoder->dev;
126         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
127         uint32_t val;
128         int or = nv_encoder->or;
129
130         NV_DEBUG_KMS(dev, "or %d mode %d\n", or, mode);
131
132         /* wait for it to be done */
133         if (!nv_wait(NV50_PDISPLAY_DAC_DPMS_CTRL(or),
134                      NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING, 0)) {
135                 NV_ERROR(dev, "timeout: DAC_DPMS_CTRL_PENDING(%d) == 0\n", or);
136                 NV_ERROR(dev, "DAC_DPMS_CTRL(%d) = 0x%08x\n", or,
137                          nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)));
138                 return;
139         }
140
141         val = nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)) & ~0x7F;
142
143         if (mode != DRM_MODE_DPMS_ON)
144                 val |= NV50_PDISPLAY_DAC_DPMS_CTRL_BLANKED;
145
146         switch (mode) {
147         case DRM_MODE_DPMS_STANDBY:
148                 val |= NV50_PDISPLAY_DAC_DPMS_CTRL_HSYNC_OFF;
149                 break;
150         case DRM_MODE_DPMS_SUSPEND:
151                 val |= NV50_PDISPLAY_DAC_DPMS_CTRL_VSYNC_OFF;
152                 break;
153         case DRM_MODE_DPMS_OFF:
154                 val |= NV50_PDISPLAY_DAC_DPMS_CTRL_OFF;
155                 val |= NV50_PDISPLAY_DAC_DPMS_CTRL_HSYNC_OFF;
156                 val |= NV50_PDISPLAY_DAC_DPMS_CTRL_VSYNC_OFF;
157                 break;
158         default:
159                 break;
160         }
161
162         nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or), val |
163                 NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
164 }
165
166 static void
167 nv50_dac_save(struct drm_encoder *encoder)
168 {
169         NV_ERROR(encoder->dev, "!!\n");
170 }
171
172 static void
173 nv50_dac_restore(struct drm_encoder *encoder)
174 {
175         NV_ERROR(encoder->dev, "!!\n");
176 }
177
178 static bool
179 nv50_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
180                     struct drm_display_mode *adjusted_mode)
181 {
182         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
183         struct nouveau_connector *connector;
184
185         NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
186
187         connector = nouveau_encoder_connector_get(nv_encoder);
188         if (!connector) {
189                 NV_ERROR(encoder->dev, "Encoder has no connector\n");
190                 return false;
191         }
192
193         if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
194              connector->native_mode) {
195                 int id = adjusted_mode->base.id;
196                 *adjusted_mode = *connector->native_mode;
197                 adjusted_mode->base.id = id;
198         }
199
200         return true;
201 }
202
203 static void
204 nv50_dac_prepare(struct drm_encoder *encoder)
205 {
206 }
207
208 static void
209 nv50_dac_commit(struct drm_encoder *encoder)
210 {
211 }
212
213 static void
214 nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
215                   struct drm_display_mode *adjusted_mode)
216 {
217         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
218         struct drm_device *dev = encoder->dev;
219         struct drm_nouveau_private *dev_priv = dev->dev_private;
220         struct nouveau_channel *evo = dev_priv->evo;
221         struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
222         uint32_t mode_ctl = 0, mode_ctl2 = 0;
223         int ret;
224
225         NV_DEBUG_KMS(dev, "or %d type %d crtc %d\n",
226                      nv_encoder->or, nv_encoder->dcb->type, crtc->index);
227
228         nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
229
230         if (crtc->index == 1)
231                 mode_ctl |= NV50_EVO_DAC_MODE_CTRL_CRTC1;
232         else
233                 mode_ctl |= NV50_EVO_DAC_MODE_CTRL_CRTC0;
234
235         /* Lacking a working tv-out, this is not a 100% sure. */
236         if (nv_encoder->dcb->type == OUTPUT_ANALOG)
237                 mode_ctl |= 0x40;
238         else
239         if (nv_encoder->dcb->type == OUTPUT_TV)
240                 mode_ctl |= 0x100;
241
242         if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
243                 mode_ctl2 |= NV50_EVO_DAC_MODE_CTRL2_NHSYNC;
244
245         if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
246                 mode_ctl2 |= NV50_EVO_DAC_MODE_CTRL2_NVSYNC;
247
248         ret = RING_SPACE(evo, 3);
249         if (ret) {
250                 NV_ERROR(dev, "no space while connecting DAC\n");
251                 return;
252         }
253         BEGIN_RING(evo, 0, NV50_EVO_DAC(nv_encoder->or, MODE_CTRL), 2);
254         OUT_RING(evo, mode_ctl);
255         OUT_RING(evo, mode_ctl2);
256
257         nv_encoder->crtc = encoder->crtc;
258 }
259
260 static struct drm_crtc *
261 nv50_dac_crtc_get(struct drm_encoder *encoder)
262 {
263         return nouveau_encoder(encoder)->crtc;
264 }
265
266 static const struct drm_encoder_helper_funcs nv50_dac_helper_funcs = {
267         .dpms = nv50_dac_dpms,
268         .save = nv50_dac_save,
269         .restore = nv50_dac_restore,
270         .mode_fixup = nv50_dac_mode_fixup,
271         .prepare = nv50_dac_prepare,
272         .commit = nv50_dac_commit,
273         .mode_set = nv50_dac_mode_set,
274         .get_crtc = nv50_dac_crtc_get,
275         .detect = nv50_dac_detect,
276         .disable = nv50_dac_disconnect
277 };
278
279 static void
280 nv50_dac_destroy(struct drm_encoder *encoder)
281 {
282         struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
283
284         if (!encoder)
285                 return;
286
287         NV_DEBUG_KMS(encoder->dev, "\n");
288
289         drm_encoder_cleanup(encoder);
290         kfree(nv_encoder);
291 }
292
293 static const struct drm_encoder_funcs nv50_dac_encoder_funcs = {
294         .destroy = nv50_dac_destroy,
295 };
296
297 int
298 nv50_dac_create(struct drm_connector *connector, struct dcb_entry *entry)
299 {
300         struct nouveau_encoder *nv_encoder;
301         struct drm_encoder *encoder;
302
303         nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
304         if (!nv_encoder)
305                 return -ENOMEM;
306         encoder = to_drm_encoder(nv_encoder);
307
308         nv_encoder->dcb = entry;
309         nv_encoder->or = ffs(entry->or) - 1;
310
311         drm_encoder_init(connector->dev, encoder, &nv50_dac_encoder_funcs,
312                          DRM_MODE_ENCODER_DAC);
313         drm_encoder_helper_add(encoder, &nv50_dac_helper_funcs);
314
315         encoder->possible_crtcs = entry->heads;
316         encoder->possible_clones = 0;
317
318         drm_mode_connector_attach_encoder(connector, encoder);
319         return 0;
320 }
321