blob: 7fab3af7473b26883db2ce980efadf0271c4735a [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"
Vincent Abriou29d1dc62015-08-03 14:22:16 +020020#include "sti_vid.h"
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020021#include "sti_vtg.h"
22
Vincent Abriou29d1dc62015-08-03 14:22:16 +020023static void sti_crtc_enable(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020024{
25 struct sti_mixer *mixer = to_sti_mixer(crtc);
26 struct device *dev = mixer->dev;
27 struct sti_compositor *compo = dev_get_drvdata(dev);
28
Vincent Abriou29d1dc62015-08-03 14:22:16 +020029 DRM_DEBUG_DRIVER("\n");
30
31 mixer->status = STI_MIXER_READY;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020032
33 /* Prepare and enable the compo IP clock */
34 if (mixer->id == STI_MIXER_MAIN) {
35 if (clk_prepare_enable(compo->clk_compo_main))
36 DRM_INFO("Failed to prepare/enable compo_main clk\n");
37 } else {
38 if (clk_prepare_enable(compo->clk_compo_aux))
39 DRM_INFO("Failed to prepare/enable compo_aux clk\n");
40 }
Benjamin Gaignard2f7d0e82c2014-12-04 11:17:32 +010041
Vincent Abriou29d1dc62015-08-03 14:22:16 +020042 drm_crtc_vblank_on(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020043}
44
Vincent Abriou29d1dc62015-08-03 14:22:16 +020045static void sti_crtc_disabling(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020046{
47 struct sti_mixer *mixer = to_sti_mixer(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020048
Vincent Abriou29d1dc62015-08-03 14:22:16 +020049 DRM_DEBUG_DRIVER("\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020050
Vincent Abriou29d1dc62015-08-03 14:22:16 +020051 mixer->status = STI_MIXER_DISABLING;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020052}
53
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020054static int
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020055sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020056{
57 struct sti_mixer *mixer = to_sti_mixer(crtc);
58 struct device *dev = mixer->dev;
59 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020060 struct clk *clk;
61 int rate = mode->clock * 1000;
62 int res;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020063
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010064 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020065 crtc->base.id, sti_mixer_to_str(mixer),
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010066 mode->base.id, mode->name);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020067
68 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
69 mode->vrefresh, mode->clock,
70 mode->hdisplay,
71 mode->hsync_start, mode->hsync_end,
72 mode->htotal,
73 mode->vdisplay,
74 mode->vsync_start, mode->vsync_end,
75 mode->vtotal, mode->type, mode->flags);
76
77 /* Set rate and prepare/enable pixel clock */
78 if (mixer->id == STI_MIXER_MAIN)
79 clk = compo->clk_pix_main;
80 else
81 clk = compo->clk_pix_aux;
82
83 res = clk_set_rate(clk, rate);
84 if (res < 0) {
85 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
86 return -EINVAL;
87 }
88 if (clk_prepare_enable(clk)) {
89 DRM_ERROR("Failed to prepare/enable pix clk\n");
90 return -EINVAL;
91 }
92
93 sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
94 compo->vtg_main : compo->vtg_aux, &crtc->mode);
95
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020096 res = sti_mixer_active_video_area(mixer, &crtc->mode);
97 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +020098 DRM_ERROR("Can't set active video area\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020099 return -EINVAL;
100 }
101
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100102 return res;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200103}
104
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200105static void sti_crtc_disable(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200106{
107 struct sti_mixer *mixer = to_sti_mixer(crtc);
108 struct device *dev = mixer->dev;
109 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200110
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200111 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
112
113 /* Disable Background */
114 sti_mixer_set_background_status(mixer, false);
115
Benjamin Gaignardca614aa2014-12-04 11:27:45 +0100116 drm_crtc_vblank_off(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200117
118 /* Disable pixel clock and compo IP clocks */
119 if (mixer->id == STI_MIXER_MAIN) {
120 clk_disable_unprepare(compo->clk_pix_main);
121 clk_disable_unprepare(compo->clk_compo_main);
122 } else {
123 clk_disable_unprepare(compo->clk_pix_aux);
124 clk_disable_unprepare(compo->clk_compo_aux);
125 }
126
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200127 mixer->status = STI_MIXER_DISABLED;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200128}
129
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100130static void
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200131sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100132{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200133 sti_crtc_enable(crtc);
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200134 sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100135}
136
Dave Airliee1474e72015-08-14 10:14:23 +1000137static void sti_crtc_atomic_begin(struct drm_crtc *crtc,
138 struct drm_crtc_state *old_crtc_state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100139{
140 struct sti_mixer *mixer = to_sti_mixer(crtc);
141
142 if (crtc->state->event) {
143 crtc->state->event->pipe = drm_crtc_index(crtc);
144
145 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
146
147 mixer->pending_event = crtc->state->event;
148 crtc->state->event = NULL;
149 }
150}
151
Dave Airliee1474e72015-08-14 10:14:23 +1000152static void sti_crtc_atomic_flush(struct drm_crtc *crtc,
153 struct drm_crtc_state *old_crtc_state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100154{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200155 struct drm_device *drm_dev = crtc->dev;
156 struct sti_mixer *mixer = to_sti_mixer(crtc);
157 struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
158 struct drm_plane *p;
159
160 DRM_DEBUG_DRIVER("\n");
161
162 /* perform plane actions */
163 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
164 struct sti_plane *plane = to_sti_plane(p);
165
166 switch (plane->status) {
167 case STI_PLANE_UPDATED:
168 /* update planes tag as updated */
169 DRM_DEBUG_DRIVER("update plane %s\n",
170 sti_plane_to_str(plane));
171
172 if (sti_mixer_set_plane_depth(mixer, plane)) {
173 DRM_ERROR("Cannot set plane %s depth\n",
174 sti_plane_to_str(plane));
175 break;
176 }
177
178 if (sti_mixer_set_plane_status(mixer, plane, true)) {
179 DRM_ERROR("Cannot enable plane %s at mixer\n",
180 sti_plane_to_str(plane));
181 break;
182 }
183
184 /* if plane is HQVDP_0 then commit the vid[0] */
185 if (plane->desc == STI_HQVDP_0)
186 sti_vid_commit(compo->vid[0], p->state);
187
188 plane->status = STI_PLANE_READY;
189
190 break;
191 case STI_PLANE_DISABLING:
192 /* disabling sequence for planes tag as disabling */
193 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
194 sti_plane_to_str(plane));
195
196 if (sti_mixer_set_plane_status(mixer, plane, false)) {
197 DRM_ERROR("Cannot disable plane %s at mixer\n",
198 sti_plane_to_str(plane));
199 continue;
200 }
201
202 if (plane->desc == STI_CURSOR)
203 /* tag plane status for disabled */
204 plane->status = STI_PLANE_DISABLED;
205 else
206 /* tag plane status for flushing */
207 plane->status = STI_PLANE_FLUSHING;
208
209 /* if plane is HQVDP_0 then disable the vid[0] */
210 if (plane->desc == STI_HQVDP_0)
211 sti_vid_disable(compo->vid[0]);
212
213 break;
214 default:
215 /* Other status case are not handled */
216 break;
217 }
218 }
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100219}
220
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300221static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200222 .enable = sti_crtc_enable,
223 .disable = sti_crtc_disabling,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100224 .mode_set = drm_helper_crtc_mode_set,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200225 .mode_set_nofb = sti_crtc_mode_set_nofb,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100226 .mode_set_base = drm_helper_crtc_mode_set_base,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200227 .atomic_begin = sti_crtc_atomic_begin,
228 .atomic_flush = sti_crtc_atomic_flush,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200229};
230
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200231static void sti_crtc_destroy(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200232{
233 DRM_DEBUG_KMS("\n");
234 drm_crtc_cleanup(crtc);
235}
236
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200237static int sti_crtc_set_property(struct drm_crtc *crtc,
238 struct drm_property *property,
239 uint64_t val)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200240{
241 DRM_DEBUG_KMS("\n");
242 return 0;
243}
244
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200245int sti_crtc_vblank_cb(struct notifier_block *nb,
246 unsigned long event, void *data)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200247{
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200248 struct sti_compositor *compo =
249 container_of(nb, struct sti_compositor, vtg_vblank_nb);
Thierry Reding23886932015-09-24 18:35:38 +0200250 struct drm_crtc *crtc = data;
251 struct sti_mixer *mixer;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200252 unsigned long flags;
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200253 struct sti_private *priv;
Thierry Reding23886932015-09-24 18:35:38 +0200254 unsigned int pipe;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200255
Thierry Reding23886932015-09-24 18:35:38 +0200256 priv = crtc->dev->dev_private;
257 pipe = drm_crtc_index(crtc);
258 mixer = compo->mixer[pipe];
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200259
260 if ((event != VTG_TOP_FIELD_EVENT) &&
261 (event != VTG_BOTTOM_FIELD_EVENT)) {
262 DRM_ERROR("unknown event: %lu\n", event);
263 return -EINVAL;
264 }
265
Thierry Reding23886932015-09-24 18:35:38 +0200266 drm_crtc_handle_vblank(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200267
Thierry Reding23886932015-09-24 18:35:38 +0200268 spin_lock_irqsave(&crtc->dev->event_lock, flags);
269 if (mixer->pending_event) {
270 drm_crtc_send_vblank_event(crtc, mixer->pending_event);
271 drm_crtc_vblank_put(crtc);
272 mixer->pending_event = NULL;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200273 }
Thierry Reding23886932015-09-24 18:35:38 +0200274 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200275
Thierry Reding23886932015-09-24 18:35:38 +0200276 if (mixer->status == STI_MIXER_DISABLING) {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200277 struct drm_plane *p;
278
279 /* Disable mixer only if all overlay planes (GDP and VDP)
280 * are disabled */
Thierry Reding23886932015-09-24 18:35:38 +0200281 list_for_each_entry(p, &crtc->dev->mode_config.plane_list,
282 head) {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200283 struct sti_plane *plane = to_sti_plane(p);
284
285 if ((plane->desc & STI_PLANE_TYPE_MASK) <= STI_VDP)
286 if (plane->status != STI_PLANE_DISABLED)
287 return 0;
288 }
Thierry Reding23886932015-09-24 18:35:38 +0200289 sti_crtc_disable(crtc);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200290 }
291
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200292 return 0;
293}
294
Thierry Reding88e72712015-09-24 18:35:31 +0200295int sti_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200296{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200297 struct sti_private *dev_priv = dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200298 struct sti_compositor *compo = dev_priv->compo;
299 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
Thierry Reding23886932015-09-24 18:35:38 +0200300 struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200301
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200302 DRM_DEBUG_DRIVER("\n");
303
Thierry Reding88e72712015-09-24 18:35:31 +0200304 if (sti_vtg_register_client(pipe == STI_MIXER_MAIN ?
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200305 compo->vtg_main : compo->vtg_aux,
Thierry Reding23886932015-09-24 18:35:38 +0200306 vtg_vblank_nb, crtc)) {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200307 DRM_ERROR("Cannot register VTG notifier\n");
308 return -EINVAL;
309 }
310
311 return 0;
312}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200313
Thierry Reding88e72712015-09-24 18:35:31 +0200314void sti_crtc_disable_vblank(struct drm_device *drm_dev, unsigned int pipe)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200315{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200316 struct sti_private *priv = drm_dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200317 struct sti_compositor *compo = priv->compo;
318 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
Thierry Reding23886932015-09-24 18:35:38 +0200319 struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200320
321 DRM_DEBUG_DRIVER("\n");
322
Thierry Reding88e72712015-09-24 18:35:31 +0200323 if (sti_vtg_unregister_client(pipe == STI_MIXER_MAIN ?
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200324 compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
325 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
326
327 /* free the resources of the pending requests */
Thierry Reding88e72712015-09-24 18:35:31 +0200328 if (compo->mixer[pipe]->pending_event) {
Thierry Reding23886932015-09-24 18:35:38 +0200329 drm_crtc_vblank_put(crtc);
Thierry Reding88e72712015-09-24 18:35:31 +0200330 compo->mixer[pipe]->pending_event = NULL;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200331 }
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200332}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200333
Benjamin Gaignard83af0a42016-06-21 15:09:39 +0200334static int sti_crtc_late_register(struct drm_crtc *crtc)
335{
336 struct sti_mixer *mixer = to_sti_mixer(crtc);
337 struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
338
339 if (drm_crtc_index(crtc) == 0)
340 return sti_compositor_debufs_init(compo, crtc->dev->primary);
341
342 return 0;
343}
344
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300345static const struct drm_crtc_funcs sti_crtc_funcs = {
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100346 .set_config = drm_atomic_helper_set_config,
347 .page_flip = drm_atomic_helper_page_flip,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200348 .destroy = sti_crtc_destroy,
349 .set_property = sti_crtc_set_property,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100350 .reset = drm_atomic_helper_crtc_reset,
351 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
352 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Benjamin Gaignard83af0a42016-06-21 15:09:39 +0200353 .late_register = sti_crtc_late_register,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200354};
355
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200356bool sti_crtc_is_main(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200357{
358 struct sti_mixer *mixer = to_sti_mixer(crtc);
359
360 if (mixer->id == STI_MIXER_MAIN)
361 return true;
362
363 return false;
364}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200365
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200366int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
367 struct drm_plane *primary, struct drm_plane *cursor)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200368{
369 struct drm_crtc *crtc = &mixer->drm_crtc;
370 int res;
371
372 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200373 &sti_crtc_funcs, NULL);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200374 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200375 DRM_ERROR("Can't initialze CRTC\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200376 return -EINVAL;
377 }
378
379 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
380
381 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
382 crtc->base.id, sti_mixer_to_str(mixer));
383
384 return 0;
385}