blob: 163f111cd48117fdbb09a511cdfd0c99404e6559 [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>
Jyri Sarha305198d2016-04-07 15:05:16 +030020#include <drm/drm_atomic_helper.h>
Rob Clark16ea9752013-01-08 15:04:28 -060021
22#include "tilcdc_drv.h"
23#include "tilcdc_regs.h"
24
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020025#define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
26
Rob Clark16ea9752013-01-08 15:04:28 -060027struct tilcdc_crtc {
28 struct drm_crtc base;
29
Jyri Sarha47f571c2016-04-07 15:04:18 +030030 struct drm_plane primary;
Rob Clark16ea9752013-01-08 15:04:28 -060031 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060032 struct drm_pending_vblank_event *event;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +030033 bool enabled;
Rob Clark16ea9752013-01-08 15:04:28 -060034 wait_queue_head_t frame_done_wq;
35 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020036 spinlock_t irq_lock;
37
38 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060039
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030040 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020041 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060042
43 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040044 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020045
46 /* Only set if an external encoder is connected */
47 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020048
49 int sync_lost_count;
50 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060051};
52#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
53
Rob Clarka464d612013-08-07 13:41:20 -040054static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060055{
Darren Etheridgef7b45752013-06-21 13:52:26 -050056 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040057 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060058 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060059
60 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040061 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060062 mutex_unlock(&dev->mode_config.mutex);
63}
64
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030065static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060066{
67 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
68 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060069 struct drm_gem_cma_object *gem;
70 unsigned int depth, bpp;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030071 dma_addr_t start, end;
Rob Clark16ea9752013-01-08 15:04:28 -060072
73 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
74 gem = drm_fb_cma_get_gem_obj(fb, 0);
75
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030076 start = gem->paddr + fb->offsets[0] +
77 crtc->y * fb->pitches[0] +
78 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060079
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030080 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060081
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030082 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
Karl Beldanee8c42b2016-08-23 12:56:59 +000083 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end - 1);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030084
85 if (tilcdc_crtc->curr_fb)
86 drm_flip_work_queue(&tilcdc_crtc->unref_work,
87 tilcdc_crtc->curr_fb);
88
89 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060090}
91
Jyri Sarhaafaf8332016-06-21 16:00:44 +030092static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
93{
94 struct tilcdc_drm_private *priv = dev->dev_private;
95
96 tilcdc_clear_irqstatus(dev, 0xffffffff);
97
98 if (priv->rev == 1) {
99 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
100 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000101 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
102 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300103 } else {
104 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
105 LCDC_V2_UNDERFLOW_INT_ENA |
106 LCDC_V2_END_OF_FRAME0_INT_ENA |
107 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
108 }
109}
110
111static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
112{
113 struct tilcdc_drm_private *priv = dev->dev_private;
114
115 /* disable irqs that we might have enabled: */
116 if (priv->rev == 1) {
117 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
118 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
119 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
120 LCDC_V1_END_OF_FRAME_INT_ENA);
121 } else {
122 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
123 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
124 LCDC_V2_END_OF_FRAME0_INT_ENA |
125 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
126 }
127}
128
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300129static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600130{
131 struct drm_device *dev = crtc->dev;
132 struct tilcdc_drm_private *priv = dev->dev_private;
133
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300134 if (priv->rev != 2)
135 return;
136
137 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
138 usleep_range(250, 1000);
139 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
140}
141
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300142static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300143{
144 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300145 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
146
147 if (tilcdc_crtc->enabled)
148 return;
149
150 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300151
152 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600153
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300154 tilcdc_crtc_enable_irqs(dev);
155
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300156 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600157 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
158 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300159
160 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300161
162 tilcdc_crtc->enabled = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600163}
164
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300165void tilcdc_crtc_disable(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600166{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300167 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600168 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300169 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600170
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300171 if (!tilcdc_crtc->enabled)
172 return;
173
Jyri Sarha2d5be882016-04-07 20:20:23 +0300174 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600175 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300176
177 /*
178 * if necessary wait for framedone irq which will still come
179 * before putting things to sleep..
180 */
181 if (priv->rev == 2) {
182 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
183 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300184 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300185 if (ret == 0)
186 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
187 __func__);
188 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300189
190 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300191
192 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300193
194 pm_runtime_put_sync(dev->dev);
195
196 if (tilcdc_crtc->next_fb) {
197 drm_flip_work_queue(&tilcdc_crtc->unref_work,
198 tilcdc_crtc->next_fb);
199 tilcdc_crtc->next_fb = NULL;
200 }
201
202 if (tilcdc_crtc->curr_fb) {
203 drm_flip_work_queue(&tilcdc_crtc->unref_work,
204 tilcdc_crtc->curr_fb);
205 tilcdc_crtc->curr_fb = NULL;
206 }
207
208 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
209 tilcdc_crtc->last_vblank = ktime_set(0, 0);
210
211 tilcdc_crtc->enabled = false;
212}
213
214static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
215{
216 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600217}
218
219static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
220{
221 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
222
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300223 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600224
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300225 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600226 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400227 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600228}
229
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300230int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600231 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300232 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600233{
234 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
235 struct drm_device *dev = crtc->dev;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300236 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000237
Rob Clark16ea9752013-01-08 15:04:28 -0600238 if (tilcdc_crtc->event) {
239 dev_err(dev->dev, "already pending page flip!\n");
240 return -EBUSY;
241 }
242
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300243 drm_framebuffer_reference(fb);
244
Matt Roperf4510a22014-04-01 15:22:40 -0700245 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300246
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200247 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300248
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300249 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
250 ktime_t next_vblank;
251 s64 tdiff;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300252
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300253 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
254 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200255
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300256 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
257
258 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
259 tilcdc_crtc->next_fb = fb;
260 }
261
262 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200263 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200264
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300265 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200266
267 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600268
269 return 0;
270}
271
Rob Clark16ea9752013-01-08 15:04:28 -0600272static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
273 const struct drm_display_mode *mode,
274 struct drm_display_mode *adjusted_mode)
275{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200276 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
277
278 if (!tilcdc_crtc->simulate_vesa_sync)
279 return true;
280
281 /*
282 * tilcdc does not generate VESA-compliant sync but aligns
283 * VS on the second edge of HS instead of first edge.
284 * We use adjusted_mode, to fixup sync by aligning both rising
285 * edges and add HSKEW offset to fix the sync.
286 */
287 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
288 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
289
290 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
291 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
292 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
293 } else {
294 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
295 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
296 }
297
Rob Clark16ea9752013-01-08 15:04:28 -0600298 return true;
299}
300
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300301static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
302{
303 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
304 struct drm_device *dev = crtc->dev;
305 struct tilcdc_drm_private *priv = dev->dev_private;
306 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
307 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
308 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
309 struct drm_framebuffer *fb = crtc->primary->state->fb;
310
311 if (WARN_ON(!info))
312 return;
313
314 if (WARN_ON(!fb))
315 return;
316
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300317 /* Configure the Burst Size and fifo threshold of DMA: */
318 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
319 switch (info->dma_burst_sz) {
320 case 1:
321 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
322 break;
323 case 2:
324 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
325 break;
326 case 4:
327 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
328 break;
329 case 8:
330 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
331 break;
332 case 16:
333 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
334 break;
335 default:
336 dev_err(dev->dev, "invalid burst size\n");
337 return;
338 }
339 reg |= (info->fifo_th << 8);
340 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
341
342 /* Configure timings: */
343 hbp = mode->htotal - mode->hsync_end;
344 hfp = mode->hsync_start - mode->hdisplay;
345 hsw = mode->hsync_end - mode->hsync_start;
346 vbp = mode->vtotal - mode->vsync_end;
347 vfp = mode->vsync_start - mode->vdisplay;
348 vsw = mode->vsync_end - mode->vsync_start;
349
350 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
351 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
352
353 /* Set AC Bias Period and Number of Transitions per Interrupt: */
354 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
355 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
356 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
357
358 /*
359 * subtract one from hfp, hbp, hsw because the hardware uses
360 * a value of 0 as 1
361 */
362 if (priv->rev == 2) {
363 /* clear bits we're going to set */
364 reg &= ~0x78000033;
365 reg |= ((hfp-1) & 0x300) >> 8;
366 reg |= ((hbp-1) & 0x300) >> 4;
367 reg |= ((hsw-1) & 0x3c0) << 21;
368 }
369 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
370
371 reg = (((mode->hdisplay >> 4) - 1) << 4) |
372 (((hbp-1) & 0xff) << 24) |
373 (((hfp-1) & 0xff) << 16) |
374 (((hsw-1) & 0x3f) << 10);
375 if (priv->rev == 2)
376 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
377 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
378
379 reg = ((mode->vdisplay - 1) & 0x3ff) |
380 ((vbp & 0xff) << 24) |
381 ((vfp & 0xff) << 16) |
382 (((vsw-1) & 0x3f) << 10);
383 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
384
385 /*
386 * be sure to set Bit 10 for the V2 LCDC controller,
387 * otherwise limited to 1024 pixels width, stopping
388 * 1920x1080 being supported.
389 */
390 if (priv->rev == 2) {
391 if ((mode->vdisplay - 1) & 0x400) {
392 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
393 LCDC_LPP_B10);
394 } else {
395 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
396 LCDC_LPP_B10);
397 }
398 }
399
400 /* Configure display type: */
401 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
402 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
403 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
404 0x000ff000 /* Palette Loading Delay bits */);
405 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
406 if (info->tft_alt_mode)
407 reg |= LCDC_TFT_ALT_ENABLE;
408 if (priv->rev == 2) {
409 unsigned int depth, bpp;
410
411 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
412 switch (bpp) {
413 case 16:
414 break;
415 case 32:
416 reg |= LCDC_V2_TFT_24BPP_UNPACK;
417 /* fallthrough */
418 case 24:
419 reg |= LCDC_V2_TFT_24BPP_MODE;
420 break;
421 default:
422 dev_err(dev->dev, "invalid pixel format\n");
423 return;
424 }
425 }
426 reg |= info->fdd < 12;
427 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
428
429 if (info->invert_pxl_clk)
430 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
431 else
432 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
433
434 if (info->sync_ctrl)
435 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
436 else
437 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
438
439 if (info->sync_edge)
440 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
441 else
442 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
443
444 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
445 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
446 else
447 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
448
449 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
450 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
451 else
452 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
453
454 if (info->raster_order)
455 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
456 else
457 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
458
459 drm_framebuffer_reference(fb);
460
461 set_scanout(crtc, fb);
462
463 tilcdc_crtc_update_clk(crtc);
464
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300465 crtc->hwmode = crtc->state->adjusted_mode;
466}
467
Jyri Sarhadb380c52016-04-07 15:10:23 +0300468static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
469 struct drm_crtc_state *state)
470{
471 struct drm_display_mode *mode = &state->mode;
472 int ret;
473
474 /* If we are not active we don't care */
475 if (!state->active)
476 return 0;
477
478 if (state->state->planes[0].ptr != crtc->primary ||
479 state->state->planes[0].state == NULL ||
480 state->state->planes[0].state->crtc != crtc) {
481 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
482 return -EINVAL;
483 }
484
485 ret = tilcdc_crtc_mode_valid(crtc, mode);
486 if (ret) {
487 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
488 return -EINVAL;
489 }
490
491 return 0;
492}
493
Rob Clark16ea9752013-01-08 15:04:28 -0600494static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300495 .destroy = tilcdc_crtc_destroy,
496 .set_config = drm_atomic_helper_set_config,
497 .page_flip = drm_atomic_helper_page_flip,
498 .reset = drm_atomic_helper_crtc_reset,
499 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
500 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600501};
502
503static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600504 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300505 .enable = tilcdc_crtc_enable,
506 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300507 .atomic_check = tilcdc_crtc_atomic_check,
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300508 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
Rob Clark16ea9752013-01-08 15:04:28 -0600509};
510
511int tilcdc_crtc_max_width(struct drm_crtc *crtc)
512{
513 struct drm_device *dev = crtc->dev;
514 struct tilcdc_drm_private *priv = dev->dev_private;
515 int max_width = 0;
516
517 if (priv->rev == 1)
518 max_width = 1024;
519 else if (priv->rev == 2)
520 max_width = 2048;
521
522 return max_width;
523}
524
525int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
526{
527 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
528 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500529 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600530
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500531 /*
532 * check to see if the width is within the range that
533 * the LCD Controller physically supports
534 */
Rob Clark16ea9752013-01-08 15:04:28 -0600535 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
536 return MODE_VIRTUAL_X;
537
538 /* width must be multiple of 16 */
539 if (mode->hdisplay & 0xf)
540 return MODE_VIRTUAL_X;
541
542 if (mode->vdisplay > 2048)
543 return MODE_VIRTUAL_Y;
544
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500545 DBG("Processing mode %dx%d@%d with pixel clock %d",
546 mode->hdisplay, mode->vdisplay,
547 drm_mode_vrefresh(mode), mode->clock);
548
549 hbp = mode->htotal - mode->hsync_end;
550 hfp = mode->hsync_start - mode->hdisplay;
551 hsw = mode->hsync_end - mode->hsync_start;
552 vbp = mode->vtotal - mode->vsync_end;
553 vfp = mode->vsync_start - mode->vdisplay;
554 vsw = mode->vsync_end - mode->vsync_start;
555
556 if ((hbp-1) & ~0x3ff) {
557 DBG("Pruning mode: Horizontal Back Porch out of range");
558 return MODE_HBLANK_WIDE;
559 }
560
561 if ((hfp-1) & ~0x3ff) {
562 DBG("Pruning mode: Horizontal Front Porch out of range");
563 return MODE_HBLANK_WIDE;
564 }
565
566 if ((hsw-1) & ~0x3ff) {
567 DBG("Pruning mode: Horizontal Sync Width out of range");
568 return MODE_HSYNC_WIDE;
569 }
570
571 if (vbp & ~0xff) {
572 DBG("Pruning mode: Vertical Back Porch out of range");
573 return MODE_VBLANK_WIDE;
574 }
575
576 if (vfp & ~0xff) {
577 DBG("Pruning mode: Vertical Front Porch out of range");
578 return MODE_VBLANK_WIDE;
579 }
580
581 if ((vsw-1) & ~0x3f) {
582 DBG("Pruning mode: Vertical Sync Width out of range");
583 return MODE_VSYNC_WIDE;
584 }
585
Darren Etheridge4e564342013-06-21 13:52:23 -0500586 /*
587 * some devices have a maximum allowed pixel clock
588 * configured from the DT
589 */
590 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500591 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500592 return MODE_CLOCK_HIGH;
593 }
594
595 /*
596 * some devices further limit the max horizontal resolution
597 * configured from the DT
598 */
599 if (mode->hdisplay > priv->max_width)
600 return MODE_BAD_WIDTH;
601
Rob Clark16ea9752013-01-08 15:04:28 -0600602 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500603 bandwidth = mode->hdisplay * mode->vdisplay *
604 drm_mode_vrefresh(mode);
605 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500606 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600607 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500608 }
Rob Clark16ea9752013-01-08 15:04:28 -0600609
610 return MODE_OK;
611}
612
613void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
614 const struct tilcdc_panel_info *info)
615{
616 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
617 tilcdc_crtc->info = info;
618}
619
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200620void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
621 bool simulate_vesa_sync)
622{
623 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
624
625 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
626}
627
Rob Clark16ea9752013-01-08 15:04:28 -0600628void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
629{
Rob Clark16ea9752013-01-08 15:04:28 -0600630 struct drm_device *dev = crtc->dev;
631 struct tilcdc_drm_private *priv = dev->dev_private;
Darren Etheridge3d193062014-01-15 15:52:36 -0600632 unsigned long lcd_clk;
633 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600634 int ret;
635
636 pm_runtime_get_sync(dev->dev);
637
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300638 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600639
Darren Etheridge3d193062014-01-15 15:52:36 -0600640 /* mode.clock is in KHz, set_rate wants parameter in Hz */
641 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
642 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600643 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
644 crtc->mode.clock);
645 goto out;
646 }
647
648 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600649
Darren Etheridge3d193062014-01-15 15:52:36 -0600650 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
651 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600652
653 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600654 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600655 LCDC_RASTER_MODE);
656
657 if (priv->rev == 2)
658 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
659 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
660 LCDC_V2_CORE_CLK_EN);
661
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300662 if (tilcdc_crtc_is_on(crtc))
663 tilcdc_crtc_enable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600664
665out:
666 pm_runtime_put_sync(dev->dev);
667}
668
Jyri Sarha5895d082016-01-08 14:33:09 +0200669#define SYNC_LOST_COUNT_LIMIT 50
670
Rob Clark16ea9752013-01-08 15:04:28 -0600671irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
672{
673 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
674 struct drm_device *dev = crtc->dev;
675 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300676 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600677
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300678 stat = tilcdc_read_irqstatus(dev);
679 tilcdc_clear_irqstatus(dev, stat);
680
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300681 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600682 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200683 bool skip_event = false;
684 ktime_t now;
685
686 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600687
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300688 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600689
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200690 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600691
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200692 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600693
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200694 if (tilcdc_crtc->next_fb) {
695 set_scanout(crtc, tilcdc_crtc->next_fb);
696 tilcdc_crtc->next_fb = NULL;
697 skip_event = true;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300698 }
699
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200700 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
701
Gustavo Padovan099ede82016-07-04 21:04:52 -0300702 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200703
704 if (!skip_event) {
705 struct drm_pending_vblank_event *event;
706
707 spin_lock_irqsave(&dev->event_lock, flags);
708
709 event = tilcdc_crtc->event;
710 tilcdc_crtc->event = NULL;
711 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700712 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200713
714 spin_unlock_irqrestore(&dev->event_lock, flags);
715 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200716
717 if (tilcdc_crtc->frame_intact)
718 tilcdc_crtc->sync_lost_count = 0;
719 else
720 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600721 }
722
Jyri Sarha14944112016-04-07 20:36:48 +0300723 if (stat & LCDC_FIFO_UNDERFLOW)
724 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
725 __func__, stat);
726
727 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600728 if (priv->rev == 2) {
729 if (stat & LCDC_FRAME_DONE) {
730 tilcdc_crtc->frame_done = true;
731 wake_up(&tilcdc_crtc->frame_done_wq);
732 }
Rob Clark16ea9752013-01-08 15:04:28 -0600733
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300734 if (stat & LCDC_SYNC_LOST) {
735 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
736 __func__, stat);
737 tilcdc_crtc->frame_intact = false;
738 if (tilcdc_crtc->sync_lost_count++ >
739 SYNC_LOST_COUNT_LIMIT) {
740 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
741 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
742 LCDC_SYNC_LOST);
743 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200744 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200745
Jyri Sarha14944112016-04-07 20:36:48 +0300746 /* Indicate to LCDC that the interrupt service routine has
747 * completed, see 13.3.6.1.6 in AM335x TRM.
748 */
749 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
750 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200751
Rob Clark16ea9752013-01-08 15:04:28 -0600752 return IRQ_HANDLED;
753}
754
Rob Clark16ea9752013-01-08 15:04:28 -0600755struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
756{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300757 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600758 struct tilcdc_crtc *tilcdc_crtc;
759 struct drm_crtc *crtc;
760 int ret;
761
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200762 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600763 if (!tilcdc_crtc) {
764 dev_err(dev->dev, "allocation failed\n");
765 return NULL;
766 }
767
768 crtc = &tilcdc_crtc->base;
769
Jyri Sarha47f571c2016-04-07 15:04:18 +0300770 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
771 if (ret < 0)
772 goto fail;
773
Rob Clark16ea9752013-01-08 15:04:28 -0600774 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
775
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100776 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400777 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600778
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200779 spin_lock_init(&tilcdc_crtc->irq_lock);
780
Jyri Sarha47f571c2016-04-07 15:04:18 +0300781 ret = drm_crtc_init_with_planes(dev, crtc,
782 &tilcdc_crtc->primary,
783 NULL,
784 &tilcdc_crtc_funcs,
785 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -0600786 if (ret < 0)
787 goto fail;
788
789 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
790
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300791 if (priv->is_componentized) {
792 struct device_node *ports =
793 of_get_child_by_name(dev->dev->of_node, "ports");
794
795 if (ports) {
796 crtc->port = of_get_child_by_name(ports, "port");
797 of_node_put(ports);
798 } else {
799 crtc->port =
800 of_get_child_by_name(dev->dev->of_node, "port");
801 }
802 if (!crtc->port) { /* This should never happen */
803 dev_err(dev->dev, "Port node not found in %s\n",
804 dev->dev->of_node->full_name);
805 goto fail;
806 }
807 }
808
Rob Clark16ea9752013-01-08 15:04:28 -0600809 return crtc;
810
811fail:
812 tilcdc_crtc_destroy(crtc);
813 return NULL;
814}