2 * Copyright (C) STMicroelectronics SA 2014
3 * Author: Vincent Abriou <vincent.abriou@st.com> for STMicroelectronics.
4 * License terms: GNU General Public License (GPL), version 2
8 #include <linux/component.h>
9 #include <linux/hdmi.h>
10 #include <linux/module.h>
11 #include <linux/of_gpio.h>
12 #include <linux/platform_device.h>
13 #include <linux/reset.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_edid.h>
21 #include "sti_hdmi_tx3g4c28phy.h"
22 #include "sti_hdmi_tx3g0c55phy.h"
25 #define HDMI_CFG 0x0000
26 #define HDMI_INT_EN 0x0004
27 #define HDMI_INT_STA 0x0008
28 #define HDMI_INT_CLR 0x000C
29 #define HDMI_STA 0x0010
30 #define HDMI_ACTIVE_VID_XMIN 0x0100
31 #define HDMI_ACTIVE_VID_XMAX 0x0104
32 #define HDMI_ACTIVE_VID_YMIN 0x0108
33 #define HDMI_ACTIVE_VID_YMAX 0x010C
34 #define HDMI_DFLT_CHL0_DAT 0x0110
35 #define HDMI_DFLT_CHL1_DAT 0x0114
36 #define HDMI_DFLT_CHL2_DAT 0x0118
37 #define HDMI_SW_DI_1_HEAD_WORD 0x0210
38 #define HDMI_SW_DI_1_PKT_WORD0 0x0214
39 #define HDMI_SW_DI_1_PKT_WORD1 0x0218
40 #define HDMI_SW_DI_1_PKT_WORD2 0x021C
41 #define HDMI_SW_DI_1_PKT_WORD3 0x0220
42 #define HDMI_SW_DI_1_PKT_WORD4 0x0224
43 #define HDMI_SW_DI_1_PKT_WORD5 0x0228
44 #define HDMI_SW_DI_1_PKT_WORD6 0x022C
45 #define HDMI_SW_DI_CFG 0x0230
46 #define HDMI_SW_DI_2_HEAD_WORD 0x0600
47 #define HDMI_SW_DI_2_PKT_WORD0 0x0604
48 #define HDMI_SW_DI_2_PKT_WORD1 0x0608
49 #define HDMI_SW_DI_2_PKT_WORD2 0x060C
50 #define HDMI_SW_DI_2_PKT_WORD3 0x0610
51 #define HDMI_SW_DI_2_PKT_WORD4 0x0614
52 #define HDMI_SW_DI_2_PKT_WORD5 0x0618
53 #define HDMI_SW_DI_2_PKT_WORD6 0x061C
54 #define HDMI_SW_DI_3_HEAD_WORD 0x0620
55 #define HDMI_SW_DI_3_PKT_WORD0 0x0624
56 #define HDMI_SW_DI_3_PKT_WORD1 0x0628
57 #define HDMI_SW_DI_3_PKT_WORD2 0x062C
58 #define HDMI_SW_DI_3_PKT_WORD3 0x0630
59 #define HDMI_SW_DI_3_PKT_WORD4 0x0634
60 #define HDMI_SW_DI_3_PKT_WORD5 0x0638
61 #define HDMI_SW_DI_3_PKT_WORD6 0x063C
63 #define HDMI_IFRAME_SLOT_AVI 1
64 #define HDMI_IFRAME_SLOT_AUDIO 2
65 #define HDMI_IFRAME_SLOT_VENDOR 3
67 #define XCAT(prefix, x, suffix) prefix ## x ## suffix
68 #define HDMI_SW_DI_N_HEAD_WORD(x) XCAT(HDMI_SW_DI_, x, _HEAD_WORD)
69 #define HDMI_SW_DI_N_PKT_WORD0(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD0)
70 #define HDMI_SW_DI_N_PKT_WORD1(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD1)
71 #define HDMI_SW_DI_N_PKT_WORD2(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD2)
72 #define HDMI_SW_DI_N_PKT_WORD3(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD3)
73 #define HDMI_SW_DI_N_PKT_WORD4(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD4)
74 #define HDMI_SW_DI_N_PKT_WORD5(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD5)
75 #define HDMI_SW_DI_N_PKT_WORD6(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD6)
77 #define HDMI_SW_DI_MAX_WORD 7
79 #define HDMI_IFRAME_DISABLED 0x0
80 #define HDMI_IFRAME_SINGLE_SHOT 0x1
81 #define HDMI_IFRAME_FIELD 0x2
82 #define HDMI_IFRAME_FRAME 0x3
83 #define HDMI_IFRAME_MASK 0x3
84 #define HDMI_IFRAME_CFG_DI_N(x, n) ((x) << ((n-1)*4)) /* n from 1 to 6 */
86 #define HDMI_CFG_DEVICE_EN BIT(0)
87 #define HDMI_CFG_HDMI_NOT_DVI BIT(1)
88 #define HDMI_CFG_HDCP_EN BIT(2)
89 #define HDMI_CFG_ESS_NOT_OESS BIT(3)
90 #define HDMI_CFG_H_SYNC_POL_NEG BIT(4)
91 #define HDMI_CFG_SINK_TERM_DET_EN BIT(5)
92 #define HDMI_CFG_V_SYNC_POL_NEG BIT(6)
93 #define HDMI_CFG_422_EN BIT(8)
94 #define HDMI_CFG_FIFO_OVERRUN_CLR BIT(12)
95 #define HDMI_CFG_FIFO_UNDERRUN_CLR BIT(13)
96 #define HDMI_CFG_SW_RST_EN BIT(31)
98 #define HDMI_INT_GLOBAL BIT(0)
99 #define HDMI_INT_SW_RST BIT(1)
100 #define HDMI_INT_PIX_CAP BIT(3)
101 #define HDMI_INT_HOT_PLUG BIT(4)
102 #define HDMI_INT_DLL_LCK BIT(5)
103 #define HDMI_INT_NEW_FRAME BIT(6)
104 #define HDMI_INT_GENCTRL_PKT BIT(7)
105 #define HDMI_INT_SINK_TERM_PRESENT BIT(11)
107 #define HDMI_DEFAULT_INT (HDMI_INT_SINK_TERM_PRESENT \
109 | HDMI_INT_HOT_PLUG \
112 #define HDMI_WORKING_INT (HDMI_INT_SINK_TERM_PRESENT \
113 | HDMI_INT_GENCTRL_PKT \
114 | HDMI_INT_NEW_FRAME \
116 | HDMI_INT_HOT_PLUG \
121 #define HDMI_STA_SW_RST BIT(1)
123 #define HDMI_INFOFRAME_HEADER_TYPE(x) (((x) & 0xff) << 0)
124 #define HDMI_INFOFRAME_HEADER_VERSION(x) (((x) & 0xff) << 8)
125 #define HDMI_INFOFRAME_HEADER_LEN(x) (((x) & 0x0f) << 16)
127 struct sti_hdmi_connector {
128 struct drm_connector drm_connector;
129 struct drm_encoder *encoder;
130 struct sti_hdmi *hdmi;
133 #define to_sti_hdmi_connector(x) \
134 container_of(x, struct sti_hdmi_connector, drm_connector)
136 u32 hdmi_read(struct sti_hdmi *hdmi, int offset)
138 return readl(hdmi->regs + offset);
141 void hdmi_write(struct sti_hdmi *hdmi, u32 val, int offset)
143 writel(val, hdmi->regs + offset);
147 * HDMI interrupt handler threaded
150 * @arg: connector structure
152 static irqreturn_t hdmi_irq_thread(int irq, void *arg)
154 struct sti_hdmi *hdmi = arg;
156 /* Hot plug/unplug IRQ */
157 if (hdmi->irq_status & HDMI_INT_HOT_PLUG) {
158 hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
160 drm_helper_hpd_irq_event(hdmi->drm_dev);
163 /* Sw reset and PLL lock are exclusive so we can use the same
164 * event to signal them
166 if (hdmi->irq_status & (HDMI_INT_SW_RST | HDMI_INT_DLL_LCK)) {
167 hdmi->event_received = true;
168 wake_up_interruptible(&hdmi->wait_event);
175 * HDMI interrupt handler
178 * @arg: connector structure
180 static irqreturn_t hdmi_irq(int irq, void *arg)
182 struct sti_hdmi *hdmi = arg;
184 /* read interrupt status */
185 hdmi->irq_status = hdmi_read(hdmi, HDMI_INT_STA);
187 /* clear interrupt status */
188 hdmi_write(hdmi, hdmi->irq_status, HDMI_INT_CLR);
190 /* force sync bus write */
191 hdmi_read(hdmi, HDMI_INT_STA);
193 return IRQ_WAKE_THREAD;
197 * Set hdmi active area depending on the drm display mode selected
199 * @hdmi: pointer on the hdmi internal structure
201 static void hdmi_active_area(struct sti_hdmi *hdmi)
206 xmin = sti_vtg_get_pixel_number(hdmi->mode, 1);
207 xmax = sti_vtg_get_pixel_number(hdmi->mode, hdmi->mode.hdisplay);
208 ymin = sti_vtg_get_line_number(hdmi->mode, 0);
209 ymax = sti_vtg_get_line_number(hdmi->mode, hdmi->mode.vdisplay - 1);
211 hdmi_write(hdmi, xmin, HDMI_ACTIVE_VID_XMIN);
212 hdmi_write(hdmi, xmax, HDMI_ACTIVE_VID_XMAX);
213 hdmi_write(hdmi, ymin, HDMI_ACTIVE_VID_YMIN);
214 hdmi_write(hdmi, ymax, HDMI_ACTIVE_VID_YMAX);
218 * Overall hdmi configuration
220 * @hdmi: pointer on the hdmi internal structure
222 static void hdmi_config(struct sti_hdmi *hdmi)
226 DRM_DEBUG_DRIVER("\n");
228 /* Clear overrun and underrun fifo */
229 conf = HDMI_CFG_FIFO_OVERRUN_CLR | HDMI_CFG_FIFO_UNDERRUN_CLR;
231 /* Enable HDMI mode not DVI */
232 conf |= HDMI_CFG_HDMI_NOT_DVI | HDMI_CFG_ESS_NOT_OESS;
234 /* Enable sink term detection */
235 conf |= HDMI_CFG_SINK_TERM_DET_EN;
237 /* Set Hsync polarity */
238 if (hdmi->mode.flags & DRM_MODE_FLAG_NHSYNC) {
239 DRM_DEBUG_DRIVER("H Sync Negative\n");
240 conf |= HDMI_CFG_H_SYNC_POL_NEG;
243 /* Set Vsync polarity */
244 if (hdmi->mode.flags & DRM_MODE_FLAG_NVSYNC) {
245 DRM_DEBUG_DRIVER("V Sync Negative\n");
246 conf |= HDMI_CFG_V_SYNC_POL_NEG;
250 conf |= HDMI_CFG_DEVICE_EN;
252 hdmi_write(hdmi, conf, HDMI_CFG);
256 * Helper to reset info frame
258 * @hdmi: pointer on the hdmi internal structure
259 * @slot: infoframe to reset
261 static void hdmi_infoframe_reset(struct sti_hdmi *hdmi,
265 u32 head_offset, pack_offset;
268 case HDMI_IFRAME_SLOT_AVI:
269 head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AVI);
270 pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AVI);
272 case HDMI_IFRAME_SLOT_AUDIO:
273 head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AUDIO);
274 pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AUDIO);
276 case HDMI_IFRAME_SLOT_VENDOR:
277 head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_VENDOR);
278 pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_VENDOR);
281 DRM_ERROR("unsupported infoframe slot: %#x\n", slot);
285 /* Disable transmission for the selected slot */
286 val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
287 val &= ~HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, slot);
288 hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
290 /* Reset info frame registers */
291 hdmi_write(hdmi, 0x0, head_offset);
292 for (i = 0; i < HDMI_SW_DI_MAX_WORD; i += sizeof(u32))
293 hdmi_write(hdmi, 0x0, pack_offset + i);
297 * Helper to concatenate infoframe in 32 bits word
299 * @ptr: pointer on the hdmi internal structure
300 * @data: infoframe to write
301 * @size: size to write
303 static inline unsigned int hdmi_infoframe_subpack(const u8 *ptr, size_t size)
305 unsigned long value = 0;
308 for (i = size; i > 0; i--)
309 value = (value << 8) | ptr[i - 1];
315 * Helper to write info frame
317 * @hdmi: pointer on the hdmi internal structure
318 * @data: infoframe to write
319 * @size: size to write
321 static void hdmi_infoframe_write_infopack(struct sti_hdmi *hdmi,
325 const u8 *ptr = data;
326 u32 val, slot, mode, i;
327 u32 head_offset, pack_offset;
330 case HDMI_INFOFRAME_TYPE_AVI:
331 slot = HDMI_IFRAME_SLOT_AVI;
332 mode = HDMI_IFRAME_FIELD;
333 head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AVI);
334 pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AVI);
336 case HDMI_INFOFRAME_TYPE_AUDIO:
337 slot = HDMI_IFRAME_SLOT_AUDIO;
338 mode = HDMI_IFRAME_FRAME;
339 head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AUDIO);
340 pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AUDIO);
342 case HDMI_INFOFRAME_TYPE_VENDOR:
343 slot = HDMI_IFRAME_SLOT_VENDOR;
344 mode = HDMI_IFRAME_FRAME;
345 head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_VENDOR);
346 pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_VENDOR);
349 DRM_ERROR("unsupported infoframe type: %#x\n", *ptr);
353 /* Disable transmission slot for updated infoframe */
354 val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
355 val &= ~HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, slot);
356 hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
358 val = HDMI_INFOFRAME_HEADER_TYPE(*ptr++);
359 val |= HDMI_INFOFRAME_HEADER_VERSION(*ptr++);
360 val |= HDMI_INFOFRAME_HEADER_LEN(*ptr++);
361 writel(val, hdmi->regs + head_offset);
364 * Each subpack contains 4 bytes
365 * The First Bytes of the first subpacket must contain the checksum
366 * Packet size is increase by one.
368 size = size - HDMI_INFOFRAME_HEADER_SIZE + 1;
369 for (i = 0; i < size; i += sizeof(u32)) {
372 num = min_t(size_t, size - i, sizeof(u32));
373 val = hdmi_infoframe_subpack(ptr, num);
375 writel(val, hdmi->regs + pack_offset + i);
378 /* Enable transmission slot for updated infoframe */
379 val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
380 val |= HDMI_IFRAME_CFG_DI_N(mode, slot);
381 hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
385 * Prepare and configure the AVI infoframe
387 * AVI infoframe are transmitted at least once per two video field and
388 * contains information about HDMI transmission mode such as color space,
391 * @hdmi: pointer on the hdmi internal structure
393 * Return negative value if error occurs
395 static int hdmi_avi_infoframe_config(struct sti_hdmi *hdmi)
397 struct drm_display_mode *mode = &hdmi->mode;
398 struct hdmi_avi_infoframe infoframe;
399 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
402 DRM_DEBUG_DRIVER("\n");
404 ret = drm_hdmi_avi_infoframe_from_display_mode(&infoframe, mode);
406 DRM_ERROR("failed to setup AVI infoframe: %d\n", ret);
410 /* fixed infoframe configuration not linked to the mode */
411 infoframe.colorspace = HDMI_COLORSPACE_RGB;
412 infoframe.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
413 infoframe.colorimetry = HDMI_COLORIMETRY_NONE;
415 ret = hdmi_avi_infoframe_pack(&infoframe, buffer, sizeof(buffer));
417 DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
421 hdmi_infoframe_write_infopack(hdmi, buffer, ret);
427 * Prepare and configure the AUDIO infoframe
429 * AUDIO infoframe are transmitted once per frame and
430 * contains information about HDMI transmission mode such as audio codec,
433 * @hdmi: pointer on the hdmi internal structure
435 * Return negative value if error occurs
437 static int hdmi_audio_infoframe_config(struct sti_hdmi *hdmi)
439 struct hdmi_audio_infoframe infofame;
440 u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
443 ret = hdmi_audio_infoframe_init(&infofame);
445 DRM_ERROR("failed to setup audio infoframe: %d\n", ret);
449 infofame.channels = 2;
451 ret = hdmi_audio_infoframe_pack(&infofame, buffer, sizeof(buffer));
453 DRM_ERROR("failed to pack audio infoframe: %d\n", ret);
457 hdmi_infoframe_write_infopack(hdmi, buffer, ret);
463 * Prepare and configure the VS infoframe
465 * Vendor Specific infoframe are transmitted once per frame and
466 * contains vendor specific information.
468 * @hdmi: pointer on the hdmi internal structure
470 * Return negative value if error occurs
472 #define HDMI_VENDOR_INFOFRAME_MAX_SIZE 6
473 static int hdmi_vendor_infoframe_config(struct sti_hdmi *hdmi)
475 struct drm_display_mode *mode = &hdmi->mode;
476 struct hdmi_vendor_infoframe infoframe;
477 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_VENDOR_INFOFRAME_MAX_SIZE];
480 DRM_DEBUG_DRIVER("\n");
482 ret = drm_hdmi_vendor_infoframe_from_display_mode(&infoframe, mode);
485 * Going into that statement does not means vendor infoframe
486 * fails. It just informed us that vendor infoframe is not
487 * needed for the selected mode. Only 4k or stereoscopic 3D
488 * mode requires vendor infoframe. So just simply return 0.
493 ret = hdmi_vendor_infoframe_pack(&infoframe, buffer, sizeof(buffer));
495 DRM_ERROR("failed to pack VS infoframe: %d\n", ret);
499 hdmi_infoframe_write_infopack(hdmi, buffer, ret);
505 * Software reset of the hdmi subsystem
507 * @hdmi: pointer on the hdmi internal structure
510 #define HDMI_TIMEOUT_SWRESET 100 /*milliseconds */
511 static void hdmi_swreset(struct sti_hdmi *hdmi)
515 DRM_DEBUG_DRIVER("\n");
517 /* Enable hdmi_audio clock only during hdmi reset */
518 if (clk_prepare_enable(hdmi->clk_audio))
519 DRM_INFO("Failed to prepare/enable hdmi_audio clk\n");
522 hdmi->event_received = false;
524 val = hdmi_read(hdmi, HDMI_CFG);
525 val |= HDMI_CFG_SW_RST_EN;
526 hdmi_write(hdmi, val, HDMI_CFG);
528 /* Wait reset completed */
529 wait_event_interruptible_timeout(hdmi->wait_event,
530 hdmi->event_received == true,
532 (HDMI_TIMEOUT_SWRESET));
535 * HDMI_STA_SW_RST bit is set to '1' when SW_RST bit in HDMI_CFG is
536 * set to '1' and clk_audio is running.
538 if ((hdmi_read(hdmi, HDMI_STA) & HDMI_STA_SW_RST) == 0)
539 DRM_DEBUG_DRIVER("Warning: HDMI sw reset timeout occurs\n");
541 val = hdmi_read(hdmi, HDMI_CFG);
542 val &= ~HDMI_CFG_SW_RST_EN;
543 hdmi_write(hdmi, val, HDMI_CFG);
545 /* Disable hdmi_audio clock. Not used anymore for drm purpose */
546 clk_disable_unprepare(hdmi->clk_audio);
549 static void sti_hdmi_disable(struct drm_bridge *bridge)
551 struct sti_hdmi *hdmi = bridge->driver_private;
553 u32 val = hdmi_read(hdmi, HDMI_CFG);
558 DRM_DEBUG_DRIVER("\n");
561 val &= ~HDMI_CFG_DEVICE_EN;
562 hdmi_write(hdmi, val, HDMI_CFG);
564 hdmi_write(hdmi, 0xffffffff, HDMI_INT_CLR);
567 hdmi->phy_ops->stop(hdmi);
569 /* Reset info frame transmission */
570 hdmi_infoframe_reset(hdmi, HDMI_IFRAME_SLOT_AVI);
571 hdmi_infoframe_reset(hdmi, HDMI_IFRAME_SLOT_AUDIO);
572 hdmi_infoframe_reset(hdmi, HDMI_IFRAME_SLOT_VENDOR);
574 /* Set the default channel data to be a dark red */
575 hdmi_write(hdmi, 0x0000, HDMI_DFLT_CHL0_DAT);
576 hdmi_write(hdmi, 0x0000, HDMI_DFLT_CHL1_DAT);
577 hdmi_write(hdmi, 0x0060, HDMI_DFLT_CHL2_DAT);
579 /* Disable/unprepare hdmi clock */
580 clk_disable_unprepare(hdmi->clk_phy);
581 clk_disable_unprepare(hdmi->clk_tmds);
582 clk_disable_unprepare(hdmi->clk_pix);
584 hdmi->enabled = false;
587 static void sti_hdmi_pre_enable(struct drm_bridge *bridge)
589 struct sti_hdmi *hdmi = bridge->driver_private;
591 DRM_DEBUG_DRIVER("\n");
596 /* Prepare/enable clocks */
597 if (clk_prepare_enable(hdmi->clk_pix))
598 DRM_ERROR("Failed to prepare/enable hdmi_pix clk\n");
599 if (clk_prepare_enable(hdmi->clk_tmds))
600 DRM_ERROR("Failed to prepare/enable hdmi_tmds clk\n");
601 if (clk_prepare_enable(hdmi->clk_phy))
602 DRM_ERROR("Failed to prepare/enable hdmi_rejec_pll clk\n");
604 hdmi->enabled = true;
606 /* Program hdmi serializer and start phy */
607 if (!hdmi->phy_ops->start(hdmi)) {
608 DRM_ERROR("Unable to start hdmi phy\n");
612 /* Program hdmi active area */
613 hdmi_active_area(hdmi);
615 /* Enable working interrupts */
616 hdmi_write(hdmi, HDMI_WORKING_INT, HDMI_INT_EN);
618 /* Program hdmi config */
621 /* Program AVI infoframe */
622 if (hdmi_avi_infoframe_config(hdmi))
623 DRM_ERROR("Unable to configure AVI infoframe\n");
625 /* Program AUDIO infoframe */
626 if (hdmi_audio_infoframe_config(hdmi))
627 DRM_ERROR("Unable to configure AUDIO infoframe\n");
629 /* Program VS infoframe */
630 if (hdmi_vendor_infoframe_config(hdmi))
631 DRM_ERROR("Unable to configure VS infoframe\n");
637 static void sti_hdmi_set_mode(struct drm_bridge *bridge,
638 struct drm_display_mode *mode,
639 struct drm_display_mode *adjusted_mode)
641 struct sti_hdmi *hdmi = bridge->driver_private;
644 DRM_DEBUG_DRIVER("\n");
646 /* Copy the drm display mode in the connector local structure */
647 memcpy(&hdmi->mode, mode, sizeof(struct drm_display_mode));
649 /* Update clock framerate according to the selected mode */
650 ret = clk_set_rate(hdmi->clk_pix, mode->clock * 1000);
652 DRM_ERROR("Cannot set rate (%dHz) for hdmi_pix clk\n",
656 ret = clk_set_rate(hdmi->clk_phy, mode->clock * 1000);
658 DRM_ERROR("Cannot set rate (%dHz) for hdmi_rejection_pll clk\n",
664 static void sti_hdmi_bridge_nope(struct drm_bridge *bridge)
669 static const struct drm_bridge_funcs sti_hdmi_bridge_funcs = {
670 .pre_enable = sti_hdmi_pre_enable,
671 .enable = sti_hdmi_bridge_nope,
672 .disable = sti_hdmi_disable,
673 .post_disable = sti_hdmi_bridge_nope,
674 .mode_set = sti_hdmi_set_mode,
677 static int sti_hdmi_connector_get_modes(struct drm_connector *connector)
679 struct sti_hdmi_connector *hdmi_connector
680 = to_sti_hdmi_connector(connector);
681 struct sti_hdmi *hdmi = hdmi_connector->hdmi;
685 DRM_DEBUG_DRIVER("\n");
687 edid = drm_get_edid(connector, hdmi->ddc_adapt);
691 count = drm_add_edid_modes(connector, edid);
692 drm_mode_connector_update_edid_property(connector, edid);
698 DRM_ERROR("Can't read HDMI EDID\n");
702 #define CLK_TOLERANCE_HZ 50
704 static int sti_hdmi_connector_mode_valid(struct drm_connector *connector,
705 struct drm_display_mode *mode)
707 int target = mode->clock * 1000;
708 int target_min = target - CLK_TOLERANCE_HZ;
709 int target_max = target + CLK_TOLERANCE_HZ;
711 struct sti_hdmi_connector *hdmi_connector
712 = to_sti_hdmi_connector(connector);
713 struct sti_hdmi *hdmi = hdmi_connector->hdmi;
716 result = clk_round_rate(hdmi->clk_pix, target);
718 DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
721 if ((result < target_min) || (result > target_max)) {
722 DRM_DEBUG_DRIVER("hdmi pixclk=%d not supported\n", target);
729 struct drm_encoder *sti_hdmi_best_encoder(struct drm_connector *connector)
731 struct sti_hdmi_connector *hdmi_connector
732 = to_sti_hdmi_connector(connector);
734 /* Best encoder is the one associated during connector creation */
735 return hdmi_connector->encoder;
739 struct drm_connector_helper_funcs sti_hdmi_connector_helper_funcs = {
740 .get_modes = sti_hdmi_connector_get_modes,
741 .mode_valid = sti_hdmi_connector_mode_valid,
742 .best_encoder = sti_hdmi_best_encoder,
745 /* get detection status of display device */
746 static enum drm_connector_status
747 sti_hdmi_connector_detect(struct drm_connector *connector, bool force)
749 struct sti_hdmi_connector *hdmi_connector
750 = to_sti_hdmi_connector(connector);
751 struct sti_hdmi *hdmi = hdmi_connector->hdmi;
753 DRM_DEBUG_DRIVER("\n");
756 DRM_DEBUG_DRIVER("hdmi cable connected\n");
757 return connector_status_connected;
760 DRM_DEBUG_DRIVER("hdmi cable disconnected\n");
761 return connector_status_disconnected;
764 static void sti_hdmi_connector_destroy(struct drm_connector *connector)
766 struct sti_hdmi_connector *hdmi_connector
767 = to_sti_hdmi_connector(connector);
769 drm_connector_unregister(connector);
770 drm_connector_cleanup(connector);
771 kfree(hdmi_connector);
774 static const struct drm_connector_funcs sti_hdmi_connector_funcs = {
775 .dpms = drm_atomic_helper_connector_dpms,
776 .fill_modes = drm_helper_probe_single_connector_modes,
777 .detect = sti_hdmi_connector_detect,
778 .destroy = sti_hdmi_connector_destroy,
779 .reset = drm_atomic_helper_connector_reset,
780 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
781 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
784 static struct drm_encoder *sti_hdmi_find_encoder(struct drm_device *dev)
786 struct drm_encoder *encoder;
788 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
789 if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
796 static int sti_hdmi_bind(struct device *dev, struct device *master, void *data)
798 struct sti_hdmi *hdmi = dev_get_drvdata(dev);
799 struct drm_device *drm_dev = data;
800 struct drm_encoder *encoder;
801 struct sti_hdmi_connector *connector;
802 struct drm_connector *drm_connector;
803 struct drm_bridge *bridge;
806 /* Set the drm device handle */
807 hdmi->drm_dev = drm_dev;
809 encoder = sti_hdmi_find_encoder(drm_dev);
813 connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
817 connector->hdmi = hdmi;
819 bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
823 bridge->driver_private = hdmi;
824 bridge->funcs = &sti_hdmi_bridge_funcs;
825 drm_bridge_attach(drm_dev, bridge);
827 encoder->bridge = bridge;
828 connector->encoder = encoder;
830 drm_connector = (struct drm_connector *)connector;
832 drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
834 drm_connector_init(drm_dev, drm_connector,
835 &sti_hdmi_connector_funcs, DRM_MODE_CONNECTOR_HDMIA);
836 drm_connector_helper_add(drm_connector,
837 &sti_hdmi_connector_helper_funcs);
839 err = drm_connector_register(drm_connector);
843 err = drm_mode_connector_attach_encoder(drm_connector, encoder);
845 DRM_ERROR("Failed to attach a connector to a encoder\n");
849 /* Enable default interrupts */
850 hdmi_write(hdmi, HDMI_DEFAULT_INT, HDMI_INT_EN);
855 drm_connector_unregister(drm_connector);
857 drm_connector_cleanup(drm_connector);
862 static void sti_hdmi_unbind(struct device *dev,
863 struct device *master, void *data)
868 static const struct component_ops sti_hdmi_ops = {
869 .bind = sti_hdmi_bind,
870 .unbind = sti_hdmi_unbind,
873 static const struct of_device_id hdmi_of_match[] = {
875 .compatible = "st,stih416-hdmi",
876 .data = &tx3g0c55phy_ops,
878 .compatible = "st,stih407-hdmi",
879 .data = &tx3g4c28phy_ops,
884 MODULE_DEVICE_TABLE(of, hdmi_of_match);
886 static int sti_hdmi_probe(struct platform_device *pdev)
888 struct device *dev = &pdev->dev;
889 struct sti_hdmi *hdmi;
890 struct device_node *np = dev->of_node;
891 struct resource *res;
892 struct device_node *ddc;
895 DRM_INFO("%s\n", __func__);
897 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
901 ddc = of_parse_phandle(pdev->dev.of_node, "ddc", 0);
903 hdmi->ddc_adapt = of_get_i2c_adapter_by_node(ddc);
905 if (!hdmi->ddc_adapt)
906 return -EPROBE_DEFER;
909 hdmi->dev = pdev->dev;
912 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi-reg");
914 DRM_ERROR("Invalid hdmi resource\n");
916 goto release_adapter;
918 hdmi->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
921 goto release_adapter;
924 if (of_device_is_compatible(np, "st,stih416-hdmi")) {
925 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
928 DRM_ERROR("Invalid syscfg resource\n");
930 goto release_adapter;
932 hdmi->syscfg = devm_ioremap_nocache(dev, res->start,
936 goto release_adapter;
940 hdmi->phy_ops = (struct hdmi_phy_ops *)
941 of_match_node(hdmi_of_match, np)->data;
943 /* Get clock resources */
944 hdmi->clk_pix = devm_clk_get(dev, "pix");
945 if (IS_ERR(hdmi->clk_pix)) {
946 DRM_ERROR("Cannot get hdmi_pix clock\n");
947 ret = PTR_ERR(hdmi->clk_pix);
948 goto release_adapter;
951 hdmi->clk_tmds = devm_clk_get(dev, "tmds");
952 if (IS_ERR(hdmi->clk_tmds)) {
953 DRM_ERROR("Cannot get hdmi_tmds clock\n");
954 ret = PTR_ERR(hdmi->clk_tmds);
955 goto release_adapter;
958 hdmi->clk_phy = devm_clk_get(dev, "phy");
959 if (IS_ERR(hdmi->clk_phy)) {
960 DRM_ERROR("Cannot get hdmi_phy clock\n");
961 ret = PTR_ERR(hdmi->clk_phy);
962 goto release_adapter;
965 hdmi->clk_audio = devm_clk_get(dev, "audio");
966 if (IS_ERR(hdmi->clk_audio)) {
967 DRM_ERROR("Cannot get hdmi_audio clock\n");
968 ret = PTR_ERR(hdmi->clk_audio);
969 goto release_adapter;
972 hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
974 init_waitqueue_head(&hdmi->wait_event);
976 hdmi->irq = platform_get_irq_byname(pdev, "irq");
978 ret = devm_request_threaded_irq(dev, hdmi->irq, hdmi_irq,
979 hdmi_irq_thread, IRQF_ONESHOT, dev_name(dev), hdmi);
981 DRM_ERROR("Failed to register HDMI interrupt\n");
982 goto release_adapter;
985 hdmi->reset = devm_reset_control_get(dev, "hdmi");
986 /* Take hdmi out of reset */
987 if (!IS_ERR(hdmi->reset))
988 reset_control_deassert(hdmi->reset);
990 platform_set_drvdata(pdev, hdmi);
992 return component_add(&pdev->dev, &sti_hdmi_ops);
995 i2c_put_adapter(hdmi->ddc_adapt);
1000 static int sti_hdmi_remove(struct platform_device *pdev)
1002 struct sti_hdmi *hdmi = dev_get_drvdata(&pdev->dev);
1004 i2c_put_adapter(hdmi->ddc_adapt);
1005 component_del(&pdev->dev, &sti_hdmi_ops);
1010 struct platform_driver sti_hdmi_driver = {
1013 .owner = THIS_MODULE,
1014 .of_match_table = hdmi_of_match,
1016 .probe = sti_hdmi_probe,
1017 .remove = sti_hdmi_remove,
1020 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
1021 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
1022 MODULE_LICENSE("GPL");