Commit 084181fe authored by Alan Tull's avatar Alan Tull Committed by Greg Kroah-Hartman

fpga: mgr: add devm_fpga_mgr_create

Add devm_fpga_mgr_create() which is the managed
version of fpga_mgr_create().

Change current FPGA manager drivers to use
devm_fpga_mgr_create()
Signed-off-by: default avatarAlan Tull <atull@kernel.org>
Suggested-by: default avatarFederico Vaga <federico.vaga@cern.ch>
Acked-by: default avatarMoritz Fischer <mdf@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 1c87dc89
...@@ -49,18 +49,14 @@ probe function calls fpga_mgr_register(), such as:: ...@@ -49,18 +49,14 @@ probe function calls fpga_mgr_register(), such as::
* them in priv * them in priv
*/ */
mgr = fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager", mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
&socfpga_fpga_ops, priv); &socfpga_fpga_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
platform_set_drvdata(pdev, mgr); platform_set_drvdata(pdev, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int socfpga_fpga_remove(struct platform_device *pdev) static int socfpga_fpga_remove(struct platform_device *pdev)
...@@ -169,6 +165,9 @@ API for implementing a new FPGA Manager driver ...@@ -169,6 +165,9 @@ API for implementing a new FPGA Manager driver
.. kernel-doc:: include/linux/fpga/fpga-mgr.h .. kernel-doc:: include/linux/fpga/fpga-mgr.h
:functions: fpga_manager_ops :functions: fpga_manager_ops
.. kernel-doc:: drivers/fpga/fpga-mgr.c
:functions: devm_fpga_mgr_create
.. kernel-doc:: drivers/fpga/fpga-mgr.c .. kernel-doc:: drivers/fpga/fpga-mgr.c
:functions: fpga_mgr_create :functions: fpga_mgr_create
......
...@@ -453,8 +453,8 @@ static int altera_cvp_probe(struct pci_dev *pdev, ...@@ -453,8 +453,8 @@ static int altera_cvp_probe(struct pci_dev *pdev,
snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s", snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
ALTERA_CVP_MGR_NAME, pci_name(pdev)); ALTERA_CVP_MGR_NAME, pci_name(pdev));
mgr = fpga_mgr_create(&pdev->dev, conf->mgr_name, mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
&altera_cvp_ops, conf); &altera_cvp_ops, conf);
if (!mgr) { if (!mgr) {
ret = -ENOMEM; ret = -ENOMEM;
goto err_unmap; goto err_unmap;
...@@ -463,10 +463,8 @@ static int altera_cvp_probe(struct pci_dev *pdev, ...@@ -463,10 +463,8 @@ static int altera_cvp_probe(struct pci_dev *pdev,
pci_set_drvdata(pdev, mgr); pci_set_drvdata(pdev, mgr);
ret = fpga_mgr_register(mgr); ret = fpga_mgr_register(mgr);
if (ret) { if (ret)
fpga_mgr_free(mgr);
goto err_unmap; goto err_unmap;
}
ret = driver_create_file(&altera_cvp_driver.driver, ret = driver_create_file(&altera_cvp_driver.driver,
&driver_attr_chkcfg); &driver_attr_chkcfg);
......
...@@ -177,7 +177,6 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base) ...@@ -177,7 +177,6 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
{ {
struct alt_pr_priv *priv; struct alt_pr_priv *priv;
struct fpga_manager *mgr; struct fpga_manager *mgr;
int ret;
u32 val; u32 val;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
...@@ -192,17 +191,13 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base) ...@@ -192,17 +191,13 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT, (val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
(int)(val & ALT_PR_CSR_PR_START)); (int)(val & ALT_PR_CSR_PR_START));
mgr = fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv); mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
dev_set_drvdata(dev, mgr); dev_set_drvdata(dev, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
EXPORT_SYMBOL_GPL(alt_pr_register); EXPORT_SYMBOL_GPL(alt_pr_register);
......
...@@ -239,7 +239,6 @@ static int altera_ps_probe(struct spi_device *spi) ...@@ -239,7 +239,6 @@ static int altera_ps_probe(struct spi_device *spi)
struct altera_ps_conf *conf; struct altera_ps_conf *conf;
const struct of_device_id *of_id; const struct of_device_id *of_id;
struct fpga_manager *mgr; struct fpga_manager *mgr;
int ret;
conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL); conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
if (!conf) if (!conf)
...@@ -275,18 +274,14 @@ static int altera_ps_probe(struct spi_device *spi) ...@@ -275,18 +274,14 @@ static int altera_ps_probe(struct spi_device *spi)
snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s", snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
dev_driver_string(&spi->dev), dev_name(&spi->dev)); dev_driver_string(&spi->dev), dev_name(&spi->dev));
mgr = fpga_mgr_create(&spi->dev, conf->mgr_name, mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
&altera_ps_ops, conf); &altera_ps_ops, conf);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
spi_set_drvdata(spi, mgr); spi_set_drvdata(spi, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int altera_ps_remove(struct spi_device *spi) static int altera_ps_remove(struct spi_device *spi)
......
...@@ -287,7 +287,6 @@ static int fme_mgr_probe(struct platform_device *pdev) ...@@ -287,7 +287,6 @@ static int fme_mgr_probe(struct platform_device *pdev)
struct fme_mgr_priv *priv; struct fme_mgr_priv *priv;
struct fpga_manager *mgr; struct fpga_manager *mgr;
struct resource *res; struct resource *res;
int ret;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv) if (!priv)
...@@ -309,19 +308,15 @@ static int fme_mgr_probe(struct platform_device *pdev) ...@@ -309,19 +308,15 @@ static int fme_mgr_probe(struct platform_device *pdev)
fme_mgr_get_compat_id(priv->ioaddr, compat_id); fme_mgr_get_compat_id(priv->ioaddr, compat_id);
mgr = fpga_mgr_create(dev, "DFL FME FPGA Manager", mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
&fme_mgr_ops, priv); &fme_mgr_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
mgr->compat_id = compat_id; mgr->compat_id = compat_id;
platform_set_drvdata(pdev, mgr); platform_set_drvdata(pdev, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int fme_mgr_remove(struct platform_device *pdev) static int fme_mgr_remove(struct platform_device *pdev)
......
...@@ -558,6 +558,9 @@ EXPORT_SYMBOL_GPL(fpga_mgr_unlock); ...@@ -558,6 +558,9 @@ EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
* @mops: pointer to structure of fpga manager ops * @mops: pointer to structure of fpga manager ops
* @priv: fpga manager private data * @priv: fpga manager private data
* *
* The caller of this function is responsible for freeing the struct with
* fpga_mgr_free(). Using devm_fpga_mgr_create() instead is recommended.
*
* Return: pointer to struct fpga_manager or NULL * Return: pointer to struct fpga_manager or NULL
*/ */
struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name, struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
...@@ -618,8 +621,8 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name, ...@@ -618,8 +621,8 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
EXPORT_SYMBOL_GPL(fpga_mgr_create); EXPORT_SYMBOL_GPL(fpga_mgr_create);
/** /**
* fpga_mgr_free - deallocate a FPGA manager * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
* @mgr: fpga manager struct created by fpga_mgr_create * @mgr: fpga manager struct
*/ */
void fpga_mgr_free(struct fpga_manager *mgr) void fpga_mgr_free(struct fpga_manager *mgr)
{ {
...@@ -628,9 +631,55 @@ void fpga_mgr_free(struct fpga_manager *mgr) ...@@ -628,9 +631,55 @@ void fpga_mgr_free(struct fpga_manager *mgr)
} }
EXPORT_SYMBOL_GPL(fpga_mgr_free); EXPORT_SYMBOL_GPL(fpga_mgr_free);
static void devm_fpga_mgr_release(struct device *dev, void *res)
{
struct fpga_manager *mgr = *(struct fpga_manager **)res;
fpga_mgr_free(mgr);
}
/**
* devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
* @dev: fpga manager device from pdev
* @name: fpga manager name
* @mops: pointer to structure of fpga manager ops
* @priv: fpga manager private data
*
* This function is intended for use in a FPGA manager driver's probe function.
* After the manager driver creates the manager struct with
* devm_fpga_mgr_create(), it should register it with fpga_mgr_register(). The
* manager driver's remove function should call fpga_mgr_unregister(). The
* manager struct allocated with this function will be freed automatically on
* driver detach. This includes the case of a probe function returning error
* before calling fpga_mgr_register(), the struct will still get cleaned up.
*
* Return: pointer to struct fpga_manager or NULL
*/
struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
const struct fpga_manager_ops *mops,
void *priv)
{
struct fpga_manager **ptr, *mgr;
ptr = devres_alloc(devm_fpga_mgr_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return NULL;
mgr = fpga_mgr_create(dev, name, mops, priv);
if (!mgr) {
devres_free(ptr);
} else {
*ptr = mgr;
devres_add(dev, ptr);
}
return mgr;
}
EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
/** /**
* fpga_mgr_register - register a FPGA manager * fpga_mgr_register - register a FPGA manager
* @mgr: fpga manager struct created by fpga_mgr_create * @mgr: fpga manager struct
* *
* Return: 0 on success, negative error code otherwise. * Return: 0 on success, negative error code otherwise.
*/ */
...@@ -661,8 +710,10 @@ int fpga_mgr_register(struct fpga_manager *mgr) ...@@ -661,8 +710,10 @@ int fpga_mgr_register(struct fpga_manager *mgr)
EXPORT_SYMBOL_GPL(fpga_mgr_register); EXPORT_SYMBOL_GPL(fpga_mgr_register);
/** /**
* fpga_mgr_unregister - unregister and free a FPGA manager * fpga_mgr_unregister - unregister a FPGA manager
* @mgr: fpga manager struct * @mgr: fpga manager struct
*
* This function is intended for use in a FPGA manager driver's remove function.
*/ */
void fpga_mgr_unregister(struct fpga_manager *mgr) void fpga_mgr_unregister(struct fpga_manager *mgr)
{ {
...@@ -681,9 +732,6 @@ EXPORT_SYMBOL_GPL(fpga_mgr_unregister); ...@@ -681,9 +732,6 @@ EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
static void fpga_mgr_dev_release(struct device *dev) static void fpga_mgr_dev_release(struct device *dev)
{ {
struct fpga_manager *mgr = to_fpga_manager(dev);
fpga_mgr_free(mgr);
} }
static int __init fpga_mgr_class_init(void) static int __init fpga_mgr_class_init(void)
......
...@@ -175,18 +175,14 @@ static int ice40_fpga_probe(struct spi_device *spi) ...@@ -175,18 +175,14 @@ static int ice40_fpga_probe(struct spi_device *spi)
return ret; return ret;
} }
mgr = fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager", mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
&ice40_fpga_ops, priv); &ice40_fpga_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
spi_set_drvdata(spi, mgr); spi_set_drvdata(spi, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int ice40_fpga_remove(struct spi_device *spi) static int ice40_fpga_remove(struct spi_device *spi)
......
...@@ -356,25 +356,20 @@ static int machxo2_spi_probe(struct spi_device *spi) ...@@ -356,25 +356,20 @@ static int machxo2_spi_probe(struct spi_device *spi)
{ {
struct device *dev = &spi->dev; struct device *dev = &spi->dev;
struct fpga_manager *mgr; struct fpga_manager *mgr;
int ret;
if (spi->max_speed_hz > MACHXO2_MAX_SPEED) { if (spi->max_speed_hz > MACHXO2_MAX_SPEED) {
dev_err(dev, "Speed is too high\n"); dev_err(dev, "Speed is too high\n");
return -EINVAL; return -EINVAL;
} }
mgr = fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager", mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
&machxo2_ops, spi); &machxo2_ops, spi);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
spi_set_drvdata(spi, mgr); spi_set_drvdata(spi, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int machxo2_spi_remove(struct spi_device *spi) static int machxo2_spi_remove(struct spi_device *spi)
......
...@@ -508,8 +508,8 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev) ...@@ -508,8 +508,8 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
return -EBUSY; return -EBUSY;
} }
mgr = fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager", mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
&socfpga_a10_fpga_mgr_ops, priv); &socfpga_a10_fpga_mgr_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
...@@ -517,7 +517,6 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev) ...@@ -517,7 +517,6 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
ret = fpga_mgr_register(mgr); ret = fpga_mgr_register(mgr);
if (ret) { if (ret) {
fpga_mgr_free(mgr);
clk_disable_unprepare(priv->clk); clk_disable_unprepare(priv->clk);
return ret; return ret;
} }
......
...@@ -571,18 +571,14 @@ static int socfpga_fpga_probe(struct platform_device *pdev) ...@@ -571,18 +571,14 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
if (ret) if (ret)
return ret; return ret;
mgr = fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager", mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
&socfpga_fpga_ops, priv); &socfpga_fpga_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
platform_set_drvdata(pdev, mgr); platform_set_drvdata(pdev, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int socfpga_fpga_remove(struct platform_device *pdev) static int socfpga_fpga_remove(struct platform_device *pdev)
......
...@@ -118,7 +118,6 @@ static int ts73xx_fpga_probe(struct platform_device *pdev) ...@@ -118,7 +118,6 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
struct ts73xx_fpga_priv *priv; struct ts73xx_fpga_priv *priv;
struct fpga_manager *mgr; struct fpga_manager *mgr;
struct resource *res; struct resource *res;
int ret;
priv = devm_kzalloc(kdev, sizeof(*priv), GFP_KERNEL); priv = devm_kzalloc(kdev, sizeof(*priv), GFP_KERNEL);
if (!priv) if (!priv)
...@@ -133,18 +132,14 @@ static int ts73xx_fpga_probe(struct platform_device *pdev) ...@@ -133,18 +132,14 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
return PTR_ERR(priv->io_base); return PTR_ERR(priv->io_base);
} }
mgr = fpga_mgr_create(kdev, "TS-73xx FPGA Manager", mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
&ts73xx_fpga_ops, priv); &ts73xx_fpga_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
platform_set_drvdata(pdev, mgr); platform_set_drvdata(pdev, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int ts73xx_fpga_remove(struct platform_device *pdev) static int ts73xx_fpga_remove(struct platform_device *pdev)
......
...@@ -144,7 +144,6 @@ static int xilinx_spi_probe(struct spi_device *spi) ...@@ -144,7 +144,6 @@ static int xilinx_spi_probe(struct spi_device *spi)
{ {
struct xilinx_spi_conf *conf; struct xilinx_spi_conf *conf;
struct fpga_manager *mgr; struct fpga_manager *mgr;
int ret;
conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL); conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
if (!conf) if (!conf)
...@@ -167,18 +166,15 @@ static int xilinx_spi_probe(struct spi_device *spi) ...@@ -167,18 +166,15 @@ static int xilinx_spi_probe(struct spi_device *spi)
return PTR_ERR(conf->done); return PTR_ERR(conf->done);
} }
mgr = fpga_mgr_create(&spi->dev, "Xilinx Slave Serial FPGA Manager", mgr = devm_fpga_mgr_create(&spi->dev,
&xilinx_spi_ops, conf); "Xilinx Slave Serial FPGA Manager",
&xilinx_spi_ops, conf);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
spi_set_drvdata(spi, mgr); spi_set_drvdata(spi, mgr);
ret = fpga_mgr_register(mgr); return fpga_mgr_register(mgr);
if (ret)
fpga_mgr_free(mgr);
return ret;
} }
static int xilinx_spi_remove(struct spi_device *spi) static int xilinx_spi_remove(struct spi_device *spi)
......
...@@ -614,8 +614,8 @@ static int zynq_fpga_probe(struct platform_device *pdev) ...@@ -614,8 +614,8 @@ static int zynq_fpga_probe(struct platform_device *pdev)
clk_disable(priv->clk); clk_disable(priv->clk);
mgr = fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager", mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
&zynq_fpga_ops, priv); &zynq_fpga_ops, priv);
if (!mgr) if (!mgr)
return -ENOMEM; return -ENOMEM;
...@@ -624,7 +624,6 @@ static int zynq_fpga_probe(struct platform_device *pdev) ...@@ -624,7 +624,6 @@ static int zynq_fpga_probe(struct platform_device *pdev)
err = fpga_mgr_register(mgr); err = fpga_mgr_register(mgr);
if (err) { if (err) {
dev_err(dev, "unable to register FPGA manager\n"); dev_err(dev, "unable to register FPGA manager\n");
fpga_mgr_free(mgr);
clk_unprepare(priv->clk); clk_unprepare(priv->clk);
return err; return err;
} }
......
...@@ -198,4 +198,8 @@ void fpga_mgr_free(struct fpga_manager *mgr); ...@@ -198,4 +198,8 @@ void fpga_mgr_free(struct fpga_manager *mgr);
int fpga_mgr_register(struct fpga_manager *mgr); int fpga_mgr_register(struct fpga_manager *mgr);
void fpga_mgr_unregister(struct fpga_manager *mgr); void fpga_mgr_unregister(struct fpga_manager *mgr);
struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
const struct fpga_manager_ops *mops,
void *priv);
#endif /*_LINUX_FPGA_MGR_H */ #endif /*_LINUX_FPGA_MGR_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment