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