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