xref: /openbmc/linux/drivers/gpu/drm/arm/malidp_drv.c (revision 752beb5e)
1 /*
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 KMS/DRM driver
11  */
12 
13 #include <linux/module.h>
14 #include <linux/clk.h>
15 #include <linux/component.h>
16 #include <linux/of_device.h>
17 #include <linux/of_graph.h>
18 #include <linux/of_reserved_mem.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/debugfs.h>
21 
22 #include <drm/drmP.h>
23 #include <drm/drm_atomic.h>
24 #include <drm/drm_atomic_helper.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_fb_helper.h>
28 #include <drm/drm_fb_cma_helper.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_modeset_helper.h>
32 #include <drm/drm_of.h>
33 
34 #include "malidp_drv.h"
35 #include "malidp_mw.h"
36 #include "malidp_regs.h"
37 #include "malidp_hw.h"
38 
39 #define MALIDP_CONF_VALID_TIMEOUT	250
40 #define AFBC_HEADER_SIZE		16
41 #define AFBC_SUPERBLK_ALIGNMENT		128
42 
43 static void malidp_write_gamma_table(struct malidp_hw_device *hwdev,
44 				     u32 data[MALIDP_COEFFTAB_NUM_COEFFS])
45 {
46 	int i;
47 	/* Update all channels with a single gamma curve. */
48 	const u32 gamma_write_mask = GENMASK(18, 16);
49 	/*
50 	 * Always write an entire table, so the address field in
51 	 * DE_COEFFTAB_ADDR is 0 and we can use the gamma_write_mask bitmask
52 	 * directly.
53 	 */
54 	malidp_hw_write(hwdev, gamma_write_mask,
55 			hwdev->hw->map.coeffs_base + MALIDP_COEF_TABLE_ADDR);
56 	for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i)
57 		malidp_hw_write(hwdev, data[i],
58 				hwdev->hw->map.coeffs_base +
59 				MALIDP_COEF_TABLE_DATA);
60 }
61 
62 static void malidp_atomic_commit_update_gamma(struct drm_crtc *crtc,
63 					      struct drm_crtc_state *old_state)
64 {
65 	struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
66 	struct malidp_hw_device *hwdev = malidp->dev;
67 
68 	if (!crtc->state->color_mgmt_changed)
69 		return;
70 
71 	if (!crtc->state->gamma_lut) {
72 		malidp_hw_clearbits(hwdev,
73 				    MALIDP_DISP_FUNC_GAMMA,
74 				    MALIDP_DE_DISPLAY_FUNC);
75 	} else {
76 		struct malidp_crtc_state *mc =
77 			to_malidp_crtc_state(crtc->state);
78 
79 		if (!old_state->gamma_lut || (crtc->state->gamma_lut->base.id !=
80 					      old_state->gamma_lut->base.id))
81 			malidp_write_gamma_table(hwdev, mc->gamma_coeffs);
82 
83 		malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_GAMMA,
84 				  MALIDP_DE_DISPLAY_FUNC);
85 	}
86 }
87 
88 static
89 void malidp_atomic_commit_update_coloradj(struct drm_crtc *crtc,
90 					  struct drm_crtc_state *old_state)
91 {
92 	struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
93 	struct malidp_hw_device *hwdev = malidp->dev;
94 	int i;
95 
96 	if (!crtc->state->color_mgmt_changed)
97 		return;
98 
99 	if (!crtc->state->ctm) {
100 		malidp_hw_clearbits(hwdev, MALIDP_DISP_FUNC_CADJ,
101 				    MALIDP_DE_DISPLAY_FUNC);
102 	} else {
103 		struct malidp_crtc_state *mc =
104 			to_malidp_crtc_state(crtc->state);
105 
106 		if (!old_state->ctm || (crtc->state->ctm->base.id !=
107 					old_state->ctm->base.id))
108 			for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; ++i)
109 				malidp_hw_write(hwdev,
110 						mc->coloradj_coeffs[i],
111 						hwdev->hw->map.coeffs_base +
112 						MALIDP_COLOR_ADJ_COEF + 4 * i);
113 
114 		malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_CADJ,
115 				  MALIDP_DE_DISPLAY_FUNC);
116 	}
117 }
118 
119 static void malidp_atomic_commit_se_config(struct drm_crtc *crtc,
120 					   struct drm_crtc_state *old_state)
121 {
122 	struct malidp_crtc_state *cs = to_malidp_crtc_state(crtc->state);
123 	struct malidp_crtc_state *old_cs = to_malidp_crtc_state(old_state);
124 	struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
125 	struct malidp_hw_device *hwdev = malidp->dev;
126 	struct malidp_se_config *s = &cs->scaler_config;
127 	struct malidp_se_config *old_s = &old_cs->scaler_config;
128 	u32 se_control = hwdev->hw->map.se_base +
129 			 ((hwdev->hw->map.features & MALIDP_REGMAP_HAS_CLEARIRQ) ?
130 			 0x10 : 0xC);
131 	u32 layer_control = se_control + MALIDP_SE_LAYER_CONTROL;
132 	u32 scr = se_control + MALIDP_SE_SCALING_CONTROL;
133 	u32 val;
134 
135 	/* Set SE_CONTROL */
136 	if (!s->scale_enable) {
137 		val = malidp_hw_read(hwdev, se_control);
138 		val &= ~MALIDP_SE_SCALING_EN;
139 		malidp_hw_write(hwdev, val, se_control);
140 		return;
141 	}
142 
143 	hwdev->hw->se_set_scaling_coeffs(hwdev, s, old_s);
144 	val = malidp_hw_read(hwdev, se_control);
145 	val |= MALIDP_SE_SCALING_EN | MALIDP_SE_ALPHA_EN;
146 
147 	val &= ~MALIDP_SE_ENH(MALIDP_SE_ENH_MASK);
148 	val |= s->enhancer_enable ? MALIDP_SE_ENH(3) : 0;
149 
150 	val |= MALIDP_SE_RGBO_IF_EN;
151 	malidp_hw_write(hwdev, val, se_control);
152 
153 	/* Set IN_SIZE & OUT_SIZE. */
154 	val = MALIDP_SE_SET_V_SIZE(s->input_h) |
155 	      MALIDP_SE_SET_H_SIZE(s->input_w);
156 	malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_IN_SIZE);
157 	val = MALIDP_SE_SET_V_SIZE(s->output_h) |
158 	      MALIDP_SE_SET_H_SIZE(s->output_w);
159 	malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_OUT_SIZE);
160 
161 	/* Set phase regs. */
162 	malidp_hw_write(hwdev, s->h_init_phase, scr + MALIDP_SE_H_INIT_PH);
163 	malidp_hw_write(hwdev, s->h_delta_phase, scr + MALIDP_SE_H_DELTA_PH);
164 	malidp_hw_write(hwdev, s->v_init_phase, scr + MALIDP_SE_V_INIT_PH);
165 	malidp_hw_write(hwdev, s->v_delta_phase, scr + MALIDP_SE_V_DELTA_PH);
166 }
167 
168 /*
169  * set the "config valid" bit and wait until the hardware acts on it
170  */
171 static int malidp_set_and_wait_config_valid(struct drm_device *drm)
172 {
173 	struct malidp_drm *malidp = drm->dev_private;
174 	struct malidp_hw_device *hwdev = malidp->dev;
175 	int ret;
176 
177 	hwdev->hw->set_config_valid(hwdev, 1);
178 	/* don't wait for config_valid flag if we are in config mode */
179 	if (hwdev->hw->in_config_mode(hwdev)) {
180 		atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_DONE);
181 		return 0;
182 	}
183 
184 	ret = wait_event_interruptible_timeout(malidp->wq,
185 			atomic_read(&malidp->config_valid) == MALIDP_CONFIG_VALID_DONE,
186 			msecs_to_jiffies(MALIDP_CONF_VALID_TIMEOUT));
187 
188 	return (ret > 0) ? 0 : -ETIMEDOUT;
189 }
190 
191 static void malidp_atomic_commit_hw_done(struct drm_atomic_state *state)
192 {
193 	struct drm_device *drm = state->dev;
194 	struct malidp_drm *malidp = drm->dev_private;
195 
196 	malidp->event = malidp->crtc.state->event;
197 	malidp->crtc.state->event = NULL;
198 
199 	if (malidp->crtc.state->active) {
200 		/*
201 		 * if we have an event to deliver to userspace, make sure
202 		 * the vblank is enabled as we are sending it from the IRQ
203 		 * handler.
204 		 */
205 		if (malidp->event)
206 			drm_crtc_vblank_get(&malidp->crtc);
207 
208 		/* only set config_valid if the CRTC is enabled */
209 		if (malidp_set_and_wait_config_valid(drm) < 0)
210 			DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
211 	} else if (malidp->event) {
212 		/* CRTC inactive means vblank IRQ is disabled, send event directly */
213 		spin_lock_irq(&drm->event_lock);
214 		drm_crtc_send_vblank_event(&malidp->crtc, malidp->event);
215 		malidp->event = NULL;
216 		spin_unlock_irq(&drm->event_lock);
217 	}
218 	drm_atomic_helper_commit_hw_done(state);
219 }
220 
221 static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
222 {
223 	struct drm_device *drm = state->dev;
224 	struct malidp_drm *malidp = drm->dev_private;
225 	struct drm_crtc *crtc;
226 	struct drm_crtc_state *old_crtc_state;
227 	int i;
228 
229 	pm_runtime_get_sync(drm->dev);
230 
231 	/*
232 	 * set config_valid to a special value to let IRQ handlers
233 	 * know that we are updating registers
234 	 */
235 	atomic_set(&malidp->config_valid, MALIDP_CONFIG_START);
236 	malidp->dev->hw->set_config_valid(malidp->dev, 0);
237 
238 	drm_atomic_helper_commit_modeset_disables(drm, state);
239 
240 	for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
241 		malidp_atomic_commit_update_gamma(crtc, old_crtc_state);
242 		malidp_atomic_commit_update_coloradj(crtc, old_crtc_state);
243 		malidp_atomic_commit_se_config(crtc, old_crtc_state);
244 	}
245 
246 	drm_atomic_helper_commit_planes(drm, state, DRM_PLANE_COMMIT_ACTIVE_ONLY);
247 
248 	malidp_mw_atomic_commit(drm, state);
249 
250 	drm_atomic_helper_commit_modeset_enables(drm, state);
251 
252 	malidp_atomic_commit_hw_done(state);
253 
254 	pm_runtime_put(drm->dev);
255 
256 	drm_atomic_helper_cleanup_planes(drm, state);
257 }
258 
259 static const struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
260 	.atomic_commit_tail = malidp_atomic_commit_tail,
261 };
262 
263 static bool
264 malidp_verify_afbc_framebuffer_caps(struct drm_device *dev,
265 				    const struct drm_mode_fb_cmd2 *mode_cmd)
266 {
267 	if (malidp_format_mod_supported(dev, mode_cmd->pixel_format,
268 					mode_cmd->modifier[0]) == false)
269 		return false;
270 
271 	if (mode_cmd->offsets[0] != 0) {
272 		DRM_DEBUG_KMS("AFBC buffers' plane offset should be 0\n");
273 		return false;
274 	}
275 
276 	switch (mode_cmd->modifier[0] & AFBC_SIZE_MASK) {
277 	case AFBC_SIZE_16X16:
278 		if ((mode_cmd->width % 16) || (mode_cmd->height % 16)) {
279 			DRM_DEBUG_KMS("AFBC buffers must be aligned to 16 pixels\n");
280 			return false;
281 		}
282 		break;
283 	default:
284 		DRM_DEBUG_KMS("Unsupported AFBC block size\n");
285 		return false;
286 	}
287 
288 	return true;
289 }
290 
291 static bool
292 malidp_verify_afbc_framebuffer_size(struct drm_device *dev,
293 				    struct drm_file *file,
294 				    const struct drm_mode_fb_cmd2 *mode_cmd)
295 {
296 	int n_superblocks = 0;
297 	const struct drm_format_info *info;
298 	struct drm_gem_object *objs = NULL;
299 	u32 afbc_superblock_size = 0, afbc_superblock_height = 0;
300 	u32 afbc_superblock_width = 0, afbc_size = 0;
301 	int bpp = 0;
302 
303 	switch (mode_cmd->modifier[0] & AFBC_SIZE_MASK) {
304 	case AFBC_SIZE_16X16:
305 		afbc_superblock_height = 16;
306 		afbc_superblock_width = 16;
307 		break;
308 	default:
309 		DRM_DEBUG_KMS("AFBC superblock size is not supported\n");
310 		return false;
311 	}
312 
313 	info = drm_get_format_info(dev, mode_cmd);
314 
315 	n_superblocks = (mode_cmd->width / afbc_superblock_width) *
316 		(mode_cmd->height / afbc_superblock_height);
317 
318 	bpp = malidp_format_get_bpp(info->format);
319 
320 	afbc_superblock_size = (bpp * afbc_superblock_width * afbc_superblock_height)
321 				/ BITS_PER_BYTE;
322 
323 	afbc_size = ALIGN(n_superblocks * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
324 	afbc_size += n_superblocks * ALIGN(afbc_superblock_size, AFBC_SUPERBLK_ALIGNMENT);
325 
326 	if ((mode_cmd->width * bpp) != (mode_cmd->pitches[0] * BITS_PER_BYTE)) {
327 		DRM_DEBUG_KMS("Invalid value of (pitch * BITS_PER_BYTE) (=%u) "
328 			      "should be same as width (=%u) * bpp (=%u)\n",
329 			      (mode_cmd->pitches[0] * BITS_PER_BYTE),
330 			      mode_cmd->width, bpp);
331 		return false;
332 	}
333 
334 	objs = drm_gem_object_lookup(file, mode_cmd->handles[0]);
335 	if (!objs) {
336 		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
337 		return false;
338 	}
339 
340 	if (objs->size < afbc_size) {
341 		DRM_DEBUG_KMS("buffer size (%zu) too small for AFBC buffer size = %u\n",
342 			      objs->size, afbc_size);
343 		drm_gem_object_put_unlocked(objs);
344 		return false;
345 	}
346 
347 	drm_gem_object_put_unlocked(objs);
348 
349 	return true;
350 }
351 
352 static bool
353 malidp_verify_afbc_framebuffer(struct drm_device *dev, struct drm_file *file,
354 			       const struct drm_mode_fb_cmd2 *mode_cmd)
355 {
356 	if (malidp_verify_afbc_framebuffer_caps(dev, mode_cmd))
357 		return malidp_verify_afbc_framebuffer_size(dev, file, mode_cmd);
358 
359 	return false;
360 }
361 
362 struct drm_framebuffer *
363 malidp_fb_create(struct drm_device *dev, struct drm_file *file,
364 		 const struct drm_mode_fb_cmd2 *mode_cmd)
365 {
366 	if (mode_cmd->modifier[0]) {
367 		if (!malidp_verify_afbc_framebuffer(dev, file, mode_cmd))
368 			return ERR_PTR(-EINVAL);
369 	}
370 
371 	return drm_gem_fb_create(dev, file, mode_cmd);
372 }
373 
374 static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
375 	.fb_create = malidp_fb_create,
376 	.atomic_check = drm_atomic_helper_check,
377 	.atomic_commit = drm_atomic_helper_commit,
378 };
379 
380 static int malidp_init(struct drm_device *drm)
381 {
382 	int ret;
383 	struct malidp_drm *malidp = drm->dev_private;
384 	struct malidp_hw_device *hwdev = malidp->dev;
385 
386 	drm_mode_config_init(drm);
387 
388 	drm->mode_config.min_width = hwdev->min_line_size;
389 	drm->mode_config.min_height = hwdev->min_line_size;
390 	drm->mode_config.max_width = hwdev->max_line_size;
391 	drm->mode_config.max_height = hwdev->max_line_size;
392 	drm->mode_config.funcs = &malidp_mode_config_funcs;
393 	drm->mode_config.helper_private = &malidp_mode_config_helpers;
394 	drm->mode_config.allow_fb_modifiers = true;
395 
396 	ret = malidp_crtc_init(drm);
397 	if (ret)
398 		goto crtc_fail;
399 
400 	ret = malidp_mw_connector_init(drm);
401 	if (ret)
402 		goto crtc_fail;
403 
404 	return 0;
405 
406 crtc_fail:
407 	drm_mode_config_cleanup(drm);
408 	return ret;
409 }
410 
411 static void malidp_fini(struct drm_device *drm)
412 {
413 	drm_mode_config_cleanup(drm);
414 }
415 
416 static int malidp_irq_init(struct platform_device *pdev)
417 {
418 	int irq_de, irq_se, ret = 0;
419 	struct drm_device *drm = dev_get_drvdata(&pdev->dev);
420 	struct malidp_drm *malidp = drm->dev_private;
421 	struct malidp_hw_device *hwdev = malidp->dev;
422 
423 	/* fetch the interrupts from DT */
424 	irq_de = platform_get_irq_byname(pdev, "DE");
425 	if (irq_de < 0) {
426 		DRM_ERROR("no 'DE' IRQ specified!\n");
427 		return irq_de;
428 	}
429 	irq_se = platform_get_irq_byname(pdev, "SE");
430 	if (irq_se < 0) {
431 		DRM_ERROR("no 'SE' IRQ specified!\n");
432 		return irq_se;
433 	}
434 
435 	ret = malidp_de_irq_init(drm, irq_de);
436 	if (ret)
437 		return ret;
438 
439 	ret = malidp_se_irq_init(drm, irq_se);
440 	if (ret) {
441 		malidp_de_irq_fini(hwdev);
442 		return ret;
443 	}
444 
445 	return 0;
446 }
447 
448 DEFINE_DRM_GEM_CMA_FOPS(fops);
449 
450 static int malidp_dumb_create(struct drm_file *file_priv,
451 			      struct drm_device *drm,
452 			      struct drm_mode_create_dumb *args)
453 {
454 	struct malidp_drm *malidp = drm->dev_private;
455 	/* allocate for the worst case scenario, i.e. rotated buffers */
456 	u8 alignment = malidp_hw_get_pitch_align(malidp->dev, 1);
457 
458 	args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), alignment);
459 
460 	return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
461 }
462 
463 #ifdef CONFIG_DEBUG_FS
464 
465 static void malidp_error_stats_init(struct malidp_error_stats *error_stats)
466 {
467 	error_stats->num_errors = 0;
468 	error_stats->last_error_status = 0;
469 	error_stats->last_error_vblank = -1;
470 }
471 
472 void malidp_error(struct malidp_drm *malidp,
473 		  struct malidp_error_stats *error_stats, u32 status,
474 		  u64 vblank)
475 {
476 	unsigned long irqflags;
477 
478 	spin_lock_irqsave(&malidp->errors_lock, irqflags);
479 	error_stats->last_error_status = status;
480 	error_stats->last_error_vblank = vblank;
481 	error_stats->num_errors++;
482 	spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
483 }
484 
485 void malidp_error_stats_dump(const char *prefix,
486 			     struct malidp_error_stats error_stats,
487 			     struct seq_file *m)
488 {
489 	seq_printf(m, "[%s] num_errors : %d\n", prefix,
490 		   error_stats.num_errors);
491 	seq_printf(m, "[%s] last_error_status  : 0x%08x\n", prefix,
492 		   error_stats.last_error_status);
493 	seq_printf(m, "[%s] last_error_vblank : %lld\n", prefix,
494 		   error_stats.last_error_vblank);
495 }
496 
497 static int malidp_show_stats(struct seq_file *m, void *arg)
498 {
499 	struct drm_device *drm = m->private;
500 	struct malidp_drm *malidp = drm->dev_private;
501 	unsigned long irqflags;
502 	struct malidp_error_stats de_errors, se_errors;
503 
504 	spin_lock_irqsave(&malidp->errors_lock, irqflags);
505 	de_errors = malidp->de_errors;
506 	se_errors = malidp->se_errors;
507 	spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
508 	malidp_error_stats_dump("DE", de_errors, m);
509 	malidp_error_stats_dump("SE", se_errors, m);
510 	return 0;
511 }
512 
513 static int malidp_debugfs_open(struct inode *inode, struct file *file)
514 {
515 	return single_open(file, malidp_show_stats, inode->i_private);
516 }
517 
518 static ssize_t malidp_debugfs_write(struct file *file, const char __user *ubuf,
519 				    size_t len, loff_t *offp)
520 {
521 	struct seq_file *m = file->private_data;
522 	struct drm_device *drm = m->private;
523 	struct malidp_drm *malidp = drm->dev_private;
524 	unsigned long irqflags;
525 
526 	spin_lock_irqsave(&malidp->errors_lock, irqflags);
527 	malidp_error_stats_init(&malidp->de_errors);
528 	malidp_error_stats_init(&malidp->se_errors);
529 	spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
530 	return len;
531 }
532 
533 static const struct file_operations malidp_debugfs_fops = {
534 	.owner = THIS_MODULE,
535 	.open = malidp_debugfs_open,
536 	.read = seq_read,
537 	.write = malidp_debugfs_write,
538 	.llseek = seq_lseek,
539 	.release = single_release,
540 };
541 
542 static int malidp_debugfs_init(struct drm_minor *minor)
543 {
544 	struct malidp_drm *malidp = minor->dev->dev_private;
545 	struct dentry *dentry = NULL;
546 
547 	malidp_error_stats_init(&malidp->de_errors);
548 	malidp_error_stats_init(&malidp->se_errors);
549 	spin_lock_init(&malidp->errors_lock);
550 	dentry = debugfs_create_file("debug",
551 				     S_IRUGO | S_IWUSR,
552 				     minor->debugfs_root, minor->dev,
553 				     &malidp_debugfs_fops);
554 	if (!dentry) {
555 		DRM_ERROR("Cannot create debug file\n");
556 		return -ENOMEM;
557 	}
558 	return 0;
559 }
560 
561 #endif //CONFIG_DEBUG_FS
562 
563 static struct drm_driver malidp_driver = {
564 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
565 			   DRIVER_PRIME,
566 	.gem_free_object_unlocked = drm_gem_cma_free_object,
567 	.gem_vm_ops = &drm_gem_cma_vm_ops,
568 	.dumb_create = malidp_dumb_create,
569 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
570 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
571 	.gem_prime_export = drm_gem_prime_export,
572 	.gem_prime_import = drm_gem_prime_import,
573 	.gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
574 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
575 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
576 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
577 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
578 #ifdef CONFIG_DEBUG_FS
579 	.debugfs_init = malidp_debugfs_init,
580 #endif
581 	.fops = &fops,
582 	.name = "mali-dp",
583 	.desc = "ARM Mali Display Processor driver",
584 	.date = "20160106",
585 	.major = 1,
586 	.minor = 0,
587 };
588 
589 static const struct of_device_id  malidp_drm_of_match[] = {
590 	{
591 		.compatible = "arm,mali-dp500",
592 		.data = &malidp_device[MALIDP_500]
593 	},
594 	{
595 		.compatible = "arm,mali-dp550",
596 		.data = &malidp_device[MALIDP_550]
597 	},
598 	{
599 		.compatible = "arm,mali-dp650",
600 		.data = &malidp_device[MALIDP_650]
601 	},
602 	{},
603 };
604 MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
605 
606 static bool malidp_is_compatible_hw_id(struct malidp_hw_device *hwdev,
607 				       const struct of_device_id *dev_id)
608 {
609 	u32 core_id;
610 	const char *compatstr_dp500 = "arm,mali-dp500";
611 	bool is_dp500;
612 	bool dt_is_dp500;
613 
614 	/*
615 	 * The DP500 CORE_ID register is in a different location, so check it
616 	 * first. If the product id field matches, then this is DP500, otherwise
617 	 * check the DP550/650 CORE_ID register.
618 	 */
619 	core_id = malidp_hw_read(hwdev, MALIDP500_DC_BASE + MALIDP_DE_CORE_ID);
620 	/* Offset 0x18 will never read 0x500 on products other than DP500. */
621 	is_dp500 = (MALIDP_PRODUCT_ID(core_id) == 0x500);
622 	dt_is_dp500 = strnstr(dev_id->compatible, compatstr_dp500,
623 			      sizeof(dev_id->compatible)) != NULL;
624 	if (is_dp500 != dt_is_dp500) {
625 		DRM_ERROR("Device-tree expects %s, but hardware %s DP500.\n",
626 			  dev_id->compatible, is_dp500 ? "is" : "is not");
627 		return false;
628 	} else if (!dt_is_dp500) {
629 		u16 product_id;
630 		char buf[32];
631 
632 		core_id = malidp_hw_read(hwdev,
633 					 MALIDP550_DC_BASE + MALIDP_DE_CORE_ID);
634 		product_id = MALIDP_PRODUCT_ID(core_id);
635 		snprintf(buf, sizeof(buf), "arm,mali-dp%X", product_id);
636 		if (!strnstr(dev_id->compatible, buf,
637 			     sizeof(dev_id->compatible))) {
638 			DRM_ERROR("Device-tree expects %s, but hardware is DP%03X.\n",
639 				  dev_id->compatible, product_id);
640 			return false;
641 		}
642 	}
643 	return true;
644 }
645 
646 static bool malidp_has_sufficient_address_space(const struct resource *res,
647 						const struct of_device_id *dev_id)
648 {
649 	resource_size_t res_size = resource_size(res);
650 	const char *compatstr_dp500 = "arm,mali-dp500";
651 
652 	if (!strnstr(dev_id->compatible, compatstr_dp500,
653 		     sizeof(dev_id->compatible)))
654 		return res_size >= MALIDP550_ADDR_SPACE_SIZE;
655 	else if (res_size < MALIDP500_ADDR_SPACE_SIZE)
656 		return false;
657 	return true;
658 }
659 
660 static ssize_t core_id_show(struct device *dev, struct device_attribute *attr,
661 			    char *buf)
662 {
663 	struct drm_device *drm = dev_get_drvdata(dev);
664 	struct malidp_drm *malidp = drm->dev_private;
665 
666 	return snprintf(buf, PAGE_SIZE, "%08x\n", malidp->core_id);
667 }
668 
669 DEVICE_ATTR_RO(core_id);
670 
671 static int malidp_init_sysfs(struct device *dev)
672 {
673 	int ret = device_create_file(dev, &dev_attr_core_id);
674 
675 	if (ret)
676 		DRM_ERROR("failed to create device file for core_id\n");
677 
678 	return ret;
679 }
680 
681 static void malidp_fini_sysfs(struct device *dev)
682 {
683 	device_remove_file(dev, &dev_attr_core_id);
684 }
685 
686 #define MAX_OUTPUT_CHANNELS	3
687 
688 static int malidp_runtime_pm_suspend(struct device *dev)
689 {
690 	struct drm_device *drm = dev_get_drvdata(dev);
691 	struct malidp_drm *malidp = drm->dev_private;
692 	struct malidp_hw_device *hwdev = malidp->dev;
693 
694 	/* we can only suspend if the hardware is in config mode */
695 	WARN_ON(!hwdev->hw->in_config_mode(hwdev));
696 
697 	malidp_se_irq_fini(hwdev);
698 	malidp_de_irq_fini(hwdev);
699 	hwdev->pm_suspended = true;
700 	clk_disable_unprepare(hwdev->mclk);
701 	clk_disable_unprepare(hwdev->aclk);
702 	clk_disable_unprepare(hwdev->pclk);
703 
704 	return 0;
705 }
706 
707 static int malidp_runtime_pm_resume(struct device *dev)
708 {
709 	struct drm_device *drm = dev_get_drvdata(dev);
710 	struct malidp_drm *malidp = drm->dev_private;
711 	struct malidp_hw_device *hwdev = malidp->dev;
712 
713 	clk_prepare_enable(hwdev->pclk);
714 	clk_prepare_enable(hwdev->aclk);
715 	clk_prepare_enable(hwdev->mclk);
716 	hwdev->pm_suspended = false;
717 	malidp_de_irq_hw_init(hwdev);
718 	malidp_se_irq_hw_init(hwdev);
719 
720 	return 0;
721 }
722 
723 static int malidp_bind(struct device *dev)
724 {
725 	struct resource *res;
726 	struct drm_device *drm;
727 	struct malidp_drm *malidp;
728 	struct malidp_hw_device *hwdev;
729 	struct platform_device *pdev = to_platform_device(dev);
730 	struct of_device_id const *dev_id;
731 	struct drm_encoder *encoder;
732 	/* number of lines for the R, G and B output */
733 	u8 output_width[MAX_OUTPUT_CHANNELS];
734 	int ret = 0, i;
735 	u32 version, out_depth = 0;
736 
737 	malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
738 	if (!malidp)
739 		return -ENOMEM;
740 
741 	hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
742 	if (!hwdev)
743 		return -ENOMEM;
744 
745 	hwdev->hw = (struct malidp_hw *)of_device_get_match_data(dev);
746 	malidp->dev = hwdev;
747 
748 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
749 	hwdev->regs = devm_ioremap_resource(dev, res);
750 	if (IS_ERR(hwdev->regs))
751 		return PTR_ERR(hwdev->regs);
752 
753 	hwdev->pclk = devm_clk_get(dev, "pclk");
754 	if (IS_ERR(hwdev->pclk))
755 		return PTR_ERR(hwdev->pclk);
756 
757 	hwdev->aclk = devm_clk_get(dev, "aclk");
758 	if (IS_ERR(hwdev->aclk))
759 		return PTR_ERR(hwdev->aclk);
760 
761 	hwdev->mclk = devm_clk_get(dev, "mclk");
762 	if (IS_ERR(hwdev->mclk))
763 		return PTR_ERR(hwdev->mclk);
764 
765 	hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
766 	if (IS_ERR(hwdev->pxlclk))
767 		return PTR_ERR(hwdev->pxlclk);
768 
769 	/* Get the optional framebuffer memory resource */
770 	ret = of_reserved_mem_device_init(dev);
771 	if (ret && ret != -ENODEV)
772 		return ret;
773 
774 	drm = drm_dev_alloc(&malidp_driver, dev);
775 	if (IS_ERR(drm)) {
776 		ret = PTR_ERR(drm);
777 		goto alloc_fail;
778 	}
779 
780 	drm->dev_private = malidp;
781 	dev_set_drvdata(dev, drm);
782 
783 	/* Enable power management */
784 	pm_runtime_enable(dev);
785 
786 	/* Resume device to enable the clocks */
787 	if (pm_runtime_enabled(dev))
788 		pm_runtime_get_sync(dev);
789 	else
790 		malidp_runtime_pm_resume(dev);
791 
792 	dev_id = of_match_device(malidp_drm_of_match, dev);
793 	if (!dev_id) {
794 		ret = -EINVAL;
795 		goto query_hw_fail;
796 	}
797 
798 	if (!malidp_has_sufficient_address_space(res, dev_id)) {
799 		DRM_ERROR("Insufficient address space in device-tree.\n");
800 		ret = -EINVAL;
801 		goto query_hw_fail;
802 	}
803 
804 	if (!malidp_is_compatible_hw_id(hwdev, dev_id)) {
805 		ret = -EINVAL;
806 		goto query_hw_fail;
807 	}
808 
809 	ret = hwdev->hw->query_hw(hwdev);
810 	if (ret) {
811 		DRM_ERROR("Invalid HW configuration\n");
812 		goto query_hw_fail;
813 	}
814 
815 	version = malidp_hw_read(hwdev, hwdev->hw->map.dc_base + MALIDP_DE_CORE_ID);
816 	DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
817 		 (version >> 12) & 0xf, (version >> 8) & 0xf);
818 
819 	malidp->core_id = version;
820 
821 	/* set the number of lines used for output of RGB data */
822 	ret = of_property_read_u8_array(dev->of_node,
823 					"arm,malidp-output-port-lines",
824 					output_width, MAX_OUTPUT_CHANNELS);
825 	if (ret)
826 		goto query_hw_fail;
827 
828 	for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
829 		out_depth = (out_depth << 8) | (output_width[i] & 0xf);
830 	malidp_hw_write(hwdev, out_depth, hwdev->hw->map.out_depth_base);
831 	hwdev->output_color_depth = out_depth;
832 
833 	atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_INIT);
834 	init_waitqueue_head(&malidp->wq);
835 
836 	ret = malidp_init(drm);
837 	if (ret < 0)
838 		goto query_hw_fail;
839 
840 	ret = malidp_init_sysfs(dev);
841 	if (ret)
842 		goto init_fail;
843 
844 	/* Set the CRTC's port so that the encoder component can find it */
845 	malidp->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
846 
847 	ret = component_bind_all(dev, drm);
848 	if (ret) {
849 		DRM_ERROR("Failed to bind all components\n");
850 		goto bind_fail;
851 	}
852 
853 	/* We expect to have a maximum of two encoders one for the actual
854 	 * display and a virtual one for the writeback connector
855 	 */
856 	WARN_ON(drm->mode_config.num_encoder > 2);
857 	list_for_each_entry(encoder, &drm->mode_config.encoder_list, head) {
858 		encoder->possible_clones =
859 				(1 << drm->mode_config.num_encoder) -  1;
860 	}
861 
862 	ret = malidp_irq_init(pdev);
863 	if (ret < 0)
864 		goto irq_init_fail;
865 
866 	drm->irq_enabled = true;
867 
868 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
869 	drm_crtc_vblank_reset(&malidp->crtc);
870 	if (ret < 0) {
871 		DRM_ERROR("failed to initialise vblank\n");
872 		goto vblank_fail;
873 	}
874 	pm_runtime_put(dev);
875 
876 	drm_mode_config_reset(drm);
877 
878 	drm_kms_helper_poll_init(drm);
879 
880 	ret = drm_dev_register(drm, 0);
881 	if (ret)
882 		goto register_fail;
883 
884 	drm_fbdev_generic_setup(drm, 32);
885 
886 	return 0;
887 
888 register_fail:
889 	drm_kms_helper_poll_fini(drm);
890 	pm_runtime_get_sync(dev);
891 vblank_fail:
892 	malidp_se_irq_fini(hwdev);
893 	malidp_de_irq_fini(hwdev);
894 	drm->irq_enabled = false;
895 irq_init_fail:
896 	drm_atomic_helper_shutdown(drm);
897 	component_unbind_all(dev, drm);
898 bind_fail:
899 	of_node_put(malidp->crtc.port);
900 	malidp->crtc.port = NULL;
901 init_fail:
902 	malidp_fini_sysfs(dev);
903 	malidp_fini(drm);
904 query_hw_fail:
905 	pm_runtime_put(dev);
906 	if (pm_runtime_enabled(dev))
907 		pm_runtime_disable(dev);
908 	else
909 		malidp_runtime_pm_suspend(dev);
910 	drm->dev_private = NULL;
911 	dev_set_drvdata(dev, NULL);
912 	drm_dev_put(drm);
913 alloc_fail:
914 	of_reserved_mem_device_release(dev);
915 
916 	return ret;
917 }
918 
919 static void malidp_unbind(struct device *dev)
920 {
921 	struct drm_device *drm = dev_get_drvdata(dev);
922 	struct malidp_drm *malidp = drm->dev_private;
923 	struct malidp_hw_device *hwdev = malidp->dev;
924 
925 	drm_dev_unregister(drm);
926 	drm_kms_helper_poll_fini(drm);
927 	pm_runtime_get_sync(dev);
928 	drm_crtc_vblank_off(&malidp->crtc);
929 	malidp_se_irq_fini(hwdev);
930 	malidp_de_irq_fini(hwdev);
931 	drm->irq_enabled = false;
932 	drm_atomic_helper_shutdown(drm);
933 	component_unbind_all(dev, drm);
934 	of_node_put(malidp->crtc.port);
935 	malidp->crtc.port = NULL;
936 	malidp_fini_sysfs(dev);
937 	malidp_fini(drm);
938 	pm_runtime_put(dev);
939 	if (pm_runtime_enabled(dev))
940 		pm_runtime_disable(dev);
941 	else
942 		malidp_runtime_pm_suspend(dev);
943 	drm->dev_private = NULL;
944 	dev_set_drvdata(dev, NULL);
945 	drm_dev_put(drm);
946 	of_reserved_mem_device_release(dev);
947 }
948 
949 static const struct component_master_ops malidp_master_ops = {
950 	.bind = malidp_bind,
951 	.unbind = malidp_unbind,
952 };
953 
954 static int malidp_compare_dev(struct device *dev, void *data)
955 {
956 	struct device_node *np = data;
957 
958 	return dev->of_node == np;
959 }
960 
961 static int malidp_platform_probe(struct platform_device *pdev)
962 {
963 	struct device_node *port;
964 	struct component_match *match = NULL;
965 
966 	if (!pdev->dev.of_node)
967 		return -ENODEV;
968 
969 	/* there is only one output port inside each device, find it */
970 	port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
971 	if (!port)
972 		return -ENODEV;
973 
974 	drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
975 				   port);
976 	of_node_put(port);
977 	return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
978 					       match);
979 }
980 
981 static int malidp_platform_remove(struct platform_device *pdev)
982 {
983 	component_master_del(&pdev->dev, &malidp_master_ops);
984 	return 0;
985 }
986 
987 static int __maybe_unused malidp_pm_suspend(struct device *dev)
988 {
989 	struct drm_device *drm = dev_get_drvdata(dev);
990 
991 	return drm_mode_config_helper_suspend(drm);
992 }
993 
994 static int __maybe_unused malidp_pm_resume(struct device *dev)
995 {
996 	struct drm_device *drm = dev_get_drvdata(dev);
997 
998 	drm_mode_config_helper_resume(drm);
999 
1000 	return 0;
1001 }
1002 
1003 static int __maybe_unused malidp_pm_suspend_late(struct device *dev)
1004 {
1005 	if (!pm_runtime_status_suspended(dev)) {
1006 		malidp_runtime_pm_suspend(dev);
1007 		pm_runtime_set_suspended(dev);
1008 	}
1009 	return 0;
1010 }
1011 
1012 static int __maybe_unused malidp_pm_resume_early(struct device *dev)
1013 {
1014 	malidp_runtime_pm_resume(dev);
1015 	pm_runtime_set_active(dev);
1016 	return 0;
1017 }
1018 
1019 static const struct dev_pm_ops malidp_pm_ops = {
1020 	SET_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend, malidp_pm_resume) \
1021 	SET_LATE_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend_late, malidp_pm_resume_early) \
1022 	SET_RUNTIME_PM_OPS(malidp_runtime_pm_suspend, malidp_runtime_pm_resume, NULL)
1023 };
1024 
1025 static struct platform_driver malidp_platform_driver = {
1026 	.probe		= malidp_platform_probe,
1027 	.remove		= malidp_platform_remove,
1028 	.driver	= {
1029 		.name = "mali-dp",
1030 		.pm = &malidp_pm_ops,
1031 		.of_match_table	= malidp_drm_of_match,
1032 	},
1033 };
1034 
1035 module_platform_driver(malidp_platform_driver);
1036 
1037 MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
1038 MODULE_DESCRIPTION("ARM Mali DP DRM driver");
1039 MODULE_LICENSE("GPL v2");
1040