<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

#   Copyright (C) 2008 Mauro Carvalho Chehab
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, version 2 of the License.
#
#   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.
#
# FIXME: need to properly map 32 bites aligned read/writes also.
#

use strict;

my $debug = 0;

my %reg_map = (
	0x101 =&gt; "SAA7134_INCR_DELAY",
	0x102 =&gt; "SAA7134_ANALOG_IN_CTRL1",
	0x103 =&gt; "SAA7134_ANALOG_IN_CTRL2",
	0x104 =&gt; "SAA7134_ANALOG_IN_CTRL3",
	0x105 =&gt; "SAA7134_ANALOG_IN_CTRL4",
	0x106 =&gt; "SAA7134_HSYNC_START",
	0x107 =&gt; "SAA7134_HSYNC_STOP",
	0x108 =&gt; "SAA7134_SYNC_CTRL",
	0x109 =&gt; "SAA7134_LUMA_CTRL",
	0x10a =&gt; "SAA7134_DEC_LUMA_BRIGHT",
	0x10b =&gt; "SAA7134_DEC_LUMA_CONTRAST",
	0x10c =&gt; "SAA7134_DEC_CHROMA_SATURATION",
	0x10d =&gt; "SAA7134_DEC_CHROMA_HUE",
	0x10e =&gt; "SAA7134_CHROMA_CTRL1",
	0x10f =&gt; "SAA7134_CHROMA_GAIN",
	0x110 =&gt; "SAA7134_CHROMA_CTRL2",
	0x111 =&gt; "SAA7134_MODE_DELAY_CTRL",
	0x114 =&gt; "SAA7134_ANALOG_ADC",
	0x115 =&gt; "SAA7134_VGATE_START",
	0x116 =&gt; "SAA7134_VGATE_STOP",
	0x117 =&gt; "SAA7134_MISC_VGATE_MSB",
	0x118 =&gt; "SAA7134_RAW_DATA_GAIN",
	0x119 =&gt; "SAA7134_RAW_DATA_OFFSET",
	0x11e =&gt; "SAA7134_STATUS_VIDEO1",
	0x11f =&gt; "SAA7134_STATUS_VIDEO2",
	0x300 =&gt; "SAA7134_OFMT_VIDEO_A",
	0x301 =&gt; "SAA7134_OFMT_DATA_A",
	0x302 =&gt; "SAA7134_OFMT_VIDEO_B",
	0x303 =&gt; "SAA7134_OFMT_DATA_B",
	0x304 =&gt; "SAA7134_ALPHA_NOCLIP",
	0x305 =&gt; "SAA7134_ALPHA_CLIP",
	0x308 =&gt; "SAA7134_UV_PIXEL",
	0x309 =&gt; "SAA7134_CLIP_RED",
	0x30a =&gt; "SAA7134_CLIP_GREEN",
	0x30b =&gt; "SAA7134_CLIP_BLUE",
	0x180 =&gt; "SAA7134_I2C_ATTR_STATUS",
	0x181 =&gt; "SAA7134_I2C_DATA",
	0x182 =&gt; "SAA7134_I2C_CLOCK_SELECT",
	0x183 =&gt; "SAA7134_I2C_TIMER",
	0x140 =&gt; "SAA7134_NICAM_ADD_DATA1",
	0x141 =&gt; "SAA7134_NICAM_ADD_DATA2",
	0x142 =&gt; "SAA7134_NICAM_STATUS",
	0x143 =&gt; "SAA7134_AUDIO_STATUS",
	0x144 =&gt; "SAA7134_NICAM_ERROR_COUNT",
	0x145 =&gt; "SAA7134_IDENT_SIF",
	0x146 =&gt; "SAA7134_LEVEL_READOUT1",
	0x147 =&gt; "SAA7134_LEVEL_READOUT2",
	0x148 =&gt; "SAA7134_NICAM_ERROR_LOW",
	0x149 =&gt; "SAA7134_NICAM_ERROR_HIGH",
	0x14a =&gt; "SAA7134_DCXO_IDENT_CTRL",
	0x14b =&gt; "SAA7134_DEMODULATOR",
	0x14c =&gt; "SAA7134_AGC_GAIN_SELECT",
	0x150 =&gt; "SAA7134_CARRIER1_FREQ0",
	0x151 =&gt; "SAA7134_CARRIER1_FREQ1",
	0x152 =&gt; "SAA7134_CARRIER1_FREQ2",
	0x154 =&gt; "SAA7134_CARRIER2_FREQ0",
	0x155 =&gt; "SAA7134_CARRIER2_FREQ1",
	0x156 =&gt; "SAA7134_CARRIER2_FREQ2",
	0x158 =&gt; "SAA7134_NUM_SAMPLES0",
	0x159 =&gt; "SAA7134_NUM_SAMPLES1",
	0x15a =&gt; "SAA7134_NUM_SAMPLES2",
	0x15b =&gt; "SAA7134_AUDIO_FORMAT_CTRL",
	0x160 =&gt; "SAA7134_MONITOR_SELECT",
	0x161 =&gt; "SAA7134_FM_DEEMPHASIS",
	0x162 =&gt; "SAA7134_FM_DEMATRIX",
	0x163 =&gt; "SAA7134_CHANNEL1_LEVEL",
	0x164 =&gt; "SAA7134_CHANNEL2_LEVEL",
	0x165 =&gt; "SAA7134_NICAM_CONFIG",
	0x166 =&gt; "SAA7134_NICAM_LEVEL_ADJUST",
	0x167 =&gt; "SAA7134_STEREO_DAC_OUTPUT_SELECT",
	0x168 =&gt; "SAA7134_I2S_OUTPUT_FORMAT",
	0x169 =&gt; "SAA7134_I2S_OUTPUT_SELECT",
	0x16a =&gt; "SAA7134_I2S_OUTPUT_LEVEL",
	0x16b =&gt; "SAA7134_DSP_OUTPUT_SELECT",
	0x16c =&gt; "SAA7134_AUDIO_MUTE_CTRL",
	0x16d =&gt; "SAA7134_SIF_SAMPLE_FREQ",
	0x16e =&gt; "SAA7134_ANALOG_IO_SELECT",
	0x170 =&gt; "SAA7134_AUDIO_CLOCK0",
	0x171 =&gt; "SAA7134_AUDIO_CLOCK1",
	0x172 =&gt; "SAA7134_AUDIO_CLOCK2",
	0x173 =&gt; "SAA7134_AUDIO_PLL_CTRL",
	0x174 =&gt; "SAA7134_AUDIO_CLOCKS_PER_FIELD0",
	0x175 =&gt; "SAA7134_AUDIO_CLOCKS_PER_FIELD1",
	0x176 =&gt; "SAA7134_AUDIO_CLOCKS_PER_FIELD2",
	0x190 =&gt; "SAA7134_VIDEO_PORT_CTRL0",
	0x191 =&gt; "SAA7134_VIDEO_PORT_CTRL1",
	0x192 =&gt; "SAA7134_VIDEO_PORT_CTRL2",
	0x193 =&gt; "SAA7134_VIDEO_PORT_CTRL3",
	0x194 =&gt; "SAA7134_VIDEO_PORT_CTRL4",
	0x195 =&gt; "SAA7134_VIDEO_PORT_CTRL5",
	0x196 =&gt; "SAA7134_VIDEO_PORT_CTRL6",
	0x197 =&gt; "SAA7134_VIDEO_PORT_CTRL7",
	0x198 =&gt; "SAA7134_VIDEO_PORT_CTRL8",
	0x1a0 =&gt; "SAA7134_TS_PARALLEL",
	0x1a1 =&gt; "SAA7134_TS_PARALLEL_SERIAL",
	0x1a2 =&gt; "SAA7134_TS_SERIAL0",
	0x1a3 =&gt; "SAA7134_TS_SERIAL1",
	0x1a4 =&gt; "SAA7134_TS_DMA0",
	0x1a5 =&gt; "SAA7134_TS_DMA1",
	0x1a6 =&gt; "SAA7134_TS_DMA2",
	0x1B0 =&gt; "SAA7134_GPIO_GPMODE0",
	0x1B1 =&gt; "SAA7134_GPIO_GPMODE1",
	0x1B2 =&gt; "SAA7134_GPIO_GPMODE2",
	0x1B3 =&gt; "SAA7134_GPIO_GPMODE3",
	0x1B4 =&gt; "SAA7134_GPIO_GPSTATUS0",
	0x1B5 =&gt; "SAA7134_GPIO_GPSTATUS1",
	0x1B6 =&gt; "SAA7134_GPIO_GPSTATUS2",
	0x1B7 =&gt; "SAA7134_GPIO_GPSTATUS3",
	0x1c0 =&gt; "SAA7134_I2S_AUDIO_OUTPUT",
	0x1d0 =&gt; "SAA7134_SPECIAL_MODE",
	0x1d1 =&gt; "SAA7134_PRODUCTION_TEST_MODE",
	0x580 =&gt; "SAA7135_DSP_RWSTATE",
	0x586 =&gt; "SAA7135_DSP_RWCLEAR",
	0x591 =&gt; "SAA7133_I2S_AUDIO_CONTROL",
);

my %i2c_status = (
	0  =&gt; "IDLE",
	1  =&gt; "DONE_STOP",
	2  =&gt; "BUSY",
	3  =&gt; "TO_SCL",
	4  =&gt; "TO_ARB",
	5  =&gt; "DONE_WRITE",
	6  =&gt; "DONE_READ",
	7  =&gt; "DONE_WRITE_TO",
	8  =&gt; "DONE_READ_TO",
	9  =&gt; "NO_DEVICE",
	10 =&gt; "NO_ACKN",
	11 =&gt; "BUS_ERR",
	12 =&gt; "ARB_LOST",
	13 =&gt; "SEQ_ERR",
	14 =&gt; "ST_ERR",
	15 =&gt; "SW_ERR",
);

my %i2c_attr = (
	0 =&gt; "NOP",
	1 =&gt; "STOP",
	2 =&gt; "CONTINUE",
	3 =&gt; "START",
);

my $addr = -1;
my $write = 0;
my $direction;
my @buf;

sub flush_i2c_transaction($$)
{
	my $direction = shift;
	my $is_complete = shift;

	my $size = scalar(@buf);

	if ($direction == 0) {
		my $v = shift @buf;
		printf("write_i2c_addr(0x%02x, %d, { 0x%02x",
			$addr, $size, $v);
		while (scalar(@buf)) {
			my $v = shift @buf;
			printf(", 0x%02x", $v);
		}
		printf("});");
	} else {
		my $size = scalar(@buf);
		my $v = shift @buf;
		printf("read_i2c_addr(0x%02x, %d) /* 0x%02x",
			$addr, $size, $v);
		while (scalar(@buf)) {
			my $v = shift @buf;
			printf(", 0x%02x", $v);
		}
		printf(" */;");
	}
	@buf = ();
	$addr = -1;

	printf (" /* INCOMPLETE */") if (!$is_complete);
	printf "\n";
}
sub parse_i2c($$$$$)
{
	my $time = shift;
	my $optype = shift;
	my $align = shift;
	my $reg = shift;
	my $val = shift;
	my $discard = 0;

	if ($align ne 'l') {
		print("FIXME: currently, parser work only with 32 bits i2c reg $optype\n");
		return;
	}

	my $status = $i2c_status{$val &amp; 0x0f};
	my $attr = $i2c_attr{($val &gt;&gt; 6) &amp; 0x03};

	# Avoid poluting the logs with busy msgs
	$discard = 1 if ($status eq "BUSY" | $status eq "TO_SCL" | $status eq "TO_ARB");

	# Avoid poluting the logs with read msgs during write
	$discard = 1 if ($optype eq "read" &amp;&amp; $status eq "DONE_WRITE");

	# Avoid poluting the logs with NOP operations
	$discard = 1 if ($attr eq "NOP");

	# Prints I2C raw transaction
	if ($debug &gt;= 1) {
		return if ($debug == 1 &amp;&amp; $discard);
		if ($optype eq "write") {
			printf("write_i2c(%s, %s, 0x%02x)\t/* %s */\n", $status, $attr, $val &gt;&gt; 8, $time);
		} else {
			printf("val = read_i2c()\t/* %s: read %s, %s, val=0x%02x */\n", $time, $status, $attr, $val &gt;&gt; 8);
		}
	}
	return if ($discard);
	$val &gt;&gt;= 8;

	if (($attr eq "START") &amp;&amp; ($optype eq "write")) {
		flush_i2c_transaction(0, 0) if (scalar(@buf) &amp;&amp; $addr &gt;= 0);

		$direction = $val &amp; 1;
		$addr = $val &gt;&gt; 1;
	}
	# Prints I2C transaction. This state machine is not 100%
	# it is known to fail with eeprom access
	if (($direction == 0) &amp;&amp; ($optype eq "write")) {
		if ($attr eq "CONTINUE") {
			# Discard transactions on the wrong direction
			push @buf, $val;
		} elsif ($attr eq "STOP" &amp;&amp; $addr &gt;= 0) {
			flush_i2c_transaction(0, 1);
		}
	} elsif ($direction == 1) {
		if ($optype eq "write") {
			$write = 1;
			return;
		}
		return if (!$write);
		$write = 0;

		if ($attr eq "CONTINUE") {
			push @buf, $val;
		} elsif ($attr eq "STOP" &amp;&amp; $addr &gt;= 0) {
			flush_i2c_transaction(1, 1);
		}
	}
}

while (&lt;&gt;) {
	# 1286074716 slow_bar_read.: slow_bar_readl addr=0x0000000000000180 val=0x00008182
	if (m/^(\d+)\s+slow_bar_(read|write)(.).*addr=([^\s]+)\s+val=([^\s]+)/) {
		my $time = $1;
		my $optype = $2;
		my $align = $3;
		my $op = "$2$3";
		my $reg = hex($4);
		my $val = hex($5);
		if (defined($reg_map{$reg})) {
			$reg = $reg_map{$reg};
		} else {
			$reg = sprintf("0x%04x", $reg);
		}
		if ($reg =~ m/SAA7134_I2C_ATTR_STATUS/) {
			parse_i2c($time, $optype, $align, $reg, $val);
		} elsif ($optype eq "read") {
			printf ("reg = saa_%s(%s);	/* %s: read 0x%04x */\n", $op, $reg, $time, $val);
		} else {
			printf ("saa_%s(%s, 0x%04x);	/* %s */\n", $op, $reg, $val, $time);
		}
	}
}
</pre></body></html>