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; |
Philipp Zabel | 2872c80 | 2015-02-02 17:25:59 +0100 | [diff] [blame] | 48 | u32 bus_format; |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 49 | int di_hsync_pin; |
| 50 | int di_vsync_pin; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base) |
| 54 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 55 | static void ipu_fb_enable(struct ipu_crtc *ipu_crtc) |
| 56 | { |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 57 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
| 58 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 59 | if (ipu_crtc->enabled) |
| 60 | return; |
| 61 | |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 62 | ipu_dc_enable(ipu); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 63 | ipu_plane_enable(ipu_crtc->plane[0]); |
Philipp Zabel | c115edb | 2014-04-14 23:53:22 +0200 | [diff] [blame] | 64 | /* Start DC channel and DI after IDMAC */ |
| 65 | ipu_dc_enable_channel(ipu_crtc->dc); |
| 66 | ipu_di_enable(ipu_crtc->di); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 67 | |
| 68 | ipu_crtc->enabled = 1; |
| 69 | } |
| 70 | |
| 71 | static void ipu_fb_disable(struct ipu_crtc *ipu_crtc) |
| 72 | { |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 73 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
| 74 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 75 | if (!ipu_crtc->enabled) |
| 76 | return; |
| 77 | |
Philipp Zabel | c115edb | 2014-04-14 23:53:22 +0200 | [diff] [blame] | 78 | /* Stop DC channel and DI before IDMAC */ |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 79 | ipu_dc_disable_channel(ipu_crtc->dc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 80 | ipu_di_disable(ipu_crtc->di); |
Philipp Zabel | c115edb | 2014-04-14 23:53:22 +0200 | [diff] [blame] | 81 | ipu_plane_disable(ipu_crtc->plane[0]); |
Philipp Zabel | 1e6d486 | 2014-04-14 23:53:23 +0200 | [diff] [blame] | 82 | ipu_dc_disable(ipu); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 83 | |
| 84 | ipu_crtc->enabled = 0; |
| 85 | } |
| 86 | |
| 87 | static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode) |
| 88 | { |
| 89 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 90 | |
Philipp Zabel | a8e4e23 | 2012-11-12 16:29:01 +0100 | [diff] [blame] | 91 | dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 92 | |
| 93 | switch (mode) { |
| 94 | case DRM_MODE_DPMS_ON: |
| 95 | ipu_fb_enable(ipu_crtc); |
| 96 | break; |
| 97 | case DRM_MODE_DPMS_STANDBY: |
| 98 | case DRM_MODE_DPMS_SUSPEND: |
| 99 | case DRM_MODE_DPMS_OFF: |
| 100 | ipu_fb_disable(ipu_crtc); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static int ipu_page_flip(struct drm_crtc *crtc, |
| 106 | struct drm_framebuffer *fb, |
Keith Packard | ed8d197 | 2013-07-22 18:49:58 -0700 | [diff] [blame] | 107 | struct drm_pending_vblank_event *event, |
| 108 | uint32_t page_flip_flags) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 109 | { |
| 110 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 111 | int ret; |
| 112 | |
| 113 | if (ipu_crtc->newfb) |
| 114 | return -EBUSY; |
| 115 | |
| 116 | ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc); |
| 117 | if (ret) { |
| 118 | dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n"); |
| 119 | list_del(&event->base.link); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | ipu_crtc->newfb = fb; |
| 125 | ipu_crtc->page_flip_event = event; |
Matt Roper | f4510a2 | 2014-04-01 15:22:40 -0700 | [diff] [blame] | 126 | crtc->primary->fb = fb; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 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, |
| 135 | }; |
| 136 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 137 | static int ipu_crtc_mode_set(struct drm_crtc *crtc, |
| 138 | struct drm_display_mode *orig_mode, |
| 139 | struct drm_display_mode *mode, |
| 140 | int x, int y, |
| 141 | struct drm_framebuffer *old_fb) |
| 142 | { |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 143 | struct drm_device *dev = crtc->dev; |
| 144 | struct drm_encoder *encoder; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 145 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 146 | struct ipu_di_signal_cfg sig_cfg = {}; |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 147 | unsigned long encoder_types = 0; |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 148 | int ret; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 149 | |
| 150 | dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__, |
| 151 | mode->hdisplay); |
| 152 | dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__, |
| 153 | mode->vdisplay); |
| 154 | |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 155 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) |
| 156 | if (encoder->crtc == crtc) |
| 157 | encoder_types |= BIT(encoder->encoder_type); |
| 158 | |
| 159 | dev_dbg(ipu_crtc->dev, "%s: attached to encoder types 0x%lx\n", |
| 160 | __func__, encoder_types); |
| 161 | |
| 162 | /* |
Philipp Zabel | e0d155c | 2014-07-11 17:28:45 +0200 | [diff] [blame] | 163 | * If we have DAC or LDB, then we need the IPU DI clock to be |
| 164 | * the same as the LDB DI clock. For TVDAC, derive the IPU DI |
| 165 | * clock from 27 MHz TVE_DI clock, but allow to divide it. |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 166 | */ |
| 167 | if (encoder_types & (BIT(DRM_MODE_ENCODER_DAC) | |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 168 | BIT(DRM_MODE_ENCODER_LVDS))) |
| 169 | sig_cfg.clkflags = IPU_DI_CLKMODE_SYNC | IPU_DI_CLKMODE_EXT; |
Philipp Zabel | e0d155c | 2014-07-11 17:28:45 +0200 | [diff] [blame] | 170 | else if (encoder_types & BIT(DRM_MODE_ENCODER_TVDAC)) |
| 171 | sig_cfg.clkflags = IPU_DI_CLKMODE_EXT; |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 172 | else |
| 173 | sig_cfg.clkflags = 0; |
| 174 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 175 | sig_cfg.enable_pol = 1; |
Denis Carikli | 85de9d1 | 2014-04-07 14:44:43 +0200 | [diff] [blame] | 176 | sig_cfg.clk_pol = 0; |
Philipp Zabel | 2872c80 | 2015-02-02 17:25:59 +0100 | [diff] [blame] | 177 | sig_cfg.bus_format = ipu_crtc->bus_format; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 178 | sig_cfg.v_to_h_sync = 0; |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 179 | sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin; |
| 180 | sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin; |
| 181 | |
Steve Longerbeam | b6835a7 | 2014-12-18 18:00:25 -0800 | [diff] [blame] | 182 | drm_display_mode_to_videomode(mode, &sig_cfg.mode); |
| 183 | |
| 184 | ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di, |
| 185 | mode->flags & DRM_MODE_FLAG_INTERLACE, |
Philipp Zabel | 2872c80 | 2015-02-02 17:25:59 +0100 | [diff] [blame] | 186 | ipu_crtc->bus_format, mode->hdisplay); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 187 | if (ret) { |
| 188 | dev_err(ipu_crtc->dev, |
| 189 | "initializing display controller failed with %d\n", |
| 190 | ret); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg); |
| 195 | if (ret) { |
| 196 | dev_err(ipu_crtc->dev, |
| 197 | "initializing panel failed with %d\n", ret); |
| 198 | return ret; |
| 199 | } |
| 200 | |
Yannis Damigos | 30e94a5 | 2014-08-19 18:26:46 +0300 | [diff] [blame] | 201 | return ipu_plane_mode_set(ipu_crtc->plane[0], crtc, mode, |
| 202 | crtc->primary->fb, |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 203 | 0, 0, mode->hdisplay, mode->vdisplay, |
Philipp Zabel | dd7fa6d | 2014-07-11 18:02:06 +0200 | [diff] [blame] | 204 | x, y, mode->hdisplay, mode->vdisplay, |
| 205 | mode->flags & DRM_MODE_FLAG_INTERLACE); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc) |
| 209 | { |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 210 | unsigned long flags; |
| 211 | struct drm_device *drm = ipu_crtc->base.dev; |
| 212 | |
| 213 | spin_lock_irqsave(&drm->event_lock, flags); |
Rob Clark | 0eca56f | 2012-10-08 19:50:46 +0000 | [diff] [blame] | 214 | if (ipu_crtc->page_flip_event) |
Russell King | 69d21fc | 2015-11-25 10:25:39 +0000 | [diff] [blame] | 215 | drm_crtc_send_vblank_event(&ipu_crtc->base, |
| 216 | ipu_crtc->page_flip_event); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 217 | ipu_crtc->page_flip_event = NULL; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 218 | imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 219 | spin_unlock_irqrestore(&drm->event_lock, flags); |
| 220 | } |
| 221 | |
| 222 | static irqreturn_t ipu_irq_handler(int irq, void *dev_id) |
| 223 | { |
| 224 | struct ipu_crtc *ipu_crtc = dev_id; |
| 225 | |
| 226 | imx_drm_handle_vblank(ipu_crtc->imx_crtc); |
| 227 | |
| 228 | if (ipu_crtc->newfb) { |
Yannis Damigos | 30e94a5 | 2014-08-19 18:26:46 +0300 | [diff] [blame] | 229 | struct ipu_plane *plane = ipu_crtc->plane[0]; |
| 230 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 231 | ipu_crtc->newfb = NULL; |
Yannis Damigos | 30e94a5 | 2014-08-19 18:26:46 +0300 | [diff] [blame] | 232 | ipu_plane_set_base(plane, ipu_crtc->base.primary->fb, |
| 233 | plane->x, plane->y); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 234 | ipu_crtc_handle_pageflip(ipu_crtc); |
| 235 | } |
| 236 | |
| 237 | return IRQ_HANDLED; |
| 238 | } |
| 239 | |
| 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) |
| 243 | { |
Steve Longerbeam | 0c460a5 | 2014-12-18 18:00:23 -0800 | [diff] [blame] | 244 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 245 | struct videomode vm; |
| 246 | int ret; |
| 247 | |
| 248 | drm_display_mode_to_videomode(adjusted_mode, &vm); |
| 249 | |
| 250 | ret = ipu_di_adjust_videomode(ipu_crtc->di, &vm); |
| 251 | if (ret) |
| 252 | return false; |
| 253 | |
| 254 | drm_display_mode_from_videomode(&vm, adjusted_mode); |
| 255 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | |
| 259 | static void ipu_crtc_prepare(struct drm_crtc *crtc) |
| 260 | { |
| 261 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 262 | |
| 263 | ipu_fb_disable(ipu_crtc); |
| 264 | } |
| 265 | |
| 266 | static void ipu_crtc_commit(struct drm_crtc *crtc) |
| 267 | { |
| 268 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 269 | |
| 270 | ipu_fb_enable(ipu_crtc); |
| 271 | } |
| 272 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 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, |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | static int ipu_enable_vblank(struct drm_crtc *crtc) |
| 282 | { |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static void ipu_disable_vblank(struct drm_crtc *crtc) |
| 287 | { |
| 288 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 289 | |
Sascha Hauer | 6ee4d7f | 2013-10-10 16:18:44 +0200 | [diff] [blame] | 290 | ipu_crtc->page_flip_event = NULL; |
| 291 | ipu_crtc->newfb = NULL; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Russell King | d50141d | 2014-12-21 15:58:19 +0000 | [diff] [blame] | 294 | static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc, |
Philipp Zabel | 2872c80 | 2015-02-02 17:25:59 +0100 | [diff] [blame] | 295 | u32 bus_format, int hsync_pin, int vsync_pin) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 296 | { |
| 297 | struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc); |
| 298 | |
Philipp Zabel | 2872c80 | 2015-02-02 17:25:59 +0100 | [diff] [blame] | 299 | ipu_crtc->bus_format = bus_format; |
Philipp Zabel | 2ea4260 | 2013-04-08 18:04:35 +0200 | [diff] [blame] | 300 | ipu_crtc->di_hsync_pin = hsync_pin; |
| 301 | ipu_crtc->di_vsync_pin = vsync_pin; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 302 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 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, |
| 312 | }; |
| 313 | |
| 314 | static void ipu_put_resources(struct ipu_crtc *ipu_crtc) |
| 315 | { |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 316 | if (!IS_ERR_OR_NULL(ipu_crtc->dc)) |
| 317 | ipu_dc_put(ipu_crtc->dc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 318 | if (!IS_ERR_OR_NULL(ipu_crtc->di)) |
| 319 | ipu_di_put(ipu_crtc->di); |
| 320 | } |
| 321 | |
| 322 | static int ipu_get_resources(struct ipu_crtc *ipu_crtc, |
| 323 | struct ipu_client_platformdata *pdata) |
| 324 | { |
| 325 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
| 326 | int ret; |
| 327 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 328 | ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc); |
| 329 | if (IS_ERR(ipu_crtc->dc)) { |
| 330 | ret = PTR_ERR(ipu_crtc->dc); |
| 331 | goto err_out; |
| 332 | } |
| 333 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 334 | ipu_crtc->di = ipu_di_get(ipu, pdata->di); |
| 335 | if (IS_ERR(ipu_crtc->di)) { |
| 336 | ret = PTR_ERR(ipu_crtc->di); |
| 337 | goto err_out; |
| 338 | } |
| 339 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 340 | return 0; |
| 341 | err_out: |
| 342 | ipu_put_resources(ipu_crtc); |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | static int ipu_crtc_init(struct ipu_crtc *ipu_crtc, |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 348 | struct ipu_client_platformdata *pdata, struct drm_device *drm) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 349 | { |
Philipp Zabel | 47b1be5 | 2013-02-20 10:57:01 +0800 | [diff] [blame] | 350 | struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 351 | int dp = -EINVAL; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 352 | int ret; |
| 353 | |
| 354 | ret = ipu_get_resources(ipu_crtc, pdata); |
| 355 | if (ret) { |
| 356 | dev_err(ipu_crtc->dev, "getting resources failed with %d.\n", |
| 357 | ret); |
| 358 | return ret; |
| 359 | } |
| 360 | |
Philipp Zabel | 4389559 | 2015-11-06 11:08:02 +0100 | [diff] [blame] | 361 | if (pdata->dp >= 0) |
| 362 | dp = IPU_DP_FLOW_SYNC_BG; |
| 363 | ipu_crtc->plane[0] = ipu_plane_init(drm, ipu, pdata->dma[0], dp, 0, |
| 364 | DRM_PLANE_TYPE_PRIMARY); |
Liu Ying | a7ed3c2 | 2015-11-06 22:42:45 +0800 | [diff] [blame] | 365 | if (IS_ERR(ipu_crtc->plane[0])) { |
| 366 | ret = PTR_ERR(ipu_crtc->plane[0]); |
| 367 | goto err_put_resources; |
| 368 | } |
Philipp Zabel | 4389559 | 2015-11-06 11:08:02 +0100 | [diff] [blame] | 369 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 370 | ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc, |
Philipp Zabel | 4389559 | 2015-11-06 11:08:02 +0100 | [diff] [blame] | 371 | &ipu_crtc->plane[0]->base, &ipu_crtc_helper_funcs, |
| 372 | 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 | ret = ipu_plane_get_resources(ipu_crtc->plane[0]); |
| 379 | if (ret) { |
| 380 | dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n", |
| 381 | ret); |
| 382 | goto err_remove_crtc; |
| 383 | } |
| 384 | |
| 385 | /* If this crtc is using the DP, add an overlay plane */ |
| 386 | if (pdata->dp >= 0 && pdata->dma[1] > 0) { |
Philipp Zabel | 4389559 | 2015-11-06 11:08:02 +0100 | [diff] [blame] | 387 | ipu_crtc->plane[1] = ipu_plane_init(drm, ipu, pdata->dma[1], |
| 388 | IPU_DP_FLOW_SYNC_FG, |
| 389 | drm_crtc_mask(&ipu_crtc->base), |
| 390 | DRM_PLANE_TYPE_OVERLAY); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 391 | if (IS_ERR(ipu_crtc->plane[1])) |
| 392 | ipu_crtc->plane[1] = NULL; |
| 393 | } |
| 394 | |
| 395 | ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]); |
Philipp Zabel | 47b1be5 | 2013-02-20 10:57:01 +0800 | [diff] [blame] | 396 | ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0, |
| 397 | "imx_drm", ipu_crtc); |
| 398 | if (ret < 0) { |
| 399 | dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 400 | goto err_put_plane_res; |
Philipp Zabel | 47b1be5 | 2013-02-20 10:57:01 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 403 | return 0; |
| 404 | |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 405 | err_put_plane_res: |
| 406 | ipu_plane_put_resources(ipu_crtc->plane[0]); |
| 407 | err_remove_crtc: |
| 408 | imx_drm_remove_crtc(ipu_crtc->imx_crtc); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 409 | err_put_resources: |
| 410 | ipu_put_resources(ipu_crtc); |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 415 | 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] | 416 | { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 417 | struct ipu_client_platformdata *pdata = dev->platform_data; |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 418 | struct drm_device *drm = data; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 419 | struct ipu_crtc *ipu_crtc; |
| 420 | int ret; |
| 421 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 422 | ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL); |
| 423 | if (!ipu_crtc) |
| 424 | return -ENOMEM; |
| 425 | |
| 426 | ipu_crtc->dev = dev; |
| 427 | |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 428 | ret = ipu_crtc_init(ipu_crtc, pdata, drm); |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 429 | if (ret) |
| 430 | return ret; |
| 431 | |
| 432 | dev_set_drvdata(dev, ipu_crtc); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static void ipu_drm_unbind(struct device *dev, struct device *master, |
| 438 | void *data) |
| 439 | { |
| 440 | struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev); |
| 441 | |
| 442 | imx_drm_remove_crtc(ipu_crtc->imx_crtc); |
| 443 | |
| 444 | ipu_plane_put_resources(ipu_crtc->plane[0]); |
| 445 | ipu_put_resources(ipu_crtc); |
| 446 | } |
| 447 | |
| 448 | static const struct component_ops ipu_crtc_ops = { |
| 449 | .bind = ipu_drm_bind, |
| 450 | .unbind = ipu_drm_unbind, |
| 451 | }; |
| 452 | |
| 453 | static int ipu_drm_probe(struct platform_device *pdev) |
| 454 | { |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 455 | struct device *dev = &pdev->dev; |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 456 | int ret; |
| 457 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 458 | if (!dev->platform_data) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 459 | return -EINVAL; |
| 460 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 461 | ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); |
Russell King | 4cdbb4f | 2013-06-10 16:56:16 +0100 | [diff] [blame] | 462 | if (ret) |
| 463 | return ret; |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 464 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 465 | return component_add(dev, &ipu_crtc_ops); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Bill Pemberton | 8aa1be4 | 2012-11-19 13:26:38 -0500 | [diff] [blame] | 468 | static int ipu_drm_remove(struct platform_device *pdev) |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 469 | { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 470 | component_del(&pdev->dev, &ipu_crtc_ops); |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static struct platform_driver ipu_drm_driver = { |
| 475 | .driver = { |
| 476 | .name = "imx-ipuv3-crtc", |
| 477 | }, |
| 478 | .probe = ipu_drm_probe, |
Bill Pemberton | 99c28f1 | 2012-11-19 13:20:51 -0500 | [diff] [blame] | 479 | .remove = ipu_drm_remove, |
Sascha Hauer | f326f79 | 2012-09-21 10:07:50 +0200 | [diff] [blame] | 480 | }; |
| 481 | module_platform_driver(ipu_drm_driver); |
| 482 | |
| 483 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 484 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 485 | MODULE_LICENSE("GPL"); |
Fabio Estevam | ce9c1ce | 2013-08-18 21:40:06 -0300 | [diff] [blame] | 486 | MODULE_ALIAS("platform:imx-ipuv3-crtc"); |