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