blob: 6485e1c03fe31606a50492bba8a88469360d21c4 [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
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200141static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode);
Rob Clark16ea9752013-01-08 15:04:28 -0600142static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
143{
144 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
145
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200146 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600147
148 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400149 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
150
Rob Clark16ea9752013-01-08 15:04:28 -0600151 kfree(tilcdc_crtc);
152}
153
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000154static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
155{
156 struct drm_device *dev = crtc->dev;
157 unsigned int depth, bpp;
158
159 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
160
161 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
162 dev_err(dev->dev,
163 "Invalid pitch: fb and crtc widths must be the same");
164 return -EINVAL;
165 }
166
167 return 0;
168}
169
Rob Clark16ea9752013-01-08 15:04:28 -0600170static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
171 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700172 struct drm_pending_vblank_event *event,
173 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600174{
175 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
176 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000177 int r;
178
179 r = tilcdc_verify_fb(crtc, fb);
180 if (r)
181 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600182
183 if (tilcdc_crtc->event) {
184 dev_err(dev->dev, "already pending page flip!\n");
185 return -EBUSY;
186 }
187
Matt Roperf4510a22014-04-01 15:22:40 -0700188 crtc->primary->fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600189 tilcdc_crtc->event = event;
190 update_scanout(crtc);
191
192 return 0;
193}
194
195static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
196{
197 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
198 struct drm_device *dev = crtc->dev;
199 struct tilcdc_drm_private *priv = dev->dev_private;
200
201 /* we really only care about on or off: */
202 if (mode != DRM_MODE_DPMS_ON)
203 mode = DRM_MODE_DPMS_OFF;
204
205 if (tilcdc_crtc->dpms == mode)
206 return;
207
208 tilcdc_crtc->dpms = mode;
209
210 pm_runtime_get_sync(dev->dev);
211
212 if (mode == DRM_MODE_DPMS_ON) {
213 pm_runtime_forbid(dev->dev);
214 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 }
231 pm_runtime_allow(dev->dev);
232 }
233
234 pm_runtime_put_sync(dev->dev);
235}
236
237static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
238 const struct drm_display_mode *mode,
239 struct drm_display_mode *adjusted_mode)
240{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200241 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
242
243 if (!tilcdc_crtc->simulate_vesa_sync)
244 return true;
245
246 /*
247 * tilcdc does not generate VESA-compliant sync but aligns
248 * VS on the second edge of HS instead of first edge.
249 * We use adjusted_mode, to fixup sync by aligning both rising
250 * edges and add HSKEW offset to fix the sync.
251 */
252 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
253 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
254
255 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
256 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
257 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
258 } else {
259 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
260 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
261 }
262
Rob Clark16ea9752013-01-08 15:04:28 -0600263 return true;
264}
265
266static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
267{
268 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
269}
270
271static void tilcdc_crtc_commit(struct drm_crtc *crtc)
272{
273 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
274}
275
276static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
277 struct drm_display_mode *mode,
278 struct drm_display_mode *adjusted_mode,
279 int x, int y,
280 struct drm_framebuffer *old_fb)
281{
282 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
283 struct drm_device *dev = crtc->dev;
284 struct tilcdc_drm_private *priv = dev->dev_private;
285 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
286 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
287 int ret;
288
289 ret = tilcdc_crtc_mode_valid(crtc, mode);
290 if (WARN_ON(ret))
291 return ret;
292
293 if (WARN_ON(!info))
294 return -EINVAL;
295
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000296 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
297 if (ret)
298 return ret;
299
Rob Clark16ea9752013-01-08 15:04:28 -0600300 pm_runtime_get_sync(dev->dev);
301
302 /* Configure the Burst Size and fifo threshold of DMA: */
303 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
304 switch (info->dma_burst_sz) {
305 case 1:
306 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
307 break;
308 case 2:
309 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
310 break;
311 case 4:
312 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
313 break;
314 case 8:
315 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
316 break;
317 case 16:
318 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
319 break;
320 default:
321 return -EINVAL;
322 }
323 reg |= (info->fifo_th << 8);
324 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
325
326 /* Configure timings: */
327 hbp = mode->htotal - mode->hsync_end;
328 hfp = mode->hsync_start - mode->hdisplay;
329 hsw = mode->hsync_end - mode->hsync_start;
330 vbp = mode->vtotal - mode->vsync_end;
331 vfp = mode->vsync_start - mode->vdisplay;
332 vsw = mode->vsync_end - mode->vsync_start;
333
334 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
335 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
336
337 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
338 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
339 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
340 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500341
342 /*
343 * subtract one from hfp, hbp, hsw because the hardware uses
344 * a value of 0 as 1
345 */
Rob Clark16ea9752013-01-08 15:04:28 -0600346 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500347 /* clear bits we're going to set */
348 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500349 reg |= ((hfp-1) & 0x300) >> 8;
350 reg |= ((hbp-1) & 0x300) >> 4;
351 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600352 }
353 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
354
355 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500356 (((hbp-1) & 0xff) << 24) |
357 (((hfp-1) & 0xff) << 16) |
358 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600359 if (priv->rev == 2)
360 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
361 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
362
363 reg = ((mode->vdisplay - 1) & 0x3ff) |
364 ((vbp & 0xff) << 24) |
365 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500366 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600367 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
368
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500369 /*
370 * be sure to set Bit 10 for the V2 LCDC controller,
371 * otherwise limited to 1024 pixels width, stopping
372 * 1920x1080 being suppoted.
373 */
374 if (priv->rev == 2) {
375 if ((mode->vdisplay - 1) & 0x400) {
376 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
377 LCDC_LPP_B10);
378 } else {
379 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
380 LCDC_LPP_B10);
381 }
382 }
383
Rob Clark16ea9752013-01-08 15:04:28 -0600384 /* Configure display type: */
385 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
386 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
387 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
388 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
389 if (info->tft_alt_mode)
390 reg |= LCDC_TFT_ALT_ENABLE;
391 if (priv->rev == 2) {
392 unsigned int depth, bpp;
393
Matt Roperf4510a22014-04-01 15:22:40 -0700394 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600395 switch (bpp) {
396 case 16:
397 break;
398 case 32:
399 reg |= LCDC_V2_TFT_24BPP_UNPACK;
400 /* fallthrough */
401 case 24:
402 reg |= LCDC_V2_TFT_24BPP_MODE;
403 break;
404 default:
405 dev_err(dev->dev, "invalid pixel format\n");
406 return -EINVAL;
407 }
408 }
409 reg |= info->fdd < 12;
410 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
411
412 if (info->invert_pxl_clk)
413 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
414 else
415 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
416
417 if (info->sync_ctrl)
418 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
419 else
420 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
421
422 if (info->sync_edge)
423 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
424 else
425 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
426
Darren Etheridgea9767182013-08-14 21:43:33 +0200427 /*
428 * use value from adjusted_mode here as this might have been
429 * changed as part of the fixup for slave encoders to solve the
430 * issue where tilcdc timings are not VESA compliant
431 */
432 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600433 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
434 else
435 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
436
437 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
438 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
439 else
440 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
441
442 if (info->raster_order)
443 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
444 else
445 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
446
447
448 update_scanout(crtc);
449 tilcdc_crtc_update_clk(crtc);
450
451 pm_runtime_put_sync(dev->dev);
452
453 return 0;
454}
455
456static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
457 struct drm_framebuffer *old_fb)
458{
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
Rob Clark16ea9752013-01-08 15:04:28 -0600465 update_scanout(crtc);
466 return 0;
467}
468
Rob Clark16ea9752013-01-08 15:04:28 -0600469static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
470 .destroy = tilcdc_crtc_destroy,
471 .set_config = drm_crtc_helper_set_config,
472 .page_flip = tilcdc_crtc_page_flip,
473};
474
475static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
476 .dpms = tilcdc_crtc_dpms,
477 .mode_fixup = tilcdc_crtc_mode_fixup,
478 .prepare = tilcdc_crtc_prepare,
479 .commit = tilcdc_crtc_commit,
480 .mode_set = tilcdc_crtc_mode_set,
481 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600482};
483
484int tilcdc_crtc_max_width(struct drm_crtc *crtc)
485{
486 struct drm_device *dev = crtc->dev;
487 struct tilcdc_drm_private *priv = dev->dev_private;
488 int max_width = 0;
489
490 if (priv->rev == 1)
491 max_width = 1024;
492 else if (priv->rev == 2)
493 max_width = 2048;
494
495 return max_width;
496}
497
498int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
499{
500 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
501 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500502 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600503
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500504 /*
505 * check to see if the width is within the range that
506 * the LCD Controller physically supports
507 */
Rob Clark16ea9752013-01-08 15:04:28 -0600508 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
509 return MODE_VIRTUAL_X;
510
511 /* width must be multiple of 16 */
512 if (mode->hdisplay & 0xf)
513 return MODE_VIRTUAL_X;
514
515 if (mode->vdisplay > 2048)
516 return MODE_VIRTUAL_Y;
517
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500518 DBG("Processing mode %dx%d@%d with pixel clock %d",
519 mode->hdisplay, mode->vdisplay,
520 drm_mode_vrefresh(mode), mode->clock);
521
522 hbp = mode->htotal - mode->hsync_end;
523 hfp = mode->hsync_start - mode->hdisplay;
524 hsw = mode->hsync_end - mode->hsync_start;
525 vbp = mode->vtotal - mode->vsync_end;
526 vfp = mode->vsync_start - mode->vdisplay;
527 vsw = mode->vsync_end - mode->vsync_start;
528
529 if ((hbp-1) & ~0x3ff) {
530 DBG("Pruning mode: Horizontal Back Porch out of range");
531 return MODE_HBLANK_WIDE;
532 }
533
534 if ((hfp-1) & ~0x3ff) {
535 DBG("Pruning mode: Horizontal Front Porch out of range");
536 return MODE_HBLANK_WIDE;
537 }
538
539 if ((hsw-1) & ~0x3ff) {
540 DBG("Pruning mode: Horizontal Sync Width out of range");
541 return MODE_HSYNC_WIDE;
542 }
543
544 if (vbp & ~0xff) {
545 DBG("Pruning mode: Vertical Back Porch out of range");
546 return MODE_VBLANK_WIDE;
547 }
548
549 if (vfp & ~0xff) {
550 DBG("Pruning mode: Vertical Front Porch out of range");
551 return MODE_VBLANK_WIDE;
552 }
553
554 if ((vsw-1) & ~0x3f) {
555 DBG("Pruning mode: Vertical Sync Width out of range");
556 return MODE_VSYNC_WIDE;
557 }
558
Darren Etheridge4e564342013-06-21 13:52:23 -0500559 /*
560 * some devices have a maximum allowed pixel clock
561 * configured from the DT
562 */
563 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500564 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500565 return MODE_CLOCK_HIGH;
566 }
567
568 /*
569 * some devices further limit the max horizontal resolution
570 * configured from the DT
571 */
572 if (mode->hdisplay > priv->max_width)
573 return MODE_BAD_WIDTH;
574
Rob Clark16ea9752013-01-08 15:04:28 -0600575 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500576 bandwidth = mode->hdisplay * mode->vdisplay *
577 drm_mode_vrefresh(mode);
578 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500579 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600580 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500581 }
Rob Clark16ea9752013-01-08 15:04:28 -0600582
583 return MODE_OK;
584}
585
586void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
587 const struct tilcdc_panel_info *info)
588{
589 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
590 tilcdc_crtc->info = info;
591}
592
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200593void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
594 bool simulate_vesa_sync)
595{
596 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
597
598 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
599}
600
Rob Clark16ea9752013-01-08 15:04:28 -0600601void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
602{
603 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
604 struct drm_device *dev = crtc->dev;
605 struct tilcdc_drm_private *priv = dev->dev_private;
606 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600607 unsigned long lcd_clk;
608 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600609 int ret;
610
611 pm_runtime_get_sync(dev->dev);
612
613 if (dpms == DRM_MODE_DPMS_ON)
614 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
615
Darren Etheridge3d193062014-01-15 15:52:36 -0600616 /* mode.clock is in KHz, set_rate wants parameter in Hz */
617 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
618 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600619 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
620 crtc->mode.clock);
621 goto out;
622 }
623
624 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600625
Darren Etheridge3d193062014-01-15 15:52:36 -0600626 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
627 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600628
629 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600630 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600631 LCDC_RASTER_MODE);
632
633 if (priv->rev == 2)
634 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
635 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
636 LCDC_V2_CORE_CLK_EN);
637
638 if (dpms == DRM_MODE_DPMS_ON)
639 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
640
641out:
642 pm_runtime_put_sync(dev->dev);
643}
644
645irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
646{
647 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
648 struct drm_device *dev = crtc->dev;
649 struct tilcdc_drm_private *priv = dev->dev_private;
650 uint32_t stat = tilcdc_read_irqstatus(dev);
651
652 if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
653 stop(crtc);
654 dev_err(dev->dev, "error: %08x\n", stat);
655 tilcdc_clear_irqstatus(dev, stat);
656 start(crtc);
657 } else if (stat & LCDC_PL_LOAD_DONE) {
658 tilcdc_clear_irqstatus(dev, stat);
659 } else {
660 struct drm_pending_vblank_event *event;
661 unsigned long flags;
662 uint32_t dirty = tilcdc_crtc->dirty & stat;
663
664 tilcdc_clear_irqstatus(dev, stat);
665
666 if (dirty & LCDC_END_OF_FRAME0)
667 set_scanout(crtc, 0);
668
669 if (dirty & LCDC_END_OF_FRAME1)
670 set_scanout(crtc, 1);
671
672 drm_handle_vblank(dev, 0);
673
674 spin_lock_irqsave(&dev->event_lock, flags);
675 event = tilcdc_crtc->event;
676 tilcdc_crtc->event = NULL;
677 if (event)
678 drm_send_vblank_event(dev, 0, event);
679 spin_unlock_irqrestore(&dev->event_lock, flags);
680
681 if (dirty && !tilcdc_crtc->dirty)
682 drm_vblank_put(dev, 0);
683 }
684
685 if (priv->rev == 2) {
686 if (stat & LCDC_FRAME_DONE) {
687 tilcdc_crtc->frame_done = true;
688 wake_up(&tilcdc_crtc->frame_done_wq);
689 }
690 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
691 }
692
693 return IRQ_HANDLED;
694}
695
Rob Clark16ea9752013-01-08 15:04:28 -0600696struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
697{
698 struct tilcdc_crtc *tilcdc_crtc;
699 struct drm_crtc *crtc;
700 int ret;
701
702 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
703 if (!tilcdc_crtc) {
704 dev_err(dev->dev, "allocation failed\n");
705 return NULL;
706 }
707
708 crtc = &tilcdc_crtc->base;
709
710 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
711 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
712
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100713 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400714 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600715
Rob Clark16ea9752013-01-08 15:04:28 -0600716 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
717 if (ret < 0)
718 goto fail;
719
720 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
721
722 return crtc;
723
724fail:
725 tilcdc_crtc_destroy(crtc);
726 return NULL;
727}