blob: c2d5980b996529117a9c412a3d48c02a0aea37a6 [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
24struct tilcdc_crtc {
25 struct drm_crtc base;
26
27 const struct tilcdc_panel_info *info;
28 uint32_t dirty;
29 dma_addr_t start, end;
30 struct drm_pending_vblank_event *event;
31 int dpms;
32 wait_queue_head_t frame_done_wq;
33 bool frame_done;
34
35 /* fb currently set to scanout 0/1: */
36 struct drm_framebuffer *scanout[2];
37
38 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040039 struct drm_flip_work unref_work;
Rob Clark16ea9752013-01-08 15:04:28 -060040};
41#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
42
Rob Clarka464d612013-08-07 13:41:20 -040043static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060044{
Darren Etheridgef7b45752013-06-21 13:52:26 -050045 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040046 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060047 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060048
49 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040050 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060051 mutex_unlock(&dev->mode_config.mutex);
52}
53
54static void set_scanout(struct drm_crtc *crtc, int n)
55{
56 static const uint32_t base_reg[] = {
Darren Etheridgef7b45752013-06-21 13:52:26 -050057 LCDC_DMA_FB_BASE_ADDR_0_REG,
58 LCDC_DMA_FB_BASE_ADDR_1_REG,
Rob Clark16ea9752013-01-08 15:04:28 -060059 };
60 static const uint32_t ceil_reg[] = {
Darren Etheridgef7b45752013-06-21 13:52:26 -050061 LCDC_DMA_FB_CEILING_ADDR_0_REG,
62 LCDC_DMA_FB_CEILING_ADDR_1_REG,
Rob Clark16ea9752013-01-08 15:04:28 -060063 };
64 static const uint32_t stat[] = {
65 LCDC_END_OF_FRAME0, LCDC_END_OF_FRAME1,
66 };
67 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
68 struct drm_device *dev = crtc->dev;
Rob Clarka464d612013-08-07 13:41:20 -040069 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -060070
71 pm_runtime_get_sync(dev->dev);
72 tilcdc_write(dev, base_reg[n], tilcdc_crtc->start);
73 tilcdc_write(dev, ceil_reg[n], tilcdc_crtc->end);
74 if (tilcdc_crtc->scanout[n]) {
Rob Clarka464d612013-08-07 13:41:20 -040075 drm_flip_work_queue(&tilcdc_crtc->unref_work, tilcdc_crtc->scanout[n]);
76 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -060077 }
Matt Roperf4510a22014-04-01 15:22:40 -070078 tilcdc_crtc->scanout[n] = crtc->primary->fb;
Rob Clark16ea9752013-01-08 15:04:28 -060079 drm_framebuffer_reference(tilcdc_crtc->scanout[n]);
80 tilcdc_crtc->dirty &= ~stat[n];
81 pm_runtime_put_sync(dev->dev);
82}
83
84static void update_scanout(struct drm_crtc *crtc)
85{
86 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
87 struct drm_device *dev = crtc->dev;
Matt Roperf4510a22014-04-01 15:22:40 -070088 struct drm_framebuffer *fb = crtc->primary->fb;
Rob Clark16ea9752013-01-08 15:04:28 -060089 struct drm_gem_cma_object *gem;
90 unsigned int depth, bpp;
91
92 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
93 gem = drm_fb_cma_get_gem_obj(fb, 0);
94
95 tilcdc_crtc->start = gem->paddr + fb->offsets[0] +
96 (crtc->y * fb->pitches[0]) + (crtc->x * bpp/8);
97
98 tilcdc_crtc->end = tilcdc_crtc->start +
99 (crtc->mode.vdisplay * fb->pitches[0]);
100
101 if (tilcdc_crtc->dpms == DRM_MODE_DPMS_ON) {
102 /* already enabled, so just mark the frames that need
103 * updating and they will be updated on vblank:
104 */
105 tilcdc_crtc->dirty |= LCDC_END_OF_FRAME0 | LCDC_END_OF_FRAME1;
106 drm_vblank_get(dev, 0);
107 } else {
108 /* not enabled yet, so update registers immediately: */
109 set_scanout(crtc, 0);
110 set_scanout(crtc, 1);
111 }
112}
113
114static void start(struct drm_crtc *crtc)
115{
116 struct drm_device *dev = crtc->dev;
117 struct tilcdc_drm_private *priv = dev->dev_private;
118
119 if (priv->rev == 2) {
120 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
121 msleep(1);
122 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
123 msleep(1);
124 }
125
126 tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
127 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
128 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
129}
130
131static void stop(struct drm_crtc *crtc)
132{
133 struct drm_device *dev = crtc->dev;
134
135 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
136}
137
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200138static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode);
Rob Clark16ea9752013-01-08 15:04:28 -0600139static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
140{
141 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
142
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200143 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600144
145 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400146 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
147
Rob Clark16ea9752013-01-08 15:04:28 -0600148 kfree(tilcdc_crtc);
149}
150
151static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
152 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700153 struct drm_pending_vblank_event *event,
154 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600155{
156 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
157 struct drm_device *dev = crtc->dev;
158
159 if (tilcdc_crtc->event) {
160 dev_err(dev->dev, "already pending page flip!\n");
161 return -EBUSY;
162 }
163
Matt Roperf4510a22014-04-01 15:22:40 -0700164 crtc->primary->fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600165 tilcdc_crtc->event = event;
166 update_scanout(crtc);
167
168 return 0;
169}
170
171static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
172{
173 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
174 struct drm_device *dev = crtc->dev;
175 struct tilcdc_drm_private *priv = dev->dev_private;
176
177 /* we really only care about on or off: */
178 if (mode != DRM_MODE_DPMS_ON)
179 mode = DRM_MODE_DPMS_OFF;
180
181 if (tilcdc_crtc->dpms == mode)
182 return;
183
184 tilcdc_crtc->dpms = mode;
185
186 pm_runtime_get_sync(dev->dev);
187
188 if (mode == DRM_MODE_DPMS_ON) {
189 pm_runtime_forbid(dev->dev);
190 start(crtc);
191 } else {
192 tilcdc_crtc->frame_done = false;
193 stop(crtc);
194
Darren Etheridgef7b45752013-06-21 13:52:26 -0500195 /*
196 * if necessary wait for framedone irq which will still come
Rob Clark16ea9752013-01-08 15:04:28 -0600197 * before putting things to sleep..
198 */
199 if (priv->rev == 2) {
200 int ret = wait_event_timeout(
201 tilcdc_crtc->frame_done_wq,
202 tilcdc_crtc->frame_done,
203 msecs_to_jiffies(50));
204 if (ret == 0)
205 dev_err(dev->dev, "timeout waiting for framedone\n");
206 }
207 pm_runtime_allow(dev->dev);
208 }
209
210 pm_runtime_put_sync(dev->dev);
211}
212
213static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
214 const struct drm_display_mode *mode,
215 struct drm_display_mode *adjusted_mode)
216{
217 return true;
218}
219
220static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
221{
222 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
223}
224
225static void tilcdc_crtc_commit(struct drm_crtc *crtc)
226{
227 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
228}
229
230static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
231 struct drm_display_mode *mode,
232 struct drm_display_mode *adjusted_mode,
233 int x, int y,
234 struct drm_framebuffer *old_fb)
235{
236 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
237 struct drm_device *dev = crtc->dev;
238 struct tilcdc_drm_private *priv = dev->dev_private;
239 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
240 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
241 int ret;
242
243 ret = tilcdc_crtc_mode_valid(crtc, mode);
244 if (WARN_ON(ret))
245 return ret;
246
247 if (WARN_ON(!info))
248 return -EINVAL;
249
250 pm_runtime_get_sync(dev->dev);
251
252 /* Configure the Burst Size and fifo threshold of DMA: */
253 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
254 switch (info->dma_burst_sz) {
255 case 1:
256 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
257 break;
258 case 2:
259 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
260 break;
261 case 4:
262 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
263 break;
264 case 8:
265 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
266 break;
267 case 16:
268 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
269 break;
270 default:
271 return -EINVAL;
272 }
273 reg |= (info->fifo_th << 8);
274 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
275
276 /* Configure timings: */
277 hbp = mode->htotal - mode->hsync_end;
278 hfp = mode->hsync_start - mode->hdisplay;
279 hsw = mode->hsync_end - mode->hsync_start;
280 vbp = mode->vtotal - mode->vsync_end;
281 vfp = mode->vsync_start - mode->vdisplay;
282 vsw = mode->vsync_end - mode->vsync_start;
283
284 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
285 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
286
287 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
288 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
289 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
290 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500291
292 /*
293 * subtract one from hfp, hbp, hsw because the hardware uses
294 * a value of 0 as 1
295 */
Rob Clark16ea9752013-01-08 15:04:28 -0600296 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500297 /* clear bits we're going to set */
298 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500299 reg |= ((hfp-1) & 0x300) >> 8;
300 reg |= ((hbp-1) & 0x300) >> 4;
301 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600302 }
303 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
304
305 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500306 (((hbp-1) & 0xff) << 24) |
307 (((hfp-1) & 0xff) << 16) |
308 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600309 if (priv->rev == 2)
310 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
311 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
312
313 reg = ((mode->vdisplay - 1) & 0x3ff) |
314 ((vbp & 0xff) << 24) |
315 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500316 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600317 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
318
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500319 /*
320 * be sure to set Bit 10 for the V2 LCDC controller,
321 * otherwise limited to 1024 pixels width, stopping
322 * 1920x1080 being suppoted.
323 */
324 if (priv->rev == 2) {
325 if ((mode->vdisplay - 1) & 0x400) {
326 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
327 LCDC_LPP_B10);
328 } else {
329 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
330 LCDC_LPP_B10);
331 }
332 }
333
Rob Clark16ea9752013-01-08 15:04:28 -0600334 /* Configure display type: */
335 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
336 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
337 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
338 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
339 if (info->tft_alt_mode)
340 reg |= LCDC_TFT_ALT_ENABLE;
341 if (priv->rev == 2) {
342 unsigned int depth, bpp;
343
Matt Roperf4510a22014-04-01 15:22:40 -0700344 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600345 switch (bpp) {
346 case 16:
347 break;
348 case 32:
349 reg |= LCDC_V2_TFT_24BPP_UNPACK;
350 /* fallthrough */
351 case 24:
352 reg |= LCDC_V2_TFT_24BPP_MODE;
353 break;
354 default:
355 dev_err(dev->dev, "invalid pixel format\n");
356 return -EINVAL;
357 }
358 }
359 reg |= info->fdd < 12;
360 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
361
362 if (info->invert_pxl_clk)
363 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
364 else
365 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
366
367 if (info->sync_ctrl)
368 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
369 else
370 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
371
372 if (info->sync_edge)
373 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
374 else
375 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
376
Darren Etheridgea9767182013-08-14 21:43:33 +0200377 /*
378 * use value from adjusted_mode here as this might have been
379 * changed as part of the fixup for slave encoders to solve the
380 * issue where tilcdc timings are not VESA compliant
381 */
382 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600383 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
384 else
385 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
386
387 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
388 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
389 else
390 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
391
392 if (info->raster_order)
393 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
394 else
395 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
396
397
398 update_scanout(crtc);
399 tilcdc_crtc_update_clk(crtc);
400
401 pm_runtime_put_sync(dev->dev);
402
403 return 0;
404}
405
406static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
407 struct drm_framebuffer *old_fb)
408{
409 update_scanout(crtc);
410 return 0;
411}
412
Rob Clark16ea9752013-01-08 15:04:28 -0600413static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
414 .destroy = tilcdc_crtc_destroy,
415 .set_config = drm_crtc_helper_set_config,
416 .page_flip = tilcdc_crtc_page_flip,
417};
418
419static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
420 .dpms = tilcdc_crtc_dpms,
421 .mode_fixup = tilcdc_crtc_mode_fixup,
422 .prepare = tilcdc_crtc_prepare,
423 .commit = tilcdc_crtc_commit,
424 .mode_set = tilcdc_crtc_mode_set,
425 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600426};
427
428int tilcdc_crtc_max_width(struct drm_crtc *crtc)
429{
430 struct drm_device *dev = crtc->dev;
431 struct tilcdc_drm_private *priv = dev->dev_private;
432 int max_width = 0;
433
434 if (priv->rev == 1)
435 max_width = 1024;
436 else if (priv->rev == 2)
437 max_width = 2048;
438
439 return max_width;
440}
441
442int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
443{
444 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
445 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500446 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600447
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500448 /*
449 * check to see if the width is within the range that
450 * the LCD Controller physically supports
451 */
Rob Clark16ea9752013-01-08 15:04:28 -0600452 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
453 return MODE_VIRTUAL_X;
454
455 /* width must be multiple of 16 */
456 if (mode->hdisplay & 0xf)
457 return MODE_VIRTUAL_X;
458
459 if (mode->vdisplay > 2048)
460 return MODE_VIRTUAL_Y;
461
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500462 DBG("Processing mode %dx%d@%d with pixel clock %d",
463 mode->hdisplay, mode->vdisplay,
464 drm_mode_vrefresh(mode), mode->clock);
465
466 hbp = mode->htotal - mode->hsync_end;
467 hfp = mode->hsync_start - mode->hdisplay;
468 hsw = mode->hsync_end - mode->hsync_start;
469 vbp = mode->vtotal - mode->vsync_end;
470 vfp = mode->vsync_start - mode->vdisplay;
471 vsw = mode->vsync_end - mode->vsync_start;
472
473 if ((hbp-1) & ~0x3ff) {
474 DBG("Pruning mode: Horizontal Back Porch out of range");
475 return MODE_HBLANK_WIDE;
476 }
477
478 if ((hfp-1) & ~0x3ff) {
479 DBG("Pruning mode: Horizontal Front Porch out of range");
480 return MODE_HBLANK_WIDE;
481 }
482
483 if ((hsw-1) & ~0x3ff) {
484 DBG("Pruning mode: Horizontal Sync Width out of range");
485 return MODE_HSYNC_WIDE;
486 }
487
488 if (vbp & ~0xff) {
489 DBG("Pruning mode: Vertical Back Porch out of range");
490 return MODE_VBLANK_WIDE;
491 }
492
493 if (vfp & ~0xff) {
494 DBG("Pruning mode: Vertical Front Porch out of range");
495 return MODE_VBLANK_WIDE;
496 }
497
498 if ((vsw-1) & ~0x3f) {
499 DBG("Pruning mode: Vertical Sync Width out of range");
500 return MODE_VSYNC_WIDE;
501 }
502
Darren Etheridge4e564342013-06-21 13:52:23 -0500503 /*
504 * some devices have a maximum allowed pixel clock
505 * configured from the DT
506 */
507 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500508 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500509 return MODE_CLOCK_HIGH;
510 }
511
512 /*
513 * some devices further limit the max horizontal resolution
514 * configured from the DT
515 */
516 if (mode->hdisplay > priv->max_width)
517 return MODE_BAD_WIDTH;
518
Rob Clark16ea9752013-01-08 15:04:28 -0600519 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500520 bandwidth = mode->hdisplay * mode->vdisplay *
521 drm_mode_vrefresh(mode);
522 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500523 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600524 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500525 }
Rob Clark16ea9752013-01-08 15:04:28 -0600526
527 return MODE_OK;
528}
529
530void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
531 const struct tilcdc_panel_info *info)
532{
533 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
534 tilcdc_crtc->info = info;
535}
536
537void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
538{
539 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
540 struct drm_device *dev = crtc->dev;
541 struct tilcdc_drm_private *priv = dev->dev_private;
542 int dpms = tilcdc_crtc->dpms;
543 unsigned int lcd_clk, div;
544 int ret;
545
546 pm_runtime_get_sync(dev->dev);
547
548 if (dpms == DRM_MODE_DPMS_ON)
549 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
550
551 /* in raster mode, minimum divisor is 2: */
552 ret = clk_set_rate(priv->disp_clk, crtc->mode.clock * 1000 * 2);
553 if (ret) {
554 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
555 crtc->mode.clock);
556 goto out;
557 }
558
559 lcd_clk = clk_get_rate(priv->clk);
560 div = lcd_clk / (crtc->mode.clock * 1000);
561
562 DBG("lcd_clk=%u, mode clock=%d, div=%u", lcd_clk, crtc->mode.clock, div);
563 DBG("fck=%lu, dpll_disp_ck=%lu", clk_get_rate(priv->clk), clk_get_rate(priv->disp_clk));
564
565 /* Configure the LCD clock divisor. */
566 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(div) |
567 LCDC_RASTER_MODE);
568
569 if (priv->rev == 2)
570 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
571 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
572 LCDC_V2_CORE_CLK_EN);
573
574 if (dpms == DRM_MODE_DPMS_ON)
575 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
576
577out:
578 pm_runtime_put_sync(dev->dev);
579}
580
581irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
582{
583 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
584 struct drm_device *dev = crtc->dev;
585 struct tilcdc_drm_private *priv = dev->dev_private;
586 uint32_t stat = tilcdc_read_irqstatus(dev);
587
588 if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
589 stop(crtc);
590 dev_err(dev->dev, "error: %08x\n", stat);
591 tilcdc_clear_irqstatus(dev, stat);
592 start(crtc);
593 } else if (stat & LCDC_PL_LOAD_DONE) {
594 tilcdc_clear_irqstatus(dev, stat);
595 } else {
596 struct drm_pending_vblank_event *event;
597 unsigned long flags;
598 uint32_t dirty = tilcdc_crtc->dirty & stat;
599
600 tilcdc_clear_irqstatus(dev, stat);
601
602 if (dirty & LCDC_END_OF_FRAME0)
603 set_scanout(crtc, 0);
604
605 if (dirty & LCDC_END_OF_FRAME1)
606 set_scanout(crtc, 1);
607
608 drm_handle_vblank(dev, 0);
609
610 spin_lock_irqsave(&dev->event_lock, flags);
611 event = tilcdc_crtc->event;
612 tilcdc_crtc->event = NULL;
613 if (event)
614 drm_send_vblank_event(dev, 0, event);
615 spin_unlock_irqrestore(&dev->event_lock, flags);
616
617 if (dirty && !tilcdc_crtc->dirty)
618 drm_vblank_put(dev, 0);
619 }
620
621 if (priv->rev == 2) {
622 if (stat & LCDC_FRAME_DONE) {
623 tilcdc_crtc->frame_done = true;
624 wake_up(&tilcdc_crtc->frame_done_wq);
625 }
626 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
627 }
628
629 return IRQ_HANDLED;
630}
631
632void tilcdc_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
633{
634 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
635 struct drm_pending_vblank_event *event;
636 struct drm_device *dev = crtc->dev;
637 unsigned long flags;
638
639 /* Destroy the pending vertical blanking event associated with the
640 * pending page flip, if any, and disable vertical blanking interrupts.
641 */
642 spin_lock_irqsave(&dev->event_lock, flags);
643 event = tilcdc_crtc->event;
644 if (event && event->base.file_priv == file) {
645 tilcdc_crtc->event = NULL;
646 event->base.destroy(&event->base);
647 drm_vblank_put(dev, 0);
648 }
649 spin_unlock_irqrestore(&dev->event_lock, flags);
650}
651
652struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
653{
654 struct tilcdc_crtc *tilcdc_crtc;
655 struct drm_crtc *crtc;
656 int ret;
657
658 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
659 if (!tilcdc_crtc) {
660 dev_err(dev->dev, "allocation failed\n");
661 return NULL;
662 }
663
664 crtc = &tilcdc_crtc->base;
665
666 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
667 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
668
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100669 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400670 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600671
Rob Clark16ea9752013-01-08 15:04:28 -0600672 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
673 if (ret < 0)
674 goto fail;
675
676 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
677
678 return crtc;
679
680fail:
681 tilcdc_crtc_destroy(crtc);
682 return NULL;
683}