dma-buf/fence: kerneldoc: remove spurious section header
[cascardo/linux.git] / drivers / gpu / drm / mediatek / mtk_drm_crtc.c
1 /*
2  * Copyright (c) 2015 MediaTek Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <asm/barrier.h>
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_plane_helper.h>
19 #include <linux/clk.h>
20 #include <linux/pm_runtime.h>
21 #include <soc/mediatek/smi.h>
22
23 #include "mtk_drm_drv.h"
24 #include "mtk_drm_crtc.h"
25 #include "mtk_drm_ddp.h"
26 #include "mtk_drm_ddp_comp.h"
27 #include "mtk_drm_gem.h"
28 #include "mtk_drm_plane.h"
29
30 /**
31  * struct mtk_drm_crtc - MediaTek specific crtc structure.
32  * @base: crtc object.
33  * @enabled: records whether crtc_enable succeeded
34  * @planes: array of 4 drm_plane structures, one for each overlay plane
35  * @pending_planes: whether any plane has pending changes to be applied
36  * @config_regs: memory mapped mmsys configuration register space
37  * @mutex: handle to one of the ten disp_mutex streams
38  * @ddp_comp_nr: number of components in ddp_comp
39  * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
40  */
41 struct mtk_drm_crtc {
42         struct drm_crtc                 base;
43         bool                            enabled;
44
45         bool                            pending_needs_vblank;
46         struct drm_pending_vblank_event *event;
47
48         struct drm_plane                planes[OVL_LAYER_NR];
49         bool                            pending_planes;
50
51         void __iomem                    *config_regs;
52         struct mtk_disp_mutex           *mutex;
53         unsigned int                    ddp_comp_nr;
54         struct mtk_ddp_comp             **ddp_comp;
55 };
56
57 struct mtk_crtc_state {
58         struct drm_crtc_state           base;
59
60         bool                            pending_config;
61         unsigned int                    pending_width;
62         unsigned int                    pending_height;
63         unsigned int                    pending_vrefresh;
64 };
65
66 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
67 {
68         return container_of(c, struct mtk_drm_crtc, base);
69 }
70
71 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
72 {
73         return container_of(s, struct mtk_crtc_state, base);
74 }
75
76 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
77 {
78         struct drm_crtc *crtc = &mtk_crtc->base;
79         unsigned long flags;
80
81         spin_lock_irqsave(&crtc->dev->event_lock, flags);
82         drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
83         drm_crtc_vblank_put(crtc);
84         mtk_crtc->event = NULL;
85         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
86 }
87
88 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
89 {
90         drm_crtc_handle_vblank(&mtk_crtc->base);
91         if (mtk_crtc->pending_needs_vblank) {
92                 mtk_drm_crtc_finish_page_flip(mtk_crtc);
93                 mtk_crtc->pending_needs_vblank = false;
94         }
95 }
96
97 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
98 {
99         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
100         int i;
101
102         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
103                 clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
104
105         mtk_disp_mutex_put(mtk_crtc->mutex);
106
107         drm_crtc_cleanup(crtc);
108 }
109
110 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
111 {
112         struct mtk_crtc_state *state;
113
114         if (crtc->state) {
115                 __drm_atomic_helper_crtc_destroy_state(crtc->state);
116
117                 state = to_mtk_crtc_state(crtc->state);
118                 memset(state, 0, sizeof(*state));
119         } else {
120                 state = kzalloc(sizeof(*state), GFP_KERNEL);
121                 if (!state)
122                         return;
123                 crtc->state = &state->base;
124         }
125
126         state->base.crtc = crtc;
127 }
128
129 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
130 {
131         struct mtk_crtc_state *state;
132
133         state = kzalloc(sizeof(*state), GFP_KERNEL);
134         if (!state)
135                 return NULL;
136
137         __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
138
139         WARN_ON(state->base.crtc != crtc);
140         state->base.crtc = crtc;
141
142         return &state->base;
143 }
144
145 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
146                                        struct drm_crtc_state *state)
147 {
148         __drm_atomic_helper_crtc_destroy_state(state);
149         kfree(to_mtk_crtc_state(state));
150 }
151
152 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
153                                     const struct drm_display_mode *mode,
154                                     struct drm_display_mode *adjusted_mode)
155 {
156         /* Nothing to do here, but this callback is mandatory. */
157         return true;
158 }
159
160 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
161 {
162         struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
163
164         state->pending_width = crtc->mode.hdisplay;
165         state->pending_height = crtc->mode.vdisplay;
166         state->pending_vrefresh = crtc->mode.vrefresh;
167         wmb();  /* Make sure the above parameters are set before update */
168         state->pending_config = true;
169 }
170
171 int mtk_drm_crtc_enable_vblank(struct drm_device *drm, unsigned int pipe)
172 {
173         struct mtk_drm_private *priv = drm->dev_private;
174         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(priv->crtc[pipe]);
175         struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
176
177         mtk_ddp_comp_enable_vblank(ovl, &mtk_crtc->base);
178
179         return 0;
180 }
181
182 void mtk_drm_crtc_disable_vblank(struct drm_device *drm, unsigned int pipe)
183 {
184         struct mtk_drm_private *priv = drm->dev_private;
185         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(priv->crtc[pipe]);
186         struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
187
188         mtk_ddp_comp_disable_vblank(ovl);
189 }
190
191 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
192 {
193         int ret;
194         int i;
195
196         DRM_DEBUG_DRIVER("%s\n", __func__);
197         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
198                 ret = clk_enable(mtk_crtc->ddp_comp[i]->clk);
199                 if (ret) {
200                         DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
201                         goto err;
202                 }
203         }
204
205         return 0;
206 err:
207         while (--i >= 0)
208                 clk_disable(mtk_crtc->ddp_comp[i]->clk);
209         return ret;
210 }
211
212 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
213 {
214         int i;
215
216         DRM_DEBUG_DRIVER("%s\n", __func__);
217         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
218                 clk_disable(mtk_crtc->ddp_comp[i]->clk);
219 }
220
221 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
222 {
223         struct drm_crtc *crtc = &mtk_crtc->base;
224         unsigned int width, height, vrefresh;
225         int ret;
226         int i;
227
228         DRM_DEBUG_DRIVER("%s\n", __func__);
229         if (WARN_ON(!crtc->state))
230                 return -EINVAL;
231
232         width = crtc->state->adjusted_mode.hdisplay;
233         height = crtc->state->adjusted_mode.vdisplay;
234         vrefresh = crtc->state->adjusted_mode.vrefresh;
235
236         ret = pm_runtime_get_sync(crtc->dev->dev);
237         if (ret < 0) {
238                 DRM_ERROR("Failed to enable power domain: %d\n", ret);
239                 return ret;
240         }
241
242         ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
243         if (ret < 0) {
244                 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
245                 goto err_pm_runtime_put;
246         }
247
248         ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
249         if (ret < 0) {
250                 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
251                 goto err_mutex_unprepare;
252         }
253
254         DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
255         for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
256                 mtk_ddp_add_comp_to_path(mtk_crtc->config_regs,
257                                          mtk_crtc->ddp_comp[i]->id,
258                                          mtk_crtc->ddp_comp[i + 1]->id);
259                 mtk_disp_mutex_add_comp(mtk_crtc->mutex,
260                                         mtk_crtc->ddp_comp[i]->id);
261         }
262         mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
263         mtk_disp_mutex_enable(mtk_crtc->mutex);
264
265         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
266                 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
267
268                 mtk_ddp_comp_config(comp, width, height, vrefresh);
269                 mtk_ddp_comp_start(comp);
270         }
271
272         /* Initially configure all planes */
273         for (i = 0; i < OVL_LAYER_NR; i++) {
274                 struct drm_plane *plane = &mtk_crtc->planes[i];
275                 struct mtk_plane_state *plane_state;
276
277                 plane_state = to_mtk_plane_state(plane->state);
278                 mtk_ddp_comp_layer_config(mtk_crtc->ddp_comp[0], i,
279                                           plane_state);
280         }
281
282         return 0;
283
284 err_mutex_unprepare:
285         mtk_disp_mutex_unprepare(mtk_crtc->mutex);
286 err_pm_runtime_put:
287         pm_runtime_put(crtc->dev->dev);
288         return ret;
289 }
290
291 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
292 {
293         struct drm_device *drm = mtk_crtc->base.dev;
294         int i;
295
296         DRM_DEBUG_DRIVER("%s\n", __func__);
297         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
298                 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
299         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
300                 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
301                                            mtk_crtc->ddp_comp[i]->id);
302         mtk_disp_mutex_disable(mtk_crtc->mutex);
303         for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
304                 mtk_ddp_remove_comp_from_path(mtk_crtc->config_regs,
305                                               mtk_crtc->ddp_comp[i]->id,
306                                               mtk_crtc->ddp_comp[i + 1]->id);
307                 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
308                                            mtk_crtc->ddp_comp[i]->id);
309         }
310         mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
311         mtk_crtc_ddp_clk_disable(mtk_crtc);
312         mtk_disp_mutex_unprepare(mtk_crtc->mutex);
313
314         pm_runtime_put(drm->dev);
315 }
316
317 static void mtk_drm_crtc_enable(struct drm_crtc *crtc)
318 {
319         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
320         struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
321         int ret;
322
323         DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
324
325         ret = mtk_smi_larb_get(ovl->larb_dev);
326         if (ret) {
327                 DRM_ERROR("Failed to get larb: %d\n", ret);
328                 return;
329         }
330
331         ret = mtk_crtc_ddp_hw_init(mtk_crtc);
332         if (ret) {
333                 mtk_smi_larb_put(ovl->larb_dev);
334                 return;
335         }
336
337         drm_crtc_vblank_on(crtc);
338         mtk_crtc->enabled = true;
339 }
340
341 static void mtk_drm_crtc_disable(struct drm_crtc *crtc)
342 {
343         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
344         struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
345         int i;
346
347         DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
348         if (!mtk_crtc->enabled)
349                 return;
350
351         /* Set all pending plane state to disabled */
352         for (i = 0; i < OVL_LAYER_NR; i++) {
353                 struct drm_plane *plane = &mtk_crtc->planes[i];
354                 struct mtk_plane_state *plane_state;
355
356                 plane_state = to_mtk_plane_state(plane->state);
357                 plane_state->pending.enable = false;
358                 plane_state->pending.config = true;
359         }
360         mtk_crtc->pending_planes = true;
361
362         /* Wait for planes to be disabled */
363         drm_crtc_wait_one_vblank(crtc);
364
365         drm_crtc_vblank_off(crtc);
366         mtk_crtc_ddp_hw_fini(mtk_crtc);
367         mtk_smi_larb_put(ovl->larb_dev);
368
369         mtk_crtc->enabled = false;
370 }
371
372 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
373                                       struct drm_crtc_state *old_crtc_state)
374 {
375         struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
376         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
377
378         if (mtk_crtc->event && state->base.event)
379                 DRM_ERROR("new event while there is still a pending event\n");
380
381         if (state->base.event) {
382                 state->base.event->pipe = drm_crtc_index(crtc);
383                 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
384                 mtk_crtc->event = state->base.event;
385                 state->base.event = NULL;
386         }
387 }
388
389 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
390                                       struct drm_crtc_state *old_crtc_state)
391 {
392         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
393         unsigned int pending_planes = 0;
394         int i;
395
396         if (mtk_crtc->event)
397                 mtk_crtc->pending_needs_vblank = true;
398         for (i = 0; i < OVL_LAYER_NR; i++) {
399                 struct drm_plane *plane = &mtk_crtc->planes[i];
400                 struct mtk_plane_state *plane_state;
401
402                 plane_state = to_mtk_plane_state(plane->state);
403                 if (plane_state->pending.dirty) {
404                         plane_state->pending.config = true;
405                         plane_state->pending.dirty = false;
406                         pending_planes |= BIT(i);
407                 }
408         }
409         if (pending_planes)
410                 mtk_crtc->pending_planes = true;
411 }
412
413 static const struct drm_crtc_funcs mtk_crtc_funcs = {
414         .set_config             = drm_atomic_helper_set_config,
415         .page_flip              = drm_atomic_helper_page_flip,
416         .destroy                = mtk_drm_crtc_destroy,
417         .reset                  = mtk_drm_crtc_reset,
418         .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
419         .atomic_destroy_state   = mtk_drm_crtc_destroy_state,
420 };
421
422 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
423         .mode_fixup     = mtk_drm_crtc_mode_fixup,
424         .mode_set_nofb  = mtk_drm_crtc_mode_set_nofb,
425         .enable         = mtk_drm_crtc_enable,
426         .disable        = mtk_drm_crtc_disable,
427         .atomic_begin   = mtk_drm_crtc_atomic_begin,
428         .atomic_flush   = mtk_drm_crtc_atomic_flush,
429 };
430
431 static int mtk_drm_crtc_init(struct drm_device *drm,
432                              struct mtk_drm_crtc *mtk_crtc,
433                              struct drm_plane *primary,
434                              struct drm_plane *cursor, unsigned int pipe)
435 {
436         int ret;
437
438         ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
439                                         &mtk_crtc_funcs, NULL);
440         if (ret)
441                 goto err_cleanup_crtc;
442
443         drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
444
445         return 0;
446
447 err_cleanup_crtc:
448         drm_crtc_cleanup(&mtk_crtc->base);
449         return ret;
450 }
451
452 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *ovl)
453 {
454         struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
455         struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
456         unsigned int i;
457
458         /*
459          * TODO: instead of updating the registers here, we should prepare
460          * working registers in atomic_commit and let the hardware command
461          * queue update module registers on vblank.
462          */
463         if (state->pending_config) {
464                 mtk_ddp_comp_config(ovl, state->pending_width,
465                                     state->pending_height,
466                                     state->pending_vrefresh);
467
468                 state->pending_config = false;
469         }
470
471         if (mtk_crtc->pending_planes) {
472                 for (i = 0; i < OVL_LAYER_NR; i++) {
473                         struct drm_plane *plane = &mtk_crtc->planes[i];
474                         struct mtk_plane_state *plane_state;
475
476                         plane_state = to_mtk_plane_state(plane->state);
477
478                         if (plane_state->pending.config) {
479                                 mtk_ddp_comp_layer_config(ovl, i, plane_state);
480                                 plane_state->pending.config = false;
481                         }
482                 }
483                 mtk_crtc->pending_planes = false;
484         }
485
486         mtk_drm_finish_page_flip(mtk_crtc);
487 }
488
489 int mtk_drm_crtc_create(struct drm_device *drm_dev,
490                         const enum mtk_ddp_comp_id *path, unsigned int path_len)
491 {
492         struct mtk_drm_private *priv = drm_dev->dev_private;
493         struct device *dev = drm_dev->dev;
494         struct mtk_drm_crtc *mtk_crtc;
495         enum drm_plane_type type;
496         unsigned int zpos;
497         int pipe = priv->num_pipes;
498         int ret;
499         int i;
500
501         for (i = 0; i < path_len; i++) {
502                 enum mtk_ddp_comp_id comp_id = path[i];
503                 struct device_node *node;
504
505                 node = priv->comp_node[comp_id];
506                 if (!node) {
507                         dev_info(dev,
508                                  "Not creating crtc %d because component %d is disabled or missing\n",
509                                  pipe, comp_id);
510                         return 0;
511                 }
512         }
513
514         mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
515         if (!mtk_crtc)
516                 return -ENOMEM;
517
518         mtk_crtc->config_regs = priv->config_regs;
519         mtk_crtc->ddp_comp_nr = path_len;
520         mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
521                                                 sizeof(*mtk_crtc->ddp_comp),
522                                                 GFP_KERNEL);
523
524         mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
525         if (IS_ERR(mtk_crtc->mutex)) {
526                 ret = PTR_ERR(mtk_crtc->mutex);
527                 dev_err(dev, "Failed to get mutex: %d\n", ret);
528                 return ret;
529         }
530
531         for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
532                 enum mtk_ddp_comp_id comp_id = path[i];
533                 struct mtk_ddp_comp *comp;
534                 struct device_node *node;
535
536                 node = priv->comp_node[comp_id];
537                 comp = priv->ddp_comp[comp_id];
538                 if (!comp) {
539                         dev_err(dev, "Component %s not initialized\n",
540                                 node->full_name);
541                         ret = -ENODEV;
542                         goto unprepare;
543                 }
544
545                 ret = clk_prepare(comp->clk);
546                 if (ret) {
547                         dev_err(dev,
548                                 "Failed to prepare clock for component %s: %d\n",
549                                 node->full_name, ret);
550                         goto unprepare;
551                 }
552
553                 mtk_crtc->ddp_comp[i] = comp;
554         }
555
556         for (zpos = 0; zpos < OVL_LAYER_NR; zpos++) {
557                 type = (zpos == 0) ? DRM_PLANE_TYPE_PRIMARY :
558                                 (zpos == 1) ? DRM_PLANE_TYPE_CURSOR :
559                                                 DRM_PLANE_TYPE_OVERLAY;
560                 ret = mtk_plane_init(drm_dev, &mtk_crtc->planes[zpos],
561                                      BIT(pipe), type);
562                 if (ret)
563                         goto unprepare;
564         }
565
566         ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, &mtk_crtc->planes[0],
567                                 &mtk_crtc->planes[1], pipe);
568         if (ret < 0)
569                 goto unprepare;
570
571         priv->crtc[pipe] = &mtk_crtc->base;
572         priv->num_pipes++;
573
574         return 0;
575
576 unprepare:
577         while (--i >= 0)
578                 clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
579
580         return ret;
581 }