blob: db2f538f10ae9eff4301f3446f5cbe674ff97fef [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 Sarha4e910c7a2016-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 Sarha47bfd6c2016-06-22 16:27:54 +030036 bool enabled;
Rob Clark16ea9752013-01-08 15:04:28 -060037 wait_queue_head_t frame_done_wq;
38 bool frame_done;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020039 spinlock_t irq_lock;
40
Jyri Sarha642e5162016-09-06 16:19:54 +030041 unsigned int lcd_fck_rate;
42
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020043 ktime_t last_vblank;
Rob Clark16ea9752013-01-08 15:04:28 -060044
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030045 struct drm_framebuffer *curr_fb;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +020046 struct drm_framebuffer *next_fb;
Rob Clark16ea9752013-01-08 15:04:28 -060047
48 /* for deferred fb unref's: */
Rob Clarka464d612013-08-07 13:41:20 -040049 struct drm_flip_work unref_work;
Jyri Sarha103cd8b2015-02-10 14:13:23 +020050
51 /* Only set if an external encoder is connected */
52 bool simulate_vesa_sync;
Jyri Sarha5895d082016-01-08 14:33:09 +020053
54 int sync_lost_count;
55 bool frame_intact;
Rob Clark16ea9752013-01-08 15:04:28 -060056};
57#define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
58
Rob Clarka464d612013-08-07 13:41:20 -040059static void unref_worker(struct drm_flip_work *work, void *val)
Rob Clark16ea9752013-01-08 15:04:28 -060060{
Darren Etheridgef7b45752013-06-21 13:52:26 -050061 struct tilcdc_crtc *tilcdc_crtc =
Rob Clarka464d612013-08-07 13:41:20 -040062 container_of(work, struct tilcdc_crtc, unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -060063 struct drm_device *dev = tilcdc_crtc->base.dev;
Rob Clark16ea9752013-01-08 15:04:28 -060064
65 mutex_lock(&dev->mode_config.mutex);
Rob Clarka464d612013-08-07 13:41:20 -040066 drm_framebuffer_unreference(val);
Rob Clark16ea9752013-01-08 15:04:28 -060067 mutex_unlock(&dev->mode_config.mutex);
68}
69
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030070static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
Rob Clark16ea9752013-01-08 15:04:28 -060071{
72 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
73 struct drm_device *dev = crtc->dev;
Rob Clark16ea9752013-01-08 15:04:28 -060074 struct drm_gem_cma_object *gem;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030075 dma_addr_t start, end;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030076 u64 dma_base_and_ceiling;
Rob Clark16ea9752013-01-08 15:04:28 -060077
Rob Clark16ea9752013-01-08 15:04:28 -060078 gem = drm_fb_cma_get_gem_obj(fb, 0);
79
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030080 start = gem->paddr + fb->offsets[0] +
81 crtc->y * fb->pitches[0] +
Laurent Pinchart59f11a42016-10-18 01:41:14 +030082 crtc->x * drm_format_plane_cpp(fb->pixel_format, 0);
Rob Clark16ea9752013-01-08 15:04:28 -060083
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030084 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060085
Jyri Sarha7eb9f062016-08-26 15:10:14 +030086 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
87 * with a single insruction, if available. This should make it more
88 * unlikely that LCDC would fetch the DMA addresses in the middle of
89 * an update.
90 */
91 dma_base_and_ceiling = (u64)(end - 1) << 32 | start;
92 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +030093
94 if (tilcdc_crtc->curr_fb)
95 drm_flip_work_queue(&tilcdc_crtc->unref_work,
96 tilcdc_crtc->curr_fb);
97
98 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -060099}
100
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300101static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
102{
103 struct tilcdc_drm_private *priv = dev->dev_private;
104
105 tilcdc_clear_irqstatus(dev, 0xffffffff);
106
107 if (priv->rev == 1) {
108 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
109 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000110 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
111 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300112 } else {
113 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
114 LCDC_V2_UNDERFLOW_INT_ENA |
115 LCDC_V2_END_OF_FRAME0_INT_ENA |
116 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
117 }
118}
119
120static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
121{
122 struct tilcdc_drm_private *priv = dev->dev_private;
123
124 /* disable irqs that we might have enabled: */
125 if (priv->rev == 1) {
126 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
127 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
128 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
129 LCDC_V1_END_OF_FRAME_INT_ENA);
130 } else {
131 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
132 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
133 LCDC_V2_END_OF_FRAME0_INT_ENA |
134 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
135 }
136}
137
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300138static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600139{
140 struct drm_device *dev = crtc->dev;
141 struct tilcdc_drm_private *priv = dev->dev_private;
142
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300143 if (priv->rev != 2)
144 return;
145
146 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
147 usleep_range(250, 1000);
148 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
149}
150
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300151static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300152{
153 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300154 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
155
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300156 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
157
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300158 if (tilcdc_crtc->enabled)
159 return;
160
161 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300162
163 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600164
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300165 tilcdc_crtc_enable_irqs(dev);
166
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300167 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600168 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
169 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300170
171 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300172
173 tilcdc_crtc->enabled = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600174}
175
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300176void tilcdc_crtc_disable(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600177{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300178 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600179 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300180 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600181
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300182 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
183
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300184 if (!tilcdc_crtc->enabled)
185 return;
186
Jyri Sarha2d5be882016-04-07 20:20:23 +0300187 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600188 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300189
190 /*
191 * if necessary wait for framedone irq which will still come
192 * before putting things to sleep..
193 */
194 if (priv->rev == 2) {
195 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
196 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300197 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300198 if (ret == 0)
199 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
200 __func__);
201 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300202
203 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300204
205 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300206
207 pm_runtime_put_sync(dev->dev);
208
209 if (tilcdc_crtc->next_fb) {
210 drm_flip_work_queue(&tilcdc_crtc->unref_work,
211 tilcdc_crtc->next_fb);
212 tilcdc_crtc->next_fb = NULL;
213 }
214
215 if (tilcdc_crtc->curr_fb) {
216 drm_flip_work_queue(&tilcdc_crtc->unref_work,
217 tilcdc_crtc->curr_fb);
218 tilcdc_crtc->curr_fb = NULL;
219 }
220
221 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
222 tilcdc_crtc->last_vblank = ktime_set(0, 0);
223
224 tilcdc_crtc->enabled = false;
225}
226
227static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
228{
229 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600230}
231
232static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
233{
234 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Jyri Sarha4e910c7a2016-09-06 22:55:33 +0300235 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600236
Jyri Sarha6c94c712016-09-07 11:46:40 +0300237 drm_modeset_lock_crtc(crtc, NULL);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300238 tilcdc_crtc_disable(crtc);
Jyri Sarha6c94c712016-09-07 11:46:40 +0300239 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600240
Jyri Sarha4e910c7a2016-09-06 22:55:33 +0300241 flush_workqueue(priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600242
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300243 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600244 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400245 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600246}
247
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300248int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600249 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300250 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600251{
252 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
253 struct drm_device *dev = crtc->dev;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300254 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000255
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300256 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
257
Rob Clark16ea9752013-01-08 15:04:28 -0600258 if (tilcdc_crtc->event) {
259 dev_err(dev->dev, "already pending page flip!\n");
260 return -EBUSY;
261 }
262
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300263 drm_framebuffer_reference(fb);
264
Matt Roperf4510a22014-04-01 15:22:40 -0700265 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300266
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200267 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300268
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300269 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
270 ktime_t next_vblank;
271 s64 tdiff;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300272
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300273 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
274 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200275
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300276 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
277
278 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
279 tilcdc_crtc->next_fb = fb;
280 }
281
282 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200283 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200284
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300285 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200286
287 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600288
289 return 0;
290}
291
Rob Clark16ea9752013-01-08 15:04:28 -0600292static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
293 const struct drm_display_mode *mode,
294 struct drm_display_mode *adjusted_mode)
295{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200296 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
297
298 if (!tilcdc_crtc->simulate_vesa_sync)
299 return true;
300
301 /*
302 * tilcdc does not generate VESA-compliant sync but aligns
303 * VS on the second edge of HS instead of first edge.
304 * We use adjusted_mode, to fixup sync by aligning both rising
305 * edges and add HSKEW offset to fix the sync.
306 */
307 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
308 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
309
310 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
311 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
312 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
313 } else {
314 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
315 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
316 }
317
Rob Clark16ea9752013-01-08 15:04:28 -0600318 return true;
319}
320
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200321/*
322 * Calculate the percentage difference between the requested pixel clock rate
323 * and the effective rate resulting from calculating the clock divider value.
324 */
325static unsigned int tilcdc_pclk_diff(unsigned long rate,
326 unsigned long real_rate)
327{
328 int r = rate / 100, rr = real_rate / 100;
329
330 return (unsigned int)(abs(((rr - r) * 100) / r));
331}
332
Jyri Sarha642e5162016-09-06 16:19:54 +0300333static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
334{
335 struct drm_device *dev = crtc->dev;
336 struct tilcdc_drm_private *priv = dev->dev_private;
337 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200338 unsigned long clk_rate, real_rate, req_rate;
339 unsigned int clkdiv;
Jyri Sarha642e5162016-09-06 16:19:54 +0300340 int ret;
341
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200342 clkdiv = 2; /* first try using a standard divider of 2 */
343
Jyri Sarha642e5162016-09-06 16:19:54 +0300344 /* mode.clock is in KHz, set_rate wants parameter in Hz */
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200345 req_rate = crtc->mode.clock * 1000;
346
347 ret = clk_set_rate(priv->clk, req_rate * clkdiv);
348 clk_rate = clk_get_rate(priv->clk);
Jyri Sarha642e5162016-09-06 16:19:54 +0300349 if (ret < 0) {
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200350 /*
351 * If we fail to set the clock rate (some architectures don't
352 * use the common clock framework yet and may not implement
353 * all the clk API calls for every clock), try the next best
354 * thing: adjusting the clock divider, unless clk_get_rate()
355 * failed as well.
356 */
357 if (!clk_rate) {
358 /* Nothing more we can do. Just bail out. */
359 dev_err(dev->dev,
360 "failed to set the pixel clock - unable to read current lcdc clock rate\n");
361 return;
362 }
363
364 clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);
365
366 /*
367 * Emit a warning if the real clock rate resulting from the
368 * calculated divider differs much from the requested rate.
369 *
370 * 5% is an arbitrary value - LCDs are usually quite tolerant
371 * about pixel clock rates.
372 */
373 real_rate = clkdiv * req_rate;
374
375 if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
376 dev_warn(dev->dev,
377 "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
378 clk_rate, real_rate);
379 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300380 }
381
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200382 tilcdc_crtc->lcd_fck_rate = clk_rate;
Jyri Sarha642e5162016-09-06 16:19:54 +0300383
384 DBG("lcd_clk=%u, mode clock=%d, div=%u",
385 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
386
387 /* Configure the LCD clock divisor. */
388 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
389 LCDC_RASTER_MODE);
390
391 if (priv->rev == 2)
392 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
393 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
394 LCDC_V2_CORE_CLK_EN);
395}
396
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300397static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
398{
399 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
400 struct drm_device *dev = crtc->dev;
401 struct tilcdc_drm_private *priv = dev->dev_private;
402 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
403 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
404 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
405 struct drm_framebuffer *fb = crtc->primary->state->fb;
406
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300407 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
408
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300409 if (WARN_ON(!info))
410 return;
411
412 if (WARN_ON(!fb))
413 return;
414
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300415 /* Configure the Burst Size and fifo threshold of DMA: */
416 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
417 switch (info->dma_burst_sz) {
418 case 1:
419 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
420 break;
421 case 2:
422 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
423 break;
424 case 4:
425 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
426 break;
427 case 8:
428 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
429 break;
430 case 16:
431 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
432 break;
433 default:
434 dev_err(dev->dev, "invalid burst size\n");
435 return;
436 }
437 reg |= (info->fifo_th << 8);
438 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
439
440 /* Configure timings: */
441 hbp = mode->htotal - mode->hsync_end;
442 hfp = mode->hsync_start - mode->hdisplay;
443 hsw = mode->hsync_end - mode->hsync_start;
444 vbp = mode->vtotal - mode->vsync_end;
445 vfp = mode->vsync_start - mode->vdisplay;
446 vsw = mode->vsync_end - mode->vsync_start;
447
448 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
449 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
450
451 /* Set AC Bias Period and Number of Transitions per Interrupt: */
452 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
453 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
454 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
455
456 /*
457 * subtract one from hfp, hbp, hsw because the hardware uses
458 * a value of 0 as 1
459 */
460 if (priv->rev == 2) {
461 /* clear bits we're going to set */
462 reg &= ~0x78000033;
463 reg |= ((hfp-1) & 0x300) >> 8;
464 reg |= ((hbp-1) & 0x300) >> 4;
465 reg |= ((hsw-1) & 0x3c0) << 21;
466 }
467 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
468
469 reg = (((mode->hdisplay >> 4) - 1) << 4) |
470 (((hbp-1) & 0xff) << 24) |
471 (((hfp-1) & 0xff) << 16) |
472 (((hsw-1) & 0x3f) << 10);
473 if (priv->rev == 2)
474 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
475 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
476
477 reg = ((mode->vdisplay - 1) & 0x3ff) |
478 ((vbp & 0xff) << 24) |
479 ((vfp & 0xff) << 16) |
480 (((vsw-1) & 0x3f) << 10);
481 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
482
483 /*
484 * be sure to set Bit 10 for the V2 LCDC controller,
485 * otherwise limited to 1024 pixels width, stopping
486 * 1920x1080 being supported.
487 */
488 if (priv->rev == 2) {
489 if ((mode->vdisplay - 1) & 0x400) {
490 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
491 LCDC_LPP_B10);
492 } else {
493 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
494 LCDC_LPP_B10);
495 }
496 }
497
498 /* Configure display type: */
499 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
500 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
501 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
502 0x000ff000 /* Palette Loading Delay bits */);
503 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
504 if (info->tft_alt_mode)
505 reg |= LCDC_TFT_ALT_ENABLE;
506 if (priv->rev == 2) {
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300507 switch (fb->pixel_format) {
508 case DRM_FORMAT_BGR565:
509 case DRM_FORMAT_RGB565:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300510 break;
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300511 case DRM_FORMAT_XBGR8888:
512 case DRM_FORMAT_XRGB8888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300513 reg |= LCDC_V2_TFT_24BPP_UNPACK;
514 /* fallthrough */
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300515 case DRM_FORMAT_BGR888:
516 case DRM_FORMAT_RGB888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300517 reg |= LCDC_V2_TFT_24BPP_MODE;
518 break;
519 default:
520 dev_err(dev->dev, "invalid pixel format\n");
521 return;
522 }
523 }
524 reg |= info->fdd < 12;
525 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
526
527 if (info->invert_pxl_clk)
528 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
529 else
530 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
531
532 if (info->sync_ctrl)
533 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
534 else
535 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
536
537 if (info->sync_edge)
538 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
539 else
540 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
541
542 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
543 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
544 else
545 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
546
547 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
548 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
549 else
550 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
551
552 if (info->raster_order)
553 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
554 else
555 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
556
557 drm_framebuffer_reference(fb);
558
559 set_scanout(crtc, fb);
560
Jyri Sarha642e5162016-09-06 16:19:54 +0300561 tilcdc_crtc_set_clk(crtc);
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300562
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300563 crtc->hwmode = crtc->state->adjusted_mode;
564}
565
Jyri Sarhadb380c52016-04-07 15:10:23 +0300566static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
567 struct drm_crtc_state *state)
568{
569 struct drm_display_mode *mode = &state->mode;
570 int ret;
571
572 /* If we are not active we don't care */
573 if (!state->active)
574 return 0;
575
576 if (state->state->planes[0].ptr != crtc->primary ||
577 state->state->planes[0].state == NULL ||
578 state->state->planes[0].state->crtc != crtc) {
579 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
580 return -EINVAL;
581 }
582
583 ret = tilcdc_crtc_mode_valid(crtc, mode);
584 if (ret) {
585 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
586 return -EINVAL;
587 }
588
589 return 0;
590}
591
Rob Clark16ea9752013-01-08 15:04:28 -0600592static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300593 .destroy = tilcdc_crtc_destroy,
594 .set_config = drm_atomic_helper_set_config,
595 .page_flip = drm_atomic_helper_page_flip,
596 .reset = drm_atomic_helper_crtc_reset,
597 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
598 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600599};
600
601static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600602 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300603 .enable = tilcdc_crtc_enable,
604 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300605 .atomic_check = tilcdc_crtc_atomic_check,
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300606 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
Rob Clark16ea9752013-01-08 15:04:28 -0600607};
608
609int tilcdc_crtc_max_width(struct drm_crtc *crtc)
610{
611 struct drm_device *dev = crtc->dev;
612 struct tilcdc_drm_private *priv = dev->dev_private;
613 int max_width = 0;
614
615 if (priv->rev == 1)
616 max_width = 1024;
617 else if (priv->rev == 2)
618 max_width = 2048;
619
620 return max_width;
621}
622
623int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
624{
625 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
626 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500627 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600628
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500629 /*
630 * check to see if the width is within the range that
631 * the LCD Controller physically supports
632 */
Rob Clark16ea9752013-01-08 15:04:28 -0600633 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
634 return MODE_VIRTUAL_X;
635
636 /* width must be multiple of 16 */
637 if (mode->hdisplay & 0xf)
638 return MODE_VIRTUAL_X;
639
640 if (mode->vdisplay > 2048)
641 return MODE_VIRTUAL_Y;
642
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500643 DBG("Processing mode %dx%d@%d with pixel clock %d",
644 mode->hdisplay, mode->vdisplay,
645 drm_mode_vrefresh(mode), mode->clock);
646
647 hbp = mode->htotal - mode->hsync_end;
648 hfp = mode->hsync_start - mode->hdisplay;
649 hsw = mode->hsync_end - mode->hsync_start;
650 vbp = mode->vtotal - mode->vsync_end;
651 vfp = mode->vsync_start - mode->vdisplay;
652 vsw = mode->vsync_end - mode->vsync_start;
653
654 if ((hbp-1) & ~0x3ff) {
655 DBG("Pruning mode: Horizontal Back Porch out of range");
656 return MODE_HBLANK_WIDE;
657 }
658
659 if ((hfp-1) & ~0x3ff) {
660 DBG("Pruning mode: Horizontal Front Porch out of range");
661 return MODE_HBLANK_WIDE;
662 }
663
664 if ((hsw-1) & ~0x3ff) {
665 DBG("Pruning mode: Horizontal Sync Width out of range");
666 return MODE_HSYNC_WIDE;
667 }
668
669 if (vbp & ~0xff) {
670 DBG("Pruning mode: Vertical Back Porch out of range");
671 return MODE_VBLANK_WIDE;
672 }
673
674 if (vfp & ~0xff) {
675 DBG("Pruning mode: Vertical Front Porch out of range");
676 return MODE_VBLANK_WIDE;
677 }
678
679 if ((vsw-1) & ~0x3f) {
680 DBG("Pruning mode: Vertical Sync Width out of range");
681 return MODE_VSYNC_WIDE;
682 }
683
Darren Etheridge4e564342013-06-21 13:52:23 -0500684 /*
685 * some devices have a maximum allowed pixel clock
686 * configured from the DT
687 */
688 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500689 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500690 return MODE_CLOCK_HIGH;
691 }
692
693 /*
694 * some devices further limit the max horizontal resolution
695 * configured from the DT
696 */
697 if (mode->hdisplay > priv->max_width)
698 return MODE_BAD_WIDTH;
699
Rob Clark16ea9752013-01-08 15:04:28 -0600700 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500701 bandwidth = mode->hdisplay * mode->vdisplay *
702 drm_mode_vrefresh(mode);
703 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500704 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600705 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500706 }
Rob Clark16ea9752013-01-08 15:04:28 -0600707
708 return MODE_OK;
709}
710
711void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
712 const struct tilcdc_panel_info *info)
713{
714 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
715 tilcdc_crtc->info = info;
716}
717
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200718void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
719 bool simulate_vesa_sync)
720{
721 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
722
723 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
724}
725
Rob Clark16ea9752013-01-08 15:04:28 -0600726void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
727{
Rob Clark16ea9752013-01-08 15:04:28 -0600728 struct drm_device *dev = crtc->dev;
729 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha642e5162016-09-06 16:19:54 +0300730 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600731
Jyri Sarha642e5162016-09-06 16:19:54 +0300732 drm_modeset_lock_crtc(crtc, NULL);
733 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
734 if (tilcdc_crtc_is_on(crtc)) {
735 pm_runtime_get_sync(dev->dev);
736 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600737
Jyri Sarha642e5162016-09-06 16:19:54 +0300738 tilcdc_crtc_set_clk(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600739
Jyri Sarha642e5162016-09-06 16:19:54 +0300740 tilcdc_crtc_enable(crtc);
741 pm_runtime_put_sync(dev->dev);
742 }
Rob Clark16ea9752013-01-08 15:04:28 -0600743 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300744 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600745}
746
Jyri Sarha5895d082016-01-08 14:33:09 +0200747#define SYNC_LOST_COUNT_LIMIT 50
748
Rob Clark16ea9752013-01-08 15:04:28 -0600749irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
750{
751 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
752 struct drm_device *dev = crtc->dev;
753 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300754 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600755
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300756 stat = tilcdc_read_irqstatus(dev);
757 tilcdc_clear_irqstatus(dev, stat);
758
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300759 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600760 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200761 bool skip_event = false;
762 ktime_t now;
763
764 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600765
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300766 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600767
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200768 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600769
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200770 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600771
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200772 if (tilcdc_crtc->next_fb) {
773 set_scanout(crtc, tilcdc_crtc->next_fb);
774 tilcdc_crtc->next_fb = NULL;
775 skip_event = true;
Tomi Valkeinen2b2080d72015-10-20 09:37:27 +0300776 }
777
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200778 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
779
Gustavo Padovan099ede82016-07-04 21:04:52 -0300780 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200781
782 if (!skip_event) {
783 struct drm_pending_vblank_event *event;
784
785 spin_lock_irqsave(&dev->event_lock, flags);
786
787 event = tilcdc_crtc->event;
788 tilcdc_crtc->event = NULL;
789 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700790 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200791
792 spin_unlock_irqrestore(&dev->event_lock, flags);
793 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200794
795 if (tilcdc_crtc->frame_intact)
796 tilcdc_crtc->sync_lost_count = 0;
797 else
798 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600799 }
800
Jyri Sarha14944112016-04-07 20:36:48 +0300801 if (stat & LCDC_FIFO_UNDERFLOW)
802 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
803 __func__, stat);
804
805 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600806 if (priv->rev == 2) {
807 if (stat & LCDC_FRAME_DONE) {
808 tilcdc_crtc->frame_done = true;
809 wake_up(&tilcdc_crtc->frame_done_wq);
810 }
Rob Clark16ea9752013-01-08 15:04:28 -0600811
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300812 if (stat & LCDC_SYNC_LOST) {
813 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
814 __func__, stat);
815 tilcdc_crtc->frame_intact = false;
816 if (tilcdc_crtc->sync_lost_count++ >
817 SYNC_LOST_COUNT_LIMIT) {
818 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
819 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
820 LCDC_SYNC_LOST);
821 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200822 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200823
Jyri Sarha14944112016-04-07 20:36:48 +0300824 /* Indicate to LCDC that the interrupt service routine has
825 * completed, see 13.3.6.1.6 in AM335x TRM.
826 */
827 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
828 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200829
Rob Clark16ea9752013-01-08 15:04:28 -0600830 return IRQ_HANDLED;
831}
832
Rob Clark16ea9752013-01-08 15:04:28 -0600833struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
834{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300835 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600836 struct tilcdc_crtc *tilcdc_crtc;
837 struct drm_crtc *crtc;
838 int ret;
839
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200840 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600841 if (!tilcdc_crtc) {
842 dev_err(dev->dev, "allocation failed\n");
843 return NULL;
844 }
845
846 crtc = &tilcdc_crtc->base;
847
Jyri Sarha47f571c2016-04-07 15:04:18 +0300848 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
849 if (ret < 0)
850 goto fail;
851
Rob Clark16ea9752013-01-08 15:04:28 -0600852 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
853
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100854 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400855 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600856
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200857 spin_lock_init(&tilcdc_crtc->irq_lock);
858
Jyri Sarha47f571c2016-04-07 15:04:18 +0300859 ret = drm_crtc_init_with_planes(dev, crtc,
860 &tilcdc_crtc->primary,
861 NULL,
862 &tilcdc_crtc_funcs,
863 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -0600864 if (ret < 0)
865 goto fail;
866
867 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
868
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300869 if (priv->is_componentized) {
870 struct device_node *ports =
871 of_get_child_by_name(dev->dev->of_node, "ports");
872
873 if (ports) {
874 crtc->port = of_get_child_by_name(ports, "port");
875 of_node_put(ports);
876 } else {
877 crtc->port =
878 of_get_child_by_name(dev->dev->of_node, "port");
879 }
880 if (!crtc->port) { /* This should never happen */
881 dev_err(dev->dev, "Port node not found in %s\n",
882 dev->dev->of_node->full_name);
883 goto fail;
884 }
885 }
886
Rob Clark16ea9752013-01-08 15:04:28 -0600887 return crtc;
888
889fail:
890 tilcdc_crtc_destroy(crtc);
891 return NULL;
892}