drm/fb-helper: Add drm_fb_helper_fbdev_setup/teardown()
authorNoralf Trønnes <noralf@tronnes.org>
Fri, 15 Dec 2017 17:51:14 +0000 (18:51 +0100)
committerNoralf Trønnes <noralf@tronnes.org>
Wed, 20 Dec 2017 13:50:48 +0000 (14:50 +0100)
Add helpers to setup and teardown fbdev emulation.

Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20171215175119.36181-3-noralf@tronnes.org
drivers/gpu/drm/drm_fb_helper.c
include/drm/drm_fb_helper.h

index 04a3a5ce370a059d8fb36f700a7f93a48182e2c6..823fc8f50d8563b746138473c2fa471833c46017 100644 (file)
@@ -2729,6 +2729,112 @@ int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
 }
 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
 
+/**
+ * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
+ * @dev: DRM device
+ * @fb_helper: fbdev helper structure to set up
+ * @funcs: fbdev helper functions
+ * @preferred_bpp: Preferred bits per pixel for the device.
+ *                 @dev->mode_config.preferred_depth is used if this is zero.
+ * @max_conn_count: Maximum number of connectors.
+ *                  @dev->mode_config.num_connector is used if this is zero.
+ *
+ * This function sets up fbdev emulation and registers fbdev for access by
+ * userspace. If all connectors are disconnected, setup is deferred to the next
+ * time drm_fb_helper_hotplug_event() is called.
+ * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
+ * function.
+ *
+ * See also: drm_fb_helper_initial_config()
+ *
+ * Returns:
+ * Zero on success or negative error code on failure.
+ */
+int drm_fb_helper_fbdev_setup(struct drm_device *dev,
+                             struct drm_fb_helper *fb_helper,
+                             const struct drm_fb_helper_funcs *funcs,
+                             unsigned int preferred_bpp,
+                             unsigned int max_conn_count)
+{
+       int ret;
+
+       if (!preferred_bpp)
+               preferred_bpp = dev->mode_config.preferred_depth;
+       if (!preferred_bpp)
+               preferred_bpp = 32;
+
+       if (!max_conn_count)
+               max_conn_count = dev->mode_config.num_connector;
+       if (!max_conn_count) {
+               DRM_DEV_ERROR(dev->dev, "No connectors\n");
+               return -EINVAL;
+       }
+
+       drm_fb_helper_prepare(dev, fb_helper, funcs);
+
+       ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
+       if (ret < 0) {
+               DRM_DEV_ERROR(dev->dev, "Failed to initialize fbdev helper\n");
+               return ret;
+       }
+
+       ret = drm_fb_helper_single_add_all_connectors(fb_helper);
+       if (ret < 0) {
+               DRM_DEV_ERROR(dev->dev, "Failed to add connectors\n");
+               goto err_drm_fb_helper_fini;
+       }
+
+       if (!drm_drv_uses_atomic_modeset(dev))
+               drm_helper_disable_unused_functions(dev);
+
+       ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
+       if (ret < 0) {
+               DRM_DEV_ERROR(dev->dev, "Failed to set fbdev configuration\n");
+               goto err_drm_fb_helper_fini;
+       }
+
+       return 0;
+
+err_drm_fb_helper_fini:
+       drm_fb_helper_fini(fb_helper);
+
+       return ret;
+}
+EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
+
+/**
+ * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
+ * @dev: DRM device
+ *
+ * This function unregisters fbdev if not already done and cleans up the
+ * associated resources including the &drm_framebuffer.
+ * The driver is responsible for freeing the &drm_fb_helper structure which is
+ * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
+ * when this function returns.
+ *
+ * In order to support device removal/unplug while file handles are still open,
+ * drm_fb_helper_unregister_fbi() should be called on device removal and
+ * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
+ * file handles are closed.
+ */
+void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
+{
+       struct drm_fb_helper *fb_helper = dev->fb_helper;
+
+       if (!fb_helper)
+               return;
+
+       /* Unregister if it hasn't been done already */
+       if (fb_helper->fbdev && fb_helper->fbdev->dev)
+               drm_fb_helper_unregister_fbi(fb_helper);
+
+       drm_fb_helper_fini(fb_helper);
+
+       if (fb_helper->fb)
+               drm_framebuffer_remove(fb_helper->fb);
+}
+EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
+
 /**
  * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
  * @dev: DRM device
index 1bd624579db75a32e3326f10fe668da57e4c7f26..aa78ac3b8ad059cc14a1cc5b870daeb942ae08ff 100644 (file)
@@ -320,6 +320,13 @@ int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_
 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
                                       struct drm_connector *connector);
 
+int drm_fb_helper_fbdev_setup(struct drm_device *dev,
+                             struct drm_fb_helper *fb_helper,
+                             const struct drm_fb_helper_funcs *funcs,
+                             unsigned int preferred_bpp,
+                             unsigned int max_conn_count);
+void drm_fb_helper_fbdev_teardown(struct drm_device *dev);
+
 void drm_fb_helper_lastclose(struct drm_device *dev);
 void drm_fb_helper_output_poll_changed(struct drm_device *dev);
 #else
@@ -525,6 +532,24 @@ drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
        return 0;
 }
 
+static inline int
+drm_fb_helper_fbdev_setup(struct drm_device *dev,
+                         struct drm_fb_helper *fb_helper,
+                         const struct drm_fb_helper_funcs *funcs,
+                         unsigned int preferred_bpp,
+                         unsigned int max_conn_count)
+{
+       /* So drivers can use it to free the struct */
+       dev->fb_helper = fb_helper;
+
+       return 0;
+}
+
+static inline void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
+{
+       dev->fb_helper = NULL;
+}
+
 static inline void drm_fb_helper_lastclose(struct drm_device *dev)
 {
 }