xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_crtc.c (revision f7c35abe)
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_irq.h>
23 #include <linux/regmap.h>
24 
25 #include <video/videomode.h>
26 
27 #include "sun4i_backend.h"
28 #include "sun4i_crtc.h"
29 #include "sun4i_drv.h"
30 #include "sun4i_tcon.h"
31 
32 static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
33 				    struct drm_crtc_state *old_state)
34 {
35 	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
36 	struct drm_device *dev = crtc->dev;
37 	unsigned long flags;
38 
39 	if (crtc->state->event) {
40 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
41 
42 		spin_lock_irqsave(&dev->event_lock, flags);
43 		scrtc->event = crtc->state->event;
44 		spin_unlock_irqrestore(&dev->event_lock, flags);
45 		crtc->state->event = NULL;
46 	 }
47 }
48 
49 static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
50 				    struct drm_crtc_state *old_state)
51 {
52 	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
53 	struct sun4i_drv *drv = scrtc->drv;
54 	struct drm_pending_vblank_event *event = crtc->state->event;
55 
56 	DRM_DEBUG_DRIVER("Committing plane changes\n");
57 
58 	sun4i_backend_commit(drv->backend);
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 	struct sun4i_drv *drv = scrtc->drv;
76 
77 	DRM_DEBUG_DRIVER("Disabling the CRTC\n");
78 
79 	sun4i_tcon_disable(drv->tcon);
80 
81 	if (crtc->state->event && !crtc->state->active) {
82 		spin_lock_irq(&crtc->dev->event_lock);
83 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
84 		spin_unlock_irq(&crtc->dev->event_lock);
85 
86 		crtc->state->event = NULL;
87 	}
88 }
89 
90 static void sun4i_crtc_enable(struct drm_crtc *crtc)
91 {
92 	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
93 	struct sun4i_drv *drv = scrtc->drv;
94 
95 	DRM_DEBUG_DRIVER("Enabling the CRTC\n");
96 
97 	sun4i_tcon_enable(drv->tcon);
98 }
99 
100 static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
101 	.atomic_begin	= sun4i_crtc_atomic_begin,
102 	.atomic_flush	= sun4i_crtc_atomic_flush,
103 	.disable	= sun4i_crtc_disable,
104 	.enable		= sun4i_crtc_enable,
105 };
106 
107 static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
108 {
109 	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
110 	struct sun4i_drv *drv = scrtc->drv;
111 
112 	DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
113 
114 	sun4i_tcon_enable_vblank(drv->tcon, true);
115 
116 	return 0;
117 }
118 
119 static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
120 {
121 	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
122 	struct sun4i_drv *drv = scrtc->drv;
123 
124 	DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
125 
126 	sun4i_tcon_enable_vblank(drv->tcon, false);
127 }
128 
129 static const struct drm_crtc_funcs sun4i_crtc_funcs = {
130 	.atomic_destroy_state	= drm_atomic_helper_crtc_destroy_state,
131 	.atomic_duplicate_state	= drm_atomic_helper_crtc_duplicate_state,
132 	.destroy		= drm_crtc_cleanup,
133 	.page_flip		= drm_atomic_helper_page_flip,
134 	.reset			= drm_atomic_helper_crtc_reset,
135 	.set_config		= drm_atomic_helper_set_config,
136 	.enable_vblank		= sun4i_crtc_enable_vblank,
137 	.disable_vblank		= sun4i_crtc_disable_vblank,
138 };
139 
140 struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm)
141 {
142 	struct sun4i_drv *drv = drm->dev_private;
143 	struct sun4i_crtc *scrtc;
144 	int ret;
145 
146 	scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
147 	if (!scrtc)
148 		return NULL;
149 	scrtc->drv = drv;
150 
151 	ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
152 					drv->primary,
153 					NULL,
154 					&sun4i_crtc_funcs,
155 					NULL);
156 	if (ret) {
157 		dev_err(drm->dev, "Couldn't init DRM CRTC\n");
158 		return NULL;
159 	}
160 
161 	drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
162 
163 	return scrtc;
164 }
165