18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci    yuv support
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci    Copyright (C) 2007  Ian Armstrong <ian@iarmst.demon.co.uk>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "ivtv-driver.h"
108c2ecf20Sopenharmony_ci#include "ivtv-udma.h"
118c2ecf20Sopenharmony_ci#include "ivtv-yuv.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/* YUV buffer offsets */
148c2ecf20Sopenharmony_ciconst u32 yuv_offset[IVTV_YUV_BUFFERS] = {
158c2ecf20Sopenharmony_ci	0x001a8600,
168c2ecf20Sopenharmony_ci	0x00240400,
178c2ecf20Sopenharmony_ci	0x002d8200,
188c2ecf20Sopenharmony_ci	0x00370000,
198c2ecf20Sopenharmony_ci	0x00029000,
208c2ecf20Sopenharmony_ci	0x000C0E00,
218c2ecf20Sopenharmony_ci	0x006B0400,
228c2ecf20Sopenharmony_ci	0x00748200
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
268c2ecf20Sopenharmony_ci				  struct ivtv_dma_frame *args)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	struct ivtv_dma_page_info y_dma;
298c2ecf20Sopenharmony_ci	struct ivtv_dma_page_info uv_dma;
308c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
318c2ecf20Sopenharmony_ci	u8 frame = yi->draw_frame;
328c2ecf20Sopenharmony_ci	struct yuv_frame_info *f = &yi->new_frame_info[frame];
338c2ecf20Sopenharmony_ci	int y_pages, uv_pages;
348c2ecf20Sopenharmony_ci	unsigned long y_buffer_offset, uv_buffer_offset;
358c2ecf20Sopenharmony_ci	int y_decode_height, uv_decode_height, y_size;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	y_buffer_offset = IVTV_DECODER_OFFSET + yuv_offset[frame];
388c2ecf20Sopenharmony_ci	uv_buffer_offset = y_buffer_offset + IVTV_YUV_BUFFER_UV_OFFSET;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	y_decode_height = uv_decode_height = f->src_h + f->src_y;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (f->offset_y)
438c2ecf20Sopenharmony_ci		y_buffer_offset += 720 * 16;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (y_decode_height & 15)
468c2ecf20Sopenharmony_ci		y_decode_height = (y_decode_height + 16) & ~15;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	if (uv_decode_height & 31)
498c2ecf20Sopenharmony_ci		uv_decode_height = (uv_decode_height + 32) & ~31;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	y_size = 720 * y_decode_height;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	/* Still in USE */
548c2ecf20Sopenharmony_ci	if (dma->SG_length || dma->page_count) {
558c2ecf20Sopenharmony_ci		IVTV_DEBUG_WARN
568c2ecf20Sopenharmony_ci		    ("prep_user_dma: SG_length %d page_count %d still full?\n",
578c2ecf20Sopenharmony_ci		     dma->SG_length, dma->page_count);
588c2ecf20Sopenharmony_ci		return -EBUSY;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	ivtv_udma_get_page_info (&y_dma, (unsigned long)args->y_source, 720 * y_decode_height);
628c2ecf20Sopenharmony_ci	ivtv_udma_get_page_info (&uv_dma, (unsigned long)args->uv_source, 360 * uv_decode_height);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* Pin user pages for DMA Xfer */
658c2ecf20Sopenharmony_ci	y_pages = pin_user_pages_unlocked(y_dma.uaddr,
668c2ecf20Sopenharmony_ci			y_dma.page_count, &dma->map[0], FOLL_FORCE);
678c2ecf20Sopenharmony_ci	uv_pages = 0; /* silence gcc. value is set and consumed only if: */
688c2ecf20Sopenharmony_ci	if (y_pages == y_dma.page_count) {
698c2ecf20Sopenharmony_ci		uv_pages = pin_user_pages_unlocked(uv_dma.uaddr,
708c2ecf20Sopenharmony_ci				uv_dma.page_count, &dma->map[y_pages],
718c2ecf20Sopenharmony_ci				FOLL_FORCE);
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	if (y_pages != y_dma.page_count || uv_pages != uv_dma.page_count) {
758c2ecf20Sopenharmony_ci		int rc = -EFAULT;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci		if (y_pages == y_dma.page_count) {
788c2ecf20Sopenharmony_ci			IVTV_DEBUG_WARN
798c2ecf20Sopenharmony_ci				("failed to map uv user pages, returned %d expecting %d\n",
808c2ecf20Sopenharmony_ci				 uv_pages, uv_dma.page_count);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci			if (uv_pages >= 0) {
838c2ecf20Sopenharmony_ci				unpin_user_pages(&dma->map[y_pages], uv_pages);
848c2ecf20Sopenharmony_ci				rc = -EFAULT;
858c2ecf20Sopenharmony_ci			} else {
868c2ecf20Sopenharmony_ci				rc = uv_pages;
878c2ecf20Sopenharmony_ci			}
888c2ecf20Sopenharmony_ci		} else {
898c2ecf20Sopenharmony_ci			IVTV_DEBUG_WARN
908c2ecf20Sopenharmony_ci				("failed to map y user pages, returned %d expecting %d\n",
918c2ecf20Sopenharmony_ci				 y_pages, y_dma.page_count);
928c2ecf20Sopenharmony_ci		}
938c2ecf20Sopenharmony_ci		if (y_pages >= 0) {
948c2ecf20Sopenharmony_ci			unpin_user_pages(dma->map, y_pages);
958c2ecf20Sopenharmony_ci			/*
968c2ecf20Sopenharmony_ci			 * Inherit the -EFAULT from rc's
978c2ecf20Sopenharmony_ci			 * initialization, but allow it to be
988c2ecf20Sopenharmony_ci			 * overridden by uv_pages above if it was an
998c2ecf20Sopenharmony_ci			 * actual errno.
1008c2ecf20Sopenharmony_ci			 */
1018c2ecf20Sopenharmony_ci		} else {
1028c2ecf20Sopenharmony_ci			rc = y_pages;
1038c2ecf20Sopenharmony_ci		}
1048c2ecf20Sopenharmony_ci		return rc;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	dma->page_count = y_pages + uv_pages;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* Fill & map SG List */
1108c2ecf20Sopenharmony_ci	if (ivtv_udma_fill_sg_list (dma, &uv_dma, ivtv_udma_fill_sg_list (dma, &y_dma, 0)) < 0) {
1118c2ecf20Sopenharmony_ci		IVTV_DEBUG_WARN("could not allocate bounce buffers for highmem userspace buffers\n");
1128c2ecf20Sopenharmony_ci		unpin_user_pages(dma->map, dma->page_count);
1138c2ecf20Sopenharmony_ci		dma->page_count = 0;
1148c2ecf20Sopenharmony_ci		return -ENOMEM;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci	dma->SG_length = pci_map_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Fill SG Array with new values */
1198c2ecf20Sopenharmony_ci	ivtv_udma_fill_sg_array(dma, y_buffer_offset, uv_buffer_offset, y_size);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* If we've offset the y plane, ensure top area is blanked */
1228c2ecf20Sopenharmony_ci	if (f->offset_y && yi->blanking_dmaptr) {
1238c2ecf20Sopenharmony_ci		dma->SGarray[dma->SG_length].size = cpu_to_le32(720*16);
1248c2ecf20Sopenharmony_ci		dma->SGarray[dma->SG_length].src = cpu_to_le32(yi->blanking_dmaptr);
1258c2ecf20Sopenharmony_ci		dma->SGarray[dma->SG_length].dst = cpu_to_le32(IVTV_DECODER_OFFSET + yuv_offset[frame]);
1268c2ecf20Sopenharmony_ci		dma->SG_length++;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* Tag SG Array with Interrupt Bit */
1308c2ecf20Sopenharmony_ci	dma->SGarray[dma->SG_length - 1].size |= cpu_to_le32(0x80000000);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	ivtv_udma_sync_for_device(itv);
1338c2ecf20Sopenharmony_ci	return 0;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* We rely on a table held in the firmware - Quick check. */
1378c2ecf20Sopenharmony_ciint ivtv_yuv_filter_check(struct ivtv *itv)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	int i, y, uv;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	for (i = 0, y = 16, uv = 4; i < 16; i++, y += 24, uv += 12) {
1428c2ecf20Sopenharmony_ci		if ((read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + y) != i << 16) ||
1438c2ecf20Sopenharmony_ci		    (read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + uv) != i << 16)) {
1448c2ecf20Sopenharmony_ci			IVTV_WARN ("YUV filter table not found in firmware.\n");
1458c2ecf20Sopenharmony_ci			return -1;
1468c2ecf20Sopenharmony_ci		}
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void ivtv_yuv_filter(struct ivtv *itv, int h_filter, int v_filter_1, int v_filter_2)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	u32 i, line;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* If any filter is -1, then don't update it */
1568c2ecf20Sopenharmony_ci	if (h_filter > -1) {
1578c2ecf20Sopenharmony_ci		if (h_filter > 4)
1588c2ecf20Sopenharmony_ci			h_filter = 4;
1598c2ecf20Sopenharmony_ci		i = IVTV_YUV_HORIZONTAL_FILTER_OFFSET + (h_filter * 384);
1608c2ecf20Sopenharmony_ci		for (line = 0; line < 16; line++) {
1618c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02804);
1628c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x0281c);
1638c2ecf20Sopenharmony_ci			i += 4;
1648c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02808);
1658c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02820);
1668c2ecf20Sopenharmony_ci			i += 4;
1678c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x0280c);
1688c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02824);
1698c2ecf20Sopenharmony_ci			i += 4;
1708c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02810);
1718c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02828);
1728c2ecf20Sopenharmony_ci			i += 4;
1738c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02814);
1748c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x0282c);
1758c2ecf20Sopenharmony_ci			i += 8;
1768c2ecf20Sopenharmony_ci			write_reg(0, 0x02818);
1778c2ecf20Sopenharmony_ci			write_reg(0, 0x02830);
1788c2ecf20Sopenharmony_ci		}
1798c2ecf20Sopenharmony_ci		IVTV_DEBUG_YUV("h_filter -> %d\n", h_filter);
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (v_filter_1 > -1) {
1838c2ecf20Sopenharmony_ci		if (v_filter_1 > 4)
1848c2ecf20Sopenharmony_ci			v_filter_1 = 4;
1858c2ecf20Sopenharmony_ci		i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_1 * 192);
1868c2ecf20Sopenharmony_ci		for (line = 0; line < 16; line++) {
1878c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02900);
1888c2ecf20Sopenharmony_ci			i += 4;
1898c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02904);
1908c2ecf20Sopenharmony_ci			i += 8;
1918c2ecf20Sopenharmony_ci			write_reg(0, 0x02908);
1928c2ecf20Sopenharmony_ci		}
1938c2ecf20Sopenharmony_ci		IVTV_DEBUG_YUV("v_filter_1 -> %d\n", v_filter_1);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (v_filter_2 > -1) {
1978c2ecf20Sopenharmony_ci		if (v_filter_2 > 4)
1988c2ecf20Sopenharmony_ci			v_filter_2 = 4;
1998c2ecf20Sopenharmony_ci		i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_2 * 192);
2008c2ecf20Sopenharmony_ci		for (line = 0; line < 16; line++) {
2018c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x0290c);
2028c2ecf20Sopenharmony_ci			i += 4;
2038c2ecf20Sopenharmony_ci			write_reg(read_dec(i), 0x02910);
2048c2ecf20Sopenharmony_ci			i += 8;
2058c2ecf20Sopenharmony_ci			write_reg(0, 0x02914);
2068c2ecf20Sopenharmony_ci		}
2078c2ecf20Sopenharmony_ci		IVTV_DEBUG_YUV("v_filter_2 -> %d\n", v_filter_2);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *f)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
2148c2ecf20Sopenharmony_ci	u32 reg_2834, reg_2838, reg_283c;
2158c2ecf20Sopenharmony_ci	u32 reg_2844, reg_2854, reg_285c;
2168c2ecf20Sopenharmony_ci	u32 reg_2864, reg_2874, reg_2890;
2178c2ecf20Sopenharmony_ci	u32 reg_2870, reg_2870_base, reg_2870_offset;
2188c2ecf20Sopenharmony_ci	int x_cutoff;
2198c2ecf20Sopenharmony_ci	int h_filter;
2208c2ecf20Sopenharmony_ci	u32 master_width;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	IVTV_DEBUG_WARN
2238c2ecf20Sopenharmony_ci	    ("Adjust to width %d src_w %d dst_w %d src_x %d dst_x %d\n",
2248c2ecf20Sopenharmony_ci	     f->tru_w, f->src_w, f->dst_w, f->src_x, f->dst_x);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* How wide is the src image */
2278c2ecf20Sopenharmony_ci	x_cutoff = f->src_w + f->src_x;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Set the display width */
2308c2ecf20Sopenharmony_ci	reg_2834 = f->dst_w;
2318c2ecf20Sopenharmony_ci	reg_2838 = reg_2834;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	/* Set the display position */
2348c2ecf20Sopenharmony_ci	reg_2890 = f->dst_x;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* Index into the image horizontally */
2378c2ecf20Sopenharmony_ci	reg_2870 = 0;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* 2870 is normally fudged to align video coords with osd coords.
2408c2ecf20Sopenharmony_ci	   If running full screen, it causes an unwanted left shift
2418c2ecf20Sopenharmony_ci	   Remove the fudge if we almost fill the screen.
2428c2ecf20Sopenharmony_ci	   Gradually adjust the offset to avoid the video 'snapping'
2438c2ecf20Sopenharmony_ci	   left/right if it gets dragged through this region.
2448c2ecf20Sopenharmony_ci	   Only do this if osd is full width. */
2458c2ecf20Sopenharmony_ci	if (f->vis_w == 720) {
2468c2ecf20Sopenharmony_ci		if ((f->tru_x - f->pan_x > -1) && (f->tru_x - f->pan_x <= 40) && (f->dst_w >= 680))
2478c2ecf20Sopenharmony_ci			reg_2870 = 10 - (f->tru_x - f->pan_x) / 4;
2488c2ecf20Sopenharmony_ci		else if ((f->tru_x - f->pan_x < 0) && (f->tru_x - f->pan_x >= -20) && (f->dst_w >= 660))
2498c2ecf20Sopenharmony_ci			reg_2870 = (10 + (f->tru_x - f->pan_x) / 2);
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci		if (f->dst_w >= f->src_w)
2528c2ecf20Sopenharmony_ci			reg_2870 = reg_2870 << 16 | reg_2870;
2538c2ecf20Sopenharmony_ci		else
2548c2ecf20Sopenharmony_ci			reg_2870 = ((reg_2870 & ~1) << 15) | (reg_2870 & ~1);
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (f->dst_w < f->src_w)
2588c2ecf20Sopenharmony_ci		reg_2870 = 0x000d000e - reg_2870;
2598c2ecf20Sopenharmony_ci	else
2608c2ecf20Sopenharmony_ci		reg_2870 = 0x0012000e - reg_2870;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* We're also using 2870 to shift the image left (src_x & negative dst_x) */
2638c2ecf20Sopenharmony_ci	reg_2870_offset = (f->src_x * ((f->dst_w << 21) / f->src_w)) >> 19;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (f->dst_w >= f->src_w) {
2668c2ecf20Sopenharmony_ci		x_cutoff &= ~1;
2678c2ecf20Sopenharmony_ci		master_width = (f->src_w * 0x00200000) / (f->dst_w);
2688c2ecf20Sopenharmony_ci		if (master_width * f->dst_w != f->src_w * 0x00200000)
2698c2ecf20Sopenharmony_ci			master_width++;
2708c2ecf20Sopenharmony_ci		reg_2834 = (reg_2834 << 16) | x_cutoff;
2718c2ecf20Sopenharmony_ci		reg_2838 = (reg_2838 << 16) | x_cutoff;
2728c2ecf20Sopenharmony_ci		reg_283c = master_width >> 2;
2738c2ecf20Sopenharmony_ci		reg_2844 = master_width >> 2;
2748c2ecf20Sopenharmony_ci		reg_2854 = master_width;
2758c2ecf20Sopenharmony_ci		reg_285c = master_width >> 1;
2768c2ecf20Sopenharmony_ci		reg_2864 = master_width >> 1;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci		/* We also need to factor in the scaling
2798c2ecf20Sopenharmony_ci		   (src_w - dst_w) / (src_w / 4) */
2808c2ecf20Sopenharmony_ci		if (f->dst_w > f->src_w)
2818c2ecf20Sopenharmony_ci			reg_2870_base = ((f->dst_w - f->src_w)<<16) / (f->src_w <<14);
2828c2ecf20Sopenharmony_ci		else
2838c2ecf20Sopenharmony_ci			reg_2870_base = 0;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		reg_2870 += (((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 2) + (reg_2870_base << 17 | reg_2870_base);
2868c2ecf20Sopenharmony_ci		reg_2874 = 0;
2878c2ecf20Sopenharmony_ci	} else if (f->dst_w < f->src_w / 2) {
2888c2ecf20Sopenharmony_ci		master_width = (f->src_w * 0x00080000) / f->dst_w;
2898c2ecf20Sopenharmony_ci		if (master_width * f->dst_w != f->src_w * 0x00080000)
2908c2ecf20Sopenharmony_ci			master_width++;
2918c2ecf20Sopenharmony_ci		reg_2834 = (reg_2834 << 16) | x_cutoff;
2928c2ecf20Sopenharmony_ci		reg_2838 = (reg_2838 << 16) | x_cutoff;
2938c2ecf20Sopenharmony_ci		reg_283c = master_width >> 2;
2948c2ecf20Sopenharmony_ci		reg_2844 = master_width >> 1;
2958c2ecf20Sopenharmony_ci		reg_2854 = master_width;
2968c2ecf20Sopenharmony_ci		reg_285c = master_width >> 1;
2978c2ecf20Sopenharmony_ci		reg_2864 = master_width >> 1;
2988c2ecf20Sopenharmony_ci		reg_2870 += ((reg_2870_offset << 15) & 0xFFFF0000) | reg_2870_offset;
2998c2ecf20Sopenharmony_ci		reg_2870 += (5 - (((f->src_w + f->src_w / 2) - 1) / f->dst_w)) << 16;
3008c2ecf20Sopenharmony_ci		reg_2874 = 0x00000012;
3018c2ecf20Sopenharmony_ci	} else {
3028c2ecf20Sopenharmony_ci		master_width = (f->src_w * 0x00100000) / f->dst_w;
3038c2ecf20Sopenharmony_ci		if (master_width * f->dst_w != f->src_w * 0x00100000)
3048c2ecf20Sopenharmony_ci			master_width++;
3058c2ecf20Sopenharmony_ci		reg_2834 = (reg_2834 << 16) | x_cutoff;
3068c2ecf20Sopenharmony_ci		reg_2838 = (reg_2838 << 16) | x_cutoff;
3078c2ecf20Sopenharmony_ci		reg_283c = master_width >> 2;
3088c2ecf20Sopenharmony_ci		reg_2844 = master_width >> 1;
3098c2ecf20Sopenharmony_ci		reg_2854 = master_width;
3108c2ecf20Sopenharmony_ci		reg_285c = master_width >> 1;
3118c2ecf20Sopenharmony_ci		reg_2864 = master_width >> 1;
3128c2ecf20Sopenharmony_ci		reg_2870 += ((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 1;
3138c2ecf20Sopenharmony_ci		reg_2870 += (5 - (((f->src_w * 3) - 1) / f->dst_w)) << 16;
3148c2ecf20Sopenharmony_ci		reg_2874 = 0x00000001;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* Select the horizontal filter */
3188c2ecf20Sopenharmony_ci	if (f->src_w == f->dst_w) {
3198c2ecf20Sopenharmony_ci		/* An exact size match uses filter 0 */
3208c2ecf20Sopenharmony_ci		h_filter = 0;
3218c2ecf20Sopenharmony_ci	} else {
3228c2ecf20Sopenharmony_ci		/* Figure out which filter to use */
3238c2ecf20Sopenharmony_ci		h_filter = ((f->src_w << 16) / f->dst_w) >> 15;
3248c2ecf20Sopenharmony_ci		h_filter = (h_filter >> 1) + (h_filter & 1);
3258c2ecf20Sopenharmony_ci		/* Only an exact size match can use filter 0 */
3268c2ecf20Sopenharmony_ci		h_filter += !h_filter;
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	write_reg(reg_2834, 0x02834);
3308c2ecf20Sopenharmony_ci	write_reg(reg_2838, 0x02838);
3318c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2834 %08x->%08x 0x2838 %08x->%08x\n",
3328c2ecf20Sopenharmony_ci		       yi->reg_2834, reg_2834, yi->reg_2838, reg_2838);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	write_reg(reg_283c, 0x0283c);
3358c2ecf20Sopenharmony_ci	write_reg(reg_2844, 0x02844);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x283c %08x->%08x 0x2844 %08x->%08x\n",
3388c2ecf20Sopenharmony_ci		       yi->reg_283c, reg_283c, yi->reg_2844, reg_2844);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	write_reg(0x00080514, 0x02840);
3418c2ecf20Sopenharmony_ci	write_reg(0x00100514, 0x02848);
3428c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2840 %08x->%08x 0x2848 %08x->%08x\n",
3438c2ecf20Sopenharmony_ci		       yi->reg_2840, 0x00080514, yi->reg_2848, 0x00100514);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	write_reg(reg_2854, 0x02854);
3468c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2854 %08x->%08x \n",
3478c2ecf20Sopenharmony_ci		       yi->reg_2854, reg_2854);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	write_reg(reg_285c, 0x0285c);
3508c2ecf20Sopenharmony_ci	write_reg(reg_2864, 0x02864);
3518c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x285c %08x->%08x 0x2864 %08x->%08x\n",
3528c2ecf20Sopenharmony_ci		       yi->reg_285c, reg_285c, yi->reg_2864, reg_2864);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	write_reg(reg_2874, 0x02874);
3558c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2874 %08x->%08x\n",
3568c2ecf20Sopenharmony_ci		       yi->reg_2874, reg_2874);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	write_reg(reg_2870, 0x02870);
3598c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2870 %08x->%08x\n",
3608c2ecf20Sopenharmony_ci		       yi->reg_2870, reg_2870);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	write_reg(reg_2890, 0x02890);
3638c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2890 %08x->%08x\n",
3648c2ecf20Sopenharmony_ci		       yi->reg_2890, reg_2890);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* Only update the filter if we really need to */
3678c2ecf20Sopenharmony_ci	if (h_filter != yi->h_filter) {
3688c2ecf20Sopenharmony_ci		ivtv_yuv_filter(itv, h_filter, -1, -1);
3698c2ecf20Sopenharmony_ci		yi->h_filter = h_filter;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *f)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
3768c2ecf20Sopenharmony_ci	u32 master_height;
3778c2ecf20Sopenharmony_ci	u32 reg_2918, reg_291c, reg_2920, reg_2928;
3788c2ecf20Sopenharmony_ci	u32 reg_2930, reg_2934, reg_293c;
3798c2ecf20Sopenharmony_ci	u32 reg_2940, reg_2944, reg_294c;
3808c2ecf20Sopenharmony_ci	u32 reg_2950, reg_2954, reg_2958, reg_295c;
3818c2ecf20Sopenharmony_ci	u32 reg_2960, reg_2964, reg_2968, reg_296c;
3828c2ecf20Sopenharmony_ci	u32 reg_289c;
3838c2ecf20Sopenharmony_ci	u32 src_major_y, src_minor_y;
3848c2ecf20Sopenharmony_ci	u32 src_major_uv, src_minor_uv;
3858c2ecf20Sopenharmony_ci	u32 reg_2964_base, reg_2968_base;
3868c2ecf20Sopenharmony_ci	int v_filter_1, v_filter_2;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	IVTV_DEBUG_WARN
3898c2ecf20Sopenharmony_ci	    ("Adjust to height %d src_h %d dst_h %d src_y %d dst_y %d\n",
3908c2ecf20Sopenharmony_ci	     f->tru_h, f->src_h, f->dst_h, f->src_y, f->dst_y);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* What scaling mode is being used... */
3938c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Scaling mode Y: %s\n",
3948c2ecf20Sopenharmony_ci		       f->interlaced_y ? "Interlaced" : "Progressive");
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Scaling mode UV: %s\n",
3978c2ecf20Sopenharmony_ci		       f->interlaced_uv ? "Interlaced" : "Progressive");
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* What is the source video being treated as... */
4008c2ecf20Sopenharmony_ci	IVTV_DEBUG_WARN("Source video: %s\n",
4018c2ecf20Sopenharmony_ci			f->interlaced ? "Interlaced" : "Progressive");
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	/* We offset into the image using two different index methods, so split
4048c2ecf20Sopenharmony_ci	   the y source coord into two parts. */
4058c2ecf20Sopenharmony_ci	if (f->src_y < 8) {
4068c2ecf20Sopenharmony_ci		src_minor_uv = f->src_y;
4078c2ecf20Sopenharmony_ci		src_major_uv = 0;
4088c2ecf20Sopenharmony_ci	} else {
4098c2ecf20Sopenharmony_ci		src_minor_uv = 8;
4108c2ecf20Sopenharmony_ci		src_major_uv = f->src_y - 8;
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	src_minor_y = src_minor_uv;
4148c2ecf20Sopenharmony_ci	src_major_y = src_major_uv;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (f->offset_y)
4178c2ecf20Sopenharmony_ci		src_minor_y += 16;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	if (f->interlaced_y)
4208c2ecf20Sopenharmony_ci		reg_2918 = (f->dst_h << 16) | (f->src_h + src_minor_y);
4218c2ecf20Sopenharmony_ci	else
4228c2ecf20Sopenharmony_ci		reg_2918 = (f->dst_h << 16) | ((f->src_h + src_minor_y) << 1);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	if (f->interlaced_uv)
4258c2ecf20Sopenharmony_ci		reg_291c = (f->dst_h << 16) | ((f->src_h + src_minor_uv) >> 1);
4268c2ecf20Sopenharmony_ci	else
4278c2ecf20Sopenharmony_ci		reg_291c = (f->dst_h << 16) | (f->src_h + src_minor_uv);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	reg_2964_base = (src_minor_y * ((f->dst_h << 16) / f->src_h)) >> 14;
4308c2ecf20Sopenharmony_ci	reg_2968_base = (src_minor_uv * ((f->dst_h << 16) / f->src_h)) >> 14;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	if (f->dst_h / 2 >= f->src_h && !f->interlaced_y) {
4338c2ecf20Sopenharmony_ci		master_height = (f->src_h * 0x00400000) / f->dst_h;
4348c2ecf20Sopenharmony_ci		if ((f->src_h * 0x00400000) - (master_height * f->dst_h) >= f->dst_h / 2)
4358c2ecf20Sopenharmony_ci			master_height++;
4368c2ecf20Sopenharmony_ci		reg_2920 = master_height >> 2;
4378c2ecf20Sopenharmony_ci		reg_2928 = master_height >> 3;
4388c2ecf20Sopenharmony_ci		reg_2930 = master_height;
4398c2ecf20Sopenharmony_ci		reg_2940 = master_height >> 1;
4408c2ecf20Sopenharmony_ci		reg_2964_base >>= 3;
4418c2ecf20Sopenharmony_ci		reg_2968_base >>= 3;
4428c2ecf20Sopenharmony_ci		reg_296c = 0x00000000;
4438c2ecf20Sopenharmony_ci	} else if (f->dst_h >= f->src_h) {
4448c2ecf20Sopenharmony_ci		master_height = (f->src_h * 0x00400000) / f->dst_h;
4458c2ecf20Sopenharmony_ci		master_height = (master_height >> 1) + (master_height & 1);
4468c2ecf20Sopenharmony_ci		reg_2920 = master_height >> 2;
4478c2ecf20Sopenharmony_ci		reg_2928 = master_height >> 2;
4488c2ecf20Sopenharmony_ci		reg_2930 = master_height;
4498c2ecf20Sopenharmony_ci		reg_2940 = master_height >> 1;
4508c2ecf20Sopenharmony_ci		reg_296c = 0x00000000;
4518c2ecf20Sopenharmony_ci		if (f->interlaced_y) {
4528c2ecf20Sopenharmony_ci			reg_2964_base >>= 3;
4538c2ecf20Sopenharmony_ci		} else {
4548c2ecf20Sopenharmony_ci			reg_296c++;
4558c2ecf20Sopenharmony_ci			reg_2964_base >>= 2;
4568c2ecf20Sopenharmony_ci		}
4578c2ecf20Sopenharmony_ci		if (f->interlaced_uv)
4588c2ecf20Sopenharmony_ci			reg_2928 >>= 1;
4598c2ecf20Sopenharmony_ci		reg_2968_base >>= 3;
4608c2ecf20Sopenharmony_ci	} else if (f->dst_h >= f->src_h / 2) {
4618c2ecf20Sopenharmony_ci		master_height = (f->src_h * 0x00200000) / f->dst_h;
4628c2ecf20Sopenharmony_ci		master_height = (master_height >> 1) + (master_height & 1);
4638c2ecf20Sopenharmony_ci		reg_2920 = master_height >> 2;
4648c2ecf20Sopenharmony_ci		reg_2928 = master_height >> 2;
4658c2ecf20Sopenharmony_ci		reg_2930 = master_height;
4668c2ecf20Sopenharmony_ci		reg_2940 = master_height;
4678c2ecf20Sopenharmony_ci		reg_296c = 0x00000101;
4688c2ecf20Sopenharmony_ci		if (f->interlaced_y) {
4698c2ecf20Sopenharmony_ci			reg_2964_base >>= 2;
4708c2ecf20Sopenharmony_ci		} else {
4718c2ecf20Sopenharmony_ci			reg_296c++;
4728c2ecf20Sopenharmony_ci			reg_2964_base >>= 1;
4738c2ecf20Sopenharmony_ci		}
4748c2ecf20Sopenharmony_ci		if (f->interlaced_uv)
4758c2ecf20Sopenharmony_ci			reg_2928 >>= 1;
4768c2ecf20Sopenharmony_ci		reg_2968_base >>= 2;
4778c2ecf20Sopenharmony_ci	} else {
4788c2ecf20Sopenharmony_ci		master_height = (f->src_h * 0x00100000) / f->dst_h;
4798c2ecf20Sopenharmony_ci		master_height = (master_height >> 1) + (master_height & 1);
4808c2ecf20Sopenharmony_ci		reg_2920 = master_height >> 2;
4818c2ecf20Sopenharmony_ci		reg_2928 = master_height >> 2;
4828c2ecf20Sopenharmony_ci		reg_2930 = master_height;
4838c2ecf20Sopenharmony_ci		reg_2940 = master_height;
4848c2ecf20Sopenharmony_ci		reg_2964_base >>= 1;
4858c2ecf20Sopenharmony_ci		reg_2968_base >>= 2;
4868c2ecf20Sopenharmony_ci		reg_296c = 0x00000102;
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* FIXME These registers change depending on scaled / unscaled output
4908c2ecf20Sopenharmony_ci	   We really need to work out what they should be */
4918c2ecf20Sopenharmony_ci	if (f->src_h == f->dst_h) {
4928c2ecf20Sopenharmony_ci		reg_2934 = 0x00020000;
4938c2ecf20Sopenharmony_ci		reg_293c = 0x00100000;
4948c2ecf20Sopenharmony_ci		reg_2944 = 0x00040000;
4958c2ecf20Sopenharmony_ci		reg_294c = 0x000b0000;
4968c2ecf20Sopenharmony_ci	} else {
4978c2ecf20Sopenharmony_ci		reg_2934 = 0x00000FF0;
4988c2ecf20Sopenharmony_ci		reg_293c = 0x00000FF0;
4998c2ecf20Sopenharmony_ci		reg_2944 = 0x00000FF0;
5008c2ecf20Sopenharmony_ci		reg_294c = 0x00000FF0;
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	/* The first line to be displayed */
5048c2ecf20Sopenharmony_ci	reg_2950 = 0x00010000 + src_major_y;
5058c2ecf20Sopenharmony_ci	if (f->interlaced_y)
5068c2ecf20Sopenharmony_ci		reg_2950 += 0x00010000;
5078c2ecf20Sopenharmony_ci	reg_2954 = reg_2950 + 1;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	reg_2958 = 0x00010000 + (src_major_y >> 1);
5108c2ecf20Sopenharmony_ci	if (f->interlaced_uv)
5118c2ecf20Sopenharmony_ci		reg_2958 += 0x00010000;
5128c2ecf20Sopenharmony_ci	reg_295c = reg_2958 + 1;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	if (yi->decode_height == 480)
5158c2ecf20Sopenharmony_ci		reg_289c = 0x011e0017;
5168c2ecf20Sopenharmony_ci	else
5178c2ecf20Sopenharmony_ci		reg_289c = 0x01500017;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (f->dst_y < 0)
5208c2ecf20Sopenharmony_ci		reg_289c = (reg_289c - ((f->dst_y & ~1)<<15))-(f->dst_y >>1);
5218c2ecf20Sopenharmony_ci	else
5228c2ecf20Sopenharmony_ci		reg_289c = (reg_289c + ((f->dst_y & ~1)<<15))+(f->dst_y >>1);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/* How much of the source to decode.
5258c2ecf20Sopenharmony_ci	   Take into account the source offset */
5268c2ecf20Sopenharmony_ci	reg_2960 = ((src_minor_y + f->src_h + src_major_y) - 1) |
5278c2ecf20Sopenharmony_ci		(((src_minor_uv + f->src_h + src_major_uv - 1) & ~1) << 15);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	/* Calculate correct value for register 2964 */
5308c2ecf20Sopenharmony_ci	if (f->src_h == f->dst_h) {
5318c2ecf20Sopenharmony_ci		reg_2964 = 1;
5328c2ecf20Sopenharmony_ci	} else {
5338c2ecf20Sopenharmony_ci		reg_2964 = 2 + ((f->dst_h << 1) / f->src_h);
5348c2ecf20Sopenharmony_ci		reg_2964 = (reg_2964 >> 1) + (reg_2964 & 1);
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci	reg_2968 = (reg_2964 << 16) + reg_2964 + (reg_2964 >> 1);
5378c2ecf20Sopenharmony_ci	reg_2964 = (reg_2964 << 16) + reg_2964 + (reg_2964 * 46 / 94);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* Okay, we've wasted time working out the correct value,
5408c2ecf20Sopenharmony_ci	   but if we use it, it fouls the the window alignment.
5418c2ecf20Sopenharmony_ci	   Fudge it to what we want... */
5428c2ecf20Sopenharmony_ci	reg_2964 = 0x00010001 + ((reg_2964 & 0x0000FFFF) - (reg_2964 >> 16));
5438c2ecf20Sopenharmony_ci	reg_2968 = 0x00010001 + ((reg_2968 & 0x0000FFFF) - (reg_2968 >> 16));
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	/* Deviate further from what it should be. I find the flicker headache
5468c2ecf20Sopenharmony_ci	   inducing so try to reduce it slightly. Leave 2968 as-is otherwise
5478c2ecf20Sopenharmony_ci	   colours foul. */
5488c2ecf20Sopenharmony_ci	if ((reg_2964 != 0x00010001) && (f->dst_h / 2 <= f->src_h))
5498c2ecf20Sopenharmony_ci		reg_2964 = (reg_2964 & 0xFFFF0000) + ((reg_2964 & 0x0000FFFF) / 2);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	if (!f->interlaced_y)
5528c2ecf20Sopenharmony_ci		reg_2964 -= 0x00010001;
5538c2ecf20Sopenharmony_ci	if (!f->interlaced_uv)
5548c2ecf20Sopenharmony_ci		reg_2968 -= 0x00010001;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	reg_2964 += ((reg_2964_base << 16) | reg_2964_base);
5578c2ecf20Sopenharmony_ci	reg_2968 += ((reg_2968_base << 16) | reg_2968_base);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	/* Select the vertical filter */
5608c2ecf20Sopenharmony_ci	if (f->src_h == f->dst_h) {
5618c2ecf20Sopenharmony_ci		/* An exact size match uses filter 0/1 */
5628c2ecf20Sopenharmony_ci		v_filter_1 = 0;
5638c2ecf20Sopenharmony_ci		v_filter_2 = 1;
5648c2ecf20Sopenharmony_ci	} else {
5658c2ecf20Sopenharmony_ci		/* Figure out which filter to use */
5668c2ecf20Sopenharmony_ci		v_filter_1 = ((f->src_h << 16) / f->dst_h) >> 15;
5678c2ecf20Sopenharmony_ci		v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
5688c2ecf20Sopenharmony_ci		/* Only an exact size match can use filter 0 */
5698c2ecf20Sopenharmony_ci		v_filter_1 += !v_filter_1;
5708c2ecf20Sopenharmony_ci		v_filter_2 = v_filter_1;
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	write_reg(reg_2934, 0x02934);
5748c2ecf20Sopenharmony_ci	write_reg(reg_293c, 0x0293c);
5758c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2934 %08x->%08x 0x293c %08x->%08x\n",
5768c2ecf20Sopenharmony_ci		       yi->reg_2934, reg_2934, yi->reg_293c, reg_293c);
5778c2ecf20Sopenharmony_ci	write_reg(reg_2944, 0x02944);
5788c2ecf20Sopenharmony_ci	write_reg(reg_294c, 0x0294c);
5798c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2944 %08x->%08x 0x294c %08x->%08x\n",
5808c2ecf20Sopenharmony_ci		       yi->reg_2944, reg_2944, yi->reg_294c, reg_294c);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	/* Ensure 2970 is 0 (does it ever change ?) */
5838c2ecf20Sopenharmony_ci/*	write_reg(0,0x02970); */
5848c2ecf20Sopenharmony_ci/*	IVTV_DEBUG_YUV("Update reg 0x2970 %08x->%08x\n", yi->reg_2970, 0); */
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	write_reg(reg_2930, 0x02938);
5878c2ecf20Sopenharmony_ci	write_reg(reg_2930, 0x02930);
5888c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2930 %08x->%08x 0x2938 %08x->%08x\n",
5898c2ecf20Sopenharmony_ci		       yi->reg_2930, reg_2930, yi->reg_2938, reg_2930);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	write_reg(reg_2928, 0x02928);
5928c2ecf20Sopenharmony_ci	write_reg(reg_2928 + 0x514, 0x0292C);
5938c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2928 %08x->%08x 0x292c %08x->%08x\n",
5948c2ecf20Sopenharmony_ci		       yi->reg_2928, reg_2928, yi->reg_292c, reg_2928 + 0x514);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	write_reg(reg_2920, 0x02920);
5978c2ecf20Sopenharmony_ci	write_reg(reg_2920 + 0x514, 0x02924);
5988c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2920 %08x->%08x 0x2924 %08x->%08x\n",
5998c2ecf20Sopenharmony_ci		       yi->reg_2920, reg_2920, yi->reg_2924, reg_2920 + 0x514);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	write_reg(reg_2918, 0x02918);
6028c2ecf20Sopenharmony_ci	write_reg(reg_291c, 0x0291C);
6038c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2918 %08x->%08x 0x291C %08x->%08x\n",
6048c2ecf20Sopenharmony_ci		       yi->reg_2918, reg_2918, yi->reg_291c, reg_291c);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	write_reg(reg_296c, 0x0296c);
6078c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x296c %08x->%08x\n",
6088c2ecf20Sopenharmony_ci		       yi->reg_296c, reg_296c);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	write_reg(reg_2940, 0x02948);
6118c2ecf20Sopenharmony_ci	write_reg(reg_2940, 0x02940);
6128c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2940 %08x->%08x 0x2948 %08x->%08x\n",
6138c2ecf20Sopenharmony_ci		       yi->reg_2940, reg_2940, yi->reg_2948, reg_2940);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	write_reg(reg_2950, 0x02950);
6168c2ecf20Sopenharmony_ci	write_reg(reg_2954, 0x02954);
6178c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2950 %08x->%08x 0x2954 %08x->%08x\n",
6188c2ecf20Sopenharmony_ci		       yi->reg_2950, reg_2950, yi->reg_2954, reg_2954);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	write_reg(reg_2958, 0x02958);
6218c2ecf20Sopenharmony_ci	write_reg(reg_295c, 0x0295C);
6228c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2958 %08x->%08x 0x295C %08x->%08x\n",
6238c2ecf20Sopenharmony_ci		       yi->reg_2958, reg_2958, yi->reg_295c, reg_295c);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	write_reg(reg_2960, 0x02960);
6268c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2960 %08x->%08x \n",
6278c2ecf20Sopenharmony_ci		       yi->reg_2960, reg_2960);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	write_reg(reg_2964, 0x02964);
6308c2ecf20Sopenharmony_ci	write_reg(reg_2968, 0x02968);
6318c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x2964 %08x->%08x 0x2968 %08x->%08x\n",
6328c2ecf20Sopenharmony_ci		       yi->reg_2964, reg_2964, yi->reg_2968, reg_2968);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	write_reg(reg_289c, 0x0289c);
6358c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update reg 0x289c %08x->%08x\n",
6368c2ecf20Sopenharmony_ci		       yi->reg_289c, reg_289c);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* Only update filter 1 if we really need to */
6398c2ecf20Sopenharmony_ci	if (v_filter_1 != yi->v_filter_1) {
6408c2ecf20Sopenharmony_ci		ivtv_yuv_filter(itv, -1, v_filter_1, -1);
6418c2ecf20Sopenharmony_ci		yi->v_filter_1 = v_filter_1;
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	/* Only update filter 2 if we really need to */
6458c2ecf20Sopenharmony_ci	if (v_filter_2 != yi->v_filter_2) {
6468c2ecf20Sopenharmony_ci		ivtv_yuv_filter(itv, -1, -1, v_filter_2);
6478c2ecf20Sopenharmony_ci		yi->v_filter_2 = v_filter_2;
6488c2ecf20Sopenharmony_ci	}
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/* Modify the supplied coordinate information to fit the visible osd area */
6528c2ecf20Sopenharmony_cistatic u32 ivtv_yuv_window_setup(struct ivtv *itv, struct yuv_frame_info *f)
6538c2ecf20Sopenharmony_ci{
6548c2ecf20Sopenharmony_ci	struct yuv_frame_info *of = &itv->yuv_info.old_frame_info;
6558c2ecf20Sopenharmony_ci	int osd_crop;
6568c2ecf20Sopenharmony_ci	u32 osd_scale;
6578c2ecf20Sopenharmony_ci	u32 yuv_update = 0;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	/* Sorry, but no negative coords for src */
6608c2ecf20Sopenharmony_ci	if (f->src_x < 0)
6618c2ecf20Sopenharmony_ci		f->src_x = 0;
6628c2ecf20Sopenharmony_ci	if (f->src_y < 0)
6638c2ecf20Sopenharmony_ci		f->src_y = 0;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	/* Can only reduce width down to 1/4 original size */
6668c2ecf20Sopenharmony_ci	if ((osd_crop = f->src_w - 4 * f->dst_w) > 0) {
6678c2ecf20Sopenharmony_ci		f->src_x += osd_crop / 2;
6688c2ecf20Sopenharmony_ci		f->src_w = (f->src_w - osd_crop) & ~3;
6698c2ecf20Sopenharmony_ci		f->dst_w = f->src_w / 4;
6708c2ecf20Sopenharmony_ci		f->dst_w += f->dst_w & 1;
6718c2ecf20Sopenharmony_ci	}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	/* Can only reduce height down to 1/4 original size */
6748c2ecf20Sopenharmony_ci	if (f->src_h / f->dst_h >= 2) {
6758c2ecf20Sopenharmony_ci		/* Overflow may be because we're running progressive,
6768c2ecf20Sopenharmony_ci		   so force mode switch */
6778c2ecf20Sopenharmony_ci		f->interlaced_y = 1;
6788c2ecf20Sopenharmony_ci		/* Make sure we're still within limits for interlace */
6798c2ecf20Sopenharmony_ci		if ((osd_crop = f->src_h - 4 * f->dst_h) > 0) {
6808c2ecf20Sopenharmony_ci			/* If we reach here we'll have to force the height. */
6818c2ecf20Sopenharmony_ci			f->src_y += osd_crop / 2;
6828c2ecf20Sopenharmony_ci			f->src_h = (f->src_h - osd_crop) & ~3;
6838c2ecf20Sopenharmony_ci			f->dst_h = f->src_h / 4;
6848c2ecf20Sopenharmony_ci			f->dst_h += f->dst_h & 1;
6858c2ecf20Sopenharmony_ci		}
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	/* If there's nothing to safe to display, we may as well stop now */
6898c2ecf20Sopenharmony_ci	if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
6908c2ecf20Sopenharmony_ci	    (int)f->src_w <= 2 || (int)f->src_h <= 2) {
6918c2ecf20Sopenharmony_ci		return IVTV_YUV_UPDATE_INVALID;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	/* Ensure video remains inside OSD area */
6958c2ecf20Sopenharmony_ci	osd_scale = (f->src_h << 16) / f->dst_h;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	if ((osd_crop = f->pan_y - f->dst_y) > 0) {
6988c2ecf20Sopenharmony_ci		/* Falls off the upper edge - crop */
6998c2ecf20Sopenharmony_ci		f->src_y += (osd_scale * osd_crop) >> 16;
7008c2ecf20Sopenharmony_ci		f->src_h -= (osd_scale * osd_crop) >> 16;
7018c2ecf20Sopenharmony_ci		f->dst_h -= osd_crop;
7028c2ecf20Sopenharmony_ci		f->dst_y = 0;
7038c2ecf20Sopenharmony_ci	} else {
7048c2ecf20Sopenharmony_ci		f->dst_y -= f->pan_y;
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	if ((osd_crop = f->dst_h + f->dst_y - f->vis_h) > 0) {
7088c2ecf20Sopenharmony_ci		/* Falls off the lower edge - crop */
7098c2ecf20Sopenharmony_ci		f->dst_h -= osd_crop;
7108c2ecf20Sopenharmony_ci		f->src_h -= (osd_scale * osd_crop) >> 16;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	osd_scale = (f->src_w << 16) / f->dst_w;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	if ((osd_crop = f->pan_x - f->dst_x) > 0) {
7168c2ecf20Sopenharmony_ci		/* Fall off the left edge - crop */
7178c2ecf20Sopenharmony_ci		f->src_x += (osd_scale * osd_crop) >> 16;
7188c2ecf20Sopenharmony_ci		f->src_w -= (osd_scale * osd_crop) >> 16;
7198c2ecf20Sopenharmony_ci		f->dst_w -= osd_crop;
7208c2ecf20Sopenharmony_ci		f->dst_x = 0;
7218c2ecf20Sopenharmony_ci	} else {
7228c2ecf20Sopenharmony_ci		f->dst_x -= f->pan_x;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	if ((osd_crop = f->dst_w + f->dst_x - f->vis_w) > 0) {
7268c2ecf20Sopenharmony_ci		/* Falls off the right edge - crop */
7278c2ecf20Sopenharmony_ci		f->dst_w -= osd_crop;
7288c2ecf20Sopenharmony_ci		f->src_w -= (osd_scale * osd_crop) >> 16;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	if (itv->yuv_info.track_osd) {
7328c2ecf20Sopenharmony_ci		/* The OSD can be moved. Track to it */
7338c2ecf20Sopenharmony_ci		f->dst_x += itv->yuv_info.osd_x_offset;
7348c2ecf20Sopenharmony_ci		f->dst_y += itv->yuv_info.osd_y_offset;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	/* Width & height for both src & dst must be even.
7388c2ecf20Sopenharmony_ci	   Same for coordinates. */
7398c2ecf20Sopenharmony_ci	f->dst_w &= ~1;
7408c2ecf20Sopenharmony_ci	f->dst_x &= ~1;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	f->src_w += f->src_x & 1;
7438c2ecf20Sopenharmony_ci	f->src_x &= ~1;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	f->src_w &= ~1;
7468c2ecf20Sopenharmony_ci	f->dst_w &= ~1;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	f->dst_h &= ~1;
7498c2ecf20Sopenharmony_ci	f->dst_y &= ~1;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	f->src_h += f->src_y & 1;
7528c2ecf20Sopenharmony_ci	f->src_y &= ~1;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	f->src_h &= ~1;
7558c2ecf20Sopenharmony_ci	f->dst_h &= ~1;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	/* Due to rounding, we may have reduced the output size to <1/4 of
7588c2ecf20Sopenharmony_ci	   the source. Check again, but this time just resize. Don't change
7598c2ecf20Sopenharmony_ci	   source coordinates */
7608c2ecf20Sopenharmony_ci	if (f->dst_w < f->src_w / 4) {
7618c2ecf20Sopenharmony_ci		f->src_w &= ~3;
7628c2ecf20Sopenharmony_ci		f->dst_w = f->src_w / 4;
7638c2ecf20Sopenharmony_ci		f->dst_w += f->dst_w & 1;
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci	if (f->dst_h < f->src_h / 4) {
7668c2ecf20Sopenharmony_ci		f->src_h &= ~3;
7678c2ecf20Sopenharmony_ci		f->dst_h = f->src_h / 4;
7688c2ecf20Sopenharmony_ci		f->dst_h += f->dst_h & 1;
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	/* Check again. If there's nothing to safe to display, stop now */
7728c2ecf20Sopenharmony_ci	if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
7738c2ecf20Sopenharmony_ci	    (int)f->src_w <= 2 || (int)f->src_h <= 2) {
7748c2ecf20Sopenharmony_ci		return IVTV_YUV_UPDATE_INVALID;
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	/* Both x offset & width are linked, so they have to be done together */
7788c2ecf20Sopenharmony_ci	if ((of->dst_w != f->dst_w) || (of->src_w != f->src_w) ||
7798c2ecf20Sopenharmony_ci	    (of->dst_x != f->dst_x) || (of->src_x != f->src_x) ||
7808c2ecf20Sopenharmony_ci	    (of->pan_x != f->pan_x) || (of->vis_w != f->vis_w)) {
7818c2ecf20Sopenharmony_ci		yuv_update |= IVTV_YUV_UPDATE_HORIZONTAL;
7828c2ecf20Sopenharmony_ci	}
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if ((of->src_h != f->src_h) || (of->dst_h != f->dst_h) ||
7858c2ecf20Sopenharmony_ci	    (of->dst_y != f->dst_y) || (of->src_y != f->src_y) ||
7868c2ecf20Sopenharmony_ci	    (of->pan_y != f->pan_y) || (of->vis_h != f->vis_h) ||
7878c2ecf20Sopenharmony_ci	    (of->lace_mode != f->lace_mode) ||
7888c2ecf20Sopenharmony_ci	    (of->interlaced_y != f->interlaced_y) ||
7898c2ecf20Sopenharmony_ci	    (of->interlaced_uv != f->interlaced_uv)) {
7908c2ecf20Sopenharmony_ci		yuv_update |= IVTV_YUV_UPDATE_VERTICAL;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	return yuv_update;
7948c2ecf20Sopenharmony_ci}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci/* Update the scaling register to the requested value */
7978c2ecf20Sopenharmony_civoid ivtv_yuv_work_handler(struct ivtv *itv)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
8008c2ecf20Sopenharmony_ci	struct yuv_frame_info f;
8018c2ecf20Sopenharmony_ci	int frame = yi->update_frame;
8028c2ecf20Sopenharmony_ci	u32 yuv_update;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("Update yuv registers for frame %d\n", frame);
8058c2ecf20Sopenharmony_ci	f = yi->new_frame_info[frame];
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (yi->track_osd) {
8088c2ecf20Sopenharmony_ci		/* Snapshot the osd pan info */
8098c2ecf20Sopenharmony_ci		f.pan_x = yi->osd_x_pan;
8108c2ecf20Sopenharmony_ci		f.pan_y = yi->osd_y_pan;
8118c2ecf20Sopenharmony_ci		f.vis_w = yi->osd_vis_w;
8128c2ecf20Sopenharmony_ci		f.vis_h = yi->osd_vis_h;
8138c2ecf20Sopenharmony_ci	} else {
8148c2ecf20Sopenharmony_ci		/* Not tracking the osd, so assume full screen */
8158c2ecf20Sopenharmony_ci		f.pan_x = 0;
8168c2ecf20Sopenharmony_ci		f.pan_y = 0;
8178c2ecf20Sopenharmony_ci		f.vis_w = 720;
8188c2ecf20Sopenharmony_ci		f.vis_h = yi->decode_height;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	/* Calculate the display window coordinates. Exit if nothing left */
8228c2ecf20Sopenharmony_ci	if (!(yuv_update = ivtv_yuv_window_setup(itv, &f)))
8238c2ecf20Sopenharmony_ci		return;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	if (yuv_update & IVTV_YUV_UPDATE_INVALID) {
8268c2ecf20Sopenharmony_ci		write_reg(0x01008080, 0x2898);
8278c2ecf20Sopenharmony_ci	} else if (yuv_update) {
8288c2ecf20Sopenharmony_ci		write_reg(0x00108080, 0x2898);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci		if (yuv_update & IVTV_YUV_UPDATE_HORIZONTAL)
8318c2ecf20Sopenharmony_ci			ivtv_yuv_handle_horizontal(itv, &f);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci		if (yuv_update & IVTV_YUV_UPDATE_VERTICAL)
8348c2ecf20Sopenharmony_ci			ivtv_yuv_handle_vertical(itv, &f);
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci	yi->old_frame_info = f;
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_cistatic void ivtv_yuv_init(struct ivtv *itv)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("ivtv_yuv_init\n");
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	/* Take a snapshot of the current register settings */
8468c2ecf20Sopenharmony_ci	yi->reg_2834 = read_reg(0x02834);
8478c2ecf20Sopenharmony_ci	yi->reg_2838 = read_reg(0x02838);
8488c2ecf20Sopenharmony_ci	yi->reg_283c = read_reg(0x0283c);
8498c2ecf20Sopenharmony_ci	yi->reg_2840 = read_reg(0x02840);
8508c2ecf20Sopenharmony_ci	yi->reg_2844 = read_reg(0x02844);
8518c2ecf20Sopenharmony_ci	yi->reg_2848 = read_reg(0x02848);
8528c2ecf20Sopenharmony_ci	yi->reg_2854 = read_reg(0x02854);
8538c2ecf20Sopenharmony_ci	yi->reg_285c = read_reg(0x0285c);
8548c2ecf20Sopenharmony_ci	yi->reg_2864 = read_reg(0x02864);
8558c2ecf20Sopenharmony_ci	yi->reg_2870 = read_reg(0x02870);
8568c2ecf20Sopenharmony_ci	yi->reg_2874 = read_reg(0x02874);
8578c2ecf20Sopenharmony_ci	yi->reg_2898 = read_reg(0x02898);
8588c2ecf20Sopenharmony_ci	yi->reg_2890 = read_reg(0x02890);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	yi->reg_289c = read_reg(0x0289c);
8618c2ecf20Sopenharmony_ci	yi->reg_2918 = read_reg(0x02918);
8628c2ecf20Sopenharmony_ci	yi->reg_291c = read_reg(0x0291c);
8638c2ecf20Sopenharmony_ci	yi->reg_2920 = read_reg(0x02920);
8648c2ecf20Sopenharmony_ci	yi->reg_2924 = read_reg(0x02924);
8658c2ecf20Sopenharmony_ci	yi->reg_2928 = read_reg(0x02928);
8668c2ecf20Sopenharmony_ci	yi->reg_292c = read_reg(0x0292c);
8678c2ecf20Sopenharmony_ci	yi->reg_2930 = read_reg(0x02930);
8688c2ecf20Sopenharmony_ci	yi->reg_2934 = read_reg(0x02934);
8698c2ecf20Sopenharmony_ci	yi->reg_2938 = read_reg(0x02938);
8708c2ecf20Sopenharmony_ci	yi->reg_293c = read_reg(0x0293c);
8718c2ecf20Sopenharmony_ci	yi->reg_2940 = read_reg(0x02940);
8728c2ecf20Sopenharmony_ci	yi->reg_2944 = read_reg(0x02944);
8738c2ecf20Sopenharmony_ci	yi->reg_2948 = read_reg(0x02948);
8748c2ecf20Sopenharmony_ci	yi->reg_294c = read_reg(0x0294c);
8758c2ecf20Sopenharmony_ci	yi->reg_2950 = read_reg(0x02950);
8768c2ecf20Sopenharmony_ci	yi->reg_2954 = read_reg(0x02954);
8778c2ecf20Sopenharmony_ci	yi->reg_2958 = read_reg(0x02958);
8788c2ecf20Sopenharmony_ci	yi->reg_295c = read_reg(0x0295c);
8798c2ecf20Sopenharmony_ci	yi->reg_2960 = read_reg(0x02960);
8808c2ecf20Sopenharmony_ci	yi->reg_2964 = read_reg(0x02964);
8818c2ecf20Sopenharmony_ci	yi->reg_2968 = read_reg(0x02968);
8828c2ecf20Sopenharmony_ci	yi->reg_296c = read_reg(0x0296c);
8838c2ecf20Sopenharmony_ci	yi->reg_2970 = read_reg(0x02970);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	yi->v_filter_1 = -1;
8868c2ecf20Sopenharmony_ci	yi->v_filter_2 = -1;
8878c2ecf20Sopenharmony_ci	yi->h_filter = -1;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	/* Set some valid size info */
8908c2ecf20Sopenharmony_ci	yi->osd_x_offset = read_reg(0x02a04) & 0x00000FFF;
8918c2ecf20Sopenharmony_ci	yi->osd_y_offset = (read_reg(0x02a04) >> 16) & 0x00000FFF;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	/* Bit 2 of reg 2878 indicates current decoder output format
8948c2ecf20Sopenharmony_ci	   0 : NTSC    1 : PAL */
8958c2ecf20Sopenharmony_ci	if (read_reg(0x2878) & 4)
8968c2ecf20Sopenharmony_ci		yi->decode_height = 576;
8978c2ecf20Sopenharmony_ci	else
8988c2ecf20Sopenharmony_ci		yi->decode_height = 480;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	if (!itv->osd_info) {
9018c2ecf20Sopenharmony_ci		yi->osd_vis_w = 720 - yi->osd_x_offset;
9028c2ecf20Sopenharmony_ci		yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
9038c2ecf20Sopenharmony_ci	} else {
9048c2ecf20Sopenharmony_ci		/* If no visible size set, assume full size */
9058c2ecf20Sopenharmony_ci		if (!yi->osd_vis_w)
9068c2ecf20Sopenharmony_ci			yi->osd_vis_w = 720 - yi->osd_x_offset;
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci		if (!yi->osd_vis_h) {
9098c2ecf20Sopenharmony_ci			yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
9108c2ecf20Sopenharmony_ci		} else if (yi->osd_vis_h + yi->osd_y_offset > yi->decode_height) {
9118c2ecf20Sopenharmony_ci			/* If output video standard has changed, requested height may
9128c2ecf20Sopenharmony_ci			   not be legal */
9138c2ecf20Sopenharmony_ci			IVTV_DEBUG_WARN("Clipping yuv output - fb size (%d) exceeds video standard limit (%d)\n",
9148c2ecf20Sopenharmony_ci					yi->osd_vis_h + yi->osd_y_offset,
9158c2ecf20Sopenharmony_ci					yi->decode_height);
9168c2ecf20Sopenharmony_ci			yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
9178c2ecf20Sopenharmony_ci		}
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	/* We need a buffer for blanking when Y plane is offset - non-fatal if we can't get one */
9218c2ecf20Sopenharmony_ci	yi->blanking_ptr = kzalloc(720 * 16, GFP_ATOMIC|__GFP_NOWARN);
9228c2ecf20Sopenharmony_ci	if (yi->blanking_ptr) {
9238c2ecf20Sopenharmony_ci		yi->blanking_dmaptr = pci_map_single(itv->pdev, yi->blanking_ptr, 720*16, PCI_DMA_TODEVICE);
9248c2ecf20Sopenharmony_ci	} else {
9258c2ecf20Sopenharmony_ci		yi->blanking_dmaptr = 0;
9268c2ecf20Sopenharmony_ci		IVTV_DEBUG_WARN("Failed to allocate yuv blanking buffer\n");
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* Enable YUV decoder output */
9308c2ecf20Sopenharmony_ci	write_reg_sync(0x01, IVTV_REG_VDM);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	set_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
9338c2ecf20Sopenharmony_ci	atomic_set(&yi->next_dma_frame, 0);
9348c2ecf20Sopenharmony_ci}
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci/* Get next available yuv buffer on PVR350 */
9378c2ecf20Sopenharmony_cistatic void ivtv_yuv_next_free(struct ivtv *itv)
9388c2ecf20Sopenharmony_ci{
9398c2ecf20Sopenharmony_ci	int draw, display;
9408c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (atomic_read(&yi->next_dma_frame) == -1)
9438c2ecf20Sopenharmony_ci		ivtv_yuv_init(itv);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	draw = atomic_read(&yi->next_fill_frame);
9468c2ecf20Sopenharmony_ci	display = atomic_read(&yi->next_dma_frame);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	if (display > draw)
9498c2ecf20Sopenharmony_ci		display -= IVTV_YUV_BUFFERS;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	if (draw - display >= yi->max_frames_buffered)
9528c2ecf20Sopenharmony_ci		draw = (u8)(draw - 1) % IVTV_YUV_BUFFERS;
9538c2ecf20Sopenharmony_ci	else
9548c2ecf20Sopenharmony_ci		yi->new_frame_info[draw].update = 0;
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	yi->draw_frame = draw;
9578c2ecf20Sopenharmony_ci}
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci/* Set up frame according to ivtv_dma_frame parameters */
9608c2ecf20Sopenharmony_cistatic void ivtv_yuv_setup_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
9638c2ecf20Sopenharmony_ci	u8 frame = yi->draw_frame;
9648c2ecf20Sopenharmony_ci	u8 last_frame = (u8)(frame - 1) % IVTV_YUV_BUFFERS;
9658c2ecf20Sopenharmony_ci	struct yuv_frame_info *nf = &yi->new_frame_info[frame];
9668c2ecf20Sopenharmony_ci	struct yuv_frame_info *of = &yi->new_frame_info[last_frame];
9678c2ecf20Sopenharmony_ci	int lace_threshold = yi->lace_threshold;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	/* Preserve old update flag in case we're overwriting a queued frame */
9708c2ecf20Sopenharmony_ci	int update = nf->update;
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	/* Take a snapshot of the yuv coordinate information */
9738c2ecf20Sopenharmony_ci	nf->src_x = args->src.left;
9748c2ecf20Sopenharmony_ci	nf->src_y = args->src.top;
9758c2ecf20Sopenharmony_ci	nf->src_w = args->src.width;
9768c2ecf20Sopenharmony_ci	nf->src_h = args->src.height;
9778c2ecf20Sopenharmony_ci	nf->dst_x = args->dst.left;
9788c2ecf20Sopenharmony_ci	nf->dst_y = args->dst.top;
9798c2ecf20Sopenharmony_ci	nf->dst_w = args->dst.width;
9808c2ecf20Sopenharmony_ci	nf->dst_h = args->dst.height;
9818c2ecf20Sopenharmony_ci	nf->tru_x = args->dst.left;
9828c2ecf20Sopenharmony_ci	nf->tru_w = args->src_width;
9838c2ecf20Sopenharmony_ci	nf->tru_h = args->src_height;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	/* Are we going to offset the Y plane */
9868c2ecf20Sopenharmony_ci	nf->offset_y = (nf->tru_h + nf->src_x < 512 - 16) ? 1 : 0;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	nf->update = 0;
9898c2ecf20Sopenharmony_ci	nf->interlaced_y = 0;
9908c2ecf20Sopenharmony_ci	nf->interlaced_uv = 0;
9918c2ecf20Sopenharmony_ci	nf->delay = 0;
9928c2ecf20Sopenharmony_ci	nf->sync_field = 0;
9938c2ecf20Sopenharmony_ci	nf->lace_mode = yi->lace_mode & IVTV_YUV_MODE_MASK;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	if (lace_threshold < 0)
9968c2ecf20Sopenharmony_ci		lace_threshold = yi->decode_height - 1;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	/* Work out the lace settings */
9998c2ecf20Sopenharmony_ci	switch (nf->lace_mode) {
10008c2ecf20Sopenharmony_ci	case IVTV_YUV_MODE_PROGRESSIVE: /* Progressive mode */
10018c2ecf20Sopenharmony_ci		nf->interlaced = 0;
10028c2ecf20Sopenharmony_ci		if (nf->tru_h < 512 || (nf->tru_h > 576 && nf->tru_h < 1021))
10038c2ecf20Sopenharmony_ci			nf->interlaced_y = 0;
10048c2ecf20Sopenharmony_ci		else
10058c2ecf20Sopenharmony_ci			nf->interlaced_y = 1;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci		if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
10088c2ecf20Sopenharmony_ci			nf->interlaced_uv = 0;
10098c2ecf20Sopenharmony_ci		else
10108c2ecf20Sopenharmony_ci			nf->interlaced_uv = 1;
10118c2ecf20Sopenharmony_ci		break;
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	case IVTV_YUV_MODE_AUTO:
10148c2ecf20Sopenharmony_ci		if (nf->tru_h <= lace_threshold || nf->tru_h > 576 || nf->tru_w > 720) {
10158c2ecf20Sopenharmony_ci			nf->interlaced = 0;
10168c2ecf20Sopenharmony_ci			if ((nf->tru_h < 512) ||
10178c2ecf20Sopenharmony_ci			    (nf->tru_h > 576 && nf->tru_h < 1021) ||
10188c2ecf20Sopenharmony_ci			    (nf->tru_w > 720 && nf->tru_h < 1021))
10198c2ecf20Sopenharmony_ci				nf->interlaced_y = 0;
10208c2ecf20Sopenharmony_ci			else
10218c2ecf20Sopenharmony_ci				nf->interlaced_y = 1;
10228c2ecf20Sopenharmony_ci			if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
10238c2ecf20Sopenharmony_ci				nf->interlaced_uv = 0;
10248c2ecf20Sopenharmony_ci			else
10258c2ecf20Sopenharmony_ci				nf->interlaced_uv = 1;
10268c2ecf20Sopenharmony_ci		} else {
10278c2ecf20Sopenharmony_ci			nf->interlaced = 1;
10288c2ecf20Sopenharmony_ci			nf->interlaced_y = 1;
10298c2ecf20Sopenharmony_ci			nf->interlaced_uv = 1;
10308c2ecf20Sopenharmony_ci		}
10318c2ecf20Sopenharmony_ci		break;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	case IVTV_YUV_MODE_INTERLACED: /* Interlace mode */
10348c2ecf20Sopenharmony_ci	default:
10358c2ecf20Sopenharmony_ci		nf->interlaced = 1;
10368c2ecf20Sopenharmony_ci		nf->interlaced_y = 1;
10378c2ecf20Sopenharmony_ci		nf->interlaced_uv = 1;
10388c2ecf20Sopenharmony_ci		break;
10398c2ecf20Sopenharmony_ci	}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	if (memcmp(&yi->old_frame_info_args, nf, sizeof(*nf))) {
10428c2ecf20Sopenharmony_ci		yi->old_frame_info_args = *nf;
10438c2ecf20Sopenharmony_ci		nf->update = 1;
10448c2ecf20Sopenharmony_ci		IVTV_DEBUG_YUV("Requesting reg update for frame %d\n", frame);
10458c2ecf20Sopenharmony_ci	}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	nf->update |= update;
10488c2ecf20Sopenharmony_ci	nf->sync_field = yi->lace_sync_field;
10498c2ecf20Sopenharmony_ci	nf->delay = nf->sync_field != of->sync_field;
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci/* Frame is complete & ready for display */
10538c2ecf20Sopenharmony_civoid ivtv_yuv_frame_complete(struct ivtv *itv)
10548c2ecf20Sopenharmony_ci{
10558c2ecf20Sopenharmony_ci	atomic_set(&itv->yuv_info.next_fill_frame,
10568c2ecf20Sopenharmony_ci			(itv->yuv_info.draw_frame + 1) % IVTV_YUV_BUFFERS);
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_cistatic int ivtv_yuv_udma_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
10608c2ecf20Sopenharmony_ci{
10618c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
10628c2ecf20Sopenharmony_ci	int rc = 0;
10638c2ecf20Sopenharmony_ci	int got_sig = 0;
10648c2ecf20Sopenharmony_ci	/* DMA the frame */
10658c2ecf20Sopenharmony_ci	mutex_lock(&itv->udma.lock);
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	if ((rc = ivtv_yuv_prep_user_dma(itv, &itv->udma, args)) != 0) {
10688c2ecf20Sopenharmony_ci		mutex_unlock(&itv->udma.lock);
10698c2ecf20Sopenharmony_ci		return rc;
10708c2ecf20Sopenharmony_ci	}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	ivtv_udma_prepare(itv);
10738c2ecf20Sopenharmony_ci	prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
10748c2ecf20Sopenharmony_ci	/* if no UDMA is pending and no UDMA is in progress, then the DMA
10758c2ecf20Sopenharmony_ci	   is finished */
10768c2ecf20Sopenharmony_ci	while (test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags) ||
10778c2ecf20Sopenharmony_ci	       test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
10788c2ecf20Sopenharmony_ci		/* don't interrupt if the DMA is in progress but break off
10798c2ecf20Sopenharmony_ci		   a still pending DMA. */
10808c2ecf20Sopenharmony_ci		got_sig = signal_pending(current);
10818c2ecf20Sopenharmony_ci		if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
10828c2ecf20Sopenharmony_ci			break;
10838c2ecf20Sopenharmony_ci		got_sig = 0;
10848c2ecf20Sopenharmony_ci		schedule();
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ci	finish_wait(&itv->dma_waitq, &wait);
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	/* Unmap Last DMA Xfer */
10898c2ecf20Sopenharmony_ci	ivtv_udma_unmap(itv);
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	if (got_sig) {
10928c2ecf20Sopenharmony_ci		IVTV_DEBUG_INFO("User stopped YUV UDMA\n");
10938c2ecf20Sopenharmony_ci		mutex_unlock(&itv->udma.lock);
10948c2ecf20Sopenharmony_ci		return -EINTR;
10958c2ecf20Sopenharmony_ci	}
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	ivtv_yuv_frame_complete(itv);
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	mutex_unlock(&itv->udma.lock);
11008c2ecf20Sopenharmony_ci	return rc;
11018c2ecf20Sopenharmony_ci}
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci/* Setup frame according to V4L2 parameters */
11048c2ecf20Sopenharmony_civoid ivtv_yuv_setup_stream_frame(struct ivtv *itv)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
11078c2ecf20Sopenharmony_ci	struct ivtv_dma_frame dma_args;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	ivtv_yuv_next_free(itv);
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	/* Copy V4L2 parameters to an ivtv_dma_frame struct... */
11128c2ecf20Sopenharmony_ci	dma_args.y_source = NULL;
11138c2ecf20Sopenharmony_ci	dma_args.uv_source = NULL;
11148c2ecf20Sopenharmony_ci	dma_args.src.left = 0;
11158c2ecf20Sopenharmony_ci	dma_args.src.top = 0;
11168c2ecf20Sopenharmony_ci	dma_args.src.width = yi->v4l2_src_w;
11178c2ecf20Sopenharmony_ci	dma_args.src.height = yi->v4l2_src_h;
11188c2ecf20Sopenharmony_ci	dma_args.dst = yi->main_rect;
11198c2ecf20Sopenharmony_ci	dma_args.src_width = yi->v4l2_src_w;
11208c2ecf20Sopenharmony_ci	dma_args.src_height = yi->v4l2_src_h;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	/* ... and use the same setup routine as ivtv_yuv_prep_frame */
11238c2ecf20Sopenharmony_ci	ivtv_yuv_setup_frame(itv, &dma_args);
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	if (!itv->dma_data_req_offset)
11268c2ecf20Sopenharmony_ci		itv->dma_data_req_offset = yuv_offset[yi->draw_frame];
11278c2ecf20Sopenharmony_ci}
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci/* Attempt to dma a frame from a user buffer */
11308c2ecf20Sopenharmony_ciint ivtv_yuv_udma_stream_frame(struct ivtv *itv, void __user *src)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
11338c2ecf20Sopenharmony_ci	struct ivtv_dma_frame dma_args;
11348c2ecf20Sopenharmony_ci	int res;
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	ivtv_yuv_setup_stream_frame(itv);
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	/* We only need to supply source addresses for this */
11398c2ecf20Sopenharmony_ci	dma_args.y_source = src;
11408c2ecf20Sopenharmony_ci	dma_args.uv_source = src + 720 * ((yi->v4l2_src_h + 31) & ~31);
11418c2ecf20Sopenharmony_ci	/* Wait for frame DMA. Note that serialize_lock is locked,
11428c2ecf20Sopenharmony_ci	   so to allow other processes to access the driver while
11438c2ecf20Sopenharmony_ci	   we are waiting unlock first and later lock again. */
11448c2ecf20Sopenharmony_ci	mutex_unlock(&itv->serialize_lock);
11458c2ecf20Sopenharmony_ci	res = ivtv_yuv_udma_frame(itv, &dma_args);
11468c2ecf20Sopenharmony_ci	mutex_lock(&itv->serialize_lock);
11478c2ecf20Sopenharmony_ci	return res;
11488c2ecf20Sopenharmony_ci}
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci/* IVTV_IOC_DMA_FRAME ioctl handler */
11518c2ecf20Sopenharmony_ciint ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
11528c2ecf20Sopenharmony_ci{
11538c2ecf20Sopenharmony_ci	int res;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci/*	IVTV_DEBUG_INFO("yuv_prep_frame\n"); */
11568c2ecf20Sopenharmony_ci	ivtv_yuv_next_free(itv);
11578c2ecf20Sopenharmony_ci	ivtv_yuv_setup_frame(itv, args);
11588c2ecf20Sopenharmony_ci	/* Wait for frame DMA. Note that serialize_lock is locked,
11598c2ecf20Sopenharmony_ci	   so to allow other processes to access the driver while
11608c2ecf20Sopenharmony_ci	   we are waiting unlock first and later lock again. */
11618c2ecf20Sopenharmony_ci	mutex_unlock(&itv->serialize_lock);
11628c2ecf20Sopenharmony_ci	res = ivtv_yuv_udma_frame(itv, args);
11638c2ecf20Sopenharmony_ci	mutex_lock(&itv->serialize_lock);
11648c2ecf20Sopenharmony_ci	return res;
11658c2ecf20Sopenharmony_ci}
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_civoid ivtv_yuv_close(struct ivtv *itv)
11688c2ecf20Sopenharmony_ci{
11698c2ecf20Sopenharmony_ci	struct yuv_playback_info *yi = &itv->yuv_info;
11708c2ecf20Sopenharmony_ci	int h_filter, v_filter_1, v_filter_2;
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	IVTV_DEBUG_YUV("ivtv_yuv_close\n");
11738c2ecf20Sopenharmony_ci	mutex_unlock(&itv->serialize_lock);
11748c2ecf20Sopenharmony_ci	ivtv_waitq(&itv->vsync_waitq);
11758c2ecf20Sopenharmony_ci	mutex_lock(&itv->serialize_lock);
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	yi->running = 0;
11788c2ecf20Sopenharmony_ci	atomic_set(&yi->next_dma_frame, -1);
11798c2ecf20Sopenharmony_ci	atomic_set(&yi->next_fill_frame, 0);
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	/* Reset registers we have changed so mpeg playback works */
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	/* If we fully restore this register, the display may remain active.
11848c2ecf20Sopenharmony_ci	   Restore, but set one bit to blank the video. Firmware will always
11858c2ecf20Sopenharmony_ci	   clear this bit when needed, so not a problem. */
11868c2ecf20Sopenharmony_ci	write_reg(yi->reg_2898 | 0x01000000, 0x2898);
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	write_reg(yi->reg_2834, 0x02834);
11898c2ecf20Sopenharmony_ci	write_reg(yi->reg_2838, 0x02838);
11908c2ecf20Sopenharmony_ci	write_reg(yi->reg_283c, 0x0283c);
11918c2ecf20Sopenharmony_ci	write_reg(yi->reg_2840, 0x02840);
11928c2ecf20Sopenharmony_ci	write_reg(yi->reg_2844, 0x02844);
11938c2ecf20Sopenharmony_ci	write_reg(yi->reg_2848, 0x02848);
11948c2ecf20Sopenharmony_ci	write_reg(yi->reg_2854, 0x02854);
11958c2ecf20Sopenharmony_ci	write_reg(yi->reg_285c, 0x0285c);
11968c2ecf20Sopenharmony_ci	write_reg(yi->reg_2864, 0x02864);
11978c2ecf20Sopenharmony_ci	write_reg(yi->reg_2870, 0x02870);
11988c2ecf20Sopenharmony_ci	write_reg(yi->reg_2874, 0x02874);
11998c2ecf20Sopenharmony_ci	write_reg(yi->reg_2890, 0x02890);
12008c2ecf20Sopenharmony_ci	write_reg(yi->reg_289c, 0x0289c);
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	write_reg(yi->reg_2918, 0x02918);
12038c2ecf20Sopenharmony_ci	write_reg(yi->reg_291c, 0x0291c);
12048c2ecf20Sopenharmony_ci	write_reg(yi->reg_2920, 0x02920);
12058c2ecf20Sopenharmony_ci	write_reg(yi->reg_2924, 0x02924);
12068c2ecf20Sopenharmony_ci	write_reg(yi->reg_2928, 0x02928);
12078c2ecf20Sopenharmony_ci	write_reg(yi->reg_292c, 0x0292c);
12088c2ecf20Sopenharmony_ci	write_reg(yi->reg_2930, 0x02930);
12098c2ecf20Sopenharmony_ci	write_reg(yi->reg_2934, 0x02934);
12108c2ecf20Sopenharmony_ci	write_reg(yi->reg_2938, 0x02938);
12118c2ecf20Sopenharmony_ci	write_reg(yi->reg_293c, 0x0293c);
12128c2ecf20Sopenharmony_ci	write_reg(yi->reg_2940, 0x02940);
12138c2ecf20Sopenharmony_ci	write_reg(yi->reg_2944, 0x02944);
12148c2ecf20Sopenharmony_ci	write_reg(yi->reg_2948, 0x02948);
12158c2ecf20Sopenharmony_ci	write_reg(yi->reg_294c, 0x0294c);
12168c2ecf20Sopenharmony_ci	write_reg(yi->reg_2950, 0x02950);
12178c2ecf20Sopenharmony_ci	write_reg(yi->reg_2954, 0x02954);
12188c2ecf20Sopenharmony_ci	write_reg(yi->reg_2958, 0x02958);
12198c2ecf20Sopenharmony_ci	write_reg(yi->reg_295c, 0x0295c);
12208c2ecf20Sopenharmony_ci	write_reg(yi->reg_2960, 0x02960);
12218c2ecf20Sopenharmony_ci	write_reg(yi->reg_2964, 0x02964);
12228c2ecf20Sopenharmony_ci	write_reg(yi->reg_2968, 0x02968);
12238c2ecf20Sopenharmony_ci	write_reg(yi->reg_296c, 0x0296c);
12248c2ecf20Sopenharmony_ci	write_reg(yi->reg_2970, 0x02970);
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	/* Prepare to restore filters */
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	/* First the horizontal filter */
12298c2ecf20Sopenharmony_ci	if ((yi->reg_2834 & 0x0000FFFF) == (yi->reg_2834 >> 16)) {
12308c2ecf20Sopenharmony_ci		/* An exact size match uses filter 0 */
12318c2ecf20Sopenharmony_ci		h_filter = 0;
12328c2ecf20Sopenharmony_ci	} else {
12338c2ecf20Sopenharmony_ci		/* Figure out which filter to use */
12348c2ecf20Sopenharmony_ci		h_filter = ((yi->reg_2834 << 16) / (yi->reg_2834 >> 16)) >> 15;
12358c2ecf20Sopenharmony_ci		h_filter = (h_filter >> 1) + (h_filter & 1);
12368c2ecf20Sopenharmony_ci		/* Only an exact size match can use filter 0. */
12378c2ecf20Sopenharmony_ci		h_filter += !h_filter;
12388c2ecf20Sopenharmony_ci	}
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	/* Now the vertical filter */
12418c2ecf20Sopenharmony_ci	if ((yi->reg_2918 & 0x0000FFFF) == (yi->reg_2918 >> 16)) {
12428c2ecf20Sopenharmony_ci		/* An exact size match uses filter 0/1 */
12438c2ecf20Sopenharmony_ci		v_filter_1 = 0;
12448c2ecf20Sopenharmony_ci		v_filter_2 = 1;
12458c2ecf20Sopenharmony_ci	} else {
12468c2ecf20Sopenharmony_ci		/* Figure out which filter to use */
12478c2ecf20Sopenharmony_ci		v_filter_1 = ((yi->reg_2918 << 16) / (yi->reg_2918 >> 16)) >> 15;
12488c2ecf20Sopenharmony_ci		v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
12498c2ecf20Sopenharmony_ci		/* Only an exact size match can use filter 0 */
12508c2ecf20Sopenharmony_ci		v_filter_1 += !v_filter_1;
12518c2ecf20Sopenharmony_ci		v_filter_2 = v_filter_1;
12528c2ecf20Sopenharmony_ci	}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	/* Now restore the filters */
12558c2ecf20Sopenharmony_ci	ivtv_yuv_filter(itv, h_filter, v_filter_1, v_filter_2);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	/* and clear a few registers */
12588c2ecf20Sopenharmony_ci	write_reg(0, 0x02814);
12598c2ecf20Sopenharmony_ci	write_reg(0, 0x0282c);
12608c2ecf20Sopenharmony_ci	write_reg(0, 0x02904);
12618c2ecf20Sopenharmony_ci	write_reg(0, 0x02910);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	/* Release the blanking buffer */
12648c2ecf20Sopenharmony_ci	if (yi->blanking_ptr) {
12658c2ecf20Sopenharmony_ci		kfree(yi->blanking_ptr);
12668c2ecf20Sopenharmony_ci		yi->blanking_ptr = NULL;
12678c2ecf20Sopenharmony_ci		pci_unmap_single(itv->pdev, yi->blanking_dmaptr, 720*16, PCI_DMA_TODEVICE);
12688c2ecf20Sopenharmony_ci	}
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	/* Invalidate the old dimension information */
12718c2ecf20Sopenharmony_ci	yi->old_frame_info.src_w = 0;
12728c2ecf20Sopenharmony_ci	yi->old_frame_info.src_h = 0;
12738c2ecf20Sopenharmony_ci	yi->old_frame_info_args.src_w = 0;
12748c2ecf20Sopenharmony_ci	yi->old_frame_info_args.src_h = 0;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	/* All done. */
12778c2ecf20Sopenharmony_ci	clear_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
12788c2ecf20Sopenharmony_ci}
1279