NULL);
 #endif
 
-#ifdef CONFIG_DEBUG_FS
        mgbdev->debugfs = debugfs_create_dir(dev_name(&pdev->dev), NULL);
-#endif
 
        /* Get card serial number. On systems without MTD flash support we may
         * get an error thus ignore the return value. An invalid serial number
        hwmon_device_unregister(mgbdev->hwmon_dev);
 #endif
 
+       debugfs_remove_recursive(mgbdev->debugfs);
+
        if (mgbdev->indio_dev)
                mgb4_trigger_free(mgbdev->indio_dev);
 
                if (mgbdev->vin[i])
                        mgb4_vin_free(mgbdev->vin[i]);
 
-#ifdef CONFIG_DEBUG_FS
-       debugfs_remove_recursive(mgbdev->debugfs);
-#endif
-
        device_remove_groups(&mgbdev->pdev->dev, mgb4_pci_groups);
        free_spi(mgbdev);
        free_i2c(mgbdev);
 
        u8 module_version;
        u32 serial_number;
 
-#ifdef CONFIG_DEBUG_FS
        struct dentry *debugfs;
-#endif
 };
 
 #endif
 
        mgb4_write_reg(video, regs->config, 1U << 9);
 }
 
-#ifdef CONFIG_DEBUG_FS
-static void debugfs_init(struct mgb4_vin_dev *vindev)
+static void create_debugfs(struct mgb4_vin_dev *vindev)
 {
+#ifdef CONFIG_DEBUG_FS
        struct mgb4_regs *video = &vindev->mgbdev->video;
+       struct dentry *entry;
 
-       vindev->debugfs = debugfs_create_dir(vindev->vdev.name,
-                                            vindev->mgbdev->debugfs);
-       if (!vindev->debugfs)
+       if (IS_ERR_OR_NULL(vindev->mgbdev->debugfs))
+               return;
+       entry = debugfs_create_dir(vindev->vdev.name, vindev->mgbdev->debugfs);
+       if (IS_ERR(entry))
                return;
 
        vindev->regs[0].name = "CONFIG";
        vindev->regset.base = video->membase;
        vindev->regset.regs = vindev->regs;
 
-       debugfs_create_regset32("registers", 0444, vindev->debugfs,
-                               &vindev->regset);
-}
+       debugfs_create_regset32("registers", 0444, entry, &vindev->regset);
 #endif
+}
 
 struct mgb4_vin_dev *mgb4_vin_create(struct mgb4_dev *mgbdev, int id)
 {
                goto err_video_dev;
        }
 
-#ifdef CONFIG_DEBUG_FS
-       debugfs_init(vindev);
-#endif
+       create_debugfs(vindev);
 
        return vindev;
 
        free_irq(vin_irq, vindev);
        free_irq(err_irq, vindev);
 
-#ifdef CONFIG_DEBUG_FS
-       debugfs_remove_recursive(vindev->debugfs);
-#endif
-
        groups = MGB4_IS_GMSL(vindev->mgbdev)
          ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups;
        device_remove_groups(&vindev->vdev.dev, groups);
 
        const struct mgb4_vin_config *config;
 
 #ifdef CONFIG_DEBUG_FS
-       struct dentry *debugfs;
        struct debugfs_regset32 regset;
        struct debugfs_reg32 regs[sizeof(struct mgb4_vin_regs) / 4];
 #endif
 
                       (voutdev->config->id + MGB4_VIN_DEVICES) << 2 | 1 << 4);
 }
 
-#ifdef CONFIG_DEBUG_FS
-static void debugfs_init(struct mgb4_vout_dev *voutdev)
+static void create_debugfs(struct mgb4_vout_dev *voutdev)
 {
+#ifdef CONFIG_DEBUG_FS
        struct mgb4_regs *video = &voutdev->mgbdev->video;
+       struct dentry *entry;
 
-       voutdev->debugfs = debugfs_create_dir(voutdev->vdev.name,
-                                             voutdev->mgbdev->debugfs);
-       if (!voutdev->debugfs)
+       if (IS_ERR_OR_NULL(voutdev->mgbdev->debugfs))
+               return;
+       entry = debugfs_create_dir(voutdev->vdev.name, voutdev->mgbdev->debugfs);
+       if (IS_ERR(entry))
                return;
 
        voutdev->regs[0].name = "CONFIG";
        voutdev->regset.base = video->membase;
        voutdev->regset.regs = voutdev->regs;
 
-       debugfs_create_regset32("registers", 0444, voutdev->debugfs,
-                               &voutdev->regset);
-}
+       debugfs_create_regset32("registers", 0444, entry, &voutdev->regset);
 #endif
+}
 
 struct mgb4_vout_dev *mgb4_vout_create(struct mgb4_dev *mgbdev, int id)
 {
                goto err_video_dev;
        }
 
-#ifdef CONFIG_DEBUG_FS
-       debugfs_init(voutdev);
-#endif
+       create_debugfs(voutdev);
 
        return voutdev;
 
 
        free_irq(irq, voutdev);
 
-#ifdef CONFIG_DEBUG_FS
-       debugfs_remove_recursive(voutdev->debugfs);
-#endif
-
        groups = MGB4_IS_GMSL(voutdev->mgbdev)
          ? mgb4_gmsl_out_groups : mgb4_fpdl3_out_groups;
        device_remove_groups(&voutdev->vdev.dev, groups);
 
        const struct mgb4_vout_config *config;
 
 #ifdef CONFIG_DEBUG_FS
-       struct dentry *debugfs;
        struct debugfs_regset32 regset;
        struct debugfs_reg32 regs[sizeof(struct mgb4_vout_regs) / 4];
 #endif