• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <drm/drm_crtc.h>
7 #include <drm/drm_probe_helper.h>
8 
9 #include "mdp5_kms.h"
10 
get_kms(struct drm_encoder * encoder)11 static struct mdp5_kms *get_kms(struct drm_encoder *encoder)
12 {
13 	struct msm_drm_private *priv = encoder->dev->dev_private;
14 	return to_mdp5_kms(to_mdp_kms(priv->kms));
15 }
16 
17 #define VSYNC_CLK_RATE 19200000
pingpong_tearcheck_setup(struct drm_encoder * encoder,struct drm_display_mode * mode)18 static int pingpong_tearcheck_setup(struct drm_encoder *encoder,
19 				    struct drm_display_mode *mode)
20 {
21 	struct mdp5_kms *mdp5_kms = get_kms(encoder);
22 	struct device *dev = encoder->dev->dev;
23 	u32 total_lines, vclks_line, cfg;
24 	long vsync_clk_speed;
25 	struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
26 	int pp_id = mixer->pp;
27 
28 	if (IS_ERR_OR_NULL(mdp5_kms->vsync_clk)) {
29 		DRM_DEV_ERROR(dev, "vsync_clk is not initialized\n");
30 		return -EINVAL;
31 	}
32 
33 	total_lines = mode->vtotal * drm_mode_vrefresh(mode);
34 	if (!total_lines) {
35 		DRM_DEV_ERROR(dev, "%s: vtotal(%d) or vrefresh(%d) is 0\n",
36 			      __func__, mode->vtotal, drm_mode_vrefresh(mode));
37 		return -EINVAL;
38 	}
39 
40 	vsync_clk_speed = clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE);
41 	if (vsync_clk_speed <= 0) {
42 		DRM_DEV_ERROR(dev, "vsync_clk round rate failed %ld\n",
43 							vsync_clk_speed);
44 		return -EINVAL;
45 	}
46 	vclks_line = vsync_clk_speed / total_lines;
47 
48 	cfg = MDP5_PP_SYNC_CONFIG_VSYNC_COUNTER_EN
49 		| MDP5_PP_SYNC_CONFIG_VSYNC_IN_EN;
50 	cfg |= MDP5_PP_SYNC_CONFIG_VSYNC_COUNT(vclks_line);
51 
52 	/*
53 	 * Tearcheck emits a blanking signal every vclks_line * vtotal * 2 ticks on
54 	 * the vsync_clk equating to roughly half the desired panel refresh rate.
55 	 * This is only necessary as stability fallback if interrupts from the
56 	 * panel arrive too late or not at all, but is currently used by default
57 	 * because these panel interrupts are not wired up yet.
58 	 */
59 	mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_CONFIG_VSYNC(pp_id), cfg);
60 	mdp5_write(mdp5_kms,
61 		REG_MDP5_PP_SYNC_CONFIG_HEIGHT(pp_id), (2 * mode->vtotal));
62 
63 	mdp5_write(mdp5_kms,
64 		REG_MDP5_PP_VSYNC_INIT_VAL(pp_id), mode->vdisplay);
65 	mdp5_write(mdp5_kms, REG_MDP5_PP_RD_PTR_IRQ(pp_id), mode->vdisplay + 1);
66 	mdp5_write(mdp5_kms, REG_MDP5_PP_START_POS(pp_id), mode->vdisplay);
67 	mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_THRESH(pp_id),
68 			MDP5_PP_SYNC_THRESH_START(4) |
69 			MDP5_PP_SYNC_THRESH_CONTINUE(4));
70 
71 	return 0;
72 }
73 
pingpong_tearcheck_enable(struct drm_encoder * encoder)74 static int pingpong_tearcheck_enable(struct drm_encoder *encoder)
75 {
76 	struct mdp5_kms *mdp5_kms = get_kms(encoder);
77 	struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
78 	int pp_id = mixer->pp;
79 	int ret;
80 
81 	ret = clk_set_rate(mdp5_kms->vsync_clk,
82 		clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE));
83 	if (ret) {
84 		DRM_DEV_ERROR(encoder->dev->dev,
85 			"vsync_clk clk_set_rate failed, %d\n", ret);
86 		return ret;
87 	}
88 	ret = clk_prepare_enable(mdp5_kms->vsync_clk);
89 	if (ret) {
90 		DRM_DEV_ERROR(encoder->dev->dev,
91 			"vsync_clk clk_prepare_enable failed, %d\n", ret);
92 		return ret;
93 	}
94 
95 	mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 1);
96 
97 	return 0;
98 }
99 
pingpong_tearcheck_disable(struct drm_encoder * encoder)100 static void pingpong_tearcheck_disable(struct drm_encoder *encoder)
101 {
102 	struct mdp5_kms *mdp5_kms = get_kms(encoder);
103 	struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
104 	int pp_id = mixer->pp;
105 
106 	mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 0);
107 	clk_disable_unprepare(mdp5_kms->vsync_clk);
108 }
109 
mdp5_cmd_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)110 void mdp5_cmd_encoder_mode_set(struct drm_encoder *encoder,
111 			       struct drm_display_mode *mode,
112 			       struct drm_display_mode *adjusted_mode)
113 {
114 	mode = adjusted_mode;
115 
116 	DBG("set mode: " DRM_MODE_FMT, DRM_MODE_ARG(mode));
117 	pingpong_tearcheck_setup(encoder, mode);
118 	mdp5_crtc_set_pipeline(encoder->crtc);
119 }
120 
mdp5_cmd_encoder_disable(struct drm_encoder * encoder)121 void mdp5_cmd_encoder_disable(struct drm_encoder *encoder)
122 {
123 	struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
124 	struct mdp5_ctl *ctl = mdp5_cmd_enc->ctl;
125 	struct mdp5_interface *intf = mdp5_cmd_enc->intf;
126 	struct mdp5_pipeline *pipeline = mdp5_crtc_get_pipeline(encoder->crtc);
127 
128 	if (WARN_ON(!mdp5_cmd_enc->enabled))
129 		return;
130 
131 	pingpong_tearcheck_disable(encoder);
132 
133 	mdp5_ctl_set_encoder_state(ctl, pipeline, false);
134 	mdp5_ctl_commit(ctl, pipeline, mdp_ctl_flush_mask_encoder(intf), true);
135 
136 	mdp5_cmd_enc->enabled = false;
137 }
138 
mdp5_cmd_encoder_enable(struct drm_encoder * encoder)139 void mdp5_cmd_encoder_enable(struct drm_encoder *encoder)
140 {
141 	struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
142 	struct mdp5_ctl *ctl = mdp5_cmd_enc->ctl;
143 	struct mdp5_interface *intf = mdp5_cmd_enc->intf;
144 	struct mdp5_pipeline *pipeline = mdp5_crtc_get_pipeline(encoder->crtc);
145 
146 	if (WARN_ON(mdp5_cmd_enc->enabled))
147 		return;
148 
149 	if (pingpong_tearcheck_enable(encoder))
150 		return;
151 
152 	mdp5_ctl_commit(ctl, pipeline, mdp_ctl_flush_mask_encoder(intf), true);
153 
154 	mdp5_ctl_set_encoder_state(ctl, pipeline, true);
155 
156 	mdp5_cmd_enc->enabled = true;
157 }
158 
mdp5_cmd_encoder_set_split_display(struct drm_encoder * encoder,struct drm_encoder * slave_encoder)159 int mdp5_cmd_encoder_set_split_display(struct drm_encoder *encoder,
160 				       struct drm_encoder *slave_encoder)
161 {
162 	struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
163 	struct mdp5_kms *mdp5_kms;
164 	struct device *dev;
165 	int intf_num;
166 	u32 data = 0;
167 
168 	if (!encoder || !slave_encoder)
169 		return -EINVAL;
170 
171 	mdp5_kms = get_kms(encoder);
172 	intf_num = mdp5_cmd_enc->intf->num;
173 
174 	/* Switch slave encoder's trigger MUX, to use the master's
175 	 * start signal for the slave encoder
176 	 */
177 	if (intf_num == 1)
178 		data |= MDP5_SPLIT_DPL_UPPER_INTF2_SW_TRG_MUX;
179 	else if (intf_num == 2)
180 		data |= MDP5_SPLIT_DPL_UPPER_INTF1_SW_TRG_MUX;
181 	else
182 		return -EINVAL;
183 
184 	/* Smart Panel, Sync mode */
185 	data |= MDP5_SPLIT_DPL_UPPER_SMART_PANEL;
186 
187 	dev = &mdp5_kms->pdev->dev;
188 
189 	/* Make sure clocks are on when connectors calling this function. */
190 	pm_runtime_get_sync(dev);
191 	mdp5_write(mdp5_kms, REG_MDP5_SPLIT_DPL_UPPER, data);
192 
193 	mdp5_write(mdp5_kms, REG_MDP5_SPLIT_DPL_LOWER,
194 		   MDP5_SPLIT_DPL_LOWER_SMART_PANEL);
195 	mdp5_write(mdp5_kms, REG_MDP5_SPLIT_DPL_EN, 1);
196 	pm_runtime_put_sync(dev);
197 
198 	return 0;
199 }
200