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