1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_crtc.c
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5  * Authors:
6  *	Inki Dae <inki.dae@samsung.com>
7  *	Joonyoung Shim <jy0922.shim@samsung.com>
8  *	Seung-Woo Kim <sw0312.kim@samsung.com>
9  */
10 
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_encoder.h>
14 #include <drm/drm_probe_helper.h>
15 #include <drm/drm_vblank.h>
16 
17 #include "exynos_drm_crtc.h"
18 #include "exynos_drm_drv.h"
19 #include "exynos_drm_plane.h"
20 
21 static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
22 					  struct drm_atomic_state *state)
23 {
24 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
25 
26 	if (exynos_crtc->ops->atomic_enable)
27 		exynos_crtc->ops->atomic_enable(exynos_crtc);
28 
29 	drm_crtc_vblank_on(crtc);
30 }
31 
32 static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc,
33 					   struct drm_atomic_state *state)
34 {
35 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
36 
37 	drm_crtc_vblank_off(crtc);
38 
39 	if (exynos_crtc->ops->atomic_disable)
40 		exynos_crtc->ops->atomic_disable(exynos_crtc);
41 
42 	if (crtc->state->event && !crtc->state->active) {
43 		spin_lock_irq(&crtc->dev->event_lock);
44 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
45 		spin_unlock_irq(&crtc->dev->event_lock);
46 
47 		crtc->state->event = NULL;
48 	}
49 }
50 
51 static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
52 				     struct drm_atomic_state *state)
53 {
54 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
55 									  crtc);
56 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
57 
58 	if (!crtc_state->enable)
59 		return 0;
60 
61 	if (exynos_crtc->ops->atomic_check)
62 		return exynos_crtc->ops->atomic_check(exynos_crtc, crtc_state);
63 
64 	return 0;
65 }
66 
67 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
68 				     struct drm_atomic_state *state)
69 {
70 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
71 
72 	if (exynos_crtc->ops->atomic_begin)
73 		exynos_crtc->ops->atomic_begin(exynos_crtc);
74 }
75 
76 static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
77 				     struct drm_atomic_state *state)
78 {
79 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
80 
81 	if (exynos_crtc->ops->atomic_flush)
82 		exynos_crtc->ops->atomic_flush(exynos_crtc);
83 }
84 
85 static enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc,
86 	const struct drm_display_mode *mode)
87 {
88 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
89 
90 	if (exynos_crtc->ops->mode_valid)
91 		return exynos_crtc->ops->mode_valid(exynos_crtc, mode);
92 
93 	return MODE_OK;
94 }
95 
96 static bool exynos_crtc_mode_fixup(struct drm_crtc *crtc,
97 		const struct drm_display_mode *mode,
98 		struct drm_display_mode *adjusted_mode)
99 {
100 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
101 
102 	if (exynos_crtc->ops->mode_fixup)
103 		return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
104 				adjusted_mode);
105 
106 	return true;
107 }
108 
109 
110 static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
111 	.mode_valid	= exynos_crtc_mode_valid,
112 	.mode_fixup	= exynos_crtc_mode_fixup,
113 	.atomic_check	= exynos_crtc_atomic_check,
114 	.atomic_begin	= exynos_crtc_atomic_begin,
115 	.atomic_flush	= exynos_crtc_atomic_flush,
116 	.atomic_enable	= exynos_drm_crtc_atomic_enable,
117 	.atomic_disable	= exynos_drm_crtc_atomic_disable,
118 };
119 
120 void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
121 {
122 	struct drm_crtc *crtc = &exynos_crtc->base;
123 	struct drm_pending_vblank_event *event = crtc->state->event;
124 	unsigned long flags;
125 
126 	if (!event)
127 		return;
128 	crtc->state->event = NULL;
129 
130 	WARN_ON(drm_crtc_vblank_get(crtc) != 0);
131 
132 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
133 	drm_crtc_arm_vblank_event(crtc, event);
134 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
135 }
136 
137 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
138 {
139 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
140 
141 	drm_crtc_cleanup(crtc);
142 	kfree(exynos_crtc);
143 }
144 
145 static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
146 {
147 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
148 
149 	if (exynos_crtc->ops->enable_vblank)
150 		return exynos_crtc->ops->enable_vblank(exynos_crtc);
151 
152 	return 0;
153 }
154 
155 static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
156 {
157 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
158 
159 	if (exynos_crtc->ops->disable_vblank)
160 		exynos_crtc->ops->disable_vblank(exynos_crtc);
161 }
162 
163 static const struct drm_crtc_funcs exynos_crtc_funcs = {
164 	.set_config	= drm_atomic_helper_set_config,
165 	.page_flip	= drm_atomic_helper_page_flip,
166 	.destroy	= exynos_drm_crtc_destroy,
167 	.reset = drm_atomic_helper_crtc_reset,
168 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
169 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
170 	.enable_vblank = exynos_drm_crtc_enable_vblank,
171 	.disable_vblank = exynos_drm_crtc_disable_vblank,
172 };
173 
174 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
175 					struct drm_plane *plane,
176 					enum exynos_drm_output_type type,
177 					const struct exynos_drm_crtc_ops *ops,
178 					void *ctx)
179 {
180 	struct exynos_drm_crtc *exynos_crtc;
181 	struct drm_crtc *crtc;
182 	int ret;
183 
184 	exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
185 	if (!exynos_crtc)
186 		return ERR_PTR(-ENOMEM);
187 
188 	exynos_crtc->type = type;
189 	exynos_crtc->ops = ops;
190 	exynos_crtc->ctx = ctx;
191 
192 	crtc = &exynos_crtc->base;
193 
194 	ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
195 					&exynos_crtc_funcs, NULL);
196 	if (ret < 0)
197 		goto err_crtc;
198 
199 	drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
200 
201 	return exynos_crtc;
202 
203 err_crtc:
204 	plane->funcs->destroy(plane);
205 	kfree(exynos_crtc);
206 	return ERR_PTR(ret);
207 }
208 
209 struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
210 				       enum exynos_drm_output_type out_type)
211 {
212 	struct drm_crtc *crtc;
213 
214 	drm_for_each_crtc(crtc, drm_dev)
215 		if (to_exynos_crtc(crtc)->type == out_type)
216 			return to_exynos_crtc(crtc);
217 
218 	return ERR_PTR(-ENODEV);
219 }
220 
221 int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
222 		enum exynos_drm_output_type out_type)
223 {
224 	struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
225 						out_type);
226 
227 	if (IS_ERR(crtc))
228 		return PTR_ERR(crtc);
229 
230 	encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
231 
232 	return 0;
233 }
234 
235 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
236 {
237 	struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
238 
239 	if (exynos_crtc->ops->te_handler)
240 		exynos_crtc->ops->te_handler(exynos_crtc);
241 }
242