]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/gpu/drm/sun4i/sun4i_crtc.c
drm: Add old state pointer to CRTC .enable() helper function
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / sun4i / sun4i_crtc.c
1 /*
2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
4 *
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
13 #include <drm/drmP.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_modes.h>
18
19 #include <linux/clk-provider.h>
20 #include <linux/ioport.h>
21 #include <linux/of_address.h>
22 #include <linux/of_graph.h>
23 #include <linux/of_irq.h>
24 #include <linux/regmap.h>
25
26 #include <video/videomode.h>
27
28 #include "sun4i_crtc.h"
29 #include "sun4i_drv.h"
30 #include "sunxi_engine.h"
31 #include "sun4i_tcon.h"
32
33 static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
34 struct drm_crtc_state *old_state)
35 {
36 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
37 struct drm_device *dev = crtc->dev;
38 unsigned long flags;
39
40 if (crtc->state->event) {
41 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
42
43 spin_lock_irqsave(&dev->event_lock, flags);
44 scrtc->event = crtc->state->event;
45 spin_unlock_irqrestore(&dev->event_lock, flags);
46 crtc->state->event = NULL;
47 }
48 }
49
50 static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
51 struct drm_crtc_state *old_state)
52 {
53 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
54 struct drm_pending_vblank_event *event = crtc->state->event;
55
56 DRM_DEBUG_DRIVER("Committing plane changes\n");
57
58 sunxi_engine_commit(scrtc->engine);
59
60 if (event) {
61 crtc->state->event = NULL;
62
63 spin_lock_irq(&crtc->dev->event_lock);
64 if (drm_crtc_vblank_get(crtc) == 0)
65 drm_crtc_arm_vblank_event(crtc, event);
66 else
67 drm_crtc_send_vblank_event(crtc, event);
68 spin_unlock_irq(&crtc->dev->event_lock);
69 }
70 }
71
72 static void sun4i_crtc_disable(struct drm_crtc *crtc)
73 {
74 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
75
76 DRM_DEBUG_DRIVER("Disabling the CRTC\n");
77
78 sun4i_tcon_disable(scrtc->tcon);
79
80 if (crtc->state->event && !crtc->state->active) {
81 spin_lock_irq(&crtc->dev->event_lock);
82 drm_crtc_send_vblank_event(crtc, crtc->state->event);
83 spin_unlock_irq(&crtc->dev->event_lock);
84
85 crtc->state->event = NULL;
86 }
87 }
88
89 static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
90 struct drm_crtc_state *old_state)
91 {
92 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
93
94 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
95
96 sun4i_tcon_enable(scrtc->tcon);
97 }
98
99 static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
100 .atomic_begin = sun4i_crtc_atomic_begin,
101 .atomic_flush = sun4i_crtc_atomic_flush,
102 .atomic_enable = sun4i_crtc_atomic_enable,
103 .disable = sun4i_crtc_disable,
104 };
105
106 static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
107 {
108 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
109
110 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
111
112 sun4i_tcon_enable_vblank(scrtc->tcon, true);
113
114 return 0;
115 }
116
117 static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
118 {
119 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
120
121 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
122
123 sun4i_tcon_enable_vblank(scrtc->tcon, false);
124 }
125
126 static const struct drm_crtc_funcs sun4i_crtc_funcs = {
127 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
128 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
129 .destroy = drm_crtc_cleanup,
130 .page_flip = drm_atomic_helper_page_flip,
131 .reset = drm_atomic_helper_crtc_reset,
132 .set_config = drm_atomic_helper_set_config,
133 .enable_vblank = sun4i_crtc_enable_vblank,
134 .disable_vblank = sun4i_crtc_disable_vblank,
135 };
136
137 struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
138 struct sunxi_engine *engine,
139 struct sun4i_tcon *tcon)
140 {
141 struct sun4i_crtc *scrtc;
142 struct drm_plane **planes;
143 struct drm_plane *primary = NULL, *cursor = NULL;
144 int ret, i;
145
146 scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
147 if (!scrtc)
148 return ERR_PTR(-ENOMEM);
149 scrtc->engine = engine;
150 scrtc->tcon = tcon;
151
152 /* Create our layers */
153 planes = sunxi_engine_layers_init(drm, engine);
154 if (IS_ERR(planes)) {
155 dev_err(drm->dev, "Couldn't create the planes\n");
156 return NULL;
157 }
158
159 /* find primary and cursor planes for drm_crtc_init_with_planes */
160 for (i = 0; planes[i]; i++) {
161 struct drm_plane *plane = planes[i];
162
163 switch (plane->type) {
164 case DRM_PLANE_TYPE_PRIMARY:
165 primary = plane;
166 break;
167 case DRM_PLANE_TYPE_CURSOR:
168 cursor = plane;
169 break;
170 default:
171 break;
172 }
173 }
174
175 ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
176 primary,
177 cursor,
178 &sun4i_crtc_funcs,
179 NULL);
180 if (ret) {
181 dev_err(drm->dev, "Couldn't init DRM CRTC\n");
182 return ERR_PTR(ret);
183 }
184
185 drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
186
187 /* Set crtc.port to output port node of the tcon */
188 scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
189 1);
190
191 /* Set possible_crtcs to this crtc for overlay planes */
192 for (i = 0; planes[i]; i++) {
193 uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
194 struct drm_plane *plane = planes[i];
195
196 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
197 plane->possible_crtcs = possible_crtcs;
198 }
199
200 return scrtc;
201 }