Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * i.MX IPUv3 Graphics driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Sascha Hauer, Pengutronix |
| 5 | * |
| 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. |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 14 | */ |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 15 | #include <linux/component.h> |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/export.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <drm/drmP.h> |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 21 | #include <drm/drm_crtc_helper.h> |
| 22 | #include <linux/fb.h> |
| 23 | #include <linux/clk.h> |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 24 | #include <linux/errno.h> |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 25 | #include <drm/drm_gem_cma_helper.h> |
| 26 | #include <drm/drm_fb_cma_helper.h> |
| 27 | |
Philipp Zabel | 39b9004 | 2013-09-30 16:13:39 +0200 | [diff] [blame] | 28 | #include <video/imx-ipu-v3.h> |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 29 | #include "imx-drm.h" |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 30 | #include "ipuv3-plane.h" |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 31 | |
| 32 | #define DRIVER_DESC "i.MX IPUv3 Graphics" |
| 33 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 34 | struct ipu_crtc { |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 35 | struct device *dev; |
| 36 | struct drm_crtc base; |
| 37 | struct imx_drm_crtc *imx_crtc; |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 38 | |
| 39 | /* plane[0] is the full plane, plane[1] is the partial plane */ |
| 40 | struct ipu_plane *plane[2]; |
| 41 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 42 | struct ipu_dc *dc; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 43 | struct ipu_di *di; |
| 44 | int enabled; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 45 | struct drm_pending_vblank_event *page_flip_event; |
| 46 | struct drm_framebuffer *newfb; |
| 47 | int irq; |
| 48 | u32 interface_pix_fmt; |
| 49 | unsigned long di_clkflags; |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 50 | int di_hsync_pin; |
| 51 | int di_vsync_pin; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base) |
| 55 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 56 | static void ipu_fb_enable(struct ipu_crtc *ipu_crtc) |
| 57 | { |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 58 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
| 59 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 60 | if (ipu_crtc->enabled) |
| 61 | return; |
| 62 | |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 63 | ipu_dc_enable(ipu); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 64 | ipu_plane_enable(ipu_crtc->plane[0]); |
Philipp Zabel | c115edb | 2014-04-14 23:53:22 +0200 | [diff] [blame] | 65 | /* Start DC channel and DI after IDMAC */ |
| 66 | ipu_dc_enable_channel(ipu_crtc->dc); |
| 67 | ipu_di_enable(ipu_crtc->di); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 68 | |
| 69 | ipu_crtc->enabled = 1; |
| 70 | } |
| 71 | |
| 72 | static void ipu_fb_disable(struct ipu_crtc *ipu_crtc) |
| 73 | { |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 74 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
| 75 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 76 | if (!ipu_crtc->enabled) |
| 77 | return; |
| 78 | |
Philipp Zabel | c115edb | 2014-04-14 23:53:22 +0200 | [diff] [blame] | 79 | /* Stop DC channel and DI before IDMAC */ |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 80 | ipu_dc_disable_channel(ipu_crtc->dc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 81 | ipu_di_disable(ipu_crtc->di); |
Philipp Zabel | c115edb | 2014-04-14 23:53:22 +0200 | [diff] [blame] | 82 | ipu_plane_disable(ipu_crtc->plane[0]); |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 83 | ipu_dc_disable(ipu); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 84 | |
| 85 | ipu_crtc->enabled = 0; |
| 86 | } |
| 87 | |
| 88 | static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode) |
| 89 | { |
| 90 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 91 | |
Philipp Zabel | a8e4e23 | 2012-11-12 16:29:01 +0100 | [diff] [blame] | 92 | dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 93 | |
| 94 | switch (mode) { |
| 95 | case DRM_MODE_DPMS_ON: |
| 96 | ipu_fb_enable(ipu_crtc); |
| 97 | break; |
| 98 | case DRM_MODE_DPMS_STANDBY: |
| 99 | case DRM_MODE_DPMS_SUSPEND: |
| 100 | case DRM_MODE_DPMS_OFF: |
| 101 | ipu_fb_disable(ipu_crtc); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static int ipu_page_flip(struct drm_crtc *crtc, |
| 107 | struct drm_framebuffer *fb, |
Keith Packard | ed8d197 | 2013-07-22 18:49:58 -0700 | [diff] [blame] | 108 | struct drm_pending_vblank_event *event, |
| 109 | uint32_t page_flip_flags) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 110 | { |
| 111 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 112 | int ret; |
| 113 | |
| 114 | if (ipu_crtc->newfb) |
| 115 | return -EBUSY; |
| 116 | |
| 117 | ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc); |
| 118 | if (ret) { |
| 119 | dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n"); |
| 120 | list_del(&event->base.link); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | ipu_crtc->newfb = fb; |
| 126 | ipu_crtc->page_flip_event = event; |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 127 | crtc->primary->fb = fb; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 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, |
| 136 | }; |
| 137 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 138 | static int ipu_crtc_mode_set(struct drm_crtc *crtc, |
| 139 | struct drm_display_mode *orig_mode, |
| 140 | struct drm_display_mode *mode, |
| 141 | int x, int y, |
| 142 | struct drm_framebuffer *old_fb) |
| 143 | { |
| 144 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 145 | int ret; |
| 146 | struct ipu_di_signal_cfg sig_cfg = {}; |
| 147 | u32 out_pixel_fmt; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 148 | |
| 149 | dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__, |
| 150 | mode->hdisplay); |
| 151 | dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__, |
| 152 | mode->vdisplay); |
| 153 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 154 | out_pixel_fmt = ipu_crtc->interface_pix_fmt; |
| 155 | |
| 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; |
| 162 | |
| 163 | sig_cfg.enable_pol = 1; |
Denis Carikli | 85de9d1 | 2014-04-07 14:44:43 +0200 | [diff] [blame] | 164 | sig_cfg.clk_pol = 0; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 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; |
| 171 | |
| 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; |
| 177 | |
| 178 | sig_cfg.v_to_h_sync = 0; |
| 179 | |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 180 | sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin; |
| 181 | sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin; |
| 182 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 183 | ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di, sig_cfg.interlaced, |
| 184 | out_pixel_fmt, mode->hdisplay); |
| 185 | if (ret) { |
| 186 | dev_err(ipu_crtc->dev, |
| 187 | "initializing display controller failed with %d\n", |
| 188 | ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg); |
| 193 | if (ret) { |
| 194 | dev_err(ipu_crtc->dev, |
| 195 | "initializing panel failed with %d\n", ret); |
| 196 | return ret; |
| 197 | } |
| 198 | |
Yannis Damigos | 30e94a5 | 2014-08-19 18:26:46 +0300 | [diff] [blame] | 199 | return ipu_plane_mode_set(ipu_crtc->plane[0], crtc, mode, |
| 200 | crtc->primary->fb, |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 201 | 0, 0, mode->hdisplay, mode->vdisplay, |
| 202 | x, y, mode->hdisplay, mode->vdisplay); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc) |
| 206 | { |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 207 | unsigned long flags; |
| 208 | struct drm_device *drm = ipu_crtc->base.dev; |
| 209 | |
| 210 | spin_lock_irqsave(&drm->event_lock, flags); |
Rob Clark | 0eca56f | 2012-10-08 19:50:46 +0000 | [diff] [blame] | 211 | if (ipu_crtc->page_flip_event) |
| 212 | drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 213 | ipu_crtc->page_flip_event = NULL; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 214 | imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 215 | spin_unlock_irqrestore(&drm->event_lock, flags); |
| 216 | } |
| 217 | |
| 218 | static irqreturn_t ipu_irq_handler(int irq, void *dev_id) |
| 219 | { |
| 220 | struct ipu_crtc *ipu_crtc = dev_id; |
| 221 | |
| 222 | imx_drm_handle_vblank(ipu_crtc->imx_crtc); |
| 223 | |
| 224 | if (ipu_crtc->newfb) { |
Yannis Damigos | 30e94a5 | 2014-08-19 18:26:46 +0300 | [diff] [blame] | 225 | struct ipu_plane *plane = ipu_crtc->plane[0]; |
| 226 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 227 | ipu_crtc->newfb = NULL; |
Yannis Damigos | 30e94a5 | 2014-08-19 18:26:46 +0300 | [diff] [blame] | 228 | ipu_plane_set_base(plane, ipu_crtc->base.primary->fb, |
| 229 | plane->x, plane->y); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 230 | ipu_crtc_handle_pageflip(ipu_crtc); |
| 231 | } |
| 232 | |
| 233 | return IRQ_HANDLED; |
| 234 | } |
| 235 | |
| 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) |
| 239 | { |
Steve Longerbeam | 0c460a5 | 2014-12-18 18:00:23 -0800 | [diff] [blame] | 240 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 241 | struct videomode vm; |
| 242 | int ret; |
| 243 | |
| 244 | drm_display_mode_to_videomode(adjusted_mode, &vm); |
| 245 | |
| 246 | ret = ipu_di_adjust_videomode(ipu_crtc->di, &vm); |
| 247 | if (ret) |
| 248 | return false; |
| 249 | |
| 250 | drm_display_mode_from_videomode(&vm, adjusted_mode); |
| 251 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | |
| 255 | static void ipu_crtc_prepare(struct drm_crtc *crtc) |
| 256 | { |
| 257 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 258 | |
| 259 | ipu_fb_disable(ipu_crtc); |
| 260 | } |
| 261 | |
| 262 | static void ipu_crtc_commit(struct drm_crtc *crtc) |
| 263 | { |
| 264 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 265 | |
| 266 | ipu_fb_enable(ipu_crtc); |
| 267 | } |
| 268 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 269 | static struct drm_crtc_helper_funcs ipu_helper_funcs = { |
| 270 | .dpms = ipu_crtc_dpms, |
| 271 | .mode_fixup = ipu_crtc_mode_fixup, |
| 272 | .mode_set = ipu_crtc_mode_set, |
| 273 | .prepare = ipu_crtc_prepare, |
| 274 | .commit = ipu_crtc_commit, |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | static int ipu_enable_vblank(struct drm_crtc *crtc) |
| 278 | { |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static void ipu_disable_vblank(struct drm_crtc *crtc) |
| 283 | { |
| 284 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 285 | |
Sascha Hauer | 6ee4d7f | 2013-10-10 16:18:44 +0200 | [diff] [blame] | 286 | ipu_crtc->page_flip_event = NULL; |
| 287 | ipu_crtc->newfb = NULL; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc, u32 encoder_type, |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 291 | u32 pixfmt, int hsync_pin, int vsync_pin) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 292 | { |
| 293 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 294 | |
| 295 | ipu_crtc->interface_pix_fmt = pixfmt; |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 296 | ipu_crtc->di_hsync_pin = hsync_pin; |
| 297 | ipu_crtc->di_vsync_pin = vsync_pin; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 298 | |
| 299 | switch (encoder_type) { |
Philipp Zabel | b60b5bc | 2013-04-08 18:04:33 +0200 | [diff] [blame] | 300 | case DRM_MODE_ENCODER_DAC: |
| 301 | case DRM_MODE_ENCODER_TVDAC: |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 302 | case DRM_MODE_ENCODER_LVDS: |
| 303 | ipu_crtc->di_clkflags = IPU_DI_CLKMODE_SYNC | |
| 304 | IPU_DI_CLKMODE_EXT; |
| 305 | break; |
Russell King | f2d66aa | 2013-11-03 15:52:16 +0000 | [diff] [blame] | 306 | case DRM_MODE_ENCODER_TMDS: |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 307 | case DRM_MODE_ENCODER_NONE: |
| 308 | ipu_crtc->di_clkflags = 0; |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = { |
| 316 | .enable_vblank = ipu_enable_vblank, |
| 317 | .disable_vblank = ipu_disable_vblank, |
| 318 | .set_interface_pix_fmt = ipu_set_interface_pix_fmt, |
| 319 | .crtc_funcs = &ipu_crtc_funcs, |
| 320 | .crtc_helper_funcs = &ipu_helper_funcs, |
| 321 | }; |
| 322 | |
| 323 | static void ipu_put_resources(struct ipu_crtc *ipu_crtc) |
| 324 | { |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 325 | if (!IS_ERR_OR_NULL(ipu_crtc->dc)) |
| 326 | ipu_dc_put(ipu_crtc->dc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 327 | if (!IS_ERR_OR_NULL(ipu_crtc->di)) |
| 328 | ipu_di_put(ipu_crtc->di); |
| 329 | } |
| 330 | |
| 331 | static int ipu_get_resources(struct ipu_crtc *ipu_crtc, |
| 332 | struct ipu_client_platformdata *pdata) |
| 333 | { |
| 334 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
| 335 | int ret; |
| 336 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 337 | ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc); |
| 338 | if (IS_ERR(ipu_crtc->dc)) { |
| 339 | ret = PTR_ERR(ipu_crtc->dc); |
| 340 | goto err_out; |
| 341 | } |
| 342 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 343 | ipu_crtc->di = ipu_di_get(ipu, pdata->di); |
| 344 | if (IS_ERR(ipu_crtc->di)) { |
| 345 | ret = PTR_ERR(ipu_crtc->di); |
| 346 | goto err_out; |
| 347 | } |
| 348 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 349 | return 0; |
| 350 | err_out: |
| 351 | ipu_put_resources(ipu_crtc); |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int ipu_crtc_init(struct ipu_crtc *ipu_crtc, |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 357 | struct ipu_client_platformdata *pdata, struct drm_device *drm) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 358 | { |
Philipp Zabel | 47b1be5 | 2013-02-20 10:57:01 +0800 | [diff] [blame] | 359 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 360 | int dp = -EINVAL; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 361 | int ret; |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 362 | int id; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 363 | |
| 364 | ret = ipu_get_resources(ipu_crtc, pdata); |
| 365 | if (ret) { |
| 366 | dev_err(ipu_crtc->dev, "getting resources failed with %d.\n", |
| 367 | ret); |
| 368 | return ret; |
| 369 | } |
| 370 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 371 | ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc, |
| 372 | &ipu_crtc_helper_funcs, ipu_crtc->dev->of_node); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 373 | if (ret) { |
| 374 | dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret); |
| 375 | goto err_put_resources; |
| 376 | } |
| 377 | |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 378 | if (pdata->dp >= 0) |
| 379 | dp = IPU_DP_FLOW_SYNC_BG; |
| 380 | id = imx_drm_crtc_id(ipu_crtc->imx_crtc); |
| 381 | ipu_crtc->plane[0] = ipu_plane_init(ipu_crtc->base.dev, ipu, |
| 382 | pdata->dma[0], dp, BIT(id), true); |
| 383 | ret = ipu_plane_get_resources(ipu_crtc->plane[0]); |
| 384 | if (ret) { |
| 385 | dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n", |
| 386 | ret); |
| 387 | goto err_remove_crtc; |
| 388 | } |
| 389 | |
| 390 | /* If this crtc is using the DP, add an overlay plane */ |
| 391 | if (pdata->dp >= 0 && pdata->dma[1] > 0) { |
| 392 | ipu_crtc->plane[1] = ipu_plane_init(ipu_crtc->base.dev, ipu, |
| 393 | pdata->dma[1], |
| 394 | IPU_DP_FLOW_SYNC_FG, |
| 395 | BIT(id), false); |
| 396 | if (IS_ERR(ipu_crtc->plane[1])) |
| 397 | ipu_crtc->plane[1] = NULL; |
| 398 | } |
| 399 | |
| 400 | ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]); |
Philipp Zabel | 47b1be5 | 2013-02-20 10:57:01 +0800 | [diff] [blame] | 401 | ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0, |
| 402 | "imx_drm", ipu_crtc); |
| 403 | if (ret < 0) { |
| 404 | dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 405 | goto err_put_plane_res; |
Philipp Zabel | 47b1be5 | 2013-02-20 10:57:01 +0800 | [diff] [blame] | 406 | } |
| 407 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 408 | return 0; |
| 409 | |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 410 | err_put_plane_res: |
| 411 | ipu_plane_put_resources(ipu_crtc->plane[0]); |
| 412 | err_remove_crtc: |
| 413 | imx_drm_remove_crtc(ipu_crtc->imx_crtc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 414 | err_put_resources: |
| 415 | ipu_put_resources(ipu_crtc); |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 420 | static struct device_node *ipu_drm_get_port_by_id(struct device_node *parent, |
| 421 | int port_id) |
| 422 | { |
| 423 | struct device_node *port; |
| 424 | int id, ret; |
| 425 | |
| 426 | port = of_get_child_by_name(parent, "port"); |
| 427 | while (port) { |
| 428 | ret = of_property_read_u32(port, "reg", &id); |
| 429 | if (!ret && id == port_id) |
| 430 | return port; |
| 431 | |
| 432 | do { |
| 433 | port = of_get_next_child(parent, port); |
| 434 | if (!port) |
| 435 | return NULL; |
| 436 | } while (of_node_cmp(port->name, "port")); |
| 437 | } |
| 438 | |
| 439 | return NULL; |
| 440 | } |
| 441 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 442 | static int ipu_drm_bind(struct device *dev, struct device *master, void *data) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 443 | { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 444 | struct ipu_client_platformdata *pdata = dev->platform_data; |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 445 | struct drm_device *drm = data; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 446 | struct ipu_crtc *ipu_crtc; |
| 447 | int ret; |
| 448 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 449 | ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL); |
| 450 | if (!ipu_crtc) |
| 451 | return -ENOMEM; |
| 452 | |
| 453 | ipu_crtc->dev = dev; |
| 454 | |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 455 | ret = ipu_crtc_init(ipu_crtc, pdata, drm); |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 456 | if (ret) |
| 457 | return ret; |
| 458 | |
| 459 | dev_set_drvdata(dev, ipu_crtc); |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static void ipu_drm_unbind(struct device *dev, struct device *master, |
| 465 | void *data) |
| 466 | { |
| 467 | struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev); |
| 468 | |
| 469 | imx_drm_remove_crtc(ipu_crtc->imx_crtc); |
| 470 | |
| 471 | ipu_plane_put_resources(ipu_crtc->plane[0]); |
| 472 | ipu_put_resources(ipu_crtc); |
| 473 | } |
| 474 | |
| 475 | static const struct component_ops ipu_crtc_ops = { |
| 476 | .bind = ipu_drm_bind, |
| 477 | .unbind = ipu_drm_unbind, |
| 478 | }; |
| 479 | |
| 480 | static int ipu_drm_probe(struct platform_device *pdev) |
| 481 | { |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 482 | struct device *dev = &pdev->dev; |
| 483 | struct ipu_client_platformdata *pdata = dev->platform_data; |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 484 | int ret; |
| 485 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 486 | if (!dev->platform_data) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 487 | return -EINVAL; |
| 488 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 489 | if (!dev->of_node) { |
| 490 | /* Associate crtc device with the corresponding DI port node */ |
| 491 | dev->of_node = ipu_drm_get_port_by_id(dev->parent->of_node, |
| 492 | pdata->di + 2); |
| 493 | if (!dev->of_node) { |
| 494 | dev_err(dev, "missing port@%d node in %s\n", |
| 495 | pdata->di + 2, dev->parent->of_node->full_name); |
| 496 | return -ENODEV; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); |
Russell King | 4cdbb4f | 2013-06-10 16:56:16 +0100 | [diff] [blame] | 501 | if (ret) |
| 502 | return ret; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 503 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 504 | return component_add(dev, &ipu_crtc_ops); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Bill Pemberton | 8aa1be4 | 2012-11-19 13:26:38 -0500 | [diff] [blame] | 507 | static int ipu_drm_remove(struct platform_device *pdev) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 508 | { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 509 | component_del(&pdev->dev, &ipu_crtc_ops); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static struct platform_driver ipu_drm_driver = { |
| 514 | .driver = { |
| 515 | .name = "imx-ipuv3-crtc", |
| 516 | }, |
| 517 | .probe = ipu_drm_probe, |
Bill Pemberton | 99c28f1 | 2012-11-19 13:20:51 -0500 | [diff] [blame] | 518 | .remove = ipu_drm_remove, |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 519 | }; |
| 520 | module_platform_driver(ipu_drm_driver); |
| 521 | |
| 522 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 523 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 524 | MODULE_LICENSE("GPL"); |
Fabio Estevam | ce9c1ce | 2013-08-18 21:40:06 -0300 | [diff] [blame] | 525 | MODULE_ALIAS("platform:imx-ipuv3-crtc"); |