blob: 27ab09459b9d1cbeff8d6176e16cb765d2fcf48c [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
25static bool malidp_crtc_mode_fixup(struct drm_crtc *crtc,
26 const struct drm_display_mode *mode,
27 struct drm_display_mode *adjusted_mode)
28{
29 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
30 struct malidp_hw_device *hwdev = malidp->dev;
31
32 /*
33 * check that the hardware can drive the required clock rate,
34 * but skip the check if the clock is meant to be disabled (req_rate = 0)
35 */
36 long rate, req_rate = mode->crtc_clock * 1000;
37
38 if (req_rate) {
39 rate = clk_round_rate(hwdev->mclk, req_rate);
40 if (rate < req_rate) {
41 DRM_DEBUG_DRIVER("mclk clock unable to reach %d kHz\n",
42 mode->crtc_clock);
43 return false;
44 }
45
46 rate = clk_round_rate(hwdev->pxlclk, req_rate);
47 if (rate != req_rate) {
48 DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
49 req_rate);
50 return false;
51 }
52 }
53
54 return true;
55}
56
57static void malidp_crtc_enable(struct drm_crtc *crtc)
58{
59 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
60 struct malidp_hw_device *hwdev = malidp->dev;
61 struct videomode vm;
Liviu Dudau85f64212017-03-22 10:44:57 +000062 int err = pm_runtime_get_sync(crtc->dev->dev);
63
64 if (err < 0) {
65 DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
66 return;
67 }
Liviu Dudauad49f862016-03-07 10:00:53 +000068
69 drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
Liviu Dudauad49f862016-03-07 10:00:53 +000070 clk_prepare_enable(hwdev->pxlclk);
71
Mihail Atanassov9a8b0a22017-02-15 14:00:15 +000072 /* We rely on firmware to set mclk to a sensible level. */
Liviu Dudauad49f862016-03-07 10:00:53 +000073 clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
74
75 hwdev->modeset(hwdev, &vm);
76 hwdev->leave_config_mode(hwdev);
77 drm_crtc_vblank_on(crtc);
78}
79
80static void malidp_crtc_disable(struct drm_crtc *crtc)
81{
82 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
83 struct malidp_hw_device *hwdev = malidp->dev;
Liviu Dudau85f64212017-03-22 10:44:57 +000084 int err;
Liviu Dudauad49f862016-03-07 10:00:53 +000085
86 drm_crtc_vblank_off(crtc);
87 hwdev->enter_config_mode(hwdev);
88 clk_disable_unprepare(hwdev->pxlclk);
Liviu Dudau85f64212017-03-22 10:44:57 +000089
90 err = pm_runtime_put(crtc->dev->dev);
91 if (err < 0) {
92 DRM_DEBUG_DRIVER("Failed to disable runtime power management: %d\n", err);
93 }
Liviu Dudauad49f862016-03-07 10:00:53 +000094}
95
Mihail Atanassov02725d32017-02-01 14:48:50 +000096static const struct gamma_curve_segment {
97 u16 start;
98 u16 end;
99} segments[MALIDP_COEFFTAB_NUM_COEFFS] = {
100 /* sector 0 */
101 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 },
102 { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
103 { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 },
104 { 12, 12 }, { 13, 13 }, { 14, 14 }, { 15, 15 },
105 /* sector 1 */
106 { 16, 19 }, { 20, 23 }, { 24, 27 }, { 28, 31 },
107 /* sector 2 */
108 { 32, 39 }, { 40, 47 }, { 48, 55 }, { 56, 63 },
109 /* sector 3 */
110 { 64, 79 }, { 80, 95 }, { 96, 111 }, { 112, 127 },
111 /* sector 4 */
112 { 128, 159 }, { 160, 191 }, { 192, 223 }, { 224, 255 },
113 /* sector 5 */
114 { 256, 319 }, { 320, 383 }, { 384, 447 }, { 448, 511 },
115 /* sector 6 */
116 { 512, 639 }, { 640, 767 }, { 768, 895 }, { 896, 1023 },
117 { 1024, 1151 }, { 1152, 1279 }, { 1280, 1407 }, { 1408, 1535 },
118 { 1536, 1663 }, { 1664, 1791 }, { 1792, 1919 }, { 1920, 2047 },
119 { 2048, 2175 }, { 2176, 2303 }, { 2304, 2431 }, { 2432, 2559 },
120 { 2560, 2687 }, { 2688, 2815 }, { 2816, 2943 }, { 2944, 3071 },
121 { 3072, 3199 }, { 3200, 3327 }, { 3328, 3455 }, { 3456, 3583 },
122 { 3584, 3711 }, { 3712, 3839 }, { 3840, 3967 }, { 3968, 4095 },
123};
124
125#define DE_COEFTAB_DATA(a, b) ((((a) & 0xfff) << 16) | (((b) & 0xfff)))
126
127static void malidp_generate_gamma_table(struct drm_property_blob *lut_blob,
128 u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])
129{
130 struct drm_color_lut *lut = (struct drm_color_lut *)lut_blob->data;
131 int i;
132
133 for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i) {
134 u32 a, b, delta_in, out_start, out_end;
135
136 delta_in = segments[i].end - segments[i].start;
137 /* DP has 12-bit internal precision for its LUTs. */
138 out_start = drm_color_lut_extract(lut[segments[i].start].green,
139 12);
140 out_end = drm_color_lut_extract(lut[segments[i].end].green, 12);
141 a = (delta_in == 0) ? 0 : ((out_end - out_start) * 256) / delta_in;
142 b = out_start;
143 coeffs[i] = DE_COEFTAB_DATA(a, b);
144 }
145}
146
147/*
148 * Check if there is a new gamma LUT and if it is of an acceptable size. Also,
149 * reject any LUTs that use distinct red, green, and blue curves.
150 */
151static int malidp_crtc_atomic_check_gamma(struct drm_crtc *crtc,
152 struct drm_crtc_state *state)
153{
154 struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
155 struct drm_color_lut *lut;
156 size_t lut_size;
157 int i;
158
159 if (!state->color_mgmt_changed || !state->gamma_lut)
160 return 0;
161
162 if (crtc->state->gamma_lut &&
163 (crtc->state->gamma_lut->base.id == state->gamma_lut->base.id))
164 return 0;
165
166 if (state->gamma_lut->length % sizeof(struct drm_color_lut))
167 return -EINVAL;
168
169 lut_size = state->gamma_lut->length / sizeof(struct drm_color_lut);
170 if (lut_size != MALIDP_GAMMA_LUT_SIZE)
171 return -EINVAL;
172
173 lut = (struct drm_color_lut *)state->gamma_lut->data;
174 for (i = 0; i < lut_size; ++i)
175 if (!((lut[i].red == lut[i].green) &&
176 (lut[i].red == lut[i].blue)))
177 return -EINVAL;
178
179 if (!state->mode_changed) {
180 int ret;
181
182 state->mode_changed = true;
183 /*
184 * Kerneldoc for drm_atomic_helper_check_modeset mandates that
185 * it be invoked when the driver sets ->mode_changed. Since
186 * changing the gamma LUT doesn't depend on any external
187 * resources, it is safe to call it only once.
188 */
189 ret = drm_atomic_helper_check_modeset(crtc->dev, state->state);
190 if (ret)
191 return ret;
192 }
193
194 malidp_generate_gamma_table(state->gamma_lut, mc->gamma_coeffs);
195 return 0;
196}
197
Liviu Dudauad49f862016-03-07 10:00:53 +0000198static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
199 struct drm_crtc_state *state)
200{
201 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
202 struct malidp_hw_device *hwdev = malidp->dev;
203 struct drm_plane *plane;
204 const struct drm_plane_state *pstate;
205 u32 rot_mem_free, rot_mem_usable;
206 int rotated_planes = 0;
207
208 /*
209 * check if there is enough rotation memory available for planes
210 * that need 90° and 270° rotation. Each plane has set its required
211 * memory size in the ->plane_check() callback, here we only make
212 * sure that the sums are less that the total usable memory.
213 *
214 * The rotation memory allocation algorithm (for each plane):
215 * a. If no more rotated planes exist, all remaining rotate
216 * memory in the bank is available for use by the plane.
217 * b. If other rotated planes exist, and plane's layer ID is
218 * DE_VIDEO1, it can use all the memory from first bank if
219 * secondary rotation memory bank is available, otherwise it can
220 * use up to half the bank's memory.
221 * c. If other rotated planes exist, and plane's layer ID is not
222 * DE_VIDEO1, it can use half of the available memory
223 *
224 * Note: this algorithm assumes that the order in which the planes are
225 * checked always has DE_VIDEO1 plane first in the list if it is
226 * rotated. Because that is how we create the planes in the first
227 * place, under current DRM version things work, but if ever the order
228 * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
229 * changes, we need to pre-sort the planes before validation.
230 */
231
232 /* first count the number of rotated planes */
233 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
234 if (pstate->rotation & MALIDP_ROTATED_MASK)
235 rotated_planes++;
236 }
237
238 rot_mem_free = hwdev->rotation_memory[0];
239 /*
240 * if we have more than 1 plane using rotation memory, use the second
241 * block of rotation memory as well
242 */
243 if (rotated_planes > 1)
244 rot_mem_free += hwdev->rotation_memory[1];
245
246 /* now validate the rotation memory requirements */
247 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
248 struct malidp_plane *mp = to_malidp_plane(plane);
249 struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
250
251 if (pstate->rotation & MALIDP_ROTATED_MASK) {
252 /* process current plane */
253 rotated_planes--;
254
255 if (!rotated_planes) {
256 /* no more rotated planes, we can use what's left */
257 rot_mem_usable = rot_mem_free;
258 } else {
259 if ((mp->layer->id != DE_VIDEO1) ||
260 (hwdev->rotation_memory[1] == 0))
261 rot_mem_usable = rot_mem_free / 2;
262 else
263 rot_mem_usable = hwdev->rotation_memory[0];
264 }
265
266 rot_mem_free -= rot_mem_usable;
267
268 if (ms->rotmem_size > rot_mem_usable)
269 return -EINVAL;
270 }
271 }
272
Mihail Atanassov02725d32017-02-01 14:48:50 +0000273 return malidp_crtc_atomic_check_gamma(crtc, state);
Liviu Dudauad49f862016-03-07 10:00:53 +0000274}
275
276static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
277 .mode_fixup = malidp_crtc_mode_fixup,
278 .enable = malidp_crtc_enable,
279 .disable = malidp_crtc_disable,
280 .atomic_check = malidp_crtc_atomic_check,
281};
282
Mihail Atanassov99665d02017-02-01 14:48:49 +0000283static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
284{
Mihail Atanassov02725d32017-02-01 14:48:50 +0000285 struct malidp_crtc_state *state, *old_state;
Mihail Atanassov99665d02017-02-01 14:48:49 +0000286
287 if (WARN_ON(!crtc->state))
288 return NULL;
289
Mihail Atanassov02725d32017-02-01 14:48:50 +0000290 old_state = to_malidp_crtc_state(crtc->state);
Mihail Atanassov99665d02017-02-01 14:48:49 +0000291 state = kmalloc(sizeof(*state), GFP_KERNEL);
292 if (!state)
293 return NULL;
294
295 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
Mihail Atanassov02725d32017-02-01 14:48:50 +0000296 memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
297 sizeof(state->gamma_coeffs));
Mihail Atanassov99665d02017-02-01 14:48:49 +0000298
299 return &state->base;
300}
301
302static void malidp_crtc_reset(struct drm_crtc *crtc)
303{
304 struct malidp_crtc_state *state = NULL;
305
306 if (crtc->state) {
307 state = to_malidp_crtc_state(crtc->state);
308 __drm_atomic_helper_crtc_destroy_state(crtc->state);
309 }
310
311 kfree(state);
312 state = kzalloc(sizeof(*state), GFP_KERNEL);
313 if (state) {
314 crtc->state = &state->base;
315 crtc->state->crtc = crtc;
316 }
317}
318
319static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
320 struct drm_crtc_state *state)
321{
322 struct malidp_crtc_state *mali_state = NULL;
323
324 if (state) {
325 mali_state = to_malidp_crtc_state(state);
326 __drm_atomic_helper_crtc_destroy_state(state);
327 }
328
329 kfree(mali_state);
330}
331
Shawn Guod7ae94b2017-02-07 17:16:17 +0800332static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
333{
334 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
335 struct malidp_hw_device *hwdev = malidp->dev;
336
337 malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
338 hwdev->map.de_irq_map.vsync_irq);
339 return 0;
340}
341
342static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
343{
344 struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
345 struct malidp_hw_device *hwdev = malidp->dev;
346
347 malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
348 hwdev->map.de_irq_map.vsync_irq);
349}
350
Liviu Dudauad49f862016-03-07 10:00:53 +0000351static const struct drm_crtc_funcs malidp_crtc_funcs = {
Mihail Atanassov02725d32017-02-01 14:48:50 +0000352 .gamma_set = drm_atomic_helper_legacy_gamma_set,
Liviu Dudauad49f862016-03-07 10:00:53 +0000353 .destroy = drm_crtc_cleanup,
354 .set_config = drm_atomic_helper_set_config,
355 .page_flip = drm_atomic_helper_page_flip,
Mihail Atanassov99665d02017-02-01 14:48:49 +0000356 .reset = malidp_crtc_reset,
357 .atomic_duplicate_state = malidp_crtc_duplicate_state,
358 .atomic_destroy_state = malidp_crtc_destroy_state,
Shawn Guod7ae94b2017-02-07 17:16:17 +0800359 .enable_vblank = malidp_crtc_enable_vblank,
360 .disable_vblank = malidp_crtc_disable_vblank,
Liviu Dudauad49f862016-03-07 10:00:53 +0000361};
362
363int malidp_crtc_init(struct drm_device *drm)
364{
365 struct malidp_drm *malidp = drm->dev_private;
366 struct drm_plane *primary = NULL, *plane;
367 int ret;
368
369 ret = malidp_de_planes_init(drm);
370 if (ret < 0) {
371 DRM_ERROR("Failed to initialise planes\n");
372 return ret;
373 }
374
375 drm_for_each_plane(plane, drm) {
376 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
377 primary = plane;
378 break;
379 }
380 }
381
382 if (!primary) {
383 DRM_ERROR("no primary plane found\n");
384 ret = -EINVAL;
385 goto crtc_cleanup_planes;
386 }
387
388 ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
389 &malidp_crtc_funcs, NULL);
Mihail Atanassov02725d32017-02-01 14:48:50 +0000390 if (ret)
391 goto crtc_cleanup_planes;
Liviu Dudauad49f862016-03-07 10:00:53 +0000392
Mihail Atanassov02725d32017-02-01 14:48:50 +0000393 drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
394 drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
395 /* No inverse-gamma and color adjustments yet. */
396 drm_crtc_enable_color_mgmt(&malidp->crtc, 0, false, MALIDP_GAMMA_LUT_SIZE);
397
398 return 0;
Liviu Dudauad49f862016-03-07 10:00:53 +0000399
400crtc_cleanup_planes:
401 malidp_de_planes_destroy(drm);
402
403 return ret;
404}