venc.c 23.5 KB
Newer Older
T
Tomi Valkeinen 已提交
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 33
/*
 * Copyright (C) 2009 Nokia Corporation
 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
 *
 * VENC settings from TI's DSS driver
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define DSS_SUBSYS_NAME "VENC"

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/mutex.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/seq_file.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
34
#include <linux/pm_runtime.h>
T
Tomi Valkeinen 已提交
35
#include <linux/of.h>
36
#include <linux/of_graph.h>
T
Tomi Valkeinen 已提交
37
#include <linux/component.h>
38
#include <linux/sys_soc.h>
T
Tomi Valkeinen 已提交
39

40
#include "omapdss.h"
T
Tomi Valkeinen 已提交
41 42 43 44 45 46 47 48 49 50 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
#include "dss.h"

/* Venc registers */
#define VENC_REV_ID				0x00
#define VENC_STATUS				0x04
#define VENC_F_CONTROL				0x08
#define VENC_VIDOUT_CTRL			0x10
#define VENC_SYNC_CTRL				0x14
#define VENC_LLEN				0x1C
#define VENC_FLENS				0x20
#define VENC_HFLTR_CTRL				0x24
#define VENC_CC_CARR_WSS_CARR			0x28
#define VENC_C_PHASE				0x2C
#define VENC_GAIN_U				0x30
#define VENC_GAIN_V				0x34
#define VENC_GAIN_Y				0x38
#define VENC_BLACK_LEVEL			0x3C
#define VENC_BLANK_LEVEL			0x40
#define VENC_X_COLOR				0x44
#define VENC_M_CONTROL				0x48
#define VENC_BSTAMP_WSS_DATA			0x4C
#define VENC_S_CARR				0x50
#define VENC_LINE21				0x54
#define VENC_LN_SEL				0x58
#define VENC_L21__WC_CTL			0x5C
#define VENC_HTRIGGER_VTRIGGER			0x60
#define VENC_SAVID__EAVID			0x64
#define VENC_FLEN__FAL				0x68
#define VENC_LAL__PHASE_RESET			0x6C
#define VENC_HS_INT_START_STOP_X		0x70
#define VENC_HS_EXT_START_STOP_X		0x74
#define VENC_VS_INT_START_X			0x78
#define VENC_VS_INT_STOP_X__VS_INT_START_Y	0x7C
#define VENC_VS_INT_STOP_Y__VS_EXT_START_X	0x80
#define VENC_VS_EXT_STOP_X__VS_EXT_START_Y	0x84
#define VENC_VS_EXT_STOP_Y			0x88
#define VENC_AVID_START_STOP_X			0x90
#define VENC_AVID_START_STOP_Y			0x94
#define VENC_FID_INT_START_X__FID_INT_START_Y	0xA0
#define VENC_FID_INT_OFFSET_Y__FID_EXT_START_X	0xA4
#define VENC_FID_EXT_START_Y__FID_EXT_OFFSET_Y	0xA8
#define VENC_TVDETGP_INT_START_STOP_X		0xB0
#define VENC_TVDETGP_INT_START_STOP_Y		0xB4
#define VENC_GEN_CTRL				0xB8
#define VENC_OUTPUT_CONTROL			0xC4
#define VENC_OUTPUT_TEST			0xC8
#define VENC_DAC_B__DAC_C			0xC8

struct venc_config {
	u32 f_control;
	u32 vidout_ctrl;
	u32 sync_ctrl;
	u32 llen;
	u32 flens;
	u32 hfltr_ctrl;
	u32 cc_carr_wss_carr;
	u32 c_phase;
	u32 gain_u;
	u32 gain_v;
	u32 gain_y;
	u32 black_level;
	u32 blank_level;
	u32 x_color;
	u32 m_control;
	u32 bstamp_wss_data;
	u32 s_carr;
	u32 line21;
	u32 ln_sel;
	u32 l21__wc_ctl;
	u32 htrigger_vtrigger;
	u32 savid__eavid;
	u32 flen__fal;
	u32 lal__phase_reset;
	u32 hs_int_start_stop_x;
	u32 hs_ext_start_stop_x;
	u32 vs_int_start_x;
	u32 vs_int_stop_x__vs_int_start_y;
	u32 vs_int_stop_y__vs_ext_start_x;
	u32 vs_ext_stop_x__vs_ext_start_y;
	u32 vs_ext_stop_y;
	u32 avid_start_stop_x;
	u32 avid_start_stop_y;
	u32 fid_int_start_x__fid_int_start_y;
	u32 fid_int_offset_y__fid_ext_start_x;
	u32 fid_ext_start_y__fid_ext_offset_y;
	u32 tvdetgp_int_start_stop_x;
	u32 tvdetgp_int_start_stop_y;
	u32 gen_ctrl;
};

/* from TRM */
static const struct venc_config venc_config_pal_trm = {
	.f_control				= 0,
	.vidout_ctrl				= 1,
	.sync_ctrl				= 0x40,
	.llen					= 0x35F, /* 863 */
	.flens					= 0x270, /* 624 */
	.hfltr_ctrl				= 0,
	.cc_carr_wss_carr			= 0x2F7225ED,
	.c_phase				= 0,
	.gain_u					= 0x111,
	.gain_v					= 0x181,
	.gain_y					= 0x140,
	.black_level				= 0x3B,
	.blank_level				= 0x3B,
	.x_color				= 0x7,
	.m_control				= 0x2,
	.bstamp_wss_data			= 0x3F,
	.s_carr					= 0x2A098ACB,
	.line21					= 0,
	.ln_sel					= 0x01290015,
	.l21__wc_ctl				= 0x0000F603,
	.htrigger_vtrigger			= 0,

	.savid__eavid				= 0x06A70108,
	.flen__fal				= 0x00180270,
	.lal__phase_reset			= 0x00040135,
	.hs_int_start_stop_x			= 0x00880358,
	.hs_ext_start_stop_x			= 0x000F035F,
	.vs_int_start_x				= 0x01A70000,
	.vs_int_stop_x__vs_int_start_y		= 0x000001A7,
	.vs_int_stop_y__vs_ext_start_x		= 0x01AF0000,
	.vs_ext_stop_x__vs_ext_start_y		= 0x000101AF,
	.vs_ext_stop_y				= 0x00000025,
	.avid_start_stop_x			= 0x03530083,
	.avid_start_stop_y			= 0x026C002E,
	.fid_int_start_x__fid_int_start_y	= 0x0001008A,
	.fid_int_offset_y__fid_ext_start_x	= 0x002E0138,
	.fid_ext_start_y__fid_ext_offset_y	= 0x01380001,

	.tvdetgp_int_start_stop_x		= 0x00140001,
	.tvdetgp_int_start_stop_y		= 0x00010001,
	.gen_ctrl				= 0x00FF0000,
};

/* from TRM */
static const struct venc_config venc_config_ntsc_trm = {
	.f_control				= 0,
	.vidout_ctrl				= 1,
	.sync_ctrl				= 0x8040,
	.llen					= 0x359,
	.flens					= 0x20C,
	.hfltr_ctrl				= 0,
	.cc_carr_wss_carr			= 0x043F2631,
	.c_phase				= 0,
	.gain_u					= 0x102,
	.gain_v					= 0x16C,
	.gain_y					= 0x12F,
	.black_level				= 0x43,
	.blank_level				= 0x38,
	.x_color				= 0x7,
	.m_control				= 0x1,
	.bstamp_wss_data			= 0x38,
	.s_carr					= 0x21F07C1F,
	.line21					= 0,
	.ln_sel					= 0x01310011,
	.l21__wc_ctl				= 0x0000F003,
	.htrigger_vtrigger			= 0,

	.savid__eavid				= 0x069300F4,
	.flen__fal				= 0x0016020C,
	.lal__phase_reset			= 0x00060107,
	.hs_int_start_stop_x			= 0x008E0350,
	.hs_ext_start_stop_x			= 0x000F0359,
	.vs_int_start_x				= 0x01A00000,
	.vs_int_stop_x__vs_int_start_y		= 0x020701A0,
	.vs_int_stop_y__vs_ext_start_x		= 0x01AC0024,
	.vs_ext_stop_x__vs_ext_start_y		= 0x020D01AC,
	.vs_ext_stop_y				= 0x00000006,
	.avid_start_stop_x			= 0x03480078,
	.avid_start_stop_y			= 0x02060024,
	.fid_int_start_x__fid_int_start_y	= 0x0001008A,
	.fid_int_offset_y__fid_ext_start_x	= 0x01AC0106,
	.fid_ext_start_y__fid_ext_offset_y	= 0x01060006,

	.tvdetgp_int_start_stop_x		= 0x00140001,
	.tvdetgp_int_start_stop_y		= 0x00010001,
	.gen_ctrl				= 0x00F90000,
};

static const struct venc_config venc_config_pal_bdghi = {
	.f_control				= 0,
	.vidout_ctrl				= 0,
	.sync_ctrl				= 0,
	.hfltr_ctrl				= 0,
	.x_color				= 0,
	.line21					= 0,
	.ln_sel					= 21,
	.htrigger_vtrigger			= 0,
	.tvdetgp_int_start_stop_x		= 0x00140001,
	.tvdetgp_int_start_stop_y		= 0x00010001,
	.gen_ctrl				= 0x00FB0000,

	.llen					= 864-1,
	.flens					= 625-1,
	.cc_carr_wss_carr			= 0x2F7625ED,
	.c_phase				= 0xDF,
	.gain_u					= 0x111,
	.gain_v					= 0x181,
	.gain_y					= 0x140,
	.black_level				= 0x3e,
	.blank_level				= 0x3e,
	.m_control				= 0<<2 | 1<<1,
	.bstamp_wss_data			= 0x42,
	.s_carr					= 0x2a098acb,
	.l21__wc_ctl				= 0<<13 | 0x16<<8 | 0<<0,
	.savid__eavid				= 0x06A70108,
	.flen__fal				= 23<<16 | 624<<0,
	.lal__phase_reset			= 2<<17 | 310<<0,
	.hs_int_start_stop_x			= 0x00920358,
	.hs_ext_start_stop_x			= 0x000F035F,
	.vs_int_start_x				= 0x1a7<<16,
	.vs_int_stop_x__vs_int_start_y		= 0x000601A7,
	.vs_int_stop_y__vs_ext_start_x		= 0x01AF0036,
	.vs_ext_stop_x__vs_ext_start_y		= 0x27101af,
	.vs_ext_stop_y				= 0x05,
	.avid_start_stop_x			= 0x03530082,
	.avid_start_stop_y			= 0x0270002E,
	.fid_int_start_x__fid_int_start_y	= 0x0005008A,
	.fid_int_offset_y__fid_ext_start_x	= 0x002E0138,
	.fid_ext_start_y__fid_ext_offset_y	= 0x01380005,
};

264 265 266 267 268 269
enum venc_videomode {
	VENC_MODE_UNKNOWN,
	VENC_MODE_PAL,
	VENC_MODE_NTSC,
};

270
static const struct videomode omap_dss_pal_vm = {
271
	.hactive	= 720,
272
	.vactive	= 574,
273
	.pixelclock	= 13500000,
274
	.hsync_len	= 64,
275
	.hfront_porch	= 12,
276
	.hback_porch	= 68,
277
	.vsync_len	= 5,
278
	.vfront_porch	= 5,
279
	.vback_porch	= 41,
280

281
	.flags		= DISPLAY_FLAGS_INTERLACED | DISPLAY_FLAGS_HSYNC_LOW |
282
			  DISPLAY_FLAGS_VSYNC_LOW | DISPLAY_FLAGS_DE_HIGH |
283 284
			  DISPLAY_FLAGS_PIXDATA_POSEDGE |
			  DISPLAY_FLAGS_SYNC_NEGEDGE,
T
Tomi Valkeinen 已提交
285 286
};

287
static const struct videomode omap_dss_ntsc_vm = {
288
	.hactive	= 720,
289
	.vactive	= 482,
290
	.pixelclock	= 13500000,
291
	.hsync_len	= 64,
292
	.hfront_porch	= 16,
293
	.hback_porch	= 58,
294
	.vsync_len	= 6,
295
	.vfront_porch	= 6,
296
	.vback_porch	= 31,
297

298
	.flags		= DISPLAY_FLAGS_INTERLACED | DISPLAY_FLAGS_HSYNC_LOW |
299
			  DISPLAY_FLAGS_VSYNC_LOW | DISPLAY_FLAGS_DE_HIGH |
300 301
			  DISPLAY_FLAGS_PIXDATA_POSEDGE |
			  DISPLAY_FLAGS_SYNC_NEGEDGE,
T
Tomi Valkeinen 已提交
302 303
};

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static enum venc_videomode venc_get_videomode(const struct videomode *vm)
{
	if (!(vm->flags & DISPLAY_FLAGS_INTERLACED))
		return VENC_MODE_UNKNOWN;

	if (vm->pixelclock == omap_dss_pal_vm.pixelclock &&
	    vm->hactive == omap_dss_pal_vm.hactive &&
	    vm->vactive == omap_dss_pal_vm.vactive)
		return VENC_MODE_PAL;

	if (vm->pixelclock == omap_dss_ntsc_vm.pixelclock &&
	    vm->hactive == omap_dss_ntsc_vm.hactive &&
	    vm->vactive == omap_dss_ntsc_vm.vactive)
		return VENC_MODE_NTSC;

	return VENC_MODE_UNKNOWN;
}

T
Tomi Valkeinen 已提交
322
static struct {
323
	struct platform_device *pdev;
T
Tomi Valkeinen 已提交
324 325 326 327
	void __iomem *base;
	struct mutex venc_lock;
	u32 wss_data;
	struct regulator *vdda_dac_reg;
328 329

	struct clk	*tv_dac_clk;
330

331
	struct videomode vm;
332
	enum omap_dss_venc_type type;
333
	bool invert_polarity;
334
	bool requires_tv_dac_clk;
335

336
	struct omap_dss_device output;
T
Tomi Valkeinen 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 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 415 416 417
} venc;

static inline void venc_write_reg(int idx, u32 val)
{
	__raw_writel(val, venc.base + idx);
}

static inline u32 venc_read_reg(int idx)
{
	u32 l = __raw_readl(venc.base + idx);
	return l;
}

static void venc_write_config(const struct venc_config *config)
{
	DSSDBG("write venc conf\n");

	venc_write_reg(VENC_LLEN, config->llen);
	venc_write_reg(VENC_FLENS, config->flens);
	venc_write_reg(VENC_CC_CARR_WSS_CARR, config->cc_carr_wss_carr);
	venc_write_reg(VENC_C_PHASE, config->c_phase);
	venc_write_reg(VENC_GAIN_U, config->gain_u);
	venc_write_reg(VENC_GAIN_V, config->gain_v);
	venc_write_reg(VENC_GAIN_Y, config->gain_y);
	venc_write_reg(VENC_BLACK_LEVEL, config->black_level);
	venc_write_reg(VENC_BLANK_LEVEL, config->blank_level);
	venc_write_reg(VENC_M_CONTROL, config->m_control);
	venc_write_reg(VENC_BSTAMP_WSS_DATA, config->bstamp_wss_data |
			venc.wss_data);
	venc_write_reg(VENC_S_CARR, config->s_carr);
	venc_write_reg(VENC_L21__WC_CTL, config->l21__wc_ctl);
	venc_write_reg(VENC_SAVID__EAVID, config->savid__eavid);
	venc_write_reg(VENC_FLEN__FAL, config->flen__fal);
	venc_write_reg(VENC_LAL__PHASE_RESET, config->lal__phase_reset);
	venc_write_reg(VENC_HS_INT_START_STOP_X, config->hs_int_start_stop_x);
	venc_write_reg(VENC_HS_EXT_START_STOP_X, config->hs_ext_start_stop_x);
	venc_write_reg(VENC_VS_INT_START_X, config->vs_int_start_x);
	venc_write_reg(VENC_VS_INT_STOP_X__VS_INT_START_Y,
		       config->vs_int_stop_x__vs_int_start_y);
	venc_write_reg(VENC_VS_INT_STOP_Y__VS_EXT_START_X,
		       config->vs_int_stop_y__vs_ext_start_x);
	venc_write_reg(VENC_VS_EXT_STOP_X__VS_EXT_START_Y,
		       config->vs_ext_stop_x__vs_ext_start_y);
	venc_write_reg(VENC_VS_EXT_STOP_Y, config->vs_ext_stop_y);
	venc_write_reg(VENC_AVID_START_STOP_X, config->avid_start_stop_x);
	venc_write_reg(VENC_AVID_START_STOP_Y, config->avid_start_stop_y);
	venc_write_reg(VENC_FID_INT_START_X__FID_INT_START_Y,
		       config->fid_int_start_x__fid_int_start_y);
	venc_write_reg(VENC_FID_INT_OFFSET_Y__FID_EXT_START_X,
		       config->fid_int_offset_y__fid_ext_start_x);
	venc_write_reg(VENC_FID_EXT_START_Y__FID_EXT_OFFSET_Y,
		       config->fid_ext_start_y__fid_ext_offset_y);

	venc_write_reg(VENC_DAC_B__DAC_C,  venc_read_reg(VENC_DAC_B__DAC_C));
	venc_write_reg(VENC_VIDOUT_CTRL, config->vidout_ctrl);
	venc_write_reg(VENC_HFLTR_CTRL, config->hfltr_ctrl);
	venc_write_reg(VENC_X_COLOR, config->x_color);
	venc_write_reg(VENC_LINE21, config->line21);
	venc_write_reg(VENC_LN_SEL, config->ln_sel);
	venc_write_reg(VENC_HTRIGGER_VTRIGGER, config->htrigger_vtrigger);
	venc_write_reg(VENC_TVDETGP_INT_START_STOP_X,
		       config->tvdetgp_int_start_stop_x);
	venc_write_reg(VENC_TVDETGP_INT_START_STOP_Y,
		       config->tvdetgp_int_start_stop_y);
	venc_write_reg(VENC_GEN_CTRL, config->gen_ctrl);
	venc_write_reg(VENC_F_CONTROL, config->f_control);
	venc_write_reg(VENC_SYNC_CTRL, config->sync_ctrl);
}

static void venc_reset(void)
{
	int t = 1000;

	venc_write_reg(VENC_F_CONTROL, 1<<8);
	while (venc_read_reg(VENC_F_CONTROL) & (1<<8)) {
		if (--t == 0) {
			DSSERR("Failed to reset venc\n");
			return;
		}
	}

418
#ifdef CONFIG_OMAP2_DSS_SLEEP_AFTER_VENC_RESET
T
Tomi Valkeinen 已提交
419
	/* the magical sleep that makes things work */
420
	/* XXX more info? What bug this circumvents? */
T
Tomi Valkeinen 已提交
421
	msleep(20);
422
#endif
T
Tomi Valkeinen 已提交
423 424
}

425
static int venc_runtime_get(void)
T
Tomi Valkeinen 已提交
426
{
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	int r;

	DSSDBG("venc_runtime_get\n");

	r = pm_runtime_get_sync(&venc.pdev->dev);
	WARN_ON(r < 0);
	return r < 0 ? r : 0;
}

static void venc_runtime_put(void)
{
	int r;

	DSSDBG("venc_runtime_put\n");

442
	r = pm_runtime_put_sync(&venc.pdev->dev);
443
	WARN_ON(r < 0 && r != -ENOSYS);
T
Tomi Valkeinen 已提交
444 445
}

446
static const struct venc_config *venc_timings_to_config(struct videomode *vm)
T
Tomi Valkeinen 已提交
447
{
448 449 450 451
	switch (venc_get_videomode(vm)) {
	default:
		WARN_ON_ONCE(1);
	case VENC_MODE_PAL:
T
Tomi Valkeinen 已提交
452
		return &venc_config_pal_trm;
453
	case VENC_MODE_NTSC:
T
Tomi Valkeinen 已提交
454
		return &venc_config_ntsc_trm;
455
	}
T
Tomi Valkeinen 已提交
456 457
}

458
static int venc_power_on(struct omap_dss_device *dssdev)
459
{
460
	enum omap_channel channel = dssdev->dispc_channel;
461
	u32 l;
462
	int r;
463

464 465 466 467
	r = venc_runtime_get();
	if (r)
		goto err0;

468
	venc_reset();
469
	venc_write_config(venc_timings_to_config(&venc.vm));
470

471
	dss_set_venc_output(venc.type);
472 473 474 475
	dss_set_dac_pwrdn_bgz(1);

	l = 0;

476
	if (venc.type == OMAP_DSS_VENC_TYPE_COMPOSITE)
477 478 479 480
		l |= 1 << 1;
	else /* S-Video */
		l |= (1 << 0) | (1 << 2);

481
	if (venc.invert_polarity == false)
482 483 484 485
		l |= 1 << 3;

	venc_write_reg(VENC_OUTPUT_CONTROL, l);

486
	dss_mgr_set_timings(channel, &venc.vm);
487

488 489
	r = regulator_enable(venc.vdda_dac_reg);
	if (r)
490
		goto err1;
491

492
	r = dss_mgr_enable(channel);
493
	if (r)
494
		goto err2;
495 496 497

	return 0;

498 499 500
err2:
	regulator_disable(venc.vdda_dac_reg);
err1:
501 502 503
	venc_write_reg(VENC_OUTPUT_CONTROL, 0);
	dss_set_dac_pwrdn_bgz(0);

504 505
	venc_runtime_put();
err0:
506
	return r;
507 508 509 510
}

static void venc_power_off(struct omap_dss_device *dssdev)
{
511
	enum omap_channel channel = dssdev->dispc_channel;
512

513 514 515
	venc_write_reg(VENC_OUTPUT_CONTROL, 0);
	dss_set_dac_pwrdn_bgz(0);

516
	dss_mgr_disable(channel);
517 518

	regulator_disable(venc.vdda_dac_reg);
519 520

	venc_runtime_put();
521 522
}

523
static int venc_display_enable(struct omap_dss_device *dssdev)
524
{
525
	struct omap_dss_device *out = &venc.output;
526
	int r;
527

528
	DSSDBG("venc_display_enable\n");
529 530 531

	mutex_lock(&venc.venc_lock);

532
	if (!out->dispc_channel_connected) {
533
		DSSERR("Failed to enable display: no output/manager\n");
534 535
		r = -ENODEV;
		goto err0;
536 537
	}

538 539
	r = venc_power_on(dssdev);
	if (r)
540
		goto err0;
541 542 543

	venc.wss_data = 0;

544
	mutex_unlock(&venc.venc_lock);
545

546 547
	return 0;
err0:
548
	mutex_unlock(&venc.venc_lock);
T
Tomi Valkeinen 已提交
549 550 551
	return r;
}

552
static void venc_display_disable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
553
{
554
	DSSDBG("venc_display_disable\n");
T
Tomi Valkeinen 已提交
555

556 557 558 559
	mutex_lock(&venc.venc_lock);

	venc_power_off(dssdev);

560
	mutex_unlock(&venc.venc_lock);
T
Tomi Valkeinen 已提交
561 562
}

563
static void venc_set_timings(struct omap_dss_device *dssdev,
564
			     struct videomode *vm)
565
{
566 567
	struct videomode actual_vm;

568 569
	DSSDBG("venc_set_timings\n");

570 571
	mutex_lock(&venc.venc_lock);

572 573 574 575 576 577 578 579 580 581 582
	switch (venc_get_videomode(vm)) {
	default:
		WARN_ON_ONCE(1);
	case VENC_MODE_PAL:
		actual_vm = omap_dss_pal_vm;
		break;
	case VENC_MODE_NTSC:
		actual_vm = omap_dss_ntsc_vm;
		break;
	}

583
	/* Reset WSS data when the TV standard changes. */
584
	if (memcmp(&venc.vm, &actual_vm, sizeof(actual_vm)))
585 586
		venc.wss_data = 0;

587
	venc.vm = actual_vm;
588

589 590
	dispc_set_tv_pclk(13500000);

591
	mutex_unlock(&venc.venc_lock);
592 593
}

594
static int venc_check_timings(struct omap_dss_device *dssdev,
595
			      struct videomode *vm)
596 597 598
{
	DSSDBG("venc_check_timings\n");

599 600 601
	switch (venc_get_videomode(vm)) {
	case VENC_MODE_PAL:
	case VENC_MODE_NTSC:
602
		return 0;
603 604 605
	default:
		return -EINVAL;
	}
606 607
}

T
Tomi Valkeinen 已提交
608
static void venc_get_timings(struct omap_dss_device *dssdev,
609
			     struct videomode *vm)
T
Tomi Valkeinen 已提交
610 611 612
{
	mutex_lock(&venc.venc_lock);

613
	*vm = venc.vm;
T
Tomi Valkeinen 已提交
614 615 616 617

	mutex_unlock(&venc.venc_lock);
}

618
static u32 venc_get_wss(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
619 620 621 622 623
{
	/* Invert due to VENC_L21_WC_CTL:INV=1 */
	return (venc.wss_data >> 8) ^ 0xfffff;
}

624
static int venc_set_wss(struct omap_dss_device *dssdev, u32 wss)
T
Tomi Valkeinen 已提交
625 626
{
	const struct venc_config *config;
627
	int r;
T
Tomi Valkeinen 已提交
628 629 630 631 632

	DSSDBG("venc_set_wss\n");

	mutex_lock(&venc.venc_lock);

633
	config = venc_timings_to_config(&venc.vm);
T
Tomi Valkeinen 已提交
634 635 636 637

	/* Invert due to VENC_L21_WC_CTL:INV=1 */
	venc.wss_data = (wss ^ 0xfffff) << 8;

638 639 640
	r = venc_runtime_get();
	if (r)
		goto err;
T
Tomi Valkeinen 已提交
641 642 643 644

	venc_write_reg(VENC_BSTAMP_WSS_DATA, config->bstamp_wss_data |
			venc.wss_data);

645
	venc_runtime_put();
T
Tomi Valkeinen 已提交
646

647
err:
T
Tomi Valkeinen 已提交
648 649
	mutex_unlock(&venc.venc_lock);

650
	return r;
T
Tomi Valkeinen 已提交
651 652
}

653
static int venc_init_regulator(void)
T
Tomi Valkeinen 已提交
654
{
655
	struct regulator *vdda_dac;
T
Tomi Valkeinen 已提交
656

657 658
	if (venc.vdda_dac_reg != NULL)
		return 0;
659

660
	vdda_dac = devm_regulator_get(&venc.pdev->dev, "vdda");
661
	if (IS_ERR(vdda_dac)) {
662 663
		if (PTR_ERR(vdda_dac) != -EPROBE_DEFER)
			DSSERR("can't get VDDA_DAC regulator\n");
664
		return PTR_ERR(vdda_dac);
665 666
	}

667 668
	venc.vdda_dac_reg = vdda_dac;

T
Tomi Valkeinen 已提交
669 670 671
	return 0;
}

672
static void venc_dump_regs(struct seq_file *s)
T
Tomi Valkeinen 已提交
673 674 675
{
#define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, venc_read_reg(r))

676 677
	if (venc_runtime_get())
		return;
T
Tomi Valkeinen 已提交
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

	DUMPREG(VENC_F_CONTROL);
	DUMPREG(VENC_VIDOUT_CTRL);
	DUMPREG(VENC_SYNC_CTRL);
	DUMPREG(VENC_LLEN);
	DUMPREG(VENC_FLENS);
	DUMPREG(VENC_HFLTR_CTRL);
	DUMPREG(VENC_CC_CARR_WSS_CARR);
	DUMPREG(VENC_C_PHASE);
	DUMPREG(VENC_GAIN_U);
	DUMPREG(VENC_GAIN_V);
	DUMPREG(VENC_GAIN_Y);
	DUMPREG(VENC_BLACK_LEVEL);
	DUMPREG(VENC_BLANK_LEVEL);
	DUMPREG(VENC_X_COLOR);
	DUMPREG(VENC_M_CONTROL);
	DUMPREG(VENC_BSTAMP_WSS_DATA);
	DUMPREG(VENC_S_CARR);
	DUMPREG(VENC_LINE21);
	DUMPREG(VENC_LN_SEL);
	DUMPREG(VENC_L21__WC_CTL);
	DUMPREG(VENC_HTRIGGER_VTRIGGER);
	DUMPREG(VENC_SAVID__EAVID);
	DUMPREG(VENC_FLEN__FAL);
	DUMPREG(VENC_LAL__PHASE_RESET);
	DUMPREG(VENC_HS_INT_START_STOP_X);
	DUMPREG(VENC_HS_EXT_START_STOP_X);
	DUMPREG(VENC_VS_INT_START_X);
	DUMPREG(VENC_VS_INT_STOP_X__VS_INT_START_Y);
	DUMPREG(VENC_VS_INT_STOP_Y__VS_EXT_START_X);
	DUMPREG(VENC_VS_EXT_STOP_X__VS_EXT_START_Y);
	DUMPREG(VENC_VS_EXT_STOP_Y);
	DUMPREG(VENC_AVID_START_STOP_X);
	DUMPREG(VENC_AVID_START_STOP_Y);
	DUMPREG(VENC_FID_INT_START_X__FID_INT_START_Y);
	DUMPREG(VENC_FID_INT_OFFSET_Y__FID_EXT_START_X);
	DUMPREG(VENC_FID_EXT_START_Y__FID_EXT_OFFSET_Y);
	DUMPREG(VENC_TVDETGP_INT_START_STOP_X);
	DUMPREG(VENC_TVDETGP_INT_START_STOP_Y);
	DUMPREG(VENC_GEN_CTRL);
	DUMPREG(VENC_OUTPUT_CONTROL);
	DUMPREG(VENC_OUTPUT_TEST);

721
	venc_runtime_put();
T
Tomi Valkeinen 已提交
722 723 724

#undef DUMPREG
}
725

726 727 728 729
static int venc_get_clocks(struct platform_device *pdev)
{
	struct clk *clk;

730
	if (venc.requires_tv_dac_clk) {
A
Archit Taneja 已提交
731
		clk = devm_clk_get(&pdev->dev, "tv_dac_clk");
732 733 734 735 736 737 738 739 740 741 742 743 744
		if (IS_ERR(clk)) {
			DSSERR("can't get tv_dac_clk\n");
			return PTR_ERR(clk);
		}
	} else {
		clk = NULL;
	}

	venc.tv_dac_clk = clk;

	return 0;
}

T
Tomi Valkeinen 已提交
745 746 747
static int venc_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
748
	enum omap_channel channel = dssdev->dispc_channel;
T
Tomi Valkeinen 已提交
749 750 751 752 753 754
	int r;

	r = venc_init_regulator();
	if (r)
		return r;

755
	r = dss_mgr_connect(channel, dssdev);
T
Tomi Valkeinen 已提交
756 757 758 759 760 761 762
	if (r)
		return r;

	r = omapdss_output_set_device(dssdev, dst);
	if (r) {
		DSSERR("failed to connect output to new device: %s\n",
				dst->name);
763
		dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
764 765 766 767 768 769 770 771 772
		return r;
	}

	return 0;
}

static void venc_disconnect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
773 774
	enum omap_channel channel = dssdev->dispc_channel;

775
	WARN_ON(dst != dssdev->dst);
T
Tomi Valkeinen 已提交
776

777
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
778 779 780 781
		return;

	omapdss_output_unset_device(dssdev);

782
	dss_mgr_disconnect(channel, dssdev);
T
Tomi Valkeinen 已提交
783 784 785 786 787 788
}

static const struct omapdss_atv_ops venc_ops = {
	.connect = venc_connect,
	.disconnect = venc_disconnect,

789 790
	.enable = venc_display_enable,
	.disable = venc_display_disable,
T
Tomi Valkeinen 已提交
791

792 793
	.check_timings = venc_check_timings,
	.set_timings = venc_set_timings,
T
Tomi Valkeinen 已提交
794 795
	.get_timings = venc_get_timings,

796 797
	.set_wss = venc_set_wss,
	.get_wss = venc_get_wss,
T
Tomi Valkeinen 已提交
798 799
};

800
static void venc_init_output(struct platform_device *pdev)
801
{
802
	struct omap_dss_device *out = &venc.output;
803

804
	out->dev = &pdev->dev;
805
	out->id = OMAP_DSS_OUTPUT_VENC;
806
	out->output_type = OMAP_DISPLAY_TYPE_VENC;
T
Tomi Valkeinen 已提交
807
	out->name = "venc.0";
808
	out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
T
Tomi Valkeinen 已提交
809
	out->ops.atv = &venc_ops;
810
	out->owner = THIS_MODULE;
811

812
	omapdss_register_output(out);
813 814
}

815
static void venc_uninit_output(struct platform_device *pdev)
816
{
817
	struct omap_dss_device *out = &venc.output;
818

819
	omapdss_unregister_output(out);
820 821
}

T
Tomi Valkeinen 已提交
822 823 824 825 826 827 828
static int venc_probe_of(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	struct device_node *ep;
	u32 channels;
	int r;

829
	ep = of_graph_get_endpoint_by_regs(node, 0, 0);
T
Tomi Valkeinen 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	if (!ep)
		return 0;

	venc.invert_polarity = of_property_read_bool(ep, "ti,invert-polarity");

	r = of_property_read_u32(ep, "ti,channels", &channels);
	if (r) {
		dev_err(&pdev->dev,
			"failed to read property 'ti,channels': %d\n", r);
		goto err;
	}

	switch (channels) {
	case 1:
		venc.type = OMAP_DSS_VENC_TYPE_COMPOSITE;
		break;
	case 2:
		venc.type = OMAP_DSS_VENC_TYPE_SVIDEO;
		break;
	default:
		dev_err(&pdev->dev, "bad channel propert '%d'\n", channels);
		r = -EINVAL;
		goto err;
	}

	of_node_put(ep);

	return 0;
858

T
Tomi Valkeinen 已提交
859 860
err:
	of_node_put(ep);
861
	return r;
T
Tomi Valkeinen 已提交
862 863
}

864
/* VENC HW IP initialisation */
865 866 867 868 869 870
static const struct soc_device_attribute venc_soc_devices[] = {
	{ .machine = "OMAP3[45]*" },
	{ .machine = "AM35*" },
	{ /* sentinel */ }
};

T
Tomi Valkeinen 已提交
871
static int venc_bind(struct device *dev, struct device *master, void *data)
872
{
T
Tomi Valkeinen 已提交
873
	struct platform_device *pdev = to_platform_device(dev);
874
	u8 rev_id;
875
	struct resource *venc_mem;
876
	int r;
877

878 879
	venc.pdev = pdev;

880 881 882 883
	/* The OMAP34xx, OMAP35xx and AM35xx VENC require the TV DAC clock. */
	if (soc_device_match(venc_soc_devices))
		venc.requires_tv_dac_clk = true;

884 885 886 887
	mutex_init(&venc.venc_lock);

	venc.wss_data = 0;

888
	venc_mem = platform_get_resource(venc.pdev, IORESOURCE_MEM, 0);
889 890 891
	venc.base = devm_ioremap_resource(&pdev->dev, venc_mem);
	if (IS_ERR(venc.base))
		return PTR_ERR(venc.base);
892

893 894
	r = venc_get_clocks(pdev);
	if (r)
895
		return r;
896 897 898 899 900

	pm_runtime_enable(&pdev->dev);

	r = venc_runtime_get();
	if (r)
901
		goto err_runtime_get;
902 903

	rev_id = (u8)(venc_read_reg(VENC_REV_ID) & 0xff);
904
	dev_dbg(&pdev->dev, "OMAP VENC rev %d\n", rev_id);
905

906
	venc_runtime_put();
907

908 909 910 911
	r = venc_probe_of(pdev);
	if (r) {
		DSSERR("Invalid DT data\n");
		goto err_probe_of;
T
Tomi Valkeinen 已提交
912 913
	}

914 915
	dss_debugfs_create_file("venc", venc_dump_regs);

916 917
	venc_init_output(pdev);

918
	return 0;
919

T
Tomi Valkeinen 已提交
920
err_probe_of:
921
err_runtime_get:
922 923
	pm_runtime_disable(&pdev->dev);
	return r;
924 925
}

T
Tomi Valkeinen 已提交
926
static void venc_unbind(struct device *dev, struct device *master, void *data)
927
{
T
Tomi Valkeinen 已提交
928 929
	struct platform_device *pdev = to_platform_device(dev);

930 931
	venc_uninit_output(pdev);

932
	pm_runtime_disable(&pdev->dev);
T
Tomi Valkeinen 已提交
933
}
934

T
Tomi Valkeinen 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947
static const struct component_ops venc_component_ops = {
	.bind	= venc_bind,
	.unbind	= venc_unbind,
};

static int venc_probe(struct platform_device *pdev)
{
	return component_add(&pdev->dev, &venc_component_ops);
}

static int venc_remove(struct platform_device *pdev)
{
	component_del(&pdev->dev, &venc_component_ops);
948 949 950
	return 0;
}

951 952 953
static int venc_runtime_suspend(struct device *dev)
{
	if (venc.tv_dac_clk)
954
		clk_disable_unprepare(venc.tv_dac_clk);
955 956 957 958 959 960 961 962 963 964 965 966

	dispc_runtime_put();

	return 0;
}

static int venc_runtime_resume(struct device *dev)
{
	int r;

	r = dispc_runtime_get();
	if (r < 0)
967
		return r;
968 969

	if (venc.tv_dac_clk)
970
		clk_prepare_enable(venc.tv_dac_clk);
971 972 973 974 975 976 977 978 979

	return 0;
}

static const struct dev_pm_ops venc_pm_ops = {
	.runtime_suspend = venc_runtime_suspend,
	.runtime_resume = venc_runtime_resume,
};

T
Tomi Valkeinen 已提交
980 981 982 983 984 985 986
static const struct of_device_id venc_of_match[] = {
	{ .compatible = "ti,omap2-venc", },
	{ .compatible = "ti,omap3-venc", },
	{ .compatible = "ti,omap4-venc", },
	{},
};

987
static struct platform_driver omap_venchw_driver = {
T
Tomi Valkeinen 已提交
988 989
	.probe		= venc_probe,
	.remove		= venc_remove,
990 991
	.driver         = {
		.name   = "omapdss_venc",
992
		.pm	= &venc_pm_ops,
T
Tomi Valkeinen 已提交
993
		.of_match_table = venc_of_match,
T
Tomi Valkeinen 已提交
994
		.suppress_bind_attrs = true,
995 996 997
	},
};

T
Tomi Valkeinen 已提交
998
int __init venc_init_platform_driver(void)
999
{
1000
	return platform_driver_register(&omap_venchw_driver);
1001 1002
}

1003
void venc_uninit_platform_driver(void)
1004
{
1005
	platform_driver_unregister(&omap_venchw_driver);
1006
}