xref: /openbmc/linux/drivers/gpu/drm/sti/sti_hdmi.c (revision b85d4594)
1 /*
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
5  */
6 
7 #include <linux/clk.h>
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>
14 
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_edid.h>
19 
20 #include "sti_hdmi.h"
21 #include "sti_hdmi_tx3g4c28phy.h"
22 #include "sti_hdmi_tx3g0c55phy.h"
23 #include "sti_vtg.h"
24 
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 
55 #define HDMI_IFRAME_SLOT_AVI            1
56 #define HDMI_IFRAME_SLOT_AUDIO          2
57 
58 #define  XCAT(prefix, x, suffix)        prefix ## x ## suffix
59 #define  HDMI_SW_DI_N_HEAD_WORD(x)      XCAT(HDMI_SW_DI_, x, _HEAD_WORD)
60 #define  HDMI_SW_DI_N_PKT_WORD0(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD0)
61 #define  HDMI_SW_DI_N_PKT_WORD1(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD1)
62 #define  HDMI_SW_DI_N_PKT_WORD2(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD2)
63 #define  HDMI_SW_DI_N_PKT_WORD3(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD3)
64 #define  HDMI_SW_DI_N_PKT_WORD4(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD4)
65 #define  HDMI_SW_DI_N_PKT_WORD5(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD5)
66 #define  HDMI_SW_DI_N_PKT_WORD6(x)      XCAT(HDMI_SW_DI_, x, _PKT_WORD6)
67 
68 #define HDMI_IFRAME_DISABLED            0x0
69 #define HDMI_IFRAME_SINGLE_SHOT         0x1
70 #define HDMI_IFRAME_FIELD               0x2
71 #define HDMI_IFRAME_FRAME               0x3
72 #define HDMI_IFRAME_MASK                0x3
73 #define HDMI_IFRAME_CFG_DI_N(x, n)       ((x) << ((n-1)*4)) /* n from 1 to 6 */
74 
75 #define HDMI_CFG_DEVICE_EN              BIT(0)
76 #define HDMI_CFG_HDMI_NOT_DVI           BIT(1)
77 #define HDMI_CFG_HDCP_EN                BIT(2)
78 #define HDMI_CFG_ESS_NOT_OESS           BIT(3)
79 #define HDMI_CFG_H_SYNC_POL_NEG         BIT(4)
80 #define HDMI_CFG_SINK_TERM_DET_EN       BIT(5)
81 #define HDMI_CFG_V_SYNC_POL_NEG         BIT(6)
82 #define HDMI_CFG_422_EN                 BIT(8)
83 #define HDMI_CFG_FIFO_OVERRUN_CLR       BIT(12)
84 #define HDMI_CFG_FIFO_UNDERRUN_CLR      BIT(13)
85 #define HDMI_CFG_SW_RST_EN              BIT(31)
86 
87 #define HDMI_INT_GLOBAL                 BIT(0)
88 #define HDMI_INT_SW_RST                 BIT(1)
89 #define HDMI_INT_PIX_CAP                BIT(3)
90 #define HDMI_INT_HOT_PLUG               BIT(4)
91 #define HDMI_INT_DLL_LCK                BIT(5)
92 #define HDMI_INT_NEW_FRAME              BIT(6)
93 #define HDMI_INT_GENCTRL_PKT            BIT(7)
94 #define HDMI_INT_SINK_TERM_PRESENT      BIT(11)
95 
96 #define HDMI_DEFAULT_INT (HDMI_INT_SINK_TERM_PRESENT \
97 			| HDMI_INT_DLL_LCK \
98 			| HDMI_INT_HOT_PLUG \
99 			| HDMI_INT_GLOBAL)
100 
101 #define HDMI_WORKING_INT (HDMI_INT_SINK_TERM_PRESENT \
102 			| HDMI_INT_GENCTRL_PKT \
103 			| HDMI_INT_NEW_FRAME \
104 			| HDMI_INT_DLL_LCK \
105 			| HDMI_INT_HOT_PLUG \
106 			| HDMI_INT_PIX_CAP \
107 			| HDMI_INT_SW_RST \
108 			| HDMI_INT_GLOBAL)
109 
110 #define HDMI_STA_SW_RST                 BIT(1)
111 
112 #define HDMI_INFOFRAME_HEADER_TYPE(x)    (((x) & 0xff) <<  0)
113 #define HDMI_INFOFRAME_HEADER_VERSION(x) (((x) & 0xff) <<  8)
114 #define HDMI_INFOFRAME_HEADER_LEN(x)     (((x) & 0x0f) << 16)
115 
116 struct sti_hdmi_connector {
117 	struct drm_connector drm_connector;
118 	struct drm_encoder *encoder;
119 	struct sti_hdmi *hdmi;
120 };
121 
122 #define to_sti_hdmi_connector(x) \
123 	container_of(x, struct sti_hdmi_connector, drm_connector)
124 
125 u32 hdmi_read(struct sti_hdmi *hdmi, int offset)
126 {
127 	return readl(hdmi->regs + offset);
128 }
129 
130 void hdmi_write(struct sti_hdmi *hdmi, u32 val, int offset)
131 {
132 	writel(val, hdmi->regs + offset);
133 }
134 
135 /**
136  * HDMI interrupt handler threaded
137  *
138  * @irq: irq number
139  * @arg: connector structure
140  */
141 static irqreturn_t hdmi_irq_thread(int irq, void *arg)
142 {
143 	struct sti_hdmi *hdmi = arg;
144 
145 	/* Hot plug/unplug IRQ */
146 	if (hdmi->irq_status & HDMI_INT_HOT_PLUG) {
147 		hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
148 		if (hdmi->drm_dev)
149 			drm_helper_hpd_irq_event(hdmi->drm_dev);
150 	}
151 
152 	/* Sw reset and PLL lock are exclusive so we can use the same
153 	 * event to signal them
154 	 */
155 	if (hdmi->irq_status & (HDMI_INT_SW_RST | HDMI_INT_DLL_LCK)) {
156 		hdmi->event_received = true;
157 		wake_up_interruptible(&hdmi->wait_event);
158 	}
159 
160 	return IRQ_HANDLED;
161 }
162 
163 /**
164  * HDMI interrupt handler
165  *
166  * @irq: irq number
167  * @arg: connector structure
168  */
169 static irqreturn_t hdmi_irq(int irq, void *arg)
170 {
171 	struct sti_hdmi *hdmi = arg;
172 
173 	/* read interrupt status */
174 	hdmi->irq_status = hdmi_read(hdmi, HDMI_INT_STA);
175 
176 	/* clear interrupt status */
177 	hdmi_write(hdmi, hdmi->irq_status, HDMI_INT_CLR);
178 
179 	/* force sync bus write */
180 	hdmi_read(hdmi, HDMI_INT_STA);
181 
182 	return IRQ_WAKE_THREAD;
183 }
184 
185 /**
186  * Set hdmi active area depending on the drm display mode selected
187  *
188  * @hdmi: pointer on the hdmi internal structure
189  */
190 static void hdmi_active_area(struct sti_hdmi *hdmi)
191 {
192 	u32 xmin, xmax;
193 	u32 ymin, ymax;
194 
195 	xmin = sti_vtg_get_pixel_number(hdmi->mode, 1);
196 	xmax = sti_vtg_get_pixel_number(hdmi->mode, hdmi->mode.hdisplay);
197 	ymin = sti_vtg_get_line_number(hdmi->mode, 0);
198 	ymax = sti_vtg_get_line_number(hdmi->mode, hdmi->mode.vdisplay - 1);
199 
200 	hdmi_write(hdmi, xmin, HDMI_ACTIVE_VID_XMIN);
201 	hdmi_write(hdmi, xmax, HDMI_ACTIVE_VID_XMAX);
202 	hdmi_write(hdmi, ymin, HDMI_ACTIVE_VID_YMIN);
203 	hdmi_write(hdmi, ymax, HDMI_ACTIVE_VID_YMAX);
204 }
205 
206 /**
207  * Overall hdmi configuration
208  *
209  * @hdmi: pointer on the hdmi internal structure
210  */
211 static void hdmi_config(struct sti_hdmi *hdmi)
212 {
213 	u32 conf;
214 
215 	DRM_DEBUG_DRIVER("\n");
216 
217 	/* Clear overrun and underrun fifo */
218 	conf = HDMI_CFG_FIFO_OVERRUN_CLR | HDMI_CFG_FIFO_UNDERRUN_CLR;
219 
220 	/* Enable HDMI mode not DVI */
221 	conf |= HDMI_CFG_HDMI_NOT_DVI | HDMI_CFG_ESS_NOT_OESS;
222 
223 	/* Enable sink term detection */
224 	conf |= HDMI_CFG_SINK_TERM_DET_EN;
225 
226 	/* Set Hsync polarity */
227 	if (hdmi->mode.flags & DRM_MODE_FLAG_NHSYNC) {
228 		DRM_DEBUG_DRIVER("H Sync Negative\n");
229 		conf |= HDMI_CFG_H_SYNC_POL_NEG;
230 	}
231 
232 	/* Set Vsync polarity */
233 	if (hdmi->mode.flags & DRM_MODE_FLAG_NVSYNC) {
234 		DRM_DEBUG_DRIVER("V Sync Negative\n");
235 		conf |= HDMI_CFG_V_SYNC_POL_NEG;
236 	}
237 
238 	/* Enable HDMI */
239 	conf |= HDMI_CFG_DEVICE_EN;
240 
241 	hdmi_write(hdmi, conf, HDMI_CFG);
242 }
243 
244 /**
245  * Helper to concatenate infoframe in 32 bits word
246  *
247  * @ptr: pointer on the hdmi internal structure
248  * @data: infoframe to write
249  * @size: size to write
250  */
251 static inline unsigned int hdmi_infoframe_subpack(const u8 *ptr, size_t size)
252 {
253 	unsigned long value = 0;
254 	size_t i;
255 
256 	for (i = size; i > 0; i--)
257 		value = (value << 8) | ptr[i - 1];
258 
259 	return value;
260 }
261 
262 /**
263  * Helper to write info frame
264  *
265  * @hdmi: pointer on the hdmi internal structure
266  * @data: infoframe to write
267  * @size: size to write
268  */
269 static void hdmi_infoframe_write_infopack(struct sti_hdmi *hdmi, const u8 *data)
270 {
271 	const u8 *ptr = data;
272 	u32 val, slot, mode, i;
273 	u32 head_offset, pack_offset;
274 	size_t size;
275 
276 	switch (*ptr) {
277 	case HDMI_INFOFRAME_TYPE_AVI:
278 		slot = HDMI_IFRAME_SLOT_AVI;
279 		mode = HDMI_IFRAME_FIELD;
280 		head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AVI);
281 		pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AVI);
282 		size = HDMI_AVI_INFOFRAME_SIZE;
283 		break;
284 
285 	case HDMI_INFOFRAME_TYPE_AUDIO:
286 		slot = HDMI_IFRAME_SLOT_AUDIO;
287 		mode = HDMI_IFRAME_FRAME;
288 		head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AUDIO);
289 		pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AUDIO);
290 		size = HDMI_AUDIO_INFOFRAME_SIZE;
291 		break;
292 
293 	default:
294 		DRM_ERROR("unsupported infoframe type: %#x\n", *ptr);
295 		return;
296 	}
297 
298 	/* Disable transmission slot for updated infoframe */
299 	val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
300 	val &= ~HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, slot);
301 	hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
302 
303 	val = HDMI_INFOFRAME_HEADER_TYPE(*ptr++);
304 	val |= HDMI_INFOFRAME_HEADER_VERSION(*ptr++);
305 	val |= HDMI_INFOFRAME_HEADER_LEN(*ptr++);
306 	writel(val, hdmi->regs + head_offset);
307 
308 	/*
309 	 * Each subpack contains 4 bytes
310 	 * The First Bytes of the first subpacket must contain the checksum
311 	 * Packet size in increase by one.
312 	 */
313 	for (i = 0; i < size; i += sizeof(u32)) {
314 		size_t num;
315 
316 		num = min_t(size_t, size - i, sizeof(u32));
317 		val = hdmi_infoframe_subpack(ptr, num);
318 		ptr += sizeof(u32);
319 		writel(val, hdmi->regs + pack_offset + i);
320 	}
321 
322 	/* Enable transmission slot for updated infoframe */
323 	val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
324 	val |= HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_FIELD, slot);
325 	hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
326 }
327 
328 /**
329  * Prepare and configure the AVI infoframe
330  *
331  * AVI infoframe are transmitted at least once per two video field and
332  * contains information about HDMI transmission mode such as color space,
333  * colorimetry, ...
334  *
335  * @hdmi: pointer on the hdmi internal structure
336  *
337  * Return negative value if error occurs
338  */
339 static int hdmi_avi_infoframe_config(struct sti_hdmi *hdmi)
340 {
341 	struct drm_display_mode *mode = &hdmi->mode;
342 	struct hdmi_avi_infoframe infoframe;
343 	u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
344 	int ret;
345 
346 	DRM_DEBUG_DRIVER("\n");
347 
348 	ret = drm_hdmi_avi_infoframe_from_display_mode(&infoframe, mode);
349 	if (ret < 0) {
350 		DRM_ERROR("failed to setup AVI infoframe: %d\n", ret);
351 		return ret;
352 	}
353 
354 	/* fixed infoframe configuration not linked to the mode */
355 	infoframe.colorspace = HDMI_COLORSPACE_RGB;
356 	infoframe.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
357 	infoframe.colorimetry = HDMI_COLORIMETRY_NONE;
358 
359 	ret = hdmi_avi_infoframe_pack(&infoframe, buffer, sizeof(buffer));
360 	if (ret < 0) {
361 		DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
362 		return ret;
363 	}
364 
365 	hdmi_infoframe_write_infopack(hdmi, buffer);
366 
367 	return 0;
368 }
369 
370 /**
371  * Prepare and configure the AUDIO infoframe
372  *
373  * AUDIO infoframe are transmitted once per frame and
374  * contains information about HDMI transmission mode such as audio codec,
375  * sample size, ...
376  *
377  * @hdmi: pointer on the hdmi internal structure
378  *
379  * Return negative value if error occurs
380  */
381 static int hdmi_audio_infoframe_config(struct sti_hdmi *hdmi)
382 {
383 	struct hdmi_audio_infoframe infofame;
384 	u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
385 	int ret;
386 
387 	ret = hdmi_audio_infoframe_init(&infofame);
388 	if (ret < 0) {
389 		DRM_ERROR("failed to setup audio infoframe: %d\n", ret);
390 		return ret;
391 	}
392 
393 	infofame.channels = 2;
394 
395 	ret = hdmi_audio_infoframe_pack(&infofame, buffer, sizeof(buffer));
396 	if (ret < 0) {
397 		DRM_ERROR("failed to pack audio infoframe: %d\n", ret);
398 		return ret;
399 	}
400 
401 	hdmi_infoframe_write_infopack(hdmi, buffer);
402 
403 	return 0;
404 }
405 
406 /**
407  * Software reset of the hdmi subsystem
408  *
409  * @hdmi: pointer on the hdmi internal structure
410  *
411  */
412 #define HDMI_TIMEOUT_SWRESET  100   /*milliseconds */
413 static void hdmi_swreset(struct sti_hdmi *hdmi)
414 {
415 	u32 val;
416 
417 	DRM_DEBUG_DRIVER("\n");
418 
419 	/* Enable hdmi_audio clock only during hdmi reset */
420 	if (clk_prepare_enable(hdmi->clk_audio))
421 		DRM_INFO("Failed to prepare/enable hdmi_audio clk\n");
422 
423 	/* Sw reset */
424 	hdmi->event_received = false;
425 
426 	val = hdmi_read(hdmi, HDMI_CFG);
427 	val |= HDMI_CFG_SW_RST_EN;
428 	hdmi_write(hdmi, val, HDMI_CFG);
429 
430 	/* Wait reset completed */
431 	wait_event_interruptible_timeout(hdmi->wait_event,
432 					 hdmi->event_received == true,
433 					 msecs_to_jiffies
434 					 (HDMI_TIMEOUT_SWRESET));
435 
436 	/*
437 	 * HDMI_STA_SW_RST bit is set to '1' when SW_RST bit in HDMI_CFG is
438 	 * set to '1' and clk_audio is running.
439 	 */
440 	if ((hdmi_read(hdmi, HDMI_STA) & HDMI_STA_SW_RST) == 0)
441 		DRM_DEBUG_DRIVER("Warning: HDMI sw reset timeout occurs\n");
442 
443 	val = hdmi_read(hdmi, HDMI_CFG);
444 	val &= ~HDMI_CFG_SW_RST_EN;
445 	hdmi_write(hdmi, val, HDMI_CFG);
446 
447 	/* Disable hdmi_audio clock. Not used anymore for drm purpose */
448 	clk_disable_unprepare(hdmi->clk_audio);
449 }
450 
451 static void sti_hdmi_disable(struct drm_bridge *bridge)
452 {
453 	struct sti_hdmi *hdmi = bridge->driver_private;
454 
455 	u32 val = hdmi_read(hdmi, HDMI_CFG);
456 
457 	if (!hdmi->enabled)
458 		return;
459 
460 	DRM_DEBUG_DRIVER("\n");
461 
462 	/* Disable HDMI */
463 	val &= ~HDMI_CFG_DEVICE_EN;
464 	hdmi_write(hdmi, val, HDMI_CFG);
465 
466 	hdmi_write(hdmi, 0xffffffff, HDMI_INT_CLR);
467 
468 	/* Stop the phy */
469 	hdmi->phy_ops->stop(hdmi);
470 
471 	/* Set the default channel data to be a dark red */
472 	hdmi_write(hdmi, 0x0000, HDMI_DFLT_CHL0_DAT);
473 	hdmi_write(hdmi, 0x0000, HDMI_DFLT_CHL1_DAT);
474 	hdmi_write(hdmi, 0x0060, HDMI_DFLT_CHL2_DAT);
475 
476 	/* Disable/unprepare hdmi clock */
477 	clk_disable_unprepare(hdmi->clk_phy);
478 	clk_disable_unprepare(hdmi->clk_tmds);
479 	clk_disable_unprepare(hdmi->clk_pix);
480 
481 	hdmi->enabled = false;
482 }
483 
484 static void sti_hdmi_pre_enable(struct drm_bridge *bridge)
485 {
486 	struct sti_hdmi *hdmi = bridge->driver_private;
487 
488 	DRM_DEBUG_DRIVER("\n");
489 
490 	if (hdmi->enabled)
491 		return;
492 
493 	/* Prepare/enable clocks */
494 	if (clk_prepare_enable(hdmi->clk_pix))
495 		DRM_ERROR("Failed to prepare/enable hdmi_pix clk\n");
496 	if (clk_prepare_enable(hdmi->clk_tmds))
497 		DRM_ERROR("Failed to prepare/enable hdmi_tmds clk\n");
498 	if (clk_prepare_enable(hdmi->clk_phy))
499 		DRM_ERROR("Failed to prepare/enable hdmi_rejec_pll clk\n");
500 
501 	hdmi->enabled = true;
502 
503 	/* Program hdmi serializer and start phy */
504 	if (!hdmi->phy_ops->start(hdmi)) {
505 		DRM_ERROR("Unable to start hdmi phy\n");
506 		return;
507 	}
508 
509 	/* Program hdmi active area */
510 	hdmi_active_area(hdmi);
511 
512 	/* Enable working interrupts */
513 	hdmi_write(hdmi, HDMI_WORKING_INT, HDMI_INT_EN);
514 
515 	/* Program hdmi config */
516 	hdmi_config(hdmi);
517 
518 	/* Program AVI infoframe */
519 	if (hdmi_avi_infoframe_config(hdmi))
520 		DRM_ERROR("Unable to configure AVI infoframe\n");
521 
522 	/* Program AUDIO infoframe */
523 	if (hdmi_audio_infoframe_config(hdmi))
524 		DRM_ERROR("Unable to configure AUDIO infoframe\n");
525 
526 	/* Sw reset */
527 	hdmi_swreset(hdmi);
528 }
529 
530 static void sti_hdmi_set_mode(struct drm_bridge *bridge,
531 		struct drm_display_mode *mode,
532 		struct drm_display_mode *adjusted_mode)
533 {
534 	struct sti_hdmi *hdmi = bridge->driver_private;
535 	int ret;
536 
537 	DRM_DEBUG_DRIVER("\n");
538 
539 	/* Copy the drm display mode in the connector local structure */
540 	memcpy(&hdmi->mode, mode, sizeof(struct drm_display_mode));
541 
542 	/* Update clock framerate according to the selected mode */
543 	ret = clk_set_rate(hdmi->clk_pix, mode->clock * 1000);
544 	if (ret < 0) {
545 		DRM_ERROR("Cannot set rate (%dHz) for hdmi_pix clk\n",
546 			  mode->clock * 1000);
547 		return;
548 	}
549 	ret = clk_set_rate(hdmi->clk_phy, mode->clock * 1000);
550 	if (ret < 0) {
551 		DRM_ERROR("Cannot set rate (%dHz) for hdmi_rejection_pll clk\n",
552 			  mode->clock * 1000);
553 		return;
554 	}
555 }
556 
557 static void sti_hdmi_bridge_nope(struct drm_bridge *bridge)
558 {
559 	/* do nothing */
560 }
561 
562 static const struct drm_bridge_funcs sti_hdmi_bridge_funcs = {
563 	.pre_enable = sti_hdmi_pre_enable,
564 	.enable = sti_hdmi_bridge_nope,
565 	.disable = sti_hdmi_disable,
566 	.post_disable = sti_hdmi_bridge_nope,
567 	.mode_set = sti_hdmi_set_mode,
568 };
569 
570 static int sti_hdmi_connector_get_modes(struct drm_connector *connector)
571 {
572 	struct sti_hdmi_connector *hdmi_connector
573 		= to_sti_hdmi_connector(connector);
574 	struct sti_hdmi *hdmi = hdmi_connector->hdmi;
575 	struct edid *edid;
576 	int count;
577 
578 	DRM_DEBUG_DRIVER("\n");
579 
580 	edid = drm_get_edid(connector, hdmi->ddc_adapt);
581 	if (!edid)
582 		goto fail;
583 
584 	count = drm_add_edid_modes(connector, edid);
585 	drm_mode_connector_update_edid_property(connector, edid);
586 
587 	kfree(edid);
588 	return count;
589 
590 fail:
591 	DRM_ERROR("Can't read HDMI EDID\n");
592 	return 0;
593 }
594 
595 #define CLK_TOLERANCE_HZ 50
596 
597 static int sti_hdmi_connector_mode_valid(struct drm_connector *connector,
598 					struct drm_display_mode *mode)
599 {
600 	int target = mode->clock * 1000;
601 	int target_min = target - CLK_TOLERANCE_HZ;
602 	int target_max = target + CLK_TOLERANCE_HZ;
603 	int result;
604 	struct sti_hdmi_connector *hdmi_connector
605 		= to_sti_hdmi_connector(connector);
606 	struct sti_hdmi *hdmi = hdmi_connector->hdmi;
607 
608 
609 	result = clk_round_rate(hdmi->clk_pix, target);
610 
611 	DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
612 			 target, result);
613 
614 	if ((result < target_min) || (result > target_max)) {
615 		DRM_DEBUG_DRIVER("hdmi pixclk=%d not supported\n", target);
616 		return MODE_BAD;
617 	}
618 
619 	return MODE_OK;
620 }
621 
622 struct drm_encoder *sti_hdmi_best_encoder(struct drm_connector *connector)
623 {
624 	struct sti_hdmi_connector *hdmi_connector
625 		= to_sti_hdmi_connector(connector);
626 
627 	/* Best encoder is the one associated during connector creation */
628 	return hdmi_connector->encoder;
629 }
630 
631 static struct drm_connector_helper_funcs sti_hdmi_connector_helper_funcs = {
632 	.get_modes = sti_hdmi_connector_get_modes,
633 	.mode_valid = sti_hdmi_connector_mode_valid,
634 	.best_encoder = sti_hdmi_best_encoder,
635 };
636 
637 /* get detection status of display device */
638 static enum drm_connector_status
639 sti_hdmi_connector_detect(struct drm_connector *connector, bool force)
640 {
641 	struct sti_hdmi_connector *hdmi_connector
642 		= to_sti_hdmi_connector(connector);
643 	struct sti_hdmi *hdmi = hdmi_connector->hdmi;
644 
645 	DRM_DEBUG_DRIVER("\n");
646 
647 	if (hdmi->hpd) {
648 		DRM_DEBUG_DRIVER("hdmi cable connected\n");
649 		return connector_status_connected;
650 	}
651 
652 	DRM_DEBUG_DRIVER("hdmi cable disconnected\n");
653 	return connector_status_disconnected;
654 }
655 
656 static void sti_hdmi_connector_destroy(struct drm_connector *connector)
657 {
658 	struct sti_hdmi_connector *hdmi_connector
659 		= to_sti_hdmi_connector(connector);
660 
661 	drm_connector_unregister(connector);
662 	drm_connector_cleanup(connector);
663 	kfree(hdmi_connector);
664 }
665 
666 static struct drm_connector_funcs sti_hdmi_connector_funcs = {
667 	.dpms = drm_atomic_helper_connector_dpms,
668 	.fill_modes = drm_helper_probe_single_connector_modes,
669 	.detect = sti_hdmi_connector_detect,
670 	.destroy = sti_hdmi_connector_destroy,
671 	.reset = drm_atomic_helper_connector_reset,
672 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
673 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
674 };
675 
676 static struct drm_encoder *sti_hdmi_find_encoder(struct drm_device *dev)
677 {
678 	struct drm_encoder *encoder;
679 
680 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
681 		if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
682 			return encoder;
683 	}
684 
685 	return NULL;
686 }
687 
688 static int sti_hdmi_bind(struct device *dev, struct device *master, void *data)
689 {
690 	struct sti_hdmi *hdmi = dev_get_drvdata(dev);
691 	struct drm_device *drm_dev = data;
692 	struct drm_encoder *encoder;
693 	struct sti_hdmi_connector *connector;
694 	struct drm_connector *drm_connector;
695 	struct drm_bridge *bridge;
696 	int err;
697 
698 	/* Set the drm device handle */
699 	hdmi->drm_dev = drm_dev;
700 
701 	encoder = sti_hdmi_find_encoder(drm_dev);
702 	if (!encoder)
703 		goto err_adapt;
704 
705 	connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
706 	if (!connector)
707 		goto err_adapt;
708 
709 
710 	connector->hdmi = hdmi;
711 
712 	bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
713 	if (!bridge)
714 		goto err_adapt;
715 
716 	bridge->driver_private = hdmi;
717 	bridge->funcs = &sti_hdmi_bridge_funcs;
718 	drm_bridge_attach(drm_dev, bridge);
719 
720 	encoder->bridge = bridge;
721 	connector->encoder = encoder;
722 
723 	drm_connector = (struct drm_connector *)connector;
724 
725 	drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
726 
727 	drm_connector_init(drm_dev, drm_connector,
728 			&sti_hdmi_connector_funcs, DRM_MODE_CONNECTOR_HDMIA);
729 	drm_connector_helper_add(drm_connector,
730 			&sti_hdmi_connector_helper_funcs);
731 
732 	err = drm_connector_register(drm_connector);
733 	if (err)
734 		goto err_connector;
735 
736 	err = drm_mode_connector_attach_encoder(drm_connector, encoder);
737 	if (err) {
738 		DRM_ERROR("Failed to attach a connector to a encoder\n");
739 		goto err_sysfs;
740 	}
741 
742 	/* Enable default interrupts */
743 	hdmi_write(hdmi, HDMI_DEFAULT_INT, HDMI_INT_EN);
744 
745 	return 0;
746 
747 err_sysfs:
748 	drm_connector_unregister(drm_connector);
749 err_connector:
750 	drm_connector_cleanup(drm_connector);
751 err_adapt:
752 	put_device(&hdmi->ddc_adapt->dev);
753 	return -EINVAL;
754 }
755 
756 static void sti_hdmi_unbind(struct device *dev,
757 		struct device *master, void *data)
758 {
759 	/* do nothing */
760 }
761 
762 static const struct component_ops sti_hdmi_ops = {
763 	.bind = sti_hdmi_bind,
764 	.unbind = sti_hdmi_unbind,
765 };
766 
767 static const struct of_device_id hdmi_of_match[] = {
768 	{
769 		.compatible = "st,stih416-hdmi",
770 		.data = &tx3g0c55phy_ops,
771 	}, {
772 		.compatible = "st,stih407-hdmi",
773 		.data = &tx3g4c28phy_ops,
774 	}, {
775 		/* end node */
776 	}
777 };
778 MODULE_DEVICE_TABLE(of, hdmi_of_match);
779 
780 static int sti_hdmi_probe(struct platform_device *pdev)
781 {
782 	struct device *dev = &pdev->dev;
783 	struct sti_hdmi *hdmi;
784 	struct device_node *np = dev->of_node;
785 	struct resource *res;
786 	struct device_node *ddc;
787 	int ret;
788 
789 	DRM_INFO("%s\n", __func__);
790 
791 	hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
792 	if (!hdmi)
793 		return -ENOMEM;
794 
795 	ddc = of_parse_phandle(pdev->dev.of_node, "ddc", 0);
796 	if (ddc) {
797 		hdmi->ddc_adapt = of_find_i2c_adapter_by_node(ddc);
798 		if (!hdmi->ddc_adapt) {
799 			of_node_put(ddc);
800 			return -EPROBE_DEFER;
801 		}
802 
803 		of_node_put(ddc);
804 	}
805 
806 	hdmi->dev = pdev->dev;
807 
808 	/* Get resources */
809 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi-reg");
810 	if (!res) {
811 		DRM_ERROR("Invalid hdmi resource\n");
812 		return -ENOMEM;
813 	}
814 	hdmi->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
815 	if (!hdmi->regs)
816 		return -ENOMEM;
817 
818 	if (of_device_is_compatible(np, "st,stih416-hdmi")) {
819 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
820 						   "syscfg");
821 		if (!res) {
822 			DRM_ERROR("Invalid syscfg resource\n");
823 			return -ENOMEM;
824 		}
825 		hdmi->syscfg = devm_ioremap_nocache(dev, res->start,
826 						    resource_size(res));
827 		if (!hdmi->syscfg)
828 			return -ENOMEM;
829 
830 	}
831 
832 	hdmi->phy_ops = (struct hdmi_phy_ops *)
833 		of_match_node(hdmi_of_match, np)->data;
834 
835 	/* Get clock resources */
836 	hdmi->clk_pix = devm_clk_get(dev, "pix");
837 	if (IS_ERR(hdmi->clk_pix)) {
838 		DRM_ERROR("Cannot get hdmi_pix clock\n");
839 		return PTR_ERR(hdmi->clk_pix);
840 	}
841 
842 	hdmi->clk_tmds = devm_clk_get(dev, "tmds");
843 	if (IS_ERR(hdmi->clk_tmds)) {
844 		DRM_ERROR("Cannot get hdmi_tmds clock\n");
845 		return PTR_ERR(hdmi->clk_tmds);
846 	}
847 
848 	hdmi->clk_phy = devm_clk_get(dev, "phy");
849 	if (IS_ERR(hdmi->clk_phy)) {
850 		DRM_ERROR("Cannot get hdmi_phy clock\n");
851 		return PTR_ERR(hdmi->clk_phy);
852 	}
853 
854 	hdmi->clk_audio = devm_clk_get(dev, "audio");
855 	if (IS_ERR(hdmi->clk_audio)) {
856 		DRM_ERROR("Cannot get hdmi_audio clock\n");
857 		return PTR_ERR(hdmi->clk_audio);
858 	}
859 
860 	hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
861 
862 	init_waitqueue_head(&hdmi->wait_event);
863 
864 	hdmi->irq = platform_get_irq_byname(pdev, "irq");
865 
866 	ret = devm_request_threaded_irq(dev, hdmi->irq, hdmi_irq,
867 			hdmi_irq_thread, IRQF_ONESHOT, dev_name(dev), hdmi);
868 	if (ret) {
869 		DRM_ERROR("Failed to register HDMI interrupt\n");
870 		return ret;
871 	}
872 
873 	hdmi->reset = devm_reset_control_get(dev, "hdmi");
874 	/* Take hdmi out of reset */
875 	if (!IS_ERR(hdmi->reset))
876 		reset_control_deassert(hdmi->reset);
877 
878 	platform_set_drvdata(pdev, hdmi);
879 
880 	return component_add(&pdev->dev, &sti_hdmi_ops);
881 }
882 
883 static int sti_hdmi_remove(struct platform_device *pdev)
884 {
885 	struct sti_hdmi *hdmi = dev_get_drvdata(&pdev->dev);
886 
887 	if (hdmi->ddc_adapt)
888 		put_device(&hdmi->ddc_adapt->dev);
889 
890 	component_del(&pdev->dev, &sti_hdmi_ops);
891 	return 0;
892 }
893 
894 struct platform_driver sti_hdmi_driver = {
895 	.driver = {
896 		.name = "sti-hdmi",
897 		.owner = THIS_MODULE,
898 		.of_match_table = hdmi_of_match,
899 	},
900 	.probe = sti_hdmi_probe,
901 	.remove = sti_hdmi_remove,
902 };
903 
904 module_platform_driver(sti_hdmi_driver);
905 
906 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
907 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
908 MODULE_LICENSE("GPL");
909