• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) STMicroelectronics SA 2014
3  * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
4  *          Fabien Dessenne <fabien.dessenne@st.com>
5  *          for STMicroelectronics.
6  * License terms:  GNU General Public License (GPL), version 2
7  */
8 
9 #include <drm/drmP.h>
10 #include <drm/drm_gem_cma_helper.h>
11 #include <drm/drm_fb_cma_helper.h>
12 
13 #include "sti_compositor.h"
14 #include "sti_gdp.h"
15 #include "sti_layer.h"
16 #include "sti_vid.h"
17 
sti_layer_to_str(struct sti_layer * layer)18 const char *sti_layer_to_str(struct sti_layer *layer)
19 {
20 	switch (layer->desc) {
21 	case STI_GDP_0:
22 		return "GDP0";
23 	case STI_GDP_1:
24 		return "GDP1";
25 	case STI_GDP_2:
26 		return "GDP2";
27 	case STI_GDP_3:
28 		return "GDP3";
29 	case STI_VID_0:
30 		return "VID0";
31 	case STI_VID_1:
32 		return "VID1";
33 	case STI_CURSOR:
34 		return "CURSOR";
35 	default:
36 		return "<UNKNOWN LAYER>";
37 	}
38 }
39 
sti_layer_create(struct device * dev,int desc,void __iomem * baseaddr)40 struct sti_layer *sti_layer_create(struct device *dev, int desc,
41 				   void __iomem *baseaddr)
42 {
43 
44 	struct sti_layer *layer = NULL;
45 
46 	switch (desc & STI_LAYER_TYPE_MASK) {
47 	case STI_GDP:
48 		layer = sti_gdp_create(dev, desc);
49 		break;
50 	case STI_VID:
51 		layer = sti_vid_create(dev);
52 		break;
53 	}
54 
55 	if (!layer) {
56 		DRM_ERROR("Failed to create layer\n");
57 		return NULL;
58 	}
59 
60 	layer->desc = desc;
61 	layer->dev = dev;
62 	layer->regs = baseaddr;
63 
64 	layer->ops->init(layer);
65 
66 	DRM_DEBUG_DRIVER("%s created\n", sti_layer_to_str(layer));
67 
68 	return layer;
69 }
70 
sti_layer_prepare(struct sti_layer * layer,struct drm_framebuffer * fb,struct drm_display_mode * mode,int mixer_id,int dest_x,int dest_y,int dest_w,int dest_h,int src_x,int src_y,int src_w,int src_h)71 int sti_layer_prepare(struct sti_layer *layer, struct drm_framebuffer *fb,
72 		      struct drm_display_mode *mode, int mixer_id,
73 		      int dest_x, int dest_y, int dest_w, int dest_h,
74 		      int src_x, int src_y, int src_w, int src_h)
75 {
76 	int ret;
77 	unsigned int i;
78 	struct drm_gem_cma_object *cma_obj;
79 
80 	if (!layer || !fb || !mode) {
81 		DRM_ERROR("Null fb, layer or mode\n");
82 		return 1;
83 	}
84 
85 	cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
86 	if (!cma_obj) {
87 		DRM_ERROR("Can't get CMA GEM object for fb\n");
88 		return 1;
89 	}
90 
91 	layer->fb = fb;
92 	layer->mode = mode;
93 	layer->mixer_id = mixer_id;
94 	layer->dst_x = dest_x;
95 	layer->dst_y = dest_y;
96 	layer->dst_w = clamp_val(dest_w, 0, mode->crtc_hdisplay - dest_x);
97 	layer->dst_h = clamp_val(dest_h, 0, mode->crtc_vdisplay - dest_y);
98 	layer->src_x = src_x;
99 	layer->src_y = src_y;
100 	layer->src_w = src_w;
101 	layer->src_h = src_h;
102 	layer->format = fb->pixel_format;
103 	layer->paddr = cma_obj->paddr;
104 	for (i = 0; i < 4; i++) {
105 		layer->pitches[i] = fb->pitches[i];
106 		layer->offsets[i] = fb->offsets[i];
107 	}
108 
109 	DRM_DEBUG_DRIVER("%s is associated with mixer_id %d\n",
110 			 sti_layer_to_str(layer),
111 			 layer->mixer_id);
112 	DRM_DEBUG_DRIVER("%s dst=(%dx%d)@(%d,%d) - src=(%dx%d)@(%d,%d)\n",
113 			 sti_layer_to_str(layer),
114 			 layer->dst_w, layer->dst_h, layer->dst_x, layer->dst_y,
115 			 layer->src_w, layer->src_h, layer->src_x,
116 			 layer->src_y);
117 
118 	DRM_DEBUG_DRIVER("drm FB:%d format:%.4s phys@:0x%lx\n", fb->base.id,
119 			 (char *)&layer->format, (unsigned long)layer->paddr);
120 
121 	if (!layer->ops->prepare)
122 		goto err_no_prepare;
123 
124 	ret = layer->ops->prepare(layer, !layer->enabled);
125 	if (!ret)
126 		layer->enabled = true;
127 
128 	return ret;
129 
130 err_no_prepare:
131 	DRM_ERROR("Cannot prepare\n");
132 	return 1;
133 }
134 
sti_layer_commit(struct sti_layer * layer)135 int sti_layer_commit(struct sti_layer *layer)
136 {
137 	if (!layer)
138 		return 1;
139 
140 	if (!layer->ops->commit)
141 		goto err_no_commit;
142 
143 	return layer->ops->commit(layer);
144 
145 err_no_commit:
146 	DRM_ERROR("Cannot commit\n");
147 	return 1;
148 }
149 
sti_layer_disable(struct sti_layer * layer)150 int sti_layer_disable(struct sti_layer *layer)
151 {
152 	int ret;
153 
154 	DRM_DEBUG_DRIVER("%s\n", sti_layer_to_str(layer));
155 	if (!layer)
156 		return 1;
157 
158 	if (!layer->enabled)
159 		return 0;
160 
161 	if (!layer->ops->disable)
162 		goto err_no_disable;
163 
164 	ret = layer->ops->disable(layer);
165 	if (!ret)
166 		layer->enabled = false;
167 	else
168 		DRM_ERROR("Disable failed\n");
169 
170 	return ret;
171 
172 err_no_disable:
173 	DRM_ERROR("Cannot disable\n");
174 	return 1;
175 }
176 
sti_layer_get_formats(struct sti_layer * layer)177 const uint32_t *sti_layer_get_formats(struct sti_layer *layer)
178 {
179 	if (!layer)
180 		return NULL;
181 
182 	if (!layer->ops->get_formats)
183 		return NULL;
184 
185 	return layer->ops->get_formats(layer);
186 }
187 
sti_layer_get_nb_formats(struct sti_layer * layer)188 unsigned int sti_layer_get_nb_formats(struct sti_layer *layer)
189 {
190 	if (!layer)
191 		return 0;
192 
193 	if (!layer->ops->get_nb_formats)
194 		return 0;
195 
196 	return layer->ops->get_nb_formats(layer);
197 }
198