blob: 153a4967062628e4116e08c4d1a11b6ff144869f [file] [log] [blame]
Liviu Dudauad49f862016-03-07 10:00:53 +00001/*
2 * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
3 * Author: Liviu Dudau <Liviu.Dudau@arm.com>
4 *
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
8 * of such GNU licence.
9 *
10 * ARM Mali DP500/DP550/DP650 driver (crtc operations)
11 */
12
13#include <drm/drmP.h>
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>
Liviu Dudau85f64212017-03-22 10:44:57 +000019#include <linux/pm_runtime.h>
Liviu Dudauad49f862016-03-07 10:00:53 +000020#include <video/videomode.h>
21
22#include "malidp_drv.h"
23#include "malidp_hw.h"
24
Jose Abreue2113c02017-05-19 01:52:17 +010025static enum drm_mode_status malidp_crtc_mode_valid(struct drm_crtc *crtc,
26 const struct drm_display_mode *mode)
Liviu Dudauad49f862016-03-07 10:00:53 +000027{
28 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
29 struct malidp_hw_device *hwdev = malidp->dev;
30
31 /*
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)
34 */
35 long rate, req_rate = mode->crtc_clock * 1000;
36
37 if (req_rate) {
Liviu Dudauad49f862016-03-07 10:00:53 +000038 rate = clk_round_rate(hwdev->pxlclk, req_rate);
39 if (rate != req_rate) {
40 DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
41 req_rate);
Jose Abreue2113c02017-05-19 01:52:17 +010042 return MODE_NOCLOCK;
Liviu Dudauad49f862016-03-07 10:00:53 +000043 }
44 }
45
Jose Abreue2113c02017-05-19 01:52:17 +010046 return MODE_OK;
Liviu Dudauad49f862016-03-07 10:00:53 +000047}
48
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030049static void malidp_crtc_atomic_enable(struct drm_crtc *crtc,
50 struct drm_crtc_state *old_state)
Liviu Dudauad49f862016-03-07 10:00:53 +000051{
52 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
53 struct malidp_hw_device *hwdev = malidp->dev;
54 struct videomode vm;
Liviu Dudau85f64212017-03-22 10:44:57 +000055 int err = pm_runtime_get_sync(crtc->dev->dev);
56
57 if (err < 0) {
58 DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
59 return;
60 }
Liviu Dudauad49f862016-03-07 10:00:53 +000061
62 drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
Liviu Dudauad49f862016-03-07 10:00:53 +000063 clk_prepare_enable(hwdev->pxlclk);
64
Mihail Atanassov9a8b0a22017-02-15 14:00:15 +000065 /* We rely on firmware to set mclk to a sensible level. */
Liviu Dudauad49f862016-03-07 10:00:53 +000066 clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
67
Liviu Dudaua6993b22017-08-31 15:48:43 +010068 hwdev->hw->modeset(hwdev, &vm);
69 hwdev->hw->leave_config_mode(hwdev);
Liviu Dudauad49f862016-03-07 10:00:53 +000070 drm_crtc_vblank_on(crtc);
71}
72
Laurent Pinchart64581712017-06-30 12:36:45 +030073static void malidp_crtc_atomic_disable(struct drm_crtc *crtc,
74 struct drm_crtc_state *old_state)
Liviu Dudauad49f862016-03-07 10:00:53 +000075{
76 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
77 struct malidp_hw_device *hwdev = malidp->dev;
Liviu Dudau85f64212017-03-22 10:44:57 +000078 int err;
Liviu Dudauad49f862016-03-07 10:00:53 +000079
80 drm_crtc_vblank_off(crtc);
Liviu Dudaua6993b22017-08-31 15:48:43 +010081 hwdev->hw->enter_config_mode(hwdev);
82
Liviu Dudauad49f862016-03-07 10:00:53 +000083 clk_disable_unprepare(hwdev->pxlclk);
Liviu Dudau85f64212017-03-22 10:44:57 +000084
85 err = pm_runtime_put(crtc->dev->dev);
86 if (err < 0) {
87 DRM_DEBUG_DRIVER("Failed to disable runtime power management: %d\n", err);
88 }
Liviu Dudauad49f862016-03-07 10:00:53 +000089}
90
Mihail Atanassov02725d32017-02-01 14:48:50 +000091static const struct gamma_curve_segment {
92 u16 start;
93 u16 end;
94} segments[MALIDP_COEFFTAB_NUM_COEFFS] = {
95 /* sector 0 */
96 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 },
97 { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
98 { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 },
99 { 12, 12 }, { 13, 13 }, { 14, 14 }, { 15, 15 },
100 /* sector 1 */
101 { 16, 19 }, { 20, 23 }, { 24, 27 }, { 28, 31 },
102 /* sector 2 */
103 { 32, 39 }, { 40, 47 }, { 48, 55 }, { 56, 63 },
104 /* sector 3 */
105 { 64, 79 }, { 80, 95 }, { 96, 111 }, { 112, 127 },
106 /* sector 4 */
107 { 128, 159 }, { 160, 191 }, { 192, 223 }, { 224, 255 },
108 /* sector 5 */
109 { 256, 319 }, { 320, 383 }, { 384, 447 }, { 448, 511 },
110 /* sector 6 */
111 { 512, 639 }, { 640, 767 }, { 768, 895 }, { 896, 1023 },
112 { 1024, 1151 }, { 1152, 1279 }, { 1280, 1407 }, { 1408, 1535 },
113 { 1536, 1663 }, { 1664, 1791 }, { 1792, 1919 }, { 1920, 2047 },
114 { 2048, 2175 }, { 2176, 2303 }, { 2304, 2431 }, { 2432, 2559 },
115 { 2560, 2687 }, { 2688, 2815 }, { 2816, 2943 }, { 2944, 3071 },
116 { 3072, 3199 }, { 3200, 3327 }, { 3328, 3455 }, { 3456, 3583 },
117 { 3584, 3711 }, { 3712, 3839 }, { 3840, 3967 }, { 3968, 4095 },
118};
119
120#define DE_COEFTAB_DATA(a, b) ((((a) & 0xfff) << 16) | (((b) & 0xfff)))
121
122static void malidp_generate_gamma_table(struct drm_property_blob *lut_blob,
123 u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])
124{
125 struct drm_color_lut *lut = (struct drm_color_lut *)lut_blob->data;
126 int i;
127
128 for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i) {
129 u32 a, b, delta_in, out_start, out_end;
130
131 delta_in = segments[i].end - segments[i].start;
132 /* DP has 12-bit internal precision for its LUTs. */
133 out_start = drm_color_lut_extract(lut[segments[i].start].green,
134 12);
135 out_end = drm_color_lut_extract(lut[segments[i].end].green, 12);
136 a = (delta_in == 0) ? 0 : ((out_end - out_start) * 256) / delta_in;
137 b = out_start;
138 coeffs[i] = DE_COEFTAB_DATA(a, b);
139 }
140}
141
142/*
143 * Check if there is a new gamma LUT and if it is of an acceptable size. Also,
144 * reject any LUTs that use distinct red, green, and blue curves.
145 */
146static int malidp_crtc_atomic_check_gamma(struct drm_crtc *crtc,
147 struct drm_crtc_state *state)
148{
149 struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
150 struct drm_color_lut *lut;
151 size_t lut_size;
152 int i;
153
154 if (!state->color_mgmt_changed || !state->gamma_lut)
155 return 0;
156
157 if (crtc->state->gamma_lut &&
158 (crtc->state->gamma_lut->base.id == state->gamma_lut->base.id))
159 return 0;
160
161 if (state->gamma_lut->length % sizeof(struct drm_color_lut))
162 return -EINVAL;
163
164 lut_size = state->gamma_lut->length / sizeof(struct drm_color_lut);
165 if (lut_size != MALIDP_GAMMA_LUT_SIZE)
166 return -EINVAL;
167
168 lut = (struct drm_color_lut *)state->gamma_lut->data;
169 for (i = 0; i < lut_size; ++i)
170 if (!((lut[i].red == lut[i].green) &&
171 (lut[i].red == lut[i].blue)))
172 return -EINVAL;
173
174 if (!state->mode_changed) {
175 int ret;
176
177 state->mode_changed = true;
178 /*
179 * Kerneldoc for drm_atomic_helper_check_modeset mandates that
180 * it be invoked when the driver sets ->mode_changed. Since
181 * changing the gamma LUT doesn't depend on any external
182 * resources, it is safe to call it only once.
183 */
184 ret = drm_atomic_helper_check_modeset(crtc->dev, state->state);
185 if (ret)
186 return ret;
187 }
188
189 malidp_generate_gamma_table(state->gamma_lut, mc->gamma_coeffs);
190 return 0;
191}
192
Mihail Atanassov6954f242017-02-13 12:49:03 +0000193/*
194 * Check if there is a new CTM and if it contains valid input. Valid here means
195 * that the number is inside the representable range for a Q3.12 number,
196 * excluding truncating the fractional part of the input data.
197 *
198 * The COLORADJ registers can be changed atomically.
199 */
200static int malidp_crtc_atomic_check_ctm(struct drm_crtc *crtc,
201 struct drm_crtc_state *state)
202{
203 struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
204 struct drm_color_ctm *ctm;
205 int i;
206
207 if (!state->color_mgmt_changed)
208 return 0;
209
210 if (!state->ctm)
211 return 0;
212
213 if (crtc->state->ctm && (crtc->state->ctm->base.id ==
214 state->ctm->base.id))
215 return 0;
216
217 /*
218 * The size of the ctm is checked in
219 * drm_atomic_replace_property_blob_from_id.
220 */
221 ctm = (struct drm_color_ctm *)state->ctm->data;
222 for (i = 0; i < ARRAY_SIZE(ctm->matrix); ++i) {
223 /* Convert from S31.32 to Q3.12. */
224 s64 val = ctm->matrix[i];
225 u32 mag = ((((u64)val) & ~BIT_ULL(63)) >> 20) &
226 GENMASK_ULL(14, 0);
227
228 /*
229 * Convert to 2s complement and check the destination's top bit
230 * for overflow. NB: Can't check before converting or it'd
231 * incorrectly reject the case:
232 * sign == 1
233 * mag == 0x2000
234 */
235 if (val & BIT_ULL(63))
236 mag = ~mag + 1;
237 if (!!(val & BIT_ULL(63)) != !!(mag & BIT(14)))
238 return -EINVAL;
239 mc->coloradj_coeffs[i] = mag;
240 }
241
242 return 0;
243}
244
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000245static int malidp_crtc_atomic_check_scaling(struct drm_crtc *crtc,
246 struct drm_crtc_state *state)
247{
Mihail Atanassovc2e7f822017-02-13 15:09:01 +0000248 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
249 struct malidp_hw_device *hwdev = malidp->dev;
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000250 struct malidp_crtc_state *cs = to_malidp_crtc_state(state);
251 struct malidp_se_config *s = &cs->scaler_config;
252 struct drm_plane *plane;
Mihail Atanassovc2e7f822017-02-13 15:09:01 +0000253 struct videomode vm;
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000254 const struct drm_plane_state *pstate;
255 u32 h_upscale_factor = 0; /* U16.16 */
256 u32 v_upscale_factor = 0; /* U16.16 */
257 u8 scaling = cs->scaled_planes_mask;
Mihail Atanassovc2e7f822017-02-13 15:09:01 +0000258 int ret;
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000259
260 if (!scaling) {
261 s->scale_enable = false;
Mihail Atanassovc2e7f822017-02-13 15:09:01 +0000262 goto mclk_calc;
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000263 }
264
265 /* The scaling engine can only handle one plane at a time. */
266 if (scaling & (scaling - 1))
267 return -EINVAL;
268
269 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
270 struct malidp_plane *mp = to_malidp_plane(plane);
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000271 u32 phase;
272
273 if (!(mp->layer->id & scaling))
274 continue;
275
276 /*
277 * Convert crtc_[w|h] to U32.32, then divide by U16.16 src_[w|h]
278 * to get the U16.16 result.
279 */
Arnd Bergmann763656d2017-04-25 21:56:53 +0200280 h_upscale_factor = div_u64((u64)pstate->crtc_w << 32,
281 pstate->src_w);
282 v_upscale_factor = div_u64((u64)pstate->crtc_h << 32,
283 pstate->src_h);
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000284
Mihail Atanassov0274e6a2017-02-06 12:20:56 +0000285 s->enhancer_enable = ((h_upscale_factor >> 16) >= 2 ||
286 (v_upscale_factor >> 16) >= 2);
287
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000288 s->input_w = pstate->src_w >> 16;
289 s->input_h = pstate->src_h >> 16;
290 s->output_w = pstate->crtc_w;
291 s->output_h = pstate->crtc_h;
292
293#define SE_N_PHASE 4
294#define SE_SHIFT_N_PHASE 12
295 /* Calculate initial_phase and delta_phase for horizontal. */
296 phase = s->input_w;
297 s->h_init_phase =
298 ((phase << SE_N_PHASE) / s->output_w + 1) / 2;
299
300 phase = s->input_w;
301 phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
302 s->h_delta_phase = phase / s->output_w;
303
304 /* Same for vertical. */
305 phase = s->input_h;
306 s->v_init_phase =
307 ((phase << SE_N_PHASE) / s->output_h + 1) / 2;
308
309 phase = s->input_h;
310 phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
311 s->v_delta_phase = phase / s->output_h;
312#undef SE_N_PHASE
313#undef SE_SHIFT_N_PHASE
314 s->plane_src_id = mp->layer->id;
315 }
316
317 s->scale_enable = true;
318 s->hcoeff = malidp_se_select_coeffs(h_upscale_factor);
319 s->vcoeff = malidp_se_select_coeffs(v_upscale_factor);
Mihail Atanassovc2e7f822017-02-13 15:09:01 +0000320
321mclk_calc:
322 drm_display_mode_to_videomode(&state->adjusted_mode, &vm);
Liviu Dudaua6993b22017-08-31 15:48:43 +0100323 ret = hwdev->hw->se_calc_mclk(hwdev, s, &vm);
Mihail Atanassovc2e7f822017-02-13 15:09:01 +0000324 if (ret < 0)
325 return -EINVAL;
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000326 return 0;
327}
328
Liviu Dudauad49f862016-03-07 10:00:53 +0000329static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
330 struct drm_crtc_state *state)
331{
332 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
333 struct malidp_hw_device *hwdev = malidp->dev;
334 struct drm_plane *plane;
335 const struct drm_plane_state *pstate;
336 u32 rot_mem_free, rot_mem_usable;
337 int rotated_planes = 0;
Mihail Atanassov6954f242017-02-13 12:49:03 +0000338 int ret;
Liviu Dudauad49f862016-03-07 10:00:53 +0000339
340 /*
341 * check if there is enough rotation memory available for planes
342 * that need 90° and 270° rotation. Each plane has set its required
343 * memory size in the ->plane_check() callback, here we only make
344 * sure that the sums are less that the total usable memory.
345 *
346 * The rotation memory allocation algorithm (for each plane):
347 * a. If no more rotated planes exist, all remaining rotate
348 * memory in the bank is available for use by the plane.
349 * b. If other rotated planes exist, and plane's layer ID is
350 * DE_VIDEO1, it can use all the memory from first bank if
351 * secondary rotation memory bank is available, otherwise it can
352 * use up to half the bank's memory.
353 * c. If other rotated planes exist, and plane's layer ID is not
354 * DE_VIDEO1, it can use half of the available memory
355 *
356 * Note: this algorithm assumes that the order in which the planes are
357 * checked always has DE_VIDEO1 plane first in the list if it is
358 * rotated. Because that is how we create the planes in the first
359 * place, under current DRM version things work, but if ever the order
360 * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
361 * changes, we need to pre-sort the planes before validation.
362 */
363
364 /* first count the number of rotated planes */
365 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
366 if (pstate->rotation & MALIDP_ROTATED_MASK)
367 rotated_planes++;
368 }
369
370 rot_mem_free = hwdev->rotation_memory[0];
371 /*
372 * if we have more than 1 plane using rotation memory, use the second
373 * block of rotation memory as well
374 */
375 if (rotated_planes > 1)
376 rot_mem_free += hwdev->rotation_memory[1];
377
378 /* now validate the rotation memory requirements */
379 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
380 struct malidp_plane *mp = to_malidp_plane(plane);
381 struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
382
383 if (pstate->rotation & MALIDP_ROTATED_MASK) {
384 /* process current plane */
385 rotated_planes--;
386
387 if (!rotated_planes) {
388 /* no more rotated planes, we can use what's left */
389 rot_mem_usable = rot_mem_free;
390 } else {
391 if ((mp->layer->id != DE_VIDEO1) ||
392 (hwdev->rotation_memory[1] == 0))
393 rot_mem_usable = rot_mem_free / 2;
394 else
395 rot_mem_usable = hwdev->rotation_memory[0];
396 }
397
398 rot_mem_free -= rot_mem_usable;
399
400 if (ms->rotmem_size > rot_mem_usable)
401 return -EINVAL;
402 }
403 }
404
Mihail Atanassov6954f242017-02-13 12:49:03 +0000405 ret = malidp_crtc_atomic_check_gamma(crtc, state);
406 ret = ret ? ret : malidp_crtc_atomic_check_ctm(crtc, state);
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000407 ret = ret ? ret : malidp_crtc_atomic_check_scaling(crtc, state);
Mihail Atanassov6954f242017-02-13 12:49:03 +0000408
409 return ret;
Liviu Dudauad49f862016-03-07 10:00:53 +0000410}
411
412static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
Jose Abreue2113c02017-05-19 01:52:17 +0100413 .mode_valid = malidp_crtc_mode_valid,
Liviu Dudauad49f862016-03-07 10:00:53 +0000414 .atomic_check = malidp_crtc_atomic_check,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300415 .atomic_enable = malidp_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300416 .atomic_disable = malidp_crtc_atomic_disable,
Liviu Dudauad49f862016-03-07 10:00:53 +0000417};
418
Mihail Atanassov99665d02017-02-01 14:48:49 +0000419static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
420{
Mihail Atanassov02725d32017-02-01 14:48:50 +0000421 struct malidp_crtc_state *state, *old_state;
Mihail Atanassov99665d02017-02-01 14:48:49 +0000422
423 if (WARN_ON(!crtc->state))
424 return NULL;
425
Mihail Atanassov02725d32017-02-01 14:48:50 +0000426 old_state = to_malidp_crtc_state(crtc->state);
Mihail Atanassov99665d02017-02-01 14:48:49 +0000427 state = kmalloc(sizeof(*state), GFP_KERNEL);
428 if (!state)
429 return NULL;
430
431 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
Mihail Atanassov02725d32017-02-01 14:48:50 +0000432 memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
433 sizeof(state->gamma_coeffs));
Mihail Atanassov6954f242017-02-13 12:49:03 +0000434 memcpy(state->coloradj_coeffs, old_state->coloradj_coeffs,
435 sizeof(state->coloradj_coeffs));
Mihail Atanassov28ce6752017-02-13 15:14:05 +0000436 memcpy(&state->scaler_config, &old_state->scaler_config,
437 sizeof(state->scaler_config));
438 state->scaled_planes_mask = 0;
Mihail Atanassov99665d02017-02-01 14:48:49 +0000439
440 return &state->base;
441}
442
443static void malidp_crtc_reset(struct drm_crtc *crtc)
444{
445 struct malidp_crtc_state *state = NULL;
446
447 if (crtc->state) {
448 state = to_malidp_crtc_state(crtc->state);
449 __drm_atomic_helper_crtc_destroy_state(crtc->state);
450 }
451
452 kfree(state);
453 state = kzalloc(sizeof(*state), GFP_KERNEL);
454 if (state) {
455 crtc->state = &state->base;
456 crtc->state->crtc = crtc;
457 }
458}
459
460static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
461 struct drm_crtc_state *state)
462{
463 struct malidp_crtc_state *mali_state = NULL;
464
465 if (state) {
466 mali_state = to_malidp_crtc_state(state);
467 __drm_atomic_helper_crtc_destroy_state(state);
468 }
469
470 kfree(mali_state);
471}
472
Shawn Guod7ae94b2017-02-07 17:16:17 +0800473static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
474{
475 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
476 struct malidp_hw_device *hwdev = malidp->dev;
477
478 malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
Liviu Dudaua6993b22017-08-31 15:48:43 +0100479 hwdev->hw->map.de_irq_map.vsync_irq);
Shawn Guod7ae94b2017-02-07 17:16:17 +0800480 return 0;
481}
482
483static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
484{
485 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
486 struct malidp_hw_device *hwdev = malidp->dev;
487
488 malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
Liviu Dudaua6993b22017-08-31 15:48:43 +0100489 hwdev->hw->map.de_irq_map.vsync_irq);
Shawn Guod7ae94b2017-02-07 17:16:17 +0800490}
491
Liviu Dudauad49f862016-03-07 10:00:53 +0000492static const struct drm_crtc_funcs malidp_crtc_funcs = {
Mihail Atanassov02725d32017-02-01 14:48:50 +0000493 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Liviu Dudauad49f862016-03-07 10:00:53 +0000494 .destroy = drm_crtc_cleanup,
495 .set_config = drm_atomic_helper_set_config,
496 .page_flip = drm_atomic_helper_page_flip,
Mihail Atanassov99665d02017-02-01 14:48:49 +0000497 .reset = malidp_crtc_reset,
498 .atomic_duplicate_state = malidp_crtc_duplicate_state,
499 .atomic_destroy_state = malidp_crtc_destroy_state,
Shawn Guod7ae94b2017-02-07 17:16:17 +0800500 .enable_vblank = malidp_crtc_enable_vblank,
501 .disable_vblank = malidp_crtc_disable_vblank,
Liviu Dudauad49f862016-03-07 10:00:53 +0000502};
503
504int malidp_crtc_init(struct drm_device *drm)
505{
506 struct malidp_drm *malidp = drm->dev_private;
507 struct drm_plane *primary = NULL, *plane;
508 int ret;
509
510 ret = malidp_de_planes_init(drm);
511 if (ret < 0) {
512 DRM_ERROR("Failed to initialise planes\n");
513 return ret;
514 }
515
516 drm_for_each_plane(plane, drm) {
517 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
518 primary = plane;
519 break;
520 }
521 }
522
523 if (!primary) {
524 DRM_ERROR("no primary plane found\n");
525 ret = -EINVAL;
526 goto crtc_cleanup_planes;
527 }
528
529 ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
530 &malidp_crtc_funcs, NULL);
Mihail Atanassov02725d32017-02-01 14:48:50 +0000531 if (ret)
532 goto crtc_cleanup_planes;
Liviu Dudauad49f862016-03-07 10:00:53 +0000533
Mihail Atanassov02725d32017-02-01 14:48:50 +0000534 drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
535 drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
Mihail Atanassov0274e6a2017-02-06 12:20:56 +0000536 /* No inverse-gamma: it is per-plane. */
Mihail Atanassov6954f242017-02-13 12:49:03 +0000537 drm_crtc_enable_color_mgmt(&malidp->crtc, 0, true, MALIDP_GAMMA_LUT_SIZE);
Mihail Atanassov02725d32017-02-01 14:48:50 +0000538
Mihail Atanassov0274e6a2017-02-06 12:20:56 +0000539 malidp_se_set_enh_coeffs(malidp->dev);
540
Mihail Atanassov02725d32017-02-01 14:48:50 +0000541 return 0;
Liviu Dudauad49f862016-03-07 10:00:53 +0000542
543crtc_cleanup_planes:
544 malidp_de_planes_destroy(drm);
545
546 return ret;
547}