blob: 465fd047875a9972856321fa1dafe0c2802209c6 [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
74 pm_runtime_get_sync(dev->dev);
75 tilcdc_write(dev, base_reg[n], tilcdc_crtc->start);
76 tilcdc_write(dev, ceil_reg[n], tilcdc_crtc->end);
77 if (tilcdc_crtc->scanout[n]) {
Rob Clarka464d612013-08-07 13:41:20 -040078 drm_flip_work_queue(&tilcdc_crtc->unref_work, tilcdc_crtc->scanout[n]);
79 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -060080 }
Matt Roperf4510a22014-04-01 15:22:40 -070081 tilcdc_crtc->scanout[n] = crtc->primary->fb;
Rob Clark16ea9752013-01-08 15:04:28 -060082 drm_framebuffer_reference(tilcdc_crtc->scanout[n]);
83 tilcdc_crtc->dirty &= ~stat[n];
84 pm_runtime_put_sync(dev->dev);
85}
86
87static void update_scanout(struct drm_crtc *crtc)
88{
89 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
90 struct drm_device *dev = crtc->dev;
Matt Roperf4510a22014-04-01 15:22:40 -070091 struct drm_framebuffer *fb = crtc->primary->fb;
Rob Clark16ea9752013-01-08 15:04:28 -060092 struct drm_gem_cma_object *gem;
93 unsigned int depth, bpp;
94
95 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
96 gem = drm_fb_cma_get_gem_obj(fb, 0);
97
98 tilcdc_crtc->start = gem->paddr + fb->offsets[0] +
99 (crtc->y * fb->pitches[0]) + (crtc->x * bpp/8);
100
101 tilcdc_crtc->end = tilcdc_crtc->start +
102 (crtc->mode.vdisplay * fb->pitches[0]);
103
104 if (tilcdc_crtc->dpms == DRM_MODE_DPMS_ON) {
105 /* already enabled, so just mark the frames that need
106 * updating and they will be updated on vblank:
107 */
108 tilcdc_crtc->dirty |= LCDC_END_OF_FRAME0 | LCDC_END_OF_FRAME1;
109 drm_vblank_get(dev, 0);
110 } else {
111 /* not enabled yet, so update registers immediately: */
112 set_scanout(crtc, 0);
113 set_scanout(crtc, 1);
114 }
115}
116
117static void start(struct drm_crtc *crtc)
118{
119 struct drm_device *dev = crtc->dev;
120 struct tilcdc_drm_private *priv = dev->dev_private;
121
122 if (priv->rev == 2) {
123 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
124 msleep(1);
125 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
126 msleep(1);
127 }
128
129 tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
130 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
131 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
132}
133
134static void stop(struct drm_crtc *crtc)
135{
136 struct drm_device *dev = crtc->dev;
137
138 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
139}
140
141static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
142{
143 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
144
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200145 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600146
147 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400148 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
149
Rob Clark16ea9752013-01-08 15:04:28 -0600150 kfree(tilcdc_crtc);
151}
152
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000153static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
154{
155 struct drm_device *dev = crtc->dev;
156 unsigned int depth, bpp;
157
158 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
159
160 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
161 dev_err(dev->dev,
162 "Invalid pitch: fb and crtc widths must be the same");
163 return -EINVAL;
164 }
165
166 return 0;
167}
168
Rob Clark16ea9752013-01-08 15:04:28 -0600169static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
170 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700171 struct drm_pending_vblank_event *event,
172 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600173{
174 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
175 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000176 int r;
177
178 r = tilcdc_verify_fb(crtc, fb);
179 if (r)
180 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600181
182 if (tilcdc_crtc->event) {
183 dev_err(dev->dev, "already pending page flip!\n");
184 return -EBUSY;
185 }
186
Matt Roperf4510a22014-04-01 15:22:40 -0700187 crtc->primary->fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600188 tilcdc_crtc->event = event;
189 update_scanout(crtc);
190
191 return 0;
192}
193
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000194void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
Rob Clark16ea9752013-01-08 15:04:28 -0600195{
196 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
197 struct drm_device *dev = crtc->dev;
198 struct tilcdc_drm_private *priv = dev->dev_private;
199
200 /* we really only care about on or off: */
201 if (mode != DRM_MODE_DPMS_ON)
202 mode = DRM_MODE_DPMS_OFF;
203
204 if (tilcdc_crtc->dpms == mode)
205 return;
206
207 tilcdc_crtc->dpms = mode;
208
209 pm_runtime_get_sync(dev->dev);
210
211 if (mode == DRM_MODE_DPMS_ON) {
212 pm_runtime_forbid(dev->dev);
213 start(crtc);
214 } else {
215 tilcdc_crtc->frame_done = false;
216 stop(crtc);
217
Darren Etheridgef7b45752013-06-21 13:52:26 -0500218 /*
219 * if necessary wait for framedone irq which will still come
Rob Clark16ea9752013-01-08 15:04:28 -0600220 * before putting things to sleep..
221 */
222 if (priv->rev == 2) {
223 int ret = wait_event_timeout(
224 tilcdc_crtc->frame_done_wq,
225 tilcdc_crtc->frame_done,
226 msecs_to_jiffies(50));
227 if (ret == 0)
228 dev_err(dev->dev, "timeout waiting for framedone\n");
229 }
230 pm_runtime_allow(dev->dev);
231 }
232
233 pm_runtime_put_sync(dev->dev);
234}
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 Valkeinen6f206e92014-02-07 17:37:07 +0000458 int r;
459
460 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
461 if (r)
462 return r;
463
Rob Clark16ea9752013-01-08 15:04:28 -0600464 update_scanout(crtc);
465 return 0;
466}
467
Rob Clark16ea9752013-01-08 15:04:28 -0600468static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
469 .destroy = tilcdc_crtc_destroy,
470 .set_config = drm_crtc_helper_set_config,
471 .page_flip = tilcdc_crtc_page_flip,
472};
473
474static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
475 .dpms = tilcdc_crtc_dpms,
476 .mode_fixup = tilcdc_crtc_mode_fixup,
477 .prepare = tilcdc_crtc_prepare,
478 .commit = tilcdc_crtc_commit,
479 .mode_set = tilcdc_crtc_mode_set,
480 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600481};
482
483int tilcdc_crtc_max_width(struct drm_crtc *crtc)
484{
485 struct drm_device *dev = crtc->dev;
486 struct tilcdc_drm_private *priv = dev->dev_private;
487 int max_width = 0;
488
489 if (priv->rev == 1)
490 max_width = 1024;
491 else if (priv->rev == 2)
492 max_width = 2048;
493
494 return max_width;
495}
496
497int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
498{
499 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
500 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500501 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600502
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500503 /*
504 * check to see if the width is within the range that
505 * the LCD Controller physically supports
506 */
Rob Clark16ea9752013-01-08 15:04:28 -0600507 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
508 return MODE_VIRTUAL_X;
509
510 /* width must be multiple of 16 */
511 if (mode->hdisplay & 0xf)
512 return MODE_VIRTUAL_X;
513
514 if (mode->vdisplay > 2048)
515 return MODE_VIRTUAL_Y;
516
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500517 DBG("Processing mode %dx%d@%d with pixel clock %d",
518 mode->hdisplay, mode->vdisplay,
519 drm_mode_vrefresh(mode), mode->clock);
520
521 hbp = mode->htotal - mode->hsync_end;
522 hfp = mode->hsync_start - mode->hdisplay;
523 hsw = mode->hsync_end - mode->hsync_start;
524 vbp = mode->vtotal - mode->vsync_end;
525 vfp = mode->vsync_start - mode->vdisplay;
526 vsw = mode->vsync_end - mode->vsync_start;
527
528 if ((hbp-1) & ~0x3ff) {
529 DBG("Pruning mode: Horizontal Back Porch out of range");
530 return MODE_HBLANK_WIDE;
531 }
532
533 if ((hfp-1) & ~0x3ff) {
534 DBG("Pruning mode: Horizontal Front Porch out of range");
535 return MODE_HBLANK_WIDE;
536 }
537
538 if ((hsw-1) & ~0x3ff) {
539 DBG("Pruning mode: Horizontal Sync Width out of range");
540 return MODE_HSYNC_WIDE;
541 }
542
543 if (vbp & ~0xff) {
544 DBG("Pruning mode: Vertical Back Porch out of range");
545 return MODE_VBLANK_WIDE;
546 }
547
548 if (vfp & ~0xff) {
549 DBG("Pruning mode: Vertical Front Porch out of range");
550 return MODE_VBLANK_WIDE;
551 }
552
553 if ((vsw-1) & ~0x3f) {
554 DBG("Pruning mode: Vertical Sync Width out of range");
555 return MODE_VSYNC_WIDE;
556 }
557
Darren Etheridge4e564342013-06-21 13:52:23 -0500558 /*
559 * some devices have a maximum allowed pixel clock
560 * configured from the DT
561 */
562 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500563 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500564 return MODE_CLOCK_HIGH;
565 }
566
567 /*
568 * some devices further limit the max horizontal resolution
569 * configured from the DT
570 */
571 if (mode->hdisplay > priv->max_width)
572 return MODE_BAD_WIDTH;
573
Rob Clark16ea9752013-01-08 15:04:28 -0600574 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500575 bandwidth = mode->hdisplay * mode->vdisplay *
576 drm_mode_vrefresh(mode);
577 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500578 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600579 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500580 }
Rob Clark16ea9752013-01-08 15:04:28 -0600581
582 return MODE_OK;
583}
584
585void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
586 const struct tilcdc_panel_info *info)
587{
588 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
589 tilcdc_crtc->info = info;
590}
591
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200592void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
593 bool simulate_vesa_sync)
594{
595 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
596
597 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
598}
599
Rob Clark16ea9752013-01-08 15:04:28 -0600600void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
601{
602 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
603 struct drm_device *dev = crtc->dev;
604 struct tilcdc_drm_private *priv = dev->dev_private;
605 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600606 unsigned long lcd_clk;
607 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600608 int ret;
609
610 pm_runtime_get_sync(dev->dev);
611
612 if (dpms == DRM_MODE_DPMS_ON)
613 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
614
Darren Etheridge3d193062014-01-15 15:52:36 -0600615 /* mode.clock is in KHz, set_rate wants parameter in Hz */
616 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
617 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600618 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
619 crtc->mode.clock);
620 goto out;
621 }
622
623 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600624
Darren Etheridge3d193062014-01-15 15:52:36 -0600625 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
626 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600627
628 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600629 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600630 LCDC_RASTER_MODE);
631
632 if (priv->rev == 2)
633 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
634 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
635 LCDC_V2_CORE_CLK_EN);
636
637 if (dpms == DRM_MODE_DPMS_ON)
638 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
639
640out:
641 pm_runtime_put_sync(dev->dev);
642}
643
644irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
645{
646 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
647 struct drm_device *dev = crtc->dev;
648 struct tilcdc_drm_private *priv = dev->dev_private;
649 uint32_t stat = tilcdc_read_irqstatus(dev);
650
651 if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
652 stop(crtc);
653 dev_err(dev->dev, "error: %08x\n", stat);
654 tilcdc_clear_irqstatus(dev, stat);
655 start(crtc);
656 } else if (stat & LCDC_PL_LOAD_DONE) {
657 tilcdc_clear_irqstatus(dev, stat);
658 } else {
659 struct drm_pending_vblank_event *event;
660 unsigned long flags;
661 uint32_t dirty = tilcdc_crtc->dirty & stat;
662
663 tilcdc_clear_irqstatus(dev, stat);
664
665 if (dirty & LCDC_END_OF_FRAME0)
666 set_scanout(crtc, 0);
667
668 if (dirty & LCDC_END_OF_FRAME1)
669 set_scanout(crtc, 1);
670
671 drm_handle_vblank(dev, 0);
672
673 spin_lock_irqsave(&dev->event_lock, flags);
674 event = tilcdc_crtc->event;
675 tilcdc_crtc->event = NULL;
676 if (event)
677 drm_send_vblank_event(dev, 0, event);
678 spin_unlock_irqrestore(&dev->event_lock, flags);
679
680 if (dirty && !tilcdc_crtc->dirty)
681 drm_vblank_put(dev, 0);
682 }
683
684 if (priv->rev == 2) {
685 if (stat & LCDC_FRAME_DONE) {
686 tilcdc_crtc->frame_done = true;
687 wake_up(&tilcdc_crtc->frame_done_wq);
688 }
689 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
690 }
691
692 return IRQ_HANDLED;
693}
694
Rob Clark16ea9752013-01-08 15:04:28 -0600695struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
696{
697 struct tilcdc_crtc *tilcdc_crtc;
698 struct drm_crtc *crtc;
699 int ret;
700
701 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
702 if (!tilcdc_crtc) {
703 dev_err(dev->dev, "allocation failed\n");
704 return NULL;
705 }
706
707 crtc = &tilcdc_crtc->base;
708
709 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
710 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
711
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100712 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400713 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600714
Rob Clark16ea9752013-01-08 15:04:28 -0600715 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
716 if (ret < 0)
717 goto fail;
718
719 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
720
721 return crtc;
722
723fail:
724 tilcdc_crtc_destroy(crtc);
725 return NULL;
726}