Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Free Electrons |
| 3 | * Copyright (C) 2015 NextThing Co |
| 4 | * |
| 5 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <drm/drmP.h> |
| 14 | #include <drm/drm_atomic_helper.h> |
| 15 | #include <drm/drm_crtc.h> |
| 16 | #include <drm/drm_crtc_helper.h> |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 17 | #include <drm/drm_encoder.h> |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 18 | #include <drm/drm_modes.h> |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 19 | #include <drm/drm_of.h> |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 20 | |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 21 | #include <uapi/drm/drm_mode.h> |
| 22 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 23 | #include <linux/component.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/of_address.h> |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 27 | #include <linux/of_irq.h> |
| 28 | #include <linux/regmap.h> |
| 29 | #include <linux/reset.h> |
| 30 | |
| 31 | #include "sun4i_crtc.h" |
| 32 | #include "sun4i_dotclock.h" |
| 33 | #include "sun4i_drv.h" |
Maxime Ripard | 29e57fa | 2015-10-29 09:37:32 +0100 | [diff] [blame] | 34 | #include "sun4i_rgb.h" |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 35 | #include "sun4i_tcon.h" |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 36 | #include "sunxi_engine.h" |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 37 | |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 38 | static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel, |
| 39 | bool enabled) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 40 | { |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 41 | struct clk *clk; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 42 | |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 43 | switch (channel) { |
| 44 | case 0: |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 45 | regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, |
| 46 | SUN4I_TCON0_CTL_TCON_ENABLE, |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 47 | enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0); |
| 48 | clk = tcon->dclk; |
| 49 | break; |
| 50 | case 1: |
| 51 | WARN_ON(!tcon->quirks->has_channel_1); |
| 52 | regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, |
| 53 | SUN4I_TCON1_CTL_TCON_ENABLE, |
| 54 | enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0); |
| 55 | clk = tcon->sclk1; |
| 56 | break; |
| 57 | default: |
| 58 | DRM_WARN("Unknown channel... doing nothing\n"); |
Maxime Ripard | 8e92404 | 2016-01-07 12:32:07 +0100 | [diff] [blame] | 59 | return; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 60 | } |
Maxime Ripard | 8e92404 | 2016-01-07 12:32:07 +0100 | [diff] [blame] | 61 | |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 62 | if (enabled) |
| 63 | clk_prepare_enable(clk); |
| 64 | else |
| 65 | clk_disable_unprepare(clk); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 66 | } |
Maxime Ripard | 45e88f9 | 2017-10-17 11:06:12 +0200 | [diff] [blame] | 67 | |
| 68 | void sun4i_tcon_set_status(struct sun4i_tcon *tcon, |
| 69 | const struct drm_encoder *encoder, |
| 70 | bool enabled) |
| 71 | { |
| 72 | int channel; |
| 73 | |
| 74 | switch (encoder->encoder_type) { |
| 75 | case DRM_MODE_ENCODER_NONE: |
| 76 | channel = 0; |
| 77 | break; |
| 78 | case DRM_MODE_ENCODER_TMDS: |
| 79 | case DRM_MODE_ENCODER_TVDAC: |
| 80 | channel = 1; |
| 81 | break; |
| 82 | default: |
| 83 | DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, |
| 88 | SUN4I_TCON_GCTL_TCON_ENABLE, |
| 89 | enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0); |
| 90 | |
| 91 | sun4i_tcon_channel_set_status(tcon, channel, enabled); |
| 92 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 93 | |
| 94 | void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) |
| 95 | { |
| 96 | u32 mask, val = 0; |
| 97 | |
| 98 | DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); |
| 99 | |
| 100 | mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | |
| 101 | SUN4I_TCON_GINT0_VBLANK_ENABLE(1); |
| 102 | |
| 103 | if (enable) |
| 104 | val = mask; |
| 105 | |
| 106 | regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); |
| 107 | } |
| 108 | EXPORT_SYMBOL(sun4i_tcon_enable_vblank); |
| 109 | |
Chen-Yu Tsai | 67e3264 | 2017-10-10 11:19:59 +0800 | [diff] [blame] | 110 | /* |
| 111 | * This function is a helper for TCON output muxing. The TCON output |
| 112 | * muxing control register in earlier SoCs (without the TCON TOP block) |
| 113 | * are located in TCON0. This helper returns a pointer to TCON0's |
| 114 | * sun4i_tcon structure, or NULL if not found. |
| 115 | */ |
| 116 | static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm) |
| 117 | { |
| 118 | struct sun4i_drv *drv = drm->dev_private; |
| 119 | struct sun4i_tcon *tcon; |
| 120 | |
| 121 | list_for_each_entry(tcon, &drv->tcon_list, list) |
| 122 | if (tcon->id == 0) |
| 123 | return tcon; |
| 124 | |
| 125 | dev_warn(drm->dev, |
| 126 | "TCON0 not found, display output muxing may not work\n"); |
| 127 | |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Maxime Ripard | f8c73f4 | 2017-05-27 18:09:27 +0200 | [diff] [blame] | 131 | void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel, |
Maxime Ripard | abcb876 | 2017-10-17 11:06:10 +0200 | [diff] [blame] | 132 | const struct drm_encoder *encoder) |
Maxime Ripard | f8c73f4 | 2017-05-27 18:09:27 +0200 | [diff] [blame] | 133 | { |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 134 | int ret = -ENOTSUPP; |
Maxime Ripard | b7cb9b9 | 2017-05-27 18:09:28 +0200 | [diff] [blame] | 135 | |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 136 | if (tcon->quirks->set_mux) |
| 137 | ret = tcon->quirks->set_mux(tcon, encoder); |
Maxime Ripard | f8c73f4 | 2017-05-27 18:09:27 +0200 | [diff] [blame] | 138 | |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 139 | DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n", |
| 140 | encoder->name, encoder->crtc->name, ret); |
Maxime Ripard | f8c73f4 | 2017-05-27 18:09:27 +0200 | [diff] [blame] | 141 | } |
Maxime Ripard | f8c73f4 | 2017-05-27 18:09:27 +0200 | [diff] [blame] | 142 | |
Maxime Ripard | 961c645 | 2017-10-17 11:06:11 +0200 | [diff] [blame] | 143 | static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 144 | int channel) |
| 145 | { |
| 146 | int delay = mode->vtotal - mode->vdisplay; |
| 147 | |
| 148 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 149 | delay /= 2; |
| 150 | |
| 151 | if (channel == 1) |
| 152 | delay -= 2; |
| 153 | |
| 154 | delay = min(delay, 30); |
| 155 | |
| 156 | DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); |
| 157 | |
| 158 | return delay; |
| 159 | } |
| 160 | |
Maxime Ripard | 5b8f091 | 2017-10-17 11:06:13 +0200 | [diff] [blame^] | 161 | static void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon, |
| 162 | const struct drm_display_mode *mode) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 163 | { |
| 164 | unsigned int bp, hsync, vsync; |
| 165 | u8 clk_delay; |
| 166 | u32 val = 0; |
| 167 | |
Chen-Yu Tsai | 86cf678 | 2017-04-25 23:25:04 +0800 | [diff] [blame] | 168 | /* Configure the dot clock */ |
| 169 | clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); |
| 170 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 171 | /* Adjust clock delay */ |
| 172 | clk_delay = sun4i_tcon_get_clk_delay(mode, 0); |
| 173 | regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, |
| 174 | SUN4I_TCON0_CTL_CLK_DELAY_MASK, |
| 175 | SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); |
| 176 | |
| 177 | /* Set the resolution */ |
| 178 | regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, |
| 179 | SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | |
| 180 | SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); |
| 181 | |
| 182 | /* |
| 183 | * This is called a backporch in the register documentation, |
Chen-Yu Tsai | 23a1cb1 | 2017-03-09 18:05:25 +0800 | [diff] [blame] | 184 | * but it really is the back porch + hsync |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 185 | */ |
| 186 | bp = mode->crtc_htotal - mode->crtc_hsync_start; |
| 187 | DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", |
| 188 | mode->crtc_htotal, bp); |
| 189 | |
| 190 | /* Set horizontal display timings */ |
| 191 | regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, |
| 192 | SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | |
| 193 | SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); |
| 194 | |
| 195 | /* |
| 196 | * This is called a backporch in the register documentation, |
Chen-Yu Tsai | 23a1cb1 | 2017-03-09 18:05:25 +0800 | [diff] [blame] | 197 | * but it really is the back porch + hsync |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 198 | */ |
| 199 | bp = mode->crtc_vtotal - mode->crtc_vsync_start; |
| 200 | DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", |
| 201 | mode->crtc_vtotal, bp); |
| 202 | |
| 203 | /* Set vertical display timings */ |
| 204 | regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, |
Maxime Ripard | a88cbbd | 2017-05-27 18:09:30 +0200 | [diff] [blame] | 205 | SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 206 | SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); |
| 207 | |
| 208 | /* Set Hsync and Vsync length */ |
| 209 | hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; |
| 210 | vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; |
| 211 | DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); |
| 212 | regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, |
| 213 | SUN4I_TCON0_BASIC3_V_SYNC(vsync) | |
| 214 | SUN4I_TCON0_BASIC3_H_SYNC(hsync)); |
| 215 | |
| 216 | /* Setup the polarity of the various signals */ |
| 217 | if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) |
| 218 | val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; |
| 219 | |
| 220 | if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) |
| 221 | val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; |
| 222 | |
| 223 | regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, |
| 224 | SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE, |
| 225 | val); |
| 226 | |
| 227 | /* Map output pins to channel 0 */ |
| 228 | regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, |
| 229 | SUN4I_TCON_GCTL_IOMAP_MASK, |
| 230 | SUN4I_TCON_GCTL_IOMAP_TCON0); |
| 231 | |
| 232 | /* Enable the output on the pins */ |
| 233 | regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); |
| 234 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 235 | |
Maxime Ripard | 5b8f091 | 2017-10-17 11:06:13 +0200 | [diff] [blame^] | 236 | static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, |
| 237 | const struct drm_display_mode *mode) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 238 | { |
Maxime Ripard | b8317a3 | 2017-05-27 18:09:31 +0200 | [diff] [blame] | 239 | unsigned int bp, hsync, vsync, vtotal; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 240 | u8 clk_delay; |
| 241 | u32 val; |
| 242 | |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 243 | WARN_ON(!tcon->quirks->has_channel_1); |
Maxime Ripard | 8e92404 | 2016-01-07 12:32:07 +0100 | [diff] [blame] | 244 | |
Chen-Yu Tsai | 86cf678 | 2017-04-25 23:25:04 +0800 | [diff] [blame] | 245 | /* Configure the dot clock */ |
| 246 | clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); |
| 247 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 248 | /* Adjust clock delay */ |
| 249 | clk_delay = sun4i_tcon_get_clk_delay(mode, 1); |
| 250 | regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, |
| 251 | SUN4I_TCON1_CTL_CLK_DELAY_MASK, |
| 252 | SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); |
| 253 | |
| 254 | /* Set interlaced mode */ |
| 255 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 256 | val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; |
| 257 | else |
| 258 | val = 0; |
| 259 | regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, |
| 260 | SUN4I_TCON1_CTL_INTERLACE_ENABLE, |
| 261 | val); |
| 262 | |
| 263 | /* Set the input resolution */ |
| 264 | regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, |
| 265 | SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | |
| 266 | SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); |
| 267 | |
| 268 | /* Set the upscaling resolution */ |
| 269 | regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, |
| 270 | SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | |
| 271 | SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); |
| 272 | |
| 273 | /* Set the output resolution */ |
| 274 | regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, |
| 275 | SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | |
| 276 | SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); |
| 277 | |
| 278 | /* Set horizontal display timings */ |
Maxime Ripard | 3cb2f46 | 2017-05-27 18:09:29 +0200 | [diff] [blame] | 279 | bp = mode->crtc_htotal - mode->crtc_hsync_start; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 280 | DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", |
| 281 | mode->htotal, bp); |
| 282 | regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, |
| 283 | SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | |
| 284 | SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); |
| 285 | |
Maxime Ripard | 3cb2f46 | 2017-05-27 18:09:29 +0200 | [diff] [blame] | 286 | bp = mode->crtc_vtotal - mode->crtc_vsync_start; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 287 | DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", |
Maxime Ripard | b8317a3 | 2017-05-27 18:09:31 +0200 | [diff] [blame] | 288 | mode->crtc_vtotal, bp); |
| 289 | |
| 290 | /* |
| 291 | * The vertical resolution needs to be doubled in all |
| 292 | * cases. We could use crtc_vtotal and always multiply by two, |
| 293 | * but that leads to a rounding error in interlace when vtotal |
| 294 | * is odd. |
| 295 | * |
| 296 | * This happens with TV's PAL for example, where vtotal will |
| 297 | * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be |
| 298 | * 624, which apparently confuses the hardware. |
| 299 | * |
| 300 | * To work around this, we will always use vtotal, and |
| 301 | * multiply by two only if we're not in interlace. |
| 302 | */ |
| 303 | vtotal = mode->vtotal; |
| 304 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) |
| 305 | vtotal = vtotal * 2; |
| 306 | |
| 307 | /* Set vertical display timings */ |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 308 | regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, |
Maxime Ripard | b8317a3 | 2017-05-27 18:09:31 +0200 | [diff] [blame] | 309 | SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 310 | SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); |
| 311 | |
| 312 | /* Set Hsync and Vsync length */ |
| 313 | hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; |
| 314 | vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; |
| 315 | DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); |
| 316 | regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, |
| 317 | SUN4I_TCON1_BASIC5_V_SYNC(vsync) | |
| 318 | SUN4I_TCON1_BASIC5_H_SYNC(hsync)); |
| 319 | |
| 320 | /* Map output pins to channel 1 */ |
| 321 | regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, |
| 322 | SUN4I_TCON_GCTL_IOMAP_MASK, |
| 323 | SUN4I_TCON_GCTL_IOMAP_TCON1); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 324 | } |
Maxime Ripard | 5b8f091 | 2017-10-17 11:06:13 +0200 | [diff] [blame^] | 325 | |
| 326 | void sun4i_tcon_mode_set(struct sun4i_tcon *tcon, |
| 327 | const struct drm_encoder *encoder, |
| 328 | const struct drm_display_mode *mode) |
| 329 | { |
| 330 | switch (encoder->encoder_type) { |
| 331 | case DRM_MODE_ENCODER_NONE: |
| 332 | sun4i_tcon0_mode_set(tcon, mode); |
| 333 | sun4i_tcon_set_mux(tcon, 0, encoder); |
| 334 | break; |
| 335 | case DRM_MODE_ENCODER_TVDAC: |
| 336 | case DRM_MODE_ENCODER_TMDS: |
| 337 | sun4i_tcon1_mode_set(tcon, mode); |
| 338 | sun4i_tcon_set_mux(tcon, 1, encoder); |
| 339 | break; |
| 340 | default: |
| 341 | DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); |
| 342 | } |
| 343 | } |
| 344 | EXPORT_SYMBOL(sun4i_tcon_mode_set); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 345 | |
| 346 | static void sun4i_tcon_finish_page_flip(struct drm_device *dev, |
| 347 | struct sun4i_crtc *scrtc) |
| 348 | { |
| 349 | unsigned long flags; |
| 350 | |
| 351 | spin_lock_irqsave(&dev->event_lock, flags); |
| 352 | if (scrtc->event) { |
| 353 | drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); |
| 354 | drm_crtc_vblank_put(&scrtc->crtc); |
| 355 | scrtc->event = NULL; |
| 356 | } |
| 357 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 358 | } |
| 359 | |
| 360 | static irqreturn_t sun4i_tcon_handler(int irq, void *private) |
| 361 | { |
| 362 | struct sun4i_tcon *tcon = private; |
| 363 | struct drm_device *drm = tcon->drm; |
Chen-Yu Tsai | 46cce6d | 2017-02-23 16:05:37 +0800 | [diff] [blame] | 364 | struct sun4i_crtc *scrtc = tcon->crtc; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 365 | unsigned int status; |
| 366 | |
| 367 | regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); |
| 368 | |
| 369 | if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | |
| 370 | SUN4I_TCON_GINT0_VBLANK_INT(1)))) |
| 371 | return IRQ_NONE; |
| 372 | |
| 373 | drm_crtc_handle_vblank(&scrtc->crtc); |
| 374 | sun4i_tcon_finish_page_flip(drm, scrtc); |
| 375 | |
| 376 | /* Acknowledge the interrupt */ |
| 377 | regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, |
| 378 | SUN4I_TCON_GINT0_VBLANK_INT(0) | |
| 379 | SUN4I_TCON_GINT0_VBLANK_INT(1), |
| 380 | 0); |
| 381 | |
| 382 | return IRQ_HANDLED; |
| 383 | } |
| 384 | |
| 385 | static int sun4i_tcon_init_clocks(struct device *dev, |
| 386 | struct sun4i_tcon *tcon) |
| 387 | { |
| 388 | tcon->clk = devm_clk_get(dev, "ahb"); |
| 389 | if (IS_ERR(tcon->clk)) { |
| 390 | dev_err(dev, "Couldn't get the TCON bus clock\n"); |
| 391 | return PTR_ERR(tcon->clk); |
| 392 | } |
| 393 | clk_prepare_enable(tcon->clk); |
| 394 | |
| 395 | tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); |
| 396 | if (IS_ERR(tcon->sclk0)) { |
| 397 | dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); |
| 398 | return PTR_ERR(tcon->sclk0); |
| 399 | } |
| 400 | |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 401 | if (tcon->quirks->has_channel_1) { |
Maxime Ripard | 8e92404 | 2016-01-07 12:32:07 +0100 | [diff] [blame] | 402 | tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); |
| 403 | if (IS_ERR(tcon->sclk1)) { |
| 404 | dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); |
| 405 | return PTR_ERR(tcon->sclk1); |
| 406 | } |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 409 | return 0; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) |
| 413 | { |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 414 | clk_disable_unprepare(tcon->clk); |
| 415 | } |
| 416 | |
| 417 | static int sun4i_tcon_init_irq(struct device *dev, |
| 418 | struct sun4i_tcon *tcon) |
| 419 | { |
| 420 | struct platform_device *pdev = to_platform_device(dev); |
| 421 | int irq, ret; |
| 422 | |
| 423 | irq = platform_get_irq(pdev, 0); |
| 424 | if (irq < 0) { |
| 425 | dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); |
| 426 | return irq; |
| 427 | } |
| 428 | |
| 429 | ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, |
| 430 | dev_name(dev), tcon); |
| 431 | if (ret) { |
| 432 | dev_err(dev, "Couldn't request the IRQ\n"); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static struct regmap_config sun4i_tcon_regmap_config = { |
| 440 | .reg_bits = 32, |
| 441 | .val_bits = 32, |
| 442 | .reg_stride = 4, |
| 443 | .max_register = 0x800, |
| 444 | }; |
| 445 | |
| 446 | static int sun4i_tcon_init_regmap(struct device *dev, |
| 447 | struct sun4i_tcon *tcon) |
| 448 | { |
| 449 | struct platform_device *pdev = to_platform_device(dev); |
| 450 | struct resource *res; |
| 451 | void __iomem *regs; |
| 452 | |
| 453 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 454 | regs = devm_ioremap_resource(dev, res); |
Wei Yongjun | af346f5 | 2016-08-26 14:25:25 +0000 | [diff] [blame] | 455 | if (IS_ERR(regs)) |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 456 | return PTR_ERR(regs); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 457 | |
| 458 | tcon->regs = devm_regmap_init_mmio(dev, regs, |
| 459 | &sun4i_tcon_regmap_config); |
| 460 | if (IS_ERR(tcon->regs)) { |
| 461 | dev_err(dev, "Couldn't create the TCON regmap\n"); |
| 462 | return PTR_ERR(tcon->regs); |
| 463 | } |
| 464 | |
| 465 | /* Make sure the TCON is disabled and all IRQs are off */ |
| 466 | regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); |
| 467 | regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); |
| 468 | regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); |
| 469 | |
| 470 | /* Disable IO lines and set them to tristate */ |
| 471 | regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); |
| 472 | regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 477 | /* |
| 478 | * On SoCs with the old display pipeline design (Display Engine 1.0), |
| 479 | * the TCON is always tied to just one backend. Hence we can traverse |
| 480 | * the of_graph upwards to find the backend our tcon is connected to, |
| 481 | * and take its ID as our own. |
| 482 | * |
| 483 | * We can either identify backends from their compatible strings, which |
| 484 | * means maintaining a large list of them. Or, since the backend is |
| 485 | * registered and binded before the TCON, we can just go through the |
| 486 | * list of registered backends and compare the device node. |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 487 | * |
| 488 | * As the structures now store engines instead of backends, here this |
| 489 | * function in fact searches the corresponding engine, and the ID is |
| 490 | * requested via the get_id function of the engine. |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 491 | */ |
Chen-Yu Tsai | e8d5bbf | 2017-09-08 15:50:12 +0800 | [diff] [blame] | 492 | static struct sunxi_engine * |
| 493 | sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv, |
| 494 | struct device_node *node) |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 495 | { |
| 496 | struct device_node *port, *ep, *remote; |
Chen-Yu Tsai | be3fe0f | 2017-09-08 15:50:13 +0800 | [diff] [blame] | 497 | struct sunxi_engine *engine = ERR_PTR(-EINVAL); |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 498 | |
| 499 | port = of_graph_get_port_by_id(node, 0); |
| 500 | if (!port) |
| 501 | return ERR_PTR(-EINVAL); |
| 502 | |
Chen-Yu Tsai | 1469619 | 2017-09-08 15:50:11 +0800 | [diff] [blame] | 503 | /* |
| 504 | * This only works if there is only one path from the TCON |
| 505 | * to any display engine. Otherwise the probe order of the |
| 506 | * TCONs and display engines is not guaranteed. They may |
| 507 | * either bind to the wrong one, or worse, bind to the same |
| 508 | * one if additional checks are not done. |
| 509 | * |
| 510 | * Bail out if there are multiple input connections. |
| 511 | */ |
Chen-Yu Tsai | be3fe0f | 2017-09-08 15:50:13 +0800 | [diff] [blame] | 512 | if (of_get_available_child_count(port) != 1) |
| 513 | goto out_put_port; |
Chen-Yu Tsai | 1469619 | 2017-09-08 15:50:11 +0800 | [diff] [blame] | 514 | |
Chen-Yu Tsai | be3fe0f | 2017-09-08 15:50:13 +0800 | [diff] [blame] | 515 | /* Get the first connection without specifying an ID */ |
| 516 | ep = of_get_next_available_child(port, NULL); |
| 517 | if (!ep) |
| 518 | goto out_put_port; |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 519 | |
Chen-Yu Tsai | be3fe0f | 2017-09-08 15:50:13 +0800 | [diff] [blame] | 520 | remote = of_graph_get_remote_port_parent(ep); |
| 521 | if (!remote) |
| 522 | goto out_put_ep; |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 523 | |
Chen-Yu Tsai | be3fe0f | 2017-09-08 15:50:13 +0800 | [diff] [blame] | 524 | /* does this node match any registered engines? */ |
| 525 | list_for_each_entry(engine, &drv->engine_list, list) |
| 526 | if (remote == engine->node) |
| 527 | goto out_put_remote; |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 528 | |
Chen-Yu Tsai | be3fe0f | 2017-09-08 15:50:13 +0800 | [diff] [blame] | 529 | /* keep looking through upstream ports */ |
| 530 | engine = sun4i_tcon_find_engine_traverse(drv, remote); |
| 531 | |
| 532 | out_put_remote: |
| 533 | of_node_put(remote); |
| 534 | out_put_ep: |
| 535 | of_node_put(ep); |
| 536 | out_put_port: |
| 537 | of_node_put(port); |
| 538 | |
| 539 | return engine; |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 540 | } |
| 541 | |
Chen-Yu Tsai | e8d5bbf | 2017-09-08 15:50:12 +0800 | [diff] [blame] | 542 | /* |
| 543 | * The device tree binding says that the remote endpoint ID of any |
| 544 | * connection between components, up to and including the TCON, of |
| 545 | * the display pipeline should be equal to the actual ID of the local |
| 546 | * component. Thus we can look at any one of the input connections of |
| 547 | * the TCONs, and use that connection's remote endpoint ID as our own. |
| 548 | * |
| 549 | * Since the user of this function already finds the input port, |
| 550 | * the port is passed in directly without further checks. |
| 551 | */ |
| 552 | static int sun4i_tcon_of_get_id_from_port(struct device_node *port) |
| 553 | { |
| 554 | struct device_node *ep; |
| 555 | int ret = -EINVAL; |
| 556 | |
| 557 | /* try finding an upstream endpoint */ |
| 558 | for_each_available_child_of_node(port, ep) { |
| 559 | struct device_node *remote; |
| 560 | u32 reg; |
| 561 | |
| 562 | remote = of_graph_get_remote_endpoint(ep); |
| 563 | if (!remote) |
| 564 | continue; |
| 565 | |
| 566 | ret = of_property_read_u32(remote, "reg", ®); |
| 567 | if (ret) |
| 568 | continue; |
| 569 | |
| 570 | ret = reg; |
| 571 | } |
| 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Once we know the TCON's id, we can look through the list of |
| 578 | * engines to find a matching one. We assume all engines have |
| 579 | * been probed and added to the list. |
| 580 | */ |
| 581 | static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv, |
| 582 | int id) |
| 583 | { |
| 584 | struct sunxi_engine *engine; |
| 585 | |
| 586 | list_for_each_entry(engine, &drv->engine_list, list) |
| 587 | if (engine->id == id) |
| 588 | return engine; |
| 589 | |
| 590 | return ERR_PTR(-EINVAL); |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * On SoCs with the old display pipeline design (Display Engine 1.0), |
| 595 | * we assumed the TCON was always tied to just one backend. However |
| 596 | * this proved not to be the case. On the A31, the TCON can select |
| 597 | * either backend as its source. On the A20 (and likely on the A10), |
| 598 | * the backend can choose which TCON to output to. |
| 599 | * |
| 600 | * The device tree binding says that the remote endpoint ID of any |
| 601 | * connection between components, up to and including the TCON, of |
| 602 | * the display pipeline should be equal to the actual ID of the local |
| 603 | * component. Thus we should be able to look at any one of the input |
| 604 | * connections of the TCONs, and use that connection's remote endpoint |
| 605 | * ID as our own. |
| 606 | * |
| 607 | * However the connections between the backend and TCON were assumed |
| 608 | * to be always singular, and their endpoit IDs were all incorrectly |
| 609 | * set to 0. This means for these old device trees, we cannot just look |
| 610 | * up the remote endpoint ID of a TCON input endpoint. TCON1 would be |
| 611 | * incorrectly identified as TCON0. |
| 612 | * |
| 613 | * This function first checks if the TCON node has 2 input endpoints. |
| 614 | * If so, then the device tree is a corrected version, and it will use |
| 615 | * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above |
| 616 | * to fetch the ID and engine directly. If not, then it is likely an |
| 617 | * old device trees, where the endpoint IDs were incorrect, but did not |
| 618 | * have endpoint connections between the backend and TCON across |
| 619 | * different display pipelines. It will fall back to the old method of |
| 620 | * traversing the of_graph to try and find a matching engine by device |
| 621 | * node. |
| 622 | * |
| 623 | * In the case of single display pipeline device trees, either method |
| 624 | * works. |
| 625 | */ |
| 626 | static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv, |
| 627 | struct device_node *node) |
| 628 | { |
| 629 | struct device_node *port; |
| 630 | struct sunxi_engine *engine; |
| 631 | |
| 632 | port = of_graph_get_port_by_id(node, 0); |
| 633 | if (!port) |
| 634 | return ERR_PTR(-EINVAL); |
| 635 | |
| 636 | /* |
| 637 | * Is this a corrected device tree with cross pipeline |
| 638 | * connections between the backend and TCON? |
| 639 | */ |
| 640 | if (of_get_child_count(port) > 1) { |
| 641 | /* Get our ID directly from an upstream endpoint */ |
| 642 | int id = sun4i_tcon_of_get_id_from_port(port); |
| 643 | |
| 644 | /* Get our engine by matching our ID */ |
| 645 | engine = sun4i_tcon_get_engine_by_id(drv, id); |
| 646 | |
| 647 | of_node_put(port); |
| 648 | return engine; |
| 649 | } |
| 650 | |
| 651 | /* Fallback to old method by traversing input endpoints */ |
| 652 | of_node_put(port); |
| 653 | return sun4i_tcon_find_engine_traverse(drv, node); |
| 654 | } |
| 655 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 656 | static int sun4i_tcon_bind(struct device *dev, struct device *master, |
| 657 | void *data) |
| 658 | { |
| 659 | struct drm_device *drm = data; |
| 660 | struct sun4i_drv *drv = drm->dev_private; |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 661 | struct sunxi_engine *engine; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 662 | struct sun4i_tcon *tcon; |
| 663 | int ret; |
| 664 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 665 | engine = sun4i_tcon_find_engine(drv, dev->of_node); |
| 666 | if (IS_ERR(engine)) { |
| 667 | dev_err(dev, "Couldn't find matching engine\n"); |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 668 | return -EPROBE_DEFER; |
Chen-Yu Tsai | b317fa3 | 2017-04-21 16:38:54 +0800 | [diff] [blame] | 669 | } |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 670 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 671 | tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); |
| 672 | if (!tcon) |
| 673 | return -ENOMEM; |
| 674 | dev_set_drvdata(dev, tcon); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 675 | tcon->drm = drm; |
Maxime Ripard | ae55811 | 2016-07-19 15:17:27 +0200 | [diff] [blame] | 676 | tcon->dev = dev; |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 677 | tcon->id = engine->id; |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 678 | tcon->quirks = of_device_get_match_data(dev); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 679 | |
| 680 | tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); |
| 681 | if (IS_ERR(tcon->lcd_rst)) { |
| 682 | dev_err(dev, "Couldn't get our reset line\n"); |
| 683 | return PTR_ERR(tcon->lcd_rst); |
| 684 | } |
| 685 | |
| 686 | /* Make sure our TCON is reset */ |
Chen-Yu Tsai | d57294c | 2017-09-08 17:00:16 +0800 | [diff] [blame] | 687 | ret = reset_control_reset(tcon->lcd_rst); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 688 | if (ret) { |
| 689 | dev_err(dev, "Couldn't deassert our reset line\n"); |
| 690 | return ret; |
| 691 | } |
| 692 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 693 | ret = sun4i_tcon_init_clocks(dev, tcon); |
| 694 | if (ret) { |
| 695 | dev_err(dev, "Couldn't init our TCON clocks\n"); |
| 696 | goto err_assert_reset; |
| 697 | } |
| 698 | |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 699 | ret = sun4i_tcon_init_regmap(dev, tcon); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 700 | if (ret) { |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 701 | dev_err(dev, "Couldn't init our TCON regmap\n"); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 702 | goto err_free_clocks; |
| 703 | } |
| 704 | |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 705 | ret = sun4i_dclk_create(dev, tcon); |
| 706 | if (ret) { |
| 707 | dev_err(dev, "Couldn't create our TCON dot clock\n"); |
| 708 | goto err_free_clocks; |
| 709 | } |
| 710 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 711 | ret = sun4i_tcon_init_irq(dev, tcon); |
| 712 | if (ret) { |
| 713 | dev_err(dev, "Couldn't init our TCON interrupts\n"); |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 714 | goto err_free_dotclock; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Icenowy Zheng | 8796933 | 2017-05-17 22:47:17 +0800 | [diff] [blame] | 717 | tcon->crtc = sun4i_crtc_init(drm, engine, tcon); |
Chen-Yu Tsai | 46cce6d | 2017-02-23 16:05:37 +0800 | [diff] [blame] | 718 | if (IS_ERR(tcon->crtc)) { |
| 719 | dev_err(dev, "Couldn't create our CRTC\n"); |
| 720 | ret = PTR_ERR(tcon->crtc); |
| 721 | goto err_free_clocks; |
| 722 | } |
| 723 | |
Chen-Yu Tsai | b9c8506 | 2017-02-23 16:05:41 +0800 | [diff] [blame] | 724 | ret = sun4i_rgb_init(drm, tcon); |
Chen-Yu Tsai | 13fef09 | 2016-05-17 23:56:06 +0800 | [diff] [blame] | 725 | if (ret < 0) |
| 726 | goto err_free_clocks; |
| 727 | |
Chen-Yu Tsai | 27e18de | 2017-09-08 15:50:14 +0800 | [diff] [blame] | 728 | if (tcon->quirks->needs_de_be_mux) { |
| 729 | /* |
| 730 | * We assume there is no dynamic muxing of backends |
| 731 | * and TCONs, so we select the backend with same ID. |
| 732 | * |
| 733 | * While dynamic selection might be interesting, since |
| 734 | * the CRTC is tied to the TCON, while the layers are |
| 735 | * tied to the backends, this means, we will need to |
| 736 | * switch between groups of layers. There might not be |
| 737 | * a way to represent this constraint in DRM. |
| 738 | */ |
| 739 | regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, |
| 740 | SUN4I_TCON0_CTL_SRC_SEL_MASK, |
| 741 | tcon->id); |
| 742 | regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, |
| 743 | SUN4I_TCON1_CTL_SRC_SEL_MASK, |
| 744 | tcon->id); |
| 745 | } |
| 746 | |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 747 | list_add_tail(&tcon->list, &drv->tcon_list); |
| 748 | |
Chen-Yu Tsai | 13fef09 | 2016-05-17 23:56:06 +0800 | [diff] [blame] | 749 | return 0; |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 750 | |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 751 | err_free_dotclock: |
| 752 | sun4i_dclk_free(tcon); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 753 | err_free_clocks: |
| 754 | sun4i_tcon_free_clocks(tcon); |
| 755 | err_assert_reset: |
| 756 | reset_control_assert(tcon->lcd_rst); |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | static void sun4i_tcon_unbind(struct device *dev, struct device *master, |
| 761 | void *data) |
| 762 | { |
| 763 | struct sun4i_tcon *tcon = dev_get_drvdata(dev); |
| 764 | |
Chen-Yu Tsai | 80a5824 | 2017-04-21 16:38:50 +0800 | [diff] [blame] | 765 | list_del(&tcon->list); |
Chen-Yu Tsai | 4c7f16d | 2017-03-09 18:05:24 +0800 | [diff] [blame] | 766 | sun4i_dclk_free(tcon); |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 767 | sun4i_tcon_free_clocks(tcon); |
| 768 | } |
| 769 | |
Julia Lawall | dfeb693 | 2016-11-12 18:19:58 +0100 | [diff] [blame] | 770 | static const struct component_ops sun4i_tcon_ops = { |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 771 | .bind = sun4i_tcon_bind, |
| 772 | .unbind = sun4i_tcon_unbind, |
| 773 | }; |
| 774 | |
| 775 | static int sun4i_tcon_probe(struct platform_device *pdev) |
| 776 | { |
Maxime Ripard | 29e57fa | 2015-10-29 09:37:32 +0100 | [diff] [blame] | 777 | struct device_node *node = pdev->dev.of_node; |
Maxime Ripard | 894f5a9 | 2016-04-11 12:16:33 +0200 | [diff] [blame] | 778 | struct drm_bridge *bridge; |
Maxime Ripard | 29e57fa | 2015-10-29 09:37:32 +0100 | [diff] [blame] | 779 | struct drm_panel *panel; |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 780 | int ret; |
Maxime Ripard | 29e57fa | 2015-10-29 09:37:32 +0100 | [diff] [blame] | 781 | |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 782 | ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); |
| 783 | if (ret == -EPROBE_DEFER) |
| 784 | return ret; |
Maxime Ripard | 29e57fa | 2015-10-29 09:37:32 +0100 | [diff] [blame] | 785 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 786 | return component_add(&pdev->dev, &sun4i_tcon_ops); |
| 787 | } |
| 788 | |
| 789 | static int sun4i_tcon_remove(struct platform_device *pdev) |
| 790 | { |
| 791 | component_del(&pdev->dev, &sun4i_tcon_ops); |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 796 | /* platform specific TCON muxing callbacks */ |
| 797 | static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon, |
Maxime Ripard | abcb876 | 2017-10-17 11:06:10 +0200 | [diff] [blame] | 798 | const struct drm_encoder *encoder) |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 799 | { |
| 800 | u32 val; |
| 801 | |
| 802 | if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC) |
| 803 | val = 1; |
| 804 | else |
| 805 | val = 0; |
| 806 | |
| 807 | /* |
| 808 | * FIXME: Undocumented bits |
| 809 | */ |
| 810 | return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val); |
| 811 | } |
| 812 | |
Chen-Yu Tsai | 67e3264 | 2017-10-10 11:19:59 +0800 | [diff] [blame] | 813 | static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon, |
Maxime Ripard | abcb876 | 2017-10-17 11:06:10 +0200 | [diff] [blame] | 814 | const struct drm_encoder *encoder) |
Chen-Yu Tsai | 67e3264 | 2017-10-10 11:19:59 +0800 | [diff] [blame] | 815 | { |
| 816 | struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); |
| 817 | u32 shift; |
| 818 | |
| 819 | if (!tcon0) |
| 820 | return -EINVAL; |
| 821 | |
| 822 | switch (encoder->encoder_type) { |
| 823 | case DRM_MODE_ENCODER_TMDS: |
| 824 | /* HDMI */ |
| 825 | shift = 8; |
| 826 | break; |
| 827 | default: |
| 828 | /* TODO A31 has MIPI DSI but A31s does not */ |
| 829 | return -EINVAL; |
| 830 | } |
| 831 | |
| 832 | regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, |
| 833 | 0x3 << shift, tcon->id << shift); |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 838 | static const struct sun4i_tcon_quirks sun5i_a13_quirks = { |
Chen-Yu Tsai | ad537fb | 2017-10-10 11:19:58 +0800 | [diff] [blame] | 839 | .has_channel_1 = true, |
| 840 | .set_mux = sun5i_a13_tcon_set_mux, |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 841 | }; |
| 842 | |
Chen-Yu Tsai | 93a5ec1 | 2016-10-20 11:43:40 +0800 | [diff] [blame] | 843 | static const struct sun4i_tcon_quirks sun6i_a31_quirks = { |
Chen-Yu Tsai | 27e18de | 2017-09-08 15:50:14 +0800 | [diff] [blame] | 844 | .has_channel_1 = true, |
| 845 | .needs_de_be_mux = true, |
Chen-Yu Tsai | 67e3264 | 2017-10-10 11:19:59 +0800 | [diff] [blame] | 846 | .set_mux = sun6i_tcon_set_mux, |
Chen-Yu Tsai | 93a5ec1 | 2016-10-20 11:43:40 +0800 | [diff] [blame] | 847 | }; |
| 848 | |
| 849 | static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { |
Chen-Yu Tsai | 27e18de | 2017-09-08 15:50:14 +0800 | [diff] [blame] | 850 | .has_channel_1 = true, |
| 851 | .needs_de_be_mux = true, |
Chen-Yu Tsai | 93a5ec1 | 2016-10-20 11:43:40 +0800 | [diff] [blame] | 852 | }; |
| 853 | |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 854 | static const struct sun4i_tcon_quirks sun8i_a33_quirks = { |
| 855 | /* nothing is supported */ |
| 856 | }; |
| 857 | |
Icenowy Zheng | 1a0edb3 | 2017-05-17 22:47:22 +0800 | [diff] [blame] | 858 | static const struct sun4i_tcon_quirks sun8i_v3s_quirks = { |
| 859 | /* nothing is supported */ |
| 860 | }; |
| 861 | |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 862 | static const struct of_device_id sun4i_tcon_of_table[] = { |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 863 | { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, |
Chen-Yu Tsai | 93a5ec1 | 2016-10-20 11:43:40 +0800 | [diff] [blame] | 864 | { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, |
| 865 | { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, |
Chen-Yu Tsai | 91ea2f2 | 2016-10-20 11:43:39 +0800 | [diff] [blame] | 866 | { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, |
Icenowy Zheng | 1a0edb3 | 2017-05-17 22:47:22 +0800 | [diff] [blame] | 867 | { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks }, |
Maxime Ripard | 9026e0d | 2015-10-29 09:36:23 +0100 | [diff] [blame] | 868 | { } |
| 869 | }; |
| 870 | MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); |
| 871 | |
| 872 | static struct platform_driver sun4i_tcon_platform_driver = { |
| 873 | .probe = sun4i_tcon_probe, |
| 874 | .remove = sun4i_tcon_remove, |
| 875 | .driver = { |
| 876 | .name = "sun4i-tcon", |
| 877 | .of_match_table = sun4i_tcon_of_table, |
| 878 | }, |
| 879 | }; |
| 880 | module_platform_driver(sun4i_tcon_platform_driver); |
| 881 | |
| 882 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 883 | MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); |
| 884 | MODULE_LICENSE("GPL"); |