2 * Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include <linux/slab.h>
25 #include <linux/delay.h>
27 #include <drm/drm_scdc_helper.h>
33 * Status and Control Data Channel (SCDC) is a mechanism introduced by the
34 * HDMI 2.0 specification. It is a point-to-point protocol that allows the
35 * HDMI source and HDMI sink to exchange data. The same I2C interface that
36 * is used to access EDID serves as the transport mechanism for SCDC.
39 #define SCDC_I2C_SLAVE_ADDRESS 0x54
42 * drm_scdc_read - read a block of data from SCDC
43 * @adapter: I2C controller
44 * @offset: start offset of block to read
45 * @buffer: return location for the block to read
46 * @size: size of the block to read
48 * Reads a block of data from SCDC, starting at a given offset.
51 * 0 on success, negative error code on failure.
53 ssize_t
drm_scdc_read(struct i2c_adapter
*adapter
, u8 offset
, void *buffer
,
57 struct i2c_msg msgs
[2] = {
59 .addr
= SCDC_I2C_SLAVE_ADDRESS
,
64 .addr
= SCDC_I2C_SLAVE_ADDRESS
,
71 ret
= i2c_transfer(adapter
, msgs
, ARRAY_SIZE(msgs
));
74 if (ret
!= ARRAY_SIZE(msgs
))
79 EXPORT_SYMBOL(drm_scdc_read
);
82 * drm_scdc_write - write a block of data to SCDC
83 * @adapter: I2C controller
84 * @offset: start offset of block to write
85 * @buffer: block of data to write
86 * @size: size of the block to write
88 * Writes a block of data to SCDC, starting at a given offset.
91 * 0 on success, negative error code on failure.
93 ssize_t
drm_scdc_write(struct i2c_adapter
*adapter
, u8 offset
,
94 const void *buffer
, size_t size
)
96 struct i2c_msg msg
= {
97 .addr
= SCDC_I2C_SLAVE_ADDRESS
,
105 data
= kmalloc(1 + size
, M_DRM
, GFP_KERNEL
);
111 memcpy(data
, &offset
, sizeof(offset
));
112 memcpy(data
+ 1, buffer
, size
);
114 err
= i2c_transfer(adapter
, &msg
, 1);
125 EXPORT_SYMBOL(drm_scdc_write
);
128 * drm_scdc_check_scrambling_status - what is status of scrambling?
129 * @adapter: I2C adapter for DDC channel
131 * Reads the scrambler status over SCDC, and checks the
135 * True if the scrambling is enabled, false otherwise.
137 bool drm_scdc_get_scrambling_status(struct i2c_adapter
*adapter
)
142 ret
= drm_scdc_readb(adapter
, SCDC_SCRAMBLER_STATUS
, &status
);
144 DRM_ERROR("Failed to read scrambling status: %d\n", ret
);
148 return status
& SCDC_SCRAMBLING_STATUS
;
150 EXPORT_SYMBOL(drm_scdc_get_scrambling_status
);
153 * drm_scdc_set_scrambling - enable scrambling
154 * @adapter: I2C adapter for DDC channel
155 * @enable: bool to indicate if scrambling is to be enabled/disabled
157 * Writes the TMDS config register over SCDC channel, and:
158 * enables scrambling when enable = 1
159 * disables scrambling when enable = 0
162 * True if scrambling is set/reset successfully, false otherwise.
164 bool drm_scdc_set_scrambling(struct i2c_adapter
*adapter
, bool enable
)
169 ret
= drm_scdc_readb(adapter
, SCDC_TMDS_CONFIG
, &config
);
171 DRM_ERROR("Failed to read TMDS config: %d\n", ret
);
176 config
|= SCDC_SCRAMBLING_ENABLE
;
178 config
&= ~SCDC_SCRAMBLING_ENABLE
;
180 ret
= drm_scdc_writeb(adapter
, SCDC_TMDS_CONFIG
, config
);
182 DRM_ERROR("Failed to enable scrambling: %d\n", ret
);
188 EXPORT_SYMBOL(drm_scdc_set_scrambling
);
191 * drm_scdc_set_high_tmds_clock_ratio - set TMDS clock ratio
192 * @adapter: I2C adapter for DDC channel
193 * @set: ret or reset the high clock ratio
196 * TMDS clock ratio calculations go like this:
197 * TMDS character = 10 bit TMDS encoded value
199 * TMDS character rate = The rate at which TMDS characters are
202 * TMDS bit rate = 10x TMDS character rate
205 * TMDS clock rate for pixel clock < 340 MHz = 1x the character
206 * rate = 1/10 pixel clock rate
208 * TMDS clock rate for pixel clock > 340 MHz = 0.25x the character
209 * rate = 1/40 pixel clock rate
211 * Writes to the TMDS config register over SCDC channel, and:
212 * sets TMDS clock ratio to 1/40 when set = 1
214 * sets TMDS clock ratio to 1/10 when set = 0
217 * True if write is successful, false otherwise.
219 bool drm_scdc_set_high_tmds_clock_ratio(struct i2c_adapter
*adapter
, bool set
)
224 ret
= drm_scdc_readb(adapter
, SCDC_TMDS_CONFIG
, &config
);
226 DRM_ERROR("Failed to read TMDS config: %d\n", ret
);
231 config
|= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40
;
233 config
&= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40
;
235 ret
= drm_scdc_writeb(adapter
, SCDC_TMDS_CONFIG
, config
);
237 DRM_ERROR("Failed to set TMDS clock ratio: %d\n", ret
);
242 * The spec says that a source should wait minimum 1ms and maximum
243 * 100ms after writing the TMDS config for clock ratio. Lets allow a
244 * wait of upto 2ms here.
246 usleep_range(1000, 2000);
249 EXPORT_SYMBOL(drm_scdc_set_high_tmds_clock_ratio
);