blob: 3fab98a2f9592b6c40253026b632d6400090e3aa [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
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 "drmP.h"
28#include "drm_crtc_helper.h"
29
30#define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
31#include "nouveau_reg.h"
32#include "nouveau_drv.h"
33#include "nouveau_dma.h"
34#include "nouveau_encoder.h"
35#include "nouveau_connector.h"
36#include "nouveau_crtc.h"
37#include "nv50_display.h"
38
39static void
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100040nv50_sor_disconnect(struct drm_encoder *encoder)
Ben Skeggs6ee73862009-12-11 19:24:15 +100041{
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100042 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
43 struct drm_device *dev = encoder->dev;
Ben Skeggs59c0f572011-02-01 10:24:41 +100044 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +100045 int ret;
46
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100047 if (!nv_encoder->crtc)
48 return;
Ben Skeggs835aadb2010-07-05 15:19:16 +100049 nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100050
Maarten Maathuisef2bb502009-12-13 16:53:12 +010051 NV_DEBUG_KMS(dev, "Disconnecting SOR %d\n", nv_encoder->or);
Ben Skeggs6ee73862009-12-11 19:24:15 +100052
Ben Skeggs835aadb2010-07-05 15:19:16 +100053 ret = RING_SPACE(evo, 4);
Ben Skeggs6ee73862009-12-11 19:24:15 +100054 if (ret) {
55 NV_ERROR(dev, "no space while disconnecting SOR\n");
56 return;
57 }
58 BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
Ben Skeggs835aadb2010-07-05 15:19:16 +100059 OUT_RING (evo, 0);
60 BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
61 OUT_RING (evo, 0);
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100062
Ben Skeggs25575b42011-10-05 11:05:07 +100063 nouveau_hdmi_mode_set(encoder, NULL);
64
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100065 nv_encoder->crtc = NULL;
66 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
Ben Skeggs6ee73862009-12-11 19:24:15 +100067}
68
69static void
Ben Skeggs6ee73862009-12-11 19:24:15 +100070nv50_sor_dpms(struct drm_encoder *encoder, int mode)
71{
72 struct drm_device *dev = encoder->dev;
73 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs16226532010-01-22 10:57:01 +100074 struct drm_encoder *enc;
Ben Skeggs6ee73862009-12-11 19:24:15 +100075 uint32_t val;
76 int or = nv_encoder->or;
77
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100078 NV_DEBUG_KMS(dev, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
Ben Skeggs6ee73862009-12-11 19:24:15 +100079
Ben Skeggs16226532010-01-22 10:57:01 +100080 nv_encoder->last_dpms = mode;
81 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
82 struct nouveau_encoder *nvenc = nouveau_encoder(enc);
83
84 if (nvenc == nv_encoder ||
Ben Skeggsec7fc4a2010-07-01 15:33:45 +100085 (nvenc->dcb->type != OUTPUT_TMDS &&
86 nvenc->dcb->type != OUTPUT_LVDS &&
87 nvenc->dcb->type != OUTPUT_DP) ||
Ben Skeggs16226532010-01-22 10:57:01 +100088 nvenc->dcb->or != nv_encoder->dcb->or)
89 continue;
90
91 if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
92 return;
93 }
94
Ben Skeggs6ee73862009-12-11 19:24:15 +100095 /* wait for it to be done */
Francisco Jerez4b5c1522010-09-07 17:34:44 +020096 if (!nv_wait(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or),
Ben Skeggs6ee73862009-12-11 19:24:15 +100097 NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING, 0)) {
98 NV_ERROR(dev, "timeout: SOR_DPMS_CTRL_PENDING(%d) == 0\n", or);
99 NV_ERROR(dev, "SOR_DPMS_CTRL(%d) = 0x%08x\n", or,
100 nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or)));
101 }
102
103 val = nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or));
104
105 if (mode == DRM_MODE_DPMS_ON)
106 val |= NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
107 else
108 val &= ~NV50_PDISPLAY_SOR_DPMS_CTRL_ON;
109
110 nv_wr32(dev, NV50_PDISPLAY_SOR_DPMS_CTRL(or), val |
111 NV50_PDISPLAY_SOR_DPMS_CTRL_PENDING);
Francisco Jerez4b5c1522010-09-07 17:34:44 +0200112 if (!nv_wait(dev, NV50_PDISPLAY_SOR_DPMS_STATE(or),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000113 NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
114 NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", or);
115 NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", or,
116 nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(or)));
117 }
118
Ben Skeggs1f403d92010-07-06 11:20:17 +1000119 if (nv_encoder->dcb->type == OUTPUT_DP) {
120 struct nouveau_i2c_chan *auxch;
121
122 auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
123 if (!auxch)
124 return;
125
126 if (mode == DRM_MODE_DPMS_ON) {
127 u8 status = DP_SET_POWER_D0;
128 nouveau_dp_auxch(auxch, 8, DP_SET_POWER, &status, 1);
Ben Skeggsa002fec2011-08-04 11:04:47 +1000129 nouveau_dp_link_train(encoder, nv_encoder->dp.datarate);
Ben Skeggs1f403d92010-07-06 11:20:17 +1000130 } else {
131 u8 status = DP_SET_POWER_D3;
132 nouveau_dp_auxch(auxch, 8, DP_SET_POWER, &status, 1);
133 }
134 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000135}
136
137static void
138nv50_sor_save(struct drm_encoder *encoder)
139{
140 NV_ERROR(encoder->dev, "!!\n");
141}
142
143static void
144nv50_sor_restore(struct drm_encoder *encoder)
145{
146 NV_ERROR(encoder->dev, "!!\n");
147}
148
149static bool
150nv50_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
151 struct drm_display_mode *adjusted_mode)
152{
153 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
154 struct nouveau_connector *connector;
155
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100156 NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000157
158 connector = nouveau_encoder_connector_get(nv_encoder);
159 if (!connector) {
160 NV_ERROR(encoder->dev, "Encoder has no connector\n");
161 return false;
162 }
163
164 if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
165 connector->native_mode) {
166 int id = adjusted_mode->base.id;
167 *adjusted_mode = *connector->native_mode;
168 adjusted_mode->base.id = id;
169 }
170
171 return true;
172}
173
174static void
175nv50_sor_prepare(struct drm_encoder *encoder)
176{
Ben Skeggs7ae494e2011-10-14 14:43:20 +1000177 nv50_sor_disconnect(encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000178}
179
180static void
181nv50_sor_commit(struct drm_encoder *encoder)
182{
183}
184
185static void
186nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
187 struct drm_display_mode *adjusted_mode)
188{
Ben Skeggs59c0f572011-02-01 10:24:41 +1000189 struct nouveau_channel *evo = nv50_display(encoder->dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
191 struct drm_device *dev = encoder->dev;
192 struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs46959b72011-07-01 15:51:49 +1000193 struct nouveau_connector *nv_connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000194 uint32_t mode_ctl = 0;
195 int ret;
196
Ben Skeggs04412922010-07-02 13:47:38 +1000197 NV_DEBUG_KMS(dev, "or %d type %d -> crtc %d\n",
198 nv_encoder->or, nv_encoder->dcb->type, crtc->index);
Ben Skeggs52c7bcd2011-10-05 14:52:02 +1000199 nv_encoder->crtc = encoder->crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000200
Ben Skeggs6ee73862009-12-11 19:24:15 +1000201 switch (nv_encoder->dcb->type) {
202 case OUTPUT_TMDS:
203 if (nv_encoder->dcb->sorconf.link & 1) {
204 if (adjusted_mode->clock < 165000)
205 mode_ctl = 0x0100;
206 else
207 mode_ctl = 0x0500;
208 } else
209 mode_ctl = 0x0200;
Ben Skeggs25575b42011-10-05 11:05:07 +1000210
Ben Skeggs52c7bcd2011-10-05 14:52:02 +1000211 nouveau_hdmi_mode_set(encoder, adjusted_mode);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212 break;
213 case OUTPUT_DP:
Ben Skeggs46959b72011-07-01 15:51:49 +1000214 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggsa002fec2011-08-04 11:04:47 +1000215 if (nv_connector && nv_connector->base.display_info.bpc == 6) {
216 nv_encoder->dp.datarate = crtc->mode->clock * 18 / 8;
Ben Skeggs46959b72011-07-01 15:51:49 +1000217 mode_ctl |= 0x00020000;
Ben Skeggsa002fec2011-08-04 11:04:47 +1000218 } else {
219 nv_encoder->dp.datarate = crtc->mode->clock * 24 / 8;
Ben Skeggs46959b72011-07-01 15:51:49 +1000220 mode_ctl |= 0x00050000;
Ben Skeggsa002fec2011-08-04 11:04:47 +1000221 }
Ben Skeggs46959b72011-07-01 15:51:49 +1000222
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 if (nv_encoder->dcb->sorconf.link & 1)
224 mode_ctl |= 0x00000800;
225 else
226 mode_ctl |= 0x00000900;
227 break;
228 default:
229 break;
230 }
231
232 if (crtc->index == 1)
233 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
234 else
235 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
236
237 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
238 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
239
240 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
241 mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
242
Ben Skeggsa002fec2011-08-04 11:04:47 +1000243 nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
244
Ben Skeggs6ee73862009-12-11 19:24:15 +1000245 ret = RING_SPACE(evo, 2);
246 if (ret) {
247 NV_ERROR(dev, "no space while connecting SOR\n");
Ben Skeggs52c7bcd2011-10-05 14:52:02 +1000248 nv_encoder->crtc = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249 return;
250 }
251 BEGIN_RING(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
252 OUT_RING(evo, mode_ctl);
Ben Skeggsec7fc4a2010-07-01 15:33:45 +1000253}
254
255static struct drm_crtc *
256nv50_sor_crtc_get(struct drm_encoder *encoder)
257{
258 return nouveau_encoder(encoder)->crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000259}
260
261static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
262 .dpms = nv50_sor_dpms,
263 .save = nv50_sor_save,
264 .restore = nv50_sor_restore,
265 .mode_fixup = nv50_sor_mode_fixup,
266 .prepare = nv50_sor_prepare,
267 .commit = nv50_sor_commit,
268 .mode_set = nv50_sor_mode_set,
Ben Skeggsec7fc4a2010-07-01 15:33:45 +1000269 .get_crtc = nv50_sor_crtc_get,
270 .detect = NULL,
271 .disable = nv50_sor_disconnect
Ben Skeggs6ee73862009-12-11 19:24:15 +1000272};
273
274static void
275nv50_sor_destroy(struct drm_encoder *encoder)
276{
277 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
278
279 if (!encoder)
280 return;
281
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100282 NV_DEBUG_KMS(encoder->dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000283
284 drm_encoder_cleanup(encoder);
285
286 kfree(nv_encoder);
287}
288
289static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
290 .destroy = nv50_sor_destroy,
291};
292
293int
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000294nv50_sor_create(struct drm_connector *connector, struct dcb_entry *entry)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000295{
296 struct nouveau_encoder *nv_encoder = NULL;
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000297 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 struct drm_encoder *encoder;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000299 int type;
300
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100301 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302
303 switch (entry->type) {
304 case OUTPUT_TMDS:
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000305 case OUTPUT_DP:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306 type = DRM_MODE_ENCODER_TMDS;
307 break;
308 case OUTPUT_LVDS:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309 type = DRM_MODE_ENCODER_LVDS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000310 break;
311 default:
312 return -EINVAL;
313 }
314
315 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
316 if (!nv_encoder)
317 return -ENOMEM;
318 encoder = to_drm_encoder(nv_encoder);
319
320 nv_encoder->dcb = entry;
321 nv_encoder->or = ffs(entry->or) - 1;
Ben Skeggsec7fc4a2010-07-01 15:33:45 +1000322 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000323
324 drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
325 drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
326
327 encoder->possible_crtcs = entry->heads;
328 encoder->possible_clones = 0;
329
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000330 drm_mode_connector_attach_encoder(connector, encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000331 return 0;
332}