Merge tag 'iio-fixes-for-3.16c' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / gpu / drm / msm / mdp / mdp5 / mdp5_kms.c
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18
19 #include "msm_drv.h"
20 #include "msm_mmu.h"
21 #include "mdp5_kms.h"
22
23 static const char *iommu_ports[] = {
24                 "mdp_0",
25 };
26
27 static struct mdp5_platform_config *mdp5_get_config(struct platform_device *dev);
28
29 static int mdp5_hw_init(struct msm_kms *kms)
30 {
31         struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
32         struct drm_device *dev = mdp5_kms->dev;
33         uint32_t version, major, minor;
34         int ret = 0;
35
36         pm_runtime_get_sync(dev->dev);
37
38         mdp5_enable(mdp5_kms);
39         version = mdp5_read(mdp5_kms, REG_MDP5_MDP_VERSION);
40         mdp5_disable(mdp5_kms);
41
42         major = FIELD(version, MDP5_MDP_VERSION_MAJOR);
43         minor = FIELD(version, MDP5_MDP_VERSION_MINOR);
44
45         DBG("found MDP5 version v%d.%d", major, minor);
46
47         if ((major != 1) || ((minor != 0) && (minor != 2))) {
48                 dev_err(dev->dev, "unexpected MDP version: v%d.%d\n",
49                                 major, minor);
50                 ret = -ENXIO;
51                 goto out;
52         }
53
54         mdp5_kms->rev = minor;
55
56         /* Magic unknown register writes:
57          *
58          *    W VBIF:0x004 00000001      (mdss_mdp.c:839)
59          *    W MDP5:0x2e0 0xe9          (mdss_mdp.c:839)
60          *    W MDP5:0x2e4 0x55          (mdss_mdp.c:839)
61          *    W MDP5:0x3ac 0xc0000ccc    (mdss_mdp.c:839)
62          *    W MDP5:0x3b4 0xc0000ccc    (mdss_mdp.c:839)
63          *    W MDP5:0x3bc 0xcccccc      (mdss_mdp.c:839)
64          *    W MDP5:0x4a8 0xcccc0c0     (mdss_mdp.c:839)
65          *    W MDP5:0x4b0 0xccccc0c0    (mdss_mdp.c:839)
66          *    W MDP5:0x4b8 0xccccc000    (mdss_mdp.c:839)
67          *
68          * Downstream fbdev driver gets these register offsets/values
69          * from DT.. not really sure what these registers are or if
70          * different values for different boards/SoC's, etc.  I guess
71          * they are the golden registers.
72          *
73          * Not setting these does not seem to cause any problem.  But
74          * we may be getting lucky with the bootloader initializing
75          * them for us.  OTOH, if we can always count on the bootloader
76          * setting the golden registers, then perhaps we don't need to
77          * care.
78          */
79
80         mdp5_write(mdp5_kms, REG_MDP5_DISP_INTF_SEL, 0);
81         mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(0), 0);
82         mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(1), 0);
83         mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(2), 0);
84         mdp5_write(mdp5_kms, REG_MDP5_CTL_OP(3), 0);
85
86 out:
87         pm_runtime_put_sync(dev->dev);
88
89         return ret;
90 }
91
92 static long mdp5_round_pixclk(struct msm_kms *kms, unsigned long rate,
93                 struct drm_encoder *encoder)
94 {
95         return rate;
96 }
97
98 static void mdp5_preclose(struct msm_kms *kms, struct drm_file *file)
99 {
100         struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
101         struct msm_drm_private *priv = mdp5_kms->dev->dev_private;
102         unsigned i;
103
104         for (i = 0; i < priv->num_crtcs; i++)
105                 mdp5_crtc_cancel_pending_flip(priv->crtcs[i], file);
106 }
107
108 static void mdp5_destroy(struct msm_kms *kms)
109 {
110         struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
111         struct msm_mmu *mmu = mdp5_kms->mmu;
112
113         if (mmu) {
114                 mmu->funcs->detach(mmu, iommu_ports, ARRAY_SIZE(iommu_ports));
115                 mmu->funcs->destroy(mmu);
116         }
117         kfree(mdp5_kms);
118 }
119
120 static const struct mdp_kms_funcs kms_funcs = {
121         .base = {
122                 .hw_init         = mdp5_hw_init,
123                 .irq_preinstall  = mdp5_irq_preinstall,
124                 .irq_postinstall = mdp5_irq_postinstall,
125                 .irq_uninstall   = mdp5_irq_uninstall,
126                 .irq             = mdp5_irq,
127                 .enable_vblank   = mdp5_enable_vblank,
128                 .disable_vblank  = mdp5_disable_vblank,
129                 .get_format      = mdp_get_format,
130                 .round_pixclk    = mdp5_round_pixclk,
131                 .preclose        = mdp5_preclose,
132                 .destroy         = mdp5_destroy,
133         },
134         .set_irqmask         = mdp5_set_irqmask,
135 };
136
137 int mdp5_disable(struct mdp5_kms *mdp5_kms)
138 {
139         DBG("");
140
141         clk_disable_unprepare(mdp5_kms->ahb_clk);
142         clk_disable_unprepare(mdp5_kms->axi_clk);
143         clk_disable_unprepare(mdp5_kms->core_clk);
144         clk_disable_unprepare(mdp5_kms->lut_clk);
145
146         return 0;
147 }
148
149 int mdp5_enable(struct mdp5_kms *mdp5_kms)
150 {
151         DBG("");
152
153         clk_prepare_enable(mdp5_kms->ahb_clk);
154         clk_prepare_enable(mdp5_kms->axi_clk);
155         clk_prepare_enable(mdp5_kms->core_clk);
156         clk_prepare_enable(mdp5_kms->lut_clk);
157
158         return 0;
159 }
160
161 static int modeset_init(struct mdp5_kms *mdp5_kms)
162 {
163         static const enum mdp5_pipe crtcs[] = {
164                         SSPP_RGB0, SSPP_RGB1, SSPP_RGB2,
165         };
166         struct drm_device *dev = mdp5_kms->dev;
167         struct msm_drm_private *priv = dev->dev_private;
168         struct drm_encoder *encoder;
169         int i, ret;
170
171         /* construct CRTCs: */
172         for (i = 0; i < ARRAY_SIZE(crtcs); i++) {
173                 struct drm_plane *plane;
174                 struct drm_crtc *crtc;
175
176                 plane = mdp5_plane_init(dev, crtcs[i], true);
177                 if (IS_ERR(plane)) {
178                         ret = PTR_ERR(plane);
179                         dev_err(dev->dev, "failed to construct plane for %s (%d)\n",
180                                         pipe2name(crtcs[i]), ret);
181                         goto fail;
182                 }
183
184                 crtc  = mdp5_crtc_init(dev, plane, i);
185                 if (IS_ERR(crtc)) {
186                         ret = PTR_ERR(crtc);
187                         dev_err(dev->dev, "failed to construct crtc for %s (%d)\n",
188                                         pipe2name(crtcs[i]), ret);
189                         goto fail;
190                 }
191                 priv->crtcs[priv->num_crtcs++] = crtc;
192         }
193
194         /* Construct encoder for HDMI: */
195         encoder = mdp5_encoder_init(dev, 3, INTF_HDMI);
196         if (IS_ERR(encoder)) {
197                 dev_err(dev->dev, "failed to construct encoder\n");
198                 ret = PTR_ERR(encoder);
199                 goto fail;
200         }
201
202         /* NOTE: the vsync and error irq's are actually associated with
203          * the INTF/encoder.. the easiest way to deal with this (ie. what
204          * we do now) is assume a fixed relationship between crtc's and
205          * encoders.  I'm not sure if there is ever a need to more freely
206          * assign crtcs to encoders, but if there is then we need to take
207          * care of error and vblank irq's that the crtc has registered,
208          * and also update user-requested vblank_mask.
209          */
210         encoder->possible_crtcs = BIT(0);
211         mdp5_crtc_set_intf(priv->crtcs[0], 3, INTF_HDMI);
212
213         priv->encoders[priv->num_encoders++] = encoder;
214
215         /* Construct bridge/connector for HDMI: */
216         mdp5_kms->hdmi = hdmi_init(dev, encoder);
217         if (IS_ERR(mdp5_kms->hdmi)) {
218                 ret = PTR_ERR(mdp5_kms->hdmi);
219                 dev_err(dev->dev, "failed to initialize HDMI: %d\n", ret);
220                 goto fail;
221         }
222
223         return 0;
224
225 fail:
226         return ret;
227 }
228
229 static int get_clk(struct platform_device *pdev, struct clk **clkp,
230                 const char *name)
231 {
232         struct device *dev = &pdev->dev;
233         struct clk *clk = devm_clk_get(dev, name);
234         if (IS_ERR(clk)) {
235                 dev_err(dev, "failed to get %s (%ld)\n", name, PTR_ERR(clk));
236                 return PTR_ERR(clk);
237         }
238         *clkp = clk;
239         return 0;
240 }
241
242 struct msm_kms *mdp5_kms_init(struct drm_device *dev)
243 {
244         struct platform_device *pdev = dev->platformdev;
245         struct mdp5_platform_config *config = mdp5_get_config(pdev);
246         struct mdp5_kms *mdp5_kms;
247         struct msm_kms *kms = NULL;
248         struct msm_mmu *mmu;
249         int ret;
250
251         mdp5_kms = kzalloc(sizeof(*mdp5_kms), GFP_KERNEL);
252         if (!mdp5_kms) {
253                 dev_err(dev->dev, "failed to allocate kms\n");
254                 ret = -ENOMEM;
255                 goto fail;
256         }
257
258         mdp_kms_init(&mdp5_kms->base, &kms_funcs);
259
260         kms = &mdp5_kms->base.base;
261
262         mdp5_kms->dev = dev;
263         mdp5_kms->smp_blk_cnt = config->smp_blk_cnt;
264
265         mdp5_kms->mmio = msm_ioremap(pdev, "mdp_phys", "MDP5");
266         if (IS_ERR(mdp5_kms->mmio)) {
267                 ret = PTR_ERR(mdp5_kms->mmio);
268                 goto fail;
269         }
270
271         mdp5_kms->vbif = msm_ioremap(pdev, "vbif_phys", "VBIF");
272         if (IS_ERR(mdp5_kms->vbif)) {
273                 ret = PTR_ERR(mdp5_kms->vbif);
274                 goto fail;
275         }
276
277         mdp5_kms->vdd = devm_regulator_get(&pdev->dev, "vdd");
278         if (IS_ERR(mdp5_kms->vdd)) {
279                 ret = PTR_ERR(mdp5_kms->vdd);
280                 goto fail;
281         }
282
283         ret = regulator_enable(mdp5_kms->vdd);
284         if (ret) {
285                 dev_err(dev->dev, "failed to enable regulator vdd: %d\n", ret);
286                 goto fail;
287         }
288
289         ret = get_clk(pdev, &mdp5_kms->axi_clk, "bus_clk");
290         if (ret)
291                 goto fail;
292         ret = get_clk(pdev, &mdp5_kms->ahb_clk, "iface_clk");
293         if (ret)
294                 goto fail;
295         ret = get_clk(pdev, &mdp5_kms->src_clk, "core_clk_src");
296         if (ret)
297                 goto fail;
298         ret = get_clk(pdev, &mdp5_kms->core_clk, "core_clk");
299         if (ret)
300                 goto fail;
301         ret = get_clk(pdev, &mdp5_kms->lut_clk, "lut_clk");
302         if (ret)
303                 goto fail;
304         ret = get_clk(pdev, &mdp5_kms->vsync_clk, "vsync_clk");
305         if (ret)
306                 goto fail;
307
308         ret = clk_set_rate(mdp5_kms->src_clk, config->max_clk);
309
310         /* make sure things are off before attaching iommu (bootloader could
311          * have left things on, in which case we'll start getting faults if
312          * we don't disable):
313          */
314         mdp5_enable(mdp5_kms);
315         mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(0), 0);
316         mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(1), 0);
317         mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(2), 0);
318         mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(3), 0);
319         mdp5_disable(mdp5_kms);
320         mdelay(16);
321
322         if (config->iommu) {
323                 mmu = msm_iommu_new(dev, config->iommu);
324                 if (IS_ERR(mmu)) {
325                         ret = PTR_ERR(mmu);
326                         dev_err(dev->dev, "failed to init iommu: %d\n", ret);
327                         goto fail;
328                 }
329
330                 ret = mmu->funcs->attach(mmu, iommu_ports,
331                                 ARRAY_SIZE(iommu_ports));
332                 if (ret) {
333                         dev_err(dev->dev, "failed to attach iommu: %d\n", ret);
334                         mmu->funcs->destroy(mmu);
335                         goto fail;
336                 }
337         } else {
338                 dev_info(dev->dev, "no iommu, fallback to phys "
339                                 "contig buffers for scanout\n");
340                 mmu = NULL;
341         }
342         mdp5_kms->mmu = mmu;
343
344         mdp5_kms->id = msm_register_mmu(dev, mmu);
345         if (mdp5_kms->id < 0) {
346                 ret = mdp5_kms->id;
347                 dev_err(dev->dev, "failed to register mdp5 iommu: %d\n", ret);
348                 goto fail;
349         }
350
351         ret = modeset_init(mdp5_kms);
352         if (ret) {
353                 dev_err(dev->dev, "modeset_init failed: %d\n", ret);
354                 goto fail;
355         }
356
357         return kms;
358
359 fail:
360         if (kms)
361                 mdp5_destroy(kms);
362         return ERR_PTR(ret);
363 }
364
365 static struct mdp5_platform_config *mdp5_get_config(struct platform_device *dev)
366 {
367         static struct mdp5_platform_config config = {};
368 #ifdef CONFIG_OF
369         /* TODO */
370 #endif
371         return &config;
372 }