1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_probe_helper.h>
13 static struct mdp5_kms
*get_kms(struct drm_encoder
*encoder
)
15 struct msm_drm_private
*priv
= encoder
->dev
->dev_private
;
16 return to_mdp5_kms(to_mdp_kms(priv
->kms
));
19 #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
20 #include <mach/board.h>
21 #include <mach/msm_bus.h>
22 #include <mach/msm_bus_board.h>
23 #define MDP_BUS_VECTOR_ENTRY(ab_val, ib_val) \
25 .src = MSM_BUS_MASTER_MDP_PORT0, \
26 .dst = MSM_BUS_SLAVE_EBI_CH0, \
31 static struct msm_bus_vectors mdp_bus_vectors
[] = {
32 MDP_BUS_VECTOR_ENTRY(0, 0),
33 MDP_BUS_VECTOR_ENTRY(2000000000, 2000000000),
35 static struct msm_bus_paths mdp_bus_usecases
[] = { {
37 .vectors
= &mdp_bus_vectors
[0],
40 .vectors
= &mdp_bus_vectors
[1],
42 static struct msm_bus_scale_pdata mdp_bus_scale_table
= {
43 .usecase
= mdp_bus_usecases
,
44 .num_usecases
= ARRAY_SIZE(mdp_bus_usecases
),
48 static void bs_init(struct mdp5_encoder
*mdp5_encoder
)
50 mdp5_encoder
->bsc
= msm_bus_scale_register_client(
51 &mdp_bus_scale_table
);
52 DBG("bus scale client: %08x", mdp5_encoder
->bsc
);
55 static void bs_fini(struct mdp5_encoder
*mdp5_encoder
)
57 if (mdp5_encoder
->bsc
) {
58 msm_bus_scale_unregister_client(mdp5_encoder
->bsc
);
59 mdp5_encoder
->bsc
= 0;
63 static void bs_set(struct mdp5_encoder
*mdp5_encoder
, int idx
)
65 if (mdp5_encoder
->bsc
) {
66 DBG("set bus scaling: %d", idx
);
67 /* HACK: scaling down, and then immediately back up
68 * seems to leave things broken (underflow).. so
72 msm_bus_scale_client_update_request(mdp5_encoder
->bsc
, idx
);
76 static void bs_init(struct mdp5_encoder
*mdp5_encoder
) {}
77 static void bs_fini(struct mdp5_encoder
*mdp5_encoder
) {}
78 static void bs_set(struct mdp5_encoder
*mdp5_encoder
, int idx
) {}
81 static void mdp5_encoder_destroy(struct drm_encoder
*encoder
)
83 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
84 bs_fini(mdp5_encoder
);
85 drm_encoder_cleanup(encoder
);
89 static const struct drm_encoder_funcs mdp5_encoder_funcs
= {
90 .destroy
= mdp5_encoder_destroy
,
93 static void mdp5_vid_encoder_mode_set(struct drm_encoder
*encoder
,
94 struct drm_display_mode
*mode
,
95 struct drm_display_mode
*adjusted_mode
)
97 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
98 struct mdp5_kms
*mdp5_kms
= get_kms(encoder
);
99 struct drm_device
*dev
= encoder
->dev
;
100 struct drm_connector
*connector
;
101 int intf
= mdp5_encoder
->intf
->num
;
102 uint32_t dtv_hsync_skew
, vsync_period
, vsync_len
, ctrl_pol
;
103 uint32_t display_v_start
, display_v_end
;
104 uint32_t hsync_start_x
, hsync_end_x
;
105 uint32_t format
= 0x2100;
108 mode
= adjusted_mode
;
110 DBG("set mode: " DRM_MODE_FMT
, DRM_MODE_ARG(mode
));
114 /* DSI controller cannot handle active-low sync signals. */
115 if (mdp5_encoder
->intf
->type
!= INTF_DSI
) {
116 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
117 ctrl_pol
|= MDP5_INTF_POLARITY_CTL_HSYNC_LOW
;
118 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
119 ctrl_pol
|= MDP5_INTF_POLARITY_CTL_VSYNC_LOW
;
121 /* probably need to get DATA_EN polarity from panel.. */
123 dtv_hsync_skew
= 0; /* get this from panel? */
125 /* Get color format from panel, default is 8bpc */
126 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
127 if (connector
->encoder
== encoder
) {
128 switch (connector
->display_info
.bpc
) {
147 hsync_start_x
= (mode
->htotal
- mode
->hsync_start
);
148 hsync_end_x
= mode
->htotal
- (mode
->hsync_start
- mode
->hdisplay
) - 1;
150 vsync_period
= mode
->vtotal
* mode
->htotal
;
151 vsync_len
= (mode
->vsync_end
- mode
->vsync_start
) * mode
->htotal
;
152 display_v_start
= (mode
->vtotal
- mode
->vsync_start
) * mode
->htotal
+ dtv_hsync_skew
;
153 display_v_end
= vsync_period
- ((mode
->vsync_start
- mode
->vdisplay
) * mode
->htotal
) + dtv_hsync_skew
- 1;
157 * DISPLAY_V_START = (VBP * HCYCLE) + HBP
158 * DISPLAY_V_END = (VBP + VACTIVE) * HCYCLE - 1 - HFP
160 if (mdp5_encoder
->intf
->type
== INTF_eDP
) {
161 display_v_start
+= mode
->htotal
- mode
->hsync_start
;
162 display_v_end
-= mode
->hsync_start
- mode
->hdisplay
;
165 spin_lock_irqsave(&mdp5_encoder
->intf_lock
, flags
);
167 mdp5_write(mdp5_kms
, REG_MDP5_INTF_HSYNC_CTL(intf
),
168 MDP5_INTF_HSYNC_CTL_PULSEW(mode
->hsync_end
- mode
->hsync_start
) |
169 MDP5_INTF_HSYNC_CTL_PERIOD(mode
->htotal
));
170 mdp5_write(mdp5_kms
, REG_MDP5_INTF_VSYNC_PERIOD_F0(intf
), vsync_period
);
171 mdp5_write(mdp5_kms
, REG_MDP5_INTF_VSYNC_LEN_F0(intf
), vsync_len
);
172 mdp5_write(mdp5_kms
, REG_MDP5_INTF_DISPLAY_HCTL(intf
),
173 MDP5_INTF_DISPLAY_HCTL_START(hsync_start_x
) |
174 MDP5_INTF_DISPLAY_HCTL_END(hsync_end_x
));
175 mdp5_write(mdp5_kms
, REG_MDP5_INTF_DISPLAY_VSTART_F0(intf
), display_v_start
);
176 mdp5_write(mdp5_kms
, REG_MDP5_INTF_DISPLAY_VEND_F0(intf
), display_v_end
);
177 mdp5_write(mdp5_kms
, REG_MDP5_INTF_BORDER_COLOR(intf
), 0);
178 mdp5_write(mdp5_kms
, REG_MDP5_INTF_UNDERFLOW_COLOR(intf
), 0xff);
179 mdp5_write(mdp5_kms
, REG_MDP5_INTF_HSYNC_SKEW(intf
), dtv_hsync_skew
);
180 mdp5_write(mdp5_kms
, REG_MDP5_INTF_POLARITY_CTL(intf
), ctrl_pol
);
181 mdp5_write(mdp5_kms
, REG_MDP5_INTF_ACTIVE_HCTL(intf
),
182 MDP5_INTF_ACTIVE_HCTL_START(0) |
183 MDP5_INTF_ACTIVE_HCTL_END(0));
184 mdp5_write(mdp5_kms
, REG_MDP5_INTF_ACTIVE_VSTART_F0(intf
), 0);
185 mdp5_write(mdp5_kms
, REG_MDP5_INTF_ACTIVE_VEND_F0(intf
), 0);
186 mdp5_write(mdp5_kms
, REG_MDP5_INTF_PANEL_FORMAT(intf
), format
);
187 mdp5_write(mdp5_kms
, REG_MDP5_INTF_FRAME_LINE_COUNT_EN(intf
), 0x3); /* frame+line? */
189 spin_unlock_irqrestore(&mdp5_encoder
->intf_lock
, flags
);
191 mdp5_crtc_set_pipeline(encoder
->crtc
);
194 static void mdp5_vid_encoder_disable(struct drm_encoder
*encoder
)
196 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
197 struct mdp5_kms
*mdp5_kms
= get_kms(encoder
);
198 struct mdp5_ctl
*ctl
= mdp5_encoder
->ctl
;
199 struct mdp5_pipeline
*pipeline
= mdp5_crtc_get_pipeline(encoder
->crtc
);
200 struct mdp5_hw_mixer
*mixer
= mdp5_crtc_get_mixer(encoder
->crtc
);
201 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
202 int intfn
= mdp5_encoder
->intf
->num
;
205 if (WARN_ON(!mdp5_encoder
->enabled
))
208 mdp5_ctl_set_encoder_state(ctl
, pipeline
, false);
210 spin_lock_irqsave(&mdp5_encoder
->intf_lock
, flags
);
211 mdp5_write(mdp5_kms
, REG_MDP5_INTF_TIMING_ENGINE_EN(intfn
), 0);
212 spin_unlock_irqrestore(&mdp5_encoder
->intf_lock
, flags
);
213 mdp5_ctl_commit(ctl
, pipeline
, mdp_ctl_flush_mask_encoder(intf
), true);
216 * Wait for a vsync so we know the ENABLE=0 latched before
217 * the (connector) source of the vsync's gets disabled,
218 * otherwise we end up in a funny state if we re-enable
219 * before the disable latches, which results that some of
220 * the settings changes for the new modeset (like new
221 * scanout buffer) don't latch properly..
223 mdp_irq_wait(&mdp5_kms
->base
, intf2vblank(mixer
, intf
));
225 bs_set(mdp5_encoder
, 0);
227 mdp5_encoder
->enabled
= false;
230 static void mdp5_vid_encoder_enable(struct drm_encoder
*encoder
)
232 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
233 struct mdp5_kms
*mdp5_kms
= get_kms(encoder
);
234 struct mdp5_ctl
*ctl
= mdp5_encoder
->ctl
;
235 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
236 struct mdp5_pipeline
*pipeline
= mdp5_crtc_get_pipeline(encoder
->crtc
);
237 int intfn
= intf
->num
;
240 if (WARN_ON(mdp5_encoder
->enabled
))
243 bs_set(mdp5_encoder
, 1);
244 spin_lock_irqsave(&mdp5_encoder
->intf_lock
, flags
);
245 mdp5_write(mdp5_kms
, REG_MDP5_INTF_TIMING_ENGINE_EN(intfn
), 1);
246 spin_unlock_irqrestore(&mdp5_encoder
->intf_lock
, flags
);
247 mdp5_ctl_commit(ctl
, pipeline
, mdp_ctl_flush_mask_encoder(intf
), true);
249 mdp5_ctl_set_encoder_state(ctl
, pipeline
, true);
251 mdp5_encoder
->enabled
= true;
254 static void mdp5_encoder_mode_set(struct drm_encoder
*encoder
,
255 struct drm_display_mode
*mode
,
256 struct drm_display_mode
*adjusted_mode
)
258 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
259 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
261 if (intf
->mode
== MDP5_INTF_DSI_MODE_COMMAND
)
262 mdp5_cmd_encoder_mode_set(encoder
, mode
, adjusted_mode
);
264 mdp5_vid_encoder_mode_set(encoder
, mode
, adjusted_mode
);
267 static void mdp5_encoder_disable(struct drm_encoder
*encoder
)
269 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
270 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
272 if (intf
->mode
== MDP5_INTF_DSI_MODE_COMMAND
)
273 mdp5_cmd_encoder_disable(encoder
);
275 mdp5_vid_encoder_disable(encoder
);
278 static void mdp5_encoder_enable(struct drm_encoder
*encoder
)
280 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
281 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
282 /* this isn't right I think */
283 struct drm_crtc_state
*cstate
= encoder
->crtc
->state
;
285 mdp5_encoder_mode_set(encoder
, &cstate
->mode
, &cstate
->adjusted_mode
);
287 if (intf
->mode
== MDP5_INTF_DSI_MODE_COMMAND
)
288 mdp5_cmd_encoder_enable(encoder
);
290 mdp5_vid_encoder_enable(encoder
);
293 static int mdp5_encoder_atomic_check(struct drm_encoder
*encoder
,
294 struct drm_crtc_state
*crtc_state
,
295 struct drm_connector_state
*conn_state
)
297 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
298 struct mdp5_crtc_state
*mdp5_cstate
= to_mdp5_crtc_state(crtc_state
);
299 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
300 struct mdp5_ctl
*ctl
= mdp5_encoder
->ctl
;
302 mdp5_cstate
->ctl
= ctl
;
303 mdp5_cstate
->pipeline
.intf
= intf
;
306 * This is a bit awkward, but we want to flush the CTL and hit the
307 * START bit at most once for an atomic update. In the non-full-
308 * modeset case, this is done from crtc->atomic_flush(), but that
309 * is too early in the case of full modeset, in which case we
310 * defer to encoder->enable(). But we need to *know* whether
311 * encoder->enable() will be called to do this:
313 if (drm_atomic_crtc_needs_modeset(crtc_state
))
314 mdp5_cstate
->defer_start
= true;
319 static const struct drm_encoder_helper_funcs mdp5_encoder_helper_funcs
= {
320 .disable
= mdp5_encoder_disable
,
321 .enable
= mdp5_encoder_enable
,
322 .atomic_check
= mdp5_encoder_atomic_check
,
325 int mdp5_encoder_get_linecount(struct drm_encoder
*encoder
)
327 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
328 struct mdp5_kms
*mdp5_kms
= get_kms(encoder
);
329 int intf
= mdp5_encoder
->intf
->num
;
331 return mdp5_read(mdp5_kms
, REG_MDP5_INTF_LINE_COUNT(intf
));
334 u32
mdp5_encoder_get_framecount(struct drm_encoder
*encoder
)
336 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
337 struct mdp5_kms
*mdp5_kms
= get_kms(encoder
);
338 int intf
= mdp5_encoder
->intf
->num
;
340 return mdp5_read(mdp5_kms
, REG_MDP5_INTF_FRAME_COUNT(intf
));
343 int mdp5_vid_encoder_set_split_display(struct drm_encoder
*encoder
,
344 struct drm_encoder
*slave_encoder
)
346 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
347 struct mdp5_encoder
*mdp5_slave_enc
= to_mdp5_encoder(slave_encoder
);
348 struct mdp5_kms
*mdp5_kms
;
353 if (!encoder
|| !slave_encoder
)
356 mdp5_kms
= get_kms(encoder
);
357 intf_num
= mdp5_encoder
->intf
->num
;
359 /* Switch slave encoder's TimingGen Sync mode,
360 * to use the master's enable signal for the slave encoder.
363 data
|= MDP5_SPLIT_DPL_LOWER_INTF2_TG_SYNC
;
364 else if (intf_num
== 2)
365 data
|= MDP5_SPLIT_DPL_LOWER_INTF1_TG_SYNC
;
369 dev
= &mdp5_kms
->pdev
->dev
;
370 /* Make sure clocks are on when connectors calling this function. */
371 pm_runtime_get_sync(dev
);
373 /* Dumb Panel, Sync mode */
374 mdp5_write(mdp5_kms
, REG_MDP5_SPLIT_DPL_UPPER
, 0);
375 mdp5_write(mdp5_kms
, REG_MDP5_SPLIT_DPL_LOWER
, data
);
376 mdp5_write(mdp5_kms
, REG_MDP5_SPLIT_DPL_EN
, 1);
378 mdp5_ctl_pair(mdp5_encoder
->ctl
, mdp5_slave_enc
->ctl
, true);
380 pm_runtime_put_sync(dev
);
385 void mdp5_encoder_set_intf_mode(struct drm_encoder
*encoder
, bool cmd_mode
)
387 struct mdp5_encoder
*mdp5_encoder
= to_mdp5_encoder(encoder
);
388 struct mdp5_interface
*intf
= mdp5_encoder
->intf
;
390 /* TODO: Expand this to set writeback modes too */
392 WARN_ON(intf
->type
!= INTF_DSI
);
393 intf
->mode
= MDP5_INTF_DSI_MODE_COMMAND
;
395 if (intf
->type
== INTF_DSI
)
396 intf
->mode
= MDP5_INTF_DSI_MODE_VIDEO
;
398 intf
->mode
= MDP5_INTF_MODE_NONE
;
402 /* initialize encoder */
403 struct drm_encoder
*mdp5_encoder_init(struct drm_device
*dev
,
404 struct mdp5_interface
*intf
,
405 struct mdp5_ctl
*ctl
)
407 struct drm_encoder
*encoder
= NULL
;
408 struct mdp5_encoder
*mdp5_encoder
;
409 int enc_type
= (intf
->type
== INTF_DSI
) ?
410 DRM_MODE_ENCODER_DSI
: DRM_MODE_ENCODER_TMDS
;
413 mdp5_encoder
= kzalloc(sizeof(*mdp5_encoder
), GFP_KERNEL
);
419 encoder
= &mdp5_encoder
->base
;
420 mdp5_encoder
->ctl
= ctl
;
421 mdp5_encoder
->intf
= intf
;
423 spin_lock_init(&mdp5_encoder
->intf_lock
);
425 drm_encoder_init(dev
, encoder
, &mdp5_encoder_funcs
, enc_type
, NULL
);
427 drm_encoder_helper_add(encoder
, &mdp5_encoder_helper_funcs
);
429 bs_init(mdp5_encoder
);
435 mdp5_encoder_destroy(encoder
);