1 /*
2  * drivers/staging/omapdrm/omap_plane.c
3  *
4  * Copyright (C) 2011 Texas Instruments
5  * Author: Rob Clark <rob.clark@linaro.org>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "omap_drv.h"
21 
22 /* some hackery because omapdss has an 'enum omap_plane' (which would be
23  * better named omap_plane_id).. and compiler seems unhappy about having
24  * both a 'struct omap_plane' and 'enum omap_plane'
25  */
26 #define omap_plane _omap_plane
27 
28 /*
29  * plane funcs
30  */
31 
32 #define to_omap_plane(x) container_of(x, struct omap_plane, base)
33 
34 struct omap_plane {
35 	struct drm_plane base;
36 	struct omap_overlay *ovl;
37 	struct omap_overlay_info info;
38 
39 	/* Source values, converted to integers because we don't support
40 	 * fractional positions:
41 	 */
42 	unsigned int src_x, src_y;
43 
44 	/* last fb that we pinned: */
45 	struct drm_framebuffer *pinned_fb;
46 };
47 
48 
49 /* push changes down to dss2 */
commit(struct drm_plane * plane)50 static int commit(struct drm_plane *plane)
51 {
52 	struct drm_device *dev = plane->dev;
53 	struct omap_plane *omap_plane = to_omap_plane(plane);
54 	struct omap_overlay *ovl = omap_plane->ovl;
55 	struct omap_overlay_info *info = &omap_plane->info;
56 	int ret;
57 
58 	DBG("%s", ovl->name);
59 	DBG("%dx%d -> %dx%d (%d)", info->width, info->height, info->out_width,
60 			info->out_height, info->screen_width);
61 	DBG("%d,%d %08x %08x", info->pos_x, info->pos_y,
62 			info->paddr, info->p_uv_addr);
63 
64 	/* NOTE: do we want to do this at all here, or just wait
65 	 * for dpms(ON) since other CRTC's may not have their mode
66 	 * set yet, so fb dimensions may still change..
67 	 */
68 	ret = ovl->set_overlay_info(ovl, info);
69 	if (ret) {
70 		dev_err(dev->dev, "could not set overlay info\n");
71 		return ret;
72 	}
73 
74 	/* our encoder doesn't necessarily get a commit() after this, in
75 	 * particular in the dpms() and mode_set_base() cases, so force the
76 	 * manager to update:
77 	 *
78 	 * could this be in the encoder somehow?
79 	 */
80 	if (ovl->manager) {
81 		ret = ovl->manager->apply(ovl->manager);
82 		if (ret) {
83 			dev_err(dev->dev, "could not apply settings\n");
84 			return ret;
85 		}
86 	}
87 
88 	if (ovl->is_enabled(ovl)) {
89 		omap_framebuffer_flush(plane->fb, info->pos_x, info->pos_y,
90 				info->out_width, info->out_height);
91 	}
92 
93 	return 0;
94 }
95 
96 /* when CRTC that we are attached to has potentially changed, this checks
97  * if we are attached to proper manager, and if necessary updates.
98  */
update_manager(struct drm_plane * plane)99 static void update_manager(struct drm_plane *plane)
100 {
101 	struct omap_drm_private *priv = plane->dev->dev_private;
102 	struct omap_plane *omap_plane = to_omap_plane(plane);
103 	struct omap_overlay *ovl = omap_plane->ovl;
104 	struct omap_overlay_manager *mgr = NULL;
105 	int i;
106 
107 	if (plane->crtc) {
108 		for (i = 0; i < priv->num_encoders; i++) {
109 			struct drm_encoder *encoder = priv->encoders[i];
110 			if (encoder->crtc == plane->crtc) {
111 				mgr = omap_encoder_get_manager(encoder);
112 				break;
113 			}
114 		}
115 	}
116 
117 	if (ovl->manager != mgr) {
118 		bool enabled = ovl->is_enabled(ovl);
119 
120 		/* don't switch things around with enabled overlays: */
121 		if (enabled)
122 			omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
123 
124 		if (ovl->manager) {
125 			DBG("disconnecting %s from %s", ovl->name,
126 					ovl->manager->name);
127 			ovl->unset_manager(ovl);
128 		}
129 
130 		if (mgr) {
131 			DBG("connecting %s to %s", ovl->name, mgr->name);
132 			ovl->set_manager(ovl, mgr);
133 		}
134 
135 		if (enabled && mgr)
136 			omap_plane_dpms(plane, DRM_MODE_DPMS_ON);
137 	}
138 }
139 
140 /* update which fb (if any) is pinned for scanout */
update_pin(struct drm_plane * plane,struct drm_framebuffer * fb)141 static int update_pin(struct drm_plane *plane, struct drm_framebuffer *fb)
142 {
143 	struct omap_plane *omap_plane = to_omap_plane(plane);
144 	int ret = 0;
145 
146 	if (omap_plane->pinned_fb != fb) {
147 		if (omap_plane->pinned_fb)
148 			omap_framebuffer_unpin(omap_plane->pinned_fb);
149 		omap_plane->pinned_fb = fb;
150 		if (fb)
151 			ret = omap_framebuffer_pin(fb);
152 	}
153 
154 	return ret;
155 }
156 
157 /* update parameters that are dependent on the framebuffer dimensions and
158  * position within the fb that this plane scans out from. This is called
159  * when framebuffer or x,y base may have changed.
160  */
update_scanout(struct drm_plane * plane)161 static void update_scanout(struct drm_plane *plane)
162 {
163 	struct omap_plane *omap_plane = to_omap_plane(plane);
164 	struct omap_overlay_info *info = &omap_plane->info;
165 	int ret;
166 
167 	ret = update_pin(plane, plane->fb);
168 	if (ret) {
169 		dev_err(plane->dev->dev,
170 			"could not pin fb: %d\n", ret);
171 		omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
172 		return;
173 	}
174 
175 	omap_framebuffer_update_scanout(plane->fb,
176 			omap_plane->src_x, omap_plane->src_y, info);
177 
178 	DBG("%s: %d,%d: %08x %08x (%d)", omap_plane->ovl->name,
179 			omap_plane->src_x, omap_plane->src_y,
180 			(u32)info->paddr, (u32)info->p_uv_addr,
181 			info->screen_width);
182 }
183 
omap_plane_mode_set(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)184 int omap_plane_mode_set(struct drm_plane *plane,
185 		struct drm_crtc *crtc, struct drm_framebuffer *fb,
186 		int crtc_x, int crtc_y,
187 		unsigned int crtc_w, unsigned int crtc_h,
188 		uint32_t src_x, uint32_t src_y,
189 		uint32_t src_w, uint32_t src_h)
190 {
191 	struct omap_plane *omap_plane = to_omap_plane(plane);
192 
193 	/* src values are in Q16 fixed point, convert to integer: */
194 	src_x = src_x >> 16;
195 	src_y = src_y >> 16;
196 	src_w = src_w >> 16;
197 	src_h = src_h >> 16;
198 
199 	omap_plane->info.pos_x = crtc_x;
200 	omap_plane->info.pos_y = crtc_y;
201 	omap_plane->info.out_width = crtc_w;
202 	omap_plane->info.out_height = crtc_h;
203 	omap_plane->info.width = src_w;
204 	omap_plane->info.height = src_h;
205 	omap_plane->src_x = src_x;
206 	omap_plane->src_y = src_y;
207 
208 	/* note: this is done after this fxn returns.. but if we need
209 	 * to do a commit/update_scanout, etc before this returns we
210 	 * need the current value.
211 	 */
212 	plane->fb = fb;
213 	plane->crtc = crtc;
214 
215 	update_scanout(plane);
216 	update_manager(plane);
217 
218 	return 0;
219 }
220 
omap_plane_update(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h)221 static int omap_plane_update(struct drm_plane *plane,
222 		struct drm_crtc *crtc, struct drm_framebuffer *fb,
223 		int crtc_x, int crtc_y,
224 		unsigned int crtc_w, unsigned int crtc_h,
225 		uint32_t src_x, uint32_t src_y,
226 		uint32_t src_w, uint32_t src_h)
227 {
228 	omap_plane_mode_set(plane, crtc, fb, crtc_x, crtc_y, crtc_w, crtc_h,
229 			src_x, src_y, src_w, src_h);
230 	return omap_plane_dpms(plane, DRM_MODE_DPMS_ON);
231 }
232 
omap_plane_disable(struct drm_plane * plane)233 static int omap_plane_disable(struct drm_plane *plane)
234 {
235 	return omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
236 }
237 
omap_plane_destroy(struct drm_plane * plane)238 static void omap_plane_destroy(struct drm_plane *plane)
239 {
240 	struct omap_plane *omap_plane = to_omap_plane(plane);
241 	DBG("%s", omap_plane->ovl->name);
242 	omap_plane_disable(plane);
243 	drm_plane_cleanup(plane);
244 	kfree(omap_plane);
245 }
246 
omap_plane_dpms(struct drm_plane * plane,int mode)247 int omap_plane_dpms(struct drm_plane *plane, int mode)
248 {
249 	struct omap_plane *omap_plane = to_omap_plane(plane);
250 	struct omap_overlay *ovl = omap_plane->ovl;
251 	int r;
252 
253 	DBG("%s: %d", omap_plane->ovl->name, mode);
254 
255 	if (mode == DRM_MODE_DPMS_ON) {
256 		update_scanout(plane);
257 		r = commit(plane);
258 		if (!r)
259 			r = ovl->enable(ovl);
260 	} else {
261 		r = ovl->disable(ovl);
262 		update_pin(plane, NULL);
263 	}
264 
265 	return r;
266 }
267 
268 static const struct drm_plane_funcs omap_plane_funcs = {
269 		.update_plane = omap_plane_update,
270 		.disable_plane = omap_plane_disable,
271 		.destroy = omap_plane_destroy,
272 };
273 
274 static const uint32_t formats[] = {
275 		DRM_FORMAT_RGB565,
276 		DRM_FORMAT_RGBX4444,
277 		DRM_FORMAT_XRGB4444,
278 		DRM_FORMAT_RGBA4444,
279 		DRM_FORMAT_ABGR4444,
280 		DRM_FORMAT_XRGB1555,
281 		DRM_FORMAT_ARGB1555,
282 		DRM_FORMAT_RGB888,
283 		DRM_FORMAT_RGBX8888,
284 		DRM_FORMAT_XRGB8888,
285 		DRM_FORMAT_RGBA8888,
286 		DRM_FORMAT_ARGB8888,
287 		DRM_FORMAT_NV12,
288 		DRM_FORMAT_YUYV,
289 		DRM_FORMAT_UYVY,
290 };
291 
292 /* initialize plane */
omap_plane_init(struct drm_device * dev,struct omap_overlay * ovl,unsigned int possible_crtcs,bool priv)293 struct drm_plane *omap_plane_init(struct drm_device *dev,
294 		struct omap_overlay *ovl, unsigned int possible_crtcs,
295 		bool priv)
296 {
297 	struct drm_plane *plane = NULL;
298 	struct omap_plane *omap_plane;
299 
300 	DBG("%s: possible_crtcs=%08x, priv=%d", ovl->name,
301 			possible_crtcs, priv);
302 
303 	omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
304 	if (!omap_plane) {
305 		dev_err(dev->dev, "could not allocate plane\n");
306 		goto fail;
307 	}
308 
309 	omap_plane->ovl = ovl;
310 	plane = &omap_plane->base;
311 
312 	drm_plane_init(dev, plane, possible_crtcs, &omap_plane_funcs,
313 			formats, ARRAY_SIZE(formats), priv);
314 
315 	/* get our starting configuration, set defaults for parameters
316 	 * we don't currently use, etc:
317 	 */
318 	ovl->get_overlay_info(ovl, &omap_plane->info);
319 	omap_plane->info.rotation_type = OMAP_DSS_ROT_DMA;
320 	omap_plane->info.rotation = OMAP_DSS_ROT_0;
321 	omap_plane->info.global_alpha = 0xff;
322 	omap_plane->info.mirror = 0;
323 	omap_plane->info.mirror = 0;
324 
325 	/* Set defaults depending on whether we are a CRTC or overlay
326 	 * layer.
327 	 * TODO add ioctl to give userspace an API to change this.. this
328 	 * will come in a subsequent patch.
329 	 */
330 	if (priv)
331 		omap_plane->info.zorder = 0;
332 	else
333 		omap_plane->info.zorder = 1;
334 
335 	update_manager(plane);
336 
337 	return plane;
338 
339 fail:
340 	if (plane) {
341 		omap_plane_destroy(plane);
342 	}
343 	return NULL;
344 }
345