1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Noralf Trønnes 4 */ 5 6 #include <linux/export.h> 7 #include <linux/module.h> 8 #include <linux/slab.h> 9 10 #include <drm/drm_atomic.h> 11 #include <drm/drm_atomic_helper.h> 12 #include <drm/drm_bridge.h> 13 #include <drm/drm_drv.h> 14 #include <drm/drm_gem_atomic_helper.h> 15 #include <drm/drm_managed.h> 16 #include <drm/drm_probe_helper.h> 17 #include <drm/drm_simple_kms_helper.h> 18 19 static const struct drm_encoder_funcs drm_simple_encoder_funcs_cleanup = { 20 .destroy = drm_encoder_cleanup, 21 }; 22 23 void *__drmm_simple_encoder_alloc(struct drm_device *dev, size_t size, 24 size_t offset, int encoder_type) 25 { 26 return __drmm_encoder_alloc(dev, size, offset, NULL, encoder_type, 27 NULL); 28 } 29 EXPORT_SYMBOL(__drmm_simple_encoder_alloc); 30 31 static enum drm_mode_status 32 drm_simple_kms_crtc_mode_valid(struct drm_crtc *crtc, 33 const struct drm_display_mode *mode) 34 { 35 struct drm_simple_display_pipe *pipe; 36 37 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 38 if (!pipe->funcs || !pipe->funcs->mode_valid) 39 /* Anything goes */ 40 return MODE_OK; 41 42 return pipe->funcs->mode_valid(pipe, mode); 43 } 44 45 static int drm_simple_kms_crtc_check(struct drm_crtc *crtc, 46 struct drm_atomic_commit *state) 47 { 48 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 49 int ret; 50 51 if (!crtc_state->enable) 52 goto out; 53 54 ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state); 55 if (ret) 56 return ret; 57 58 out: 59 return drm_atomic_add_affected_planes(state, crtc); 60 } 61 62 static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc, 63 struct drm_atomic_commit *state) 64 { 65 struct drm_plane *plane; 66 struct drm_simple_display_pipe *pipe; 67 68 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 69 if (!pipe->funcs || !pipe->funcs->enable) 70 return; 71 72 plane = &pipe->plane; 73 pipe->funcs->enable(pipe, crtc->state, plane->state); 74 } 75 76 static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc, 77 struct drm_atomic_commit *state) 78 { 79 struct drm_simple_display_pipe *pipe; 80 81 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 82 if (!pipe->funcs || !pipe->funcs->disable) 83 return; 84 85 pipe->funcs->disable(pipe); 86 } 87 88 static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = { 89 .mode_valid = drm_simple_kms_crtc_mode_valid, 90 .atomic_check = drm_simple_kms_crtc_check, 91 .atomic_enable = drm_simple_kms_crtc_enable, 92 .atomic_disable = drm_simple_kms_crtc_disable, 93 }; 94 95 static struct drm_crtc_state *drm_simple_kms_crtc_duplicate_state(struct drm_crtc *crtc) 96 { 97 struct drm_simple_display_pipe *pipe; 98 99 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 100 if (!pipe->funcs || !pipe->funcs->duplicate_crtc_state) 101 return drm_atomic_helper_crtc_duplicate_state(crtc); 102 103 return pipe->funcs->duplicate_crtc_state(pipe); 104 } 105 106 static void drm_simple_kms_crtc_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *state) 107 { 108 struct drm_simple_display_pipe *pipe; 109 110 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 111 if (!pipe->funcs || !pipe->funcs->destroy_crtc_state) 112 drm_atomic_helper_crtc_destroy_state(crtc, state); 113 else 114 pipe->funcs->destroy_crtc_state(pipe, state); 115 } 116 117 static int drm_simple_kms_crtc_enable_vblank(struct drm_crtc *crtc) 118 { 119 struct drm_simple_display_pipe *pipe; 120 121 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 122 if (!pipe->funcs || !pipe->funcs->enable_vblank) 123 return 0; 124 125 return pipe->funcs->enable_vblank(pipe); 126 } 127 128 static void drm_simple_kms_crtc_disable_vblank(struct drm_crtc *crtc) 129 { 130 struct drm_simple_display_pipe *pipe; 131 132 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 133 if (!pipe->funcs || !pipe->funcs->disable_vblank) 134 return; 135 136 pipe->funcs->disable_vblank(pipe); 137 } 138 139 static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = { 140 .atomic_create_state = drm_atomic_helper_crtc_create_state, 141 .destroy = drm_crtc_cleanup, 142 .set_config = drm_atomic_helper_set_config, 143 .page_flip = drm_atomic_helper_page_flip, 144 .atomic_duplicate_state = drm_simple_kms_crtc_duplicate_state, 145 .atomic_destroy_state = drm_simple_kms_crtc_destroy_state, 146 .enable_vblank = drm_simple_kms_crtc_enable_vblank, 147 .disable_vblank = drm_simple_kms_crtc_disable_vblank, 148 }; 149 150 static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane, 151 struct drm_atomic_commit *state) 152 { 153 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, 154 plane); 155 struct drm_simple_display_pipe *pipe; 156 struct drm_crtc_state *crtc_state; 157 int ret; 158 159 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 160 crtc_state = drm_atomic_get_new_crtc_state(state, 161 &pipe->crtc); 162 163 ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state, 164 DRM_PLANE_NO_SCALING, 165 DRM_PLANE_NO_SCALING, 166 false, false); 167 if (ret) 168 return ret; 169 170 if (!plane_state->visible) 171 return 0; 172 173 if (!pipe->funcs || !pipe->funcs->check) 174 return 0; 175 176 return pipe->funcs->check(pipe, plane_state, crtc_state); 177 } 178 179 static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane, 180 struct drm_atomic_commit *state) 181 { 182 struct drm_plane_state *old_pstate = drm_atomic_get_old_plane_state(state, 183 plane); 184 struct drm_simple_display_pipe *pipe; 185 186 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 187 if (!pipe->funcs || !pipe->funcs->update) 188 return; 189 190 pipe->funcs->update(pipe, old_pstate); 191 } 192 193 static int drm_simple_kms_plane_prepare_fb(struct drm_plane *plane, 194 struct drm_plane_state *state) 195 { 196 struct drm_simple_display_pipe *pipe; 197 198 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 199 if (!pipe->funcs || !pipe->funcs->prepare_fb) { 200 if (WARN_ON_ONCE(!drm_core_check_feature(plane->dev, DRIVER_GEM))) 201 return 0; 202 203 WARN_ON_ONCE(pipe->funcs && pipe->funcs->cleanup_fb); 204 205 return drm_gem_plane_helper_prepare_fb(plane, state); 206 } 207 208 return pipe->funcs->prepare_fb(pipe, state); 209 } 210 211 static void drm_simple_kms_plane_cleanup_fb(struct drm_plane *plane, 212 struct drm_plane_state *state) 213 { 214 struct drm_simple_display_pipe *pipe; 215 216 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 217 if (!pipe->funcs || !pipe->funcs->cleanup_fb) 218 return; 219 220 pipe->funcs->cleanup_fb(pipe, state); 221 } 222 223 static int drm_simple_kms_plane_begin_fb_access(struct drm_plane *plane, 224 struct drm_plane_state *new_plane_state) 225 { 226 struct drm_simple_display_pipe *pipe; 227 228 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 229 if (!pipe->funcs || !pipe->funcs->begin_fb_access) 230 return 0; 231 232 return pipe->funcs->begin_fb_access(pipe, new_plane_state); 233 } 234 235 static void drm_simple_kms_plane_end_fb_access(struct drm_plane *plane, 236 struct drm_plane_state *new_plane_state) 237 { 238 struct drm_simple_display_pipe *pipe; 239 240 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 241 if (!pipe->funcs || !pipe->funcs->end_fb_access) 242 return; 243 244 pipe->funcs->end_fb_access(pipe, new_plane_state); 245 } 246 247 static bool drm_simple_kms_format_mod_supported(struct drm_plane *plane, 248 uint32_t format, 249 uint64_t modifier) 250 { 251 return modifier == DRM_FORMAT_MOD_LINEAR; 252 } 253 254 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = { 255 .prepare_fb = drm_simple_kms_plane_prepare_fb, 256 .cleanup_fb = drm_simple_kms_plane_cleanup_fb, 257 .begin_fb_access = drm_simple_kms_plane_begin_fb_access, 258 .end_fb_access = drm_simple_kms_plane_end_fb_access, 259 .atomic_check = drm_simple_kms_plane_atomic_check, 260 .atomic_update = drm_simple_kms_plane_atomic_update, 261 }; 262 263 static struct drm_plane_state * 264 drm_simple_kms_plane_create_state(struct drm_plane *plane) 265 { 266 struct drm_simple_display_pipe *pipe; 267 268 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 269 if (!pipe->funcs || !pipe->funcs->create_plane_state) 270 return drm_atomic_helper_plane_create_state(plane); 271 272 return pipe->funcs->create_plane_state(pipe); 273 } 274 275 static struct drm_plane_state *drm_simple_kms_plane_duplicate_state(struct drm_plane *plane) 276 { 277 struct drm_simple_display_pipe *pipe; 278 279 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 280 if (!pipe->funcs || !pipe->funcs->duplicate_plane_state) 281 return drm_atomic_helper_plane_duplicate_state(plane); 282 283 return pipe->funcs->duplicate_plane_state(pipe); 284 } 285 286 static void drm_simple_kms_plane_destroy_state(struct drm_plane *plane, 287 struct drm_plane_state *state) 288 { 289 struct drm_simple_display_pipe *pipe; 290 291 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 292 if (!pipe->funcs || !pipe->funcs->destroy_plane_state) 293 drm_atomic_helper_plane_destroy_state(plane, state); 294 else 295 pipe->funcs->destroy_plane_state(pipe, state); 296 } 297 298 static const struct drm_plane_funcs drm_simple_kms_plane_funcs = { 299 .update_plane = drm_atomic_helper_update_plane, 300 .disable_plane = drm_atomic_helper_disable_plane, 301 .destroy = drm_plane_cleanup, 302 .atomic_create_state = drm_simple_kms_plane_create_state, 303 .atomic_duplicate_state = drm_simple_kms_plane_duplicate_state, 304 .atomic_destroy_state = drm_simple_kms_plane_destroy_state, 305 .format_mod_supported = drm_simple_kms_format_mod_supported, 306 }; 307 308 int drm_simple_display_pipe_attach_bridge(struct drm_simple_display_pipe *pipe, 309 struct drm_bridge *bridge) 310 { 311 return drm_bridge_attach(&pipe->encoder, bridge, NULL, 0); 312 } 313 EXPORT_SYMBOL(drm_simple_display_pipe_attach_bridge); 314 315 int drm_simple_display_pipe_init(struct drm_device *dev, 316 struct drm_simple_display_pipe *pipe, 317 const struct drm_simple_display_pipe_funcs *funcs, 318 const uint32_t *formats, unsigned int format_count, 319 const uint64_t *format_modifiers, 320 struct drm_connector *connector) 321 { 322 struct drm_encoder *encoder = &pipe->encoder; 323 struct drm_plane *plane = &pipe->plane; 324 struct drm_crtc *crtc = &pipe->crtc; 325 int ret; 326 327 pipe->connector = connector; 328 pipe->funcs = funcs; 329 330 drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs); 331 ret = drm_universal_plane_init(dev, plane, 0, 332 &drm_simple_kms_plane_funcs, 333 formats, format_count, 334 format_modifiers, 335 DRM_PLANE_TYPE_PRIMARY, NULL); 336 if (ret) 337 return ret; 338 339 drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs); 340 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL, 341 &drm_simple_kms_crtc_funcs, NULL); 342 if (ret) 343 return ret; 344 345 encoder->possible_crtcs = drm_crtc_mask(crtc); 346 ret = drm_encoder_init(dev, encoder, &drm_simple_encoder_funcs_cleanup, 347 DRM_MODE_ENCODER_NONE, NULL); 348 if (ret || !connector) 349 return ret; 350 351 return drm_connector_attach_encoder(connector, encoder); 352 } 353 EXPORT_SYMBOL(drm_simple_display_pipe_init); 354 355 MODULE_DESCRIPTION("Helpers for drivers for simple display hardware"); 356 MODULE_LICENSE("GPL"); 357