2 * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
10 * ARM Mali DP500/DP550/DP650 driver (crtc operations)
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <linux/clk.h>
19 #include <linux/pm_runtime.h>
20 #include <video/videomode.h>
22 #include "malidp_drv.h"
23 #include "malidp_hw.h"
25 static enum drm_mode_status malidp_crtc_mode_valid(struct drm_crtc *crtc,
26 const struct drm_display_mode *mode)
28 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
29 struct malidp_hw_device *hwdev = malidp->dev;
32 * check that the hardware can drive the required clock rate,
33 * but skip the check if the clock is meant to be disabled (req_rate = 0)
35 long rate, req_rate = mode->crtc_clock * 1000;
38 rate = clk_round_rate(hwdev->pxlclk, req_rate);
39 if (rate != req_rate) {
40 DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
49 static void malidp_crtc_enable(struct drm_crtc *crtc)
51 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
52 struct malidp_hw_device *hwdev = malidp->dev;
54 int err = pm_runtime_get_sync(crtc->dev->dev);
57 DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
61 drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
62 clk_prepare_enable(hwdev->pxlclk);
64 /* We rely on firmware to set mclk to a sensible level. */
65 clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
67 hwdev->modeset(hwdev, &vm);
68 hwdev->leave_config_mode(hwdev);
69 drm_crtc_vblank_on(crtc);
72 static void malidp_crtc_disable(struct drm_crtc *crtc)
74 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
75 struct malidp_hw_device *hwdev = malidp->dev;
78 drm_crtc_vblank_off(crtc);
79 hwdev->enter_config_mode(hwdev);
80 clk_disable_unprepare(hwdev->pxlclk);
82 err = pm_runtime_put(crtc->dev->dev);
84 DRM_DEBUG_DRIVER("Failed to disable runtime power management: %d\n", err);
88 static const struct gamma_curve_segment {
91 } segments[MALIDP_COEFFTAB_NUM_COEFFS] = {
93 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 },
94 { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
95 { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 },
96 { 12, 12 }, { 13, 13 }, { 14, 14 }, { 15, 15 },
98 { 16, 19 }, { 20, 23 }, { 24, 27 }, { 28, 31 },
100 { 32, 39 }, { 40, 47 }, { 48, 55 }, { 56, 63 },
102 { 64, 79 }, { 80, 95 }, { 96, 111 }, { 112, 127 },
104 { 128, 159 }, { 160, 191 }, { 192, 223 }, { 224, 255 },
106 { 256, 319 }, { 320, 383 }, { 384, 447 }, { 448, 511 },
108 { 512, 639 }, { 640, 767 }, { 768, 895 }, { 896, 1023 },
109 { 1024, 1151 }, { 1152, 1279 }, { 1280, 1407 }, { 1408, 1535 },
110 { 1536, 1663 }, { 1664, 1791 }, { 1792, 1919 }, { 1920, 2047 },
111 { 2048, 2175 }, { 2176, 2303 }, { 2304, 2431 }, { 2432, 2559 },
112 { 2560, 2687 }, { 2688, 2815 }, { 2816, 2943 }, { 2944, 3071 },
113 { 3072, 3199 }, { 3200, 3327 }, { 3328, 3455 }, { 3456, 3583 },
114 { 3584, 3711 }, { 3712, 3839 }, { 3840, 3967 }, { 3968, 4095 },
117 #define DE_COEFTAB_DATA(a, b) ((((a) & 0xfff) << 16) | (((b) & 0xfff)))
119 static void malidp_generate_gamma_table(struct drm_property_blob *lut_blob,
120 u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])
122 struct drm_color_lut *lut = (struct drm_color_lut *)lut_blob->data;
125 for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i) {
126 u32 a, b, delta_in, out_start, out_end;
128 delta_in = segments[i].end - segments[i].start;
129 /* DP has 12-bit internal precision for its LUTs. */
130 out_start = drm_color_lut_extract(lut[segments[i].start].green,
132 out_end = drm_color_lut_extract(lut[segments[i].end].green, 12);
133 a = (delta_in == 0) ? 0 : ((out_end - out_start) * 256) / delta_in;
135 coeffs[i] = DE_COEFTAB_DATA(a, b);
140 * Check if there is a new gamma LUT and if it is of an acceptable size. Also,
141 * reject any LUTs that use distinct red, green, and blue curves.
143 static int malidp_crtc_atomic_check_gamma(struct drm_crtc *crtc,
144 struct drm_crtc_state *state)
146 struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
147 struct drm_color_lut *lut;
151 if (!state->color_mgmt_changed || !state->gamma_lut)
154 if (crtc->state->gamma_lut &&
155 (crtc->state->gamma_lut->base.id == state->gamma_lut->base.id))
158 if (state->gamma_lut->length % sizeof(struct drm_color_lut))
161 lut_size = state->gamma_lut->length / sizeof(struct drm_color_lut);
162 if (lut_size != MALIDP_GAMMA_LUT_SIZE)
165 lut = (struct drm_color_lut *)state->gamma_lut->data;
166 for (i = 0; i < lut_size; ++i)
167 if (!((lut[i].red == lut[i].green) &&
168 (lut[i].red == lut[i].blue)))
171 if (!state->mode_changed) {
174 state->mode_changed = true;
176 * Kerneldoc for drm_atomic_helper_check_modeset mandates that
177 * it be invoked when the driver sets ->mode_changed. Since
178 * changing the gamma LUT doesn't depend on any external
179 * resources, it is safe to call it only once.
181 ret = drm_atomic_helper_check_modeset(crtc->dev, state->state);
186 malidp_generate_gamma_table(state->gamma_lut, mc->gamma_coeffs);
191 * Check if there is a new CTM and if it contains valid input. Valid here means
192 * that the number is inside the representable range for a Q3.12 number,
193 * excluding truncating the fractional part of the input data.
195 * The COLORADJ registers can be changed atomically.
197 static int malidp_crtc_atomic_check_ctm(struct drm_crtc *crtc,
198 struct drm_crtc_state *state)
200 struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
201 struct drm_color_ctm *ctm;
204 if (!state->color_mgmt_changed)
210 if (crtc->state->ctm && (crtc->state->ctm->base.id ==
211 state->ctm->base.id))
215 * The size of the ctm is checked in
216 * drm_atomic_replace_property_blob_from_id.
218 ctm = (struct drm_color_ctm *)state->ctm->data;
219 for (i = 0; i < ARRAY_SIZE(ctm->matrix); ++i) {
220 /* Convert from S31.32 to Q3.12. */
221 s64 val = ctm->matrix[i];
222 u32 mag = ((((u64)val) & ~BIT_ULL(63)) >> 20) &
226 * Convert to 2s complement and check the destination's top bit
227 * for overflow. NB: Can't check before converting or it'd
228 * incorrectly reject the case:
232 if (val & BIT_ULL(63))
234 if (!!(val & BIT_ULL(63)) != !!(mag & BIT(14)))
236 mc->coloradj_coeffs[i] = mag;
242 static int malidp_crtc_atomic_check_scaling(struct drm_crtc *crtc,
243 struct drm_crtc_state *state)
245 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
246 struct malidp_hw_device *hwdev = malidp->dev;
247 struct malidp_crtc_state *cs = to_malidp_crtc_state(state);
248 struct malidp_se_config *s = &cs->scaler_config;
249 struct drm_plane *plane;
251 const struct drm_plane_state *pstate;
252 u32 h_upscale_factor = 0; /* U16.16 */
253 u32 v_upscale_factor = 0; /* U16.16 */
254 u8 scaling = cs->scaled_planes_mask;
258 s->scale_enable = false;
262 /* The scaling engine can only handle one plane at a time. */
263 if (scaling & (scaling - 1))
266 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
267 struct malidp_plane *mp = to_malidp_plane(plane);
270 if (!(mp->layer->id & scaling))
274 * Convert crtc_[w|h] to U32.32, then divide by U16.16 src_[w|h]
275 * to get the U16.16 result.
277 h_upscale_factor = div_u64((u64)pstate->crtc_w << 32,
279 v_upscale_factor = div_u64((u64)pstate->crtc_h << 32,
282 s->enhancer_enable = ((h_upscale_factor >> 16) >= 2 ||
283 (v_upscale_factor >> 16) >= 2);
285 s->input_w = pstate->src_w >> 16;
286 s->input_h = pstate->src_h >> 16;
287 s->output_w = pstate->crtc_w;
288 s->output_h = pstate->crtc_h;
291 #define SE_SHIFT_N_PHASE 12
292 /* Calculate initial_phase and delta_phase for horizontal. */
295 ((phase << SE_N_PHASE) / s->output_w + 1) / 2;
298 phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
299 s->h_delta_phase = phase / s->output_w;
301 /* Same for vertical. */
304 ((phase << SE_N_PHASE) / s->output_h + 1) / 2;
307 phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
308 s->v_delta_phase = phase / s->output_h;
310 #undef SE_SHIFT_N_PHASE
311 s->plane_src_id = mp->layer->id;
314 s->scale_enable = true;
315 s->hcoeff = malidp_se_select_coeffs(h_upscale_factor);
316 s->vcoeff = malidp_se_select_coeffs(v_upscale_factor);
319 drm_display_mode_to_videomode(&state->adjusted_mode, &vm);
320 ret = hwdev->se_calc_mclk(hwdev, s, &vm);
326 static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
327 struct drm_crtc_state *state)
329 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
330 struct malidp_hw_device *hwdev = malidp->dev;
331 struct drm_plane *plane;
332 const struct drm_plane_state *pstate;
333 u32 rot_mem_free, rot_mem_usable;
334 int rotated_planes = 0;
338 * check if there is enough rotation memory available for planes
339 * that need 90° and 270° rotation. Each plane has set its required
340 * memory size in the ->plane_check() callback, here we only make
341 * sure that the sums are less that the total usable memory.
343 * The rotation memory allocation algorithm (for each plane):
344 * a. If no more rotated planes exist, all remaining rotate
345 * memory in the bank is available for use by the plane.
346 * b. If other rotated planes exist, and plane's layer ID is
347 * DE_VIDEO1, it can use all the memory from first bank if
348 * secondary rotation memory bank is available, otherwise it can
349 * use up to half the bank's memory.
350 * c. If other rotated planes exist, and plane's layer ID is not
351 * DE_VIDEO1, it can use half of the available memory
353 * Note: this algorithm assumes that the order in which the planes are
354 * checked always has DE_VIDEO1 plane first in the list if it is
355 * rotated. Because that is how we create the planes in the first
356 * place, under current DRM version things work, but if ever the order
357 * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
358 * changes, we need to pre-sort the planes before validation.
361 /* first count the number of rotated planes */
362 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
363 if (pstate->rotation & MALIDP_ROTATED_MASK)
367 rot_mem_free = hwdev->rotation_memory[0];
369 * if we have more than 1 plane using rotation memory, use the second
370 * block of rotation memory as well
372 if (rotated_planes > 1)
373 rot_mem_free += hwdev->rotation_memory[1];
375 /* now validate the rotation memory requirements */
376 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
377 struct malidp_plane *mp = to_malidp_plane(plane);
378 struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
380 if (pstate->rotation & MALIDP_ROTATED_MASK) {
381 /* process current plane */
384 if (!rotated_planes) {
385 /* no more rotated planes, we can use what's left */
386 rot_mem_usable = rot_mem_free;
388 if ((mp->layer->id != DE_VIDEO1) ||
389 (hwdev->rotation_memory[1] == 0))
390 rot_mem_usable = rot_mem_free / 2;
392 rot_mem_usable = hwdev->rotation_memory[0];
395 rot_mem_free -= rot_mem_usable;
397 if (ms->rotmem_size > rot_mem_usable)
402 ret = malidp_crtc_atomic_check_gamma(crtc, state);
403 ret = ret ? ret : malidp_crtc_atomic_check_ctm(crtc, state);
404 ret = ret ? ret : malidp_crtc_atomic_check_scaling(crtc, state);
409 static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
410 .mode_valid = malidp_crtc_mode_valid,
411 .enable = malidp_crtc_enable,
412 .disable = malidp_crtc_disable,
413 .atomic_check = malidp_crtc_atomic_check,
416 static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
418 struct malidp_crtc_state *state, *old_state;
420 if (WARN_ON(!crtc->state))
423 old_state = to_malidp_crtc_state(crtc->state);
424 state = kmalloc(sizeof(*state), GFP_KERNEL);
428 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
429 memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
430 sizeof(state->gamma_coeffs));
431 memcpy(state->coloradj_coeffs, old_state->coloradj_coeffs,
432 sizeof(state->coloradj_coeffs));
433 memcpy(&state->scaler_config, &old_state->scaler_config,
434 sizeof(state->scaler_config));
435 state->scaled_planes_mask = 0;
440 static void malidp_crtc_reset(struct drm_crtc *crtc)
442 struct malidp_crtc_state *state = NULL;
445 state = to_malidp_crtc_state(crtc->state);
446 __drm_atomic_helper_crtc_destroy_state(crtc->state);
450 state = kzalloc(sizeof(*state), GFP_KERNEL);
452 crtc->state = &state->base;
453 crtc->state->crtc = crtc;
457 static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
458 struct drm_crtc_state *state)
460 struct malidp_crtc_state *mali_state = NULL;
463 mali_state = to_malidp_crtc_state(state);
464 __drm_atomic_helper_crtc_destroy_state(state);
470 static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
472 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
473 struct malidp_hw_device *hwdev = malidp->dev;
475 malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
476 hwdev->map.de_irq_map.vsync_irq);
480 static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
482 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
483 struct malidp_hw_device *hwdev = malidp->dev;
485 malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
486 hwdev->map.de_irq_map.vsync_irq);
489 static const struct drm_crtc_funcs malidp_crtc_funcs = {
490 .gamma_set = drm_atomic_helper_legacy_gamma_set,
491 .destroy = drm_crtc_cleanup,
492 .set_config = drm_atomic_helper_set_config,
493 .page_flip = drm_atomic_helper_page_flip,
494 .reset = malidp_crtc_reset,
495 .atomic_duplicate_state = malidp_crtc_duplicate_state,
496 .atomic_destroy_state = malidp_crtc_destroy_state,
497 .enable_vblank = malidp_crtc_enable_vblank,
498 .disable_vblank = malidp_crtc_disable_vblank,
501 int malidp_crtc_init(struct drm_device *drm)
503 struct malidp_drm *malidp = drm->dev_private;
504 struct drm_plane *primary = NULL, *plane;
507 ret = malidp_de_planes_init(drm);
509 DRM_ERROR("Failed to initialise planes\n");
513 drm_for_each_plane(plane, drm) {
514 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
521 DRM_ERROR("no primary plane found\n");
523 goto crtc_cleanup_planes;
526 ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
527 &malidp_crtc_funcs, NULL);
529 goto crtc_cleanup_planes;
531 drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
532 drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
533 /* No inverse-gamma: it is per-plane. */
534 drm_crtc_enable_color_mgmt(&malidp->crtc, 0, true, MALIDP_GAMMA_LUT_SIZE);
536 malidp_se_set_enh_coeffs(malidp->dev);
541 malidp_de_planes_destroy(drm);