blob: e762b4ed9308dbb3709575006e20bf2f20772137 [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
Jyri Sarha47f571c2016-04-07 15:04:18 +030029 struct drm_plane primary;
Rob Clark16ea9752013-01-08 15:04:28 -060030 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060031 struct drm_pending_vblank_event *event;
32 int dpms;
33 wait_queue_head_t frame_done_wq;
34 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020035 spinlock_t irq_lock;
36
37 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060038
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030039 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020040 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060041
42 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040043 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020044
45 /* Only set if an external encoder is connected */
46 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020047
48 int sync_lost_count;
49 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060050};
51#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
52
Rob Clarka464d612013-08-07 13:41:20 -040053static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060054{
Darren Etheridgef7b45752013-06-21 13:52:26 -050055 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040056 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060057 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060058
59 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040060 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060061 mutex_unlock(&dev->mode_config.mutex);
62}
63
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030064static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060065{
66 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
67 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060068 struct drm_gem_cma_object *gem;
69 unsigned int depth, bpp;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030070 dma_addr_t start, end;
Rob Clark16ea9752013-01-08 15:04:28 -060071
72 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
73 gem = drm_fb_cma_get_gem_obj(fb, 0);
74
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030075 start = gem->paddr + fb->offsets[0] +
76 crtc->y * fb->pitches[0] +
77 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060078
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030079 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060080
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030081 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
82 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
83
84 if (tilcdc_crtc->curr_fb)
85 drm_flip_work_queue(&tilcdc_crtc->unref_work,
86 tilcdc_crtc->curr_fb);
87
88 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060089}
90
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030091static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -060092{
93 struct drm_device *dev = crtc->dev;
94 struct tilcdc_drm_private *priv = dev->dev_private;
95
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030096 if (priv->rev != 2)
97 return;
98
99 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
100 usleep_range(250, 1000);
101 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
102}
103
104static void start(struct drm_crtc *crtc)
105{
106 struct drm_device *dev = crtc->dev;
107
108 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600109
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300110 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600111 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
112 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300113
114 drm_crtc_vblank_on(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600115}
116
117static void stop(struct drm_crtc *crtc)
118{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300119 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600120 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300121 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600122
Jyri Sarha2d5be882016-04-07 20:20:23 +0300123 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600124 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300125
126 /*
127 * if necessary wait for framedone irq which will still come
128 * before putting things to sleep..
129 */
130 if (priv->rev == 2) {
131 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
132 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300133 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300134 if (ret == 0)
135 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
136 __func__);
137 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300138
139 drm_crtc_vblank_off(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600140}
141
142static 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
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300148 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600149 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400150 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600151}
152
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000153static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
154{
155 struct drm_device *dev = crtc->dev;
156 unsigned int depth, bpp;
157
158 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
159
160 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
161 dev_err(dev->dev,
162 "Invalid pitch: fb and crtc widths must be the same");
163 return -EINVAL;
164 }
165
166 return 0;
167}
168
Jyri Sarha8c65abb2016-04-07 14:56:32 +0300169int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600170 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700171 struct drm_pending_vblank_event *event,
172 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600173{
174 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
175 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000176 int r;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300177 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000178
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
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300188 drm_framebuffer_reference(fb);
189
Matt Roperf4510a22014-04-01 15:22:40 -0700190 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300191
192 pm_runtime_get_sync(dev->dev);
193
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200194 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300195
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300196 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
197 ktime_t next_vblank;
198 s64 tdiff;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300199
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300200 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
201 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200202
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300203 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
204
205 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
206 tilcdc_crtc->next_fb = fb;
207 }
208
209 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200210 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200211
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300212 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200213
214 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600215
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300216 pm_runtime_put_sync(dev->dev);
217
Rob Clark16ea9752013-01-08 15:04:28 -0600218 return 0;
219}
220
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000221void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
Rob Clark16ea9752013-01-08 15:04:28 -0600222{
223 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
224 struct drm_device *dev = crtc->dev;
225 struct tilcdc_drm_private *priv = dev->dev_private;
226
227 /* we really only care about on or off: */
228 if (mode != DRM_MODE_DPMS_ON)
229 mode = DRM_MODE_DPMS_OFF;
230
231 if (tilcdc_crtc->dpms == mode)
232 return;
233
234 tilcdc_crtc->dpms = mode;
235
Rob Clark16ea9752013-01-08 15:04:28 -0600236 if (mode == DRM_MODE_DPMS_ON) {
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300237 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600238 start(crtc);
239 } else {
Rob Clark16ea9752013-01-08 15:04:28 -0600240 stop(crtc);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300241 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300242
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200243 if (tilcdc_crtc->next_fb) {
244 drm_flip_work_queue(&tilcdc_crtc->unref_work,
245 tilcdc_crtc->next_fb);
246 tilcdc_crtc->next_fb = NULL;
247 }
248
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300249 if (tilcdc_crtc->curr_fb) {
250 drm_flip_work_queue(&tilcdc_crtc->unref_work,
251 tilcdc_crtc->curr_fb);
252 tilcdc_crtc->curr_fb = NULL;
253 }
254
255 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300256 tilcdc_crtc->last_vblank = ktime_set(0, 0);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300257 }
Rob Clark16ea9752013-01-08 15:04:28 -0600258}
259
Jyri Sarha8fe56162016-06-14 11:43:30 +0300260int tilcdc_crtc_current_dpms_state(struct drm_crtc *crtc)
261{
262 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
263
264 return tilcdc_crtc->dpms;
265}
266
Rob Clark16ea9752013-01-08 15:04:28 -0600267static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
268 const struct drm_display_mode *mode,
269 struct drm_display_mode *adjusted_mode)
270{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200271 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
272
273 if (!tilcdc_crtc->simulate_vesa_sync)
274 return true;
275
276 /*
277 * tilcdc does not generate VESA-compliant sync but aligns
278 * VS on the second edge of HS instead of first edge.
279 * We use adjusted_mode, to fixup sync by aligning both rising
280 * edges and add HSKEW offset to fix the sync.
281 */
282 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
283 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
284
285 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
286 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
287 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
288 } else {
289 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
290 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
291 }
292
Rob Clark16ea9752013-01-08 15:04:28 -0600293 return true;
294}
295
296static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
297{
298 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
299}
300
301static void tilcdc_crtc_commit(struct drm_crtc *crtc)
302{
303 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
304}
305
306static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
307 struct drm_display_mode *mode,
308 struct drm_display_mode *adjusted_mode,
309 int x, int y,
310 struct drm_framebuffer *old_fb)
311{
312 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
313 struct drm_device *dev = crtc->dev;
314 struct tilcdc_drm_private *priv = dev->dev_private;
315 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
316 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
317 int ret;
318
319 ret = tilcdc_crtc_mode_valid(crtc, mode);
320 if (WARN_ON(ret))
321 return ret;
322
323 if (WARN_ON(!info))
324 return -EINVAL;
325
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000326 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
327 if (ret)
328 return ret;
329
Rob Clark16ea9752013-01-08 15:04:28 -0600330 pm_runtime_get_sync(dev->dev);
331
332 /* Configure the Burst Size and fifo threshold of DMA: */
333 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
334 switch (info->dma_burst_sz) {
335 case 1:
336 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
337 break;
338 case 2:
339 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
340 break;
341 case 4:
342 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
343 break;
344 case 8:
345 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
346 break;
347 case 16:
348 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
349 break;
350 default:
351 return -EINVAL;
352 }
353 reg |= (info->fifo_th << 8);
354 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
355
356 /* Configure timings: */
357 hbp = mode->htotal - mode->hsync_end;
358 hfp = mode->hsync_start - mode->hdisplay;
359 hsw = mode->hsync_end - mode->hsync_start;
360 vbp = mode->vtotal - mode->vsync_end;
361 vfp = mode->vsync_start - mode->vdisplay;
362 vsw = mode->vsync_end - mode->vsync_start;
363
364 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
365 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
366
367 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
368 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
369 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
370 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500371
372 /*
373 * subtract one from hfp, hbp, hsw because the hardware uses
374 * a value of 0 as 1
375 */
Rob Clark16ea9752013-01-08 15:04:28 -0600376 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500377 /* clear bits we're going to set */
378 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500379 reg |= ((hfp-1) & 0x300) >> 8;
380 reg |= ((hbp-1) & 0x300) >> 4;
381 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600382 }
383 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
384
385 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500386 (((hbp-1) & 0xff) << 24) |
387 (((hfp-1) & 0xff) << 16) |
388 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600389 if (priv->rev == 2)
390 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
391 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
392
393 reg = ((mode->vdisplay - 1) & 0x3ff) |
394 ((vbp & 0xff) << 24) |
395 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500396 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600397 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
398
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500399 /*
400 * be sure to set Bit 10 for the V2 LCDC controller,
401 * otherwise limited to 1024 pixels width, stopping
402 * 1920x1080 being suppoted.
403 */
404 if (priv->rev == 2) {
405 if ((mode->vdisplay - 1) & 0x400) {
406 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
407 LCDC_LPP_B10);
408 } else {
409 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
410 LCDC_LPP_B10);
411 }
412 }
413
Rob Clark16ea9752013-01-08 15:04:28 -0600414 /* Configure display type: */
415 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
416 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
417 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
418 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
419 if (info->tft_alt_mode)
420 reg |= LCDC_TFT_ALT_ENABLE;
421 if (priv->rev == 2) {
422 unsigned int depth, bpp;
423
Matt Roperf4510a22014-04-01 15:22:40 -0700424 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600425 switch (bpp) {
426 case 16:
427 break;
428 case 32:
429 reg |= LCDC_V2_TFT_24BPP_UNPACK;
430 /* fallthrough */
431 case 24:
432 reg |= LCDC_V2_TFT_24BPP_MODE;
433 break;
434 default:
435 dev_err(dev->dev, "invalid pixel format\n");
436 return -EINVAL;
437 }
438 }
439 reg |= info->fdd < 12;
440 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
441
442 if (info->invert_pxl_clk)
443 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
444 else
445 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
446
447 if (info->sync_ctrl)
448 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
449 else
450 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
451
452 if (info->sync_edge)
453 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
454 else
455 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
456
Darren Etheridgea9767182013-08-14 21:43:33 +0200457 /*
458 * use value from adjusted_mode here as this might have been
459 * changed as part of the fixup for slave encoders to solve the
460 * issue where tilcdc timings are not VESA compliant
461 */
462 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600463 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
464 else
465 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
466
467 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
468 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
469 else
470 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
471
472 if (info->raster_order)
473 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
474 else
475 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
476
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300477 drm_framebuffer_reference(crtc->primary->fb);
Rob Clark16ea9752013-01-08 15:04:28 -0600478
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300479 set_scanout(crtc, crtc->primary->fb);
480
Rob Clark16ea9752013-01-08 15:04:28 -0600481 tilcdc_crtc_update_clk(crtc);
482
483 pm_runtime_put_sync(dev->dev);
484
485 return 0;
486}
487
488static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
489 struct drm_framebuffer *old_fb)
490{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300491 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000492 int r;
493
494 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
495 if (r)
496 return r;
497
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300498 drm_framebuffer_reference(crtc->primary->fb);
499
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300500 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300501
502 set_scanout(crtc, crtc->primary->fb);
503
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300504 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300505
Rob Clark16ea9752013-01-08 15:04:28 -0600506 return 0;
507}
508
Rob Clark16ea9752013-01-08 15:04:28 -0600509static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
510 .destroy = tilcdc_crtc_destroy,
511 .set_config = drm_crtc_helper_set_config,
512 .page_flip = tilcdc_crtc_page_flip,
513};
514
515static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
516 .dpms = tilcdc_crtc_dpms,
517 .mode_fixup = tilcdc_crtc_mode_fixup,
518 .prepare = tilcdc_crtc_prepare,
519 .commit = tilcdc_crtc_commit,
520 .mode_set = tilcdc_crtc_mode_set,
521 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600522};
523
524int tilcdc_crtc_max_width(struct drm_crtc *crtc)
525{
526 struct drm_device *dev = crtc->dev;
527 struct tilcdc_drm_private *priv = dev->dev_private;
528 int max_width = 0;
529
530 if (priv->rev == 1)
531 max_width = 1024;
532 else if (priv->rev == 2)
533 max_width = 2048;
534
535 return max_width;
536}
537
538int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
539{
540 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
541 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500542 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600543
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500544 /*
545 * check to see if the width is within the range that
546 * the LCD Controller physically supports
547 */
Rob Clark16ea9752013-01-08 15:04:28 -0600548 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
549 return MODE_VIRTUAL_X;
550
551 /* width must be multiple of 16 */
552 if (mode->hdisplay & 0xf)
553 return MODE_VIRTUAL_X;
554
555 if (mode->vdisplay > 2048)
556 return MODE_VIRTUAL_Y;
557
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500558 DBG("Processing mode %dx%d@%d with pixel clock %d",
559 mode->hdisplay, mode->vdisplay,
560 drm_mode_vrefresh(mode), mode->clock);
561
562 hbp = mode->htotal - mode->hsync_end;
563 hfp = mode->hsync_start - mode->hdisplay;
564 hsw = mode->hsync_end - mode->hsync_start;
565 vbp = mode->vtotal - mode->vsync_end;
566 vfp = mode->vsync_start - mode->vdisplay;
567 vsw = mode->vsync_end - mode->vsync_start;
568
569 if ((hbp-1) & ~0x3ff) {
570 DBG("Pruning mode: Horizontal Back Porch out of range");
571 return MODE_HBLANK_WIDE;
572 }
573
574 if ((hfp-1) & ~0x3ff) {
575 DBG("Pruning mode: Horizontal Front Porch out of range");
576 return MODE_HBLANK_WIDE;
577 }
578
579 if ((hsw-1) & ~0x3ff) {
580 DBG("Pruning mode: Horizontal Sync Width out of range");
581 return MODE_HSYNC_WIDE;
582 }
583
584 if (vbp & ~0xff) {
585 DBG("Pruning mode: Vertical Back Porch out of range");
586 return MODE_VBLANK_WIDE;
587 }
588
589 if (vfp & ~0xff) {
590 DBG("Pruning mode: Vertical Front Porch out of range");
591 return MODE_VBLANK_WIDE;
592 }
593
594 if ((vsw-1) & ~0x3f) {
595 DBG("Pruning mode: Vertical Sync Width out of range");
596 return MODE_VSYNC_WIDE;
597 }
598
Darren Etheridge4e564342013-06-21 13:52:23 -0500599 /*
600 * some devices have a maximum allowed pixel clock
601 * configured from the DT
602 */
603 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500604 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500605 return MODE_CLOCK_HIGH;
606 }
607
608 /*
609 * some devices further limit the max horizontal resolution
610 * configured from the DT
611 */
612 if (mode->hdisplay > priv->max_width)
613 return MODE_BAD_WIDTH;
614
Rob Clark16ea9752013-01-08 15:04:28 -0600615 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500616 bandwidth = mode->hdisplay * mode->vdisplay *
617 drm_mode_vrefresh(mode);
618 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500619 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600620 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500621 }
Rob Clark16ea9752013-01-08 15:04:28 -0600622
623 return MODE_OK;
624}
625
626void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
627 const struct tilcdc_panel_info *info)
628{
629 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
630 tilcdc_crtc->info = info;
631}
632
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200633void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
634 bool simulate_vesa_sync)
635{
636 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
637
638 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
639}
640
Rob Clark16ea9752013-01-08 15:04:28 -0600641void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
642{
643 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
644 struct drm_device *dev = crtc->dev;
645 struct tilcdc_drm_private *priv = dev->dev_private;
646 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600647 unsigned long lcd_clk;
648 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600649 int ret;
650
651 pm_runtime_get_sync(dev->dev);
652
653 if (dpms == DRM_MODE_DPMS_ON)
654 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
655
Darren Etheridge3d193062014-01-15 15:52:36 -0600656 /* mode.clock is in KHz, set_rate wants parameter in Hz */
657 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
658 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600659 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
660 crtc->mode.clock);
661 goto out;
662 }
663
664 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600665
Darren Etheridge3d193062014-01-15 15:52:36 -0600666 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
667 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600668
669 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600670 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600671 LCDC_RASTER_MODE);
672
673 if (priv->rev == 2)
674 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
675 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
676 LCDC_V2_CORE_CLK_EN);
677
678 if (dpms == DRM_MODE_DPMS_ON)
679 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
680
681out:
682 pm_runtime_put_sync(dev->dev);
683}
684
Jyri Sarha5895d082016-01-08 14:33:09 +0200685#define SYNC_LOST_COUNT_LIMIT 50
686
Rob Clark16ea9752013-01-08 15:04:28 -0600687irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
688{
689 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
690 struct drm_device *dev = crtc->dev;
691 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300692 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600693
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300694 stat = tilcdc_read_irqstatus(dev);
695 tilcdc_clear_irqstatus(dev, stat);
696
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300697 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600698 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200699 bool skip_event = false;
700 ktime_t now;
701
702 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600703
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300704 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600705
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200706 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600707
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200708 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600709
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200710 if (tilcdc_crtc->next_fb) {
711 set_scanout(crtc, tilcdc_crtc->next_fb);
712 tilcdc_crtc->next_fb = NULL;
713 skip_event = true;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300714 }
715
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200716 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
717
Gustavo Padovan099ede82016-07-04 21:04:52 -0300718 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200719
720 if (!skip_event) {
721 struct drm_pending_vblank_event *event;
722
723 spin_lock_irqsave(&dev->event_lock, flags);
724
725 event = tilcdc_crtc->event;
726 tilcdc_crtc->event = NULL;
727 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700728 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200729
730 spin_unlock_irqrestore(&dev->event_lock, flags);
731 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200732
733 if (tilcdc_crtc->frame_intact)
734 tilcdc_crtc->sync_lost_count = 0;
735 else
736 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600737 }
738
Jyri Sarha14944112016-04-07 20:36:48 +0300739 if (stat & LCDC_FIFO_UNDERFLOW)
740 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
741 __func__, stat);
742
743 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600744 if (priv->rev == 2) {
745 if (stat & LCDC_FRAME_DONE) {
746 tilcdc_crtc->frame_done = true;
747 wake_up(&tilcdc_crtc->frame_done_wq);
748 }
Rob Clark16ea9752013-01-08 15:04:28 -0600749
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300750 if (stat & LCDC_SYNC_LOST) {
751 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
752 __func__, stat);
753 tilcdc_crtc->frame_intact = false;
754 if (tilcdc_crtc->sync_lost_count++ >
755 SYNC_LOST_COUNT_LIMIT) {
756 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
757 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
758 LCDC_SYNC_LOST);
759 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200760 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200761
Jyri Sarha14944112016-04-07 20:36:48 +0300762 /* Indicate to LCDC that the interrupt service routine has
763 * completed, see 13.3.6.1.6 in AM335x TRM.
764 */
765 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
766 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200767
Rob Clark16ea9752013-01-08 15:04:28 -0600768 return IRQ_HANDLED;
769}
770
Rob Clark16ea9752013-01-08 15:04:28 -0600771struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
772{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300773 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600774 struct tilcdc_crtc *tilcdc_crtc;
775 struct drm_crtc *crtc;
776 int ret;
777
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200778 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600779 if (!tilcdc_crtc) {
780 dev_err(dev->dev, "allocation failed\n");
781 return NULL;
782 }
783
784 crtc = &tilcdc_crtc->base;
785
Jyri Sarha47f571c2016-04-07 15:04:18 +0300786 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
787 if (ret < 0)
788 goto fail;
789
Rob Clark16ea9752013-01-08 15:04:28 -0600790 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
791 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
792
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100793 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400794 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600795
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200796 spin_lock_init(&tilcdc_crtc->irq_lock);
797
Jyri Sarha47f571c2016-04-07 15:04:18 +0300798 ret = drm_crtc_init_with_planes(dev, crtc,
799 &tilcdc_crtc->primary,
800 NULL,
801 &tilcdc_crtc_funcs,
802 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -0600803 if (ret < 0)
804 goto fail;
805
806 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
807
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300808 if (priv->is_componentized) {
809 struct device_node *ports =
810 of_get_child_by_name(dev->dev->of_node, "ports");
811
812 if (ports) {
813 crtc->port = of_get_child_by_name(ports, "port");
814 of_node_put(ports);
815 } else {
816 crtc->port =
817 of_get_child_by_name(dev->dev->of_node, "port");
818 }
819 if (!crtc->port) { /* This should never happen */
820 dev_err(dev->dev, "Port node not found in %s\n",
821 dev->dev->of_node->full_name);
822 goto fail;
823 }
824 }
825
Rob Clark16ea9752013-01-08 15:04:28 -0600826 return crtc;
827
828fail:
829 tilcdc_crtc_destroy(crtc);
830 return NULL;
831}