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