xref: /openbmc/linux/drivers/gpu/drm/arm/malidp_drv.c (revision 0424fdaf)
1e559355aSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ad49f860SLiviu Dudau /*
3ad49f860SLiviu Dudau  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
4ad49f860SLiviu Dudau  * Author: Liviu Dudau <Liviu.Dudau@arm.com>
5ad49f860SLiviu Dudau  *
6ad49f860SLiviu Dudau  * ARM Mali DP500/DP550/DP650 KMS/DRM driver
7ad49f860SLiviu Dudau  */
8ad49f860SLiviu Dudau 
9ad49f860SLiviu Dudau #include <linux/module.h>
10ad49f860SLiviu Dudau #include <linux/clk.h>
11ad49f860SLiviu Dudau #include <linux/component.h>
12ad49f860SLiviu Dudau #include <linux/of_device.h>
13ad49f860SLiviu Dudau #include <linux/of_graph.h>
14ad49f860SLiviu Dudau #include <linux/of_reserved_mem.h>
1585f64218SLiviu Dudau #include <linux/pm_runtime.h>
16613c5c7fSAlexandru Gheorghe #include <linux/debugfs.h>
17ad49f860SLiviu Dudau 
18ad49f860SLiviu Dudau #include <drm/drmP.h>
19ad49f860SLiviu Dudau #include <drm/drm_atomic.h>
20ad49f860SLiviu Dudau #include <drm/drm_atomic_helper.h>
21ad49f860SLiviu Dudau #include <drm/drm_crtc.h>
22fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
23bdecd835SNoralf Trønnes #include <drm/drm_fb_helper.h>
24ad49f860SLiviu Dudau #include <drm/drm_fb_cma_helper.h>
25ad49f860SLiviu Dudau #include <drm/drm_gem_cma_helper.h>
26783f7d97SNoralf Trønnes #include <drm/drm_gem_framebuffer_helper.h>
27194b8799SNoralf Trønnes #include <drm/drm_modeset_helper.h>
28ad49f860SLiviu Dudau #include <drm/drm_of.h>
29ad49f860SLiviu Dudau 
30ad49f860SLiviu Dudau #include "malidp_drv.h"
318cbc5cafSBrian Starkey #include "malidp_mw.h"
32ad49f860SLiviu Dudau #include "malidp_regs.h"
33ad49f860SLiviu Dudau #include "malidp_hw.h"
34ad49f860SLiviu Dudau 
35ad49f860SLiviu Dudau #define MALIDP_CONF_VALID_TIMEOUT	250
363dae1c09SAyan Kumar Halder #define AFBC_HEADER_SIZE		16
37fd99bd8bSLiviu Dudau #define AFBC_SUPERBLK_ALIGNMENT		128
38ad49f860SLiviu Dudau 
3902725d31SMihail Atanassov static void malidp_write_gamma_table(struct malidp_hw_device *hwdev,
4002725d31SMihail Atanassov 				     u32 data[MALIDP_COEFFTAB_NUM_COEFFS])
4102725d31SMihail Atanassov {
4202725d31SMihail Atanassov 	int i;
4302725d31SMihail Atanassov 	/* Update all channels with a single gamma curve. */
4402725d31SMihail Atanassov 	const u32 gamma_write_mask = GENMASK(18, 16);
4502725d31SMihail Atanassov 	/*
4602725d31SMihail Atanassov 	 * Always write an entire table, so the address field in
4702725d31SMihail Atanassov 	 * DE_COEFFTAB_ADDR is 0 and we can use the gamma_write_mask bitmask
4802725d31SMihail Atanassov 	 * directly.
4902725d31SMihail Atanassov 	 */
5002725d31SMihail Atanassov 	malidp_hw_write(hwdev, gamma_write_mask,
51a6993b21SLiviu Dudau 			hwdev->hw->map.coeffs_base + MALIDP_COEF_TABLE_ADDR);
5202725d31SMihail Atanassov 	for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i)
5302725d31SMihail Atanassov 		malidp_hw_write(hwdev, data[i],
54a6993b21SLiviu Dudau 				hwdev->hw->map.coeffs_base +
5502725d31SMihail Atanassov 				MALIDP_COEF_TABLE_DATA);
5602725d31SMihail Atanassov }
5702725d31SMihail Atanassov 
5802725d31SMihail Atanassov static void malidp_atomic_commit_update_gamma(struct drm_crtc *crtc,
5902725d31SMihail Atanassov 					      struct drm_crtc_state *old_state)
6002725d31SMihail Atanassov {
6102725d31SMihail Atanassov 	struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
6202725d31SMihail Atanassov 	struct malidp_hw_device *hwdev = malidp->dev;
6302725d31SMihail Atanassov 
6402725d31SMihail Atanassov 	if (!crtc->state->color_mgmt_changed)
6502725d31SMihail Atanassov 		return;
6602725d31SMihail Atanassov 
6702725d31SMihail Atanassov 	if (!crtc->state->gamma_lut) {
6802725d31SMihail Atanassov 		malidp_hw_clearbits(hwdev,
6902725d31SMihail Atanassov 				    MALIDP_DISP_FUNC_GAMMA,
7002725d31SMihail Atanassov 				    MALIDP_DE_DISPLAY_FUNC);
7102725d31SMihail Atanassov 	} else {
7202725d31SMihail Atanassov 		struct malidp_crtc_state *mc =
7302725d31SMihail Atanassov 			to_malidp_crtc_state(crtc->state);
7402725d31SMihail Atanassov 
7502725d31SMihail Atanassov 		if (!old_state->gamma_lut || (crtc->state->gamma_lut->base.id !=
7602725d31SMihail Atanassov 					      old_state->gamma_lut->base.id))
7702725d31SMihail Atanassov 			malidp_write_gamma_table(hwdev, mc->gamma_coeffs);
7802725d31SMihail Atanassov 
7902725d31SMihail Atanassov 		malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_GAMMA,
8002725d31SMihail Atanassov 				  MALIDP_DE_DISPLAY_FUNC);
8102725d31SMihail Atanassov 	}
8202725d31SMihail Atanassov }
8302725d31SMihail Atanassov 
846954f245SMihail Atanassov static
856954f245SMihail Atanassov void malidp_atomic_commit_update_coloradj(struct drm_crtc *crtc,
866954f245SMihail Atanassov 					  struct drm_crtc_state *old_state)
876954f245SMihail Atanassov {
886954f245SMihail Atanassov 	struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
896954f245SMihail Atanassov 	struct malidp_hw_device *hwdev = malidp->dev;
906954f245SMihail Atanassov 	int i;
916954f245SMihail Atanassov 
926954f245SMihail Atanassov 	if (!crtc->state->color_mgmt_changed)
936954f245SMihail Atanassov 		return;
946954f245SMihail Atanassov 
956954f245SMihail Atanassov 	if (!crtc->state->ctm) {
966954f245SMihail Atanassov 		malidp_hw_clearbits(hwdev, MALIDP_DISP_FUNC_CADJ,
976954f245SMihail Atanassov 				    MALIDP_DE_DISPLAY_FUNC);
986954f245SMihail Atanassov 	} else {
996954f245SMihail Atanassov 		struct malidp_crtc_state *mc =
1006954f245SMihail Atanassov 			to_malidp_crtc_state(crtc->state);
1016954f245SMihail Atanassov 
1026954f245SMihail Atanassov 		if (!old_state->ctm || (crtc->state->ctm->base.id !=
1036954f245SMihail Atanassov 					old_state->ctm->base.id))
1046954f245SMihail Atanassov 			for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; ++i)
1056954f245SMihail Atanassov 				malidp_hw_write(hwdev,
1066954f245SMihail Atanassov 						mc->coloradj_coeffs[i],
107a6993b21SLiviu Dudau 						hwdev->hw->map.coeffs_base +
1086954f245SMihail Atanassov 						MALIDP_COLOR_ADJ_COEF + 4 * i);
1096954f245SMihail Atanassov 
1106954f245SMihail Atanassov 		malidp_hw_setbits(hwdev, MALIDP_DISP_FUNC_CADJ,
1116954f245SMihail Atanassov 				  MALIDP_DE_DISPLAY_FUNC);
1126954f245SMihail Atanassov 	}
1136954f245SMihail Atanassov }
1146954f245SMihail Atanassov 
11528ce675bSMihail Atanassov static void malidp_atomic_commit_se_config(struct drm_crtc *crtc,
11628ce675bSMihail Atanassov 					   struct drm_crtc_state *old_state)
11728ce675bSMihail Atanassov {
11828ce675bSMihail Atanassov 	struct malidp_crtc_state *cs = to_malidp_crtc_state(crtc->state);
11928ce675bSMihail Atanassov 	struct malidp_crtc_state *old_cs = to_malidp_crtc_state(old_state);
12028ce675bSMihail Atanassov 	struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
12128ce675bSMihail Atanassov 	struct malidp_hw_device *hwdev = malidp->dev;
12228ce675bSMihail Atanassov 	struct malidp_se_config *s = &cs->scaler_config;
12328ce675bSMihail Atanassov 	struct malidp_se_config *old_s = &old_cs->scaler_config;
124a6993b21SLiviu Dudau 	u32 se_control = hwdev->hw->map.se_base +
125a6993b21SLiviu Dudau 			 ((hwdev->hw->map.features & MALIDP_REGMAP_HAS_CLEARIRQ) ?
12628ce675bSMihail Atanassov 			 0x10 : 0xC);
12728ce675bSMihail Atanassov 	u32 layer_control = se_control + MALIDP_SE_LAYER_CONTROL;
12828ce675bSMihail Atanassov 	u32 scr = se_control + MALIDP_SE_SCALING_CONTROL;
12928ce675bSMihail Atanassov 	u32 val;
13028ce675bSMihail Atanassov 
13128ce675bSMihail Atanassov 	/* Set SE_CONTROL */
13228ce675bSMihail Atanassov 	if (!s->scale_enable) {
13328ce675bSMihail Atanassov 		val = malidp_hw_read(hwdev, se_control);
13428ce675bSMihail Atanassov 		val &= ~MALIDP_SE_SCALING_EN;
13528ce675bSMihail Atanassov 		malidp_hw_write(hwdev, val, se_control);
13628ce675bSMihail Atanassov 		return;
13728ce675bSMihail Atanassov 	}
13828ce675bSMihail Atanassov 
139a6993b21SLiviu Dudau 	hwdev->hw->se_set_scaling_coeffs(hwdev, s, old_s);
14028ce675bSMihail Atanassov 	val = malidp_hw_read(hwdev, se_control);
14128ce675bSMihail Atanassov 	val |= MALIDP_SE_SCALING_EN | MALIDP_SE_ALPHA_EN;
14228ce675bSMihail Atanassov 
1430274e6a0SMihail Atanassov 	val &= ~MALIDP_SE_ENH(MALIDP_SE_ENH_MASK);
1440274e6a0SMihail Atanassov 	val |= s->enhancer_enable ? MALIDP_SE_ENH(3) : 0;
1450274e6a0SMihail Atanassov 
14628ce675bSMihail Atanassov 	val |= MALIDP_SE_RGBO_IF_EN;
14728ce675bSMihail Atanassov 	malidp_hw_write(hwdev, val, se_control);
14828ce675bSMihail Atanassov 
14928ce675bSMihail Atanassov 	/* Set IN_SIZE & OUT_SIZE. */
15028ce675bSMihail Atanassov 	val = MALIDP_SE_SET_V_SIZE(s->input_h) |
15128ce675bSMihail Atanassov 	      MALIDP_SE_SET_H_SIZE(s->input_w);
15228ce675bSMihail Atanassov 	malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_IN_SIZE);
15328ce675bSMihail Atanassov 	val = MALIDP_SE_SET_V_SIZE(s->output_h) |
15428ce675bSMihail Atanassov 	      MALIDP_SE_SET_H_SIZE(s->output_w);
15528ce675bSMihail Atanassov 	malidp_hw_write(hwdev, val, layer_control + MALIDP_SE_L0_OUT_SIZE);
15628ce675bSMihail Atanassov 
15728ce675bSMihail Atanassov 	/* Set phase regs. */
15828ce675bSMihail Atanassov 	malidp_hw_write(hwdev, s->h_init_phase, scr + MALIDP_SE_H_INIT_PH);
15928ce675bSMihail Atanassov 	malidp_hw_write(hwdev, s->h_delta_phase, scr + MALIDP_SE_H_DELTA_PH);
16028ce675bSMihail Atanassov 	malidp_hw_write(hwdev, s->v_init_phase, scr + MALIDP_SE_V_INIT_PH);
16128ce675bSMihail Atanassov 	malidp_hw_write(hwdev, s->v_delta_phase, scr + MALIDP_SE_V_DELTA_PH);
16228ce675bSMihail Atanassov }
16328ce675bSMihail Atanassov 
164ad49f860SLiviu Dudau /*
165ad49f860SLiviu Dudau  * set the "config valid" bit and wait until the hardware acts on it
166ad49f860SLiviu Dudau  */
167ad49f860SLiviu Dudau static int malidp_set_and_wait_config_valid(struct drm_device *drm)
168ad49f860SLiviu Dudau {
169ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
170ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
171ad49f860SLiviu Dudau 	int ret;
172ad49f860SLiviu Dudau 
1730735cfdfSLiviu Dudau 	hwdev->hw->set_config_valid(hwdev, 1);
174ad49f860SLiviu Dudau 	/* don't wait for config_valid flag if we are in config mode */
1751cb3cbe7SLiviu Dudau 	if (hwdev->hw->in_config_mode(hwdev)) {
1761cb3cbe7SLiviu Dudau 		atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_DONE);
177ad49f860SLiviu Dudau 		return 0;
1781cb3cbe7SLiviu Dudau 	}
179ad49f860SLiviu Dudau 
180ad49f860SLiviu Dudau 	ret = wait_event_interruptible_timeout(malidp->wq,
1811cb3cbe7SLiviu Dudau 			atomic_read(&malidp->config_valid) == MALIDP_CONFIG_VALID_DONE,
182ad49f860SLiviu Dudau 			msecs_to_jiffies(MALIDP_CONF_VALID_TIMEOUT));
183ad49f860SLiviu Dudau 
184ad49f860SLiviu Dudau 	return (ret > 0) ? 0 : -ETIMEDOUT;
185ad49f860SLiviu Dudau }
186ad49f860SLiviu Dudau 
187ad49f860SLiviu Dudau static void malidp_atomic_commit_hw_done(struct drm_atomic_state *state)
188ad49f860SLiviu Dudau {
189ad49f860SLiviu Dudau 	struct drm_device *drm = state->dev;
190ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
1916a88e0c1SWen He 	int loop = 5;
192ad49f860SLiviu Dudau 
193d862b2d6SLiviu Dudau 	malidp->event = malidp->crtc.state->event;
194ad49f860SLiviu Dudau 	malidp->crtc.state->event = NULL;
195ad49f860SLiviu Dudau 
196d862b2d6SLiviu Dudau 	if (malidp->crtc.state->active) {
197d862b2d6SLiviu Dudau 		/*
198d862b2d6SLiviu Dudau 		 * if we have an event to deliver to userspace, make sure
199d862b2d6SLiviu Dudau 		 * the vblank is enabled as we are sending it from the IRQ
200d862b2d6SLiviu Dudau 		 * handler.
201d862b2d6SLiviu Dudau 		 */
202d862b2d6SLiviu Dudau 		if (malidp->event)
203d862b2d6SLiviu Dudau 			drm_crtc_vblank_get(&malidp->crtc);
204d862b2d6SLiviu Dudau 
205d862b2d6SLiviu Dudau 		/* only set config_valid if the CRTC is enabled */
2066a88e0c1SWen He 		if (malidp_set_and_wait_config_valid(drm) < 0) {
2076a88e0c1SWen He 			/*
2086a88e0c1SWen He 			 * make a loop around the second CVAL setting and
2096a88e0c1SWen He 			 * try 5 times before giving up.
2106a88e0c1SWen He 			 */
2116a88e0c1SWen He 			while (loop--) {
2126a88e0c1SWen He 				if (!malidp_set_and_wait_config_valid(drm))
2136a88e0c1SWen He 					break;
2146a88e0c1SWen He 			}
215d862b2d6SLiviu Dudau 			DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
2166a88e0c1SWen He 		}
2176a88e0c1SWen He 
218d862b2d6SLiviu Dudau 	} else if (malidp->event) {
219d862b2d6SLiviu Dudau 		/* CRTC inactive means vblank IRQ is disabled, send event directly */
220ad49f860SLiviu Dudau 		spin_lock_irq(&drm->event_lock);
221d862b2d6SLiviu Dudau 		drm_crtc_send_vblank_event(&malidp->crtc, malidp->event);
222d862b2d6SLiviu Dudau 		malidp->event = NULL;
223ad49f860SLiviu Dudau 		spin_unlock_irq(&drm->event_lock);
224ad49f860SLiviu Dudau 	}
225ad49f860SLiviu Dudau 	drm_atomic_helper_commit_hw_done(state);
226ad49f860SLiviu Dudau }
227ad49f860SLiviu Dudau 
228ad49f860SLiviu Dudau static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
229ad49f860SLiviu Dudau {
230ad49f860SLiviu Dudau 	struct drm_device *drm = state->dev;
2311cb3cbe7SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
23202725d31SMihail Atanassov 	struct drm_crtc *crtc;
23302725d31SMihail Atanassov 	struct drm_crtc_state *old_crtc_state;
23402725d31SMihail Atanassov 	int i;
235ad49f860SLiviu Dudau 
23685f64218SLiviu Dudau 	pm_runtime_get_sync(drm->dev);
23785f64218SLiviu Dudau 
2381cb3cbe7SLiviu Dudau 	/*
2391cb3cbe7SLiviu Dudau 	 * set config_valid to a special value to let IRQ handlers
2401cb3cbe7SLiviu Dudau 	 * know that we are updating registers
2411cb3cbe7SLiviu Dudau 	 */
2421cb3cbe7SLiviu Dudau 	atomic_set(&malidp->config_valid, MALIDP_CONFIG_START);
2430735cfdfSLiviu Dudau 	malidp->dev->hw->set_config_valid(malidp->dev, 0);
2441cb3cbe7SLiviu Dudau 
245ad49f860SLiviu Dudau 	drm_atomic_helper_commit_modeset_disables(drm, state);
24646f1d42fSLiviu Dudau 
247a8e3fb55SMaarten Lankhorst 	for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
24802725d31SMihail Atanassov 		malidp_atomic_commit_update_gamma(crtc, old_crtc_state);
2496954f245SMihail Atanassov 		malidp_atomic_commit_update_coloradj(crtc, old_crtc_state);
25028ce675bSMihail Atanassov 		malidp_atomic_commit_se_config(crtc, old_crtc_state);
2516954f245SMihail Atanassov 	}
25202725d31SMihail Atanassov 
2538cbc5cafSBrian Starkey 	drm_atomic_helper_commit_planes(drm, state, DRM_PLANE_COMMIT_ACTIVE_ONLY);
2548cbc5cafSBrian Starkey 
2558cbc5cafSBrian Starkey 	malidp_mw_atomic_commit(drm, state);
256ad49f860SLiviu Dudau 
25746f1d42fSLiviu Dudau 	drm_atomic_helper_commit_modeset_enables(drm, state);
25846f1d42fSLiviu Dudau 
259ad49f860SLiviu Dudau 	malidp_atomic_commit_hw_done(state);
260ad49f860SLiviu Dudau 
26185f64218SLiviu Dudau 	pm_runtime_put(drm->dev);
26285f64218SLiviu Dudau 
263ad49f860SLiviu Dudau 	drm_atomic_helper_cleanup_planes(drm, state);
264ad49f860SLiviu Dudau }
265ad49f860SLiviu Dudau 
266a4b10cceSLaurent Pinchart static const struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
267ad49f860SLiviu Dudau 	.atomic_commit_tail = malidp_atomic_commit_tail,
268ad49f860SLiviu Dudau };
269ad49f860SLiviu Dudau 
2703dae1c09SAyan Kumar Halder static bool
2713dae1c09SAyan Kumar Halder malidp_verify_afbc_framebuffer_caps(struct drm_device *dev,
2723dae1c09SAyan Kumar Halder 				    const struct drm_mode_fb_cmd2 *mode_cmd)
2733dae1c09SAyan Kumar Halder {
2745e290226SAyan Kumar Halder 	if (malidp_format_mod_supported(dev, mode_cmd->pixel_format,
2755e290226SAyan Kumar Halder 					mode_cmd->modifier[0]) == false)
2763dae1c09SAyan Kumar Halder 		return false;
2773dae1c09SAyan Kumar Halder 
2783dae1c09SAyan Kumar Halder 	if (mode_cmd->offsets[0] != 0) {
2793dae1c09SAyan Kumar Halder 		DRM_DEBUG_KMS("AFBC buffers' plane offset should be 0\n");
2803dae1c09SAyan Kumar Halder 		return false;
2813dae1c09SAyan Kumar Halder 	}
2823dae1c09SAyan Kumar Halder 
2835e290226SAyan Kumar Halder 	switch (mode_cmd->modifier[0] & AFBC_SIZE_MASK) {
2845e290226SAyan Kumar Halder 	case AFBC_SIZE_16X16:
2853dae1c09SAyan Kumar Halder 		if ((mode_cmd->width % 16) || (mode_cmd->height % 16)) {
2863dae1c09SAyan Kumar Halder 			DRM_DEBUG_KMS("AFBC buffers must be aligned to 16 pixels\n");
2873dae1c09SAyan Kumar Halder 			return false;
2883dae1c09SAyan Kumar Halder 		}
2893dae1c09SAyan Kumar Halder 		break;
2903dae1c09SAyan Kumar Halder 	default:
2913dae1c09SAyan Kumar Halder 		DRM_DEBUG_KMS("Unsupported AFBC block size\n");
2923dae1c09SAyan Kumar Halder 		return false;
2933dae1c09SAyan Kumar Halder 	}
2943dae1c09SAyan Kumar Halder 
2953dae1c09SAyan Kumar Halder 	return true;
2963dae1c09SAyan Kumar Halder }
2973dae1c09SAyan Kumar Halder 
2983dae1c09SAyan Kumar Halder static bool
2993dae1c09SAyan Kumar Halder malidp_verify_afbc_framebuffer_size(struct drm_device *dev,
3003dae1c09SAyan Kumar Halder 				    struct drm_file *file,
3013dae1c09SAyan Kumar Halder 				    const struct drm_mode_fb_cmd2 *mode_cmd)
3023dae1c09SAyan Kumar Halder {
3033dae1c09SAyan Kumar Halder 	int n_superblocks = 0;
3043dae1c09SAyan Kumar Halder 	const struct drm_format_info *info;
3053dae1c09SAyan Kumar Halder 	struct drm_gem_object *objs = NULL;
3063dae1c09SAyan Kumar Halder 	u32 afbc_superblock_size = 0, afbc_superblock_height = 0;
3073dae1c09SAyan Kumar Halder 	u32 afbc_superblock_width = 0, afbc_size = 0;
3087834c577SAyan Kumar Halder 	int bpp = 0;
3093dae1c09SAyan Kumar Halder 
3105e290226SAyan Kumar Halder 	switch (mode_cmd->modifier[0] & AFBC_SIZE_MASK) {
3115e290226SAyan Kumar Halder 	case AFBC_SIZE_16X16:
3123dae1c09SAyan Kumar Halder 		afbc_superblock_height = 16;
3133dae1c09SAyan Kumar Halder 		afbc_superblock_width = 16;
3143dae1c09SAyan Kumar Halder 		break;
3153dae1c09SAyan Kumar Halder 	default:
3163dae1c09SAyan Kumar Halder 		DRM_DEBUG_KMS("AFBC superblock size is not supported\n");
3173dae1c09SAyan Kumar Halder 		return false;
3183dae1c09SAyan Kumar Halder 	}
3193dae1c09SAyan Kumar Halder 
3203dae1c09SAyan Kumar Halder 	info = drm_get_format_info(dev, mode_cmd);
3213dae1c09SAyan Kumar Halder 
3223dae1c09SAyan Kumar Halder 	n_superblocks = (mode_cmd->width / afbc_superblock_width) *
3233dae1c09SAyan Kumar Halder 		(mode_cmd->height / afbc_superblock_height);
3243dae1c09SAyan Kumar Halder 
3257834c577SAyan Kumar Halder 	bpp = malidp_format_get_bpp(info->format);
3267834c577SAyan Kumar Halder 
3277834c577SAyan Kumar Halder 	afbc_superblock_size = (bpp * afbc_superblock_width * afbc_superblock_height)
3287834c577SAyan Kumar Halder 				/ BITS_PER_BYTE;
3293dae1c09SAyan Kumar Halder 
330fd99bd8bSLiviu Dudau 	afbc_size = ALIGN(n_superblocks * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
331fd99bd8bSLiviu Dudau 	afbc_size += n_superblocks * ALIGN(afbc_superblock_size, AFBC_SUPERBLK_ALIGNMENT);
3323dae1c09SAyan Kumar Halder 
3337834c577SAyan Kumar Halder 	if ((mode_cmd->width * bpp) != (mode_cmd->pitches[0] * BITS_PER_BYTE)) {
3347834c577SAyan Kumar Halder 		DRM_DEBUG_KMS("Invalid value of (pitch * BITS_PER_BYTE) (=%u) "
3357834c577SAyan Kumar Halder 			      "should be same as width (=%u) * bpp (=%u)\n",
3367834c577SAyan Kumar Halder 			      (mode_cmd->pitches[0] * BITS_PER_BYTE),
3377834c577SAyan Kumar Halder 			      mode_cmd->width, bpp);
3383dae1c09SAyan Kumar Halder 		return false;
3393dae1c09SAyan Kumar Halder 	}
3403dae1c09SAyan Kumar Halder 
3413dae1c09SAyan Kumar Halder 	objs = drm_gem_object_lookup(file, mode_cmd->handles[0]);
3423dae1c09SAyan Kumar Halder 	if (!objs) {
3433dae1c09SAyan Kumar Halder 		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
3443dae1c09SAyan Kumar Halder 		return false;
3453dae1c09SAyan Kumar Halder 	}
3463dae1c09SAyan Kumar Halder 
3473dae1c09SAyan Kumar Halder 	if (objs->size < afbc_size) {
3483dae1c09SAyan Kumar Halder 		DRM_DEBUG_KMS("buffer size (%zu) too small for AFBC buffer size = %u\n",
3493dae1c09SAyan Kumar Halder 			      objs->size, afbc_size);
3503dae1c09SAyan Kumar Halder 		drm_gem_object_put_unlocked(objs);
3513dae1c09SAyan Kumar Halder 		return false;
3523dae1c09SAyan Kumar Halder 	}
3533dae1c09SAyan Kumar Halder 
3543dae1c09SAyan Kumar Halder 	drm_gem_object_put_unlocked(objs);
3553dae1c09SAyan Kumar Halder 
3563dae1c09SAyan Kumar Halder 	return true;
3573dae1c09SAyan Kumar Halder }
3583dae1c09SAyan Kumar Halder 
3593dae1c09SAyan Kumar Halder static bool
3603dae1c09SAyan Kumar Halder malidp_verify_afbc_framebuffer(struct drm_device *dev, struct drm_file *file,
3613dae1c09SAyan Kumar Halder 			       const struct drm_mode_fb_cmd2 *mode_cmd)
3623dae1c09SAyan Kumar Halder {
3633dae1c09SAyan Kumar Halder 	if (malidp_verify_afbc_framebuffer_caps(dev, mode_cmd))
3643dae1c09SAyan Kumar Halder 		return malidp_verify_afbc_framebuffer_size(dev, file, mode_cmd);
3653dae1c09SAyan Kumar Halder 
3663dae1c09SAyan Kumar Halder 	return false;
3673dae1c09SAyan Kumar Halder }
3683dae1c09SAyan Kumar Halder 
3693dae1c09SAyan Kumar Halder struct drm_framebuffer *
3703dae1c09SAyan Kumar Halder malidp_fb_create(struct drm_device *dev, struct drm_file *file,
3713dae1c09SAyan Kumar Halder 		 const struct drm_mode_fb_cmd2 *mode_cmd)
3723dae1c09SAyan Kumar Halder {
3733dae1c09SAyan Kumar Halder 	if (mode_cmd->modifier[0]) {
3743dae1c09SAyan Kumar Halder 		if (!malidp_verify_afbc_framebuffer(dev, file, mode_cmd))
3753dae1c09SAyan Kumar Halder 			return ERR_PTR(-EINVAL);
3763dae1c09SAyan Kumar Halder 	}
3773dae1c09SAyan Kumar Halder 
3783dae1c09SAyan Kumar Halder 	return drm_gem_fb_create(dev, file, mode_cmd);
3793dae1c09SAyan Kumar Halder }
3803dae1c09SAyan Kumar Halder 
381ad49f860SLiviu Dudau static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
3823dae1c09SAyan Kumar Halder 	.fb_create = malidp_fb_create,
383ad49f860SLiviu Dudau 	.atomic_check = drm_atomic_helper_check,
384ad49f860SLiviu Dudau 	.atomic_commit = drm_atomic_helper_commit,
385ad49f860SLiviu Dudau };
386ad49f860SLiviu Dudau 
387ad49f860SLiviu Dudau static int malidp_init(struct drm_device *drm)
388ad49f860SLiviu Dudau {
389ad49f860SLiviu Dudau 	int ret;
390ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
391ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
392ad49f860SLiviu Dudau 
393ad49f860SLiviu Dudau 	drm_mode_config_init(drm);
394ad49f860SLiviu Dudau 
395ad49f860SLiviu Dudau 	drm->mode_config.min_width = hwdev->min_line_size;
396ad49f860SLiviu Dudau 	drm->mode_config.min_height = hwdev->min_line_size;
397ad49f860SLiviu Dudau 	drm->mode_config.max_width = hwdev->max_line_size;
398ad49f860SLiviu Dudau 	drm->mode_config.max_height = hwdev->max_line_size;
399ad49f860SLiviu Dudau 	drm->mode_config.funcs = &malidp_mode_config_funcs;
400ad49f860SLiviu Dudau 	drm->mode_config.helper_private = &malidp_mode_config_helpers;
40125570b5eSAyan Kumar Halder 	drm->mode_config.allow_fb_modifiers = true;
402ad49f860SLiviu Dudau 
403ad49f860SLiviu Dudau 	ret = malidp_crtc_init(drm);
4048cbc5cafSBrian Starkey 	if (ret)
4058cbc5cafSBrian Starkey 		goto crtc_fail;
4068cbc5cafSBrian Starkey 
4078cbc5cafSBrian Starkey 	ret = malidp_mw_connector_init(drm);
4088cbc5cafSBrian Starkey 	if (ret)
4098cbc5cafSBrian Starkey 		goto crtc_fail;
410ad49f860SLiviu Dudau 
411ad49f860SLiviu Dudau 	return 0;
4128cbc5cafSBrian Starkey 
4138cbc5cafSBrian Starkey crtc_fail:
4148cbc5cafSBrian Starkey 	drm_mode_config_cleanup(drm);
4158cbc5cafSBrian Starkey 	return ret;
416ad49f860SLiviu Dudau }
417ad49f860SLiviu Dudau 
418de9c4810SBrian Starkey static void malidp_fini(struct drm_device *drm)
419de9c4810SBrian Starkey {
420de9c4810SBrian Starkey 	drm_mode_config_cleanup(drm);
421de9c4810SBrian Starkey }
422de9c4810SBrian Starkey 
423ad49f860SLiviu Dudau static int malidp_irq_init(struct platform_device *pdev)
424ad49f860SLiviu Dudau {
425ad49f860SLiviu Dudau 	int irq_de, irq_se, ret = 0;
426ad49f860SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(&pdev->dev);
42762862cfbSAyan Kumar Halder 	struct malidp_drm *malidp = drm->dev_private;
42862862cfbSAyan Kumar Halder 	struct malidp_hw_device *hwdev = malidp->dev;
429ad49f860SLiviu Dudau 
430ad49f860SLiviu Dudau 	/* fetch the interrupts from DT */
431ad49f860SLiviu Dudau 	irq_de = platform_get_irq_byname(pdev, "DE");
432ad49f860SLiviu Dudau 	if (irq_de < 0) {
433ad49f860SLiviu Dudau 		DRM_ERROR("no 'DE' IRQ specified!\n");
434ad49f860SLiviu Dudau 		return irq_de;
435ad49f860SLiviu Dudau 	}
436ad49f860SLiviu Dudau 	irq_se = platform_get_irq_byname(pdev, "SE");
437ad49f860SLiviu Dudau 	if (irq_se < 0) {
438ad49f860SLiviu Dudau 		DRM_ERROR("no 'SE' IRQ specified!\n");
439ad49f860SLiviu Dudau 		return irq_se;
440ad49f860SLiviu Dudau 	}
441ad49f860SLiviu Dudau 
442ad49f860SLiviu Dudau 	ret = malidp_de_irq_init(drm, irq_de);
443ad49f860SLiviu Dudau 	if (ret)
444ad49f860SLiviu Dudau 		return ret;
445ad49f860SLiviu Dudau 
446ad49f860SLiviu Dudau 	ret = malidp_se_irq_init(drm, irq_se);
447ad49f860SLiviu Dudau 	if (ret) {
44862862cfbSAyan Kumar Halder 		malidp_de_irq_fini(hwdev);
449ad49f860SLiviu Dudau 		return ret;
450ad49f860SLiviu Dudau 	}
451ad49f860SLiviu Dudau 
452ad49f860SLiviu Dudau 	return 0;
453ad49f860SLiviu Dudau }
454ad49f860SLiviu Dudau 
455d55f7e5dSDaniel Vetter DEFINE_DRM_GEM_CMA_FOPS(fops);
456ad49f860SLiviu Dudau 
4575ed4fdfaSLiviu Dudau static int malidp_dumb_create(struct drm_file *file_priv,
4585ed4fdfaSLiviu Dudau 			      struct drm_device *drm,
4595ed4fdfaSLiviu Dudau 			      struct drm_mode_create_dumb *args)
4605ed4fdfaSLiviu Dudau {
4615ed4fdfaSLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
4625ed4fdfaSLiviu Dudau 	/* allocate for the worst case scenario, i.e. rotated buffers */
4635ed4fdfaSLiviu Dudau 	u8 alignment = malidp_hw_get_pitch_align(malidp->dev, 1);
4645ed4fdfaSLiviu Dudau 
4655ed4fdfaSLiviu Dudau 	args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), alignment);
4665ed4fdfaSLiviu Dudau 
4675ed4fdfaSLiviu Dudau 	return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
4685ed4fdfaSLiviu Dudau }
4695ed4fdfaSLiviu Dudau 
470613c5c7fSAlexandru Gheorghe #ifdef CONFIG_DEBUG_FS
471613c5c7fSAlexandru Gheorghe 
472613c5c7fSAlexandru Gheorghe static void malidp_error_stats_init(struct malidp_error_stats *error_stats)
473613c5c7fSAlexandru Gheorghe {
474613c5c7fSAlexandru Gheorghe 	error_stats->num_errors = 0;
475613c5c7fSAlexandru Gheorghe 	error_stats->last_error_status = 0;
476613c5c7fSAlexandru Gheorghe 	error_stats->last_error_vblank = -1;
477613c5c7fSAlexandru Gheorghe }
478613c5c7fSAlexandru Gheorghe 
479613c5c7fSAlexandru Gheorghe void malidp_error(struct malidp_drm *malidp,
480613c5c7fSAlexandru Gheorghe 		  struct malidp_error_stats *error_stats, u32 status,
481613c5c7fSAlexandru Gheorghe 		  u64 vblank)
482613c5c7fSAlexandru Gheorghe {
483613c5c7fSAlexandru Gheorghe 	unsigned long irqflags;
484613c5c7fSAlexandru Gheorghe 
485613c5c7fSAlexandru Gheorghe 	spin_lock_irqsave(&malidp->errors_lock, irqflags);
486613c5c7fSAlexandru Gheorghe 	error_stats->last_error_status = status;
487613c5c7fSAlexandru Gheorghe 	error_stats->last_error_vblank = vblank;
488613c5c7fSAlexandru Gheorghe 	error_stats->num_errors++;
489613c5c7fSAlexandru Gheorghe 	spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
490613c5c7fSAlexandru Gheorghe }
491613c5c7fSAlexandru Gheorghe 
492613c5c7fSAlexandru Gheorghe void malidp_error_stats_dump(const char *prefix,
493613c5c7fSAlexandru Gheorghe 			     struct malidp_error_stats error_stats,
494613c5c7fSAlexandru Gheorghe 			     struct seq_file *m)
495613c5c7fSAlexandru Gheorghe {
496613c5c7fSAlexandru Gheorghe 	seq_printf(m, "[%s] num_errors : %d\n", prefix,
497613c5c7fSAlexandru Gheorghe 		   error_stats.num_errors);
498613c5c7fSAlexandru Gheorghe 	seq_printf(m, "[%s] last_error_status  : 0x%08x\n", prefix,
499613c5c7fSAlexandru Gheorghe 		   error_stats.last_error_status);
500613c5c7fSAlexandru Gheorghe 	seq_printf(m, "[%s] last_error_vblank : %lld\n", prefix,
501613c5c7fSAlexandru Gheorghe 		   error_stats.last_error_vblank);
502613c5c7fSAlexandru Gheorghe }
503613c5c7fSAlexandru Gheorghe 
504613c5c7fSAlexandru Gheorghe static int malidp_show_stats(struct seq_file *m, void *arg)
505613c5c7fSAlexandru Gheorghe {
506613c5c7fSAlexandru Gheorghe 	struct drm_device *drm = m->private;
507613c5c7fSAlexandru Gheorghe 	struct malidp_drm *malidp = drm->dev_private;
508613c5c7fSAlexandru Gheorghe 	unsigned long irqflags;
509613c5c7fSAlexandru Gheorghe 	struct malidp_error_stats de_errors, se_errors;
510613c5c7fSAlexandru Gheorghe 
511613c5c7fSAlexandru Gheorghe 	spin_lock_irqsave(&malidp->errors_lock, irqflags);
512613c5c7fSAlexandru Gheorghe 	de_errors = malidp->de_errors;
513613c5c7fSAlexandru Gheorghe 	se_errors = malidp->se_errors;
514613c5c7fSAlexandru Gheorghe 	spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
515613c5c7fSAlexandru Gheorghe 	malidp_error_stats_dump("DE", de_errors, m);
516613c5c7fSAlexandru Gheorghe 	malidp_error_stats_dump("SE", se_errors, m);
517613c5c7fSAlexandru Gheorghe 	return 0;
518613c5c7fSAlexandru Gheorghe }
519613c5c7fSAlexandru Gheorghe 
520613c5c7fSAlexandru Gheorghe static int malidp_debugfs_open(struct inode *inode, struct file *file)
521613c5c7fSAlexandru Gheorghe {
522613c5c7fSAlexandru Gheorghe 	return single_open(file, malidp_show_stats, inode->i_private);
523613c5c7fSAlexandru Gheorghe }
524613c5c7fSAlexandru Gheorghe 
525613c5c7fSAlexandru Gheorghe static ssize_t malidp_debugfs_write(struct file *file, const char __user *ubuf,
526613c5c7fSAlexandru Gheorghe 				    size_t len, loff_t *offp)
527613c5c7fSAlexandru Gheorghe {
528613c5c7fSAlexandru Gheorghe 	struct seq_file *m = file->private_data;
529613c5c7fSAlexandru Gheorghe 	struct drm_device *drm = m->private;
530613c5c7fSAlexandru Gheorghe 	struct malidp_drm *malidp = drm->dev_private;
531613c5c7fSAlexandru Gheorghe 	unsigned long irqflags;
532613c5c7fSAlexandru Gheorghe 
533613c5c7fSAlexandru Gheorghe 	spin_lock_irqsave(&malidp->errors_lock, irqflags);
534613c5c7fSAlexandru Gheorghe 	malidp_error_stats_init(&malidp->de_errors);
535613c5c7fSAlexandru Gheorghe 	malidp_error_stats_init(&malidp->se_errors);
536613c5c7fSAlexandru Gheorghe 	spin_unlock_irqrestore(&malidp->errors_lock, irqflags);
537613c5c7fSAlexandru Gheorghe 	return len;
538613c5c7fSAlexandru Gheorghe }
539613c5c7fSAlexandru Gheorghe 
540613c5c7fSAlexandru Gheorghe static const struct file_operations malidp_debugfs_fops = {
541613c5c7fSAlexandru Gheorghe 	.owner = THIS_MODULE,
542613c5c7fSAlexandru Gheorghe 	.open = malidp_debugfs_open,
543613c5c7fSAlexandru Gheorghe 	.read = seq_read,
544613c5c7fSAlexandru Gheorghe 	.write = malidp_debugfs_write,
545613c5c7fSAlexandru Gheorghe 	.llseek = seq_lseek,
546613c5c7fSAlexandru Gheorghe 	.release = single_release,
547613c5c7fSAlexandru Gheorghe };
548613c5c7fSAlexandru Gheorghe 
549613c5c7fSAlexandru Gheorghe static int malidp_debugfs_init(struct drm_minor *minor)
550613c5c7fSAlexandru Gheorghe {
551613c5c7fSAlexandru Gheorghe 	struct malidp_drm *malidp = minor->dev->dev_private;
552613c5c7fSAlexandru Gheorghe 	struct dentry *dentry = NULL;
553613c5c7fSAlexandru Gheorghe 
554613c5c7fSAlexandru Gheorghe 	malidp_error_stats_init(&malidp->de_errors);
555613c5c7fSAlexandru Gheorghe 	malidp_error_stats_init(&malidp->se_errors);
556613c5c7fSAlexandru Gheorghe 	spin_lock_init(&malidp->errors_lock);
557613c5c7fSAlexandru Gheorghe 	dentry = debugfs_create_file("debug",
558613c5c7fSAlexandru Gheorghe 				     S_IRUGO | S_IWUSR,
559613c5c7fSAlexandru Gheorghe 				     minor->debugfs_root, minor->dev,
560613c5c7fSAlexandru Gheorghe 				     &malidp_debugfs_fops);
561613c5c7fSAlexandru Gheorghe 	if (!dentry) {
562613c5c7fSAlexandru Gheorghe 		DRM_ERROR("Cannot create debug file\n");
563613c5c7fSAlexandru Gheorghe 		return -ENOMEM;
564613c5c7fSAlexandru Gheorghe 	}
565613c5c7fSAlexandru Gheorghe 	return 0;
566613c5c7fSAlexandru Gheorghe }
567613c5c7fSAlexandru Gheorghe 
568613c5c7fSAlexandru Gheorghe #endif //CONFIG_DEBUG_FS
569613c5c7fSAlexandru Gheorghe 
570ad49f860SLiviu Dudau static struct drm_driver malidp_driver = {
5710424fdafSDaniel Vetter 	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
572ad49f860SLiviu Dudau 	.gem_free_object_unlocked = drm_gem_cma_free_object,
573ad49f860SLiviu Dudau 	.gem_vm_ops = &drm_gem_cma_vm_ops,
5745ed4fdfaSLiviu Dudau 	.dumb_create = malidp_dumb_create,
575ad49f860SLiviu Dudau 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
576ad49f860SLiviu Dudau 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
577ad49f860SLiviu Dudau 	.gem_prime_export = drm_gem_prime_export,
578ad49f860SLiviu Dudau 	.gem_prime_import = drm_gem_prime_import,
579ad49f860SLiviu Dudau 	.gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
580ad49f860SLiviu Dudau 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
581ad49f860SLiviu Dudau 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
582ad49f860SLiviu Dudau 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
583ad49f860SLiviu Dudau 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
584613c5c7fSAlexandru Gheorghe #ifdef CONFIG_DEBUG_FS
585613c5c7fSAlexandru Gheorghe 	.debugfs_init = malidp_debugfs_init,
586613c5c7fSAlexandru Gheorghe #endif
587ad49f860SLiviu Dudau 	.fops = &fops,
588ad49f860SLiviu Dudau 	.name = "mali-dp",
589ad49f860SLiviu Dudau 	.desc = "ARM Mali Display Processor driver",
590ad49f860SLiviu Dudau 	.date = "20160106",
591ad49f860SLiviu Dudau 	.major = 1,
592ad49f860SLiviu Dudau 	.minor = 0,
593ad49f860SLiviu Dudau };
594ad49f860SLiviu Dudau 
595ad49f860SLiviu Dudau static const struct of_device_id  malidp_drm_of_match[] = {
596ad49f860SLiviu Dudau 	{
597ad49f860SLiviu Dudau 		.compatible = "arm,mali-dp500",
598ad49f860SLiviu Dudau 		.data = &malidp_device[MALIDP_500]
599ad49f860SLiviu Dudau 	},
600ad49f860SLiviu Dudau 	{
601ad49f860SLiviu Dudau 		.compatible = "arm,mali-dp550",
602ad49f860SLiviu Dudau 		.data = &malidp_device[MALIDP_550]
603ad49f860SLiviu Dudau 	},
604ad49f860SLiviu Dudau 	{
605ad49f860SLiviu Dudau 		.compatible = "arm,mali-dp650",
606ad49f860SLiviu Dudau 		.data = &malidp_device[MALIDP_650]
607ad49f860SLiviu Dudau 	},
608ad49f860SLiviu Dudau 	{},
609ad49f860SLiviu Dudau };
610ad49f860SLiviu Dudau MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
611ad49f860SLiviu Dudau 
612592d8c8cSMihail Atanassov static bool malidp_is_compatible_hw_id(struct malidp_hw_device *hwdev,
613592d8c8cSMihail Atanassov 				       const struct of_device_id *dev_id)
614592d8c8cSMihail Atanassov {
615592d8c8cSMihail Atanassov 	u32 core_id;
616592d8c8cSMihail Atanassov 	const char *compatstr_dp500 = "arm,mali-dp500";
617592d8c8cSMihail Atanassov 	bool is_dp500;
618592d8c8cSMihail Atanassov 	bool dt_is_dp500;
619592d8c8cSMihail Atanassov 
620592d8c8cSMihail Atanassov 	/*
621592d8c8cSMihail Atanassov 	 * The DP500 CORE_ID register is in a different location, so check it
622592d8c8cSMihail Atanassov 	 * first. If the product id field matches, then this is DP500, otherwise
623592d8c8cSMihail Atanassov 	 * check the DP550/650 CORE_ID register.
624592d8c8cSMihail Atanassov 	 */
625592d8c8cSMihail Atanassov 	core_id = malidp_hw_read(hwdev, MALIDP500_DC_BASE + MALIDP_DE_CORE_ID);
626592d8c8cSMihail Atanassov 	/* Offset 0x18 will never read 0x500 on products other than DP500. */
627592d8c8cSMihail Atanassov 	is_dp500 = (MALIDP_PRODUCT_ID(core_id) == 0x500);
628592d8c8cSMihail Atanassov 	dt_is_dp500 = strnstr(dev_id->compatible, compatstr_dp500,
629592d8c8cSMihail Atanassov 			      sizeof(dev_id->compatible)) != NULL;
630592d8c8cSMihail Atanassov 	if (is_dp500 != dt_is_dp500) {
631592d8c8cSMihail Atanassov 		DRM_ERROR("Device-tree expects %s, but hardware %s DP500.\n",
632592d8c8cSMihail Atanassov 			  dev_id->compatible, is_dp500 ? "is" : "is not");
633592d8c8cSMihail Atanassov 		return false;
634592d8c8cSMihail Atanassov 	} else if (!dt_is_dp500) {
635592d8c8cSMihail Atanassov 		u16 product_id;
636592d8c8cSMihail Atanassov 		char buf[32];
637592d8c8cSMihail Atanassov 
638592d8c8cSMihail Atanassov 		core_id = malidp_hw_read(hwdev,
639592d8c8cSMihail Atanassov 					 MALIDP550_DC_BASE + MALIDP_DE_CORE_ID);
640592d8c8cSMihail Atanassov 		product_id = MALIDP_PRODUCT_ID(core_id);
641592d8c8cSMihail Atanassov 		snprintf(buf, sizeof(buf), "arm,mali-dp%X", product_id);
642592d8c8cSMihail Atanassov 		if (!strnstr(dev_id->compatible, buf,
643592d8c8cSMihail Atanassov 			     sizeof(dev_id->compatible))) {
644592d8c8cSMihail Atanassov 			DRM_ERROR("Device-tree expects %s, but hardware is DP%03X.\n",
645592d8c8cSMihail Atanassov 				  dev_id->compatible, product_id);
646592d8c8cSMihail Atanassov 			return false;
647592d8c8cSMihail Atanassov 		}
648592d8c8cSMihail Atanassov 	}
649592d8c8cSMihail Atanassov 	return true;
650592d8c8cSMihail Atanassov }
651592d8c8cSMihail Atanassov 
6524d6000edSMihail Atanassov static bool malidp_has_sufficient_address_space(const struct resource *res,
6534d6000edSMihail Atanassov 						const struct of_device_id *dev_id)
6544d6000edSMihail Atanassov {
6554d6000edSMihail Atanassov 	resource_size_t res_size = resource_size(res);
6564d6000edSMihail Atanassov 	const char *compatstr_dp500 = "arm,mali-dp500";
6574d6000edSMihail Atanassov 
6584d6000edSMihail Atanassov 	if (!strnstr(dev_id->compatible, compatstr_dp500,
6594d6000edSMihail Atanassov 		     sizeof(dev_id->compatible)))
6604d6000edSMihail Atanassov 		return res_size >= MALIDP550_ADDR_SPACE_SIZE;
6614d6000edSMihail Atanassov 	else if (res_size < MALIDP500_ADDR_SPACE_SIZE)
6624d6000edSMihail Atanassov 		return false;
6634d6000edSMihail Atanassov 	return true;
6644d6000edSMihail Atanassov }
6654d6000edSMihail Atanassov 
66650c7512fSLiviu Dudau static ssize_t core_id_show(struct device *dev, struct device_attribute *attr,
66750c7512fSLiviu Dudau 			    char *buf)
66850c7512fSLiviu Dudau {
66950c7512fSLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
67050c7512fSLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
67150c7512fSLiviu Dudau 
67250c7512fSLiviu Dudau 	return snprintf(buf, PAGE_SIZE, "%08x\n", malidp->core_id);
67350c7512fSLiviu Dudau }
67450c7512fSLiviu Dudau 
67550c7512fSLiviu Dudau DEVICE_ATTR_RO(core_id);
67650c7512fSLiviu Dudau 
67750c7512fSLiviu Dudau static int malidp_init_sysfs(struct device *dev)
67850c7512fSLiviu Dudau {
67950c7512fSLiviu Dudau 	int ret = device_create_file(dev, &dev_attr_core_id);
68050c7512fSLiviu Dudau 
68150c7512fSLiviu Dudau 	if (ret)
68250c7512fSLiviu Dudau 		DRM_ERROR("failed to create device file for core_id\n");
68350c7512fSLiviu Dudau 
68450c7512fSLiviu Dudau 	return ret;
68550c7512fSLiviu Dudau }
68650c7512fSLiviu Dudau 
68750c7512fSLiviu Dudau static void malidp_fini_sysfs(struct device *dev)
68850c7512fSLiviu Dudau {
68950c7512fSLiviu Dudau 	device_remove_file(dev, &dev_attr_core_id);
69050c7512fSLiviu Dudau }
69150c7512fSLiviu Dudau 
692ad49f860SLiviu Dudau #define MAX_OUTPUT_CHANNELS	3
693ad49f860SLiviu Dudau 
69485f64218SLiviu Dudau static int malidp_runtime_pm_suspend(struct device *dev)
69585f64218SLiviu Dudau {
69685f64218SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
69785f64218SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
69885f64218SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
69985f64218SLiviu Dudau 
70085f64218SLiviu Dudau 	/* we can only suspend if the hardware is in config mode */
701a6993b21SLiviu Dudau 	WARN_ON(!hwdev->hw->in_config_mode(hwdev));
70285f64218SLiviu Dudau 
703fbcc454eSAyan Kumar Halder 	malidp_se_irq_fini(hwdev);
704fbcc454eSAyan Kumar Halder 	malidp_de_irq_fini(hwdev);
70585f64218SLiviu Dudau 	hwdev->pm_suspended = true;
70685f64218SLiviu Dudau 	clk_disable_unprepare(hwdev->mclk);
70785f64218SLiviu Dudau 	clk_disable_unprepare(hwdev->aclk);
70885f64218SLiviu Dudau 	clk_disable_unprepare(hwdev->pclk);
70985f64218SLiviu Dudau 
71085f64218SLiviu Dudau 	return 0;
71185f64218SLiviu Dudau }
71285f64218SLiviu Dudau 
71385f64218SLiviu Dudau static int malidp_runtime_pm_resume(struct device *dev)
71485f64218SLiviu Dudau {
71585f64218SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
71685f64218SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
71785f64218SLiviu Dudau 	struct malidp_hw_device *hwdev = malidp->dev;
71885f64218SLiviu Dudau 
71985f64218SLiviu Dudau 	clk_prepare_enable(hwdev->pclk);
72085f64218SLiviu Dudau 	clk_prepare_enable(hwdev->aclk);
72185f64218SLiviu Dudau 	clk_prepare_enable(hwdev->mclk);
72285f64218SLiviu Dudau 	hwdev->pm_suspended = false;
723fbcc454eSAyan Kumar Halder 	malidp_de_irq_hw_init(hwdev);
724fbcc454eSAyan Kumar Halder 	malidp_se_irq_hw_init(hwdev);
72585f64218SLiviu Dudau 
72685f64218SLiviu Dudau 	return 0;
72785f64218SLiviu Dudau }
72885f64218SLiviu Dudau 
729ad49f860SLiviu Dudau static int malidp_bind(struct device *dev)
730ad49f860SLiviu Dudau {
731ad49f860SLiviu Dudau 	struct resource *res;
732ad49f860SLiviu Dudau 	struct drm_device *drm;
733ad49f860SLiviu Dudau 	struct malidp_drm *malidp;
734ad49f860SLiviu Dudau 	struct malidp_hw_device *hwdev;
735ad49f860SLiviu Dudau 	struct platform_device *pdev = to_platform_device(dev);
736592d8c8cSMihail Atanassov 	struct of_device_id const *dev_id;
7372e012e76SAlexandru Gheorghe 	struct drm_encoder *encoder;
738ad49f860SLiviu Dudau 	/* number of lines for the R, G and B output */
739ad49f860SLiviu Dudau 	u8 output_width[MAX_OUTPUT_CHANNELS];
740ad49f860SLiviu Dudau 	int ret = 0, i;
741ad49f860SLiviu Dudau 	u32 version, out_depth = 0;
742ad49f860SLiviu Dudau 
743ad49f860SLiviu Dudau 	malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
744ad49f860SLiviu Dudau 	if (!malidp)
745ad49f860SLiviu Dudau 		return -ENOMEM;
746ad49f860SLiviu Dudau 
747ad49f860SLiviu Dudau 	hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
748ad49f860SLiviu Dudau 	if (!hwdev)
749ad49f860SLiviu Dudau 		return -ENOMEM;
750ad49f860SLiviu Dudau 
751a6993b21SLiviu Dudau 	hwdev->hw = (struct malidp_hw *)of_device_get_match_data(dev);
752ad49f860SLiviu Dudau 	malidp->dev = hwdev;
753ad49f860SLiviu Dudau 
754ad49f860SLiviu Dudau 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
755ad49f860SLiviu Dudau 	hwdev->regs = devm_ioremap_resource(dev, res);
7561a9d71f8SWei Yongjun 	if (IS_ERR(hwdev->regs))
757ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->regs);
758ad49f860SLiviu Dudau 
759ad49f860SLiviu Dudau 	hwdev->pclk = devm_clk_get(dev, "pclk");
760ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->pclk))
761ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->pclk);
762ad49f860SLiviu Dudau 
763ad49f860SLiviu Dudau 	hwdev->aclk = devm_clk_get(dev, "aclk");
764ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->aclk))
765ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->aclk);
766ad49f860SLiviu Dudau 
767ad49f860SLiviu Dudau 	hwdev->mclk = devm_clk_get(dev, "mclk");
768ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->mclk))
769ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->mclk);
770ad49f860SLiviu Dudau 
771ad49f860SLiviu Dudau 	hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
772ad49f860SLiviu Dudau 	if (IS_ERR(hwdev->pxlclk))
773ad49f860SLiviu Dudau 		return PTR_ERR(hwdev->pxlclk);
774ad49f860SLiviu Dudau 
775ad49f860SLiviu Dudau 	/* Get the optional framebuffer memory resource */
776ad49f860SLiviu Dudau 	ret = of_reserved_mem_device_init(dev);
777ad49f860SLiviu Dudau 	if (ret && ret != -ENODEV)
778ad49f860SLiviu Dudau 		return ret;
779ad49f860SLiviu Dudau 
780ad49f860SLiviu Dudau 	drm = drm_dev_alloc(&malidp_driver, dev);
7810f288605STom Gundersen 	if (IS_ERR(drm)) {
7820f288605STom Gundersen 		ret = PTR_ERR(drm);
783ad49f860SLiviu Dudau 		goto alloc_fail;
784ad49f860SLiviu Dudau 	}
785ad49f860SLiviu Dudau 
78685f64218SLiviu Dudau 	drm->dev_private = malidp;
78785f64218SLiviu Dudau 	dev_set_drvdata(dev, drm);
78885f64218SLiviu Dudau 
78985f64218SLiviu Dudau 	/* Enable power management */
79085f64218SLiviu Dudau 	pm_runtime_enable(dev);
79185f64218SLiviu Dudau 
79285f64218SLiviu Dudau 	/* Resume device to enable the clocks */
79385f64218SLiviu Dudau 	if (pm_runtime_enabled(dev))
79485f64218SLiviu Dudau 		pm_runtime_get_sync(dev);
79585f64218SLiviu Dudau 	else
79685f64218SLiviu Dudau 		malidp_runtime_pm_resume(dev);
797ad49f860SLiviu Dudau 
798592d8c8cSMihail Atanassov 	dev_id = of_match_device(malidp_drm_of_match, dev);
799592d8c8cSMihail Atanassov 	if (!dev_id) {
800592d8c8cSMihail Atanassov 		ret = -EINVAL;
801592d8c8cSMihail Atanassov 		goto query_hw_fail;
802592d8c8cSMihail Atanassov 	}
803592d8c8cSMihail Atanassov 
8044d6000edSMihail Atanassov 	if (!malidp_has_sufficient_address_space(res, dev_id)) {
8054d6000edSMihail Atanassov 		DRM_ERROR("Insufficient address space in device-tree.\n");
8064d6000edSMihail Atanassov 		ret = -EINVAL;
8074d6000edSMihail Atanassov 		goto query_hw_fail;
8084d6000edSMihail Atanassov 	}
8094d6000edSMihail Atanassov 
810592d8c8cSMihail Atanassov 	if (!malidp_is_compatible_hw_id(hwdev, dev_id)) {
811592d8c8cSMihail Atanassov 		ret = -EINVAL;
812592d8c8cSMihail Atanassov 		goto query_hw_fail;
813592d8c8cSMihail Atanassov 	}
814592d8c8cSMihail Atanassov 
815a6993b21SLiviu Dudau 	ret = hwdev->hw->query_hw(hwdev);
816ad49f860SLiviu Dudau 	if (ret) {
817ad49f860SLiviu Dudau 		DRM_ERROR("Invalid HW configuration\n");
818ad49f860SLiviu Dudau 		goto query_hw_fail;
819ad49f860SLiviu Dudau 	}
820ad49f860SLiviu Dudau 
821a6993b21SLiviu Dudau 	version = malidp_hw_read(hwdev, hwdev->hw->map.dc_base + MALIDP_DE_CORE_ID);
822ad49f860SLiviu Dudau 	DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
823ad49f860SLiviu Dudau 		 (version >> 12) & 0xf, (version >> 8) & 0xf);
824ad49f860SLiviu Dudau 
82550c7512fSLiviu Dudau 	malidp->core_id = version;
82650c7512fSLiviu Dudau 
827ad49f860SLiviu Dudau 	/* set the number of lines used for output of RGB data */
828ad49f860SLiviu Dudau 	ret = of_property_read_u8_array(dev->of_node,
829ad49f860SLiviu Dudau 					"arm,malidp-output-port-lines",
830ad49f860SLiviu Dudau 					output_width, MAX_OUTPUT_CHANNELS);
831ad49f860SLiviu Dudau 	if (ret)
832ad49f860SLiviu Dudau 		goto query_hw_fail;
833ad49f860SLiviu Dudau 
834ad49f860SLiviu Dudau 	for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
835ad49f860SLiviu Dudau 		out_depth = (out_depth << 8) | (output_width[i] & 0xf);
836a6993b21SLiviu Dudau 	malidp_hw_write(hwdev, out_depth, hwdev->hw->map.out_depth_base);
837f877006dSAyan Kumar Halder 	hwdev->output_color_depth = out_depth;
838ad49f860SLiviu Dudau 
8391cb3cbe7SLiviu Dudau 	atomic_set(&malidp->config_valid, MALIDP_CONFIG_VALID_INIT);
840ad49f860SLiviu Dudau 	init_waitqueue_head(&malidp->wq);
841ad49f860SLiviu Dudau 
842ad49f860SLiviu Dudau 	ret = malidp_init(drm);
843ad49f860SLiviu Dudau 	if (ret < 0)
84485f64218SLiviu Dudau 		goto query_hw_fail;
845ad49f860SLiviu Dudau 
84650c7512fSLiviu Dudau 	ret = malidp_init_sysfs(dev);
84750c7512fSLiviu Dudau 	if (ret)
84850c7512fSLiviu Dudau 		goto init_fail;
84950c7512fSLiviu Dudau 
850ad49f860SLiviu Dudau 	/* Set the CRTC's port so that the encoder component can find it */
85186418f90SRob Herring 	malidp->crtc.port = of_graph_get_port_by_id(dev->of_node, 0);
852ad49f860SLiviu Dudau 
853ad49f860SLiviu Dudau 	ret = component_bind_all(dev, drm);
854ad49f860SLiviu Dudau 	if (ret) {
855ad49f860SLiviu Dudau 		DRM_ERROR("Failed to bind all components\n");
856ad49f860SLiviu Dudau 		goto bind_fail;
857ad49f860SLiviu Dudau 	}
858ad49f860SLiviu Dudau 
8592e012e76SAlexandru Gheorghe 	/* We expect to have a maximum of two encoders one for the actual
8602e012e76SAlexandru Gheorghe 	 * display and a virtual one for the writeback connector
8612e012e76SAlexandru Gheorghe 	 */
8622e012e76SAlexandru Gheorghe 	WARN_ON(drm->mode_config.num_encoder > 2);
8632e012e76SAlexandru Gheorghe 	list_for_each_entry(encoder, &drm->mode_config.encoder_list, head) {
8642e012e76SAlexandru Gheorghe 		encoder->possible_clones =
8652e012e76SAlexandru Gheorghe 				(1 << drm->mode_config.num_encoder) -  1;
8662e012e76SAlexandru Gheorghe 	}
8672e012e76SAlexandru Gheorghe 
868ad49f860SLiviu Dudau 	ret = malidp_irq_init(pdev);
869ad49f860SLiviu Dudau 	if (ret < 0)
870ad49f860SLiviu Dudau 		goto irq_init_fail;
871ad49f860SLiviu Dudau 
872a6a7b9a2SLiviu Dudau 	drm->irq_enabled = true;
873a6a7b9a2SLiviu Dudau 
874ad49f860SLiviu Dudau 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
875cabce634SAlexandru Gheorghe 	drm_crtc_vblank_reset(&malidp->crtc);
876ad49f860SLiviu Dudau 	if (ret < 0) {
877ad49f860SLiviu Dudau 		DRM_ERROR("failed to initialise vblank\n");
878ad49f860SLiviu Dudau 		goto vblank_fail;
879ad49f860SLiviu Dudau 	}
88085f64218SLiviu Dudau 	pm_runtime_put(dev);
881ad49f860SLiviu Dudau 
882ad49f860SLiviu Dudau 	drm_mode_config_reset(drm);
883ad49f860SLiviu Dudau 
884ad49f860SLiviu Dudau 	drm_kms_helper_poll_init(drm);
88590731c24SBrian Starkey 
88690731c24SBrian Starkey 	ret = drm_dev_register(drm, 0);
88790731c24SBrian Starkey 	if (ret)
88890731c24SBrian Starkey 		goto register_fail;
88990731c24SBrian Starkey 
89095958098SNoralf Trønnes 	drm_fbdev_generic_setup(drm, 32);
89195958098SNoralf Trønnes 
892ad49f860SLiviu Dudau 	return 0;
893ad49f860SLiviu Dudau 
89490731c24SBrian Starkey register_fail:
89585f64218SLiviu Dudau 	drm_kms_helper_poll_fini(drm);
89685f64218SLiviu Dudau 	pm_runtime_get_sync(dev);
897ad49f860SLiviu Dudau vblank_fail:
89862862cfbSAyan Kumar Halder 	malidp_se_irq_fini(hwdev);
89962862cfbSAyan Kumar Halder 	malidp_de_irq_fini(hwdev);
900a6a7b9a2SLiviu Dudau 	drm->irq_enabled = false;
901ad49f860SLiviu Dudau irq_init_fail:
902109c4d18SAyan Kumar Halder 	drm_atomic_helper_shutdown(drm);
903ad49f860SLiviu Dudau 	component_unbind_all(dev, drm);
904ad49f860SLiviu Dudau bind_fail:
9053c31760eSBrian Starkey 	of_node_put(malidp->crtc.port);
9063c31760eSBrian Starkey 	malidp->crtc.port = NULL;
90750c7512fSLiviu Dudau init_fail:
90850c7512fSLiviu Dudau 	malidp_fini_sysfs(dev);
909de9c4810SBrian Starkey 	malidp_fini(drm);
91085f64218SLiviu Dudau query_hw_fail:
91185f64218SLiviu Dudau 	pm_runtime_put(dev);
91285f64218SLiviu Dudau 	if (pm_runtime_enabled(dev))
91385f64218SLiviu Dudau 		pm_runtime_disable(dev);
91485f64218SLiviu Dudau 	else
91585f64218SLiviu Dudau 		malidp_runtime_pm_suspend(dev);
916ad49f860SLiviu Dudau 	drm->dev_private = NULL;
917ad49f860SLiviu Dudau 	dev_set_drvdata(dev, NULL);
9180970d7a2SSrishti Sharma 	drm_dev_put(drm);
919ad49f860SLiviu Dudau alloc_fail:
920ad49f860SLiviu Dudau 	of_reserved_mem_device_release(dev);
921ad49f860SLiviu Dudau 
922ad49f860SLiviu Dudau 	return ret;
923ad49f860SLiviu Dudau }
924ad49f860SLiviu Dudau 
925ad49f860SLiviu Dudau static void malidp_unbind(struct device *dev)
926ad49f860SLiviu Dudau {
927ad49f860SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
928ad49f860SLiviu Dudau 	struct malidp_drm *malidp = drm->dev_private;
92962862cfbSAyan Kumar Halder 	struct malidp_hw_device *hwdev = malidp->dev;
930ad49f860SLiviu Dudau 
93190731c24SBrian Starkey 	drm_dev_unregister(drm);
932ad49f860SLiviu Dudau 	drm_kms_helper_poll_fini(drm);
93385f64218SLiviu Dudau 	pm_runtime_get_sync(dev);
93457085dcaSLiviu Dudau 	drm_crtc_vblank_off(&malidp->crtc);
93562862cfbSAyan Kumar Halder 	malidp_se_irq_fini(hwdev);
93662862cfbSAyan Kumar Halder 	malidp_de_irq_fini(hwdev);
93757085dcaSLiviu Dudau 	drm->irq_enabled = false;
938109c4d18SAyan Kumar Halder 	drm_atomic_helper_shutdown(drm);
939ad49f860SLiviu Dudau 	component_unbind_all(dev, drm);
9403c31760eSBrian Starkey 	of_node_put(malidp->crtc.port);
9413c31760eSBrian Starkey 	malidp->crtc.port = NULL;
94250c7512fSLiviu Dudau 	malidp_fini_sysfs(dev);
943de9c4810SBrian Starkey 	malidp_fini(drm);
94485f64218SLiviu Dudau 	pm_runtime_put(dev);
94585f64218SLiviu Dudau 	if (pm_runtime_enabled(dev))
94685f64218SLiviu Dudau 		pm_runtime_disable(dev);
94785f64218SLiviu Dudau 	else
94885f64218SLiviu Dudau 		malidp_runtime_pm_suspend(dev);
949ad49f860SLiviu Dudau 	drm->dev_private = NULL;
950ad49f860SLiviu Dudau 	dev_set_drvdata(dev, NULL);
9510970d7a2SSrishti Sharma 	drm_dev_put(drm);
952ad49f860SLiviu Dudau 	of_reserved_mem_device_release(dev);
953ad49f860SLiviu Dudau }
954ad49f860SLiviu Dudau 
955ad49f860SLiviu Dudau static const struct component_master_ops malidp_master_ops = {
956ad49f860SLiviu Dudau 	.bind = malidp_bind,
957ad49f860SLiviu Dudau 	.unbind = malidp_unbind,
958ad49f860SLiviu Dudau };
959ad49f860SLiviu Dudau 
960ad49f860SLiviu Dudau static int malidp_compare_dev(struct device *dev, void *data)
961ad49f860SLiviu Dudau {
962ad49f860SLiviu Dudau 	struct device_node *np = data;
963ad49f860SLiviu Dudau 
964ad49f860SLiviu Dudau 	return dev->of_node == np;
965ad49f860SLiviu Dudau }
966ad49f860SLiviu Dudau 
967ad49f860SLiviu Dudau static int malidp_platform_probe(struct platform_device *pdev)
968ad49f860SLiviu Dudau {
96986418f90SRob Herring 	struct device_node *port;
970ad49f860SLiviu Dudau 	struct component_match *match = NULL;
971ad49f860SLiviu Dudau 
972ad49f860SLiviu Dudau 	if (!pdev->dev.of_node)
973ad49f860SLiviu Dudau 		return -ENODEV;
974ad49f860SLiviu Dudau 
975ad49f860SLiviu Dudau 	/* there is only one output port inside each device, find it */
97686418f90SRob Herring 	port = of_graph_get_remote_node(pdev->dev.of_node, 0, 0);
97786418f90SRob Herring 	if (!port)
978ad49f860SLiviu Dudau 		return -ENODEV;
979ad49f860SLiviu Dudau 
98097ac0e47SRussell King 	drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
98197ac0e47SRussell King 				   port);
98297ac0e47SRussell King 	of_node_put(port);
983ad49f860SLiviu Dudau 	return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
984ad49f860SLiviu Dudau 					       match);
985ad49f860SLiviu Dudau }
986ad49f860SLiviu Dudau 
987ad49f860SLiviu Dudau static int malidp_platform_remove(struct platform_device *pdev)
988ad49f860SLiviu Dudau {
989ad49f860SLiviu Dudau 	component_master_del(&pdev->dev, &malidp_master_ops);
990ad49f860SLiviu Dudau 	return 0;
991ad49f860SLiviu Dudau }
992ad49f860SLiviu Dudau 
99385f64218SLiviu Dudau static int __maybe_unused malidp_pm_suspend(struct device *dev)
99485f64218SLiviu Dudau {
99585f64218SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
99685f64218SLiviu Dudau 
997194b8799SNoralf Trønnes 	return drm_mode_config_helper_suspend(drm);
99885f64218SLiviu Dudau }
99985f64218SLiviu Dudau 
100085f64218SLiviu Dudau static int __maybe_unused malidp_pm_resume(struct device *dev)
100185f64218SLiviu Dudau {
100285f64218SLiviu Dudau 	struct drm_device *drm = dev_get_drvdata(dev);
100385f64218SLiviu Dudau 
1004194b8799SNoralf Trønnes 	drm_mode_config_helper_resume(drm);
100585f64218SLiviu Dudau 
100685f64218SLiviu Dudau 	return 0;
100785f64218SLiviu Dudau }
100885f64218SLiviu Dudau 
1009e368fc75SAyan Kumar Halder static int __maybe_unused malidp_pm_suspend_late(struct device *dev)
1010e368fc75SAyan Kumar Halder {
1011e368fc75SAyan Kumar Halder 	if (!pm_runtime_status_suspended(dev)) {
1012e368fc75SAyan Kumar Halder 		malidp_runtime_pm_suspend(dev);
1013e368fc75SAyan Kumar Halder 		pm_runtime_set_suspended(dev);
1014e368fc75SAyan Kumar Halder 	}
1015e368fc75SAyan Kumar Halder 	return 0;
1016e368fc75SAyan Kumar Halder }
1017e368fc75SAyan Kumar Halder 
1018e368fc75SAyan Kumar Halder static int __maybe_unused malidp_pm_resume_early(struct device *dev)
1019e368fc75SAyan Kumar Halder {
1020e368fc75SAyan Kumar Halder 	malidp_runtime_pm_resume(dev);
1021e368fc75SAyan Kumar Halder 	pm_runtime_set_active(dev);
1022e368fc75SAyan Kumar Halder 	return 0;
1023e368fc75SAyan Kumar Halder }
1024e368fc75SAyan Kumar Halder 
102585f64218SLiviu Dudau static const struct dev_pm_ops malidp_pm_ops = {
102685f64218SLiviu Dudau 	SET_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend, malidp_pm_resume) \
1027e368fc75SAyan Kumar Halder 	SET_LATE_SYSTEM_SLEEP_PM_OPS(malidp_pm_suspend_late, malidp_pm_resume_early) \
102885f64218SLiviu Dudau 	SET_RUNTIME_PM_OPS(malidp_runtime_pm_suspend, malidp_runtime_pm_resume, NULL)
102985f64218SLiviu Dudau };
103085f64218SLiviu Dudau 
1031ad49f860SLiviu Dudau static struct platform_driver malidp_platform_driver = {
1032ad49f860SLiviu Dudau 	.probe		= malidp_platform_probe,
1033ad49f860SLiviu Dudau 	.remove		= malidp_platform_remove,
1034ad49f860SLiviu Dudau 	.driver	= {
1035ad49f860SLiviu Dudau 		.name = "mali-dp",
103685f64218SLiviu Dudau 		.pm = &malidp_pm_ops,
1037ad49f860SLiviu Dudau 		.of_match_table	= malidp_drm_of_match,
1038ad49f860SLiviu Dudau 	},
1039ad49f860SLiviu Dudau };
1040ad49f860SLiviu Dudau 
1041ad49f860SLiviu Dudau module_platform_driver(malidp_platform_driver);
1042ad49f860SLiviu Dudau 
1043ad49f860SLiviu Dudau MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
1044ad49f860SLiviu Dudau MODULE_DESCRIPTION("ARM Mali DP DRM driver");
1045ad49f860SLiviu Dudau MODULE_LICENSE("GPL v2");
1046