drm/nouveau: tidy connector/encoder creation a little
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / nouveau / nv04_display.c
blobb35b7ed0833b036ac2edc07be4b2a4a0eb0bf8f7
1 /*
2 * Copyright 2009 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Author: Ben Skeggs
25 #include "drmP.h"
26 #include "drm.h"
27 #include "drm_crtc_helper.h"
29 #include "nouveau_drv.h"
30 #include "nouveau_fb.h"
31 #include "nouveau_hw.h"
32 #include "nouveau_encoder.h"
33 #include "nouveau_connector.h"
35 #define MULTIPLE_ENCODERS(e) (e & (e - 1))
37 static void
38 nv04_display_store_initial_head_owner(struct drm_device *dev)
40 struct drm_nouveau_private *dev_priv = dev->dev_private;
42 if (dev_priv->chipset != 0x11) {
43 dev_priv->crtc_owner = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_44);
44 goto ownerknown;
47 /* reading CR44 is broken on nv11, so we attempt to infer it */
48 if (nvReadMC(dev, NV_PBUS_DEBUG_1) & (1 << 28)) /* heads tied, restore both */
49 dev_priv->crtc_owner = 0x4;
50 else {
51 uint8_t slaved_on_A, slaved_on_B;
52 bool tvA = false;
53 bool tvB = false;
55 NVLockVgaCrtcs(dev, false);
57 slaved_on_B = NVReadVgaCrtc(dev, 1, NV_CIO_CRE_PIXEL_INDEX) &
58 0x80;
59 if (slaved_on_B)
60 tvB = !(NVReadVgaCrtc(dev, 1, NV_CIO_CRE_LCD__INDEX) &
61 MASK(NV_CIO_CRE_LCD_LCD_SELECT));
63 slaved_on_A = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_PIXEL_INDEX) &
64 0x80;
65 if (slaved_on_A)
66 tvA = !(NVReadVgaCrtc(dev, 0, NV_CIO_CRE_LCD__INDEX) &
67 MASK(NV_CIO_CRE_LCD_LCD_SELECT));
69 NVLockVgaCrtcs(dev, true);
71 if (slaved_on_A && !tvA)
72 dev_priv->crtc_owner = 0x0;
73 else if (slaved_on_B && !tvB)
74 dev_priv->crtc_owner = 0x3;
75 else if (slaved_on_A)
76 dev_priv->crtc_owner = 0x0;
77 else if (slaved_on_B)
78 dev_priv->crtc_owner = 0x3;
79 else
80 dev_priv->crtc_owner = 0x0;
83 ownerknown:
84 NV_INFO(dev, "Initial CRTC_OWNER is %d\n", dev_priv->crtc_owner);
86 /* we need to ensure the heads are not tied henceforth, or reading any
87 * 8 bit reg on head B will fail
88 * setting a single arbitrary head solves that */
89 NVSetOwner(dev, 0);
92 int
93 nv04_display_create(struct drm_device *dev)
95 struct drm_nouveau_private *dev_priv = dev->dev_private;
96 struct dcb_table *dcb = &dev_priv->vbios.dcb;
97 struct drm_connector *connector, *ct;
98 struct drm_encoder *encoder;
99 struct drm_crtc *crtc;
100 int i, ret;
102 NV_DEBUG_KMS(dev, "\n");
104 if (nv_two_heads(dev))
105 nv04_display_store_initial_head_owner(dev);
106 nouveau_hw_save_vga_fonts(dev, 1);
108 drm_mode_config_init(dev);
109 drm_mode_create_scaling_mode_property(dev);
110 drm_mode_create_dithering_property(dev);
112 dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
114 dev->mode_config.min_width = 0;
115 dev->mode_config.min_height = 0;
116 switch (dev_priv->card_type) {
117 case NV_04:
118 dev->mode_config.max_width = 2048;
119 dev->mode_config.max_height = 2048;
120 break;
121 default:
122 dev->mode_config.max_width = 4096;
123 dev->mode_config.max_height = 4096;
124 break;
127 dev->mode_config.fb_base = dev_priv->fb_phys;
129 nv04_crtc_create(dev, 0);
130 if (nv_two_heads(dev))
131 nv04_crtc_create(dev, 1);
133 for (i = 0; i < dcb->entries; i++) {
134 struct dcb_entry *dcbent = &dcb->entry[i];
136 connector = nouveau_connector_create(dev, dcbent->connector);
137 if (IS_ERR(connector))
138 continue;
140 switch (dcbent->type) {
141 case OUTPUT_ANALOG:
142 ret = nv04_dac_create(connector, dcbent);
143 break;
144 case OUTPUT_LVDS:
145 case OUTPUT_TMDS:
146 ret = nv04_dfp_create(connector, dcbent);
147 break;
148 case OUTPUT_TV:
149 if (dcbent->location == DCB_LOC_ON_CHIP)
150 ret = nv17_tv_create(connector, dcbent);
151 else
152 ret = nv04_tv_create(connector, dcbent);
153 break;
154 default:
155 NV_WARN(dev, "DCB type %d not known\n", dcbent->type);
156 continue;
159 if (ret)
160 continue;
163 list_for_each_entry_safe(connector, ct,
164 &dev->mode_config.connector_list, head) {
165 if (!connector->encoder_ids[0]) {
166 NV_WARN(dev, "%s has no encoders, removing\n",
167 drm_get_connector_name(connector));
168 connector->funcs->destroy(connector);
172 /* Save previous state */
173 NVLockVgaCrtcs(dev, false);
175 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
176 crtc->funcs->save(crtc);
178 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
179 struct drm_encoder_helper_funcs *func = encoder->helper_private;
181 func->save(encoder);
184 return 0;
187 void
188 nv04_display_destroy(struct drm_device *dev)
190 struct drm_encoder *encoder;
191 struct drm_crtc *crtc;
193 NV_DEBUG_KMS(dev, "\n");
195 /* Turn every CRTC off. */
196 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
197 struct drm_mode_set modeset = {
198 .crtc = crtc,
201 crtc->funcs->set_config(&modeset);
204 /* Restore state */
205 NVLockVgaCrtcs(dev, false);
207 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
208 struct drm_encoder_helper_funcs *func = encoder->helper_private;
210 func->restore(encoder);
213 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
214 crtc->funcs->restore(crtc);
216 drm_mode_config_cleanup(dev);
218 nouveau_hw_save_vga_fonts(dev, 0);
221 void
222 nv04_display_restore(struct drm_device *dev)
224 struct drm_nouveau_private *dev_priv = dev->dev_private;
225 struct drm_encoder *encoder;
226 struct drm_crtc *crtc;
228 NVLockVgaCrtcs(dev, false);
230 /* meh.. modeset apparently doesn't setup all the regs and depends
231 * on pre-existing state, for now load the state of the card *before*
232 * nouveau was loaded, and then do a modeset.
234 * best thing to do probably is to make save/restore routines not
235 * save/restore "pre-load" state, but more general so we can save
236 * on suspend too.
238 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
239 struct drm_encoder_helper_funcs *func = encoder->helper_private;
241 func->restore(encoder);
244 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
245 crtc->funcs->restore(crtc);
247 if (nv_two_heads(dev)) {
248 NV_INFO(dev, "Restoring CRTC_OWNER to %d.\n",
249 dev_priv->crtc_owner);
250 NVSetOwner(dev, dev_priv->crtc_owner);
253 NVLockVgaCrtcs(dev, true);