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 #include <linux/component.h>
16 #include <linux/module.h>
17 #include <linux/export.h>
18 #include <linux/device.h>
19 #include <linux/platform_device.h>
21 #include <drm/drm_crtc_helper.h>
23 #include <linux/clk.h>
24 #include <linux/errno.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
28 #include <video/imx-ipu-v3.h>
30 #include "ipuv3-plane.h"
32 #define DRIVER_DESC "i.MX IPUv3 Graphics"
37 struct imx_drm_crtc *imx_crtc;
39 /* plane[0] is the full plane, plane[1] is the partial plane */
40 struct ipu_plane *plane[2];
45 struct drm_pending_vblank_event *page_flip_event;
46 struct drm_framebuffer *newfb;
48 u32 interface_pix_fmt;
49 unsigned long di_clkflags;
54 #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
56 static void ipu_fb_enable(struct ipu_crtc *ipu_crtc)
58 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
60 if (ipu_crtc->enabled)
64 ipu_plane_enable(ipu_crtc->plane[0]);
65 /* Start DC channel and DI after IDMAC */
66 ipu_dc_enable_channel(ipu_crtc->dc);
67 ipu_di_enable(ipu_crtc->di);
69 ipu_crtc->enabled = 1;
72 static void ipu_fb_disable(struct ipu_crtc *ipu_crtc)
74 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
76 if (!ipu_crtc->enabled)
79 /* Stop DC channel and DI before IDMAC */
80 ipu_dc_disable_channel(ipu_crtc->dc);
81 ipu_di_disable(ipu_crtc->di);
82 ipu_plane_disable(ipu_crtc->plane[0]);
85 ipu_crtc->enabled = 0;
88 static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode)
90 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
92 dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode);
95 case DRM_MODE_DPMS_ON:
96 ipu_fb_enable(ipu_crtc);
98 case DRM_MODE_DPMS_STANDBY:
99 case DRM_MODE_DPMS_SUSPEND:
100 case DRM_MODE_DPMS_OFF:
101 ipu_fb_disable(ipu_crtc);
106 static int ipu_page_flip(struct drm_crtc *crtc,
107 struct drm_framebuffer *fb,
108 struct drm_pending_vblank_event *event,
109 uint32_t page_flip_flags)
111 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
117 ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc);
119 dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n");
120 list_del(&event->base.link);
125 ipu_crtc->newfb = fb;
126 ipu_crtc->page_flip_event = event;
127 crtc->primary->fb = fb;
132 static const struct drm_crtc_funcs ipu_crtc_funcs = {
133 .set_config = drm_crtc_helper_set_config,
134 .destroy = drm_crtc_cleanup,
135 .page_flip = ipu_page_flip,
138 static int ipu_crtc_mode_set(struct drm_crtc *crtc,
139 struct drm_display_mode *orig_mode,
140 struct drm_display_mode *mode,
142 struct drm_framebuffer *old_fb)
144 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
146 struct ipu_di_signal_cfg sig_cfg = {};
149 dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
151 dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
154 out_pixel_fmt = ipu_crtc->interface_pix_fmt;
156 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
157 sig_cfg.interlaced = 1;
158 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
159 sig_cfg.Hsync_pol = 1;
160 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
161 sig_cfg.Vsync_pol = 1;
163 sig_cfg.enable_pol = 1;
165 sig_cfg.width = mode->hdisplay;
166 sig_cfg.height = mode->vdisplay;
167 sig_cfg.pixel_fmt = out_pixel_fmt;
168 sig_cfg.h_start_width = mode->htotal - mode->hsync_end;
169 sig_cfg.h_sync_width = mode->hsync_end - mode->hsync_start;
170 sig_cfg.h_end_width = mode->hsync_start - mode->hdisplay;
172 sig_cfg.v_start_width = mode->vtotal - mode->vsync_end;
173 sig_cfg.v_sync_width = mode->vsync_end - mode->vsync_start;
174 sig_cfg.v_end_width = mode->vsync_start - mode->vdisplay;
175 sig_cfg.pixelclock = mode->clock * 1000;
176 sig_cfg.clkflags = ipu_crtc->di_clkflags;
178 sig_cfg.v_to_h_sync = 0;
180 sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin;
181 sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin;
183 ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di, sig_cfg.interlaced,
184 out_pixel_fmt, mode->hdisplay);
186 dev_err(ipu_crtc->dev,
187 "initializing display controller failed with %d\n",
192 ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg);
194 dev_err(ipu_crtc->dev,
195 "initializing panel failed with %d\n", ret);
199 return ipu_plane_mode_set(ipu_crtc->plane[0], crtc, mode,
201 0, 0, mode->hdisplay, mode->vdisplay,
202 x, y, mode->hdisplay, mode->vdisplay);
205 static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc)
208 struct drm_device *drm = ipu_crtc->base.dev;
210 spin_lock_irqsave(&drm->event_lock, flags);
211 if (ipu_crtc->page_flip_event)
212 drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event);
213 ipu_crtc->page_flip_event = NULL;
214 imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
215 spin_unlock_irqrestore(&drm->event_lock, flags);
218 static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
220 struct ipu_crtc *ipu_crtc = dev_id;
222 imx_drm_handle_vblank(ipu_crtc->imx_crtc);
224 if (ipu_crtc->newfb) {
225 struct ipu_plane *plane = ipu_crtc->plane[0];
227 ipu_crtc->newfb = NULL;
228 ipu_plane_set_base(plane, ipu_crtc->base.primary->fb,
230 ipu_crtc_handle_pageflip(ipu_crtc);
236 static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
237 const struct drm_display_mode *mode,
238 struct drm_display_mode *adjusted_mode)
243 static void ipu_crtc_prepare(struct drm_crtc *crtc)
245 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
247 ipu_fb_disable(ipu_crtc);
250 static void ipu_crtc_commit(struct drm_crtc *crtc)
252 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
254 ipu_fb_enable(ipu_crtc);
257 static struct drm_crtc_helper_funcs ipu_helper_funcs = {
258 .dpms = ipu_crtc_dpms,
259 .mode_fixup = ipu_crtc_mode_fixup,
260 .mode_set = ipu_crtc_mode_set,
261 .prepare = ipu_crtc_prepare,
262 .commit = ipu_crtc_commit,
265 static int ipu_enable_vblank(struct drm_crtc *crtc)
270 static void ipu_disable_vblank(struct drm_crtc *crtc)
272 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
274 ipu_crtc->page_flip_event = NULL;
275 ipu_crtc->newfb = NULL;
278 static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc, u32 encoder_type,
279 u32 pixfmt, int hsync_pin, int vsync_pin)
281 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
283 ipu_crtc->interface_pix_fmt = pixfmt;
284 ipu_crtc->di_hsync_pin = hsync_pin;
285 ipu_crtc->di_vsync_pin = vsync_pin;
287 switch (encoder_type) {
288 case DRM_MODE_ENCODER_DAC:
289 case DRM_MODE_ENCODER_TVDAC:
290 case DRM_MODE_ENCODER_LVDS:
291 ipu_crtc->di_clkflags = IPU_DI_CLKMODE_SYNC |
294 case DRM_MODE_ENCODER_TMDS:
295 case DRM_MODE_ENCODER_NONE:
296 ipu_crtc->di_clkflags = 0;
303 static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
304 .enable_vblank = ipu_enable_vblank,
305 .disable_vblank = ipu_disable_vblank,
306 .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
307 .crtc_funcs = &ipu_crtc_funcs,
308 .crtc_helper_funcs = &ipu_helper_funcs,
311 static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
313 if (!IS_ERR_OR_NULL(ipu_crtc->dc))
314 ipu_dc_put(ipu_crtc->dc);
315 if (!IS_ERR_OR_NULL(ipu_crtc->di))
316 ipu_di_put(ipu_crtc->di);
319 static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
320 struct ipu_client_platformdata *pdata)
322 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
325 ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
326 if (IS_ERR(ipu_crtc->dc)) {
327 ret = PTR_ERR(ipu_crtc->dc);
331 ipu_crtc->di = ipu_di_get(ipu, pdata->di);
332 if (IS_ERR(ipu_crtc->di)) {
333 ret = PTR_ERR(ipu_crtc->di);
339 ipu_put_resources(ipu_crtc);
344 static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
345 struct ipu_client_platformdata *pdata, struct drm_device *drm)
347 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
352 ret = ipu_get_resources(ipu_crtc, pdata);
354 dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
359 ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc,
360 &ipu_crtc_helper_funcs, ipu_crtc->dev->of_node);
362 dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
363 goto err_put_resources;
367 dp = IPU_DP_FLOW_SYNC_BG;
368 id = imx_drm_crtc_id(ipu_crtc->imx_crtc);
369 ipu_crtc->plane[0] = ipu_plane_init(ipu_crtc->base.dev, ipu,
370 pdata->dma[0], dp, BIT(id), true);
371 ret = ipu_plane_get_resources(ipu_crtc->plane[0]);
373 dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n",
375 goto err_remove_crtc;
378 /* If this crtc is using the DP, add an overlay plane */
379 if (pdata->dp >= 0 && pdata->dma[1] > 0) {
380 ipu_crtc->plane[1] = ipu_plane_init(ipu_crtc->base.dev, ipu,
384 if (IS_ERR(ipu_crtc->plane[1]))
385 ipu_crtc->plane[1] = NULL;
388 ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]);
389 ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
390 "imx_drm", ipu_crtc);
392 dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
393 goto err_put_plane_res;
399 ipu_plane_put_resources(ipu_crtc->plane[0]);
401 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
403 ipu_put_resources(ipu_crtc);
408 static struct device_node *ipu_drm_get_port_by_id(struct device_node *parent,
411 struct device_node *port;
414 port = of_get_child_by_name(parent, "port");
416 ret = of_property_read_u32(port, "reg", &id);
417 if (!ret && id == port_id)
421 port = of_get_next_child(parent, port);
424 } while (of_node_cmp(port->name, "port"));
430 static int ipu_drm_bind(struct device *dev, struct device *master, void *data)
432 struct ipu_client_platformdata *pdata = dev->platform_data;
433 struct drm_device *drm = data;
434 struct ipu_crtc *ipu_crtc;
437 ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL);
443 ret = ipu_crtc_init(ipu_crtc, pdata, drm);
447 dev_set_drvdata(dev, ipu_crtc);
452 static void ipu_drm_unbind(struct device *dev, struct device *master,
455 struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev);
457 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
459 ipu_plane_put_resources(ipu_crtc->plane[0]);
460 ipu_put_resources(ipu_crtc);
463 static const struct component_ops ipu_crtc_ops = {
464 .bind = ipu_drm_bind,
465 .unbind = ipu_drm_unbind,
468 static int ipu_drm_probe(struct platform_device *pdev)
470 struct device *dev = &pdev->dev;
471 struct ipu_client_platformdata *pdata = dev->platform_data;
474 if (!dev->platform_data)
478 /* Associate crtc device with the corresponding DI port node */
479 dev->of_node = ipu_drm_get_port_by_id(dev->parent->of_node,
482 dev_err(dev, "missing port@%d node in %s\n",
483 pdata->di + 2, dev->parent->of_node->full_name);
488 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
492 return component_add(dev, &ipu_crtc_ops);
495 static int ipu_drm_remove(struct platform_device *pdev)
497 component_del(&pdev->dev, &ipu_crtc_ops);
501 static struct platform_driver ipu_drm_driver = {
503 .name = "imx-ipuv3-crtc",
505 .probe = ipu_drm_probe,
506 .remove = ipu_drm_remove,
508 module_platform_driver(ipu_drm_driver);
510 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
511 MODULE_DESCRIPTION(DRIVER_DESC);
512 MODULE_LICENSE("GPL");
513 MODULE_ALIAS("platform:imx-ipuv3-crtc");