blob: 6c5892763d27f31b4d5288d115e615a04b166ede [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
Sean Paulce2f2c32016-09-21 06:14:53 -070018#include <drm/drm_atomic.h>
Jyri Sarha305198d2016-04-07 15:05:16 +030019#include <drm/drm_atomic_helper.h>
Sean Paulce2f2c32016-09-21 06:14:53 -070020#include <drm/drm_crtc.h>
21#include <drm/drm_flip_work.h>
22#include <drm/drm_plane_helper.h>
Jyri Sarha4e910c72016-09-06 22:55:33 +030023#include <linux/workqueue.h>
Bartosz Golaszewski93452352016-10-31 15:19:26 +010024#include <linux/completion.h>
25#include <linux/dma-mapping.h>
Rob Herring86418f92017-03-22 08:26:06 -050026#include <linux/of_graph.h>
Rob Clark16ea9752013-01-08 15:04:28 -060027
28#include "tilcdc_drv.h"
29#include "tilcdc_regs.h"
30
Bartosz Golaszewski93452352016-10-31 15:19:26 +010031#define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
Jyri Sarha55e165c2016-11-15 23:37:24 +020032#define TILCDC_PALETTE_SIZE 32
33#define TILCDC_PALETTE_FIRST_ENTRY 0x4000
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020034
Rob Clark16ea9752013-01-08 15:04:28 -060035struct tilcdc_crtc {
36 struct drm_crtc base;
37
Jyri Sarha47f571c2016-04-07 15:04:18 +030038 struct drm_plane primary;
Rob Clark16ea9752013-01-08 15:04:28 -060039 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060040 struct drm_pending_vblank_event *event;
Jyri Sarha2d53a182016-10-25 12:27:31 +030041 struct mutex enable_lock;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +030042 bool enabled;
Jyri Sarha2d53a182016-10-25 12:27:31 +030043 bool shutdown;
Rob Clark16ea9752013-01-08 15:04:28 -060044 wait_queue_head_t frame_done_wq;
45 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020046 spinlock_t irq_lock;
47
Jyri Sarha642e5162016-09-06 16:19:54 +030048 unsigned int lcd_fck_rate;
49
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020050 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060051
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030052 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020053 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060054
55 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040056 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020057
58 /* Only set if an external encoder is connected */
59 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020060
61 int sync_lost_count;
62 bool frame_intact;
Jyri Sarha13b3d722016-04-06 14:02:38 +030063 struct work_struct recover_work;
Bartosz Golaszewski93452352016-10-31 15:19:26 +010064
65 dma_addr_t palette_dma_handle;
Jyri Sarha55e165c2016-11-15 23:37:24 +020066 u16 *palette_base;
Bartosz Golaszewski93452352016-10-31 15:19:26 +010067 struct completion palette_loaded;
Rob Clark16ea9752013-01-08 15:04:28 -060068};
69#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
70
Rob Clarka464d612013-08-07 13:41:20 -040071static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060072{
Darren Etheridgef7b45752013-06-21 13:52:26 -050073 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040074 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060075 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060076
77 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040078 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060079 mutex_unlock(&dev->mode_config.mutex);
80}
81
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030082static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060083{
84 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
85 struct drm_device *dev = crtc->dev;
Daniel Schultz4c268d62016-10-28 13:52:41 +020086 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -060087 struct drm_gem_cma_object *gem;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030088 dma_addr_t start, end;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030089 u64 dma_base_and_ceiling;
Rob Clark16ea9752013-01-08 15:04:28 -060090
Rob Clark16ea9752013-01-08 15:04:28 -060091 gem = drm_fb_cma_get_gem_obj(fb, 0);
92
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030093 start = gem->paddr + fb->offsets[0] +
94 crtc->y * fb->pitches[0] +
Ville Syrjälä353c8592016-12-14 23:30:57 +020095 crtc->x * fb->format->cpp[0];
Rob Clark16ea9752013-01-08 15:04:28 -060096
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030097 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060098
Jyri Sarha7eb9f062016-08-26 15:10:14 +030099 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
100 * with a single insruction, if available. This should make it more
101 * unlikely that LCDC would fetch the DMA addresses in the middle of
102 * an update.
103 */
Daniel Schultz4c268d62016-10-28 13:52:41 +0200104 if (priv->rev == 1)
105 end -= 1;
106
107 dma_base_and_ceiling = (u64)end << 32 | start;
Jyri Sarha7eb9f062016-08-26 15:10:14 +0300108 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300109
110 if (tilcdc_crtc->curr_fb)
111 drm_flip_work_queue(&tilcdc_crtc->unref_work,
112 tilcdc_crtc->curr_fb);
113
114 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600115}
116
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100117/*
Jyri Sarha55e165c2016-11-15 23:37:24 +0200118 * The driver currently only supports only true color formats. For
119 * true color the palette block is bypassed, but a 32 byte palette
120 * should still be loaded. The first 16-bit entry must be 0x4000 while
121 * all other entries must be zeroed.
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100122 */
123static void tilcdc_crtc_load_palette(struct drm_crtc *crtc)
124{
Jyri Sarha55e165c2016-11-15 23:37:24 +0200125 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
126 struct drm_device *dev = crtc->dev;
127 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarhae59f5af2016-11-17 18:46:16 +0200128 int ret;
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100129
Jyri Sarha274c34d2016-11-15 23:57:42 +0200130 reinit_completion(&tilcdc_crtc->palette_loaded);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100131
132 /* Tell the LCDC where the palette is located. */
133 tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG,
134 tilcdc_crtc->palette_dma_handle);
135 tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG,
Jyri Sarha55e165c2016-11-15 23:37:24 +0200136 (u32) tilcdc_crtc->palette_dma_handle +
137 TILCDC_PALETTE_SIZE - 1);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100138
Jyri Sarha55e165c2016-11-15 23:37:24 +0200139 /* Set dma load mode for palette loading only. */
140 tilcdc_write_mask(dev, LCDC_RASTER_CTRL_REG,
141 LCDC_PALETTE_LOAD_MODE(PALETTE_ONLY),
142 LCDC_PALETTE_LOAD_MODE_MASK);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100143
Jyri Sarha55e165c2016-11-15 23:37:24 +0200144 /* Enable DMA Palette Loaded Interrupt */
145 if (priv->rev == 1)
146 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
147 else
148 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG, LCDC_V2_PL_INT_ENA);
149
150 /* Enable LCDC DMA and wait for palette to be loaded. */
151 tilcdc_clear_irqstatus(dev, 0xffffffff);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100152 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
153
Jyri Sarhae59f5af2016-11-17 18:46:16 +0200154 ret = wait_for_completion_timeout(&tilcdc_crtc->palette_loaded,
155 msecs_to_jiffies(50));
156 if (ret == 0)
157 dev_err(dev->dev, "%s: Palette loading timeout", __func__);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100158
Jyri Sarha55e165c2016-11-15 23:37:24 +0200159 /* Disable LCDC DMA and DMA Palette Loaded Interrupt. */
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100160 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha55e165c2016-11-15 23:37:24 +0200161 if (priv->rev == 1)
162 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
163 else
164 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG, LCDC_V2_PL_INT_ENA);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100165}
166
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300167static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
168{
169 struct tilcdc_drm_private *priv = dev->dev_private;
170
171 tilcdc_clear_irqstatus(dev, 0xffffffff);
172
173 if (priv->rev == 1) {
174 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
Jyri Sarha36725832016-11-21 18:30:19 +0200175 LCDC_V1_SYNC_LOST_INT_ENA | LCDC_V1_FRAME_DONE_INT_ENA |
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300176 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000177 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
178 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300179 } else {
180 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
181 LCDC_V2_UNDERFLOW_INT_ENA |
182 LCDC_V2_END_OF_FRAME0_INT_ENA |
183 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
184 }
185}
186
187static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
188{
189 struct tilcdc_drm_private *priv = dev->dev_private;
190
191 /* disable irqs that we might have enabled: */
192 if (priv->rev == 1) {
193 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
Jyri Sarha36725832016-11-21 18:30:19 +0200194 LCDC_V1_SYNC_LOST_INT_ENA | LCDC_V1_FRAME_DONE_INT_ENA |
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300195 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
196 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
197 LCDC_V1_END_OF_FRAME_INT_ENA);
198 } else {
199 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
200 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
201 LCDC_V2_END_OF_FRAME0_INT_ENA |
202 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
203 }
204}
205
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300206static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600207{
208 struct drm_device *dev = crtc->dev;
209 struct tilcdc_drm_private *priv = dev->dev_private;
210
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300211 if (priv->rev != 2)
212 return;
213
214 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
215 usleep_range(250, 1000);
216 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
217}
218
Jyri Sarha75d7f272016-11-24 23:25:08 +0200219/*
220 * Calculate the percentage difference between the requested pixel clock rate
221 * and the effective rate resulting from calculating the clock divider value.
222 */
223static unsigned int tilcdc_pclk_diff(unsigned long rate,
224 unsigned long real_rate)
225{
226 int r = rate / 100, rr = real_rate / 100;
227
228 return (unsigned int)(abs(((rr - r) * 100) / r));
229}
230
231static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
232{
233 struct drm_device *dev = crtc->dev;
234 struct tilcdc_drm_private *priv = dev->dev_private;
235 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
236 unsigned long clk_rate, real_rate, req_rate;
237 unsigned int clkdiv;
238 int ret;
239
240 clkdiv = 2; /* first try using a standard divider of 2 */
241
242 /* mode.clock is in KHz, set_rate wants parameter in Hz */
243 req_rate = crtc->mode.clock * 1000;
244
245 ret = clk_set_rate(priv->clk, req_rate * clkdiv);
246 clk_rate = clk_get_rate(priv->clk);
247 if (ret < 0) {
248 /*
249 * If we fail to set the clock rate (some architectures don't
250 * use the common clock framework yet and may not implement
251 * all the clk API calls for every clock), try the next best
252 * thing: adjusting the clock divider, unless clk_get_rate()
253 * failed as well.
254 */
255 if (!clk_rate) {
256 /* Nothing more we can do. Just bail out. */
257 dev_err(dev->dev,
258 "failed to set the pixel clock - unable to read current lcdc clock rate\n");
259 return;
260 }
261
262 clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);
263
264 /*
265 * Emit a warning if the real clock rate resulting from the
266 * calculated divider differs much from the requested rate.
267 *
268 * 5% is an arbitrary value - LCDs are usually quite tolerant
269 * about pixel clock rates.
270 */
271 real_rate = clkdiv * req_rate;
272
273 if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
274 dev_warn(dev->dev,
275 "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
276 clk_rate, real_rate);
277 }
278 }
279
280 tilcdc_crtc->lcd_fck_rate = clk_rate;
281
282 DBG("lcd_clk=%u, mode clock=%d, div=%u",
283 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
284
285 /* Configure the LCD clock divisor. */
286 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
287 LCDC_RASTER_MODE);
288
289 if (priv->rev == 2)
290 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
291 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
292 LCDC_V2_CORE_CLK_EN);
293}
294
295static void tilcdc_crtc_set_mode(struct drm_crtc *crtc)
296{
297 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
298 struct drm_device *dev = crtc->dev;
299 struct tilcdc_drm_private *priv = dev->dev_private;
300 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
301 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
302 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
303 struct drm_framebuffer *fb = crtc->primary->state->fb;
304
305 if (WARN_ON(!info))
306 return;
307
308 if (WARN_ON(!fb))
309 return;
310
311 /* Configure the Burst Size and fifo threshold of DMA: */
312 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
313 switch (info->dma_burst_sz) {
314 case 1:
315 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
316 break;
317 case 2:
318 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
319 break;
320 case 4:
321 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
322 break;
323 case 8:
324 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
325 break;
326 case 16:
327 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
328 break;
329 default:
330 dev_err(dev->dev, "invalid burst size\n");
331 return;
332 }
333 reg |= (info->fifo_th << 8);
334 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
335
336 /* Configure timings: */
337 hbp = mode->htotal - mode->hsync_end;
338 hfp = mode->hsync_start - mode->hdisplay;
339 hsw = mode->hsync_end - mode->hsync_start;
340 vbp = mode->vtotal - mode->vsync_end;
341 vfp = mode->vsync_start - mode->vdisplay;
342 vsw = mode->vsync_end - mode->vsync_start;
343
344 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
345 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
346
347 /* Set AC Bias Period and Number of Transitions per Interrupt: */
348 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
349 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
350 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
351
352 /*
353 * subtract one from hfp, hbp, hsw because the hardware uses
354 * a value of 0 as 1
355 */
356 if (priv->rev == 2) {
357 /* clear bits we're going to set */
358 reg &= ~0x78000033;
359 reg |= ((hfp-1) & 0x300) >> 8;
360 reg |= ((hbp-1) & 0x300) >> 4;
361 reg |= ((hsw-1) & 0x3c0) << 21;
362 }
363 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
364
365 reg = (((mode->hdisplay >> 4) - 1) << 4) |
366 (((hbp-1) & 0xff) << 24) |
367 (((hfp-1) & 0xff) << 16) |
368 (((hsw-1) & 0x3f) << 10);
369 if (priv->rev == 2)
370 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
371 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
372
373 reg = ((mode->vdisplay - 1) & 0x3ff) |
374 ((vbp & 0xff) << 24) |
375 ((vfp & 0xff) << 16) |
376 (((vsw-1) & 0x3f) << 10);
377 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
378
379 /*
380 * be sure to set Bit 10 for the V2 LCDC controller,
381 * otherwise limited to 1024 pixels width, stopping
382 * 1920x1080 being supported.
383 */
384 if (priv->rev == 2) {
385 if ((mode->vdisplay - 1) & 0x400) {
386 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
387 LCDC_LPP_B10);
388 } else {
389 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
390 LCDC_LPP_B10);
391 }
392 }
393
394 /* Configure display type: */
395 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
396 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
397 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
398 0x000ff000 /* Palette Loading Delay bits */);
399 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
400 if (info->tft_alt_mode)
401 reg |= LCDC_TFT_ALT_ENABLE;
402 if (priv->rev == 2) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200403 switch (fb->format->format) {
Jyri Sarha75d7f272016-11-24 23:25:08 +0200404 case DRM_FORMAT_BGR565:
405 case DRM_FORMAT_RGB565:
406 break;
407 case DRM_FORMAT_XBGR8888:
408 case DRM_FORMAT_XRGB8888:
409 reg |= LCDC_V2_TFT_24BPP_UNPACK;
410 /* fallthrough */
411 case DRM_FORMAT_BGR888:
412 case DRM_FORMAT_RGB888:
413 reg |= LCDC_V2_TFT_24BPP_MODE;
414 break;
415 default:
416 dev_err(dev->dev, "invalid pixel format\n");
417 return;
418 }
419 }
420 reg |= info->fdd < 12;
421 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
422
423 if (info->invert_pxl_clk)
424 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
425 else
426 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
427
428 if (info->sync_ctrl)
429 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
430 else
431 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
432
433 if (info->sync_edge)
434 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
435 else
436 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
437
438 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
439 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
440 else
441 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
442
443 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
444 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
445 else
446 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
447
448 if (info->raster_order)
449 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
450 else
451 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
452
453 tilcdc_crtc_set_clk(crtc);
454
455 tilcdc_crtc_load_palette(crtc);
456
457 set_scanout(crtc, fb);
458
459 drm_framebuffer_reference(fb);
460
461 crtc->hwmode = crtc->state->adjusted_mode;
462}
463
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300464static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300465{
466 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300467 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Jyri Sarha11abbc92017-03-01 10:30:28 +0200468 unsigned long flags;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300469
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300470 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Jyri Sarha2d53a182016-10-25 12:27:31 +0300471 mutex_lock(&tilcdc_crtc->enable_lock);
472 if (tilcdc_crtc->enabled || tilcdc_crtc->shutdown) {
473 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300474 return;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300475 }
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300476
477 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300478
479 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600480
Jyri Sarha75d7f272016-11-24 23:25:08 +0200481 tilcdc_crtc_set_mode(crtc);
482
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300483 tilcdc_crtc_enable_irqs(dev);
484
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300485 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Jyri Sarhaf13e0882016-11-19 18:00:32 +0200486 tilcdc_write_mask(dev, LCDC_RASTER_CTRL_REG,
487 LCDC_PALETTE_LOAD_MODE(DATA_ONLY),
488 LCDC_PALETTE_LOAD_MODE_MASK);
Jyri Sarha11abbc92017-03-01 10:30:28 +0200489
490 /* There is no real chance for a race here as the time stamp
491 * is taken before the raster DMA is started. The spin-lock is
492 * taken to have a memory barrier after taking the time-stamp
493 * and to avoid a context switch between taking the stamp and
494 * enabling the raster.
495 */
496 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
497 tilcdc_crtc->last_vblank = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600498 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha11abbc92017-03-01 10:30:28 +0200499 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300500
501 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300502
503 tilcdc_crtc->enabled = true;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300504 mutex_unlock(&tilcdc_crtc->enable_lock);
Rob Clark16ea9752013-01-08 15:04:28 -0600505}
506
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300507static void tilcdc_crtc_atomic_enable(struct drm_crtc *crtc,
508 struct drm_crtc_state *old_state)
509{
510 tilcdc_crtc_enable(crtc);
511}
512
Jyri Sarha2d53a182016-10-25 12:27:31 +0300513static void tilcdc_crtc_off(struct drm_crtc *crtc, bool shutdown)
Rob Clark16ea9752013-01-08 15:04:28 -0600514{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300515 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600516 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300517 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha75d7f272016-11-24 23:25:08 +0200518 int ret;
Rob Clark16ea9752013-01-08 15:04:28 -0600519
Jyri Sarha2d53a182016-10-25 12:27:31 +0300520 mutex_lock(&tilcdc_crtc->enable_lock);
521 if (shutdown)
522 tilcdc_crtc->shutdown = true;
523 if (!tilcdc_crtc->enabled) {
524 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300525 return;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300526 }
Jyri Sarha2d5be882016-04-07 20:20:23 +0300527 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600528 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300529
530 /*
Jyri Sarha75d7f272016-11-24 23:25:08 +0200531 * Wait for framedone irq which will still come before putting
532 * things to sleep..
Jyri Sarha2d5be882016-04-07 20:20:23 +0300533 */
Jyri Sarha75d7f272016-11-24 23:25:08 +0200534 ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
535 tilcdc_crtc->frame_done,
536 msecs_to_jiffies(500));
537 if (ret == 0)
538 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
539 __func__);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300540
541 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300542
543 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300544
545 pm_runtime_put_sync(dev->dev);
546
547 if (tilcdc_crtc->next_fb) {
548 drm_flip_work_queue(&tilcdc_crtc->unref_work,
549 tilcdc_crtc->next_fb);
550 tilcdc_crtc->next_fb = NULL;
551 }
552
553 if (tilcdc_crtc->curr_fb) {
554 drm_flip_work_queue(&tilcdc_crtc->unref_work,
555 tilcdc_crtc->curr_fb);
556 tilcdc_crtc->curr_fb = NULL;
557 }
558
559 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300560
561 tilcdc_crtc->enabled = false;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300562 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300563}
564
Jyri Sarha9e79e062016-10-18 23:23:27 +0300565static void tilcdc_crtc_disable(struct drm_crtc *crtc)
566{
567 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Jyri Sarha2d53a182016-10-25 12:27:31 +0300568 tilcdc_crtc_off(crtc, false);
569}
570
571void tilcdc_crtc_shutdown(struct drm_crtc *crtc)
572{
573 tilcdc_crtc_off(crtc, true);
Jyri Sarha9e79e062016-10-18 23:23:27 +0300574}
575
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300576static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
577{
578 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600579}
580
Jyri Sarha13b3d722016-04-06 14:02:38 +0300581static void tilcdc_crtc_recover_work(struct work_struct *work)
582{
583 struct tilcdc_crtc *tilcdc_crtc =
584 container_of(work, struct tilcdc_crtc, recover_work);
585 struct drm_crtc *crtc = &tilcdc_crtc->base;
586
587 dev_info(crtc->dev->dev, "%s: Reset CRTC", __func__);
588
Daniel Vetter33e5b662017-03-22 22:50:47 +0100589 drm_modeset_lock(&crtc->mutex, NULL);
Jyri Sarha13b3d722016-04-06 14:02:38 +0300590
591 if (!tilcdc_crtc_is_on(crtc))
592 goto out;
593
594 tilcdc_crtc_disable(crtc);
595 tilcdc_crtc_enable(crtc);
596out:
Daniel Vetter33e5b662017-03-22 22:50:47 +0100597 drm_modeset_unlock(&crtc->mutex);
Jyri Sarha13b3d722016-04-06 14:02:38 +0300598}
599
Rob Clark16ea9752013-01-08 15:04:28 -0600600static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
601{
602 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Jyri Sarha4e910c72016-09-06 22:55:33 +0300603 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600604
Daniel Vetter33e5b662017-03-22 22:50:47 +0100605 drm_modeset_lock(&crtc->mutex, NULL);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300606 tilcdc_crtc_disable(crtc);
Daniel Vetter33e5b662017-03-22 22:50:47 +0100607 drm_modeset_unlock(&crtc->mutex);
Rob Clark16ea9752013-01-08 15:04:28 -0600608
Jyri Sarha4e910c72016-09-06 22:55:33 +0300609 flush_workqueue(priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600610
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300611 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600612 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400613 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600614}
615
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300616int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600617 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300618 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600619{
620 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
621 struct drm_device *dev = crtc->dev;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000622
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300623 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
624
Rob Clark16ea9752013-01-08 15:04:28 -0600625 if (tilcdc_crtc->event) {
626 dev_err(dev->dev, "already pending page flip!\n");
627 return -EBUSY;
628 }
629
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300630 drm_framebuffer_reference(fb);
631
Matt Roperf4510a22014-04-01 15:22:40 -0700632 crtc->primary->fb = fb;
Jyri Sarha11abbc92017-03-01 10:30:28 +0200633 tilcdc_crtc->event = event;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300634
Jyri Sarha11abbc92017-03-01 10:30:28 +0200635 mutex_lock(&tilcdc_crtc->enable_lock);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300636
Jyri Sarha11abbc92017-03-01 10:30:28 +0200637 if (tilcdc_crtc->enabled) {
638 unsigned long flags;
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300639 ktime_t next_vblank;
640 s64 tdiff;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300641
Jyri Sarha11abbc92017-03-01 10:30:28 +0200642 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200643
Jyri Sarha11abbc92017-03-01 10:30:28 +0200644 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
645 1000000 / crtc->hwmode.vrefresh);
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300646 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
647
648 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
649 tilcdc_crtc->next_fb = fb;
Jyri Sarha11abbc92017-03-01 10:30:28 +0200650 else
651 set_scanout(crtc, fb);
652
653 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300654 }
655
Jyri Sarha11abbc92017-03-01 10:30:28 +0200656 mutex_unlock(&tilcdc_crtc->enable_lock);
Rob Clark16ea9752013-01-08 15:04:28 -0600657
658 return 0;
659}
660
Rob Clark16ea9752013-01-08 15:04:28 -0600661static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
662 const struct drm_display_mode *mode,
663 struct drm_display_mode *adjusted_mode)
664{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200665 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
666
667 if (!tilcdc_crtc->simulate_vesa_sync)
668 return true;
669
670 /*
671 * tilcdc does not generate VESA-compliant sync but aligns
672 * VS on the second edge of HS instead of first edge.
673 * We use adjusted_mode, to fixup sync by aligning both rising
674 * edges and add HSKEW offset to fix the sync.
675 */
676 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
677 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
678
679 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
680 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
681 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
682 } else {
683 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
684 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
685 }
686
Rob Clark16ea9752013-01-08 15:04:28 -0600687 return true;
688}
689
Jyri Sarhadb380c52016-04-07 15:10:23 +0300690static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
691 struct drm_crtc_state *state)
692{
693 struct drm_display_mode *mode = &state->mode;
694 int ret;
695
696 /* If we are not active we don't care */
697 if (!state->active)
698 return 0;
699
700 if (state->state->planes[0].ptr != crtc->primary ||
701 state->state->planes[0].state == NULL ||
702 state->state->planes[0].state->crtc != crtc) {
703 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
704 return -EINVAL;
705 }
706
707 ret = tilcdc_crtc_mode_valid(crtc, mode);
708 if (ret) {
709 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
710 return -EINVAL;
711 }
712
713 return 0;
714}
715
Shawn Guo55cbc4d2017-02-07 17:16:33 +0800716static int tilcdc_crtc_enable_vblank(struct drm_crtc *crtc)
717{
718 return 0;
719}
720
721static void tilcdc_crtc_disable_vblank(struct drm_crtc *crtc)
722{
723}
724
Rob Clark16ea9752013-01-08 15:04:28 -0600725static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300726 .destroy = tilcdc_crtc_destroy,
727 .set_config = drm_atomic_helper_set_config,
728 .page_flip = drm_atomic_helper_page_flip,
729 .reset = drm_atomic_helper_crtc_reset,
730 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
731 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Shawn Guo55cbc4d2017-02-07 17:16:33 +0800732 .enable_vblank = tilcdc_crtc_enable_vblank,
733 .disable_vblank = tilcdc_crtc_disable_vblank,
Rob Clark16ea9752013-01-08 15:04:28 -0600734};
735
736static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600737 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300738 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300739 .atomic_check = tilcdc_crtc_atomic_check,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300740 .atomic_enable = tilcdc_crtc_atomic_enable,
Rob Clark16ea9752013-01-08 15:04:28 -0600741};
742
743int tilcdc_crtc_max_width(struct drm_crtc *crtc)
744{
745 struct drm_device *dev = crtc->dev;
746 struct tilcdc_drm_private *priv = dev->dev_private;
747 int max_width = 0;
748
749 if (priv->rev == 1)
750 max_width = 1024;
751 else if (priv->rev == 2)
752 max_width = 2048;
753
754 return max_width;
755}
756
757int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
758{
759 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
760 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500761 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600762
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500763 /*
764 * check to see if the width is within the range that
765 * the LCD Controller physically supports
766 */
Rob Clark16ea9752013-01-08 15:04:28 -0600767 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
768 return MODE_VIRTUAL_X;
769
770 /* width must be multiple of 16 */
771 if (mode->hdisplay & 0xf)
772 return MODE_VIRTUAL_X;
773
774 if (mode->vdisplay > 2048)
775 return MODE_VIRTUAL_Y;
776
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500777 DBG("Processing mode %dx%d@%d with pixel clock %d",
778 mode->hdisplay, mode->vdisplay,
779 drm_mode_vrefresh(mode), mode->clock);
780
781 hbp = mode->htotal - mode->hsync_end;
782 hfp = mode->hsync_start - mode->hdisplay;
783 hsw = mode->hsync_end - mode->hsync_start;
784 vbp = mode->vtotal - mode->vsync_end;
785 vfp = mode->vsync_start - mode->vdisplay;
786 vsw = mode->vsync_end - mode->vsync_start;
787
788 if ((hbp-1) & ~0x3ff) {
789 DBG("Pruning mode: Horizontal Back Porch out of range");
790 return MODE_HBLANK_WIDE;
791 }
792
793 if ((hfp-1) & ~0x3ff) {
794 DBG("Pruning mode: Horizontal Front Porch out of range");
795 return MODE_HBLANK_WIDE;
796 }
797
798 if ((hsw-1) & ~0x3ff) {
799 DBG("Pruning mode: Horizontal Sync Width out of range");
800 return MODE_HSYNC_WIDE;
801 }
802
803 if (vbp & ~0xff) {
804 DBG("Pruning mode: Vertical Back Porch out of range");
805 return MODE_VBLANK_WIDE;
806 }
807
808 if (vfp & ~0xff) {
809 DBG("Pruning mode: Vertical Front Porch out of range");
810 return MODE_VBLANK_WIDE;
811 }
812
813 if ((vsw-1) & ~0x3f) {
814 DBG("Pruning mode: Vertical Sync Width out of range");
815 return MODE_VSYNC_WIDE;
816 }
817
Darren Etheridge4e564342013-06-21 13:52:23 -0500818 /*
819 * some devices have a maximum allowed pixel clock
820 * configured from the DT
821 */
822 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500823 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500824 return MODE_CLOCK_HIGH;
825 }
826
827 /*
828 * some devices further limit the max horizontal resolution
829 * configured from the DT
830 */
831 if (mode->hdisplay > priv->max_width)
832 return MODE_BAD_WIDTH;
833
Rob Clark16ea9752013-01-08 15:04:28 -0600834 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500835 bandwidth = mode->hdisplay * mode->vdisplay *
836 drm_mode_vrefresh(mode);
837 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500838 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600839 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500840 }
Rob Clark16ea9752013-01-08 15:04:28 -0600841
842 return MODE_OK;
843}
844
845void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
846 const struct tilcdc_panel_info *info)
847{
848 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
849 tilcdc_crtc->info = info;
850}
851
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200852void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
853 bool simulate_vesa_sync)
854{
855 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
856
857 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
858}
859
Rob Clark16ea9752013-01-08 15:04:28 -0600860void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
861{
Rob Clark16ea9752013-01-08 15:04:28 -0600862 struct drm_device *dev = crtc->dev;
863 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha642e5162016-09-06 16:19:54 +0300864 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600865
Daniel Vetter33e5b662017-03-22 22:50:47 +0100866 drm_modeset_lock(&crtc->mutex, NULL);
Jyri Sarha642e5162016-09-06 16:19:54 +0300867 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
868 if (tilcdc_crtc_is_on(crtc)) {
869 pm_runtime_get_sync(dev->dev);
870 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600871
Jyri Sarha642e5162016-09-06 16:19:54 +0300872 tilcdc_crtc_set_clk(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600873
Jyri Sarha642e5162016-09-06 16:19:54 +0300874 tilcdc_crtc_enable(crtc);
875 pm_runtime_put_sync(dev->dev);
876 }
Rob Clark16ea9752013-01-08 15:04:28 -0600877 }
Daniel Vetter33e5b662017-03-22 22:50:47 +0100878 drm_modeset_unlock(&crtc->mutex);
Rob Clark16ea9752013-01-08 15:04:28 -0600879}
880
Jyri Sarha5895d082016-01-08 14:33:09 +0200881#define SYNC_LOST_COUNT_LIMIT 50
882
Rob Clark16ea9752013-01-08 15:04:28 -0600883irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
884{
885 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
886 struct drm_device *dev = crtc->dev;
887 struct tilcdc_drm_private *priv = dev->dev_private;
Bartosz Golaszewskif97fd382016-12-19 15:47:14 +0100888 uint32_t stat, reg;
Rob Clark16ea9752013-01-08 15:04:28 -0600889
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300890 stat = tilcdc_read_irqstatus(dev);
891 tilcdc_clear_irqstatus(dev, stat);
892
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300893 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600894 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200895 bool skip_event = false;
896 ktime_t now;
897
898 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600899
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300900 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600901
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200902 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600903
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200904 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600905
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200906 if (tilcdc_crtc->next_fb) {
907 set_scanout(crtc, tilcdc_crtc->next_fb);
908 tilcdc_crtc->next_fb = NULL;
909 skip_event = true;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300910 }
911
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200912 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
913
Gustavo Padovan099ede82016-07-04 21:04:52 -0300914 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200915
916 if (!skip_event) {
917 struct drm_pending_vblank_event *event;
918
919 spin_lock_irqsave(&dev->event_lock, flags);
920
921 event = tilcdc_crtc->event;
922 tilcdc_crtc->event = NULL;
923 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700924 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200925
926 spin_unlock_irqrestore(&dev->event_lock, flags);
927 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200928
929 if (tilcdc_crtc->frame_intact)
930 tilcdc_crtc->sync_lost_count = 0;
931 else
932 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600933 }
934
Jyri Sarha14944112016-04-07 20:36:48 +0300935 if (stat & LCDC_FIFO_UNDERFLOW)
Daniel Schultzd7014532016-10-28 13:52:42 +0200936 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underflow",
Jyri Sarha14944112016-04-07 20:36:48 +0300937 __func__, stat);
938
Jyri Sarha55e165c2016-11-15 23:37:24 +0200939 if (stat & LCDC_PL_LOAD_DONE) {
940 complete(&tilcdc_crtc->palette_loaded);
941 if (priv->rev == 1)
942 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
943 LCDC_V1_PL_INT_ENA);
944 else
945 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
946 LCDC_V2_PL_INT_ENA);
Bartosz Golaszewski93452352016-10-31 15:19:26 +0100947 }
948
Jyri Sarhacba88442016-11-16 00:12:27 +0200949 if (stat & LCDC_SYNC_LOST) {
950 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
951 __func__, stat);
952 tilcdc_crtc->frame_intact = false;
Bartosz Golaszewskif97fd382016-12-19 15:47:14 +0100953 if (priv->rev == 1) {
954 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG);
955 if (reg & LCDC_RASTER_ENABLE) {
Jyri Sarhacba88442016-11-16 00:12:27 +0200956 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
Bartosz Golaszewskif97fd382016-12-19 15:47:14 +0100957 LCDC_RASTER_ENABLE);
958 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
959 LCDC_RASTER_ENABLE);
960 }
961 } else {
962 if (tilcdc_crtc->sync_lost_count++ >
963 SYNC_LOST_COUNT_LIMIT) {
964 dev_err(dev->dev,
965 "%s(0x%08x): Sync lost flood detected, recovering",
966 __func__, stat);
967 queue_work(system_wq,
968 &tilcdc_crtc->recover_work);
Jyri Sarhacba88442016-11-16 00:12:27 +0200969 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
970 LCDC_SYNC_LOST);
Bartosz Golaszewskif97fd382016-12-19 15:47:14 +0100971 tilcdc_crtc->sync_lost_count = 0;
972 }
Jyri Sarhacba88442016-11-16 00:12:27 +0200973 }
974 }
975
Jyri Sarha36725832016-11-21 18:30:19 +0200976 if (stat & LCDC_FRAME_DONE) {
977 tilcdc_crtc->frame_done = true;
978 wake_up(&tilcdc_crtc->frame_done_wq);
979 /* rev 1 lcdc appears to hang if irq is not disbaled here */
980 if (priv->rev == 1)
981 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
982 LCDC_V1_FRAME_DONE_INT_ENA);
983 }
984
Jyri Sarha14944112016-04-07 20:36:48 +0300985 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600986 if (priv->rev == 2) {
Jyri Sarha14944112016-04-07 20:36:48 +0300987 /* Indicate to LCDC that the interrupt service routine has
988 * completed, see 13.3.6.1.6 in AM335x TRM.
989 */
990 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
991 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200992
Rob Clark16ea9752013-01-08 15:04:28 -0600993 return IRQ_HANDLED;
994}
995
Jyri Sarha9963d362016-11-15 22:56:46 +0200996int tilcdc_crtc_create(struct drm_device *dev)
Rob Clark16ea9752013-01-08 15:04:28 -0600997{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300998 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600999 struct tilcdc_crtc *tilcdc_crtc;
1000 struct drm_crtc *crtc;
1001 int ret;
1002
Jyri Sarhad0ec32c2016-02-23 12:44:27 +02001003 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -06001004 if (!tilcdc_crtc) {
1005 dev_err(dev->dev, "allocation failed\n");
Jyri Sarha9963d362016-11-15 22:56:46 +02001006 return -ENOMEM;
Rob Clark16ea9752013-01-08 15:04:28 -06001007 }
1008
Jyri Sarha55e165c2016-11-15 23:37:24 +02001009 init_completion(&tilcdc_crtc->palette_loaded);
1010 tilcdc_crtc->palette_base = dmam_alloc_coherent(dev->dev,
1011 TILCDC_PALETTE_SIZE,
Bartosz Golaszewski93452352016-10-31 15:19:26 +01001012 &tilcdc_crtc->palette_dma_handle,
1013 GFP_KERNEL | __GFP_ZERO);
Jyri Sarha55e165c2016-11-15 23:37:24 +02001014 if (!tilcdc_crtc->palette_base)
1015 return -ENOMEM;
1016 *tilcdc_crtc->palette_base = TILCDC_PALETTE_FIRST_ENTRY;
Bartosz Golaszewski93452352016-10-31 15:19:26 +01001017
Rob Clark16ea9752013-01-08 15:04:28 -06001018 crtc = &tilcdc_crtc->base;
1019
Jyri Sarha47f571c2016-04-07 15:04:18 +03001020 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
1021 if (ret < 0)
1022 goto fail;
1023
Jyri Sarha2d53a182016-10-25 12:27:31 +03001024 mutex_init(&tilcdc_crtc->enable_lock);
1025
Rob Clark16ea9752013-01-08 15:04:28 -06001026 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
1027
Boris BREZILLONd7f8db52014-11-14 19:30:30 +01001028 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -04001029 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -06001030
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +02001031 spin_lock_init(&tilcdc_crtc->irq_lock);
Jyri Sarha13b3d722016-04-06 14:02:38 +03001032 INIT_WORK(&tilcdc_crtc->recover_work, tilcdc_crtc_recover_work);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +02001033
Jyri Sarha47f571c2016-04-07 15:04:18 +03001034 ret = drm_crtc_init_with_planes(dev, crtc,
1035 &tilcdc_crtc->primary,
1036 NULL,
1037 &tilcdc_crtc_funcs,
1038 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -06001039 if (ret < 0)
1040 goto fail;
1041
1042 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
1043
Jyri Sarhad66284fb2015-05-27 11:58:37 +03001044 if (priv->is_componentized) {
Rob Herring86418f92017-03-22 08:26:06 -05001045 crtc->port = of_graph_get_port_by_id(dev->dev->of_node, 0);
Jyri Sarhad66284fb2015-05-27 11:58:37 +03001046 if (!crtc->port) { /* This should never happen */
1047 dev_err(dev->dev, "Port node not found in %s\n",
1048 dev->dev->of_node->full_name);
Jyri Sarha9963d362016-11-15 22:56:46 +02001049 ret = -EINVAL;
Jyri Sarhad66284fb2015-05-27 11:58:37 +03001050 goto fail;
1051 }
1052 }
1053
Jyri Sarha9963d362016-11-15 22:56:46 +02001054 priv->crtc = crtc;
1055 return 0;
Rob Clark16ea9752013-01-08 15:04:28 -06001056
1057fail:
1058 tilcdc_crtc_destroy(crtc);
Jyri Sarhaabf83152017-01-31 16:18:42 +02001059 return ret;
Rob Clark16ea9752013-01-08 15:04:28 -06001060}