Merge branch 'for-next' of git://people.freedesktop.org/~seanpaul/dogwood into drm...
[linux-2.6-block.git] / drivers / gpu / drm / drm_simple_kms_helper.c
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 clip = { 0 };
77         struct drm_simple_display_pipe *pipe;
78         struct drm_crtc_state *crtc_state;
79         int ret;
80
81         pipe = container_of(plane, struct drm_simple_display_pipe, plane);
82         crtc_state = drm_atomic_get_existing_crtc_state(plane_state->state,
83                                                         &pipe->crtc);
84         if (crtc_state->enable != !!plane_state->crtc)
85                 return -EINVAL; /* plane must match crtc enable state */
86
87         if (!crtc_state->enable)
88                 return 0; /* nothing to check when disabling or disabled */
89
90         clip.x2 = crtc_state->adjusted_mode.hdisplay;
91         clip.y2 = crtc_state->adjusted_mode.vdisplay;
92
93         ret = drm_plane_helper_check_state(plane_state, &clip,
94                                            DRM_PLANE_HELPER_NO_SCALING,
95                                            DRM_PLANE_HELPER_NO_SCALING,
96                                            false, true);
97         if (ret)
98                 return ret;
99
100         if (!plane_state->visible)
101                 return -EINVAL;
102
103         if (!pipe->funcs || !pipe->funcs->check)
104                 return 0;
105
106         return pipe->funcs->check(pipe, plane_state, crtc_state);
107 }
108
109 static void drm_simple_kms_plane_atomic_update(struct drm_plane *plane,
110                                         struct drm_plane_state *pstate)
111 {
112         struct drm_simple_display_pipe *pipe;
113
114         pipe = container_of(plane, struct drm_simple_display_pipe, plane);
115         if (!pipe->funcs || !pipe->funcs->update)
116                 return;
117
118         pipe->funcs->update(pipe, pstate);
119 }
120
121 static const struct drm_plane_helper_funcs drm_simple_kms_plane_helper_funcs = {
122         .atomic_check = drm_simple_kms_plane_atomic_check,
123         .atomic_update = drm_simple_kms_plane_atomic_update,
124 };
125
126 static const struct drm_plane_funcs drm_simple_kms_plane_funcs = {
127         .update_plane           = drm_atomic_helper_update_plane,
128         .disable_plane          = drm_atomic_helper_disable_plane,
129         .destroy                = drm_plane_cleanup,
130         .reset                  = drm_atomic_helper_plane_reset,
131         .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
132         .atomic_destroy_state   = drm_atomic_helper_plane_destroy_state,
133 };
134
135 /**
136  * drm_simple_display_pipe_init - Initialize a simple display pipeline
137  * @dev: DRM device
138  * @pipe: simple display pipe object to initialize
139  * @funcs: callbacks for the display pipe (optional)
140  * @formats: array of supported formats (DRM_FORMAT\_\*)
141  * @format_count: number of elements in @formats
142  * @connector: connector to attach and register
143  *
144  * Sets up a display pipeline which consist of a really simple
145  * plane-crtc-encoder pipe coupled with the provided connector.
146  * Teardown of a simple display pipe is all handled automatically by the drm
147  * core through calling drm_mode_config_cleanup(). Drivers afterwards need to
148  * release the memory for the structure themselves.
149  *
150  * Returns:
151  * Zero on success, negative error code on failure.
152  */
153 int drm_simple_display_pipe_init(struct drm_device *dev,
154                         struct drm_simple_display_pipe *pipe,
155                         const struct drm_simple_display_pipe_funcs *funcs,
156                         const uint32_t *formats, unsigned int format_count,
157                         struct drm_connector *connector)
158 {
159         struct drm_encoder *encoder = &pipe->encoder;
160         struct drm_plane *plane = &pipe->plane;
161         struct drm_crtc *crtc = &pipe->crtc;
162         int ret;
163
164         pipe->connector = connector;
165         pipe->funcs = funcs;
166
167         drm_plane_helper_add(plane, &drm_simple_kms_plane_helper_funcs);
168         ret = drm_universal_plane_init(dev, plane, 0,
169                                        &drm_simple_kms_plane_funcs,
170                                        formats, format_count,
171                                        DRM_PLANE_TYPE_PRIMARY, NULL);
172         if (ret)
173                 return ret;
174
175         drm_crtc_helper_add(crtc, &drm_simple_kms_crtc_helper_funcs);
176         ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
177                                         &drm_simple_kms_crtc_funcs, NULL);
178         if (ret)
179                 return ret;
180
181         encoder->possible_crtcs = 1 << drm_crtc_index(crtc);
182         ret = drm_encoder_init(dev, encoder, &drm_simple_kms_encoder_funcs,
183                                DRM_MODE_ENCODER_NONE, NULL);
184         if (ret)
185                 return ret;
186
187         return drm_mode_connector_attach_encoder(connector, encoder);
188 }
189 EXPORT_SYMBOL(drm_simple_display_pipe_init);
190
191 MODULE_LICENSE("GPL");