Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Maxime Ripard |
| 3 | * |
| 4 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <drm/drmP.h> |
| 13 | #include <drm/drm_atomic_helper.h> |
| 14 | #include <drm/drm_crtc_helper.h> |
| 15 | #include <drm/drm_edid.h> |
| 16 | #include <drm/drm_encoder.h> |
| 17 | #include <drm/drm_of.h> |
| 18 | #include <drm/drm_panel.h> |
| 19 | |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/component.h> |
| 22 | #include <linux/iopoll.h> |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 23 | #include <linux/of_device.h> |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/pm_runtime.h> |
Chen-Yu Tsai | 4b1c924 | 2017-10-10 11:20:01 +0800 | [diff] [blame] | 26 | #include <linux/regmap.h> |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 27 | #include <linux/reset.h> |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 28 | |
| 29 | #include "sun4i_backend.h" |
| 30 | #include "sun4i_crtc.h" |
| 31 | #include "sun4i_drv.h" |
| 32 | #include "sun4i_hdmi.h" |
| 33 | #include "sun4i_tcon.h" |
| 34 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 35 | static inline struct sun4i_hdmi * |
| 36 | drm_encoder_to_sun4i_hdmi(struct drm_encoder *encoder) |
| 37 | { |
| 38 | return container_of(encoder, struct sun4i_hdmi, |
| 39 | encoder); |
| 40 | } |
| 41 | |
| 42 | static inline struct sun4i_hdmi * |
| 43 | drm_connector_to_sun4i_hdmi(struct drm_connector *connector) |
| 44 | { |
| 45 | return container_of(connector, struct sun4i_hdmi, |
| 46 | connector); |
| 47 | } |
| 48 | |
| 49 | static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi, |
| 50 | struct drm_display_mode *mode) |
| 51 | { |
| 52 | struct hdmi_avi_infoframe frame; |
| 53 | u8 buffer[17]; |
| 54 | int i, ret; |
| 55 | |
Shashank Sharma | 0c1f528 | 2017-07-13 21:03:07 +0530 | [diff] [blame] | 56 | ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 57 | if (ret < 0) { |
| 58 | DRM_ERROR("Failed to get infoframes from mode\n"); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | ret = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer)); |
| 63 | if (ret < 0) { |
| 64 | DRM_ERROR("Failed to pack infoframes\n"); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | for (i = 0; i < sizeof(buffer); i++) |
| 69 | writeb(buffer[i], hdmi->base + SUN4I_HDMI_AVI_INFOFRAME_REG(i)); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder, |
| 75 | struct drm_crtc_state *crtc_state, |
| 76 | struct drm_connector_state *conn_state) |
| 77 | { |
| 78 | struct drm_display_mode *mode = &crtc_state->mode; |
| 79 | |
| 80 | if (mode->flags & DRM_MODE_FLAG_DBLCLK) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void sun4i_hdmi_disable(struct drm_encoder *encoder) |
| 87 | { |
| 88 | struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 89 | u32 val; |
| 90 | |
| 91 | DRM_DEBUG_DRIVER("Disabling the HDMI Output\n"); |
| 92 | |
| 93 | val = readl(hdmi->base + SUN4I_HDMI_VID_CTRL_REG); |
| 94 | val &= ~SUN4I_HDMI_VID_CTRL_ENABLE; |
| 95 | writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void sun4i_hdmi_enable(struct drm_encoder *encoder) |
| 99 | { |
| 100 | struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; |
| 101 | struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 102 | u32 val = 0; |
| 103 | |
| 104 | DRM_DEBUG_DRIVER("Enabling the HDMI Output\n"); |
| 105 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 106 | sun4i_hdmi_setup_avi_infoframes(hdmi, mode); |
| 107 | val |= SUN4I_HDMI_PKT_CTRL_TYPE(0, SUN4I_HDMI_PKT_AVI); |
| 108 | val |= SUN4I_HDMI_PKT_CTRL_TYPE(1, SUN4I_HDMI_PKT_END); |
| 109 | writel(val, hdmi->base + SUN4I_HDMI_PKT_CTRL_REG(0)); |
| 110 | |
| 111 | val = SUN4I_HDMI_VID_CTRL_ENABLE; |
| 112 | if (hdmi->hdmi_monitor) |
| 113 | val |= SUN4I_HDMI_VID_CTRL_HDMI_MODE; |
| 114 | |
| 115 | writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG); |
| 116 | } |
| 117 | |
| 118 | static void sun4i_hdmi_mode_set(struct drm_encoder *encoder, |
| 119 | struct drm_display_mode *mode, |
| 120 | struct drm_display_mode *adjusted_mode) |
| 121 | { |
| 122 | struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder); |
| 123 | struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc); |
| 124 | struct sun4i_tcon *tcon = crtc->tcon; |
| 125 | unsigned int x, y; |
| 126 | u32 val; |
| 127 | |
| 128 | sun4i_tcon1_mode_set(tcon, mode); |
| 129 | sun4i_tcon_set_mux(tcon, 1, encoder); |
| 130 | |
| 131 | clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); |
| 132 | clk_set_rate(hdmi->mod_clk, mode->crtc_clock * 1000); |
| 133 | clk_set_rate(hdmi->tmds_clk, mode->crtc_clock * 1000); |
| 134 | |
| 135 | /* Set input sync enable */ |
| 136 | writel(SUN4I_HDMI_UNKNOWN_INPUT_SYNC, |
| 137 | hdmi->base + SUN4I_HDMI_UNKNOWN_REG); |
| 138 | |
Chen-Yu Tsai | bfddd14 | 2017-10-14 12:02:52 +0800 | [diff] [blame] | 139 | /* |
| 140 | * Setup output pad (?) controls |
| 141 | * |
| 142 | * This is done here instead of at probe/bind time because |
| 143 | * the controller seems to toggle some of the bits on its own. |
| 144 | * |
| 145 | * We can't just initialize the register there, we need to |
| 146 | * protect the clock bits that have already been read out and |
| 147 | * cached by the clock framework. |
| 148 | */ |
| 149 | val = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG); |
| 150 | val &= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK; |
| 151 | val |= hdmi->variant->pad_ctrl1_init_val; |
| 152 | writel(val, hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG); |
| 153 | val = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG); |
| 154 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 155 | /* Setup timing registers */ |
| 156 | writel(SUN4I_HDMI_VID_TIMING_X(mode->hdisplay) | |
| 157 | SUN4I_HDMI_VID_TIMING_Y(mode->vdisplay), |
| 158 | hdmi->base + SUN4I_HDMI_VID_TIMING_ACT_REG); |
| 159 | |
| 160 | x = mode->htotal - mode->hsync_start; |
| 161 | y = mode->vtotal - mode->vsync_start; |
| 162 | writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y), |
| 163 | hdmi->base + SUN4I_HDMI_VID_TIMING_BP_REG); |
| 164 | |
| 165 | x = mode->hsync_start - mode->hdisplay; |
| 166 | y = mode->vsync_start - mode->vdisplay; |
| 167 | writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y), |
| 168 | hdmi->base + SUN4I_HDMI_VID_TIMING_FP_REG); |
| 169 | |
| 170 | x = mode->hsync_end - mode->hsync_start; |
| 171 | y = mode->vsync_end - mode->vsync_start; |
| 172 | writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y), |
| 173 | hdmi->base + SUN4I_HDMI_VID_TIMING_SPW_REG); |
| 174 | |
| 175 | val = SUN4I_HDMI_VID_TIMING_POL_TX_CLK; |
| 176 | if (mode->flags & DRM_MODE_FLAG_PHSYNC) |
| 177 | val |= SUN4I_HDMI_VID_TIMING_POL_HSYNC; |
| 178 | |
| 179 | if (mode->flags & DRM_MODE_FLAG_PVSYNC) |
| 180 | val |= SUN4I_HDMI_VID_TIMING_POL_VSYNC; |
| 181 | |
| 182 | writel(val, hdmi->base + SUN4I_HDMI_VID_TIMING_POL_REG); |
| 183 | } |
| 184 | |
| 185 | static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = { |
| 186 | .atomic_check = sun4i_hdmi_atomic_check, |
| 187 | .disable = sun4i_hdmi_disable, |
| 188 | .enable = sun4i_hdmi_enable, |
| 189 | .mode_set = sun4i_hdmi_mode_set, |
| 190 | }; |
| 191 | |
| 192 | static const struct drm_encoder_funcs sun4i_hdmi_funcs = { |
| 193 | .destroy = drm_encoder_cleanup, |
| 194 | }; |
| 195 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 196 | static int sun4i_hdmi_get_modes(struct drm_connector *connector) |
| 197 | { |
| 198 | struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 199 | struct edid *edid; |
| 200 | int ret; |
| 201 | |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 202 | edid = drm_get_edid(connector, hdmi->i2c); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 203 | if (!edid) |
| 204 | return 0; |
| 205 | |
| 206 | hdmi->hdmi_monitor = drm_detect_hdmi_monitor(edid); |
| 207 | DRM_DEBUG_DRIVER("Monitor is %s monitor\n", |
| 208 | hdmi->hdmi_monitor ? "an HDMI" : "a DVI"); |
| 209 | |
| 210 | drm_mode_connector_update_edid_property(connector, edid); |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 211 | cec_s_phys_addr_from_edid(hdmi->cec_adap, edid); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 212 | ret = drm_add_edid_modes(connector, edid); |
| 213 | kfree(edid); |
| 214 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = { |
| 219 | .get_modes = sun4i_hdmi_get_modes, |
| 220 | }; |
| 221 | |
| 222 | static enum drm_connector_status |
| 223 | sun4i_hdmi_connector_detect(struct drm_connector *connector, bool force) |
| 224 | { |
| 225 | struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector); |
| 226 | unsigned long reg; |
| 227 | |
| 228 | if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_HPD_REG, reg, |
| 229 | reg & SUN4I_HDMI_HPD_HIGH, |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 230 | 0, 500000)) { |
| 231 | cec_phys_addr_invalidate(hdmi->cec_adap); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 232 | return connector_status_disconnected; |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 233 | } |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 234 | |
| 235 | return connector_status_connected; |
| 236 | } |
| 237 | |
| 238 | static const struct drm_connector_funcs sun4i_hdmi_connector_funcs = { |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 239 | .detect = sun4i_hdmi_connector_detect, |
| 240 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 241 | .destroy = drm_connector_cleanup, |
| 242 | .reset = drm_atomic_helper_connector_reset, |
| 243 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 244 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 245 | }; |
| 246 | |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 247 | #ifdef CONFIG_DRM_SUN4I_HDMI_CEC |
| 248 | static bool sun4i_hdmi_cec_pin_read(struct cec_adapter *adap) |
| 249 | { |
| 250 | struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); |
| 251 | |
| 252 | return readl(hdmi->base + SUN4I_HDMI_CEC) & SUN4I_HDMI_CEC_RX; |
| 253 | } |
| 254 | |
| 255 | static void sun4i_hdmi_cec_pin_low(struct cec_adapter *adap) |
| 256 | { |
| 257 | struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); |
| 258 | |
| 259 | /* Start driving the CEC pin low */ |
| 260 | writel(SUN4I_HDMI_CEC_ENABLE, hdmi->base + SUN4I_HDMI_CEC); |
| 261 | } |
| 262 | |
| 263 | static void sun4i_hdmi_cec_pin_high(struct cec_adapter *adap) |
| 264 | { |
| 265 | struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); |
| 266 | |
| 267 | /* |
| 268 | * Stop driving the CEC pin, the pull up will take over |
| 269 | * unless another CEC device is driving the pin low. |
| 270 | */ |
| 271 | writel(0, hdmi->base + SUN4I_HDMI_CEC); |
| 272 | } |
| 273 | |
| 274 | static const struct cec_pin_ops sun4i_hdmi_cec_pin_ops = { |
| 275 | .read = sun4i_hdmi_cec_pin_read, |
| 276 | .low = sun4i_hdmi_cec_pin_low, |
| 277 | .high = sun4i_hdmi_cec_pin_high, |
| 278 | }; |
| 279 | #endif |
| 280 | |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 281 | #define SUN4I_HDMI_PAD_CTRL1_MASK (GENMASK(24, 7) | GENMASK(5, 0)) |
| 282 | #define SUN4I_HDMI_PLL_CTRL_MASK (GENMASK(31, 8) | GENMASK(3, 0)) |
| 283 | |
| 284 | static const struct sun4i_hdmi_variant sun5i_variant = { |
| 285 | .pad_ctrl0_init_val = SUN4I_HDMI_PAD_CTRL0_TXEN | |
| 286 | SUN4I_HDMI_PAD_CTRL0_CKEN | |
| 287 | SUN4I_HDMI_PAD_CTRL0_PWENG | |
| 288 | SUN4I_HDMI_PAD_CTRL0_PWEND | |
| 289 | SUN4I_HDMI_PAD_CTRL0_PWENC | |
| 290 | SUN4I_HDMI_PAD_CTRL0_LDODEN | |
| 291 | SUN4I_HDMI_PAD_CTRL0_LDOCEN | |
| 292 | SUN4I_HDMI_PAD_CTRL0_BIASEN, |
| 293 | .pad_ctrl1_init_val = SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) | |
| 294 | SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) | |
| 295 | SUN4I_HDMI_PAD_CTRL1_REG_DENCK | |
| 296 | SUN4I_HDMI_PAD_CTRL1_REG_DEN | |
| 297 | SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT | |
| 298 | SUN4I_HDMI_PAD_CTRL1_EMP_OPT | |
| 299 | SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT | |
| 300 | SUN4I_HDMI_PAD_CTRL1_AMP_OPT, |
| 301 | .pll_ctrl_init_val = SUN4I_HDMI_PLL_CTRL_VCO_S(8) | |
| 302 | SUN4I_HDMI_PLL_CTRL_CS(7) | |
| 303 | SUN4I_HDMI_PLL_CTRL_CP_S(15) | |
| 304 | SUN4I_HDMI_PLL_CTRL_S(7) | |
| 305 | SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) | |
| 306 | SUN4I_HDMI_PLL_CTRL_SDIV2 | |
| 307 | SUN4I_HDMI_PLL_CTRL_LDO2_EN | |
| 308 | SUN4I_HDMI_PLL_CTRL_LDO1_EN | |
| 309 | SUN4I_HDMI_PLL_CTRL_HV_IS_33 | |
| 310 | SUN4I_HDMI_PLL_CTRL_BWS | |
| 311 | SUN4I_HDMI_PLL_CTRL_PLL_EN, |
| 312 | |
| 313 | .ddc_clk_reg = REG_FIELD(SUN4I_HDMI_DDC_CLK_REG, 0, 6), |
| 314 | .ddc_clk_pre_divider = 2, |
| 315 | .ddc_clk_m_offset = 1, |
| 316 | |
| 317 | .field_ddc_en = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 31, 31), |
| 318 | .field_ddc_start = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 30, 30), |
| 319 | .field_ddc_reset = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 0, 0), |
| 320 | .field_ddc_addr_reg = REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG, 0, 31), |
| 321 | .field_ddc_slave_addr = REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG, 0, 6), |
| 322 | .field_ddc_int_status = REG_FIELD(SUN4I_HDMI_DDC_INT_STATUS_REG, 0, 8), |
| 323 | .field_ddc_fifo_clear = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 31, 31), |
| 324 | .field_ddc_fifo_rx_thres = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 4, 7), |
| 325 | .field_ddc_fifo_tx_thres = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 0, 3), |
| 326 | .field_ddc_byte_count = REG_FIELD(SUN4I_HDMI_DDC_BYTE_COUNT_REG, 0, 9), |
| 327 | .field_ddc_cmd = REG_FIELD(SUN4I_HDMI_DDC_CMD_REG, 0, 2), |
| 328 | .field_ddc_sda_en = REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG, 9, 9), |
| 329 | .field_ddc_sck_en = REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG, 8, 8), |
| 330 | |
| 331 | .ddc_fifo_reg = SUN4I_HDMI_DDC_FIFO_DATA_REG, |
| 332 | .ddc_fifo_has_dir = true, |
| 333 | }; |
| 334 | |
Chen-Yu Tsai | da184dee | 2017-10-10 11:20:06 +0800 | [diff] [blame] | 335 | static const struct sun4i_hdmi_variant sun6i_variant = { |
| 336 | .has_ddc_parent_clk = true, |
| 337 | .has_reset_control = true, |
| 338 | .pad_ctrl0_init_val = 0xff | |
| 339 | SUN4I_HDMI_PAD_CTRL0_TXEN | |
| 340 | SUN4I_HDMI_PAD_CTRL0_CKEN | |
| 341 | SUN4I_HDMI_PAD_CTRL0_PWENG | |
| 342 | SUN4I_HDMI_PAD_CTRL0_PWEND | |
| 343 | SUN4I_HDMI_PAD_CTRL0_PWENC | |
| 344 | SUN4I_HDMI_PAD_CTRL0_LDODEN | |
| 345 | SUN4I_HDMI_PAD_CTRL0_LDOCEN, |
| 346 | .pad_ctrl1_init_val = SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) | |
| 347 | SUN4I_HDMI_PAD_CTRL1_REG_EMP(4) | |
| 348 | SUN4I_HDMI_PAD_CTRL1_REG_DENCK | |
| 349 | SUN4I_HDMI_PAD_CTRL1_REG_DEN | |
| 350 | SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT | |
| 351 | SUN4I_HDMI_PAD_CTRL1_EMP_OPT | |
| 352 | SUN4I_HDMI_PAD_CTRL1_PWSDT | |
| 353 | SUN4I_HDMI_PAD_CTRL1_PWSCK | |
| 354 | SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT | |
| 355 | SUN4I_HDMI_PAD_CTRL1_AMP_OPT | |
| 356 | SUN4I_HDMI_PAD_CTRL1_UNKNOWN, |
| 357 | .pll_ctrl_init_val = SUN4I_HDMI_PLL_CTRL_VCO_S(8) | |
| 358 | SUN4I_HDMI_PLL_CTRL_CS(3) | |
| 359 | SUN4I_HDMI_PLL_CTRL_CP_S(10) | |
| 360 | SUN4I_HDMI_PLL_CTRL_S(4) | |
| 361 | SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) | |
| 362 | SUN4I_HDMI_PLL_CTRL_SDIV2 | |
| 363 | SUN4I_HDMI_PLL_CTRL_LDO2_EN | |
| 364 | SUN4I_HDMI_PLL_CTRL_LDO1_EN | |
| 365 | SUN4I_HDMI_PLL_CTRL_HV_IS_33 | |
| 366 | SUN4I_HDMI_PLL_CTRL_PLL_EN, |
| 367 | |
| 368 | .ddc_clk_reg = REG_FIELD(SUN6I_HDMI_DDC_CLK_REG, 0, 6), |
| 369 | .ddc_clk_pre_divider = 1, |
| 370 | .ddc_clk_m_offset = 2, |
| 371 | |
| 372 | .tmds_clk_div_offset = 1, |
| 373 | |
| 374 | .field_ddc_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 0, 0), |
| 375 | .field_ddc_start = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 27, 27), |
| 376 | .field_ddc_reset = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 31, 31), |
| 377 | .field_ddc_addr_reg = REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG, 1, 31), |
| 378 | .field_ddc_slave_addr = REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG, 1, 7), |
| 379 | .field_ddc_int_status = REG_FIELD(SUN6I_HDMI_DDC_INT_STATUS_REG, 0, 8), |
| 380 | .field_ddc_fifo_clear = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 18, 18), |
| 381 | .field_ddc_fifo_rx_thres = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 4, 7), |
| 382 | .field_ddc_fifo_tx_thres = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 0, 3), |
| 383 | .field_ddc_byte_count = REG_FIELD(SUN6I_HDMI_DDC_CMD_REG, 16, 25), |
| 384 | .field_ddc_cmd = REG_FIELD(SUN6I_HDMI_DDC_CMD_REG, 0, 2), |
| 385 | .field_ddc_sda_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 6, 6), |
| 386 | .field_ddc_sck_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 4, 4), |
| 387 | |
| 388 | .ddc_fifo_reg = SUN6I_HDMI_DDC_FIFO_DATA_REG, |
| 389 | .ddc_fifo_thres_incl = true, |
| 390 | }; |
| 391 | |
Chen-Yu Tsai | 4b1c924 | 2017-10-10 11:20:01 +0800 | [diff] [blame] | 392 | static const struct regmap_config sun4i_hdmi_regmap_config = { |
| 393 | .reg_bits = 32, |
| 394 | .val_bits = 32, |
| 395 | .reg_stride = 4, |
| 396 | .max_register = 0x580, |
| 397 | }; |
| 398 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 399 | static int sun4i_hdmi_bind(struct device *dev, struct device *master, |
| 400 | void *data) |
| 401 | { |
| 402 | struct platform_device *pdev = to_platform_device(dev); |
| 403 | struct drm_device *drm = data; |
| 404 | struct sun4i_drv *drv = drm->dev_private; |
| 405 | struct sun4i_hdmi *hdmi; |
| 406 | struct resource *res; |
| 407 | u32 reg; |
| 408 | int ret; |
| 409 | |
| 410 | hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL); |
| 411 | if (!hdmi) |
| 412 | return -ENOMEM; |
| 413 | dev_set_drvdata(dev, hdmi); |
| 414 | hdmi->dev = dev; |
| 415 | hdmi->drv = drv; |
| 416 | |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 417 | hdmi->variant = of_device_get_match_data(dev); |
| 418 | if (!hdmi->variant) |
| 419 | return -EINVAL; |
| 420 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 421 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 422 | hdmi->base = devm_ioremap_resource(dev, res); |
| 423 | if (IS_ERR(hdmi->base)) { |
| 424 | dev_err(dev, "Couldn't map the HDMI encoder registers\n"); |
| 425 | return PTR_ERR(hdmi->base); |
| 426 | } |
| 427 | |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 428 | if (hdmi->variant->has_reset_control) { |
| 429 | hdmi->reset = devm_reset_control_get(dev, NULL); |
| 430 | if (IS_ERR(hdmi->reset)) { |
| 431 | dev_err(dev, "Couldn't get the HDMI reset control\n"); |
| 432 | return PTR_ERR(hdmi->reset); |
| 433 | } |
| 434 | |
| 435 | ret = reset_control_deassert(hdmi->reset); |
| 436 | if (ret) { |
| 437 | dev_err(dev, "Couldn't deassert HDMI reset\n"); |
| 438 | return ret; |
| 439 | } |
| 440 | } |
| 441 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 442 | hdmi->bus_clk = devm_clk_get(dev, "ahb"); |
| 443 | if (IS_ERR(hdmi->bus_clk)) { |
| 444 | dev_err(dev, "Couldn't get the HDMI bus clock\n"); |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 445 | ret = PTR_ERR(hdmi->bus_clk); |
| 446 | goto err_assert_reset; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 447 | } |
| 448 | clk_prepare_enable(hdmi->bus_clk); |
| 449 | |
| 450 | hdmi->mod_clk = devm_clk_get(dev, "mod"); |
| 451 | if (IS_ERR(hdmi->mod_clk)) { |
| 452 | dev_err(dev, "Couldn't get the HDMI mod clock\n"); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 453 | ret = PTR_ERR(hdmi->mod_clk); |
| 454 | goto err_disable_bus_clk; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 455 | } |
| 456 | clk_prepare_enable(hdmi->mod_clk); |
| 457 | |
| 458 | hdmi->pll0_clk = devm_clk_get(dev, "pll-0"); |
| 459 | if (IS_ERR(hdmi->pll0_clk)) { |
| 460 | dev_err(dev, "Couldn't get the HDMI PLL 0 clock\n"); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 461 | ret = PTR_ERR(hdmi->pll0_clk); |
| 462 | goto err_disable_mod_clk; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | hdmi->pll1_clk = devm_clk_get(dev, "pll-1"); |
| 466 | if (IS_ERR(hdmi->pll1_clk)) { |
| 467 | dev_err(dev, "Couldn't get the HDMI PLL 1 clock\n"); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 468 | ret = PTR_ERR(hdmi->pll1_clk); |
| 469 | goto err_disable_mod_clk; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Chen-Yu Tsai | 4b1c924 | 2017-10-10 11:20:01 +0800 | [diff] [blame] | 472 | hdmi->regmap = devm_regmap_init_mmio(dev, hdmi->base, |
| 473 | &sun4i_hdmi_regmap_config); |
| 474 | if (IS_ERR(hdmi->regmap)) { |
| 475 | dev_err(dev, "Couldn't create HDMI encoder regmap\n"); |
| 476 | return PTR_ERR(hdmi->regmap); |
| 477 | } |
| 478 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 479 | ret = sun4i_tmds_create(hdmi); |
| 480 | if (ret) { |
| 481 | dev_err(dev, "Couldn't create the TMDS clock\n"); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 482 | goto err_disable_mod_clk; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 485 | if (hdmi->variant->has_ddc_parent_clk) { |
| 486 | hdmi->ddc_parent_clk = devm_clk_get(dev, "ddc"); |
| 487 | if (IS_ERR(hdmi->ddc_parent_clk)) { |
| 488 | dev_err(dev, "Couldn't get the HDMI DDC clock\n"); |
| 489 | return PTR_ERR(hdmi->ddc_parent_clk); |
| 490 | } |
| 491 | } else { |
| 492 | hdmi->ddc_parent_clk = hdmi->tmds_clk; |
| 493 | } |
| 494 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 495 | writel(SUN4I_HDMI_CTRL_ENABLE, hdmi->base + SUN4I_HDMI_CTRL_REG); |
| 496 | |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 497 | writel(hdmi->variant->pad_ctrl0_init_val, |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 498 | hdmi->base + SUN4I_HDMI_PAD_CTRL0_REG); |
| 499 | |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 500 | reg = readl(hdmi->base + SUN4I_HDMI_PLL_CTRL_REG); |
| 501 | reg &= SUN4I_HDMI_PLL_CTRL_DIV_MASK; |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 502 | reg |= hdmi->variant->pll_ctrl_init_val; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 503 | writel(reg, hdmi->base + SUN4I_HDMI_PLL_CTRL_REG); |
| 504 | |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 505 | ret = sun4i_hdmi_i2c_create(dev, hdmi); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 506 | if (ret) { |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 507 | dev_err(dev, "Couldn't create the HDMI I2C adapter\n"); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 508 | goto err_disable_mod_clk; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | drm_encoder_helper_add(&hdmi->encoder, |
| 512 | &sun4i_hdmi_helper_funcs); |
| 513 | ret = drm_encoder_init(drm, |
| 514 | &hdmi->encoder, |
| 515 | &sun4i_hdmi_funcs, |
| 516 | DRM_MODE_ENCODER_TMDS, |
| 517 | NULL); |
| 518 | if (ret) { |
| 519 | dev_err(dev, "Couldn't initialise the HDMI encoder\n"); |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 520 | goto err_del_i2c_adapter; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm, |
| 524 | dev->of_node); |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 525 | if (!hdmi->encoder.possible_crtcs) { |
| 526 | ret = -EPROBE_DEFER; |
| 527 | goto err_del_i2c_adapter; |
| 528 | } |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 529 | |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 530 | #ifdef CONFIG_DRM_SUN4I_HDMI_CEC |
| 531 | hdmi->cec_adap = cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops, |
| 532 | hdmi, "sun4i", CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS | |
| 533 | CEC_CAP_PASSTHROUGH | CEC_CAP_RC); |
| 534 | ret = PTR_ERR_OR_ZERO(hdmi->cec_adap); |
| 535 | if (ret < 0) |
| 536 | goto err_cleanup_connector; |
| 537 | writel(readl(hdmi->base + SUN4I_HDMI_CEC) & ~SUN4I_HDMI_CEC_TX, |
| 538 | hdmi->base + SUN4I_HDMI_CEC); |
| 539 | #endif |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 540 | |
| 541 | drm_connector_helper_add(&hdmi->connector, |
| 542 | &sun4i_hdmi_connector_helper_funcs); |
| 543 | ret = drm_connector_init(drm, &hdmi->connector, |
| 544 | &sun4i_hdmi_connector_funcs, |
| 545 | DRM_MODE_CONNECTOR_HDMIA); |
| 546 | if (ret) { |
| 547 | dev_err(dev, |
| 548 | "Couldn't initialise the HDMI connector\n"); |
| 549 | goto err_cleanup_connector; |
| 550 | } |
| 551 | |
| 552 | /* There is no HPD interrupt, so we need to poll the controller */ |
| 553 | hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | |
| 554 | DRM_CONNECTOR_POLL_DISCONNECT; |
| 555 | |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 556 | ret = cec_register_adapter(hdmi->cec_adap, dev); |
| 557 | if (ret < 0) |
| 558 | goto err_cleanup_connector; |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 559 | drm_mode_connector_attach_encoder(&hdmi->connector, &hdmi->encoder); |
| 560 | |
| 561 | return 0; |
| 562 | |
| 563 | err_cleanup_connector: |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 564 | cec_delete_adapter(hdmi->cec_adap); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 565 | drm_encoder_cleanup(&hdmi->encoder); |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 566 | err_del_i2c_adapter: |
| 567 | i2c_del_adapter(hdmi->i2c); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 568 | err_disable_mod_clk: |
| 569 | clk_disable_unprepare(hdmi->mod_clk); |
| 570 | err_disable_bus_clk: |
| 571 | clk_disable_unprepare(hdmi->bus_clk); |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 572 | err_assert_reset: |
| 573 | reset_control_assert(hdmi->reset); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | static void sun4i_hdmi_unbind(struct device *dev, struct device *master, |
| 578 | void *data) |
| 579 | { |
| 580 | struct sun4i_hdmi *hdmi = dev_get_drvdata(dev); |
| 581 | |
Hans Verkuil | 998140d | 2017-07-11 08:30:44 +0200 | [diff] [blame] | 582 | cec_unregister_adapter(hdmi->cec_adap); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 583 | drm_connector_cleanup(&hdmi->connector); |
| 584 | drm_encoder_cleanup(&hdmi->encoder); |
Jonathan Liu | f0a3dd3 | 2017-07-02 17:27:10 +1000 | [diff] [blame] | 585 | i2c_del_adapter(hdmi->i2c); |
Chen-Yu Tsai | 544c504 | 2017-10-10 11:20:00 +0800 | [diff] [blame] | 586 | clk_disable_unprepare(hdmi->mod_clk); |
| 587 | clk_disable_unprepare(hdmi->bus_clk); |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | static const struct component_ops sun4i_hdmi_ops = { |
| 591 | .bind = sun4i_hdmi_bind, |
| 592 | .unbind = sun4i_hdmi_unbind, |
| 593 | }; |
| 594 | |
| 595 | static int sun4i_hdmi_probe(struct platform_device *pdev) |
| 596 | { |
| 597 | return component_add(&pdev->dev, &sun4i_hdmi_ops); |
| 598 | } |
| 599 | |
| 600 | static int sun4i_hdmi_remove(struct platform_device *pdev) |
| 601 | { |
| 602 | component_del(&pdev->dev, &sun4i_hdmi_ops); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static const struct of_device_id sun4i_hdmi_of_table[] = { |
Chen-Yu Tsai | 939d749 | 2017-10-10 11:20:04 +0800 | [diff] [blame] | 608 | { .compatible = "allwinner,sun5i-a10s-hdmi", .data = &sun5i_variant, }, |
Chen-Yu Tsai | da184dee | 2017-10-10 11:20:06 +0800 | [diff] [blame] | 609 | { .compatible = "allwinner,sun6i-a31-hdmi", .data = &sun6i_variant, }, |
Maxime Ripard | 9c56810 | 2017-05-27 18:09:35 +0200 | [diff] [blame] | 610 | { } |
| 611 | }; |
| 612 | MODULE_DEVICE_TABLE(of, sun4i_hdmi_of_table); |
| 613 | |
| 614 | static struct platform_driver sun4i_hdmi_driver = { |
| 615 | .probe = sun4i_hdmi_probe, |
| 616 | .remove = sun4i_hdmi_remove, |
| 617 | .driver = { |
| 618 | .name = "sun4i-hdmi", |
| 619 | .of_match_table = sun4i_hdmi_of_table, |
| 620 | }, |
| 621 | }; |
| 622 | module_platform_driver(sun4i_hdmi_driver); |
| 623 | |
| 624 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 625 | MODULE_DESCRIPTION("Allwinner A10 HDMI Driver"); |
| 626 | MODULE_LICENSE("GPL"); |