blob: 5ee22c6b37e860da40667dfb728aa93544e29009 [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
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020024#define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
25
Rob Clark16ea9752013-01-08 15:04:28 -060026struct tilcdc_crtc {
27 struct drm_crtc base;
28
29 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060030 struct drm_pending_vblank_event *event;
31 int dpms;
32 wait_queue_head_t frame_done_wq;
33 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020034 spinlock_t irq_lock;
35
36 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060037
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030038 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020039 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060040
41 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040042 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020043
44 /* Only set if an external encoder is connected */
45 bool simulate_vesa_sync;
Rob Clark16ea9752013-01-08 15:04:28 -060046};
47#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
48
Rob Clarka464d612013-08-07 13:41:20 -040049static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060050{
Darren Etheridgef7b45752013-06-21 13:52:26 -050051 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040052 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060053 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060054
55 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040056 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060057 mutex_unlock(&dev->mode_config.mutex);
58}
59
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030060static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060061{
62 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
63 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060064 struct drm_gem_cma_object *gem;
65 unsigned int depth, bpp;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030066 dma_addr_t start, end;
Rob Clark16ea9752013-01-08 15:04:28 -060067
68 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
69 gem = drm_fb_cma_get_gem_obj(fb, 0);
70
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030071 start = gem->paddr + fb->offsets[0] +
72 crtc->y * fb->pitches[0] +
73 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060074
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030075 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060076
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030077 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
78 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
79
80 if (tilcdc_crtc->curr_fb)
81 drm_flip_work_queue(&tilcdc_crtc->unref_work,
82 tilcdc_crtc->curr_fb);
83
84 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060085}
86
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030087static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -060088{
89 struct drm_device *dev = crtc->dev;
90 struct tilcdc_drm_private *priv = dev->dev_private;
91
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030092 if (priv->rev != 2)
93 return;
94
95 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
96 usleep_range(250, 1000);
97 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
98}
99
100static void start(struct drm_crtc *crtc)
101{
102 struct drm_device *dev = crtc->dev;
103
104 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600105
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300106 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600107 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
108 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
109}
110
111static void stop(struct drm_crtc *crtc)
112{
113 struct drm_device *dev = crtc->dev;
114
115 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
116}
117
118static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
119{
120 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
121
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200122 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600123
124 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400125 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
126
Rob Clark16ea9752013-01-08 15:04:28 -0600127 kfree(tilcdc_crtc);
128}
129
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000130static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
131{
132 struct drm_device *dev = crtc->dev;
133 unsigned int depth, bpp;
134
135 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
136
137 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
138 dev_err(dev->dev,
139 "Invalid pitch: fb and crtc widths must be the same");
140 return -EINVAL;
141 }
142
143 return 0;
144}
145
Rob Clark16ea9752013-01-08 15:04:28 -0600146static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
147 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700148 struct drm_pending_vblank_event *event,
149 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600150{
151 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
152 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000153 int r;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300154 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200155 s64 tdiff;
156 ktime_t next_vblank;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000157
158 r = tilcdc_verify_fb(crtc, fb);
159 if (r)
160 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600161
162 if (tilcdc_crtc->event) {
163 dev_err(dev->dev, "already pending page flip!\n");
164 return -EBUSY;
165 }
166
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300167 drm_framebuffer_reference(fb);
168
Matt Roperf4510a22014-04-01 15:22:40 -0700169 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300170
171 pm_runtime_get_sync(dev->dev);
172
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200173 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300174
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200175 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
176 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300177
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200178 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
179
180 if (tdiff >= TILCDC_VBLANK_SAFETY_THRESHOLD_US)
181 set_scanout(crtc, fb);
182 else
183 tilcdc_crtc->next_fb = fb;
184
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300185 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200186
187 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600188
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300189 pm_runtime_put_sync(dev->dev);
190
Rob Clark16ea9752013-01-08 15:04:28 -0600191 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
Rob Clark16ea9752013-01-08 15:04:28 -0600209 if (mode == DRM_MODE_DPMS_ON) {
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300210 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600211 start(crtc);
212 } else {
213 tilcdc_crtc->frame_done = false;
214 stop(crtc);
215
Darren Etheridgef7b45752013-06-21 13:52:26 -0500216 /*
217 * if necessary wait for framedone irq which will still come
Rob Clark16ea9752013-01-08 15:04:28 -0600218 * before putting things to sleep..
219 */
220 if (priv->rev == 2) {
221 int ret = wait_event_timeout(
222 tilcdc_crtc->frame_done_wq,
223 tilcdc_crtc->frame_done,
224 msecs_to_jiffies(50));
225 if (ret == 0)
226 dev_err(dev->dev, "timeout waiting for framedone\n");
227 }
Rob Clark16ea9752013-01-08 15:04:28 -0600228
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300229 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300230
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200231 if (tilcdc_crtc->next_fb) {
232 drm_flip_work_queue(&tilcdc_crtc->unref_work,
233 tilcdc_crtc->next_fb);
234 tilcdc_crtc->next_fb = NULL;
235 }
236
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300237 if (tilcdc_crtc->curr_fb) {
238 drm_flip_work_queue(&tilcdc_crtc->unref_work,
239 tilcdc_crtc->curr_fb);
240 tilcdc_crtc->curr_fb = NULL;
241 }
242
243 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300244 }
Rob Clark16ea9752013-01-08 15:04:28 -0600245}
246
247static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
248 const struct drm_display_mode *mode,
249 struct drm_display_mode *adjusted_mode)
250{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200251 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
252
253 if (!tilcdc_crtc->simulate_vesa_sync)
254 return true;
255
256 /*
257 * tilcdc does not generate VESA-compliant sync but aligns
258 * VS on the second edge of HS instead of first edge.
259 * We use adjusted_mode, to fixup sync by aligning both rising
260 * edges and add HSKEW offset to fix the sync.
261 */
262 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
263 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
264
265 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
266 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
267 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
268 } else {
269 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
270 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
271 }
272
Rob Clark16ea9752013-01-08 15:04:28 -0600273 return true;
274}
275
276static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
277{
278 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
279}
280
281static void tilcdc_crtc_commit(struct drm_crtc *crtc)
282{
283 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
284}
285
286static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
287 struct drm_display_mode *mode,
288 struct drm_display_mode *adjusted_mode,
289 int x, int y,
290 struct drm_framebuffer *old_fb)
291{
292 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
293 struct drm_device *dev = crtc->dev;
294 struct tilcdc_drm_private *priv = dev->dev_private;
295 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
296 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
297 int ret;
298
299 ret = tilcdc_crtc_mode_valid(crtc, mode);
300 if (WARN_ON(ret))
301 return ret;
302
303 if (WARN_ON(!info))
304 return -EINVAL;
305
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000306 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
307 if (ret)
308 return ret;
309
Rob Clark16ea9752013-01-08 15:04:28 -0600310 pm_runtime_get_sync(dev->dev);
311
312 /* Configure the Burst Size and fifo threshold of DMA: */
313 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
314 switch (info->dma_burst_sz) {
315 case 1:
316 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
317 break;
318 case 2:
319 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
320 break;
321 case 4:
322 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
323 break;
324 case 8:
325 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
326 break;
327 case 16:
328 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
329 break;
330 default:
331 return -EINVAL;
332 }
333 reg |= (info->fifo_th << 8);
334 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
335
336 /* Configure timings: */
337 hbp = mode->htotal - mode->hsync_end;
338 hfp = mode->hsync_start - mode->hdisplay;
339 hsw = mode->hsync_end - mode->hsync_start;
340 vbp = mode->vtotal - mode->vsync_end;
341 vfp = mode->vsync_start - mode->vdisplay;
342 vsw = mode->vsync_end - mode->vsync_start;
343
344 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
345 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
346
347 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
348 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
349 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
350 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500351
352 /*
353 * subtract one from hfp, hbp, hsw because the hardware uses
354 * a value of 0 as 1
355 */
Rob Clark16ea9752013-01-08 15:04:28 -0600356 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500357 /* clear bits we're going to set */
358 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500359 reg |= ((hfp-1) & 0x300) >> 8;
360 reg |= ((hbp-1) & 0x300) >> 4;
361 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600362 }
363 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
364
365 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500366 (((hbp-1) & 0xff) << 24) |
367 (((hfp-1) & 0xff) << 16) |
368 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600369 if (priv->rev == 2)
370 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
371 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
372
373 reg = ((mode->vdisplay - 1) & 0x3ff) |
374 ((vbp & 0xff) << 24) |
375 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500376 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600377 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
378
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500379 /*
380 * be sure to set Bit 10 for the V2 LCDC controller,
381 * otherwise limited to 1024 pixels width, stopping
382 * 1920x1080 being suppoted.
383 */
384 if (priv->rev == 2) {
385 if ((mode->vdisplay - 1) & 0x400) {
386 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
387 LCDC_LPP_B10);
388 } else {
389 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
390 LCDC_LPP_B10);
391 }
392 }
393
Rob Clark16ea9752013-01-08 15:04:28 -0600394 /* Configure display type: */
395 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
396 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
397 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
398 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
399 if (info->tft_alt_mode)
400 reg |= LCDC_TFT_ALT_ENABLE;
401 if (priv->rev == 2) {
402 unsigned int depth, bpp;
403
Matt Roperf4510a22014-04-01 15:22:40 -0700404 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600405 switch (bpp) {
406 case 16:
407 break;
408 case 32:
409 reg |= LCDC_V2_TFT_24BPP_UNPACK;
410 /* fallthrough */
411 case 24:
412 reg |= LCDC_V2_TFT_24BPP_MODE;
413 break;
414 default:
415 dev_err(dev->dev, "invalid pixel format\n");
416 return -EINVAL;
417 }
418 }
419 reg |= info->fdd < 12;
420 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
421
422 if (info->invert_pxl_clk)
423 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
424 else
425 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
426
427 if (info->sync_ctrl)
428 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
429 else
430 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
431
432 if (info->sync_edge)
433 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
434 else
435 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
436
Darren Etheridgea9767182013-08-14 21:43:33 +0200437 /*
438 * use value from adjusted_mode here as this might have been
439 * changed as part of the fixup for slave encoders to solve the
440 * issue where tilcdc timings are not VESA compliant
441 */
442 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600443 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
444 else
445 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
446
447 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
448 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
449 else
450 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
451
452 if (info->raster_order)
453 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
454 else
455 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
456
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300457 drm_framebuffer_reference(crtc->primary->fb);
Rob Clark16ea9752013-01-08 15:04:28 -0600458
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300459 set_scanout(crtc, crtc->primary->fb);
460
Rob Clark16ea9752013-01-08 15:04:28 -0600461 tilcdc_crtc_update_clk(crtc);
462
463 pm_runtime_put_sync(dev->dev);
464
465 return 0;
466}
467
468static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
469 struct drm_framebuffer *old_fb)
470{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300471 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000472 int r;
473
474 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
475 if (r)
476 return r;
477
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300478 drm_framebuffer_reference(crtc->primary->fb);
479
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300480 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300481
482 set_scanout(crtc, crtc->primary->fb);
483
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300484 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300485
Rob Clark16ea9752013-01-08 15:04:28 -0600486 return 0;
487}
488
Rob Clark16ea9752013-01-08 15:04:28 -0600489static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
490 .destroy = tilcdc_crtc_destroy,
491 .set_config = drm_crtc_helper_set_config,
492 .page_flip = tilcdc_crtc_page_flip,
493};
494
495static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
496 .dpms = tilcdc_crtc_dpms,
497 .mode_fixup = tilcdc_crtc_mode_fixup,
498 .prepare = tilcdc_crtc_prepare,
499 .commit = tilcdc_crtc_commit,
500 .mode_set = tilcdc_crtc_mode_set,
501 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600502};
503
504int tilcdc_crtc_max_width(struct drm_crtc *crtc)
505{
506 struct drm_device *dev = crtc->dev;
507 struct tilcdc_drm_private *priv = dev->dev_private;
508 int max_width = 0;
509
510 if (priv->rev == 1)
511 max_width = 1024;
512 else if (priv->rev == 2)
513 max_width = 2048;
514
515 return max_width;
516}
517
518int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
519{
520 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
521 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500522 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600523
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500524 /*
525 * check to see if the width is within the range that
526 * the LCD Controller physically supports
527 */
Rob Clark16ea9752013-01-08 15:04:28 -0600528 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
529 return MODE_VIRTUAL_X;
530
531 /* width must be multiple of 16 */
532 if (mode->hdisplay & 0xf)
533 return MODE_VIRTUAL_X;
534
535 if (mode->vdisplay > 2048)
536 return MODE_VIRTUAL_Y;
537
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500538 DBG("Processing mode %dx%d@%d with pixel clock %d",
539 mode->hdisplay, mode->vdisplay,
540 drm_mode_vrefresh(mode), mode->clock);
541
542 hbp = mode->htotal - mode->hsync_end;
543 hfp = mode->hsync_start - mode->hdisplay;
544 hsw = mode->hsync_end - mode->hsync_start;
545 vbp = mode->vtotal - mode->vsync_end;
546 vfp = mode->vsync_start - mode->vdisplay;
547 vsw = mode->vsync_end - mode->vsync_start;
548
549 if ((hbp-1) & ~0x3ff) {
550 DBG("Pruning mode: Horizontal Back Porch out of range");
551 return MODE_HBLANK_WIDE;
552 }
553
554 if ((hfp-1) & ~0x3ff) {
555 DBG("Pruning mode: Horizontal Front Porch out of range");
556 return MODE_HBLANK_WIDE;
557 }
558
559 if ((hsw-1) & ~0x3ff) {
560 DBG("Pruning mode: Horizontal Sync Width out of range");
561 return MODE_HSYNC_WIDE;
562 }
563
564 if (vbp & ~0xff) {
565 DBG("Pruning mode: Vertical Back Porch out of range");
566 return MODE_VBLANK_WIDE;
567 }
568
569 if (vfp & ~0xff) {
570 DBG("Pruning mode: Vertical Front Porch out of range");
571 return MODE_VBLANK_WIDE;
572 }
573
574 if ((vsw-1) & ~0x3f) {
575 DBG("Pruning mode: Vertical Sync Width out of range");
576 return MODE_VSYNC_WIDE;
577 }
578
Darren Etheridge4e564342013-06-21 13:52:23 -0500579 /*
580 * some devices have a maximum allowed pixel clock
581 * configured from the DT
582 */
583 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500584 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500585 return MODE_CLOCK_HIGH;
586 }
587
588 /*
589 * some devices further limit the max horizontal resolution
590 * configured from the DT
591 */
592 if (mode->hdisplay > priv->max_width)
593 return MODE_BAD_WIDTH;
594
Rob Clark16ea9752013-01-08 15:04:28 -0600595 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500596 bandwidth = mode->hdisplay * mode->vdisplay *
597 drm_mode_vrefresh(mode);
598 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500599 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600600 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500601 }
Rob Clark16ea9752013-01-08 15:04:28 -0600602
603 return MODE_OK;
604}
605
606void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
607 const struct tilcdc_panel_info *info)
608{
609 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
610 tilcdc_crtc->info = info;
611}
612
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200613void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
614 bool simulate_vesa_sync)
615{
616 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
617
618 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
619}
620
Rob Clark16ea9752013-01-08 15:04:28 -0600621void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
622{
623 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
624 struct drm_device *dev = crtc->dev;
625 struct tilcdc_drm_private *priv = dev->dev_private;
626 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600627 unsigned long lcd_clk;
628 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600629 int ret;
630
631 pm_runtime_get_sync(dev->dev);
632
633 if (dpms == DRM_MODE_DPMS_ON)
634 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
635
Darren Etheridge3d193062014-01-15 15:52:36 -0600636 /* mode.clock is in KHz, set_rate wants parameter in Hz */
637 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
638 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600639 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
640 crtc->mode.clock);
641 goto out;
642 }
643
644 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600645
Darren Etheridge3d193062014-01-15 15:52:36 -0600646 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
647 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600648
649 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600650 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600651 LCDC_RASTER_MODE);
652
653 if (priv->rev == 2)
654 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
655 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
656 LCDC_V2_CORE_CLK_EN);
657
658 if (dpms == DRM_MODE_DPMS_ON)
659 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
660
661out:
662 pm_runtime_put_sync(dev->dev);
663}
664
665irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
666{
667 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
668 struct drm_device *dev = crtc->dev;
669 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300670 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600671
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300672 stat = tilcdc_read_irqstatus(dev);
673 tilcdc_clear_irqstatus(dev, stat);
674
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300675 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600676 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200677 bool skip_event = false;
678 ktime_t now;
679
680 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600681
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300682 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600683
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200684 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600685
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200686 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600687
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200688 if (tilcdc_crtc->next_fb) {
689 set_scanout(crtc, tilcdc_crtc->next_fb);
690 tilcdc_crtc->next_fb = NULL;
691 skip_event = true;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300692 }
693
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200694 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
695
696 drm_handle_vblank(dev, 0);
697
698 if (!skip_event) {
699 struct drm_pending_vblank_event *event;
700
701 spin_lock_irqsave(&dev->event_lock, flags);
702
703 event = tilcdc_crtc->event;
704 tilcdc_crtc->event = NULL;
705 if (event)
706 drm_send_vblank_event(dev, 0, event);
707
708 spin_unlock_irqrestore(&dev->event_lock, flags);
709 }
Rob Clark16ea9752013-01-08 15:04:28 -0600710 }
711
712 if (priv->rev == 2) {
713 if (stat & LCDC_FRAME_DONE) {
714 tilcdc_crtc->frame_done = true;
715 wake_up(&tilcdc_crtc->frame_done_wq);
716 }
717 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
718 }
719
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200720 if (stat & LCDC_SYNC_LOST)
721 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
722 __func__, stat);
723
724 if (stat & LCDC_FIFO_UNDERFLOW)
725 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
726 __func__, stat);
727
Rob Clark16ea9752013-01-08 15:04:28 -0600728 return IRQ_HANDLED;
729}
730
Rob Clark16ea9752013-01-08 15:04:28 -0600731struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
732{
733 struct tilcdc_crtc *tilcdc_crtc;
734 struct drm_crtc *crtc;
735 int ret;
736
737 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
738 if (!tilcdc_crtc) {
739 dev_err(dev->dev, "allocation failed\n");
740 return NULL;
741 }
742
743 crtc = &tilcdc_crtc->base;
744
745 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
746 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
747
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100748 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400749 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600750
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200751 spin_lock_init(&tilcdc_crtc->irq_lock);
752
Rob Clark16ea9752013-01-08 15:04:28 -0600753 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
754 if (ret < 0)
755 goto fail;
756
757 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
758
759 return crtc;
760
761fail:
762 tilcdc_crtc_destroy(crtc);
763 return NULL;
764}