blob: 248e3ea7568cb161479e9777407105b217f2c6ae [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
Rob Clarka464d612013-08-07 13:41:20 -040018#include "drm_flip_work.h"
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010019#include <drm/drm_plane_helper.h>
Rob Clark16ea9752013-01-08 15:04:28 -060020
21#include "tilcdc_drv.h"
22#include "tilcdc_regs.h"
23
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020024#define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
25
Rob Clark16ea9752013-01-08 15:04:28 -060026struct tilcdc_crtc {
27 struct drm_crtc base;
28
29 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060030 struct drm_pending_vblank_event *event;
31 int dpms;
32 wait_queue_head_t frame_done_wq;
33 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020034 spinlock_t irq_lock;
35
36 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060037
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030038 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020039 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060040
41 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040042 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020043
44 /* Only set if an external encoder is connected */
45 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020046
47 int sync_lost_count;
48 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060049};
50#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
51
Rob Clarka464d612013-08-07 13:41:20 -040052static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060053{
Darren Etheridgef7b45752013-06-21 13:52:26 -050054 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040055 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060056 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060057
58 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040059 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060060 mutex_unlock(&dev->mode_config.mutex);
61}
62
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030063static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060064{
65 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
66 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060067 struct drm_gem_cma_object *gem;
68 unsigned int depth, bpp;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030069 dma_addr_t start, end;
Rob Clark16ea9752013-01-08 15:04:28 -060070
71 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
72 gem = drm_fb_cma_get_gem_obj(fb, 0);
73
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030074 start = gem->paddr + fb->offsets[0] +
75 crtc->y * fb->pitches[0] +
76 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060077
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030078 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060079
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030080 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
81 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
82
83 if (tilcdc_crtc->curr_fb)
84 drm_flip_work_queue(&tilcdc_crtc->unref_work,
85 tilcdc_crtc->curr_fb);
86
87 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060088}
89
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030090static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -060091{
92 struct drm_device *dev = crtc->dev;
93 struct tilcdc_drm_private *priv = dev->dev_private;
94
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030095 if (priv->rev != 2)
96 return;
97
98 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
99 usleep_range(250, 1000);
100 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
101}
102
103static void start(struct drm_crtc *crtc)
104{
105 struct drm_device *dev = crtc->dev;
106
107 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600108
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300109 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600110 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
111 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
112}
113
114static void stop(struct drm_crtc *crtc)
115{
116 struct drm_device *dev = crtc->dev;
117
118 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
119}
120
121static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
122{
123 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
124
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200125 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600126
127 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400128 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
129
Rob Clark16ea9752013-01-08 15:04:28 -0600130 kfree(tilcdc_crtc);
131}
132
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000133static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
134{
135 struct drm_device *dev = crtc->dev;
136 unsigned int depth, bpp;
137
138 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
139
140 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
141 dev_err(dev->dev,
142 "Invalid pitch: fb and crtc widths must be the same");
143 return -EINVAL;
144 }
145
146 return 0;
147}
148
Rob Clark16ea9752013-01-08 15:04:28 -0600149static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
150 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700151 struct drm_pending_vblank_event *event,
152 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600153{
154 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
155 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000156 int r;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300157 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200158 s64 tdiff;
159 ktime_t next_vblank;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000160
161 r = tilcdc_verify_fb(crtc, fb);
162 if (r)
163 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600164
165 if (tilcdc_crtc->event) {
166 dev_err(dev->dev, "already pending page flip!\n");
167 return -EBUSY;
168 }
169
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300170 drm_framebuffer_reference(fb);
171
Matt Roperf4510a22014-04-01 15:22:40 -0700172 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300173
174 pm_runtime_get_sync(dev->dev);
175
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200176 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300177
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200178 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
179 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300180
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200181 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
182
183 if (tdiff >= TILCDC_VBLANK_SAFETY_THRESHOLD_US)
184 set_scanout(crtc, fb);
185 else
186 tilcdc_crtc->next_fb = fb;
187
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300188 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200189
190 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600191
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300192 pm_runtime_put_sync(dev->dev);
193
Rob Clark16ea9752013-01-08 15:04:28 -0600194 return 0;
195}
196
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000197void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
Rob Clark16ea9752013-01-08 15:04:28 -0600198{
199 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
200 struct drm_device *dev = crtc->dev;
201 struct tilcdc_drm_private *priv = dev->dev_private;
202
203 /* we really only care about on or off: */
204 if (mode != DRM_MODE_DPMS_ON)
205 mode = DRM_MODE_DPMS_OFF;
206
207 if (tilcdc_crtc->dpms == mode)
208 return;
209
210 tilcdc_crtc->dpms = mode;
211
Rob Clark16ea9752013-01-08 15:04:28 -0600212 if (mode == DRM_MODE_DPMS_ON) {
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300213 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600214 start(crtc);
215 } else {
216 tilcdc_crtc->frame_done = false;
217 stop(crtc);
218
Darren Etheridgef7b45752013-06-21 13:52:26 -0500219 /*
220 * if necessary wait for framedone irq which will still come
Rob Clark16ea9752013-01-08 15:04:28 -0600221 * before putting things to sleep..
222 */
223 if (priv->rev == 2) {
224 int ret = wait_event_timeout(
225 tilcdc_crtc->frame_done_wq,
226 tilcdc_crtc->frame_done,
227 msecs_to_jiffies(50));
228 if (ret == 0)
229 dev_err(dev->dev, "timeout waiting for framedone\n");
230 }
Rob Clark16ea9752013-01-08 15:04:28 -0600231
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300232 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300233
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200234 if (tilcdc_crtc->next_fb) {
235 drm_flip_work_queue(&tilcdc_crtc->unref_work,
236 tilcdc_crtc->next_fb);
237 tilcdc_crtc->next_fb = NULL;
238 }
239
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300240 if (tilcdc_crtc->curr_fb) {
241 drm_flip_work_queue(&tilcdc_crtc->unref_work,
242 tilcdc_crtc->curr_fb);
243 tilcdc_crtc->curr_fb = NULL;
244 }
245
246 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300247 }
Rob Clark16ea9752013-01-08 15:04:28 -0600248}
249
250static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
251 const struct drm_display_mode *mode,
252 struct drm_display_mode *adjusted_mode)
253{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200254 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
255
256 if (!tilcdc_crtc->simulate_vesa_sync)
257 return true;
258
259 /*
260 * tilcdc does not generate VESA-compliant sync but aligns
261 * VS on the second edge of HS instead of first edge.
262 * We use adjusted_mode, to fixup sync by aligning both rising
263 * edges and add HSKEW offset to fix the sync.
264 */
265 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
266 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
267
268 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
269 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
270 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
271 } else {
272 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
273 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
274 }
275
Rob Clark16ea9752013-01-08 15:04:28 -0600276 return true;
277}
278
279static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
280{
281 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
282}
283
284static void tilcdc_crtc_commit(struct drm_crtc *crtc)
285{
286 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
287}
288
289static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
290 struct drm_display_mode *mode,
291 struct drm_display_mode *adjusted_mode,
292 int x, int y,
293 struct drm_framebuffer *old_fb)
294{
295 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
296 struct drm_device *dev = crtc->dev;
297 struct tilcdc_drm_private *priv = dev->dev_private;
298 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
299 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
300 int ret;
301
302 ret = tilcdc_crtc_mode_valid(crtc, mode);
303 if (WARN_ON(ret))
304 return ret;
305
306 if (WARN_ON(!info))
307 return -EINVAL;
308
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000309 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
310 if (ret)
311 return ret;
312
Rob Clark16ea9752013-01-08 15:04:28 -0600313 pm_runtime_get_sync(dev->dev);
314
315 /* Configure the Burst Size and fifo threshold of DMA: */
316 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
317 switch (info->dma_burst_sz) {
318 case 1:
319 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
320 break;
321 case 2:
322 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
323 break;
324 case 4:
325 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
326 break;
327 case 8:
328 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
329 break;
330 case 16:
331 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
332 break;
333 default:
334 return -EINVAL;
335 }
336 reg |= (info->fifo_th << 8);
337 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
338
339 /* Configure timings: */
340 hbp = mode->htotal - mode->hsync_end;
341 hfp = mode->hsync_start - mode->hdisplay;
342 hsw = mode->hsync_end - mode->hsync_start;
343 vbp = mode->vtotal - mode->vsync_end;
344 vfp = mode->vsync_start - mode->vdisplay;
345 vsw = mode->vsync_end - mode->vsync_start;
346
347 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
348 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
349
350 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
351 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
352 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
353 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500354
355 /*
356 * subtract one from hfp, hbp, hsw because the hardware uses
357 * a value of 0 as 1
358 */
Rob Clark16ea9752013-01-08 15:04:28 -0600359 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500360 /* clear bits we're going to set */
361 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500362 reg |= ((hfp-1) & 0x300) >> 8;
363 reg |= ((hbp-1) & 0x300) >> 4;
364 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600365 }
366 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
367
368 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500369 (((hbp-1) & 0xff) << 24) |
370 (((hfp-1) & 0xff) << 16) |
371 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600372 if (priv->rev == 2)
373 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
374 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
375
376 reg = ((mode->vdisplay - 1) & 0x3ff) |
377 ((vbp & 0xff) << 24) |
378 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500379 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600380 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
381
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500382 /*
383 * be sure to set Bit 10 for the V2 LCDC controller,
384 * otherwise limited to 1024 pixels width, stopping
385 * 1920x1080 being suppoted.
386 */
387 if (priv->rev == 2) {
388 if ((mode->vdisplay - 1) & 0x400) {
389 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
390 LCDC_LPP_B10);
391 } else {
392 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
393 LCDC_LPP_B10);
394 }
395 }
396
Rob Clark16ea9752013-01-08 15:04:28 -0600397 /* Configure display type: */
398 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
399 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
400 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
401 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
402 if (info->tft_alt_mode)
403 reg |= LCDC_TFT_ALT_ENABLE;
404 if (priv->rev == 2) {
405 unsigned int depth, bpp;
406
Matt Roperf4510a22014-04-01 15:22:40 -0700407 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600408 switch (bpp) {
409 case 16:
410 break;
411 case 32:
412 reg |= LCDC_V2_TFT_24BPP_UNPACK;
413 /* fallthrough */
414 case 24:
415 reg |= LCDC_V2_TFT_24BPP_MODE;
416 break;
417 default:
418 dev_err(dev->dev, "invalid pixel format\n");
419 return -EINVAL;
420 }
421 }
422 reg |= info->fdd < 12;
423 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
424
425 if (info->invert_pxl_clk)
426 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
427 else
428 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
429
430 if (info->sync_ctrl)
431 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
432 else
433 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
434
435 if (info->sync_edge)
436 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
437 else
438 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
439
Darren Etheridgea9767182013-08-14 21:43:33 +0200440 /*
441 * use value from adjusted_mode here as this might have been
442 * changed as part of the fixup for slave encoders to solve the
443 * issue where tilcdc timings are not VESA compliant
444 */
445 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600446 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
447 else
448 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
449
450 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
451 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
452 else
453 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
454
455 if (info->raster_order)
456 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
457 else
458 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
459
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300460 drm_framebuffer_reference(crtc->primary->fb);
Rob Clark16ea9752013-01-08 15:04:28 -0600461
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300462 set_scanout(crtc, crtc->primary->fb);
463
Rob Clark16ea9752013-01-08 15:04:28 -0600464 tilcdc_crtc_update_clk(crtc);
465
466 pm_runtime_put_sync(dev->dev);
467
468 return 0;
469}
470
471static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
472 struct drm_framebuffer *old_fb)
473{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300474 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000475 int r;
476
477 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
478 if (r)
479 return r;
480
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300481 drm_framebuffer_reference(crtc->primary->fb);
482
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300483 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300484
485 set_scanout(crtc, crtc->primary->fb);
486
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300487 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300488
Rob Clark16ea9752013-01-08 15:04:28 -0600489 return 0;
490}
491
Rob Clark16ea9752013-01-08 15:04:28 -0600492static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
493 .destroy = tilcdc_crtc_destroy,
494 .set_config = drm_crtc_helper_set_config,
495 .page_flip = tilcdc_crtc_page_flip,
496};
497
498static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
499 .dpms = tilcdc_crtc_dpms,
500 .mode_fixup = tilcdc_crtc_mode_fixup,
501 .prepare = tilcdc_crtc_prepare,
502 .commit = tilcdc_crtc_commit,
503 .mode_set = tilcdc_crtc_mode_set,
504 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600505};
506
507int tilcdc_crtc_max_width(struct drm_crtc *crtc)
508{
509 struct drm_device *dev = crtc->dev;
510 struct tilcdc_drm_private *priv = dev->dev_private;
511 int max_width = 0;
512
513 if (priv->rev == 1)
514 max_width = 1024;
515 else if (priv->rev == 2)
516 max_width = 2048;
517
518 return max_width;
519}
520
521int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
522{
523 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
524 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500525 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600526
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500527 /*
528 * check to see if the width is within the range that
529 * the LCD Controller physically supports
530 */
Rob Clark16ea9752013-01-08 15:04:28 -0600531 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
532 return MODE_VIRTUAL_X;
533
534 /* width must be multiple of 16 */
535 if (mode->hdisplay & 0xf)
536 return MODE_VIRTUAL_X;
537
538 if (mode->vdisplay > 2048)
539 return MODE_VIRTUAL_Y;
540
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500541 DBG("Processing mode %dx%d@%d with pixel clock %d",
542 mode->hdisplay, mode->vdisplay,
543 drm_mode_vrefresh(mode), mode->clock);
544
545 hbp = mode->htotal - mode->hsync_end;
546 hfp = mode->hsync_start - mode->hdisplay;
547 hsw = mode->hsync_end - mode->hsync_start;
548 vbp = mode->vtotal - mode->vsync_end;
549 vfp = mode->vsync_start - mode->vdisplay;
550 vsw = mode->vsync_end - mode->vsync_start;
551
552 if ((hbp-1) & ~0x3ff) {
553 DBG("Pruning mode: Horizontal Back Porch out of range");
554 return MODE_HBLANK_WIDE;
555 }
556
557 if ((hfp-1) & ~0x3ff) {
558 DBG("Pruning mode: Horizontal Front Porch out of range");
559 return MODE_HBLANK_WIDE;
560 }
561
562 if ((hsw-1) & ~0x3ff) {
563 DBG("Pruning mode: Horizontal Sync Width out of range");
564 return MODE_HSYNC_WIDE;
565 }
566
567 if (vbp & ~0xff) {
568 DBG("Pruning mode: Vertical Back Porch out of range");
569 return MODE_VBLANK_WIDE;
570 }
571
572 if (vfp & ~0xff) {
573 DBG("Pruning mode: Vertical Front Porch out of range");
574 return MODE_VBLANK_WIDE;
575 }
576
577 if ((vsw-1) & ~0x3f) {
578 DBG("Pruning mode: Vertical Sync Width out of range");
579 return MODE_VSYNC_WIDE;
580 }
581
Darren Etheridge4e564342013-06-21 13:52:23 -0500582 /*
583 * some devices have a maximum allowed pixel clock
584 * configured from the DT
585 */
586 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500587 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500588 return MODE_CLOCK_HIGH;
589 }
590
591 /*
592 * some devices further limit the max horizontal resolution
593 * configured from the DT
594 */
595 if (mode->hdisplay > priv->max_width)
596 return MODE_BAD_WIDTH;
597
Rob Clark16ea9752013-01-08 15:04:28 -0600598 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500599 bandwidth = mode->hdisplay * mode->vdisplay *
600 drm_mode_vrefresh(mode);
601 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500602 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600603 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500604 }
Rob Clark16ea9752013-01-08 15:04:28 -0600605
606 return MODE_OK;
607}
608
609void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
610 const struct tilcdc_panel_info *info)
611{
612 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
613 tilcdc_crtc->info = info;
614}
615
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200616void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
617 bool simulate_vesa_sync)
618{
619 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
620
621 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
622}
623
Rob Clark16ea9752013-01-08 15:04:28 -0600624void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
625{
626 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
627 struct drm_device *dev = crtc->dev;
628 struct tilcdc_drm_private *priv = dev->dev_private;
629 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600630 unsigned long lcd_clk;
631 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600632 int ret;
633
634 pm_runtime_get_sync(dev->dev);
635
636 if (dpms == DRM_MODE_DPMS_ON)
637 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
638
Darren Etheridge3d193062014-01-15 15:52:36 -0600639 /* mode.clock is in KHz, set_rate wants parameter in Hz */
640 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
641 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600642 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
643 crtc->mode.clock);
644 goto out;
645 }
646
647 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600648
Darren Etheridge3d193062014-01-15 15:52:36 -0600649 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
650 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600651
652 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600653 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600654 LCDC_RASTER_MODE);
655
656 if (priv->rev == 2)
657 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
658 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
659 LCDC_V2_CORE_CLK_EN);
660
661 if (dpms == DRM_MODE_DPMS_ON)
662 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
663
664out:
665 pm_runtime_put_sync(dev->dev);
666}
667
Jyri Sarha5895d082016-01-08 14:33:09 +0200668#define SYNC_LOST_COUNT_LIMIT 50
669
Rob Clark16ea9752013-01-08 15:04:28 -0600670irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
671{
672 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
673 struct drm_device *dev = crtc->dev;
674 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300675 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600676
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300677 stat = tilcdc_read_irqstatus(dev);
678 tilcdc_clear_irqstatus(dev, stat);
679
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300680 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600681 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200682 bool skip_event = false;
683 ktime_t now;
684
685 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600686
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300687 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600688
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200689 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600690
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200691 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600692
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200693 if (tilcdc_crtc->next_fb) {
694 set_scanout(crtc, tilcdc_crtc->next_fb);
695 tilcdc_crtc->next_fb = NULL;
696 skip_event = true;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300697 }
698
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200699 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
700
701 drm_handle_vblank(dev, 0);
702
703 if (!skip_event) {
704 struct drm_pending_vblank_event *event;
705
706 spin_lock_irqsave(&dev->event_lock, flags);
707
708 event = tilcdc_crtc->event;
709 tilcdc_crtc->event = NULL;
710 if (event)
711 drm_send_vblank_event(dev, 0, event);
712
713 spin_unlock_irqrestore(&dev->event_lock, flags);
714 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200715
716 if (tilcdc_crtc->frame_intact)
717 tilcdc_crtc->sync_lost_count = 0;
718 else
719 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600720 }
721
722 if (priv->rev == 2) {
723 if (stat & LCDC_FRAME_DONE) {
724 tilcdc_crtc->frame_done = true;
725 wake_up(&tilcdc_crtc->frame_done_wq);
726 }
727 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
728 }
729
Jyri Sarha5895d082016-01-08 14:33:09 +0200730 if (stat & LCDC_SYNC_LOST) {
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200731 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
732 __func__, stat);
Jyri Sarha5895d082016-01-08 14:33:09 +0200733 tilcdc_crtc->frame_intact = false;
734 if (tilcdc_crtc->sync_lost_count++ > SYNC_LOST_COUNT_LIMIT) {
735 dev_err(dev->dev,
736 "%s(0x%08x): Sync lost flood detected, disabling the interrupt",
737 __func__, stat);
738 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
739 LCDC_SYNC_LOST);
740 }
741 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200742
743 if (stat & LCDC_FIFO_UNDERFLOW)
744 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
745 __func__, stat);
746
Rob Clark16ea9752013-01-08 15:04:28 -0600747 return IRQ_HANDLED;
748}
749
Rob Clark16ea9752013-01-08 15:04:28 -0600750struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
751{
752 struct tilcdc_crtc *tilcdc_crtc;
753 struct drm_crtc *crtc;
754 int ret;
755
756 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
757 if (!tilcdc_crtc) {
758 dev_err(dev->dev, "allocation failed\n");
759 return NULL;
760 }
761
762 crtc = &tilcdc_crtc->base;
763
764 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
765 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
766
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100767 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400768 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600769
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200770 spin_lock_init(&tilcdc_crtc->irq_lock);
771
Rob Clark16ea9752013-01-08 15:04:28 -0600772 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
773 if (ret < 0)
774 goto fail;
775
776 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
777
778 return crtc;
779
780fail:
781 tilcdc_crtc_destroy(crtc);
782 return NULL;
783}