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;
53 #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
55 static void ipu_fb_enable(struct ipu_crtc *ipu_crtc)
57 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
59 if (ipu_crtc->enabled)
63 ipu_plane_enable(ipu_crtc->plane[0]);
64 /* Start DC channel and DI after IDMAC */
65 ipu_dc_enable_channel(ipu_crtc->dc);
66 ipu_di_enable(ipu_crtc->di);
68 ipu_crtc->enabled = 1;
71 static void ipu_fb_disable(struct ipu_crtc *ipu_crtc)
73 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
75 if (!ipu_crtc->enabled)
78 /* Stop DC channel and DI before IDMAC */
79 ipu_dc_disable_channel(ipu_crtc->dc);
80 ipu_di_disable(ipu_crtc->di);
81 ipu_plane_disable(ipu_crtc->plane[0]);
84 ipu_crtc->enabled = 0;
87 static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode)
89 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
91 dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode);
94 case DRM_MODE_DPMS_ON:
95 ipu_fb_enable(ipu_crtc);
97 case DRM_MODE_DPMS_STANDBY:
98 case DRM_MODE_DPMS_SUSPEND:
99 case DRM_MODE_DPMS_OFF:
100 ipu_fb_disable(ipu_crtc);
105 static int ipu_page_flip(struct drm_crtc *crtc,
106 struct drm_framebuffer *fb,
107 struct drm_pending_vblank_event *event,
108 uint32_t page_flip_flags)
110 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
116 ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc);
118 dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n");
119 list_del(&event->base.link);
124 ipu_crtc->newfb = fb;
125 ipu_crtc->page_flip_event = event;
126 crtc->primary->fb = fb;
131 static const struct drm_crtc_funcs ipu_crtc_funcs = {
132 .set_config = drm_crtc_helper_set_config,
133 .destroy = drm_crtc_cleanup,
134 .page_flip = ipu_page_flip,
137 static int ipu_crtc_mode_set(struct drm_crtc *crtc,
138 struct drm_display_mode *orig_mode,
139 struct drm_display_mode *mode,
141 struct drm_framebuffer *old_fb)
143 struct drm_device *dev = crtc->dev;
144 struct drm_encoder *encoder;
145 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
146 struct ipu_di_signal_cfg sig_cfg = {};
147 unsigned long encoder_types = 0;
151 dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
153 dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
156 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
157 if (encoder->crtc == crtc)
158 encoder_types |= BIT(encoder->encoder_type);
160 dev_dbg(ipu_crtc->dev, "%s: attached to encoder types 0x%lx\n",
161 __func__, encoder_types);
164 * If we have DAC, TVDAC or LDB, then we need the IPU DI clock
165 * to be the same as the LDB DI clock.
167 if (encoder_types & (BIT(DRM_MODE_ENCODER_DAC) |
168 BIT(DRM_MODE_ENCODER_TVDAC) |
169 BIT(DRM_MODE_ENCODER_LVDS)))
170 sig_cfg.clkflags = IPU_DI_CLKMODE_SYNC | IPU_DI_CLKMODE_EXT;
172 sig_cfg.clkflags = 0;
174 out_pixel_fmt = ipu_crtc->interface_pix_fmt;
176 sig_cfg.enable_pol = 1;
178 sig_cfg.pixel_fmt = out_pixel_fmt;
179 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 drm_display_mode_to_videomode(mode, &sig_cfg.mode);
185 ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di,
186 mode->flags & DRM_MODE_FLAG_INTERLACE,
187 out_pixel_fmt, mode->hdisplay);
189 dev_err(ipu_crtc->dev,
190 "initializing display controller failed with %d\n",
195 ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg);
197 dev_err(ipu_crtc->dev,
198 "initializing panel failed with %d\n", ret);
202 return ipu_plane_mode_set(ipu_crtc->plane[0], crtc, mode,
204 0, 0, mode->hdisplay, mode->vdisplay,
205 x, y, mode->hdisplay, mode->vdisplay,
206 mode->flags & DRM_MODE_FLAG_INTERLACE);
209 static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc)
212 struct drm_device *drm = ipu_crtc->base.dev;
214 spin_lock_irqsave(&drm->event_lock, flags);
215 if (ipu_crtc->page_flip_event)
216 drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event);
217 ipu_crtc->page_flip_event = NULL;
218 imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
219 spin_unlock_irqrestore(&drm->event_lock, flags);
222 static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
224 struct ipu_crtc *ipu_crtc = dev_id;
226 imx_drm_handle_vblank(ipu_crtc->imx_crtc);
228 if (ipu_crtc->newfb) {
229 struct ipu_plane *plane = ipu_crtc->plane[0];
231 ipu_crtc->newfb = NULL;
232 ipu_plane_set_base(plane, ipu_crtc->base.primary->fb,
234 ipu_crtc_handle_pageflip(ipu_crtc);
240 static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
241 const struct drm_display_mode *mode,
242 struct drm_display_mode *adjusted_mode)
244 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
248 drm_display_mode_to_videomode(adjusted_mode, &vm);
250 ret = ipu_di_adjust_videomode(ipu_crtc->di, &vm);
254 drm_display_mode_from_videomode(&vm, adjusted_mode);
259 static void ipu_crtc_prepare(struct drm_crtc *crtc)
261 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
263 ipu_fb_disable(ipu_crtc);
266 static void ipu_crtc_commit(struct drm_crtc *crtc)
268 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
270 ipu_fb_enable(ipu_crtc);
273 static struct drm_crtc_helper_funcs ipu_helper_funcs = {
274 .dpms = ipu_crtc_dpms,
275 .mode_fixup = ipu_crtc_mode_fixup,
276 .mode_set = ipu_crtc_mode_set,
277 .prepare = ipu_crtc_prepare,
278 .commit = ipu_crtc_commit,
281 static int ipu_enable_vblank(struct drm_crtc *crtc)
286 static void ipu_disable_vblank(struct drm_crtc *crtc)
288 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
290 ipu_crtc->page_flip_event = NULL;
291 ipu_crtc->newfb = NULL;
294 static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc,
295 u32 pixfmt, int hsync_pin, int vsync_pin)
297 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
299 ipu_crtc->interface_pix_fmt = pixfmt;
300 ipu_crtc->di_hsync_pin = hsync_pin;
301 ipu_crtc->di_vsync_pin = vsync_pin;
306 static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
307 .enable_vblank = ipu_enable_vblank,
308 .disable_vblank = ipu_disable_vblank,
309 .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
310 .crtc_funcs = &ipu_crtc_funcs,
311 .crtc_helper_funcs = &ipu_helper_funcs,
314 static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
316 if (!IS_ERR_OR_NULL(ipu_crtc->dc))
317 ipu_dc_put(ipu_crtc->dc);
318 if (!IS_ERR_OR_NULL(ipu_crtc->di))
319 ipu_di_put(ipu_crtc->di);
322 static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
323 struct ipu_client_platformdata *pdata)
325 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
328 ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
329 if (IS_ERR(ipu_crtc->dc)) {
330 ret = PTR_ERR(ipu_crtc->dc);
334 ipu_crtc->di = ipu_di_get(ipu, pdata->di);
335 if (IS_ERR(ipu_crtc->di)) {
336 ret = PTR_ERR(ipu_crtc->di);
342 ipu_put_resources(ipu_crtc);
347 static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
348 struct ipu_client_platformdata *pdata, struct drm_device *drm)
350 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
355 ret = ipu_get_resources(ipu_crtc, pdata);
357 dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
362 ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc,
363 &ipu_crtc_helper_funcs, ipu_crtc->dev->of_node);
365 dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
366 goto err_put_resources;
370 dp = IPU_DP_FLOW_SYNC_BG;
371 id = imx_drm_crtc_id(ipu_crtc->imx_crtc);
372 ipu_crtc->plane[0] = ipu_plane_init(ipu_crtc->base.dev, ipu,
373 pdata->dma[0], dp, BIT(id), true);
374 ret = ipu_plane_get_resources(ipu_crtc->plane[0]);
376 dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n",
378 goto err_remove_crtc;
381 /* If this crtc is using the DP, add an overlay plane */
382 if (pdata->dp >= 0 && pdata->dma[1] > 0) {
383 ipu_crtc->plane[1] = ipu_plane_init(ipu_crtc->base.dev, ipu,
387 if (IS_ERR(ipu_crtc->plane[1]))
388 ipu_crtc->plane[1] = NULL;
391 ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]);
392 ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
393 "imx_drm", ipu_crtc);
395 dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
396 goto err_put_plane_res;
402 ipu_plane_put_resources(ipu_crtc->plane[0]);
404 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
406 ipu_put_resources(ipu_crtc);
411 static struct device_node *ipu_drm_get_port_by_id(struct device_node *parent,
414 struct device_node *port;
417 port = of_get_child_by_name(parent, "port");
419 ret = of_property_read_u32(port, "reg", &id);
420 if (!ret && id == port_id)
424 port = of_get_next_child(parent, port);
427 } while (of_node_cmp(port->name, "port"));
433 static int ipu_drm_bind(struct device *dev, struct device *master, void *data)
435 struct ipu_client_platformdata *pdata = dev->platform_data;
436 struct drm_device *drm = data;
437 struct ipu_crtc *ipu_crtc;
440 ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL);
446 ret = ipu_crtc_init(ipu_crtc, pdata, drm);
450 dev_set_drvdata(dev, ipu_crtc);
455 static void ipu_drm_unbind(struct device *dev, struct device *master,
458 struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev);
460 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
462 ipu_plane_put_resources(ipu_crtc->plane[0]);
463 ipu_put_resources(ipu_crtc);
466 static const struct component_ops ipu_crtc_ops = {
467 .bind = ipu_drm_bind,
468 .unbind = ipu_drm_unbind,
471 static int ipu_drm_probe(struct platform_device *pdev)
473 struct device *dev = &pdev->dev;
474 struct ipu_client_platformdata *pdata = dev->platform_data;
477 if (!dev->platform_data)
481 /* Associate crtc device with the corresponding DI port node */
482 dev->of_node = ipu_drm_get_port_by_id(dev->parent->of_node,
485 dev_err(dev, "missing port@%d node in %s\n",
486 pdata->di + 2, dev->parent->of_node->full_name);
491 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
495 return component_add(dev, &ipu_crtc_ops);
498 static int ipu_drm_remove(struct platform_device *pdev)
500 component_del(&pdev->dev, &ipu_crtc_ops);
504 static struct platform_driver ipu_drm_driver = {
506 .name = "imx-ipuv3-crtc",
508 .probe = ipu_drm_probe,
509 .remove = ipu_drm_remove,
511 module_platform_driver(ipu_drm_driver);
513 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
514 MODULE_DESCRIPTION(DRIVER_DESC);
515 MODULE_LICENSE("GPL");
516 MODULE_ALIAS("platform:imx-ipuv3-crtc");