drm: sti: clear all mixer control
[deliverable/linux.git] / drivers / gpu / drm / sti / sti_drm_crtc.c
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 <linux/clk.h>
10
11 #include <drm/drmP.h>
12 #include <drm/drm_crtc_helper.h>
13 #include <drm/drm_plane_helper.h>
14
15 #include "sti_compositor.h"
16 #include "sti_drm_drv.h"
17 #include "sti_drm_crtc.h"
18 #include "sti_vtg.h"
19
20 static void sti_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
21 {
22 DRM_DEBUG_KMS("\n");
23 }
24
25 static void sti_drm_crtc_prepare(struct drm_crtc *crtc)
26 {
27 struct sti_mixer *mixer = to_sti_mixer(crtc);
28 struct device *dev = mixer->dev;
29 struct sti_compositor *compo = dev_get_drvdata(dev);
30
31 compo->enable = true;
32
33 /* Prepare and enable the compo IP clock */
34 if (mixer->id == STI_MIXER_MAIN) {
35 if (clk_prepare_enable(compo->clk_compo_main))
36 DRM_INFO("Failed to prepare/enable compo_main clk\n");
37 } else {
38 if (clk_prepare_enable(compo->clk_compo_aux))
39 DRM_INFO("Failed to prepare/enable compo_aux clk\n");
40 }
41
42 sti_mixer_clear_all_layers(mixer);
43 }
44
45 static void sti_drm_crtc_commit(struct drm_crtc *crtc)
46 {
47 struct sti_mixer *mixer = to_sti_mixer(crtc);
48 struct device *dev = mixer->dev;
49 struct sti_compositor *compo = dev_get_drvdata(dev);
50 struct sti_layer *layer;
51
52 if ((!mixer || !compo)) {
53 DRM_ERROR("Can not find mixer or compositor)\n");
54 return;
55 }
56
57 /* get GDP which is reserved to the CRTC FB */
58 layer = to_sti_layer(crtc->primary);
59 if (layer)
60 sti_layer_commit(layer);
61 else
62 DRM_ERROR("Can not find CRTC dedicated plane (GDP0)\n");
63
64 /* Enable layer on mixer */
65 if (sti_mixer_set_layer_status(mixer, layer, true))
66 DRM_ERROR("Can not enable layer at mixer\n");
67 }
68
69 static bool sti_drm_crtc_mode_fixup(struct drm_crtc *crtc,
70 const struct drm_display_mode *mode,
71 struct drm_display_mode *adjusted_mode)
72 {
73 /* accept the provided drm_display_mode, do not fix it up */
74 return true;
75 }
76
77 static int
78 sti_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
79 struct drm_display_mode *adjusted_mode, int x, int y,
80 struct drm_framebuffer *old_fb)
81 {
82 struct sti_mixer *mixer = to_sti_mixer(crtc);
83 struct device *dev = mixer->dev;
84 struct sti_compositor *compo = dev_get_drvdata(dev);
85 struct sti_layer *layer;
86 struct clk *clk;
87 int rate = mode->clock * 1000;
88 int res;
89 unsigned int w, h;
90
91 DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d mode:%d (%s)\n",
92 crtc->base.id, sti_mixer_to_str(mixer),
93 crtc->primary->fb->base.id, mode->base.id, mode->name);
94
95 DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
96 mode->vrefresh, mode->clock,
97 mode->hdisplay,
98 mode->hsync_start, mode->hsync_end,
99 mode->htotal,
100 mode->vdisplay,
101 mode->vsync_start, mode->vsync_end,
102 mode->vtotal, mode->type, mode->flags);
103
104 /* Set rate and prepare/enable pixel clock */
105 if (mixer->id == STI_MIXER_MAIN)
106 clk = compo->clk_pix_main;
107 else
108 clk = compo->clk_pix_aux;
109
110 res = clk_set_rate(clk, rate);
111 if (res < 0) {
112 DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
113 return -EINVAL;
114 }
115 if (clk_prepare_enable(clk)) {
116 DRM_ERROR("Failed to prepare/enable pix clk\n");
117 return -EINVAL;
118 }
119
120 sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
121 compo->vtg_main : compo->vtg_aux, &crtc->mode);
122
123 /* a GDP is reserved to the CRTC FB */
124 layer = to_sti_layer(crtc->primary);
125 if (!layer) {
126 DRM_ERROR("Can not find GDP0)\n");
127 return -EINVAL;
128 }
129
130 /* copy the mode data adjusted by mode_fixup() into crtc->mode
131 * so that hardware can be set to proper mode
132 */
133 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
134
135 res = sti_mixer_set_layer_depth(mixer, layer);
136 if (res) {
137 DRM_ERROR("Can not set layer depth\n");
138 return -EINVAL;
139 }
140 res = sti_mixer_active_video_area(mixer, &crtc->mode);
141 if (res) {
142 DRM_ERROR("Can not set active video area\n");
143 return -EINVAL;
144 }
145
146 w = crtc->primary->fb->width - x;
147 h = crtc->primary->fb->height - y;
148
149 return sti_layer_prepare(layer, crtc->primary->fb, &crtc->mode,
150 mixer->id, 0, 0, w, h, x, y, w, h);
151 }
152
153 static int sti_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
154 struct drm_framebuffer *old_fb)
155 {
156 struct sti_mixer *mixer = to_sti_mixer(crtc);
157 struct sti_layer *layer;
158 unsigned int w, h;
159 int ret;
160
161 DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d (%d,%d)\n",
162 crtc->base.id, sti_mixer_to_str(mixer),
163 crtc->primary->fb->base.id, x, y);
164
165 /* GDP is reserved to the CRTC FB */
166 layer = to_sti_layer(crtc->primary);
167 if (!layer) {
168 DRM_ERROR("Can not find GDP0)\n");
169 ret = -EINVAL;
170 goto out;
171 }
172
173 w = crtc->primary->fb->width - crtc->x;
174 h = crtc->primary->fb->height - crtc->y;
175
176 ret = sti_layer_prepare(layer, crtc->primary->fb, &crtc->mode,
177 mixer->id, 0, 0, w, h,
178 crtc->x, crtc->y, w, h);
179 if (ret) {
180 DRM_ERROR("Can not prepare layer\n");
181 goto out;
182 }
183
184 sti_drm_crtc_commit(crtc);
185 out:
186 return ret;
187 }
188
189 static void sti_drm_crtc_load_lut(struct drm_crtc *crtc)
190 {
191 /* do nothing */
192 }
193
194 static void sti_drm_crtc_disable(struct drm_crtc *crtc)
195 {
196 struct sti_mixer *mixer = to_sti_mixer(crtc);
197 struct device *dev = mixer->dev;
198 struct sti_compositor *compo = dev_get_drvdata(dev);
199 struct sti_layer *layer;
200
201 if (!compo->enable)
202 return;
203
204 DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
205
206 /* Disable Background */
207 sti_mixer_set_background_status(mixer, false);
208
209 /* Disable GDP */
210 layer = to_sti_layer(crtc->primary);
211 if (!layer) {
212 DRM_ERROR("Cannot find GDP0\n");
213 return;
214 }
215
216 /* Disable layer at mixer level */
217 if (sti_mixer_set_layer_status(mixer, layer, false))
218 DRM_ERROR("Can not disable %s layer at mixer\n",
219 sti_layer_to_str(layer));
220
221 /* Wait a while to be sure that a Vsync event is received */
222 msleep(WAIT_NEXT_VSYNC_MS);
223
224 /* Then disable layer itself */
225 sti_layer_disable(layer);
226
227 drm_vblank_off(crtc->dev, mixer->id);
228
229 /* Disable pixel clock and compo IP clocks */
230 if (mixer->id == STI_MIXER_MAIN) {
231 clk_disable_unprepare(compo->clk_pix_main);
232 clk_disable_unprepare(compo->clk_compo_main);
233 } else {
234 clk_disable_unprepare(compo->clk_pix_aux);
235 clk_disable_unprepare(compo->clk_compo_aux);
236 }
237
238 compo->enable = false;
239 }
240
241 static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
242 .dpms = sti_drm_crtc_dpms,
243 .prepare = sti_drm_crtc_prepare,
244 .commit = sti_drm_crtc_commit,
245 .mode_fixup = sti_drm_crtc_mode_fixup,
246 .mode_set = sti_drm_crtc_mode_set,
247 .mode_set_base = sti_drm_crtc_mode_set_base,
248 .load_lut = sti_drm_crtc_load_lut,
249 .disable = sti_drm_crtc_disable,
250 };
251
252 static int sti_drm_crtc_page_flip(struct drm_crtc *crtc,
253 struct drm_framebuffer *fb,
254 struct drm_pending_vblank_event *event,
255 uint32_t page_flip_flags)
256 {
257 struct drm_device *drm_dev = crtc->dev;
258 struct drm_framebuffer *old_fb;
259 struct sti_mixer *mixer = to_sti_mixer(crtc);
260 unsigned long flags;
261 int ret;
262
263 DRM_DEBUG_KMS("fb %d --> fb %d\n",
264 crtc->primary->fb->base.id, fb->base.id);
265
266 mutex_lock(&drm_dev->struct_mutex);
267
268 old_fb = crtc->primary->fb;
269 crtc->primary->fb = fb;
270 ret = sti_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, old_fb);
271 if (ret) {
272 DRM_ERROR("failed\n");
273 crtc->primary->fb = old_fb;
274 goto out;
275 }
276
277 if (event) {
278 event->pipe = mixer->id;
279
280 ret = drm_vblank_get(drm_dev, event->pipe);
281 if (ret) {
282 DRM_ERROR("Cannot get vblank\n");
283 goto out;
284 }
285
286 spin_lock_irqsave(&drm_dev->event_lock, flags);
287 if (mixer->pending_event) {
288 drm_vblank_put(drm_dev, event->pipe);
289 ret = -EBUSY;
290 } else {
291 mixer->pending_event = event;
292 }
293 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
294 }
295 out:
296 mutex_unlock(&drm_dev->struct_mutex);
297 return ret;
298 }
299
300 static void sti_drm_crtc_destroy(struct drm_crtc *crtc)
301 {
302 DRM_DEBUG_KMS("\n");
303 drm_crtc_cleanup(crtc);
304 }
305
306 static int sti_drm_crtc_set_property(struct drm_crtc *crtc,
307 struct drm_property *property,
308 uint64_t val)
309 {
310 DRM_DEBUG_KMS("\n");
311 return 0;
312 }
313
314 int sti_drm_crtc_vblank_cb(struct notifier_block *nb,
315 unsigned long event, void *data)
316 {
317 struct drm_device *drm_dev;
318 struct sti_compositor *compo =
319 container_of(nb, struct sti_compositor, vtg_vblank_nb);
320 int *crtc = data;
321 unsigned long flags;
322 struct sti_drm_private *priv;
323
324 drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
325 priv = drm_dev->dev_private;
326
327 if ((event != VTG_TOP_FIELD_EVENT) &&
328 (event != VTG_BOTTOM_FIELD_EVENT)) {
329 DRM_ERROR("unknown event: %lu\n", event);
330 return -EINVAL;
331 }
332
333 drm_handle_vblank(drm_dev, *crtc);
334
335 spin_lock_irqsave(&drm_dev->event_lock, flags);
336 if (compo->mixer[*crtc]->pending_event) {
337 drm_send_vblank_event(drm_dev, -1,
338 compo->mixer[*crtc]->pending_event);
339 drm_vblank_put(drm_dev, *crtc);
340 compo->mixer[*crtc]->pending_event = NULL;
341 }
342 spin_unlock_irqrestore(&drm_dev->event_lock, flags);
343
344 return 0;
345 }
346
347 int sti_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
348 {
349 struct sti_drm_private *dev_priv = dev->dev_private;
350 struct sti_compositor *compo = dev_priv->compo;
351 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
352
353 if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
354 compo->vtg_main : compo->vtg_aux,
355 vtg_vblank_nb, crtc)) {
356 DRM_ERROR("Cannot register VTG notifier\n");
357 return -EINVAL;
358 }
359
360 return 0;
361 }
362 EXPORT_SYMBOL(sti_drm_crtc_enable_vblank);
363
364 void sti_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
365 {
366 struct sti_drm_private *priv = dev->dev_private;
367 struct sti_compositor *compo = priv->compo;
368 struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
369 unsigned long flags;
370
371 DRM_DEBUG_DRIVER("\n");
372
373 if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
374 compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
375 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
376
377 /* free the resources of the pending requests */
378 spin_lock_irqsave(&dev->event_lock, flags);
379 if (compo->mixer[crtc]->pending_event) {
380 drm_vblank_put(dev, crtc);
381 compo->mixer[crtc]->pending_event = NULL;
382 }
383 spin_unlock_irqrestore(&dev->event_lock, flags);
384
385 }
386 EXPORT_SYMBOL(sti_drm_crtc_disable_vblank);
387
388 static struct drm_crtc_funcs sti_crtc_funcs = {
389 .set_config = drm_crtc_helper_set_config,
390 .page_flip = sti_drm_crtc_page_flip,
391 .destroy = sti_drm_crtc_destroy,
392 .set_property = sti_drm_crtc_set_property,
393 };
394
395 bool sti_drm_crtc_is_main(struct drm_crtc *crtc)
396 {
397 struct sti_mixer *mixer = to_sti_mixer(crtc);
398
399 if (mixer->id == STI_MIXER_MAIN)
400 return true;
401
402 return false;
403 }
404
405 int sti_drm_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
406 struct drm_plane *primary, struct drm_plane *cursor)
407 {
408 struct drm_crtc *crtc = &mixer->drm_crtc;
409 int res;
410
411 res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
412 &sti_crtc_funcs);
413 if (res) {
414 DRM_ERROR("Can not initialze CRTC\n");
415 return -EINVAL;
416 }
417
418 drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
419
420 DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
421 crtc->base.id, sti_mixer_to_str(mixer));
422
423 return 0;
424 }
This page took 0.062408 seconds and 5 git commands to generate.