NV50: A few minor added safeties + cleanup.
[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_destroy(struct nv50_output *output)
100 {
101         struct drm_device *dev = output->dev;
102         struct drm_nouveau_private *dev_priv = dev->dev_private;
103         struct nv50_display *display = nv50_get_display(dev);
104
105         NV50_DEBUG("\n");
106
107         if (!display || !output)
108                 return -EINVAL;
109
110         list_del(&output->head);
111
112         kfree(output->native_mode);
113         if (dev_priv->free_output)
114                 dev_priv->free_output(output);
115
116         return 0;
117 }
118
119 int nv50_dac_create(struct drm_device *dev, int dcb_entry)
120 {
121         struct drm_nouveau_private *dev_priv = dev->dev_private;
122         struct nv50_output *output = NULL;
123         struct nv50_display *display = NULL;
124         struct dcb_entry *entry = NULL;
125         int rval = 0;
126
127         NV50_DEBUG("\n");
128
129         /* This allows the public layer to do it's thing. */
130         if (dev_priv->alloc_output)
131                 output = dev_priv->alloc_output(dev);
132
133         if (!output)
134                 return -ENOMEM;
135
136         output->dev = dev;
137
138         display = nv50_get_display(dev);
139         if (!display) {
140                 rval = -EINVAL;
141                 goto out;
142         }
143
144         entry = &dev_priv->dcb_table.entry[dcb_entry];
145         if (!entry) {
146                 rval = -EINVAL;
147                 goto out;
148         }
149
150         switch (entry->type) {
151                 case DCB_OUTPUT_ANALOG:
152                         output->type = OUTPUT_DAC;
153                         DRM_INFO("Detected a DAC output\n");
154                         break;
155                 default:
156                         rval = -EINVAL;
157                         goto out;
158         }
159
160         output->dcb_entry = dcb_entry;
161         output->bus = entry->bus;
162
163         list_add_tail(&output->head, &display->outputs);
164
165         output->native_mode = kzalloc(sizeof(struct nouveau_hw_mode), GFP_KERNEL);
166         if (!output->native_mode) {
167                 rval = -ENOMEM;
168                 goto out;
169         }
170
171         /* Set function pointers. */
172         output->validate_mode = nv50_dac_validate_mode;
173         output->execute_mode = nv50_dac_execute_mode;
174         output->set_clock_mode = nv50_dac_set_clock_mode;
175         output->detect = NULL; /* TODO */
176         output->destroy = nv50_dac_destroy;
177
178         return 0;
179
180 out:
181         if (output->native_mode)
182                 kfree(output->native_mode);
183         if (dev_priv->free_output)
184                 dev_priv->free_output(output);
185         return rval;
186 }
187