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