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_LVDS:
417 ipu_crtc->di_clkflags = IPU_DI_CLKMODE_SYNC |
420 case DRM_MODE_ENCODER_NONE:
421 ipu_crtc->di_clkflags = 0;
428 static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
429 .enable_vblank = ipu_enable_vblank,
430 .disable_vblank = ipu_disable_vblank,
431 .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
432 .crtc_funcs = &ipu_crtc_funcs,
433 .crtc_helper_funcs = &ipu_helper_funcs,
436 static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
438 if (!IS_ERR_OR_NULL(ipu_crtc->ipu_ch))
439 ipu_idmac_put(ipu_crtc->ipu_ch);
440 if (!IS_ERR_OR_NULL(ipu_crtc->dmfc))
441 ipu_dmfc_put(ipu_crtc->dmfc);
442 if (!IS_ERR_OR_NULL(ipu_crtc->dp))
443 ipu_dp_put(ipu_crtc->dp);
444 if (!IS_ERR_OR_NULL(ipu_crtc->di))
445 ipu_di_put(ipu_crtc->di);
448 static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
449 struct ipu_client_platformdata *pdata)
451 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
454 ipu_crtc->ipu_ch = ipu_idmac_get(ipu, pdata->dma[0]);
455 if (IS_ERR(ipu_crtc->ipu_ch)) {
456 ret = PTR_ERR(ipu_crtc->ipu_ch);
460 ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
461 if (IS_ERR(ipu_crtc->dc)) {
462 ret = PTR_ERR(ipu_crtc->dc);
466 ipu_crtc->dmfc = ipu_dmfc_get(ipu, pdata->dma[0]);
467 if (IS_ERR(ipu_crtc->dmfc)) {
468 ret = PTR_ERR(ipu_crtc->dmfc);
472 if (pdata->dp >= 0) {
473 ipu_crtc->dp = ipu_dp_get(ipu, pdata->dp);
474 if (IS_ERR(ipu_crtc->dp)) {
475 ret = PTR_ERR(ipu_crtc->dp);
480 ipu_crtc->di = ipu_di_get(ipu, pdata->di);
481 if (IS_ERR(ipu_crtc->di)) {
482 ret = PTR_ERR(ipu_crtc->di);
486 ipu_crtc->irq = ipu_idmac_channel_irq(ipu, ipu_crtc->ipu_ch,
488 ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
489 "imx_drm", ipu_crtc);
491 dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
495 disable_irq(ipu_crtc->irq);
499 ipu_put_resources(ipu_crtc);
504 static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
505 struct ipu_client_platformdata *pdata)
509 ret = ipu_get_resources(ipu_crtc, pdata);
511 dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
516 ret = imx_drm_add_crtc(&ipu_crtc->base,
518 &ipu_crtc_helper_funcs, THIS_MODULE,
519 ipu_crtc->dev->parent->of_node, pdata->di);
521 dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
522 goto err_put_resources;
528 ipu_put_resources(ipu_crtc);
533 static int ipu_drm_probe(struct platform_device *pdev)
535 struct ipu_client_platformdata *pdata = pdev->dev.platform_data;
536 struct ipu_crtc *ipu_crtc;
542 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
544 ipu_crtc = devm_kzalloc(&pdev->dev, sizeof(*ipu_crtc), GFP_KERNEL);
548 ipu_crtc->dev = &pdev->dev;
550 ret = ipu_crtc_init(ipu_crtc, pdata);
554 platform_set_drvdata(pdev, ipu_crtc);
559 static int ipu_drm_remove(struct platform_device *pdev)
561 struct ipu_crtc *ipu_crtc = platform_get_drvdata(pdev);
563 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
565 ipu_put_resources(ipu_crtc);
570 static struct platform_driver ipu_drm_driver = {
572 .name = "imx-ipuv3-crtc",
574 .probe = ipu_drm_probe,
575 .remove = ipu_drm_remove,
577 module_platform_driver(ipu_drm_driver);
579 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
580 MODULE_DESCRIPTION(DRIVER_DESC);
581 MODULE_LICENSE("GPL");