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->normalize_zpos = true; 56 57 config->funcs = &dcss_drm_mode_config_funcs; 58 config->helper_private = &dcss_mode_config_helpers; 59 } 60 61 static const struct drm_encoder_funcs dcss_kms_simple_encoder_funcs = { 62 .destroy = drm_encoder_cleanup, 63 }; 64 65 static int dcss_kms_bridge_connector_init(struct dcss_kms_dev *kms) 66 { 67 struct drm_device *ddev = &kms->base; 68 struct drm_encoder *encoder = &kms->encoder; 69 struct drm_crtc *crtc = (struct drm_crtc *)&kms->crtc; 70 struct drm_panel *panel; 71 struct drm_bridge *bridge; 72 int ret; 73 74 ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0, 75 &panel, &bridge); 76 if (ret) 77 return ret; 78 79 if (!bridge) { 80 dev_err(ddev->dev, "No bridge found %d.\n", ret); 81 return -ENODEV; 82 } 83 84 encoder->possible_crtcs = drm_crtc_mask(crtc); 85 86 ret = drm_encoder_init(&kms->base, encoder, 87 &dcss_kms_simple_encoder_funcs, 88 DRM_MODE_ENCODER_NONE, NULL); 89 if (ret) { 90 dev_err(ddev->dev, "Failed initializing encoder %d.\n", ret); 91 return ret; 92 } 93 94 ret = drm_bridge_attach(encoder, bridge, NULL, 95 DRM_BRIDGE_ATTACH_NO_CONNECTOR); 96 if (ret < 0) { 97 dev_err(ddev->dev, "Unable to attach bridge %pOF\n", 98 bridge->of_node); 99 return ret; 100 } 101 102 kms->connector = drm_bridge_connector_init(ddev, encoder); 103 if (IS_ERR(kms->connector)) { 104 dev_err(ddev->dev, "Unable to create bridge connector.\n"); 105 return PTR_ERR(kms->connector); 106 } 107 108 drm_connector_attach_encoder(kms->connector, encoder); 109 110 return 0; 111 } 112 113 struct dcss_kms_dev *dcss_kms_attach(struct dcss_dev *dcss) 114 { 115 struct dcss_kms_dev *kms; 116 struct drm_device *drm; 117 struct dcss_crtc *crtc; 118 int ret; 119 120 kms = devm_drm_dev_alloc(dcss->dev, &dcss_kms_driver, 121 struct dcss_kms_dev, base); 122 if (IS_ERR(kms)) 123 return kms; 124 125 drm = &kms->base; 126 crtc = &kms->crtc; 127 128 drm->dev_private = dcss; 129 130 dcss_kms_mode_config_init(kms); 131 132 ret = drm_vblank_init(drm, 1); 133 if (ret) 134 goto cleanup_mode_config; 135 136 drm->irq_enabled = true; 137 138 ret = dcss_kms_bridge_connector_init(kms); 139 if (ret) 140 goto cleanup_mode_config; 141 142 ret = dcss_crtc_init(crtc, drm); 143 if (ret) 144 goto cleanup_mode_config; 145 146 drm_mode_config_reset(drm); 147 148 drm_kms_helper_poll_init(drm); 149 150 drm_bridge_connector_enable_hpd(kms->connector); 151 152 ret = drm_dev_register(drm, 0); 153 if (ret) 154 goto cleanup_crtc; 155 156 drm_fbdev_generic_setup(drm, 32); 157 158 return kms; 159 160 cleanup_crtc: 161 drm_bridge_connector_disable_hpd(kms->connector); 162 drm_kms_helper_poll_fini(drm); 163 dcss_crtc_deinit(crtc, drm); 164 165 cleanup_mode_config: 166 drm_mode_config_cleanup(drm); 167 drm->dev_private = NULL; 168 169 return ERR_PTR(ret); 170 } 171 172 void dcss_kms_detach(struct dcss_kms_dev *kms) 173 { 174 struct drm_device *drm = &kms->base; 175 176 drm_dev_unregister(drm); 177 drm_bridge_connector_disable_hpd(kms->connector); 178 drm_kms_helper_poll_fini(drm); 179 drm_atomic_helper_shutdown(drm); 180 drm_crtc_vblank_off(&kms->crtc.base); 181 drm->irq_enabled = false; 182 drm_mode_config_cleanup(drm); 183 dcss_crtc_deinit(&kms->crtc, drm); 184 drm->dev_private = NULL; 185 } 186