2 * i.MX IPUv3 Graphics driver
4 * Copyright (C) 2011 Sascha Hauer, Pengutronix
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 #include <linux/module.h>
21 #include <linux/export.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
25 #include <drm/drm_fb_helper.h>
26 #include <drm/drm_crtc_helper.h>
28 #include <linux/clk.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_fb_cma_helper.h>
32 #include "ipu-v3/imx-ipu-v3.h"
35 #define DRIVER_DESC "i.MX IPUv3 Graphics"
37 struct ipu_framebuffer
{
38 struct drm_framebuffer base
;
45 struct drm_fb_helper fb_helper
;
46 struct ipu_framebuffer ifb
;
50 struct imx_drm_crtc
*imx_crtc
;
51 struct ipuv3_channel
*ipu_ch
;
54 struct dmfc_channel
*dmfc
;
57 struct ipu_priv
*ipu_priv
;
58 struct drm_pending_vblank_event
*page_flip_event
;
59 struct drm_framebuffer
*newfb
;
61 u32 interface_pix_fmt
;
62 unsigned long di_clkflags
;
65 #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
67 static int calc_vref(struct drm_display_mode
*mode
)
69 unsigned long htotal
, vtotal
;
71 htotal
= mode
->htotal
;
72 vtotal
= mode
->vtotal
;
74 if (!htotal
|| !vtotal
)
77 return mode
->clock
* 1000 / vtotal
/ htotal
;
80 static int calc_bandwidth(struct drm_display_mode
*mode
, unsigned int vref
)
82 return mode
->hdisplay
* mode
->vdisplay
* vref
;
85 static void ipu_fb_enable(struct ipu_crtc
*ipu_crtc
)
87 if (ipu_crtc
->enabled
)
90 ipu_di_enable(ipu_crtc
->di
);
91 ipu_dmfc_enable_channel(ipu_crtc
->dmfc
);
92 ipu_idmac_enable_channel(ipu_crtc
->ipu_ch
);
93 ipu_dc_enable_channel(ipu_crtc
->dc
);
95 ipu_dp_enable_channel(ipu_crtc
->dp
);
97 ipu_crtc
->enabled
= 1;
100 static void ipu_fb_disable(struct ipu_crtc
*ipu_crtc
)
102 if (!ipu_crtc
->enabled
)
106 ipu_dp_disable_channel(ipu_crtc
->dp
);
107 ipu_dc_disable_channel(ipu_crtc
->dc
);
108 ipu_idmac_disable_channel(ipu_crtc
->ipu_ch
);
109 ipu_dmfc_disable_channel(ipu_crtc
->dmfc
);
110 ipu_di_disable(ipu_crtc
->di
);
112 ipu_crtc
->enabled
= 0;
115 static void ipu_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
117 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
119 dev_dbg(ipu_crtc
->dev
, "%s mode: %d\n", __func__
, mode
);
122 case DRM_MODE_DPMS_ON
:
123 ipu_fb_enable(ipu_crtc
);
125 case DRM_MODE_DPMS_STANDBY
:
126 case DRM_MODE_DPMS_SUSPEND
:
127 case DRM_MODE_DPMS_OFF
:
128 ipu_fb_disable(ipu_crtc
);
133 static int ipu_page_flip(struct drm_crtc
*crtc
,
134 struct drm_framebuffer
*fb
,
135 struct drm_pending_vblank_event
*event
)
137 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
143 ret
= imx_drm_crtc_vblank_get(ipu_crtc
->imx_crtc
);
145 dev_dbg(ipu_crtc
->dev
, "failed to acquire vblank counter\n");
146 list_del(&event
->base
.link
);
151 ipu_crtc
->newfb
= fb
;
152 ipu_crtc
->page_flip_event
= event
;
157 static const struct drm_crtc_funcs ipu_crtc_funcs
= {
158 .set_config
= drm_crtc_helper_set_config
,
159 .destroy
= drm_crtc_cleanup
,
160 .page_flip
= ipu_page_flip
,
163 static int ipu_drm_set_base(struct drm_crtc
*crtc
, int x
, int y
)
165 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
166 struct drm_gem_cma_object
*cma_obj
;
167 struct drm_framebuffer
*fb
= crtc
->fb
;
170 cma_obj
= drm_fb_cma_get_gem_obj(fb
, 0);
172 DRM_LOG_KMS("entry is null.\n");
176 phys
= cma_obj
->paddr
;
177 phys
+= x
* (fb
->bits_per_pixel
>> 3);
178 phys
+= y
* fb
->pitches
[0];
180 dev_dbg(ipu_crtc
->dev
, "%s: phys: 0x%lx\n", __func__
, phys
);
181 dev_dbg(ipu_crtc
->dev
, "%s: xy: %dx%d\n", __func__
, x
, y
);
183 ipu_cpmem_set_stride(ipu_get_cpmem(ipu_crtc
->ipu_ch
), fb
->pitches
[0]);
184 ipu_cpmem_set_buffer(ipu_get_cpmem(ipu_crtc
->ipu_ch
),
190 static int ipu_crtc_mode_set(struct drm_crtc
*crtc
,
191 struct drm_display_mode
*orig_mode
,
192 struct drm_display_mode
*mode
,
194 struct drm_framebuffer
*old_fb
)
196 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
197 struct drm_framebuffer
*fb
= ipu_crtc
->base
.fb
;
199 struct ipu_di_signal_cfg sig_cfg
= {};
201 struct ipu_ch_param __iomem
*cpmem
= ipu_get_cpmem(ipu_crtc
->ipu_ch
);
205 dev_dbg(ipu_crtc
->dev
, "%s: mode->hdisplay: %d\n", __func__
,
207 dev_dbg(ipu_crtc
->dev
, "%s: mode->vdisplay: %d\n", __func__
,
210 ipu_ch_param_zero(cpmem
);
212 switch (fb
->pixel_format
) {
213 case DRM_FORMAT_XRGB8888
:
214 case DRM_FORMAT_ARGB8888
:
215 v4l2_fmt
= V4L2_PIX_FMT_RGB32
;
218 case DRM_FORMAT_RGB565
:
219 v4l2_fmt
= V4L2_PIX_FMT_RGB565
;
222 case DRM_FORMAT_RGB888
:
223 v4l2_fmt
= V4L2_PIX_FMT_RGB24
;
227 dev_err(ipu_crtc
->dev
, "unsupported pixel format 0x%08x\n",
232 out_pixel_fmt
= ipu_crtc
->interface_pix_fmt
;
234 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
235 sig_cfg
.interlaced
= 1;
236 if (mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
237 sig_cfg
.Hsync_pol
= 1;
238 if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
239 sig_cfg
.Vsync_pol
= 1;
241 sig_cfg
.enable_pol
= 1;
243 sig_cfg
.width
= mode
->hdisplay
;
244 sig_cfg
.height
= mode
->vdisplay
;
245 sig_cfg
.pixel_fmt
= out_pixel_fmt
;
246 sig_cfg
.h_start_width
= mode
->htotal
- mode
->hsync_end
;
247 sig_cfg
.h_sync_width
= mode
->hsync_end
- mode
->hsync_start
;
248 sig_cfg
.h_end_width
= mode
->hsync_start
- mode
->hdisplay
;
250 sig_cfg
.v_start_width
= mode
->vtotal
- mode
->vsync_end
;
251 sig_cfg
.v_sync_width
= mode
->vsync_end
- mode
->vsync_start
;
252 sig_cfg
.v_end_width
= mode
->vsync_start
- mode
->vdisplay
;
253 sig_cfg
.pixelclock
= mode
->clock
* 1000;
254 sig_cfg
.clkflags
= ipu_crtc
->di_clkflags
;
256 sig_cfg
.v_to_h_sync
= 0;
259 ret
= ipu_dp_setup_channel(ipu_crtc
->dp
, IPUV3_COLORSPACE_RGB
,
260 IPUV3_COLORSPACE_RGB
);
262 dev_err(ipu_crtc
->dev
,
263 "initializing display processor failed with %d\n",
267 ipu_dp_set_global_alpha(ipu_crtc
->dp
, 1, 0, 1);
270 ret
= ipu_dc_init_sync(ipu_crtc
->dc
, ipu_crtc
->di
, sig_cfg
.interlaced
,
271 out_pixel_fmt
, mode
->hdisplay
);
273 dev_err(ipu_crtc
->dev
,
274 "initializing display controller failed with %d\n",
279 ret
= ipu_di_init_sync_panel(ipu_crtc
->di
, &sig_cfg
);
281 dev_err(ipu_crtc
->dev
,
282 "initializing panel failed with %d\n", ret
);
286 ipu_cpmem_set_resolution(cpmem
, mode
->hdisplay
, mode
->vdisplay
);
287 ipu_cpmem_set_fmt(cpmem
, v4l2_fmt
);
288 ipu_cpmem_set_high_priority(ipu_crtc
->ipu_ch
);
290 ret
= ipu_dmfc_init_channel(ipu_crtc
->dmfc
, mode
->hdisplay
);
292 dev_err(ipu_crtc
->dev
,
293 "initializing dmfc channel failed with %d\n",
298 ret
= ipu_dmfc_alloc_bandwidth(ipu_crtc
->dmfc
,
299 calc_bandwidth(mode
, calc_vref(mode
)), 64);
301 dev_err(ipu_crtc
->dev
,
302 "allocating dmfc bandwidth failed with %d\n",
307 ipu_drm_set_base(crtc
, x
, y
);
312 static void ipu_crtc_handle_pageflip(struct ipu_crtc
*ipu_crtc
)
314 struct drm_pending_vblank_event
*e
;
317 struct drm_device
*drm
= ipu_crtc
->base
.dev
;
319 spin_lock_irqsave(&drm
->event_lock
, flags
);
321 e
= ipu_crtc
->page_flip_event
;
323 spin_unlock_irqrestore(&drm
->event_lock
, flags
);
327 do_gettimeofday(&now
);
328 e
->event
.sequence
= 0;
329 e
->event
.tv_sec
= now
.tv_sec
;
330 e
->event
.tv_usec
= now
.tv_usec
;
331 ipu_crtc
->page_flip_event
= NULL
;
333 imx_drm_crtc_vblank_put(ipu_crtc
->imx_crtc
);
335 list_add_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
337 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
339 spin_unlock_irqrestore(&drm
->event_lock
, flags
);
342 static irqreturn_t
ipu_irq_handler(int irq
, void *dev_id
)
344 struct ipu_crtc
*ipu_crtc
= dev_id
;
346 imx_drm_handle_vblank(ipu_crtc
->imx_crtc
);
348 if (ipu_crtc
->newfb
) {
349 ipu_crtc
->base
.fb
= ipu_crtc
->newfb
;
350 ipu_crtc
->newfb
= NULL
;
351 ipu_drm_set_base(&ipu_crtc
->base
, 0, 0);
352 ipu_crtc_handle_pageflip(ipu_crtc
);
358 static bool ipu_crtc_mode_fixup(struct drm_crtc
*crtc
,
359 const struct drm_display_mode
*mode
,
360 struct drm_display_mode
*adjusted_mode
)
365 static void ipu_crtc_prepare(struct drm_crtc
*crtc
)
367 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
369 ipu_fb_disable(ipu_crtc
);
372 static void ipu_crtc_commit(struct drm_crtc
*crtc
)
374 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
376 ipu_fb_enable(ipu_crtc
);
379 static void ipu_crtc_load_lut(struct drm_crtc
*crtc
)
383 static struct drm_crtc_helper_funcs ipu_helper_funcs
= {
384 .dpms
= ipu_crtc_dpms
,
385 .mode_fixup
= ipu_crtc_mode_fixup
,
386 .mode_set
= ipu_crtc_mode_set
,
387 .prepare
= ipu_crtc_prepare
,
388 .commit
= ipu_crtc_commit
,
389 .load_lut
= ipu_crtc_load_lut
,
392 static int ipu_enable_vblank(struct drm_crtc
*crtc
)
394 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
396 enable_irq(ipu_crtc
->irq
);
401 static void ipu_disable_vblank(struct drm_crtc
*crtc
)
403 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
405 disable_irq(ipu_crtc
->irq
);
408 static int ipu_set_interface_pix_fmt(struct drm_crtc
*crtc
, u32 encoder_type
,
411 struct ipu_crtc
*ipu_crtc
= to_ipu_crtc(crtc
);
413 ipu_crtc
->interface_pix_fmt
= pixfmt
;
415 switch (encoder_type
) {
416 case DRM_MODE_ENCODER_DAC
:
417 case DRM_MODE_ENCODER_TVDAC
:
418 case DRM_MODE_ENCODER_LVDS
:
419 ipu_crtc
->di_clkflags
= IPU_DI_CLKMODE_SYNC
|
422 case DRM_MODE_ENCODER_NONE
:
423 ipu_crtc
->di_clkflags
= 0;
430 static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs
= {
431 .enable_vblank
= ipu_enable_vblank
,
432 .disable_vblank
= ipu_disable_vblank
,
433 .set_interface_pix_fmt
= ipu_set_interface_pix_fmt
,
434 .crtc_funcs
= &ipu_crtc_funcs
,
435 .crtc_helper_funcs
= &ipu_helper_funcs
,
438 static void ipu_put_resources(struct ipu_crtc
*ipu_crtc
)
440 if (!IS_ERR_OR_NULL(ipu_crtc
->ipu_ch
))
441 ipu_idmac_put(ipu_crtc
->ipu_ch
);
442 if (!IS_ERR_OR_NULL(ipu_crtc
->dmfc
))
443 ipu_dmfc_put(ipu_crtc
->dmfc
);
444 if (!IS_ERR_OR_NULL(ipu_crtc
->dp
))
445 ipu_dp_put(ipu_crtc
->dp
);
446 if (!IS_ERR_OR_NULL(ipu_crtc
->di
))
447 ipu_di_put(ipu_crtc
->di
);
450 static int ipu_get_resources(struct ipu_crtc
*ipu_crtc
,
451 struct ipu_client_platformdata
*pdata
)
453 struct ipu_soc
*ipu
= dev_get_drvdata(ipu_crtc
->dev
->parent
);
456 ipu_crtc
->ipu_ch
= ipu_idmac_get(ipu
, pdata
->dma
[0]);
457 if (IS_ERR(ipu_crtc
->ipu_ch
)) {
458 ret
= PTR_ERR(ipu_crtc
->ipu_ch
);
462 ipu_crtc
->dc
= ipu_dc_get(ipu
, pdata
->dc
);
463 if (IS_ERR(ipu_crtc
->dc
)) {
464 ret
= PTR_ERR(ipu_crtc
->dc
);
468 ipu_crtc
->dmfc
= ipu_dmfc_get(ipu
, pdata
->dma
[0]);
469 if (IS_ERR(ipu_crtc
->dmfc
)) {
470 ret
= PTR_ERR(ipu_crtc
->dmfc
);
474 if (pdata
->dp
>= 0) {
475 ipu_crtc
->dp
= ipu_dp_get(ipu
, pdata
->dp
);
476 if (IS_ERR(ipu_crtc
->dp
)) {
477 ret
= PTR_ERR(ipu_crtc
->dp
);
482 ipu_crtc
->di
= ipu_di_get(ipu
, pdata
->di
);
483 if (IS_ERR(ipu_crtc
->di
)) {
484 ret
= PTR_ERR(ipu_crtc
->di
);
490 ipu_put_resources(ipu_crtc
);
495 static int ipu_crtc_init(struct ipu_crtc
*ipu_crtc
,
496 struct ipu_client_platformdata
*pdata
)
498 struct ipu_soc
*ipu
= dev_get_drvdata(ipu_crtc
->dev
->parent
);
501 ret
= ipu_get_resources(ipu_crtc
, pdata
);
503 dev_err(ipu_crtc
->dev
, "getting resources failed with %d.\n",
508 ret
= imx_drm_add_crtc(&ipu_crtc
->base
,
510 &ipu_crtc_helper_funcs
, THIS_MODULE
,
511 ipu_crtc
->dev
->parent
->of_node
, pdata
->di
);
513 dev_err(ipu_crtc
->dev
, "adding crtc failed with %d.\n", ret
);
514 goto err_put_resources
;
517 ipu_crtc
->irq
= ipu_idmac_channel_irq(ipu
, ipu_crtc
->ipu_ch
,
519 ret
= devm_request_irq(ipu_crtc
->dev
, ipu_crtc
->irq
, ipu_irq_handler
, 0,
520 "imx_drm", ipu_crtc
);
522 dev_err(ipu_crtc
->dev
, "irq request failed with %d.\n", ret
);
523 goto err_put_resources
;
526 disable_irq(ipu_crtc
->irq
);
531 ipu_put_resources(ipu_crtc
);
536 static int ipu_drm_probe(struct platform_device
*pdev
)
538 struct ipu_client_platformdata
*pdata
= pdev
->dev
.platform_data
;
539 struct ipu_crtc
*ipu_crtc
;
545 pdev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
547 ipu_crtc
= devm_kzalloc(&pdev
->dev
, sizeof(*ipu_crtc
), GFP_KERNEL
);
551 ipu_crtc
->dev
= &pdev
->dev
;
553 ret
= ipu_crtc_init(ipu_crtc
, pdata
);
557 platform_set_drvdata(pdev
, ipu_crtc
);
562 static int ipu_drm_remove(struct platform_device
*pdev
)
564 struct ipu_crtc
*ipu_crtc
= platform_get_drvdata(pdev
);
566 imx_drm_remove_crtc(ipu_crtc
->imx_crtc
);
568 ipu_put_resources(ipu_crtc
);
573 static struct platform_driver ipu_drm_driver
= {
575 .name
= "imx-ipuv3-crtc",
577 .probe
= ipu_drm_probe
,
578 .remove
= ipu_drm_remove
,
580 module_platform_driver(ipu_drm_driver
);
582 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
583 MODULE_DESCRIPTION(DRIVER_DESC
);
584 MODULE_LICENSE("GPL");