blob: b2071d495ada8661be30e799387e64c62a68ab7a [file] [log] [blame]
Noralf Trønnes5b809072016-06-10 16:55:59 +02001/*
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
33static const struct drm_encoder_funcs drm_simple_kms_encoder_funcs = {
34 .destroy = drm_encoder_cleanup,
35};
36
37static 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
48static 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
59static 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
64static 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
73static 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 DRM_PLANE_HELPER_NO_SCALING,
109 DRM_PLANE_HELPER_NO_SCALING,
110 false, true, &visible);
111 if (ret)
112 return ret;
113
114 if (!visible)
115 return -EINVAL;
116
117 if (!pipe->funcs || !pipe->funcs->check)
118 return 0;
119
120 return pipe->funcs->check(pipe, plane_state, crtc_state);
121}
122
123static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
124 struct drm_plane_state *pstate)
125{
126 struct drm_simple_display_pipe *pipe;
127
128 pipe = container_of(plane, struct drm_simple_display_pipe, plane);
129 if (!pipe->funcs || !pipe->funcs->update)
130 return;
131
132 pipe->funcs->update(pipe, pstate);
133}
134
135static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
136 .atomic_check = drm_simple_kms_plane_atomic_check,
137 .atomic_update = drm_simple_kms_plane_atomic_update,
138};
139
140static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
141 .update_plane = drm_atomic_helper_update_plane,
142 .disable_plane = drm_atomic_helper_disable_plane,
143 .destroy = drm_plane_cleanup,
144 .reset = drm_atomic_helper_plane_reset,
145 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
146 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
147};
148
149/**
150 * drm_simple_display_pipe_init - Initialize a simple display pipeline
151 * @dev: DRM device
152 * @pipe: simple display pipe object to initialize
153 * @funcs: callbacks for the display pipe (optional)
154 * @formats: array of supported formats (%DRM_FORMAT_*)
155 * @format_count: number of elements in @formats
156 * @connector: connector to attach and register
157 *
158 * Sets up a display pipeline which consist of a really simple
159 * plane-crtc-encoder pipe coupled with the provided connector.
160 * Teardown of a simple display pipe is all handled automatically by the drm
161 * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
162 * release the memory for the structure themselves.
163 *
164 * Returns:
165 * Zero on success, negative error code on failure.
166 */
167int drm_simple_display_pipe_init(struct drm_device *dev,
168 struct drm_simple_display_pipe *pipe,
169 const struct drm_simple_display_pipe_funcs *funcs,
170 const uint32_t *formats, unsigned int format_count,
171 struct drm_connector *connector)
172{
173 struct drm_encoder *encoder = &pipe->encoder;
174 struct drm_plane *plane = &pipe->plane;
175 struct drm_crtc *crtc = &pipe->crtc;
176 int ret;
177
178 pipe->connector = connector;
179 pipe->funcs = funcs;
180
181 drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
182 ret = drm_universal_plane_init(dev, plane, 0,
183 &drm_simple_kms_plane_funcs,
184 formats, format_count,
185 DRM_PLANE_TYPE_PRIMARY, NULL);
186 if (ret)
187 return ret;
188
189 drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
190 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
191 &drm_simple_kms_crtc_funcs, NULL);
192 if (ret)
193 return ret;
194
195 encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
196 ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
197 DRM_MODE_ENCODER_NONE, NULL);
198 if (ret)
199 return ret;
200
201 return drm_mode_connector_attach_encoder(connector, encoder);
202}
203EXPORT_SYMBOL(drm_simple_display_pipe_init);
204
205MODULE_LICENSE("GPL");