2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_crtc_helper.h>
19 static struct mdp5_kms *get_kms(struct drm_encoder *encoder)
21 struct msm_drm_private *priv = encoder->dev->dev_private;
22 return to_mdp5_kms(to_mdp_kms(priv->kms));
25 #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
26 #include <mach/board.h>
27 #include <linux/msm-bus.h>
28 #include <linux/msm-bus-board.h>
30 static void bs_set(struct mdp5_encoder *mdp5_cmd_enc, int idx)
32 if (mdp5_cmd_enc->bsc) {
33 DBG("set bus scaling: %d", idx);
34 /* HACK: scaling down, and then immediately back up
35 * seems to leave things broken (underflow).. so
39 msm_bus_scale_client_update_request(mdp5_cmd_enc->bsc, idx);
43 static void bs_set(struct mdp5_encoder *mdp5_cmd_enc, int idx) {}
46 #define VSYNC_CLK_RATE 19200000
47 static int pingpong_tearcheck_setup(struct drm_encoder *encoder,
48 struct drm_display_mode *mode)
50 struct mdp5_kms *mdp5_kms = get_kms(encoder);
51 struct device *dev = encoder->dev->dev;
52 u32 total_lines_x100, vclks_line, cfg;
54 struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
55 int pp_id = mixer->pp;
57 if (IS_ERR_OR_NULL(mdp5_kms->vsync_clk)) {
58 dev_err(dev, "vsync_clk is not initialized\n");
62 total_lines_x100 = mode->vtotal * mode->vrefresh;
63 if (!total_lines_x100) {
64 dev_err(dev, "%s: vtotal(%d) or vrefresh(%d) is 0\n",
65 __func__, mode->vtotal, mode->vrefresh);
69 vsync_clk_speed = clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE);
70 if (vsync_clk_speed <= 0) {
71 dev_err(dev, "vsync_clk round rate failed %ld\n",
75 vclks_line = vsync_clk_speed * 100 / total_lines_x100;
77 cfg = MDP5_PP_SYNC_CONFIG_VSYNC_COUNTER_EN
78 | MDP5_PP_SYNC_CONFIG_VSYNC_IN_EN;
79 cfg |= MDP5_PP_SYNC_CONFIG_VSYNC_COUNT(vclks_line);
82 * Tearcheck emits a blanking signal every vclks_line * vtotal * 2 ticks on
83 * the vsync_clk equating to roughly half the desired panel refresh rate.
84 * This is only necessary as stability fallback if interrupts from the
85 * panel arrive too late or not at all, but is currently used by default
86 * because these panel interrupts are not wired up yet.
88 mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_CONFIG_VSYNC(pp_id), cfg);
90 REG_MDP5_PP_SYNC_CONFIG_HEIGHT(pp_id), (2 * mode->vtotal));
93 REG_MDP5_PP_VSYNC_INIT_VAL(pp_id), mode->vdisplay);
94 mdp5_write(mdp5_kms, REG_MDP5_PP_RD_PTR_IRQ(pp_id), mode->vdisplay + 1);
95 mdp5_write(mdp5_kms, REG_MDP5_PP_START_POS(pp_id), mode->vdisplay);
96 mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_THRESH(pp_id),
97 MDP5_PP_SYNC_THRESH_START(4) |
98 MDP5_PP_SYNC_THRESH_CONTINUE(4));
103 static int pingpong_tearcheck_enable(struct drm_encoder *encoder)
105 struct mdp5_kms *mdp5_kms = get_kms(encoder);
106 struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
107 int pp_id = mixer->pp;
110 ret = clk_set_rate(mdp5_kms->vsync_clk,
111 clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE));
113 dev_err(encoder->dev->dev,
114 "vsync_clk clk_set_rate failed, %d\n", ret);
117 ret = clk_prepare_enable(mdp5_kms->vsync_clk);
119 dev_err(encoder->dev->dev,
120 "vsync_clk clk_prepare_enable failed, %d\n", ret);
124 mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 1);
129 static void pingpong_tearcheck_disable(struct drm_encoder *encoder)
131 struct mdp5_kms *mdp5_kms = get_kms(encoder);
132 struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
133 int pp_id = mixer->pp;
135 mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 0);
136 clk_disable_unprepare(mdp5_kms->vsync_clk);
139 void mdp5_cmd_encoder_mode_set(struct drm_encoder *encoder,
140 struct drm_display_mode *mode,
141 struct drm_display_mode *adjusted_mode)
143 mode = adjusted_mode;
145 DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
146 mode->base.id, mode->name,
147 mode->vrefresh, mode->clock,
148 mode->hdisplay, mode->hsync_start,
149 mode->hsync_end, mode->htotal,
150 mode->vdisplay, mode->vsync_start,
151 mode->vsync_end, mode->vtotal,
152 mode->type, mode->flags);
153 pingpong_tearcheck_setup(encoder, mode);
154 mdp5_crtc_set_pipeline(encoder->crtc);
157 void mdp5_cmd_encoder_disable(struct drm_encoder *encoder)
159 struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
160 struct mdp5_ctl *ctl = mdp5_cmd_enc->ctl;
161 struct mdp5_interface *intf = mdp5_cmd_enc->intf;
162 struct mdp5_pipeline *pipeline = mdp5_crtc_get_pipeline(encoder->crtc);
164 if (WARN_ON(!mdp5_cmd_enc->enabled))
167 pingpong_tearcheck_disable(encoder);
169 mdp5_ctl_set_encoder_state(ctl, pipeline, false);
170 mdp5_ctl_commit(ctl, pipeline, mdp_ctl_flush_mask_encoder(intf));
172 bs_set(mdp5_cmd_enc, 0);
174 mdp5_cmd_enc->enabled = false;
177 void mdp5_cmd_encoder_enable(struct drm_encoder *encoder)
179 struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
180 struct mdp5_ctl *ctl = mdp5_cmd_enc->ctl;
181 struct mdp5_interface *intf = mdp5_cmd_enc->intf;
182 struct mdp5_pipeline *pipeline = mdp5_crtc_get_pipeline(encoder->crtc);
184 if (WARN_ON(mdp5_cmd_enc->enabled))
187 bs_set(mdp5_cmd_enc, 1);
188 if (pingpong_tearcheck_enable(encoder))
191 mdp5_ctl_commit(ctl, pipeline, mdp_ctl_flush_mask_encoder(intf));
193 mdp5_ctl_set_encoder_state(ctl, pipeline, true);
195 mdp5_cmd_enc->enabled = true;
198 int mdp5_cmd_encoder_set_split_display(struct drm_encoder *encoder,
199 struct drm_encoder *slave_encoder)
201 struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
202 struct mdp5_kms *mdp5_kms;
207 if (!encoder || !slave_encoder)
210 mdp5_kms = get_kms(encoder);
211 intf_num = mdp5_cmd_enc->intf->num;
213 /* Switch slave encoder's trigger MUX, to use the master's
214 * start signal for the slave encoder
217 data |= MDP5_SPLIT_DPL_UPPER_INTF2_SW_TRG_MUX;
218 else if (intf_num == 2)
219 data |= MDP5_SPLIT_DPL_UPPER_INTF1_SW_TRG_MUX;
223 /* Smart Panel, Sync mode */
224 data |= MDP5_SPLIT_DPL_UPPER_SMART_PANEL;
226 dev = &mdp5_kms->pdev->dev;
228 /* Make sure clocks are on when connectors calling this function. */
229 pm_runtime_get_sync(dev);
230 mdp5_write(mdp5_kms, REG_MDP5_SPLIT_DPL_UPPER, data);
232 mdp5_write(mdp5_kms, REG_MDP5_SPLIT_DPL_LOWER,
233 MDP5_SPLIT_DPL_LOWER_SMART_PANEL);
234 mdp5_write(mdp5_kms, REG_MDP5_SPLIT_DPL_EN, 1);
235 pm_runtime_put_autosuspend(dev);