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