blob: d45a4335df5df92a213be2119eb84d792aa59c3f [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);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020026
Vincent Abriou29d1dc62015-08-03 14:22:16 +020027 DRM_DEBUG_DRIVER("\n");
28
29 mixer->status = STI_MIXER_READY;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020030
Vincent Abriou29d1dc62015-08-03 14:22:16 +020031 drm_crtc_vblank_on(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020032}
33
Vincent Abriou29d1dc62015-08-03 14:22:16 +020034static void sti_crtc_disabling(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020035{
36 struct sti_mixer *mixer = to_sti_mixer(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020037
Vincent Abriou29d1dc62015-08-03 14:22:16 +020038 DRM_DEBUG_DRIVER("\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020039
Vincent Abriou29d1dc62015-08-03 14:22:16 +020040 mixer->status = STI_MIXER_DISABLING;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020041}
42
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020043static int
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020044sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020045{
46 struct sti_mixer *mixer = to_sti_mixer(crtc);
47 struct device *dev = mixer->dev;
48 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard32e14592016-05-26 10:39:20 +020049 struct clk *compo_clk, *pix_clk;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020050 int rate = mode->clock * 1000;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020051
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010052 DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020053 crtc->base.id, sti_mixer_to_str(mixer),
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010054 mode->base.id, mode->name);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020055
56 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
57 mode->vrefresh, mode->clock,
58 mode->hdisplay,
59 mode->hsync_start, mode->hsync_end,
60 mode->htotal,
61 mode->vdisplay,
62 mode->vsync_start, mode->vsync_end,
63 mode->vtotal, mode->type, mode->flags);
64
Benjamin Gaignard32e14592016-05-26 10:39:20 +020065 if (mixer->id == STI_MIXER_MAIN) {
66 compo_clk = compo->clk_compo_main;
67 pix_clk = compo->clk_pix_main;
68 } else {
69 compo_clk = compo->clk_compo_aux;
70 pix_clk = compo->clk_pix_aux;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020071 }
Benjamin Gaignard32e14592016-05-26 10:39:20 +020072
73 /* Prepare and enable the compo IP clock */
74 if (clk_prepare_enable(compo_clk)) {
75 DRM_INFO("Failed to prepare/enable compositor clk\n");
76 goto compo_error;
77 }
78
79 /* Set rate and prepare/enable pixel clock */
80 if (clk_set_rate(pix_clk, rate) < 0) {
81 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
82 goto pix_error;
83 }
84 if (clk_prepare_enable(pix_clk)) {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020085 DRM_ERROR("Failed to prepare/enable pix clk\n");
Benjamin Gaignard32e14592016-05-26 10:39:20 +020086 goto pix_error;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020087 }
88
Fabien Dessenneffdbb822016-09-06 09:42:25 +020089 sti_vtg_set_config(compo->vtg[mixer->id], &crtc->mode);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020090
Benjamin Gaignard32e14592016-05-26 10:39:20 +020091 if (sti_mixer_active_video_area(mixer, &crtc->mode)) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +020092 DRM_ERROR("Can't set active video area\n");
Benjamin Gaignard32e14592016-05-26 10:39:20 +020093 goto mixer_error;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020094 }
95
Benjamin Gaignard32e14592016-05-26 10:39:20 +020096 return 0;
97
98mixer_error:
99 clk_disable_unprepare(pix_clk);
100pix_error:
101 clk_disable_unprepare(compo_clk);
102compo_error:
103 return -EINVAL;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200104}
105
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200106static void sti_crtc_disable(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200107{
108 struct sti_mixer *mixer = to_sti_mixer(crtc);
109 struct device *dev = mixer->dev;
110 struct sti_compositor *compo = dev_get_drvdata(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200111
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200112 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
113
114 /* Disable Background */
115 sti_mixer_set_background_status(mixer, false);
116
Benjamin Gaignardca614aa2014-12-04 11:27:45 +0100117 drm_crtc_vblank_off(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200118
119 /* Disable pixel clock and compo IP clocks */
120 if (mixer->id == STI_MIXER_MAIN) {
121 clk_disable_unprepare(compo->clk_pix_main);
122 clk_disable_unprepare(compo->clk_compo_main);
123 } else {
124 clk_disable_unprepare(compo->clk_pix_aux);
125 clk_disable_unprepare(compo->clk_compo_aux);
126 }
127
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200128 mixer->status = STI_MIXER_DISABLED;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200129}
130
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100131static void
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200132sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100133{
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_flush(struct drm_crtc *crtc,
138 struct drm_crtc_state *old_crtc_state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100139{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200140 struct drm_device *drm_dev = crtc->dev;
141 struct sti_mixer *mixer = to_sti_mixer(crtc);
142 struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
143 struct drm_plane *p;
Fabien DESSENNEc62052d2017-01-12 17:27:36 +0100144 struct drm_pending_vblank_event *event;
145 unsigned long flags;
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200146
147 DRM_DEBUG_DRIVER("\n");
148
149 /* perform plane actions */
150 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
151 struct sti_plane *plane = to_sti_plane(p);
152
153 switch (plane->status) {
154 case STI_PLANE_UPDATED:
Fabien Dessenne3bc6b012016-09-06 09:42:39 +0200155 /* ignore update for other CRTC */
156 if (p->state->crtc != crtc)
157 continue;
158
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200159 /* update planes tag as updated */
160 DRM_DEBUG_DRIVER("update plane %s\n",
161 sti_plane_to_str(plane));
162
163 if (sti_mixer_set_plane_depth(mixer, plane)) {
164 DRM_ERROR("Cannot set plane %s depth\n",
165 sti_plane_to_str(plane));
166 break;
167 }
168
169 if (sti_mixer_set_plane_status(mixer, plane, true)) {
170 DRM_ERROR("Cannot enable plane %s at mixer\n",
171 sti_plane_to_str(plane));
172 break;
173 }
174
175 /* if plane is HQVDP_0 then commit the vid[0] */
176 if (plane->desc == STI_HQVDP_0)
177 sti_vid_commit(compo->vid[0], p->state);
178
179 plane->status = STI_PLANE_READY;
180
181 break;
182 case STI_PLANE_DISABLING:
183 /* disabling sequence for planes tag as disabling */
184 DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
185 sti_plane_to_str(plane));
186
187 if (sti_mixer_set_plane_status(mixer, plane, false)) {
188 DRM_ERROR("Cannot disable plane %s at mixer\n",
189 sti_plane_to_str(plane));
190 continue;
191 }
192
193 if (plane->desc == STI_CURSOR)
194 /* tag plane status for disabled */
195 plane->status = STI_PLANE_DISABLED;
196 else
197 /* tag plane status for flushing */
198 plane->status = STI_PLANE_FLUSHING;
199
200 /* if plane is HQVDP_0 then disable the vid[0] */
201 if (plane->desc == STI_HQVDP_0)
202 sti_vid_disable(compo->vid[0]);
203
204 break;
205 default:
206 /* Other status case are not handled */
207 break;
208 }
209 }
Fabien DESSENNEc62052d2017-01-12 17:27:36 +0100210
211 event = crtc->state->event;
212 if (event) {
213 crtc->state->event = NULL;
214
215 spin_lock_irqsave(&crtc->dev->event_lock, flags);
216 if (drm_crtc_vblank_get(crtc) == 0)
217 drm_crtc_arm_vblank_event(crtc, event);
218 else
219 drm_crtc_send_vblank_event(crtc, event);
220 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
221 }
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100222}
223
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300224static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200225 .enable = sti_crtc_enable,
226 .disable = sti_crtc_disabling,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200227 .mode_set_nofb = sti_crtc_mode_set_nofb,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200228 .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{
Fabien Dessenneae217852016-09-06 09:42:13 +0200248 struct sti_compositor *compo;
Thierry Reding23886932015-09-24 18:35:38 +0200249 struct drm_crtc *crtc = data;
250 struct sti_mixer *mixer;
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200251 struct sti_private *priv;
Thierry Reding23886932015-09-24 18:35:38 +0200252 unsigned int pipe;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200253
Thierry Reding23886932015-09-24 18:35:38 +0200254 priv = crtc->dev->dev_private;
255 pipe = drm_crtc_index(crtc);
Fabien Dessenneae217852016-09-06 09:42:13 +0200256 compo = container_of(nb, struct sti_compositor, vtg_vblank_nb[pipe]);
Thierry Reding23886932015-09-24 18:35:38 +0200257 mixer = compo->mixer[pipe];
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200258
259 if ((event != VTG_TOP_FIELD_EVENT) &&
260 (event != VTG_BOTTOM_FIELD_EVENT)) {
261 DRM_ERROR("unknown event: %lu\n", event);
262 return -EINVAL;
263 }
264
Thierry Reding23886932015-09-24 18:35:38 +0200265 drm_crtc_handle_vblank(crtc);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200266
Thierry Reding23886932015-09-24 18:35:38 +0200267 if (mixer->status == STI_MIXER_DISABLING) {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200268 struct drm_plane *p;
269
270 /* Disable mixer only if all overlay planes (GDP and VDP)
271 * are disabled */
Thierry Reding23886932015-09-24 18:35:38 +0200272 list_for_each_entry(p, &crtc->dev->mode_config.plane_list,
273 head) {
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200274 struct sti_plane *plane = to_sti_plane(p);
275
276 if ((plane->desc & STI_PLANE_TYPE_MASK) <= STI_VDP)
277 if (plane->status != STI_PLANE_DISABLED)
278 return 0;
279 }
Thierry Reding23886932015-09-24 18:35:38 +0200280 sti_crtc_disable(crtc);
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200281 }
282
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200283 return 0;
284}
285
Thierry Reding88e72712015-09-24 18:35:31 +0200286int sti_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200287{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200288 struct sti_private *dev_priv = dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200289 struct sti_compositor *compo = dev_priv->compo;
Fabien Dessenneae217852016-09-06 09:42:13 +0200290 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe];
Thierry Reding23886932015-09-24 18:35:38 +0200291 struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
Fabien Dessenneffdbb822016-09-06 09:42:25 +0200292 struct sti_vtg *vtg = compo->vtg[pipe];
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200293
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200294 DRM_DEBUG_DRIVER("\n");
295
Fabien Dessenneffdbb822016-09-06 09:42:25 +0200296 if (sti_vtg_register_client(vtg, vtg_vblank_nb, crtc)) {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200297 DRM_ERROR("Cannot register VTG notifier\n");
298 return -EINVAL;
299 }
300
301 return 0;
302}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200303
Thierry Reding88e72712015-09-24 18:35:31 +0200304void sti_crtc_disable_vblank(struct drm_device *drm_dev, unsigned int pipe)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200305{
Vincent Abriou29d1dc62015-08-03 14:22:16 +0200306 struct sti_private *priv = drm_dev->dev_private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200307 struct sti_compositor *compo = priv->compo;
Fabien Dessenneae217852016-09-06 09:42:13 +0200308 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe];
Fabien Dessenneffdbb822016-09-06 09:42:25 +0200309 struct sti_vtg *vtg = compo->vtg[pipe];
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200310
311 DRM_DEBUG_DRIVER("\n");
312
Fabien Dessenneffdbb822016-09-06 09:42:25 +0200313 if (sti_vtg_unregister_client(vtg, vtg_vblank_nb))
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200314 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200315}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200316
Benjamin Gaignard83af0a42016-06-21 15:09:39 +0200317static int sti_crtc_late_register(struct drm_crtc *crtc)
318{
319 struct sti_mixer *mixer = to_sti_mixer(crtc);
320 struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
321
322 if (drm_crtc_index(crtc) == 0)
Vincent Abriou38fdb8d2016-09-15 17:11:07 +0200323 return sti_compositor_debugfs_init(compo, crtc->dev->primary);
Benjamin Gaignard83af0a42016-06-21 15:09:39 +0200324
325 return 0;
326}
327
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300328static const struct drm_crtc_funcs sti_crtc_funcs = {
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100329 .set_config = drm_atomic_helper_set_config,
330 .page_flip = drm_atomic_helper_page_flip,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200331 .destroy = sti_crtc_destroy,
332 .set_property = sti_crtc_set_property,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100333 .reset = drm_atomic_helper_crtc_reset,
334 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
335 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Benjamin Gaignard83af0a42016-06-21 15:09:39 +0200336 .late_register = sti_crtc_late_register,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200337};
338
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200339bool sti_crtc_is_main(struct drm_crtc *crtc)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200340{
341 struct sti_mixer *mixer = to_sti_mixer(crtc);
342
343 if (mixer->id == STI_MIXER_MAIN)
344 return true;
345
346 return false;
347}
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200348
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200349int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
350 struct drm_plane *primary, struct drm_plane *cursor)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200351{
352 struct drm_crtc *crtc = &mixer->drm_crtc;
353 int res;
354
355 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200356 &sti_crtc_funcs, NULL);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200357 if (res) {
Vincent Abriou871bcdf2015-07-31 11:32:13 +0200358 DRM_ERROR("Can't initialze CRTC\n");
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200359 return -EINVAL;
360 }
361
362 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
363
364 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
365 crtc->base.id, sti_mixer_to_str(mixer));
366
367 return 0;
368}