blob: 62773633ef5a32315e9d1b6b760601d33d6ba544 [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 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 Valkeinen2b2080d2015-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 Valkeinen2b2080d2015-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;
Daniel Schultz4c268d62016-10-28 13:52:41 +020074 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -060075 struct drm_gem_cma_object *gem;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030076 dma_addr_t start, end;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030077 u64 dma_base_and_ceiling;
Rob Clark16ea9752013-01-08 15:04:28 -060078
Rob Clark16ea9752013-01-08 15:04:28 -060079 gem = drm_fb_cma_get_gem_obj(fb, 0);
80
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030081 start = gem->paddr + fb->offsets[0] +
82 crtc->y * fb->pitches[0] +
Laurent Pinchart59f11a42016-10-18 01:41:14 +030083 crtc->x * drm_format_plane_cpp(fb->pixel_format, 0);
Rob Clark16ea9752013-01-08 15:04:28 -060084
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030085 end = start + (crtc->mode.vdisplay * fb->pitches[0]);
Rob Clark16ea9752013-01-08 15:04:28 -060086
Jyri Sarha7eb9f062016-08-26 15:10:14 +030087 /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
88 * with a single insruction, if available. This should make it more
89 * unlikely that LCDC would fetch the DMA addresses in the middle of
90 * an update.
91 */
Daniel Schultz4c268d62016-10-28 13:52:41 +020092 if (priv->rev == 1)
93 end -= 1;
94
95 dma_base_and_ceiling = (u64)end << 32 | start;
Jyri Sarha7eb9f062016-08-26 15:10:14 +030096 tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +030097
98 if (tilcdc_crtc->curr_fb)
99 drm_flip_work_queue(&tilcdc_crtc->unref_work,
100 tilcdc_crtc->curr_fb);
101
102 tilcdc_crtc->curr_fb = fb;
Rob Clark16ea9752013-01-08 15:04:28 -0600103}
104
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300105static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
106{
107 struct tilcdc_drm_private *priv = dev->dev_private;
108
109 tilcdc_clear_irqstatus(dev, 0xffffffff);
110
111 if (priv->rev == 1) {
112 tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
113 LCDC_V1_UNDERFLOW_INT_ENA);
Karl Beldan8d6c3f72016-08-23 12:57:00 +0000114 tilcdc_set(dev, LCDC_DMA_CTRL_REG,
115 LCDC_V1_END_OF_FRAME_INT_ENA);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300116 } else {
117 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
118 LCDC_V2_UNDERFLOW_INT_ENA |
119 LCDC_V2_END_OF_FRAME0_INT_ENA |
120 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
121 }
122}
123
124static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
125{
126 struct tilcdc_drm_private *priv = dev->dev_private;
127
128 /* disable irqs that we might have enabled: */
129 if (priv->rev == 1) {
130 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
131 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
132 tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
133 LCDC_V1_END_OF_FRAME_INT_ENA);
134 } else {
135 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
136 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
137 LCDC_V2_END_OF_FRAME0_INT_ENA |
138 LCDC_FRAME_DONE | LCDC_SYNC_LOST);
139 }
140}
141
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300142static void reset(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600143{
144 struct drm_device *dev = crtc->dev;
145 struct tilcdc_drm_private *priv = dev->dev_private;
146
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300147 if (priv->rev != 2)
148 return;
149
150 tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
151 usleep_range(250, 1000);
152 tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
153}
154
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300155static void tilcdc_crtc_enable(struct drm_crtc *crtc)
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300156{
157 struct drm_device *dev = crtc->dev;
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300158 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
159
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300160 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
161
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300162 if (tilcdc_crtc->enabled)
163 return;
164
165 pm_runtime_get_sync(dev->dev);
Tomi Valkeinen2efec4f2015-10-20 09:37:27 +0300166
167 reset(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600168
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300169 tilcdc_crtc_enable_irqs(dev);
170
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300171 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
Rob Clark16ea9752013-01-08 15:04:28 -0600172 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
173 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300174
175 drm_crtc_vblank_on(crtc);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300176
177 tilcdc_crtc->enabled = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600178}
179
Jyri Sarha9e79e062016-10-18 23:23:27 +0300180void tilcdc_crtc_off(struct drm_crtc *crtc)
Rob Clark16ea9752013-01-08 15:04:28 -0600181{
Jyri Sarha2d5be882016-04-07 20:20:23 +0300182 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600183 struct drm_device *dev = crtc->dev;
Jyri Sarha2d5be882016-04-07 20:20:23 +0300184 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600185
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300186 if (!tilcdc_crtc->enabled)
187 return;
188
Jyri Sarha2d5be882016-04-07 20:20:23 +0300189 tilcdc_crtc->frame_done = false;
Rob Clark16ea9752013-01-08 15:04:28 -0600190 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
Jyri Sarha2d5be882016-04-07 20:20:23 +0300191
192 /*
193 * if necessary wait for framedone irq which will still come
194 * before putting things to sleep..
195 */
196 if (priv->rev == 2) {
197 int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
198 tilcdc_crtc->frame_done,
Jyri Sarha437c7d92016-06-16 16:19:17 +0300199 msecs_to_jiffies(500));
Jyri Sarha2d5be882016-04-07 20:20:23 +0300200 if (ret == 0)
201 dev_err(dev->dev, "%s: timeout waiting for framedone\n",
202 __func__);
203 }
Jyri Sarhad85f850e2016-06-15 11:16:23 +0300204
205 drm_crtc_vblank_off(crtc);
Jyri Sarhaafaf8332016-06-21 16:00:44 +0300206
207 tilcdc_crtc_disable_irqs(dev);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300208
209 pm_runtime_put_sync(dev->dev);
210
211 if (tilcdc_crtc->next_fb) {
212 drm_flip_work_queue(&tilcdc_crtc->unref_work,
213 tilcdc_crtc->next_fb);
214 tilcdc_crtc->next_fb = NULL;
215 }
216
217 if (tilcdc_crtc->curr_fb) {
218 drm_flip_work_queue(&tilcdc_crtc->unref_work,
219 tilcdc_crtc->curr_fb);
220 tilcdc_crtc->curr_fb = NULL;
221 }
222
223 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
224 tilcdc_crtc->last_vblank = ktime_set(0, 0);
225
226 tilcdc_crtc->enabled = false;
227}
228
Jyri Sarha9e79e062016-10-18 23:23:27 +0300229static void tilcdc_crtc_disable(struct drm_crtc *crtc)
230{
231 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
232 tilcdc_crtc_off(crtc);
233}
234
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300235static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
236{
237 return crtc->state && crtc->state->enable && crtc->state->active;
Rob Clark16ea9752013-01-08 15:04:28 -0600238}
239
240static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
241{
242 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Jyri Sarha4e910c72016-09-06 22:55:33 +0300243 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600244
Jyri Sarha6c94c712016-09-07 11:46:40 +0300245 drm_modeset_lock_crtc(crtc, NULL);
Jyri Sarha47bfd6c2016-06-22 16:27:54 +0300246 tilcdc_crtc_disable(crtc);
Jyri Sarha6c94c712016-09-07 11:46:40 +0300247 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600248
Jyri Sarha4e910c72016-09-06 22:55:33 +0300249 flush_workqueue(priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600250
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300251 of_node_put(crtc->port);
Rob Clark16ea9752013-01-08 15:04:28 -0600252 drm_crtc_cleanup(crtc);
Rob Clarka464d612013-08-07 13:41:20 -0400253 drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
Rob Clark16ea9752013-01-08 15:04:28 -0600254}
255
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300256int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
Rob Clark16ea9752013-01-08 15:04:28 -0600257 struct drm_framebuffer *fb,
Jyri Sarhae0e344e2016-06-22 17:21:06 +0300258 struct drm_pending_vblank_event *event)
Rob Clark16ea9752013-01-08 15:04:28 -0600259{
260 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
261 struct drm_device *dev = crtc->dev;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300262 unsigned long flags;
Tomi Valkeinen6f206e92014-02-07 17:37:07 +0000263
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300264 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
265
Rob Clark16ea9752013-01-08 15:04:28 -0600266 if (tilcdc_crtc->event) {
267 dev_err(dev->dev, "already pending page flip!\n");
268 return -EBUSY;
269 }
270
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300271 drm_framebuffer_reference(fb);
272
Matt Roperf4510a22014-04-01 15:22:40 -0700273 crtc->primary->fb = fb;
Tomi Valkeinen65734a22015-10-19 12:30:03 +0300274
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200275 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300276
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300277 if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
278 ktime_t next_vblank;
279 s64 tdiff;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300280
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300281 next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
282 1000000 / crtc->hwmode.vrefresh);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200283
Jyri Sarha0a1fe1b2016-06-13 09:53:36 +0300284 tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
285
286 if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
287 tilcdc_crtc->next_fb = fb;
288 }
289
290 if (tilcdc_crtc->next_fb != fb)
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200291 set_scanout(crtc, fb);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200292
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300293 tilcdc_crtc->event = event;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200294
295 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600296
297 return 0;
298}
299
Rob Clark16ea9752013-01-08 15:04:28 -0600300static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
301 const struct drm_display_mode *mode,
302 struct drm_display_mode *adjusted_mode)
303{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200304 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
305
306 if (!tilcdc_crtc->simulate_vesa_sync)
307 return true;
308
309 /*
310 * tilcdc does not generate VESA-compliant sync but aligns
311 * VS on the second edge of HS instead of first edge.
312 * We use adjusted_mode, to fixup sync by aligning both rising
313 * edges and add HSKEW offset to fix the sync.
314 */
315 adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
316 adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
317
318 if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
319 adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
320 adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
321 } else {
322 adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
323 adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
324 }
325
Rob Clark16ea9752013-01-08 15:04:28 -0600326 return true;
327}
328
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200329/*
330 * Calculate the percentage difference between the requested pixel clock rate
331 * and the effective rate resulting from calculating the clock divider value.
332 */
333static unsigned int tilcdc_pclk_diff(unsigned long rate,
334 unsigned long real_rate)
335{
336 int r = rate / 100, rr = real_rate / 100;
337
338 return (unsigned int)(abs(((rr - r) * 100) / r));
339}
340
Jyri Sarha642e5162016-09-06 16:19:54 +0300341static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
342{
343 struct drm_device *dev = crtc->dev;
344 struct tilcdc_drm_private *priv = dev->dev_private;
345 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200346 unsigned long clk_rate, real_rate, req_rate;
347 unsigned int clkdiv;
Jyri Sarha642e5162016-09-06 16:19:54 +0300348 int ret;
349
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200350 clkdiv = 2; /* first try using a standard divider of 2 */
351
Jyri Sarha642e5162016-09-06 16:19:54 +0300352 /* mode.clock is in KHz, set_rate wants parameter in Hz */
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200353 req_rate = crtc->mode.clock * 1000;
354
355 ret = clk_set_rate(priv->clk, req_rate * clkdiv);
356 clk_rate = clk_get_rate(priv->clk);
Jyri Sarha642e5162016-09-06 16:19:54 +0300357 if (ret < 0) {
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200358 /*
359 * If we fail to set the clock rate (some architectures don't
360 * use the common clock framework yet and may not implement
361 * all the clk API calls for every clock), try the next best
362 * thing: adjusting the clock divider, unless clk_get_rate()
363 * failed as well.
364 */
365 if (!clk_rate) {
366 /* Nothing more we can do. Just bail out. */
367 dev_err(dev->dev,
368 "failed to set the pixel clock - unable to read current lcdc clock rate\n");
369 return;
370 }
371
372 clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);
373
374 /*
375 * Emit a warning if the real clock rate resulting from the
376 * calculated divider differs much from the requested rate.
377 *
378 * 5% is an arbitrary value - LCDs are usually quite tolerant
379 * about pixel clock rates.
380 */
381 real_rate = clkdiv * req_rate;
382
383 if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
384 dev_warn(dev->dev,
385 "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
386 clk_rate, real_rate);
387 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300388 }
389
Bartosz Golaszewskicb42e202016-09-29 18:43:57 +0200390 tilcdc_crtc->lcd_fck_rate = clk_rate;
Jyri Sarha642e5162016-09-06 16:19:54 +0300391
392 DBG("lcd_clk=%u, mode clock=%d, div=%u",
393 tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
394
395 /* Configure the LCD clock divisor. */
396 tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
397 LCDC_RASTER_MODE);
398
399 if (priv->rev == 2)
400 tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
401 LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
402 LCDC_V2_CORE_CLK_EN);
403}
404
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300405static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
406{
407 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
408 struct drm_device *dev = crtc->dev;
409 struct tilcdc_drm_private *priv = dev->dev_private;
410 const struct tilcdc_panel_info *info = tilcdc_crtc->info;
411 uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
412 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
413 struct drm_framebuffer *fb = crtc->primary->state->fb;
414
Jyri Sarha2e0965b2016-09-06 17:25:08 +0300415 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
416
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300417 if (WARN_ON(!info))
418 return;
419
420 if (WARN_ON(!fb))
421 return;
422
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300423 /* Configure the Burst Size and fifo threshold of DMA: */
424 reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
425 switch (info->dma_burst_sz) {
426 case 1:
427 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
428 break;
429 case 2:
430 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
431 break;
432 case 4:
433 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
434 break;
435 case 8:
436 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
437 break;
438 case 16:
439 reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
440 break;
441 default:
442 dev_err(dev->dev, "invalid burst size\n");
443 return;
444 }
445 reg |= (info->fifo_th << 8);
446 tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
447
448 /* Configure timings: */
449 hbp = mode->htotal - mode->hsync_end;
450 hfp = mode->hsync_start - mode->hdisplay;
451 hsw = mode->hsync_end - mode->hsync_start;
452 vbp = mode->vtotal - mode->vsync_end;
453 vfp = mode->vsync_start - mode->vdisplay;
454 vsw = mode->vsync_end - mode->vsync_start;
455
456 DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
457 mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
458
459 /* Set AC Bias Period and Number of Transitions per Interrupt: */
460 reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
461 reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
462 LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
463
464 /*
465 * subtract one from hfp, hbp, hsw because the hardware uses
466 * a value of 0 as 1
467 */
468 if (priv->rev == 2) {
469 /* clear bits we're going to set */
470 reg &= ~0x78000033;
471 reg |= ((hfp-1) & 0x300) >> 8;
472 reg |= ((hbp-1) & 0x300) >> 4;
473 reg |= ((hsw-1) & 0x3c0) << 21;
474 }
475 tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
476
477 reg = (((mode->hdisplay >> 4) - 1) << 4) |
478 (((hbp-1) & 0xff) << 24) |
479 (((hfp-1) & 0xff) << 16) |
480 (((hsw-1) & 0x3f) << 10);
481 if (priv->rev == 2)
482 reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
483 tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
484
485 reg = ((mode->vdisplay - 1) & 0x3ff) |
486 ((vbp & 0xff) << 24) |
487 ((vfp & 0xff) << 16) |
488 (((vsw-1) & 0x3f) << 10);
489 tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
490
491 /*
492 * be sure to set Bit 10 for the V2 LCDC controller,
493 * otherwise limited to 1024 pixels width, stopping
494 * 1920x1080 being supported.
495 */
496 if (priv->rev == 2) {
497 if ((mode->vdisplay - 1) & 0x400) {
498 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
499 LCDC_LPP_B10);
500 } else {
501 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
502 LCDC_LPP_B10);
503 }
504 }
505
506 /* Configure display type: */
507 reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
508 ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
509 LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
510 0x000ff000 /* Palette Loading Delay bits */);
511 reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
512 if (info->tft_alt_mode)
513 reg |= LCDC_TFT_ALT_ENABLE;
514 if (priv->rev == 2) {
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300515 switch (fb->pixel_format) {
516 case DRM_FORMAT_BGR565:
517 case DRM_FORMAT_RGB565:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300518 break;
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300519 case DRM_FORMAT_XBGR8888:
520 case DRM_FORMAT_XRGB8888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300521 reg |= LCDC_V2_TFT_24BPP_UNPACK;
522 /* fallthrough */
Laurent Pinchart59f11a42016-10-18 01:41:14 +0300523 case DRM_FORMAT_BGR888:
524 case DRM_FORMAT_RGB888:
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300525 reg |= LCDC_V2_TFT_24BPP_MODE;
526 break;
527 default:
528 dev_err(dev->dev, "invalid pixel format\n");
529 return;
530 }
531 }
532 reg |= info->fdd < 12;
533 tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
534
535 if (info->invert_pxl_clk)
536 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
537 else
538 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
539
540 if (info->sync_ctrl)
541 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
542 else
543 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
544
545 if (info->sync_edge)
546 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
547 else
548 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
549
550 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
551 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
552 else
553 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
554
555 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
556 tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
557 else
558 tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
559
560 if (info->raster_order)
561 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
562 else
563 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
564
565 drm_framebuffer_reference(fb);
566
567 set_scanout(crtc, fb);
568
Jyri Sarha642e5162016-09-06 16:19:54 +0300569 tilcdc_crtc_set_clk(crtc);
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300570
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300571 crtc->hwmode = crtc->state->adjusted_mode;
572}
573
Jyri Sarhadb380c52016-04-07 15:10:23 +0300574static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
575 struct drm_crtc_state *state)
576{
577 struct drm_display_mode *mode = &state->mode;
578 int ret;
579
580 /* If we are not active we don't care */
581 if (!state->active)
582 return 0;
583
584 if (state->state->planes[0].ptr != crtc->primary ||
585 state->state->planes[0].state == NULL ||
586 state->state->planes[0].state->crtc != crtc) {
587 dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
588 return -EINVAL;
589 }
590
591 ret = tilcdc_crtc_mode_valid(crtc, mode);
592 if (ret) {
593 dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
594 return -EINVAL;
595 }
596
597 return 0;
598}
599
Rob Clark16ea9752013-01-08 15:04:28 -0600600static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
Jyri Sarha305198d2016-04-07 15:05:16 +0300601 .destroy = tilcdc_crtc_destroy,
602 .set_config = drm_atomic_helper_set_config,
603 .page_flip = drm_atomic_helper_page_flip,
604 .reset = drm_atomic_helper_crtc_reset,
605 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
606 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600607};
608
609static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
Rob Clark16ea9752013-01-08 15:04:28 -0600610 .mode_fixup = tilcdc_crtc_mode_fixup,
Jyri Sarha305198d2016-04-07 15:05:16 +0300611 .enable = tilcdc_crtc_enable,
612 .disable = tilcdc_crtc_disable,
Jyri Sarhadb380c52016-04-07 15:10:23 +0300613 .atomic_check = tilcdc_crtc_atomic_check,
Jyri Sarhaf6382f12016-04-07 15:09:50 +0300614 .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
Rob Clark16ea9752013-01-08 15:04:28 -0600615};
616
617int tilcdc_crtc_max_width(struct drm_crtc *crtc)
618{
619 struct drm_device *dev = crtc->dev;
620 struct tilcdc_drm_private *priv = dev->dev_private;
621 int max_width = 0;
622
623 if (priv->rev == 1)
624 max_width = 1024;
625 else if (priv->rev == 2)
626 max_width = 2048;
627
628 return max_width;
629}
630
631int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
632{
633 struct tilcdc_drm_private *priv = crtc->dev->dev_private;
634 unsigned int bandwidth;
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500635 uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
Rob Clark16ea9752013-01-08 15:04:28 -0600636
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500637 /*
638 * check to see if the width is within the range that
639 * the LCD Controller physically supports
640 */
Rob Clark16ea9752013-01-08 15:04:28 -0600641 if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
642 return MODE_VIRTUAL_X;
643
644 /* width must be multiple of 16 */
645 if (mode->hdisplay & 0xf)
646 return MODE_VIRTUAL_X;
647
648 if (mode->vdisplay > 2048)
649 return MODE_VIRTUAL_Y;
650
Darren Etheridgee1c5d0a2013-06-21 13:52:25 -0500651 DBG("Processing mode %dx%d@%d with pixel clock %d",
652 mode->hdisplay, mode->vdisplay,
653 drm_mode_vrefresh(mode), mode->clock);
654
655 hbp = mode->htotal - mode->hsync_end;
656 hfp = mode->hsync_start - mode->hdisplay;
657 hsw = mode->hsync_end - mode->hsync_start;
658 vbp = mode->vtotal - mode->vsync_end;
659 vfp = mode->vsync_start - mode->vdisplay;
660 vsw = mode->vsync_end - mode->vsync_start;
661
662 if ((hbp-1) & ~0x3ff) {
663 DBG("Pruning mode: Horizontal Back Porch out of range");
664 return MODE_HBLANK_WIDE;
665 }
666
667 if ((hfp-1) & ~0x3ff) {
668 DBG("Pruning mode: Horizontal Front Porch out of range");
669 return MODE_HBLANK_WIDE;
670 }
671
672 if ((hsw-1) & ~0x3ff) {
673 DBG("Pruning mode: Horizontal Sync Width out of range");
674 return MODE_HSYNC_WIDE;
675 }
676
677 if (vbp & ~0xff) {
678 DBG("Pruning mode: Vertical Back Porch out of range");
679 return MODE_VBLANK_WIDE;
680 }
681
682 if (vfp & ~0xff) {
683 DBG("Pruning mode: Vertical Front Porch out of range");
684 return MODE_VBLANK_WIDE;
685 }
686
687 if ((vsw-1) & ~0x3f) {
688 DBG("Pruning mode: Vertical Sync Width out of range");
689 return MODE_VSYNC_WIDE;
690 }
691
Darren Etheridge4e564342013-06-21 13:52:23 -0500692 /*
693 * some devices have a maximum allowed pixel clock
694 * configured from the DT
695 */
696 if (mode->clock > priv->max_pixelclock) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500697 DBG("Pruning mode: pixel clock too high");
Darren Etheridge4e564342013-06-21 13:52:23 -0500698 return MODE_CLOCK_HIGH;
699 }
700
701 /*
702 * some devices further limit the max horizontal resolution
703 * configured from the DT
704 */
705 if (mode->hdisplay > priv->max_width)
706 return MODE_BAD_WIDTH;
707
Rob Clark16ea9752013-01-08 15:04:28 -0600708 /* filter out modes that would require too much memory bandwidth: */
Darren Etheridge4e564342013-06-21 13:52:23 -0500709 bandwidth = mode->hdisplay * mode->vdisplay *
710 drm_mode_vrefresh(mode);
711 if (bandwidth > priv->max_bandwidth) {
Darren Etheridgef7b45752013-06-21 13:52:26 -0500712 DBG("Pruning mode: exceeds defined bandwidth limit");
Rob Clark16ea9752013-01-08 15:04:28 -0600713 return MODE_BAD;
Darren Etheridge4e564342013-06-21 13:52:23 -0500714 }
Rob Clark16ea9752013-01-08 15:04:28 -0600715
716 return MODE_OK;
717}
718
719void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
720 const struct tilcdc_panel_info *info)
721{
722 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
723 tilcdc_crtc->info = info;
724}
725
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200726void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
727 bool simulate_vesa_sync)
728{
729 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
730
731 tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
732}
733
Rob Clark16ea9752013-01-08 15:04:28 -0600734void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
735{
Rob Clark16ea9752013-01-08 15:04:28 -0600736 struct drm_device *dev = crtc->dev;
737 struct tilcdc_drm_private *priv = dev->dev_private;
Jyri Sarha642e5162016-09-06 16:19:54 +0300738 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600739
Jyri Sarha642e5162016-09-06 16:19:54 +0300740 drm_modeset_lock_crtc(crtc, NULL);
741 if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
742 if (tilcdc_crtc_is_on(crtc)) {
743 pm_runtime_get_sync(dev->dev);
744 tilcdc_crtc_disable(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600745
Jyri Sarha642e5162016-09-06 16:19:54 +0300746 tilcdc_crtc_set_clk(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600747
Jyri Sarha642e5162016-09-06 16:19:54 +0300748 tilcdc_crtc_enable(crtc);
749 pm_runtime_put_sync(dev->dev);
750 }
Rob Clark16ea9752013-01-08 15:04:28 -0600751 }
Jyri Sarha642e5162016-09-06 16:19:54 +0300752 drm_modeset_unlock_crtc(crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600753}
754
Jyri Sarha5895d082016-01-08 14:33:09 +0200755#define SYNC_LOST_COUNT_LIMIT 50
756
Rob Clark16ea9752013-01-08 15:04:28 -0600757irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
758{
759 struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
760 struct drm_device *dev = crtc->dev;
761 struct tilcdc_drm_private *priv = dev->dev_private;
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300762 uint32_t stat;
Rob Clark16ea9752013-01-08 15:04:28 -0600763
Tomi Valkeinen317aae72015-10-20 12:08:03 +0300764 stat = tilcdc_read_irqstatus(dev);
765 tilcdc_clear_irqstatus(dev, stat);
766
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300767 if (stat & LCDC_END_OF_FRAME0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600768 unsigned long flags;
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200769 bool skip_event = false;
770 ktime_t now;
771
772 now = ktime_get();
Rob Clark16ea9752013-01-08 15:04:28 -0600773
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300774 drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
Rob Clark16ea9752013-01-08 15:04:28 -0600775
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200776 spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
Rob Clark16ea9752013-01-08 15:04:28 -0600777
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200778 tilcdc_crtc->last_vblank = now;
Rob Clark16ea9752013-01-08 15:04:28 -0600779
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200780 if (tilcdc_crtc->next_fb) {
781 set_scanout(crtc, tilcdc_crtc->next_fb);
782 tilcdc_crtc->next_fb = NULL;
783 skip_event = true;
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300784 }
785
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200786 spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
787
Gustavo Padovan099ede82016-07-04 21:04:52 -0300788 drm_crtc_handle_vblank(crtc);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200789
790 if (!skip_event) {
791 struct drm_pending_vblank_event *event;
792
793 spin_lock_irqsave(&dev->event_lock, flags);
794
795 event = tilcdc_crtc->event;
796 tilcdc_crtc->event = NULL;
797 if (event)
Gustavo Padovandfebc152016-04-14 10:48:22 -0700798 drm_crtc_send_vblank_event(crtc, event);
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200799
800 spin_unlock_irqrestore(&dev->event_lock, flags);
801 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200802
803 if (tilcdc_crtc->frame_intact)
804 tilcdc_crtc->sync_lost_count = 0;
805 else
806 tilcdc_crtc->frame_intact = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600807 }
808
Jyri Sarha14944112016-04-07 20:36:48 +0300809 if (stat & LCDC_FIFO_UNDERFLOW)
Daniel Schultzd7014532016-10-28 13:52:42 +0200810 dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underflow",
Jyri Sarha14944112016-04-07 20:36:48 +0300811 __func__, stat);
812
813 /* For revision 2 only */
Rob Clark16ea9752013-01-08 15:04:28 -0600814 if (priv->rev == 2) {
815 if (stat & LCDC_FRAME_DONE) {
816 tilcdc_crtc->frame_done = true;
817 wake_up(&tilcdc_crtc->frame_done_wq);
818 }
Rob Clark16ea9752013-01-08 15:04:28 -0600819
Jyri Sarha1abcdac2016-06-17 11:54:06 +0300820 if (stat & LCDC_SYNC_LOST) {
821 dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
822 __func__, stat);
823 tilcdc_crtc->frame_intact = false;
824 if (tilcdc_crtc->sync_lost_count++ >
825 SYNC_LOST_COUNT_LIMIT) {
826 dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
827 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
828 LCDC_SYNC_LOST);
829 }
Jyri Sarha5895d082016-01-08 14:33:09 +0200830 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200831
Jyri Sarha14944112016-04-07 20:36:48 +0300832 /* Indicate to LCDC that the interrupt service routine has
833 * completed, see 13.3.6.1.6 in AM335x TRM.
834 */
835 tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
836 }
Jyri Sarhac0c2baa2015-12-18 13:07:52 +0200837
Rob Clark16ea9752013-01-08 15:04:28 -0600838 return IRQ_HANDLED;
839}
840
Rob Clark16ea9752013-01-08 15:04:28 -0600841struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
842{
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300843 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600844 struct tilcdc_crtc *tilcdc_crtc;
845 struct drm_crtc *crtc;
846 int ret;
847
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200848 tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600849 if (!tilcdc_crtc) {
850 dev_err(dev->dev, "allocation failed\n");
851 return NULL;
852 }
853
854 crtc = &tilcdc_crtc->base;
855
Jyri Sarha47f571c2016-04-07 15:04:18 +0300856 ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
857 if (ret < 0)
858 goto fail;
859
Rob Clark16ea9752013-01-08 15:04:28 -0600860 init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
861
Boris BREZILLONd7f8db52014-11-14 19:30:30 +0100862 drm_flip_work_init(&tilcdc_crtc->unref_work,
Rob Clarka464d612013-08-07 13:41:20 -0400863 "unref", unref_worker);
Rob Clark16ea9752013-01-08 15:04:28 -0600864
Tomi Valkeinen2b3a8cd2015-11-03 12:00:51 +0200865 spin_lock_init(&tilcdc_crtc->irq_lock);
866
Jyri Sarha47f571c2016-04-07 15:04:18 +0300867 ret = drm_crtc_init_with_planes(dev, crtc,
868 &tilcdc_crtc->primary,
869 NULL,
870 &tilcdc_crtc_funcs,
871 "tilcdc crtc");
Rob Clark16ea9752013-01-08 15:04:28 -0600872 if (ret < 0)
873 goto fail;
874
875 drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
876
Jyri Sarhad66284fb2015-05-27 11:58:37 +0300877 if (priv->is_componentized) {
878 struct device_node *ports =
879 of_get_child_by_name(dev->dev->of_node, "ports");
880
881 if (ports) {
882 crtc->port = of_get_child_by_name(ports, "port");
883 of_node_put(ports);
884 } else {
885 crtc->port =
886 of_get_child_by_name(dev->dev->of_node, "port");
887 }
888 if (!crtc->port) { /* This should never happen */
889 dev_err(dev->dev, "Port node not found in %s\n",
890 dev->dev->of_node->full_name);
891 goto fail;
892 }
893 }
894
Rob Clark16ea9752013-01-08 15:04:28 -0600895 return crtc;
896
897fail:
898 tilcdc_crtc_destroy(crtc);
899 return NULL;
900}