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 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 24 | #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000 |
| 25 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 26 | struct tilcdc_crtc { |
| 27 | struct drm_crtc base; |
| 28 | |
Jyri Sarha | 47f571c | 2016-04-07 15:04:18 +0300 | [diff] [blame] | 29 | struct drm_plane primary; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 30 | const struct tilcdc_panel_info *info; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 31 | struct drm_pending_vblank_event *event; |
| 32 | int dpms; |
| 33 | wait_queue_head_t frame_done_wq; |
| 34 | bool frame_done; |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 35 | spinlock_t irq_lock; |
| 36 | |
| 37 | ktime_t last_vblank; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 38 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 39 | struct drm_framebuffer *curr_fb; |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 40 | struct drm_framebuffer *next_fb; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 41 | |
| 42 | /* for deferred fb unref's: */ |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 43 | struct drm_flip_work unref_work; |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 44 | |
| 45 | /* Only set if an external encoder is connected */ |
| 46 | bool simulate_vesa_sync; |
Jyri Sarha | 5895d08 | 2016-01-08 14:33:09 +0200 | [diff] [blame] | 47 | |
| 48 | int sync_lost_count; |
| 49 | bool frame_intact; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 50 | }; |
| 51 | #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base) |
| 52 | |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 53 | static void unref_worker(struct drm_flip_work *work, void *val) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 54 | { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 55 | struct tilcdc_crtc *tilcdc_crtc = |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 56 | container_of(work, struct tilcdc_crtc, unref_work); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 57 | struct drm_device *dev = tilcdc_crtc->base.dev; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 58 | |
| 59 | mutex_lock(&dev->mode_config.mutex); |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 60 | drm_framebuffer_unreference(val); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 61 | mutex_unlock(&dev->mode_config.mutex); |
| 62 | } |
| 63 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 64 | static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 65 | { |
| 66 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 67 | struct drm_device *dev = crtc->dev; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 68 | struct drm_gem_cma_object *gem; |
| 69 | unsigned int depth, bpp; |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 70 | dma_addr_t start, end; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 71 | |
| 72 | drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp); |
| 73 | gem = drm_fb_cma_get_gem_obj(fb, 0); |
| 74 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 75 | start = gem->paddr + fb->offsets[0] + |
| 76 | crtc->y * fb->pitches[0] + |
| 77 | crtc->x * bpp / 8; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 78 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 79 | end = start + (crtc->mode.vdisplay * fb->pitches[0]); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 80 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 81 | tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start); |
| 82 | tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end); |
| 83 | |
| 84 | if (tilcdc_crtc->curr_fb) |
| 85 | drm_flip_work_queue(&tilcdc_crtc->unref_work, |
| 86 | tilcdc_crtc->curr_fb); |
| 87 | |
| 88 | tilcdc_crtc->curr_fb = fb; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 89 | } |
| 90 | |
Tomi Valkeinen | 2efec4f | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 91 | static void reset(struct drm_crtc *crtc) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 92 | { |
| 93 | struct drm_device *dev = crtc->dev; |
| 94 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 95 | |
Tomi Valkeinen | 2efec4f | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 96 | if (priv->rev != 2) |
| 97 | return; |
| 98 | |
| 99 | tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET); |
| 100 | usleep_range(250, 1000); |
| 101 | tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET); |
| 102 | } |
| 103 | |
| 104 | static void start(struct drm_crtc *crtc) |
| 105 | { |
| 106 | struct drm_device *dev = crtc->dev; |
| 107 | |
| 108 | reset(crtc); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 109 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 110 | tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 111 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY)); |
| 112 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE); |
Jyri Sarha | d85f850e | 2016-06-15 11:16:23 +0300 | [diff] [blame] | 113 | |
| 114 | drm_crtc_vblank_on(crtc); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void stop(struct drm_crtc *crtc) |
| 118 | { |
Jyri Sarha | 2d5be88 | 2016-04-07 20:20:23 +0300 | [diff] [blame] | 119 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 120 | struct drm_device *dev = crtc->dev; |
Jyri Sarha | 2d5be88 | 2016-04-07 20:20:23 +0300 | [diff] [blame] | 121 | struct tilcdc_drm_private *priv = dev->dev_private; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 122 | |
Jyri Sarha | 2d5be88 | 2016-04-07 20:20:23 +0300 | [diff] [blame] | 123 | tilcdc_crtc->frame_done = false; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 124 | tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE); |
Jyri Sarha | 2d5be88 | 2016-04-07 20:20:23 +0300 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * if necessary wait for framedone irq which will still come |
| 128 | * before putting things to sleep.. |
| 129 | */ |
| 130 | if (priv->rev == 2) { |
| 131 | int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq, |
| 132 | tilcdc_crtc->frame_done, |
Jyri Sarha | 437c7d9 | 2016-06-16 16:19:17 +0300 | [diff] [blame] | 133 | msecs_to_jiffies(500)); |
Jyri Sarha | 2d5be88 | 2016-04-07 20:20:23 +0300 | [diff] [blame] | 134 | if (ret == 0) |
| 135 | dev_err(dev->dev, "%s: timeout waiting for framedone\n", |
| 136 | __func__); |
| 137 | } |
Jyri Sarha | d85f850e | 2016-06-15 11:16:23 +0300 | [diff] [blame] | 138 | |
| 139 | drm_crtc_vblank_off(crtc); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void tilcdc_crtc_destroy(struct drm_crtc *crtc) |
| 143 | { |
| 144 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 145 | |
Jyri Sarha | de9cb5f | 2015-02-26 10:12:41 +0200 | [diff] [blame] | 146 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 147 | |
Jyri Sarha | d66284fb | 2015-05-27 11:58:37 +0300 | [diff] [blame] | 148 | of_node_put(crtc->port); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 149 | drm_crtc_cleanup(crtc); |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 150 | drm_flip_work_cleanup(&tilcdc_crtc->unref_work); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 151 | } |
| 152 | |
Tomi Valkeinen | 6f206e9 | 2014-02-07 17:37:07 +0000 | [diff] [blame] | 153 | static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb) |
| 154 | { |
| 155 | struct drm_device *dev = crtc->dev; |
| 156 | unsigned int depth, bpp; |
| 157 | |
| 158 | drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp); |
| 159 | |
| 160 | if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) { |
| 161 | dev_err(dev->dev, |
| 162 | "Invalid pitch: fb and crtc widths must be the same"); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jyri Sarha | 8c65abb | 2016-04-07 14:56:32 +0300 | [diff] [blame] | 169 | int tilcdc_crtc_page_flip(struct drm_crtc *crtc, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 170 | struct drm_framebuffer *fb, |
Keith Packard | ed8d197 | 2013-07-22 18:49:58 -0700 | [diff] [blame] | 171 | struct drm_pending_vblank_event *event, |
| 172 | uint32_t page_flip_flags) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 173 | { |
| 174 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 175 | struct drm_device *dev = crtc->dev; |
Tomi Valkeinen | 6f206e9 | 2014-02-07 17:37:07 +0000 | [diff] [blame] | 176 | int r; |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 177 | unsigned long flags; |
Tomi Valkeinen | 6f206e9 | 2014-02-07 17:37:07 +0000 | [diff] [blame] | 178 | |
| 179 | r = tilcdc_verify_fb(crtc, fb); |
| 180 | if (r) |
| 181 | return r; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 182 | |
| 183 | if (tilcdc_crtc->event) { |
| 184 | dev_err(dev->dev, "already pending page flip!\n"); |
| 185 | return -EBUSY; |
| 186 | } |
| 187 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 188 | drm_framebuffer_reference(fb); |
| 189 | |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 190 | crtc->primary->fb = fb; |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 191 | |
| 192 | pm_runtime_get_sync(dev->dev); |
| 193 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 194 | spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags); |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 195 | |
Jyri Sarha | 0a1fe1b | 2016-06-13 09:53:36 +0300 | [diff] [blame] | 196 | if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) { |
| 197 | ktime_t next_vblank; |
| 198 | s64 tdiff; |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 199 | |
Jyri Sarha | 0a1fe1b | 2016-06-13 09:53:36 +0300 | [diff] [blame] | 200 | next_vblank = ktime_add_us(tilcdc_crtc->last_vblank, |
| 201 | 1000000 / crtc->hwmode.vrefresh); |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 202 | |
Jyri Sarha | 0a1fe1b | 2016-06-13 09:53:36 +0300 | [diff] [blame] | 203 | tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get())); |
| 204 | |
| 205 | if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US) |
| 206 | tilcdc_crtc->next_fb = fb; |
| 207 | } |
| 208 | |
| 209 | if (tilcdc_crtc->next_fb != fb) |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 210 | set_scanout(crtc, fb); |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 211 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 212 | tilcdc_crtc->event = event; |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 213 | |
| 214 | spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 215 | |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 216 | pm_runtime_put_sync(dev->dev); |
| 217 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
Darren Etheridge | 614b3cfe | 2014-09-25 00:59:32 +0000 | [diff] [blame] | 221 | void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 222 | { |
| 223 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 224 | struct drm_device *dev = crtc->dev; |
| 225 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 226 | |
| 227 | /* we really only care about on or off: */ |
| 228 | if (mode != DRM_MODE_DPMS_ON) |
| 229 | mode = DRM_MODE_DPMS_OFF; |
| 230 | |
| 231 | if (tilcdc_crtc->dpms == mode) |
| 232 | return; |
| 233 | |
| 234 | tilcdc_crtc->dpms = mode; |
| 235 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 236 | if (mode == DRM_MODE_DPMS_ON) { |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 237 | pm_runtime_get_sync(dev->dev); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 238 | start(crtc); |
| 239 | } else { |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 240 | stop(crtc); |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 241 | pm_runtime_put_sync(dev->dev); |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 242 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 243 | if (tilcdc_crtc->next_fb) { |
| 244 | drm_flip_work_queue(&tilcdc_crtc->unref_work, |
| 245 | tilcdc_crtc->next_fb); |
| 246 | tilcdc_crtc->next_fb = NULL; |
| 247 | } |
| 248 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 249 | if (tilcdc_crtc->curr_fb) { |
| 250 | drm_flip_work_queue(&tilcdc_crtc->unref_work, |
| 251 | tilcdc_crtc->curr_fb); |
| 252 | tilcdc_crtc->curr_fb = NULL; |
| 253 | } |
| 254 | |
| 255 | drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq); |
Jyri Sarha | 0a1fe1b | 2016-06-13 09:53:36 +0300 | [diff] [blame] | 256 | tilcdc_crtc->last_vblank = ktime_set(0, 0); |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 257 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 258 | } |
| 259 | |
Jyri Sarha | 8fe5616 | 2016-06-14 11:43:30 +0300 | [diff] [blame] | 260 | int tilcdc_crtc_current_dpms_state(struct drm_crtc *crtc) |
| 261 | { |
| 262 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 263 | |
| 264 | return tilcdc_crtc->dpms; |
| 265 | } |
| 266 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 267 | static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc, |
| 268 | const struct drm_display_mode *mode, |
| 269 | struct drm_display_mode *adjusted_mode) |
| 270 | { |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 271 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 272 | |
| 273 | if (!tilcdc_crtc->simulate_vesa_sync) |
| 274 | return true; |
| 275 | |
| 276 | /* |
| 277 | * tilcdc does not generate VESA-compliant sync but aligns |
| 278 | * VS on the second edge of HS instead of first edge. |
| 279 | * We use adjusted_mode, to fixup sync by aligning both rising |
| 280 | * edges and add HSKEW offset to fix the sync. |
| 281 | */ |
| 282 | adjusted_mode->hskew = mode->hsync_end - mode->hsync_start; |
| 283 | adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW; |
| 284 | |
| 285 | if (mode->flags & DRM_MODE_FLAG_NHSYNC) { |
| 286 | adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 287 | adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC; |
| 288 | } else { |
| 289 | adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 290 | adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC; |
| 291 | } |
| 292 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
| 296 | static void tilcdc_crtc_prepare(struct drm_crtc *crtc) |
| 297 | { |
| 298 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
| 299 | } |
| 300 | |
| 301 | static void tilcdc_crtc_commit(struct drm_crtc *crtc) |
| 302 | { |
| 303 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON); |
| 304 | } |
| 305 | |
Jyri Sarha | f6382f1 | 2016-04-07 15:09:50 +0300 | [diff] [blame^] | 306 | static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc) |
| 307 | { |
| 308 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 309 | struct drm_device *dev = crtc->dev; |
| 310 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 311 | const struct tilcdc_panel_info *info = tilcdc_crtc->info; |
| 312 | uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw; |
| 313 | struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
| 314 | struct drm_framebuffer *fb = crtc->primary->state->fb; |
| 315 | |
| 316 | if (WARN_ON(!info)) |
| 317 | return; |
| 318 | |
| 319 | if (WARN_ON(!fb)) |
| 320 | return; |
| 321 | |
| 322 | pm_runtime_get_sync(dev->dev); |
| 323 | |
| 324 | /* Configure the Burst Size and fifo threshold of DMA: */ |
| 325 | reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770; |
| 326 | switch (info->dma_burst_sz) { |
| 327 | case 1: |
| 328 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1); |
| 329 | break; |
| 330 | case 2: |
| 331 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2); |
| 332 | break; |
| 333 | case 4: |
| 334 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4); |
| 335 | break; |
| 336 | case 8: |
| 337 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8); |
| 338 | break; |
| 339 | case 16: |
| 340 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16); |
| 341 | break; |
| 342 | default: |
| 343 | dev_err(dev->dev, "invalid burst size\n"); |
| 344 | return; |
| 345 | } |
| 346 | reg |= (info->fifo_th << 8); |
| 347 | tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg); |
| 348 | |
| 349 | /* Configure timings: */ |
| 350 | hbp = mode->htotal - mode->hsync_end; |
| 351 | hfp = mode->hsync_start - mode->hdisplay; |
| 352 | hsw = mode->hsync_end - mode->hsync_start; |
| 353 | vbp = mode->vtotal - mode->vsync_end; |
| 354 | vfp = mode->vsync_start - mode->vdisplay; |
| 355 | vsw = mode->vsync_end - mode->vsync_start; |
| 356 | |
| 357 | DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u", |
| 358 | mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw); |
| 359 | |
| 360 | /* Set AC Bias Period and Number of Transitions per Interrupt: */ |
| 361 | reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00; |
| 362 | reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) | |
| 363 | LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt); |
| 364 | |
| 365 | /* |
| 366 | * subtract one from hfp, hbp, hsw because the hardware uses |
| 367 | * a value of 0 as 1 |
| 368 | */ |
| 369 | if (priv->rev == 2) { |
| 370 | /* clear bits we're going to set */ |
| 371 | reg &= ~0x78000033; |
| 372 | reg |= ((hfp-1) & 0x300) >> 8; |
| 373 | reg |= ((hbp-1) & 0x300) >> 4; |
| 374 | reg |= ((hsw-1) & 0x3c0) << 21; |
| 375 | } |
| 376 | tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg); |
| 377 | |
| 378 | reg = (((mode->hdisplay >> 4) - 1) << 4) | |
| 379 | (((hbp-1) & 0xff) << 24) | |
| 380 | (((hfp-1) & 0xff) << 16) | |
| 381 | (((hsw-1) & 0x3f) << 10); |
| 382 | if (priv->rev == 2) |
| 383 | reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3; |
| 384 | tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg); |
| 385 | |
| 386 | reg = ((mode->vdisplay - 1) & 0x3ff) | |
| 387 | ((vbp & 0xff) << 24) | |
| 388 | ((vfp & 0xff) << 16) | |
| 389 | (((vsw-1) & 0x3f) << 10); |
| 390 | tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg); |
| 391 | |
| 392 | /* |
| 393 | * be sure to set Bit 10 for the V2 LCDC controller, |
| 394 | * otherwise limited to 1024 pixels width, stopping |
| 395 | * 1920x1080 being supported. |
| 396 | */ |
| 397 | if (priv->rev == 2) { |
| 398 | if ((mode->vdisplay - 1) & 0x400) { |
| 399 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, |
| 400 | LCDC_LPP_B10); |
| 401 | } else { |
| 402 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, |
| 403 | LCDC_LPP_B10); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /* Configure display type: */ |
| 408 | reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) & |
| 409 | ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE | |
| 410 | LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | |
| 411 | 0x000ff000 /* Palette Loading Delay bits */); |
| 412 | reg |= LCDC_TFT_MODE; /* no monochrome/passive support */ |
| 413 | if (info->tft_alt_mode) |
| 414 | reg |= LCDC_TFT_ALT_ENABLE; |
| 415 | if (priv->rev == 2) { |
| 416 | unsigned int depth, bpp; |
| 417 | |
| 418 | drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp); |
| 419 | switch (bpp) { |
| 420 | case 16: |
| 421 | break; |
| 422 | case 32: |
| 423 | reg |= LCDC_V2_TFT_24BPP_UNPACK; |
| 424 | /* fallthrough */ |
| 425 | case 24: |
| 426 | reg |= LCDC_V2_TFT_24BPP_MODE; |
| 427 | break; |
| 428 | default: |
| 429 | dev_err(dev->dev, "invalid pixel format\n"); |
| 430 | return; |
| 431 | } |
| 432 | } |
| 433 | reg |= info->fdd < 12; |
| 434 | tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg); |
| 435 | |
| 436 | if (info->invert_pxl_clk) |
| 437 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK); |
| 438 | else |
| 439 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK); |
| 440 | |
| 441 | if (info->sync_ctrl) |
| 442 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL); |
| 443 | else |
| 444 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL); |
| 445 | |
| 446 | if (info->sync_edge) |
| 447 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE); |
| 448 | else |
| 449 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE); |
| 450 | |
| 451 | if (mode->flags & DRM_MODE_FLAG_NHSYNC) |
| 452 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC); |
| 453 | else |
| 454 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC); |
| 455 | |
| 456 | if (mode->flags & DRM_MODE_FLAG_NVSYNC) |
| 457 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC); |
| 458 | else |
| 459 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC); |
| 460 | |
| 461 | if (info->raster_order) |
| 462 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER); |
| 463 | else |
| 464 | tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER); |
| 465 | |
| 466 | drm_framebuffer_reference(fb); |
| 467 | |
| 468 | set_scanout(crtc, fb); |
| 469 | |
| 470 | tilcdc_crtc_update_clk(crtc); |
| 471 | |
| 472 | pm_runtime_put_sync(dev->dev); |
| 473 | |
| 474 | crtc->hwmode = crtc->state->adjusted_mode; |
| 475 | } |
| 476 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 477 | static int tilcdc_crtc_mode_set(struct drm_crtc *crtc, |
| 478 | struct drm_display_mode *mode, |
| 479 | struct drm_display_mode *adjusted_mode, |
| 480 | int x, int y, |
| 481 | struct drm_framebuffer *old_fb) |
| 482 | { |
| 483 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 484 | struct drm_device *dev = crtc->dev; |
| 485 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 486 | const struct tilcdc_panel_info *info = tilcdc_crtc->info; |
| 487 | uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw; |
| 488 | int ret; |
| 489 | |
| 490 | ret = tilcdc_crtc_mode_valid(crtc, mode); |
| 491 | if (WARN_ON(ret)) |
| 492 | return ret; |
| 493 | |
| 494 | if (WARN_ON(!info)) |
| 495 | return -EINVAL; |
| 496 | |
Tomi Valkeinen | 6f206e9 | 2014-02-07 17:37:07 +0000 | [diff] [blame] | 497 | ret = tilcdc_verify_fb(crtc, crtc->primary->fb); |
| 498 | if (ret) |
| 499 | return ret; |
| 500 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 501 | pm_runtime_get_sync(dev->dev); |
| 502 | |
| 503 | /* Configure the Burst Size and fifo threshold of DMA: */ |
| 504 | reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770; |
| 505 | switch (info->dma_burst_sz) { |
| 506 | case 1: |
| 507 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1); |
| 508 | break; |
| 509 | case 2: |
| 510 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2); |
| 511 | break; |
| 512 | case 4: |
| 513 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4); |
| 514 | break; |
| 515 | case 8: |
| 516 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8); |
| 517 | break; |
| 518 | case 16: |
| 519 | reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16); |
| 520 | break; |
| 521 | default: |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | reg |= (info->fifo_th << 8); |
| 525 | tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg); |
| 526 | |
| 527 | /* Configure timings: */ |
| 528 | hbp = mode->htotal - mode->hsync_end; |
| 529 | hfp = mode->hsync_start - mode->hdisplay; |
| 530 | hsw = mode->hsync_end - mode->hsync_start; |
| 531 | vbp = mode->vtotal - mode->vsync_end; |
| 532 | vfp = mode->vsync_start - mode->vdisplay; |
| 533 | vsw = mode->vsync_end - mode->vsync_start; |
| 534 | |
| 535 | DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u", |
| 536 | mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw); |
| 537 | |
| 538 | /* Configure the AC Bias Period and Number of Transitions per Interrupt: */ |
| 539 | reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00; |
| 540 | reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) | |
| 541 | LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt); |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 542 | |
| 543 | /* |
| 544 | * subtract one from hfp, hbp, hsw because the hardware uses |
| 545 | * a value of 0 as 1 |
| 546 | */ |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 547 | if (priv->rev == 2) { |
Pantelis Antoniou | c19b3e2 | 2013-06-21 13:52:28 -0500 | [diff] [blame] | 548 | /* clear bits we're going to set */ |
| 549 | reg &= ~0x78000033; |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 550 | reg |= ((hfp-1) & 0x300) >> 8; |
| 551 | reg |= ((hbp-1) & 0x300) >> 4; |
| 552 | reg |= ((hsw-1) & 0x3c0) << 21; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 553 | } |
| 554 | tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg); |
| 555 | |
| 556 | reg = (((mode->hdisplay >> 4) - 1) << 4) | |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 557 | (((hbp-1) & 0xff) << 24) | |
| 558 | (((hfp-1) & 0xff) << 16) | |
| 559 | (((hsw-1) & 0x3f) << 10); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 560 | if (priv->rev == 2) |
| 561 | reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3; |
| 562 | tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg); |
| 563 | |
| 564 | reg = ((mode->vdisplay - 1) & 0x3ff) | |
| 565 | ((vbp & 0xff) << 24) | |
| 566 | ((vfp & 0xff) << 16) | |
Darren Etheridge | db2b4bd | 2013-06-21 13:52:24 -0500 | [diff] [blame] | 567 | (((vsw-1) & 0x3f) << 10); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 568 | tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg); |
| 569 | |
Darren Etheridge | 6bf02c6 | 2013-06-21 13:52:22 -0500 | [diff] [blame] | 570 | /* |
| 571 | * be sure to set Bit 10 for the V2 LCDC controller, |
| 572 | * otherwise limited to 1024 pixels width, stopping |
| 573 | * 1920x1080 being suppoted. |
| 574 | */ |
| 575 | if (priv->rev == 2) { |
| 576 | if ((mode->vdisplay - 1) & 0x400) { |
| 577 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, |
| 578 | LCDC_LPP_B10); |
| 579 | } else { |
| 580 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, |
| 581 | LCDC_LPP_B10); |
| 582 | } |
| 583 | } |
| 584 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 585 | /* Configure display type: */ |
| 586 | reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) & |
| 587 | ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE | |
| 588 | LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000); |
| 589 | reg |= LCDC_TFT_MODE; /* no monochrome/passive support */ |
| 590 | if (info->tft_alt_mode) |
| 591 | reg |= LCDC_TFT_ALT_ENABLE; |
| 592 | if (priv->rev == 2) { |
| 593 | unsigned int depth, bpp; |
| 594 | |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 595 | drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 596 | switch (bpp) { |
| 597 | case 16: |
| 598 | break; |
| 599 | case 32: |
| 600 | reg |= LCDC_V2_TFT_24BPP_UNPACK; |
| 601 | /* fallthrough */ |
| 602 | case 24: |
| 603 | reg |= LCDC_V2_TFT_24BPP_MODE; |
| 604 | break; |
| 605 | default: |
| 606 | dev_err(dev->dev, "invalid pixel format\n"); |
| 607 | return -EINVAL; |
| 608 | } |
| 609 | } |
| 610 | reg |= info->fdd < 12; |
| 611 | tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg); |
| 612 | |
| 613 | if (info->invert_pxl_clk) |
| 614 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK); |
| 615 | else |
| 616 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK); |
| 617 | |
| 618 | if (info->sync_ctrl) |
| 619 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL); |
| 620 | else |
| 621 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL); |
| 622 | |
| 623 | if (info->sync_edge) |
| 624 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE); |
| 625 | else |
| 626 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE); |
| 627 | |
Darren Etheridge | a976718 | 2013-08-14 21:43:33 +0200 | [diff] [blame] | 628 | /* |
| 629 | * use value from adjusted_mode here as this might have been |
| 630 | * changed as part of the fixup for slave encoders to solve the |
| 631 | * issue where tilcdc timings are not VESA compliant |
| 632 | */ |
| 633 | if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 634 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC); |
| 635 | else |
| 636 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC); |
| 637 | |
| 638 | if (mode->flags & DRM_MODE_FLAG_NVSYNC) |
| 639 | tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC); |
| 640 | else |
| 641 | tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC); |
| 642 | |
| 643 | if (info->raster_order) |
| 644 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER); |
| 645 | else |
| 646 | tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER); |
| 647 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 648 | drm_framebuffer_reference(crtc->primary->fb); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 649 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 650 | set_scanout(crtc, crtc->primary->fb); |
| 651 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 652 | tilcdc_crtc_update_clk(crtc); |
| 653 | |
| 654 | pm_runtime_put_sync(dev->dev); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, |
| 660 | struct drm_framebuffer *old_fb) |
| 661 | { |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 662 | struct drm_device *dev = crtc->dev; |
Tomi Valkeinen | 6f206e9 | 2014-02-07 17:37:07 +0000 | [diff] [blame] | 663 | int r; |
| 664 | |
| 665 | r = tilcdc_verify_fb(crtc, crtc->primary->fb); |
| 666 | if (r) |
| 667 | return r; |
| 668 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 669 | drm_framebuffer_reference(crtc->primary->fb); |
| 670 | |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 671 | pm_runtime_get_sync(dev->dev); |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 672 | |
| 673 | set_scanout(crtc, crtc->primary->fb); |
| 674 | |
Tomi Valkeinen | 65734a2 | 2015-10-19 12:30:03 +0300 | [diff] [blame] | 675 | pm_runtime_put_sync(dev->dev); |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 676 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 677 | return 0; |
| 678 | } |
| 679 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 680 | static const struct drm_crtc_funcs tilcdc_crtc_funcs = { |
| 681 | .destroy = tilcdc_crtc_destroy, |
| 682 | .set_config = drm_crtc_helper_set_config, |
| 683 | .page_flip = tilcdc_crtc_page_flip, |
| 684 | }; |
| 685 | |
| 686 | static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = { |
| 687 | .dpms = tilcdc_crtc_dpms, |
| 688 | .mode_fixup = tilcdc_crtc_mode_fixup, |
| 689 | .prepare = tilcdc_crtc_prepare, |
| 690 | .commit = tilcdc_crtc_commit, |
| 691 | .mode_set = tilcdc_crtc_mode_set, |
| 692 | .mode_set_base = tilcdc_crtc_mode_set_base, |
Jyri Sarha | f6382f1 | 2016-04-07 15:09:50 +0300 | [diff] [blame^] | 693 | .mode_set_nofb = tilcdc_crtc_mode_set_nofb, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 694 | }; |
| 695 | |
| 696 | int tilcdc_crtc_max_width(struct drm_crtc *crtc) |
| 697 | { |
| 698 | struct drm_device *dev = crtc->dev; |
| 699 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 700 | int max_width = 0; |
| 701 | |
| 702 | if (priv->rev == 1) |
| 703 | max_width = 1024; |
| 704 | else if (priv->rev == 2) |
| 705 | max_width = 2048; |
| 706 | |
| 707 | return max_width; |
| 708 | } |
| 709 | |
| 710 | int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode) |
| 711 | { |
| 712 | struct tilcdc_drm_private *priv = crtc->dev->dev_private; |
| 713 | unsigned int bandwidth; |
Darren Etheridge | e1c5d0a | 2013-06-21 13:52:25 -0500 | [diff] [blame] | 714 | uint32_t hbp, hfp, hsw, vbp, vfp, vsw; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 715 | |
Darren Etheridge | e1c5d0a | 2013-06-21 13:52:25 -0500 | [diff] [blame] | 716 | /* |
| 717 | * check to see if the width is within the range that |
| 718 | * the LCD Controller physically supports |
| 719 | */ |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 720 | if (mode->hdisplay > tilcdc_crtc_max_width(crtc)) |
| 721 | return MODE_VIRTUAL_X; |
| 722 | |
| 723 | /* width must be multiple of 16 */ |
| 724 | if (mode->hdisplay & 0xf) |
| 725 | return MODE_VIRTUAL_X; |
| 726 | |
| 727 | if (mode->vdisplay > 2048) |
| 728 | return MODE_VIRTUAL_Y; |
| 729 | |
Darren Etheridge | e1c5d0a | 2013-06-21 13:52:25 -0500 | [diff] [blame] | 730 | DBG("Processing mode %dx%d@%d with pixel clock %d", |
| 731 | mode->hdisplay, mode->vdisplay, |
| 732 | drm_mode_vrefresh(mode), mode->clock); |
| 733 | |
| 734 | hbp = mode->htotal - mode->hsync_end; |
| 735 | hfp = mode->hsync_start - mode->hdisplay; |
| 736 | hsw = mode->hsync_end - mode->hsync_start; |
| 737 | vbp = mode->vtotal - mode->vsync_end; |
| 738 | vfp = mode->vsync_start - mode->vdisplay; |
| 739 | vsw = mode->vsync_end - mode->vsync_start; |
| 740 | |
| 741 | if ((hbp-1) & ~0x3ff) { |
| 742 | DBG("Pruning mode: Horizontal Back Porch out of range"); |
| 743 | return MODE_HBLANK_WIDE; |
| 744 | } |
| 745 | |
| 746 | if ((hfp-1) & ~0x3ff) { |
| 747 | DBG("Pruning mode: Horizontal Front Porch out of range"); |
| 748 | return MODE_HBLANK_WIDE; |
| 749 | } |
| 750 | |
| 751 | if ((hsw-1) & ~0x3ff) { |
| 752 | DBG("Pruning mode: Horizontal Sync Width out of range"); |
| 753 | return MODE_HSYNC_WIDE; |
| 754 | } |
| 755 | |
| 756 | if (vbp & ~0xff) { |
| 757 | DBG("Pruning mode: Vertical Back Porch out of range"); |
| 758 | return MODE_VBLANK_WIDE; |
| 759 | } |
| 760 | |
| 761 | if (vfp & ~0xff) { |
| 762 | DBG("Pruning mode: Vertical Front Porch out of range"); |
| 763 | return MODE_VBLANK_WIDE; |
| 764 | } |
| 765 | |
| 766 | if ((vsw-1) & ~0x3f) { |
| 767 | DBG("Pruning mode: Vertical Sync Width out of range"); |
| 768 | return MODE_VSYNC_WIDE; |
| 769 | } |
| 770 | |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 771 | /* |
| 772 | * some devices have a maximum allowed pixel clock |
| 773 | * configured from the DT |
| 774 | */ |
| 775 | if (mode->clock > priv->max_pixelclock) { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 776 | DBG("Pruning mode: pixel clock too high"); |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 777 | return MODE_CLOCK_HIGH; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * some devices further limit the max horizontal resolution |
| 782 | * configured from the DT |
| 783 | */ |
| 784 | if (mode->hdisplay > priv->max_width) |
| 785 | return MODE_BAD_WIDTH; |
| 786 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 787 | /* filter out modes that would require too much memory bandwidth: */ |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 788 | bandwidth = mode->hdisplay * mode->vdisplay * |
| 789 | drm_mode_vrefresh(mode); |
| 790 | if (bandwidth > priv->max_bandwidth) { |
Darren Etheridge | f7b4575 | 2013-06-21 13:52:26 -0500 | [diff] [blame] | 791 | DBG("Pruning mode: exceeds defined bandwidth limit"); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 792 | return MODE_BAD; |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 793 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 794 | |
| 795 | return MODE_OK; |
| 796 | } |
| 797 | |
| 798 | void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc, |
| 799 | const struct tilcdc_panel_info *info) |
| 800 | { |
| 801 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 802 | tilcdc_crtc->info = info; |
| 803 | } |
| 804 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 805 | void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc, |
| 806 | bool simulate_vesa_sync) |
| 807 | { |
| 808 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 809 | |
| 810 | tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync; |
| 811 | } |
| 812 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 813 | void tilcdc_crtc_update_clk(struct drm_crtc *crtc) |
| 814 | { |
| 815 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 816 | struct drm_device *dev = crtc->dev; |
| 817 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 818 | int dpms = tilcdc_crtc->dpms; |
Darren Etheridge | 3d19306 | 2014-01-15 15:52:36 -0600 | [diff] [blame] | 819 | unsigned long lcd_clk; |
| 820 | const unsigned clkdiv = 2; /* using a fixed divider of 2 */ |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 821 | int ret; |
| 822 | |
| 823 | pm_runtime_get_sync(dev->dev); |
| 824 | |
| 825 | if (dpms == DRM_MODE_DPMS_ON) |
| 826 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); |
| 827 | |
Darren Etheridge | 3d19306 | 2014-01-15 15:52:36 -0600 | [diff] [blame] | 828 | /* mode.clock is in KHz, set_rate wants parameter in Hz */ |
| 829 | ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv); |
| 830 | if (ret < 0) { |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 831 | dev_err(dev->dev, "failed to set display clock rate to: %d\n", |
| 832 | crtc->mode.clock); |
| 833 | goto out; |
| 834 | } |
| 835 | |
| 836 | lcd_clk = clk_get_rate(priv->clk); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 837 | |
Darren Etheridge | 3d19306 | 2014-01-15 15:52:36 -0600 | [diff] [blame] | 838 | DBG("lcd_clk=%lu, mode clock=%d, div=%u", |
| 839 | lcd_clk, crtc->mode.clock, clkdiv); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 840 | |
| 841 | /* Configure the LCD clock divisor. */ |
Darren Etheridge | 3d19306 | 2014-01-15 15:52:36 -0600 | [diff] [blame] | 842 | tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 843 | LCDC_RASTER_MODE); |
| 844 | |
| 845 | if (priv->rev == 2) |
| 846 | tilcdc_set(dev, LCDC_CLK_ENABLE_REG, |
| 847 | LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN | |
| 848 | LCDC_V2_CORE_CLK_EN); |
| 849 | |
| 850 | if (dpms == DRM_MODE_DPMS_ON) |
| 851 | tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON); |
| 852 | |
| 853 | out: |
| 854 | pm_runtime_put_sync(dev->dev); |
| 855 | } |
| 856 | |
Jyri Sarha | 5895d08 | 2016-01-08 14:33:09 +0200 | [diff] [blame] | 857 | #define SYNC_LOST_COUNT_LIMIT 50 |
| 858 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 859 | irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc) |
| 860 | { |
| 861 | struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); |
| 862 | struct drm_device *dev = crtc->dev; |
| 863 | struct tilcdc_drm_private *priv = dev->dev_private; |
Tomi Valkeinen | 317aae7 | 2015-10-20 12:08:03 +0300 | [diff] [blame] | 864 | uint32_t stat; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 865 | |
Tomi Valkeinen | 317aae7 | 2015-10-20 12:08:03 +0300 | [diff] [blame] | 866 | stat = tilcdc_read_irqstatus(dev); |
| 867 | tilcdc_clear_irqstatus(dev, stat); |
| 868 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 869 | if (stat & LCDC_END_OF_FRAME0) { |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 870 | unsigned long flags; |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 871 | bool skip_event = false; |
| 872 | ktime_t now; |
| 873 | |
| 874 | now = ktime_get(); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 875 | |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 876 | drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 877 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 878 | spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 879 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 880 | tilcdc_crtc->last_vblank = now; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 881 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 882 | if (tilcdc_crtc->next_fb) { |
| 883 | set_scanout(crtc, tilcdc_crtc->next_fb); |
| 884 | tilcdc_crtc->next_fb = NULL; |
| 885 | skip_event = true; |
Tomi Valkeinen | 2b2080d7 | 2015-10-20 09:37:27 +0300 | [diff] [blame] | 886 | } |
| 887 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 888 | spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags); |
| 889 | |
Gustavo Padovan | 099ede8 | 2016-07-04 21:04:52 -0300 | [diff] [blame] | 890 | drm_crtc_handle_vblank(crtc); |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 891 | |
| 892 | if (!skip_event) { |
| 893 | struct drm_pending_vblank_event *event; |
| 894 | |
| 895 | spin_lock_irqsave(&dev->event_lock, flags); |
| 896 | |
| 897 | event = tilcdc_crtc->event; |
| 898 | tilcdc_crtc->event = NULL; |
| 899 | if (event) |
Gustavo Padovan | dfebc15 | 2016-04-14 10:48:22 -0700 | [diff] [blame] | 900 | drm_crtc_send_vblank_event(crtc, event); |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 901 | |
| 902 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 903 | } |
Jyri Sarha | 5895d08 | 2016-01-08 14:33:09 +0200 | [diff] [blame] | 904 | |
| 905 | if (tilcdc_crtc->frame_intact) |
| 906 | tilcdc_crtc->sync_lost_count = 0; |
| 907 | else |
| 908 | tilcdc_crtc->frame_intact = true; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 909 | } |
| 910 | |
Jyri Sarha | 1494411 | 2016-04-07 20:36:48 +0300 | [diff] [blame] | 911 | if (stat & LCDC_FIFO_UNDERFLOW) |
| 912 | dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow", |
| 913 | __func__, stat); |
| 914 | |
| 915 | /* For revision 2 only */ |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 916 | if (priv->rev == 2) { |
| 917 | if (stat & LCDC_FRAME_DONE) { |
| 918 | tilcdc_crtc->frame_done = true; |
| 919 | wake_up(&tilcdc_crtc->frame_done_wq); |
| 920 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 921 | |
Jyri Sarha | 1abcdac | 2016-06-17 11:54:06 +0300 | [diff] [blame] | 922 | if (stat & LCDC_SYNC_LOST) { |
| 923 | dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost", |
| 924 | __func__, stat); |
| 925 | tilcdc_crtc->frame_intact = false; |
| 926 | if (tilcdc_crtc->sync_lost_count++ > |
| 927 | SYNC_LOST_COUNT_LIMIT) { |
| 928 | dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat); |
| 929 | tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG, |
| 930 | LCDC_SYNC_LOST); |
| 931 | } |
Jyri Sarha | 5895d08 | 2016-01-08 14:33:09 +0200 | [diff] [blame] | 932 | } |
Jyri Sarha | c0c2baa | 2015-12-18 13:07:52 +0200 | [diff] [blame] | 933 | |
Jyri Sarha | 1494411 | 2016-04-07 20:36:48 +0300 | [diff] [blame] | 934 | /* Indicate to LCDC that the interrupt service routine has |
| 935 | * completed, see 13.3.6.1.6 in AM335x TRM. |
| 936 | */ |
| 937 | tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0); |
| 938 | } |
Jyri Sarha | c0c2baa | 2015-12-18 13:07:52 +0200 | [diff] [blame] | 939 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 940 | return IRQ_HANDLED; |
| 941 | } |
| 942 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 943 | struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev) |
| 944 | { |
Jyri Sarha | d66284fb | 2015-05-27 11:58:37 +0300 | [diff] [blame] | 945 | struct tilcdc_drm_private *priv = dev->dev_private; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 946 | struct tilcdc_crtc *tilcdc_crtc; |
| 947 | struct drm_crtc *crtc; |
| 948 | int ret; |
| 949 | |
Jyri Sarha | d0ec32c | 2016-02-23 12:44:27 +0200 | [diff] [blame] | 950 | tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 951 | if (!tilcdc_crtc) { |
| 952 | dev_err(dev->dev, "allocation failed\n"); |
| 953 | return NULL; |
| 954 | } |
| 955 | |
| 956 | crtc = &tilcdc_crtc->base; |
| 957 | |
Jyri Sarha | 47f571c | 2016-04-07 15:04:18 +0300 | [diff] [blame] | 958 | ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary); |
| 959 | if (ret < 0) |
| 960 | goto fail; |
| 961 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 962 | tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF; |
| 963 | init_waitqueue_head(&tilcdc_crtc->frame_done_wq); |
| 964 | |
Boris BREZILLON | d7f8db5 | 2014-11-14 19:30:30 +0100 | [diff] [blame] | 965 | drm_flip_work_init(&tilcdc_crtc->unref_work, |
Rob Clark | a464d61 | 2013-08-07 13:41:20 -0400 | [diff] [blame] | 966 | "unref", unref_worker); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 967 | |
Tomi Valkeinen | 2b3a8cd | 2015-11-03 12:00:51 +0200 | [diff] [blame] | 968 | spin_lock_init(&tilcdc_crtc->irq_lock); |
| 969 | |
Jyri Sarha | 47f571c | 2016-04-07 15:04:18 +0300 | [diff] [blame] | 970 | ret = drm_crtc_init_with_planes(dev, crtc, |
| 971 | &tilcdc_crtc->primary, |
| 972 | NULL, |
| 973 | &tilcdc_crtc_funcs, |
| 974 | "tilcdc crtc"); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 975 | if (ret < 0) |
| 976 | goto fail; |
| 977 | |
| 978 | drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs); |
| 979 | |
Jyri Sarha | d66284fb | 2015-05-27 11:58:37 +0300 | [diff] [blame] | 980 | if (priv->is_componentized) { |
| 981 | struct device_node *ports = |
| 982 | of_get_child_by_name(dev->dev->of_node, "ports"); |
| 983 | |
| 984 | if (ports) { |
| 985 | crtc->port = of_get_child_by_name(ports, "port"); |
| 986 | of_node_put(ports); |
| 987 | } else { |
| 988 | crtc->port = |
| 989 | of_get_child_by_name(dev->dev->of_node, "port"); |
| 990 | } |
| 991 | if (!crtc->port) { /* This should never happen */ |
| 992 | dev_err(dev->dev, "Port node not found in %s\n", |
| 993 | dev->dev->of_node->full_name); |
| 994 | goto fail; |
| 995 | } |
| 996 | } |
| 997 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 998 | return crtc; |
| 999 | |
| 1000 | fail: |
| 1001 | tilcdc_crtc_destroy(crtc); |
| 1002 | return NULL; |
| 1003 | } |