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