blob: 0d09acce4916115a486b8d0c3ba392a7c7200d99 [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>
Rob Clark16ea9752013-01-08 15:04:28 -060024
25#include "tilcdc_drv.h"
26#include "tilcdc_regs.h"
27
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020028#define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
29
Rob Clark16ea9752013-01-08 15:04:28 -060030struct tilcdc_crtc {
31 struct drm_crtc base;
32
Jyri Sarha47f571c2016-04-07 15:04:18 +030033 struct drm_plane primary;
Rob Clark16ea9752013-01-08 15:04:28 -060034 const struct tilcdc_panel_info *info;
Rob Clark16ea9752013-01-08 15:04:28 -060035 struct drm_pending_vblank_event *event;
Jyri Sarha2d53a182016-10-25 12:27:31 +030036 struct mutex enable_lock;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +030037 bool enabled;
Jyri Sarha2d53a182016-10-25 12:27:31 +030038 bool shutdown;
Rob Clark16ea9752013-01-08 15:04:28 -060039 wait_queue_head_t frame_done_wq;
40 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020041 spinlock_t irq_lock;
42
Jyri Sarha642e5162016-09-06 16:19:54 +030043 unsigned int lcd_fck_rate;
44
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020045 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060046
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030047 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020048 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060049
50 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040051 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020052
53 /* Only set if an external encoder is connected */
54 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020055
56 int sync_lost_count;
57 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060058};
59#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
60
Rob Clarka464d612013-08-07 13:41:20 -040061static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060062{
Darren Etheridgef7b45752013-06-21 13:52:26 -050063 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040064 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060065 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060066
67 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040068 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060069 mutex_unlock(&dev->mode_config.mutex);
70}
71
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030072static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060073{
74 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
75 struct drm_device *dev = crtc->dev;
Daniel Schultz4c268d62016-10-28 13:52:41 +020076 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -060077 struct drm_gem_cma_object *gem;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030078 dma_addr_t start, end;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030079 u64 dma_base_and_ceiling;
Rob Clark16ea9752013-01-08 15:04:28 -060080
Rob Clark16ea9752013-01-08 15:04:28 -060081 gem = drm_fb_cma_get_gem_obj(fb, 0);
82
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030083 start = gem->paddr + fb->offsets[0] +
84 crtc->y * fb->pitches[0] +
Laurent Pinchart59f11a42016-10-18 01:41:14 +030085 crtc->x * drm_format_plane_cpp(fb->pixel_format, 0);
Rob Clark16ea9752013-01-08 15:04:28 -060086
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030087 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060088
Jyri Sarha7eb9f062016-08-26 15:10:14 +030089 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
90 * with a single insruction, if available. This should make it more
91 * unlikely that LCDC would fetch the DMA addresses in the middle of
92 * an update.
93 */
Daniel Schultz4c268d62016-10-28 13:52:41 +020094 if (priv->rev == 1)
95 end -= 1;
96
97 dma_base_and_ceiling = (u64)end << 32 | start;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030098 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030099
100 if (tilcdc_crtc->curr_fb)
101 drm_flip_work_queue(&tilcdc_crtc->unref_work,
102 tilcdc_crtc->curr_fb);
103
104 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600105}
106
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300107static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
108{
109 struct tilcdc_drm_private *priv = dev->dev_private;
110
111 tilcdc_clear_irqstatus(dev, 0xffffffff);
112
113 if (priv->rev == 1) {
114 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
115 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000116 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
117 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300118 } else {
119 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
120 LCDC_V2_UNDERFLOW_INT_ENA |
121 LCDC_V2_END_OF_FRAME0_INT_ENA |
122 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
123 }
124}
125
126static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
127{
128 struct tilcdc_drm_private *priv = dev->dev_private;
129
130 /* disable irqs that we might have enabled: */
131 if (priv->rev == 1) {
132 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
133 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
134 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
135 LCDC_V1_END_OF_FRAME_INT_ENA);
136 } else {
137 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
138 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
139 LCDC_V2_END_OF_FRAME0_INT_ENA |
140 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
141 }
142}
143
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300144static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600145{
146 struct drm_device *dev = crtc->dev;
147 struct tilcdc_drm_private *priv = dev->dev_private;
148
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300149 if (priv->rev != 2)
150 return;
151
152 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
153 usleep_range(250, 1000);
154 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
155}
156
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300157static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300158{
159 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300160 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
161
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300162 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Jyri Sarha2d53a182016-10-25 12:27:31 +0300163 mutex_lock(&tilcdc_crtc->enable_lock);
164 if (tilcdc_crtc->enabled || tilcdc_crtc->shutdown) {
165 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300166 return;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300167 }
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300168
169 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300170
171 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600172
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300173 tilcdc_crtc_enable_irqs(dev);
174
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300175 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600176 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
177 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300178
179 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300180
181 tilcdc_crtc->enabled = true;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300182 mutex_unlock(&tilcdc_crtc->enable_lock);
Rob Clark16ea9752013-01-08 15:04:28 -0600183}
184
Jyri Sarha2d53a182016-10-25 12:27:31 +0300185static void tilcdc_crtc_off(struct drm_crtc *crtc, bool shutdown)
Rob Clark16ea9752013-01-08 15:04:28 -0600186{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300187 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600188 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300189 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600190
Jyri Sarha2d53a182016-10-25 12:27:31 +0300191 mutex_lock(&tilcdc_crtc->enable_lock);
192 if (shutdown)
193 tilcdc_crtc->shutdown = true;
194 if (!tilcdc_crtc->enabled) {
195 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300196 return;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300197 }
Jyri Sarha2d5be882016-04-07 20:20:23 +0300198 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600199 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300200
201 /*
202 * if necessary wait for framedone irq which will still come
203 * before putting things to sleep..
204 */
205 if (priv->rev == 2) {
206 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
207 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300208 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300209 if (ret == 0)
210 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
211 __func__);
212 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300213
214 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300215
216 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300217
218 pm_runtime_put_sync(dev->dev);
219
220 if (tilcdc_crtc->next_fb) {
221 drm_flip_work_queue(&tilcdc_crtc->unref_work,
222 tilcdc_crtc->next_fb);
223 tilcdc_crtc->next_fb = NULL;
224 }
225
226 if (tilcdc_crtc->curr_fb) {
227 drm_flip_work_queue(&tilcdc_crtc->unref_work,
228 tilcdc_crtc->curr_fb);
229 tilcdc_crtc->curr_fb = NULL;
230 }
231
232 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
233 tilcdc_crtc->last_vblank = ktime_set(0, 0);
234
235 tilcdc_crtc->enabled = false;
Jyri Sarha2d53a182016-10-25 12:27:31 +0300236 mutex_unlock(&tilcdc_crtc->enable_lock);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300237}
238
Jyri Sarha9e79e062016-10-18 23:23:27 +0300239static void tilcdc_crtc_disable(struct drm_crtc *crtc)
240{
241 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
Jyri Sarha2d53a182016-10-25 12:27:31 +0300242 tilcdc_crtc_off(crtc, false);
243}
244
245void tilcdc_crtc_shutdown(struct drm_crtc *crtc)
246{
247 tilcdc_crtc_off(crtc, true);
Jyri Sarha9e79e062016-10-18 23:23:27 +0300248}
249
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300250static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
251{
252 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600253}
254
255static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
256{
257 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Jyri Sarha4e910c72016-09-06 22:55:33 +0300258 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600259
Jyri Sarha6c94c712016-09-07 11:46:40 +0300260 drm_modeset_lock_crtc(crtc, NULL);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300261 tilcdc_crtc_disable(crtc);
Jyri Sarha6c94c712016-09-07 11:46:40 +0300262 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600263
Jyri Sarha4e910c72016-09-06 22:55:33 +0300264 flush_workqueue(priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600265
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300266 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600267 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400268 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600269}
270
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300271int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600272 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300273 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600274{
275 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
276 struct drm_device *dev = crtc->dev;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300277 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000278
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300279 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
280
Rob Clark16ea9752013-01-08 15:04:28 -0600281 if (tilcdc_crtc->event) {
282 dev_err(dev->dev, "already pending page flip!\n");
283 return -EBUSY;
284 }
285
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300286 drm_framebuffer_reference(fb);
287
Matt Roperf4510a22014-04-01 15:22:40 -0700288 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300289
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200290 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300291
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300292 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
293 ktime_t next_vblank;
294 s64 tdiff;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300295
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300296 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
297 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200298
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300299 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
300
301 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
302 tilcdc_crtc->next_fb = fb;
303 }
304
305 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200306 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200307
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300308 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200309
310 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600311
312 return 0;
313}
314
Rob Clark16ea9752013-01-08 15:04:28 -0600315static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
316 const struct drm_display_mode *mode,
317 struct drm_display_mode *adjusted_mode)
318{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200319 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
320
321 if (!tilcdc_crtc->simulate_vesa_sync)
322 return true;
323
324 /*
325 * tilcdc does not generate VESA-compliant sync but aligns
326 * VS on the second edge of HS instead of first edge.
327 * We use adjusted_mode, to fixup sync by aligning both rising
328 * edges and add HSKEW offset to fix the sync.
329 */
330 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
331 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
332
333 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
334 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
335 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
336 } else {
337 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
338 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
339 }
340
Rob Clark16ea9752013-01-08 15:04:28 -0600341 return true;
342}
343
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200344/*
345 * Calculate the percentage difference between the requested pixel clock rate
346 * and the effective rate resulting from calculating the clock divider value.
347 */
348static unsigned int tilcdc_pclk_diff(unsigned long rate,
349 unsigned long real_rate)
350{
351 int r = rate / 100, rr = real_rate / 100;
352
353 return (unsigned int)(abs(((rr - r) * 100) / r));
354}
355
Jyri Sarha642e5162016-09-06 16:19:54 +0300356static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
357{
358 struct drm_device *dev = crtc->dev;
359 struct tilcdc_drm_private *priv = dev->dev_private;
360 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200361 unsigned long clk_rate, real_rate, req_rate;
362 unsigned int clkdiv;
Jyri Sarha642e5162016-09-06 16:19:54 +0300363 int ret;
364
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200365 clkdiv = 2; /* first try using a standard divider of 2 */
366
Jyri Sarha642e5162016-09-06 16:19:54 +0300367 /* mode.clock is in KHz, set_rate wants parameter in Hz */
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200368 req_rate = crtc->mode.clock * 1000;
369
370 ret = clk_set_rate(priv->clk, req_rate * clkdiv);
371 clk_rate = clk_get_rate(priv->clk);
Jyri Sarha642e5162016-09-06 16:19:54 +0300372 if (ret < 0) {
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200373 /*
374 * If we fail to set the clock rate (some architectures don't
375 * use the common clock framework yet and may not implement
376 * all the clk API calls for every clock), try the next best
377 * thing: adjusting the clock divider, unless clk_get_rate()
378 * failed as well.
379 */
380 if (!clk_rate) {
381 /* Nothing more we can do. Just bail out. */
382 dev_err(dev->dev,
383 "failed to set the pixel clock - unable to read current lcdc clock rate\n");
384 return;
385 }
386
387 clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);
388
389 /*
390 * Emit a warning if the real clock rate resulting from the
391 * calculated divider differs much from the requested rate.
392 *
393 * 5% is an arbitrary value - LCDs are usually quite tolerant
394 * about pixel clock rates.
395 */
396 real_rate = clkdiv * req_rate;
397
398 if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
399 dev_warn(dev->dev,
400 "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
401 clk_rate, real_rate);
402 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300403 }
404
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200405 tilcdc_crtc->lcd_fck_rate = clk_rate;
Jyri Sarha642e5162016-09-06 16:19:54 +0300406
407 DBG("lcd_clk=%u, mode clock=%d, div=%u",
408 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
409
410 /* Configure the LCD clock divisor. */
411 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
412 LCDC_RASTER_MODE);
413
414 if (priv->rev == 2)
415 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
416 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
417 LCDC_V2_CORE_CLK_EN);
418}
419
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300420static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
421{
422 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
423 struct drm_device *dev = crtc->dev;
424 struct tilcdc_drm_private *priv = dev->dev_private;
425 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
426 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
427 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
428 struct drm_framebuffer *fb = crtc->primary->state->fb;
429
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300430 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
431
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300432 if (WARN_ON(!info))
433 return;
434
435 if (WARN_ON(!fb))
436 return;
437
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300438 /* Configure the Burst Size and fifo threshold of DMA: */
439 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
440 switch (info->dma_burst_sz) {
441 case 1:
442 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
443 break;
444 case 2:
445 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
446 break;
447 case 4:
448 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
449 break;
450 case 8:
451 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
452 break;
453 case 16:
454 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
455 break;
456 default:
457 dev_err(dev->dev, "invalid burst size\n");
458 return;
459 }
460 reg |= (info->fifo_th << 8);
461 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
462
463 /* Configure timings: */
464 hbp = mode->htotal - mode->hsync_end;
465 hfp = mode->hsync_start - mode->hdisplay;
466 hsw = mode->hsync_end - mode->hsync_start;
467 vbp = mode->vtotal - mode->vsync_end;
468 vfp = mode->vsync_start - mode->vdisplay;
469 vsw = mode->vsync_end - mode->vsync_start;
470
471 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
472 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
473
474 /* Set AC Bias Period and Number of Transitions per Interrupt: */
475 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
476 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
477 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
478
479 /*
480 * subtract one from hfp, hbp, hsw because the hardware uses
481 * a value of 0 as 1
482 */
483 if (priv->rev == 2) {
484 /* clear bits we're going to set */
485 reg &= ~0x78000033;
486 reg |= ((hfp-1) & 0x300) >> 8;
487 reg |= ((hbp-1) & 0x300) >> 4;
488 reg |= ((hsw-1) & 0x3c0) << 21;
489 }
490 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
491
492 reg = (((mode->hdisplay >> 4) - 1) << 4) |
493 (((hbp-1) & 0xff) << 24) |
494 (((hfp-1) & 0xff) << 16) |
495 (((hsw-1) & 0x3f) << 10);
496 if (priv->rev == 2)
497 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
498 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
499
500 reg = ((mode->vdisplay - 1) & 0x3ff) |
501 ((vbp & 0xff) << 24) |
502 ((vfp & 0xff) << 16) |
503 (((vsw-1) & 0x3f) << 10);
504 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
505
506 /*
507 * be sure to set Bit 10 for the V2 LCDC controller,
508 * otherwise limited to 1024 pixels width, stopping
509 * 1920x1080 being supported.
510 */
511 if (priv->rev == 2) {
512 if ((mode->vdisplay - 1) & 0x400) {
513 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
514 LCDC_LPP_B10);
515 } else {
516 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
517 LCDC_LPP_B10);
518 }
519 }
520
521 /* Configure display type: */
522 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
523 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
524 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
525 0x000ff000 /* Palette Loading Delay bits */);
526 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
527 if (info->tft_alt_mode)
528 reg |= LCDC_TFT_ALT_ENABLE;
529 if (priv->rev == 2) {
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300530 switch (fb->pixel_format) {
531 case DRM_FORMAT_BGR565:
532 case DRM_FORMAT_RGB565:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300533 break;
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300534 case DRM_FORMAT_XBGR8888:
535 case DRM_FORMAT_XRGB8888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300536 reg |= LCDC_V2_TFT_24BPP_UNPACK;
537 /* fallthrough */
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300538 case DRM_FORMAT_BGR888:
539 case DRM_FORMAT_RGB888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300540 reg |= LCDC_V2_TFT_24BPP_MODE;
541 break;
542 default:
543 dev_err(dev->dev, "invalid pixel format\n");
544 return;
545 }
546 }
547 reg |= info->fdd < 12;
548 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
549
550 if (info->invert_pxl_clk)
551 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
552 else
553 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
554
555 if (info->sync_ctrl)
556 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
557 else
558 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
559
560 if (info->sync_edge)
561 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
562 else
563 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
564
565 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
566 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
567 else
568 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
569
570 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
571 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
572 else
573 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
574
575 if (info->raster_order)
576 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
577 else
578 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
579
580 drm_framebuffer_reference(fb);
581
582 set_scanout(crtc, fb);
583
Jyri Sarha642e5162016-09-06 16:19:54 +0300584 tilcdc_crtc_set_clk(crtc);
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300585
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300586 crtc->hwmode = crtc->state->adjusted_mode;
587}
588
Jyri Sarhadb380c52016-04-07 15:10:23 +0300589static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
590 struct drm_crtc_state *state)
591{
592 struct drm_display_mode *mode = &state->mode;
593 int ret;
594
595 /* If we are not active we don't care */
596 if (!state->active)
597 return 0;
598
599 if (state->state->planes[0].ptr != crtc->primary ||
600 state->state->planes[0].state == NULL ||
601 state->state->planes[0].state->crtc != crtc) {
602 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
603 return -EINVAL;
604 }
605
606 ret = tilcdc_crtc_mode_valid(crtc, mode);
607 if (ret) {
608 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
609 return -EINVAL;
610 }
611
612 return 0;
613}
614
Rob Clark16ea9752013-01-08 15:04:28 -0600615static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300616 .destroy = tilcdc_crtc_destroy,
617 .set_config = drm_atomic_helper_set_config,
618 .page_flip = drm_atomic_helper_page_flip,
619 .reset = drm_atomic_helper_crtc_reset,
620 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
621 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600622};
623
624static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600625 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300626 .enable = tilcdc_crtc_enable,
627 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300628 .atomic_check = tilcdc_crtc_atomic_check,
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300629 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
Rob Clark16ea9752013-01-08 15:04:28 -0600630};
631
632int tilcdc_crtc_max_width(struct drm_crtc *crtc)
633{
634 struct drm_device *dev = crtc->dev;
635 struct tilcdc_drm_private *priv = dev->dev_private;
636 int max_width = 0;
637
638 if (priv->rev == 1)
639 max_width = 1024;
640 else if (priv->rev == 2)
641 max_width = 2048;
642
643 return max_width;
644}
645
646int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
647{
648 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
649 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500650 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600651
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500652 /*
653 * check to see if the width is within the range that
654 * the LCD Controller physically supports
655 */
Rob Clark16ea9752013-01-08 15:04:28 -0600656 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
657 return MODE_VIRTUAL_X;
658
659 /* width must be multiple of 16 */
660 if (mode->hdisplay & 0xf)
661 return MODE_VIRTUAL_X;
662
663 if (mode->vdisplay > 2048)
664 return MODE_VIRTUAL_Y;
665
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500666 DBG("Processing mode %dx%d@%d with pixel clock %d",
667 mode->hdisplay, mode->vdisplay,
668 drm_mode_vrefresh(mode), mode->clock);
669
670 hbp = mode->htotal - mode->hsync_end;
671 hfp = mode->hsync_start - mode->hdisplay;
672 hsw = mode->hsync_end - mode->hsync_start;
673 vbp = mode->vtotal - mode->vsync_end;
674 vfp = mode->vsync_start - mode->vdisplay;
675 vsw = mode->vsync_end - mode->vsync_start;
676
677 if ((hbp-1) & ~0x3ff) {
678 DBG("Pruning mode: Horizontal Back Porch out of range");
679 return MODE_HBLANK_WIDE;
680 }
681
682 if ((hfp-1) & ~0x3ff) {
683 DBG("Pruning mode: Horizontal Front Porch out of range");
684 return MODE_HBLANK_WIDE;
685 }
686
687 if ((hsw-1) & ~0x3ff) {
688 DBG("Pruning mode: Horizontal Sync Width out of range");
689 return MODE_HSYNC_WIDE;
690 }
691
692 if (vbp & ~0xff) {
693 DBG("Pruning mode: Vertical Back Porch out of range");
694 return MODE_VBLANK_WIDE;
695 }
696
697 if (vfp & ~0xff) {
698 DBG("Pruning mode: Vertical Front Porch out of range");
699 return MODE_VBLANK_WIDE;
700 }
701
702 if ((vsw-1) & ~0x3f) {
703 DBG("Pruning mode: Vertical Sync Width out of range");
704 return MODE_VSYNC_WIDE;
705 }
706
Darren Etheridge4e564342013-06-21 13:52:23 -0500707 /*
708 * some devices have a maximum allowed pixel clock
709 * configured from the DT
710 */
711 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500712 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500713 return MODE_CLOCK_HIGH;
714 }
715
716 /*
717 * some devices further limit the max horizontal resolution
718 * configured from the DT
719 */
720 if (mode->hdisplay > priv->max_width)
721 return MODE_BAD_WIDTH;
722
Rob Clark16ea9752013-01-08 15:04:28 -0600723 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500724 bandwidth = mode->hdisplay * mode->vdisplay *
725 drm_mode_vrefresh(mode);
726 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500727 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600728 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500729 }
Rob Clark16ea9752013-01-08 15:04:28 -0600730
731 return MODE_OK;
732}
733
734void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
735 const struct tilcdc_panel_info *info)
736{
737 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
738 tilcdc_crtc->info = info;
739}
740
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200741void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
742 bool simulate_vesa_sync)
743{
744 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
745
746 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
747}
748
Rob Clark16ea9752013-01-08 15:04:28 -0600749void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
750{
Rob Clark16ea9752013-01-08 15:04:28 -0600751 struct drm_device *dev = crtc->dev;
752 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha642e5162016-09-06 16:19:54 +0300753 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600754
Jyri Sarha642e5162016-09-06 16:19:54 +0300755 drm_modeset_lock_crtc(crtc, NULL);
756 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
757 if (tilcdc_crtc_is_on(crtc)) {
758 pm_runtime_get_sync(dev->dev);
759 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600760
Jyri Sarha642e5162016-09-06 16:19:54 +0300761 tilcdc_crtc_set_clk(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600762
Jyri Sarha642e5162016-09-06 16:19:54 +0300763 tilcdc_crtc_enable(crtc);
764 pm_runtime_put_sync(dev->dev);
765 }
Rob Clark16ea9752013-01-08 15:04:28 -0600766 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300767 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600768}
769
Jyri Sarha5895d082016-01-08 14:33:09 +0200770#define SYNC_LOST_COUNT_LIMIT 50
771
Rob Clark16ea9752013-01-08 15:04:28 -0600772irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
773{
774 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
775 struct drm_device *dev = crtc->dev;
776 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300777 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600778
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300779 stat = tilcdc_read_irqstatus(dev);
780 tilcdc_clear_irqstatus(dev, stat);
781
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300782 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600783 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200784 bool skip_event = false;
785 ktime_t now;
786
787 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600788
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300789 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600790
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200791 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600792
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200793 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600794
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200795 if (tilcdc_crtc->next_fb) {
796 set_scanout(crtc, tilcdc_crtc->next_fb);
797 tilcdc_crtc->next_fb = NULL;
798 skip_event = true;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300799 }
800
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200801 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
802
Gustavo Padovan099ede82016-07-04 21:04:52 -0300803 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200804
805 if (!skip_event) {
806 struct drm_pending_vblank_event *event;
807
808 spin_lock_irqsave(&dev->event_lock, flags);
809
810 event = tilcdc_crtc->event;
811 tilcdc_crtc->event = NULL;
812 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700813 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200814
815 spin_unlock_irqrestore(&dev->event_lock, flags);
816 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200817
818 if (tilcdc_crtc->frame_intact)
819 tilcdc_crtc->sync_lost_count = 0;
820 else
821 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600822 }
823
Jyri Sarha14944112016-04-07 20:36:48 +0300824 if (stat & LCDC_FIFO_UNDERFLOW)
Daniel Schultzd7014532016-10-28 13:52:42 +0200825 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underflow",
Jyri Sarha14944112016-04-07 20:36:48 +0300826 __func__, stat);
827
828 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600829 if (priv->rev == 2) {
830 if (stat & LCDC_FRAME_DONE) {
831 tilcdc_crtc->frame_done = true;
832 wake_up(&tilcdc_crtc->frame_done_wq);
833 }
Rob Clark16ea9752013-01-08 15:04:28 -0600834
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300835 if (stat & LCDC_SYNC_LOST) {
836 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
837 __func__, stat);
838 tilcdc_crtc->frame_intact = false;
839 if (tilcdc_crtc->sync_lost_count++ >
840 SYNC_LOST_COUNT_LIMIT) {
841 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
842 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
843 LCDC_SYNC_LOST);
844 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200845 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200846
Jyri Sarha14944112016-04-07 20:36:48 +0300847 /* Indicate to LCDC that the interrupt service routine has
848 * completed, see 13.3.6.1.6 in AM335x TRM.
849 */
850 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
851 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200852
Rob Clark16ea9752013-01-08 15:04:28 -0600853 return IRQ_HANDLED;
854}
855
Rob Clark16ea9752013-01-08 15:04:28 -0600856struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
857{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300858 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600859 struct tilcdc_crtc *tilcdc_crtc;
860 struct drm_crtc *crtc;
861 int ret;
862
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200863 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600864 if (!tilcdc_crtc) {
865 dev_err(dev->dev, "allocation failed\n");
866 return NULL;
867 }
868
869 crtc = &tilcdc_crtc->base;
870
Jyri Sarha47f571c2016-04-07 15:04:18 +0300871 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
872 if (ret < 0)
873 goto fail;
874
Jyri Sarha2d53a182016-10-25 12:27:31 +0300875 mutex_init(&tilcdc_crtc->enable_lock);
876
Rob Clark16ea9752013-01-08 15:04:28 -0600877 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
878
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100879 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400880 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600881
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200882 spin_lock_init(&tilcdc_crtc->irq_lock);
883
Jyri Sarha47f571c2016-04-07 15:04:18 +0300884 ret = drm_crtc_init_with_planes(dev, crtc,
885 &tilcdc_crtc->primary,
886 NULL,
887 &tilcdc_crtc_funcs,
888 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -0600889 if (ret < 0)
890 goto fail;
891
892 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
893
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300894 if (priv->is_componentized) {
895 struct device_node *ports =
896 of_get_child_by_name(dev->dev->of_node, "ports");
897
898 if (ports) {
899 crtc->port = of_get_child_by_name(ports, "port");
900 of_node_put(ports);
901 } else {
902 crtc->port =
903 of_get_child_by_name(dev->dev->of_node, "port");
904 }
905 if (!crtc->port) { /* This should never happen */
906 dev_err(dev->dev, "Port node not found in %s\n",
907 dev->dev->of_node->full_name);
908 goto fail;
909 }
910 }
911
Rob Clark16ea9752013-01-08 15:04:28 -0600912 return crtc;
913
914fail:
915 tilcdc_crtc_destroy(crtc);
916 return NULL;
917}