1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
4*4882a593Smuzhiyun * Author: Liviu Dudau <Liviu.Dudau@arm.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * ARM Mali DP500/DP550/DP650 driver (crtc operations)
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/clk.h>
10*4882a593Smuzhiyun #include <linux/pm_runtime.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <video/videomode.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <drm/drm_atomic.h>
15*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
16*4882a593Smuzhiyun #include <drm/drm_crtc.h>
17*4882a593Smuzhiyun #include <drm/drm_print.h>
18*4882a593Smuzhiyun #include <drm/drm_probe_helper.h>
19*4882a593Smuzhiyun #include <drm/drm_vblank.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "malidp_drv.h"
22*4882a593Smuzhiyun #include "malidp_hw.h"
23*4882a593Smuzhiyun
malidp_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)24*4882a593Smuzhiyun static enum drm_mode_status malidp_crtc_mode_valid(struct drm_crtc *crtc,
25*4882a593Smuzhiyun const struct drm_display_mode *mode)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
28*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * check that the hardware can drive the required clock rate,
32*4882a593Smuzhiyun * but skip the check if the clock is meant to be disabled (req_rate = 0)
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun long rate, req_rate = mode->crtc_clock * 1000;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun if (req_rate) {
37*4882a593Smuzhiyun rate = clk_round_rate(hwdev->pxlclk, req_rate);
38*4882a593Smuzhiyun if (rate != req_rate) {
39*4882a593Smuzhiyun DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
40*4882a593Smuzhiyun req_rate);
41*4882a593Smuzhiyun return MODE_NOCLOCK;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun return MODE_OK;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
malidp_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)48*4882a593Smuzhiyun static void malidp_crtc_atomic_enable(struct drm_crtc *crtc,
49*4882a593Smuzhiyun struct drm_crtc_state *old_state)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
52*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
53*4882a593Smuzhiyun struct videomode vm;
54*4882a593Smuzhiyun int err = pm_runtime_get_sync(crtc->dev->dev);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (err < 0) {
57*4882a593Smuzhiyun DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
58*4882a593Smuzhiyun return;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
62*4882a593Smuzhiyun clk_prepare_enable(hwdev->pxlclk);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* We rely on firmware to set mclk to a sensible level. */
65*4882a593Smuzhiyun clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun hwdev->hw->modeset(hwdev, &vm);
68*4882a593Smuzhiyun hwdev->hw->leave_config_mode(hwdev);
69*4882a593Smuzhiyun drm_crtc_vblank_on(crtc);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
malidp_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)72*4882a593Smuzhiyun static void malidp_crtc_atomic_disable(struct drm_crtc *crtc,
73*4882a593Smuzhiyun struct drm_crtc_state *old_state)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
76*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
77*4882a593Smuzhiyun int err;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* always disable planes on the CRTC that is being turned off */
80*4882a593Smuzhiyun drm_atomic_helper_disable_planes_on_crtc(old_state, false);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun drm_crtc_vblank_off(crtc);
83*4882a593Smuzhiyun hwdev->hw->enter_config_mode(hwdev);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun clk_disable_unprepare(hwdev->pxlclk);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun err = pm_runtime_put(crtc->dev->dev);
88*4882a593Smuzhiyun if (err < 0) {
89*4882a593Smuzhiyun DRM_DEBUG_DRIVER("Failed to disable runtime power management: %d\n", err);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun static const struct gamma_curve_segment {
94*4882a593Smuzhiyun u16 start;
95*4882a593Smuzhiyun u16 end;
96*4882a593Smuzhiyun } segments[MALIDP_COEFFTAB_NUM_COEFFS] = {
97*4882a593Smuzhiyun /* sector 0 */
98*4882a593Smuzhiyun { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 },
99*4882a593Smuzhiyun { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
100*4882a593Smuzhiyun { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 },
101*4882a593Smuzhiyun { 12, 12 }, { 13, 13 }, { 14, 14 }, { 15, 15 },
102*4882a593Smuzhiyun /* sector 1 */
103*4882a593Smuzhiyun { 16, 19 }, { 20, 23 }, { 24, 27 }, { 28, 31 },
104*4882a593Smuzhiyun /* sector 2 */
105*4882a593Smuzhiyun { 32, 39 }, { 40, 47 }, { 48, 55 }, { 56, 63 },
106*4882a593Smuzhiyun /* sector 3 */
107*4882a593Smuzhiyun { 64, 79 }, { 80, 95 }, { 96, 111 }, { 112, 127 },
108*4882a593Smuzhiyun /* sector 4 */
109*4882a593Smuzhiyun { 128, 159 }, { 160, 191 }, { 192, 223 }, { 224, 255 },
110*4882a593Smuzhiyun /* sector 5 */
111*4882a593Smuzhiyun { 256, 319 }, { 320, 383 }, { 384, 447 }, { 448, 511 },
112*4882a593Smuzhiyun /* sector 6 */
113*4882a593Smuzhiyun { 512, 639 }, { 640, 767 }, { 768, 895 }, { 896, 1023 },
114*4882a593Smuzhiyun { 1024, 1151 }, { 1152, 1279 }, { 1280, 1407 }, { 1408, 1535 },
115*4882a593Smuzhiyun { 1536, 1663 }, { 1664, 1791 }, { 1792, 1919 }, { 1920, 2047 },
116*4882a593Smuzhiyun { 2048, 2175 }, { 2176, 2303 }, { 2304, 2431 }, { 2432, 2559 },
117*4882a593Smuzhiyun { 2560, 2687 }, { 2688, 2815 }, { 2816, 2943 }, { 2944, 3071 },
118*4882a593Smuzhiyun { 3072, 3199 }, { 3200, 3327 }, { 3328, 3455 }, { 3456, 3583 },
119*4882a593Smuzhiyun { 3584, 3711 }, { 3712, 3839 }, { 3840, 3967 }, { 3968, 4095 },
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun #define DE_COEFTAB_DATA(a, b) ((((a) & 0xfff) << 16) | (((b) & 0xfff)))
123*4882a593Smuzhiyun
malidp_generate_gamma_table(struct drm_property_blob * lut_blob,u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])124*4882a593Smuzhiyun static void malidp_generate_gamma_table(struct drm_property_blob *lut_blob,
125*4882a593Smuzhiyun u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun struct drm_color_lut *lut = (struct drm_color_lut *)lut_blob->data;
128*4882a593Smuzhiyun int i;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i) {
131*4882a593Smuzhiyun u32 a, b, delta_in, out_start, out_end;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun delta_in = segments[i].end - segments[i].start;
134*4882a593Smuzhiyun /* DP has 12-bit internal precision for its LUTs. */
135*4882a593Smuzhiyun out_start = drm_color_lut_extract(lut[segments[i].start].green,
136*4882a593Smuzhiyun 12);
137*4882a593Smuzhiyun out_end = drm_color_lut_extract(lut[segments[i].end].green, 12);
138*4882a593Smuzhiyun a = (delta_in == 0) ? 0 : ((out_end - out_start) * 256) / delta_in;
139*4882a593Smuzhiyun b = out_start;
140*4882a593Smuzhiyun coeffs[i] = DE_COEFTAB_DATA(a, b);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * Check if there is a new gamma LUT and if it is of an acceptable size. Also,
146*4882a593Smuzhiyun * reject any LUTs that use distinct red, green, and blue curves.
147*4882a593Smuzhiyun */
malidp_crtc_atomic_check_gamma(struct drm_crtc * crtc,struct drm_crtc_state * state)148*4882a593Smuzhiyun static int malidp_crtc_atomic_check_gamma(struct drm_crtc *crtc,
149*4882a593Smuzhiyun struct drm_crtc_state *state)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
152*4882a593Smuzhiyun struct drm_color_lut *lut;
153*4882a593Smuzhiyun size_t lut_size;
154*4882a593Smuzhiyun int i;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (!state->color_mgmt_changed || !state->gamma_lut)
157*4882a593Smuzhiyun return 0;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if (crtc->state->gamma_lut &&
160*4882a593Smuzhiyun (crtc->state->gamma_lut->base.id == state->gamma_lut->base.id))
161*4882a593Smuzhiyun return 0;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (state->gamma_lut->length % sizeof(struct drm_color_lut))
164*4882a593Smuzhiyun return -EINVAL;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun lut_size = state->gamma_lut->length / sizeof(struct drm_color_lut);
167*4882a593Smuzhiyun if (lut_size != MALIDP_GAMMA_LUT_SIZE)
168*4882a593Smuzhiyun return -EINVAL;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun lut = (struct drm_color_lut *)state->gamma_lut->data;
171*4882a593Smuzhiyun for (i = 0; i < lut_size; ++i)
172*4882a593Smuzhiyun if (!((lut[i].red == lut[i].green) &&
173*4882a593Smuzhiyun (lut[i].red == lut[i].blue)))
174*4882a593Smuzhiyun return -EINVAL;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (!state->mode_changed) {
177*4882a593Smuzhiyun int ret;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun state->mode_changed = true;
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * Kerneldoc for drm_atomic_helper_check_modeset mandates that
182*4882a593Smuzhiyun * it be invoked when the driver sets ->mode_changed. Since
183*4882a593Smuzhiyun * changing the gamma LUT doesn't depend on any external
184*4882a593Smuzhiyun * resources, it is safe to call it only once.
185*4882a593Smuzhiyun */
186*4882a593Smuzhiyun ret = drm_atomic_helper_check_modeset(crtc->dev, state->state);
187*4882a593Smuzhiyun if (ret)
188*4882a593Smuzhiyun return ret;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun malidp_generate_gamma_table(state->gamma_lut, mc->gamma_coeffs);
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * Check if there is a new CTM and if it contains valid input. Valid here means
197*4882a593Smuzhiyun * that the number is inside the representable range for a Q3.12 number,
198*4882a593Smuzhiyun * excluding truncating the fractional part of the input data.
199*4882a593Smuzhiyun *
200*4882a593Smuzhiyun * The COLORADJ registers can be changed atomically.
201*4882a593Smuzhiyun */
malidp_crtc_atomic_check_ctm(struct drm_crtc * crtc,struct drm_crtc_state * state)202*4882a593Smuzhiyun static int malidp_crtc_atomic_check_ctm(struct drm_crtc *crtc,
203*4882a593Smuzhiyun struct drm_crtc_state *state)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
206*4882a593Smuzhiyun struct drm_color_ctm *ctm;
207*4882a593Smuzhiyun int i;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (!state->color_mgmt_changed)
210*4882a593Smuzhiyun return 0;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (!state->ctm)
213*4882a593Smuzhiyun return 0;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun if (crtc->state->ctm && (crtc->state->ctm->base.id ==
216*4882a593Smuzhiyun state->ctm->base.id))
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun * The size of the ctm is checked in
221*4882a593Smuzhiyun * drm_atomic_replace_property_blob_from_id.
222*4882a593Smuzhiyun */
223*4882a593Smuzhiyun ctm = (struct drm_color_ctm *)state->ctm->data;
224*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ctm->matrix); ++i) {
225*4882a593Smuzhiyun /* Convert from S31.32 to Q3.12. */
226*4882a593Smuzhiyun s64 val = ctm->matrix[i];
227*4882a593Smuzhiyun u32 mag = ((((u64)val) & ~BIT_ULL(63)) >> 20) &
228*4882a593Smuzhiyun GENMASK_ULL(14, 0);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * Convert to 2s complement and check the destination's top bit
232*4882a593Smuzhiyun * for overflow. NB: Can't check before converting or it'd
233*4882a593Smuzhiyun * incorrectly reject the case:
234*4882a593Smuzhiyun * sign == 1
235*4882a593Smuzhiyun * mag == 0x2000
236*4882a593Smuzhiyun */
237*4882a593Smuzhiyun if (val & BIT_ULL(63))
238*4882a593Smuzhiyun mag = ~mag + 1;
239*4882a593Smuzhiyun if (!!(val & BIT_ULL(63)) != !!(mag & BIT(14)))
240*4882a593Smuzhiyun return -EINVAL;
241*4882a593Smuzhiyun mc->coloradj_coeffs[i] = mag;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun return 0;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
malidp_crtc_atomic_check_scaling(struct drm_crtc * crtc,struct drm_crtc_state * state)247*4882a593Smuzhiyun static int malidp_crtc_atomic_check_scaling(struct drm_crtc *crtc,
248*4882a593Smuzhiyun struct drm_crtc_state *state)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
251*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
252*4882a593Smuzhiyun struct malidp_crtc_state *cs = to_malidp_crtc_state(state);
253*4882a593Smuzhiyun struct malidp_se_config *s = &cs->scaler_config;
254*4882a593Smuzhiyun struct drm_plane *plane;
255*4882a593Smuzhiyun struct videomode vm;
256*4882a593Smuzhiyun const struct drm_plane_state *pstate;
257*4882a593Smuzhiyun u32 h_upscale_factor = 0; /* U16.16 */
258*4882a593Smuzhiyun u32 v_upscale_factor = 0; /* U16.16 */
259*4882a593Smuzhiyun u8 scaling = cs->scaled_planes_mask;
260*4882a593Smuzhiyun int ret;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (!scaling) {
263*4882a593Smuzhiyun s->scale_enable = false;
264*4882a593Smuzhiyun goto mclk_calc;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* The scaling engine can only handle one plane at a time. */
268*4882a593Smuzhiyun if (scaling & (scaling - 1))
269*4882a593Smuzhiyun return -EINVAL;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
272*4882a593Smuzhiyun struct malidp_plane *mp = to_malidp_plane(plane);
273*4882a593Smuzhiyun u32 phase;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun if (!(mp->layer->id & scaling))
276*4882a593Smuzhiyun continue;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * Convert crtc_[w|h] to U32.32, then divide by U16.16 src_[w|h]
280*4882a593Smuzhiyun * to get the U16.16 result.
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun h_upscale_factor = div_u64((u64)pstate->crtc_w << 32,
283*4882a593Smuzhiyun pstate->src_w);
284*4882a593Smuzhiyun v_upscale_factor = div_u64((u64)pstate->crtc_h << 32,
285*4882a593Smuzhiyun pstate->src_h);
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun s->enhancer_enable = ((h_upscale_factor >> 16) >= 2 ||
288*4882a593Smuzhiyun (v_upscale_factor >> 16) >= 2);
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun if (pstate->rotation & MALIDP_ROTATED_MASK) {
291*4882a593Smuzhiyun s->input_w = pstate->src_h >> 16;
292*4882a593Smuzhiyun s->input_h = pstate->src_w >> 16;
293*4882a593Smuzhiyun } else {
294*4882a593Smuzhiyun s->input_w = pstate->src_w >> 16;
295*4882a593Smuzhiyun s->input_h = pstate->src_h >> 16;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun s->output_w = pstate->crtc_w;
299*4882a593Smuzhiyun s->output_h = pstate->crtc_h;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun #define SE_N_PHASE 4
302*4882a593Smuzhiyun #define SE_SHIFT_N_PHASE 12
303*4882a593Smuzhiyun /* Calculate initial_phase and delta_phase for horizontal. */
304*4882a593Smuzhiyun phase = s->input_w;
305*4882a593Smuzhiyun s->h_init_phase =
306*4882a593Smuzhiyun ((phase << SE_N_PHASE) / s->output_w + 1) / 2;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun phase = s->input_w;
309*4882a593Smuzhiyun phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
310*4882a593Smuzhiyun s->h_delta_phase = phase / s->output_w;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* Same for vertical. */
313*4882a593Smuzhiyun phase = s->input_h;
314*4882a593Smuzhiyun s->v_init_phase =
315*4882a593Smuzhiyun ((phase << SE_N_PHASE) / s->output_h + 1) / 2;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun phase = s->input_h;
318*4882a593Smuzhiyun phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
319*4882a593Smuzhiyun s->v_delta_phase = phase / s->output_h;
320*4882a593Smuzhiyun #undef SE_N_PHASE
321*4882a593Smuzhiyun #undef SE_SHIFT_N_PHASE
322*4882a593Smuzhiyun s->plane_src_id = mp->layer->id;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun s->scale_enable = true;
326*4882a593Smuzhiyun s->hcoeff = malidp_se_select_coeffs(h_upscale_factor);
327*4882a593Smuzhiyun s->vcoeff = malidp_se_select_coeffs(v_upscale_factor);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun mclk_calc:
330*4882a593Smuzhiyun drm_display_mode_to_videomode(&state->adjusted_mode, &vm);
331*4882a593Smuzhiyun ret = hwdev->hw->se_calc_mclk(hwdev, s, &vm);
332*4882a593Smuzhiyun if (ret < 0)
333*4882a593Smuzhiyun return -EINVAL;
334*4882a593Smuzhiyun return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
malidp_crtc_atomic_check(struct drm_crtc * crtc,struct drm_crtc_state * state)337*4882a593Smuzhiyun static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
338*4882a593Smuzhiyun struct drm_crtc_state *state)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
341*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
342*4882a593Smuzhiyun struct drm_plane *plane;
343*4882a593Smuzhiyun const struct drm_plane_state *pstate;
344*4882a593Smuzhiyun u32 rot_mem_free, rot_mem_usable;
345*4882a593Smuzhiyun int rotated_planes = 0;
346*4882a593Smuzhiyun int ret;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /*
349*4882a593Smuzhiyun * check if there is enough rotation memory available for planes
350*4882a593Smuzhiyun * that need 90° and 270° rotion or planes that are compressed.
351*4882a593Smuzhiyun * Each plane has set its required memory size in the ->plane_check()
352*4882a593Smuzhiyun * callback, here we only make sure that the sums are less that the
353*4882a593Smuzhiyun * total usable memory.
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * The rotation memory allocation algorithm (for each plane):
356*4882a593Smuzhiyun * a. If no more rotated or compressed planes exist, all remaining
357*4882a593Smuzhiyun * rotate memory in the bank is available for use by the plane.
358*4882a593Smuzhiyun * b. If other rotated or compressed planes exist, and plane's
359*4882a593Smuzhiyun * layer ID is DE_VIDEO1, it can use all the memory from first bank
360*4882a593Smuzhiyun * if secondary rotation memory bank is available, otherwise it can
361*4882a593Smuzhiyun * use up to half the bank's memory.
362*4882a593Smuzhiyun * c. If other rotated or compressed planes exist, and plane's layer ID
363*4882a593Smuzhiyun * is not DE_VIDEO1, it can use half of the available memory.
364*4882a593Smuzhiyun *
365*4882a593Smuzhiyun * Note: this algorithm assumes that the order in which the planes are
366*4882a593Smuzhiyun * checked always has DE_VIDEO1 plane first in the list if it is
367*4882a593Smuzhiyun * rotated. Because that is how we create the planes in the first
368*4882a593Smuzhiyun * place, under current DRM version things work, but if ever the order
369*4882a593Smuzhiyun * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
370*4882a593Smuzhiyun * changes, we need to pre-sort the planes before validation.
371*4882a593Smuzhiyun */
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* first count the number of rotated planes */
374*4882a593Smuzhiyun drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
375*4882a593Smuzhiyun struct drm_framebuffer *fb = pstate->fb;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if ((pstate->rotation & MALIDP_ROTATED_MASK) || fb->modifier)
378*4882a593Smuzhiyun rotated_planes++;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun rot_mem_free = hwdev->rotation_memory[0];
382*4882a593Smuzhiyun /*
383*4882a593Smuzhiyun * if we have more than 1 plane using rotation memory, use the second
384*4882a593Smuzhiyun * block of rotation memory as well
385*4882a593Smuzhiyun */
386*4882a593Smuzhiyun if (rotated_planes > 1)
387*4882a593Smuzhiyun rot_mem_free += hwdev->rotation_memory[1];
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /* now validate the rotation memory requirements */
390*4882a593Smuzhiyun drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
391*4882a593Smuzhiyun struct malidp_plane *mp = to_malidp_plane(plane);
392*4882a593Smuzhiyun struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
393*4882a593Smuzhiyun struct drm_framebuffer *fb = pstate->fb;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if ((pstate->rotation & MALIDP_ROTATED_MASK) || fb->modifier) {
396*4882a593Smuzhiyun /* process current plane */
397*4882a593Smuzhiyun rotated_planes--;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if (!rotated_planes) {
400*4882a593Smuzhiyun /* no more rotated planes, we can use what's left */
401*4882a593Smuzhiyun rot_mem_usable = rot_mem_free;
402*4882a593Smuzhiyun } else {
403*4882a593Smuzhiyun if ((mp->layer->id != DE_VIDEO1) ||
404*4882a593Smuzhiyun (hwdev->rotation_memory[1] == 0))
405*4882a593Smuzhiyun rot_mem_usable = rot_mem_free / 2;
406*4882a593Smuzhiyun else
407*4882a593Smuzhiyun rot_mem_usable = hwdev->rotation_memory[0];
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun rot_mem_free -= rot_mem_usable;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun if (ms->rotmem_size > rot_mem_usable)
413*4882a593Smuzhiyun return -EINVAL;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /* If only the writeback routing has changed, we don't need a modeset */
418*4882a593Smuzhiyun if (state->connectors_changed) {
419*4882a593Smuzhiyun u32 old_mask = crtc->state->connector_mask;
420*4882a593Smuzhiyun u32 new_mask = state->connector_mask;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun if ((old_mask ^ new_mask) ==
423*4882a593Smuzhiyun (1 << drm_connector_index(&malidp->mw_connector.base)))
424*4882a593Smuzhiyun state->connectors_changed = false;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun ret = malidp_crtc_atomic_check_gamma(crtc, state);
428*4882a593Smuzhiyun ret = ret ? ret : malidp_crtc_atomic_check_ctm(crtc, state);
429*4882a593Smuzhiyun ret = ret ? ret : malidp_crtc_atomic_check_scaling(crtc, state);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun return ret;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
435*4882a593Smuzhiyun .mode_valid = malidp_crtc_mode_valid,
436*4882a593Smuzhiyun .atomic_check = malidp_crtc_atomic_check,
437*4882a593Smuzhiyun .atomic_enable = malidp_crtc_atomic_enable,
438*4882a593Smuzhiyun .atomic_disable = malidp_crtc_atomic_disable,
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun
malidp_crtc_duplicate_state(struct drm_crtc * crtc)441*4882a593Smuzhiyun static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun struct malidp_crtc_state *state, *old_state;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun if (WARN_ON(!crtc->state))
446*4882a593Smuzhiyun return NULL;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun old_state = to_malidp_crtc_state(crtc->state);
449*4882a593Smuzhiyun state = kmalloc(sizeof(*state), GFP_KERNEL);
450*4882a593Smuzhiyun if (!state)
451*4882a593Smuzhiyun return NULL;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
454*4882a593Smuzhiyun memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
455*4882a593Smuzhiyun sizeof(state->gamma_coeffs));
456*4882a593Smuzhiyun memcpy(state->coloradj_coeffs, old_state->coloradj_coeffs,
457*4882a593Smuzhiyun sizeof(state->coloradj_coeffs));
458*4882a593Smuzhiyun memcpy(&state->scaler_config, &old_state->scaler_config,
459*4882a593Smuzhiyun sizeof(state->scaler_config));
460*4882a593Smuzhiyun state->scaled_planes_mask = 0;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun return &state->base;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun
malidp_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)465*4882a593Smuzhiyun static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
466*4882a593Smuzhiyun struct drm_crtc_state *state)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun struct malidp_crtc_state *mali_state = NULL;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun if (state) {
471*4882a593Smuzhiyun mali_state = to_malidp_crtc_state(state);
472*4882a593Smuzhiyun __drm_atomic_helper_crtc_destroy_state(state);
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun kfree(mali_state);
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
malidp_crtc_reset(struct drm_crtc * crtc)478*4882a593Smuzhiyun static void malidp_crtc_reset(struct drm_crtc *crtc)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun struct malidp_crtc_state *state =
481*4882a593Smuzhiyun kzalloc(sizeof(*state), GFP_KERNEL);
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (crtc->state)
484*4882a593Smuzhiyun malidp_crtc_destroy_state(crtc, crtc->state);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun if (state)
487*4882a593Smuzhiyun __drm_atomic_helper_crtc_reset(crtc, &state->base);
488*4882a593Smuzhiyun else
489*4882a593Smuzhiyun __drm_atomic_helper_crtc_reset(crtc, NULL);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
malidp_crtc_enable_vblank(struct drm_crtc * crtc)492*4882a593Smuzhiyun static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
495*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
498*4882a593Smuzhiyun hwdev->hw->map.de_irq_map.vsync_irq);
499*4882a593Smuzhiyun return 0;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
malidp_crtc_disable_vblank(struct drm_crtc * crtc)502*4882a593Smuzhiyun static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
505*4882a593Smuzhiyun struct malidp_hw_device *hwdev = malidp->dev;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
508*4882a593Smuzhiyun hwdev->hw->map.de_irq_map.vsync_irq);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun static const struct drm_crtc_funcs malidp_crtc_funcs = {
512*4882a593Smuzhiyun .gamma_set = drm_atomic_helper_legacy_gamma_set,
513*4882a593Smuzhiyun .destroy = drm_crtc_cleanup,
514*4882a593Smuzhiyun .set_config = drm_atomic_helper_set_config,
515*4882a593Smuzhiyun .page_flip = drm_atomic_helper_page_flip,
516*4882a593Smuzhiyun .reset = malidp_crtc_reset,
517*4882a593Smuzhiyun .atomic_duplicate_state = malidp_crtc_duplicate_state,
518*4882a593Smuzhiyun .atomic_destroy_state = malidp_crtc_destroy_state,
519*4882a593Smuzhiyun .enable_vblank = malidp_crtc_enable_vblank,
520*4882a593Smuzhiyun .disable_vblank = malidp_crtc_disable_vblank,
521*4882a593Smuzhiyun };
522*4882a593Smuzhiyun
malidp_crtc_init(struct drm_device * drm)523*4882a593Smuzhiyun int malidp_crtc_init(struct drm_device *drm)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun struct malidp_drm *malidp = drm->dev_private;
526*4882a593Smuzhiyun struct drm_plane *primary = NULL, *plane;
527*4882a593Smuzhiyun int ret;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun ret = malidp_de_planes_init(drm);
530*4882a593Smuzhiyun if (ret < 0) {
531*4882a593Smuzhiyun DRM_ERROR("Failed to initialise planes\n");
532*4882a593Smuzhiyun return ret;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun drm_for_each_plane(plane, drm) {
536*4882a593Smuzhiyun if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
537*4882a593Smuzhiyun primary = plane;
538*4882a593Smuzhiyun break;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun if (!primary) {
543*4882a593Smuzhiyun DRM_ERROR("no primary plane found\n");
544*4882a593Smuzhiyun return -EINVAL;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
548*4882a593Smuzhiyun &malidp_crtc_funcs, NULL);
549*4882a593Smuzhiyun if (ret)
550*4882a593Smuzhiyun return ret;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
553*4882a593Smuzhiyun drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
554*4882a593Smuzhiyun /* No inverse-gamma: it is per-plane. */
555*4882a593Smuzhiyun drm_crtc_enable_color_mgmt(&malidp->crtc, 0, true, MALIDP_GAMMA_LUT_SIZE);
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun malidp_se_set_enh_coeffs(malidp->dev);
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun return 0;
560*4882a593Smuzhiyun }
561