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