Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / sun4i / sun4i_crtc.c
blob5decae0069d0bfda0f45a0c78ea033f1186282c6
1 /*
2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
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
10 * the License, or (at your option) any later version.
13 #include <drm/drmP.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_modes.h>
19 #include <linux/clk-provider.h>
20 #include <linux/ioport.h>
21 #include <linux/of_address.h>
22 #include <linux/of_graph.h>
23 #include <linux/of_irq.h>
24 #include <linux/regmap.h>
26 #include <video/videomode.h>
28 #include "sun4i_crtc.h"
29 #include "sun4i_drv.h"
30 #include "sunxi_engine.h"
31 #include "sun4i_tcon.h"
34 * While this isn't really working in the DRM theory, in practice we
35 * can only ever have one encoder per TCON since we have a mux in our
36 * TCON.
38 static struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc)
40 struct drm_encoder *encoder;
42 drm_for_each_encoder(encoder, crtc->dev)
43 if (encoder->crtc == crtc)
44 return encoder;
46 return NULL;
49 static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
50 struct drm_crtc_state *old_state)
52 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
53 struct drm_device *dev = crtc->dev;
54 unsigned long flags;
56 if (crtc->state->event) {
57 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
59 spin_lock_irqsave(&dev->event_lock, flags);
60 scrtc->event = crtc->state->event;
61 spin_unlock_irqrestore(&dev->event_lock, flags);
62 crtc->state->event = NULL;
66 static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
67 struct drm_crtc_state *old_state)
69 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
70 struct drm_pending_vblank_event *event = crtc->state->event;
72 DRM_DEBUG_DRIVER("Committing plane changes\n");
74 sunxi_engine_commit(scrtc->engine);
76 if (event) {
77 crtc->state->event = NULL;
79 spin_lock_irq(&crtc->dev->event_lock);
80 if (drm_crtc_vblank_get(crtc) == 0)
81 drm_crtc_arm_vblank_event(crtc, event);
82 else
83 drm_crtc_send_vblank_event(crtc, event);
84 spin_unlock_irq(&crtc->dev->event_lock);
88 static void sun4i_crtc_atomic_disable(struct drm_crtc *crtc,
89 struct drm_crtc_state *old_state)
91 struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
92 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
94 DRM_DEBUG_DRIVER("Disabling the CRTC\n");
96 sun4i_tcon_set_status(scrtc->tcon, encoder, false);
98 if (crtc->state->event && !crtc->state->active) {
99 spin_lock_irq(&crtc->dev->event_lock);
100 drm_crtc_send_vblank_event(crtc, crtc->state->event);
101 spin_unlock_irq(&crtc->dev->event_lock);
103 crtc->state->event = NULL;
107 static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
108 struct drm_crtc_state *old_state)
110 struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
111 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
113 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
115 sun4i_tcon_set_status(scrtc->tcon, encoder, true);
118 static void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc)
120 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
121 struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
122 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
124 sun4i_tcon_mode_set(scrtc->tcon, encoder, mode);
127 static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
128 .atomic_begin = sun4i_crtc_atomic_begin,
129 .atomic_flush = sun4i_crtc_atomic_flush,
130 .atomic_enable = sun4i_crtc_atomic_enable,
131 .atomic_disable = sun4i_crtc_atomic_disable,
132 .mode_set_nofb = sun4i_crtc_mode_set_nofb,
135 static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
137 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
139 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
141 sun4i_tcon_enable_vblank(scrtc->tcon, true);
143 return 0;
146 static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
148 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
150 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
152 sun4i_tcon_enable_vblank(scrtc->tcon, false);
155 static const struct drm_crtc_funcs sun4i_crtc_funcs = {
156 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
157 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
158 .destroy = drm_crtc_cleanup,
159 .page_flip = drm_atomic_helper_page_flip,
160 .reset = drm_atomic_helper_crtc_reset,
161 .set_config = drm_atomic_helper_set_config,
162 .enable_vblank = sun4i_crtc_enable_vblank,
163 .disable_vblank = sun4i_crtc_disable_vblank,
166 struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
167 struct sunxi_engine *engine,
168 struct sun4i_tcon *tcon)
170 struct sun4i_crtc *scrtc;
171 struct drm_plane **planes;
172 struct drm_plane *primary = NULL, *cursor = NULL;
173 int ret, i;
175 scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
176 if (!scrtc)
177 return ERR_PTR(-ENOMEM);
178 scrtc->engine = engine;
179 scrtc->tcon = tcon;
181 /* Create our layers */
182 planes = sunxi_engine_layers_init(drm, engine);
183 if (IS_ERR(planes)) {
184 dev_err(drm->dev, "Couldn't create the planes\n");
185 return NULL;
188 /* find primary and cursor planes for drm_crtc_init_with_planes */
189 for (i = 0; planes[i]; i++) {
190 struct drm_plane *plane = planes[i];
192 switch (plane->type) {
193 case DRM_PLANE_TYPE_PRIMARY:
194 primary = plane;
195 break;
196 case DRM_PLANE_TYPE_CURSOR:
197 cursor = plane;
198 break;
199 default:
200 break;
204 ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
205 primary,
206 cursor,
207 &sun4i_crtc_funcs,
208 NULL);
209 if (ret) {
210 dev_err(drm->dev, "Couldn't init DRM CRTC\n");
211 return ERR_PTR(ret);
214 drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
216 /* Set crtc.port to output port node of the tcon */
217 scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
220 /* Set possible_crtcs to this crtc for overlay planes */
221 for (i = 0; planes[i]; i++) {
222 uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
223 struct drm_plane *plane = planes[i];
225 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
226 plane->possible_crtcs = possible_crtcs;
229 return scrtc;