1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2019 NXP. 4 */ 5 6 #include <drm/drm_atomic.h> 7 #include <drm/drm_atomic_helper.h> 8 #include <drm/drm_bridge_connector.h> 9 #include <drm/drm_drv.h> 10 #include <drm/drm_fb_helper.h> 11 #include <drm/drm_gem_cma_helper.h> 12 #include <drm/drm_gem_framebuffer_helper.h> 13 #include <drm/drm_of.h> 14 #include <drm/drm_probe_helper.h> 15 #include <drm/drm_vblank.h> 16 17 #include "dcss-dev.h" 18 #include "dcss-kms.h" 19 20 DEFINE_DRM_GEM_CMA_FOPS(dcss_cma_fops); 21 22 static const struct drm_mode_config_funcs dcss_drm_mode_config_funcs = { 23 .fb_create = drm_gem_fb_create, 24 .output_poll_changed = drm_fb_helper_output_poll_changed, 25 .atomic_check = drm_atomic_helper_check, 26 .atomic_commit = drm_atomic_helper_commit, 27 }; 28 29 static const struct drm_driver dcss_kms_driver = { 30 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 31 DRM_GEM_CMA_DRIVER_OPS, 32 .fops = &dcss_cma_fops, 33 .name = "imx-dcss", 34 .desc = "i.MX8MQ Display Subsystem", 35 .date = "20190917", 36 .major = 1, 37 .minor = 0, 38 .patchlevel = 0, 39 }; 40 41 static const struct drm_mode_config_helper_funcs dcss_mode_config_helpers = { 42 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, 43 }; 44 45 static void dcss_kms_mode_config_init(struct dcss_kms_dev *kms) 46 { 47 struct drm_mode_config *config = &kms->base.mode_config; 48 49 drm_mode_config_init(&kms->base); 50 51 config->min_width = 1; 52 config->min_height = 1; 53 config->max_width = 4096; 54 config->max_height = 4096; 55 config->allow_fb_modifiers = true; 56 config->normalize_zpos = true; 57 58 config->funcs = &dcss_drm_mode_config_funcs; 59 config->helper_private = &dcss_mode_config_helpers; 60 } 61 62 static const struct drm_encoder_funcs dcss_kms_simple_encoder_funcs = { 63 .destroy = drm_encoder_cleanup, 64 }; 65 66 static int dcss_kms_bridge_connector_init(struct dcss_kms_dev *kms) 67 { 68 struct drm_device *ddev = &kms->base; 69 struct drm_encoder *encoder = &kms->encoder; 70 struct drm_crtc *crtc = (struct drm_crtc *)&kms->crtc; 71 struct drm_panel *panel; 72 struct drm_bridge *bridge; 73 int ret; 74 75 ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0, 76 &panel, &bridge); 77 if (ret) 78 return ret; 79 80 if (!bridge) { 81 dev_err(ddev->dev, "No bridge found %d.\n", ret); 82 return -ENODEV; 83 } 84 85 encoder->possible_crtcs = drm_crtc_mask(crtc); 86 87 ret = drm_encoder_init(&kms->base, encoder, 88 &dcss_kms_simple_encoder_funcs, 89 DRM_MODE_ENCODER_NONE, NULL); 90 if (ret) { 91 dev_err(ddev->dev, "Failed initializing encoder %d.\n", ret); 92 return ret; 93 } 94 95 ret = drm_bridge_attach(encoder, bridge, NULL, 96 DRM_BRIDGE_ATTACH_NO_CONNECTOR); 97 if (ret < 0) { 98 dev_err(ddev->dev, "Unable to attach bridge %pOF\n", 99 bridge->of_node); 100 return ret; 101 } 102 103 kms->connector = drm_bridge_connector_init(ddev, encoder); 104 if (IS_ERR(kms->connector)) { 105 dev_err(ddev->dev, "Unable to create bridge connector.\n"); 106 return PTR_ERR(kms->connector); 107 } 108 109 drm_connector_attach_encoder(kms->connector, encoder); 110 111 return 0; 112 } 113 114 struct dcss_kms_dev *dcss_kms_attach(struct dcss_dev *dcss) 115 { 116 struct dcss_kms_dev *kms; 117 struct drm_device *drm; 118 struct dcss_crtc *crtc; 119 int ret; 120 121 kms = devm_drm_dev_alloc(dcss->dev, &dcss_kms_driver, 122 struct dcss_kms_dev, base); 123 if (IS_ERR(kms)) 124 return kms; 125 126 drm = &kms->base; 127 crtc = &kms->crtc; 128 129 drm->dev_private = dcss; 130 131 dcss_kms_mode_config_init(kms); 132 133 ret = drm_vblank_init(drm, 1); 134 if (ret) 135 goto cleanup_mode_config; 136 137 drm->irq_enabled = true; 138 139 ret = dcss_kms_bridge_connector_init(kms); 140 if (ret) 141 goto cleanup_mode_config; 142 143 ret = dcss_crtc_init(crtc, drm); 144 if (ret) 145 goto cleanup_mode_config; 146 147 drm_mode_config_reset(drm); 148 149 drm_kms_helper_poll_init(drm); 150 151 drm_bridge_connector_enable_hpd(kms->connector); 152 153 ret = drm_dev_register(drm, 0); 154 if (ret) 155 goto cleanup_crtc; 156 157 drm_fbdev_generic_setup(drm, 32); 158 159 return kms; 160 161 cleanup_crtc: 162 drm_bridge_connector_disable_hpd(kms->connector); 163 drm_kms_helper_poll_fini(drm); 164 dcss_crtc_deinit(crtc, drm); 165 166 cleanup_mode_config: 167 drm_mode_config_cleanup(drm); 168 drm->dev_private = NULL; 169 170 return ERR_PTR(ret); 171 } 172 173 void dcss_kms_detach(struct dcss_kms_dev *kms) 174 { 175 struct drm_device *drm = &kms->base; 176 177 drm_dev_unregister(drm); 178 drm_bridge_connector_disable_hpd(kms->connector); 179 drm_kms_helper_poll_fini(drm); 180 drm_atomic_helper_shutdown(drm); 181 drm_crtc_vblank_off(&kms->crtc.base); 182 drm->irq_enabled = false; 183 drm_mode_config_cleanup(drm); 184 dcss_crtc_deinit(&kms->crtc, drm); 185 drm->dev_private = NULL; 186 } 187