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