]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/msm/mdp/mdp5/mdp5_kms.c
drm/msm/mdp5: Add MDSS top level driver
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / msm / mdp / mdp5 / mdp5_kms.c
CommitLineData
06c0dd96 1/*
2e362e17 2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
06c0dd96
RC
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#include "msm_drv.h"
21#include "msm_mmu.h"
22#include "mdp5_kms.h"
23
87e956e9
SV
24static const char *iommu_ports[] = {
25 "mdp_0",
26};
27
3d47fd47
SV
28static int mdp5_hw_init(struct msm_kms *kms)
29{
30 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
31 struct drm_device *dev = mdp5_kms->dev;
0deed25b 32 unsigned long flags;
3d47fd47
SV
33
34 pm_runtime_get_sync(dev->dev);
35
06c0dd96
RC
36 /* Magic unknown register writes:
37 *
38 * W VBIF:0x004 00000001 (mdss_mdp.c:839)
39 * W MDP5:0x2e0 0xe9 (mdss_mdp.c:839)
40 * W MDP5:0x2e4 0x55 (mdss_mdp.c:839)
41 * W MDP5:0x3ac 0xc0000ccc (mdss_mdp.c:839)
42 * W MDP5:0x3b4 0xc0000ccc (mdss_mdp.c:839)
43 * W MDP5:0x3bc 0xcccccc (mdss_mdp.c:839)
44 * W MDP5:0x4a8 0xcccc0c0 (mdss_mdp.c:839)
45 * W MDP5:0x4b0 0xccccc0c0 (mdss_mdp.c:839)
46 * W MDP5:0x4b8 0xccccc000 (mdss_mdp.c:839)
47 *
48 * Downstream fbdev driver gets these register offsets/values
49 * from DT.. not really sure what these registers are or if
50 * different values for different boards/SoC's, etc. I guess
51 * they are the golden registers.
52 *
53 * Not setting these does not seem to cause any problem. But
54 * we may be getting lucky with the bootloader initializing
55 * them for us. OTOH, if we can always count on the bootloader
56 * setting the golden registers, then perhaps we don't need to
57 * care.
58 */
59
0deed25b 60 spin_lock_irqsave(&mdp5_kms->resource_lock, flags);
f5253812 61 mdp5_write(mdp5_kms, REG_MDP5_MDP_DISP_INTF_SEL(0), 0);
0deed25b 62 spin_unlock_irqrestore(&mdp5_kms->resource_lock, flags);
06c0dd96 63
42238da8 64 mdp5_ctlm_hw_reset(mdp5_kms->ctlm);
3d47fd47 65
06c0dd96
RC
66 pm_runtime_put_sync(dev->dev);
67
3d47fd47 68 return 0;
06c0dd96
RC
69}
70
0b776d45
RC
71static void mdp5_prepare_commit(struct msm_kms *kms, struct drm_atomic_state *state)
72{
73 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
74 mdp5_enable(mdp5_kms);
75}
76
77static void mdp5_complete_commit(struct msm_kms *kms, struct drm_atomic_state *state)
78{
657c63f0 79 int i;
0b776d45 80 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
8d76b79f
DV
81 struct drm_plane *plane;
82 struct drm_plane_state *plane_state;
657c63f0 83
8d76b79f 84 for_each_plane_in_state(state, plane, plane_state, i)
657c63f0 85 mdp5_plane_complete_commit(plane, plane_state);
657c63f0 86
0b776d45
RC
87 mdp5_disable(mdp5_kms);
88}
89
0a5c9aad
HL
90static void mdp5_wait_for_crtc_commit_done(struct msm_kms *kms,
91 struct drm_crtc *crtc)
92{
93 mdp5_crtc_wait_for_commit_done(crtc);
94}
95
06c0dd96
RC
96static long mdp5_round_pixclk(struct msm_kms *kms, unsigned long rate,
97 struct drm_encoder *encoder)
98{
99 return rate;
100}
101
d5af49c9
HL
102static int mdp5_set_split_display(struct msm_kms *kms,
103 struct drm_encoder *encoder,
104 struct drm_encoder *slave_encoder,
105 bool is_cmd_mode)
106{
107 if (is_cmd_mode)
108 return mdp5_cmd_encoder_set_split_display(encoder,
109 slave_encoder);
110 else
111 return mdp5_encoder_set_split_display(encoder, slave_encoder);
112}
113
06c0dd96
RC
114static void mdp5_destroy(struct msm_kms *kms)
115{
116 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
87e956e9
SV
117 struct msm_mmu *mmu = mdp5_kms->mmu;
118
f6a8eaca
RC
119 mdp5_irq_domain_fini(mdp5_kms);
120
87e956e9
SV
121 if (mmu) {
122 mmu->funcs->detach(mmu, iommu_ports, ARRAY_SIZE(iommu_ports));
123 mmu->funcs->destroy(mmu);
124 }
42238da8
RC
125
126 if (mdp5_kms->ctlm)
127 mdp5_ctlm_destroy(mdp5_kms->ctlm);
128 if (mdp5_kms->smp)
129 mdp5_smp_destroy(mdp5_kms->smp);
130 if (mdp5_kms->cfg)
131 mdp5_cfg_destroy(mdp5_kms->cfg);
bfcdfb0e 132
06c0dd96
RC
133 kfree(mdp5_kms);
134}
135
136static const struct mdp_kms_funcs kms_funcs = {
137 .base = {
138 .hw_init = mdp5_hw_init,
139 .irq_preinstall = mdp5_irq_preinstall,
140 .irq_postinstall = mdp5_irq_postinstall,
141 .irq_uninstall = mdp5_irq_uninstall,
142 .irq = mdp5_irq,
143 .enable_vblank = mdp5_enable_vblank,
144 .disable_vblank = mdp5_disable_vblank,
0b776d45
RC
145 .prepare_commit = mdp5_prepare_commit,
146 .complete_commit = mdp5_complete_commit,
0a5c9aad 147 .wait_for_crtc_commit_done = mdp5_wait_for_crtc_commit_done,
06c0dd96
RC
148 .get_format = mdp_get_format,
149 .round_pixclk = mdp5_round_pixclk,
d5af49c9 150 .set_split_display = mdp5_set_split_display,
06c0dd96
RC
151 .destroy = mdp5_destroy,
152 },
153 .set_irqmask = mdp5_set_irqmask,
154};
155
156int mdp5_disable(struct mdp5_kms *mdp5_kms)
157{
158 DBG("");
159
160 clk_disable_unprepare(mdp5_kms->ahb_clk);
161 clk_disable_unprepare(mdp5_kms->axi_clk);
162 clk_disable_unprepare(mdp5_kms->core_clk);
3a84f846
SV
163 if (mdp5_kms->lut_clk)
164 clk_disable_unprepare(mdp5_kms->lut_clk);
06c0dd96
RC
165
166 return 0;
167}
168
169int mdp5_enable(struct mdp5_kms *mdp5_kms)
170{
171 DBG("");
172
173 clk_prepare_enable(mdp5_kms->ahb_clk);
174 clk_prepare_enable(mdp5_kms->axi_clk);
175 clk_prepare_enable(mdp5_kms->core_clk);
3a84f846
SV
176 if (mdp5_kms->lut_clk)
177 clk_prepare_enable(mdp5_kms->lut_clk);
06c0dd96
RC
178
179 return 0;
180}
181
5722a9e3
HL
182static struct drm_encoder *construct_encoder(struct mdp5_kms *mdp5_kms,
183 enum mdp5_intf_type intf_type, int intf_num,
c71716b1 184 enum mdp5_intf_mode intf_mode, struct mdp5_ctl *ctl)
67ac0a2d
SV
185{
186 struct drm_device *dev = mdp5_kms->dev;
187 struct msm_drm_private *priv = dev->dev_private;
188 struct drm_encoder *encoder;
189 struct mdp5_interface intf = {
190 .num = intf_num,
191 .type = intf_type,
5722a9e3 192 .mode = intf_mode,
67ac0a2d 193 };
67ac0a2d 194
d5af49c9
HL
195 if ((intf_type == INTF_DSI) &&
196 (intf_mode == MDP5_INTF_DSI_MODE_COMMAND))
c71716b1 197 encoder = mdp5_cmd_encoder_init(dev, &intf, ctl);
d5af49c9 198 else
c71716b1 199 encoder = mdp5_encoder_init(dev, &intf, ctl);
d5af49c9 200
67ac0a2d 201 if (IS_ERR(encoder)) {
5722a9e3
HL
202 dev_err(dev->dev, "failed to construct encoder\n");
203 return encoder;
67ac0a2d
SV
204 }
205
206 encoder->possible_crtcs = (1 << priv->num_crtcs) - 1;
207 priv->encoders[priv->num_encoders++] = encoder;
208
5722a9e3
HL
209 return encoder;
210}
211
d5af49c9
HL
212static int get_dsi_id_from_intf(const struct mdp5_cfg_hw *hw_cfg, int intf_num)
213{
fe34464d
SV
214 const enum mdp5_intf_type *intfs = hw_cfg->intf.connect;
215 const int intf_cnt = ARRAY_SIZE(hw_cfg->intf.connect);
d5af49c9
HL
216 int id = 0, i;
217
218 for (i = 0; i < intf_cnt; i++) {
219 if (intfs[i] == INTF_DSI) {
220 if (intf_num == i)
221 return id;
222
223 id++;
224 }
225 }
226
227 return -EINVAL;
228}
229
5722a9e3
HL
230static int modeset_init_intf(struct mdp5_kms *mdp5_kms, int intf_num)
231{
232 struct drm_device *dev = mdp5_kms->dev;
233 struct msm_drm_private *priv = dev->dev_private;
234 const struct mdp5_cfg_hw *hw_cfg =
235 mdp5_cfg_get_hw_config(mdp5_kms->cfg);
fe34464d 236 enum mdp5_intf_type intf_type = hw_cfg->intf.connect[intf_num];
c71716b1
HL
237 struct mdp5_ctl_manager *ctlm = mdp5_kms->ctlm;
238 struct mdp5_ctl *ctl;
5722a9e3
HL
239 struct drm_encoder *encoder;
240 int ret = 0;
241
242 switch (intf_type) {
243 case INTF_DISABLED:
244 break;
245 case INTF_eDP:
246 if (!priv->edp)
247 break;
248
c71716b1
HL
249 ctl = mdp5_ctlm_request(ctlm, intf_num);
250 if (!ctl) {
251 ret = -EINVAL;
252 break;
253 }
254
5722a9e3 255 encoder = construct_encoder(mdp5_kms, INTF_eDP, intf_num,
c71716b1 256 MDP5_INTF_MODE_NONE, ctl);
5722a9e3
HL
257 if (IS_ERR(encoder)) {
258 ret = PTR_ERR(encoder);
259 break;
260 }
67ac0a2d 261
67ac0a2d 262 ret = msm_edp_modeset_init(priv->edp, dev, encoder);
5722a9e3
HL
263 break;
264 case INTF_HDMI:
265 if (!priv->hdmi)
266 break;
267
c71716b1
HL
268 ctl = mdp5_ctlm_request(ctlm, intf_num);
269 if (!ctl) {
270 ret = -EINVAL;
271 break;
272 }
273
5722a9e3 274 encoder = construct_encoder(mdp5_kms, INTF_HDMI, intf_num,
c71716b1 275 MDP5_INTF_MODE_NONE, ctl);
5722a9e3
HL
276 if (IS_ERR(encoder)) {
277 ret = PTR_ERR(encoder);
278 break;
279 }
280
fcda50c8 281 ret = msm_hdmi_modeset_init(priv->hdmi, dev, encoder);
5722a9e3 282 break;
d5af49c9
HL
283 case INTF_DSI:
284 {
285 int dsi_id = get_dsi_id_from_intf(hw_cfg, intf_num);
286 struct drm_encoder *dsi_encs[MSM_DSI_ENCODER_NUM];
287 enum mdp5_intf_mode mode;
288 int i;
289
290 if ((dsi_id >= ARRAY_SIZE(priv->dsi)) || (dsi_id < 0)) {
291 dev_err(dev->dev, "failed to find dsi from intf %d\n",
292 intf_num);
293 ret = -EINVAL;
294 break;
295 }
296
297 if (!priv->dsi[dsi_id])
298 break;
299
c71716b1
HL
300 ctl = mdp5_ctlm_request(ctlm, intf_num);
301 if (!ctl) {
302 ret = -EINVAL;
303 break;
304 }
305
d5af49c9
HL
306 for (i = 0; i < MSM_DSI_ENCODER_NUM; i++) {
307 mode = (i == MSM_DSI_CMD_ENCODER_ID) ?
308 MDP5_INTF_DSI_MODE_COMMAND :
309 MDP5_INTF_DSI_MODE_VIDEO;
310 dsi_encs[i] = construct_encoder(mdp5_kms, INTF_DSI,
c71716b1
HL
311 intf_num, mode, ctl);
312 if (IS_ERR(dsi_encs[i])) {
313 ret = PTR_ERR(dsi_encs[i]);
d5af49c9
HL
314 break;
315 }
316 }
317
318 ret = msm_dsi_modeset_init(priv->dsi[dsi_id], dev, dsi_encs);
319 break;
320 }
5722a9e3
HL
321 default:
322 dev_err(dev->dev, "unknown intf: %d\n", intf_type);
323 ret = -EINVAL;
324 break;
67ac0a2d
SV
325 }
326
327 return ret;
328}
329
06c0dd96
RC
330static int modeset_init(struct mdp5_kms *mdp5_kms)
331{
332 static const enum mdp5_pipe crtcs[] = {
3d47fd47 333 SSPP_RGB0, SSPP_RGB1, SSPP_RGB2, SSPP_RGB3,
06c0dd96 334 };
8155ad4c 335 static const enum mdp5_pipe vig_planes[] = {
0deed25b
SV
336 SSPP_VIG0, SSPP_VIG1, SSPP_VIG2, SSPP_VIG3,
337 };
8155ad4c 338 static const enum mdp5_pipe dma_planes[] = {
339 SSPP_DMA0, SSPP_DMA1,
340 };
06c0dd96
RC
341 struct drm_device *dev = mdp5_kms->dev;
342 struct msm_drm_private *priv = dev->dev_private;
2e362e17 343 const struct mdp5_cfg_hw *hw_cfg;
06c0dd96
RC
344 int i, ret;
345
42238da8 346 hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
2e362e17 347
f6a8eaca
RC
348 /* register our interrupt-controller for hdmi/eDP/dsi/etc
349 * to use for irqs routed through mdp:
350 */
351 ret = mdp5_irq_domain_init(mdp5_kms);
352 if (ret)
353 goto fail;
354
0deed25b 355 /* construct CRTCs and their private planes: */
2e362e17 356 for (i = 0; i < hw_cfg->pipe_rgb.count; i++) {
06c0dd96
RC
357 struct drm_plane *plane;
358 struct drm_crtc *crtc;
359
0deed25b 360 plane = mdp5_plane_init(dev, crtcs[i], true,
3498409f 361 hw_cfg->pipe_rgb.base[i], hw_cfg->pipe_rgb.caps);
06c0dd96
RC
362 if (IS_ERR(plane)) {
363 ret = PTR_ERR(plane);
364 dev_err(dev->dev, "failed to construct plane for %s (%d)\n",
365 pipe2name(crtcs[i]), ret);
366 goto fail;
367 }
368
369 crtc = mdp5_crtc_init(dev, plane, i);
370 if (IS_ERR(crtc)) {
371 ret = PTR_ERR(crtc);
372 dev_err(dev->dev, "failed to construct crtc for %s (%d)\n",
373 pipe2name(crtcs[i]), ret);
374 goto fail;
375 }
376 priv->crtcs[priv->num_crtcs++] = crtc;
377 }
378
8155ad4c 379 /* Construct video planes: */
0deed25b
SV
380 for (i = 0; i < hw_cfg->pipe_vig.count; i++) {
381 struct drm_plane *plane;
382
8155ad4c 383 plane = mdp5_plane_init(dev, vig_planes[i], false,
3498409f 384 hw_cfg->pipe_vig.base[i], hw_cfg->pipe_vig.caps);
0deed25b
SV
385 if (IS_ERR(plane)) {
386 ret = PTR_ERR(plane);
387 dev_err(dev->dev, "failed to construct %s plane: %d\n",
8155ad4c 388 pipe2name(vig_planes[i]), ret);
389 goto fail;
390 }
391 }
392
393 /* DMA planes */
394 for (i = 0; i < hw_cfg->pipe_dma.count; i++) {
395 struct drm_plane *plane;
396
397 plane = mdp5_plane_init(dev, dma_planes[i], false,
398 hw_cfg->pipe_dma.base[i], hw_cfg->pipe_dma.caps);
399 if (IS_ERR(plane)) {
400 ret = PTR_ERR(plane);
401 dev_err(dev->dev, "failed to construct %s plane: %d\n",
402 pipe2name(dma_planes[i]), ret);
0deed25b
SV
403 goto fail;
404 }
405 }
406
5722a9e3
HL
407 /* Construct encoders and modeset initialize connector devices
408 * for each external display interface.
409 */
fe34464d 410 for (i = 0; i < ARRAY_SIZE(hw_cfg->intf.connect); i++) {
5722a9e3 411 ret = modeset_init_intf(mdp5_kms, i);
67ac0a2d 412 if (ret)
00453981 413 goto fail;
00453981
HL
414 }
415
06c0dd96
RC
416 return 0;
417
418fail:
419 return ret;
420}
421
2e362e17
SV
422static void read_hw_revision(struct mdp5_kms *mdp5_kms,
423 uint32_t *major, uint32_t *minor)
424{
425 uint32_t version;
426
427 mdp5_enable(mdp5_kms);
f5253812 428 version = mdp5_read(mdp5_kms, REG_MDSS_HW_VERSION);
2e362e17
SV
429 mdp5_disable(mdp5_kms);
430
f5253812
SV
431 *major = FIELD(version, MDSS_HW_VERSION_MAJOR);
432 *minor = FIELD(version, MDSS_HW_VERSION_MINOR);
2e362e17
SV
433
434 DBG("MDP5 version v%d.%d", *major, *minor);
435}
436
06c0dd96 437static int get_clk(struct platform_device *pdev, struct clk **clkp,
d40325b4 438 const char *name, bool mandatory)
06c0dd96
RC
439{
440 struct device *dev = &pdev->dev;
441 struct clk *clk = devm_clk_get(dev, name);
d40325b4 442 if (IS_ERR(clk) && mandatory) {
06c0dd96
RC
443 dev_err(dev, "failed to get %s (%ld)\n", name, PTR_ERR(clk));
444 return PTR_ERR(clk);
445 }
d40325b4
SV
446 if (IS_ERR(clk))
447 DBG("skipping %s", name);
448 else
449 *clkp = clk;
450
06c0dd96
RC
451 return 0;
452}
453
e2dd9f9f
AT
454static struct drm_encoder *get_encoder_from_crtc(struct drm_crtc *crtc)
455{
456 struct drm_device *dev = crtc->dev;
457 struct drm_encoder *encoder;
458
459 drm_for_each_encoder(encoder, dev)
460 if (encoder->crtc == crtc)
461 return encoder;
462
463 return NULL;
464}
465
466static int mdp5_get_scanoutpos(struct drm_device *dev, unsigned int pipe,
467 unsigned int flags, int *vpos, int *hpos,
468 ktime_t *stime, ktime_t *etime,
469 const struct drm_display_mode *mode)
470{
471 struct msm_drm_private *priv = dev->dev_private;
472 struct drm_crtc *crtc;
473 struct drm_encoder *encoder;
474 int line, vsw, vbp, vactive_start, vactive_end, vfp_end;
475 int ret = 0;
476
477 crtc = priv->crtcs[pipe];
478 if (!crtc) {
479 DRM_ERROR("Invalid crtc %d\n", pipe);
480 return 0;
481 }
482
483 encoder = get_encoder_from_crtc(crtc);
484 if (!encoder) {
485 DRM_ERROR("no encoder found for crtc %d\n", pipe);
486 return 0;
487 }
488
489 ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
490
491 vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
492 vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
493
494 /*
495 * the line counter is 1 at the start of the VSYNC pulse and VTOTAL at
496 * the end of VFP. Translate the porch values relative to the line
497 * counter positions.
498 */
499
500 vactive_start = vsw + vbp + 1;
501
502 vactive_end = vactive_start + mode->crtc_vdisplay;
503
504 /* last scan line before VSYNC */
505 vfp_end = mode->crtc_vtotal;
506
507 if (stime)
508 *stime = ktime_get();
509
510 line = mdp5_encoder_get_linecount(encoder);
511
512 if (line < vactive_start) {
513 line -= vactive_start;
514 ret |= DRM_SCANOUTPOS_IN_VBLANK;
515 } else if (line > vactive_end) {
516 line = line - vfp_end - vactive_start;
517 ret |= DRM_SCANOUTPOS_IN_VBLANK;
518 } else {
519 line -= vactive_start;
520 }
521
522 *vpos = line;
523 *hpos = 0;
524
525 if (etime)
526 *etime = ktime_get();
527
528 return ret;
529}
530
531static int mdp5_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
532 int *max_error,
533 struct timeval *vblank_time,
534 unsigned flags)
535{
536 struct msm_drm_private *priv = dev->dev_private;
537 struct drm_crtc *crtc;
538
539 if (pipe < 0 || pipe >= priv->num_crtcs) {
540 DRM_ERROR("Invalid crtc %d\n", pipe);
541 return -EINVAL;
542 }
543
544 crtc = priv->crtcs[pipe];
545 if (!crtc) {
546 DRM_ERROR("Invalid crtc %d\n", pipe);
547 return -EINVAL;
548 }
549
550 return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
551 vblank_time, flags,
552 &crtc->mode);
553}
554
555static u32 mdp5_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
556{
557 struct msm_drm_private *priv = dev->dev_private;
558 struct drm_crtc *crtc;
559 struct drm_encoder *encoder;
560
561 if (pipe < 0 || pipe >= priv->num_crtcs)
562 return 0;
563
564 crtc = priv->crtcs[pipe];
565 if (!crtc)
566 return 0;
567
568 encoder = get_encoder_from_crtc(crtc);
569 if (!encoder)
570 return 0;
571
572 return mdp5_encoder_get_framecount(encoder);
573}
574
06c0dd96
RC
575struct msm_kms *mdp5_kms_init(struct drm_device *dev)
576{
577 struct platform_device *pdev = dev->platformdev;
2e362e17 578 struct mdp5_cfg *config;
06c0dd96
RC
579 struct mdp5_kms *mdp5_kms;
580 struct msm_kms *kms = NULL;
581 struct msm_mmu *mmu;
2e362e17 582 uint32_t major, minor;
a2b3a557 583 int irq, i, ret;
06c0dd96
RC
584
585 mdp5_kms = kzalloc(sizeof(*mdp5_kms), GFP_KERNEL);
586 if (!mdp5_kms) {
587 dev_err(dev->dev, "failed to allocate kms\n");
588 ret = -ENOMEM;
589 goto fail;
590 }
591
0deed25b
SV
592 spin_lock_init(&mdp5_kms->resource_lock);
593
06c0dd96
RC
594 mdp_kms_init(&mdp5_kms->base, &kms_funcs);
595
596 kms = &mdp5_kms->base.base;
597
598 mdp5_kms->dev = dev;
06c0dd96 599
f5253812 600 /* mdp5_kms->mmio actually represents the MDSS base address */
06c0dd96
RC
601 mdp5_kms->mmio = msm_ioremap(pdev, "mdp_phys", "MDP5");
602 if (IS_ERR(mdp5_kms->mmio)) {
603 ret = PTR_ERR(mdp5_kms->mmio);
604 goto fail;
605 }
606
607 mdp5_kms->vbif = msm_ioremap(pdev, "vbif_phys", "VBIF");
608 if (IS_ERR(mdp5_kms->vbif)) {
609 ret = PTR_ERR(mdp5_kms->vbif);
610 goto fail;
611 }
612
a2b3a557
AT
613 irq = platform_get_irq(pdev, 0);
614 if (irq < 0) {
615 ret = irq;
616 dev_err(dev->dev, "failed to get irq: %d\n", ret);
617 goto fail;
618 }
619
620 kms->irq = irq;
621
06c0dd96
RC
622 mdp5_kms->vdd = devm_regulator_get(&pdev->dev, "vdd");
623 if (IS_ERR(mdp5_kms->vdd)) {
624 ret = PTR_ERR(mdp5_kms->vdd);
625 goto fail;
626 }
627
628 ret = regulator_enable(mdp5_kms->vdd);
629 if (ret) {
630 dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
631 goto fail;
632 }
633
d40325b4
SV
634 /* mandatory clocks: */
635 ret = get_clk(pdev, &mdp5_kms->axi_clk, "bus_clk", true);
a0906a02
RC
636 if (ret)
637 goto fail;
d40325b4 638 ret = get_clk(pdev, &mdp5_kms->ahb_clk, "iface_clk", true);
a0906a02
RC
639 if (ret)
640 goto fail;
d40325b4 641 ret = get_clk(pdev, &mdp5_kms->core_clk, "core_clk", true);
a0906a02
RC
642 if (ret)
643 goto fail;
d40325b4 644 ret = get_clk(pdev, &mdp5_kms->vsync_clk, "vsync_clk", true);
06c0dd96
RC
645 if (ret)
646 goto fail;
647
d40325b4
SV
648 /* optional clocks: */
649 get_clk(pdev, &mdp5_kms->lut_clk, "lut_clk", false);
650
ac7a5704
RC
651 /* we need to set a default rate before enabling. Set a safe
652 * rate first, then figure out hw revision, and then set a
653 * more optimal rate:
654 */
0e0d9dfe 655 clk_set_rate(mdp5_kms->core_clk, 200000000);
ac7a5704 656
2e362e17 657 read_hw_revision(mdp5_kms, &major, &minor);
42238da8
RC
658
659 mdp5_kms->cfg = mdp5_cfg_init(mdp5_kms, major, minor);
660 if (IS_ERR(mdp5_kms->cfg)) {
661 ret = PTR_ERR(mdp5_kms->cfg);
662 mdp5_kms->cfg = NULL;
3d47fd47 663 goto fail;
2e362e17 664 }
42238da8
RC
665
666 config = mdp5_cfg_get_config(mdp5_kms->cfg);
d879eb5a 667 mdp5_kms->caps = config->hw->mdp.caps;
3d47fd47 668
3f307963 669 /* TODO: compute core clock rate at runtime */
0e0d9dfe 670 clk_set_rate(mdp5_kms->core_clk, config->hw->max_clk);
3f307963 671
d879eb5a
SV
672 /*
673 * Some chipsets have a Shared Memory Pool (SMP), while others
674 * have dedicated latency buffering per source pipe instead;
675 * this section initializes the SMP:
676 */
677 if (mdp5_kms->caps & MDP_CAP_SMP) {
678 mdp5_kms->smp = mdp5_smp_init(mdp5_kms->dev, &config->hw->smp);
679 if (IS_ERR(mdp5_kms->smp)) {
680 ret = PTR_ERR(mdp5_kms->smp);
681 mdp5_kms->smp = NULL;
682 goto fail;
683 }
bfcdfb0e 684 }
bfcdfb0e 685
b96b3a06 686 mdp5_kms->ctlm = mdp5_ctlm_init(dev, mdp5_kms->mmio, mdp5_kms->cfg);
42238da8
RC
687 if (IS_ERR(mdp5_kms->ctlm)) {
688 ret = PTR_ERR(mdp5_kms->ctlm);
689 mdp5_kms->ctlm = NULL;
0deed25b
SV
690 goto fail;
691 }
0deed25b 692
06c0dd96
RC
693 /* make sure things are off before attaching iommu (bootloader could
694 * have left things on, in which case we'll start getting faults if
695 * we don't disable):
696 */
697 mdp5_enable(mdp5_kms);
67ac0a2d 698 for (i = 0; i < MDP5_INTF_NUM_MAX; i++) {
fe34464d
SV
699 if (mdp5_cfg_intf_is_virtual(config->hw->intf.connect[i]) ||
700 !config->hw->intf.base[i])
67ac0a2d 701 continue;
3d47fd47 702 mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(i), 0);
e2dd9f9f
AT
703
704 mdp5_write(mdp5_kms, REG_MDP5_INTF_FRAME_LINE_COUNT_EN(i), 0x3);
67ac0a2d 705 }
06c0dd96
RC
706 mdp5_disable(mdp5_kms);
707 mdelay(16);
708
2e362e17
SV
709 if (config->platform.iommu) {
710 mmu = msm_iommu_new(&pdev->dev, config->platform.iommu);
06c0dd96
RC
711 if (IS_ERR(mmu)) {
712 ret = PTR_ERR(mmu);
87e956e9 713 dev_err(dev->dev, "failed to init iommu: %d\n", ret);
5e921b19 714 iommu_domain_free(config->platform.iommu);
06c0dd96
RC
715 goto fail;
716 }
87e956e9 717
06c0dd96
RC
718 ret = mmu->funcs->attach(mmu, iommu_ports,
719 ARRAY_SIZE(iommu_ports));
87e956e9
SV
720 if (ret) {
721 dev_err(dev->dev, "failed to attach iommu: %d\n", ret);
722 mmu->funcs->destroy(mmu);
06c0dd96 723 goto fail;
87e956e9 724 }
06c0dd96
RC
725 } else {
726 dev_info(dev->dev, "no iommu, fallback to phys "
727 "contig buffers for scanout\n");
728 mmu = NULL;
729 }
87e956e9 730 mdp5_kms->mmu = mmu;
06c0dd96
RC
731
732 mdp5_kms->id = msm_register_mmu(dev, mmu);
733 if (mdp5_kms->id < 0) {
734 ret = mdp5_kms->id;
735 dev_err(dev->dev, "failed to register mdp5 iommu: %d\n", ret);
736 goto fail;
737 }
738
739 ret = modeset_init(mdp5_kms);
740 if (ret) {
741 dev_err(dev->dev, "modeset_init failed: %d\n", ret);
742 goto fail;
743 }
744
9b7a9fc2
HL
745 dev->mode_config.min_width = 0;
746 dev->mode_config.min_height = 0;
747 dev->mode_config.max_width = config->hw->lm.max_width;
748 dev->mode_config.max_height = config->hw->lm.max_height;
749
e2dd9f9f
AT
750 dev->driver->get_vblank_timestamp = mdp5_get_vblank_timestamp;
751 dev->driver->get_scanout_position = mdp5_get_scanoutpos;
752 dev->driver->get_vblank_counter = mdp5_get_vblank_counter;
753 dev->max_vblank_count = 0xffffffff;
754 dev->vblank_disable_immediate = true;
755
06c0dd96
RC
756 return kms;
757
758fail:
759 if (kms)
760 mdp5_destroy(kms);
761 return ERR_PTR(ret);
762}