blob: f549f2874353e361dd8f1393a649f11c9aa5abc1 [file] [log] [blame]
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001/*
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>
Chen-Yu Tsai75448602017-02-23 16:05:34 +080022#include <linux/of_graph.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010023#include <linux/of_irq.h>
24#include <linux/regmap.h>
25
26#include <video/videomode.h>
27
Maxime Ripard9026e0d2015-10-29 09:36:23 +010028#include "sun4i_crtc.h"
29#include "sun4i_drv.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080030#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010031#include "sun4i_tcon.h"
32
Maxime Ripard45e88f92017-10-17 11:06:12 +020033/*
34 * While this isn't really working in the DRM theory, in practice we
35 * can only ever have one encoder per TCON since we have a mux in our
36 * TCON.
37 */
38static struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc)
39{
40 struct drm_encoder *encoder;
41
42 drm_for_each_encoder(encoder, crtc->dev)
43 if (encoder->crtc == crtc)
44 return encoder;
45
46 return NULL;
47}
48
Maxime Ripard656e5f62018-01-22 10:25:19 +010049static int sun4i_crtc_atomic_check(struct drm_crtc *crtc,
50 struct drm_crtc_state *state)
51{
52 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
53 struct sunxi_engine *engine = scrtc->engine;
54 int ret = 0;
55
56 if (engine && engine->ops && engine->ops->atomic_check)
57 ret = engine->ops->atomic_check(engine, state);
58
59 return ret;
60}
61
Maxime Ripard9026e0d2015-10-29 09:36:23 +010062static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
63 struct drm_crtc_state *old_state)
64{
65 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
66 struct drm_device *dev = crtc->dev;
Maxime Ripard6b8562c2018-01-22 10:25:21 +010067 struct sunxi_engine *engine = scrtc->engine;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010068 unsigned long flags;
69
70 if (crtc->state->event) {
71 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
72
73 spin_lock_irqsave(&dev->event_lock, flags);
74 scrtc->event = crtc->state->event;
75 spin_unlock_irqrestore(&dev->event_lock, flags);
76 crtc->state->event = NULL;
Maxime Ripard6b8562c2018-01-22 10:25:21 +010077 }
78
79 if (engine->ops->atomic_begin)
80 engine->ops->atomic_begin(engine, old_state);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010081}
82
83static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
84 struct drm_crtc_state *old_state)
85{
86 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Daniel Vettera33e93d2016-06-08 14:18:58 +020087 struct drm_pending_vblank_event *event = crtc->state->event;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010088
89 DRM_DEBUG_DRIVER("Committing plane changes\n");
90
Icenowy Zheng87969332017-05-17 22:47:17 +080091 sunxi_engine_commit(scrtc->engine);
Daniel Vettera33e93d2016-06-08 14:18:58 +020092
93 if (event) {
94 crtc->state->event = NULL;
95
96 spin_lock_irq(&crtc->dev->event_lock);
97 if (drm_crtc_vblank_get(crtc) == 0)
98 drm_crtc_arm_vblank_event(crtc, event);
99 else
100 drm_crtc_send_vblank_event(crtc, event);
101 spin_unlock_irq(&crtc->dev->event_lock);
102 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100103}
104
Laurent Pinchart64581712017-06-30 12:36:45 +0300105static void sun4i_crtc_atomic_disable(struct drm_crtc *crtc,
106 struct drm_crtc_state *old_state)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100107{
Maxime Ripard45e88f92017-10-17 11:06:12 +0200108 struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100109 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100110
111 DRM_DEBUG_DRIVER("Disabling the CRTC\n");
112
Maxime Ripard45e88f92017-10-17 11:06:12 +0200113 sun4i_tcon_set_status(scrtc->tcon, encoder, false);
Maxime Ripard2cd36832016-06-20 12:20:59 +0200114
115 if (crtc->state->event && !crtc->state->active) {
116 spin_lock_irq(&crtc->dev->event_lock);
117 drm_crtc_send_vblank_event(crtc, crtc->state->event);
118 spin_unlock_irq(&crtc->dev->event_lock);
119
120 crtc->state->event = NULL;
121 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100122}
123
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300124static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
125 struct drm_crtc_state *old_state)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100126{
Maxime Ripard45e88f92017-10-17 11:06:12 +0200127 struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100128 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100129
130 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
131
Maxime Ripard45e88f92017-10-17 11:06:12 +0200132 sun4i_tcon_set_status(scrtc->tcon, encoder, true);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100133}
134
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200135static void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc)
136{
137 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
138 struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
139 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
140
141 sun4i_tcon_mode_set(scrtc->tcon, encoder, mode);
142}
143
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100144static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
Maxime Ripard656e5f62018-01-22 10:25:19 +0100145 .atomic_check = sun4i_crtc_atomic_check,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100146 .atomic_begin = sun4i_crtc_atomic_begin,
147 .atomic_flush = sun4i_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300148 .atomic_enable = sun4i_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300149 .atomic_disable = sun4i_crtc_atomic_disable,
Maxime Ripard5b8f0912017-10-17 11:06:13 +0200150 .mode_set_nofb = sun4i_crtc_mode_set_nofb,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100151};
152
Shawn Guo50480a72017-02-07 17:16:31 +0800153static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
154{
155 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Shawn Guo50480a72017-02-07 17:16:31 +0800156
157 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
158
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +0800159 sun4i_tcon_enable_vblank(scrtc->tcon, true);
Shawn Guo50480a72017-02-07 17:16:31 +0800160
161 return 0;
162}
163
164static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
165{
166 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Shawn Guo50480a72017-02-07 17:16:31 +0800167
168 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
169
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +0800170 sun4i_tcon_enable_vblank(scrtc->tcon, false);
Shawn Guo50480a72017-02-07 17:16:31 +0800171}
172
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100173static const struct drm_crtc_funcs sun4i_crtc_funcs = {
174 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
175 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
176 .destroy = drm_crtc_cleanup,
177 .page_flip = drm_atomic_helper_page_flip,
178 .reset = drm_atomic_helper_crtc_reset,
179 .set_config = drm_atomic_helper_set_config,
Shawn Guo50480a72017-02-07 17:16:31 +0800180 .enable_vblank = sun4i_crtc_enable_vblank,
181 .disable_vblank = sun4i_crtc_disable_vblank,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100182};
183
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800184struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
Icenowy Zheng87969332017-05-17 22:47:17 +0800185 struct sunxi_engine *engine,
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800186 struct sun4i_tcon *tcon)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100187{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100188 struct sun4i_crtc *scrtc;
Icenowy Zheng7921e142017-05-15 00:30:36 +0800189 struct drm_plane **planes;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800190 struct drm_plane *primary = NULL, *cursor = NULL;
191 int ret, i;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100192
193 scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
194 if (!scrtc)
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800195 return ERR_PTR(-ENOMEM);
Icenowy Zheng87969332017-05-17 22:47:17 +0800196 scrtc->engine = engine;
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800197 scrtc->tcon = tcon;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100198
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800199 /* Create our layers */
Icenowy Zheng87969332017-05-17 22:47:17 +0800200 planes = sunxi_engine_layers_init(drm, engine);
Icenowy Zheng7921e142017-05-15 00:30:36 +0800201 if (IS_ERR(planes)) {
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800202 dev_err(drm->dev, "Couldn't create the planes\n");
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800203 return NULL;
204 }
205
206 /* find primary and cursor planes for drm_crtc_init_with_planes */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800207 for (i = 0; planes[i]; i++) {
208 struct drm_plane *plane = planes[i];
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800209
Icenowy Zheng7921e142017-05-15 00:30:36 +0800210 switch (plane->type) {
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800211 case DRM_PLANE_TYPE_PRIMARY:
Icenowy Zheng7921e142017-05-15 00:30:36 +0800212 primary = plane;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800213 break;
214 case DRM_PLANE_TYPE_CURSOR:
Icenowy Zheng7921e142017-05-15 00:30:36 +0800215 cursor = plane;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800216 break;
217 default:
218 break;
219 }
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800220 }
221
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100222 ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800223 primary,
224 cursor,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100225 &sun4i_crtc_funcs,
226 NULL);
227 if (ret) {
228 dev_err(drm->dev, "Couldn't init DRM CRTC\n");
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800229 return ERR_PTR(ret);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100230 }
231
232 drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
233
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800234 /* Set crtc.port to output port node of the tcon */
Chen-Yu Tsaie4cdcb72017-03-09 18:05:26 +0800235 scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800236 1);
237
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800238 /* Set possible_crtcs to this crtc for overlay planes */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800239 for (i = 0; planes[i]; i++) {
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800240 uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
Icenowy Zheng7921e142017-05-15 00:30:36 +0800241 struct drm_plane *plane = planes[i];
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800242
Icenowy Zheng7921e142017-05-15 00:30:36 +0800243 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
244 plane->possible_crtcs = possible_crtcs;
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800245 }
246
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100247 return scrtc;
248}