Merge tag 'imx-fixes-5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/shawnguo...
[linux-2.6-microblaze.git] / drivers / gpu / drm / sti / sti_crtc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2014
4  * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
5  *          Fabien Dessenne <fabien.dessenne@st.com>
6  *          for STMicroelectronics.
7  */
8
9 #include <linux/clk.h>
10
11 #include <drm/drmP.h>
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_probe_helper.h>
16
17 #include "sti_compositor.h"
18 #include "sti_crtc.h"
19 #include "sti_drv.h"
20 #include "sti_vid.h"
21 #include "sti_vtg.h"
22
23 static void sti_crtc_atomic_enable(struct drm_crtc *crtc,
24                                    struct drm_crtc_state *old_state)
25 {
26         struct sti_mixer *mixer = to_sti_mixer(crtc);
27
28         DRM_DEBUG_DRIVER("\n");
29
30         mixer->status = STI_MIXER_READY;
31
32         drm_crtc_vblank_on(crtc);
33 }
34
35 static void sti_crtc_atomic_disable(struct drm_crtc *crtc,
36                                     struct drm_crtc_state *old_state)
37 {
38         struct sti_mixer *mixer = to_sti_mixer(crtc);
39
40         DRM_DEBUG_DRIVER("\n");
41
42         mixer->status = STI_MIXER_DISABLING;
43
44         drm_crtc_wait_one_vblank(crtc);
45 }
46
47 static int
48 sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
49 {
50         struct sti_mixer *mixer = to_sti_mixer(crtc);
51         struct device *dev = mixer->dev;
52         struct sti_compositor *compo = dev_get_drvdata(dev);
53         struct clk *compo_clk, *pix_clk;
54         int rate = mode->clock * 1000;
55
56         DRM_DEBUG_KMS("CRTC:%d (%s) mode: (%s)\n",
57                       crtc->base.id, sti_mixer_to_str(mixer), mode->name);
58
59         DRM_DEBUG_KMS(DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
60
61         if (mixer->id == STI_MIXER_MAIN) {
62                 compo_clk = compo->clk_compo_main;
63                 pix_clk = compo->clk_pix_main;
64         } else {
65                 compo_clk = compo->clk_compo_aux;
66                 pix_clk = compo->clk_pix_aux;
67         }
68
69         /* Prepare and enable the compo IP clock */
70         if (clk_prepare_enable(compo_clk)) {
71                 DRM_INFO("Failed to prepare/enable compositor clk\n");
72                 goto compo_error;
73         }
74
75         /* Set rate and prepare/enable pixel clock */
76         if (clk_set_rate(pix_clk, rate) < 0) {
77                 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
78                 goto pix_error;
79         }
80         if (clk_prepare_enable(pix_clk)) {
81                 DRM_ERROR("Failed to prepare/enable pix clk\n");
82                 goto pix_error;
83         }
84
85         sti_vtg_set_config(compo->vtg[mixer->id], &crtc->mode);
86
87         if (sti_mixer_active_video_area(mixer, &crtc->mode)) {
88                 DRM_ERROR("Can't set active video area\n");
89                 goto mixer_error;
90         }
91
92         return 0;
93
94 mixer_error:
95         clk_disable_unprepare(pix_clk);
96 pix_error:
97         clk_disable_unprepare(compo_clk);
98 compo_error:
99         return -EINVAL;
100 }
101
102 static void sti_crtc_disable(struct drm_crtc *crtc)
103 {
104         struct sti_mixer *mixer = to_sti_mixer(crtc);
105         struct device *dev = mixer->dev;
106         struct sti_compositor *compo = dev_get_drvdata(dev);
107
108         DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
109
110         /* Disable Background */
111         sti_mixer_set_background_status(mixer, false);
112
113         drm_crtc_vblank_off(crtc);
114
115         /* Disable pixel clock and compo IP clocks */
116         if (mixer->id == STI_MIXER_MAIN) {
117                 clk_disable_unprepare(compo->clk_pix_main);
118                 clk_disable_unprepare(compo->clk_compo_main);
119         } else {
120                 clk_disable_unprepare(compo->clk_pix_aux);
121                 clk_disable_unprepare(compo->clk_compo_aux);
122         }
123
124         mixer->status = STI_MIXER_DISABLED;
125 }
126
127 static void
128 sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
129 {
130         sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
131 }
132
133 static void sti_crtc_atomic_flush(struct drm_crtc *crtc,
134                                   struct drm_crtc_state *old_crtc_state)
135 {
136         struct drm_device *drm_dev = crtc->dev;
137         struct sti_mixer *mixer = to_sti_mixer(crtc);
138         struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
139         struct drm_plane *p;
140         struct drm_pending_vblank_event *event;
141         unsigned long flags;
142
143         DRM_DEBUG_DRIVER("\n");
144
145         /* perform plane actions */
146         list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
147                 struct sti_plane *plane = to_sti_plane(p);
148
149                 switch (plane->status) {
150                 case STI_PLANE_UPDATED:
151                         /* ignore update for other CRTC */
152                         if (p->state->crtc != crtc)
153                                 continue;
154
155                         /* update planes tag as updated */
156                         DRM_DEBUG_DRIVER("update plane %s\n",
157                                          sti_plane_to_str(plane));
158
159                         if (sti_mixer_set_plane_depth(mixer, plane)) {
160                                 DRM_ERROR("Cannot set plane %s depth\n",
161                                           sti_plane_to_str(plane));
162                                 break;
163                         }
164
165                         if (sti_mixer_set_plane_status(mixer, plane, true)) {
166                                 DRM_ERROR("Cannot enable plane %s at mixer\n",
167                                           sti_plane_to_str(plane));
168                                 break;
169                         }
170
171                         /* if plane is HQVDP_0 then commit the vid[0] */
172                         if (plane->desc == STI_HQVDP_0)
173                                 sti_vid_commit(compo->vid[0], p->state);
174
175                         plane->status = STI_PLANE_READY;
176
177                         break;
178                 case STI_PLANE_DISABLING:
179                         /* disabling sequence for planes tag as disabling */
180                         DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
181                                          sti_plane_to_str(plane));
182
183                         if (sti_mixer_set_plane_status(mixer, plane, false)) {
184                                 DRM_ERROR("Cannot disable plane %s at mixer\n",
185                                           sti_plane_to_str(plane));
186                                 continue;
187                         }
188
189                         if (plane->desc == STI_CURSOR)
190                                 /* tag plane status for disabled */
191                                 plane->status = STI_PLANE_DISABLED;
192                         else
193                                 /* tag plane status for flushing */
194                                 plane->status = STI_PLANE_FLUSHING;
195
196                         /* if plane is HQVDP_0 then disable the vid[0] */
197                         if (plane->desc == STI_HQVDP_0)
198                                 sti_vid_disable(compo->vid[0]);
199
200                         break;
201                 default:
202                         /* Other status case are not handled */
203                         break;
204                 }
205         }
206
207         event = crtc->state->event;
208         if (event) {
209                 crtc->state->event = NULL;
210
211                 spin_lock_irqsave(&crtc->dev->event_lock, flags);
212                 if (drm_crtc_vblank_get(crtc) == 0)
213                         drm_crtc_arm_vblank_event(crtc, event);
214                 else
215                         drm_crtc_send_vblank_event(crtc, event);
216                 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
217         }
218 }
219
220 static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
221         .mode_set_nofb = sti_crtc_mode_set_nofb,
222         .atomic_flush = sti_crtc_atomic_flush,
223         .atomic_enable = sti_crtc_atomic_enable,
224         .atomic_disable = sti_crtc_atomic_disable,
225 };
226
227 static void sti_crtc_destroy(struct drm_crtc *crtc)
228 {
229         DRM_DEBUG_KMS("\n");
230         drm_crtc_cleanup(crtc);
231 }
232
233 static int sti_crtc_set_property(struct drm_crtc *crtc,
234                                  struct drm_property *property,
235                                  uint64_t val)
236 {
237         DRM_DEBUG_KMS("\n");
238         return 0;
239 }
240
241 int sti_crtc_vblank_cb(struct notifier_block *nb,
242                        unsigned long event, void *data)
243 {
244         struct sti_compositor *compo;
245         struct drm_crtc *crtc = data;
246         struct sti_mixer *mixer;
247         unsigned int pipe;
248
249         pipe = drm_crtc_index(crtc);
250         compo = container_of(nb, struct sti_compositor, vtg_vblank_nb[pipe]);
251         mixer = compo->mixer[pipe];
252
253         if ((event != VTG_TOP_FIELD_EVENT) &&
254             (event != VTG_BOTTOM_FIELD_EVENT)) {
255                 DRM_ERROR("unknown event: %lu\n", event);
256                 return -EINVAL;
257         }
258
259         drm_crtc_handle_vblank(crtc);
260
261         if (mixer->status == STI_MIXER_DISABLING) {
262                 struct drm_plane *p;
263
264                 /* Disable mixer only if all overlay planes (GDP and VDP)
265                  * are disabled */
266                 list_for_each_entry(p, &crtc->dev->mode_config.plane_list,
267                                     head) {
268                         struct sti_plane *plane = to_sti_plane(p);
269
270                         if ((plane->desc & STI_PLANE_TYPE_MASK) <= STI_VDP)
271                                 if (plane->status != STI_PLANE_DISABLED)
272                                         return 0;
273                 }
274                 sti_crtc_disable(crtc);
275         }
276
277         return 0;
278 }
279
280 int sti_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
281 {
282         struct sti_private *dev_priv = dev->dev_private;
283         struct sti_compositor *compo = dev_priv->compo;
284         struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe];
285         struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
286         struct sti_vtg *vtg = compo->vtg[pipe];
287
288         DRM_DEBUG_DRIVER("\n");
289
290         if (sti_vtg_register_client(vtg, vtg_vblank_nb, crtc)) {
291                 DRM_ERROR("Cannot register VTG notifier\n");
292                 return -EINVAL;
293         }
294
295         return 0;
296 }
297
298 void sti_crtc_disable_vblank(struct drm_device *drm_dev, unsigned int pipe)
299 {
300         struct sti_private *priv = drm_dev->dev_private;
301         struct sti_compositor *compo = priv->compo;
302         struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe];
303         struct sti_vtg *vtg = compo->vtg[pipe];
304
305         DRM_DEBUG_DRIVER("\n");
306
307         if (sti_vtg_unregister_client(vtg, vtg_vblank_nb))
308                 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
309 }
310
311 static int sti_crtc_late_register(struct drm_crtc *crtc)
312 {
313         struct sti_mixer *mixer = to_sti_mixer(crtc);
314         struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
315
316         if (drm_crtc_index(crtc) == 0)
317                 return sti_compositor_debugfs_init(compo, crtc->dev->primary);
318
319         return 0;
320 }
321
322 static const struct drm_crtc_funcs sti_crtc_funcs = {
323         .set_config = drm_atomic_helper_set_config,
324         .page_flip = drm_atomic_helper_page_flip,
325         .destroy = sti_crtc_destroy,
326         .set_property = sti_crtc_set_property,
327         .reset = drm_atomic_helper_crtc_reset,
328         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
329         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
330         .late_register = sti_crtc_late_register,
331 };
332
333 bool sti_crtc_is_main(struct drm_crtc *crtc)
334 {
335         struct sti_mixer *mixer = to_sti_mixer(crtc);
336
337         if (mixer->id == STI_MIXER_MAIN)
338                 return true;
339
340         return false;
341 }
342
343 int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
344                   struct drm_plane *primary, struct drm_plane *cursor)
345 {
346         struct drm_crtc *crtc = &mixer->drm_crtc;
347         int res;
348
349         res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
350                                         &sti_crtc_funcs, NULL);
351         if (res) {
352                 DRM_ERROR("Can't initialize CRTC\n");
353                 return -EINVAL;
354         }
355
356         drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
357
358         DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
359                          crtc->base.id, sti_mixer_to_str(mixer));
360
361         return 0;
362 }