1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Maxime Ripard
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
9 #include <linux/component.h>
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/reset.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_encoder.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_panel.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_probe_helper.h>
26 #include "sun4i_backend.h"
27 #include "sun4i_crtc.h"
28 #include "sun4i_drv.h"
29 #include "sun4i_hdmi.h"
31 static inline struct sun4i_hdmi
*
32 drm_encoder_to_sun4i_hdmi(struct drm_encoder
*encoder
)
34 return container_of(encoder
, struct sun4i_hdmi
,
38 static inline struct sun4i_hdmi
*
39 drm_connector_to_sun4i_hdmi(struct drm_connector
*connector
)
41 return container_of(connector
, struct sun4i_hdmi
,
45 static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi
*hdmi
,
46 struct drm_display_mode
*mode
)
48 struct hdmi_avi_infoframe frame
;
52 ret
= drm_hdmi_avi_infoframe_from_display_mode(&frame
,
53 &hdmi
->connector
, mode
);
55 DRM_ERROR("Failed to get infoframes from mode\n");
59 ret
= hdmi_avi_infoframe_pack(&frame
, buffer
, sizeof(buffer
));
61 DRM_ERROR("Failed to pack infoframes\n");
65 for (i
= 0; i
< sizeof(buffer
); i
++)
66 writeb(buffer
[i
], hdmi
->base
+ SUN4I_HDMI_AVI_INFOFRAME_REG(i
));
71 static int sun4i_hdmi_atomic_check(struct drm_encoder
*encoder
,
72 struct drm_crtc_state
*crtc_state
,
73 struct drm_connector_state
*conn_state
)
75 struct drm_display_mode
*mode
= &crtc_state
->mode
;
77 if (mode
->flags
& DRM_MODE_FLAG_DBLCLK
)
83 static void sun4i_hdmi_disable(struct drm_encoder
*encoder
)
85 struct sun4i_hdmi
*hdmi
= drm_encoder_to_sun4i_hdmi(encoder
);
88 DRM_DEBUG_DRIVER("Disabling the HDMI Output\n");
90 val
= readl(hdmi
->base
+ SUN4I_HDMI_VID_CTRL_REG
);
91 val
&= ~SUN4I_HDMI_VID_CTRL_ENABLE
;
92 writel(val
, hdmi
->base
+ SUN4I_HDMI_VID_CTRL_REG
);
94 clk_disable_unprepare(hdmi
->tmds_clk
);
97 static void sun4i_hdmi_enable(struct drm_encoder
*encoder
)
99 struct drm_display_mode
*mode
= &encoder
->crtc
->state
->adjusted_mode
;
100 struct sun4i_hdmi
*hdmi
= drm_encoder_to_sun4i_hdmi(encoder
);
103 DRM_DEBUG_DRIVER("Enabling the HDMI Output\n");
105 clk_prepare_enable(hdmi
->tmds_clk
);
107 sun4i_hdmi_setup_avi_infoframes(hdmi
, mode
);
108 val
|= SUN4I_HDMI_PKT_CTRL_TYPE(0, SUN4I_HDMI_PKT_AVI
);
109 val
|= SUN4I_HDMI_PKT_CTRL_TYPE(1, SUN4I_HDMI_PKT_END
);
110 writel(val
, hdmi
->base
+ SUN4I_HDMI_PKT_CTRL_REG(0));
112 val
= SUN4I_HDMI_VID_CTRL_ENABLE
;
113 if (hdmi
->hdmi_monitor
)
114 val
|= SUN4I_HDMI_VID_CTRL_HDMI_MODE
;
116 writel(val
, hdmi
->base
+ SUN4I_HDMI_VID_CTRL_REG
);
119 static void sun4i_hdmi_mode_set(struct drm_encoder
*encoder
,
120 struct drm_display_mode
*mode
,
121 struct drm_display_mode
*adjusted_mode
)
123 struct sun4i_hdmi
*hdmi
= drm_encoder_to_sun4i_hdmi(encoder
);
127 clk_set_rate(hdmi
->mod_clk
, mode
->crtc_clock
* 1000);
128 clk_set_rate(hdmi
->tmds_clk
, mode
->crtc_clock
* 1000);
130 /* Set input sync enable */
131 writel(SUN4I_HDMI_UNKNOWN_INPUT_SYNC
,
132 hdmi
->base
+ SUN4I_HDMI_UNKNOWN_REG
);
135 * Setup output pad (?) controls
137 * This is done here instead of at probe/bind time because
138 * the controller seems to toggle some of the bits on its own.
140 * We can't just initialize the register there, we need to
141 * protect the clock bits that have already been read out and
142 * cached by the clock framework.
144 val
= readl(hdmi
->base
+ SUN4I_HDMI_PAD_CTRL1_REG
);
145 val
&= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK
;
146 val
|= hdmi
->variant
->pad_ctrl1_init_val
;
147 writel(val
, hdmi
->base
+ SUN4I_HDMI_PAD_CTRL1_REG
);
148 val
= readl(hdmi
->base
+ SUN4I_HDMI_PAD_CTRL1_REG
);
150 /* Setup timing registers */
151 writel(SUN4I_HDMI_VID_TIMING_X(mode
->hdisplay
) |
152 SUN4I_HDMI_VID_TIMING_Y(mode
->vdisplay
),
153 hdmi
->base
+ SUN4I_HDMI_VID_TIMING_ACT_REG
);
155 x
= mode
->htotal
- mode
->hsync_start
;
156 y
= mode
->vtotal
- mode
->vsync_start
;
157 writel(SUN4I_HDMI_VID_TIMING_X(x
) | SUN4I_HDMI_VID_TIMING_Y(y
),
158 hdmi
->base
+ SUN4I_HDMI_VID_TIMING_BP_REG
);
160 x
= mode
->hsync_start
- mode
->hdisplay
;
161 y
= mode
->vsync_start
- mode
->vdisplay
;
162 writel(SUN4I_HDMI_VID_TIMING_X(x
) | SUN4I_HDMI_VID_TIMING_Y(y
),
163 hdmi
->base
+ SUN4I_HDMI_VID_TIMING_FP_REG
);
165 x
= mode
->hsync_end
- mode
->hsync_start
;
166 y
= mode
->vsync_end
- mode
->vsync_start
;
167 writel(SUN4I_HDMI_VID_TIMING_X(x
) | SUN4I_HDMI_VID_TIMING_Y(y
),
168 hdmi
->base
+ SUN4I_HDMI_VID_TIMING_SPW_REG
);
170 val
= SUN4I_HDMI_VID_TIMING_POL_TX_CLK
;
171 if (mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
172 val
|= SUN4I_HDMI_VID_TIMING_POL_HSYNC
;
174 if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
175 val
|= SUN4I_HDMI_VID_TIMING_POL_VSYNC
;
177 writel(val
, hdmi
->base
+ SUN4I_HDMI_VID_TIMING_POL_REG
);
180 static enum drm_mode_status
sun4i_hdmi_mode_valid(struct drm_encoder
*encoder
,
181 const struct drm_display_mode
*mode
)
183 struct sun4i_hdmi
*hdmi
= drm_encoder_to_sun4i_hdmi(encoder
);
184 unsigned long rate
= mode
->clock
* 1000;
185 unsigned long diff
= rate
/ 200; /* +-0.5% allowed by HDMI spec */
188 /* 165 MHz is the typical max pixelclock frequency for HDMI <= 1.2 */
189 if (rate
> 165000000)
190 return MODE_CLOCK_HIGH
;
191 rounded_rate
= clk_round_rate(hdmi
->tmds_clk
, rate
);
192 if (rounded_rate
> 0 &&
193 max_t(unsigned long, rounded_rate
, rate
) -
194 min_t(unsigned long, rounded_rate
, rate
) < diff
)
199 static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs
= {
200 .atomic_check
= sun4i_hdmi_atomic_check
,
201 .disable
= sun4i_hdmi_disable
,
202 .enable
= sun4i_hdmi_enable
,
203 .mode_set
= sun4i_hdmi_mode_set
,
204 .mode_valid
= sun4i_hdmi_mode_valid
,
207 static const struct drm_encoder_funcs sun4i_hdmi_funcs
= {
208 .destroy
= drm_encoder_cleanup
,
211 static int sun4i_hdmi_get_modes(struct drm_connector
*connector
)
213 struct sun4i_hdmi
*hdmi
= drm_connector_to_sun4i_hdmi(connector
);
217 edid
= drm_get_edid(connector
, hdmi
->ddc_i2c
?: hdmi
->i2c
);
221 hdmi
->hdmi_monitor
= drm_detect_hdmi_monitor(edid
);
222 DRM_DEBUG_DRIVER("Monitor is %s monitor\n",
223 hdmi
->hdmi_monitor
? "an HDMI" : "a DVI");
225 drm_connector_update_edid_property(connector
, edid
);
226 cec_s_phys_addr_from_edid(hdmi
->cec_adap
, edid
);
227 ret
= drm_add_edid_modes(connector
, edid
);
233 static struct i2c_adapter
*sun4i_hdmi_get_ddc(struct device
*dev
)
235 struct device_node
*phandle
, *remote
;
236 struct i2c_adapter
*ddc
;
238 remote
= of_graph_get_remote_node(dev
->of_node
, 1, -1);
240 return ERR_PTR(-EINVAL
);
242 phandle
= of_parse_phandle(remote
, "ddc-i2c-bus", 0);
245 return ERR_PTR(-ENODEV
);
247 ddc
= of_get_i2c_adapter_by_node(phandle
);
248 of_node_put(phandle
);
250 return ERR_PTR(-EPROBE_DEFER
);
255 static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs
= {
256 .get_modes
= sun4i_hdmi_get_modes
,
259 static enum drm_connector_status
260 sun4i_hdmi_connector_detect(struct drm_connector
*connector
, bool force
)
262 struct sun4i_hdmi
*hdmi
= drm_connector_to_sun4i_hdmi(connector
);
265 if (readl_poll_timeout(hdmi
->base
+ SUN4I_HDMI_HPD_REG
, reg
,
266 reg
& SUN4I_HDMI_HPD_HIGH
,
268 cec_phys_addr_invalidate(hdmi
->cec_adap
);
269 return connector_status_disconnected
;
272 return connector_status_connected
;
275 static const struct drm_connector_funcs sun4i_hdmi_connector_funcs
= {
276 .detect
= sun4i_hdmi_connector_detect
,
277 .fill_modes
= drm_helper_probe_single_connector_modes
,
278 .destroy
= drm_connector_cleanup
,
279 .reset
= drm_atomic_helper_connector_reset
,
280 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
281 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
284 #ifdef CONFIG_DRM_SUN4I_HDMI_CEC
285 static bool sun4i_hdmi_cec_pin_read(struct cec_adapter
*adap
)
287 struct sun4i_hdmi
*hdmi
= cec_get_drvdata(adap
);
289 return readl(hdmi
->base
+ SUN4I_HDMI_CEC
) & SUN4I_HDMI_CEC_RX
;
292 static void sun4i_hdmi_cec_pin_low(struct cec_adapter
*adap
)
294 struct sun4i_hdmi
*hdmi
= cec_get_drvdata(adap
);
296 /* Start driving the CEC pin low */
297 writel(SUN4I_HDMI_CEC_ENABLE
, hdmi
->base
+ SUN4I_HDMI_CEC
);
300 static void sun4i_hdmi_cec_pin_high(struct cec_adapter
*adap
)
302 struct sun4i_hdmi
*hdmi
= cec_get_drvdata(adap
);
305 * Stop driving the CEC pin, the pull up will take over
306 * unless another CEC device is driving the pin low.
308 writel(0, hdmi
->base
+ SUN4I_HDMI_CEC
);
311 static const struct cec_pin_ops sun4i_hdmi_cec_pin_ops
= {
312 .read
= sun4i_hdmi_cec_pin_read
,
313 .low
= sun4i_hdmi_cec_pin_low
,
314 .high
= sun4i_hdmi_cec_pin_high
,
318 #define SUN4I_HDMI_PAD_CTRL1_MASK (GENMASK(24, 7) | GENMASK(5, 0))
319 #define SUN4I_HDMI_PLL_CTRL_MASK (GENMASK(31, 8) | GENMASK(3, 0))
321 /* Only difference from sun5i is AMP is 4 instead of 6 */
322 static const struct sun4i_hdmi_variant sun4i_variant
= {
323 .pad_ctrl0_init_val
= SUN4I_HDMI_PAD_CTRL0_TXEN
|
324 SUN4I_HDMI_PAD_CTRL0_CKEN
|
325 SUN4I_HDMI_PAD_CTRL0_PWENG
|
326 SUN4I_HDMI_PAD_CTRL0_PWEND
|
327 SUN4I_HDMI_PAD_CTRL0_PWENC
|
328 SUN4I_HDMI_PAD_CTRL0_LDODEN
|
329 SUN4I_HDMI_PAD_CTRL0_LDOCEN
|
330 SUN4I_HDMI_PAD_CTRL0_BIASEN
,
331 .pad_ctrl1_init_val
= SUN4I_HDMI_PAD_CTRL1_REG_AMP(4) |
332 SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) |
333 SUN4I_HDMI_PAD_CTRL1_REG_DENCK
|
334 SUN4I_HDMI_PAD_CTRL1_REG_DEN
|
335 SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT
|
336 SUN4I_HDMI_PAD_CTRL1_EMP_OPT
|
337 SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT
|
338 SUN4I_HDMI_PAD_CTRL1_AMP_OPT
,
339 .pll_ctrl_init_val
= SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
340 SUN4I_HDMI_PLL_CTRL_CS(7) |
341 SUN4I_HDMI_PLL_CTRL_CP_S(15) |
342 SUN4I_HDMI_PLL_CTRL_S(7) |
343 SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
344 SUN4I_HDMI_PLL_CTRL_SDIV2
|
345 SUN4I_HDMI_PLL_CTRL_LDO2_EN
|
346 SUN4I_HDMI_PLL_CTRL_LDO1_EN
|
347 SUN4I_HDMI_PLL_CTRL_HV_IS_33
|
348 SUN4I_HDMI_PLL_CTRL_BWS
|
349 SUN4I_HDMI_PLL_CTRL_PLL_EN
,
351 .ddc_clk_reg
= REG_FIELD(SUN4I_HDMI_DDC_CLK_REG
, 0, 6),
352 .ddc_clk_pre_divider
= 2,
353 .ddc_clk_m_offset
= 1,
355 .field_ddc_en
= REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG
, 31, 31),
356 .field_ddc_start
= REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG
, 30, 30),
357 .field_ddc_reset
= REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG
, 0, 0),
358 .field_ddc_addr_reg
= REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG
, 0, 31),
359 .field_ddc_slave_addr
= REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG
, 0, 6),
360 .field_ddc_int_status
= REG_FIELD(SUN4I_HDMI_DDC_INT_STATUS_REG
, 0, 8),
361 .field_ddc_fifo_clear
= REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG
, 31, 31),
362 .field_ddc_fifo_rx_thres
= REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG
, 4, 7),
363 .field_ddc_fifo_tx_thres
= REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG
, 0, 3),
364 .field_ddc_byte_count
= REG_FIELD(SUN4I_HDMI_DDC_BYTE_COUNT_REG
, 0, 9),
365 .field_ddc_cmd
= REG_FIELD(SUN4I_HDMI_DDC_CMD_REG
, 0, 2),
366 .field_ddc_sda_en
= REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG
, 9, 9),
367 .field_ddc_sck_en
= REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG
, 8, 8),
369 .ddc_fifo_reg
= SUN4I_HDMI_DDC_FIFO_DATA_REG
,
370 .ddc_fifo_has_dir
= true,
373 static const struct sun4i_hdmi_variant sun5i_variant
= {
374 .pad_ctrl0_init_val
= SUN4I_HDMI_PAD_CTRL0_TXEN
|
375 SUN4I_HDMI_PAD_CTRL0_CKEN
|
376 SUN4I_HDMI_PAD_CTRL0_PWENG
|
377 SUN4I_HDMI_PAD_CTRL0_PWEND
|
378 SUN4I_HDMI_PAD_CTRL0_PWENC
|
379 SUN4I_HDMI_PAD_CTRL0_LDODEN
|
380 SUN4I_HDMI_PAD_CTRL0_LDOCEN
|
381 SUN4I_HDMI_PAD_CTRL0_BIASEN
,
382 .pad_ctrl1_init_val
= SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
383 SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) |
384 SUN4I_HDMI_PAD_CTRL1_REG_DENCK
|
385 SUN4I_HDMI_PAD_CTRL1_REG_DEN
|
386 SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT
|
387 SUN4I_HDMI_PAD_CTRL1_EMP_OPT
|
388 SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT
|
389 SUN4I_HDMI_PAD_CTRL1_AMP_OPT
,
390 .pll_ctrl_init_val
= SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
391 SUN4I_HDMI_PLL_CTRL_CS(7) |
392 SUN4I_HDMI_PLL_CTRL_CP_S(15) |
393 SUN4I_HDMI_PLL_CTRL_S(7) |
394 SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
395 SUN4I_HDMI_PLL_CTRL_SDIV2
|
396 SUN4I_HDMI_PLL_CTRL_LDO2_EN
|
397 SUN4I_HDMI_PLL_CTRL_LDO1_EN
|
398 SUN4I_HDMI_PLL_CTRL_HV_IS_33
|
399 SUN4I_HDMI_PLL_CTRL_BWS
|
400 SUN4I_HDMI_PLL_CTRL_PLL_EN
,
402 .ddc_clk_reg
= REG_FIELD(SUN4I_HDMI_DDC_CLK_REG
, 0, 6),
403 .ddc_clk_pre_divider
= 2,
404 .ddc_clk_m_offset
= 1,
406 .field_ddc_en
= REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG
, 31, 31),
407 .field_ddc_start
= REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG
, 30, 30),
408 .field_ddc_reset
= REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG
, 0, 0),
409 .field_ddc_addr_reg
= REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG
, 0, 31),
410 .field_ddc_slave_addr
= REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG
, 0, 6),
411 .field_ddc_int_status
= REG_FIELD(SUN4I_HDMI_DDC_INT_STATUS_REG
, 0, 8),
412 .field_ddc_fifo_clear
= REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG
, 31, 31),
413 .field_ddc_fifo_rx_thres
= REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG
, 4, 7),
414 .field_ddc_fifo_tx_thres
= REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG
, 0, 3),
415 .field_ddc_byte_count
= REG_FIELD(SUN4I_HDMI_DDC_BYTE_COUNT_REG
, 0, 9),
416 .field_ddc_cmd
= REG_FIELD(SUN4I_HDMI_DDC_CMD_REG
, 0, 2),
417 .field_ddc_sda_en
= REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG
, 9, 9),
418 .field_ddc_sck_en
= REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG
, 8, 8),
420 .ddc_fifo_reg
= SUN4I_HDMI_DDC_FIFO_DATA_REG
,
421 .ddc_fifo_has_dir
= true,
424 static const struct sun4i_hdmi_variant sun6i_variant
= {
425 .has_ddc_parent_clk
= true,
426 .has_reset_control
= true,
427 .pad_ctrl0_init_val
= 0xff |
428 SUN4I_HDMI_PAD_CTRL0_TXEN
|
429 SUN4I_HDMI_PAD_CTRL0_CKEN
|
430 SUN4I_HDMI_PAD_CTRL0_PWENG
|
431 SUN4I_HDMI_PAD_CTRL0_PWEND
|
432 SUN4I_HDMI_PAD_CTRL0_PWENC
|
433 SUN4I_HDMI_PAD_CTRL0_LDODEN
|
434 SUN4I_HDMI_PAD_CTRL0_LDOCEN
,
435 .pad_ctrl1_init_val
= SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
436 SUN4I_HDMI_PAD_CTRL1_REG_EMP(4) |
437 SUN4I_HDMI_PAD_CTRL1_REG_DENCK
|
438 SUN4I_HDMI_PAD_CTRL1_REG_DEN
|
439 SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT
|
440 SUN4I_HDMI_PAD_CTRL1_EMP_OPT
|
441 SUN4I_HDMI_PAD_CTRL1_PWSDT
|
442 SUN4I_HDMI_PAD_CTRL1_PWSCK
|
443 SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT
|
444 SUN4I_HDMI_PAD_CTRL1_AMP_OPT
|
445 SUN4I_HDMI_PAD_CTRL1_UNKNOWN
,
446 .pll_ctrl_init_val
= SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
447 SUN4I_HDMI_PLL_CTRL_CS(3) |
448 SUN4I_HDMI_PLL_CTRL_CP_S(10) |
449 SUN4I_HDMI_PLL_CTRL_S(4) |
450 SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
451 SUN4I_HDMI_PLL_CTRL_SDIV2
|
452 SUN4I_HDMI_PLL_CTRL_LDO2_EN
|
453 SUN4I_HDMI_PLL_CTRL_LDO1_EN
|
454 SUN4I_HDMI_PLL_CTRL_HV_IS_33
|
455 SUN4I_HDMI_PLL_CTRL_PLL_EN
,
457 .ddc_clk_reg
= REG_FIELD(SUN6I_HDMI_DDC_CLK_REG
, 0, 6),
458 .ddc_clk_pre_divider
= 1,
459 .ddc_clk_m_offset
= 2,
461 .tmds_clk_div_offset
= 1,
463 .field_ddc_en
= REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG
, 0, 0),
464 .field_ddc_start
= REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG
, 27, 27),
465 .field_ddc_reset
= REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG
, 31, 31),
466 .field_ddc_addr_reg
= REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG
, 1, 31),
467 .field_ddc_slave_addr
= REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG
, 1, 7),
468 .field_ddc_int_status
= REG_FIELD(SUN6I_HDMI_DDC_INT_STATUS_REG
, 0, 8),
469 .field_ddc_fifo_clear
= REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG
, 18, 18),
470 .field_ddc_fifo_rx_thres
= REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG
, 4, 7),
471 .field_ddc_fifo_tx_thres
= REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG
, 0, 3),
472 .field_ddc_byte_count
= REG_FIELD(SUN6I_HDMI_DDC_CMD_REG
, 16, 25),
473 .field_ddc_cmd
= REG_FIELD(SUN6I_HDMI_DDC_CMD_REG
, 0, 2),
474 .field_ddc_sda_en
= REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG
, 6, 6),
475 .field_ddc_sck_en
= REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG
, 4, 4),
477 .ddc_fifo_reg
= SUN6I_HDMI_DDC_FIFO_DATA_REG
,
478 .ddc_fifo_thres_incl
= true,
481 static const struct regmap_config sun4i_hdmi_regmap_config
= {
485 .max_register
= 0x580,
488 static int sun4i_hdmi_bind(struct device
*dev
, struct device
*master
,
491 struct platform_device
*pdev
= to_platform_device(dev
);
492 struct drm_device
*drm
= data
;
493 struct cec_connector_info conn_info
;
494 struct sun4i_drv
*drv
= drm
->dev_private
;
495 struct sun4i_hdmi
*hdmi
;
496 struct resource
*res
;
500 hdmi
= devm_kzalloc(dev
, sizeof(*hdmi
), GFP_KERNEL
);
503 dev_set_drvdata(dev
, hdmi
);
507 hdmi
->variant
= of_device_get_match_data(dev
);
511 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
512 hdmi
->base
= devm_ioremap_resource(dev
, res
);
513 if (IS_ERR(hdmi
->base
)) {
514 dev_err(dev
, "Couldn't map the HDMI encoder registers\n");
515 return PTR_ERR(hdmi
->base
);
518 if (hdmi
->variant
->has_reset_control
) {
519 hdmi
->reset
= devm_reset_control_get(dev
, NULL
);
520 if (IS_ERR(hdmi
->reset
)) {
521 dev_err(dev
, "Couldn't get the HDMI reset control\n");
522 return PTR_ERR(hdmi
->reset
);
525 ret
= reset_control_deassert(hdmi
->reset
);
527 dev_err(dev
, "Couldn't deassert HDMI reset\n");
532 hdmi
->bus_clk
= devm_clk_get(dev
, "ahb");
533 if (IS_ERR(hdmi
->bus_clk
)) {
534 dev_err(dev
, "Couldn't get the HDMI bus clock\n");
535 ret
= PTR_ERR(hdmi
->bus_clk
);
536 goto err_assert_reset
;
538 clk_prepare_enable(hdmi
->bus_clk
);
540 hdmi
->mod_clk
= devm_clk_get(dev
, "mod");
541 if (IS_ERR(hdmi
->mod_clk
)) {
542 dev_err(dev
, "Couldn't get the HDMI mod clock\n");
543 ret
= PTR_ERR(hdmi
->mod_clk
);
544 goto err_disable_bus_clk
;
546 clk_prepare_enable(hdmi
->mod_clk
);
548 hdmi
->pll0_clk
= devm_clk_get(dev
, "pll-0");
549 if (IS_ERR(hdmi
->pll0_clk
)) {
550 dev_err(dev
, "Couldn't get the HDMI PLL 0 clock\n");
551 ret
= PTR_ERR(hdmi
->pll0_clk
);
552 goto err_disable_mod_clk
;
555 hdmi
->pll1_clk
= devm_clk_get(dev
, "pll-1");
556 if (IS_ERR(hdmi
->pll1_clk
)) {
557 dev_err(dev
, "Couldn't get the HDMI PLL 1 clock\n");
558 ret
= PTR_ERR(hdmi
->pll1_clk
);
559 goto err_disable_mod_clk
;
562 hdmi
->regmap
= devm_regmap_init_mmio(dev
, hdmi
->base
,
563 &sun4i_hdmi_regmap_config
);
564 if (IS_ERR(hdmi
->regmap
)) {
565 dev_err(dev
, "Couldn't create HDMI encoder regmap\n");
566 ret
= PTR_ERR(hdmi
->regmap
);
567 goto err_disable_mod_clk
;
570 ret
= sun4i_tmds_create(hdmi
);
572 dev_err(dev
, "Couldn't create the TMDS clock\n");
573 goto err_disable_mod_clk
;
576 if (hdmi
->variant
->has_ddc_parent_clk
) {
577 hdmi
->ddc_parent_clk
= devm_clk_get(dev
, "ddc");
578 if (IS_ERR(hdmi
->ddc_parent_clk
)) {
579 dev_err(dev
, "Couldn't get the HDMI DDC clock\n");
580 ret
= PTR_ERR(hdmi
->ddc_parent_clk
);
581 goto err_disable_mod_clk
;
584 hdmi
->ddc_parent_clk
= hdmi
->tmds_clk
;
587 writel(SUN4I_HDMI_CTRL_ENABLE
, hdmi
->base
+ SUN4I_HDMI_CTRL_REG
);
589 writel(hdmi
->variant
->pad_ctrl0_init_val
,
590 hdmi
->base
+ SUN4I_HDMI_PAD_CTRL0_REG
);
592 reg
= readl(hdmi
->base
+ SUN4I_HDMI_PLL_CTRL_REG
);
593 reg
&= SUN4I_HDMI_PLL_CTRL_DIV_MASK
;
594 reg
|= hdmi
->variant
->pll_ctrl_init_val
;
595 writel(reg
, hdmi
->base
+ SUN4I_HDMI_PLL_CTRL_REG
);
597 ret
= sun4i_hdmi_i2c_create(dev
, hdmi
);
599 dev_err(dev
, "Couldn't create the HDMI I2C adapter\n");
600 goto err_disable_mod_clk
;
603 hdmi
->ddc_i2c
= sun4i_hdmi_get_ddc(dev
);
604 if (IS_ERR(hdmi
->ddc_i2c
)) {
605 ret
= PTR_ERR(hdmi
->ddc_i2c
);
607 hdmi
->ddc_i2c
= NULL
;
609 goto err_del_i2c_adapter
;
612 drm_encoder_helper_add(&hdmi
->encoder
,
613 &sun4i_hdmi_helper_funcs
);
614 ret
= drm_encoder_init(drm
,
617 DRM_MODE_ENCODER_TMDS
,
620 dev_err(dev
, "Couldn't initialise the HDMI encoder\n");
621 goto err_put_ddc_i2c
;
624 hdmi
->encoder
.possible_crtcs
= drm_of_find_possible_crtcs(drm
,
626 if (!hdmi
->encoder
.possible_crtcs
) {
628 goto err_put_ddc_i2c
;
631 #ifdef CONFIG_DRM_SUN4I_HDMI_CEC
632 hdmi
->cec_adap
= cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops
,
633 hdmi
, "sun4i", CEC_CAP_DEFAULTS
| CEC_CAP_CONNECTOR_INFO
);
634 ret
= PTR_ERR_OR_ZERO(hdmi
->cec_adap
);
636 goto err_cleanup_connector
;
637 writel(readl(hdmi
->base
+ SUN4I_HDMI_CEC
) & ~SUN4I_HDMI_CEC_TX
,
638 hdmi
->base
+ SUN4I_HDMI_CEC
);
641 drm_connector_helper_add(&hdmi
->connector
,
642 &sun4i_hdmi_connector_helper_funcs
);
643 ret
= drm_connector_init_with_ddc(drm
, &hdmi
->connector
,
644 &sun4i_hdmi_connector_funcs
,
645 DRM_MODE_CONNECTOR_HDMIA
,
649 "Couldn't initialise the HDMI connector\n");
650 goto err_cleanup_connector
;
652 cec_fill_conn_info_from_drm(&conn_info
, &hdmi
->connector
);
653 cec_s_conn_info(hdmi
->cec_adap
, &conn_info
);
655 /* There is no HPD interrupt, so we need to poll the controller */
656 hdmi
->connector
.polled
= DRM_CONNECTOR_POLL_CONNECT
|
657 DRM_CONNECTOR_POLL_DISCONNECT
;
659 ret
= cec_register_adapter(hdmi
->cec_adap
, dev
);
661 goto err_cleanup_connector
;
662 drm_connector_attach_encoder(&hdmi
->connector
, &hdmi
->encoder
);
666 err_cleanup_connector
:
667 cec_delete_adapter(hdmi
->cec_adap
);
668 drm_encoder_cleanup(&hdmi
->encoder
);
670 i2c_put_adapter(hdmi
->ddc_i2c
);
672 i2c_del_adapter(hdmi
->i2c
);
674 clk_disable_unprepare(hdmi
->mod_clk
);
676 clk_disable_unprepare(hdmi
->bus_clk
);
678 reset_control_assert(hdmi
->reset
);
682 static void sun4i_hdmi_unbind(struct device
*dev
, struct device
*master
,
685 struct sun4i_hdmi
*hdmi
= dev_get_drvdata(dev
);
687 cec_unregister_adapter(hdmi
->cec_adap
);
688 i2c_del_adapter(hdmi
->i2c
);
689 i2c_put_adapter(hdmi
->ddc_i2c
);
690 clk_disable_unprepare(hdmi
->mod_clk
);
691 clk_disable_unprepare(hdmi
->bus_clk
);
694 static const struct component_ops sun4i_hdmi_ops
= {
695 .bind
= sun4i_hdmi_bind
,
696 .unbind
= sun4i_hdmi_unbind
,
699 static int sun4i_hdmi_probe(struct platform_device
*pdev
)
701 return component_add(&pdev
->dev
, &sun4i_hdmi_ops
);
704 static int sun4i_hdmi_remove(struct platform_device
*pdev
)
706 component_del(&pdev
->dev
, &sun4i_hdmi_ops
);
711 static const struct of_device_id sun4i_hdmi_of_table
[] = {
712 { .compatible
= "allwinner,sun4i-a10-hdmi", .data
= &sun4i_variant
, },
713 { .compatible
= "allwinner,sun5i-a10s-hdmi", .data
= &sun5i_variant
, },
714 { .compatible
= "allwinner,sun6i-a31-hdmi", .data
= &sun6i_variant
, },
717 MODULE_DEVICE_TABLE(of
, sun4i_hdmi_of_table
);
719 static struct platform_driver sun4i_hdmi_driver
= {
720 .probe
= sun4i_hdmi_probe
,
721 .remove
= sun4i_hdmi_remove
,
723 .name
= "sun4i-hdmi",
724 .of_match_table
= sun4i_hdmi_of_table
,
727 module_platform_driver(sun4i_hdmi_driver
);
729 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
730 MODULE_DESCRIPTION("Allwinner A10 HDMI Driver");
731 MODULE_LICENSE("GPL");