xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/drm_scdc_helper.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
12*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial portions
13*4882a593Smuzhiyun  * of the Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun  * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/delay.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <drm/drm_print.h>
28*4882a593Smuzhiyun #include <drm/drm_scdc_helper.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /**
31*4882a593Smuzhiyun  * DOC: scdc helpers
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Status and Control Data Channel (SCDC) is a mechanism introduced by the
34*4882a593Smuzhiyun  * HDMI 2.0 specification. It is a point-to-point protocol that allows the
35*4882a593Smuzhiyun  * HDMI source and HDMI sink to exchange data. The same I2C interface that
36*4882a593Smuzhiyun  * is used to access EDID serves as the transport mechanism for SCDC.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define SCDC_I2C_SLAVE_ADDRESS 0x54
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun  * drm_scdc_read - read a block of data from SCDC
43*4882a593Smuzhiyun  * @adapter: I2C controller
44*4882a593Smuzhiyun  * @offset: start offset of block to read
45*4882a593Smuzhiyun  * @buffer: return location for the block to read
46*4882a593Smuzhiyun  * @size: size of the block to read
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * Reads a block of data from SCDC, starting at a given offset.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Returns:
51*4882a593Smuzhiyun  * 0 on success, negative error code on failure.
52*4882a593Smuzhiyun  */
drm_scdc_read(struct i2c_adapter * adapter,u8 offset,void * buffer,size_t size)53*4882a593Smuzhiyun ssize_t drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer,
54*4882a593Smuzhiyun 		      size_t size)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	int ret;
57*4882a593Smuzhiyun 	struct i2c_msg msgs[2] = {
58*4882a593Smuzhiyun 		{
59*4882a593Smuzhiyun 			.addr = SCDC_I2C_SLAVE_ADDRESS,
60*4882a593Smuzhiyun 			.flags = 0,
61*4882a593Smuzhiyun 			.len = 1,
62*4882a593Smuzhiyun 			.buf = &offset,
63*4882a593Smuzhiyun 		}, {
64*4882a593Smuzhiyun 			.addr = SCDC_I2C_SLAVE_ADDRESS,
65*4882a593Smuzhiyun 			.flags = I2C_M_RD,
66*4882a593Smuzhiyun 			.len = size,
67*4882a593Smuzhiyun 			.buf = buffer,
68*4882a593Smuzhiyun 		}
69*4882a593Smuzhiyun 	};
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
72*4882a593Smuzhiyun 	if (ret < 0)
73*4882a593Smuzhiyun 		return ret;
74*4882a593Smuzhiyun 	if (ret != ARRAY_SIZE(msgs))
75*4882a593Smuzhiyun 		return -EPROTO;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return 0;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun EXPORT_SYMBOL(drm_scdc_read);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun  * drm_scdc_write - write a block of data to SCDC
83*4882a593Smuzhiyun  * @adapter: I2C controller
84*4882a593Smuzhiyun  * @offset: start offset of block to write
85*4882a593Smuzhiyun  * @buffer: block of data to write
86*4882a593Smuzhiyun  * @size: size of the block to write
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Writes a block of data to SCDC, starting at a given offset.
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * Returns:
91*4882a593Smuzhiyun  * 0 on success, negative error code on failure.
92*4882a593Smuzhiyun  */
drm_scdc_write(struct i2c_adapter * adapter,u8 offset,const void * buffer,size_t size)93*4882a593Smuzhiyun ssize_t drm_scdc_write(struct i2c_adapter *adapter, u8 offset,
94*4882a593Smuzhiyun 		       const void *buffer, size_t size)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct i2c_msg msg = {
97*4882a593Smuzhiyun 		.addr = SCDC_I2C_SLAVE_ADDRESS,
98*4882a593Smuzhiyun 		.flags = 0,
99*4882a593Smuzhiyun 		.len = 1 + size,
100*4882a593Smuzhiyun 		.buf = NULL,
101*4882a593Smuzhiyun 	};
102*4882a593Smuzhiyun 	void *data;
103*4882a593Smuzhiyun 	int err;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	data = kmalloc(1 + size, GFP_KERNEL);
106*4882a593Smuzhiyun 	if (!data)
107*4882a593Smuzhiyun 		return -ENOMEM;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	msg.buf = data;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	memcpy(data, &offset, sizeof(offset));
112*4882a593Smuzhiyun 	memcpy(data + 1, buffer, size);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	err = i2c_transfer(adapter, &msg, 1);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	kfree(data);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	if (err < 0)
119*4882a593Smuzhiyun 		return err;
120*4882a593Smuzhiyun 	if (err != 1)
121*4882a593Smuzhiyun 		return -EPROTO;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun EXPORT_SYMBOL(drm_scdc_write);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /**
128*4882a593Smuzhiyun  * drm_scdc_check_scrambling_status - what is status of scrambling?
129*4882a593Smuzhiyun  * @adapter: I2C adapter for DDC channel
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * Reads the scrambler status over SCDC, and checks the
132*4882a593Smuzhiyun  * scrambling status.
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * Returns:
135*4882a593Smuzhiyun  * True if the scrambling is enabled, false otherwise.
136*4882a593Smuzhiyun  */
drm_scdc_get_scrambling_status(struct i2c_adapter * adapter)137*4882a593Smuzhiyun bool drm_scdc_get_scrambling_status(struct i2c_adapter *adapter)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	u8 status;
140*4882a593Smuzhiyun 	int ret;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	ret = drm_scdc_readb(adapter, SCDC_SCRAMBLER_STATUS, &status);
143*4882a593Smuzhiyun 	if (ret < 0) {
144*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Failed to read scrambling status: %d\n", ret);
145*4882a593Smuzhiyun 		return false;
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	return status & SCDC_SCRAMBLING_STATUS;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun EXPORT_SYMBOL(drm_scdc_get_scrambling_status);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /**
153*4882a593Smuzhiyun  * drm_scdc_set_scrambling - enable scrambling
154*4882a593Smuzhiyun  * @adapter: I2C adapter for DDC channel
155*4882a593Smuzhiyun  * @enable: bool to indicate if scrambling is to be enabled/disabled
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * Writes the TMDS config register over SCDC channel, and:
158*4882a593Smuzhiyun  * enables scrambling when enable = 1
159*4882a593Smuzhiyun  * disables scrambling when enable = 0
160*4882a593Smuzhiyun  *
161*4882a593Smuzhiyun  * Returns:
162*4882a593Smuzhiyun  * True if scrambling is set/reset successfully, false otherwise.
163*4882a593Smuzhiyun  */
drm_scdc_set_scrambling(struct i2c_adapter * adapter,bool enable)164*4882a593Smuzhiyun bool drm_scdc_set_scrambling(struct i2c_adapter *adapter, bool enable)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	u8 config;
167*4882a593Smuzhiyun 	int ret;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
170*4882a593Smuzhiyun 	if (ret < 0) {
171*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Failed to read TMDS config: %d\n", ret);
172*4882a593Smuzhiyun 		return false;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (enable)
176*4882a593Smuzhiyun 		config |= SCDC_SCRAMBLING_ENABLE;
177*4882a593Smuzhiyun 	else
178*4882a593Smuzhiyun 		config &= ~SCDC_SCRAMBLING_ENABLE;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	ret = drm_scdc_writeb(adapter, SCDC_TMDS_CONFIG, config);
181*4882a593Smuzhiyun 	if (ret < 0) {
182*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Failed to enable scrambling: %d\n", ret);
183*4882a593Smuzhiyun 		return false;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	return true;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun EXPORT_SYMBOL(drm_scdc_set_scrambling);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun  * drm_scdc_set_high_tmds_clock_ratio - set TMDS clock ratio
192*4882a593Smuzhiyun  * @adapter: I2C adapter for DDC channel
193*4882a593Smuzhiyun  * @set: ret or reset the high clock ratio
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  *	TMDS clock ratio calculations go like this:
197*4882a593Smuzhiyun  *		TMDS character = 10 bit TMDS encoded value
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  *		TMDS character rate = The rate at which TMDS characters are
200*4882a593Smuzhiyun  *		transmitted (Mcsc)
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  *		TMDS bit rate = 10x TMDS character rate
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  *	As per the spec:
205*4882a593Smuzhiyun  *		TMDS clock rate for pixel clock < 340 MHz = 1x the character
206*4882a593Smuzhiyun  *		rate = 1/10 pixel clock rate
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  *		TMDS clock rate for pixel clock > 340 MHz = 0.25x the character
209*4882a593Smuzhiyun  *		rate = 1/40 pixel clock rate
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  *	Writes to the TMDS config register over SCDC channel, and:
212*4882a593Smuzhiyun  *		sets TMDS clock ratio to 1/40 when set = 1
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  *		sets TMDS clock ratio to 1/10 when set = 0
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * Returns:
217*4882a593Smuzhiyun  * True if write is successful, false otherwise.
218*4882a593Smuzhiyun  */
drm_scdc_set_high_tmds_clock_ratio(struct i2c_adapter * adapter,bool set)219*4882a593Smuzhiyun bool drm_scdc_set_high_tmds_clock_ratio(struct i2c_adapter *adapter, bool set)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	u8 config;
222*4882a593Smuzhiyun 	int ret;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
225*4882a593Smuzhiyun 	if (ret < 0) {
226*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Failed to read TMDS config: %d\n", ret);
227*4882a593Smuzhiyun 		return false;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	if (set)
231*4882a593Smuzhiyun 		config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
232*4882a593Smuzhiyun 	else
233*4882a593Smuzhiyun 		config &= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	ret = drm_scdc_writeb(adapter, SCDC_TMDS_CONFIG, config);
236*4882a593Smuzhiyun 	if (ret < 0) {
237*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Failed to set TMDS clock ratio: %d\n", ret);
238*4882a593Smuzhiyun 		return false;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/*
242*4882a593Smuzhiyun 	 * The spec says that a source should wait minimum 1ms and maximum
243*4882a593Smuzhiyun 	 * 100ms after writing the TMDS config for clock ratio. Lets allow a
244*4882a593Smuzhiyun 	 * wait of upto 2ms here.
245*4882a593Smuzhiyun 	 */
246*4882a593Smuzhiyun 	usleep_range(1000, 2000);
247*4882a593Smuzhiyun 	return true;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun EXPORT_SYMBOL(drm_scdc_set_high_tmds_clock_ratio);
250