NV50: basic fbcon + misc fixes
[platform/upstream/libdrm.git] / linux-core / 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 "nv50_output.h"
28
29 static int nv50_dac_validate_mode(struct nv50_output *output, struct nouveau_hw_mode *mode)
30 {
31         NV50_DEBUG("\n");
32
33         if (mode->clock > 400000) 
34                 return MODE_CLOCK_HIGH;
35
36         if (mode->clock < 25000)
37                 return MODE_CLOCK_LOW;
38
39         return MODE_OK;
40 }
41
42 static int nv50_dac_execute_mode(struct nv50_output *output, bool disconnect)
43 {
44         struct drm_nouveau_private *dev_priv = output->dev->dev_private;
45         struct nv50_crtc *crtc = output->crtc;
46         struct nouveau_hw_mode *desired_mode = NULL;
47
48         uint32_t offset = nv50_output_or_offset(output) * 0x80;
49
50         uint32_t mode_ctl = NV50_DAC_MODE_CTRL_OFF;
51         uint32_t mode_ctl2 = 0;
52
53         NV50_DEBUG("or %d\n", nv50_output_or_offset(output));
54
55         if (disconnect) {
56                 NV50_DEBUG("Disconnecting DAC\n");
57                 OUT_MODE(NV50_DAC0_MODE_CTRL + offset, mode_ctl);
58                 return 0;
59         }
60
61         desired_mode = (crtc->use_native_mode ? crtc->native_mode :
62                                                                         crtc->mode);
63
64         if (crtc->index == 1)
65                 mode_ctl |= NV50_DAC_MODE_CTRL_CRTC1;
66         else
67                 mode_ctl |= NV50_DAC_MODE_CTRL_CRTC0;
68
69         /* Lacking a working tv-out, this is not a 100% sure. */
70         if (output->type == OUTPUT_DAC) {
71                 mode_ctl |= 0x40;
72         } else if (output->type == OUTPUT_TV) {
73                 mode_ctl |= 0x100;
74         }
75
76         if (desired_mode->flags & V_NHSYNC)
77                 mode_ctl2 |= NV50_DAC_MODE_CTRL2_NHSYNC;
78
79         if (desired_mode->flags & V_NVSYNC)
80                 mode_ctl2 |= NV50_DAC_MODE_CTRL2_NVSYNC;
81
82         OUT_MODE(NV50_DAC0_MODE_CTRL + offset, mode_ctl);
83         OUT_MODE(NV50_DAC0_MODE_CTRL2 + offset, mode_ctl2);
84
85         return 0;
86 }
87
88 static int nv50_dac_set_clock_mode(struct nv50_output *output)
89 {
90         struct drm_nouveau_private *dev_priv = output->dev->dev_private;
91
92         NV50_DEBUG("or %d\n", nv50_output_or_offset(output));
93
94         NV_WRITE(NV50_PDISPLAY_DAC_CLK_CLK_CTRL2(nv50_output_or_offset(output)),  0);
95
96         return 0;
97 }
98
99 static int nv50_dac_set_power_mode(struct nv50_output *output, int mode)
100 {
101         struct drm_nouveau_private *dev_priv = output->dev->dev_private;
102         uint32_t val;
103         int or = nv50_output_or_offset(output);
104
105         NV50_DEBUG("or %d\n", or);
106
107         /* wait for it to be done */
108         while (NV_READ(NV50_PDISPLAY_DAC_REGS_DPMS_CTRL(or)) & NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_PENDING);
109
110         val = NV_READ(NV50_PDISPLAY_DAC_REGS_DPMS_CTRL(or)) & ~0x7F;
111
112         if (mode != DPMSModeOn)
113                 val |= NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_BLANKED;
114
115         switch (mode) {
116                 case DPMSModeStandby:
117                         val |= NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_HSYNC_OFF;
118                         break;
119                 case DPMSModeSuspend:
120                         val |= NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_VSYNC_OFF;
121                         break;
122                 case DPMSModeOff:
123                         val |= NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_OFF;
124                         val |= NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_HSYNC_OFF;
125                         val |= NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_VSYNC_OFF;
126                         break;
127                 default:
128                         break;
129         }
130
131         NV_WRITE(NV50_PDISPLAY_DAC_REGS_DPMS_CTRL(or), val | NV50_PDISPLAY_DAC_REGS_DPMS_CTRL_PENDING);
132
133         return 0;
134 }
135
136 static int nv50_dac_destroy(struct nv50_output *output)
137 {
138         struct drm_device *dev = output->dev;
139         struct drm_nouveau_private *dev_priv = dev->dev_private;
140         struct nv50_display *display = nv50_get_display(dev);
141
142         NV50_DEBUG("\n");
143
144         if (!display || !output)
145                 return -EINVAL;
146
147         list_del(&output->item);
148
149         kfree(output->native_mode);
150         if (dev_priv->free_output)
151                 dev_priv->free_output(output);
152
153         return 0;
154 }
155
156 int nv50_dac_create(struct drm_device *dev, int dcb_entry)
157 {
158         struct drm_nouveau_private *dev_priv = dev->dev_private;
159         struct nv50_output *output = NULL;
160         struct nv50_display *display = NULL;
161         struct dcb_entry *entry = NULL;
162         int rval = 0;
163
164         NV50_DEBUG("\n");
165
166         /* This allows the public layer to do it's thing. */
167         if (dev_priv->alloc_output)
168                 output = dev_priv->alloc_output(dev);
169
170         if (!output)
171                 return -ENOMEM;
172
173         output->dev = dev;
174
175         display = nv50_get_display(dev);
176         if (!display) {
177                 rval = -EINVAL;
178                 goto out;
179         }
180
181         entry = &dev_priv->dcb_table.entry[dcb_entry];
182         if (!entry) {
183                 rval = -EINVAL;
184                 goto out;
185         }
186
187         switch (entry->type) {
188                 case DCB_OUTPUT_ANALOG:
189                         output->type = OUTPUT_DAC;
190                         DRM_INFO("Detected a DAC output\n");
191                         break;
192                 default:
193                         rval = -EINVAL;
194                         goto out;
195         }
196
197         output->dcb_entry = dcb_entry;
198         output->bus = entry->bus;
199
200         list_add_tail(&output->item, &display->outputs);
201
202         output->native_mode = kzalloc(sizeof(struct nouveau_hw_mode), GFP_KERNEL);
203         if (!output->native_mode) {
204                 rval = -ENOMEM;
205                 goto out;
206         }
207
208         /* Set function pointers. */
209         output->validate_mode = nv50_dac_validate_mode;
210         output->execute_mode = nv50_dac_execute_mode;
211         output->set_clock_mode = nv50_dac_set_clock_mode;
212         output->set_power_mode = nv50_dac_set_power_mode;
213         output->detect = NULL; /* TODO */
214         output->destroy = nv50_dac_destroy;
215
216         return 0;
217
218 out:
219         if (output->native_mode)
220                 kfree(output->native_mode);
221         if (dev_priv->free_output)
222                 dev_priv->free_output(output);
223         return rval;
224 }
225