2 * Copyright (C) 2016 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
5 * Copyright (C) 2014 Endless Mobile
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * Jasper St. Pierre <jstpierre@mecheye.net>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/platform_device.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_flip_work.h>
32 #include <drm/drm_crtc_helper.h>
34 #include "meson_crtc.h"
35 #include "meson_plane.h"
36 #include "meson_venc.h"
37 #include "meson_vpp.h"
38 #include "meson_viu.h"
39 #include "meson_canvas.h"
40 #include "meson_registers.h"
46 struct drm_pending_vblank_event
*event
;
47 struct meson_drm
*priv
;
50 #define to_meson_crtc(x) container_of(x, struct meson_crtc, base)
54 static int meson_crtc_enable_vblank(struct drm_crtc
*crtc
)
56 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
57 struct meson_drm
*priv
= meson_crtc
->priv
;
59 meson_venc_enable_vsync(priv
);
64 static void meson_crtc_disable_vblank(struct drm_crtc
*crtc
)
66 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
67 struct meson_drm
*priv
= meson_crtc
->priv
;
69 meson_venc_disable_vsync(priv
);
72 static const struct drm_crtc_funcs meson_crtc_funcs
= {
73 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
74 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
75 .destroy
= drm_crtc_cleanup
,
76 .page_flip
= drm_atomic_helper_page_flip
,
77 .reset
= drm_atomic_helper_crtc_reset
,
78 .set_config
= drm_atomic_helper_set_config
,
79 .enable_vblank
= meson_crtc_enable_vblank
,
80 .disable_vblank
= meson_crtc_disable_vblank
,
84 static void meson_crtc_enable(struct drm_crtc
*crtc
)
86 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
87 struct drm_crtc_state
*crtc_state
= crtc
->state
;
88 struct meson_drm
*priv
= meson_crtc
->priv
;
90 DRM_DEBUG_DRIVER("\n");
93 DRM_ERROR("Invalid crtc_state\n");
97 /* Enable VPP Postblend */
98 writel(crtc_state
->mode
.hdisplay
,
99 priv
->io_base
+ _REG(VPP_POSTBLEND_H_SIZE
));
101 writel_bits_relaxed(VPP_POSTBLEND_ENABLE
, VPP_POSTBLEND_ENABLE
,
102 priv
->io_base
+ _REG(VPP_MISC
));
104 drm_crtc_vblank_on(crtc
);
106 meson_crtc
->enabled
= true;
109 static void meson_crtc_atomic_enable(struct drm_crtc
*crtc
,
110 struct drm_crtc_state
*old_state
)
112 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
113 struct meson_drm
*priv
= meson_crtc
->priv
;
115 DRM_DEBUG_DRIVER("\n");
117 if (!meson_crtc
->enabled
)
118 meson_crtc_enable(crtc
);
120 priv
->viu
.osd1_enabled
= true;
123 static void meson_crtc_atomic_disable(struct drm_crtc
*crtc
,
124 struct drm_crtc_state
*old_state
)
126 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
127 struct meson_drm
*priv
= meson_crtc
->priv
;
129 drm_crtc_vblank_off(crtc
);
131 priv
->viu
.osd1_enabled
= false;
132 priv
->viu
.osd1_commit
= false;
134 /* Disable VPP Postblend */
135 writel_bits_relaxed(VPP_POSTBLEND_ENABLE
, 0,
136 priv
->io_base
+ _REG(VPP_MISC
));
138 if (crtc
->state
->event
&& !crtc
->state
->active
) {
139 spin_lock_irq(&crtc
->dev
->event_lock
);
140 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
141 spin_unlock_irq(&crtc
->dev
->event_lock
);
143 crtc
->state
->event
= NULL
;
146 meson_crtc
->enabled
= false;
149 static void meson_crtc_atomic_begin(struct drm_crtc
*crtc
,
150 struct drm_crtc_state
*state
)
152 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
155 if (crtc
->state
->enable
&& !meson_crtc
->enabled
)
156 meson_crtc_enable(crtc
);
158 if (crtc
->state
->event
) {
159 WARN_ON(drm_crtc_vblank_get(crtc
) != 0);
161 spin_lock_irqsave(&crtc
->dev
->event_lock
, flags
);
162 meson_crtc
->event
= crtc
->state
->event
;
163 spin_unlock_irqrestore(&crtc
->dev
->event_lock
, flags
);
164 crtc
->state
->event
= NULL
;
168 static void meson_crtc_atomic_flush(struct drm_crtc
*crtc
,
169 struct drm_crtc_state
*old_crtc_state
)
171 struct meson_crtc
*meson_crtc
= to_meson_crtc(crtc
);
172 struct meson_drm
*priv
= meson_crtc
->priv
;
174 priv
->viu
.osd1_commit
= true;
177 static const struct drm_crtc_helper_funcs meson_crtc_helper_funcs
= {
178 .atomic_begin
= meson_crtc_atomic_begin
,
179 .atomic_flush
= meson_crtc_atomic_flush
,
180 .atomic_enable
= meson_crtc_atomic_enable
,
181 .atomic_disable
= meson_crtc_atomic_disable
,
184 void meson_crtc_irq(struct meson_drm
*priv
)
186 struct meson_crtc
*meson_crtc
= to_meson_crtc(priv
->crtc
);
189 /* Update the OSD registers */
190 if (priv
->viu
.osd1_enabled
&& priv
->viu
.osd1_commit
) {
191 writel_relaxed(priv
->viu
.osd1_ctrl_stat
,
192 priv
->io_base
+ _REG(VIU_OSD1_CTRL_STAT
));
193 writel_relaxed(priv
->viu
.osd1_blk0_cfg
[0],
194 priv
->io_base
+ _REG(VIU_OSD1_BLK0_CFG_W0
));
195 writel_relaxed(priv
->viu
.osd1_blk0_cfg
[1],
196 priv
->io_base
+ _REG(VIU_OSD1_BLK0_CFG_W1
));
197 writel_relaxed(priv
->viu
.osd1_blk0_cfg
[2],
198 priv
->io_base
+ _REG(VIU_OSD1_BLK0_CFG_W2
));
199 writel_relaxed(priv
->viu
.osd1_blk0_cfg
[3],
200 priv
->io_base
+ _REG(VIU_OSD1_BLK0_CFG_W3
));
201 writel_relaxed(priv
->viu
.osd1_blk0_cfg
[4],
202 priv
->io_base
+ _REG(VIU_OSD1_BLK0_CFG_W4
));
204 /* If output is interlace, make use of the Scaler */
205 if (priv
->viu
.osd1_interlace
) {
206 struct drm_plane
*plane
= priv
->primary_plane
;
207 struct drm_plane_state
*state
= plane
->state
;
208 struct drm_rect dest
= {
211 .x2
= state
->crtc_x
+ state
->crtc_w
,
212 .y2
= state
->crtc_y
+ state
->crtc_h
,
215 meson_vpp_setup_interlace_vscaler_osd1(priv
, &dest
);
217 meson_vpp_disable_interlace_vscaler_osd1(priv
);
219 meson_canvas_setup(priv
, MESON_CANVAS_ID_OSD1
,
220 priv
->viu
.osd1_addr
, priv
->viu
.osd1_stride
,
221 priv
->viu
.osd1_height
, MESON_CANVAS_WRAP_NONE
,
222 MESON_CANVAS_BLKMODE_LINEAR
);
225 writel_bits_relaxed(VPP_OSD1_POSTBLEND
, VPP_OSD1_POSTBLEND
,
226 priv
->io_base
+ _REG(VPP_MISC
));
228 priv
->viu
.osd1_commit
= false;
231 drm_crtc_handle_vblank(priv
->crtc
);
233 spin_lock_irqsave(&priv
->drm
->event_lock
, flags
);
234 if (meson_crtc
->event
) {
235 drm_crtc_send_vblank_event(priv
->crtc
, meson_crtc
->event
);
236 drm_crtc_vblank_put(priv
->crtc
);
237 meson_crtc
->event
= NULL
;
239 spin_unlock_irqrestore(&priv
->drm
->event_lock
, flags
);
242 int meson_crtc_create(struct meson_drm
*priv
)
244 struct meson_crtc
*meson_crtc
;
245 struct drm_crtc
*crtc
;
248 meson_crtc
= devm_kzalloc(priv
->drm
->dev
, sizeof(*meson_crtc
),
253 meson_crtc
->priv
= priv
;
254 crtc
= &meson_crtc
->base
;
255 ret
= drm_crtc_init_with_planes(priv
->drm
, crtc
,
256 priv
->primary_plane
, NULL
,
257 &meson_crtc_funcs
, "meson_crtc");
259 dev_err(priv
->drm
->dev
, "Failed to init CRTC\n");
263 drm_crtc_helper_add(crtc
, &meson_crtc_helper_funcs
);