drm/sti: rename files and functions
[deliverable/linux.git] / drivers / gpu / drm / sti / sti_crtc.c
CommitLineData
9bbf86fe
BG
1/*
2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
4 * Fabien Dessenne <fabien.dessenne@st.com>
5 * for STMicroelectronics.
6 * License terms: GNU General Public License (GPL), version 2
7 */
8
9#include <linux/clk.h>
10
11#include <drm/drmP.h>
de4b00b0
BG
12#include <drm/drm_atomic.h>
13#include <drm/drm_atomic_helper.h>
9bbf86fe 14#include <drm/drm_crtc_helper.h>
3cb9ae4f 15#include <drm/drm_plane_helper.h>
9bbf86fe
BG
16
17#include "sti_compositor.h"
9e1f05b2
VA
18#include "sti_crtc.h"
19#include "sti_drv.h"
9bbf86fe
BG
20#include "sti_vtg.h"
21
9e1f05b2 22static void sti_crtc_dpms(struct drm_crtc *crtc, int mode)
9bbf86fe
BG
23{
24 DRM_DEBUG_KMS("\n");
25}
26
9e1f05b2 27static void sti_crtc_prepare(struct drm_crtc *crtc)
9bbf86fe
BG
28{
29 struct sti_mixer *mixer = to_sti_mixer(crtc);
30 struct device *dev = mixer->dev;
31 struct sti_compositor *compo = dev_get_drvdata(dev);
32
5e03abc5 33 mixer->enabled = true;
9bbf86fe
BG
34
35 /* Prepare and enable the compo IP clock */
36 if (mixer->id == STI_MIXER_MAIN) {
37 if (clk_prepare_enable(compo->clk_compo_main))
38 DRM_INFO("Failed to prepare/enable compo_main clk\n");
39 } else {
40 if (clk_prepare_enable(compo->clk_compo_aux))
41 DRM_INFO("Failed to prepare/enable compo_aux clk\n");
42 }
2f7d0e82 43
871bcdfe 44 sti_mixer_clear_all_planes(mixer);
9bbf86fe
BG
45}
46
9e1f05b2 47static void sti_crtc_commit(struct drm_crtc *crtc)
9bbf86fe
BG
48{
49 struct sti_mixer *mixer = to_sti_mixer(crtc);
50 struct device *dev = mixer->dev;
51 struct sti_compositor *compo = dev_get_drvdata(dev);
871bcdfe 52 struct sti_plane *plane;
9bbf86fe
BG
53
54 if ((!mixer || !compo)) {
871bcdfe 55 DRM_ERROR("Can't find mixer or compositor)\n");
9bbf86fe
BG
56 return;
57 }
58
59 /* get GDP which is reserved to the CRTC FB */
871bcdfe
VA
60 plane = to_sti_plane(crtc->primary);
61 if (!plane)
62 DRM_ERROR("Can't find CRTC dedicated plane (GDP0)\n");
9bbf86fe 63
871bcdfe
VA
64 /* Enable plane on mixer */
65 if (sti_mixer_set_plane_status(mixer, plane, true))
66 DRM_ERROR("Cannot enable plane at mixer\n");
ca614aad
BG
67
68 drm_crtc_vblank_on(crtc);
9bbf86fe
BG
69}
70
9e1f05b2
VA
71static bool sti_crtc_mode_fixup(struct drm_crtc *crtc,
72 const struct drm_display_mode *mode,
73 struct drm_display_mode *adjusted_mode)
9bbf86fe
BG
74{
75 /* accept the provided drm_display_mode, do not fix it up */
76 return true;
77}
78
79static int
9e1f05b2 80sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
9bbf86fe
BG
81{
82 struct sti_mixer *mixer = to_sti_mixer(crtc);
83 struct device *dev = mixer->dev;
84 struct sti_compositor *compo = dev_get_drvdata(dev);
9bbf86fe
BG
85 struct clk *clk;
86 int rate = mode->clock * 1000;
87 int res;
9bbf86fe 88
de4b00b0 89 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
9bbf86fe 90 crtc->base.id, sti_mixer_to_str(mixer),
de4b00b0 91 mode->base.id, mode->name);
9bbf86fe
BG
92
93 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
94 mode->vrefresh, mode->clock,
95 mode->hdisplay,
96 mode->hsync_start, mode->hsync_end,
97 mode->htotal,
98 mode->vdisplay,
99 mode->vsync_start, mode->vsync_end,
100 mode->vtotal, mode->type, mode->flags);
101
102 /* Set rate and prepare/enable pixel clock */
103 if (mixer->id == STI_MIXER_MAIN)
104 clk = compo->clk_pix_main;
105 else
106 clk = compo->clk_pix_aux;
107
108 res = clk_set_rate(clk, rate);
109 if (res < 0) {
110 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
111 return -EINVAL;
112 }
113 if (clk_prepare_enable(clk)) {
114 DRM_ERROR("Failed to prepare/enable pix clk\n");
115 return -EINVAL;
116 }
117
118 sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
119 compo->vtg_main : compo->vtg_aux, &crtc->mode);
120
9bbf86fe
BG
121 res = sti_mixer_active_video_area(mixer, &crtc->mode);
122 if (res) {
871bcdfe 123 DRM_ERROR("Can't set active video area\n");
9bbf86fe
BG
124 return -EINVAL;
125 }
126
de4b00b0 127 return res;
9bbf86fe
BG
128}
129
9e1f05b2 130static void sti_crtc_disable(struct drm_crtc *crtc)
9bbf86fe
BG
131{
132 struct sti_mixer *mixer = to_sti_mixer(crtc);
133 struct device *dev = mixer->dev;
134 struct sti_compositor *compo = dev_get_drvdata(dev);
9bbf86fe 135
5e03abc5 136 if (!mixer->enabled)
9bbf86fe
BG
137 return;
138
139 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
140
141 /* Disable Background */
142 sti_mixer_set_background_status(mixer, false);
143
ca614aad 144 drm_crtc_vblank_off(crtc);
9bbf86fe
BG
145
146 /* Disable pixel clock and compo IP clocks */
147 if (mixer->id == STI_MIXER_MAIN) {
148 clk_disable_unprepare(compo->clk_pix_main);
149 clk_disable_unprepare(compo->clk_compo_main);
150 } else {
151 clk_disable_unprepare(compo->clk_pix_aux);
152 clk_disable_unprepare(compo->clk_compo_aux);
153 }
154
5e03abc5 155 mixer->enabled = false;
9bbf86fe
BG
156}
157
de4b00b0 158static void
9e1f05b2 159sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
de4b00b0 160{
9e1f05b2
VA
161 sti_crtc_prepare(crtc);
162 sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
de4b00b0 163}
9bbf86fe 164
9e1f05b2 165static void sti_crtc_atomic_begin(struct drm_crtc *crtc)
9bbf86fe 166{
9bbf86fe 167 struct sti_mixer *mixer = to_sti_mixer(crtc);
9bbf86fe 168
de4b00b0
BG
169 if (crtc->state->event) {
170 crtc->state->event->pipe = drm_crtc_index(crtc);
9bbf86fe 171
de4b00b0 172 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
9bbf86fe 173
de4b00b0
BG
174 mixer->pending_event = crtc->state->event;
175 crtc->state->event = NULL;
9bbf86fe 176 }
de4b00b0 177}
9bbf86fe 178
9e1f05b2 179static void sti_crtc_atomic_flush(struct drm_crtc *crtc)
de4b00b0 180{
9bbf86fe
BG
181}
182
de4b00b0 183static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
9e1f05b2
VA
184 .dpms = sti_crtc_dpms,
185 .prepare = sti_crtc_prepare,
186 .commit = sti_crtc_commit,
187 .mode_fixup = sti_crtc_mode_fixup,
de4b00b0 188 .mode_set = drm_helper_crtc_mode_set,
9e1f05b2 189 .mode_set_nofb = sti_crtc_mode_set_nofb,
de4b00b0 190 .mode_set_base = drm_helper_crtc_mode_set_base,
9e1f05b2
VA
191 .disable = sti_crtc_disable,
192 .atomic_begin = sti_crtc_atomic_begin,
193 .atomic_flush = sti_crtc_atomic_flush,
de4b00b0
BG
194};
195
9e1f05b2 196static void sti_crtc_destroy(struct drm_crtc *crtc)
9bbf86fe
BG
197{
198 DRM_DEBUG_KMS("\n");
199 drm_crtc_cleanup(crtc);
200}
201
9e1f05b2
VA
202static int sti_crtc_set_property(struct drm_crtc *crtc,
203 struct drm_property *property,
204 uint64_t val)
9bbf86fe
BG
205{
206 DRM_DEBUG_KMS("\n");
207 return 0;
208}
209
9e1f05b2
VA
210int sti_crtc_vblank_cb(struct notifier_block *nb,
211 unsigned long event, void *data)
9bbf86fe
BG
212{
213 struct drm_device *drm_dev;
214 struct sti_compositor *compo =
215 container_of(nb, struct sti_compositor, vtg_vblank_nb);
216 int *crtc = data;
217 unsigned long flags;
9e1f05b2 218 struct sti_private *priv;
9bbf86fe
BG
219
220 drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
221 priv = drm_dev->dev_private;
222
223 if ((event != VTG_TOP_FIELD_EVENT) &&
224 (event != VTG_BOTTOM_FIELD_EVENT)) {
225 DRM_ERROR("unknown event: %lu\n", event);
226 return -EINVAL;
227 }
228
229 drm_handle_vblank(drm_dev, *crtc);
230
231 spin_lock_irqsave(&drm_dev->event_lock, flags);
232 if (compo->mixer[*crtc]->pending_event) {
233 drm_send_vblank_event(drm_dev, -1,
9e1f05b2 234 compo->mixer[*crtc]->pending_event);
9bbf86fe
BG
235 drm_vblank_put(drm_dev, *crtc);
236 compo->mixer[*crtc]->pending_event = NULL;
237 }
238 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
239
240 return 0;
241}
242
9e1f05b2 243int sti_crtc_enable_vblank(struct drm_device *dev, int crtc)
9bbf86fe 244{
9e1f05b2 245 struct sti_private *dev_priv = dev->dev_private;
9bbf86fe
BG
246 struct sti_compositor *compo = dev_priv->compo;
247 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
248
871bcdfe
VA
249 DRM_DEBUG_DRIVER("\n");
250
9bbf86fe
BG
251 if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
252 compo->vtg_main : compo->vtg_aux,
253 vtg_vblank_nb, crtc)) {
254 DRM_ERROR("Cannot register VTG notifier\n");
255 return -EINVAL;
256 }
257
258 return 0;
259}
9e1f05b2 260EXPORT_SYMBOL(sti_crtc_enable_vblank);
9bbf86fe 261
9e1f05b2 262void sti_crtc_disable_vblank(struct drm_device *dev, int crtc)
9bbf86fe 263{
9e1f05b2 264 struct sti_private *priv = dev->dev_private;
9bbf86fe
BG
265 struct sti_compositor *compo = priv->compo;
266 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
9bbf86fe
BG
267
268 DRM_DEBUG_DRIVER("\n");
269
270 if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
271 compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
272 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
273
274 /* free the resources of the pending requests */
9bbf86fe
BG
275 if (compo->mixer[crtc]->pending_event) {
276 drm_vblank_put(dev, crtc);
277 compo->mixer[crtc]->pending_event = NULL;
278 }
9bbf86fe 279}
9e1f05b2 280EXPORT_SYMBOL(sti_crtc_disable_vblank);
9bbf86fe
BG
281
282static struct drm_crtc_funcs sti_crtc_funcs = {
de4b00b0
BG
283 .set_config = drm_atomic_helper_set_config,
284 .page_flip = drm_atomic_helper_page_flip,
9e1f05b2
VA
285 .destroy = sti_crtc_destroy,
286 .set_property = sti_crtc_set_property,
de4b00b0
BG
287 .reset = drm_atomic_helper_crtc_reset,
288 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
289 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
9bbf86fe
BG
290};
291
9e1f05b2 292bool sti_crtc_is_main(struct drm_crtc *crtc)
9bbf86fe
BG
293{
294 struct sti_mixer *mixer = to_sti_mixer(crtc);
295
296 if (mixer->id == STI_MIXER_MAIN)
297 return true;
298
299 return false;
300}
9e1f05b2 301EXPORT_SYMBOL(sti_crtc_is_main);
9bbf86fe 302
9e1f05b2
VA
303int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
304 struct drm_plane *primary, struct drm_plane *cursor)
9bbf86fe
BG
305{
306 struct drm_crtc *crtc = &mixer->drm_crtc;
307 int res;
308
309 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
9e1f05b2 310 &sti_crtc_funcs);
9bbf86fe 311 if (res) {
871bcdfe 312 DRM_ERROR("Can't initialze CRTC\n");
9bbf86fe
BG
313 return -EINVAL;
314 }
315
316 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
317
318 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
319 crtc->base.id, sti_mixer_to_str(mixer));
320
321 return 0;
322}
This page took 0.084264 seconds and 5 git commands to generate.