Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Texas Instruments |
| 3 | * Author: Rob Clark <robdclark@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 18 | #include "drm_flip_work.h" |
Daniel Vetter | 3cb9ae4 | 2014-10-29 10:03:57 +0100 | [diff] [blame] | 19 | #include <drm/drm_plane_helper.h> |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 20 | |
| 21 | #include "tilcdc_drv.h" |
| 22 | #include "tilcdc_regs.h" |
| 23 | |
| 24 | struct tilcdc_crtc { |
| 25 | struct drm_crtc base; |
| 26 | |
| 27 | const struct tilcdc_panel_info *info; |
| 28 | uint32_t dirty; |
| 29 | dma_addr_t start, end; |
| 30 | struct drm_pending_vblank_event *event; |
| 31 | int dpms; |
| 32 | wait_queue_head_t frame_done_wq; |
| 33 | bool frame_done; |
| 34 | |
| 35 | /* fb currently set to scanout 0/1: */ |
| 36 | struct drm_framebuffer *scanout[2]; |
| 37 | |
| 38 | /* for deferred fb unref's: */ |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 39 | struct drm_flip_work unref_work; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 40 | }; |
| 41 | #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base) |
| 42 | |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 43 | static void unref_worker(struct drm_flip_work *work, void *val) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 44 | { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 45 | struct tilcdc_crtc *tilcdc_crtc = |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 46 | container_of(work, struct tilcdc_crtc, unref_work); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 47 | struct drm_device *dev = tilcdc_crtc->base.dev; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 48 | |
| 49 | mutex_lock(&dev->mode_config.mutex); |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 50 | drm_framebuffer_unreference(val); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 51 | mutex_unlock(&dev->mode_config.mutex); |
| 52 | } |
| 53 | |
| 54 | static void set_scanout(struct drm_crtc *crtc, int n) |
| 55 | { |
| 56 | static const uint32_t base_reg[] = { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 57 | LCDC_DMA_FB_BASE_ADDR_0_REG, |
| 58 | LCDC_DMA_FB_BASE_ADDR_1_REG, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 59 | }; |
| 60 | static const uint32_t ceil_reg[] = { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 61 | LCDC_DMA_FB_CEILING_ADDR_0_REG, |
| 62 | LCDC_DMA_FB_CEILING_ADDR_1_REG, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 63 | }; |
| 64 | static const uint32_t stat[] = { |
| 65 | LCDC_END_OF_FRAME0, LCDC_END_OF_FRAME1, |
| 66 | }; |
| 67 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 68 | struct drm_device *dev = crtc->dev; |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 69 | struct tilcdc_drm_private *priv = dev->dev_private; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 70 | |
| 71 | pm_runtime_get_sync(dev->dev); |
| 72 | tilcdc_write(dev, base_reg[n], tilcdc_crtc->start); |
| 73 | tilcdc_write(dev, ceil_reg[n], tilcdc_crtc->end); |
| 74 | if (tilcdc_crtc->scanout[n]) { |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 75 | drm_flip_work_queue(&tilcdc_crtc->unref_work, tilcdc_crtc->scanout[n]); |
| 76 | drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 77 | } |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 78 | tilcdc_crtc->scanout[n] = crtc->primary->fb; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 79 | drm_framebuffer_reference(tilcdc_crtc->scanout[n]); |
| 80 | tilcdc_crtc->dirty &= ~stat[n]; |
| 81 | pm_runtime_put_sync(dev->dev); |
| 82 | } |
| 83 | |
| 84 | static void update_scanout(struct drm_crtc *crtc) |
| 85 | { |
| 86 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 87 | struct drm_device *dev = crtc->dev; |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 88 | struct drm_framebuffer *fb = crtc->primary->fb; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 89 | struct drm_gem_cma_object *gem; |
| 90 | unsigned int depth, bpp; |
| 91 | |
| 92 | drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp); |
| 93 | gem = drm_fb_cma_get_gem_obj(fb, 0); |
| 94 | |
| 95 | tilcdc_crtc->start = gem->paddr + fb->offsets[0] + |
| 96 | (crtc->y * fb->pitches[0]) + (crtc->x * bpp/8); |
| 97 | |
| 98 | tilcdc_crtc->end = tilcdc_crtc->start + |
| 99 | (crtc->mode.vdisplay * fb->pitches[0]); |
| 100 | |
| 101 | if (tilcdc_crtc->dpms == DRM_MODE_DPMS_ON) { |
| 102 | /* already enabled, so just mark the frames that need |
| 103 | * updating and they will be updated on vblank: |
| 104 | */ |
| 105 | tilcdc_crtc->dirty |= LCDC_END_OF_FRAME0 | LCDC_END_OF_FRAME1; |
| 106 | drm_vblank_get(dev, 0); |
| 107 | } else { |
| 108 | /* not enabled yet, so update registers immediately: */ |
| 109 | set_scanout(crtc, 0); |
| 110 | set_scanout(crtc, 1); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static void start(struct drm_crtc *crtc) |
| 115 | { |
| 116 | struct drm_device *dev = crtc->dev; |
| 117 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 118 | |
| 119 | if (priv->rev == 2) { |
| 120 | tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET); |
| 121 | msleep(1); |
| 122 | tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET); |
| 123 | msleep(1); |
| 124 | } |
| 125 | |
| 126 | tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE); |
| 127 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY)); |
| 128 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE); |
| 129 | } |
| 130 | |
| 131 | static void stop(struct drm_crtc *crtc) |
| 132 | { |
| 133 | struct drm_device *dev = crtc->dev; |
| 134 | |
| 135 | tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE); |
| 136 | } |
| 137 | |
Jyri Sarha | de9cb5f | 2015-02-26 10:12:41 +0200 | [diff] [blame^] | 138 | static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 139 | static void tilcdc_crtc_destroy(struct drm_crtc *crtc) |
| 140 | { |
| 141 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 142 | |
Jyri Sarha | de9cb5f | 2015-02-26 10:12:41 +0200 | [diff] [blame^] | 143 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 144 | |
| 145 | drm_crtc_cleanup(crtc); |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 146 | drm_flip_work_cleanup(&tilcdc_crtc->unref_work); |
| 147 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 148 | kfree(tilcdc_crtc); |
| 149 | } |
| 150 | |
| 151 | static int tilcdc_crtc_page_flip(struct drm_crtc *crtc, |
| 152 | struct drm_framebuffer *fb, |
Keith Packard | ed8d197 | 2013-07-22 18:49:58 -0700 | [diff] [blame] | 153 | struct drm_pending_vblank_event *event, |
| 154 | uint32_t page_flip_flags) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 155 | { |
| 156 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 157 | struct drm_device *dev = crtc->dev; |
| 158 | |
| 159 | if (tilcdc_crtc->event) { |
| 160 | dev_err(dev->dev, "already pending page flip!\n"); |
| 161 | return -EBUSY; |
| 162 | } |
| 163 | |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 164 | crtc->primary->fb = fb; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 165 | tilcdc_crtc->event = event; |
| 166 | update_scanout(crtc); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode) |
| 172 | { |
| 173 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 174 | struct drm_device *dev = crtc->dev; |
| 175 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 176 | |
| 177 | /* we really only care about on or off: */ |
| 178 | if (mode != DRM_MODE_DPMS_ON) |
| 179 | mode = DRM_MODE_DPMS_OFF; |
| 180 | |
| 181 | if (tilcdc_crtc->dpms == mode) |
| 182 | return; |
| 183 | |
| 184 | tilcdc_crtc->dpms = mode; |
| 185 | |
| 186 | pm_runtime_get_sync(dev->dev); |
| 187 | |
| 188 | if (mode == DRM_MODE_DPMS_ON) { |
| 189 | pm_runtime_forbid(dev->dev); |
| 190 | start(crtc); |
| 191 | } else { |
| 192 | tilcdc_crtc->frame_done = false; |
| 193 | stop(crtc); |
| 194 | |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 195 | /* |
| 196 | * if necessary wait for framedone irq which will still come |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 197 | * before putting things to sleep.. |
| 198 | */ |
| 199 | if (priv->rev == 2) { |
| 200 | int ret = wait_event_timeout( |
| 201 | tilcdc_crtc->frame_done_wq, |
| 202 | tilcdc_crtc->frame_done, |
| 203 | msecs_to_jiffies(50)); |
| 204 | if (ret == 0) |
| 205 | dev_err(dev->dev, "timeout waiting for framedone\n"); |
| 206 | } |
| 207 | pm_runtime_allow(dev->dev); |
| 208 | } |
| 209 | |
| 210 | pm_runtime_put_sync(dev->dev); |
| 211 | } |
| 212 | |
| 213 | static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc, |
| 214 | const struct drm_display_mode *mode, |
| 215 | struct drm_display_mode *adjusted_mode) |
| 216 | { |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | static void tilcdc_crtc_prepare(struct drm_crtc *crtc) |
| 221 | { |
| 222 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
| 223 | } |
| 224 | |
| 225 | static void tilcdc_crtc_commit(struct drm_crtc *crtc) |
| 226 | { |
| 227 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON); |
| 228 | } |
| 229 | |
| 230 | static int tilcdc_crtc_mode_set(struct drm_crtc *crtc, |
| 231 | struct drm_display_mode *mode, |
| 232 | struct drm_display_mode *adjusted_mode, |
| 233 | int x, int y, |
| 234 | struct drm_framebuffer *old_fb) |
| 235 | { |
| 236 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 237 | struct drm_device *dev = crtc->dev; |
| 238 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 239 | const struct tilcdc_panel_info *info = tilcdc_crtc->info; |
| 240 | uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw; |
| 241 | int ret; |
| 242 | |
| 243 | ret = tilcdc_crtc_mode_valid(crtc, mode); |
| 244 | if (WARN_ON(ret)) |
| 245 | return ret; |
| 246 | |
| 247 | if (WARN_ON(!info)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | pm_runtime_get_sync(dev->dev); |
| 251 | |
| 252 | /* Configure the Burst Size and fifo threshold of DMA: */ |
| 253 | reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770; |
| 254 | switch (info->dma_burst_sz) { |
| 255 | case 1: |
| 256 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1); |
| 257 | break; |
| 258 | case 2: |
| 259 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2); |
| 260 | break; |
| 261 | case 4: |
| 262 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4); |
| 263 | break; |
| 264 | case 8: |
| 265 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8); |
| 266 | break; |
| 267 | case 16: |
| 268 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16); |
| 269 | break; |
| 270 | default: |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | reg |= (info->fifo_th << 8); |
| 274 | tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg); |
| 275 | |
| 276 | /* Configure timings: */ |
| 277 | hbp = mode->htotal - mode->hsync_end; |
| 278 | hfp = mode->hsync_start - mode->hdisplay; |
| 279 | hsw = mode->hsync_end - mode->hsync_start; |
| 280 | vbp = mode->vtotal - mode->vsync_end; |
| 281 | vfp = mode->vsync_start - mode->vdisplay; |
| 282 | vsw = mode->vsync_end - mode->vsync_start; |
| 283 | |
| 284 | DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u", |
| 285 | mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw); |
| 286 | |
| 287 | /* Configure the AC Bias Period and Number of Transitions per Interrupt: */ |
| 288 | reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00; |
| 289 | reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) | |
| 290 | LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt); |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 291 | |
| 292 | /* |
| 293 | * subtract one from hfp, hbp, hsw because the hardware uses |
| 294 | * a value of 0 as 1 |
| 295 | */ |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 296 | if (priv->rev == 2) { |
Pantelis Antoniou | c19b3e2 | 2013-06-21 13:52:28 -0500 | [diff] [blame] | 297 | /* clear bits we're going to set */ |
| 298 | reg &= ~0x78000033; |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 299 | reg |= ((hfp-1) & 0x300) >> 8; |
| 300 | reg |= ((hbp-1) & 0x300) >> 4; |
| 301 | reg |= ((hsw-1) & 0x3c0) << 21; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 302 | } |
| 303 | tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg); |
| 304 | |
| 305 | reg = (((mode->hdisplay >> 4) - 1) << 4) | |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 306 | (((hbp-1) & 0xff) << 24) | |
| 307 | (((hfp-1) & 0xff) << 16) | |
| 308 | (((hsw-1) & 0x3f) << 10); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 309 | if (priv->rev == 2) |
| 310 | reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3; |
| 311 | tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg); |
| 312 | |
| 313 | reg = ((mode->vdisplay - 1) & 0x3ff) | |
| 314 | ((vbp & 0xff) << 24) | |
| 315 | ((vfp & 0xff) << 16) | |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 316 | (((vsw-1) & 0x3f) << 10); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 317 | tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg); |
| 318 | |
Darren Etheridge | 6bf02c6 | 2013-06-21 13:52:22 -0500 | [diff] [blame] | 319 | /* |
| 320 | * be sure to set Bit 10 for the V2 LCDC controller, |
| 321 | * otherwise limited to 1024 pixels width, stopping |
| 322 | * 1920x1080 being suppoted. |
| 323 | */ |
| 324 | if (priv->rev == 2) { |
| 325 | if ((mode->vdisplay - 1) & 0x400) { |
| 326 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, |
| 327 | LCDC_LPP_B10); |
| 328 | } else { |
| 329 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, |
| 330 | LCDC_LPP_B10); |
| 331 | } |
| 332 | } |
| 333 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 334 | /* Configure display type: */ |
| 335 | reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) & |
| 336 | ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE | |
| 337 | LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000); |
| 338 | reg |= LCDC_TFT_MODE; /* no monochrome/passive support */ |
| 339 | if (info->tft_alt_mode) |
| 340 | reg |= LCDC_TFT_ALT_ENABLE; |
| 341 | if (priv->rev == 2) { |
| 342 | unsigned int depth, bpp; |
| 343 | |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 344 | drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 345 | switch (bpp) { |
| 346 | case 16: |
| 347 | break; |
| 348 | case 32: |
| 349 | reg |= LCDC_V2_TFT_24BPP_UNPACK; |
| 350 | /* fallthrough */ |
| 351 | case 24: |
| 352 | reg |= LCDC_V2_TFT_24BPP_MODE; |
| 353 | break; |
| 354 | default: |
| 355 | dev_err(dev->dev, "invalid pixel format\n"); |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | } |
| 359 | reg |= info->fdd < 12; |
| 360 | tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg); |
| 361 | |
| 362 | if (info->invert_pxl_clk) |
| 363 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK); |
| 364 | else |
| 365 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK); |
| 366 | |
| 367 | if (info->sync_ctrl) |
| 368 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL); |
| 369 | else |
| 370 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL); |
| 371 | |
| 372 | if (info->sync_edge) |
| 373 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE); |
| 374 | else |
| 375 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE); |
| 376 | |
Darren Etheridge | a976718 | 2013-08-14 21:43:33 +0200 | [diff] [blame] | 377 | /* |
| 378 | * use value from adjusted_mode here as this might have been |
| 379 | * changed as part of the fixup for slave encoders to solve the |
| 380 | * issue where tilcdc timings are not VESA compliant |
| 381 | */ |
| 382 | if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 383 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC); |
| 384 | else |
| 385 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC); |
| 386 | |
| 387 | if (mode->flags & DRM_MODE_FLAG_NVSYNC) |
| 388 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC); |
| 389 | else |
| 390 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC); |
| 391 | |
| 392 | if (info->raster_order) |
| 393 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER); |
| 394 | else |
| 395 | tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER); |
| 396 | |
| 397 | |
| 398 | update_scanout(crtc); |
| 399 | tilcdc_crtc_update_clk(crtc); |
| 400 | |
| 401 | pm_runtime_put_sync(dev->dev); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, |
| 407 | struct drm_framebuffer *old_fb) |
| 408 | { |
| 409 | update_scanout(crtc); |
| 410 | return 0; |
| 411 | } |
| 412 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 413 | static const struct drm_crtc_funcs tilcdc_crtc_funcs = { |
| 414 | .destroy = tilcdc_crtc_destroy, |
| 415 | .set_config = drm_crtc_helper_set_config, |
| 416 | .page_flip = tilcdc_crtc_page_flip, |
| 417 | }; |
| 418 | |
| 419 | static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = { |
| 420 | .dpms = tilcdc_crtc_dpms, |
| 421 | .mode_fixup = tilcdc_crtc_mode_fixup, |
| 422 | .prepare = tilcdc_crtc_prepare, |
| 423 | .commit = tilcdc_crtc_commit, |
| 424 | .mode_set = tilcdc_crtc_mode_set, |
| 425 | .mode_set_base = tilcdc_crtc_mode_set_base, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | int tilcdc_crtc_max_width(struct drm_crtc *crtc) |
| 429 | { |
| 430 | struct drm_device *dev = crtc->dev; |
| 431 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 432 | int max_width = 0; |
| 433 | |
| 434 | if (priv->rev == 1) |
| 435 | max_width = 1024; |
| 436 | else if (priv->rev == 2) |
| 437 | max_width = 2048; |
| 438 | |
| 439 | return max_width; |
| 440 | } |
| 441 | |
| 442 | int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode) |
| 443 | { |
| 444 | struct tilcdc_drm_private *priv = crtc->dev->dev_private; |
| 445 | unsigned int bandwidth; |
Darren Etheridge | e1c5d0a | 2013-06-21 13:52:25 -0500 | [diff] [blame] | 446 | uint32_t hbp, hfp, hsw, vbp, vfp, vsw; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 447 | |
Darren Etheridge | e1c5d0a | 2013-06-21 13:52:25 -0500 | [diff] [blame] | 448 | /* |
| 449 | * check to see if the width is within the range that |
| 450 | * the LCD Controller physically supports |
| 451 | */ |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 452 | if (mode->hdisplay > tilcdc_crtc_max_width(crtc)) |
| 453 | return MODE_VIRTUAL_X; |
| 454 | |
| 455 | /* width must be multiple of 16 */ |
| 456 | if (mode->hdisplay & 0xf) |
| 457 | return MODE_VIRTUAL_X; |
| 458 | |
| 459 | if (mode->vdisplay > 2048) |
| 460 | return MODE_VIRTUAL_Y; |
| 461 | |
Darren Etheridge | e1c5d0a | 2013-06-21 13:52:25 -0500 | [diff] [blame] | 462 | DBG("Processing mode %dx%d@%d with pixel clock %d", |
| 463 | mode->hdisplay, mode->vdisplay, |
| 464 | drm_mode_vrefresh(mode), mode->clock); |
| 465 | |
| 466 | hbp = mode->htotal - mode->hsync_end; |
| 467 | hfp = mode->hsync_start - mode->hdisplay; |
| 468 | hsw = mode->hsync_end - mode->hsync_start; |
| 469 | vbp = mode->vtotal - mode->vsync_end; |
| 470 | vfp = mode->vsync_start - mode->vdisplay; |
| 471 | vsw = mode->vsync_end - mode->vsync_start; |
| 472 | |
| 473 | if ((hbp-1) & ~0x3ff) { |
| 474 | DBG("Pruning mode: Horizontal Back Porch out of range"); |
| 475 | return MODE_HBLANK_WIDE; |
| 476 | } |
| 477 | |
| 478 | if ((hfp-1) & ~0x3ff) { |
| 479 | DBG("Pruning mode: Horizontal Front Porch out of range"); |
| 480 | return MODE_HBLANK_WIDE; |
| 481 | } |
| 482 | |
| 483 | if ((hsw-1) & ~0x3ff) { |
| 484 | DBG("Pruning mode: Horizontal Sync Width out of range"); |
| 485 | return MODE_HSYNC_WIDE; |
| 486 | } |
| 487 | |
| 488 | if (vbp & ~0xff) { |
| 489 | DBG("Pruning mode: Vertical Back Porch out of range"); |
| 490 | return MODE_VBLANK_WIDE; |
| 491 | } |
| 492 | |
| 493 | if (vfp & ~0xff) { |
| 494 | DBG("Pruning mode: Vertical Front Porch out of range"); |
| 495 | return MODE_VBLANK_WIDE; |
| 496 | } |
| 497 | |
| 498 | if ((vsw-1) & ~0x3f) { |
| 499 | DBG("Pruning mode: Vertical Sync Width out of range"); |
| 500 | return MODE_VSYNC_WIDE; |
| 501 | } |
| 502 | |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 503 | /* |
| 504 | * some devices have a maximum allowed pixel clock |
| 505 | * configured from the DT |
| 506 | */ |
| 507 | if (mode->clock > priv->max_pixelclock) { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 508 | DBG("Pruning mode: pixel clock too high"); |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 509 | return MODE_CLOCK_HIGH; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * some devices further limit the max horizontal resolution |
| 514 | * configured from the DT |
| 515 | */ |
| 516 | if (mode->hdisplay > priv->max_width) |
| 517 | return MODE_BAD_WIDTH; |
| 518 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 519 | /* filter out modes that would require too much memory bandwidth: */ |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 520 | bandwidth = mode->hdisplay * mode->vdisplay * |
| 521 | drm_mode_vrefresh(mode); |
| 522 | if (bandwidth > priv->max_bandwidth) { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 523 | DBG("Pruning mode: exceeds defined bandwidth limit"); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 524 | return MODE_BAD; |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 525 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 526 | |
| 527 | return MODE_OK; |
| 528 | } |
| 529 | |
| 530 | void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc, |
| 531 | const struct tilcdc_panel_info *info) |
| 532 | { |
| 533 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 534 | tilcdc_crtc->info = info; |
| 535 | } |
| 536 | |
| 537 | void tilcdc_crtc_update_clk(struct drm_crtc *crtc) |
| 538 | { |
| 539 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 540 | struct drm_device *dev = crtc->dev; |
| 541 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 542 | int dpms = tilcdc_crtc->dpms; |
| 543 | unsigned int lcd_clk, div; |
| 544 | int ret; |
| 545 | |
| 546 | pm_runtime_get_sync(dev->dev); |
| 547 | |
| 548 | if (dpms == DRM_MODE_DPMS_ON) |
| 549 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
| 550 | |
| 551 | /* in raster mode, minimum divisor is 2: */ |
| 552 | ret = clk_set_rate(priv->disp_clk, crtc->mode.clock * 1000 * 2); |
| 553 | if (ret) { |
| 554 | dev_err(dev->dev, "failed to set display clock rate to: %d\n", |
| 555 | crtc->mode.clock); |
| 556 | goto out; |
| 557 | } |
| 558 | |
| 559 | lcd_clk = clk_get_rate(priv->clk); |
| 560 | div = lcd_clk / (crtc->mode.clock * 1000); |
| 561 | |
| 562 | DBG("lcd_clk=%u, mode clock=%d, div=%u", lcd_clk, crtc->mode.clock, div); |
| 563 | DBG("fck=%lu, dpll_disp_ck=%lu", clk_get_rate(priv->clk), clk_get_rate(priv->disp_clk)); |
| 564 | |
| 565 | /* Configure the LCD clock divisor. */ |
| 566 | tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(div) | |
| 567 | LCDC_RASTER_MODE); |
| 568 | |
| 569 | if (priv->rev == 2) |
| 570 | tilcdc_set(dev, LCDC_CLK_ENABLE_REG, |
| 571 | LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN | |
| 572 | LCDC_V2_CORE_CLK_EN); |
| 573 | |
| 574 | if (dpms == DRM_MODE_DPMS_ON) |
| 575 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON); |
| 576 | |
| 577 | out: |
| 578 | pm_runtime_put_sync(dev->dev); |
| 579 | } |
| 580 | |
| 581 | irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc) |
| 582 | { |
| 583 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 584 | struct drm_device *dev = crtc->dev; |
| 585 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 586 | uint32_t stat = tilcdc_read_irqstatus(dev); |
| 587 | |
| 588 | if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) { |
| 589 | stop(crtc); |
| 590 | dev_err(dev->dev, "error: %08x\n", stat); |
| 591 | tilcdc_clear_irqstatus(dev, stat); |
| 592 | start(crtc); |
| 593 | } else if (stat & LCDC_PL_LOAD_DONE) { |
| 594 | tilcdc_clear_irqstatus(dev, stat); |
| 595 | } else { |
| 596 | struct drm_pending_vblank_event *event; |
| 597 | unsigned long flags; |
| 598 | uint32_t dirty = tilcdc_crtc->dirty & stat; |
| 599 | |
| 600 | tilcdc_clear_irqstatus(dev, stat); |
| 601 | |
| 602 | if (dirty & LCDC_END_OF_FRAME0) |
| 603 | set_scanout(crtc, 0); |
| 604 | |
| 605 | if (dirty & LCDC_END_OF_FRAME1) |
| 606 | set_scanout(crtc, 1); |
| 607 | |
| 608 | drm_handle_vblank(dev, 0); |
| 609 | |
| 610 | spin_lock_irqsave(&dev->event_lock, flags); |
| 611 | event = tilcdc_crtc->event; |
| 612 | tilcdc_crtc->event = NULL; |
| 613 | if (event) |
| 614 | drm_send_vblank_event(dev, 0, event); |
| 615 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 616 | |
| 617 | if (dirty && !tilcdc_crtc->dirty) |
| 618 | drm_vblank_put(dev, 0); |
| 619 | } |
| 620 | |
| 621 | if (priv->rev == 2) { |
| 622 | if (stat & LCDC_FRAME_DONE) { |
| 623 | tilcdc_crtc->frame_done = true; |
| 624 | wake_up(&tilcdc_crtc->frame_done_wq); |
| 625 | } |
| 626 | tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0); |
| 627 | } |
| 628 | |
| 629 | return IRQ_HANDLED; |
| 630 | } |
| 631 | |
| 632 | void tilcdc_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file) |
| 633 | { |
| 634 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 635 | struct drm_pending_vblank_event *event; |
| 636 | struct drm_device *dev = crtc->dev; |
| 637 | unsigned long flags; |
| 638 | |
| 639 | /* Destroy the pending vertical blanking event associated with the |
| 640 | * pending page flip, if any, and disable vertical blanking interrupts. |
| 641 | */ |
| 642 | spin_lock_irqsave(&dev->event_lock, flags); |
| 643 | event = tilcdc_crtc->event; |
| 644 | if (event && event->base.file_priv == file) { |
| 645 | tilcdc_crtc->event = NULL; |
| 646 | event->base.destroy(&event->base); |
| 647 | drm_vblank_put(dev, 0); |
| 648 | } |
| 649 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 650 | } |
| 651 | |
| 652 | struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev) |
| 653 | { |
| 654 | struct tilcdc_crtc *tilcdc_crtc; |
| 655 | struct drm_crtc *crtc; |
| 656 | int ret; |
| 657 | |
| 658 | tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL); |
| 659 | if (!tilcdc_crtc) { |
| 660 | dev_err(dev->dev, "allocation failed\n"); |
| 661 | return NULL; |
| 662 | } |
| 663 | |
| 664 | crtc = &tilcdc_crtc->base; |
| 665 | |
| 666 | tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF; |
| 667 | init_waitqueue_head(&tilcdc_crtc->frame_done_wq); |
| 668 | |
Boris BREZILLON | d7f8db5 | 2014-11-14 19:30:30 +0100 | [diff] [blame] | 669 | drm_flip_work_init(&tilcdc_crtc->unref_work, |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 670 | "unref", unref_worker); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 671 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 672 | ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs); |
| 673 | if (ret < 0) |
| 674 | goto fail; |
| 675 | |
| 676 | drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs); |
| 677 | |
| 678 | return crtc; |
| 679 | |
| 680 | fail: |
| 681 | tilcdc_crtc_destroy(crtc); |
| 682 | return NULL; |
| 683 | } |