1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Free Electrons
4 * Copyright (C) 2015 NextThing Co
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
9 #include <linux/clk-provider.h>
10 #include <linux/ioport.h>
11 #include <linux/of_address.h>
12 #include <linux/of_graph.h>
13 #include <linux/of_irq.h>
14 #include <linux/regmap.h>
16 #include <video/videomode.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_modes.h>
21 #include <drm/drm_print.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_vblank.h>
25 #include "sun4i_backend.h"
26 #include "sun4i_crtc.h"
27 #include "sun4i_drv.h"
28 #include "sunxi_engine.h"
29 #include "sun4i_tcon.h"
32 * While this isn't really working in the DRM theory, in practice we
33 * can only ever have one encoder per TCON since we have a mux in our
36 static struct drm_encoder
*sun4i_crtc_get_encoder(struct drm_crtc
*crtc
)
38 struct drm_encoder
*encoder
;
40 drm_for_each_encoder(encoder
, crtc
->dev
)
41 if (encoder
->crtc
== crtc
)
47 static int sun4i_crtc_atomic_check(struct drm_crtc
*crtc
,
48 struct drm_crtc_state
*state
)
50 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
51 struct sunxi_engine
*engine
= scrtc
->engine
;
54 if (engine
&& engine
->ops
&& engine
->ops
->atomic_check
)
55 ret
= engine
->ops
->atomic_check(engine
, state
);
60 static void sun4i_crtc_atomic_begin(struct drm_crtc
*crtc
,
61 struct drm_crtc_state
*old_state
)
63 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
64 struct drm_device
*dev
= crtc
->dev
;
65 struct sunxi_engine
*engine
= scrtc
->engine
;
68 if (crtc
->state
->event
) {
69 WARN_ON(drm_crtc_vblank_get(crtc
) != 0);
71 spin_lock_irqsave(&dev
->event_lock
, flags
);
72 scrtc
->event
= crtc
->state
->event
;
73 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
74 crtc
->state
->event
= NULL
;
77 if (engine
->ops
->atomic_begin
)
78 engine
->ops
->atomic_begin(engine
, old_state
);
81 static void sun4i_crtc_atomic_flush(struct drm_crtc
*crtc
,
82 struct drm_crtc_state
*old_state
)
84 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
85 struct drm_pending_vblank_event
*event
= crtc
->state
->event
;
87 DRM_DEBUG_DRIVER("Committing plane changes\n");
89 sunxi_engine_commit(scrtc
->engine
);
92 crtc
->state
->event
= NULL
;
94 spin_lock_irq(&crtc
->dev
->event_lock
);
95 if (drm_crtc_vblank_get(crtc
) == 0)
96 drm_crtc_arm_vblank_event(crtc
, event
);
98 drm_crtc_send_vblank_event(crtc
, event
);
99 spin_unlock_irq(&crtc
->dev
->event_lock
);
103 static void sun4i_crtc_atomic_disable(struct drm_crtc
*crtc
,
104 struct drm_crtc_state
*old_state
)
106 struct drm_encoder
*encoder
= sun4i_crtc_get_encoder(crtc
);
107 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
109 DRM_DEBUG_DRIVER("Disabling the CRTC\n");
111 drm_crtc_vblank_off(crtc
);
113 sun4i_tcon_set_status(scrtc
->tcon
, encoder
, false);
115 if (crtc
->state
->event
&& !crtc
->state
->active
) {
116 spin_lock_irq(&crtc
->dev
->event_lock
);
117 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
118 spin_unlock_irq(&crtc
->dev
->event_lock
);
120 crtc
->state
->event
= NULL
;
124 static void sun4i_crtc_atomic_enable(struct drm_crtc
*crtc
,
125 struct drm_crtc_state
*old_state
)
127 struct drm_encoder
*encoder
= sun4i_crtc_get_encoder(crtc
);
128 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
130 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
132 sun4i_tcon_set_status(scrtc
->tcon
, encoder
, true);
134 drm_crtc_vblank_on(crtc
);
137 static void sun4i_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
139 struct drm_display_mode
*mode
= &crtc
->state
->adjusted_mode
;
140 struct drm_encoder
*encoder
= sun4i_crtc_get_encoder(crtc
);
141 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
143 sun4i_tcon_mode_set(scrtc
->tcon
, encoder
, mode
);
146 static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs
= {
147 .atomic_check
= sun4i_crtc_atomic_check
,
148 .atomic_begin
= sun4i_crtc_atomic_begin
,
149 .atomic_flush
= sun4i_crtc_atomic_flush
,
150 .atomic_enable
= sun4i_crtc_atomic_enable
,
151 .atomic_disable
= sun4i_crtc_atomic_disable
,
152 .mode_set_nofb
= sun4i_crtc_mode_set_nofb
,
155 static int sun4i_crtc_enable_vblank(struct drm_crtc
*crtc
)
157 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
159 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc
);
161 sun4i_tcon_enable_vblank(scrtc
->tcon
, true);
166 static void sun4i_crtc_disable_vblank(struct drm_crtc
*crtc
)
168 struct sun4i_crtc
*scrtc
= drm_crtc_to_sun4i_crtc(crtc
);
170 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc
);
172 sun4i_tcon_enable_vblank(scrtc
->tcon
, false);
175 static const struct drm_crtc_funcs sun4i_crtc_funcs
= {
176 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
177 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
178 .destroy
= drm_crtc_cleanup
,
179 .page_flip
= drm_atomic_helper_page_flip
,
180 .reset
= drm_atomic_helper_crtc_reset
,
181 .set_config
= drm_atomic_helper_set_config
,
182 .enable_vblank
= sun4i_crtc_enable_vblank
,
183 .disable_vblank
= sun4i_crtc_disable_vblank
,
186 struct sun4i_crtc
*sun4i_crtc_init(struct drm_device
*drm
,
187 struct sunxi_engine
*engine
,
188 struct sun4i_tcon
*tcon
)
190 struct sun4i_crtc
*scrtc
;
191 struct drm_plane
**planes
;
192 struct drm_plane
*primary
= NULL
, *cursor
= NULL
;
195 scrtc
= devm_kzalloc(drm
->dev
, sizeof(*scrtc
), GFP_KERNEL
);
197 return ERR_PTR(-ENOMEM
);
198 scrtc
->engine
= engine
;
201 /* Create our layers */
202 planes
= sunxi_engine_layers_init(drm
, engine
);
203 if (IS_ERR(planes
)) {
204 dev_err(drm
->dev
, "Couldn't create the planes\n");
208 /* find primary and cursor planes for drm_crtc_init_with_planes */
209 for (i
= 0; planes
[i
]; i
++) {
210 struct drm_plane
*plane
= planes
[i
];
212 switch (plane
->type
) {
213 case DRM_PLANE_TYPE_PRIMARY
:
216 case DRM_PLANE_TYPE_CURSOR
:
224 ret
= drm_crtc_init_with_planes(drm
, &scrtc
->crtc
,
230 dev_err(drm
->dev
, "Couldn't init DRM CRTC\n");
234 drm_crtc_helper_add(&scrtc
->crtc
, &sun4i_crtc_helper_funcs
);
236 /* Set crtc.port to output port node of the tcon */
237 scrtc
->crtc
.port
= of_graph_get_port_by_id(scrtc
->tcon
->dev
->of_node
,
240 /* Set possible_crtcs to this crtc for overlay planes */
241 for (i
= 0; planes
[i
]; i
++) {
242 uint32_t possible_crtcs
= drm_crtc_mask(&scrtc
->crtc
);
243 struct drm_plane
*plane
= planes
[i
];
245 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
246 plane
->possible_crtcs
= possible_crtcs
;