blob: 703c188c32d66a7d58d0038550644b2b14aa7ec8 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2009 Francisco Jerez.
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#include "nouveau_drv.h"
30#include "nouveau_encoder.h"
31#include "nouveau_connector.h"
32#include "nouveau_crtc.h"
33#include "nouveau_hw.h"
34#include "nv17_tv.h"
35
Francisco Jerez08291682009-12-17 18:57:44 +010036static uint32_t nv42_tv_sample_load(struct drm_encoder *encoder)
37{
38 struct drm_device *dev = encoder->dev;
39 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsee2e0132010-07-26 09:28:25 +100040 struct nouveau_gpio_engine *gpio = &dev_priv->engine.gpio;
Francisco Jerez08291682009-12-17 18:57:44 +010041 uint32_t testval, regoffset = nv04_dac_output_offset(encoder);
42 uint32_t gpio0, gpio1, fp_htotal, fp_hsync_start, fp_hsync_end,
43 fp_control, test_ctrl, dacclk, ctv_14, ctv_1c, ctv_6c;
44 uint32_t sample = 0;
45 int head;
46
47#define RGB_TEST_DATA(r, g, b) (r << 0 | g << 10 | b << 20)
48 testval = RGB_TEST_DATA(0x82, 0xeb, 0x82);
Ben Skeggs04a39c52010-02-24 10:03:05 +100049 if (dev_priv->vbios.tvdactestval)
50 testval = dev_priv->vbios.tvdactestval;
Francisco Jerez08291682009-12-17 18:57:44 +010051
52 dacclk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset);
53 head = (dacclk & 0x100) >> 8;
54
55 /* Save the previous state. */
Ben Skeggsee2e0132010-07-26 09:28:25 +100056 gpio1 = gpio->get(dev, DCB_GPIO_TVDAC1);
57 gpio0 = gpio->get(dev, DCB_GPIO_TVDAC0);
Francisco Jerez08291682009-12-17 18:57:44 +010058 fp_htotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL);
59 fp_hsync_start = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START);
60 fp_hsync_end = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END);
61 fp_control = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
62 test_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset);
63 ctv_1c = NVReadRAMDAC(dev, head, 0x680c1c);
64 ctv_14 = NVReadRAMDAC(dev, head, 0x680c14);
65 ctv_6c = NVReadRAMDAC(dev, head, 0x680c6c);
66
67 /* Prepare the DAC for load detection. */
Ben Skeggsee2e0132010-07-26 09:28:25 +100068 gpio->set(dev, DCB_GPIO_TVDAC1, true);
69 gpio->set(dev, DCB_GPIO_TVDAC0, true);
Francisco Jerez08291682009-12-17 18:57:44 +010070
71 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, 1343);
72 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, 1047);
73 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, 1183);
74 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL,
75 NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
76 NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12 |
77 NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
78 NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS |
79 NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS);
80
81 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, 0);
82
83 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset,
84 (dacclk & ~0xff) | 0x22);
85 msleep(1);
86 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset,
87 (dacclk & ~0xff) | 0x21);
88
89 NVWriteRAMDAC(dev, head, 0x680c1c, 1 << 20);
90 NVWriteRAMDAC(dev, head, 0x680c14, 4 << 16);
91
92 /* Sample pin 0x4 (usually S-video luma). */
93 NVWriteRAMDAC(dev, head, 0x680c6c, testval >> 10 & 0x3ff);
94 msleep(20);
95 sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset)
96 & 0x4 << 28;
97
98 /* Sample the remaining pins. */
99 NVWriteRAMDAC(dev, head, 0x680c6c, testval & 0x3ff);
100 msleep(20);
101 sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset)
102 & 0xa << 28;
103
104 /* Restore the previous state. */
105 NVWriteRAMDAC(dev, head, 0x680c1c, ctv_1c);
106 NVWriteRAMDAC(dev, head, 0x680c14, ctv_14);
107 NVWriteRAMDAC(dev, head, 0x680c6c, ctv_6c);
108 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, dacclk);
109 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, test_ctrl);
110 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, fp_control);
111 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, fp_hsync_end);
112 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, fp_hsync_start);
113 NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, fp_htotal);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000114 gpio->set(dev, DCB_GPIO_TVDAC1, gpio1);
115 gpio->set(dev, DCB_GPIO_TVDAC0, gpio0);
Francisco Jerez08291682009-12-17 18:57:44 +0100116
117 return sample;
118}
119
Francisco Jerez4664c672010-07-04 16:46:01 +0200120static bool
121get_tv_detect_quirks(struct drm_device *dev, uint32_t *pin_mask)
122{
123 /* Zotac FX5200 */
Francisco Jerezacae1162010-08-15 14:31:31 +0200124 if (nv_match_device(dev, 0x0322, 0x19da, 0x1035) ||
125 nv_match_device(dev, 0x0322, 0x19da, 0x2035)) {
Francisco Jerez4664c672010-07-04 16:46:01 +0200126 *pin_mask = 0xc;
127 return false;
128 }
129
Francisco Jerez19bf5f72010-08-14 18:45:58 +0200130 /* MSI nForce2 IGP */
Francisco Jerezacae1162010-08-15 14:31:31 +0200131 if (nv_match_device(dev, 0x01f0, 0x1462, 0x5710)) {
Francisco Jerez19bf5f72010-08-14 18:45:58 +0200132 *pin_mask = 0xc;
133 return false;
134 }
135
Francisco Jerez4664c672010-07-04 16:46:01 +0200136 return true;
137}
138
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100139static enum drm_connector_status
140nv17_tv_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141{
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100142 struct drm_device *dev = encoder->dev;
Francisco Jerez08291682009-12-17 18:57:44 +0100143 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100144 struct drm_mode_config *conf = &dev->mode_config;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000145 struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100146 struct dcb_entry *dcb = tv_enc->base.dcb;
Francisco Jerez4664c672010-07-04 16:46:01 +0200147 bool reliable = get_tv_detect_quirks(dev, &tv_enc->pin_mask);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148
Francisco Jerez8ccfe9e2010-07-04 16:14:42 +0200149 if (nv04_dac_in_use(encoder))
150 return connector_status_disconnected;
151
Francisco Jerez4664c672010-07-04 16:46:01 +0200152 if (reliable) {
153 if (dev_priv->chipset == 0x42 ||
154 dev_priv->chipset == 0x43)
155 tv_enc->pin_mask =
156 nv42_tv_sample_load(encoder) >> 28 & 0xe;
157 else
158 tv_enc->pin_mask =
159 nv17_dac_sample_load(encoder) >> 28 & 0xe;
160 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161
162 switch (tv_enc->pin_mask) {
163 case 0x2:
164 case 0x4:
165 tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Composite;
166 break;
167 case 0xc:
168 tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO;
169 break;
170 case 0xe:
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100171 if (dcb->tvconf.has_component_output)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172 tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Component;
173 else
174 tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SCART;
175 break;
176 default:
177 tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
178 break;
179 }
180
181 drm_connector_property_set_value(connector,
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100182 conf->tv_subconnector_property,
183 tv_enc->subconnector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000184
Francisco Jerez4664c672010-07-04 16:46:01 +0200185 if (!reliable) {
186 return connector_status_unknown;
187 } else if (tv_enc->subconnector) {
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100188 NV_INFO(dev, "Load detected on output %c\n",
189 '@' + ffs(dcb->or));
190 return connector_status_connected;
191 } else {
192 return connector_status_disconnected;
193 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000194}
195
196static const struct {
197 int hdisplay;
198 int vdisplay;
199} modes[] = {
200 { 640, 400 },
201 { 640, 480 },
202 { 720, 480 },
203 { 720, 576 },
204 { 800, 600 },
205 { 1024, 768 },
206 { 1280, 720 },
207 { 1280, 1024 },
208 { 1920, 1080 }
209};
210
211static int nv17_tv_get_modes(struct drm_encoder *encoder,
212 struct drm_connector *connector)
213{
214 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
215 struct drm_display_mode *mode;
216 struct drm_display_mode *output_mode;
217 int n = 0;
218 int i;
219
220 if (tv_norm->kind != CTV_ENC_MODE) {
221 struct drm_display_mode *tv_mode;
222
223 for (tv_mode = nv17_tv_modes; tv_mode->hdisplay; tv_mode++) {
224 mode = drm_mode_duplicate(encoder->dev, tv_mode);
225
226 mode->clock = tv_norm->tv_enc_mode.vrefresh *
227 mode->htotal / 1000 *
228 mode->vtotal / 1000;
229
230 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
231 mode->clock *= 2;
232
233 if (mode->hdisplay == tv_norm->tv_enc_mode.hdisplay &&
234 mode->vdisplay == tv_norm->tv_enc_mode.vdisplay)
235 mode->type |= DRM_MODE_TYPE_PREFERRED;
236
237 drm_mode_probed_add(connector, mode);
238 n++;
239 }
240 return n;
241 }
242
243 /* tv_norm->kind == CTV_ENC_MODE */
244 output_mode = &tv_norm->ctv_enc_mode.mode;
245 for (i = 0; i < ARRAY_SIZE(modes); i++) {
246 if (modes[i].hdisplay > output_mode->hdisplay ||
247 modes[i].vdisplay > output_mode->vdisplay)
248 continue;
249
250 if (modes[i].hdisplay == output_mode->hdisplay &&
251 modes[i].vdisplay == output_mode->vdisplay) {
252 mode = drm_mode_duplicate(encoder->dev, output_mode);
253 mode->type |= DRM_MODE_TYPE_PREFERRED;
254 } else {
255 mode = drm_cvt_mode(encoder->dev, modes[i].hdisplay,
256 modes[i].vdisplay, 60, false,
257 output_mode->flags & DRM_MODE_FLAG_INTERLACE,
258 false);
259 }
260
261 /* CVT modes are sometimes unsuitable... */
262 if (output_mode->hdisplay <= 720
263 || output_mode->hdisplay >= 1920) {
264 mode->htotal = output_mode->htotal;
265 mode->hsync_start = (mode->hdisplay + (mode->htotal
266 - mode->hdisplay) * 9 / 10) & ~7;
267 mode->hsync_end = mode->hsync_start + 8;
268 }
269 if (output_mode->vdisplay >= 1024) {
270 mode->vtotal = output_mode->vtotal;
271 mode->vsync_start = output_mode->vsync_start;
272 mode->vsync_end = output_mode->vsync_end;
273 }
274
275 mode->type |= DRM_MODE_TYPE_DRIVER;
276 drm_mode_probed_add(connector, mode);
277 n++;
278 }
279 return n;
280}
281
282static int nv17_tv_mode_valid(struct drm_encoder *encoder,
283 struct drm_display_mode *mode)
284{
285 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
286
287 if (tv_norm->kind == CTV_ENC_MODE) {
288 struct drm_display_mode *output_mode =
289 &tv_norm->ctv_enc_mode.mode;
290
291 if (mode->clock > 400000)
292 return MODE_CLOCK_HIGH;
293
294 if (mode->hdisplay > output_mode->hdisplay ||
295 mode->vdisplay > output_mode->vdisplay)
296 return MODE_BAD;
297
298 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) !=
299 (output_mode->flags & DRM_MODE_FLAG_INTERLACE))
300 return MODE_NO_INTERLACE;
301
302 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
303 return MODE_NO_DBLESCAN;
304
305 } else {
306 const int vsync_tolerance = 600;
307
308 if (mode->clock > 70000)
309 return MODE_CLOCK_HIGH;
310
311 if (abs(drm_mode_vrefresh(mode) * 1000 -
312 tv_norm->tv_enc_mode.vrefresh) > vsync_tolerance)
313 return MODE_VSYNC;
314
315 /* The encoder takes care of the actual interlacing */
316 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
317 return MODE_NO_INTERLACE;
318 }
319
320 return MODE_OK;
321}
322
323static bool nv17_tv_mode_fixup(struct drm_encoder *encoder,
324 struct drm_display_mode *mode,
325 struct drm_display_mode *adjusted_mode)
326{
327 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
328
Francisco Jerez8ccfe9e2010-07-04 16:14:42 +0200329 if (nv04_dac_in_use(encoder))
330 return false;
331
Ben Skeggs6ee73862009-12-11 19:24:15 +1000332 if (tv_norm->kind == CTV_ENC_MODE)
333 adjusted_mode->clock = tv_norm->ctv_enc_mode.mode.clock;
334 else
335 adjusted_mode->clock = 90000;
336
337 return true;
338}
339
340static void nv17_tv_dpms(struct drm_encoder *encoder, int mode)
341{
342 struct drm_device *dev = encoder->dev;
Ben Skeggsee2e0132010-07-26 09:28:25 +1000343 struct drm_nouveau_private *dev_priv = dev->dev_private;
344 struct nouveau_gpio_engine *gpio = &dev_priv->engine.gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000345 struct nv17_tv_state *regs = &to_tv_enc(encoder)->state;
346 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
347
348 if (nouveau_encoder(encoder)->last_dpms == mode)
349 return;
350 nouveau_encoder(encoder)->last_dpms = mode;
351
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100352 NV_INFO(dev, "Setting dpms mode %d on TV encoder (output %d)\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353 mode, nouveau_encoder(encoder)->dcb->index);
354
355 regs->ptv_200 &= ~1;
356
357 if (tv_norm->kind == CTV_ENC_MODE) {
358 nv04_dfp_update_fp_control(encoder, mode);
359
360 } else {
361 nv04_dfp_update_fp_control(encoder, DRM_MODE_DPMS_OFF);
362
363 if (mode == DRM_MODE_DPMS_ON)
364 regs->ptv_200 |= 1;
365 }
366
367 nv_load_ptv(dev, regs, 200);
368
Ben Skeggsee2e0132010-07-26 09:28:25 +1000369 gpio->set(dev, DCB_GPIO_TVDAC1, mode == DRM_MODE_DPMS_ON);
370 gpio->set(dev, DCB_GPIO_TVDAC0, mode == DRM_MODE_DPMS_ON);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000371
372 nv04_dac_update_dacclk(encoder, mode == DRM_MODE_DPMS_ON);
373}
374
375static void nv17_tv_prepare(struct drm_encoder *encoder)
376{
377 struct drm_device *dev = encoder->dev;
378 struct drm_nouveau_private *dev_priv = dev->dev_private;
379 struct drm_encoder_helper_funcs *helper = encoder->helper_private;
380 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
381 int head = nouveau_crtc(encoder->crtc)->index;
382 uint8_t *cr_lcd = &dev_priv->mode_reg.crtc_reg[head].CRTC[
383 NV_CIO_CRE_LCD__INDEX];
384 uint32_t dacclk_off = NV_PRAMDAC_DACCLK +
385 nv04_dac_output_offset(encoder);
386 uint32_t dacclk;
387
388 helper->dpms(encoder, DRM_MODE_DPMS_OFF);
389
390 nv04_dfp_disable(dev, head);
391
392 /* Unbind any FP encoders from this head if we need the FP
393 * stuff enabled. */
394 if (tv_norm->kind == CTV_ENC_MODE) {
395 struct drm_encoder *enc;
396
397 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
398 struct dcb_entry *dcb = nouveau_encoder(enc)->dcb;
399
400 if ((dcb->type == OUTPUT_TMDS ||
401 dcb->type == OUTPUT_LVDS) &&
402 !enc->crtc &&
403 nv04_dfp_get_bound_head(dev, dcb) == head) {
404 nv04_dfp_bind_head(dev, dcb, head ^ 1,
Ben Skeggs04a39c52010-02-24 10:03:05 +1000405 dev_priv->vbios.fp.dual_link);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000406 }
407 }
408
409 }
410
Francisco Jerez217275d2010-08-30 19:55:52 +0200411 if (tv_norm->kind == CTV_ENC_MODE)
412 *cr_lcd = 0x1 | (head ? 0x0 : 0x8);
413 else
414 *cr_lcd = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000415
416 /* Set the DACCLK register */
417 dacclk = (NVReadRAMDAC(dev, 0, dacclk_off) & ~0x30) | 0x1;
418
419 if (dev_priv->card_type == NV_40)
420 dacclk |= 0x1a << 16;
421
422 if (tv_norm->kind == CTV_ENC_MODE) {
423 dacclk |= 0x20;
424
425 if (head)
426 dacclk |= 0x100;
427 else
428 dacclk &= ~0x100;
429
430 } else {
431 dacclk |= 0x10;
432
433 }
434
435 NVWriteRAMDAC(dev, 0, dacclk_off, dacclk);
436}
437
438static void nv17_tv_mode_set(struct drm_encoder *encoder,
439 struct drm_display_mode *drm_mode,
440 struct drm_display_mode *adjusted_mode)
441{
442 struct drm_device *dev = encoder->dev;
443 struct drm_nouveau_private *dev_priv = dev->dev_private;
444 int head = nouveau_crtc(encoder->crtc)->index;
445 struct nv04_crtc_reg *regs = &dev_priv->mode_reg.crtc_reg[head];
446 struct nv17_tv_state *tv_regs = &to_tv_enc(encoder)->state;
447 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
448 int i;
449
450 regs->CRTC[NV_CIO_CRE_53] = 0x40; /* FP_HTIMING */
451 regs->CRTC[NV_CIO_CRE_54] = 0; /* FP_VTIMING */
452 regs->ramdac_630 = 0x2; /* turn off green mode (tv test pattern?) */
453 regs->tv_setup = 1;
454 regs->ramdac_8c0 = 0x0;
455
456 if (tv_norm->kind == TV_ENC_MODE) {
457 tv_regs->ptv_200 = 0x13111100;
458 if (head)
459 tv_regs->ptv_200 |= 0x10;
460
461 tv_regs->ptv_20c = 0x808010;
462 tv_regs->ptv_304 = 0x2d00000;
463 tv_regs->ptv_600 = 0x0;
464 tv_regs->ptv_60c = 0x0;
465 tv_regs->ptv_610 = 0x1e00000;
466
467 if (tv_norm->tv_enc_mode.vdisplay == 576) {
468 tv_regs->ptv_508 = 0x1200000;
469 tv_regs->ptv_614 = 0x33;
470
471 } else if (tv_norm->tv_enc_mode.vdisplay == 480) {
472 tv_regs->ptv_508 = 0xf00000;
473 tv_regs->ptv_614 = 0x13;
474 }
475
476 if (dev_priv->card_type >= NV_30) {
477 tv_regs->ptv_500 = 0xe8e0;
478 tv_regs->ptv_504 = 0x1710;
479 tv_regs->ptv_604 = 0x0;
480 tv_regs->ptv_608 = 0x0;
481 } else {
482 if (tv_norm->tv_enc_mode.vdisplay == 576) {
483 tv_regs->ptv_604 = 0x20;
484 tv_regs->ptv_608 = 0x10;
485 tv_regs->ptv_500 = 0x19710;
486 tv_regs->ptv_504 = 0x68f0;
487
488 } else if (tv_norm->tv_enc_mode.vdisplay == 480) {
489 tv_regs->ptv_604 = 0x10;
490 tv_regs->ptv_608 = 0x20;
491 tv_regs->ptv_500 = 0x4b90;
492 tv_regs->ptv_504 = 0x1b480;
493 }
494 }
495
496 for (i = 0; i < 0x40; i++)
497 tv_regs->tv_enc[i] = tv_norm->tv_enc_mode.tv_enc[i];
498
499 } else {
500 struct drm_display_mode *output_mode =
501 &tv_norm->ctv_enc_mode.mode;
502
503 /* The registers in PRAMDAC+0xc00 control some timings and CSC
504 * parameters for the CTV encoder (It's only used for "HD" TV
505 * modes, I don't think I have enough working to guess what
506 * they exactly mean...), it's probably connected at the
507 * output of the FP encoder, but it also needs the analog
508 * encoder in its OR enabled and routed to the head it's
509 * using. It's enabled with the DACCLK register, bits [5:4].
510 */
511 for (i = 0; i < 38; i++)
512 regs->ctv_regs[i] = tv_norm->ctv_enc_mode.ctv_regs[i];
513
514 regs->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
515 regs->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
516 regs->fp_horiz_regs[FP_SYNC_START] =
517 output_mode->hsync_start - 1;
518 regs->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
519 regs->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay +
520 max((output_mode->hdisplay-600)/40 - 1, 1);
521
522 regs->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
523 regs->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
524 regs->fp_vert_regs[FP_SYNC_START] =
525 output_mode->vsync_start - 1;
526 regs->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
527 regs->fp_vert_regs[FP_CRTC] = output_mode->vdisplay - 1;
528
529 regs->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
530 NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
531 NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
532
533 if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
534 regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
535 if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
536 regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
537
538 regs->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
539 NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
540 NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
541 NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
542 NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
543 NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
544 NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
545
546 regs->fp_debug_2 = 0;
547
548 regs->fp_margin_color = 0x801080;
549
550 }
551}
552
553static void nv17_tv_commit(struct drm_encoder *encoder)
554{
555 struct drm_device *dev = encoder->dev;
556 struct drm_nouveau_private *dev_priv = dev->dev_private;
557 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
558 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
559 struct drm_encoder_helper_funcs *helper = encoder->helper_private;
560
561 if (get_tv_norm(encoder)->kind == TV_ENC_MODE) {
562 nv17_tv_update_rescaler(encoder);
563 nv17_tv_update_properties(encoder);
564 } else {
565 nv17_ctv_update_rescaler(encoder);
566 }
567
568 nv17_tv_state_load(dev, &to_tv_enc(encoder)->state);
569
570 /* This could use refinement for flatpanels, but it should work */
571 if (dev_priv->chipset < 0x44)
572 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
573 nv04_dac_output_offset(encoder),
574 0xf0000000);
575 else
576 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
577 nv04_dac_output_offset(encoder),
578 0x00100000);
579
580 helper->dpms(encoder, DRM_MODE_DPMS_ON);
581
582 NV_INFO(dev, "Output %s is running on CRTC %d using output %c\n",
583 drm_get_connector_name(
584 &nouveau_encoder_connector_get(nv_encoder)->base),
585 nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
586}
587
588static void nv17_tv_save(struct drm_encoder *encoder)
589{
590 struct drm_device *dev = encoder->dev;
591 struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
592
593 nouveau_encoder(encoder)->restore.output =
594 NVReadRAMDAC(dev, 0,
595 NV_PRAMDAC_DACCLK +
596 nv04_dac_output_offset(encoder));
597
598 nv17_tv_state_save(dev, &tv_enc->saved_state);
599
600 tv_enc->state.ptv_200 = tv_enc->saved_state.ptv_200;
601}
602
603static void nv17_tv_restore(struct drm_encoder *encoder)
604{
605 struct drm_device *dev = encoder->dev;
606
607 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK +
608 nv04_dac_output_offset(encoder),
609 nouveau_encoder(encoder)->restore.output);
610
611 nv17_tv_state_load(dev, &to_tv_enc(encoder)->saved_state);
Francisco Jerezbf929ef2010-02-11 12:47:40 +0100612
613 nouveau_encoder(encoder)->last_dpms = NV_DPMS_CLEARED;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000614}
615
616static int nv17_tv_create_resources(struct drm_encoder *encoder,
617 struct drm_connector *connector)
618{
619 struct drm_device *dev = encoder->dev;
620 struct drm_mode_config *conf = &dev->mode_config;
621 struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
622 struct dcb_entry *dcb = nouveau_encoder(encoder)->dcb;
623 int num_tv_norms = dcb->tvconf.has_component_output ? NUM_TV_NORMS :
624 NUM_LD_TV_NORMS;
625 int i;
626
627 if (nouveau_tv_norm) {
628 for (i = 0; i < num_tv_norms; i++) {
629 if (!strcmp(nv17_tv_norm_names[i], nouveau_tv_norm)) {
630 tv_enc->tv_norm = i;
631 break;
632 }
633 }
634
635 if (i == num_tv_norms)
636 NV_WARN(dev, "Invalid TV norm setting \"%s\"\n",
637 nouveau_tv_norm);
638 }
639
640 drm_mode_create_tv_properties(dev, num_tv_norms, nv17_tv_norm_names);
641
642 drm_connector_attach_property(connector,
643 conf->tv_select_subconnector_property,
644 tv_enc->select_subconnector);
645 drm_connector_attach_property(connector,
646 conf->tv_subconnector_property,
647 tv_enc->subconnector);
648 drm_connector_attach_property(connector,
649 conf->tv_mode_property,
650 tv_enc->tv_norm);
651 drm_connector_attach_property(connector,
652 conf->tv_flicker_reduction_property,
653 tv_enc->flicker);
654 drm_connector_attach_property(connector,
655 conf->tv_saturation_property,
656 tv_enc->saturation);
657 drm_connector_attach_property(connector,
658 conf->tv_hue_property,
659 tv_enc->hue);
660 drm_connector_attach_property(connector,
661 conf->tv_overscan_property,
662 tv_enc->overscan);
663
664 return 0;
665}
666
667static int nv17_tv_set_property(struct drm_encoder *encoder,
668 struct drm_connector *connector,
669 struct drm_property *property,
670 uint64_t val)
671{
672 struct drm_mode_config *conf = &encoder->dev->mode_config;
673 struct drm_crtc *crtc = encoder->crtc;
674 struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
675 struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
676 bool modes_changed = false;
677
678 if (property == conf->tv_overscan_property) {
679 tv_enc->overscan = val;
680 if (encoder->crtc) {
681 if (tv_norm->kind == CTV_ENC_MODE)
682 nv17_ctv_update_rescaler(encoder);
683 else
684 nv17_tv_update_rescaler(encoder);
685 }
686
687 } else if (property == conf->tv_saturation_property) {
688 if (tv_norm->kind != TV_ENC_MODE)
689 return -EINVAL;
690
691 tv_enc->saturation = val;
692 nv17_tv_update_properties(encoder);
693
694 } else if (property == conf->tv_hue_property) {
695 if (tv_norm->kind != TV_ENC_MODE)
696 return -EINVAL;
697
698 tv_enc->hue = val;
699 nv17_tv_update_properties(encoder);
700
701 } else if (property == conf->tv_flicker_reduction_property) {
702 if (tv_norm->kind != TV_ENC_MODE)
703 return -EINVAL;
704
705 tv_enc->flicker = val;
706 if (encoder->crtc)
707 nv17_tv_update_rescaler(encoder);
708
709 } else if (property == conf->tv_mode_property) {
710 if (connector->dpms != DRM_MODE_DPMS_OFF)
711 return -EINVAL;
712
713 tv_enc->tv_norm = val;
714
715 modes_changed = true;
716
717 } else if (property == conf->tv_select_subconnector_property) {
718 if (tv_norm->kind != TV_ENC_MODE)
719 return -EINVAL;
720
721 tv_enc->select_subconnector = val;
722 nv17_tv_update_properties(encoder);
723
724 } else {
725 return -EINVAL;
726 }
727
728 if (modes_changed) {
729 drm_helper_probe_single_connector_modes(connector, 0, 0);
730
731 /* Disable the crtc to ensure a full modeset is
732 * performed whenever it's turned on again. */
733 if (crtc) {
734 struct drm_mode_set modeset = {
735 .crtc = crtc,
736 };
737
738 crtc->funcs->set_config(&modeset);
739 }
740 }
741
742 return 0;
743}
744
745static void nv17_tv_destroy(struct drm_encoder *encoder)
746{
747 struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
748
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100749 NV_DEBUG_KMS(encoder->dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000750
751 drm_encoder_cleanup(encoder);
752 kfree(tv_enc);
753}
754
755static struct drm_encoder_helper_funcs nv17_tv_helper_funcs = {
756 .dpms = nv17_tv_dpms,
757 .save = nv17_tv_save,
758 .restore = nv17_tv_restore,
759 .mode_fixup = nv17_tv_mode_fixup,
760 .prepare = nv17_tv_prepare,
761 .commit = nv17_tv_commit,
762 .mode_set = nv17_tv_mode_set,
Francisco Jerez11d6eb22009-12-17 18:52:44 +0100763 .detect = nv17_tv_detect,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764};
765
766static struct drm_encoder_slave_funcs nv17_tv_slave_funcs = {
767 .get_modes = nv17_tv_get_modes,
768 .mode_valid = nv17_tv_mode_valid,
769 .create_resources = nv17_tv_create_resources,
770 .set_property = nv17_tv_set_property,
771};
772
773static struct drm_encoder_funcs nv17_tv_funcs = {
774 .destroy = nv17_tv_destroy,
775};
776
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000777int
778nv17_tv_create(struct drm_connector *connector, struct dcb_entry *entry)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779{
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000780 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000781 struct drm_encoder *encoder;
782 struct nv17_tv_encoder *tv_enc = NULL;
783
784 tv_enc = kzalloc(sizeof(*tv_enc), GFP_KERNEL);
785 if (!tv_enc)
786 return -ENOMEM;
787
788 tv_enc->overscan = 50;
789 tv_enc->flicker = 50;
790 tv_enc->saturation = 50;
791 tv_enc->hue = 0;
792 tv_enc->tv_norm = TV_NORM_PAL;
793 tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
794 tv_enc->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
795 tv_enc->pin_mask = 0;
796
797 encoder = to_drm_encoder(&tv_enc->base);
798
799 tv_enc->base.dcb = entry;
800 tv_enc->base.or = ffs(entry->or) - 1;
801
802 drm_encoder_init(dev, encoder, &nv17_tv_funcs, DRM_MODE_ENCODER_TVDAC);
803 drm_encoder_helper_add(encoder, &nv17_tv_helper_funcs);
804 to_encoder_slave(encoder)->slave_funcs = &nv17_tv_slave_funcs;
805
806 encoder->possible_crtcs = entry->heads;
807 encoder->possible_clones = 0;
808
Ben Skeggs8f1a6082010-06-28 14:35:50 +1000809 nv17_tv_create_resources(encoder, connector);
810 drm_mode_connector_attach_encoder(connector, encoder);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811 return 0;
812}