imxmd->m2m_vdev = imx_media_csc_scaler_device_init(imxmd);
        if (IS_ERR(imxmd->m2m_vdev)) {
                ret = PTR_ERR(imxmd->m2m_vdev);
+               imxmd->m2m_vdev = NULL;
                goto unlock;
        }
 
 
        v4l2_info(&imxmd->v4l2_dev, "Removing imx-media\n");
 
+       if (imxmd->m2m_vdev) {
+               imx_media_csc_scaler_device_unregister(imxmd->m2m_vdev);
+               imxmd->m2m_vdev = NULL;
+       }
+
        v4l2_async_notifier_unregister(&imxmd->notifier);
        imx_media_unregister_ipu_internal_subdevs(imxmd);
        v4l2_async_notifier_cleanup(&imxmd->notifier);
-       imx_media_csc_scaler_device_unregister(imxmd->m2m_vdev);
        media_device_unregister(&imxmd->md);
        v4l2_device_unregister(&imxmd->v4l2_dev);
        media_device_cleanup(&imxmd->md);