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