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