1e559355aSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ad49f860SLiviu Dudau /*
3ad49f860SLiviu Dudau * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
4ad49f860SLiviu Dudau * Author: Liviu Dudau <Liviu.Dudau@arm.com>
5ad49f860SLiviu Dudau *
6ad49f860SLiviu Dudau * ARM Mali DP500/DP550/DP650 driver (crtc operations)
7ad49f860SLiviu Dudau */
8ad49f860SLiviu Dudau
9535d1b94SSam Ravnborg #include <linux/clk.h>
10535d1b94SSam Ravnborg #include <linux/pm_runtime.h>
11535d1b94SSam Ravnborg
12535d1b94SSam Ravnborg #include <video/videomode.h>
13535d1b94SSam Ravnborg
14ad49f860SLiviu Dudau #include <drm/drm_atomic.h>
15ad49f860SLiviu Dudau #include <drm/drm_atomic_helper.h>
16ad49f860SLiviu Dudau #include <drm/drm_crtc.h>
17720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
18535d1b94SSam Ravnborg #include <drm/drm_print.h>
19fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
20535d1b94SSam Ravnborg #include <drm/drm_vblank.h>
21ad49f860SLiviu Dudau
22ad49f860SLiviu Dudau #include "malidp_drv.h"
23ad49f860SLiviu Dudau #include "malidp_hw.h"
24ad49f860SLiviu Dudau
malidp_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)25e2113c03SJose Abreu static enum drm_mode_status malidp_crtc_mode_valid(struct drm_crtc *crtc,
26e2113c03SJose Abreu const struct drm_display_mode *mode)
27ad49f860SLiviu Dudau {
28ad49f860SLiviu Dudau struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
29ad49f860SLiviu Dudau struct malidp_hw_device *hwdev = malidp->dev;
30ad49f860SLiviu Dudau
31ad49f860SLiviu Dudau /*
32ad49f860SLiviu Dudau * check that the hardware can drive the required clock rate,
33ad49f860SLiviu Dudau * but skip the check if the clock is meant to be disabled (req_rate = 0)
34ad49f860SLiviu Dudau */
35ad49f860SLiviu Dudau long rate, req_rate = mode->crtc_clock * 1000;
36ad49f860SLiviu Dudau
37ad49f860SLiviu Dudau if (req_rate) {
38ad49f860SLiviu Dudau rate = clk_round_rate(hwdev->pxlclk, req_rate);
39ad49f860SLiviu Dudau if (rate != req_rate) {
40ad49f860SLiviu Dudau DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
41ad49f860SLiviu Dudau req_rate);
42e2113c03SJose Abreu return MODE_NOCLOCK;
43ad49f860SLiviu Dudau }
44ad49f860SLiviu Dudau }
45ad49f860SLiviu Dudau
46e2113c03SJose Abreu return MODE_OK;
47ad49f860SLiviu Dudau }
48ad49f860SLiviu Dudau
malidp_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)490b20a0f8SLaurent Pinchart static void malidp_crtc_atomic_enable(struct drm_crtc *crtc,
50351f950dSMaxime Ripard struct drm_atomic_state *state)
51ad49f860SLiviu Dudau {
52ad49f860SLiviu Dudau struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
53ad49f860SLiviu Dudau struct malidp_hw_device *hwdev = malidp->dev;
54ad49f860SLiviu Dudau struct videomode vm;
5585f64218SLiviu Dudau int err = pm_runtime_get_sync(crtc->dev->dev);
5685f64218SLiviu Dudau
5785f64218SLiviu Dudau if (err < 0) {
5885f64218SLiviu Dudau DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
5985f64218SLiviu Dudau return;
6085f64218SLiviu Dudau }
61ad49f860SLiviu Dudau
62ad49f860SLiviu Dudau drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
63ad49f860SLiviu Dudau clk_prepare_enable(hwdev->pxlclk);
64ad49f860SLiviu Dudau
659a8b0a23SMihail Atanassov /* We rely on firmware to set mclk to a sensible level. */
66ad49f860SLiviu Dudau clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
67ad49f860SLiviu Dudau
68a6993b21SLiviu Dudau hwdev->hw->modeset(hwdev, &vm);
69a6993b21SLiviu Dudau hwdev->hw->leave_config_mode(hwdev);
70ad49f860SLiviu Dudau drm_crtc_vblank_on(crtc);
71ad49f860SLiviu Dudau }
72ad49f860SLiviu Dudau
malidp_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)7364581714SLaurent Pinchart static void malidp_crtc_atomic_disable(struct drm_crtc *crtc,
74351f950dSMaxime Ripard struct drm_atomic_state *state)
75ad49f860SLiviu Dudau {
76351f950dSMaxime Ripard struct drm_crtc_state *old_state = drm_atomic_get_old_crtc_state(state,
77351f950dSMaxime Ripard crtc);
78ad49f860SLiviu Dudau struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
79ad49f860SLiviu Dudau struct malidp_hw_device *hwdev = malidp->dev;
8085f64218SLiviu Dudau int err;
81ad49f860SLiviu Dudau
8254243016SLiviu Dudau /* always disable planes on the CRTC that is being turned off */
8354243016SLiviu Dudau drm_atomic_helper_disable_planes_on_crtc(old_state, false);
8454243016SLiviu Dudau
85ad49f860SLiviu Dudau drm_crtc_vblank_off(crtc);
86a6993b21SLiviu Dudau hwdev->hw->enter_config_mode(hwdev);
87a6993b21SLiviu Dudau
88ad49f860SLiviu Dudau clk_disable_unprepare(hwdev->pxlclk);
8985f64218SLiviu Dudau
9085f64218SLiviu Dudau err = pm_runtime_put(crtc->dev->dev);
9185f64218SLiviu Dudau if (err < 0) {
9285f64218SLiviu Dudau DRM_DEBUG_DRIVER("Failed to disable runtime power management: %d\n", err);
9385f64218SLiviu Dudau }
94ad49f860SLiviu Dudau }
95ad49f860SLiviu Dudau
9602725d31SMihail Atanassov static const struct gamma_curve_segment {
9702725d31SMihail Atanassov u16 start;
9802725d31SMihail Atanassov u16 end;
9902725d31SMihail Atanassov } segments[MALIDP_COEFFTAB_NUM_COEFFS] = {
10002725d31SMihail Atanassov /* sector 0 */
10102725d31SMihail Atanassov { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 },
10202725d31SMihail Atanassov { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
10302725d31SMihail Atanassov { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 },
10402725d31SMihail Atanassov { 12, 12 }, { 13, 13 }, { 14, 14 }, { 15, 15 },
10502725d31SMihail Atanassov /* sector 1 */
10602725d31SMihail Atanassov { 16, 19 }, { 20, 23 }, { 24, 27 }, { 28, 31 },
10702725d31SMihail Atanassov /* sector 2 */
10802725d31SMihail Atanassov { 32, 39 }, { 40, 47 }, { 48, 55 }, { 56, 63 },
10902725d31SMihail Atanassov /* sector 3 */
11002725d31SMihail Atanassov { 64, 79 }, { 80, 95 }, { 96, 111 }, { 112, 127 },
11102725d31SMihail Atanassov /* sector 4 */
11202725d31SMihail Atanassov { 128, 159 }, { 160, 191 }, { 192, 223 }, { 224, 255 },
11302725d31SMihail Atanassov /* sector 5 */
11402725d31SMihail Atanassov { 256, 319 }, { 320, 383 }, { 384, 447 }, { 448, 511 },
11502725d31SMihail Atanassov /* sector 6 */
11602725d31SMihail Atanassov { 512, 639 }, { 640, 767 }, { 768, 895 }, { 896, 1023 },
11702725d31SMihail Atanassov { 1024, 1151 }, { 1152, 1279 }, { 1280, 1407 }, { 1408, 1535 },
11802725d31SMihail Atanassov { 1536, 1663 }, { 1664, 1791 }, { 1792, 1919 }, { 1920, 2047 },
11902725d31SMihail Atanassov { 2048, 2175 }, { 2176, 2303 }, { 2304, 2431 }, { 2432, 2559 },
12002725d31SMihail Atanassov { 2560, 2687 }, { 2688, 2815 }, { 2816, 2943 }, { 2944, 3071 },
12102725d31SMihail Atanassov { 3072, 3199 }, { 3200, 3327 }, { 3328, 3455 }, { 3456, 3583 },
12202725d31SMihail Atanassov { 3584, 3711 }, { 3712, 3839 }, { 3840, 3967 }, { 3968, 4095 },
12302725d31SMihail Atanassov };
12402725d31SMihail Atanassov
12502725d31SMihail Atanassov #define DE_COEFTAB_DATA(a, b) ((((a) & 0xfff) << 16) | (((b) & 0xfff)))
12602725d31SMihail Atanassov
malidp_generate_gamma_table(struct drm_property_blob * lut_blob,u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])12702725d31SMihail Atanassov static void malidp_generate_gamma_table(struct drm_property_blob *lut_blob,
12802725d31SMihail Atanassov u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])
12902725d31SMihail Atanassov {
13002725d31SMihail Atanassov struct drm_color_lut *lut = (struct drm_color_lut *)lut_blob->data;
13102725d31SMihail Atanassov int i;
13202725d31SMihail Atanassov
13302725d31SMihail Atanassov for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i) {
13402725d31SMihail Atanassov u32 a, b, delta_in, out_start, out_end;
13502725d31SMihail Atanassov
13602725d31SMihail Atanassov delta_in = segments[i].end - segments[i].start;
13702725d31SMihail Atanassov /* DP has 12-bit internal precision for its LUTs. */
13802725d31SMihail Atanassov out_start = drm_color_lut_extract(lut[segments[i].start].green,
13902725d31SMihail Atanassov 12);
14002725d31SMihail Atanassov out_end = drm_color_lut_extract(lut[segments[i].end].green, 12);
14102725d31SMihail Atanassov a = (delta_in == 0) ? 0 : ((out_end - out_start) * 256) / delta_in;
14202725d31SMihail Atanassov b = out_start;
14302725d31SMihail Atanassov coeffs[i] = DE_COEFTAB_DATA(a, b);
14402725d31SMihail Atanassov }
14502725d31SMihail Atanassov }
14602725d31SMihail Atanassov
14702725d31SMihail Atanassov /*
14802725d31SMihail Atanassov * Check if there is a new gamma LUT and if it is of an acceptable size. Also,
14902725d31SMihail Atanassov * reject any LUTs that use distinct red, green, and blue curves.
15002725d31SMihail Atanassov */
malidp_crtc_atomic_check_gamma(struct drm_crtc * crtc,struct drm_crtc_state * state)15102725d31SMihail Atanassov static int malidp_crtc_atomic_check_gamma(struct drm_crtc *crtc,
15202725d31SMihail Atanassov struct drm_crtc_state *state)
15302725d31SMihail Atanassov {
15402725d31SMihail Atanassov struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
15502725d31SMihail Atanassov struct drm_color_lut *lut;
15602725d31SMihail Atanassov size_t lut_size;
15702725d31SMihail Atanassov int i;
15802725d31SMihail Atanassov
15902725d31SMihail Atanassov if (!state->color_mgmt_changed || !state->gamma_lut)
16002725d31SMihail Atanassov return 0;
16102725d31SMihail Atanassov
16202725d31SMihail Atanassov if (crtc->state->gamma_lut &&
16302725d31SMihail Atanassov (crtc->state->gamma_lut->base.id == state->gamma_lut->base.id))
16402725d31SMihail Atanassov return 0;
16502725d31SMihail Atanassov
16602725d31SMihail Atanassov if (state->gamma_lut->length % sizeof(struct drm_color_lut))
16702725d31SMihail Atanassov return -EINVAL;
16802725d31SMihail Atanassov
16902725d31SMihail Atanassov lut_size = state->gamma_lut->length / sizeof(struct drm_color_lut);
17002725d31SMihail Atanassov if (lut_size != MALIDP_GAMMA_LUT_SIZE)
17102725d31SMihail Atanassov return -EINVAL;
17202725d31SMihail Atanassov
17302725d31SMihail Atanassov lut = (struct drm_color_lut *)state->gamma_lut->data;
17402725d31SMihail Atanassov for (i = 0; i < lut_size; ++i)
17502725d31SMihail Atanassov if (!((lut[i].red == lut[i].green) &&
17602725d31SMihail Atanassov (lut[i].red == lut[i].blue)))
17702725d31SMihail Atanassov return -EINVAL;
17802725d31SMihail Atanassov
17902725d31SMihail Atanassov if (!state->mode_changed) {
18002725d31SMihail Atanassov int ret;
18102725d31SMihail Atanassov
18202725d31SMihail Atanassov state->mode_changed = true;
18302725d31SMihail Atanassov /*
18402725d31SMihail Atanassov * Kerneldoc for drm_atomic_helper_check_modeset mandates that
18502725d31SMihail Atanassov * it be invoked when the driver sets ->mode_changed. Since
18602725d31SMihail Atanassov * changing the gamma LUT doesn't depend on any external
18702725d31SMihail Atanassov * resources, it is safe to call it only once.
18802725d31SMihail Atanassov */
18902725d31SMihail Atanassov ret = drm_atomic_helper_check_modeset(crtc->dev, state->state);
19002725d31SMihail Atanassov if (ret)
19102725d31SMihail Atanassov return ret;
19202725d31SMihail Atanassov }
19302725d31SMihail Atanassov
19402725d31SMihail Atanassov malidp_generate_gamma_table(state->gamma_lut, mc->gamma_coeffs);
19502725d31SMihail Atanassov return 0;
19602725d31SMihail Atanassov }
19702725d31SMihail Atanassov
1986954f245SMihail Atanassov /*
1996954f245SMihail Atanassov * Check if there is a new CTM and if it contains valid input. Valid here means
2006954f245SMihail Atanassov * that the number is inside the representable range for a Q3.12 number,
2016954f245SMihail Atanassov * excluding truncating the fractional part of the input data.
2026954f245SMihail Atanassov *
2036954f245SMihail Atanassov * The COLORADJ registers can be changed atomically.
2046954f245SMihail Atanassov */
malidp_crtc_atomic_check_ctm(struct drm_crtc * crtc,struct drm_crtc_state * state)2056954f245SMihail Atanassov static int malidp_crtc_atomic_check_ctm(struct drm_crtc *crtc,
2066954f245SMihail Atanassov struct drm_crtc_state *state)
2076954f245SMihail Atanassov {
2086954f245SMihail Atanassov struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
2096954f245SMihail Atanassov struct drm_color_ctm *ctm;
2106954f245SMihail Atanassov int i;
2116954f245SMihail Atanassov
2126954f245SMihail Atanassov if (!state->color_mgmt_changed)
2136954f245SMihail Atanassov return 0;
2146954f245SMihail Atanassov
2156954f245SMihail Atanassov if (!state->ctm)
2166954f245SMihail Atanassov return 0;
2176954f245SMihail Atanassov
2186954f245SMihail Atanassov if (crtc->state->ctm && (crtc->state->ctm->base.id ==
2196954f245SMihail Atanassov state->ctm->base.id))
2206954f245SMihail Atanassov return 0;
2216954f245SMihail Atanassov
2226954f245SMihail Atanassov /*
2236954f245SMihail Atanassov * The size of the ctm is checked in
2246954f245SMihail Atanassov * drm_atomic_replace_property_blob_from_id.
2256954f245SMihail Atanassov */
2266954f245SMihail Atanassov ctm = (struct drm_color_ctm *)state->ctm->data;
2276954f245SMihail Atanassov for (i = 0; i < ARRAY_SIZE(ctm->matrix); ++i) {
2286954f245SMihail Atanassov /* Convert from S31.32 to Q3.12. */
2296954f245SMihail Atanassov s64 val = ctm->matrix[i];
2306954f245SMihail Atanassov u32 mag = ((((u64)val) & ~BIT_ULL(63)) >> 20) &
2316954f245SMihail Atanassov GENMASK_ULL(14, 0);
2326954f245SMihail Atanassov
2336954f245SMihail Atanassov /*
2346954f245SMihail Atanassov * Convert to 2s complement and check the destination's top bit
2356954f245SMihail Atanassov * for overflow. NB: Can't check before converting or it'd
2366954f245SMihail Atanassov * incorrectly reject the case:
2376954f245SMihail Atanassov * sign == 1
2386954f245SMihail Atanassov * mag == 0x2000
2396954f245SMihail Atanassov */
2406954f245SMihail Atanassov if (val & BIT_ULL(63))
2416954f245SMihail Atanassov mag = ~mag + 1;
2426954f245SMihail Atanassov if (!!(val & BIT_ULL(63)) != !!(mag & BIT(14)))
2436954f245SMihail Atanassov return -EINVAL;
2446954f245SMihail Atanassov mc->coloradj_coeffs[i] = mag;
2456954f245SMihail Atanassov }
2466954f245SMihail Atanassov
2476954f245SMihail Atanassov return 0;
2486954f245SMihail Atanassov }
2496954f245SMihail Atanassov
malidp_crtc_atomic_check_scaling(struct drm_crtc * crtc,struct drm_crtc_state * state)25028ce675bSMihail Atanassov static int malidp_crtc_atomic_check_scaling(struct drm_crtc *crtc,
25128ce675bSMihail Atanassov struct drm_crtc_state *state)
25228ce675bSMihail Atanassov {
253c2e7f82dSMihail Atanassov struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
254c2e7f82dSMihail Atanassov struct malidp_hw_device *hwdev = malidp->dev;
25528ce675bSMihail Atanassov struct malidp_crtc_state *cs = to_malidp_crtc_state(state);
25628ce675bSMihail Atanassov struct malidp_se_config *s = &cs->scaler_config;
25728ce675bSMihail Atanassov struct drm_plane *plane;
258c2e7f82dSMihail Atanassov struct videomode vm;
25928ce675bSMihail Atanassov const struct drm_plane_state *pstate;
26028ce675bSMihail Atanassov u32 h_upscale_factor = 0; /* U16.16 */
26128ce675bSMihail Atanassov u32 v_upscale_factor = 0; /* U16.16 */
26228ce675bSMihail Atanassov u8 scaling = cs->scaled_planes_mask;
263c2e7f82dSMihail Atanassov int ret;
26428ce675bSMihail Atanassov
26528ce675bSMihail Atanassov if (!scaling) {
26628ce675bSMihail Atanassov s->scale_enable = false;
267c2e7f82dSMihail Atanassov goto mclk_calc;
26828ce675bSMihail Atanassov }
26928ce675bSMihail Atanassov
27028ce675bSMihail Atanassov /* The scaling engine can only handle one plane at a time. */
27128ce675bSMihail Atanassov if (scaling & (scaling - 1))
27228ce675bSMihail Atanassov return -EINVAL;
27328ce675bSMihail Atanassov
27428ce675bSMihail Atanassov drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
27528ce675bSMihail Atanassov struct malidp_plane *mp = to_malidp_plane(plane);
27628ce675bSMihail Atanassov u32 phase;
27728ce675bSMihail Atanassov
27828ce675bSMihail Atanassov if (!(mp->layer->id & scaling))
27928ce675bSMihail Atanassov continue;
28028ce675bSMihail Atanassov
28128ce675bSMihail Atanassov /*
28228ce675bSMihail Atanassov * Convert crtc_[w|h] to U32.32, then divide by U16.16 src_[w|h]
28328ce675bSMihail Atanassov * to get the U16.16 result.
28428ce675bSMihail Atanassov */
285763656d3SArnd Bergmann h_upscale_factor = div_u64((u64)pstate->crtc_w << 32,
286763656d3SArnd Bergmann pstate->src_w);
287763656d3SArnd Bergmann v_upscale_factor = div_u64((u64)pstate->crtc_h << 32,
288763656d3SArnd Bergmann pstate->src_h);
28928ce675bSMihail Atanassov
2900274e6a0SMihail Atanassov s->enhancer_enable = ((h_upscale_factor >> 16) >= 2 ||
2910274e6a0SMihail Atanassov (v_upscale_factor >> 16) >= 2);
2920274e6a0SMihail Atanassov
2936cc3a505SAyan Halder if (pstate->rotation & MALIDP_ROTATED_MASK) {
2946cc3a505SAyan Halder s->input_w = pstate->src_h >> 16;
2956cc3a505SAyan Halder s->input_h = pstate->src_w >> 16;
2966cc3a505SAyan Halder } else {
29728ce675bSMihail Atanassov s->input_w = pstate->src_w >> 16;
29828ce675bSMihail Atanassov s->input_h = pstate->src_h >> 16;
2996cc3a505SAyan Halder }
3006cc3a505SAyan Halder
30128ce675bSMihail Atanassov s->output_w = pstate->crtc_w;
30228ce675bSMihail Atanassov s->output_h = pstate->crtc_h;
30328ce675bSMihail Atanassov
30428ce675bSMihail Atanassov #define SE_N_PHASE 4
30528ce675bSMihail Atanassov #define SE_SHIFT_N_PHASE 12
30628ce675bSMihail Atanassov /* Calculate initial_phase and delta_phase for horizontal. */
30728ce675bSMihail Atanassov phase = s->input_w;
30828ce675bSMihail Atanassov s->h_init_phase =
30928ce675bSMihail Atanassov ((phase << SE_N_PHASE) / s->output_w + 1) / 2;
31028ce675bSMihail Atanassov
31128ce675bSMihail Atanassov phase = s->input_w;
31228ce675bSMihail Atanassov phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
31328ce675bSMihail Atanassov s->h_delta_phase = phase / s->output_w;
31428ce675bSMihail Atanassov
31528ce675bSMihail Atanassov /* Same for vertical. */
31628ce675bSMihail Atanassov phase = s->input_h;
31728ce675bSMihail Atanassov s->v_init_phase =
31828ce675bSMihail Atanassov ((phase << SE_N_PHASE) / s->output_h + 1) / 2;
31928ce675bSMihail Atanassov
32028ce675bSMihail Atanassov phase = s->input_h;
32128ce675bSMihail Atanassov phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
32228ce675bSMihail Atanassov s->v_delta_phase = phase / s->output_h;
32328ce675bSMihail Atanassov #undef SE_N_PHASE
32428ce675bSMihail Atanassov #undef SE_SHIFT_N_PHASE
32528ce675bSMihail Atanassov s->plane_src_id = mp->layer->id;
32628ce675bSMihail Atanassov }
32728ce675bSMihail Atanassov
32828ce675bSMihail Atanassov s->scale_enable = true;
32928ce675bSMihail Atanassov s->hcoeff = malidp_se_select_coeffs(h_upscale_factor);
33028ce675bSMihail Atanassov s->vcoeff = malidp_se_select_coeffs(v_upscale_factor);
331c2e7f82dSMihail Atanassov
332c2e7f82dSMihail Atanassov mclk_calc:
333c2e7f82dSMihail Atanassov drm_display_mode_to_videomode(&state->adjusted_mode, &vm);
334a6993b21SLiviu Dudau ret = hwdev->hw->se_calc_mclk(hwdev, s, &vm);
335c2e7f82dSMihail Atanassov if (ret < 0)
336c2e7f82dSMihail Atanassov return -EINVAL;
33728ce675bSMihail Atanassov return 0;
33828ce675bSMihail Atanassov }
33928ce675bSMihail Atanassov
malidp_crtc_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)340ad49f860SLiviu Dudau static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
34129b77ad7SMaxime Ripard struct drm_atomic_state *state)
342ad49f860SLiviu Dudau {
34329b77ad7SMaxime Ripard struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
34429b77ad7SMaxime Ripard crtc);
345ad49f860SLiviu Dudau struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
346ad49f860SLiviu Dudau struct malidp_hw_device *hwdev = malidp->dev;
347ad49f860SLiviu Dudau struct drm_plane *plane;
348ad49f860SLiviu Dudau const struct drm_plane_state *pstate;
349ad49f860SLiviu Dudau u32 rot_mem_free, rot_mem_usable;
350ad49f860SLiviu Dudau int rotated_planes = 0;
3516954f245SMihail Atanassov int ret;
352ad49f860SLiviu Dudau
353ad49f860SLiviu Dudau /*
354ad49f860SLiviu Dudau * check if there is enough rotation memory available for planes
35566da13a5SLiviu Dudau * that need 90° and 270° rotion or planes that are compressed.
35666da13a5SLiviu Dudau * Each plane has set its required memory size in the ->plane_check()
35766da13a5SLiviu Dudau * callback, here we only make sure that the sums are less that the
35866da13a5SLiviu Dudau * total usable memory.
359ad49f860SLiviu Dudau *
360ad49f860SLiviu Dudau * The rotation memory allocation algorithm (for each plane):
36166da13a5SLiviu Dudau * a. If no more rotated or compressed planes exist, all remaining
36266da13a5SLiviu Dudau * rotate memory in the bank is available for use by the plane.
36366da13a5SLiviu Dudau * b. If other rotated or compressed planes exist, and plane's
36466da13a5SLiviu Dudau * layer ID is DE_VIDEO1, it can use all the memory from first bank
36566da13a5SLiviu Dudau * if secondary rotation memory bank is available, otherwise it can
366ad49f860SLiviu Dudau * use up to half the bank's memory.
36766da13a5SLiviu Dudau * c. If other rotated or compressed planes exist, and plane's layer ID
36866da13a5SLiviu Dudau * is not DE_VIDEO1, it can use half of the available memory.
369ad49f860SLiviu Dudau *
370ad49f860SLiviu Dudau * Note: this algorithm assumes that the order in which the planes are
371ad49f860SLiviu Dudau * checked always has DE_VIDEO1 plane first in the list if it is
372ad49f860SLiviu Dudau * rotated. Because that is how we create the planes in the first
373ad49f860SLiviu Dudau * place, under current DRM version things work, but if ever the order
374ad49f860SLiviu Dudau * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
375ad49f860SLiviu Dudau * changes, we need to pre-sort the planes before validation.
376ad49f860SLiviu Dudau */
377ad49f860SLiviu Dudau
378ad49f860SLiviu Dudau /* first count the number of rotated planes */
37929b77ad7SMaxime Ripard drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
38066da13a5SLiviu Dudau struct drm_framebuffer *fb = pstate->fb;
38166da13a5SLiviu Dudau
38266da13a5SLiviu Dudau if ((pstate->rotation & MALIDP_ROTATED_MASK) || fb->modifier)
383ad49f860SLiviu Dudau rotated_planes++;
384ad49f860SLiviu Dudau }
385ad49f860SLiviu Dudau
386ad49f860SLiviu Dudau rot_mem_free = hwdev->rotation_memory[0];
387ad49f860SLiviu Dudau /*
388ad49f860SLiviu Dudau * if we have more than 1 plane using rotation memory, use the second
389ad49f860SLiviu Dudau * block of rotation memory as well
390ad49f860SLiviu Dudau */
391ad49f860SLiviu Dudau if (rotated_planes > 1)
392ad49f860SLiviu Dudau rot_mem_free += hwdev->rotation_memory[1];
393ad49f860SLiviu Dudau
394ad49f860SLiviu Dudau /* now validate the rotation memory requirements */
39529b77ad7SMaxime Ripard drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
396ad49f860SLiviu Dudau struct malidp_plane *mp = to_malidp_plane(plane);
397ad49f860SLiviu Dudau struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
39866da13a5SLiviu Dudau struct drm_framebuffer *fb = pstate->fb;
399ad49f860SLiviu Dudau
40066da13a5SLiviu Dudau if ((pstate->rotation & MALIDP_ROTATED_MASK) || fb->modifier) {
401ad49f860SLiviu Dudau /* process current plane */
402ad49f860SLiviu Dudau rotated_planes--;
403ad49f860SLiviu Dudau
404ad49f860SLiviu Dudau if (!rotated_planes) {
405ad49f860SLiviu Dudau /* no more rotated planes, we can use what's left */
406ad49f860SLiviu Dudau rot_mem_usable = rot_mem_free;
407ad49f860SLiviu Dudau } else {
408ad49f860SLiviu Dudau if ((mp->layer->id != DE_VIDEO1) ||
409ad49f860SLiviu Dudau (hwdev->rotation_memory[1] == 0))
410ad49f860SLiviu Dudau rot_mem_usable = rot_mem_free / 2;
411ad49f860SLiviu Dudau else
412ad49f860SLiviu Dudau rot_mem_usable = hwdev->rotation_memory[0];
413ad49f860SLiviu Dudau }
414ad49f860SLiviu Dudau
415ad49f860SLiviu Dudau rot_mem_free -= rot_mem_usable;
416ad49f860SLiviu Dudau
417ad49f860SLiviu Dudau if (ms->rotmem_size > rot_mem_usable)
418ad49f860SLiviu Dudau return -EINVAL;
419ad49f860SLiviu Dudau }
420ad49f860SLiviu Dudau }
421ad49f860SLiviu Dudau
4228cbc5cafSBrian Starkey /* If only the writeback routing has changed, we don't need a modeset */
42329b77ad7SMaxime Ripard if (crtc_state->connectors_changed) {
4248cbc5cafSBrian Starkey u32 old_mask = crtc->state->connector_mask;
42529b77ad7SMaxime Ripard u32 new_mask = crtc_state->connector_mask;
4268cbc5cafSBrian Starkey
4278cbc5cafSBrian Starkey if ((old_mask ^ new_mask) ==
4288cbc5cafSBrian Starkey (1 << drm_connector_index(&malidp->mw_connector.base)))
42929b77ad7SMaxime Ripard crtc_state->connectors_changed = false;
4308cbc5cafSBrian Starkey }
4318cbc5cafSBrian Starkey
43229b77ad7SMaxime Ripard ret = malidp_crtc_atomic_check_gamma(crtc, crtc_state);
43329b77ad7SMaxime Ripard ret = ret ? ret : malidp_crtc_atomic_check_ctm(crtc, crtc_state);
43429b77ad7SMaxime Ripard ret = ret ? ret : malidp_crtc_atomic_check_scaling(crtc, crtc_state);
4356954f245SMihail Atanassov
4366954f245SMihail Atanassov return ret;
437ad49f860SLiviu Dudau }
438ad49f860SLiviu Dudau
439ad49f860SLiviu Dudau static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
440e2113c03SJose Abreu .mode_valid = malidp_crtc_mode_valid,
441ad49f860SLiviu Dudau .atomic_check = malidp_crtc_atomic_check,
4420b20a0f8SLaurent Pinchart .atomic_enable = malidp_crtc_atomic_enable,
44364581714SLaurent Pinchart .atomic_disable = malidp_crtc_atomic_disable,
444ad49f860SLiviu Dudau };
445ad49f860SLiviu Dudau
malidp_crtc_duplicate_state(struct drm_crtc * crtc)44699665d07SMihail Atanassov static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
44799665d07SMihail Atanassov {
44802725d31SMihail Atanassov struct malidp_crtc_state *state, *old_state;
44999665d07SMihail Atanassov
45099665d07SMihail Atanassov if (WARN_ON(!crtc->state))
45199665d07SMihail Atanassov return NULL;
45299665d07SMihail Atanassov
45302725d31SMihail Atanassov old_state = to_malidp_crtc_state(crtc->state);
45499665d07SMihail Atanassov state = kmalloc(sizeof(*state), GFP_KERNEL);
45599665d07SMihail Atanassov if (!state)
45699665d07SMihail Atanassov return NULL;
45799665d07SMihail Atanassov
45899665d07SMihail Atanassov __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
45902725d31SMihail Atanassov memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
46002725d31SMihail Atanassov sizeof(state->gamma_coeffs));
4616954f245SMihail Atanassov memcpy(state->coloradj_coeffs, old_state->coloradj_coeffs,
4626954f245SMihail Atanassov sizeof(state->coloradj_coeffs));
46328ce675bSMihail Atanassov memcpy(&state->scaler_config, &old_state->scaler_config,
46428ce675bSMihail Atanassov sizeof(state->scaler_config));
46528ce675bSMihail Atanassov state->scaled_planes_mask = 0;
46699665d07SMihail Atanassov
46799665d07SMihail Atanassov return &state->base;
46899665d07SMihail Atanassov }
46999665d07SMihail Atanassov
malidp_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)47099665d07SMihail Atanassov static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
47199665d07SMihail Atanassov struct drm_crtc_state *state)
47299665d07SMihail Atanassov {
47399665d07SMihail Atanassov struct malidp_crtc_state *mali_state = NULL;
47499665d07SMihail Atanassov
47599665d07SMihail Atanassov if (state) {
47699665d07SMihail Atanassov mali_state = to_malidp_crtc_state(state);
47799665d07SMihail Atanassov __drm_atomic_helper_crtc_destroy_state(state);
47899665d07SMihail Atanassov }
47999665d07SMihail Atanassov
48099665d07SMihail Atanassov kfree(mali_state);
48199665d07SMihail Atanassov }
48299665d07SMihail Atanassov
malidp_crtc_reset(struct drm_crtc * crtc)4839a6a19c0SMaarten Lankhorst static void malidp_crtc_reset(struct drm_crtc *crtc)
4849a6a19c0SMaarten Lankhorst {
4859a6a19c0SMaarten Lankhorst struct malidp_crtc_state *state =
4869a6a19c0SMaarten Lankhorst kzalloc(sizeof(*state), GFP_KERNEL);
4879a6a19c0SMaarten Lankhorst
4889a6a19c0SMaarten Lankhorst if (crtc->state)
4899a6a19c0SMaarten Lankhorst malidp_crtc_destroy_state(crtc, crtc->state);
4909a6a19c0SMaarten Lankhorst
49173c3ed74SJiasheng Jiang if (state)
4929a6a19c0SMaarten Lankhorst __drm_atomic_helper_crtc_reset(crtc, &state->base);
49373c3ed74SJiasheng Jiang else
49473c3ed74SJiasheng Jiang __drm_atomic_helper_crtc_reset(crtc, NULL);
4959a6a19c0SMaarten Lankhorst }
4969a6a19c0SMaarten Lankhorst
malidp_crtc_enable_vblank(struct drm_crtc * crtc)497d7ae94beSShawn Guo static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
498d7ae94beSShawn Guo {
499d7ae94beSShawn Guo struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
500d7ae94beSShawn Guo struct malidp_hw_device *hwdev = malidp->dev;
501d7ae94beSShawn Guo
502d7ae94beSShawn Guo malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
503a6993b21SLiviu Dudau hwdev->hw->map.de_irq_map.vsync_irq);
504d7ae94beSShawn Guo return 0;
505d7ae94beSShawn Guo }
506d7ae94beSShawn Guo
malidp_crtc_disable_vblank(struct drm_crtc * crtc)507d7ae94beSShawn Guo static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
508d7ae94beSShawn Guo {
509d7ae94beSShawn Guo struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
510d7ae94beSShawn Guo struct malidp_hw_device *hwdev = malidp->dev;
511d7ae94beSShawn Guo
512d7ae94beSShawn Guo malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
513a6993b21SLiviu Dudau hwdev->hw->map.de_irq_map.vsync_irq);
514d7ae94beSShawn Guo }
515d7ae94beSShawn Guo
516ad49f860SLiviu Dudau static const struct drm_crtc_funcs malidp_crtc_funcs = {
517ad49f860SLiviu Dudau .set_config = drm_atomic_helper_set_config,
518ad49f860SLiviu Dudau .page_flip = drm_atomic_helper_page_flip,
51999665d07SMihail Atanassov .reset = malidp_crtc_reset,
52099665d07SMihail Atanassov .atomic_duplicate_state = malidp_crtc_duplicate_state,
52199665d07SMihail Atanassov .atomic_destroy_state = malidp_crtc_destroy_state,
522d7ae94beSShawn Guo .enable_vblank = malidp_crtc_enable_vblank,
523d7ae94beSShawn Guo .disable_vblank = malidp_crtc_disable_vblank,
524ad49f860SLiviu Dudau };
525ad49f860SLiviu Dudau
malidp_crtc_init(struct drm_device * drm)526ad49f860SLiviu Dudau int malidp_crtc_init(struct drm_device *drm)
527ad49f860SLiviu Dudau {
5281b93d3cbSDanilo Krummrich struct malidp_drm *malidp = drm_to_malidp(drm);
529ad49f860SLiviu Dudau struct drm_plane *primary = NULL, *plane;
530ad49f860SLiviu Dudau int ret;
531ad49f860SLiviu Dudau
532ad49f860SLiviu Dudau ret = malidp_de_planes_init(drm);
533ad49f860SLiviu Dudau if (ret < 0) {
534ad49f860SLiviu Dudau DRM_ERROR("Failed to initialise planes\n");
535ad49f860SLiviu Dudau return ret;
536ad49f860SLiviu Dudau }
537ad49f860SLiviu Dudau
538ad49f860SLiviu Dudau drm_for_each_plane(plane, drm) {
539ad49f860SLiviu Dudau if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
540ad49f860SLiviu Dudau primary = plane;
541ad49f860SLiviu Dudau break;
542ad49f860SLiviu Dudau }
543ad49f860SLiviu Dudau }
544ad49f860SLiviu Dudau
545ad49f860SLiviu Dudau if (!primary) {
546ad49f860SLiviu Dudau DRM_ERROR("no primary plane found\n");
547084ffbd7SLaurent Pinchart return -EINVAL;
548ad49f860SLiviu Dudau }
549ad49f860SLiviu Dudau
550*905ff163SDanilo Krummrich ret = drmm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
551ad49f860SLiviu Dudau &malidp_crtc_funcs, NULL);
55202725d31SMihail Atanassov if (ret)
553084ffbd7SLaurent Pinchart return ret;
554ad49f860SLiviu Dudau
555ad49f860SLiviu Dudau drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
55602725d31SMihail Atanassov drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
5570274e6a0SMihail Atanassov /* No inverse-gamma: it is per-plane. */
5586954f245SMihail Atanassov drm_crtc_enable_color_mgmt(&malidp->crtc, 0, true, MALIDP_GAMMA_LUT_SIZE);
55902725d31SMihail Atanassov
5600274e6a0SMihail Atanassov malidp_se_set_enh_coeffs(malidp->dev);
5610274e6a0SMihail Atanassov
562ad49f860SLiviu Dudau return 0;
563ad49f860SLiviu Dudau }
564