]> Git Repo - linux.git/blob - drivers/gpu/drm/mxsfb/mxsfb_drv.c
drm/amdgpu: revert "add new bo flag that indicates BOs don't need fallback (v2)"
[linux.git] / drivers / gpu / drm / mxsfb / mxsfb_drv.c
1 /*
2  * Copyright (C) 2016 Marek Vasut <[email protected]>
3  *
4  * This code is based on drivers/video/fbdev/mxsfb.c :
5  * Copyright (C) 2010 Juergen Beisert, Pengutronix
6  * Copyright (C) 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
7  * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #include <linux/module.h>
20 #include <linux/spinlock.h>
21 #include <linux/clk.h>
22 #include <linux/component.h>
23 #include <linux/list.h>
24 #include <linux/of_device.h>
25 #include <linux/of_graph.h>
26 #include <linux/of_reserved_mem.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/reservation.h>
29
30 #include <drm/drmP.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_fb_helper.h>
36 #include <drm/drm_fb_cma_helper.h>
37 #include <drm/drm_gem_cma_helper.h>
38 #include <drm/drm_gem_framebuffer_helper.h>
39 #include <drm/drm_of.h>
40 #include <drm/drm_panel.h>
41 #include <drm/drm_simple_kms_helper.h>
42
43 #include "mxsfb_drv.h"
44 #include "mxsfb_regs.h"
45
46 enum mxsfb_devtype {
47         MXSFB_V3,
48         MXSFB_V4,
49 };
50
51 static const struct mxsfb_devdata mxsfb_devdata[] = {
52         [MXSFB_V3] = {
53                 .transfer_count = LCDC_V3_TRANSFER_COUNT,
54                 .cur_buf        = LCDC_V3_CUR_BUF,
55                 .next_buf       = LCDC_V3_NEXT_BUF,
56                 .debug0         = LCDC_V3_DEBUG0,
57                 .hs_wdth_mask   = 0xff,
58                 .hs_wdth_shift  = 24,
59                 .ipversion      = 3,
60         },
61         [MXSFB_V4] = {
62                 .transfer_count = LCDC_V4_TRANSFER_COUNT,
63                 .cur_buf        = LCDC_V4_CUR_BUF,
64                 .next_buf       = LCDC_V4_NEXT_BUF,
65                 .debug0         = LCDC_V4_DEBUG0,
66                 .hs_wdth_mask   = 0x3fff,
67                 .hs_wdth_shift  = 18,
68                 .ipversion      = 4,
69         },
70 };
71
72 static const uint32_t mxsfb_formats[] = {
73         DRM_FORMAT_XRGB8888,
74         DRM_FORMAT_RGB565
75 };
76
77 static struct mxsfb_drm_private *
78 drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe)
79 {
80         return container_of(pipe, struct mxsfb_drm_private, pipe);
81 }
82
83 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
84 {
85         if (mxsfb->clk_axi)
86                 clk_prepare_enable(mxsfb->clk_axi);
87 }
88
89 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
90 {
91         if (mxsfb->clk_axi)
92                 clk_disable_unprepare(mxsfb->clk_axi);
93 }
94
95 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
96         .fb_create              = drm_gem_fb_create,
97         .atomic_check           = drm_atomic_helper_check,
98         .atomic_commit          = drm_atomic_helper_commit,
99 };
100
101 static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe,
102                               struct drm_crtc_state *crtc_state)
103 {
104         struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
105
106         drm_panel_prepare(mxsfb->panel);
107         mxsfb_crtc_enable(mxsfb);
108         drm_panel_enable(mxsfb->panel);
109 }
110
111 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
112 {
113         struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
114
115         drm_panel_disable(mxsfb->panel);
116         mxsfb_crtc_disable(mxsfb);
117         drm_panel_unprepare(mxsfb->panel);
118 }
119
120 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
121                               struct drm_plane_state *plane_state)
122 {
123         struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
124
125         mxsfb_plane_atomic_update(mxsfb, plane_state);
126 }
127
128 static int mxsfb_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
129                                  struct drm_plane_state *plane_state)
130 {
131         return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
132 }
133
134 static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe)
135 {
136         struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
137
138         /* Clear and enable VBLANK IRQ */
139         mxsfb_enable_axi_clk(mxsfb);
140         writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
141         writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
142         mxsfb_disable_axi_clk(mxsfb);
143
144         return 0;
145 }
146
147 static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe)
148 {
149         struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
150
151         /* Disable and clear VBLANK IRQ */
152         mxsfb_enable_axi_clk(mxsfb);
153         writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
154         writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
155         mxsfb_disable_axi_clk(mxsfb);
156 }
157
158 static struct drm_simple_display_pipe_funcs mxsfb_funcs = {
159         .enable         = mxsfb_pipe_enable,
160         .disable        = mxsfb_pipe_disable,
161         .update         = mxsfb_pipe_update,
162         .prepare_fb     = mxsfb_pipe_prepare_fb,
163         .enable_vblank  = mxsfb_pipe_enable_vblank,
164         .disable_vblank = mxsfb_pipe_disable_vblank,
165 };
166
167 static int mxsfb_load(struct drm_device *drm, unsigned long flags)
168 {
169         struct platform_device *pdev = to_platform_device(drm->dev);
170         struct mxsfb_drm_private *mxsfb;
171         struct resource *res;
172         int ret;
173
174         mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
175         if (!mxsfb)
176                 return -ENOMEM;
177
178         drm->dev_private = mxsfb;
179         mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
180
181         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182         mxsfb->base = devm_ioremap_resource(drm->dev, res);
183         if (IS_ERR(mxsfb->base))
184                 return PTR_ERR(mxsfb->base);
185
186         mxsfb->clk = devm_clk_get(drm->dev, NULL);
187         if (IS_ERR(mxsfb->clk))
188                 return PTR_ERR(mxsfb->clk);
189
190         mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
191         if (IS_ERR(mxsfb->clk_axi))
192                 mxsfb->clk_axi = NULL;
193
194         mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
195         if (IS_ERR(mxsfb->clk_disp_axi))
196                 mxsfb->clk_disp_axi = NULL;
197
198         ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
199         if (ret)
200                 return ret;
201
202         pm_runtime_enable(drm->dev);
203
204         ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
205         if (ret < 0) {
206                 dev_err(drm->dev, "Failed to initialise vblank\n");
207                 goto err_vblank;
208         }
209
210         /* Modeset init */
211         drm_mode_config_init(drm);
212
213         ret = mxsfb_create_output(drm);
214         if (ret < 0) {
215                 dev_err(drm->dev, "Failed to create outputs\n");
216                 goto err_vblank;
217         }
218
219         ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
220                         mxsfb_formats, ARRAY_SIZE(mxsfb_formats), NULL,
221                         &mxsfb->connector);
222         if (ret < 0) {
223                 dev_err(drm->dev, "Cannot setup simple display pipe\n");
224                 goto err_vblank;
225         }
226
227         ret = drm_panel_attach(mxsfb->panel, &mxsfb->connector);
228         if (ret) {
229                 dev_err(drm->dev, "Cannot connect panel\n");
230                 goto err_vblank;
231         }
232
233         drm->mode_config.min_width      = MXSFB_MIN_XRES;
234         drm->mode_config.min_height     = MXSFB_MIN_YRES;
235         drm->mode_config.max_width      = MXSFB_MAX_XRES;
236         drm->mode_config.max_height     = MXSFB_MAX_YRES;
237         drm->mode_config.funcs          = &mxsfb_mode_config_funcs;
238
239         drm_mode_config_reset(drm);
240
241         pm_runtime_get_sync(drm->dev);
242         ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
243         pm_runtime_put_sync(drm->dev);
244
245         if (ret < 0) {
246                 dev_err(drm->dev, "Failed to install IRQ handler\n");
247                 goto err_irq;
248         }
249
250         drm_kms_helper_poll_init(drm);
251
252         mxsfb->fbdev = drm_fbdev_cma_init(drm, 32,
253                                           drm->mode_config.num_connector);
254         if (IS_ERR(mxsfb->fbdev)) {
255                 ret = PTR_ERR(mxsfb->fbdev);
256                 mxsfb->fbdev = NULL;
257                 dev_err(drm->dev, "Failed to init FB CMA area\n");
258                 goto err_cma;
259         }
260
261         platform_set_drvdata(pdev, drm);
262
263         drm_helper_hpd_irq_event(drm);
264
265         return 0;
266
267 err_cma:
268         drm_irq_uninstall(drm);
269 err_irq:
270         drm_panel_detach(mxsfb->panel);
271 err_vblank:
272         pm_runtime_disable(drm->dev);
273
274         return ret;
275 }
276
277 static void mxsfb_unload(struct drm_device *drm)
278 {
279         struct mxsfb_drm_private *mxsfb = drm->dev_private;
280
281         if (mxsfb->fbdev)
282                 drm_fbdev_cma_fini(mxsfb->fbdev);
283
284         drm_kms_helper_poll_fini(drm);
285         drm_mode_config_cleanup(drm);
286
287         pm_runtime_get_sync(drm->dev);
288         drm_irq_uninstall(drm);
289         pm_runtime_put_sync(drm->dev);
290
291         drm->dev_private = NULL;
292
293         pm_runtime_disable(drm->dev);
294 }
295
296 static void mxsfb_lastclose(struct drm_device *drm)
297 {
298         struct mxsfb_drm_private *mxsfb = drm->dev_private;
299
300         drm_fbdev_cma_restore_mode(mxsfb->fbdev);
301 }
302
303 static void mxsfb_irq_preinstall(struct drm_device *drm)
304 {
305         struct mxsfb_drm_private *mxsfb = drm->dev_private;
306
307         mxsfb_pipe_disable_vblank(&mxsfb->pipe);
308 }
309
310 static irqreturn_t mxsfb_irq_handler(int irq, void *data)
311 {
312         struct drm_device *drm = data;
313         struct mxsfb_drm_private *mxsfb = drm->dev_private;
314         u32 reg;
315
316         mxsfb_enable_axi_clk(mxsfb);
317
318         reg = readl(mxsfb->base + LCDC_CTRL1);
319
320         if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
321                 drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
322
323         writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
324
325         mxsfb_disable_axi_clk(mxsfb);
326
327         return IRQ_HANDLED;
328 }
329
330 DEFINE_DRM_GEM_CMA_FOPS(fops);
331
332 static struct drm_driver mxsfb_driver = {
333         .driver_features        = DRIVER_GEM | DRIVER_MODESET |
334                                   DRIVER_PRIME | DRIVER_ATOMIC |
335                                   DRIVER_HAVE_IRQ,
336         .lastclose              = mxsfb_lastclose,
337         .irq_handler            = mxsfb_irq_handler,
338         .irq_preinstall         = mxsfb_irq_preinstall,
339         .irq_uninstall          = mxsfb_irq_preinstall,
340         .gem_free_object_unlocked = drm_gem_cma_free_object,
341         .gem_vm_ops             = &drm_gem_cma_vm_ops,
342         .dumb_create            = drm_gem_cma_dumb_create,
343         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
344         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
345         .gem_prime_export       = drm_gem_prime_export,
346         .gem_prime_import       = drm_gem_prime_import,
347         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
348         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
349         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
350         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
351         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
352         .fops   = &fops,
353         .name   = "mxsfb-drm",
354         .desc   = "MXSFB Controller DRM",
355         .date   = "20160824",
356         .major  = 1,
357         .minor  = 0,
358 };
359
360 static const struct platform_device_id mxsfb_devtype[] = {
361         { .name = "imx23-fb", .driver_data = MXSFB_V3, },
362         { .name = "imx28-fb", .driver_data = MXSFB_V4, },
363         { .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
364         { /* sentinel */ }
365 };
366 MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
367
368 static const struct of_device_id mxsfb_dt_ids[] = {
369         { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
370         { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
371         { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
372         { /* sentinel */ }
373 };
374 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
375
376 static int mxsfb_probe(struct platform_device *pdev)
377 {
378         struct drm_device *drm;
379         const struct of_device_id *of_id =
380                         of_match_device(mxsfb_dt_ids, &pdev->dev);
381         int ret;
382
383         if (!pdev->dev.of_node)
384                 return -ENODEV;
385
386         if (of_id)
387                 pdev->id_entry = of_id->data;
388
389         drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
390         if (IS_ERR(drm))
391                 return PTR_ERR(drm);
392
393         ret = mxsfb_load(drm, 0);
394         if (ret)
395                 goto err_free;
396
397         ret = drm_dev_register(drm, 0);
398         if (ret)
399                 goto err_unload;
400
401         return 0;
402
403 err_unload:
404         mxsfb_unload(drm);
405 err_free:
406         drm_dev_unref(drm);
407
408         return ret;
409 }
410
411 static int mxsfb_remove(struct platform_device *pdev)
412 {
413         struct drm_device *drm = platform_get_drvdata(pdev);
414
415         drm_dev_unregister(drm);
416         mxsfb_unload(drm);
417         drm_dev_unref(drm);
418
419         return 0;
420 }
421
422 static struct platform_driver mxsfb_platform_driver = {
423         .probe          = mxsfb_probe,
424         .remove         = mxsfb_remove,
425         .id_table       = mxsfb_devtype,
426         .driver = {
427                 .name           = "mxsfb",
428                 .of_match_table = mxsfb_dt_ids,
429         },
430 };
431
432 module_platform_driver(mxsfb_platform_driver);
433
434 MODULE_AUTHOR("Marek Vasut <[email protected]>");
435 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
436 MODULE_LICENSE("GPL");
This page took 0.059771 seconds and 4 git commands to generate.