2 * drivers/staging/omapdrm/omap_crtc.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
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.
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
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/>.
24 #include "drm_crtc_helper.h"
26 #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
30 struct drm_plane
*plane
;
34 /* if there is a pending flip, these will be non-null: */
35 struct drm_pending_vblank_event
*event
;
36 struct drm_framebuffer
*old_fb
;
39 static void omap_crtc_gamma_set(struct drm_crtc
*crtc
,
40 u16
*red
, u16
*green
, u16
*blue
, uint32_t start
, uint32_t size
)
42 /* not supported.. at least not yet */
45 static void omap_crtc_destroy(struct drm_crtc
*crtc
)
47 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
48 omap_crtc
->plane
->funcs
->destroy(omap_crtc
->plane
);
49 drm_crtc_cleanup(crtc
);
53 static void omap_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
55 struct omap_drm_private
*priv
= crtc
->dev
->dev_private
;
56 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
59 WARN_ON(omap_plane_dpms(omap_crtc
->plane
, mode
));
61 for (i
= 0; i
< priv
->num_planes
; i
++) {
62 struct drm_plane
*plane
= priv
->planes
[i
];
63 if (plane
->crtc
== crtc
)
64 WARN_ON(omap_plane_dpms(plane
, mode
));
68 static bool omap_crtc_mode_fixup(struct drm_crtc
*crtc
,
69 struct drm_display_mode
*mode
,
70 struct drm_display_mode
*adjusted_mode
)
75 static int omap_crtc_mode_set(struct drm_crtc
*crtc
,
76 struct drm_display_mode
*mode
,
77 struct drm_display_mode
*adjusted_mode
,
79 struct drm_framebuffer
*old_fb
)
81 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
82 struct drm_plane
*plane
= omap_crtc
->plane
;
84 return omap_plane_mode_set(plane
, crtc
, crtc
->fb
,
85 0, 0, mode
->hdisplay
, mode
->vdisplay
,
87 mode
->hdisplay
<< 16, mode
->vdisplay
<< 16);
90 static void omap_crtc_prepare(struct drm_crtc
*crtc
)
92 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
93 DBG("%s", omap_crtc
->name
);
94 omap_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
97 static void omap_crtc_commit(struct drm_crtc
*crtc
)
99 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
100 DBG("%s", omap_crtc
->name
);
101 omap_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
104 static int omap_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
105 struct drm_framebuffer
*old_fb
)
107 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
108 struct drm_plane
*plane
= omap_crtc
->plane
;
109 struct drm_display_mode
*mode
= &crtc
->mode
;
111 return plane
->funcs
->update_plane(plane
, crtc
, crtc
->fb
,
112 0, 0, mode
->hdisplay
, mode
->vdisplay
,
114 mode
->hdisplay
<< 16, mode
->vdisplay
<< 16);
117 static void omap_crtc_load_lut(struct drm_crtc
*crtc
)
121 static void page_flip_cb(void *arg
)
123 struct drm_crtc
*crtc
= arg
;
124 struct drm_device
*dev
= crtc
->dev
;
125 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
126 struct drm_pending_vblank_event
*event
= omap_crtc
->event
;
127 struct drm_framebuffer
*old_fb
= omap_crtc
->old_fb
;
133 omap_crtc
->event
= NULL
;
134 omap_crtc
->old_fb
= NULL
;
136 omap_crtc_mode_set_base(crtc
, crtc
->x
, crtc
->y
, old_fb
);
138 /* wakeup userspace */
139 /* TODO: this should happen *after* flip in vsync IRQ handler */
141 spin_lock_irqsave(&dev
->event_lock
, flags
);
142 event
->event
.sequence
= drm_vblank_count_and_time(
143 dev
, omap_crtc
->id
, &now
);
144 event
->event
.tv_sec
= now
.tv_sec
;
145 event
->event
.tv_usec
= now
.tv_usec
;
146 list_add_tail(&event
->base
.link
,
147 &event
->base
.file_priv
->event_list
);
148 wake_up_interruptible(&event
->base
.file_priv
->event_wait
);
149 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
153 static int omap_crtc_page_flip_locked(struct drm_crtc
*crtc
,
154 struct drm_framebuffer
*fb
,
155 struct drm_pending_vblank_event
*event
)
157 struct drm_device
*dev
= crtc
->dev
;
158 struct omap_crtc
*omap_crtc
= to_omap_crtc(crtc
);
160 DBG("%d -> %d", crtc
->fb
? crtc
->fb
->base
.id
: -1, fb
->base
.id
);
162 if (omap_crtc
->event
) {
163 dev_err(dev
->dev
, "already a pending flip\n");
167 omap_crtc
->old_fb
= crtc
->fb
;
168 omap_crtc
->event
= event
;
171 omap_gem_op_async(omap_framebuffer_bo(fb
, 0), OMAP_GEM_READ
,
177 static const struct drm_crtc_funcs omap_crtc_funcs
= {
178 .gamma_set
= omap_crtc_gamma_set
,
179 .set_config
= drm_crtc_helper_set_config
,
180 .destroy
= omap_crtc_destroy
,
181 .page_flip
= omap_crtc_page_flip_locked
,
184 static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs
= {
185 .dpms
= omap_crtc_dpms
,
186 .mode_fixup
= omap_crtc_mode_fixup
,
187 .mode_set
= omap_crtc_mode_set
,
188 .prepare
= omap_crtc_prepare
,
189 .commit
= omap_crtc_commit
,
190 .mode_set_base
= omap_crtc_mode_set_base
,
191 .load_lut
= omap_crtc_load_lut
,
194 /* initialize crtc */
195 struct drm_crtc
*omap_crtc_init(struct drm_device
*dev
,
196 struct omap_overlay
*ovl
, int id
)
198 struct drm_crtc
*crtc
= NULL
;
199 struct omap_crtc
*omap_crtc
= kzalloc(sizeof(*omap_crtc
), GFP_KERNEL
);
201 DBG("%s", ovl
->name
);
204 dev_err(dev
->dev
, "could not allocate CRTC\n");
208 crtc
= &omap_crtc
->base
;
210 omap_crtc
->plane
= omap_plane_init(dev
, ovl
, (1 << id
), true);
211 omap_crtc
->plane
->crtc
= crtc
;
212 omap_crtc
->name
= ovl
->name
;
215 drm_crtc_init(dev
, crtc
, &omap_crtc_funcs
);
216 drm_crtc_helper_add(crtc
, &omap_crtc_helper_funcs
);
222 omap_crtc_destroy(crtc
);