blob: 27b3ef207617006c58f24dbb85e677c3d29afcc8 [file] [log] [blame]
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +02001/*
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>
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010012#include <drm/drm_atomic.h>
13#include <drm/drm_atomic_helper.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020014#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010015#include <drm/drm_plane_helper.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020016
17#include "sti_compositor.h"
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020018#include "sti_crtc.h"
19#include "sti_drv.h"
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020020#include "sti_vtg.h"
21
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020022static void sti_crtc_dpms(struct drm_crtc *crtc, int mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020023{
24 DRM_DEBUG_KMS("\n");
25}
26
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020027static void sti_crtc_prepare(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020028{
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
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +010033 mixer->enabled = true;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020034
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 }
Benjamin Gaignard2f7d0e82c2014-12-04 11:17:32 +010043
Vincent Abriou871bcdf2015-07-31 11:32:13 +020044 sti_mixer_clear_all_planes(mixer);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020045}
46
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020047static void sti_crtc_commit(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020048{
49 struct sti_mixer *mixer = to_sti_mixer(crtc);
50 struct device *dev = mixer->dev;
51 struct sti_compositor *compo = dev_get_drvdata(dev);
Vincent Abriou871bcdf2015-07-31 11:32:13 +020052 struct sti_plane *plane;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020053
54 if ((!mixer || !compo)) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +020055 DRM_ERROR("Can't find mixer or compositor)\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020056 return;
57 }
58
59 /* get GDP which is reserved to the CRTC FB */
Vincent Abriou871bcdf2015-07-31 11:32:13 +020060 plane = to_sti_plane(crtc->primary);
61 if (!plane)
62 DRM_ERROR("Can't find CRTC dedicated plane (GDP0)\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020063
Vincent Abriou871bcdf2015-07-31 11:32:13 +020064 /* Enable plane on mixer */
65 if (sti_mixer_set_plane_status(mixer, plane, true))
66 DRM_ERROR("Cannot enable plane at mixer\n");
Benjamin Gaignardca614aa2014-12-04 11:27:45 +010067
68 drm_crtc_vblank_on(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020069}
70
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020071static bool sti_crtc_mode_fixup(struct drm_crtc *crtc,
72 const struct drm_display_mode *mode,
73 struct drm_display_mode *adjusted_mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020074{
75 /* accept the provided drm_display_mode, do not fix it up */
76 return true;
77}
78
79static int
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020080sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020081{
82 struct sti_mixer *mixer = to_sti_mixer(crtc);
83 struct device *dev = mixer->dev;
84 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020085 struct clk *clk;
86 int rate = mode->clock * 1000;
87 int res;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020088
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010089 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020090 crtc->base.id, sti_mixer_to_str(mixer),
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010091 mode->base.id, mode->name);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020092
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
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200121 res = sti_mixer_active_video_area(mixer, &crtc->mode);
122 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200123 DRM_ERROR("Can't set active video area\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200124 return -EINVAL;
125 }
126
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100127 return res;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200128}
129
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200130static void sti_crtc_disable(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200131{
132 struct sti_mixer *mixer = to_sti_mixer(crtc);
133 struct device *dev = mixer->dev;
134 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200135
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100136 if (!mixer->enabled)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200137 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
Benjamin Gaignardca614aa2014-12-04 11:27:45 +0100144 drm_crtc_vblank_off(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200145
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
Benjamin Gaignard5e03abc2014-12-08 17:32:36 +0100155 mixer->enabled = false;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200156}
157
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100158static void
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200159sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100160{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200161 sti_crtc_prepare(crtc);
162 sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100163}
164
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200165static void sti_crtc_atomic_begin(struct drm_crtc *crtc)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100166{
167 struct sti_mixer *mixer = to_sti_mixer(crtc);
168
169 if (crtc->state->event) {
170 crtc->state->event->pipe = drm_crtc_index(crtc);
171
172 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
173
174 mixer->pending_event = crtc->state->event;
175 crtc->state->event = NULL;
176 }
177}
178
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200179static void sti_crtc_atomic_flush(struct drm_crtc *crtc)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100180{
181}
182
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200183static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200184 .dpms = sti_crtc_dpms,
185 .prepare = sti_crtc_prepare,
186 .commit = sti_crtc_commit,
187 .mode_fixup = sti_crtc_mode_fixup,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100188 .mode_set = drm_helper_crtc_mode_set,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200189 .mode_set_nofb = sti_crtc_mode_set_nofb,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100190 .mode_set_base = drm_helper_crtc_mode_set_base,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200191 .disable = sti_crtc_disable,
192 .atomic_begin = sti_crtc_atomic_begin,
193 .atomic_flush = sti_crtc_atomic_flush,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200194};
195
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200196static void sti_crtc_destroy(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200197{
198 DRM_DEBUG_KMS("\n");
199 drm_crtc_cleanup(crtc);
200}
201
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200202static int sti_crtc_set_property(struct drm_crtc *crtc,
203 struct drm_property *property,
204 uint64_t val)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200205{
206 DRM_DEBUG_KMS("\n");
207 return 0;
208}
209
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200210int sti_crtc_vblank_cb(struct notifier_block *nb,
211 unsigned long event, void *data)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200212{
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;
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200218 struct sti_private *priv;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200219
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,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200234 compo->mixer[*crtc]->pending_event);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200235 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
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200243int sti_crtc_enable_vblank(struct drm_device *dev, int crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200244{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200245 struct sti_private *dev_priv = dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200246 struct sti_compositor *compo = dev_priv->compo;
247 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
248
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200249 DRM_DEBUG_DRIVER("\n");
250
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200251 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}
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200260EXPORT_SYMBOL(sti_crtc_enable_vblank);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200261
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200262void sti_crtc_disable_vblank(struct drm_device *dev, int crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200263{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200264 struct sti_private *priv = dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200265 struct sti_compositor *compo = priv->compo;
266 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200267
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 */
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200275 if (compo->mixer[crtc]->pending_event) {
276 drm_vblank_put(dev, crtc);
277 compo->mixer[crtc]->pending_event = NULL;
278 }
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200279}
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200280EXPORT_SYMBOL(sti_crtc_disable_vblank);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200281
282static struct drm_crtc_funcs sti_crtc_funcs = {
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100283 .set_config = drm_atomic_helper_set_config,
284 .page_flip = drm_atomic_helper_page_flip,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200285 .destroy = sti_crtc_destroy,
286 .set_property = sti_crtc_set_property,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100287 .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,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200290};
291
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200292bool sti_crtc_is_main(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200293{
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}
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200301EXPORT_SYMBOL(sti_crtc_is_main);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200302
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200303int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
304 struct drm_plane *primary, struct drm_plane *cursor)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200305{
306 struct drm_crtc *crtc = &mixer->drm_crtc;
307 int res;
308
309 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200310 &sti_crtc_funcs);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200311 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200312 DRM_ERROR("Can't initialze CRTC\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200313 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}