intel_tv.c 47.8 KB
Newer Older
J
Jesse Barnes 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright © 2006-2008 Intel Corporation
 *   Jesse Barnes <jesse.barnes@intel.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

/** @file
 * Integrated TV-out support for the 915GM and 945GM.
 */

33 34 35
#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_edid.h>
J
Jesse Barnes 已提交
36
#include "intel_drv.h"
37
#include <drm/i915_drm.h>
J
Jesse Barnes 已提交
38 39 40 41 42 43 44 45
#include "i915_drv.h"

enum tv_margin {
	TV_MARGIN_LEFT, TV_MARGIN_TOP,
	TV_MARGIN_RIGHT, TV_MARGIN_BOTTOM
};

/** Private structure for the integrated TV support */
C
Chris Wilson 已提交
46 47 48
struct intel_tv {
	struct intel_encoder base;

J
Jesse Barnes 已提交
49
	int type;
50
	const char *tv_format;
J
Jesse Barnes 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	int margin[4];
	u32 save_TV_H_CTL_1;
	u32 save_TV_H_CTL_2;
	u32 save_TV_H_CTL_3;
	u32 save_TV_V_CTL_1;
	u32 save_TV_V_CTL_2;
	u32 save_TV_V_CTL_3;
	u32 save_TV_V_CTL_4;
	u32 save_TV_V_CTL_5;
	u32 save_TV_V_CTL_6;
	u32 save_TV_V_CTL_7;
	u32 save_TV_SC_CTL_1, save_TV_SC_CTL_2, save_TV_SC_CTL_3;

	u32 save_TV_CSC_Y;
	u32 save_TV_CSC_Y2;
	u32 save_TV_CSC_U;
	u32 save_TV_CSC_U2;
	u32 save_TV_CSC_V;
	u32 save_TV_CSC_V2;
	u32 save_TV_CLR_KNOBS;
	u32 save_TV_CLR_LEVEL;
	u32 save_TV_WIN_POS;
	u32 save_TV_WIN_SIZE;
	u32 save_TV_FILTER_CTL_1;
	u32 save_TV_FILTER_CTL_2;
	u32 save_TV_FILTER_CTL_3;

	u32 save_TV_H_LUMA[60];
	u32 save_TV_H_CHROMA[60];
	u32 save_TV_V_LUMA[43];
	u32 save_TV_V_CHROMA[43];

	u32 save_TV_DAC;
	u32 save_TV_CTL;
};

struct video_levels {
	int blank, black, burst;
};

struct color_conversion {
	u16 ry, gy, by, ay;
	u16 ru, gu, bu, au;
	u16 rv, gv, bv, av;
};

static const u32 filter_table[] = {
	0xB1403000, 0x2E203500, 0x35002E20, 0x3000B140,
	0x35A0B160, 0x2DC02E80, 0xB1403480, 0xB1603000,
	0x2EA03640, 0x34002D80, 0x3000B120, 0x36E0B160,
	0x2D202EF0, 0xB1203380, 0xB1603000, 0x2F303780,
	0x33002CC0, 0x3000B100, 0x3820B160, 0x2C802F50,
	0xB10032A0, 0xB1603000, 0x2F9038C0, 0x32202C20,
	0x3000B0E0, 0x3980B160, 0x2BC02FC0, 0xB0E031C0,
	0xB1603000, 0x2FF03A20, 0x31602B60, 0xB020B0C0,
	0x3AE0B160, 0x2B001810, 0xB0C03120, 0xB140B020,
	0x18283BA0, 0x30C02A80, 0xB020B0A0, 0x3C60B140,
	0x2A201838, 0xB0A03080, 0xB120B020, 0x18383D20,
	0x304029C0, 0xB040B080, 0x3DE0B100, 0x29601848,
	0xB0803000, 0xB100B040, 0x18483EC0, 0xB0402900,
	0xB040B060, 0x3F80B0C0, 0x28801858, 0xB060B080,
	0xB0A0B060, 0x18602820, 0xB0A02820, 0x0000B060,
	0xB1403000, 0x2E203500, 0x35002E20, 0x3000B140,
	0x35A0B160, 0x2DC02E80, 0xB1403480, 0xB1603000,
	0x2EA03640, 0x34002D80, 0x3000B120, 0x36E0B160,
	0x2D202EF0, 0xB1203380, 0xB1603000, 0x2F303780,
	0x33002CC0, 0x3000B100, 0x3820B160, 0x2C802F50,
	0xB10032A0, 0xB1603000, 0x2F9038C0, 0x32202C20,
	0x3000B0E0, 0x3980B160, 0x2BC02FC0, 0xB0E031C0,
	0xB1603000, 0x2FF03A20, 0x31602B60, 0xB020B0C0,
	0x3AE0B160, 0x2B001810, 0xB0C03120, 0xB140B020,
	0x18283BA0, 0x30C02A80, 0xB020B0A0, 0x3C60B140,
	0x2A201838, 0xB0A03080, 0xB120B020, 0x18383D20,
	0x304029C0, 0xB040B080, 0x3DE0B100, 0x29601848,
	0xB0803000, 0xB100B040, 0x18483EC0, 0xB0402900,
	0xB040B060, 0x3F80B0C0, 0x28801858, 0xB060B080,
	0xB0A0B060, 0x18602820, 0xB0A02820, 0x0000B060,
	0x36403000, 0x2D002CC0, 0x30003640, 0x2D0036C0,
	0x35C02CC0, 0x37403000, 0x2C802D40, 0x30003540,
	0x2D8037C0, 0x34C02C40, 0x38403000, 0x2BC02E00,
	0x30003440, 0x2E2038C0, 0x34002B80, 0x39803000,
	0x2B402E40, 0x30003380, 0x2E603A00, 0x33402B00,
	0x3A803040, 0x2A802EA0, 0x30403300, 0x2EC03B40,
	0x32802A40, 0x3C003040, 0x2A002EC0, 0x30803240,
	0x2EC03C80, 0x320029C0, 0x3D403080, 0x29402F00,
	0x308031C0, 0x2F203DC0, 0x31802900, 0x3E8030C0,
	0x28802F40, 0x30C03140, 0x2F203F40, 0x31402840,
	0x28003100, 0x28002F00, 0x00003100, 0x36403000,
	0x2D002CC0, 0x30003640, 0x2D0036C0,
	0x35C02CC0, 0x37403000, 0x2C802D40, 0x30003540,
	0x2D8037C0, 0x34C02C40, 0x38403000, 0x2BC02E00,
	0x30003440, 0x2E2038C0, 0x34002B80, 0x39803000,
	0x2B402E40, 0x30003380, 0x2E603A00, 0x33402B00,
	0x3A803040, 0x2A802EA0, 0x30403300, 0x2EC03B40,
	0x32802A40, 0x3C003040, 0x2A002EC0, 0x30803240,
	0x2EC03C80, 0x320029C0, 0x3D403080, 0x29402F00,
	0x308031C0, 0x2F203DC0, 0x31802900, 0x3E8030C0,
	0x28802F40, 0x30C03140, 0x2F203F40, 0x31402840,
	0x28003100, 0x28002F00, 0x00003100,
};

/*
 * Color conversion values have 3 separate fixed point formats:
 *
 * 10 bit fields (ay, au)
 *   1.9 fixed point (b.bbbbbbbbb)
 * 11 bit fields (ry, by, ru, gu, gv)
 *   exp.mantissa (ee.mmmmmmmmm)
 *   ee = 00 = 10^-1 (0.mmmmmmmmm)
 *   ee = 01 = 10^-2 (0.0mmmmmmmmm)
 *   ee = 10 = 10^-3 (0.00mmmmmmmmm)
 *   ee = 11 = 10^-4 (0.000mmmmmmmmm)
 * 12 bit fields (gy, rv, bu)
 *   exp.mantissa (eee.mmmmmmmmm)
 *   eee = 000 = 10^-1 (0.mmmmmmmmm)
 *   eee = 001 = 10^-2 (0.0mmmmmmmmm)
 *   eee = 010 = 10^-3 (0.00mmmmmmmmm)
 *   eee = 011 = 10^-4 (0.000mmmmmmmmm)
 *   eee = 100 = reserved
 *   eee = 101 = reserved
 *   eee = 110 = reserved
 *   eee = 111 = 10^0 (m.mmmmmmmm) (only usable for 1.0 representation)
 *
 * Saturation and contrast are 8 bits, with their own representation:
 * 8 bit field (saturation, contrast)
 *   exp.mantissa (ee.mmmmmm)
 *   ee = 00 = 10^-1 (0.mmmmmm)
 *   ee = 01 = 10^0 (m.mmmmm)
 *   ee = 10 = 10^1 (mm.mmmm)
 *   ee = 11 = 10^2 (mmm.mmm)
 *
 * Simple conversion function:
 *
 * static u32
 * float_to_csc_11(float f)
 * {
 *     u32 exp;
 *     u32 mant;
 *     u32 ret;
 *
 *     if (f < 0)
 *         f = -f;
 *
 *     if (f >= 1) {
 *         exp = 0x7;
196
 *	   mant = 1 << 8;
J
Jesse Barnes 已提交
197 198
 *     } else {
 *         for (exp = 0; exp < 3 && f < 0.5; exp++)
199
 *	   f *= 2.0;
J
Jesse Barnes 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
 *         mant = (f * (1 << 9) + 0.5);
 *         if (mant >= (1 << 9))
 *             mant = (1 << 9) - 1;
 *     }
 *     ret = (exp << 9) | mant;
 *     return ret;
 * }
 */

/*
 * Behold, magic numbers!  If we plant them they might grow a big
 * s-video cable to the sky... or something.
 *
 * Pre-converted to appropriate hex value.
 */

/*
 * PAL & NTSC values for composite & s-video connections
 */
static const struct color_conversion ntsc_m_csc_composite = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
221 222
	.ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
	.rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
J
Jesse Barnes 已提交
223 224 225 226 227 228 229
};

static const struct video_levels ntsc_m_levels_composite = {
	.blank = 225, .black = 267, .burst = 113,
};

static const struct color_conversion ntsc_m_csc_svideo = {
230 231 232
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
	.ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
	.rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
J
Jesse Barnes 已提交
233 234 235 236 237 238 239 240
};

static const struct video_levels ntsc_m_levels_svideo = {
	.blank = 266, .black = 316, .burst = 133,
};

static const struct color_conversion ntsc_j_csc_composite = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0119,
241 242
	.ru = 0x074c, .gu = 0x0546, .bu = 0x05ec, .au = 0x0200,
	.rv = 0x035a, .gv = 0x0322, .bv = 0x06e1, .av = 0x0200,
J
Jesse Barnes 已提交
243 244 245 246 247 248 249 250
};

static const struct video_levels ntsc_j_levels_composite = {
	.blank = 225, .black = 225, .burst = 113,
};

static const struct color_conversion ntsc_j_csc_svideo = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x014c,
251 252
	.ru = 0x0788, .gu = 0x0581, .bu = 0x0322, .au = 0x0200,
	.rv = 0x0399, .gv = 0x0356, .bv = 0x070a, .av = 0x0200,
J
Jesse Barnes 已提交
253 254 255 256 257 258 259 260
};

static const struct video_levels ntsc_j_levels_svideo = {
	.blank = 266, .black = 266, .burst = 133,
};

static const struct color_conversion pal_csc_composite = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0113,
261 262
	.ru = 0x0745, .gu = 0x053f, .bu = 0x05e1, .au = 0x0200,
	.rv = 0x0353, .gv = 0x031c, .bv = 0x06dc, .av = 0x0200,
J
Jesse Barnes 已提交
263 264 265 266 267 268 269 270
};

static const struct video_levels pal_levels_composite = {
	.blank = 237, .black = 237, .burst = 118,
};

static const struct color_conversion pal_csc_svideo = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0145,
271 272
	.ru = 0x0780, .gu = 0x0579, .bu = 0x031c, .au = 0x0200,
	.rv = 0x0390, .gv = 0x034f, .bv = 0x0705, .av = 0x0200,
J
Jesse Barnes 已提交
273 274 275 276 277 278 279 280
};

static const struct video_levels pal_levels_svideo = {
	.blank = 280, .black = 280, .burst = 139,
};

static const struct color_conversion pal_m_csc_composite = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
281 282
	.ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
	.rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
J
Jesse Barnes 已提交
283 284 285 286 287 288 289
};

static const struct video_levels pal_m_levels_composite = {
	.blank = 225, .black = 267, .burst = 113,
};

static const struct color_conversion pal_m_csc_svideo = {
290 291 292
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
	.ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
	.rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
J
Jesse Barnes 已提交
293 294 295 296 297 298 299 300
};

static const struct video_levels pal_m_levels_svideo = {
	.blank = 266, .black = 316, .burst = 133,
};

static const struct color_conversion pal_n_csc_composite = {
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
301 302
	.ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
	.rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
J
Jesse Barnes 已提交
303 304 305 306 307 308 309
};

static const struct video_levels pal_n_levels_composite = {
	.blank = 225, .black = 267, .burst = 118,
};

static const struct color_conversion pal_n_csc_svideo = {
310 311 312
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
	.ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
	.rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
J
Jesse Barnes 已提交
313 314 315 316 317 318 319 320 321 322
};

static const struct video_levels pal_n_levels_svideo = {
	.blank = 266, .black = 316, .burst = 139,
};

/*
 * Component connections
 */
static const struct color_conversion sdtv_csc_yprpb = {
323 324 325
	.ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0145,
	.ru = 0x0559, .gu = 0x0353, .bu = 0x0100, .au = 0x0200,
	.rv = 0x0100, .gv = 0x03ad, .bv = 0x074d, .av = 0x0200,
J
Jesse Barnes 已提交
326 327 328 329 330 331 332 333 334
};

static const struct color_conversion sdtv_csc_rgb = {
	.ry = 0x0000, .gy = 0x0f00, .by = 0x0000, .ay = 0x0166,
	.ru = 0x0000, .gu = 0x0000, .bu = 0x0f00, .au = 0x0166,
	.rv = 0x0f00, .gv = 0x0000, .bv = 0x0000, .av = 0x0166,
};

static const struct color_conversion hdtv_csc_yprpb = {
335 336 337
	.ry = 0x05b3, .gy = 0x016e, .by = 0x0728, .ay = 0x0145,
	.ru = 0x07d5, .gu = 0x038b, .bu = 0x0100, .au = 0x0200,
	.rv = 0x0100, .gv = 0x03d1, .bv = 0x06bc, .av = 0x0200,
J
Jesse Barnes 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351
};

static const struct color_conversion hdtv_csc_rgb = {
	.ry = 0x0000, .gy = 0x0f00, .by = 0x0000, .ay = 0x0166,
	.ru = 0x0000, .gu = 0x0000, .bu = 0x0f00, .au = 0x0166,
	.rv = 0x0f00, .gv = 0x0000, .bv = 0x0000, .av = 0x0166,
};

static const struct video_levels component_levels = {
	.blank = 279, .black = 279, .burst = 0,
};


struct tv_mode {
352
	const char *name;
J
Jesse Barnes 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	int clock;
	int refresh; /* in millihertz (for precision) */
	u32 oversample;
	int hsync_end, hblank_start, hblank_end, htotal;
	bool progressive, trilevel_sync, component_only;
	int vsync_start_f1, vsync_start_f2, vsync_len;
	bool veq_ena;
	int veq_start_f1, veq_start_f2, veq_len;
	int vi_end_f1, vi_end_f2, nbr_end;
	bool burst_ena;
	int hburst_start, hburst_len;
	int vburst_start_f1, vburst_end_f1;
	int vburst_start_f2, vburst_end_f2;
	int vburst_start_f3, vburst_end_f3;
	int vburst_start_f4, vburst_end_f4;
	/*
	 * subcarrier programming
	 */
	int dda2_size, dda3_size, dda1_inc, dda2_inc, dda3_inc;
	u32 sc_reset;
	bool pal_burst;
	/*
	 * blank/black levels
	 */
	const struct video_levels *composite_levels, *svideo_levels;
	const struct color_conversion *composite_color, *svideo_color;
	const u32 *filter_table;
	int max_srcw;
};


/*
 * Sub carrier DDA
 *
 *  I think this works as follows:
 *
 *  subcarrier freq = pixel_clock * (dda1_inc + dda2_inc / dda2_size) / 4096
 *
 * Presumably, when dda3 is added in, it gets to adjust the dda2_inc value
 *
 * So,
 *  dda1_ideal = subcarrier/pixel * 4096
 *  dda1_inc = floor (dda1_ideal)
 *  dda2 = dda1_ideal - dda1_inc
 *
 *  then pick a ratio for dda2 that gives the closest approximation. If
 *  you can't get close enough, you can play with dda3 as well. This
 *  seems likely to happen when dda2 is small as the jumps would be larger
 *
 * To invert this,
 *
 *  pixel_clock = subcarrier * 4096 / (dda1_inc + dda2_inc / dda2_size)
 *
 * The constants below were all computed using a 107.520MHz clock
 */

/**
 * Register programming values for TV modes.
 *
 * These values account for -1s required.
 */

415
static const struct tv_mode tv_modes[] = {
J
Jesse Barnes 已提交
416 417
	{
		.name		= "NTSC-M",
418
		.clock		= 108000,
419
		.refresh	= 59940,
J
Jesse Barnes 已提交
420 421 422 423 424 425 426 427 428 429 430 431
		.oversample	= TV_OVERSAMPLE_8X,
		.component_only = 0,
		/* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */

		.hsync_end	= 64,		    .hblank_end		= 124,
		.hblank_start	= 836,		    .htotal		= 857,

		.progressive	= false,	    .trilevel_sync = false,

		.vsync_start_f1	= 6,		    .vsync_start_f2	= 7,
		.vsync_len	= 6,

432
		.veq_ena	= true,		    .veq_start_f1	= 0,
J
Jesse Barnes 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445
		.veq_start_f2	= 1,		    .veq_len		= 18,

		.vi_end_f1	= 20,		    .vi_end_f2		= 21,
		.nbr_end	= 240,

		.burst_ena	= true,
		.hburst_start	= 72,		    .hburst_len		= 34,
		.vburst_start_f1 = 9,		    .vburst_end_f1	= 240,
		.vburst_start_f2 = 10,		    .vburst_end_f2	= 240,
		.vburst_start_f3 = 9,		    .vburst_end_f3	= 240,
		.vburst_start_f4 = 10,		    .vburst_end_f4	= 240,

		/* desired 3.5800000 actual 3.5800000 clock 107.52 */
446 447
		.dda1_inc	=    135,
		.dda2_inc	=  20800,	    .dda2_size		=  27456,
J
Jesse Barnes 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460
		.dda3_inc	=      0,	    .dda3_size		=      0,
		.sc_reset	= TV_SC_RESET_EVERY_4,
		.pal_burst	= false,

		.composite_levels = &ntsc_m_levels_composite,
		.composite_color = &ntsc_m_csc_composite,
		.svideo_levels  = &ntsc_m_levels_svideo,
		.svideo_color = &ntsc_m_csc_svideo,

		.filter_table = filter_table,
	},
	{
		.name		= "NTSC-443",
461
		.clock		= 108000,
462
		.refresh	= 59940,
J
Jesse Barnes 已提交
463 464 465 466 467 468 469 470 471 472 473
		.oversample	= TV_OVERSAMPLE_8X,
		.component_only = 0,
		/* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 4.43MHz */
		.hsync_end	= 64,		    .hblank_end		= 124,
		.hblank_start	= 836,		    .htotal		= 857,

		.progressive	= false,	    .trilevel_sync = false,

		.vsync_start_f1 = 6,		    .vsync_start_f2	= 7,
		.vsync_len	= 6,

474
		.veq_ena	= true,		    .veq_start_f1	= 0,
J
Jesse Barnes 已提交
475 476 477 478 479
		.veq_start_f2	= 1,		    .veq_len		= 18,

		.vi_end_f1	= 20,		    .vi_end_f2		= 21,
		.nbr_end	= 240,

480
		.burst_ena	= true,
J
Jesse Barnes 已提交
481 482 483 484 485 486 487 488
		.hburst_start	= 72,		    .hburst_len		= 34,
		.vburst_start_f1 = 9,		    .vburst_end_f1	= 240,
		.vburst_start_f2 = 10,		    .vburst_end_f2	= 240,
		.vburst_start_f3 = 9,		    .vburst_end_f3	= 240,
		.vburst_start_f4 = 10,		    .vburst_end_f4	= 240,

		/* desired 4.4336180 actual 4.4336180 clock 107.52 */
		.dda1_inc       =    168,
489 490 491 492
		.dda2_inc       =   4093,       .dda2_size      =  27456,
		.dda3_inc       =    310,       .dda3_size      =    525,
		.sc_reset   = TV_SC_RESET_NEVER,
		.pal_burst  = false,
J
Jesse Barnes 已提交
493 494 495 496 497 498 499 500 501 502

		.composite_levels = &ntsc_m_levels_composite,
		.composite_color = &ntsc_m_csc_composite,
		.svideo_levels  = &ntsc_m_levels_svideo,
		.svideo_color = &ntsc_m_csc_svideo,

		.filter_table = filter_table,
	},
	{
		.name		= "NTSC-J",
503
		.clock		= 108000,
504
		.refresh	= 59940,
J
Jesse Barnes 已提交
505 506 507 508 509 510 511 512 513 514 515 516
		.oversample	= TV_OVERSAMPLE_8X,
		.component_only = 0,

		/* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
		.hsync_end	= 64,		    .hblank_end		= 124,
		.hblank_start = 836,	    .htotal		= 857,

		.progressive	= false,    .trilevel_sync = false,

		.vsync_start_f1	= 6,	    .vsync_start_f2	= 7,
		.vsync_len	= 6,

517
		.veq_ena      = true,	    .veq_start_f1	= 0,
J
Jesse Barnes 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530
		.veq_start_f2 = 1,	    .veq_len		= 18,

		.vi_end_f1	= 20,		    .vi_end_f2		= 21,
		.nbr_end	= 240,

		.burst_ena	= true,
		.hburst_start	= 72,		    .hburst_len		= 34,
		.vburst_start_f1 = 9,		    .vburst_end_f1	= 240,
		.vburst_start_f2 = 10,		    .vburst_end_f2	= 240,
		.vburst_start_f3 = 9,		    .vburst_end_f3	= 240,
		.vburst_start_f4 = 10,		    .vburst_end_f4	= 240,

		/* desired 3.5800000 actual 3.5800000 clock 107.52 */
531 532
		.dda1_inc	=    135,
		.dda2_inc	=  20800,	    .dda2_size		=  27456,
J
Jesse Barnes 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545
		.dda3_inc	=      0,	    .dda3_size		=      0,
		.sc_reset	= TV_SC_RESET_EVERY_4,
		.pal_burst	= false,

		.composite_levels = &ntsc_j_levels_composite,
		.composite_color = &ntsc_j_csc_composite,
		.svideo_levels  = &ntsc_j_levels_svideo,
		.svideo_color = &ntsc_j_csc_svideo,

		.filter_table = filter_table,
	},
	{
		.name		= "PAL-M",
546
		.clock		= 108000,
547
		.refresh	= 59940,
J
Jesse Barnes 已提交
548 549 550 551 552 553 554 555 556 557 558 559
		.oversample	= TV_OVERSAMPLE_8X,
		.component_only = 0,

		/* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
		.hsync_end	= 64,		  .hblank_end		= 124,
		.hblank_start = 836,	  .htotal		= 857,

		.progressive	= false,	    .trilevel_sync = false,

		.vsync_start_f1	= 6,		    .vsync_start_f2	= 7,
		.vsync_len	= 6,

560
		.veq_ena	= true,		    .veq_start_f1	= 0,
J
Jesse Barnes 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573
		.veq_start_f2	= 1,		    .veq_len		= 18,

		.vi_end_f1	= 20,		    .vi_end_f2		= 21,
		.nbr_end	= 240,

		.burst_ena	= true,
		.hburst_start	= 72,		    .hburst_len		= 34,
		.vburst_start_f1 = 9,		    .vburst_end_f1	= 240,
		.vburst_start_f2 = 10,		    .vburst_end_f2	= 240,
		.vburst_start_f3 = 9,		    .vburst_end_f3	= 240,
		.vburst_start_f4 = 10,		    .vburst_end_f4	= 240,

		/* desired 3.5800000 actual 3.5800000 clock 107.52 */
574 575
		.dda1_inc	=    135,
		.dda2_inc	=  16704,	    .dda2_size		=  27456,
J
Jesse Barnes 已提交
576
		.dda3_inc	=      0,	    .dda3_size		=      0,
577 578
		.sc_reset	= TV_SC_RESET_EVERY_8,
		.pal_burst  = true,
J
Jesse Barnes 已提交
579 580 581 582 583 584 585 586 587 588 589

		.composite_levels = &pal_m_levels_composite,
		.composite_color = &pal_m_csc_composite,
		.svideo_levels  = &pal_m_levels_svideo,
		.svideo_color = &pal_m_csc_svideo,

		.filter_table = filter_table,
	},
	{
		/* 625 Lines, 50 Fields, 15.625KHz line, Sub-Carrier 4.434MHz */
		.name	    = "PAL-N",
590
		.clock		= 108000,
591
		.refresh	= 50000,
J
Jesse Barnes 已提交
592 593 594 595 596 597 598 599 600 601 602 603
		.oversample	= TV_OVERSAMPLE_8X,
		.component_only = 0,

		.hsync_end	= 64,		    .hblank_end		= 128,
		.hblank_start = 844,	    .htotal		= 863,

		.progressive  = false,    .trilevel_sync = false,


		.vsync_start_f1	= 6,	   .vsync_start_f2	= 7,
		.vsync_len	= 6,

604
		.veq_ena	= true,		    .veq_start_f1	= 0,
J
Jesse Barnes 已提交
605 606 607 608 609 610
		.veq_start_f2	= 1,		    .veq_len		= 18,

		.vi_end_f1	= 24,		    .vi_end_f2		= 25,
		.nbr_end	= 286,

		.burst_ena	= true,
611
		.hburst_start = 73,	    .hburst_len		= 34,
J
Jesse Barnes 已提交
612 613 614 615 616 617 618
		.vburst_start_f1 = 8,	    .vburst_end_f1	= 285,
		.vburst_start_f2 = 8,	    .vburst_end_f2	= 286,
		.vburst_start_f3 = 9,	    .vburst_end_f3	= 286,
		.vburst_start_f4 = 9,	    .vburst_end_f4	= 285,


		/* desired 4.4336180 actual 4.4336180 clock 107.52 */
619 620 621
		.dda1_inc       =    135,
		.dda2_inc       =  23578,       .dda2_size      =  27648,
		.dda3_inc       =    134,       .dda3_size      =    625,
J
Jesse Barnes 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634
		.sc_reset   = TV_SC_RESET_EVERY_8,
		.pal_burst  = true,

		.composite_levels = &pal_n_levels_composite,
		.composite_color = &pal_n_csc_composite,
		.svideo_levels  = &pal_n_levels_svideo,
		.svideo_color = &pal_n_csc_svideo,

		.filter_table = filter_table,
	},
	{
		/* 625 Lines, 50 Fields, 15.625KHz line, Sub-Carrier 4.434MHz */
		.name	    = "PAL",
635
		.clock		= 108000,
636
		.refresh	= 50000,
J
Jesse Barnes 已提交
637 638 639
		.oversample	= TV_OVERSAMPLE_8X,
		.component_only = 0,

640
		.hsync_end	= 64,		    .hblank_end		= 142,
J
Jesse Barnes 已提交
641 642 643 644 645 646 647
		.hblank_start	= 844,	    .htotal		= 863,

		.progressive	= false,    .trilevel_sync = false,

		.vsync_start_f1	= 5,	    .vsync_start_f2	= 6,
		.vsync_len	= 5,

648
		.veq_ena	= true,	    .veq_start_f1	= 0,
J
Jesse Barnes 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662
		.veq_start_f2	= 1,	    .veq_len		= 15,

		.vi_end_f1	= 24,		    .vi_end_f2		= 25,
		.nbr_end	= 286,

		.burst_ena	= true,
		.hburst_start	= 73,		    .hburst_len		= 32,
		.vburst_start_f1 = 8,		    .vburst_end_f1	= 285,
		.vburst_start_f2 = 8,		    .vburst_end_f2	= 286,
		.vburst_start_f3 = 9,		    .vburst_end_f3	= 286,
		.vburst_start_f4 = 9,		    .vburst_end_f4	= 285,

		/* desired 4.4336180 actual 4.4336180 clock 107.52 */
		.dda1_inc       =    168,
663 664
		.dda2_inc       =   4122,       .dda2_size      =  27648,
		.dda3_inc       =     67,       .dda3_size      =    625,
J
Jesse Barnes 已提交
665 666 667 668 669 670 671 672 673 674
		.sc_reset   = TV_SC_RESET_EVERY_8,
		.pal_burst  = true,

		.composite_levels = &pal_levels_composite,
		.composite_color = &pal_csc_composite,
		.svideo_levels  = &pal_levels_svideo,
		.svideo_color = &pal_csc_svideo,

		.filter_table = filter_table,
	},
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	{
		.name       = "480p",
		.clock		= 107520,
		.refresh	= 59940,
		.oversample     = TV_OVERSAMPLE_4X,
		.component_only = 1,

		.hsync_end      = 64,               .hblank_end         = 122,
		.hblank_start   = 842,              .htotal             = 857,

		.progressive    = true,		    .trilevel_sync = false,

		.vsync_start_f1 = 12,               .vsync_start_f2     = 12,
		.vsync_len      = 12,

		.veq_ena        = false,

		.vi_end_f1      = 44,               .vi_end_f2          = 44,
		.nbr_end        = 479,

		.burst_ena      = false,

		.filter_table = filter_table,
	},
	{
		.name       = "576p",
		.clock		= 107520,
		.refresh	= 50000,
		.oversample     = TV_OVERSAMPLE_4X,
		.component_only = 1,

		.hsync_end      = 64,               .hblank_end         = 139,
		.hblank_start   = 859,              .htotal             = 863,

		.progressive    = true,		    .trilevel_sync = false,

		.vsync_start_f1 = 10,               .vsync_start_f2     = 10,
		.vsync_len      = 10,

		.veq_ena        = false,

		.vi_end_f1      = 48,               .vi_end_f2          = 48,
		.nbr_end        = 575,

		.burst_ena      = false,

		.filter_table = filter_table,
	},
J
Jesse Barnes 已提交
723 724 725 726 727 728 729 730 731 732
	{
		.name       = "720p@60Hz",
		.clock		= 148800,
		.refresh	= 60000,
		.oversample     = TV_OVERSAMPLE_2X,
		.component_only = 1,

		.hsync_end      = 80,               .hblank_end         = 300,
		.hblank_start   = 1580,             .htotal             = 1649,

733
		.progressive	= true,		    .trilevel_sync = true,
J
Jesse Barnes 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

		.vsync_start_f1 = 10,               .vsync_start_f2     = 10,
		.vsync_len      = 10,

		.veq_ena        = false,

		.vi_end_f1      = 29,               .vi_end_f2          = 29,
		.nbr_end        = 719,

		.burst_ena      = false,

		.filter_table = filter_table,
	},
	{
		.name       = "720p@50Hz",
		.clock		= 148800,
		.refresh	= 50000,
		.oversample     = TV_OVERSAMPLE_2X,
		.component_only = 1,

		.hsync_end      = 80,               .hblank_end         = 300,
		.hblank_start   = 1580,             .htotal             = 1979,

757
		.progressive	= true,		    .trilevel_sync = true,
J
Jesse Barnes 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

		.vsync_start_f1 = 10,               .vsync_start_f2     = 10,
		.vsync_len      = 10,

		.veq_ena        = false,

		.vi_end_f1      = 29,               .vi_end_f2          = 29,
		.nbr_end        = 719,

		.burst_ena      = false,

		.filter_table = filter_table,
		.max_srcw = 800
	},
	{
		.name       = "1080i@50Hz",
		.clock		= 148800,
775
		.refresh	= 50000,
J
Jesse Barnes 已提交
776 777 778 779 780 781
		.oversample     = TV_OVERSAMPLE_2X,
		.component_only = 1,

		.hsync_end      = 88,               .hblank_end         = 235,
		.hblank_start   = 2155,             .htotal             = 2639,

782
		.progressive	= false,	  .trilevel_sync = true,
J
Jesse Barnes 已提交
783 784 785 786

		.vsync_start_f1 = 4,              .vsync_start_f2     = 5,
		.vsync_len      = 10,

787
		.veq_ena	= true,	    .veq_start_f1	= 4,
J
Jesse Barnes 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800
		.veq_start_f2   = 4,	    .veq_len		= 10,


		.vi_end_f1      = 21,           .vi_end_f2          = 22,
		.nbr_end        = 539,

		.burst_ena      = false,

		.filter_table = filter_table,
	},
	{
		.name       = "1080i@60Hz",
		.clock		= 148800,
801
		.refresh	= 60000,
J
Jesse Barnes 已提交
802 803 804 805 806 807
		.oversample     = TV_OVERSAMPLE_2X,
		.component_only = 1,

		.hsync_end      = 88,               .hblank_end         = 235,
		.hblank_start   = 2155,             .htotal             = 2199,

808
		.progressive	= false,	    .trilevel_sync = true,
J
Jesse Barnes 已提交
809 810 811 812

		.vsync_start_f1 = 4,               .vsync_start_f2     = 5,
		.vsync_len      = 10,

813
		.veq_ena	= true,		    .veq_start_f1	= 4,
J
Jesse Barnes 已提交
814 815 816 817 818 819 820 821 822 823 824 825
		.veq_start_f2	= 4,		    .veq_len		= 10,


		.vi_end_f1      = 21,               .vi_end_f2          = 22,
		.nbr_end        = 539,

		.burst_ena      = false,

		.filter_table = filter_table,
	},
};

826
static struct intel_tv *enc_to_tv(struct intel_encoder *encoder)
C
Chris Wilson 已提交
827
{
828
	return container_of(encoder, struct intel_tv, base);
C
Chris Wilson 已提交
829 830
}

831 832
static struct intel_tv *intel_attached_tv(struct drm_connector *connector)
{
833
	return enc_to_tv(intel_attached_encoder(connector));
834 835
}

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
static bool
intel_tv_get_hw_state(struct intel_encoder *encoder, enum pipe *pipe)
{
	struct drm_device *dev = encoder->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
	u32 tmp = I915_READ(TV_CTL);

	if (!(tmp & TV_ENC_ENABLE))
		return false;

	*pipe = PORT_TO_PIPE(tmp);

	return true;
}

J
Jesse Barnes 已提交
851
static void
852
intel_enable_tv(struct intel_encoder *encoder)
J
Jesse Barnes 已提交
853
{
854
	struct drm_device *dev = encoder->base.dev;
J
Jesse Barnes 已提交
855 856
	struct drm_i915_private *dev_priv = dev->dev_private;

857 858 859 860 861 862 863 864 865 866
	I915_WRITE(TV_CTL, I915_READ(TV_CTL) | TV_ENC_ENABLE);
}

static void
intel_disable_tv(struct intel_encoder *encoder)
{
	struct drm_device *dev = encoder->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;

	I915_WRITE(TV_CTL, I915_READ(TV_CTL) & ~TV_ENC_ENABLE);
J
Jesse Barnes 已提交
867 868 869
}

static const struct tv_mode *
870
intel_tv_mode_lookup(const char *tv_format)
J
Jesse Barnes 已提交
871 872 873
{
	int i;

874
	for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
J
Jesse Barnes 已提交
875 876 877 878 879 880 881 882 883
		const struct tv_mode *tv_mode = &tv_modes[i];

		if (!strcmp(tv_format, tv_mode->name))
			return tv_mode;
	}
	return NULL;
}

static const struct tv_mode *
884
intel_tv_mode_find(struct intel_tv *intel_tv)
J
Jesse Barnes 已提交
885
{
C
Chris Wilson 已提交
886
	return intel_tv_mode_lookup(intel_tv->tv_format);
J
Jesse Barnes 已提交
887 888 889
}

static enum drm_mode_status
890 891
intel_tv_mode_valid(struct drm_connector *connector,
		    struct drm_display_mode *mode)
J
Jesse Barnes 已提交
892
{
893
	struct intel_tv *intel_tv = intel_attached_tv(connector);
C
Chris Wilson 已提交
894
	const struct tv_mode *tv_mode = intel_tv_mode_find(intel_tv);
J
Jesse Barnes 已提交
895 896

	/* Ensure TV refresh is close to desired refresh */
897 898
	if (tv_mode && abs(tv_mode->refresh - drm_mode_vrefresh(mode) * 1000)
				< 1000)
J
Jesse Barnes 已提交
899
		return MODE_OK;
900

J
Jesse Barnes 已提交
901 902 903 904
	return MODE_CLOCK_RANGE;
}


905 906 907 908 909 910 911
static void
intel_tv_get_config(struct intel_encoder *encoder,
		    struct intel_crtc_config *pipe_config)
{
	pipe_config->adjusted_mode.crtc_clock = pipe_config->port_clock;
}

J
Jesse Barnes 已提交
912
static bool
913 914
intel_tv_compute_config(struct intel_encoder *encoder,
			struct intel_crtc_config *pipe_config)
J
Jesse Barnes 已提交
915
{
916
	struct intel_tv *intel_tv = enc_to_tv(encoder);
C
Chris Wilson 已提交
917
	const struct tv_mode *tv_mode = intel_tv_mode_find(intel_tv);
J
Jesse Barnes 已提交
918 919 920 921

	if (!tv_mode)
		return false;

922
	pipe_config->adjusted_mode.crtc_clock = tv_mode->clock;
923 924 925
	DRM_DEBUG_KMS("forcing bpc to 8 for TV\n");
	pipe_config->pipe_bpp = 8*3;

926 927 928 929 930 931 932 933
	/* TV has it's own notion of sync and other mode flags, so clear them. */
	pipe_config->adjusted_mode.flags = 0;

	/*
	 * FIXME: We don't check whether the input mode is actually what we want
	 * or whether userspace is doing something stupid.
	 */

J
Jesse Barnes 已提交
934 935 936
	return true;
}

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
static void
set_tv_mode_timings(struct drm_i915_private *dev_priv,
		    const struct tv_mode *tv_mode,
		    bool burst_ena)
{
	u32 hctl1, hctl2, hctl3;
	u32 vctl1, vctl2, vctl3, vctl4, vctl5, vctl6, vctl7;

	hctl1 = (tv_mode->hsync_end << TV_HSYNC_END_SHIFT) |
		(tv_mode->htotal << TV_HTOTAL_SHIFT);

	hctl2 = (tv_mode->hburst_start << 16) |
		(tv_mode->hburst_len << TV_HBURST_LEN_SHIFT);

	if (burst_ena)
		hctl2 |= TV_BURST_ENA;

	hctl3 = (tv_mode->hblank_start << TV_HBLANK_START_SHIFT) |
		(tv_mode->hblank_end << TV_HBLANK_END_SHIFT);

	vctl1 = (tv_mode->nbr_end << TV_NBR_END_SHIFT) |
		(tv_mode->vi_end_f1 << TV_VI_END_F1_SHIFT) |
		(tv_mode->vi_end_f2 << TV_VI_END_F2_SHIFT);

	vctl2 = (tv_mode->vsync_len << TV_VSYNC_LEN_SHIFT) |
		(tv_mode->vsync_start_f1 << TV_VSYNC_START_F1_SHIFT) |
		(tv_mode->vsync_start_f2 << TV_VSYNC_START_F2_SHIFT);

	vctl3 = (tv_mode->veq_len << TV_VEQ_LEN_SHIFT) |
		(tv_mode->veq_start_f1 << TV_VEQ_START_F1_SHIFT) |
		(tv_mode->veq_start_f2 << TV_VEQ_START_F2_SHIFT);

	if (tv_mode->veq_ena)
		vctl3 |= TV_EQUAL_ENA;

	vctl4 = (tv_mode->vburst_start_f1 << TV_VBURST_START_F1_SHIFT) |
		(tv_mode->vburst_end_f1 << TV_VBURST_END_F1_SHIFT);

	vctl5 = (tv_mode->vburst_start_f2 << TV_VBURST_START_F2_SHIFT) |
		(tv_mode->vburst_end_f2 << TV_VBURST_END_F2_SHIFT);

	vctl6 = (tv_mode->vburst_start_f3 << TV_VBURST_START_F3_SHIFT) |
		(tv_mode->vburst_end_f3 << TV_VBURST_END_F3_SHIFT);

	vctl7 = (tv_mode->vburst_start_f4 << TV_VBURST_START_F4_SHIFT) |
		(tv_mode->vburst_end_f4 << TV_VBURST_END_F4_SHIFT);

	I915_WRITE(TV_H_CTL_1, hctl1);
	I915_WRITE(TV_H_CTL_2, hctl2);
	I915_WRITE(TV_H_CTL_3, hctl3);
	I915_WRITE(TV_V_CTL_1, vctl1);
	I915_WRITE(TV_V_CTL_2, vctl2);
	I915_WRITE(TV_V_CTL_3, vctl3);
	I915_WRITE(TV_V_CTL_4, vctl4);
	I915_WRITE(TV_V_CTL_5, vctl5);
	I915_WRITE(TV_V_CTL_6, vctl6);
	I915_WRITE(TV_V_CTL_7, vctl7);
}

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
static void set_color_conversion(struct drm_i915_private *dev_priv,
				 const struct color_conversion *color_conversion)
{
	if (!color_conversion)
		return;

	I915_WRITE(TV_CSC_Y, (color_conversion->ry << 16) |
		   color_conversion->gy);
	I915_WRITE(TV_CSC_Y2, (color_conversion->by << 16) |
		   color_conversion->ay);
	I915_WRITE(TV_CSC_U, (color_conversion->ru << 16) |
		   color_conversion->gu);
	I915_WRITE(TV_CSC_U2, (color_conversion->bu << 16) |
		   color_conversion->au);
	I915_WRITE(TV_CSC_V, (color_conversion->rv << 16) |
		   color_conversion->gv);
	I915_WRITE(TV_CSC_V2, (color_conversion->bv << 16) |
		   color_conversion->av);
}

1016
static void intel_tv_pre_enable(struct intel_encoder *encoder)
J
Jesse Barnes 已提交
1017
{
1018
	struct drm_device *dev = encoder->base.dev;
J
Jesse Barnes 已提交
1019
	struct drm_i915_private *dev_priv = dev->dev_private;
1020 1021
	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
	struct intel_tv *intel_tv = enc_to_tv(encoder);
C
Chris Wilson 已提交
1022
	const struct tv_mode *tv_mode = intel_tv_mode_find(intel_tv);
J
Jesse Barnes 已提交
1023 1024 1025 1026 1027 1028
	u32 tv_ctl;
	u32 scctl1, scctl2, scctl3;
	int i, j;
	const struct video_levels *video_levels;
	const struct color_conversion *color_conversion;
	bool burst_ena;
1029 1030
	int xpos = 0x0, ypos = 0x0;
	unsigned int xsize, ysize;
J
Jesse Barnes 已提交
1031 1032 1033 1034

	if (!tv_mode)
		return;	/* can't happen (mode_prepare prevents this) */

1035 1036
	tv_ctl = I915_READ(TV_CTL);
	tv_ctl &= TV_CTL_SAVE;
J
Jesse Barnes 已提交
1037

C
Chris Wilson 已提交
1038
	switch (intel_tv->type) {
J
Jesse Barnes 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	default:
	case DRM_MODE_CONNECTOR_Unknown:
	case DRM_MODE_CONNECTOR_Composite:
		tv_ctl |= TV_ENC_OUTPUT_COMPOSITE;
		video_levels = tv_mode->composite_levels;
		color_conversion = tv_mode->composite_color;
		burst_ena = tv_mode->burst_ena;
		break;
	case DRM_MODE_CONNECTOR_Component:
		tv_ctl |= TV_ENC_OUTPUT_COMPONENT;
		video_levels = &component_levels;
		if (tv_mode->burst_ena)
			color_conversion = &sdtv_csc_yprpb;
		else
			color_conversion = &hdtv_csc_yprpb;
		burst_ena = false;
		break;
	case DRM_MODE_CONNECTOR_SVIDEO:
		tv_ctl |= TV_ENC_OUTPUT_SVIDEO;
		video_levels = tv_mode->svideo_levels;
		color_conversion = tv_mode->svideo_color;
		burst_ena = tv_mode->burst_ena;
		break;
	}

	if (intel_crtc->pipe == 1)
		tv_ctl |= TV_ENC_PIPEB_SELECT;
	tv_ctl |= tv_mode->oversample;

	if (tv_mode->progressive)
		tv_ctl |= TV_PROGRESSIVE;
	if (tv_mode->trilevel_sync)
		tv_ctl |= TV_TRILEVEL_SYNC;
	if (tv_mode->pal_burst)
		tv_ctl |= TV_PAL_BURST;
1074

J
Jesse Barnes 已提交
1075
	scctl1 = 0;
1076
	if (tv_mode->dda1_inc)
J
Jesse Barnes 已提交
1077 1078 1079 1080 1081 1082
		scctl1 |= TV_SC_DDA1_EN;
	if (tv_mode->dda2_inc)
		scctl1 |= TV_SC_DDA2_EN;
	if (tv_mode->dda3_inc)
		scctl1 |= TV_SC_DDA3_EN;
	scctl1 |= tv_mode->sc_reset;
1083 1084
	if (video_levels)
		scctl1 |= video_levels->burst << TV_BURST_LEVEL_SHIFT;
J
Jesse Barnes 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093
	scctl1 |= tv_mode->dda1_inc << TV_SCDDA1_INC_SHIFT;

	scctl2 = tv_mode->dda2_size << TV_SCDDA2_SIZE_SHIFT |
		tv_mode->dda2_inc << TV_SCDDA2_INC_SHIFT;

	scctl3 = tv_mode->dda3_size << TV_SCDDA3_SIZE_SHIFT |
		tv_mode->dda3_inc << TV_SCDDA3_INC_SHIFT;

	/* Enable two fixes for the chips that need them. */
1094
	if (IS_I915GM(dev))
J
Jesse Barnes 已提交
1095 1096
		tv_ctl |= TV_ENC_C0_FIX | TV_ENC_SDP_FIX;

1097 1098
	set_tv_mode_timings(dev_priv, tv_mode, burst_ena);

J
Jesse Barnes 已提交
1099 1100 1101 1102
	I915_WRITE(TV_SC_CTL_1, scctl1);
	I915_WRITE(TV_SC_CTL_2, scctl2);
	I915_WRITE(TV_SC_CTL_3, scctl3);

1103
	set_color_conversion(dev_priv, color_conversion);
J
Jesse Barnes 已提交
1104

1105
	if (INTEL_INFO(dev)->gen >= 4)
1106 1107 1108 1109
		I915_WRITE(TV_CLR_KNOBS, 0x00404000);
	else
		I915_WRITE(TV_CLR_KNOBS, 0x00606000);

J
Jesse Barnes 已提交
1110 1111 1112 1113
	if (video_levels)
		I915_WRITE(TV_CLR_LEVEL,
			   ((video_levels->black << TV_BLACK_LEVEL_SHIFT) |
			    (video_levels->blank << TV_BLANK_LEVEL_SHIFT)));
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

	assert_pipe_disabled(dev_priv, intel_crtc->pipe);

	/* Filter ctl must be set before TV_WIN_SIZE */
	I915_WRITE(TV_FILTER_CTL_1, TV_AUTO_SCALE);
	xsize = tv_mode->hblank_start - tv_mode->hblank_end;
	if (tv_mode->progressive)
		ysize = tv_mode->nbr_end + 1;
	else
		ysize = 2*tv_mode->nbr_end + 1;

	xpos += intel_tv->margin[TV_MARGIN_LEFT];
	ypos += intel_tv->margin[TV_MARGIN_TOP];
	xsize -= (intel_tv->margin[TV_MARGIN_LEFT] +
		  intel_tv->margin[TV_MARGIN_RIGHT]);
	ysize -= (intel_tv->margin[TV_MARGIN_TOP] +
		  intel_tv->margin[TV_MARGIN_BOTTOM]);
	I915_WRITE(TV_WIN_POS, (xpos<<16)|ypos);
	I915_WRITE(TV_WIN_SIZE, (xsize<<16)|ysize);
J
Jesse Barnes 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

	j = 0;
	for (i = 0; i < 60; i++)
		I915_WRITE(TV_H_LUMA_0 + (i<<2), tv_mode->filter_table[j++]);
	for (i = 0; i < 60; i++)
		I915_WRITE(TV_H_CHROMA_0 + (i<<2), tv_mode->filter_table[j++]);
	for (i = 0; i < 43; i++)
		I915_WRITE(TV_V_LUMA_0 + (i<<2), tv_mode->filter_table[j++]);
	for (i = 0; i < 43; i++)
		I915_WRITE(TV_V_CHROMA_0 + (i<<2), tv_mode->filter_table[j++]);
1143
	I915_WRITE(TV_DAC, I915_READ(TV_DAC) & TV_DAC_SAVE);
J
Jesse Barnes 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	I915_WRITE(TV_CTL, tv_ctl);
}

static const struct drm_display_mode reported_modes[] = {
	{
		.name = "NTSC 480i",
		.clock = 107520,
		.hdisplay = 1280,
		.hsync_start = 1368,
		.hsync_end = 1496,
		.htotal = 1712,

		.vdisplay = 1024,
		.vsync_start = 1027,
		.vsync_end = 1034,
		.vtotal = 1104,
		.type = DRM_MODE_TYPE_DRIVER,
	},
};

/**
 * Detects TV presence by checking for load.
 *
 * Requires that the current pipe's DPLL is active.

 * \return true if TV is connected.
 * \return false if TV is disconnected.
 */
static int
1173
intel_tv_detect_type(struct intel_tv *intel_tv,
1174
		      struct drm_connector *connector)
J
Jesse Barnes 已提交
1175
{
1176
	struct drm_encoder *encoder = &intel_tv->base.base;
1177 1178
	struct drm_crtc *crtc = encoder->crtc;
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
J
Jesse Barnes 已提交
1179 1180 1181 1182 1183
	struct drm_device *dev = encoder->dev;
	struct drm_i915_private *dev_priv = dev->dev_private;
	unsigned long irqflags;
	u32 tv_ctl, save_tv_ctl;
	u32 tv_dac, save_tv_dac;
1184
	int type;
J
Jesse Barnes 已提交
1185 1186

	/* Disable TV interrupts around load detect or we'll recurse */
1187 1188 1189
	if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
		i915_disable_pipestat(dev_priv, 0,
1190 1191
				      PIPE_HOTPLUG_INTERRUPT_STATUS |
				      PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
1192 1193
		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
	}
J
Jesse Barnes 已提交
1194

1195 1196 1197 1198 1199
	save_tv_dac = tv_dac = I915_READ(TV_DAC);
	save_tv_ctl = tv_ctl = I915_READ(TV_CTL);

	/* Poll for TV detection */
	tv_ctl &= ~(TV_ENC_ENABLE | TV_TEST_MODE_MASK);
1200
	tv_ctl |= TV_TEST_MODE_MONITOR_DETECT;
1201 1202 1203 1204
	if (intel_crtc->pipe == 1)
		tv_ctl |= TV_ENC_PIPEB_SELECT;
	else
		tv_ctl &= ~TV_ENC_PIPEB_SELECT;
1205 1206

	tv_dac &= ~(TVDAC_SENSE_MASK | DAC_A_MASK | DAC_B_MASK | DAC_C_MASK);
1207 1208 1209 1210 1211 1212 1213 1214
	tv_dac |= (TVDAC_STATE_CHG_EN |
		   TVDAC_A_SENSE_CTL |
		   TVDAC_B_SENSE_CTL |
		   TVDAC_C_SENSE_CTL |
		   DAC_CTL_OVERRIDE |
		   DAC_A_0_7_V |
		   DAC_B_0_7_V |
		   DAC_C_0_7_V);
1215

1216 1217 1218 1219 1220 1221 1222 1223 1224

	/*
	 * The TV sense state should be cleared to zero on cantiga platform. Otherwise
	 * the TV is misdetected. This is hardware requirement.
	 */
	if (IS_GM45(dev))
		tv_dac &= ~(TVDAC_STATE_CHG_EN | TVDAC_A_SENSE_CTL |
			    TVDAC_B_SENSE_CTL | TVDAC_C_SENSE_CTL);

1225 1226
	I915_WRITE(TV_CTL, tv_ctl);
	I915_WRITE(TV_DAC, tv_dac);
1227 1228
	POSTING_READ(TV_DAC);

1229 1230 1231
	intel_wait_for_vblank(intel_tv->base.base.dev,
			      to_intel_crtc(intel_tv->base.base.crtc)->pipe);

1232
	type = -1;
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	tv_dac = I915_READ(TV_DAC);
	DRM_DEBUG_KMS("TV detected: %x, %x\n", tv_ctl, tv_dac);
	/*
	 *  A B C
	 *  0 1 1 Composite
	 *  1 0 X svideo
	 *  0 0 0 Component
	 */
	if ((tv_dac & TVDAC_SENSE_MASK) == (TVDAC_B_SENSE | TVDAC_C_SENSE)) {
		DRM_DEBUG_KMS("Detected Composite TV connection\n");
		type = DRM_MODE_CONNECTOR_Composite;
	} else if ((tv_dac & (TVDAC_A_SENSE|TVDAC_B_SENSE)) == TVDAC_A_SENSE) {
		DRM_DEBUG_KMS("Detected S-Video TV connection\n");
		type = DRM_MODE_CONNECTOR_SVIDEO;
	} else if ((tv_dac & TVDAC_SENSE_MASK) == 0) {
		DRM_DEBUG_KMS("Detected Component TV connection\n");
		type = DRM_MODE_CONNECTOR_Component;
	} else {
		DRM_DEBUG_KMS("Unrecognised TV connection\n");
		type = -1;
J
Jesse Barnes 已提交
1253 1254
	}

1255 1256
	I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
	I915_WRITE(TV_CTL, save_tv_ctl);
1257 1258 1259 1260 1261
	POSTING_READ(TV_CTL);

	/* For unknown reasons the hw barfs if we don't do this vblank wait. */
	intel_wait_for_vblank(intel_tv->base.base.dev,
			      to_intel_crtc(intel_tv->base.base.crtc)->pipe);
1262

J
Jesse Barnes 已提交
1263
	/* Restore interrupt config */
1264 1265 1266
	if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
		i915_enable_pipestat(dev_priv, 0,
1267 1268
				     PIPE_HOTPLUG_INTERRUPT_STATUS |
				     PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
1269 1270
		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
	}
J
Jesse Barnes 已提交
1271 1272 1273 1274

	return type;
}

1275 1276 1277 1278 1279 1280
/*
 * Here we set accurate tv format according to connector type
 * i.e Component TV should not be assigned by NTSC or PAL
 */
static void intel_tv_find_better_format(struct drm_connector *connector)
{
1281
	struct intel_tv *intel_tv = intel_attached_tv(connector);
C
Chris Wilson 已提交
1282
	const struct tv_mode *tv_mode = intel_tv_mode_find(intel_tv);
1283 1284
	int i;

C
Chris Wilson 已提交
1285
	if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
1286 1287 1288 1289 1290 1291 1292
		tv_mode->component_only)
		return;


	for (i = 0; i < sizeof(tv_modes) / sizeof(*tv_modes); i++) {
		tv_mode = tv_modes + i;

C
Chris Wilson 已提交
1293
		if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
1294 1295 1296 1297
			tv_mode->component_only)
			break;
	}

C
Chris Wilson 已提交
1298
	intel_tv->tv_format = tv_mode->name;
1299
	drm_object_property_set_value(&connector->base,
1300 1301 1302
		connector->dev->mode_config.tv_mode_property, i);
}

J
Jesse Barnes 已提交
1303 1304 1305 1306 1307 1308 1309
/**
 * Detect the TV connection.
 *
 * Currently this always returns CONNECTOR_STATUS_UNKNOWN, as we need to be sure
 * we have a pipe programmed in order to probe the TV.
 */
static enum drm_connector_status
1310
intel_tv_detect(struct drm_connector *connector, bool force)
J
Jesse Barnes 已提交
1311 1312
{
	struct drm_display_mode mode;
1313
	struct intel_tv *intel_tv = intel_attached_tv(connector);
C
Chris Wilson 已提交
1314
	int type;
J
Jesse Barnes 已提交
1315

1316
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
1317
		      connector->base.id, connector->name,
1318 1319
		      force);

J
Jesse Barnes 已提交
1320 1321
	mode = reported_modes[0];

1322
	if (force) {
1323
		struct intel_load_detect_pipe tmp;
1324
		struct drm_modeset_acquire_ctx ctx;
C
Chris Wilson 已提交
1325

1326
		if (intel_get_load_detect_pipe(connector, &mode, &tmp, &ctx)) {
1327
			type = intel_tv_detect_type(intel_tv, connector);
1328
			intel_release_load_detect_pipe(connector, &tmp, &ctx);
J
Jesse Barnes 已提交
1329
		} else
1330 1331 1332
			return connector_status_unknown;
	} else
		return connector->status;
Z
Zhenyu Wang 已提交
1333

J
Jesse Barnes 已提交
1334 1335 1336
	if (type < 0)
		return connector_status_disconnected;

1337
	intel_tv->type = type;
1338
	intel_tv_find_better_format(connector);
1339

J
Jesse Barnes 已提交
1340 1341 1342
	return connector_status_connected;
}

1343 1344
static const struct input_res {
	const char *name;
J
Jesse Barnes 已提交
1345
	int w, h;
1346
} input_res_table[] = {
J
Jesse Barnes 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355
	{"640x480", 640, 480},
	{"800x600", 800, 600},
	{"1024x768", 1024, 768},
	{"1280x1024", 1280, 1024},
	{"848x480", 848, 480},
	{"1280x720", 1280, 720},
	{"1920x1080", 1920, 1080},
};

1356 1357 1358 1359 1360 1361 1362
/*
 * Chose preferred mode  according to line number of TV format
 */
static void
intel_tv_chose_preferred_modes(struct drm_connector *connector,
			       struct drm_display_mode *mode_ptr)
{
1363
	struct intel_tv *intel_tv = intel_attached_tv(connector);
C
Chris Wilson 已提交
1364
	const struct tv_mode *tv_mode = intel_tv_mode_find(intel_tv);
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

	if (tv_mode->nbr_end < 480 && mode_ptr->vdisplay == 480)
		mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
	else if (tv_mode->nbr_end > 480) {
		if (tv_mode->progressive == true && tv_mode->nbr_end < 720) {
			if (mode_ptr->vdisplay == 720)
				mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
		} else if (mode_ptr->vdisplay == 1080)
				mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
	}
}

J
Jesse Barnes 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
/**
 * Stub get_modes function.
 *
 * This should probably return a set of fixed modes, unless we can figure out
 * how to probe modes off of TV connections.
 */

static int
intel_tv_get_modes(struct drm_connector *connector)
{
	struct drm_display_mode *mode_ptr;
1388
	struct intel_tv *intel_tv = intel_attached_tv(connector);
C
Chris Wilson 已提交
1389
	const struct tv_mode *tv_mode = intel_tv_mode_find(intel_tv);
1390 1391
	int j, count = 0;
	u64 tmp;
J
Jesse Barnes 已提交
1392

K
Kulikov Vasiliy 已提交
1393
	for (j = 0; j < ARRAY_SIZE(input_res_table);
J
Jesse Barnes 已提交
1394
	     j++) {
1395
		const struct input_res *input = &input_res_table[j];
J
Jesse Barnes 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
		unsigned int hactive_s = input->w;
		unsigned int vactive_s = input->h;

		if (tv_mode->max_srcw && input->w > tv_mode->max_srcw)
			continue;

		if (input->w > 1024 && (!tv_mode->progressive
					&& !tv_mode->component_only))
			continue;

1406 1407 1408
		mode_ptr = drm_mode_create(connector->dev);
		if (!mode_ptr)
			continue;
J
Jesse Barnes 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
		strncpy(mode_ptr->name, input->name, DRM_DISPLAY_MODE_LEN);

		mode_ptr->hdisplay = hactive_s;
		mode_ptr->hsync_start = hactive_s + 1;
		mode_ptr->hsync_end = hactive_s + 64;
		if (mode_ptr->hsync_end <= mode_ptr->hsync_start)
			mode_ptr->hsync_end = mode_ptr->hsync_start + 1;
		mode_ptr->htotal = hactive_s + 96;

		mode_ptr->vdisplay = vactive_s;
		mode_ptr->vsync_start = vactive_s + 1;
		mode_ptr->vsync_end = vactive_s + 32;
		if (mode_ptr->vsync_end <= mode_ptr->vsync_start)
			mode_ptr->vsync_end = mode_ptr->vsync_start  + 1;
		mode_ptr->vtotal = vactive_s + 33;

1425 1426 1427 1428
		tmp = (u64) tv_mode->refresh * mode_ptr->vtotal;
		tmp *= mode_ptr->htotal;
		tmp = div_u64(tmp, 1000000);
		mode_ptr->clock = (int) tmp;
J
Jesse Barnes 已提交
1429 1430

		mode_ptr->type = DRM_MODE_TYPE_DRIVER;
1431
		intel_tv_chose_preferred_modes(connector, mode_ptr);
J
Jesse Barnes 已提交
1432
		drm_mode_probed_add(connector, mode_ptr);
1433
		count++;
J
Jesse Barnes 已提交
1434 1435
	}

1436
	return count;
J
Jesse Barnes 已提交
1437 1438 1439
}

static void
1440
intel_tv_destroy(struct drm_connector *connector)
J
Jesse Barnes 已提交
1441 1442
{
	drm_connector_cleanup(connector);
1443
	kfree(connector);
J
Jesse Barnes 已提交
1444 1445 1446 1447 1448 1449 1450 1451
}


static int
intel_tv_set_property(struct drm_connector *connector, struct drm_property *property,
		      uint64_t val)
{
	struct drm_device *dev = connector->dev;
1452 1453
	struct intel_tv *intel_tv = intel_attached_tv(connector);
	struct drm_crtc *crtc = intel_tv->base.base.crtc;
J
Jesse Barnes 已提交
1454
	int ret = 0;
1455
	bool changed = false;
J
Jesse Barnes 已提交
1456

1457
	ret = drm_object_property_set_value(&connector->base, property, val);
J
Jesse Barnes 已提交
1458 1459 1460
	if (ret < 0)
		goto out;

1461
	if (property == dev->mode_config.tv_left_margin_property &&
C
Chris Wilson 已提交
1462 1463
		intel_tv->margin[TV_MARGIN_LEFT] != val) {
		intel_tv->margin[TV_MARGIN_LEFT] = val;
1464 1465
		changed = true;
	} else if (property == dev->mode_config.tv_right_margin_property &&
C
Chris Wilson 已提交
1466 1467
		intel_tv->margin[TV_MARGIN_RIGHT] != val) {
		intel_tv->margin[TV_MARGIN_RIGHT] = val;
1468 1469
		changed = true;
	} else if (property == dev->mode_config.tv_top_margin_property &&
C
Chris Wilson 已提交
1470 1471
		intel_tv->margin[TV_MARGIN_TOP] != val) {
		intel_tv->margin[TV_MARGIN_TOP] = val;
1472 1473
		changed = true;
	} else if (property == dev->mode_config.tv_bottom_margin_property &&
C
Chris Wilson 已提交
1474 1475
		intel_tv->margin[TV_MARGIN_BOTTOM] != val) {
		intel_tv->margin[TV_MARGIN_BOTTOM] = val;
1476 1477
		changed = true;
	} else if (property == dev->mode_config.tv_mode_property) {
1478
		if (val >= ARRAY_SIZE(tv_modes)) {
J
Jesse Barnes 已提交
1479 1480 1481
			ret = -EINVAL;
			goto out;
		}
C
Chris Wilson 已提交
1482
		if (!strcmp(intel_tv->tv_format, tv_modes[val].name))
1483 1484
			goto out;

C
Chris Wilson 已提交
1485
		intel_tv->tv_format = tv_modes[val].name;
1486
		changed = true;
J
Jesse Barnes 已提交
1487 1488 1489 1490 1491
	} else {
		ret = -EINVAL;
		goto out;
	}

1492
	if (changed && crtc)
1493
		intel_crtc_restore_mode(crtc);
J
Jesse Barnes 已提交
1494 1495 1496 1497 1498
out:
	return ret;
}

static const struct drm_connector_funcs intel_tv_connector_funcs = {
1499
	.dpms = intel_connector_dpms,
J
Jesse Barnes 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508
	.detect = intel_tv_detect,
	.destroy = intel_tv_destroy,
	.set_property = intel_tv_set_property,
	.fill_modes = drm_helper_probe_single_connector_modes,
};

static const struct drm_connector_helper_funcs intel_tv_connector_helper_funcs = {
	.mode_valid = intel_tv_mode_valid,
	.get_modes = intel_tv_get_modes,
1509
	.best_encoder = intel_best_encoder,
J
Jesse Barnes 已提交
1510 1511 1512
};

static const struct drm_encoder_funcs intel_tv_enc_funcs = {
C
Chris Wilson 已提交
1513
	.destroy = intel_encoder_destroy,
J
Jesse Barnes 已提交
1514 1515
};

1516 1517 1518 1519 1520 1521 1522
/*
 * Enumerate the child dev array parsed from VBT to check whether
 * the integrated TV is present.
 * If it is present, return 1.
 * If it is not present, return false.
 * If no child dev is parsed from VBT, it assumes that the TV is present.
 */
1523
static int tv_is_present_in_vbt(struct drm_device *dev)
1524 1525
{
	struct drm_i915_private *dev_priv = dev->dev_private;
1526
	union child_device_config *p_child;
1527 1528
	int i, ret;

1529
	if (!dev_priv->vbt.child_dev_num)
1530 1531 1532
		return 1;

	ret = 0;
1533 1534
	for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
		p_child = dev_priv->vbt.child_dev + i;
1535 1536 1537
		/*
		 * If the device type is not TV, continue.
		 */
1538 1539 1540 1541 1542 1543
		switch (p_child->old.device_type) {
		case DEVICE_TYPE_INT_TV:
		case DEVICE_TYPE_TV:
		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
			break;
		default:
1544
			continue;
1545
		}
1546 1547 1548
		/* Only when the addin_offset is non-zero, it is regarded
		 * as present.
		 */
1549
		if (p_child->old.addin_offset) {
1550 1551 1552 1553 1554 1555
			ret = 1;
			break;
		}
	}
	return ret;
}
J
Jesse Barnes 已提交
1556 1557 1558 1559 1560 1561

void
intel_tv_init(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	struct drm_connector *connector;
C
Chris Wilson 已提交
1562
	struct intel_tv *intel_tv;
1563
	struct intel_encoder *intel_encoder;
1564
	struct intel_connector *intel_connector;
J
Jesse Barnes 已提交
1565
	u32 tv_dac_on, tv_dac_off, save_tv_dac;
1566
	char *tv_format_names[ARRAY_SIZE(tv_modes)];
J
Jesse Barnes 已提交
1567 1568 1569 1570 1571
	int i, initial_mode = 0;

	if ((I915_READ(TV_CTL) & TV_FUSE_STATE_MASK) == TV_FUSE_STATE_DISABLED)
		return;

1572 1573 1574 1575
	if (!tv_is_present_in_vbt(dev)) {
		DRM_DEBUG_KMS("Integrated TV is not present.\n");
		return;
	}
J
Jesse Barnes 已提交
1576
	/* Even if we have an encoder we may not have a connector */
1577
	if (!dev_priv->vbt.int_tv_support)
J
Jesse Barnes 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
		return;

	/*
	 * Sanity check the TV output by checking to see if the
	 * DAC register holds a value
	 */
	save_tv_dac = I915_READ(TV_DAC);

	I915_WRITE(TV_DAC, save_tv_dac | TVDAC_STATE_CHG_EN);
	tv_dac_on = I915_READ(TV_DAC);

	I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
	tv_dac_off = I915_READ(TV_DAC);

	I915_WRITE(TV_DAC, save_tv_dac);

	/*
	 * If the register does not hold the state change enable
	 * bit, (either as a 0 or a 1), assume it doesn't really
	 * exist
	 */
	if ((tv_dac_on & TVDAC_STATE_CHG_EN) == 0 ||
	    (tv_dac_off & TVDAC_STATE_CHG_EN) != 0)
		return;

1603
	intel_tv = kzalloc(sizeof(*intel_tv), GFP_KERNEL);
C
Chris Wilson 已提交
1604
	if (!intel_tv) {
J
Jesse Barnes 已提交
1605 1606
		return;
	}
1607

1608
	intel_connector = kzalloc(sizeof(*intel_connector), GFP_KERNEL);
1609
	if (!intel_connector) {
C
Chris Wilson 已提交
1610
		kfree(intel_tv);
1611 1612 1613
		return;
	}

C
Chris Wilson 已提交
1614
	intel_encoder = &intel_tv->base;
1615
	connector = &intel_connector->base;
J
Jesse Barnes 已提交
1616

1617 1618 1619 1620 1621 1622 1623 1624 1625
	/* The documentation, for the older chipsets at least, recommend
	 * using a polling method rather than hotplug detection for TVs.
	 * This is because in order to perform the hotplug detection, the PLLs
	 * for the TV must be kept alive increasing power drain and starving
	 * bandwidth from other encoders. Notably for instance, it causes
	 * pipe underruns on Crestline when this encoder is supposedly idle.
	 *
	 * More recent chipsets favour HDMI rather than integrated S-Video.
	 */
1626
	intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
1627

J
Jesse Barnes 已提交
1628 1629 1630
	drm_connector_init(dev, connector, &intel_tv_connector_funcs,
			   DRM_MODE_CONNECTOR_SVIDEO);

1631
	drm_encoder_init(dev, &intel_encoder->base, &intel_tv_enc_funcs,
J
Jesse Barnes 已提交
1632 1633
			 DRM_MODE_ENCODER_TVDAC);

1634
	intel_encoder->compute_config = intel_tv_compute_config;
1635
	intel_encoder->get_config = intel_tv_get_config;
1636
	intel_encoder->pre_enable = intel_tv_pre_enable;
1637 1638
	intel_encoder->enable = intel_enable_tv;
	intel_encoder->disable = intel_disable_tv;
1639 1640
	intel_encoder->get_hw_state = intel_tv_get_hw_state;
	intel_connector->get_hw_state = intel_connector_get_hw_state;
1641
	intel_connector->unregister = intel_connector_unregister;
1642

1643
	intel_connector_attach_encoder(intel_connector, intel_encoder);
1644 1645
	intel_encoder->type = INTEL_OUTPUT_TVOUT;
	intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
1646
	intel_encoder->cloneable = 0;
1647
	intel_encoder->base.possible_crtcs = ((1 << 0) | (1 << 1));
C
Chris Wilson 已提交
1648
	intel_tv->type = DRM_MODE_CONNECTOR_Unknown;
J
Jesse Barnes 已提交
1649 1650

	/* BIOS margin values */
C
Chris Wilson 已提交
1651 1652 1653 1654
	intel_tv->margin[TV_MARGIN_LEFT] = 54;
	intel_tv->margin[TV_MARGIN_TOP] = 36;
	intel_tv->margin[TV_MARGIN_RIGHT] = 46;
	intel_tv->margin[TV_MARGIN_BOTTOM] = 37;
J
Jesse Barnes 已提交
1655

1656
	intel_tv->tv_format = tv_modes[initial_mode].name;
J
Jesse Barnes 已提交
1657 1658 1659 1660 1661 1662

	drm_connector_helper_add(connector, &intel_tv_connector_helper_funcs);
	connector->interlace_allowed = false;
	connector->doublescan_allowed = false;

	/* Create TV properties then attach current values */
1663
	for (i = 0; i < ARRAY_SIZE(tv_modes); i++)
1664 1665 1666 1667
		tv_format_names[i] = (char *)tv_modes[i].name;
	drm_mode_create_tv_properties(dev,
				      ARRAY_SIZE(tv_modes),
				      tv_format_names);
J
Jesse Barnes 已提交
1668

1669
	drm_object_attach_property(&connector->base, dev->mode_config.tv_mode_property,
J
Jesse Barnes 已提交
1670
				   initial_mode);
1671
	drm_object_attach_property(&connector->base,
J
Jesse Barnes 已提交
1672
				   dev->mode_config.tv_left_margin_property,
C
Chris Wilson 已提交
1673
				   intel_tv->margin[TV_MARGIN_LEFT]);
1674
	drm_object_attach_property(&connector->base,
J
Jesse Barnes 已提交
1675
				   dev->mode_config.tv_top_margin_property,
C
Chris Wilson 已提交
1676
				   intel_tv->margin[TV_MARGIN_TOP]);
1677
	drm_object_attach_property(&connector->base,
J
Jesse Barnes 已提交
1678
				   dev->mode_config.tv_right_margin_property,
C
Chris Wilson 已提交
1679
				   intel_tv->margin[TV_MARGIN_RIGHT]);
1680
	drm_object_attach_property(&connector->base,
J
Jesse Barnes 已提交
1681
				   dev->mode_config.tv_bottom_margin_property,
C
Chris Wilson 已提交
1682
				   intel_tv->margin[TV_MARGIN_BOTTOM]);
1683
	drm_connector_register(connector);
J
Jesse Barnes 已提交
1684
}