Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Freescale i.MX drm 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. |
| 14 | * |
| 15 | */ |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 16 | #include <linux/component.h> |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 17 | #include <linux/device.h> |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 18 | #include <linux/dma-buf.h> |
Russell King | e7d6231 | 2013-11-03 15:38:09 +0000 | [diff] [blame] | 19 | #include <linux/module.h> |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 21 | #include <linux/reservation.h> |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 22 | #include <drm/drmP.h> |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 23 | #include <drm/drm_atomic.h> |
| 24 | #include <drm/drm_atomic_helper.h> |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 25 | #include <drm/drm_fb_helper.h> |
| 26 | #include <drm/drm_crtc_helper.h> |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 27 | #include <drm/drm_gem_cma_helper.h> |
| 28 | #include <drm/drm_fb_cma_helper.h> |
Daniel Vetter | 3cb9ae4 | 2014-10-29 10:03:57 +0100 | [diff] [blame] | 29 | #include <drm/drm_plane_helper.h> |
Russell King | 6457b97 | 2014-07-03 17:52:57 +0100 | [diff] [blame] | 30 | #include <drm/drm_of.h> |
Philipp Zabel | 310944d | 2016-05-12 15:00:44 +0200 | [diff] [blame] | 31 | #include <video/imx-ipu-v3.h> |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 32 | |
| 33 | #include "imx-drm.h" |
| 34 | |
| 35 | #define MAX_CRTC 4 |
| 36 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 37 | struct imx_drm_component { |
| 38 | struct device_node *of_node; |
| 39 | struct list_head list; |
| 40 | }; |
| 41 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 42 | struct imx_drm_device { |
| 43 | struct drm_device *drm; |
Russell King | 887eceac | 2013-11-24 13:23:17 +0000 | [diff] [blame] | 44 | struct imx_drm_crtc *crtc[MAX_CRTC]; |
Thierry Reding | d2ab8ad | 2015-12-15 12:20:51 +0100 | [diff] [blame] | 45 | unsigned int pipes; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 46 | struct drm_fbdev_cma *fbhelper; |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 47 | struct drm_atomic_state *state; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct imx_drm_crtc { |
| 51 | struct drm_crtc *crtc; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 52 | struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Archit Taneja | c1ff5a7 | 2015-10-27 13:40:57 +0530 | [diff] [blame] | 55 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) |
Russell King | 8acba02 | 2013-11-03 12:13:47 +0000 | [diff] [blame] | 56 | static int legacyfb_depth = 16; |
| 57 | module_param(legacyfb_depth, int, 0444); |
Archit Taneja | c1ff5a7 | 2015-10-27 13:40:57 +0530 | [diff] [blame] | 58 | #endif |
Russell King | 8acba02 | 2013-11-03 12:13:47 +0000 | [diff] [blame] | 59 | |
Thierry Reding | d2ab8ad | 2015-12-15 12:20:51 +0100 | [diff] [blame] | 60 | unsigned int imx_drm_crtc_id(struct imx_drm_crtc *crtc) |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 61 | { |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 62 | return drm_crtc_index(crtc->crtc); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 63 | } |
Josh Boyer | 9c74360 | 2013-11-12 12:15:45 -0500 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(imx_drm_crtc_id); |
Philipp Zabel | b8d181e | 2013-10-10 16:18:45 +0200 | [diff] [blame] | 65 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 66 | static void imx_drm_driver_lastclose(struct drm_device *drm) |
| 67 | { |
| 68 | struct imx_drm_device *imxdrm = drm->dev_private; |
| 69 | |
Markus Elfring | 3f3a728 | 2015-07-05 22:45:23 +0200 | [diff] [blame] | 70 | drm_fbdev_cma_restore_mode(imxdrm->fbhelper); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static int imx_drm_driver_unload(struct drm_device *drm) |
| 74 | { |
| 75 | struct imx_drm_device *imxdrm = drm->dev_private; |
| 76 | |
Russell King | 3e68439 | 2013-11-03 22:18:40 +0000 | [diff] [blame] | 77 | drm_kms_helper_poll_fini(drm); |
| 78 | |
Russell King | 8acba02 | 2013-11-03 12:13:47 +0000 | [diff] [blame] | 79 | if (imxdrm->fbhelper) |
| 80 | drm_fbdev_cma_fini(imxdrm->fbhelper); |
Russell King | 8acba02 | 2013-11-03 12:13:47 +0000 | [diff] [blame] | 81 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 82 | component_unbind_all(drm->dev, drm); |
| 83 | |
Russell King | 020a9ea | 2013-12-17 19:10:47 +0000 | [diff] [blame] | 84 | drm_vblank_cleanup(drm); |
Russell King | 020a9ea | 2013-12-17 19:10:47 +0000 | [diff] [blame] | 85 | drm_mode_config_cleanup(drm); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 86 | |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 87 | platform_set_drvdata(drm->platformdev, NULL); |
| 88 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 92 | int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc) |
| 93 | { |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 94 | return drm_crtc_vblank_get(imx_drm_crtc->crtc); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 95 | } |
| 96 | EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get); |
| 97 | |
| 98 | void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc) |
| 99 | { |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 100 | drm_crtc_vblank_put(imx_drm_crtc->crtc); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put); |
| 103 | |
| 104 | void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc) |
| 105 | { |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 106 | drm_crtc_handle_vblank(imx_drm_crtc->crtc); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 107 | } |
| 108 | EXPORT_SYMBOL_GPL(imx_drm_handle_vblank); |
| 109 | |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 110 | static int imx_drm_enable_vblank(struct drm_device *drm, unsigned int pipe) |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 111 | { |
| 112 | struct imx_drm_device *imxdrm = drm->dev_private; |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 113 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe]; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 114 | int ret; |
| 115 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 116 | if (!imx_drm_crtc) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank) |
| 120 | return -ENOSYS; |
| 121 | |
| 122 | ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank( |
| 123 | imx_drm_crtc->crtc); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 128 | static void imx_drm_disable_vblank(struct drm_device *drm, unsigned int pipe) |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 129 | { |
| 130 | struct imx_drm_device *imxdrm = drm->dev_private; |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 131 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[pipe]; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 132 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 133 | if (!imx_drm_crtc) |
| 134 | return; |
| 135 | |
| 136 | if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank) |
| 137 | return; |
| 138 | |
| 139 | imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc); |
| 140 | } |
| 141 | |
| 142 | static const struct file_operations imx_drm_driver_fops = { |
| 143 | .owner = THIS_MODULE, |
| 144 | .open = drm_open, |
| 145 | .release = drm_release, |
| 146 | .unlocked_ioctl = drm_ioctl, |
| 147 | .mmap = drm_gem_cma_mmap, |
| 148 | .poll = drm_poll, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 149 | .read = drm_read, |
| 150 | .llseek = noop_llseek, |
| 151 | }; |
| 152 | |
Russell King | 8a51a33 | 2013-11-03 13:28:24 +0000 | [diff] [blame] | 153 | void imx_drm_connector_destroy(struct drm_connector *connector) |
| 154 | { |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 155 | drm_connector_unregister(connector); |
Russell King | 8a51a33 | 2013-11-03 13:28:24 +0000 | [diff] [blame] | 156 | drm_connector_cleanup(connector); |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(imx_drm_connector_destroy); |
| 159 | |
| 160 | void imx_drm_encoder_destroy(struct drm_encoder *encoder) |
| 161 | { |
| 162 | drm_encoder_cleanup(encoder); |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy); |
| 165 | |
Russell King | 3e68439 | 2013-11-03 22:18:40 +0000 | [diff] [blame] | 166 | static void imx_drm_output_poll_changed(struct drm_device *drm) |
| 167 | { |
Russell King | 3e68439 | 2013-11-03 22:18:40 +0000 | [diff] [blame] | 168 | struct imx_drm_device *imxdrm = drm->dev_private; |
| 169 | |
| 170 | drm_fbdev_cma_hotplug_event(imxdrm->fbhelper); |
Russell King | 3e68439 | 2013-11-03 22:18:40 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ville Syrjälä | 7ae847d | 2015-12-15 12:21:09 +0100 | [diff] [blame] | 173 | static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = { |
Russell King | 1df8b53 | 2013-11-03 16:04:48 +0000 | [diff] [blame] | 174 | .fb_create = drm_fb_cma_create, |
Russell King | 3e68439 | 2013-11-03 22:18:40 +0000 | [diff] [blame] | 175 | .output_poll_changed = imx_drm_output_poll_changed, |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 176 | .atomic_check = drm_atomic_helper_check, |
| 177 | .atomic_commit = drm_atomic_helper_commit, |
| 178 | }; |
| 179 | |
| 180 | static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state) |
| 181 | { |
| 182 | struct drm_device *dev = state->dev; |
| 183 | struct drm_crtc *crtc; |
| 184 | struct drm_crtc_state *crtc_state; |
| 185 | struct drm_plane_state *plane_state; |
| 186 | struct drm_gem_cma_object *cma_obj; |
| 187 | struct fence *excl; |
| 188 | unsigned shared_count; |
| 189 | struct fence **shared; |
| 190 | unsigned int i, j; |
| 191 | int ret; |
| 192 | |
| 193 | /* Wait for fences. */ |
| 194 | for_each_crtc_in_state(state, crtc, crtc_state, i) { |
| 195 | plane_state = crtc->primary->state; |
| 196 | if (plane_state->fb) { |
| 197 | cma_obj = drm_fb_cma_get_gem_obj(plane_state->fb, 0); |
| 198 | if (cma_obj->base.dma_buf) { |
| 199 | ret = reservation_object_get_fences_rcu( |
| 200 | cma_obj->base.dma_buf->resv, &excl, |
| 201 | &shared_count, &shared); |
| 202 | if (unlikely(ret)) |
| 203 | DRM_ERROR("failed to get fences " |
| 204 | "for buffer\n"); |
| 205 | |
| 206 | if (excl) { |
| 207 | fence_wait(excl, false); |
| 208 | fence_put(excl); |
| 209 | } |
| 210 | for (j = 0; j < shared_count; i++) { |
| 211 | fence_wait(shared[j], false); |
| 212 | fence_put(shared[j]); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | drm_atomic_helper_commit_modeset_disables(dev, state); |
| 219 | |
| 220 | drm_atomic_helper_commit_planes(dev, state, true); |
| 221 | |
| 222 | drm_atomic_helper_commit_modeset_enables(dev, state); |
| 223 | |
| 224 | drm_atomic_helper_commit_hw_done(state); |
| 225 | |
| 226 | drm_atomic_helper_wait_for_vblanks(dev, state); |
| 227 | |
| 228 | drm_atomic_helper_cleanup_planes(dev, state); |
| 229 | } |
| 230 | |
| 231 | static struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = { |
| 232 | .atomic_commit_tail = imx_drm_atomic_commit_tail, |
Russell King | 1df8b53 | 2013-11-03 16:04:48 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 235 | /* |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 236 | * Main DRM initialisation. This binds, initialises and registers |
| 237 | * with DRM the subcomponents of the driver. |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 238 | */ |
| 239 | static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags) |
| 240 | { |
Russell King | b85f2b5 | 2013-11-03 15:31:22 +0000 | [diff] [blame] | 241 | struct imx_drm_device *imxdrm; |
Russell King | e355e7d | 2013-11-03 18:39:29 +0000 | [diff] [blame] | 242 | struct drm_connector *connector; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 243 | int ret; |
| 244 | |
Russell King | b85f2b5 | 2013-11-03 15:31:22 +0000 | [diff] [blame] | 245 | imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL); |
| 246 | if (!imxdrm) |
| 247 | return -ENOMEM; |
| 248 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 249 | imxdrm->drm = drm; |
| 250 | |
| 251 | drm->dev_private = imxdrm; |
| 252 | |
| 253 | /* |
| 254 | * enable drm irq mode. |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 255 | * - with irq_enabled = true, we can use the vblank feature. |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 256 | * |
| 257 | * P.S. note that we wouldn't use drm irq handler but |
| 258 | * just specific driver own one instead because |
| 259 | * drm framework supports only one irq handler and |
| 260 | * drivers can well take care of their interrupts |
| 261 | */ |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 262 | drm->irq_enabled = true; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 263 | |
Russell King | 1df8b53 | 2013-11-03 16:04:48 +0000 | [diff] [blame] | 264 | /* |
| 265 | * set max width and height as default value(4096x4096). |
| 266 | * this value would be used to check framebuffer size limitation |
| 267 | * at drm_mode_addfb(). |
| 268 | */ |
| 269 | drm->mode_config.min_width = 64; |
| 270 | drm->mode_config.min_height = 64; |
| 271 | drm->mode_config.max_width = 4096; |
| 272 | drm->mode_config.max_height = 4096; |
| 273 | drm->mode_config.funcs = &imx_drm_mode_config_funcs; |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 274 | drm->mode_config.helper_private = &imx_drm_mode_config_helpers; |
Russell King | 1df8b53 | 2013-11-03 16:04:48 +0000 | [diff] [blame] | 275 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 276 | drm_mode_config_init(drm); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 277 | |
Russell King | 020a9ea | 2013-12-17 19:10:47 +0000 | [diff] [blame] | 278 | ret = drm_vblank_init(drm, MAX_CRTC); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 279 | if (ret) |
Russell King | 8007875 | 2013-12-16 11:33:44 +0000 | [diff] [blame] | 280 | goto err_kms; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 281 | |
Daniel Vetter | a72f8be | 2013-11-03 14:31:09 +0100 | [diff] [blame] | 282 | platform_set_drvdata(drm->platformdev, drm); |
Russell King | 8d71de6 | 2013-11-03 13:55:34 +0000 | [diff] [blame] | 283 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 284 | /* Now try and bind all our sub-components */ |
| 285 | ret = component_bind_all(drm->dev, drm); |
| 286 | if (ret) |
Russell King | ccec7f6 | 2013-11-03 15:20:18 +0000 | [diff] [blame] | 287 | goto err_vblank; |
Russell King | e355e7d | 2013-11-03 18:39:29 +0000 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | * All components are now added, we can publish the connector sysfs |
| 291 | * entries to userspace. This will generate hotplug events and so |
| 292 | * userspace will expect to be able to access DRM at this point. |
| 293 | */ |
| 294 | list_for_each_entry(connector, &drm->mode_config.connector_list, head) { |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 295 | ret = drm_connector_register(connector); |
Russell King | e355e7d | 2013-11-03 18:39:29 +0000 | [diff] [blame] | 296 | if (ret) { |
| 297 | dev_err(drm->dev, |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 298 | "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n", |
Russell King | e355e7d | 2013-11-03 18:39:29 +0000 | [diff] [blame] | 299 | connector->base.id, |
Jani Nikula | 4a2a450 | 2014-06-03 14:56:16 +0300 | [diff] [blame] | 300 | connector->name, ret); |
Russell King | e355e7d | 2013-11-03 18:39:29 +0000 | [diff] [blame] | 301 | goto err_unbind; |
| 302 | } |
| 303 | } |
| 304 | |
Liu Ying | 255c35f | 2016-07-08 17:40:56 +0800 | [diff] [blame] | 305 | drm_mode_config_reset(drm); |
| 306 | |
Russell King | 8acba02 | 2013-11-03 12:13:47 +0000 | [diff] [blame] | 307 | /* |
| 308 | * All components are now initialised, so setup the fb helper. |
| 309 | * The fb helper takes copies of key hardware information, so the |
| 310 | * crtcs/connectors/encoders must not change after this point. |
| 311 | */ |
Archit Taneja | c1ff5a7 | 2015-10-27 13:40:57 +0530 | [diff] [blame] | 312 | #if IS_ENABLED(CONFIG_DRM_FBDEV_EMULATION) |
Russell King | 8acba02 | 2013-11-03 12:13:47 +0000 | [diff] [blame] | 313 | if (legacyfb_depth != 16 && legacyfb_depth != 32) { |
| 314 | dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n"); |
| 315 | legacyfb_depth = 16; |
| 316 | } |
| 317 | imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, |
| 318 | drm->mode_config.num_crtc, MAX_CRTC); |
| 319 | if (IS_ERR(imxdrm->fbhelper)) { |
| 320 | ret = PTR_ERR(imxdrm->fbhelper); |
| 321 | imxdrm->fbhelper = NULL; |
| 322 | goto err_unbind; |
| 323 | } |
| 324 | #endif |
Russell King | 3e68439 | 2013-11-03 22:18:40 +0000 | [diff] [blame] | 325 | |
| 326 | drm_kms_helper_poll_init(drm); |
| 327 | |
Russell King | 8007875 | 2013-12-16 11:33:44 +0000 | [diff] [blame] | 328 | return 0; |
Daniel Vetter | a72f8be | 2013-11-03 14:31:09 +0100 | [diff] [blame] | 329 | |
Russell King | e355e7d | 2013-11-03 18:39:29 +0000 | [diff] [blame] | 330 | err_unbind: |
| 331 | component_unbind_all(drm->dev, drm); |
Russell King | ccec7f6 | 2013-11-03 15:20:18 +0000 | [diff] [blame] | 332 | err_vblank: |
Russell King | 8007875 | 2013-12-16 11:33:44 +0000 | [diff] [blame] | 333 | drm_vblank_cleanup(drm); |
| 334 | err_kms: |
Russell King | 8007875 | 2013-12-16 11:33:44 +0000 | [diff] [blame] | 335 | drm_mode_config_cleanup(drm); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 340 | /* |
| 341 | * imx_drm_add_crtc - add a new crtc |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 342 | */ |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 343 | int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc, |
Philipp Zabel | 4389559 | 2015-11-06 11:08:02 +0100 | [diff] [blame] | 344 | struct imx_drm_crtc **new_crtc, struct drm_plane *primary_plane, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 345 | const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs, |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 346 | struct device_node *port) |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 347 | { |
Russell King | 32266b4 | 2013-11-03 12:26:23 +0000 | [diff] [blame] | 348 | struct imx_drm_device *imxdrm = drm->dev_private; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 349 | struct imx_drm_crtc *imx_drm_crtc; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 350 | |
Russell King | fd6040e | 2013-12-16 12:39:31 +0000 | [diff] [blame] | 351 | /* |
| 352 | * The vblank arrays are dimensioned by MAX_CRTC - we can't |
| 353 | * pass IDs greater than this to those functions. |
| 354 | */ |
Russell King | e7d6231 | 2013-11-03 15:38:09 +0000 | [diff] [blame] | 355 | if (imxdrm->pipes >= MAX_CRTC) |
| 356 | return -EINVAL; |
Russell King | fd6040e | 2013-12-16 12:39:31 +0000 | [diff] [blame] | 357 | |
Russell King | e7d6231 | 2013-11-03 15:38:09 +0000 | [diff] [blame] | 358 | if (imxdrm->drm->open_count) |
| 359 | return -EBUSY; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 360 | |
| 361 | imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL); |
Russell King | e7d6231 | 2013-11-03 15:38:09 +0000 | [diff] [blame] | 362 | if (!imx_drm_crtc) |
| 363 | return -ENOMEM; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 364 | |
| 365 | imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 366 | imx_drm_crtc->crtc = crtc; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 367 | |
Russell King | 6457b97 | 2014-07-03 17:52:57 +0100 | [diff] [blame] | 368 | crtc->port = port; |
| 369 | |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 370 | imxdrm->crtc[imxdrm->pipes++] = imx_drm_crtc; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 371 | |
| 372 | *new_crtc = imx_drm_crtc; |
| 373 | |
Russell King | ec9557d | 2013-12-17 19:11:07 +0000 | [diff] [blame] | 374 | drm_crtc_helper_add(crtc, |
| 375 | imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs); |
| 376 | |
Philipp Zabel | 4389559 | 2015-11-06 11:08:02 +0100 | [diff] [blame] | 377 | drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL, |
Ville Syrjälä | f988287 | 2015-12-09 16:19:31 +0200 | [diff] [blame] | 378 | imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs, NULL); |
Russell King | ec9557d | 2013-12-17 19:11:07 +0000 | [diff] [blame] | 379 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 380 | return 0; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL_GPL(imx_drm_add_crtc); |
| 383 | |
| 384 | /* |
| 385 | * imx_drm_remove_crtc - remove a crtc |
| 386 | */ |
| 387 | int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc) |
| 388 | { |
Russell King | ccec7f6 | 2013-11-03 15:20:18 +0000 | [diff] [blame] | 389 | struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private; |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 390 | unsigned int pipe = drm_crtc_index(imx_drm_crtc->crtc); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 391 | |
| 392 | drm_crtc_cleanup(imx_drm_crtc->crtc); |
| 393 | |
Thierry Reding | d4b20e4 | 2015-12-15 12:20:52 +0100 | [diff] [blame] | 394 | imxdrm->crtc[pipe] = NULL; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 395 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 396 | kfree(imx_drm_crtc); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | EXPORT_SYMBOL_GPL(imx_drm_remove_crtc); |
| 401 | |
Russell King | 9e2d410 | 2013-11-03 14:04:47 +0000 | [diff] [blame] | 402 | int imx_drm_encoder_parse_of(struct drm_device *drm, |
| 403 | struct drm_encoder *encoder, struct device_node *np) |
| 404 | { |
Russell King | 6457b97 | 2014-07-03 17:52:57 +0100 | [diff] [blame] | 405 | uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np); |
Russell King | 9e2d410 | 2013-11-03 14:04:47 +0000 | [diff] [blame] | 406 | |
Russell King | 6457b97 | 2014-07-03 17:52:57 +0100 | [diff] [blame] | 407 | /* |
| 408 | * If we failed to find the CRTC(s) which this encoder is |
| 409 | * supposed to be connected to, it's because the CRTC has |
| 410 | * not been registered yet. Defer probing, and hope that |
| 411 | * the required CRTC is added later. |
| 412 | */ |
| 413 | if (crtc_mask == 0) |
| 414 | return -EPROBE_DEFER; |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 415 | |
Russell King | 9e2d410 | 2013-11-03 14:04:47 +0000 | [diff] [blame] | 416 | encoder->possible_crtcs = crtc_mask; |
| 417 | |
| 418 | /* FIXME: this is the mask of outputs which can clone this output. */ |
| 419 | encoder->possible_clones = ~0; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of); |
| 424 | |
Rob Clark | baa7094 | 2013-08-02 13:27:49 -0400 | [diff] [blame] | 425 | static const struct drm_ioctl_desc imx_drm_ioctls[] = { |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 426 | /* none so far */ |
| 427 | }; |
| 428 | |
| 429 | static struct drm_driver imx_drm_driver = { |
Liu Ying | 8535c02 | 2016-07-08 17:41:02 +0800 | [diff] [blame] | 430 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | |
| 431 | DRIVER_ATOMIC, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 432 | .load = imx_drm_driver_load, |
| 433 | .unload = imx_drm_driver_unload, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 434 | .lastclose = imx_drm_driver_lastclose, |
Daniel Vetter | 4b19366 | 2016-04-26 19:29:52 +0200 | [diff] [blame] | 435 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 436 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 437 | .dumb_create = drm_gem_cma_dumb_create, |
| 438 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
Daniel Vetter | 43387b3 | 2013-07-16 09:12:04 +0200 | [diff] [blame] | 439 | .dumb_destroy = drm_gem_dumb_destroy, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 440 | |
Philipp Zabel | bd3665c | 2013-10-10 16:18:46 +0200 | [diff] [blame] | 441 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 442 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
| 443 | .gem_prime_import = drm_gem_prime_import, |
| 444 | .gem_prime_export = drm_gem_prime_export, |
| 445 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, |
| 446 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, |
| 447 | .gem_prime_vmap = drm_gem_cma_prime_vmap, |
| 448 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, |
| 449 | .gem_prime_mmap = drm_gem_cma_prime_mmap, |
Ville Syrjälä | b44f840 | 2015-09-30 16:46:48 +0300 | [diff] [blame] | 450 | .get_vblank_counter = drm_vblank_no_hw_counter, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 451 | .enable_vblank = imx_drm_enable_vblank, |
| 452 | .disable_vblank = imx_drm_disable_vblank, |
| 453 | .ioctls = imx_drm_ioctls, |
| 454 | .num_ioctls = ARRAY_SIZE(imx_drm_ioctls), |
| 455 | .fops = &imx_drm_driver_fops, |
| 456 | .name = "imx-drm", |
| 457 | .desc = "i.MX DRM graphics", |
| 458 | .date = "20120507", |
| 459 | .major = 1, |
| 460 | .minor = 0, |
| 461 | .patchlevel = 0, |
| 462 | }; |
| 463 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 464 | static int compare_of(struct device *dev, void *data) |
| 465 | { |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 466 | struct device_node *np = data; |
| 467 | |
Philipp Zabel | 310944d | 2016-05-12 15:00:44 +0200 | [diff] [blame] | 468 | /* Special case for DI, dev->of_node may not be set yet */ |
| 469 | if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) { |
| 470 | struct ipu_client_platformdata *pdata = dev->platform_data; |
| 471 | |
| 472 | return pdata->of_node == np; |
| 473 | } |
| 474 | |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 475 | /* Special case for LDB, one device for two channels */ |
| 476 | if (of_node_cmp(np->name, "lvds-channel") == 0) { |
| 477 | np = of_get_parent(np); |
| 478 | of_node_put(np); |
| 479 | } |
| 480 | |
| 481 | return dev->of_node == np; |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 484 | static int imx_drm_bind(struct device *dev) |
| 485 | { |
| 486 | return drm_platform_init(&imx_drm_driver, to_platform_device(dev)); |
| 487 | } |
| 488 | |
| 489 | static void imx_drm_unbind(struct device *dev) |
| 490 | { |
| 491 | drm_put_dev(dev_get_drvdata(dev)); |
| 492 | } |
| 493 | |
| 494 | static const struct component_master_ops imx_drm_ops = { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 495 | .bind = imx_drm_bind, |
| 496 | .unbind = imx_drm_unbind, |
| 497 | }; |
| 498 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 499 | static int imx_drm_platform_probe(struct platform_device *pdev) |
| 500 | { |
Liviu Dudau | 9cace32 | 2015-10-20 10:23:13 +0100 | [diff] [blame] | 501 | int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops); |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 502 | |
Liviu Dudau | 9cace32 | 2015-10-20 10:23:13 +0100 | [diff] [blame] | 503 | if (!ret) |
| 504 | ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 505 | |
Liviu Dudau | 9cace32 | 2015-10-20 10:23:13 +0100 | [diff] [blame] | 506 | return ret; |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static int imx_drm_platform_remove(struct platform_device *pdev) |
| 510 | { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 511 | component_master_del(&pdev->dev, &imx_drm_ops); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 515 | #ifdef CONFIG_PM_SLEEP |
| 516 | static int imx_drm_suspend(struct device *dev) |
| 517 | { |
| 518 | struct drm_device *drm_dev = dev_get_drvdata(dev); |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 519 | struct imx_drm_device *imxdrm; |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 520 | |
| 521 | /* The drm_dev is NULL before .load hook is called */ |
| 522 | if (drm_dev == NULL) |
| 523 | return 0; |
| 524 | |
| 525 | drm_kms_helper_poll_disable(drm_dev); |
| 526 | |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 527 | imxdrm = drm_dev->dev_private; |
| 528 | imxdrm->state = drm_atomic_helper_suspend(drm_dev); |
| 529 | if (IS_ERR(imxdrm->state)) { |
| 530 | drm_kms_helper_poll_enable(drm_dev); |
| 531 | return PTR_ERR(imxdrm->state); |
| 532 | } |
| 533 | |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int imx_drm_resume(struct device *dev) |
| 538 | { |
| 539 | struct drm_device *drm_dev = dev_get_drvdata(dev); |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 540 | struct imx_drm_device *imx_drm; |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 541 | |
| 542 | if (drm_dev == NULL) |
| 543 | return 0; |
| 544 | |
Liu Ying | 5f2f911 | 2016-07-08 17:40:59 +0800 | [diff] [blame] | 545 | imx_drm = drm_dev->dev_private; |
| 546 | drm_atomic_helper_resume(drm_dev, imx_drm->state); |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 547 | drm_kms_helper_poll_enable(drm_dev); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | #endif |
| 552 | |
| 553 | static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume); |
| 554 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 555 | static const struct of_device_id imx_drm_dt_ids[] = { |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 556 | { .compatible = "fsl,imx-display-subsystem", }, |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 557 | { /* sentinel */ }, |
| 558 | }; |
| 559 | MODULE_DEVICE_TABLE(of, imx_drm_dt_ids); |
| 560 | |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 561 | static struct platform_driver imx_drm_pdrv = { |
| 562 | .probe = imx_drm_platform_probe, |
Bill Pemberton | 99c28f1 | 2012-11-19 13:20:51 -0500 | [diff] [blame] | 563 | .remove = imx_drm_platform_remove, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 564 | .driver = { |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 565 | .name = "imx-drm", |
Shawn Guo | bfe945c | 2014-09-10 22:43:51 +0800 | [diff] [blame] | 566 | .pm = &imx_drm_pm_ops, |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 567 | .of_match_table = imx_drm_dt_ids, |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 568 | }, |
| 569 | }; |
Russell King | b85f2b5 | 2013-11-03 15:31:22 +0000 | [diff] [blame] | 570 | module_platform_driver(imx_drm_pdrv); |
Sascha Hauer | e692da4 | 2012-09-21 10:07:47 +0200 | [diff] [blame] | 571 | |
| 572 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 573 | MODULE_DESCRIPTION("i.MX drm driver core"); |
| 574 | MODULE_LICENSE("GPL"); |