[modesetting-101] update mode count after fill_modes.
[platform/upstream/libdrm.git] / linux-core / nv50_sor.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_sor_validate_mode(struct nv50_output *output, struct nouveau_hw_mode *mode)
30 {
31         NV50_DEBUG("\n");
32
33         if (mode->clock > 165000) /* no dual link until we figure it out completely */
34                 return MODE_CLOCK_HIGH;
35
36         if (mode->clock < 25000)
37                 return MODE_CLOCK_LOW;
38
39         if (output->native_mode->hdisplay > 0 && output->native_mode->vdisplay > 0) {
40                 if (mode->hdisplay > output->native_mode->hdisplay || mode->vdisplay > output->native_mode->vdisplay)
41                         return MODE_PANEL;
42         }
43
44         return MODE_OK;
45 }
46
47 static int nv50_sor_execute_mode(struct nv50_output *output, bool disconnect)
48 {
49         struct drm_nouveau_private *dev_priv = output->dev->dev_private;
50         struct nv50_crtc *crtc = output->crtc;
51         struct nouveau_hw_mode *desired_mode = NULL;
52
53         uint32_t offset = nv50_output_or_offset(output) * 0x40;
54
55         uint32_t mode_ctl = NV50_SOR_MODE_CTRL_OFF;
56
57         NV50_DEBUG("or %d\n", nv50_output_or_offset(output));
58
59         if (disconnect) {
60                 NV50_DEBUG("Disconnecting SOR\n");
61                 OUT_MODE(NV50_SOR0_MODE_CTRL + offset, mode_ctl);
62                 return 0;
63         }
64
65         desired_mode = (crtc->use_native_mode ? crtc->native_mode : crtc->mode);
66
67         if (output->type == OUTPUT_LVDS) {
68                 mode_ctl |= NV50_SOR_MODE_CTRL_LVDS;
69         } else {
70                 mode_ctl |= NV50_SOR_MODE_CTRL_TMDS;
71                 if (desired_mode->clock > 165000)
72                         mode_ctl |= NV50_SOR_MODE_CTRL_TMDS_DUAL_LINK;
73         }
74
75         if (crtc->index == 1)
76                 mode_ctl |= NV50_SOR_MODE_CTRL_CRTC1;
77         else
78                 mode_ctl |= NV50_SOR_MODE_CTRL_CRTC0;
79
80         if (desired_mode->flags & V_NHSYNC)
81                 mode_ctl |= NV50_SOR_MODE_CTRL_NHSYNC;
82
83         if (desired_mode->flags & V_NVSYNC)
84                 mode_ctl |= NV50_SOR_MODE_CTRL_NVSYNC;
85
86         /* TODO: DPMS ?????? */
87
88         OUT_MODE(NV50_SOR0_MODE_CTRL + offset, mode_ctl);
89
90         return 0;
91 }
92
93 static int nv50_sor_set_clock_mode(struct nv50_output *output)
94 {
95         struct drm_nouveau_private *dev_priv = output->dev->dev_private;
96         struct nv50_crtc *crtc = output->crtc;
97
98         uint32_t limit = 165000;
99         struct nouveau_hw_mode *hw_mode;
100
101         NV50_DEBUG("or %d\n", nv50_output_or_offset(output));
102
103         /* We don't yet know what to do, if anything at all. */
104         if (output->type == OUTPUT_LVDS)
105                 return 0;
106
107         if (crtc->use_native_mode)
108                 hw_mode = crtc->native_mode;
109         else
110                 hw_mode = crtc->mode;
111
112         /* 0x70000 was a late addition to nv, mentioned as fixing tmds initialisation on certain gpu's. */
113         /* I presume it's some kind of clock setting, but what precisely i do not know. */
114         NV_WRITE(NV50_PDISPLAY_SOR_CLK_CLK_CTRL2(nv50_output_or_offset(output)), 0x70000 | ((hw_mode->clock > limit) ? 0x101 : 0));
115
116         return 0;
117 }
118
119 static int nv50_sor_destroy(struct nv50_output *output)
120 {
121         struct drm_device *dev = output->dev;
122         struct drm_nouveau_private *dev_priv = dev->dev_private;
123         struct nv50_display *display = nv50_get_display(dev);
124
125         NV50_DEBUG("\n");
126
127         if (!display || !output)
128                 return -EINVAL;
129
130         list_del(&output->head);
131
132         kfree(output->native_mode);
133         if (dev_priv->free_output)
134                 dev_priv->free_output(output);
135
136         return 0;
137 }
138
139 int nv50_sor_create(struct drm_device *dev, int dcb_entry)
140 {
141         struct drm_nouveau_private *dev_priv = dev->dev_private;
142         struct nv50_output *output = NULL;
143         struct nv50_display *display = NULL;
144         struct dcb_entry *entry = NULL;
145         int rval = 0;
146
147         NV50_DEBUG("\n");
148
149         /* This allows the public layer to do it's thing. */
150         if (dev_priv->alloc_output)
151                 output = dev_priv->alloc_output(dev);
152
153         if (!output)
154                 return -ENOMEM;
155
156         output->dev = dev;
157
158         display = nv50_get_display(dev);
159         if (!display) {
160                 rval = -EINVAL;
161                 goto out;
162         }
163
164         entry = &dev_priv->dcb_table.entry[dcb_entry];
165         if (!entry) {
166                 rval = -EINVAL;
167                 goto out;
168         }
169
170         switch (entry->type) {
171                 case DCB_OUTPUT_TMDS:
172                         output->type = OUTPUT_TMDS;
173                         DRM_INFO("Detected a TMDS output\n");
174                         break;
175                 case DCB_OUTPUT_LVDS:
176                         output->type = OUTPUT_LVDS;
177                         DRM_INFO("Detected a LVDS output\n");
178                         break;
179                 default:
180                         rval = -EINVAL;
181                         goto out;
182         }
183
184         output->dcb_entry = dcb_entry;
185         output->bus = entry->bus;
186
187         list_add_tail(&output->head, &display->outputs);
188
189         output->native_mode = kzalloc(sizeof(struct nouveau_hw_mode), GFP_KERNEL);
190         if (!output->native_mode) {
191                 rval = -ENOMEM;
192                 goto out;
193         }
194
195         /* Set function pointers. */
196         output->validate_mode = nv50_sor_validate_mode;
197         output->execute_mode = nv50_sor_execute_mode;
198         output->set_clock_mode = nv50_sor_set_clock_mode;
199         output->detect = NULL;
200         output->destroy = nv50_sor_destroy;
201
202         /* Some default state, unknown what it precisely means. */
203         if (output->type == OUTPUT_TMDS) {
204                 NV_WRITE(NV50_PDISPLAY_SOR_REGS_UNK_00C(nv50_output_or_offset(output)), 0x03010700);
205                 NV_WRITE(NV50_PDISPLAY_SOR_REGS_UNK_010(nv50_output_or_offset(output)), 0x0000152f);
206                 NV_WRITE(NV50_PDISPLAY_SOR_REGS_UNK_014(nv50_output_or_offset(output)), 0x00000000);
207                 NV_WRITE(NV50_PDISPLAY_SOR_REGS_UNK_018(nv50_output_or_offset(output)), 0x00245af8);
208         }
209
210         return 0;
211
212 out:
213         if (output->native_mode)
214                 kfree(output->native_mode);
215         if (dev_priv->free_output)
216                 dev_priv->free_output(output);
217         return rval;
218 }