Drivers such as intel_pmc_ipc.c can be unloaded as well so in order to support those in this driver add a new function that can be called to unregister the SCU IPC when it is not needed anymore. We also add a managed version of the intel_scu_ipc_register() that takes care of calling intel_scu_ipc_unregister() automatically when the driver is unbound. Signed-off-by: Mika Westerberg <mika.westerberg@xxxxxxxxxxxxxxx> Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> --- arch/x86/include/asm/intel_scu_ipc.h | 3 ++ drivers/platform/x86/intel_scu_ipc.c | 63 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/arch/x86/include/asm/intel_scu_ipc.h b/arch/x86/include/asm/intel_scu_ipc.h index ce2ad516f0f9..23990b08c913 100644 --- a/arch/x86/include/asm/intel_scu_ipc.h +++ b/arch/x86/include/asm/intel_scu_ipc.h @@ -34,6 +34,9 @@ struct intel_scu_ipc_pdata { struct intel_scu_ipc_dev * intel_scu_ipc_register(struct device *dev, const struct intel_scu_ipc_pdata *pdata); +void intel_scu_ipc_unregister(struct intel_scu_ipc_dev *scu); +struct intel_scu_ipc_dev * +devm_intel_scu_ipc_register(struct device *dev, const struct intel_scu_ipc_pdata *pdata); struct intel_scu_ipc_dev *intel_scu_ipc_dev_get(void); void intel_scu_ipc_dev_put(struct intel_scu_ipc_dev *scu); diff --git a/drivers/platform/x86/intel_scu_ipc.c b/drivers/platform/x86/intel_scu_ipc.c index 391b65a4789e..3df2efa19d77 100644 --- a/drivers/platform/x86/intel_scu_ipc.c +++ b/drivers/platform/x86/intel_scu_ipc.c @@ -584,3 +584,66 @@ intel_scu_ipc_register(struct device *dev, const struct intel_scu_ipc_pdata *pda return ERR_PTR(err); } EXPORT_SYMBOL_GPL(intel_scu_ipc_register); + +/** + * intel_scu_ipc_unregister() - Unregister SCU IPC + * @scu: SCU IPC handle + * + * This unregisters the SCU IPC device and releases the acquired + * resources. + */ +void intel_scu_ipc_unregister(struct intel_scu_ipc_dev *scu) +{ + mutex_lock(&ipclock); + if (!WARN_ON(!scu->dev)) { + if (scu->irq > 0) + free_irq(scu->irq, scu); + iounmap(scu->ipc_base); + release_mem_region(scu->mem.start, resource_size(&scu->mem)); + scu->dev = NULL; + } + mutex_unlock(&ipclock); +} +EXPORT_SYMBOL_GPL(intel_scu_ipc_unregister); + +static void devm_intel_scu_ipc_unregister(struct device *dev, void *res) +{ + struct intel_scu_ipc_devres *devres = res; + struct intel_scu_ipc_dev *scu = devres->scu; + + intel_scu_ipc_unregister(scu); +} + +/** + * devm_intel_scu_ipc_register() - Register managed SCU IPC device + * @dev: SCU device + * @pdata: Platform specific data + * + * Call this function to initialize managed SCU IPC mechanism for @dev. + * Returns SCU IPC instance or ERR_PTR() if there was an error. The caller + * may use the returned instance if it needs to do SCU IPC calls itself. + */ +struct intel_scu_ipc_dev * +devm_intel_scu_ipc_register(struct device *dev, + const struct intel_scu_ipc_pdata *pdata) +{ + struct intel_scu_ipc_devres *devres; + struct intel_scu_ipc_dev *scu; + + devres = devres_alloc(devm_intel_scu_ipc_unregister, sizeof(*devres), + GFP_KERNEL); + if (!devres) + return NULL; + + scu = intel_scu_ipc_register(dev, pdata); + if (IS_ERR(scu)) { + devres_free(devres); + return scu; + } + + devres->scu = scu; + devres_add(dev, devres); + + return scu; +} +EXPORT_SYMBOL_GPL(devm_intel_scu_ipc_register); -- 2.24.1