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_crtc_helper.h>
27 #include <linux/clk.h>
28 #include <drm/drm_gem_cma_helper.h>
29 #include <drm/drm_fb_cma_helper.h>
31 #include "ipu-v3/imx-ipu-v3.h"
34 #define DRIVER_DESC "i.MX IPUv3 Graphics"
39 struct imx_drm_crtc *imx_crtc;
40 struct ipuv3_channel *ipu_ch;
43 struct dmfc_channel *dmfc;
46 struct drm_pending_vblank_event *page_flip_event;
47 struct drm_framebuffer *newfb;
49 u32 interface_pix_fmt;
50 unsigned long di_clkflags;
55 #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
57 static int calc_vref(struct drm_display_mode *mode)
59 unsigned long htotal, vtotal;
61 htotal = mode->htotal;
62 vtotal = mode->vtotal;
64 if (!htotal || !vtotal)
67 return mode->clock * 1000 / vtotal / htotal;
70 static int calc_bandwidth(struct drm_display_mode *mode, unsigned int vref)
72 return mode->hdisplay * mode->vdisplay * vref;
75 static void ipu_fb_enable(struct ipu_crtc *ipu_crtc)
77 if (ipu_crtc->enabled)
80 ipu_di_enable(ipu_crtc->di);
81 ipu_dmfc_enable_channel(ipu_crtc->dmfc);
82 ipu_idmac_enable_channel(ipu_crtc->ipu_ch);
83 ipu_dc_enable_channel(ipu_crtc->dc);
85 ipu_dp_enable_channel(ipu_crtc->dp);
87 ipu_crtc->enabled = 1;
90 static void ipu_fb_disable(struct ipu_crtc *ipu_crtc)
92 if (!ipu_crtc->enabled)
96 ipu_dp_disable_channel(ipu_crtc->dp);
97 ipu_dc_disable_channel(ipu_crtc->dc);
98 ipu_idmac_wait_busy(ipu_crtc->ipu_ch, 50);
99 ipu_idmac_disable_channel(ipu_crtc->ipu_ch);
100 ipu_dmfc_disable_channel(ipu_crtc->dmfc);
101 ipu_di_disable(ipu_crtc->di);
103 ipu_crtc->enabled = 0;
106 static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode)
108 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
110 dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode);
113 case DRM_MODE_DPMS_ON:
114 ipu_fb_enable(ipu_crtc);
116 case DRM_MODE_DPMS_STANDBY:
117 case DRM_MODE_DPMS_SUSPEND:
118 case DRM_MODE_DPMS_OFF:
119 ipu_fb_disable(ipu_crtc);
124 static int ipu_page_flip(struct drm_crtc *crtc,
125 struct drm_framebuffer *fb,
126 struct drm_pending_vblank_event *event,
127 uint32_t page_flip_flags)
129 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
135 ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc);
137 dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n");
138 list_del(&event->base.link);
143 ipu_crtc->newfb = fb;
144 ipu_crtc->page_flip_event = event;
150 static const struct drm_crtc_funcs ipu_crtc_funcs = {
151 .set_config = drm_crtc_helper_set_config,
152 .destroy = drm_crtc_cleanup,
153 .page_flip = ipu_page_flip,
156 static int ipu_drm_set_base(struct drm_crtc *crtc, int x, int y)
158 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
159 struct drm_gem_cma_object *cma_obj;
160 struct drm_framebuffer *fb = crtc->fb;
163 cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
165 DRM_LOG_KMS("entry is null.\n");
169 phys = cma_obj->paddr;
170 phys += x * (fb->bits_per_pixel >> 3);
171 phys += y * fb->pitches[0];
173 dev_dbg(ipu_crtc->dev, "%s: phys: 0x%lx\n", __func__, phys);
174 dev_dbg(ipu_crtc->dev, "%s: xy: %dx%d\n", __func__, x, y);
176 ipu_cpmem_set_stride(ipu_get_cpmem(ipu_crtc->ipu_ch), fb->pitches[0]);
177 ipu_cpmem_set_buffer(ipu_get_cpmem(ipu_crtc->ipu_ch),
183 static int ipu_crtc_mode_set(struct drm_crtc *crtc,
184 struct drm_display_mode *orig_mode,
185 struct drm_display_mode *mode,
187 struct drm_framebuffer *old_fb)
189 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
190 struct drm_framebuffer *fb = ipu_crtc->base.fb;
192 struct ipu_di_signal_cfg sig_cfg = {};
194 struct ipu_ch_param __iomem *cpmem = ipu_get_cpmem(ipu_crtc->ipu_ch);
198 dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
200 dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
203 ipu_ch_param_zero(cpmem);
205 switch (fb->pixel_format) {
206 case DRM_FORMAT_XRGB8888:
207 case DRM_FORMAT_ARGB8888:
208 v4l2_fmt = V4L2_PIX_FMT_RGB32;
211 case DRM_FORMAT_RGB565:
212 v4l2_fmt = V4L2_PIX_FMT_RGB565;
215 case DRM_FORMAT_RGB888:
216 v4l2_fmt = V4L2_PIX_FMT_RGB24;
219 case DRM_FORMAT_BGR888:
220 v4l2_fmt = V4L2_PIX_FMT_BGR24;
224 dev_err(ipu_crtc->dev, "unsupported pixel format 0x%08x\n",
229 out_pixel_fmt = ipu_crtc->interface_pix_fmt;
231 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
232 sig_cfg.interlaced = 1;
233 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
234 sig_cfg.Hsync_pol = 1;
235 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
236 sig_cfg.Vsync_pol = 1;
238 sig_cfg.enable_pol = 1;
240 sig_cfg.width = mode->hdisplay;
241 sig_cfg.height = mode->vdisplay;
242 sig_cfg.pixel_fmt = out_pixel_fmt;
243 sig_cfg.h_start_width = mode->htotal - mode->hsync_end;
244 sig_cfg.h_sync_width = mode->hsync_end - mode->hsync_start;
245 sig_cfg.h_end_width = mode->hsync_start - mode->hdisplay;
247 sig_cfg.v_start_width = mode->vtotal - mode->vsync_end;
248 sig_cfg.v_sync_width = mode->vsync_end - mode->vsync_start;
249 sig_cfg.v_end_width = mode->vsync_start - mode->vdisplay;
250 sig_cfg.pixelclock = mode->clock * 1000;
251 sig_cfg.clkflags = ipu_crtc->di_clkflags;
253 sig_cfg.v_to_h_sync = 0;
255 sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin;
256 sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin;
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)
315 struct drm_device *drm = ipu_crtc->base.dev;
317 spin_lock_irqsave(&drm->event_lock, flags);
318 if (ipu_crtc->page_flip_event)
319 drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event);
320 ipu_crtc->page_flip_event = NULL;
321 imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
322 spin_unlock_irqrestore(&drm->event_lock, flags);
325 static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
327 struct ipu_crtc *ipu_crtc = dev_id;
329 imx_drm_handle_vblank(ipu_crtc->imx_crtc);
331 if (ipu_crtc->newfb) {
332 ipu_crtc->newfb = NULL;
333 ipu_drm_set_base(&ipu_crtc->base, 0, 0);
334 ipu_crtc_handle_pageflip(ipu_crtc);
340 static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
341 const struct drm_display_mode *mode,
342 struct drm_display_mode *adjusted_mode)
347 static void ipu_crtc_prepare(struct drm_crtc *crtc)
349 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
351 ipu_fb_disable(ipu_crtc);
354 static void ipu_crtc_commit(struct drm_crtc *crtc)
356 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
358 ipu_fb_enable(ipu_crtc);
361 static struct drm_crtc_helper_funcs ipu_helper_funcs = {
362 .dpms = ipu_crtc_dpms,
363 .mode_fixup = ipu_crtc_mode_fixup,
364 .mode_set = ipu_crtc_mode_set,
365 .prepare = ipu_crtc_prepare,
366 .commit = ipu_crtc_commit,
369 static int ipu_enable_vblank(struct drm_crtc *crtc)
374 static void ipu_disable_vblank(struct drm_crtc *crtc)
376 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
378 ipu_crtc->page_flip_event = NULL;
379 ipu_crtc->newfb = NULL;
382 static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc, u32 encoder_type,
383 u32 pixfmt, int hsync_pin, int vsync_pin)
385 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
387 ipu_crtc->interface_pix_fmt = pixfmt;
388 ipu_crtc->di_hsync_pin = hsync_pin;
389 ipu_crtc->di_vsync_pin = vsync_pin;
391 switch (encoder_type) {
392 case DRM_MODE_ENCODER_DAC:
393 case DRM_MODE_ENCODER_TVDAC:
394 case DRM_MODE_ENCODER_LVDS:
395 ipu_crtc->di_clkflags = IPU_DI_CLKMODE_SYNC |
398 case DRM_MODE_ENCODER_NONE:
399 ipu_crtc->di_clkflags = 0;
406 static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
407 .enable_vblank = ipu_enable_vblank,
408 .disable_vblank = ipu_disable_vblank,
409 .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
410 .crtc_funcs = &ipu_crtc_funcs,
411 .crtc_helper_funcs = &ipu_helper_funcs,
414 static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
416 if (!IS_ERR_OR_NULL(ipu_crtc->ipu_ch))
417 ipu_idmac_put(ipu_crtc->ipu_ch);
418 if (!IS_ERR_OR_NULL(ipu_crtc->dmfc))
419 ipu_dmfc_put(ipu_crtc->dmfc);
420 if (!IS_ERR_OR_NULL(ipu_crtc->dp))
421 ipu_dp_put(ipu_crtc->dp);
422 if (!IS_ERR_OR_NULL(ipu_crtc->di))
423 ipu_di_put(ipu_crtc->di);
426 static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
427 struct ipu_client_platformdata *pdata)
429 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
432 ipu_crtc->ipu_ch = ipu_idmac_get(ipu, pdata->dma[0]);
433 if (IS_ERR(ipu_crtc->ipu_ch)) {
434 ret = PTR_ERR(ipu_crtc->ipu_ch);
438 ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
439 if (IS_ERR(ipu_crtc->dc)) {
440 ret = PTR_ERR(ipu_crtc->dc);
444 ipu_crtc->dmfc = ipu_dmfc_get(ipu, pdata->dma[0]);
445 if (IS_ERR(ipu_crtc->dmfc)) {
446 ret = PTR_ERR(ipu_crtc->dmfc);
450 if (pdata->dp >= 0) {
451 ipu_crtc->dp = ipu_dp_get(ipu, pdata->dp);
452 if (IS_ERR(ipu_crtc->dp)) {
453 ret = PTR_ERR(ipu_crtc->dp);
458 ipu_crtc->di = ipu_di_get(ipu, pdata->di);
459 if (IS_ERR(ipu_crtc->di)) {
460 ret = PTR_ERR(ipu_crtc->di);
466 ipu_put_resources(ipu_crtc);
471 static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
472 struct ipu_client_platformdata *pdata)
474 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
477 ret = ipu_get_resources(ipu_crtc, pdata);
479 dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
484 ret = imx_drm_add_crtc(&ipu_crtc->base,
486 &ipu_crtc_helper_funcs, THIS_MODULE,
487 ipu_crtc->dev->parent->of_node, pdata->di);
489 dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
490 goto err_put_resources;
493 ipu_crtc->irq = ipu_idmac_channel_irq(ipu, ipu_crtc->ipu_ch,
495 ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
496 "imx_drm", ipu_crtc);
498 dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
499 goto err_put_resources;
505 ipu_put_resources(ipu_crtc);
510 static int ipu_drm_probe(struct platform_device *pdev)
512 struct ipu_client_platformdata *pdata = pdev->dev.platform_data;
513 struct ipu_crtc *ipu_crtc;
519 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
521 ipu_crtc = devm_kzalloc(&pdev->dev, sizeof(*ipu_crtc), GFP_KERNEL);
525 ipu_crtc->dev = &pdev->dev;
527 ret = ipu_crtc_init(ipu_crtc, pdata);
531 platform_set_drvdata(pdev, ipu_crtc);
536 static int ipu_drm_remove(struct platform_device *pdev)
538 struct ipu_crtc *ipu_crtc = platform_get_drvdata(pdev);
540 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
542 ipu_put_resources(ipu_crtc);
547 static struct platform_driver ipu_drm_driver = {
549 .name = "imx-ipuv3-crtc",
551 .probe = ipu_drm_probe,
552 .remove = ipu_drm_remove,
554 module_platform_driver(ipu_drm_driver);
556 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
557 MODULE_DESCRIPTION(DRIVER_DESC);
558 MODULE_LICENSE("GPL");
559 MODULE_ALIAS("platform:imx-ipuv3-crtc");