blob: a6ef737c7d35c4f5f3a09a30a0f3b63ca4a5f069 [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
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300115static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600116{
117 struct drm_device *dev = crtc->dev;
118 struct tilcdc_drm_private *priv = dev->dev_private;
119
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300120 if (priv->rev != 2)
121 return;
122
123 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
124 usleep_range(250, 1000);
125 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
126}
127
128static void start(struct drm_crtc *crtc)
129{
130 struct drm_device *dev = crtc->dev;
131
132 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600133
134 tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
135 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
136 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
137}
138
139static void stop(struct drm_crtc *crtc)
140{
141 struct drm_device *dev = crtc->dev;
142
143 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
144}
145
146static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
147{
148 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
149
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200150 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600151
152 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400153 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
154
Rob Clark16ea9752013-01-08 15:04:28 -0600155 kfree(tilcdc_crtc);
156}
157
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000158static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
159{
160 struct drm_device *dev = crtc->dev;
161 unsigned int depth, bpp;
162
163 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
164
165 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
166 dev_err(dev->dev,
167 "Invalid pitch: fb and crtc widths must be the same");
168 return -EINVAL;
169 }
170
171 return 0;
172}
173
Rob Clark16ea9752013-01-08 15:04:28 -0600174static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
175 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700176 struct drm_pending_vblank_event *event,
177 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600178{
179 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
180 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000181 int r;
182
183 r = tilcdc_verify_fb(crtc, fb);
184 if (r)
185 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600186
187 if (tilcdc_crtc->event) {
188 dev_err(dev->dev, "already pending page flip!\n");
189 return -EBUSY;
190 }
191
Matt Roperf4510a22014-04-01 15:22:40 -0700192 crtc->primary->fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600193 tilcdc_crtc->event = event;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300194
195 pm_runtime_get_sync(dev->dev);
196
Rob Clark16ea9752013-01-08 15:04:28 -0600197 update_scanout(crtc);
198
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300199 pm_runtime_put_sync(dev->dev);
200
Rob Clark16ea9752013-01-08 15:04:28 -0600201 return 0;
202}
203
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000204void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
Rob Clark16ea9752013-01-08 15:04:28 -0600205{
206 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
207 struct drm_device *dev = crtc->dev;
208 struct tilcdc_drm_private *priv = dev->dev_private;
209
210 /* we really only care about on or off: */
211 if (mode != DRM_MODE_DPMS_ON)
212 mode = DRM_MODE_DPMS_OFF;
213
214 if (tilcdc_crtc->dpms == mode)
215 return;
216
217 tilcdc_crtc->dpms = mode;
218
Rob Clark16ea9752013-01-08 15:04:28 -0600219 if (mode == DRM_MODE_DPMS_ON) {
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300220 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600221 start(crtc);
222 } else {
223 tilcdc_crtc->frame_done = false;
224 stop(crtc);
225
Darren Etheridgef7b45752013-06-21 13:52:26 -0500226 /*
227 * if necessary wait for framedone irq which will still come
Rob Clark16ea9752013-01-08 15:04:28 -0600228 * before putting things to sleep..
229 */
230 if (priv->rev == 2) {
231 int ret = wait_event_timeout(
232 tilcdc_crtc->frame_done_wq,
233 tilcdc_crtc->frame_done,
234 msecs_to_jiffies(50));
235 if (ret == 0)
236 dev_err(dev->dev, "timeout waiting for framedone\n");
237 }
Rob Clark16ea9752013-01-08 15:04:28 -0600238
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300239 pm_runtime_put_sync(dev->dev);
240 }
Rob Clark16ea9752013-01-08 15:04:28 -0600241}
242
243static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
244 const struct drm_display_mode *mode,
245 struct drm_display_mode *adjusted_mode)
246{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200247 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
248
249 if (!tilcdc_crtc->simulate_vesa_sync)
250 return true;
251
252 /*
253 * tilcdc does not generate VESA-compliant sync but aligns
254 * VS on the second edge of HS instead of first edge.
255 * We use adjusted_mode, to fixup sync by aligning both rising
256 * edges and add HSKEW offset to fix the sync.
257 */
258 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
259 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
260
261 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
262 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
263 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
264 } else {
265 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
266 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
267 }
268
Rob Clark16ea9752013-01-08 15:04:28 -0600269 return true;
270}
271
272static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
273{
274 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
275}
276
277static void tilcdc_crtc_commit(struct drm_crtc *crtc)
278{
279 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
280}
281
282static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
283 struct drm_display_mode *mode,
284 struct drm_display_mode *adjusted_mode,
285 int x, int y,
286 struct drm_framebuffer *old_fb)
287{
288 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
289 struct drm_device *dev = crtc->dev;
290 struct tilcdc_drm_private *priv = dev->dev_private;
291 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
292 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
293 int ret;
294
295 ret = tilcdc_crtc_mode_valid(crtc, mode);
296 if (WARN_ON(ret))
297 return ret;
298
299 if (WARN_ON(!info))
300 return -EINVAL;
301
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000302 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
303 if (ret)
304 return ret;
305
Rob Clark16ea9752013-01-08 15:04:28 -0600306 pm_runtime_get_sync(dev->dev);
307
308 /* Configure the Burst Size and fifo threshold of DMA: */
309 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
310 switch (info->dma_burst_sz) {
311 case 1:
312 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
313 break;
314 case 2:
315 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
316 break;
317 case 4:
318 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
319 break;
320 case 8:
321 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
322 break;
323 case 16:
324 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
325 break;
326 default:
327 return -EINVAL;
328 }
329 reg |= (info->fifo_th << 8);
330 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
331
332 /* Configure timings: */
333 hbp = mode->htotal - mode->hsync_end;
334 hfp = mode->hsync_start - mode->hdisplay;
335 hsw = mode->hsync_end - mode->hsync_start;
336 vbp = mode->vtotal - mode->vsync_end;
337 vfp = mode->vsync_start - mode->vdisplay;
338 vsw = mode->vsync_end - mode->vsync_start;
339
340 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
341 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
342
343 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
344 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
345 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
346 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500347
348 /*
349 * subtract one from hfp, hbp, hsw because the hardware uses
350 * a value of 0 as 1
351 */
Rob Clark16ea9752013-01-08 15:04:28 -0600352 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500353 /* clear bits we're going to set */
354 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500355 reg |= ((hfp-1) & 0x300) >> 8;
356 reg |= ((hbp-1) & 0x300) >> 4;
357 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600358 }
359 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
360
361 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500362 (((hbp-1) & 0xff) << 24) |
363 (((hfp-1) & 0xff) << 16) |
364 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600365 if (priv->rev == 2)
366 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
367 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
368
369 reg = ((mode->vdisplay - 1) & 0x3ff) |
370 ((vbp & 0xff) << 24) |
371 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500372 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600373 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
374
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500375 /*
376 * be sure to set Bit 10 for the V2 LCDC controller,
377 * otherwise limited to 1024 pixels width, stopping
378 * 1920x1080 being suppoted.
379 */
380 if (priv->rev == 2) {
381 if ((mode->vdisplay - 1) & 0x400) {
382 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
383 LCDC_LPP_B10);
384 } else {
385 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
386 LCDC_LPP_B10);
387 }
388 }
389
Rob Clark16ea9752013-01-08 15:04:28 -0600390 /* Configure display type: */
391 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
392 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
393 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
394 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
395 if (info->tft_alt_mode)
396 reg |= LCDC_TFT_ALT_ENABLE;
397 if (priv->rev == 2) {
398 unsigned int depth, bpp;
399
Matt Roperf4510a22014-04-01 15:22:40 -0700400 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600401 switch (bpp) {
402 case 16:
403 break;
404 case 32:
405 reg |= LCDC_V2_TFT_24BPP_UNPACK;
406 /* fallthrough */
407 case 24:
408 reg |= LCDC_V2_TFT_24BPP_MODE;
409 break;
410 default:
411 dev_err(dev->dev, "invalid pixel format\n");
412 return -EINVAL;
413 }
414 }
415 reg |= info->fdd < 12;
416 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
417
418 if (info->invert_pxl_clk)
419 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
420 else
421 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
422
423 if (info->sync_ctrl)
424 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
425 else
426 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
427
428 if (info->sync_edge)
429 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
430 else
431 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
432
Darren Etheridgea9767182013-08-14 21:43:33 +0200433 /*
434 * use value from adjusted_mode here as this might have been
435 * changed as part of the fixup for slave encoders to solve the
436 * issue where tilcdc timings are not VESA compliant
437 */
438 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600439 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
440 else
441 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
442
443 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
444 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
445 else
446 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
447
448 if (info->raster_order)
449 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
450 else
451 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
452
453
454 update_scanout(crtc);
455 tilcdc_crtc_update_clk(crtc);
456
457 pm_runtime_put_sync(dev->dev);
458
459 return 0;
460}
461
462static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
463 struct drm_framebuffer *old_fb)
464{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300465 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000466 int r;
467
468 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
469 if (r)
470 return r;
471
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300472 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600473 update_scanout(crtc);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300474 pm_runtime_put_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600475 return 0;
476}
477
Rob Clark16ea9752013-01-08 15:04:28 -0600478static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
479 .destroy = tilcdc_crtc_destroy,
480 .set_config = drm_crtc_helper_set_config,
481 .page_flip = tilcdc_crtc_page_flip,
482};
483
484static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
485 .dpms = tilcdc_crtc_dpms,
486 .mode_fixup = tilcdc_crtc_mode_fixup,
487 .prepare = tilcdc_crtc_prepare,
488 .commit = tilcdc_crtc_commit,
489 .mode_set = tilcdc_crtc_mode_set,
490 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600491};
492
493int tilcdc_crtc_max_width(struct drm_crtc *crtc)
494{
495 struct drm_device *dev = crtc->dev;
496 struct tilcdc_drm_private *priv = dev->dev_private;
497 int max_width = 0;
498
499 if (priv->rev == 1)
500 max_width = 1024;
501 else if (priv->rev == 2)
502 max_width = 2048;
503
504 return max_width;
505}
506
507int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
508{
509 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
510 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500511 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600512
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500513 /*
514 * check to see if the width is within the range that
515 * the LCD Controller physically supports
516 */
Rob Clark16ea9752013-01-08 15:04:28 -0600517 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
518 return MODE_VIRTUAL_X;
519
520 /* width must be multiple of 16 */
521 if (mode->hdisplay & 0xf)
522 return MODE_VIRTUAL_X;
523
524 if (mode->vdisplay > 2048)
525 return MODE_VIRTUAL_Y;
526
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500527 DBG("Processing mode %dx%d@%d with pixel clock %d",
528 mode->hdisplay, mode->vdisplay,
529 drm_mode_vrefresh(mode), mode->clock);
530
531 hbp = mode->htotal - mode->hsync_end;
532 hfp = mode->hsync_start - mode->hdisplay;
533 hsw = mode->hsync_end - mode->hsync_start;
534 vbp = mode->vtotal - mode->vsync_end;
535 vfp = mode->vsync_start - mode->vdisplay;
536 vsw = mode->vsync_end - mode->vsync_start;
537
538 if ((hbp-1) & ~0x3ff) {
539 DBG("Pruning mode: Horizontal Back Porch out of range");
540 return MODE_HBLANK_WIDE;
541 }
542
543 if ((hfp-1) & ~0x3ff) {
544 DBG("Pruning mode: Horizontal Front Porch out of range");
545 return MODE_HBLANK_WIDE;
546 }
547
548 if ((hsw-1) & ~0x3ff) {
549 DBG("Pruning mode: Horizontal Sync Width out of range");
550 return MODE_HSYNC_WIDE;
551 }
552
553 if (vbp & ~0xff) {
554 DBG("Pruning mode: Vertical Back Porch out of range");
555 return MODE_VBLANK_WIDE;
556 }
557
558 if (vfp & ~0xff) {
559 DBG("Pruning mode: Vertical Front Porch out of range");
560 return MODE_VBLANK_WIDE;
561 }
562
563 if ((vsw-1) & ~0x3f) {
564 DBG("Pruning mode: Vertical Sync Width out of range");
565 return MODE_VSYNC_WIDE;
566 }
567
Darren Etheridge4e564342013-06-21 13:52:23 -0500568 /*
569 * some devices have a maximum allowed pixel clock
570 * configured from the DT
571 */
572 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500573 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500574 return MODE_CLOCK_HIGH;
575 }
576
577 /*
578 * some devices further limit the max horizontal resolution
579 * configured from the DT
580 */
581 if (mode->hdisplay > priv->max_width)
582 return MODE_BAD_WIDTH;
583
Rob Clark16ea9752013-01-08 15:04:28 -0600584 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500585 bandwidth = mode->hdisplay * mode->vdisplay *
586 drm_mode_vrefresh(mode);
587 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500588 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600589 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500590 }
Rob Clark16ea9752013-01-08 15:04:28 -0600591
592 return MODE_OK;
593}
594
595void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
596 const struct tilcdc_panel_info *info)
597{
598 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
599 tilcdc_crtc->info = info;
600}
601
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200602void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
603 bool simulate_vesa_sync)
604{
605 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
606
607 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
608}
609
Rob Clark16ea9752013-01-08 15:04:28 -0600610void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
611{
612 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
613 struct drm_device *dev = crtc->dev;
614 struct tilcdc_drm_private *priv = dev->dev_private;
615 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600616 unsigned long lcd_clk;
617 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600618 int ret;
619
620 pm_runtime_get_sync(dev->dev);
621
622 if (dpms == DRM_MODE_DPMS_ON)
623 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
624
Darren Etheridge3d193062014-01-15 15:52:36 -0600625 /* mode.clock is in KHz, set_rate wants parameter in Hz */
626 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
627 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600628 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
629 crtc->mode.clock);
630 goto out;
631 }
632
633 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600634
Darren Etheridge3d193062014-01-15 15:52:36 -0600635 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
636 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600637
638 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600639 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600640 LCDC_RASTER_MODE);
641
642 if (priv->rev == 2)
643 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
644 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
645 LCDC_V2_CORE_CLK_EN);
646
647 if (dpms == DRM_MODE_DPMS_ON)
648 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
649
650out:
651 pm_runtime_put_sync(dev->dev);
652}
653
654irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
655{
656 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
657 struct drm_device *dev = crtc->dev;
658 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300659 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600660
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300661 stat = tilcdc_read_irqstatus(dev);
662 tilcdc_clear_irqstatus(dev, stat);
663
664 if ((stat & LCDC_END_OF_FRAME0) || (stat & LCDC_END_OF_FRAME1)) {
Rob Clark16ea9752013-01-08 15:04:28 -0600665 struct drm_pending_vblank_event *event;
666 unsigned long flags;
667 uint32_t dirty = tilcdc_crtc->dirty & stat;
668
669 tilcdc_clear_irqstatus(dev, stat);
670
671 if (dirty & LCDC_END_OF_FRAME0)
672 set_scanout(crtc, 0);
673
674 if (dirty & LCDC_END_OF_FRAME1)
675 set_scanout(crtc, 1);
676
677 drm_handle_vblank(dev, 0);
678
679 spin_lock_irqsave(&dev->event_lock, flags);
680 event = tilcdc_crtc->event;
681 tilcdc_crtc->event = NULL;
682 if (event)
683 drm_send_vblank_event(dev, 0, event);
684 spin_unlock_irqrestore(&dev->event_lock, flags);
685
686 if (dirty && !tilcdc_crtc->dirty)
687 drm_vblank_put(dev, 0);
688 }
689
690 if (priv->rev == 2) {
691 if (stat & LCDC_FRAME_DONE) {
692 tilcdc_crtc->frame_done = true;
693 wake_up(&tilcdc_crtc->frame_done_wq);
694 }
695 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
696 }
697
698 return IRQ_HANDLED;
699}
700
Rob Clark16ea9752013-01-08 15:04:28 -0600701struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
702{
703 struct tilcdc_crtc *tilcdc_crtc;
704 struct drm_crtc *crtc;
705 int ret;
706
707 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
708 if (!tilcdc_crtc) {
709 dev_err(dev->dev, "allocation failed\n");
710 return NULL;
711 }
712
713 crtc = &tilcdc_crtc->base;
714
715 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
716 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
717
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100718 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400719 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600720
Rob Clark16ea9752013-01-08 15:04:28 -0600721 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
722 if (ret < 0)
723 goto fail;
724
725 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
726
727 return crtc;
728
729fail:
730 tilcdc_crtc_destroy(crtc);
731 return NULL;
732}