summaryrefslogtreecommitdiff
path: root/board/freescale/common/diu_ch7301.c
blob: 02a271895bdc546ad6e4212e6637d3df98c9ff84 (plain)
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
34
35
36
37
38
39
40
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2014 Freescale Semiconductor, Inc.
 * Copyright 2019 NXP
 * Authors: Priyanka Jain <Priyanka.Jain@freescale.com>
 *	    Wang Dongsheng <dongsheng.wang@freescale.com>
 *
 * This file is copied and modified from the original t1040qds/diu.c.
 * Encoder can be used in T104x and LSx Platform.
 */

#include <common.h>
#include <stdio_dev.h>
#include <i2c.h>
#include <linux/delay.h>

#define I2C_DVI_INPUT_DATA_FORMAT_REG		0x1F
#define I2C_DVI_PLL_CHARGE_CNTL_REG		0x33
#define I2C_DVI_PLL_DIVIDER_REG			0x34
#define I2C_DVI_PLL_SUPPLY_CNTL_REG		0x35
#define I2C_DVI_PLL_FILTER_REG			0x36
#define I2C_DVI_TEST_PATTERN_REG		0x48
#define I2C_DVI_POWER_MGMT_REG			0x49
#define I2C_DVI_LOCK_STATE_REG			0x4D
#define I2C_DVI_SYNC_POLARITY_REG		0x56

/*
 * Set VSYNC/HSYNC to active high. This is polarity of sync signals
 * from DIU->DVI. The DIU default is active igh, so DVI is set to
 * active high.
 */
#define I2C_DVI_INPUT_DATA_FORMAT_VAL		0x98

#define I2C_DVI_PLL_CHARGE_CNTL_HIGH_SPEED_VAL	0x06
#define I2C_DVI_PLL_DIVIDER_HIGH_SPEED_VAL	0x26
#define I2C_DVI_PLL_FILTER_HIGH_SPEED_VAL	0xA0
#define I2C_DVI_PLL_CHARGE_CNTL_LOW_SPEED_VAL	0x08
#define I2C_DVI_PLL_DIVIDER_LOW_SPEED_VAL	0x16
#define I2C_DVI_PLL_FILTER_LOW_SPEED_VAL	0x60

/* Clear test pattern */
#define I2C_DVI_TEST_PATTERN_VAL		0x18
/* Exit Power-down mode */
#define I2C_DVI_POWER_MGMT_VAL			0xC0

/* Monitor polarity is handled via DVI Sync Polarity Register */
#define I2C_DVI_SYNC_POLARITY_VAL		0x00

/* Programming of HDMI Chrontel CH7301 connector */
int diu_set_dvi_encoder(unsigned int pixclock)
{
	int ret;
	u8 temp;

	temp = I2C_DVI_TEST_PATTERN_VAL;
#ifdef CONFIG_DM_I2C
	struct udevice *dev;

	ret = i2c_get_chip_for_busnum(CONFIG_SYS_I2C_DVI_BUS_NUM,
				      CONFIG_SYS_I2C_DVI_ADDR,
				      1, &dev);
	if (ret) {
		printf("%s: Cannot find udev for a bus %d\n", __func__,
		       CONFIG_SYS_I2C_DVI_BUS_NUM);
		return ret;
	}
	ret = dm_i2c_write(dev, I2C_DVI_TEST_PATTERN_REG, &temp, 1);
	if (ret) {
		puts("I2C: failed to select proper dvi test pattern\n");
		return ret;
	}
	temp = I2C_DVI_INPUT_DATA_FORMAT_VAL;
	ret = dm_i2c_write(dev, I2C_DVI_INPUT_DATA_FORMAT_REG, &temp, 1);
	if (ret) {
		puts("I2C: failed to select dvi input data format\n");
		return ret;
	}

	/* Set Sync polarity register */
	temp = I2C_DVI_SYNC_POLARITY_VAL;
	ret = dm_i2c_write(dev, I2C_DVI_SYNC_POLARITY_REG, &temp, 1);
	if (ret) {
		puts("I2C: failed to select dvi syc polarity\n");
		return ret;
	}

	/* Set PLL registers based on pixel clock rate*/
	if (pixclock > 65000000) {
		temp = I2C_DVI_PLL_CHARGE_CNTL_HIGH_SPEED_VAL;
		ret = dm_i2c_write(dev, I2C_DVI_PLL_CHARGE_CNTL_REG, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll charge_cntl\n");
			return ret;
		}
		temp = I2C_DVI_PLL_DIVIDER_HIGH_SPEED_VAL;
		ret = dm_i2c_write(dev, I2C_DVI_PLL_DIVIDER_REG, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll divider\n");
			return ret;
		}
		temp = I2C_DVI_PLL_FILTER_HIGH_SPEED_VAL;
		ret = dm_i2c_write(dev, I2C_DVI_PLL_FILTER_REG, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll filter\n");
			return ret;
		}
	} else {
		temp = I2C_DVI_PLL_CHARGE_CNTL_LOW_SPEED_VAL;
		ret = dm_i2c_write(dev, I2C_DVI_PLL_CHARGE_CNTL_REG, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll charge_cntl\n");
			return ret;
		}
		temp = I2C_DVI_PLL_DIVIDER_LOW_SPEED_VAL;
		ret = dm_i2c_write(dev, I2C_DVI_PLL_DIVIDER_REG, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll divider\n");
			return ret;
		}
		temp = I2C_DVI_PLL_FILTER_LOW_SPEED_VAL;
		ret = dm_i2c_write(dev, I2C_DVI_PLL_FILTER_REG, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll filter\n");
			return ret;
		}
	}

	temp = I2C_DVI_POWER_MGMT_VAL;
	ret = dm_i2c_write(dev, I2C_DVI_POWER_MGMT_REG, &temp, 1);
	if (ret) {
		puts("I2C: failed to select dvi power mgmt\n");
		return ret;
	}
#else
	ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR, I2C_DVI_TEST_PATTERN_REG, 1,
			&temp, 1);
	if (ret) {
		puts("I2C: failed to select proper dvi test pattern\n");
		return ret;
	}
	temp = I2C_DVI_INPUT_DATA_FORMAT_VAL;
	ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR, I2C_DVI_INPUT_DATA_FORMAT_REG,
			1, &temp, 1);
	if (ret) {
		puts("I2C: failed to select dvi input data format\n");
		return ret;
	}

	/* Set Sync polarity register */
	temp = I2C_DVI_SYNC_POLARITY_VAL;
	ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR, I2C_DVI_SYNC_POLARITY_REG, 1,
			&temp, 1);
	if (ret) {
		puts("I2C: failed to select dvi syc polarity\n");
		return ret;
	}

	/* Set PLL registers based on pixel clock rate*/
	if (pixclock > 65000000) {
		temp = I2C_DVI_PLL_CHARGE_CNTL_HIGH_SPEED_VAL;
		ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR,
				I2C_DVI_PLL_CHARGE_CNTL_REG, 1,	&temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll charge_cntl\n");
			return ret;
		}
		temp = I2C_DVI_PLL_DIVIDER_HIGH_SPEED_VAL;
		ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR,
				I2C_DVI_PLL_DIVIDER_REG, 1, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll divider\n");
			return ret;
		}
		temp = I2C_DVI_PLL_FILTER_HIGH_SPEED_VAL;
		ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR,
				I2C_DVI_PLL_FILTER_REG, 1, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll filter\n");
			return ret;
		}
	} else {
		temp = I2C_DVI_PLL_CHARGE_CNTL_LOW_SPEED_VAL;
		ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR,
				I2C_DVI_PLL_CHARGE_CNTL_REG, 1, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll charge_cntl\n");
			return ret;
		}
		temp = I2C_DVI_PLL_DIVIDER_LOW_SPEED_VAL;
		ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR,
				I2C_DVI_PLL_DIVIDER_REG, 1, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll divider\n");
			return ret;
		}
		temp = I2C_DVI_PLL_FILTER_LOW_SPEED_VAL;
		ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR,
				I2C_DVI_PLL_FILTER_REG, 1, &temp, 1);
		if (ret) {
			puts("I2C: failed to select dvi pll filter\n");
			return ret;
		}
	}

	temp = I2C_DVI_POWER_MGMT_VAL;
	ret = i2c_write(CONFIG_SYS_I2C_DVI_ADDR, I2C_DVI_POWER_MGMT_REG, 1,
			&temp, 1);
	if (ret) {
		puts("I2C: failed to select dvi power mgmt\n");
		return ret;
	}
#endif

	udelay(500);

	return 0;
}