Merge remote-tracking branch 'asoc/fix/wm2000' into tmp
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nv04_display.c
1 /*
2  * Copyright 2009 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Author: Ben Skeggs
23  */
24
25 #include <drm/drmP.h>
26 #include <drm/drm_crtc_helper.h>
27
28 #include "nouveau_drm.h"
29 #include "nouveau_reg.h"
30 #include "nouveau_hw.h"
31 #include "nouveau_encoder.h"
32 #include "nouveau_connector.h"
33
34 int
35 nv04_display_early_init(struct drm_device *dev)
36 {
37         /* ensure vblank interrupts are off, they can't be enabled until
38          * drm_vblank has been initialised
39          */
40         NVWriteCRTC(dev, 0, NV_PCRTC_INTR_EN_0, 0);
41         if (nv_two_heads(dev))
42                 NVWriteCRTC(dev, 1, NV_PCRTC_INTR_EN_0, 0);
43
44         return 0;
45 }
46
47 void
48 nv04_display_late_takedown(struct drm_device *dev)
49 {
50 }
51
52 int
53 nv04_display_create(struct drm_device *dev)
54 {
55         struct nouveau_drm *drm = nouveau_drm(dev);
56         struct dcb_table *dcb = &drm->vbios.dcb;
57         struct drm_connector *connector, *ct;
58         struct drm_encoder *encoder;
59         struct drm_crtc *crtc;
60         struct nv04_display *disp;
61         int i, ret;
62
63         disp = kzalloc(sizeof(*disp), GFP_KERNEL);
64         if (!disp)
65                 return -ENOMEM;
66
67         nouveau_display(dev)->priv = disp;
68         nouveau_display(dev)->dtor = nv04_display_destroy;
69         nouveau_display(dev)->init = nv04_display_init;
70         nouveau_display(dev)->fini = nv04_display_fini;
71
72         nouveau_hw_save_vga_fonts(dev, 1);
73
74         nv04_crtc_create(dev, 0);
75         if (nv_two_heads(dev))
76                 nv04_crtc_create(dev, 1);
77
78         for (i = 0; i < dcb->entries; i++) {
79                 struct dcb_output *dcbent = &dcb->entry[i];
80
81                 connector = nouveau_connector_create(dev, dcbent->connector);
82                 if (IS_ERR(connector))
83                         continue;
84
85                 switch (dcbent->type) {
86                 case DCB_OUTPUT_ANALOG:
87                         ret = nv04_dac_create(connector, dcbent);
88                         break;
89                 case DCB_OUTPUT_LVDS:
90                 case DCB_OUTPUT_TMDS:
91                         ret = nv04_dfp_create(connector, dcbent);
92                         break;
93                 case DCB_OUTPUT_TV:
94                         if (dcbent->location == DCB_LOC_ON_CHIP)
95                                 ret = nv17_tv_create(connector, dcbent);
96                         else
97                                 ret = nv04_tv_create(connector, dcbent);
98                         break;
99                 default:
100                         NV_WARN(drm, "DCB type %d not known\n", dcbent->type);
101                         continue;
102                 }
103
104                 if (ret)
105                         continue;
106         }
107
108         list_for_each_entry_safe(connector, ct,
109                                  &dev->mode_config.connector_list, head) {
110                 if (!connector->encoder_ids[0]) {
111                         NV_WARN(drm, "%s has no encoders, removing\n",
112                                 drm_get_connector_name(connector));
113                         connector->funcs->destroy(connector);
114                 }
115         }
116
117         /* Save previous state */
118         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
119                 crtc->funcs->save(crtc);
120
121         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
122                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
123
124                 func->save(encoder);
125         }
126
127         return 0;
128 }
129
130 void
131 nv04_display_destroy(struct drm_device *dev)
132 {
133         struct nv04_display *disp = nv04_display(dev);
134         struct drm_encoder *encoder;
135         struct drm_crtc *crtc;
136
137         /* Turn every CRTC off. */
138         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
139                 struct drm_mode_set modeset = {
140                         .crtc = crtc,
141                 };
142
143                 crtc->funcs->set_config(&modeset);
144         }
145
146         /* Restore state */
147         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
148                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
149
150                 func->restore(encoder);
151         }
152
153         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
154                 crtc->funcs->restore(crtc);
155
156         nouveau_hw_save_vga_fonts(dev, 0);
157
158         nouveau_display(dev)->priv = NULL;
159         kfree(disp);
160 }
161
162 int
163 nv04_display_init(struct drm_device *dev)
164 {
165         struct drm_encoder *encoder;
166         struct drm_crtc *crtc;
167
168         /* meh.. modeset apparently doesn't setup all the regs and depends
169          * on pre-existing state, for now load the state of the card *before*
170          * nouveau was loaded, and then do a modeset.
171          *
172          * best thing to do probably is to make save/restore routines not
173          * save/restore "pre-load" state, but more general so we can save
174          * on suspend too.
175          */
176         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
177                 struct drm_encoder_helper_funcs *func = encoder->helper_private;
178
179                 func->restore(encoder);
180         }
181
182         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
183                 crtc->funcs->restore(crtc);
184
185         return 0;
186 }
187
188 void
189 nv04_display_fini(struct drm_device *dev)
190 {
191         /* disable vblank interrupts */
192         NVWriteCRTC(dev, 0, NV_PCRTC_INTR_EN_0, 0);
193         if (nv_two_heads(dev))
194                 NVWriteCRTC(dev, 1, NV_PCRTC_INTR_EN_0, 0);
195 }