1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Freescale i.MX drm driver 4 * 5 * Copyright (C) 2011 Sascha Hauer, Pengutronix 6 */ 7 8 #include <linux/component.h> 9 #include <linux/device.h> 10 #include <linux/dma-buf.h> 11 #include <linux/module.h> 12 #include <linux/platform_device.h> 13 14 #include <video/imx-ipu-v3.h> 15 16 #include <drm/clients/drm_client_setup.h> 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_atomic_helper.h> 19 #include <drm/drm_drv.h> 20 #include <drm/drm_dumb_buffers.h> 21 #include <drm/drm_fbdev_dma.h> 22 #include <drm/drm_fourcc.h> 23 #include <drm/drm_gem_dma_helper.h> 24 #include <drm/drm_gem_framebuffer_helper.h> 25 #include <drm/drm_managed.h> 26 #include <drm/drm_of.h> 27 #include <drm/drm_probe_helper.h> 28 #include <drm/drm_vblank.h> 29 30 #include "imx-drm.h" 31 #include "ipuv3-plane.h" 32 33 #define MAX_CRTC 4 34 35 static int legacyfb_depth = 16; 36 module_param(legacyfb_depth, int, 0444); 37 38 DEFINE_DRM_GEM_DMA_FOPS(imx_drm_driver_fops); 39 40 static int imx_drm_atomic_check(struct drm_device *dev, 41 struct drm_atomic_state *state) 42 { 43 int ret; 44 45 ret = drm_atomic_helper_check(dev, state); 46 if (ret) 47 return ret; 48 49 /* 50 * Check modeset again in case crtc_state->mode_changed is 51 * updated in plane's ->atomic_check callback. 52 */ 53 ret = drm_atomic_helper_check_modeset(dev, state); 54 if (ret) 55 return ret; 56 57 /* Assign PRG/PRE channels and check if all constrains are satisfied. */ 58 ret = ipu_planes_assign_pre(dev, state); 59 if (ret) 60 return ret; 61 62 return ret; 63 } 64 65 static const struct drm_mode_config_funcs imx_drm_mode_config_funcs = { 66 .fb_create = drm_gem_fb_create, 67 .atomic_check = imx_drm_atomic_check, 68 .atomic_commit = drm_atomic_helper_commit, 69 }; 70 71 static void imx_drm_atomic_commit_tail(struct drm_atomic_state *state) 72 { 73 struct drm_device *dev = state->dev; 74 struct drm_plane *plane; 75 struct drm_plane_state *old_plane_state, *new_plane_state; 76 bool plane_disabling = false; 77 int i; 78 79 drm_atomic_helper_commit_modeset_disables(dev, state); 80 81 drm_atomic_helper_commit_planes(dev, state, 82 DRM_PLANE_COMMIT_ACTIVE_ONLY | 83 DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET); 84 85 drm_atomic_helper_commit_modeset_enables(dev, state); 86 87 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 88 if (drm_atomic_plane_disabling(old_plane_state, new_plane_state)) 89 plane_disabling = true; 90 } 91 92 /* 93 * The flip done wait is only strictly required by imx-drm if a deferred 94 * plane disable is in-flight. As the core requires blocking commits 95 * to wait for the flip it is done here unconditionally. This keeps the 96 * workitem around a bit longer than required for the majority of 97 * non-blocking commits, but we accept that for the sake of simplicity. 98 */ 99 drm_atomic_helper_wait_for_flip_done(dev, state); 100 101 if (plane_disabling) { 102 for_each_old_plane_in_state(state, plane, old_plane_state, i) 103 ipu_plane_disable_deferred(plane); 104 105 } 106 107 drm_atomic_helper_commit_hw_done(state); 108 } 109 110 static const struct drm_mode_config_helper_funcs imx_drm_mode_config_helpers = { 111 .atomic_commit_tail = imx_drm_atomic_commit_tail, 112 }; 113 114 115 int imx_drm_encoder_parse_of(struct drm_device *drm, 116 struct drm_encoder *encoder, struct device_node *np) 117 { 118 uint32_t crtc_mask = drm_of_find_possible_crtcs(drm, np); 119 120 /* 121 * If we failed to find the CRTC(s) which this encoder is 122 * supposed to be connected to, it's because the CRTC has 123 * not been registered yet. Defer probing, and hope that 124 * the required CRTC is added later. 125 */ 126 if (crtc_mask == 0) 127 return -EPROBE_DEFER; 128 129 encoder->possible_crtcs = crtc_mask; 130 131 /* FIXME: cloning support not clear, disable it all for now */ 132 encoder->possible_clones = 0; 133 134 return 0; 135 } 136 EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of); 137 138 static const struct drm_ioctl_desc imx_drm_ioctls[] = { 139 /* none so far */ 140 }; 141 142 static int imx_drm_dumb_create(struct drm_file *file_priv, 143 struct drm_device *drm, 144 struct drm_mode_create_dumb *args) 145 { 146 u32 fourcc; 147 u64 pitch_align; 148 int ret; 149 150 /* 151 * Hardware requires the framebuffer width to be aligned to 152 * multiples of 8. The mode-setting code handles this, but 153 * the buffer pitch has to be aligned as well. Set the pitch 154 * alignment accordingly, so that the each scanline fits into 155 * the allocated buffer. 156 */ 157 fourcc = drm_driver_color_mode_format(drm, args->bpp); 158 if (fourcc != DRM_FORMAT_INVALID) { 159 const struct drm_format_info *info = drm_format_info(fourcc); 160 161 if (!info) 162 return -EINVAL; 163 pitch_align = drm_format_info_min_pitch(info, 0, 8); 164 } else { 165 pitch_align = DIV_ROUND_UP(args->bpp, SZ_8) * 8; 166 } 167 if (!pitch_align || pitch_align > U32_MAX) 168 return -EINVAL; 169 ret = drm_mode_size_dumb(drm, args, pitch_align, 0); 170 if (ret) 171 return ret; 172 173 return drm_gem_dma_dumb_create(file_priv, drm, args); 174 } 175 176 static const struct drm_driver imx_drm_driver = { 177 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 178 DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(imx_drm_dumb_create), 179 DRM_FBDEV_DMA_DRIVER_OPS, 180 .ioctls = imx_drm_ioctls, 181 .num_ioctls = ARRAY_SIZE(imx_drm_ioctls), 182 .fops = &imx_drm_driver_fops, 183 .name = "imx-drm", 184 .desc = "i.MX DRM graphics", 185 .major = 1, 186 .minor = 0, 187 .patchlevel = 0, 188 }; 189 190 static int compare_of(struct device *dev, void *data) 191 { 192 struct device_node *np = data; 193 194 /* Special case for DI, dev->of_node may not be set yet */ 195 if (strcmp(dev->driver->name, "imx-ipuv3-crtc") == 0) { 196 struct ipu_client_platformdata *pdata = dev->platform_data; 197 198 return pdata->of_node == np; 199 } 200 201 /* Special case for LDB, one device for two channels */ 202 if (of_node_name_eq(np, "lvds-channel")) { 203 np = of_get_parent(np); 204 of_node_put(np); 205 } 206 207 return dev->of_node == np; 208 } 209 210 static int imx_drm_bind(struct device *dev) 211 { 212 struct drm_device *drm; 213 int ret; 214 215 drm = drm_dev_alloc(&imx_drm_driver, dev); 216 if (IS_ERR(drm)) 217 return PTR_ERR(drm); 218 219 /* 220 * set max width and height as default value(4096x4096). 221 * this value would be used to check framebuffer size limitation 222 * at drm_mode_addfb(). 223 */ 224 drm->mode_config.min_width = 1; 225 drm->mode_config.min_height = 1; 226 drm->mode_config.max_width = 4096; 227 drm->mode_config.max_height = 4096; 228 drm->mode_config.funcs = &imx_drm_mode_config_funcs; 229 drm->mode_config.helper_private = &imx_drm_mode_config_helpers; 230 drm->mode_config.normalize_zpos = true; 231 232 ret = drmm_mode_config_init(drm); 233 if (ret) 234 goto err_kms; 235 236 ret = drm_vblank_init(drm, MAX_CRTC); 237 if (ret) 238 goto err_kms; 239 240 dev_set_drvdata(dev, drm); 241 242 /* Now try and bind all our sub-components */ 243 ret = component_bind_all(dev, drm); 244 if (ret) 245 goto err_kms; 246 247 drm_mode_config_reset(drm); 248 249 /* 250 * All components are now initialised, so setup the fb helper. 251 * The fb helper takes copies of key hardware information, so the 252 * crtcs/connectors/encoders must not change after this point. 253 */ 254 if (legacyfb_depth != 16 && legacyfb_depth != 32) { 255 dev_warn(dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n"); 256 legacyfb_depth = 16; 257 } 258 259 drm_kms_helper_poll_init(drm); 260 261 ret = drm_dev_register(drm, 0); 262 if (ret) 263 goto err_poll_fini; 264 265 drm_client_setup_with_color_mode(drm, legacyfb_depth); 266 267 return 0; 268 269 err_poll_fini: 270 drm_kms_helper_poll_fini(drm); 271 component_unbind_all(drm->dev, drm); 272 err_kms: 273 dev_set_drvdata(dev, NULL); 274 drm_dev_put(drm); 275 276 return ret; 277 } 278 279 static void imx_drm_unbind(struct device *dev) 280 { 281 struct drm_device *drm = dev_get_drvdata(dev); 282 283 drm_dev_unregister(drm); 284 285 drm_kms_helper_poll_fini(drm); 286 drm_atomic_helper_shutdown(drm); 287 288 component_unbind_all(drm->dev, drm); 289 290 drm_dev_put(drm); 291 292 dev_set_drvdata(dev, NULL); 293 } 294 295 static const struct component_master_ops imx_drm_ops = { 296 .bind = imx_drm_bind, 297 .unbind = imx_drm_unbind, 298 }; 299 300 static int imx_drm_platform_probe(struct platform_device *pdev) 301 { 302 int ret = drm_of_component_probe(&pdev->dev, compare_of, &imx_drm_ops); 303 304 if (!ret) 305 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); 306 307 return ret; 308 } 309 310 static void imx_drm_platform_remove(struct platform_device *pdev) 311 { 312 component_master_del(&pdev->dev, &imx_drm_ops); 313 } 314 315 static void imx_drm_platform_shutdown(struct platform_device *pdev) 316 { 317 drm_atomic_helper_shutdown(platform_get_drvdata(pdev)); 318 } 319 320 #ifdef CONFIG_PM_SLEEP 321 static int imx_drm_suspend(struct device *dev) 322 { 323 struct drm_device *drm_dev = dev_get_drvdata(dev); 324 325 return drm_mode_config_helper_suspend(drm_dev); 326 } 327 328 static int imx_drm_resume(struct device *dev) 329 { 330 struct drm_device *drm_dev = dev_get_drvdata(dev); 331 332 return drm_mode_config_helper_resume(drm_dev); 333 } 334 #endif 335 336 static SIMPLE_DEV_PM_OPS(imx_drm_pm_ops, imx_drm_suspend, imx_drm_resume); 337 338 static const struct of_device_id imx_drm_dt_ids[] = { 339 { .compatible = "fsl,imx-display-subsystem", }, 340 { /* sentinel */ }, 341 }; 342 MODULE_DEVICE_TABLE(of, imx_drm_dt_ids); 343 344 static struct platform_driver imx_drm_pdrv = { 345 .probe = imx_drm_platform_probe, 346 .remove = imx_drm_platform_remove, 347 .shutdown = imx_drm_platform_shutdown, 348 .driver = { 349 .name = "imx-drm", 350 .pm = &imx_drm_pm_ops, 351 .of_match_table = imx_drm_dt_ids, 352 }, 353 }; 354 355 static struct platform_driver * const drivers[] = { 356 &imx_drm_pdrv, 357 &ipu_drm_driver, 358 }; 359 360 static int __init imx_drm_init(void) 361 { 362 if (drm_firmware_drivers_only()) 363 return -ENODEV; 364 365 return platform_register_drivers(drivers, ARRAY_SIZE(drivers)); 366 } 367 module_init(imx_drm_init); 368 369 static void __exit imx_drm_exit(void) 370 { 371 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers)); 372 } 373 module_exit(imx_drm_exit); 374 375 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); 376 MODULE_DESCRIPTION("i.MX drm driver core"); 377 MODULE_LICENSE("GPL"); 378