blob: 5579d97b08e603f334fd5117f185fd4833a9858a [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
Jyri Sarha642e5162016-09-06 16:19:54 +030038 unsigned int lcd_fck_rate;
39
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020040 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060041
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030042 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020043 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060044
45 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040046 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020047
48 /* Only set if an external encoder is connected */
49 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020050
51 int sync_lost_count;
52 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060053};
54#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
55
Rob Clarka464d612013-08-07 13:41:20 -040056static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060057{
Darren Etheridgef7b45752013-06-21 13:52:26 -050058 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040059 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060060 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060061
62 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040063 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060064 mutex_unlock(&dev->mode_config.mutex);
65}
66
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030067static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060068{
69 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
70 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060071 struct drm_gem_cma_object *gem;
72 unsigned int depth, bpp;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030073 dma_addr_t start, end;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030074 u64 dma_base_and_ceiling;
Rob Clark16ea9752013-01-08 15:04:28 -060075
76 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
77 gem = drm_fb_cma_get_gem_obj(fb, 0);
78
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030079 start = gem->paddr + fb->offsets[0] +
80 crtc->y * fb->pitches[0] +
81 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060082
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030083 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060084
Jyri Sarha7eb9f062016-08-26 15:10:14 +030085 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
86 * with a single insruction, if available. This should make it more
87 * unlikely that LCDC would fetch the DMA addresses in the middle of
88 * an update.
89 */
90 dma_base_and_ceiling = (u64)(end - 1) << 32 | start;
91 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030092
93 if (tilcdc_crtc->curr_fb)
94 drm_flip_work_queue(&tilcdc_crtc->unref_work,
95 tilcdc_crtc->curr_fb);
96
97 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060098}
99
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300100static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
101{
102 struct tilcdc_drm_private *priv = dev->dev_private;
103
104 tilcdc_clear_irqstatus(dev, 0xffffffff);
105
106 if (priv->rev == 1) {
107 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
108 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000109 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
110 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300111 } else {
112 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
113 LCDC_V2_UNDERFLOW_INT_ENA |
114 LCDC_V2_END_OF_FRAME0_INT_ENA |
115 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
116 }
117}
118
119static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
120{
121 struct tilcdc_drm_private *priv = dev->dev_private;
122
123 /* disable irqs that we might have enabled: */
124 if (priv->rev == 1) {
125 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
126 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
127 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
128 LCDC_V1_END_OF_FRAME_INT_ENA);
129 } else {
130 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
131 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
132 LCDC_V2_END_OF_FRAME0_INT_ENA |
133 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
134 }
135}
136
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300137static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600138{
139 struct drm_device *dev = crtc->dev;
140 struct tilcdc_drm_private *priv = dev->dev_private;
141
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300142 if (priv->rev != 2)
143 return;
144
145 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
146 usleep_range(250, 1000);
147 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
148}
149
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300150static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300151{
152 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300153 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
154
155 if (tilcdc_crtc->enabled)
156 return;
157
158 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300159
160 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600161
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300162 tilcdc_crtc_enable_irqs(dev);
163
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300164 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600165 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
166 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300167
168 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300169
170 tilcdc_crtc->enabled = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600171}
172
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300173void tilcdc_crtc_disable(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600174{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300175 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600176 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300177 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600178
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300179 if (!tilcdc_crtc->enabled)
180 return;
181
Jyri Sarha2d5be882016-04-07 20:20:23 +0300182 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600183 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300184
185 /*
186 * if necessary wait for framedone irq which will still come
187 * before putting things to sleep..
188 */
189 if (priv->rev == 2) {
190 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
191 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300192 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300193 if (ret == 0)
194 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
195 __func__);
196 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300197
198 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300199
200 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300201
202 pm_runtime_put_sync(dev->dev);
203
204 if (tilcdc_crtc->next_fb) {
205 drm_flip_work_queue(&tilcdc_crtc->unref_work,
206 tilcdc_crtc->next_fb);
207 tilcdc_crtc->next_fb = NULL;
208 }
209
210 if (tilcdc_crtc->curr_fb) {
211 drm_flip_work_queue(&tilcdc_crtc->unref_work,
212 tilcdc_crtc->curr_fb);
213 tilcdc_crtc->curr_fb = NULL;
214 }
215
216 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
217 tilcdc_crtc->last_vblank = ktime_set(0, 0);
218
219 tilcdc_crtc->enabled = false;
220}
221
222static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
223{
224 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600225}
226
227static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
228{
229 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
230
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300231 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600232
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300233 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600234 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400235 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600236}
237
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300238int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600239 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300240 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600241{
242 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
243 struct drm_device *dev = crtc->dev;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300244 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000245
Rob Clark16ea9752013-01-08 15:04:28 -0600246 if (tilcdc_crtc->event) {
247 dev_err(dev->dev, "already pending page flip!\n");
248 return -EBUSY;
249 }
250
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300251 drm_framebuffer_reference(fb);
252
Matt Roperf4510a22014-04-01 15:22:40 -0700253 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300254
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200255 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300256
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300257 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
258 ktime_t next_vblank;
259 s64 tdiff;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300260
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300261 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
262 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200263
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300264 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
265
266 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
267 tilcdc_crtc->next_fb = fb;
268 }
269
270 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200271 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200272
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300273 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200274
275 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600276
277 return 0;
278}
279
Rob Clark16ea9752013-01-08 15:04:28 -0600280static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
281 const struct drm_display_mode *mode,
282 struct drm_display_mode *adjusted_mode)
283{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200284 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
285
286 if (!tilcdc_crtc->simulate_vesa_sync)
287 return true;
288
289 /*
290 * tilcdc does not generate VESA-compliant sync but aligns
291 * VS on the second edge of HS instead of first edge.
292 * We use adjusted_mode, to fixup sync by aligning both rising
293 * edges and add HSKEW offset to fix the sync.
294 */
295 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
296 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
297
298 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
299 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
300 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
301 } else {
302 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
303 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
304 }
305
Rob Clark16ea9752013-01-08 15:04:28 -0600306 return true;
307}
308
Jyri Sarha642e5162016-09-06 16:19:54 +0300309static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
310{
311 struct drm_device *dev = crtc->dev;
312 struct tilcdc_drm_private *priv = dev->dev_private;
313 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
314 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
315 int ret;
316
317 /* mode.clock is in KHz, set_rate wants parameter in Hz */
318 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
319 if (ret < 0) {
320 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
321 crtc->mode.clock);
322 return;
323 }
324
325 tilcdc_crtc->lcd_fck_rate = clk_get_rate(priv->clk);
326
327 DBG("lcd_clk=%u, mode clock=%d, div=%u",
328 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
329
330 /* Configure the LCD clock divisor. */
331 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
332 LCDC_RASTER_MODE);
333
334 if (priv->rev == 2)
335 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
336 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
337 LCDC_V2_CORE_CLK_EN);
338}
339
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300340static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
341{
342 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
343 struct drm_device *dev = crtc->dev;
344 struct tilcdc_drm_private *priv = dev->dev_private;
345 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
346 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
347 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
348 struct drm_framebuffer *fb = crtc->primary->state->fb;
349
350 if (WARN_ON(!info))
351 return;
352
353 if (WARN_ON(!fb))
354 return;
355
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300356 /* Configure the Burst Size and fifo threshold of DMA: */
357 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
358 switch (info->dma_burst_sz) {
359 case 1:
360 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
361 break;
362 case 2:
363 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
364 break;
365 case 4:
366 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
367 break;
368 case 8:
369 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
370 break;
371 case 16:
372 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
373 break;
374 default:
375 dev_err(dev->dev, "invalid burst size\n");
376 return;
377 }
378 reg |= (info->fifo_th << 8);
379 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
380
381 /* Configure timings: */
382 hbp = mode->htotal - mode->hsync_end;
383 hfp = mode->hsync_start - mode->hdisplay;
384 hsw = mode->hsync_end - mode->hsync_start;
385 vbp = mode->vtotal - mode->vsync_end;
386 vfp = mode->vsync_start - mode->vdisplay;
387 vsw = mode->vsync_end - mode->vsync_start;
388
389 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
390 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
391
392 /* Set AC Bias Period and Number of Transitions per Interrupt: */
393 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
394 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
395 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
396
397 /*
398 * subtract one from hfp, hbp, hsw because the hardware uses
399 * a value of 0 as 1
400 */
401 if (priv->rev == 2) {
402 /* clear bits we're going to set */
403 reg &= ~0x78000033;
404 reg |= ((hfp-1) & 0x300) >> 8;
405 reg |= ((hbp-1) & 0x300) >> 4;
406 reg |= ((hsw-1) & 0x3c0) << 21;
407 }
408 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
409
410 reg = (((mode->hdisplay >> 4) - 1) << 4) |
411 (((hbp-1) & 0xff) << 24) |
412 (((hfp-1) & 0xff) << 16) |
413 (((hsw-1) & 0x3f) << 10);
414 if (priv->rev == 2)
415 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
416 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
417
418 reg = ((mode->vdisplay - 1) & 0x3ff) |
419 ((vbp & 0xff) << 24) |
420 ((vfp & 0xff) << 16) |
421 (((vsw-1) & 0x3f) << 10);
422 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
423
424 /*
425 * be sure to set Bit 10 for the V2 LCDC controller,
426 * otherwise limited to 1024 pixels width, stopping
427 * 1920x1080 being supported.
428 */
429 if (priv->rev == 2) {
430 if ((mode->vdisplay - 1) & 0x400) {
431 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
432 LCDC_LPP_B10);
433 } else {
434 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
435 LCDC_LPP_B10);
436 }
437 }
438
439 /* Configure display type: */
440 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
441 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
442 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
443 0x000ff000 /* Palette Loading Delay bits */);
444 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
445 if (info->tft_alt_mode)
446 reg |= LCDC_TFT_ALT_ENABLE;
447 if (priv->rev == 2) {
448 unsigned int depth, bpp;
449
450 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
451 switch (bpp) {
452 case 16:
453 break;
454 case 32:
455 reg |= LCDC_V2_TFT_24BPP_UNPACK;
456 /* fallthrough */
457 case 24:
458 reg |= LCDC_V2_TFT_24BPP_MODE;
459 break;
460 default:
461 dev_err(dev->dev, "invalid pixel format\n");
462 return;
463 }
464 }
465 reg |= info->fdd < 12;
466 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
467
468 if (info->invert_pxl_clk)
469 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
470 else
471 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
472
473 if (info->sync_ctrl)
474 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
475 else
476 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
477
478 if (info->sync_edge)
479 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
480 else
481 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
482
483 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
484 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
485 else
486 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
487
488 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
489 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
490 else
491 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
492
493 if (info->raster_order)
494 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
495 else
496 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
497
498 drm_framebuffer_reference(fb);
499
500 set_scanout(crtc, fb);
501
Jyri Sarha642e5162016-09-06 16:19:54 +0300502 tilcdc_crtc_set_clk(crtc);
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300503
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300504 crtc->hwmode = crtc->state->adjusted_mode;
505}
506
Jyri Sarhadb380c52016-04-07 15:10:23 +0300507static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
508 struct drm_crtc_state *state)
509{
510 struct drm_display_mode *mode = &state->mode;
511 int ret;
512
513 /* If we are not active we don't care */
514 if (!state->active)
515 return 0;
516
517 if (state->state->planes[0].ptr != crtc->primary ||
518 state->state->planes[0].state == NULL ||
519 state->state->planes[0].state->crtc != crtc) {
520 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
521 return -EINVAL;
522 }
523
524 ret = tilcdc_crtc_mode_valid(crtc, mode);
525 if (ret) {
526 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
527 return -EINVAL;
528 }
529
530 return 0;
531}
532
Rob Clark16ea9752013-01-08 15:04:28 -0600533static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300534 .destroy = tilcdc_crtc_destroy,
535 .set_config = drm_atomic_helper_set_config,
536 .page_flip = drm_atomic_helper_page_flip,
537 .reset = drm_atomic_helper_crtc_reset,
538 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
539 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600540};
541
542static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600543 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300544 .enable = tilcdc_crtc_enable,
545 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300546 .atomic_check = tilcdc_crtc_atomic_check,
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300547 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
Rob Clark16ea9752013-01-08 15:04:28 -0600548};
549
550int tilcdc_crtc_max_width(struct drm_crtc *crtc)
551{
552 struct drm_device *dev = crtc->dev;
553 struct tilcdc_drm_private *priv = dev->dev_private;
554 int max_width = 0;
555
556 if (priv->rev == 1)
557 max_width = 1024;
558 else if (priv->rev == 2)
559 max_width = 2048;
560
561 return max_width;
562}
563
564int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
565{
566 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
567 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500568 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600569
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500570 /*
571 * check to see if the width is within the range that
572 * the LCD Controller physically supports
573 */
Rob Clark16ea9752013-01-08 15:04:28 -0600574 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
575 return MODE_VIRTUAL_X;
576
577 /* width must be multiple of 16 */
578 if (mode->hdisplay & 0xf)
579 return MODE_VIRTUAL_X;
580
581 if (mode->vdisplay > 2048)
582 return MODE_VIRTUAL_Y;
583
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500584 DBG("Processing mode %dx%d@%d with pixel clock %d",
585 mode->hdisplay, mode->vdisplay,
586 drm_mode_vrefresh(mode), mode->clock);
587
588 hbp = mode->htotal - mode->hsync_end;
589 hfp = mode->hsync_start - mode->hdisplay;
590 hsw = mode->hsync_end - mode->hsync_start;
591 vbp = mode->vtotal - mode->vsync_end;
592 vfp = mode->vsync_start - mode->vdisplay;
593 vsw = mode->vsync_end - mode->vsync_start;
594
595 if ((hbp-1) & ~0x3ff) {
596 DBG("Pruning mode: Horizontal Back Porch out of range");
597 return MODE_HBLANK_WIDE;
598 }
599
600 if ((hfp-1) & ~0x3ff) {
601 DBG("Pruning mode: Horizontal Front Porch out of range");
602 return MODE_HBLANK_WIDE;
603 }
604
605 if ((hsw-1) & ~0x3ff) {
606 DBG("Pruning mode: Horizontal Sync Width out of range");
607 return MODE_HSYNC_WIDE;
608 }
609
610 if (vbp & ~0xff) {
611 DBG("Pruning mode: Vertical Back Porch out of range");
612 return MODE_VBLANK_WIDE;
613 }
614
615 if (vfp & ~0xff) {
616 DBG("Pruning mode: Vertical Front Porch out of range");
617 return MODE_VBLANK_WIDE;
618 }
619
620 if ((vsw-1) & ~0x3f) {
621 DBG("Pruning mode: Vertical Sync Width out of range");
622 return MODE_VSYNC_WIDE;
623 }
624
Darren Etheridge4e564342013-06-21 13:52:23 -0500625 /*
626 * some devices have a maximum allowed pixel clock
627 * configured from the DT
628 */
629 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500630 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500631 return MODE_CLOCK_HIGH;
632 }
633
634 /*
635 * some devices further limit the max horizontal resolution
636 * configured from the DT
637 */
638 if (mode->hdisplay > priv->max_width)
639 return MODE_BAD_WIDTH;
640
Rob Clark16ea9752013-01-08 15:04:28 -0600641 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500642 bandwidth = mode->hdisplay * mode->vdisplay *
643 drm_mode_vrefresh(mode);
644 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500645 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600646 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500647 }
Rob Clark16ea9752013-01-08 15:04:28 -0600648
649 return MODE_OK;
650}
651
652void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
653 const struct tilcdc_panel_info *info)
654{
655 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
656 tilcdc_crtc->info = info;
657}
658
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200659void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
660 bool simulate_vesa_sync)
661{
662 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
663
664 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
665}
666
Rob Clark16ea9752013-01-08 15:04:28 -0600667void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
668{
Rob Clark16ea9752013-01-08 15:04:28 -0600669 struct drm_device *dev = crtc->dev;
670 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha642e5162016-09-06 16:19:54 +0300671 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600672
Jyri Sarha642e5162016-09-06 16:19:54 +0300673 drm_modeset_lock_crtc(crtc, NULL);
674 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
675 if (tilcdc_crtc_is_on(crtc)) {
676 pm_runtime_get_sync(dev->dev);
677 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600678
Jyri Sarha642e5162016-09-06 16:19:54 +0300679 tilcdc_crtc_set_clk(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600680
Jyri Sarha642e5162016-09-06 16:19:54 +0300681 tilcdc_crtc_enable(crtc);
682 pm_runtime_put_sync(dev->dev);
683 }
Rob Clark16ea9752013-01-08 15:04:28 -0600684 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300685 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600686}
687
Jyri Sarha5895d082016-01-08 14:33:09 +0200688#define SYNC_LOST_COUNT_LIMIT 50
689
Rob Clark16ea9752013-01-08 15:04:28 -0600690irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
691{
692 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
693 struct drm_device *dev = crtc->dev;
694 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300695 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600696
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300697 stat = tilcdc_read_irqstatus(dev);
698 tilcdc_clear_irqstatus(dev, stat);
699
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300700 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600701 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200702 bool skip_event = false;
703 ktime_t now;
704
705 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600706
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300707 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600708
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200709 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600710
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200711 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600712
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200713 if (tilcdc_crtc->next_fb) {
714 set_scanout(crtc, tilcdc_crtc->next_fb);
715 tilcdc_crtc->next_fb = NULL;
716 skip_event = true;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300717 }
718
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200719 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
720
Gustavo Padovan099ede82016-07-04 21:04:52 -0300721 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200722
723 if (!skip_event) {
724 struct drm_pending_vblank_event *event;
725
726 spin_lock_irqsave(&dev->event_lock, flags);
727
728 event = tilcdc_crtc->event;
729 tilcdc_crtc->event = NULL;
730 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700731 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200732
733 spin_unlock_irqrestore(&dev->event_lock, flags);
734 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200735
736 if (tilcdc_crtc->frame_intact)
737 tilcdc_crtc->sync_lost_count = 0;
738 else
739 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600740 }
741
Jyri Sarha14944112016-04-07 20:36:48 +0300742 if (stat & LCDC_FIFO_UNDERFLOW)
743 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
744 __func__, stat);
745
746 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600747 if (priv->rev == 2) {
748 if (stat & LCDC_FRAME_DONE) {
749 tilcdc_crtc->frame_done = true;
750 wake_up(&tilcdc_crtc->frame_done_wq);
751 }
Rob Clark16ea9752013-01-08 15:04:28 -0600752
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300753 if (stat & LCDC_SYNC_LOST) {
754 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
755 __func__, stat);
756 tilcdc_crtc->frame_intact = false;
757 if (tilcdc_crtc->sync_lost_count++ >
758 SYNC_LOST_COUNT_LIMIT) {
759 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
760 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
761 LCDC_SYNC_LOST);
762 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200763 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200764
Jyri Sarha14944112016-04-07 20:36:48 +0300765 /* Indicate to LCDC that the interrupt service routine has
766 * completed, see 13.3.6.1.6 in AM335x TRM.
767 */
768 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
769 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200770
Rob Clark16ea9752013-01-08 15:04:28 -0600771 return IRQ_HANDLED;
772}
773
Rob Clark16ea9752013-01-08 15:04:28 -0600774struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
775{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300776 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600777 struct tilcdc_crtc *tilcdc_crtc;
778 struct drm_crtc *crtc;
779 int ret;
780
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200781 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600782 if (!tilcdc_crtc) {
783 dev_err(dev->dev, "allocation failed\n");
784 return NULL;
785 }
786
787 crtc = &tilcdc_crtc->base;
788
Jyri Sarha47f571c2016-04-07 15:04:18 +0300789 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
790 if (ret < 0)
791 goto fail;
792
Rob Clark16ea9752013-01-08 15:04:28 -0600793 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
794
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100795 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400796 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600797
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200798 spin_lock_init(&tilcdc_crtc->irq_lock);
799
Jyri Sarha47f571c2016-04-07 15:04:18 +0300800 ret = drm_crtc_init_with_planes(dev, crtc,
801 &tilcdc_crtc->primary,
802 NULL,
803 &tilcdc_crtc_funcs,
804 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -0600805 if (ret < 0)
806 goto fail;
807
808 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
809
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300810 if (priv->is_componentized) {
811 struct device_node *ports =
812 of_get_child_by_name(dev->dev->of_node, "ports");
813
814 if (ports) {
815 crtc->port = of_get_child_by_name(ports, "port");
816 of_node_put(ports);
817 } else {
818 crtc->port =
819 of_get_child_by_name(dev->dev->of_node, "port");
820 }
821 if (!crtc->port) { /* This should never happen */
822 dev_err(dev->dev, "Port node not found in %s\n",
823 dev->dev->of_node->full_name);
824 goto fail;
825 }
826 }
827
Rob Clark16ea9752013-01-08 15:04:28 -0600828 return crtc;
829
830fail:
831 tilcdc_crtc_destroy(crtc);
832 return NULL;
833}