1 /* 2 * Copyright (C) 2016 Noralf Trønnes 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 */ 9 10 #include <drm/drmP.h> 11 #include <drm/drm_atomic.h> 12 #include <drm/drm_atomic_helper.h> 13 #include <drm/drm_crtc_helper.h> 14 #include <drm/drm_plane_helper.h> 15 #include <drm/drm_simple_kms_helper.h> 16 #include <linux/slab.h> 17 18 /** 19 * DOC: overview 20 * 21 * This helper library provides helpers for drivers for simple display 22 * hardware. 23 * 24 * drm_simple_display_pipe_init() initializes a simple display pipeline 25 * which has only one full-screen scanout buffer feeding one output. The 26 * pipeline is represented by struct &drm_simple_display_pipe and binds 27 * together &drm_plane, &drm_crtc and &drm_encoder structures into one fixed 28 * entity. Some flexibility for code reuse is provided through a separately 29 * allocated &drm_connector object and supporting optional &drm_bridge 30 * encoder drivers. 31 */ 32 33 static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = { 34 .destroy = drm_encoder_cleanup, 35 }; 36 37 static void drm_simple_kms_crtc_enable(struct drm_crtc *crtc) 38 { 39 struct drm_simple_display_pipe *pipe; 40 41 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 42 if (!pipe->funcs || !pipe->funcs->enable) 43 return; 44 45 pipe->funcs->enable(pipe, crtc->state); 46 } 47 48 static void drm_simple_kms_crtc_disable(struct drm_crtc *crtc) 49 { 50 struct drm_simple_display_pipe *pipe; 51 52 pipe = container_of(crtc, struct drm_simple_display_pipe, crtc); 53 if (!pipe->funcs || !pipe->funcs->disable) 54 return; 55 56 pipe->funcs->disable(pipe); 57 } 58 59 static const struct drm_crtc_helper_funcs drm_simple_kms_crtc_helper_funcs = { 60 .disable = drm_simple_kms_crtc_disable, 61 .enable = drm_simple_kms_crtc_enable, 62 }; 63 64 static const struct drm_crtc_funcs drm_simple_kms_crtc_funcs = { 65 .reset = drm_atomic_helper_crtc_reset, 66 .destroy = drm_crtc_cleanup, 67 .set_config = drm_atomic_helper_set_config, 68 .page_flip = drm_atomic_helper_page_flip, 69 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 70 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 71 }; 72 73 static int drm_simple_kms_plane_atomic_check(struct drm_plane *plane, 74 struct drm_plane_state *plane_state) 75 { 76 struct drm_rect src = { 77 .x1 = plane_state->src_x, 78 .y1 = plane_state->src_y, 79 .x2 = plane_state->src_x + plane_state->src_w, 80 .y2 = plane_state->src_y + plane_state->src_h, 81 }; 82 struct drm_rect dest = { 83 .x1 = plane_state->crtc_x, 84 .y1 = plane_state->crtc_y, 85 .x2 = plane_state->crtc_x + plane_state->crtc_w, 86 .y2 = plane_state->crtc_y + plane_state->crtc_h, 87 }; 88 struct drm_rect clip = { 0 }; 89 struct drm_simple_display_pipe *pipe; 90 struct drm_crtc_state *crtc_state; 91 bool visible; 92 int ret; 93 94 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 95 crtc_state = drm_atomic_get_existing_crtc_state(plane_state->state, 96 &pipe->crtc); 97 if (crtc_state->enable != !!plane_state->crtc) 98 return -EINVAL; /* plane must match crtc enable state */ 99 100 if (!crtc_state->enable) 101 return 0; /* nothing to check when disabling or disabled */ 102 103 clip.x2 = crtc_state->adjusted_mode.hdisplay; 104 clip.y2 = crtc_state->adjusted_mode.vdisplay; 105 ret = drm_plane_helper_check_update(plane, &pipe->crtc, 106 plane_state->fb, 107 &src, &dest, &clip, 108 plane_state->rotation, 109 DRM_PLANE_HELPER_NO_SCALING, 110 DRM_PLANE_HELPER_NO_SCALING, 111 false, true, &visible); 112 if (ret) 113 return ret; 114 115 if (!visible) 116 return -EINVAL; 117 118 if (!pipe->funcs || !pipe->funcs->check) 119 return 0; 120 121 return pipe->funcs->check(pipe, plane_state, crtc_state); 122 } 123 124 static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane, 125 struct drm_plane_state *pstate) 126 { 127 struct drm_simple_display_pipe *pipe; 128 129 pipe = container_of(plane, struct drm_simple_display_pipe, plane); 130 if (!pipe->funcs || !pipe->funcs->update) 131 return; 132 133 pipe->funcs->update(pipe, pstate); 134 } 135 136 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = { 137 .atomic_check = drm_simple_kms_plane_atomic_check, 138 .atomic_update = drm_simple_kms_plane_atomic_update, 139 }; 140 141 static const struct drm_plane_funcs drm_simple_kms_plane_funcs = { 142 .update_plane = drm_atomic_helper_update_plane, 143 .disable_plane = drm_atomic_helper_disable_plane, 144 .destroy = drm_plane_cleanup, 145 .reset = drm_atomic_helper_plane_reset, 146 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 147 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 148 }; 149 150 /** 151 * drm_simple_display_pipe_init - Initialize a simple display pipeline 152 * @dev: DRM device 153 * @pipe: simple display pipe object to initialize 154 * @funcs: callbacks for the display pipe (optional) 155 * @formats: array of supported formats (%DRM_FORMAT_*) 156 * @format_count: number of elements in @formats 157 * @connector: connector to attach and register 158 * 159 * Sets up a display pipeline which consist of a really simple 160 * plane-crtc-encoder pipe coupled with the provided connector. 161 * Teardown of a simple display pipe is all handled automatically by the drm 162 * core through calling drm_mode_config_cleanup(). Drivers afterwards need to 163 * release the memory for the structure themselves. 164 * 165 * Returns: 166 * Zero on success, negative error code on failure. 167 */ 168 int drm_simple_display_pipe_init(struct drm_device *dev, 169 struct drm_simple_display_pipe *pipe, 170 const struct drm_simple_display_pipe_funcs *funcs, 171 const uint32_t *formats, unsigned int format_count, 172 struct drm_connector *connector) 173 { 174 struct drm_encoder *encoder = &pipe->encoder; 175 struct drm_plane *plane = &pipe->plane; 176 struct drm_crtc *crtc = &pipe->crtc; 177 int ret; 178 179 pipe->connector = connector; 180 pipe->funcs = funcs; 181 182 drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs); 183 ret = drm_universal_plane_init(dev, plane, 0, 184 &drm_simple_kms_plane_funcs, 185 formats, format_count, 186 DRM_PLANE_TYPE_PRIMARY, NULL); 187 if (ret) 188 return ret; 189 190 drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs); 191 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL, 192 &drm_simple_kms_crtc_funcs, NULL); 193 if (ret) 194 return ret; 195 196 encoder->possible_crtcs = 1 << drm_crtc_index(crtc); 197 ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs, 198 DRM_MODE_ENCODER_NONE, NULL); 199 if (ret) 200 return ret; 201 202 return drm_mode_connector_attach_encoder(connector, encoder); 203 } 204 EXPORT_SYMBOL(drm_simple_display_pipe_init); 205 206 MODULE_LICENSE("GPL"); 207