GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / gpu / drm / msm / mdp / mdp5 / mdp5_cmd_encoder.c
1 /*
2  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3  *
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.
7  *
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.
12  */
13
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_crtc_helper.h>
16
17 #include "mdp5_kms.h"
18
19 static struct mdp5_kms *get_kms(struct drm_encoder *encoder)
20 {
21         struct msm_drm_private *priv = encoder->dev->dev_private;
22         return to_mdp5_kms(to_mdp_kms(priv->kms));
23 }
24
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>
29
30 static void bs_set(struct mdp5_encoder *mdp5_cmd_enc, int idx)
31 {
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
36                  * never disable:
37                  */
38                 idx = 1;
39                 msm_bus_scale_client_update_request(mdp5_cmd_enc->bsc, idx);
40         }
41 }
42 #else
43 static void bs_set(struct mdp5_encoder *mdp5_cmd_enc, int idx) {}
44 #endif
45
46 #define VSYNC_CLK_RATE 19200000
47 static int pingpong_tearcheck_setup(struct drm_encoder *encoder,
48                                     struct drm_display_mode *mode)
49 {
50         struct mdp5_kms *mdp5_kms = get_kms(encoder);
51         struct device *dev = encoder->dev->dev;
52         u32 total_lines_x100, vclks_line, cfg;
53         long vsync_clk_speed;
54         struct mdp5_hw_mixer *mixer = mdp5_crtc_get_mixer(encoder->crtc);
55         int pp_id = mixer->pp;
56
57         if (IS_ERR_OR_NULL(mdp5_kms->vsync_clk)) {
58                 dev_err(dev, "vsync_clk is not initialized\n");
59                 return -EINVAL;
60         }
61
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);
66                 return -EINVAL;
67         }
68
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",
72                                                         vsync_clk_speed);
73                 return -EINVAL;
74         }
75         vclks_line = vsync_clk_speed * 100 / total_lines_x100;
76
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);
80
81         /*
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.
87          */
88         mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_CONFIG_VSYNC(pp_id), cfg);
89         mdp5_write(mdp5_kms,
90                 REG_MDP5_PP_SYNC_CONFIG_HEIGHT(pp_id), (2 * mode->vtotal));
91
92         mdp5_write(mdp5_kms,
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));
99
100         return 0;
101 }
102
103 static int pingpong_tearcheck_enable(struct drm_encoder *encoder)
104 {
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;
108         int ret;
109
110         ret = clk_set_rate(mdp5_kms->vsync_clk,
111                 clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE));
112         if (ret) {
113                 dev_err(encoder->dev->dev,
114                         "vsync_clk clk_set_rate failed, %d\n", ret);
115                 return ret;
116         }
117         ret = clk_prepare_enable(mdp5_kms->vsync_clk);
118         if (ret) {
119                 dev_err(encoder->dev->dev,
120                         "vsync_clk clk_prepare_enable failed, %d\n", ret);
121                 return ret;
122         }
123
124         mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 1);
125
126         return 0;
127 }
128
129 static void pingpong_tearcheck_disable(struct drm_encoder *encoder)
130 {
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;
134
135         mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 0);
136         clk_disable_unprepare(mdp5_kms->vsync_clk);
137 }
138
139 void mdp5_cmd_encoder_mode_set(struct drm_encoder *encoder,
140                                struct drm_display_mode *mode,
141                                struct drm_display_mode *adjusted_mode)
142 {
143         mode = adjusted_mode;
144
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);
155 }
156
157 void mdp5_cmd_encoder_disable(struct drm_encoder *encoder)
158 {
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);
163
164         if (WARN_ON(!mdp5_cmd_enc->enabled))
165                 return;
166
167         pingpong_tearcheck_disable(encoder);
168
169         mdp5_ctl_set_encoder_state(ctl, pipeline, false);
170         mdp5_ctl_commit(ctl, pipeline, mdp_ctl_flush_mask_encoder(intf));
171
172         bs_set(mdp5_cmd_enc, 0);
173
174         mdp5_cmd_enc->enabled = false;
175 }
176
177 void mdp5_cmd_encoder_enable(struct drm_encoder *encoder)
178 {
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);
183
184         if (WARN_ON(mdp5_cmd_enc->enabled))
185                 return;
186
187         bs_set(mdp5_cmd_enc, 1);
188         if (pingpong_tearcheck_enable(encoder))
189                 return;
190
191         mdp5_ctl_commit(ctl, pipeline, mdp_ctl_flush_mask_encoder(intf));
192
193         mdp5_ctl_set_encoder_state(ctl, pipeline, true);
194
195         mdp5_cmd_enc->enabled = true;
196 }
197
198 int mdp5_cmd_encoder_set_split_display(struct drm_encoder *encoder,
199                                        struct drm_encoder *slave_encoder)
200 {
201         struct mdp5_encoder *mdp5_cmd_enc = to_mdp5_encoder(encoder);
202         struct mdp5_kms *mdp5_kms;
203         struct device *dev;
204         int intf_num;
205         u32 data = 0;
206
207         if (!encoder || !slave_encoder)
208                 return -EINVAL;
209
210         mdp5_kms = get_kms(encoder);
211         intf_num = mdp5_cmd_enc->intf->num;
212
213         /* Switch slave encoder's trigger MUX, to use the master's
214          * start signal for the slave encoder
215          */
216         if (intf_num == 1)
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;
220         else
221                 return -EINVAL;
222
223         /* Smart Panel, Sync mode */
224         data |= MDP5_SPLIT_DPL_UPPER_SMART_PANEL;
225
226         dev = &mdp5_kms->pdev->dev;
227
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);
231
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);
236
237         return 0;
238 }