2 * drm kms/fb cma (contiguous memory allocator) helper functions
4 * Copyright (C) 2012 Analog Device Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
8 * Copyright (C) 2012 Red Hat
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <drm/drm_client.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_framebuffer.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_gem_framebuffer_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
27 #include <drm/drm_print.h>
28 #include <linux/module.h>
30 struct drm_fbdev_cma
{
31 struct drm_fb_helper fb_helper
;
35 * DOC: framebuffer cma helper functions
37 * Provides helper functions for creating a cma (contiguous memory allocator)
40 * drm_gem_fb_create() is used in the &drm_mode_config_funcs.fb_create
41 * callback function to create a cma backed framebuffer.
43 * An fbdev framebuffer backed by cma is also available by calling
44 * drm_fb_cma_fbdev_init(). drm_fb_cma_fbdev_fini() tears it down.
47 static inline struct drm_fbdev_cma
*to_fbdev_cma(struct drm_fb_helper
*helper
)
49 return container_of(helper
, struct drm_fbdev_cma
, fb_helper
);
53 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
54 * @fb: The framebuffer
57 * Return the CMA GEM object for given framebuffer.
59 * This function will usually be called from the CRTC callback functions.
61 struct drm_gem_cma_object
*drm_fb_cma_get_gem_obj(struct drm_framebuffer
*fb
,
64 struct drm_gem_object
*gem
;
66 gem
= drm_gem_fb_get_obj(fb
, plane
);
70 return to_drm_gem_cma_obj(gem
);
72 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj
);
75 * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
76 * @fb: The framebuffer
77 * @state: Which state of drm plane
79 * Return the CMA GEM address for given framebuffer.
81 * This function will usually be called from the PLANE callback functions.
83 dma_addr_t
drm_fb_cma_get_gem_addr(struct drm_framebuffer
*fb
,
84 struct drm_plane_state
*state
,
87 struct drm_gem_cma_object
*obj
;
89 u8 h_div
= 1, v_div
= 1;
91 obj
= drm_fb_cma_get_gem_obj(fb
, plane
);
95 paddr
= obj
->paddr
+ fb
->offsets
[plane
];
98 h_div
= fb
->format
->hsub
;
99 v_div
= fb
->format
->vsub
;
102 paddr
+= (fb
->format
->cpp
[plane
] * (state
->src_x
>> 16)) / h_div
;
103 paddr
+= (fb
->pitches
[plane
] * (state
->src_y
>> 16)) / v_div
;
107 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr
);
110 * drm_fb_cma_fbdev_init() - Allocate and initialize fbdev emulation
112 * @preferred_bpp: Preferred bits per pixel for the device.
113 * @dev->mode_config.preferred_depth is used if this is zero.
114 * @max_conn_count: Maximum number of connectors.
115 * @dev->mode_config.num_connector is used if this is zero.
118 * Zero on success or negative error code on failure.
120 int drm_fb_cma_fbdev_init(struct drm_device
*dev
, unsigned int preferred_bpp
,
121 unsigned int max_conn_count
)
123 struct drm_fbdev_cma
*fbdev_cma
;
125 /* dev->fb_helper will indirectly point to fbdev_cma after this call */
126 fbdev_cma
= drm_fbdev_cma_init(dev
, preferred_bpp
, max_conn_count
);
127 if (IS_ERR(fbdev_cma
))
128 return PTR_ERR(fbdev_cma
);
132 EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_init
);
135 * drm_fb_cma_fbdev_fini() - Teardown fbdev emulation
138 void drm_fb_cma_fbdev_fini(struct drm_device
*dev
)
141 drm_fbdev_cma_fini(to_fbdev_cma(dev
->fb_helper
));
143 EXPORT_SYMBOL_GPL(drm_fb_cma_fbdev_fini
);
145 static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs
= {
146 .fb_probe
= drm_fb_helper_generic_probe
,
150 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
152 * @preferred_bpp: Preferred bits per pixel for the device
153 * @max_conn_count: Maximum number of connectors
155 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
157 struct drm_fbdev_cma
*drm_fbdev_cma_init(struct drm_device
*dev
,
158 unsigned int preferred_bpp
, unsigned int max_conn_count
)
160 struct drm_fbdev_cma
*fbdev_cma
;
161 struct drm_fb_helper
*fb_helper
;
164 fbdev_cma
= kzalloc(sizeof(*fbdev_cma
), GFP_KERNEL
);
166 return ERR_PTR(-ENOMEM
);
168 fb_helper
= &fbdev_cma
->fb_helper
;
170 ret
= drm_client_init(dev
, &fb_helper
->client
, "fbdev", NULL
);
174 ret
= drm_fb_helper_fbdev_setup(dev
, fb_helper
, &drm_fb_cma_helper_funcs
,
175 preferred_bpp
, max_conn_count
);
179 drm_client_add(&fb_helper
->client
);
184 drm_client_release(&fb_helper
->client
);
190 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init
);
193 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
194 * @fbdev_cma: The drm_fbdev_cma struct
196 void drm_fbdev_cma_fini(struct drm_fbdev_cma
*fbdev_cma
)
198 drm_fb_helper_unregister_fbi(&fbdev_cma
->fb_helper
);
199 /* All resources have now been freed by drm_fbdev_fb_destroy() */
201 EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini
);
204 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
205 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
207 * This function is usually called from the &drm_driver.lastclose callback.
209 void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma
*fbdev_cma
)
212 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma
->fb_helper
);
214 EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode
);
217 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
218 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
220 * This function is usually called from the &drm_mode_config.output_poll_changed
223 void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma
*fbdev_cma
)
226 drm_fb_helper_hotplug_event(&fbdev_cma
->fb_helper
);
228 EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event
);
231 * drm_fbdev_cma_set_suspend_unlocked - wrapper around
232 * drm_fb_helper_set_suspend_unlocked
233 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
234 * @state: desired state, zero to resume, non-zero to suspend
236 * Calls drm_fb_helper_set_suspend, which is a wrapper around
237 * fb_set_suspend implemented by fbdev core.
239 void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma
*fbdev_cma
,
243 drm_fb_helper_set_suspend_unlocked(&fbdev_cma
->fb_helper
,
246 EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked
);