blob: 08b1e03b502e8f2802a06f662e5da9177eb40001 [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;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020040
41 /* Only set if an external encoder is connected */
42 bool simulate_vesa_sync;
Rob Clark16ea9752013-01-08 15:04:28 -060043};
44#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
45
Rob Clarka464d612013-08-07 13:41:20 -040046static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060047{
Darren Etheridgef7b45752013-06-21 13:52:26 -050048 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040049 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060050 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060051
52 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040053 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060054 mutex_unlock(&dev->mode_config.mutex);
55}
56
57static void set_scanout(struct drm_crtc *crtc, int n)
58{
59 static const uint32_t base_reg[] = {
Darren Etheridgef7b45752013-06-21 13:52:26 -050060 LCDC_DMA_FB_BASE_ADDR_0_REG,
61 LCDC_DMA_FB_BASE_ADDR_1_REG,
Rob Clark16ea9752013-01-08 15:04:28 -060062 };
63 static const uint32_t ceil_reg[] = {
Darren Etheridgef7b45752013-06-21 13:52:26 -050064 LCDC_DMA_FB_CEILING_ADDR_0_REG,
65 LCDC_DMA_FB_CEILING_ADDR_1_REG,
Rob Clark16ea9752013-01-08 15:04:28 -060066 };
67 static const uint32_t stat[] = {
68 LCDC_END_OF_FRAME0, LCDC_END_OF_FRAME1,
69 };
70 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
71 struct drm_device *dev = crtc->dev;
Rob Clarka464d612013-08-07 13:41:20 -040072 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -060073
Rob Clark16ea9752013-01-08 15:04:28 -060074 tilcdc_write(dev, base_reg[n], tilcdc_crtc->start);
75 tilcdc_write(dev, ceil_reg[n], tilcdc_crtc->end);
76 if (tilcdc_crtc->scanout[n]) {
Rob Clarka464d612013-08-07 13:41:20 -040077 drm_flip_work_queue(&tilcdc_crtc->unref_work, tilcdc_crtc->scanout[n]);
78 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -060079 }
Matt Roperf4510a22014-04-01 15:22:40 -070080 tilcdc_crtc->scanout[n] = crtc->primary->fb;
Rob Clark16ea9752013-01-08 15:04:28 -060081 drm_framebuffer_reference(tilcdc_crtc->scanout[n]);
82 tilcdc_crtc->dirty &= ~stat[n];
Rob Clark16ea9752013-01-08 15:04:28 -060083}
84
85static void update_scanout(struct drm_crtc *crtc)
86{
87 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
88 struct drm_device *dev = crtc->dev;
Matt Roperf4510a22014-04-01 15:22:40 -070089 struct drm_framebuffer *fb = crtc->primary->fb;
Rob Clark16ea9752013-01-08 15:04:28 -060090 struct drm_gem_cma_object *gem;
91 unsigned int depth, bpp;
92
93 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
94 gem = drm_fb_cma_get_gem_obj(fb, 0);
95
96 tilcdc_crtc->start = gem->paddr + fb->offsets[0] +
97 (crtc->y * fb->pitches[0]) + (crtc->x * bpp/8);
98
99 tilcdc_crtc->end = tilcdc_crtc->start +
100 (crtc->mode.vdisplay * fb->pitches[0]);
101
102 if (tilcdc_crtc->dpms == DRM_MODE_DPMS_ON) {
103 /* already enabled, so just mark the frames that need
104 * updating and they will be updated on vblank:
105 */
106 tilcdc_crtc->dirty |= LCDC_END_OF_FRAME0 | LCDC_END_OF_FRAME1;
107 drm_vblank_get(dev, 0);
108 } else {
109 /* not enabled yet, so update registers immediately: */
110 set_scanout(crtc, 0);
111 set_scanout(crtc, 1);
112 }
113}
114
115static void start(struct drm_crtc *crtc)
116{
117 struct drm_device *dev = crtc->dev;
118 struct tilcdc_drm_private *priv = dev->dev_private;
119
120 if (priv->rev == 2) {
121 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
122 msleep(1);
123 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
124 msleep(1);
125 }
126
127 tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
128 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
129 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
130}
131
132static void stop(struct drm_crtc *crtc)
133{
134 struct drm_device *dev = crtc->dev;
135
136 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
137}
138
139static 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
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000151static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
152{
153 struct drm_device *dev = crtc->dev;
154 unsigned int depth, bpp;
155
156 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
157
158 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
159 dev_err(dev->dev,
160 "Invalid pitch: fb and crtc widths must be the same");
161 return -EINVAL;
162 }
163
164 return 0;
165}
166
Rob Clark16ea9752013-01-08 15:04:28 -0600167static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
168 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700169 struct drm_pending_vblank_event *event,
170 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600171{
172 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
173 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000174 int r;
175
176 r = tilcdc_verify_fb(crtc, fb);
177 if (r)
178 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600179
180 if (tilcdc_crtc->event) {
181 dev_err(dev->dev, "already pending page flip!\n");
182 return -EBUSY;
183 }
184
Matt Roperf4510a22014-04-01 15:22:40 -0700185 crtc->primary->fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600186 tilcdc_crtc->event = event;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300187
188 pm_runtime_get_sync(dev->dev);
189
Rob Clark16ea9752013-01-08 15:04:28 -0600190 update_scanout(crtc);
191
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);
233 }
Rob Clark16ea9752013-01-08 15:04:28 -0600234}
235
236static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
237 const struct drm_display_mode *mode,
238 struct drm_display_mode *adjusted_mode)
239{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200240 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
241
242 if (!tilcdc_crtc->simulate_vesa_sync)
243 return true;
244
245 /*
246 * tilcdc does not generate VESA-compliant sync but aligns
247 * VS on the second edge of HS instead of first edge.
248 * We use adjusted_mode, to fixup sync by aligning both rising
249 * edges and add HSKEW offset to fix the sync.
250 */
251 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
252 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
253
254 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
255 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
256 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
257 } else {
258 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
259 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
260 }
261
Rob Clark16ea9752013-01-08 15:04:28 -0600262 return true;
263}
264
265static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
266{
267 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
268}
269
270static void tilcdc_crtc_commit(struct drm_crtc *crtc)
271{
272 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
273}
274
275static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
276 struct drm_display_mode *mode,
277 struct drm_display_mode *adjusted_mode,
278 int x, int y,
279 struct drm_framebuffer *old_fb)
280{
281 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
282 struct drm_device *dev = crtc->dev;
283 struct tilcdc_drm_private *priv = dev->dev_private;
284 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
285 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
286 int ret;
287
288 ret = tilcdc_crtc_mode_valid(crtc, mode);
289 if (WARN_ON(ret))
290 return ret;
291
292 if (WARN_ON(!info))
293 return -EINVAL;
294
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000295 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
296 if (ret)
297 return ret;
298
Rob Clark16ea9752013-01-08 15:04:28 -0600299 pm_runtime_get_sync(dev->dev);
300
301 /* Configure the Burst Size and fifo threshold of DMA: */
302 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
303 switch (info->dma_burst_sz) {
304 case 1:
305 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
306 break;
307 case 2:
308 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
309 break;
310 case 4:
311 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
312 break;
313 case 8:
314 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
315 break;
316 case 16:
317 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
318 break;
319 default:
320 return -EINVAL;
321 }
322 reg |= (info->fifo_th << 8);
323 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
324
325 /* Configure timings: */
326 hbp = mode->htotal - mode->hsync_end;
327 hfp = mode->hsync_start - mode->hdisplay;
328 hsw = mode->hsync_end - mode->hsync_start;
329 vbp = mode->vtotal - mode->vsync_end;
330 vfp = mode->vsync_start - mode->vdisplay;
331 vsw = mode->vsync_end - mode->vsync_start;
332
333 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
334 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
335
336 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
337 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
338 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
339 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500340
341 /*
342 * subtract one from hfp, hbp, hsw because the hardware uses
343 * a value of 0 as 1
344 */
Rob Clark16ea9752013-01-08 15:04:28 -0600345 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500346 /* clear bits we're going to set */
347 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500348 reg |= ((hfp-1) & 0x300) >> 8;
349 reg |= ((hbp-1) & 0x300) >> 4;
350 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600351 }
352 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
353
354 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500355 (((hbp-1) & 0xff) << 24) |
356 (((hfp-1) & 0xff) << 16) |
357 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600358 if (priv->rev == 2)
359 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
360 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
361
362 reg = ((mode->vdisplay - 1) & 0x3ff) |
363 ((vbp & 0xff) << 24) |
364 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500365 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600366 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
367
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500368 /*
369 * be sure to set Bit 10 for the V2 LCDC controller,
370 * otherwise limited to 1024 pixels width, stopping
371 * 1920x1080 being suppoted.
372 */
373 if (priv->rev == 2) {
374 if ((mode->vdisplay - 1) & 0x400) {
375 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
376 LCDC_LPP_B10);
377 } else {
378 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
379 LCDC_LPP_B10);
380 }
381 }
382
Rob Clark16ea9752013-01-08 15:04:28 -0600383 /* Configure display type: */
384 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
385 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
386 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
387 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
388 if (info->tft_alt_mode)
389 reg |= LCDC_TFT_ALT_ENABLE;
390 if (priv->rev == 2) {
391 unsigned int depth, bpp;
392
Matt Roperf4510a22014-04-01 15:22:40 -0700393 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600394 switch (bpp) {
395 case 16:
396 break;
397 case 32:
398 reg |= LCDC_V2_TFT_24BPP_UNPACK;
399 /* fallthrough */
400 case 24:
401 reg |= LCDC_V2_TFT_24BPP_MODE;
402 break;
403 default:
404 dev_err(dev->dev, "invalid pixel format\n");
405 return -EINVAL;
406 }
407 }
408 reg |= info->fdd < 12;
409 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
410
411 if (info->invert_pxl_clk)
412 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
413 else
414 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
415
416 if (info->sync_ctrl)
417 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
418 else
419 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
420
421 if (info->sync_edge)
422 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
423 else
424 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
425
Darren Etheridgea9767182013-08-14 21:43:33 +0200426 /*
427 * use value from adjusted_mode here as this might have been
428 * changed as part of the fixup for slave encoders to solve the
429 * issue where tilcdc timings are not VESA compliant
430 */
431 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600432 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
433 else
434 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
435
436 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
437 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
438 else
439 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
440
441 if (info->raster_order)
442 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
443 else
444 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
445
446
447 update_scanout(crtc);
448 tilcdc_crtc_update_clk(crtc);
449
450 pm_runtime_put_sync(dev->dev);
451
452 return 0;
453}
454
455static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
456 struct drm_framebuffer *old_fb)
457{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300458 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000459 int r;
460
461 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
462 if (r)
463 return r;
464
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300465 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600466 update_scanout(crtc);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300467 pm_runtime_put_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600468 return 0;
469}
470
Rob Clark16ea9752013-01-08 15:04:28 -0600471static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
472 .destroy = tilcdc_crtc_destroy,
473 .set_config = drm_crtc_helper_set_config,
474 .page_flip = tilcdc_crtc_page_flip,
475};
476
477static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
478 .dpms = tilcdc_crtc_dpms,
479 .mode_fixup = tilcdc_crtc_mode_fixup,
480 .prepare = tilcdc_crtc_prepare,
481 .commit = tilcdc_crtc_commit,
482 .mode_set = tilcdc_crtc_mode_set,
483 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600484};
485
486int tilcdc_crtc_max_width(struct drm_crtc *crtc)
487{
488 struct drm_device *dev = crtc->dev;
489 struct tilcdc_drm_private *priv = dev->dev_private;
490 int max_width = 0;
491
492 if (priv->rev == 1)
493 max_width = 1024;
494 else if (priv->rev == 2)
495 max_width = 2048;
496
497 return max_width;
498}
499
500int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
501{
502 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
503 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500504 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600505
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500506 /*
507 * check to see if the width is within the range that
508 * the LCD Controller physically supports
509 */
Rob Clark16ea9752013-01-08 15:04:28 -0600510 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
511 return MODE_VIRTUAL_X;
512
513 /* width must be multiple of 16 */
514 if (mode->hdisplay & 0xf)
515 return MODE_VIRTUAL_X;
516
517 if (mode->vdisplay > 2048)
518 return MODE_VIRTUAL_Y;
519
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500520 DBG("Processing mode %dx%d@%d with pixel clock %d",
521 mode->hdisplay, mode->vdisplay,
522 drm_mode_vrefresh(mode), mode->clock);
523
524 hbp = mode->htotal - mode->hsync_end;
525 hfp = mode->hsync_start - mode->hdisplay;
526 hsw = mode->hsync_end - mode->hsync_start;
527 vbp = mode->vtotal - mode->vsync_end;
528 vfp = mode->vsync_start - mode->vdisplay;
529 vsw = mode->vsync_end - mode->vsync_start;
530
531 if ((hbp-1) & ~0x3ff) {
532 DBG("Pruning mode: Horizontal Back Porch out of range");
533 return MODE_HBLANK_WIDE;
534 }
535
536 if ((hfp-1) & ~0x3ff) {
537 DBG("Pruning mode: Horizontal Front Porch out of range");
538 return MODE_HBLANK_WIDE;
539 }
540
541 if ((hsw-1) & ~0x3ff) {
542 DBG("Pruning mode: Horizontal Sync Width out of range");
543 return MODE_HSYNC_WIDE;
544 }
545
546 if (vbp & ~0xff) {
547 DBG("Pruning mode: Vertical Back Porch out of range");
548 return MODE_VBLANK_WIDE;
549 }
550
551 if (vfp & ~0xff) {
552 DBG("Pruning mode: Vertical Front Porch out of range");
553 return MODE_VBLANK_WIDE;
554 }
555
556 if ((vsw-1) & ~0x3f) {
557 DBG("Pruning mode: Vertical Sync Width out of range");
558 return MODE_VSYNC_WIDE;
559 }
560
Darren Etheridge4e564342013-06-21 13:52:23 -0500561 /*
562 * some devices have a maximum allowed pixel clock
563 * configured from the DT
564 */
565 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500566 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500567 return MODE_CLOCK_HIGH;
568 }
569
570 /*
571 * some devices further limit the max horizontal resolution
572 * configured from the DT
573 */
574 if (mode->hdisplay > priv->max_width)
575 return MODE_BAD_WIDTH;
576
Rob Clark16ea9752013-01-08 15:04:28 -0600577 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500578 bandwidth = mode->hdisplay * mode->vdisplay *
579 drm_mode_vrefresh(mode);
580 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500581 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600582 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500583 }
Rob Clark16ea9752013-01-08 15:04:28 -0600584
585 return MODE_OK;
586}
587
588void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
589 const struct tilcdc_panel_info *info)
590{
591 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
592 tilcdc_crtc->info = info;
593}
594
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200595void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
596 bool simulate_vesa_sync)
597{
598 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
599
600 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
601}
602
Rob Clark16ea9752013-01-08 15:04:28 -0600603void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
604{
605 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
606 struct drm_device *dev = crtc->dev;
607 struct tilcdc_drm_private *priv = dev->dev_private;
608 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600609 unsigned long lcd_clk;
610 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600611 int ret;
612
613 pm_runtime_get_sync(dev->dev);
614
615 if (dpms == DRM_MODE_DPMS_ON)
616 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
617
Darren Etheridge3d193062014-01-15 15:52:36 -0600618 /* mode.clock is in KHz, set_rate wants parameter in Hz */
619 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
620 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600621 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
622 crtc->mode.clock);
623 goto out;
624 }
625
626 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600627
Darren Etheridge3d193062014-01-15 15:52:36 -0600628 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
629 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600630
631 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600632 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600633 LCDC_RASTER_MODE);
634
635 if (priv->rev == 2)
636 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
637 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
638 LCDC_V2_CORE_CLK_EN);
639
640 if (dpms == DRM_MODE_DPMS_ON)
641 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
642
643out:
644 pm_runtime_put_sync(dev->dev);
645}
646
647irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
648{
649 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
650 struct drm_device *dev = crtc->dev;
651 struct tilcdc_drm_private *priv = dev->dev_private;
652 uint32_t stat = tilcdc_read_irqstatus(dev);
653
654 if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
655 stop(crtc);
656 dev_err(dev->dev, "error: %08x\n", stat);
657 tilcdc_clear_irqstatus(dev, stat);
658 start(crtc);
659 } else if (stat & LCDC_PL_LOAD_DONE) {
660 tilcdc_clear_irqstatus(dev, stat);
661 } else {
662 struct drm_pending_vblank_event *event;
663 unsigned long flags;
664 uint32_t dirty = tilcdc_crtc->dirty & stat;
665
666 tilcdc_clear_irqstatus(dev, stat);
667
668 if (dirty & LCDC_END_OF_FRAME0)
669 set_scanout(crtc, 0);
670
671 if (dirty & LCDC_END_OF_FRAME1)
672 set_scanout(crtc, 1);
673
674 drm_handle_vblank(dev, 0);
675
676 spin_lock_irqsave(&dev->event_lock, flags);
677 event = tilcdc_crtc->event;
678 tilcdc_crtc->event = NULL;
679 if (event)
680 drm_send_vblank_event(dev, 0, event);
681 spin_unlock_irqrestore(&dev->event_lock, flags);
682
683 if (dirty && !tilcdc_crtc->dirty)
684 drm_vblank_put(dev, 0);
685 }
686
687 if (priv->rev == 2) {
688 if (stat & LCDC_FRAME_DONE) {
689 tilcdc_crtc->frame_done = true;
690 wake_up(&tilcdc_crtc->frame_done_wq);
691 }
692 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
693 }
694
695 return IRQ_HANDLED;
696}
697
Rob Clark16ea9752013-01-08 15:04:28 -0600698struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
699{
700 struct tilcdc_crtc *tilcdc_crtc;
701 struct drm_crtc *crtc;
702 int ret;
703
704 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
705 if (!tilcdc_crtc) {
706 dev_err(dev->dev, "allocation failed\n");
707 return NULL;
708 }
709
710 crtc = &tilcdc_crtc->base;
711
712 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
713 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
714
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100715 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400716 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600717
Rob Clark16ea9752013-01-08 15:04:28 -0600718 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
719 if (ret < 0)
720 goto fail;
721
722 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
723
724 return crtc;
725
726fail:
727 tilcdc_crtc_destroy(crtc);
728 return NULL;
729}