drm/vc4: add extcon hdmi connection uevent
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / drm_scdc_helper.c
1 /*
2  * Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
3  *
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:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
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.
22  */
23
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26
27 #include <drm/drm_print.h>
28 #include <drm/drm_scdc_helper.h>
29
30 /**
31  * DOC: scdc helpers
32  *
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.
37  *
38  * Note: The SCDC status is going to be lost when the display is
39  * disconnected. This can happen physically when the user disconnects
40  * the cable, but also when a display is switched on (such as waking up
41  * a TV).
42  *
43  * This is further complicated by the fact that, upon a disconnection /
44  * reconnection, KMS won't change the mode on its own. This means that
45  * one can't just rely on setting the SCDC status on enable, but also
46  * has to track the connector status changes using interrupts and
47  * restore the SCDC status. The typical solution for this is to trigger an
48  * empty modeset in drm_connector_helper_funcs.detect_ctx(), like what vc4 does
49  * in vc4_hdmi_reset_link().
50  */
51
52 #define SCDC_I2C_SLAVE_ADDRESS 0x54
53
54 /**
55  * drm_scdc_read - read a block of data from SCDC
56  * @adapter: I2C controller
57  * @offset: start offset of block to read
58  * @buffer: return location for the block to read
59  * @size: size of the block to read
60  *
61  * Reads a block of data from SCDC, starting at a given offset.
62  *
63  * Returns:
64  * 0 on success, negative error code on failure.
65  */
66 ssize_t drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer,
67                       size_t size)
68 {
69         int ret;
70         struct i2c_msg msgs[2] = {
71                 {
72                         .addr = SCDC_I2C_SLAVE_ADDRESS,
73                         .flags = 0,
74                         .len = 1,
75                         .buf = &offset,
76                 }, {
77                         .addr = SCDC_I2C_SLAVE_ADDRESS,
78                         .flags = I2C_M_RD,
79                         .len = size,
80                         .buf = buffer,
81                 }
82         };
83
84         ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
85         if (ret < 0)
86                 return ret;
87         if (ret != ARRAY_SIZE(msgs))
88                 return -EPROTO;
89
90         return 0;
91 }
92 EXPORT_SYMBOL(drm_scdc_read);
93
94 /**
95  * drm_scdc_write - write a block of data to SCDC
96  * @adapter: I2C controller
97  * @offset: start offset of block to write
98  * @buffer: block of data to write
99  * @size: size of the block to write
100  *
101  * Writes a block of data to SCDC, starting at a given offset.
102  *
103  * Returns:
104  * 0 on success, negative error code on failure.
105  */
106 ssize_t drm_scdc_write(struct i2c_adapter *adapter, u8 offset,
107                        const void *buffer, size_t size)
108 {
109         struct i2c_msg msg = {
110                 .addr = SCDC_I2C_SLAVE_ADDRESS,
111                 .flags = 0,
112                 .len = 1 + size,
113                 .buf = NULL,
114         };
115         void *data;
116         int err;
117
118         data = kmalloc(1 + size, GFP_KERNEL);
119         if (!data)
120                 return -ENOMEM;
121
122         msg.buf = data;
123
124         memcpy(data, &offset, sizeof(offset));
125         memcpy(data + 1, buffer, size);
126
127         err = i2c_transfer(adapter, &msg, 1);
128
129         kfree(data);
130
131         if (err < 0)
132                 return err;
133         if (err != 1)
134                 return -EPROTO;
135
136         return 0;
137 }
138 EXPORT_SYMBOL(drm_scdc_write);
139
140 /**
141  * drm_scdc_get_scrambling_status - what is status of scrambling?
142  * @adapter: I2C adapter for DDC channel
143  *
144  * Reads the scrambler status over SCDC, and checks the
145  * scrambling status.
146  *
147  * Returns:
148  * True if the scrambling is enabled, false otherwise.
149  */
150 bool drm_scdc_get_scrambling_status(struct i2c_adapter *adapter)
151 {
152         u8 status;
153         int ret;
154
155         ret = drm_scdc_readb(adapter, SCDC_SCRAMBLER_STATUS, &status);
156         if (ret < 0) {
157                 DRM_DEBUG_KMS("Failed to read scrambling status: %d\n", ret);
158                 return false;
159         }
160
161         return status & SCDC_SCRAMBLING_STATUS;
162 }
163 EXPORT_SYMBOL(drm_scdc_get_scrambling_status);
164
165 /**
166  * drm_scdc_set_scrambling - enable scrambling
167  * @adapter: I2C adapter for DDC channel
168  * @enable: bool to indicate if scrambling is to be enabled/disabled
169  *
170  * Writes the TMDS config register over SCDC channel, and:
171  * enables scrambling when enable = 1
172  * disables scrambling when enable = 0
173  *
174  * Returns:
175  * True if scrambling is set/reset successfully, false otherwise.
176  */
177 bool drm_scdc_set_scrambling(struct i2c_adapter *adapter, bool enable)
178 {
179         u8 config;
180         int ret;
181
182         ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
183         if (ret < 0) {
184                 DRM_DEBUG_KMS("Failed to read TMDS config: %d\n", ret);
185                 return false;
186         }
187
188         if (enable)
189                 config |= SCDC_SCRAMBLING_ENABLE;
190         else
191                 config &= ~SCDC_SCRAMBLING_ENABLE;
192
193         ret = drm_scdc_writeb(adapter, SCDC_TMDS_CONFIG, config);
194         if (ret < 0) {
195                 DRM_DEBUG_KMS("Failed to enable scrambling: %d\n", ret);
196                 return false;
197         }
198
199         return true;
200 }
201 EXPORT_SYMBOL(drm_scdc_set_scrambling);
202
203 /**
204  * drm_scdc_set_high_tmds_clock_ratio - set TMDS clock ratio
205  * @adapter: I2C adapter for DDC channel
206  * @set: ret or reset the high clock ratio
207  *
208  *
209  *      TMDS clock ratio calculations go like this:
210  *              TMDS character = 10 bit TMDS encoded value
211  *
212  *              TMDS character rate = The rate at which TMDS characters are
213  *              transmitted (Mcsc)
214  *
215  *              TMDS bit rate = 10x TMDS character rate
216  *
217  *      As per the spec:
218  *              TMDS clock rate for pixel clock < 340 MHz = 1x the character
219  *              rate = 1/10 pixel clock rate
220  *
221  *              TMDS clock rate for pixel clock > 340 MHz = 0.25x the character
222  *              rate = 1/40 pixel clock rate
223  *
224  *      Writes to the TMDS config register over SCDC channel, and:
225  *              sets TMDS clock ratio to 1/40 when set = 1
226  *
227  *              sets TMDS clock ratio to 1/10 when set = 0
228  *
229  * Returns:
230  * True if write is successful, false otherwise.
231  */
232 bool drm_scdc_set_high_tmds_clock_ratio(struct i2c_adapter *adapter, bool set)
233 {
234         u8 config;
235         int ret;
236
237         ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
238         if (ret < 0) {
239                 DRM_DEBUG_KMS("Failed to read TMDS config: %d\n", ret);
240                 return false;
241         }
242
243         if (set)
244                 config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
245         else
246                 config &= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
247
248         ret = drm_scdc_writeb(adapter, SCDC_TMDS_CONFIG, config);
249         if (ret < 0) {
250                 DRM_DEBUG_KMS("Failed to set TMDS clock ratio: %d\n", ret);
251                 return false;
252         }
253
254         /*
255          * The spec says that a source should wait minimum 1ms and maximum
256          * 100ms after writing the TMDS config for clock ratio. Lets allow a
257          * wait of up to 2ms here.
258          */
259         usleep_range(1000, 2000);
260         return true;
261 }
262 EXPORT_SYMBOL(drm_scdc_set_high_tmds_clock_ratio);