2 * Copyright (C) 2016 Noralf Trønnes
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_crtc_helper.h>
12 #include <drm/drm_gem_framebuffer_helper.h>
13 #include <drm/drm_modes.h>
14 #include <drm/tinydrm/tinydrm.h>
16 struct tinydrm_connector
{
17 struct drm_connector base
;
18 struct drm_display_mode mode
;
21 static inline struct tinydrm_connector
*
22 to_tinydrm_connector(struct drm_connector
*connector
)
24 return container_of(connector
, struct tinydrm_connector
, base
);
27 static int tinydrm_connector_get_modes(struct drm_connector
*connector
)
29 struct tinydrm_connector
*tconn
= to_tinydrm_connector(connector
);
30 struct drm_display_mode
*mode
;
32 mode
= drm_mode_duplicate(connector
->dev
, &tconn
->mode
);
34 DRM_ERROR("Failed to duplicate mode\n");
38 if (mode
->name
[0] == '\0')
39 drm_mode_set_name(mode
);
41 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
42 drm_mode_probed_add(connector
, mode
);
45 connector
->display_info
.width_mm
= mode
->width_mm
;
46 connector
->display_info
.height_mm
= mode
->height_mm
;
52 static const struct drm_connector_helper_funcs tinydrm_connector_hfuncs
= {
53 .get_modes
= tinydrm_connector_get_modes
,
56 static enum drm_connector_status
57 tinydrm_connector_detect(struct drm_connector
*connector
, bool force
)
59 if (drm_dev_is_unplugged(connector
->dev
))
60 return connector_status_disconnected
;
62 return connector
->status
;
65 static void tinydrm_connector_destroy(struct drm_connector
*connector
)
67 struct tinydrm_connector
*tconn
= to_tinydrm_connector(connector
);
69 drm_connector_cleanup(connector
);
73 static const struct drm_connector_funcs tinydrm_connector_funcs
= {
74 .reset
= drm_atomic_helper_connector_reset
,
75 .detect
= tinydrm_connector_detect
,
76 .fill_modes
= drm_helper_probe_single_connector_modes
,
77 .destroy
= tinydrm_connector_destroy
,
78 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
79 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
82 struct drm_connector
*
83 tinydrm_connector_create(struct drm_device
*drm
,
84 const struct drm_display_mode
*mode
,
87 struct tinydrm_connector
*tconn
;
88 struct drm_connector
*connector
;
91 tconn
= kzalloc(sizeof(*tconn
), GFP_KERNEL
);
93 return ERR_PTR(-ENOMEM
);
95 drm_mode_copy(&tconn
->mode
, mode
);
96 connector
= &tconn
->base
;
98 drm_connector_helper_add(connector
, &tinydrm_connector_hfuncs
);
99 ret
= drm_connector_init(drm
, connector
, &tinydrm_connector_funcs
,
106 connector
->status
= connector_status_connected
;
112 * tinydrm_display_pipe_update - Display pipe update helper
113 * @pipe: Simple display pipe
114 * @old_state: Old plane state
116 * This function does a full framebuffer flush if the plane framebuffer
117 * has changed. It also handles vblank events. Drivers can use this as their
118 * &drm_simple_display_pipe_funcs->update callback.
120 void tinydrm_display_pipe_update(struct drm_simple_display_pipe
*pipe
,
121 struct drm_plane_state
*old_state
)
123 struct tinydrm_device
*tdev
= pipe_to_tinydrm(pipe
);
124 struct drm_framebuffer
*fb
= pipe
->plane
.state
->fb
;
125 struct drm_crtc
*crtc
= &tdev
->pipe
.crtc
;
127 if (fb
&& (fb
!= old_state
->fb
)) {
129 tdev
->fb_dirty(fb
, NULL
, 0, 0, NULL
, 0);
132 if (crtc
->state
->event
) {
133 spin_lock_irq(&crtc
->dev
->event_lock
);
134 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
135 spin_unlock_irq(&crtc
->dev
->event_lock
);
136 crtc
->state
->event
= NULL
;
139 EXPORT_SYMBOL(tinydrm_display_pipe_update
);
141 static int tinydrm_rotate_mode(struct drm_display_mode
*mode
,
142 unsigned int rotation
)
144 if (rotation
== 0 || rotation
== 180) {
146 } else if (rotation
== 90 || rotation
== 270) {
147 swap(mode
->hdisplay
, mode
->vdisplay
);
148 swap(mode
->hsync_start
, mode
->vsync_start
);
149 swap(mode
->hsync_end
, mode
->vsync_end
);
150 swap(mode
->htotal
, mode
->vtotal
);
151 swap(mode
->width_mm
, mode
->height_mm
);
159 * tinydrm_display_pipe_init - Initialize display pipe
160 * @tdev: tinydrm device
161 * @funcs: Display pipe functions
162 * @connector_type: Connector type
163 * @formats: Array of supported formats (DRM_FORMAT\_\*)
164 * @format_count: Number of elements in @formats
165 * @mode: Supported mode
166 * @rotation: Initial @mode rotation in degrees Counter Clock Wise
168 * This function sets up a &drm_simple_display_pipe with a &drm_connector that
169 * has one fixed &drm_display_mode which is rotated according to @rotation.
172 * Zero on success, negative error code on failure.
175 tinydrm_display_pipe_init(struct tinydrm_device
*tdev
,
176 const struct drm_simple_display_pipe_funcs
*funcs
,
178 const uint32_t *formats
,
179 unsigned int format_count
,
180 const struct drm_display_mode
*mode
,
181 unsigned int rotation
)
183 struct drm_device
*drm
= tdev
->drm
;
184 struct drm_display_mode mode_copy
;
185 struct drm_connector
*connector
;
188 drm_mode_copy(&mode_copy
, mode
);
189 ret
= tinydrm_rotate_mode(&mode_copy
, rotation
);
191 DRM_ERROR("Illegal rotation value %u\n", rotation
);
195 drm
->mode_config
.min_width
= mode_copy
.hdisplay
;
196 drm
->mode_config
.max_width
= mode_copy
.hdisplay
;
197 drm
->mode_config
.min_height
= mode_copy
.vdisplay
;
198 drm
->mode_config
.max_height
= mode_copy
.vdisplay
;
200 connector
= tinydrm_connector_create(drm
, &mode_copy
, connector_type
);
201 if (IS_ERR(connector
))
202 return PTR_ERR(connector
);
204 return drm_simple_display_pipe_init(drm
, &tdev
->pipe
, funcs
, formats
,
205 format_count
, NULL
, connector
);
207 EXPORT_SYMBOL(tinydrm_display_pipe_init
);