1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
6 #include <asm/barrier.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_plane_helper.h>
10 #include <drm/drm_probe_helper.h>
11 #include <linux/clk.h>
12 #include <linux/pm_runtime.h>
13 #include <soc/mediatek/smi.h>
15 #include "mtk_drm_drv.h"
16 #include "mtk_drm_crtc.h"
17 #include "mtk_drm_ddp.h"
18 #include "mtk_drm_ddp_comp.h"
19 #include "mtk_drm_gem.h"
20 #include "mtk_drm_plane.h"
23 * struct mtk_drm_crtc - MediaTek specific crtc structure.
25 * @enabled: records whether crtc_enable succeeded
26 * @planes: array of 4 drm_plane structures, one for each overlay plane
27 * @pending_planes: whether any plane has pending changes to be applied
28 * @config_regs: memory mapped mmsys configuration register space
29 * @mutex: handle to one of the ten disp_mutex streams
30 * @ddp_comp_nr: number of components in ddp_comp
31 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
37 bool pending_needs_vblank;
38 struct drm_pending_vblank_event *event;
40 struct drm_plane *planes;
41 unsigned int layer_nr;
44 void __iomem *config_regs;
45 struct mtk_disp_mutex *mutex;
46 unsigned int ddp_comp_nr;
47 struct mtk_ddp_comp **ddp_comp;
50 struct mtk_crtc_state {
51 struct drm_crtc_state base;
54 unsigned int pending_width;
55 unsigned int pending_height;
56 unsigned int pending_vrefresh;
59 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
61 return container_of(c, struct mtk_drm_crtc, base);
64 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
66 return container_of(s, struct mtk_crtc_state, base);
69 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
71 struct drm_crtc *crtc = &mtk_crtc->base;
74 spin_lock_irqsave(&crtc->dev->event_lock, flags);
75 drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
76 drm_crtc_vblank_put(crtc);
77 mtk_crtc->event = NULL;
78 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
81 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
83 drm_crtc_handle_vblank(&mtk_crtc->base);
84 if (mtk_crtc->pending_needs_vblank) {
85 mtk_drm_crtc_finish_page_flip(mtk_crtc);
86 mtk_crtc->pending_needs_vblank = false;
90 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
92 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
95 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
96 clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
98 mtk_disp_mutex_put(mtk_crtc->mutex);
100 drm_crtc_cleanup(crtc);
103 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
105 struct mtk_crtc_state *state;
108 __drm_atomic_helper_crtc_destroy_state(crtc->state);
110 state = to_mtk_crtc_state(crtc->state);
111 memset(state, 0, sizeof(*state));
113 state = kzalloc(sizeof(*state), GFP_KERNEL);
116 crtc->state = &state->base;
119 state->base.crtc = crtc;
122 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
124 struct mtk_crtc_state *state;
126 state = kzalloc(sizeof(*state), GFP_KERNEL);
130 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
132 WARN_ON(state->base.crtc != crtc);
133 state->base.crtc = crtc;
138 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
139 struct drm_crtc_state *state)
141 __drm_atomic_helper_crtc_destroy_state(state);
142 kfree(to_mtk_crtc_state(state));
145 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
146 const struct drm_display_mode *mode,
147 struct drm_display_mode *adjusted_mode)
149 /* Nothing to do here, but this callback is mandatory. */
153 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
155 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
157 state->pending_width = crtc->mode.hdisplay;
158 state->pending_height = crtc->mode.vdisplay;
159 state->pending_vrefresh = crtc->mode.vrefresh;
160 wmb(); /* Make sure the above parameters are set before update */
161 state->pending_config = true;
164 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
166 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
167 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
169 mtk_ddp_comp_enable_vblank(comp, &mtk_crtc->base);
174 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
176 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
177 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
179 mtk_ddp_comp_disable_vblank(comp);
182 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
187 DRM_DEBUG_DRIVER("%s\n", __func__);
188 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
189 ret = clk_enable(mtk_crtc->ddp_comp[i]->clk);
191 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
199 clk_disable(mtk_crtc->ddp_comp[i]->clk);
203 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
207 DRM_DEBUG_DRIVER("%s\n", __func__);
208 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
209 clk_disable(mtk_crtc->ddp_comp[i]->clk);
212 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
214 struct drm_crtc *crtc = &mtk_crtc->base;
215 struct drm_connector *connector;
216 struct drm_encoder *encoder;
217 struct drm_connector_list_iter conn_iter;
218 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
222 DRM_DEBUG_DRIVER("%s\n", __func__);
223 if (WARN_ON(!crtc->state))
226 width = crtc->state->adjusted_mode.hdisplay;
227 height = crtc->state->adjusted_mode.vdisplay;
228 vrefresh = crtc->state->adjusted_mode.vrefresh;
230 drm_for_each_encoder(encoder, crtc->dev) {
231 if (encoder->crtc != crtc)
234 drm_connector_list_iter_begin(crtc->dev, &conn_iter);
235 drm_for_each_connector_iter(connector, &conn_iter) {
236 if (connector->encoder != encoder)
238 if (connector->display_info.bpc != 0 &&
239 bpc > connector->display_info.bpc)
240 bpc = connector->display_info.bpc;
242 drm_connector_list_iter_end(&conn_iter);
245 ret = pm_runtime_get_sync(crtc->dev->dev);
247 DRM_ERROR("Failed to enable power domain: %d\n", ret);
251 ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
253 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
254 goto err_pm_runtime_put;
257 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
259 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
260 goto err_mutex_unprepare;
263 DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
264 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
265 mtk_ddp_add_comp_to_path(mtk_crtc->config_regs,
266 mtk_crtc->ddp_comp[i]->id,
267 mtk_crtc->ddp_comp[i + 1]->id);
268 mtk_disp_mutex_add_comp(mtk_crtc->mutex,
269 mtk_crtc->ddp_comp[i]->id);
271 mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
272 mtk_disp_mutex_enable(mtk_crtc->mutex);
274 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
275 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
277 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc);
278 mtk_ddp_comp_start(comp);
281 /* Initially configure all planes */
282 for (i = 0; i < mtk_crtc->layer_nr; i++) {
283 struct drm_plane *plane = &mtk_crtc->planes[i];
284 struct mtk_plane_state *plane_state;
286 plane_state = to_mtk_plane_state(plane->state);
287 mtk_ddp_comp_layer_config(mtk_crtc->ddp_comp[0], i,
294 mtk_disp_mutex_unprepare(mtk_crtc->mutex);
296 pm_runtime_put(crtc->dev->dev);
300 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
302 struct drm_device *drm = mtk_crtc->base.dev;
305 DRM_DEBUG_DRIVER("%s\n", __func__);
306 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
307 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
308 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
309 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
310 mtk_crtc->ddp_comp[i]->id);
311 mtk_disp_mutex_disable(mtk_crtc->mutex);
312 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
313 mtk_ddp_remove_comp_from_path(mtk_crtc->config_regs,
314 mtk_crtc->ddp_comp[i]->id,
315 mtk_crtc->ddp_comp[i + 1]->id);
316 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
317 mtk_crtc->ddp_comp[i]->id);
319 mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
320 mtk_crtc_ddp_clk_disable(mtk_crtc);
321 mtk_disp_mutex_unprepare(mtk_crtc->mutex);
323 pm_runtime_put(drm->dev);
326 static void mtk_crtc_ddp_config(struct drm_crtc *crtc)
328 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
329 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
330 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
334 * TODO: instead of updating the registers here, we should prepare
335 * working registers in atomic_commit and let the hardware command
336 * queue update module registers on vblank.
338 if (state->pending_config) {
339 mtk_ddp_comp_config(comp, state->pending_width,
340 state->pending_height,
341 state->pending_vrefresh, 0);
343 state->pending_config = false;
346 if (mtk_crtc->pending_planes) {
347 for (i = 0; i < mtk_crtc->layer_nr; i++) {
348 struct drm_plane *plane = &mtk_crtc->planes[i];
349 struct mtk_plane_state *plane_state;
351 plane_state = to_mtk_plane_state(plane->state);
353 if (plane_state->pending.config) {
354 mtk_ddp_comp_layer_config(comp, i, plane_state);
355 plane_state->pending.config = false;
358 mtk_crtc->pending_planes = false;
362 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
363 struct drm_crtc_state *old_state)
365 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
366 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
369 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
371 ret = mtk_smi_larb_get(comp->larb_dev);
373 DRM_ERROR("Failed to get larb: %d\n", ret);
377 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
379 mtk_smi_larb_put(comp->larb_dev);
383 drm_crtc_vblank_on(crtc);
384 mtk_crtc->enabled = true;
387 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
388 struct drm_crtc_state *old_state)
390 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
391 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
394 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
395 if (!mtk_crtc->enabled)
398 /* Set all pending plane state to disabled */
399 for (i = 0; i < mtk_crtc->layer_nr; i++) {
400 struct drm_plane *plane = &mtk_crtc->planes[i];
401 struct mtk_plane_state *plane_state;
403 plane_state = to_mtk_plane_state(plane->state);
404 plane_state->pending.enable = false;
405 plane_state->pending.config = true;
407 mtk_crtc->pending_planes = true;
409 /* Wait for planes to be disabled */
410 drm_crtc_wait_one_vblank(crtc);
412 drm_crtc_vblank_off(crtc);
413 mtk_crtc_ddp_hw_fini(mtk_crtc);
414 mtk_smi_larb_put(comp->larb_dev);
416 mtk_crtc->enabled = false;
419 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
420 struct drm_crtc_state *old_crtc_state)
422 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
423 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
425 if (mtk_crtc->event && state->base.event)
426 DRM_ERROR("new event while there is still a pending event\n");
428 if (state->base.event) {
429 state->base.event->pipe = drm_crtc_index(crtc);
430 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
431 mtk_crtc->event = state->base.event;
432 state->base.event = NULL;
436 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
437 struct drm_crtc_state *old_crtc_state)
439 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
440 struct mtk_drm_private *priv = crtc->dev->dev_private;
441 unsigned int pending_planes = 0;
445 mtk_crtc->pending_needs_vblank = true;
446 for (i = 0; i < mtk_crtc->layer_nr; i++) {
447 struct drm_plane *plane = &mtk_crtc->planes[i];
448 struct mtk_plane_state *plane_state;
450 plane_state = to_mtk_plane_state(plane->state);
451 if (plane_state->pending.dirty) {
452 plane_state->pending.config = true;
453 plane_state->pending.dirty = false;
454 pending_planes |= BIT(i);
458 mtk_crtc->pending_planes = true;
459 if (crtc->state->color_mgmt_changed)
460 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
461 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
463 if (priv->data->shadow_register) {
464 mtk_disp_mutex_acquire(mtk_crtc->mutex);
465 mtk_crtc_ddp_config(crtc);
466 mtk_disp_mutex_release(mtk_crtc->mutex);
470 static const struct drm_crtc_funcs mtk_crtc_funcs = {
471 .set_config = drm_atomic_helper_set_config,
472 .page_flip = drm_atomic_helper_page_flip,
473 .destroy = mtk_drm_crtc_destroy,
474 .reset = mtk_drm_crtc_reset,
475 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
476 .atomic_destroy_state = mtk_drm_crtc_destroy_state,
477 .gamma_set = drm_atomic_helper_legacy_gamma_set,
478 .enable_vblank = mtk_drm_crtc_enable_vblank,
479 .disable_vblank = mtk_drm_crtc_disable_vblank,
482 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
483 .mode_fixup = mtk_drm_crtc_mode_fixup,
484 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
485 .atomic_begin = mtk_drm_crtc_atomic_begin,
486 .atomic_flush = mtk_drm_crtc_atomic_flush,
487 .atomic_enable = mtk_drm_crtc_atomic_enable,
488 .atomic_disable = mtk_drm_crtc_atomic_disable,
491 static int mtk_drm_crtc_init(struct drm_device *drm,
492 struct mtk_drm_crtc *mtk_crtc,
493 struct drm_plane *primary,
494 struct drm_plane *cursor, unsigned int pipe)
498 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
499 &mtk_crtc_funcs, NULL);
501 goto err_cleanup_crtc;
503 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
508 drm_crtc_cleanup(&mtk_crtc->base);
512 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *comp)
514 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
515 struct mtk_drm_private *priv = crtc->dev->dev_private;
517 if (!priv->data->shadow_register)
518 mtk_crtc_ddp_config(crtc);
520 mtk_drm_finish_page_flip(mtk_crtc);
523 int mtk_drm_crtc_create(struct drm_device *drm_dev,
524 const enum mtk_ddp_comp_id *path, unsigned int path_len)
526 struct mtk_drm_private *priv = drm_dev->dev_private;
527 struct device *dev = drm_dev->dev;
528 struct mtk_drm_crtc *mtk_crtc;
529 enum drm_plane_type type;
531 int pipe = priv->num_pipes;
538 for (i = 0; i < path_len; i++) {
539 enum mtk_ddp_comp_id comp_id = path[i];
540 struct device_node *node;
542 node = priv->comp_node[comp_id];
545 "Not creating crtc %d because component %d is disabled or missing\n",
551 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
555 mtk_crtc->config_regs = priv->config_regs;
556 mtk_crtc->ddp_comp_nr = path_len;
557 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
558 sizeof(*mtk_crtc->ddp_comp),
560 if (!mtk_crtc->ddp_comp)
563 mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
564 if (IS_ERR(mtk_crtc->mutex)) {
565 ret = PTR_ERR(mtk_crtc->mutex);
566 dev_err(dev, "Failed to get mutex: %d\n", ret);
570 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
571 enum mtk_ddp_comp_id comp_id = path[i];
572 struct mtk_ddp_comp *comp;
573 struct device_node *node;
575 node = priv->comp_node[comp_id];
576 comp = priv->ddp_comp[comp_id];
578 dev_err(dev, "Component %pOF not initialized\n", node);
583 ret = clk_prepare(comp->clk);
586 "Failed to prepare clock for component %pOF: %d\n",
591 mtk_crtc->ddp_comp[i] = comp;
594 mtk_crtc->layer_nr = mtk_ddp_comp_layer_nr(mtk_crtc->ddp_comp[0]);
595 mtk_crtc->planes = devm_kcalloc(dev, mtk_crtc->layer_nr,
596 sizeof(struct drm_plane),
599 for (zpos = 0; zpos < mtk_crtc->layer_nr; zpos++) {
600 type = (zpos == 0) ? DRM_PLANE_TYPE_PRIMARY :
601 (zpos == 1) ? DRM_PLANE_TYPE_CURSOR :
602 DRM_PLANE_TYPE_OVERLAY;
603 ret = mtk_plane_init(drm_dev, &mtk_crtc->planes[zpos],
609 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, &mtk_crtc->planes[0],
610 mtk_crtc->layer_nr > 1 ? &mtk_crtc->planes[1] :
614 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, MTK_LUT_SIZE);
615 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, false, MTK_LUT_SIZE);
622 clk_unprepare(mtk_crtc->ddp_comp[i]->clk);