blob: 1ed65dd5befe212e13a92f2a6841315b288e17a4 [file] [log] [blame]
Rob Clark16ea9752013-01-08 15:04:28 -06001/*
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 Paulce2f2c32016-09-21 06:14:53 -070018#include <drm/drm_atomic.h>
Jyri Sarha305198d2016-04-07 15:05:16 +030019#include <drm/drm_atomic_helper.h>
Sean Paulce2f2c32016-09-21 06:14:53 -070020#include <drm/drm_crtc.h>
21#include <drm/drm_flip_work.h>
22#include <drm/drm_plane_helper.h>
Jyri Sarha4e910c72016-09-06 22:55:33 +030023#include <linux/workqueue.h>
Bartosz Golaszewski93452352016-10-31 15:19:26 +010024#include <linux/completion.h>
25#include <linux/dma-mapping.h>
Rob Clark16ea9752013-01-08 15:04:28 -060026
27#include "tilcdc_drv.h"
28#include "tilcdc_regs.h"
29
Bartosz Golaszewski93452352016-10-31 15:19:26 +010030#define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
31#define TILCDC_REV1_PALETTE_SIZE 32
32#define TILCDC_REV1_PALETTE_FIRST_ENTRY 0x4000
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020033
Rob Clark16ea9752013-01-08 15:04:28 -060034struct tilcdc_crtc {
35 struct drm_crtc base;
36
Jyri Sarha47f571c2016-04-07 15:04:18 +030037 struct drm_plane primary;
Rob Clark16ea9752013-01-08 15:04:28 -060038 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060039 struct drm_pending_vblank_event *event;
Jyri Sarha2d53a182016-10-25 12:27:31 +030040 struct mutex enable_lock;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +030041 bool enabled;
Jyri Sarha2d53a182016-10-25 12:27:31 +030042 bool shutdown;
Rob Clark16ea9752013-01-08 15:04:28 -060043 wait_queue_head_t frame_done_wq;
44 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020045 spinlock_t irq_lock;
46
Jyri Sarha642e5162016-09-06 16:19:54 +030047 unsigned int lcd_fck_rate;
48
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020049 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060050
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030051 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020052 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060053
54 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040055 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020056
57 /* Only set if an external encoder is connected */
58 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020059
60 int sync_lost_count;
61 bool frame_intact;
Jyri Sarha13b3d722016-04-06 14:02:38 +030062 struct work_struct recover_work;
Bartosz Golaszewski93452352016-10-31 15:19:26 +010063
64 dma_addr_t palette_dma_handle;
65 void *palette_base;
66 struct completion palette_loaded;
Rob Clark16ea9752013-01-08 15:04:28 -060067};
68#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
69
Rob Clarka464d612013-08-07 13:41:20 -040070static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060071{
Darren Etheridgef7b45752013-06-21 13:52:26 -050072 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040073 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060074 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060075
76 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040077 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060078 mutex_unlock(&dev->mode_config.mutex);
79}
80
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030081static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060082{
83 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
84 struct drm_device *dev = crtc->dev;
Daniel Schultz4c268d62016-10-28 13:52:41 +020085 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -060086 struct drm_gem_cma_object *gem;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030087 dma_addr_t start, end;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030088 u64 dma_base_and_ceiling;
Rob Clark16ea9752013-01-08 15:04:28 -060089
Rob Clark16ea9752013-01-08 15:04:28 -060090 gem = drm_fb_cma_get_gem_obj(fb, 0);
91
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030092 start = gem->paddr + fb->offsets[0] +
93 crtc->y * fb->pitches[0] +
Laurent Pinchart59f11a42016-10-18 01:41:14 +030094 crtc->x * drm_format_plane_cpp(fb->pixel_format, 0);
Rob Clark16ea9752013-01-08 15:04:28 -060095
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030096 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060097
Jyri Sarha7eb9f062016-08-26 15:10:14 +030098 /* 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 Schultz4c268d62016-10-28 13:52:41 +0200103 if (priv->rev == 1)
104 end -= 1;
105
106 dma_base_and_ceiling = (u64)end << 32 | start;
Jyri Sarha7eb9f062016-08-26 15:10:14 +0300107 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300108
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 Clark16ea9752013-01-08 15:04:28 -0600114}
115
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100116/*
117 * The driver currently only supports the RGB565 format for revision 1. For
118 * 16 bits-per-pixel the palette block is bypassed, but the first 32 bytes of
119 * the framebuffer are still considered palette. The first 16-bit entry must
120 * be 0x4000 while all other entries must be zeroed.
121 */
122static void tilcdc_crtc_load_palette(struct drm_crtc *crtc)
123{
124 u32 dma_fb_base, dma_fb_ceiling, raster_ctl;
125 struct tilcdc_crtc *tilcdc_crtc;
126 struct drm_device *dev;
127 u16 *first_entry;
128
129 dev = crtc->dev;
130 tilcdc_crtc = to_tilcdc_crtc(crtc);
131 first_entry = tilcdc_crtc->palette_base;
132
133 *first_entry = TILCDC_REV1_PALETTE_FIRST_ENTRY;
134
135 dma_fb_base = tilcdc_read(dev, LCDC_DMA_FB_BASE_ADDR_0_REG);
136 dma_fb_ceiling = tilcdc_read(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG);
137 raster_ctl = tilcdc_read(dev, LCDC_RASTER_CTRL_REG);
138
139 /* Tell the LCDC where the palette is located. */
140 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG,
141 tilcdc_crtc->palette_dma_handle);
142 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG,
143 (u32)tilcdc_crtc->palette_dma_handle
144 + TILCDC_REV1_PALETTE_SIZE - 1);
145
146 /* Load it. */
147 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
148 LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
149 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
150 LCDC_PALETTE_LOAD_MODE(PALETTE_ONLY));
151
152 /* Enable the LCDC and wait for palette to be loaded. */
153 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
154 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
155
156 wait_for_completion(&tilcdc_crtc->palette_loaded);
157
158 /* Restore the registers. */
159 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
160 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_fb_base);
161 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, dma_fb_ceiling);
162 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, raster_ctl);
163}
164
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300165static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
166{
167 struct tilcdc_drm_private *priv = dev->dev_private;
168
169 tilcdc_clear_irqstatus(dev, 0xffffffff);
170
171 if (priv->rev == 1) {
172 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
Jyri Sarhacba88442016-11-16 00:12:27 +0200173 LCDC_V1_SYNC_LOST_INT_ENA |
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300174 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000175 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
176 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300177 } else {
178 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
179 LCDC_V2_UNDERFLOW_INT_ENA |
180 LCDC_V2_END_OF_FRAME0_INT_ENA |
181 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
182 }
183}
184
185static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
186{
187 struct tilcdc_drm_private *priv = dev->dev_private;
188
189 /* disable irqs that we might have enabled: */
190 if (priv->rev == 1) {
191 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
Jyri Sarhacba88442016-11-16 00:12:27 +0200192 LCDC_V1_SYNC_LOST_INT_ENA |
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300193 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
194 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
195 LCDC_V1_END_OF_FRAME_INT_ENA);
196 } else {
197 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
198 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
199 LCDC_V2_END_OF_FRAME0_INT_ENA |
200 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
201 }
202}
203
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300204static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600205{
206 struct drm_device *dev = crtc->dev;
207 struct tilcdc_drm_private *priv = dev->dev_private;
208
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300209 if (priv->rev != 2)
210 return;
211
212 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
213 usleep_range(250, 1000);
214 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
215}
216
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300217static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300218{
219 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300220 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100221 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300222
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300223 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Jyri Sarha2d53a182016-10-25 12:27:31 +0300224 mutex_lock(&tilcdc_crtc->enable_lock);
225 if (tilcdc_crtc->enabled || tilcdc_crtc->shutdown) {
226 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300227 return;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300228 }
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300229
230 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300231
232 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600233
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100234 if (priv->rev == 1 && !completion_done(&tilcdc_crtc->palette_loaded))
235 tilcdc_crtc_load_palette(crtc);
236
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300237 tilcdc_crtc_enable_irqs(dev);
238
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300239 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Jyri Sarhaf13e0882016-11-19 18:00:32 +0200240 tilcdc_write_mask(dev, LCDC_RASTER_CTRL_REG,
241 LCDC_PALETTE_LOAD_MODE(DATA_ONLY),
242 LCDC_PALETTE_LOAD_MODE_MASK);
Rob Clark16ea9752013-01-08 15:04:28 -0600243 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300244
245 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300246
247 tilcdc_crtc->enabled = true;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300248 mutex_unlock(&tilcdc_crtc->enable_lock);
Rob Clark16ea9752013-01-08 15:04:28 -0600249}
250
Jyri Sarha2d53a182016-10-25 12:27:31 +0300251static void tilcdc_crtc_off(struct drm_crtc *crtc, bool shutdown)
Rob Clark16ea9752013-01-08 15:04:28 -0600252{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300253 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600254 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300255 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600256
Jyri Sarha2d53a182016-10-25 12:27:31 +0300257 mutex_lock(&tilcdc_crtc->enable_lock);
258 if (shutdown)
259 tilcdc_crtc->shutdown = true;
260 if (!tilcdc_crtc->enabled) {
261 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300262 return;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300263 }
Jyri Sarha2d5be882016-04-07 20:20:23 +0300264 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600265 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300266
267 /*
268 * if necessary wait for framedone irq which will still come
269 * before putting things to sleep..
270 */
271 if (priv->rev == 2) {
272 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
273 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300274 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300275 if (ret == 0)
276 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
277 __func__);
278 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300279
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100280 /*
281 * LCDC will not retain the palette when reset. Make sure it gets
282 * reloaded on tilcdc_crtc_enable().
283 */
284 if (priv->rev == 1)
285 reinit_completion(&tilcdc_crtc->palette_loaded);
286
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300287 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300288
289 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300290
291 pm_runtime_put_sync(dev->dev);
292
293 if (tilcdc_crtc->next_fb) {
294 drm_flip_work_queue(&tilcdc_crtc->unref_work,
295 tilcdc_crtc->next_fb);
296 tilcdc_crtc->next_fb = NULL;
297 }
298
299 if (tilcdc_crtc->curr_fb) {
300 drm_flip_work_queue(&tilcdc_crtc->unref_work,
301 tilcdc_crtc->curr_fb);
302 tilcdc_crtc->curr_fb = NULL;
303 }
304
305 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
306 tilcdc_crtc->last_vblank = ktime_set(0, 0);
307
308 tilcdc_crtc->enabled = false;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300309 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300310}
311
Jyri Sarha9e79e062016-10-18 23:23:27 +0300312static void tilcdc_crtc_disable(struct drm_crtc *crtc)
313{
314 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Jyri Sarha2d53a182016-10-25 12:27:31 +0300315 tilcdc_crtc_off(crtc, false);
316}
317
318void tilcdc_crtc_shutdown(struct drm_crtc *crtc)
319{
320 tilcdc_crtc_off(crtc, true);
Jyri Sarha9e79e062016-10-18 23:23:27 +0300321}
322
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300323static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
324{
325 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600326}
327
Jyri Sarha13b3d722016-04-06 14:02:38 +0300328static void tilcdc_crtc_recover_work(struct work_struct *work)
329{
330 struct tilcdc_crtc *tilcdc_crtc =
331 container_of(work, struct tilcdc_crtc, recover_work);
332 struct drm_crtc *crtc = &tilcdc_crtc->base;
333
334 dev_info(crtc->dev->dev, "%s: Reset CRTC", __func__);
335
336 drm_modeset_lock_crtc(crtc, NULL);
337
338 if (!tilcdc_crtc_is_on(crtc))
339 goto out;
340
341 tilcdc_crtc_disable(crtc);
342 tilcdc_crtc_enable(crtc);
343out:
344 drm_modeset_unlock_crtc(crtc);
345}
346
Rob Clark16ea9752013-01-08 15:04:28 -0600347static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
348{
349 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Jyri Sarha4e910c72016-09-06 22:55:33 +0300350 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600351
Jyri Sarha6c94c712016-09-07 11:46:40 +0300352 drm_modeset_lock_crtc(crtc, NULL);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300353 tilcdc_crtc_disable(crtc);
Jyri Sarha6c94c712016-09-07 11:46:40 +0300354 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600355
Jyri Sarha4e910c72016-09-06 22:55:33 +0300356 flush_workqueue(priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600357
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300358 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600359 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400360 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600361}
362
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300363int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600364 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300365 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600366{
367 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
368 struct drm_device *dev = crtc->dev;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300369 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000370
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300371 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
372
Rob Clark16ea9752013-01-08 15:04:28 -0600373 if (tilcdc_crtc->event) {
374 dev_err(dev->dev, "already pending page flip!\n");
375 return -EBUSY;
376 }
377
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300378 drm_framebuffer_reference(fb);
379
Matt Roperf4510a22014-04-01 15:22:40 -0700380 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300381
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200382 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300383
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300384 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
385 ktime_t next_vblank;
386 s64 tdiff;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300387
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300388 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
389 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200390
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300391 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
392
393 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
394 tilcdc_crtc->next_fb = fb;
395 }
396
397 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200398 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200399
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300400 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200401
402 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600403
404 return 0;
405}
406
Rob Clark16ea9752013-01-08 15:04:28 -0600407static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
408 const struct drm_display_mode *mode,
409 struct drm_display_mode *adjusted_mode)
410{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200411 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
412
413 if (!tilcdc_crtc->simulate_vesa_sync)
414 return true;
415
416 /*
417 * tilcdc does not generate VESA-compliant sync but aligns
418 * VS on the second edge of HS instead of first edge.
419 * We use adjusted_mode, to fixup sync by aligning both rising
420 * edges and add HSKEW offset to fix the sync.
421 */
422 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
423 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
424
425 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
426 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
427 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
428 } else {
429 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
430 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
431 }
432
Rob Clark16ea9752013-01-08 15:04:28 -0600433 return true;
434}
435
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200436/*
437 * Calculate the percentage difference between the requested pixel clock rate
438 * and the effective rate resulting from calculating the clock divider value.
439 */
440static unsigned int tilcdc_pclk_diff(unsigned long rate,
441 unsigned long real_rate)
442{
443 int r = rate / 100, rr = real_rate / 100;
444
445 return (unsigned int)(abs(((rr - r) * 100) / r));
446}
447
Jyri Sarha642e5162016-09-06 16:19:54 +0300448static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
449{
450 struct drm_device *dev = crtc->dev;
451 struct tilcdc_drm_private *priv = dev->dev_private;
452 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200453 unsigned long clk_rate, real_rate, req_rate;
454 unsigned int clkdiv;
Jyri Sarha642e5162016-09-06 16:19:54 +0300455 int ret;
456
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200457 clkdiv = 2; /* first try using a standard divider of 2 */
458
Jyri Sarha642e5162016-09-06 16:19:54 +0300459 /* mode.clock is in KHz, set_rate wants parameter in Hz */
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200460 req_rate = crtc->mode.clock * 1000;
461
462 ret = clk_set_rate(priv->clk, req_rate * clkdiv);
463 clk_rate = clk_get_rate(priv->clk);
Jyri Sarha642e5162016-09-06 16:19:54 +0300464 if (ret < 0) {
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200465 /*
466 * If we fail to set the clock rate (some architectures don't
467 * use the common clock framework yet and may not implement
468 * all the clk API calls for every clock), try the next best
469 * thing: adjusting the clock divider, unless clk_get_rate()
470 * failed as well.
471 */
472 if (!clk_rate) {
473 /* Nothing more we can do. Just bail out. */
474 dev_err(dev->dev,
475 "failed to set the pixel clock - unable to read current lcdc clock rate\n");
476 return;
477 }
478
479 clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);
480
481 /*
482 * Emit a warning if the real clock rate resulting from the
483 * calculated divider differs much from the requested rate.
484 *
485 * 5% is an arbitrary value - LCDs are usually quite tolerant
486 * about pixel clock rates.
487 */
488 real_rate = clkdiv * req_rate;
489
490 if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
491 dev_warn(dev->dev,
492 "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
493 clk_rate, real_rate);
494 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300495 }
496
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200497 tilcdc_crtc->lcd_fck_rate = clk_rate;
Jyri Sarha642e5162016-09-06 16:19:54 +0300498
499 DBG("lcd_clk=%u, mode clock=%d, div=%u",
500 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
501
502 /* Configure the LCD clock divisor. */
503 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
504 LCDC_RASTER_MODE);
505
506 if (priv->rev == 2)
507 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
508 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
509 LCDC_V2_CORE_CLK_EN);
510}
511
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300512static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
513{
514 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
515 struct drm_device *dev = crtc->dev;
516 struct tilcdc_drm_private *priv = dev->dev_private;
517 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
518 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
519 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
520 struct drm_framebuffer *fb = crtc->primary->state->fb;
521
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300522 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
523
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300524 if (WARN_ON(!info))
525 return;
526
527 if (WARN_ON(!fb))
528 return;
529
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300530 /* Configure the Burst Size and fifo threshold of DMA: */
531 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
532 switch (info->dma_burst_sz) {
533 case 1:
534 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
535 break;
536 case 2:
537 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
538 break;
539 case 4:
540 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
541 break;
542 case 8:
543 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
544 break;
545 case 16:
546 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
547 break;
548 default:
549 dev_err(dev->dev, "invalid burst size\n");
550 return;
551 }
552 reg |= (info->fifo_th << 8);
553 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
554
555 /* Configure timings: */
556 hbp = mode->htotal - mode->hsync_end;
557 hfp = mode->hsync_start - mode->hdisplay;
558 hsw = mode->hsync_end - mode->hsync_start;
559 vbp = mode->vtotal - mode->vsync_end;
560 vfp = mode->vsync_start - mode->vdisplay;
561 vsw = mode->vsync_end - mode->vsync_start;
562
563 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
564 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
565
566 /* Set AC Bias Period and Number of Transitions per Interrupt: */
567 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
568 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
569 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
570
571 /*
572 * subtract one from hfp, hbp, hsw because the hardware uses
573 * a value of 0 as 1
574 */
575 if (priv->rev == 2) {
576 /* clear bits we're going to set */
577 reg &= ~0x78000033;
578 reg |= ((hfp-1) & 0x300) >> 8;
579 reg |= ((hbp-1) & 0x300) >> 4;
580 reg |= ((hsw-1) & 0x3c0) << 21;
581 }
582 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
583
584 reg = (((mode->hdisplay >> 4) - 1) << 4) |
585 (((hbp-1) & 0xff) << 24) |
586 (((hfp-1) & 0xff) << 16) |
587 (((hsw-1) & 0x3f) << 10);
588 if (priv->rev == 2)
589 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
590 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
591
592 reg = ((mode->vdisplay - 1) & 0x3ff) |
593 ((vbp & 0xff) << 24) |
594 ((vfp & 0xff) << 16) |
595 (((vsw-1) & 0x3f) << 10);
596 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
597
598 /*
599 * be sure to set Bit 10 for the V2 LCDC controller,
600 * otherwise limited to 1024 pixels width, stopping
601 * 1920x1080 being supported.
602 */
603 if (priv->rev == 2) {
604 if ((mode->vdisplay - 1) & 0x400) {
605 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
606 LCDC_LPP_B10);
607 } else {
608 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
609 LCDC_LPP_B10);
610 }
611 }
612
613 /* Configure display type: */
614 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
615 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
616 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
617 0x000ff000 /* Palette Loading Delay bits */);
618 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
619 if (info->tft_alt_mode)
620 reg |= LCDC_TFT_ALT_ENABLE;
621 if (priv->rev == 2) {
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300622 switch (fb->pixel_format) {
623 case DRM_FORMAT_BGR565:
624 case DRM_FORMAT_RGB565:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300625 break;
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300626 case DRM_FORMAT_XBGR8888:
627 case DRM_FORMAT_XRGB8888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300628 reg |= LCDC_V2_TFT_24BPP_UNPACK;
629 /* fallthrough */
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300630 case DRM_FORMAT_BGR888:
631 case DRM_FORMAT_RGB888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300632 reg |= LCDC_V2_TFT_24BPP_MODE;
633 break;
634 default:
635 dev_err(dev->dev, "invalid pixel format\n");
636 return;
637 }
638 }
639 reg |= info->fdd < 12;
640 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
641
642 if (info->invert_pxl_clk)
643 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
644 else
645 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
646
647 if (info->sync_ctrl)
648 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
649 else
650 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
651
652 if (info->sync_edge)
653 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
654 else
655 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
656
657 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
658 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
659 else
660 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
661
662 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
663 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
664 else
665 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
666
667 if (info->raster_order)
668 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
669 else
670 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
671
672 drm_framebuffer_reference(fb);
673
674 set_scanout(crtc, fb);
675
Jyri Sarha642e5162016-09-06 16:19:54 +0300676 tilcdc_crtc_set_clk(crtc);
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300677
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300678 crtc->hwmode = crtc->state->adjusted_mode;
679}
680
Jyri Sarhadb380c52016-04-07 15:10:23 +0300681static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
682 struct drm_crtc_state *state)
683{
684 struct drm_display_mode *mode = &state->mode;
685 int ret;
686
687 /* If we are not active we don't care */
688 if (!state->active)
689 return 0;
690
691 if (state->state->planes[0].ptr != crtc->primary ||
692 state->state->planes[0].state == NULL ||
693 state->state->planes[0].state->crtc != crtc) {
694 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
695 return -EINVAL;
696 }
697
698 ret = tilcdc_crtc_mode_valid(crtc, mode);
699 if (ret) {
700 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
701 return -EINVAL;
702 }
703
704 return 0;
705}
706
Rob Clark16ea9752013-01-08 15:04:28 -0600707static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300708 .destroy = tilcdc_crtc_destroy,
709 .set_config = drm_atomic_helper_set_config,
710 .page_flip = drm_atomic_helper_page_flip,
711 .reset = drm_atomic_helper_crtc_reset,
712 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
713 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600714};
715
716static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600717 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300718 .enable = tilcdc_crtc_enable,
719 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300720 .atomic_check = tilcdc_crtc_atomic_check,
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300721 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
Rob Clark16ea9752013-01-08 15:04:28 -0600722};
723
724int tilcdc_crtc_max_width(struct drm_crtc *crtc)
725{
726 struct drm_device *dev = crtc->dev;
727 struct tilcdc_drm_private *priv = dev->dev_private;
728 int max_width = 0;
729
730 if (priv->rev == 1)
731 max_width = 1024;
732 else if (priv->rev == 2)
733 max_width = 2048;
734
735 return max_width;
736}
737
738int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
739{
740 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
741 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500742 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600743
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500744 /*
745 * check to see if the width is within the range that
746 * the LCD Controller physically supports
747 */
Rob Clark16ea9752013-01-08 15:04:28 -0600748 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
749 return MODE_VIRTUAL_X;
750
751 /* width must be multiple of 16 */
752 if (mode->hdisplay & 0xf)
753 return MODE_VIRTUAL_X;
754
755 if (mode->vdisplay > 2048)
756 return MODE_VIRTUAL_Y;
757
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500758 DBG("Processing mode %dx%d@%d with pixel clock %d",
759 mode->hdisplay, mode->vdisplay,
760 drm_mode_vrefresh(mode), mode->clock);
761
762 hbp = mode->htotal - mode->hsync_end;
763 hfp = mode->hsync_start - mode->hdisplay;
764 hsw = mode->hsync_end - mode->hsync_start;
765 vbp = mode->vtotal - mode->vsync_end;
766 vfp = mode->vsync_start - mode->vdisplay;
767 vsw = mode->vsync_end - mode->vsync_start;
768
769 if ((hbp-1) & ~0x3ff) {
770 DBG("Pruning mode: Horizontal Back Porch out of range");
771 return MODE_HBLANK_WIDE;
772 }
773
774 if ((hfp-1) & ~0x3ff) {
775 DBG("Pruning mode: Horizontal Front Porch out of range");
776 return MODE_HBLANK_WIDE;
777 }
778
779 if ((hsw-1) & ~0x3ff) {
780 DBG("Pruning mode: Horizontal Sync Width out of range");
781 return MODE_HSYNC_WIDE;
782 }
783
784 if (vbp & ~0xff) {
785 DBG("Pruning mode: Vertical Back Porch out of range");
786 return MODE_VBLANK_WIDE;
787 }
788
789 if (vfp & ~0xff) {
790 DBG("Pruning mode: Vertical Front Porch out of range");
791 return MODE_VBLANK_WIDE;
792 }
793
794 if ((vsw-1) & ~0x3f) {
795 DBG("Pruning mode: Vertical Sync Width out of range");
796 return MODE_VSYNC_WIDE;
797 }
798
Darren Etheridge4e564342013-06-21 13:52:23 -0500799 /*
800 * some devices have a maximum allowed pixel clock
801 * configured from the DT
802 */
803 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500804 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500805 return MODE_CLOCK_HIGH;
806 }
807
808 /*
809 * some devices further limit the max horizontal resolution
810 * configured from the DT
811 */
812 if (mode->hdisplay > priv->max_width)
813 return MODE_BAD_WIDTH;
814
Rob Clark16ea9752013-01-08 15:04:28 -0600815 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500816 bandwidth = mode->hdisplay * mode->vdisplay *
817 drm_mode_vrefresh(mode);
818 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500819 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600820 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500821 }
Rob Clark16ea9752013-01-08 15:04:28 -0600822
823 return MODE_OK;
824}
825
826void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
827 const struct tilcdc_panel_info *info)
828{
829 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
830 tilcdc_crtc->info = info;
831}
832
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200833void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
834 bool simulate_vesa_sync)
835{
836 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
837
838 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
839}
840
Rob Clark16ea9752013-01-08 15:04:28 -0600841void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
842{
Rob Clark16ea9752013-01-08 15:04:28 -0600843 struct drm_device *dev = crtc->dev;
844 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha642e5162016-09-06 16:19:54 +0300845 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600846
Jyri Sarha642e5162016-09-06 16:19:54 +0300847 drm_modeset_lock_crtc(crtc, NULL);
848 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
849 if (tilcdc_crtc_is_on(crtc)) {
850 pm_runtime_get_sync(dev->dev);
851 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600852
Jyri Sarha642e5162016-09-06 16:19:54 +0300853 tilcdc_crtc_set_clk(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600854
Jyri Sarha642e5162016-09-06 16:19:54 +0300855 tilcdc_crtc_enable(crtc);
856 pm_runtime_put_sync(dev->dev);
857 }
Rob Clark16ea9752013-01-08 15:04:28 -0600858 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300859 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600860}
861
Jyri Sarha5895d082016-01-08 14:33:09 +0200862#define SYNC_LOST_COUNT_LIMIT 50
863
Rob Clark16ea9752013-01-08 15:04:28 -0600864irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
865{
866 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
867 struct drm_device *dev = crtc->dev;
868 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300869 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600870
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300871 stat = tilcdc_read_irqstatus(dev);
872 tilcdc_clear_irqstatus(dev, stat);
873
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300874 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600875 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200876 bool skip_event = false;
877 ktime_t now;
878
879 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600880
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300881 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600882
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200883 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600884
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200885 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600886
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200887 if (tilcdc_crtc->next_fb) {
888 set_scanout(crtc, tilcdc_crtc->next_fb);
889 tilcdc_crtc->next_fb = NULL;
890 skip_event = true;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300891 }
892
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200893 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
894
Gustavo Padovan099ede82016-07-04 21:04:52 -0300895 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200896
897 if (!skip_event) {
898 struct drm_pending_vblank_event *event;
899
900 spin_lock_irqsave(&dev->event_lock, flags);
901
902 event = tilcdc_crtc->event;
903 tilcdc_crtc->event = NULL;
904 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700905 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200906
907 spin_unlock_irqrestore(&dev->event_lock, flags);
908 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200909
910 if (tilcdc_crtc->frame_intact)
911 tilcdc_crtc->sync_lost_count = 0;
912 else
913 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600914 }
915
Jyri Sarha14944112016-04-07 20:36:48 +0300916 if (stat & LCDC_FIFO_UNDERFLOW)
Daniel Schultzd7014532016-10-28 13:52:42 +0200917 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underflow",
Jyri Sarha14944112016-04-07 20:36:48 +0300918 __func__, stat);
919
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100920 if (priv->rev == 1) {
921 if (stat & LCDC_PL_LOAD_DONE) {
922 complete(&tilcdc_crtc->palette_loaded);
923 tilcdc_clear(dev,
924 LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
925 }
926 }
927
Jyri Sarhacba88442016-11-16 00:12:27 +0200928 if (stat & LCDC_SYNC_LOST) {
929 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
930 __func__, stat);
931 tilcdc_crtc->frame_intact = false;
932 if (tilcdc_crtc->sync_lost_count++ >
933 SYNC_LOST_COUNT_LIMIT) {
934 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, recovering", __func__, stat);
935 queue_work(system_wq, &tilcdc_crtc->recover_work);
936 if (priv->rev == 1)
937 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
938 LCDC_V1_SYNC_LOST_INT_ENA);
939 else
940 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
941 LCDC_SYNC_LOST);
942 tilcdc_crtc->sync_lost_count = 0;
943 }
944 }
945
Jyri Sarha14944112016-04-07 20:36:48 +0300946 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600947 if (priv->rev == 2) {
948 if (stat & LCDC_FRAME_DONE) {
949 tilcdc_crtc->frame_done = true;
950 wake_up(&tilcdc_crtc->frame_done_wq);
951 }
Rob Clark16ea9752013-01-08 15:04:28 -0600952
Jyri Sarha14944112016-04-07 20:36:48 +0300953 /* Indicate to LCDC that the interrupt service routine has
954 * completed, see 13.3.6.1.6 in AM335x TRM.
955 */
956 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
957 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200958
Rob Clark16ea9752013-01-08 15:04:28 -0600959 return IRQ_HANDLED;
960}
961
Jyri Sarha9963d362016-11-15 22:56:46 +0200962int tilcdc_crtc_create(struct drm_device *dev)
Rob Clark16ea9752013-01-08 15:04:28 -0600963{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300964 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600965 struct tilcdc_crtc *tilcdc_crtc;
966 struct drm_crtc *crtc;
967 int ret;
968
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200969 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600970 if (!tilcdc_crtc) {
971 dev_err(dev->dev, "allocation failed\n");
Jyri Sarha9963d362016-11-15 22:56:46 +0200972 return -ENOMEM;
Rob Clark16ea9752013-01-08 15:04:28 -0600973 }
974
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100975 if (priv->rev == 1) {
976 init_completion(&tilcdc_crtc->palette_loaded);
977 tilcdc_crtc->palette_base = dmam_alloc_coherent(dev->dev,
978 TILCDC_REV1_PALETTE_SIZE,
979 &tilcdc_crtc->palette_dma_handle,
980 GFP_KERNEL | __GFP_ZERO);
981 if (!tilcdc_crtc->palette_base)
Jyri Sarha9963d362016-11-15 22:56:46 +0200982 return -ENOMEM;
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100983 }
984
Rob Clark16ea9752013-01-08 15:04:28 -0600985 crtc = &tilcdc_crtc->base;
986
Jyri Sarha47f571c2016-04-07 15:04:18 +0300987 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
988 if (ret < 0)
989 goto fail;
990
Jyri Sarha2d53a182016-10-25 12:27:31 +0300991 mutex_init(&tilcdc_crtc->enable_lock);
992
Rob Clark16ea9752013-01-08 15:04:28 -0600993 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
994
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100995 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400996 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600997
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200998 spin_lock_init(&tilcdc_crtc->irq_lock);
Jyri Sarha13b3d722016-04-06 14:02:38 +0300999 INIT_WORK(&tilcdc_crtc->recover_work, tilcdc_crtc_recover_work);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +02001000
Jyri Sarha47f571c2016-04-07 15:04:18 +03001001 ret = drm_crtc_init_with_planes(dev, crtc,
1002 &tilcdc_crtc->primary,
1003 NULL,
1004 &tilcdc_crtc_funcs,
1005 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -06001006 if (ret < 0)
1007 goto fail;
1008
1009 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
1010
Jyri Sarhad66284fb2015-05-27 11:58:37 +03001011 if (priv->is_componentized) {
1012 struct device_node *ports =
1013 of_get_child_by_name(dev->dev->of_node, "ports");
1014
1015 if (ports) {
1016 crtc->port = of_get_child_by_name(ports, "port");
1017 of_node_put(ports);
1018 } else {
1019 crtc->port =
1020 of_get_child_by_name(dev->dev->of_node, "port");
1021 }
1022 if (!crtc->port) { /* This should never happen */
1023 dev_err(dev->dev, "Port node not found in %s\n",
1024 dev->dev->of_node->full_name);
Jyri Sarha9963d362016-11-15 22:56:46 +02001025 ret = -EINVAL;
Jyri Sarhad66284fb2015-05-27 11:58:37 +03001026 goto fail;
1027 }
1028 }
1029
Jyri Sarha9963d362016-11-15 22:56:46 +02001030 priv->crtc = crtc;
1031 return 0;
Rob Clark16ea9752013-01-08 15:04:28 -06001032
1033fail:
1034 tilcdc_crtc_destroy(crtc);
Jyri Sarha9963d362016-11-15 22:56:46 +02001035 return -ENOMEM;
Rob Clark16ea9752013-01-08 15:04:28 -06001036}