1 // SPDX-License-Identifier: MIT
3 #include <drm/drm_client.h>
4 #include <drm/drm_crtc_helper.h>
5 #include <drm/drm_drv.h>
6 #include <drm/drm_fbdev_client.h>
7 #include <drm/drm_fb_helper.h>
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_print.h>
12 * struct drm_client_funcs
15 static void drm_fbdev_client_unregister(struct drm_client_dev
*client
)
17 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
19 if (fb_helper
->info
) {
20 drm_fb_helper_unregister_info(fb_helper
);
22 drm_client_release(&fb_helper
->client
);
23 drm_fb_helper_unprepare(fb_helper
);
28 static int drm_fbdev_client_restore(struct drm_client_dev
*client
)
30 drm_fb_helper_lastclose(client
->dev
);
35 static int drm_fbdev_client_hotplug(struct drm_client_dev
*client
)
37 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
38 struct drm_device
*dev
= client
->dev
;
42 return drm_fb_helper_hotplug_event(dev
->fb_helper
);
44 ret
= drm_fb_helper_init(dev
, fb_helper
);
48 if (!drm_drv_uses_atomic_modeset(dev
))
49 drm_helper_disable_unused_functions(dev
);
51 ret
= drm_fb_helper_initial_config(fb_helper
);
53 goto err_drm_fb_helper_fini
;
57 err_drm_fb_helper_fini
:
58 drm_fb_helper_fini(fb_helper
);
60 drm_err(dev
, "fbdev: Failed to setup emulation (ret=%d)\n", ret
);
64 static int drm_fbdev_client_suspend(struct drm_client_dev
*client
, bool holds_console_lock
)
66 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
68 if (holds_console_lock
)
69 drm_fb_helper_set_suspend(fb_helper
, true);
71 drm_fb_helper_set_suspend_unlocked(fb_helper
, true);
76 static int drm_fbdev_client_resume(struct drm_client_dev
*client
, bool holds_console_lock
)
78 struct drm_fb_helper
*fb_helper
= drm_fb_helper_from_client(client
);
80 if (holds_console_lock
)
81 drm_fb_helper_set_suspend(fb_helper
, false);
83 drm_fb_helper_set_suspend_unlocked(fb_helper
, false);
88 static const struct drm_client_funcs drm_fbdev_client_funcs
= {
90 .unregister
= drm_fbdev_client_unregister
,
91 .restore
= drm_fbdev_client_restore
,
92 .hotplug
= drm_fbdev_client_hotplug
,
93 .suspend
= drm_fbdev_client_suspend
,
94 .resume
= drm_fbdev_client_resume
,
98 * drm_fbdev_client_setup() - Setup fbdev emulation
100 * @format: Preferred color format for the device. DRM_FORMAT_XRGB8888
101 * is used if this is zero.
103 * This function sets up fbdev emulation. Restore, hotplug events and
104 * teardown are all taken care of. Drivers that do suspend/resume need
105 * to call drm_client_dev_suspend() and drm_client_dev_resume() by
106 * themselves. Simple drivers might use drm_mode_config_helper_suspend().
108 * This function is safe to call even when there are no connectors present.
109 * Setup will be retried on the next hotplug event.
111 * The fbdev client is destroyed by drm_dev_unregister().
114 * 0 on success, or a negative errno code otherwise.
116 int drm_fbdev_client_setup(struct drm_device
*dev
, const struct drm_format_info
*format
)
118 struct drm_fb_helper
*fb_helper
;
119 unsigned int color_mode
;
122 /* TODO: Use format info throughout DRM */
124 unsigned int bpp
= drm_format_info_bpp(format
, 0);
128 color_mode
= format
->depth
; // could also be 15
134 switch (dev
->mode_config
.preferred_depth
) {
140 color_mode
= dev
->mode_config
.preferred_depth
;
144 drm_WARN(dev
, !dev
->registered
, "Device has not been registered.\n");
145 drm_WARN(dev
, dev
->fb_helper
, "fb_helper is already set!\n");
147 fb_helper
= kzalloc(sizeof(*fb_helper
), GFP_KERNEL
);
150 drm_fb_helper_prepare(dev
, fb_helper
, color_mode
, NULL
);
152 ret
= drm_client_init(dev
, &fb_helper
->client
, "fbdev", &drm_fbdev_client_funcs
);
154 drm_err(dev
, "Failed to register client: %d\n", ret
);
155 goto err_drm_client_init
;
158 drm_client_register(&fb_helper
->client
);
163 drm_fb_helper_unprepare(fb_helper
);
167 EXPORT_SYMBOL(drm_fbdev_client_setup
);