• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  *    Zheng Yang <zhengyang@rock-chips.com>
5  *    Yakir Yang <ykk@rock-chips.com>
6  */
7 
8 #include <linux/irq.h>
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/hdmi.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of_device.h>
17 
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_probe_helper.h>
22 #include <drm/drm_simple_kms_helper.h>
23 
24 #include "rockchip_drm_drv.h"
25 #include "rockchip_drm_vop.h"
26 
27 #include "inno_hdmi.h"
28 
29 #define to_inno_hdmi(x) container_of(x, struct inno_hdmi, x)
30 
31 struct hdmi_data_info {
32     int vic;
33     bool sink_is_hdmi;
34     bool sink_has_audio;
35     unsigned int enc_in_format;
36     unsigned int enc_out_format;
37     unsigned int colorimetry;
38 };
39 
40 struct inno_hdmi_i2c {
41     struct i2c_adapter adap;
42 
43     u8 ddc_addr;
44     u8 segment_addr;
45 
46     struct mutex lock;
47     struct completion cmp;
48 };
49 
50 struct inno_hdmi {
51     struct device *dev;
52     struct drm_device *drm_dev;
53 
54     int irq;
55     struct clk *pclk;
56     void __iomem *regs;
57 
58     struct drm_connector connector;
59     struct drm_encoder encoder;
60 
61     struct inno_hdmi_i2c *i2c;
62     struct i2c_adapter *ddc;
63 
64     unsigned int tmds_rate;
65 
66     struct hdmi_data_info hdmi_data;
67     struct drm_display_mode previous_mode;
68 };
69 
70 enum {
71     CSC_ITU601_16_235_TO_RGB_0_255_8BIT,
72     CSC_ITU601_0_255_TO_RGB_0_255_8BIT,
73     CSC_ITU709_16_235_TO_RGB_0_255_8BIT,
74     CSC_RGB_0_255_TO_ITU601_16_235_8BIT,
75     CSC_RGB_0_255_TO_ITU709_16_235_8BIT,
76     CSC_RGB_0_255_TO_RGB_16_235_8BIT,
77 };
78 
79 static const char coeff_csc[][24] = {
80     /*
81      * YUV2RGB:601 SD mode(Y[16:235], UV[16:240], RGB[0:255]):
82      *   R = 1.164*Y + 1.596*V - 204
83      *   G = 1.164*Y - 0.391*U - 0.813*V + 154
84      *   B = 1.164*Y + 2.018*U - 258
85      */
86     {0x04, 0xa7, 0x00, 0x00, 0x06, 0x62, 0x02, 0xcc, 0x04, 0xa7, 0x11, 0x90,
87      0x13, 0x40, 0x00, 0x9a, 0x04, 0xa7, 0x08, 0x12, 0x00, 0x00, 0x03, 0x02},
88     /*
89      * YUV2RGB:601 SD mode(YUV[0:255],RGB[0:255]):
90      *   R = Y + 1.402*V - 248
91      *   G = Y - 0.344*U - 0.714*V + 135
92      *   B = Y + 1.772*U - 227
93      */
94     {0x04, 0x00, 0x00, 0x00, 0x05, 0x9b, 0x02, 0xf8, 0x04, 0x00, 0x11, 0x60,
95      0x12, 0xdb, 0x00, 0x87, 0x04, 0x00, 0x07, 0x16, 0x00, 0x00, 0x02, 0xe3},
96     /*
97      * YUV2RGB:709 HD mode(Y[16:235],UV[16:240],RGB[0:255]):
98      *   R = 1.164*Y + 1.793*V - 248
99      *   G = 1.164*Y - 0.213*U - 0.534*V + 77
100      *   B = 1.164*Y + 2.115*U - 289
101      */
102     {0x04, 0xa7, 0x00, 0x00, 0x07, 0x2c, 0x02, 0xf8, 0x04, 0xa7, 0x10, 0xda,
103      0x12, 0x22, 0x00, 0x4d, 0x04, 0xa7, 0x08, 0x74, 0x00, 0x00, 0x03, 0x21},
104 
105     /*
106      * RGB2YUV:601 SD mode:
107      *   Cb = -0.291G - 0.148R + 0.439B + 128
108      *   Y  = 0.504G  + 0.257R + 0.098B + 16
109      *   Cr = -0.368G + 0.439R - 0.071B + 128
110      */
111     {0x11, 0x5f, 0x01, 0x82, 0x10, 0x23, 0x00, 0x80, 0x02, 0x1c, 0x00, 0xa1,
112      0x00, 0x36, 0x00, 0x1e, 0x11, 0x29, 0x10, 0x59, 0x01, 0x82, 0x00, 0x80},
113     /*
114      * RGB2YUV:709 HD mode:
115      *   Cb = - 0.338G - 0.101R + 0.439B + 128
116      *   Y  = 0.614G   + 0.183R + 0.062B + 16
117      *   Cr = - 0.399G + 0.439R - 0.040B + 128
118      */
119     {0x11, 0x98, 0x01, 0xc1, 0x10, 0x28, 0x00, 0x80, 0x02, 0x74, 0x00, 0xbb,
120      0x00, 0x3f, 0x00, 0x10, 0x11, 0x5a, 0x10, 0x67, 0x01, 0xc1, 0x00, 0x80},
121     /*
122      * RGB[0:255]2RGB[16:235]:
123      *   R' = R x (235-16)/255 + 16;
124      *   G' = G x (235-16)/255 + 16;
125      *   B' = B x (235-16)/255 + 16;
126      */
127     {0x00, 0x00, 0x03, 0x6F, 0x00, 0x00, 0x00, 0x10, 0x03, 0x6F, 0x00, 0x00,
128      0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x03, 0x6F, 0x00, 0x10},
129 };
130 
hdmi_readb(struct inno_hdmi * hdmi,u16 offset)131 static inline u8 hdmi_readb(struct inno_hdmi *hdmi, u16 offset)
132 {
133     return readl_relaxed(hdmi->regs + (offset)*0x04);
134 }
135 
hdmi_writeb(struct inno_hdmi * hdmi,u16 offset,u32 val)136 static inline void hdmi_writeb(struct inno_hdmi *hdmi, u16 offset, u32 val)
137 {
138     writel_relaxed(val, hdmi->regs + (offset)*0x04);
139 }
140 
hdmi_modb(struct inno_hdmi * hdmi,u16 offset,u32 msk,u32 val)141 static inline void hdmi_modb(struct inno_hdmi *hdmi, u16 offset, u32 msk, u32 val)
142 {
143     u8 temp = hdmi_readb(hdmi, offset) & ~msk;
144 
145     temp |= val & msk;
146     hdmi_writeb(hdmi, offset, temp);
147 }
148 
inno_hdmi_i2c_init(struct inno_hdmi * hdmi)149 static void inno_hdmi_i2c_init(struct inno_hdmi *hdmi)
150 {
151     int ddc_bus_freq;
152 
153     ddc_bus_freq = (hdmi->tmds_rate >> 0x2) / HDMI_SCL_RATE;
154 
155     hdmi_writeb(hdmi, DDC_BUS_FREQ_L, ddc_bus_freq & 0xFF);
156     hdmi_writeb(hdmi, DDC_BUS_FREQ_H, (ddc_bus_freq >> 0x8) & 0xFF);
157 
158     /* Clear the EDID interrupt flag and mute the interrupt */
159     hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
160     hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
161 }
162 
inno_hdmi_sys_power(struct inno_hdmi * hdmi,bool enable)163 static void inno_hdmi_sys_power(struct inno_hdmi *hdmi, bool enable)
164 {
165     if (enable) {
166         hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_ON);
167     } else {
168         hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_OFF);
169     }
170 }
171 
inno_hdmi_set_pwr_mode(struct inno_hdmi * hdmi,int mode)172 static void inno_hdmi_set_pwr_mode(struct inno_hdmi *hdmi, int mode)
173 {
174     switch (mode) {
175         case NORMAL:
176             inno_hdmi_sys_power(hdmi, false);
177 
178             hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, 0x6f);
179             hdmi_writeb(hdmi, HDMI_PHY_DRIVER, 0xbb);
180 
181             hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x15);
182             hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x14);
183             hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x10);
184             hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x0f);
185             hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x00);
186             hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x01);
187 
188             inno_hdmi_sys_power(hdmi, true);
189             break;
190 
191         case LOWER_PWR:
192             inno_hdmi_sys_power(hdmi, false);
193             hdmi_writeb(hdmi, HDMI_PHY_DRIVER, 0x00);
194             hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, 0x00);
195             hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x00);
196             hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x15);
197 
198             break;
199 
200         default:
201             DRM_DEV_ERROR(hdmi->dev, "Unknown power mode %d\n", mode);
202     }
203 }
204 
inno_hdmi_reset(struct inno_hdmi * hdmi)205 static void inno_hdmi_reset(struct inno_hdmi *hdmi)
206 {
207     u32 val;
208     u32 msk;
209 
210     hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_DIGITAL, v_NOT_RST_DIGITAL);
211     udelay(0x64);
212 
213     hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_ANALOG, v_NOT_RST_ANALOG);
214     udelay(0x64);
215 
216     msk = m_REG_CLK_INV | m_REG_CLK_SOURCE | m_POWER | m_INT_POL;
217     val = v_REG_CLK_INV | v_REG_CLK_SOURCE_SYS | v_PWR_ON | v_INT_POL_HIGH;
218     hdmi_modb(hdmi, HDMI_SYS_CTRL, msk, val);
219 
220     inno_hdmi_set_pwr_mode(hdmi, NORMAL);
221 }
222 
inno_hdmi_upload_frame(struct inno_hdmi * hdmi,int setup_rc,union hdmi_infoframe * frame,u32 frame_index,u32 mask,u32 disable,u32 enable)223 static int inno_hdmi_upload_frame(struct inno_hdmi *hdmi, int setup_rc, union hdmi_infoframe *frame, u32 frame_index,
224                                   u32 mask, u32 disable, u32 enable)
225 {
226     if (mask) {
227         hdmi_modb(hdmi, HDMI_PACKET_SEND_AUTO, mask, disable);
228     }
229 
230     hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_BUF_INDEX, frame_index);
231 
232     if (setup_rc >= 0) {
233         u8 packed_frame[HDMI_MAXIMUM_INFO_FRAME_SIZE];
234         ssize_t rc, i;
235 
236         rc = hdmi_infoframe_pack(frame, packed_frame, sizeof(packed_frame));
237         if (rc < 0) {
238             return rc;
239         }
240 
241         for (i = 0; i < rc; i++) {
242             hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_ADDR + i, packed_frame[i]);
243         }
244 
245         if (mask) {
246             hdmi_modb(hdmi, HDMI_PACKET_SEND_AUTO, mask, enable);
247         }
248     }
249 
250     return setup_rc;
251 }
252 
inno_hdmi_config_video_vsi(struct inno_hdmi * hdmi,struct drm_display_mode * mode)253 static int inno_hdmi_config_video_vsi(struct inno_hdmi *hdmi, struct drm_display_mode *mode)
254 {
255     union hdmi_infoframe frame;
256     int rc;
257 
258     rc = drm_hdmi_vendor_infoframe_from_display_mode(&frame.vendor.hdmi, &hdmi->connector, mode);
259 
260     return inno_hdmi_upload_frame(hdmi, rc, &frame, INFOFRAME_VSI, m_PACKET_VSI_EN, v_PACKET_VSI_EN(0),
261                                   v_PACKET_VSI_EN(1));
262 }
263 
inno_hdmi_config_video_avi(struct inno_hdmi * hdmi,struct drm_display_mode * mode)264 static int inno_hdmi_config_video_avi(struct inno_hdmi *hdmi, struct drm_display_mode *mode)
265 {
266     union hdmi_infoframe frame;
267     int rc;
268 
269     rc = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, &hdmi->connector, mode);
270 
271     if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV444) {
272         frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
273     } else if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV422) {
274         frame.avi.colorspace = HDMI_COLORSPACE_YUV422;
275     } else {
276         frame.avi.colorspace = HDMI_COLORSPACE_RGB;
277     }
278 
279     return inno_hdmi_upload_frame(hdmi, rc, &frame, INFOFRAME_AVI, 0, 0, 0);
280 }
281 
inno_hdmi_config_video_csc(struct inno_hdmi * hdmi)282 static int inno_hdmi_config_video_csc(struct inno_hdmi *hdmi)
283 {
284     struct hdmi_data_info *data = &hdmi->hdmi_data;
285     int c0_c2_change = 0;
286     int csc_enable = 0;
287     int csc_mode = 0;
288     int auto_csc = 0;
289     int value;
290     int i;
291 
292     /* Input video mode is SDR RGB24bit, data enable signal from external */
293     hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL1, v_DE_EXTERNAL | v_VIDEO_INPUT_FORMAT(VIDEO_INPUT_SDR_RGB444));
294 
295     /* Input color hardcode to RGB, and output color hardcode to RGB888 */
296     value = v_VIDEO_INPUT_BITS(VIDEO_INPUT_8BITS) | v_VIDEO_OUTPUT_COLOR(0) | v_VIDEO_INPUT_CSP(0);
297     hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL2, value);
298 
299     if (data->enc_in_format == data->enc_out_format) {
300         if ((data->enc_in_format == HDMI_COLORSPACE_RGB) || (data->enc_in_format >= HDMI_COLORSPACE_YUV444)) {
301             value = v_SOF_DISABLE | v_COLOR_DEPTH_NOT_INDICATED(1);
302             hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
303 
304             hdmi_modb(hdmi, HDMI_VIDEO_CONTRL, m_VIDEO_AUTO_CSC | m_VIDEO_C0_C2_SWAP,
305                       v_VIDEO_AUTO_CSC(AUTO_CSC_DISABLE) | v_VIDEO_C0_C2_SWAP(C0_C2_CHANGE_DISABLE));
306             return 0;
307         }
308     }
309 
310     if (data->colorimetry == HDMI_COLORIMETRY_ITU_601) {
311         if ((data->enc_in_format == HDMI_COLORSPACE_RGB) && (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
312             csc_mode = CSC_RGB_0_255_TO_ITU601_16_235_8BIT;
313             auto_csc = AUTO_CSC_DISABLE;
314             c0_c2_change = C0_C2_CHANGE_DISABLE;
315             csc_enable = v_CSC_ENABLE;
316         } else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) && (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
317             csc_mode = CSC_ITU601_16_235_TO_RGB_0_255_8BIT;
318             auto_csc = AUTO_CSC_ENABLE;
319             c0_c2_change = C0_C2_CHANGE_DISABLE;
320             csc_enable = v_CSC_DISABLE;
321         }
322     } else {
323         if ((data->enc_in_format == HDMI_COLORSPACE_RGB) && (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
324             csc_mode = CSC_RGB_0_255_TO_ITU709_16_235_8BIT;
325             auto_csc = AUTO_CSC_DISABLE;
326             c0_c2_change = C0_C2_CHANGE_DISABLE;
327             csc_enable = v_CSC_ENABLE;
328         } else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) && (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
329             csc_mode = CSC_ITU709_16_235_TO_RGB_0_255_8BIT;
330             auto_csc = AUTO_CSC_ENABLE;
331             c0_c2_change = C0_C2_CHANGE_DISABLE;
332             csc_enable = v_CSC_DISABLE;
333         }
334     }
335 
336     for (i = 0; i < 0x18; i++) {
337         hdmi_writeb(hdmi, HDMI_VIDEO_CSC_COEF + i, coeff_csc[csc_mode][i]);
338     }
339 
340     value = v_SOF_DISABLE | csc_enable | v_COLOR_DEPTH_NOT_INDICATED(1);
341     hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
342     hdmi_modb(hdmi, HDMI_VIDEO_CONTRL, m_VIDEO_AUTO_CSC | m_VIDEO_C0_C2_SWAP,
343               v_VIDEO_AUTO_CSC(auto_csc) | v_VIDEO_C0_C2_SWAP(c0_c2_change));
344 
345     return 0;
346 }
347 
inno_hdmi_config_video_timing(struct inno_hdmi * hdmi,struct drm_display_mode * mode)348 static int inno_hdmi_config_video_timing(struct inno_hdmi *hdmi, struct drm_display_mode *mode)
349 {
350     int value;
351 
352     /* Set detail external video timing polarity and interlace mode */
353     value = v_EXTERANL_VIDEO(1);
354     value |= mode->flags & DRM_MODE_FLAG_PHSYNC ? v_HSYNC_POLARITY(1) : v_HSYNC_POLARITY(0);
355     value |= mode->flags & DRM_MODE_FLAG_PVSYNC ? v_VSYNC_POLARITY(1) : v_VSYNC_POLARITY(0);
356     value |= mode->flags & DRM_MODE_FLAG_INTERLACE ? v_INETLACE(1) : v_INETLACE(0);
357     hdmi_writeb(hdmi, HDMI_VIDEO_TIMING_CTL, value);
358 
359     /* Set detail external video timing */
360     value = mode->htotal;
361     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_L, value & 0xFF);
362     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_H, (value >> 0x8) & 0xFF);
363 
364     value = mode->htotal - mode->hdisplay;
365     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_L, value & 0xFF);
366     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_H, (value >> 0x8) & 0xFF);
367 
368     value = mode->hsync_start - mode->hdisplay;
369     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_L, value & 0xFF);
370     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_H, (value >> 0x8) & 0xFF);
371 
372     value = mode->hsync_end - mode->hsync_start;
373     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_L, value & 0xFF);
374     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_H, (value >> 0x8) & 0xFF);
375 
376     value = mode->vtotal;
377     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_L, value & 0xFF);
378     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_H, (value >> 0x8) & 0xFF);
379 
380     value = mode->vtotal - mode->vdisplay;
381     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VBLANK, value & 0xFF);
382 
383     value = mode->vsync_start - mode->vdisplay;
384     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDELAY, value & 0xFF);
385 
386     value = mode->vsync_end - mode->vsync_start;
387     hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDURATION, value & 0xFF);
388 
389     hdmi_writeb(hdmi, HDMI_PHY_PRE_DIV_RATIO, 0x1e);
390     hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_LOW, 0x2c);
391     hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH, 0x01);
392 
393     return 0;
394 }
395 
inno_hdmi_setup(struct inno_hdmi * hdmi,struct drm_display_mode * mode)396 static int inno_hdmi_setup(struct inno_hdmi *hdmi, struct drm_display_mode *mode)
397 {
398     hdmi->hdmi_data.vic = drm_match_cea_mode(mode);
399 
400     hdmi->hdmi_data.enc_in_format = HDMI_COLORSPACE_RGB;
401     hdmi->hdmi_data.enc_out_format = HDMI_COLORSPACE_RGB;
402 
403     if ((hdmi->hdmi_data.vic == 0x6) || (hdmi->hdmi_data.vic == 0x7) || (hdmi->hdmi_data.vic == 0x15) ||
404         (hdmi->hdmi_data.vic == 0x16) || (hdmi->hdmi_data.vic == 0x2) || (hdmi->hdmi_data.vic == 0x3) ||
405         (hdmi->hdmi_data.vic == 0x11) || (hdmi->hdmi_data.vic == 0x12)) {
406         hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
407     } else {
408         hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
409     }
410 
411     /* Mute video and audio output */
412     hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK, v_AUDIO_MUTE(1) | v_VIDEO_MUTE(1));
413 
414     /* Set HDMI Mode */
415     hdmi_writeb(hdmi, HDMI_HDCP_CTRL, v_HDMI_DVI(hdmi->hdmi_data.sink_is_hdmi));
416 
417     inno_hdmi_config_video_timing(hdmi, mode);
418 
419     inno_hdmi_config_video_csc(hdmi);
420 
421     if (hdmi->hdmi_data.sink_is_hdmi) {
422         inno_hdmi_config_video_avi(hdmi, mode);
423         inno_hdmi_config_video_vsi(hdmi, mode);
424     }
425 
426     /*
427      * When IP controller have configured to an accurate video
428      * timing, then the TMDS clock source would be switched to
429      * DCLK_LCDC, so we need to init the TMDS rate to mode pixel
430      * clock rate, and reconfigure the DDC clock.
431      */
432     hdmi->tmds_rate = mode->clock * 0x3e8;
433     inno_hdmi_i2c_init(hdmi);
434 
435     /* Unmute video and audio output */
436     hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK, v_AUDIO_MUTE(0) | v_VIDEO_MUTE(0));
437 
438     return 0;
439 }
440 
inno_hdmi_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adj_mode)441 static void inno_hdmi_encoder_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
442                                        struct drm_display_mode *adj_mode)
443 {
444     struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
445 
446     inno_hdmi_setup(hdmi, adj_mode);
447 
448     /* Store the display mode for plugin/DPMS poweron events */
449     memcpy(&hdmi->previous_mode, adj_mode, sizeof(hdmi->previous_mode));
450 }
451 
inno_hdmi_encoder_enable(struct drm_encoder * encoder)452 static void inno_hdmi_encoder_enable(struct drm_encoder *encoder)
453 {
454     struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
455 
456     inno_hdmi_set_pwr_mode(hdmi, NORMAL);
457 }
458 
inno_hdmi_encoder_disable(struct drm_encoder * encoder)459 static void inno_hdmi_encoder_disable(struct drm_encoder *encoder)
460 {
461     struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
462 
463     inno_hdmi_set_pwr_mode(hdmi, LOWER_PWR);
464 }
465 
inno_hdmi_encoder_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adj_mode)466 static bool inno_hdmi_encoder_mode_fixup(struct drm_encoder *encoder, const struct drm_display_mode *mode,
467                                          struct drm_display_mode *adj_mode)
468 {
469     return true;
470 }
471 
inno_hdmi_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)472 static int inno_hdmi_encoder_atomic_check(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state,
473                                           struct drm_connector_state *conn_state)
474 {
475     struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
476 
477     s->output_mode = ROCKCHIP_OUT_MODE_P888;
478     s->output_type = DRM_MODE_CONNECTOR_HDMIA;
479 
480     return 0;
481 }
482 
483 static struct drm_encoder_helper_funcs inno_hdmi_encoder_helper_funcs = {
484     .enable = inno_hdmi_encoder_enable,
485     .disable = inno_hdmi_encoder_disable,
486     .mode_fixup = inno_hdmi_encoder_mode_fixup,
487     .mode_set = inno_hdmi_encoder_mode_set,
488     .atomic_check = inno_hdmi_encoder_atomic_check,
489 };
490 
inno_hdmi_connector_detect(struct drm_connector * connector,bool force)491 static enum drm_connector_status inno_hdmi_connector_detect(struct drm_connector *connector, bool force)
492 {
493     struct inno_hdmi *hdmi = to_inno_hdmi(connector);
494 
495     return (hdmi_readb(hdmi, HDMI_STATUS) & m_HOTPLUG) ? connector_status_connected : connector_status_disconnected;
496 }
497 
inno_hdmi_connector_get_modes(struct drm_connector * connector)498 static int inno_hdmi_connector_get_modes(struct drm_connector *connector)
499 {
500     struct inno_hdmi *hdmi = to_inno_hdmi(connector);
501     struct edid *edid;
502     int ret = 0;
503 
504     if (!hdmi->ddc) {
505         return 0;
506     }
507 
508     edid = drm_get_edid(connector, hdmi->ddc);
509     if (edid) {
510         hdmi->hdmi_data.sink_is_hdmi = drm_detect_hdmi_monitor(edid);
511         hdmi->hdmi_data.sink_has_audio = drm_detect_monitor_audio(edid);
512         drm_connector_update_edid_property(connector, edid);
513         ret = drm_add_edid_modes(connector, edid);
514         kfree(edid);
515     }
516 
517     return ret;
518 }
519 
inno_hdmi_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)520 static enum drm_mode_status inno_hdmi_connector_mode_valid(struct drm_connector *connector,
521                                                            struct drm_display_mode *mode)
522 {
523     return MODE_OK;
524 }
525 
inno_hdmi_probe_single_connector_modes(struct drm_connector * connector,uint32_t maxX,uint32_t maxY)526 static int inno_hdmi_probe_single_connector_modes(struct drm_connector *connector, uint32_t maxX, uint32_t maxY)
527 {
528     return drm_helper_probe_single_connector_modes(connector, 0x780, 0x438);
529 }
530 
inno_hdmi_connector_destroy(struct drm_connector * connector)531 static void inno_hdmi_connector_destroy(struct drm_connector *connector)
532 {
533     drm_connector_unregister(connector);
534     drm_connector_cleanup(connector);
535 }
536 
537 static const struct drm_connector_funcs inno_hdmi_connector_funcs = {
538     .fill_modes = inno_hdmi_probe_single_connector_modes,
539     .detect = inno_hdmi_connector_detect,
540     .destroy = inno_hdmi_connector_destroy,
541     .reset = drm_atomic_helper_connector_reset,
542     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
543     .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
544 };
545 
546 static struct drm_connector_helper_funcs inno_hdmi_connector_helper_funcs = {
547     .get_modes = inno_hdmi_connector_get_modes,
548     .mode_valid = inno_hdmi_connector_mode_valid,
549 };
550 
inno_hdmi_register(struct drm_device * drm,struct inno_hdmi * hdmi)551 static int inno_hdmi_register(struct drm_device *drm, struct inno_hdmi *hdmi)
552 {
553     struct drm_encoder *encoder = &hdmi->encoder;
554     struct device *dev = hdmi->dev;
555 
556     encoder->possible_crtcs = rockchip_drm_of_find_possible_crtcs(drm, dev->of_node);
557 
558     /*
559      * If we failed to find the CRTC(s) which this encoder is
560      * supposed to be connected to, it's because the CRTC has
561      * not been registered yet.  Defer probing, and hope that
562      * the required CRTC is added later.
563      */
564     if (encoder->possible_crtcs == 0) {
565         return -EPROBE_DEFER;
566     }
567 
568     drm_encoder_helper_add(encoder, &inno_hdmi_encoder_helper_funcs);
569     drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
570 
571     hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
572 
573     drm_connector_helper_add(&hdmi->connector, &inno_hdmi_connector_helper_funcs);
574     drm_connector_init_with_ddc(drm, &hdmi->connector, &inno_hdmi_connector_funcs, DRM_MODE_CONNECTOR_HDMIA, hdmi->ddc);
575 
576     drm_connector_attach_encoder(&hdmi->connector, encoder);
577 
578     return 0;
579 }
580 
inno_hdmi_i2c_irq(struct inno_hdmi * hdmi)581 static irqreturn_t inno_hdmi_i2c_irq(struct inno_hdmi *hdmi)
582 {
583     struct inno_hdmi_i2c *i2c = hdmi->i2c;
584     u8 stat;
585 
586     stat = hdmi_readb(hdmi, HDMI_INTERRUPT_STATUS1);
587     if (!(stat & m_INT_EDID_READY)) {
588         return IRQ_NONE;
589     }
590 
591     /* Clear HDMI EDID interrupt flag */
592     hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
593 
594     complete(&i2c->cmp);
595 
596     return IRQ_HANDLED;
597 }
598 
inno_hdmi_hardirq(int irq,void * dev_id)599 static irqreturn_t inno_hdmi_hardirq(int irq, void *dev_id)
600 {
601     struct inno_hdmi *hdmi = dev_id;
602     irqreturn_t ret = IRQ_NONE;
603     u8 interrupt;
604 
605     if (hdmi->i2c) {
606         ret = inno_hdmi_i2c_irq(hdmi);
607     }
608 
609     interrupt = hdmi_readb(hdmi, HDMI_STATUS);
610     if (interrupt & m_INT_HOTPLUG) {
611         hdmi_modb(hdmi, HDMI_STATUS, m_INT_HOTPLUG, m_INT_HOTPLUG);
612         ret = IRQ_WAKE_THREAD;
613     }
614 
615     return ret;
616 }
617 
inno_hdmi_irq(int irq,void * dev_id)618 static irqreturn_t inno_hdmi_irq(int irq, void *dev_id)
619 {
620     struct inno_hdmi *hdmi = dev_id;
621 
622     drm_helper_hpd_irq_event(hdmi->connector.dev);
623 
624     return IRQ_HANDLED;
625 }
626 
inno_hdmi_i2c_read(struct inno_hdmi * hdmi,struct i2c_msg * msgs)627 static int inno_hdmi_i2c_read(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
628 {
629     int length = msgs->len;
630     u8 *buf = msgs->buf;
631     int ret;
632 
633     ret = wait_for_completion_timeout(&hdmi->i2c->cmp, HZ / 0xa);
634     if (!ret) {
635         return -EAGAIN;
636     }
637 
638     while (length--) {
639         *buf++ = hdmi_readb(hdmi, HDMI_EDID_FIFO_ADDR);
640     }
641 
642     return 0;
643 }
644 
inno_hdmi_i2c_write(struct inno_hdmi * hdmi,struct i2c_msg * msgs)645 static int inno_hdmi_i2c_write(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
646 {
647     /*
648      * The DDC module only support read EDID message, so
649      * we assume that each word write to this i2c adapter
650      * should be the offset of EDID word address.
651      */
652     if ((msgs->len != 1) || ((msgs->addr != DDC_ADDR) && (msgs->addr != DDC_SEGMENT_ADDR))) {
653         return -EINVAL;
654     }
655 
656     reinit_completion(&hdmi->i2c->cmp);
657 
658     if (msgs->addr == DDC_SEGMENT_ADDR) {
659         hdmi->i2c->segment_addr = msgs->buf[0];
660     }
661     if (msgs->addr == DDC_ADDR) {
662         hdmi->i2c->ddc_addr = msgs->buf[0];
663     }
664 
665     /* Set edid fifo first addr */
666     hdmi_writeb(hdmi, HDMI_EDID_FIFO_OFFSET, 0x00);
667 
668     /* Set edid word address 0x00/0x80 */
669     hdmi_writeb(hdmi, HDMI_EDID_WORD_ADDR, hdmi->i2c->ddc_addr);
670 
671     /* Set edid segment pointer */
672     hdmi_writeb(hdmi, HDMI_EDID_SEGMENT_POINTER, hdmi->i2c->segment_addr);
673 
674     return 0;
675 }
676 
inno_hdmi_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)677 static int inno_hdmi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
678 {
679     struct inno_hdmi *hdmi = i2c_get_adapdata(adap);
680     struct inno_hdmi_i2c *i2c = hdmi->i2c;
681     int i, ret = 0;
682 
683     mutex_lock(&i2c->lock);
684 
685     /* Clear the EDID interrupt flag and unmute the interrupt */
686     hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_EDID_READY);
687     hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
688 
689     for (i = 0; i < num; i++) {
690         DRM_DEV_DEBUG(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n", i + 1, num, msgs[i].len, msgs[i].flags);
691 
692         if (msgs[i].flags & I2C_M_RD) {
693             ret = inno_hdmi_i2c_read(hdmi, &msgs[i]);
694         } else {
695             ret = inno_hdmi_i2c_write(hdmi, &msgs[i]);
696         }
697 
698         if (ret < 0) {
699             break;
700         }
701     }
702 
703     if (!ret) {
704         ret = num;
705     }
706 
707     /* Mute HDMI EDID interrupt */
708     hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
709 
710     mutex_unlock(&i2c->lock);
711 
712     return ret;
713 }
714 
inno_hdmi_i2c_func(struct i2c_adapter * adapter)715 static u32 inno_hdmi_i2c_func(struct i2c_adapter *adapter)
716 {
717     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
718 }
719 
720 static const struct i2c_algorithm inno_hdmi_algorithm = {
721     .master_xfer = inno_hdmi_i2c_xfer,
722     .functionality = inno_hdmi_i2c_func,
723 };
724 
inno_hdmi_i2c_adapter(struct inno_hdmi * hdmi)725 static struct i2c_adapter *inno_hdmi_i2c_adapter(struct inno_hdmi *hdmi)
726 {
727     struct i2c_adapter *adap;
728     struct inno_hdmi_i2c *i2c;
729     int ret;
730 
731     i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
732     if (!i2c) {
733         return ERR_PTR(-ENOMEM);
734     }
735 
736     mutex_init(&i2c->lock);
737     init_completion(&i2c->cmp);
738 
739     adap = &i2c->adap;
740     adap->class = I2C_CLASS_DDC;
741     adap->owner = THIS_MODULE;
742     adap->dev.parent = hdmi->dev;
743     adap->dev.of_node = hdmi->dev->of_node;
744     adap->algo = &inno_hdmi_algorithm;
745     strlcpy(adap->name, "Inno HDMI", sizeof(adap->name));
746     i2c_set_adapdata(adap, hdmi);
747 
748     ret = i2c_add_adapter(adap);
749     if (ret) {
750         dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
751         devm_kfree(hdmi->dev, i2c);
752         return ERR_PTR(ret);
753     }
754 
755     hdmi->i2c = i2c;
756 
757     DRM_DEV_INFO(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
758 
759     return adap;
760 }
761 
inno_hdmi_bind(struct device * dev,struct device * master,void * data)762 static int inno_hdmi_bind(struct device *dev, struct device *master, void *data)
763 {
764     struct platform_device *pdev = to_platform_device(dev);
765     struct drm_device *drm = data;
766     struct inno_hdmi *hdmi;
767     struct resource *iores;
768     int irq;
769     int ret;
770 
771     hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
772     if (!hdmi) {
773         return -ENOMEM;
774     }
775 
776     hdmi->dev = dev;
777     hdmi->drm_dev = drm;
778 
779     iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
780     hdmi->regs = devm_ioremap_resource(dev, iores);
781     if (IS_ERR(hdmi->regs)) {
782         return PTR_ERR(hdmi->regs);
783     }
784 
785     hdmi->pclk = devm_clk_get(hdmi->dev, "pclk");
786     if (IS_ERR(hdmi->pclk)) {
787         DRM_DEV_ERROR(hdmi->dev, "Unable to get HDMI pclk clk\n");
788         return PTR_ERR(hdmi->pclk);
789     }
790 
791     ret = clk_prepare_enable(hdmi->pclk);
792     if (ret) {
793         DRM_DEV_ERROR(hdmi->dev, "Cannot enable HDMI pclk clock: %d\n", ret);
794         return ret;
795     }
796 
797     irq = platform_get_irq(pdev, 0);
798     if (irq < 0) {
799         ret = irq;
800         goto err_disable_clk;
801     }
802 
803     inno_hdmi_reset(hdmi);
804 
805     hdmi->ddc = inno_hdmi_i2c_adapter(hdmi);
806     if (IS_ERR(hdmi->ddc)) {
807         ret = PTR_ERR(hdmi->ddc);
808         hdmi->ddc = NULL;
809         goto err_disable_clk;
810     }
811 
812     /*
813      * When IP controller haven't configured to an accurate video
814      * timing, then the TMDS clock source would be switched to
815      * PCLK_HDMI, so we need to init the TMDS rate to PCLK rate,
816      * and reconfigure the DDC clock.
817      */
818     hdmi->tmds_rate = clk_get_rate(hdmi->pclk);
819     inno_hdmi_i2c_init(hdmi);
820 
821     ret = inno_hdmi_register(drm, hdmi);
822     if (ret) {
823         goto err_put_adapter;
824     }
825 
826     dev_set_drvdata(dev, hdmi);
827 
828     /* Unmute hotplug interrupt */
829     hdmi_modb(hdmi, HDMI_STATUS, m_MASK_INT_HOTPLUG, v_MASK_INT_HOTPLUG(1));
830 
831     ret = devm_request_threaded_irq(dev, irq, inno_hdmi_hardirq, inno_hdmi_irq, IRQF_SHARED, dev_name(dev), hdmi);
832     if (ret < 0) {
833         goto err_cleanup_hdmi;
834     }
835 
836     return 0;
837 err_cleanup_hdmi:
838     hdmi->connector.funcs->destroy(&hdmi->connector);
839     hdmi->encoder.funcs->destroy(&hdmi->encoder);
840 err_put_adapter:
841     i2c_put_adapter(hdmi->ddc);
842 err_disable_clk:
843     clk_disable_unprepare(hdmi->pclk);
844     return ret;
845 }
846 
inno_hdmi_unbind(struct device * dev,struct device * master,void * data)847 static void inno_hdmi_unbind(struct device *dev, struct device *master, void *data)
848 {
849     struct inno_hdmi *hdmi = dev_get_drvdata(dev);
850 
851     hdmi->connector.funcs->destroy(&hdmi->connector);
852     hdmi->encoder.funcs->destroy(&hdmi->encoder);
853 
854     i2c_put_adapter(hdmi->ddc);
855     clk_disable_unprepare(hdmi->pclk);
856 }
857 
858 static const struct component_ops inno_hdmi_ops = {
859     .bind = inno_hdmi_bind,
860     .unbind = inno_hdmi_unbind,
861 };
862 
inno_hdmi_probe(struct platform_device * pdev)863 static int inno_hdmi_probe(struct platform_device *pdev)
864 {
865     return component_add(&pdev->dev, &inno_hdmi_ops);
866 }
867 
inno_hdmi_remove(struct platform_device * pdev)868 static int inno_hdmi_remove(struct platform_device *pdev)
869 {
870     component_del(&pdev->dev, &inno_hdmi_ops);
871 
872     return 0;
873 }
874 
875 static const struct of_device_id inno_hdmi_dt_ids[] = {
876     {
877         .compatible = "rockchip,rk3036-inno-hdmi",
878     },
879     {},
880 };
881 MODULE_DEVICE_TABLE(of, inno_hdmi_dt_ids);
882 
883 struct platform_driver inno_hdmi_driver = {
884     .probe = inno_hdmi_probe,
885     .remove = inno_hdmi_remove,
886     .driver =
887         {
888             .name = "innohdmi-rockchip",
889             .of_match_table = inno_hdmi_dt_ids,
890         },
891 };
892