]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/sti/sti_drm_crtc.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[karo-tx-linux.git] / drivers / gpu / drm / sti / sti_drm_crtc.c
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>
12 #include <drm/drm_crtc_helper.h>
13 #include <drm/drm_plane_helper.h>
14
15 #include "sti_compositor.h"
16 #include "sti_drm_drv.h"
17 #include "sti_drm_crtc.h"
18 #include "sti_vtg.h"
19
20 static void sti_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
21 {
22         DRM_DEBUG_KMS("\n");
23 }
24
25 static void sti_drm_crtc_prepare(struct drm_crtc *crtc)
26 {
27         struct sti_mixer *mixer = to_sti_mixer(crtc);
28         struct device *dev = mixer->dev;
29         struct sti_compositor *compo = dev_get_drvdata(dev);
30
31         mixer->enabled = true;
32
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         }
41
42         sti_mixer_clear_all_layers(mixer);
43 }
44
45 static void sti_drm_crtc_commit(struct drm_crtc *crtc)
46 {
47         struct sti_mixer *mixer = to_sti_mixer(crtc);
48         struct device *dev = mixer->dev;
49         struct sti_compositor *compo = dev_get_drvdata(dev);
50         struct sti_layer *layer;
51
52         if ((!mixer || !compo)) {
53                 DRM_ERROR("Can not find mixer or compositor)\n");
54                 return;
55         }
56
57         /* get GDP which is reserved to the CRTC FB */
58         layer = to_sti_layer(crtc->primary);
59         if (layer)
60                 sti_layer_commit(layer);
61         else
62                 DRM_ERROR("Can not find CRTC dedicated plane (GDP0)\n");
63
64         /* Enable layer on mixer */
65         if (sti_mixer_set_layer_status(mixer, layer, true))
66                 DRM_ERROR("Can not enable layer at mixer\n");
67
68         drm_crtc_vblank_on(crtc);
69 }
70
71 static bool sti_drm_crtc_mode_fixup(struct drm_crtc *crtc,
72                                     const struct drm_display_mode *mode,
73                                     struct drm_display_mode *adjusted_mode)
74 {
75         /* accept the provided drm_display_mode, do not fix it up */
76         return true;
77 }
78
79 static int
80 sti_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
81                       struct drm_display_mode *adjusted_mode, int x, int y,
82                       struct drm_framebuffer *old_fb)
83 {
84         struct sti_mixer *mixer = to_sti_mixer(crtc);
85         struct device *dev = mixer->dev;
86         struct sti_compositor *compo = dev_get_drvdata(dev);
87         struct sti_layer *layer;
88         struct clk *clk;
89         int rate = mode->clock * 1000;
90         int res;
91         unsigned int w, h;
92
93         DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d mode:%d (%s)\n",
94                       crtc->base.id, sti_mixer_to_str(mixer),
95                       crtc->primary->fb->base.id, mode->base.id, mode->name);
96
97         DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
98                       mode->vrefresh, mode->clock,
99                       mode->hdisplay,
100                       mode->hsync_start, mode->hsync_end,
101                       mode->htotal,
102                       mode->vdisplay,
103                       mode->vsync_start, mode->vsync_end,
104                       mode->vtotal, mode->type, mode->flags);
105
106         /* Set rate and prepare/enable pixel clock */
107         if (mixer->id == STI_MIXER_MAIN)
108                 clk = compo->clk_pix_main;
109         else
110                 clk = compo->clk_pix_aux;
111
112         res = clk_set_rate(clk, rate);
113         if (res < 0) {
114                 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
115                 return -EINVAL;
116         }
117         if (clk_prepare_enable(clk)) {
118                 DRM_ERROR("Failed to prepare/enable pix clk\n");
119                 return -EINVAL;
120         }
121
122         sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
123                         compo->vtg_main : compo->vtg_aux, &crtc->mode);
124
125         /* a GDP is reserved to the CRTC FB */
126         layer = to_sti_layer(crtc->primary);
127         if (!layer) {
128                 DRM_ERROR("Can not find GDP0)\n");
129                 return -EINVAL;
130         }
131
132         /* copy the mode data adjusted by mode_fixup() into crtc->mode
133          * so that hardware can be set to proper mode
134          */
135         memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
136
137         res = sti_mixer_set_layer_depth(mixer, layer);
138         if (res) {
139                 DRM_ERROR("Can not set layer depth\n");
140                 return -EINVAL;
141         }
142         res = sti_mixer_active_video_area(mixer, &crtc->mode);
143         if (res) {
144                 DRM_ERROR("Can not set active video area\n");
145                 return -EINVAL;
146         }
147
148         w = crtc->primary->fb->width - x;
149         h = crtc->primary->fb->height - y;
150
151         return sti_layer_prepare(layer, crtc,
152                         crtc->primary->fb, &crtc->mode,
153                         mixer->id, 0, 0, w, h, x, y, w, h);
154 }
155
156 static int sti_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
157                                       struct drm_framebuffer *old_fb)
158 {
159         struct sti_mixer *mixer = to_sti_mixer(crtc);
160         struct sti_layer *layer;
161         unsigned int w, h;
162         int ret;
163
164         DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d (%d,%d)\n",
165                       crtc->base.id, sti_mixer_to_str(mixer),
166                       crtc->primary->fb->base.id, x, y);
167
168         /* GDP is reserved to the CRTC FB */
169         layer = to_sti_layer(crtc->primary);
170         if (!layer) {
171                 DRM_ERROR("Can not find GDP0)\n");
172                 ret = -EINVAL;
173                 goto out;
174         }
175
176         w = crtc->primary->fb->width - crtc->x;
177         h = crtc->primary->fb->height - crtc->y;
178
179         ret = sti_layer_prepare(layer, crtc,
180                                 crtc->primary->fb, &crtc->mode,
181                                 mixer->id, 0, 0, w, h,
182                                 crtc->x, crtc->y, w, h);
183         if (ret) {
184                 DRM_ERROR("Can not prepare layer\n");
185                 goto out;
186         }
187
188         sti_drm_crtc_commit(crtc);
189 out:
190         return ret;
191 }
192
193 static void sti_drm_crtc_load_lut(struct drm_crtc *crtc)
194 {
195         /* do nothing */
196 }
197
198 static void sti_drm_crtc_disable(struct drm_crtc *crtc)
199 {
200         struct sti_mixer *mixer = to_sti_mixer(crtc);
201         struct device *dev = mixer->dev;
202         struct sti_compositor *compo = dev_get_drvdata(dev);
203         struct sti_layer *layer;
204
205         if (!mixer->enabled)
206                 return;
207
208         DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
209
210         /* Disable Background */
211         sti_mixer_set_background_status(mixer, false);
212
213         /* Disable GDP */
214         layer = to_sti_layer(crtc->primary);
215         if (!layer) {
216                 DRM_ERROR("Cannot find GDP0\n");
217                 return;
218         }
219
220         /* Disable layer at mixer level */
221         if (sti_mixer_set_layer_status(mixer, layer, false))
222                 DRM_ERROR("Can not disable %s layer at mixer\n",
223                                 sti_layer_to_str(layer));
224
225         /* Wait a while to be sure that a Vsync event is received */
226         msleep(WAIT_NEXT_VSYNC_MS);
227
228         /* Then disable layer itself */
229         sti_layer_disable(layer);
230
231         drm_crtc_vblank_off(crtc);
232
233         /* Disable pixel clock and compo IP clocks */
234         if (mixer->id == STI_MIXER_MAIN) {
235                 clk_disable_unprepare(compo->clk_pix_main);
236                 clk_disable_unprepare(compo->clk_compo_main);
237         } else {
238                 clk_disable_unprepare(compo->clk_pix_aux);
239                 clk_disable_unprepare(compo->clk_compo_aux);
240         }
241
242         mixer->enabled = false;
243 }
244
245 static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
246         .dpms = sti_drm_crtc_dpms,
247         .prepare = sti_drm_crtc_prepare,
248         .commit = sti_drm_crtc_commit,
249         .mode_fixup = sti_drm_crtc_mode_fixup,
250         .mode_set = sti_drm_crtc_mode_set,
251         .mode_set_base = sti_drm_crtc_mode_set_base,
252         .load_lut = sti_drm_crtc_load_lut,
253         .disable = sti_drm_crtc_disable,
254 };
255
256 static int sti_drm_crtc_page_flip(struct drm_crtc *crtc,
257                                   struct drm_framebuffer *fb,
258                                   struct drm_pending_vblank_event *event,
259                                   uint32_t page_flip_flags)
260 {
261         struct drm_device *drm_dev = crtc->dev;
262         struct drm_framebuffer *old_fb;
263         struct sti_mixer *mixer = to_sti_mixer(crtc);
264         unsigned long flags;
265         int ret;
266
267         DRM_DEBUG_KMS("fb %d --> fb %d\n",
268                         crtc->primary->fb->base.id, fb->base.id);
269
270         mutex_lock(&drm_dev->struct_mutex);
271
272         old_fb = crtc->primary->fb;
273         crtc->primary->fb = fb;
274         ret = sti_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, old_fb);
275         if (ret) {
276                 DRM_ERROR("failed\n");
277                 crtc->primary->fb = old_fb;
278                 goto out;
279         }
280
281         if (event) {
282                 event->pipe = mixer->id;
283
284                 ret = drm_vblank_get(drm_dev, event->pipe);
285                 if (ret) {
286                         DRM_ERROR("Cannot get vblank\n");
287                         goto out;
288                 }
289
290                 spin_lock_irqsave(&drm_dev->event_lock, flags);
291                 if (mixer->pending_event) {
292                         drm_vblank_put(drm_dev, event->pipe);
293                         ret = -EBUSY;
294                 } else {
295                         mixer->pending_event = event;
296                 }
297                 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
298         }
299 out:
300         mutex_unlock(&drm_dev->struct_mutex);
301         return ret;
302 }
303
304 static void sti_drm_crtc_destroy(struct drm_crtc *crtc)
305 {
306         DRM_DEBUG_KMS("\n");
307         drm_crtc_cleanup(crtc);
308 }
309
310 static int sti_drm_crtc_set_property(struct drm_crtc *crtc,
311                                      struct drm_property *property,
312                                      uint64_t val)
313 {
314         DRM_DEBUG_KMS("\n");
315         return 0;
316 }
317
318 int sti_drm_crtc_vblank_cb(struct notifier_block *nb,
319                            unsigned long event, void *data)
320 {
321         struct drm_device *drm_dev;
322         struct sti_compositor *compo =
323                 container_of(nb, struct sti_compositor, vtg_vblank_nb);
324         int *crtc = data;
325         unsigned long flags;
326         struct sti_drm_private *priv;
327
328         drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
329         priv = drm_dev->dev_private;
330
331         if ((event != VTG_TOP_FIELD_EVENT) &&
332             (event != VTG_BOTTOM_FIELD_EVENT)) {
333                 DRM_ERROR("unknown event: %lu\n", event);
334                 return -EINVAL;
335         }
336
337         drm_handle_vblank(drm_dev, *crtc);
338
339         spin_lock_irqsave(&drm_dev->event_lock, flags);
340         if (compo->mixer[*crtc]->pending_event) {
341                 drm_send_vblank_event(drm_dev, -1,
342                                 compo->mixer[*crtc]->pending_event);
343                 drm_vblank_put(drm_dev, *crtc);
344                 compo->mixer[*crtc]->pending_event = NULL;
345         }
346         spin_unlock_irqrestore(&drm_dev->event_lock, flags);
347
348         return 0;
349 }
350
351 int sti_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
352 {
353         struct sti_drm_private *dev_priv = dev->dev_private;
354         struct sti_compositor *compo = dev_priv->compo;
355         struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
356
357         if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
358                         compo->vtg_main : compo->vtg_aux,
359                         vtg_vblank_nb, crtc)) {
360                 DRM_ERROR("Cannot register VTG notifier\n");
361                 return -EINVAL;
362         }
363
364         return 0;
365 }
366 EXPORT_SYMBOL(sti_drm_crtc_enable_vblank);
367
368 void sti_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
369 {
370         struct sti_drm_private *priv = dev->dev_private;
371         struct sti_compositor *compo = priv->compo;
372         struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
373
374         DRM_DEBUG_DRIVER("\n");
375
376         if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
377                         compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
378                 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
379
380         /* free the resources of the pending requests */
381         if (compo->mixer[crtc]->pending_event) {
382                 drm_vblank_put(dev, crtc);
383                 compo->mixer[crtc]->pending_event = NULL;
384         }
385 }
386 EXPORT_SYMBOL(sti_drm_crtc_disable_vblank);
387
388 static struct drm_crtc_funcs sti_crtc_funcs = {
389         .set_config = drm_crtc_helper_set_config,
390         .page_flip = sti_drm_crtc_page_flip,
391         .destroy = sti_drm_crtc_destroy,
392         .set_property = sti_drm_crtc_set_property,
393 };
394
395 bool sti_drm_crtc_is_main(struct drm_crtc *crtc)
396 {
397         struct sti_mixer *mixer = to_sti_mixer(crtc);
398
399         if (mixer->id == STI_MIXER_MAIN)
400                 return true;
401
402         return false;
403 }
404 EXPORT_SYMBOL(sti_drm_crtc_is_main);
405
406 int sti_drm_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
407                 struct drm_plane *primary, struct drm_plane *cursor)
408 {
409         struct drm_crtc *crtc = &mixer->drm_crtc;
410         int res;
411
412         res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
413                         &sti_crtc_funcs);
414         if (res) {
415                 DRM_ERROR("Can not initialze CRTC\n");
416                 return -EINVAL;
417         }
418
419         drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
420
421         DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
422                          crtc->base.id, sti_mixer_to_str(mixer));
423
424         return 0;
425 }