blob: d9d2a6cc27c7c7800bcf46b8e7c6c6b6ac89b002 [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 Valkeinen2b2080d72015-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;
Jyri Sarha5895d082016-01-08 14:33:09 +020046
47 int sync_lost_count;
48 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060049};
50#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
51
Rob Clarka464d612013-08-07 13:41:20 -040052static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060053{
Darren Etheridgef7b45752013-06-21 13:52:26 -050054 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040055 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060056 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060057
58 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040059 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060060 mutex_unlock(&dev->mode_config.mutex);
61}
62
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030063static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060064{
65 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
66 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060067 struct drm_gem_cma_object *gem;
68 unsigned int depth, bpp;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030069 dma_addr_t start, end;
Rob Clark16ea9752013-01-08 15:04:28 -060070
71 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
72 gem = drm_fb_cma_get_gem_obj(fb, 0);
73
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030074 start = gem->paddr + fb->offsets[0] +
75 crtc->y * fb->pitches[0] +
76 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060077
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030078 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060079
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030080 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
81 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
82
83 if (tilcdc_crtc->curr_fb)
84 drm_flip_work_queue(&tilcdc_crtc->unref_work,
85 tilcdc_crtc->curr_fb);
86
87 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060088}
89
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030090static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -060091{
92 struct drm_device *dev = crtc->dev;
93 struct tilcdc_drm_private *priv = dev->dev_private;
94
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030095 if (priv->rev != 2)
96 return;
97
98 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
99 usleep_range(250, 1000);
100 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
101}
102
103static void start(struct drm_crtc *crtc)
104{
105 struct drm_device *dev = crtc->dev;
106
107 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600108
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300109 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600110 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
111 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300112
113 drm_crtc_vblank_on(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600114}
115
116static void stop(struct drm_crtc *crtc)
117{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300118 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600119 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300120 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600121
Jyri Sarha2d5be882016-04-07 20:20:23 +0300122 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600123 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300124
125 /*
126 * if necessary wait for framedone irq which will still come
127 * before putting things to sleep..
128 */
129 if (priv->rev == 2) {
130 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
131 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300132 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300133 if (ret == 0)
134 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
135 __func__);
136 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300137
138 drm_crtc_vblank_off(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600139}
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
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300147 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600148 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400149 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600150}
151
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000152static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
153{
154 struct drm_device *dev = crtc->dev;
155 unsigned int depth, bpp;
156
157 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
158
159 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
160 dev_err(dev->dev,
161 "Invalid pitch: fb and crtc widths must be the same");
162 return -EINVAL;
163 }
164
165 return 0;
166}
167
Jyri Sarha8c65abb2016-04-07 14:56:32 +0300168int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600169 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700170 struct drm_pending_vblank_event *event,
171 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600172{
173 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
174 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000175 int r;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300176 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000177
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
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300187 drm_framebuffer_reference(fb);
188
Matt Roperf4510a22014-04-01 15:22:40 -0700189 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300190
191 pm_runtime_get_sync(dev->dev);
192
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200193 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300194
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300195 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
196 ktime_t next_vblank;
197 s64 tdiff;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300198
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300199 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
200 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200201
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300202 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
203
204 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
205 tilcdc_crtc->next_fb = fb;
206 }
207
208 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200209 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200210
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300211 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200212
213 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600214
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300215 pm_runtime_put_sync(dev->dev);
216
Rob Clark16ea9752013-01-08 15:04:28 -0600217 return 0;
218}
219
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000220void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
Rob Clark16ea9752013-01-08 15:04:28 -0600221{
222 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
223 struct drm_device *dev = crtc->dev;
224 struct tilcdc_drm_private *priv = dev->dev_private;
225
226 /* we really only care about on or off: */
227 if (mode != DRM_MODE_DPMS_ON)
228 mode = DRM_MODE_DPMS_OFF;
229
230 if (tilcdc_crtc->dpms == mode)
231 return;
232
233 tilcdc_crtc->dpms = mode;
234
Rob Clark16ea9752013-01-08 15:04:28 -0600235 if (mode == DRM_MODE_DPMS_ON) {
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300236 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600237 start(crtc);
238 } else {
Rob Clark16ea9752013-01-08 15:04:28 -0600239 stop(crtc);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300240 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300241
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200242 if (tilcdc_crtc->next_fb) {
243 drm_flip_work_queue(&tilcdc_crtc->unref_work,
244 tilcdc_crtc->next_fb);
245 tilcdc_crtc->next_fb = NULL;
246 }
247
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300248 if (tilcdc_crtc->curr_fb) {
249 drm_flip_work_queue(&tilcdc_crtc->unref_work,
250 tilcdc_crtc->curr_fb);
251 tilcdc_crtc->curr_fb = NULL;
252 }
253
254 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300255 tilcdc_crtc->last_vblank = ktime_set(0, 0);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300256 }
Rob Clark16ea9752013-01-08 15:04:28 -0600257}
258
Jyri Sarha8fe56162016-06-14 11:43:30 +0300259int tilcdc_crtc_current_dpms_state(struct drm_crtc *crtc)
260{
261 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
262
263 return tilcdc_crtc->dpms;
264}
265
Rob Clark16ea9752013-01-08 15:04:28 -0600266static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
267 const struct drm_display_mode *mode,
268 struct drm_display_mode *adjusted_mode)
269{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200270 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
271
272 if (!tilcdc_crtc->simulate_vesa_sync)
273 return true;
274
275 /*
276 * tilcdc does not generate VESA-compliant sync but aligns
277 * VS on the second edge of HS instead of first edge.
278 * We use adjusted_mode, to fixup sync by aligning both rising
279 * edges and add HSKEW offset to fix the sync.
280 */
281 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
282 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
283
284 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
285 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
286 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
287 } else {
288 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
289 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
290 }
291
Rob Clark16ea9752013-01-08 15:04:28 -0600292 return true;
293}
294
295static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
296{
297 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
298}
299
300static void tilcdc_crtc_commit(struct drm_crtc *crtc)
301{
302 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
303}
304
305static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
306 struct drm_display_mode *mode,
307 struct drm_display_mode *adjusted_mode,
308 int x, int y,
309 struct drm_framebuffer *old_fb)
310{
311 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
312 struct drm_device *dev = crtc->dev;
313 struct tilcdc_drm_private *priv = dev->dev_private;
314 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
315 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
316 int ret;
317
318 ret = tilcdc_crtc_mode_valid(crtc, mode);
319 if (WARN_ON(ret))
320 return ret;
321
322 if (WARN_ON(!info))
323 return -EINVAL;
324
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000325 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
326 if (ret)
327 return ret;
328
Rob Clark16ea9752013-01-08 15:04:28 -0600329 pm_runtime_get_sync(dev->dev);
330
331 /* Configure the Burst Size and fifo threshold of DMA: */
332 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
333 switch (info->dma_burst_sz) {
334 case 1:
335 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
336 break;
337 case 2:
338 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
339 break;
340 case 4:
341 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
342 break;
343 case 8:
344 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
345 break;
346 case 16:
347 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
348 break;
349 default:
350 return -EINVAL;
351 }
352 reg |= (info->fifo_th << 8);
353 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
354
355 /* Configure timings: */
356 hbp = mode->htotal - mode->hsync_end;
357 hfp = mode->hsync_start - mode->hdisplay;
358 hsw = mode->hsync_end - mode->hsync_start;
359 vbp = mode->vtotal - mode->vsync_end;
360 vfp = mode->vsync_start - mode->vdisplay;
361 vsw = mode->vsync_end - mode->vsync_start;
362
363 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
364 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
365
366 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
367 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
368 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
369 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500370
371 /*
372 * subtract one from hfp, hbp, hsw because the hardware uses
373 * a value of 0 as 1
374 */
Rob Clark16ea9752013-01-08 15:04:28 -0600375 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500376 /* clear bits we're going to set */
377 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500378 reg |= ((hfp-1) & 0x300) >> 8;
379 reg |= ((hbp-1) & 0x300) >> 4;
380 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600381 }
382 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
383
384 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500385 (((hbp-1) & 0xff) << 24) |
386 (((hfp-1) & 0xff) << 16) |
387 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600388 if (priv->rev == 2)
389 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
390 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
391
392 reg = ((mode->vdisplay - 1) & 0x3ff) |
393 ((vbp & 0xff) << 24) |
394 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500395 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600396 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
397
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500398 /*
399 * be sure to set Bit 10 for the V2 LCDC controller,
400 * otherwise limited to 1024 pixels width, stopping
401 * 1920x1080 being suppoted.
402 */
403 if (priv->rev == 2) {
404 if ((mode->vdisplay - 1) & 0x400) {
405 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
406 LCDC_LPP_B10);
407 } else {
408 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
409 LCDC_LPP_B10);
410 }
411 }
412
Rob Clark16ea9752013-01-08 15:04:28 -0600413 /* Configure display type: */
414 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
415 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
416 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
417 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
418 if (info->tft_alt_mode)
419 reg |= LCDC_TFT_ALT_ENABLE;
420 if (priv->rev == 2) {
421 unsigned int depth, bpp;
422
Matt Roperf4510a22014-04-01 15:22:40 -0700423 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600424 switch (bpp) {
425 case 16:
426 break;
427 case 32:
428 reg |= LCDC_V2_TFT_24BPP_UNPACK;
429 /* fallthrough */
430 case 24:
431 reg |= LCDC_V2_TFT_24BPP_MODE;
432 break;
433 default:
434 dev_err(dev->dev, "invalid pixel format\n");
435 return -EINVAL;
436 }
437 }
438 reg |= info->fdd < 12;
439 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
440
441 if (info->invert_pxl_clk)
442 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
443 else
444 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
445
446 if (info->sync_ctrl)
447 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
448 else
449 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
450
451 if (info->sync_edge)
452 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
453 else
454 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
455
Darren Etheridgea9767182013-08-14 21:43:33 +0200456 /*
457 * use value from adjusted_mode here as this might have been
458 * changed as part of the fixup for slave encoders to solve the
459 * issue where tilcdc timings are not VESA compliant
460 */
461 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600462 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
463 else
464 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
465
466 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
467 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
468 else
469 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
470
471 if (info->raster_order)
472 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
473 else
474 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
475
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300476 drm_framebuffer_reference(crtc->primary->fb);
Rob Clark16ea9752013-01-08 15:04:28 -0600477
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300478 set_scanout(crtc, crtc->primary->fb);
479
Rob Clark16ea9752013-01-08 15:04:28 -0600480 tilcdc_crtc_update_clk(crtc);
481
482 pm_runtime_put_sync(dev->dev);
483
484 return 0;
485}
486
487static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
488 struct drm_framebuffer *old_fb)
489{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300490 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000491 int r;
492
493 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
494 if (r)
495 return r;
496
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300497 drm_framebuffer_reference(crtc->primary->fb);
498
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300499 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300500
501 set_scanout(crtc, crtc->primary->fb);
502
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300503 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300504
Rob Clark16ea9752013-01-08 15:04:28 -0600505 return 0;
506}
507
Rob Clark16ea9752013-01-08 15:04:28 -0600508static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
509 .destroy = tilcdc_crtc_destroy,
510 .set_config = drm_crtc_helper_set_config,
511 .page_flip = tilcdc_crtc_page_flip,
512};
513
514static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
515 .dpms = tilcdc_crtc_dpms,
516 .mode_fixup = tilcdc_crtc_mode_fixup,
517 .prepare = tilcdc_crtc_prepare,
518 .commit = tilcdc_crtc_commit,
519 .mode_set = tilcdc_crtc_mode_set,
520 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600521};
522
523int tilcdc_crtc_max_width(struct drm_crtc *crtc)
524{
525 struct drm_device *dev = crtc->dev;
526 struct tilcdc_drm_private *priv = dev->dev_private;
527 int max_width = 0;
528
529 if (priv->rev == 1)
530 max_width = 1024;
531 else if (priv->rev == 2)
532 max_width = 2048;
533
534 return max_width;
535}
536
537int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
538{
539 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
540 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500541 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600542
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500543 /*
544 * check to see if the width is within the range that
545 * the LCD Controller physically supports
546 */
Rob Clark16ea9752013-01-08 15:04:28 -0600547 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
548 return MODE_VIRTUAL_X;
549
550 /* width must be multiple of 16 */
551 if (mode->hdisplay & 0xf)
552 return MODE_VIRTUAL_X;
553
554 if (mode->vdisplay > 2048)
555 return MODE_VIRTUAL_Y;
556
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500557 DBG("Processing mode %dx%d@%d with pixel clock %d",
558 mode->hdisplay, mode->vdisplay,
559 drm_mode_vrefresh(mode), mode->clock);
560
561 hbp = mode->htotal - mode->hsync_end;
562 hfp = mode->hsync_start - mode->hdisplay;
563 hsw = mode->hsync_end - mode->hsync_start;
564 vbp = mode->vtotal - mode->vsync_end;
565 vfp = mode->vsync_start - mode->vdisplay;
566 vsw = mode->vsync_end - mode->vsync_start;
567
568 if ((hbp-1) & ~0x3ff) {
569 DBG("Pruning mode: Horizontal Back Porch out of range");
570 return MODE_HBLANK_WIDE;
571 }
572
573 if ((hfp-1) & ~0x3ff) {
574 DBG("Pruning mode: Horizontal Front Porch out of range");
575 return MODE_HBLANK_WIDE;
576 }
577
578 if ((hsw-1) & ~0x3ff) {
579 DBG("Pruning mode: Horizontal Sync Width out of range");
580 return MODE_HSYNC_WIDE;
581 }
582
583 if (vbp & ~0xff) {
584 DBG("Pruning mode: Vertical Back Porch out of range");
585 return MODE_VBLANK_WIDE;
586 }
587
588 if (vfp & ~0xff) {
589 DBG("Pruning mode: Vertical Front Porch out of range");
590 return MODE_VBLANK_WIDE;
591 }
592
593 if ((vsw-1) & ~0x3f) {
594 DBG("Pruning mode: Vertical Sync Width out of range");
595 return MODE_VSYNC_WIDE;
596 }
597
Darren Etheridge4e564342013-06-21 13:52:23 -0500598 /*
599 * some devices have a maximum allowed pixel clock
600 * configured from the DT
601 */
602 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500603 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500604 return MODE_CLOCK_HIGH;
605 }
606
607 /*
608 * some devices further limit the max horizontal resolution
609 * configured from the DT
610 */
611 if (mode->hdisplay > priv->max_width)
612 return MODE_BAD_WIDTH;
613
Rob Clark16ea9752013-01-08 15:04:28 -0600614 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500615 bandwidth = mode->hdisplay * mode->vdisplay *
616 drm_mode_vrefresh(mode);
617 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500618 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600619 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500620 }
Rob Clark16ea9752013-01-08 15:04:28 -0600621
622 return MODE_OK;
623}
624
625void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
626 const struct tilcdc_panel_info *info)
627{
628 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
629 tilcdc_crtc->info = info;
630}
631
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200632void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
633 bool simulate_vesa_sync)
634{
635 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
636
637 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
638}
639
Rob Clark16ea9752013-01-08 15:04:28 -0600640void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
641{
642 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
643 struct drm_device *dev = crtc->dev;
644 struct tilcdc_drm_private *priv = dev->dev_private;
645 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600646 unsigned long lcd_clk;
647 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600648 int ret;
649
650 pm_runtime_get_sync(dev->dev);
651
652 if (dpms == DRM_MODE_DPMS_ON)
653 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
654
Darren Etheridge3d193062014-01-15 15:52:36 -0600655 /* mode.clock is in KHz, set_rate wants parameter in Hz */
656 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
657 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600658 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
659 crtc->mode.clock);
660 goto out;
661 }
662
663 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600664
Darren Etheridge3d193062014-01-15 15:52:36 -0600665 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
666 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600667
668 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600669 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600670 LCDC_RASTER_MODE);
671
672 if (priv->rev == 2)
673 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
674 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
675 LCDC_V2_CORE_CLK_EN);
676
677 if (dpms == DRM_MODE_DPMS_ON)
678 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
679
680out:
681 pm_runtime_put_sync(dev->dev);
682}
683
Jyri Sarha5895d082016-01-08 14:33:09 +0200684#define SYNC_LOST_COUNT_LIMIT 50
685
Rob Clark16ea9752013-01-08 15:04:28 -0600686irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
687{
688 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
689 struct drm_device *dev = crtc->dev;
690 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300691 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600692
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300693 stat = tilcdc_read_irqstatus(dev);
694 tilcdc_clear_irqstatus(dev, stat);
695
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300696 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600697 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200698 bool skip_event = false;
699 ktime_t now;
700
701 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600702
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300703 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600704
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200705 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600706
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200707 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600708
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200709 if (tilcdc_crtc->next_fb) {
710 set_scanout(crtc, tilcdc_crtc->next_fb);
711 tilcdc_crtc->next_fb = NULL;
712 skip_event = true;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300713 }
714
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200715 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
716
Gustavo Padovan099ede82016-07-04 21:04:52 -0300717 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200718
719 if (!skip_event) {
720 struct drm_pending_vblank_event *event;
721
722 spin_lock_irqsave(&dev->event_lock, flags);
723
724 event = tilcdc_crtc->event;
725 tilcdc_crtc->event = NULL;
726 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700727 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200728
729 spin_unlock_irqrestore(&dev->event_lock, flags);
730 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200731
732 if (tilcdc_crtc->frame_intact)
733 tilcdc_crtc->sync_lost_count = 0;
734 else
735 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600736 }
737
Jyri Sarha14944112016-04-07 20:36:48 +0300738 if (stat & LCDC_FIFO_UNDERFLOW)
739 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
740 __func__, stat);
741
742 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600743 if (priv->rev == 2) {
744 if (stat & LCDC_FRAME_DONE) {
745 tilcdc_crtc->frame_done = true;
746 wake_up(&tilcdc_crtc->frame_done_wq);
747 }
Rob Clark16ea9752013-01-08 15:04:28 -0600748
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300749 if (stat & LCDC_SYNC_LOST) {
750 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
751 __func__, stat);
752 tilcdc_crtc->frame_intact = false;
753 if (tilcdc_crtc->sync_lost_count++ >
754 SYNC_LOST_COUNT_LIMIT) {
755 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
756 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
757 LCDC_SYNC_LOST);
758 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200759 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200760
Jyri Sarha14944112016-04-07 20:36:48 +0300761 /* Indicate to LCDC that the interrupt service routine has
762 * completed, see 13.3.6.1.6 in AM335x TRM.
763 */
764 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
765 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200766
Rob Clark16ea9752013-01-08 15:04:28 -0600767 return IRQ_HANDLED;
768}
769
Rob Clark16ea9752013-01-08 15:04:28 -0600770struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
771{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300772 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600773 struct tilcdc_crtc *tilcdc_crtc;
774 struct drm_crtc *crtc;
775 int ret;
776
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200777 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600778 if (!tilcdc_crtc) {
779 dev_err(dev->dev, "allocation failed\n");
780 return NULL;
781 }
782
783 crtc = &tilcdc_crtc->base;
784
785 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
786 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
787
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100788 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400789 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600790
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200791 spin_lock_init(&tilcdc_crtc->irq_lock);
792
Rob Clark16ea9752013-01-08 15:04:28 -0600793 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
794 if (ret < 0)
795 goto fail;
796
797 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
798
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300799 if (priv->is_componentized) {
800 struct device_node *ports =
801 of_get_child_by_name(dev->dev->of_node, "ports");
802
803 if (ports) {
804 crtc->port = of_get_child_by_name(ports, "port");
805 of_node_put(ports);
806 } else {
807 crtc->port =
808 of_get_child_by_name(dev->dev->of_node, "port");
809 }
810 if (!crtc->port) { /* This should never happen */
811 dev_err(dev->dev, "Port node not found in %s\n",
812 dev->dev->of_node->full_name);
813 goto fail;
814 }
815 }
816
Rob Clark16ea9752013-01-08 15:04:28 -0600817 return crtc;
818
819fail:
820 tilcdc_crtc_destroy(crtc);
821 return NULL;
822}