blob: 3257228564d9e923fa13a8345d0e646bba2a1972 [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
24struct tilcdc_crtc {
25 struct drm_crtc base;
26
27 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060028 struct drm_pending_vblank_event *event;
29 int dpms;
30 wait_queue_head_t frame_done_wq;
31 bool frame_done;
32
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030033 struct drm_framebuffer *curr_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060034
35 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040036 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020037
38 /* Only set if an external encoder is connected */
39 bool simulate_vesa_sync;
Rob Clark16ea9752013-01-08 15:04:28 -060040};
41#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
42
Rob Clarka464d612013-08-07 13:41:20 -040043static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060044{
Darren Etheridgef7b45752013-06-21 13:52:26 -050045 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040046 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060047 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060048
49 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040050 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060051 mutex_unlock(&dev->mode_config.mutex);
52}
53
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030054static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060055{
56 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
57 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060058 struct drm_gem_cma_object *gem;
59 unsigned int depth, bpp;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030060 dma_addr_t start, end;
Rob Clark16ea9752013-01-08 15:04:28 -060061
62 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
63 gem = drm_fb_cma_get_gem_obj(fb, 0);
64
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030065 start = gem->paddr + fb->offsets[0] +
66 crtc->y * fb->pitches[0] +
67 crtc->x * bpp / 8;
Rob Clark16ea9752013-01-08 15:04:28 -060068
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030069 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060070
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030071 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, start);
72 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG, end);
73
74 if (tilcdc_crtc->curr_fb)
75 drm_flip_work_queue(&tilcdc_crtc->unref_work,
76 tilcdc_crtc->curr_fb);
77
78 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060079}
80
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030081static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -060082{
83 struct drm_device *dev = crtc->dev;
84 struct tilcdc_drm_private *priv = dev->dev_private;
85
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +030086 if (priv->rev != 2)
87 return;
88
89 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
90 usleep_range(250, 1000);
91 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
92}
93
94static void start(struct drm_crtc *crtc)
95{
96 struct drm_device *dev = crtc->dev;
97
98 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -060099
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300100 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600101 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
102 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
103}
104
105static void stop(struct drm_crtc *crtc)
106{
107 struct drm_device *dev = crtc->dev;
108
109 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
110}
111
112static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
113{
114 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
115
Jyri Sarhade9cb5f2015-02-26 10:12:41 +0200116 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -0600117
118 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400119 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
120
Rob Clark16ea9752013-01-08 15:04:28 -0600121 kfree(tilcdc_crtc);
122}
123
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000124static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
125{
126 struct drm_device *dev = crtc->dev;
127 unsigned int depth, bpp;
128
129 drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
130
131 if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
132 dev_err(dev->dev,
133 "Invalid pitch: fb and crtc widths must be the same");
134 return -EINVAL;
135 }
136
137 return 0;
138}
139
Rob Clark16ea9752013-01-08 15:04:28 -0600140static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
141 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -0700142 struct drm_pending_vblank_event *event,
143 uint32_t page_flip_flags)
Rob Clark16ea9752013-01-08 15:04:28 -0600144{
145 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
146 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000147 int r;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300148 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000149
150 r = tilcdc_verify_fb(crtc, fb);
151 if (r)
152 return r;
Rob Clark16ea9752013-01-08 15:04:28 -0600153
154 if (tilcdc_crtc->event) {
155 dev_err(dev->dev, "already pending page flip!\n");
156 return -EBUSY;
157 }
158
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300159 drm_framebuffer_reference(fb);
160
Matt Roperf4510a22014-04-01 15:22:40 -0700161 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300162
163 pm_runtime_get_sync(dev->dev);
164
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300165
166 set_scanout(crtc, fb);
167
168 spin_lock_irqsave(&dev->event_lock, flags);
169 tilcdc_crtc->event = event;
170 spin_unlock_irqrestore(&dev->event_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600171
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300172 pm_runtime_put_sync(dev->dev);
173
Rob Clark16ea9752013-01-08 15:04:28 -0600174 return 0;
175}
176
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000177void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
Rob Clark16ea9752013-01-08 15:04:28 -0600178{
179 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
180 struct drm_device *dev = crtc->dev;
181 struct tilcdc_drm_private *priv = dev->dev_private;
182
183 /* we really only care about on or off: */
184 if (mode != DRM_MODE_DPMS_ON)
185 mode = DRM_MODE_DPMS_OFF;
186
187 if (tilcdc_crtc->dpms == mode)
188 return;
189
190 tilcdc_crtc->dpms = mode;
191
Rob Clark16ea9752013-01-08 15:04:28 -0600192 if (mode == DRM_MODE_DPMS_ON) {
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300193 pm_runtime_get_sync(dev->dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600194 start(crtc);
195 } else {
196 tilcdc_crtc->frame_done = false;
197 stop(crtc);
198
Darren Etheridgef7b45752013-06-21 13:52:26 -0500199 /*
200 * if necessary wait for framedone irq which will still come
Rob Clark16ea9752013-01-08 15:04:28 -0600201 * before putting things to sleep..
202 */
203 if (priv->rev == 2) {
204 int ret = wait_event_timeout(
205 tilcdc_crtc->frame_done_wq,
206 tilcdc_crtc->frame_done,
207 msecs_to_jiffies(50));
208 if (ret == 0)
209 dev_err(dev->dev, "timeout waiting for framedone\n");
210 }
Rob Clark16ea9752013-01-08 15:04:28 -0600211
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300212 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300213
214 if (tilcdc_crtc->curr_fb) {
215 drm_flip_work_queue(&tilcdc_crtc->unref_work,
216 tilcdc_crtc->curr_fb);
217 tilcdc_crtc->curr_fb = NULL;
218 }
219
220 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300221 }
Rob Clark16ea9752013-01-08 15:04:28 -0600222}
223
224static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
225 const struct drm_display_mode *mode,
226 struct drm_display_mode *adjusted_mode)
227{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200228 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
229
230 if (!tilcdc_crtc->simulate_vesa_sync)
231 return true;
232
233 /*
234 * tilcdc does not generate VESA-compliant sync but aligns
235 * VS on the second edge of HS instead of first edge.
236 * We use adjusted_mode, to fixup sync by aligning both rising
237 * edges and add HSKEW offset to fix the sync.
238 */
239 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
240 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
241
242 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
243 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
244 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
245 } else {
246 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
247 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
248 }
249
Rob Clark16ea9752013-01-08 15:04:28 -0600250 return true;
251}
252
253static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
254{
255 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
256}
257
258static void tilcdc_crtc_commit(struct drm_crtc *crtc)
259{
260 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
261}
262
263static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
264 struct drm_display_mode *mode,
265 struct drm_display_mode *adjusted_mode,
266 int x, int y,
267 struct drm_framebuffer *old_fb)
268{
269 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
270 struct drm_device *dev = crtc->dev;
271 struct tilcdc_drm_private *priv = dev->dev_private;
272 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
273 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
274 int ret;
275
276 ret = tilcdc_crtc_mode_valid(crtc, mode);
277 if (WARN_ON(ret))
278 return ret;
279
280 if (WARN_ON(!info))
281 return -EINVAL;
282
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000283 ret = tilcdc_verify_fb(crtc, crtc->primary->fb);
284 if (ret)
285 return ret;
286
Rob Clark16ea9752013-01-08 15:04:28 -0600287 pm_runtime_get_sync(dev->dev);
288
289 /* Configure the Burst Size and fifo threshold of DMA: */
290 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
291 switch (info->dma_burst_sz) {
292 case 1:
293 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
294 break;
295 case 2:
296 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
297 break;
298 case 4:
299 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
300 break;
301 case 8:
302 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
303 break;
304 case 16:
305 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
306 break;
307 default:
308 return -EINVAL;
309 }
310 reg |= (info->fifo_th << 8);
311 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
312
313 /* Configure timings: */
314 hbp = mode->htotal - mode->hsync_end;
315 hfp = mode->hsync_start - mode->hdisplay;
316 hsw = mode->hsync_end - mode->hsync_start;
317 vbp = mode->vtotal - mode->vsync_end;
318 vfp = mode->vsync_start - mode->vdisplay;
319 vsw = mode->vsync_end - mode->vsync_start;
320
321 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
322 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
323
324 /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
325 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
326 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
327 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500328
329 /*
330 * subtract one from hfp, hbp, hsw because the hardware uses
331 * a value of 0 as 1
332 */
Rob Clark16ea9752013-01-08 15:04:28 -0600333 if (priv->rev == 2) {
Pantelis Antoniouc19b3e22013-06-21 13:52:28 -0500334 /* clear bits we're going to set */
335 reg &= ~0x78000033;
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500336 reg |= ((hfp-1) & 0x300) >> 8;
337 reg |= ((hbp-1) & 0x300) >> 4;
338 reg |= ((hsw-1) & 0x3c0) << 21;
Rob Clark16ea9752013-01-08 15:04:28 -0600339 }
340 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
341
342 reg = (((mode->hdisplay >> 4) - 1) << 4) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500343 (((hbp-1) & 0xff) << 24) |
344 (((hfp-1) & 0xff) << 16) |
345 (((hsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600346 if (priv->rev == 2)
347 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
348 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
349
350 reg = ((mode->vdisplay - 1) & 0x3ff) |
351 ((vbp & 0xff) << 24) |
352 ((vfp & 0xff) << 16) |
Darren Etheridgedb2b4bd2013-06-21 13:52:24 -0500353 (((vsw-1) & 0x3f) << 10);
Rob Clark16ea9752013-01-08 15:04:28 -0600354 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
355
Darren Etheridge6bf02c62013-06-21 13:52:22 -0500356 /*
357 * be sure to set Bit 10 for the V2 LCDC controller,
358 * otherwise limited to 1024 pixels width, stopping
359 * 1920x1080 being suppoted.
360 */
361 if (priv->rev == 2) {
362 if ((mode->vdisplay - 1) & 0x400) {
363 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
364 LCDC_LPP_B10);
365 } else {
366 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
367 LCDC_LPP_B10);
368 }
369 }
370
Rob Clark16ea9752013-01-08 15:04:28 -0600371 /* Configure display type: */
372 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
373 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
374 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
375 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
376 if (info->tft_alt_mode)
377 reg |= LCDC_TFT_ALT_ENABLE;
378 if (priv->rev == 2) {
379 unsigned int depth, bpp;
380
Matt Roperf4510a22014-04-01 15:22:40 -0700381 drm_fb_get_bpp_depth(crtc->primary->fb->pixel_format, &depth, &bpp);
Rob Clark16ea9752013-01-08 15:04:28 -0600382 switch (bpp) {
383 case 16:
384 break;
385 case 32:
386 reg |= LCDC_V2_TFT_24BPP_UNPACK;
387 /* fallthrough */
388 case 24:
389 reg |= LCDC_V2_TFT_24BPP_MODE;
390 break;
391 default:
392 dev_err(dev->dev, "invalid pixel format\n");
393 return -EINVAL;
394 }
395 }
396 reg |= info->fdd < 12;
397 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
398
399 if (info->invert_pxl_clk)
400 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
401 else
402 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
403
404 if (info->sync_ctrl)
405 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
406 else
407 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
408
409 if (info->sync_edge)
410 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
411 else
412 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
413
Darren Etheridgea9767182013-08-14 21:43:33 +0200414 /*
415 * use value from adjusted_mode here as this might have been
416 * changed as part of the fixup for slave encoders to solve the
417 * issue where tilcdc timings are not VESA compliant
418 */
419 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Rob Clark16ea9752013-01-08 15:04:28 -0600420 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
421 else
422 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
423
424 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
425 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
426 else
427 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
428
429 if (info->raster_order)
430 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
431 else
432 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
433
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300434 drm_framebuffer_reference(crtc->primary->fb);
Rob Clark16ea9752013-01-08 15:04:28 -0600435
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300436 set_scanout(crtc, crtc->primary->fb);
437
Rob Clark16ea9752013-01-08 15:04:28 -0600438 tilcdc_crtc_update_clk(crtc);
439
440 pm_runtime_put_sync(dev->dev);
441
442 return 0;
443}
444
445static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
446 struct drm_framebuffer *old_fb)
447{
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300448 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000449 int r;
450
451 r = tilcdc_verify_fb(crtc, crtc->primary->fb);
452 if (r)
453 return r;
454
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300455 drm_framebuffer_reference(crtc->primary->fb);
456
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300457 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300458
459 set_scanout(crtc, crtc->primary->fb);
460
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300461 pm_runtime_put_sync(dev->dev);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300462
Rob Clark16ea9752013-01-08 15:04:28 -0600463 return 0;
464}
465
Rob Clark16ea9752013-01-08 15:04:28 -0600466static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
467 .destroy = tilcdc_crtc_destroy,
468 .set_config = drm_crtc_helper_set_config,
469 .page_flip = tilcdc_crtc_page_flip,
470};
471
472static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
473 .dpms = tilcdc_crtc_dpms,
474 .mode_fixup = tilcdc_crtc_mode_fixup,
475 .prepare = tilcdc_crtc_prepare,
476 .commit = tilcdc_crtc_commit,
477 .mode_set = tilcdc_crtc_mode_set,
478 .mode_set_base = tilcdc_crtc_mode_set_base,
Rob Clark16ea9752013-01-08 15:04:28 -0600479};
480
481int tilcdc_crtc_max_width(struct drm_crtc *crtc)
482{
483 struct drm_device *dev = crtc->dev;
484 struct tilcdc_drm_private *priv = dev->dev_private;
485 int max_width = 0;
486
487 if (priv->rev == 1)
488 max_width = 1024;
489 else if (priv->rev == 2)
490 max_width = 2048;
491
492 return max_width;
493}
494
495int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
496{
497 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
498 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500499 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600500
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500501 /*
502 * check to see if the width is within the range that
503 * the LCD Controller physically supports
504 */
Rob Clark16ea9752013-01-08 15:04:28 -0600505 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
506 return MODE_VIRTUAL_X;
507
508 /* width must be multiple of 16 */
509 if (mode->hdisplay & 0xf)
510 return MODE_VIRTUAL_X;
511
512 if (mode->vdisplay > 2048)
513 return MODE_VIRTUAL_Y;
514
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500515 DBG("Processing mode %dx%d@%d with pixel clock %d",
516 mode->hdisplay, mode->vdisplay,
517 drm_mode_vrefresh(mode), mode->clock);
518
519 hbp = mode->htotal - mode->hsync_end;
520 hfp = mode->hsync_start - mode->hdisplay;
521 hsw = mode->hsync_end - mode->hsync_start;
522 vbp = mode->vtotal - mode->vsync_end;
523 vfp = mode->vsync_start - mode->vdisplay;
524 vsw = mode->vsync_end - mode->vsync_start;
525
526 if ((hbp-1) & ~0x3ff) {
527 DBG("Pruning mode: Horizontal Back Porch out of range");
528 return MODE_HBLANK_WIDE;
529 }
530
531 if ((hfp-1) & ~0x3ff) {
532 DBG("Pruning mode: Horizontal Front Porch out of range");
533 return MODE_HBLANK_WIDE;
534 }
535
536 if ((hsw-1) & ~0x3ff) {
537 DBG("Pruning mode: Horizontal Sync Width out of range");
538 return MODE_HSYNC_WIDE;
539 }
540
541 if (vbp & ~0xff) {
542 DBG("Pruning mode: Vertical Back Porch out of range");
543 return MODE_VBLANK_WIDE;
544 }
545
546 if (vfp & ~0xff) {
547 DBG("Pruning mode: Vertical Front Porch out of range");
548 return MODE_VBLANK_WIDE;
549 }
550
551 if ((vsw-1) & ~0x3f) {
552 DBG("Pruning mode: Vertical Sync Width out of range");
553 return MODE_VSYNC_WIDE;
554 }
555
Darren Etheridge4e564342013-06-21 13:52:23 -0500556 /*
557 * some devices have a maximum allowed pixel clock
558 * configured from the DT
559 */
560 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500561 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500562 return MODE_CLOCK_HIGH;
563 }
564
565 /*
566 * some devices further limit the max horizontal resolution
567 * configured from the DT
568 */
569 if (mode->hdisplay > priv->max_width)
570 return MODE_BAD_WIDTH;
571
Rob Clark16ea9752013-01-08 15:04:28 -0600572 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500573 bandwidth = mode->hdisplay * mode->vdisplay *
574 drm_mode_vrefresh(mode);
575 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500576 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600577 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500578 }
Rob Clark16ea9752013-01-08 15:04:28 -0600579
580 return MODE_OK;
581}
582
583void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
584 const struct tilcdc_panel_info *info)
585{
586 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
587 tilcdc_crtc->info = info;
588}
589
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200590void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
591 bool simulate_vesa_sync)
592{
593 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
594
595 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
596}
597
Rob Clark16ea9752013-01-08 15:04:28 -0600598void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
599{
600 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
601 struct drm_device *dev = crtc->dev;
602 struct tilcdc_drm_private *priv = dev->dev_private;
603 int dpms = tilcdc_crtc->dpms;
Darren Etheridge3d193062014-01-15 15:52:36 -0600604 unsigned long lcd_clk;
605 const unsigned clkdiv = 2; /* using a fixed divider of 2 */
Rob Clark16ea9752013-01-08 15:04:28 -0600606 int ret;
607
608 pm_runtime_get_sync(dev->dev);
609
610 if (dpms == DRM_MODE_DPMS_ON)
611 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
612
Darren Etheridge3d193062014-01-15 15:52:36 -0600613 /* mode.clock is in KHz, set_rate wants parameter in Hz */
614 ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
615 if (ret < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600616 dev_err(dev->dev, "failed to set display clock rate to: %d\n",
617 crtc->mode.clock);
618 goto out;
619 }
620
621 lcd_clk = clk_get_rate(priv->clk);
Rob Clark16ea9752013-01-08 15:04:28 -0600622
Darren Etheridge3d193062014-01-15 15:52:36 -0600623 DBG("lcd_clk=%lu, mode clock=%d, div=%u",
624 lcd_clk, crtc->mode.clock, clkdiv);
Rob Clark16ea9752013-01-08 15:04:28 -0600625
626 /* Configure the LCD clock divisor. */
Darren Etheridge3d193062014-01-15 15:52:36 -0600627 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
Rob Clark16ea9752013-01-08 15:04:28 -0600628 LCDC_RASTER_MODE);
629
630 if (priv->rev == 2)
631 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
632 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
633 LCDC_V2_CORE_CLK_EN);
634
635 if (dpms == DRM_MODE_DPMS_ON)
636 tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
637
638out:
639 pm_runtime_put_sync(dev->dev);
640}
641
642irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
643{
644 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
645 struct drm_device *dev = crtc->dev;
646 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300647 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600648
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300649 stat = tilcdc_read_irqstatus(dev);
650 tilcdc_clear_irqstatus(dev, stat);
651
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300652 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600653 unsigned long flags;
Rob Clark16ea9752013-01-08 15:04:28 -0600654
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300655 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600656
657 drm_handle_vblank(dev, 0);
658
659 spin_lock_irqsave(&dev->event_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600660
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300661 if (tilcdc_crtc->event) {
662 drm_send_vblank_event(dev, 0, tilcdc_crtc->event);
663 tilcdc_crtc->event = NULL;
664 }
665
666 spin_unlock_irqrestore(&dev->event_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600667 }
668
669 if (priv->rev == 2) {
670 if (stat & LCDC_FRAME_DONE) {
671 tilcdc_crtc->frame_done = true;
672 wake_up(&tilcdc_crtc->frame_done_wq);
673 }
674 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
675 }
676
677 return IRQ_HANDLED;
678}
679
Rob Clark16ea9752013-01-08 15:04:28 -0600680struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
681{
682 struct tilcdc_crtc *tilcdc_crtc;
683 struct drm_crtc *crtc;
684 int ret;
685
686 tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
687 if (!tilcdc_crtc) {
688 dev_err(dev->dev, "allocation failed\n");
689 return NULL;
690 }
691
692 crtc = &tilcdc_crtc->base;
693
694 tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
695 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
696
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100697 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400698 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600699
Rob Clark16ea9752013-01-08 15:04:28 -0600700 ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
701 if (ret < 0)
702 goto fail;
703
704 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
705
706 return crtc;
707
708fail:
709 tilcdc_crtc_destroy(crtc);
710 return NULL;
711}