Linux 4.9.243
[linux/fpc-iii.git] / drivers / gpu / drm / mediatek / mtk_drm_crtc.c
blob1ed60da76a0ce5713b4b42ab0fdfa97367a7ee41
1 /*
2 * Copyright (c) 2015 MediaTek Inc.
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.
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.
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>
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"
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
41 struct mtk_drm_crtc {
42 struct drm_crtc base;
43 bool enabled;
45 bool pending_needs_vblank;
46 struct drm_pending_vblank_event *event;
48 struct drm_plane planes[OVL_LAYER_NR];
49 bool pending_planes;
51 void __iomem *config_regs;
52 struct mtk_disp_mutex *mutex;
53 unsigned int ddp_comp_nr;
54 struct mtk_ddp_comp **ddp_comp;
57 struct mtk_crtc_state {
58 struct drm_crtc_state base;
60 bool pending_config;
61 unsigned int pending_width;
62 unsigned int pending_height;
63 unsigned int pending_vrefresh;
66 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
68 return container_of(c, struct mtk_drm_crtc, base);
71 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
73 return container_of(s, struct mtk_crtc_state, base);
76 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
78 struct drm_crtc *crtc = &mtk_crtc->base;
79 unsigned long flags;
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);
88 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
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;
97 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
99 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
100 int i;
102 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
103 clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
105 mtk_disp_mutex_put(mtk_crtc->mutex);
107 drm_crtc_cleanup(crtc);
110 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
112 struct mtk_crtc_state *state;
114 if (crtc->state) {
115 __drm_atomic_helper_crtc_destroy_state(crtc->state);
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;
126 state->base.crtc = crtc;
129 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
131 struct mtk_crtc_state *state;
133 state = kzalloc(sizeof(*state), GFP_KERNEL);
134 if (!state)
135 return NULL;
137 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
139 WARN_ON(state->base.crtc != crtc);
140 state->base.crtc = crtc;
142 return &state->base;
145 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
146 struct drm_crtc_state *state)
148 __drm_atomic_helper_crtc_destroy_state(state);
149 kfree(to_mtk_crtc_state(state));
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)
156 /* Nothing to do here, but this callback is mandatory. */
157 return true;
160 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
162 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
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;
171 int mtk_drm_crtc_enable_vblank(struct drm_device *drm, unsigned int pipe)
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];
177 mtk_ddp_comp_enable_vblank(ovl, &mtk_crtc->base);
179 return 0;
182 void mtk_drm_crtc_disable_vblank(struct drm_device *drm, unsigned int pipe)
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];
188 mtk_ddp_comp_disable_vblank(ovl);
191 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
193 int ret;
194 int i;
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;
205 return 0;
206 err:
207 while (--i >= 0)
208 clk_disable(mtk_crtc->ddp_comp[i]->clk);
209 return ret;
212 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
214 int i;
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);
221 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
223 struct drm_crtc *crtc = &mtk_crtc->base;
224 struct drm_connector *connector;
225 struct drm_encoder *encoder;
226 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
227 int ret;
228 int i;
230 DRM_DEBUG_DRIVER("%s\n", __func__);
231 if (WARN_ON(!crtc->state))
232 return -EINVAL;
234 width = crtc->state->adjusted_mode.hdisplay;
235 height = crtc->state->adjusted_mode.vdisplay;
236 vrefresh = crtc->state->adjusted_mode.vrefresh;
238 drm_for_each_encoder(encoder, crtc->dev) {
239 if (encoder->crtc != crtc)
240 continue;
242 drm_for_each_connector(connector, crtc->dev) {
243 if (connector->encoder != encoder)
244 continue;
245 if (connector->display_info.bpc != 0 &&
246 bpc > connector->display_info.bpc)
247 bpc = connector->display_info.bpc;
251 ret = pm_runtime_get_sync(crtc->dev->dev);
252 if (ret < 0) {
253 DRM_ERROR("Failed to enable power domain: %d\n", ret);
254 return ret;
257 ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
258 if (ret < 0) {
259 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
260 goto err_pm_runtime_put;
263 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
264 if (ret < 0) {
265 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
266 goto err_mutex_unprepare;
269 DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
270 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
271 mtk_ddp_add_comp_to_path(mtk_crtc->config_regs,
272 mtk_crtc->ddp_comp[i]->id,
273 mtk_crtc->ddp_comp[i + 1]->id);
274 mtk_disp_mutex_add_comp(mtk_crtc->mutex,
275 mtk_crtc->ddp_comp[i]->id);
277 mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
278 mtk_disp_mutex_enable(mtk_crtc->mutex);
280 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
281 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
283 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc);
284 mtk_ddp_comp_start(comp);
287 /* Initially configure all planes */
288 for (i = 0; i < OVL_LAYER_NR; i++) {
289 struct drm_plane *plane = &mtk_crtc->planes[i];
290 struct mtk_plane_state *plane_state;
292 plane_state = to_mtk_plane_state(plane->state);
293 mtk_ddp_comp_layer_config(mtk_crtc->ddp_comp[0], i,
294 plane_state);
297 return 0;
299 err_mutex_unprepare:
300 mtk_disp_mutex_unprepare(mtk_crtc->mutex);
301 err_pm_runtime_put:
302 pm_runtime_put(crtc->dev->dev);
303 return ret;
306 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
308 struct drm_device *drm = mtk_crtc->base.dev;
309 struct drm_crtc *crtc = &mtk_crtc->base;
310 int i;
312 DRM_DEBUG_DRIVER("%s\n", __func__);
313 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
314 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
315 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
316 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
317 mtk_crtc->ddp_comp[i]->id);
318 mtk_disp_mutex_disable(mtk_crtc->mutex);
319 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
320 mtk_ddp_remove_comp_from_path(mtk_crtc->config_regs,
321 mtk_crtc->ddp_comp[i]->id,
322 mtk_crtc->ddp_comp[i + 1]->id);
323 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
324 mtk_crtc->ddp_comp[i]->id);
326 mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
327 mtk_crtc_ddp_clk_disable(mtk_crtc);
328 mtk_disp_mutex_unprepare(mtk_crtc->mutex);
330 pm_runtime_put(drm->dev);
332 if (crtc->state->event && !crtc->state->active) {
333 spin_lock_irq(&crtc->dev->event_lock);
334 drm_crtc_send_vblank_event(crtc, crtc->state->event);
335 crtc->state->event = NULL;
336 spin_unlock_irq(&crtc->dev->event_lock);
340 static void mtk_drm_crtc_enable(struct drm_crtc *crtc)
342 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
343 struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
344 int ret;
346 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
348 ret = mtk_smi_larb_get(ovl->larb_dev);
349 if (ret) {
350 DRM_ERROR("Failed to get larb: %d\n", ret);
351 return;
354 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
355 if (ret) {
356 mtk_smi_larb_put(ovl->larb_dev);
357 return;
360 drm_crtc_vblank_on(crtc);
361 mtk_crtc->enabled = true;
364 static void mtk_drm_crtc_disable(struct drm_crtc *crtc)
366 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
367 struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
368 int i;
370 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
371 if (!mtk_crtc->enabled)
372 return;
374 /* Set all pending plane state to disabled */
375 for (i = 0; i < OVL_LAYER_NR; i++) {
376 struct drm_plane *plane = &mtk_crtc->planes[i];
377 struct mtk_plane_state *plane_state;
379 plane_state = to_mtk_plane_state(plane->state);
380 plane_state->pending.enable = false;
381 plane_state->pending.config = true;
383 mtk_crtc->pending_planes = true;
385 /* Wait for planes to be disabled */
386 drm_crtc_wait_one_vblank(crtc);
388 drm_crtc_vblank_off(crtc);
389 mtk_crtc_ddp_hw_fini(mtk_crtc);
390 mtk_smi_larb_put(ovl->larb_dev);
392 mtk_crtc->enabled = false;
395 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
396 struct drm_crtc_state *old_crtc_state)
398 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
399 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
401 if (mtk_crtc->event && state->base.event)
402 DRM_ERROR("new event while there is still a pending event\n");
404 if (state->base.event) {
405 state->base.event->pipe = drm_crtc_index(crtc);
406 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
407 mtk_crtc->event = state->base.event;
408 state->base.event = NULL;
412 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
413 struct drm_crtc_state *old_crtc_state)
415 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
416 unsigned int pending_planes = 0;
417 int i;
419 if (mtk_crtc->event)
420 mtk_crtc->pending_needs_vblank = true;
421 for (i = 0; i < OVL_LAYER_NR; i++) {
422 struct drm_plane *plane = &mtk_crtc->planes[i];
423 struct mtk_plane_state *plane_state;
425 plane_state = to_mtk_plane_state(plane->state);
426 if (plane_state->pending.dirty) {
427 plane_state->pending.config = true;
428 plane_state->pending.dirty = false;
429 pending_planes |= BIT(i);
432 if (pending_planes)
433 mtk_crtc->pending_planes = true;
434 if (crtc->state->color_mgmt_changed)
435 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
436 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
439 static const struct drm_crtc_funcs mtk_crtc_funcs = {
440 .set_config = drm_atomic_helper_set_config,
441 .page_flip = drm_atomic_helper_page_flip,
442 .destroy = mtk_drm_crtc_destroy,
443 .reset = mtk_drm_crtc_reset,
444 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
445 .atomic_destroy_state = mtk_drm_crtc_destroy_state,
446 .gamma_set = drm_atomic_helper_legacy_gamma_set,
449 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
450 .mode_fixup = mtk_drm_crtc_mode_fixup,
451 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
452 .enable = mtk_drm_crtc_enable,
453 .disable = mtk_drm_crtc_disable,
454 .atomic_begin = mtk_drm_crtc_atomic_begin,
455 .atomic_flush = mtk_drm_crtc_atomic_flush,
458 static int mtk_drm_crtc_init(struct drm_device *drm,
459 struct mtk_drm_crtc *mtk_crtc,
460 struct drm_plane *primary,
461 struct drm_plane *cursor, unsigned int pipe)
463 int ret;
465 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
466 &mtk_crtc_funcs, NULL);
467 if (ret)
468 goto err_cleanup_crtc;
470 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
472 return 0;
474 err_cleanup_crtc:
475 drm_crtc_cleanup(&mtk_crtc->base);
476 return ret;
479 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *ovl)
481 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
482 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
483 unsigned int i;
486 * TODO: instead of updating the registers here, we should prepare
487 * working registers in atomic_commit and let the hardware command
488 * queue update module registers on vblank.
490 if (state->pending_config) {
491 mtk_ddp_comp_config(ovl, state->pending_width,
492 state->pending_height,
493 state->pending_vrefresh, 0);
495 state->pending_config = false;
498 if (mtk_crtc->pending_planes) {
499 for (i = 0; i < OVL_LAYER_NR; i++) {
500 struct drm_plane *plane = &mtk_crtc->planes[i];
501 struct mtk_plane_state *plane_state;
503 plane_state = to_mtk_plane_state(plane->state);
505 if (plane_state->pending.config) {
506 mtk_ddp_comp_layer_config(ovl, i, plane_state);
507 plane_state->pending.config = false;
510 mtk_crtc->pending_planes = false;
513 mtk_drm_finish_page_flip(mtk_crtc);
516 int mtk_drm_crtc_create(struct drm_device *drm_dev,
517 const enum mtk_ddp_comp_id *path, unsigned int path_len)
519 struct mtk_drm_private *priv = drm_dev->dev_private;
520 struct device *dev = drm_dev->dev;
521 struct mtk_drm_crtc *mtk_crtc;
522 enum drm_plane_type type;
523 unsigned int zpos;
524 int pipe = priv->num_pipes;
525 int ret;
526 int i;
528 for (i = 0; i < path_len; i++) {
529 enum mtk_ddp_comp_id comp_id = path[i];
530 struct device_node *node;
532 node = priv->comp_node[comp_id];
533 if (!node) {
534 dev_info(dev,
535 "Not creating crtc %d because component %d is disabled or missing\n",
536 pipe, comp_id);
537 return 0;
541 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
542 if (!mtk_crtc)
543 return -ENOMEM;
545 mtk_crtc->config_regs = priv->config_regs;
546 mtk_crtc->ddp_comp_nr = path_len;
547 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
548 sizeof(*mtk_crtc->ddp_comp),
549 GFP_KERNEL);
551 mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
552 if (IS_ERR(mtk_crtc->mutex)) {
553 ret = PTR_ERR(mtk_crtc->mutex);
554 dev_err(dev, "Failed to get mutex: %d\n", ret);
555 return ret;
558 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
559 enum mtk_ddp_comp_id comp_id = path[i];
560 struct mtk_ddp_comp *comp;
561 struct device_node *node;
563 node = priv->comp_node[comp_id];
564 comp = priv->ddp_comp[comp_id];
565 if (!comp) {
566 dev_err(dev, "Component %s not initialized\n",
567 node->full_name);
568 ret = -ENODEV;
569 goto unprepare;
572 ret = clk_prepare(comp->clk);
573 if (ret) {
574 dev_err(dev,
575 "Failed to prepare clock for component %s: %d\n",
576 node->full_name, ret);
577 goto unprepare;
580 mtk_crtc->ddp_comp[i] = comp;
583 for (zpos = 0; zpos < OVL_LAYER_NR; zpos++) {
584 type = (zpos == 0) ? DRM_PLANE_TYPE_PRIMARY :
585 (zpos == 1) ? DRM_PLANE_TYPE_CURSOR :
586 DRM_PLANE_TYPE_OVERLAY;
587 ret = mtk_plane_init(drm_dev, &mtk_crtc->planes[zpos],
588 BIT(pipe), type);
589 if (ret)
590 goto unprepare;
593 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, &mtk_crtc->planes[0],
594 &mtk_crtc->planes[1], pipe);
595 if (ret < 0)
596 goto unprepare;
597 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, MTK_LUT_SIZE);
598 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, false, MTK_LUT_SIZE);
599 priv->crtc[pipe] = &mtk_crtc->base;
600 priv->num_pipes++;
602 return 0;
604 unprepare:
605 while (--i >= 0)
606 clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
608 return ret;