Simplify the ingenic-trng driver a bit. Switch to devm_clk_get_enabled and
devm_hwrng_register. Remove some unused code.
The changes were compile-tested only (I don't have this hardware).
Martin Kaiser (7):
hwrng: ingenic - enable compile testing
hwrng: ingenic - remove two unused defines
hwrng: ingenic - remove dead assignments
hwrng: ingenic - use devm_clk_get_enabled
hwrng: ingenic - use dev_err_probe in error paths
hwrng: ingenic - don't disable the rng in ingenic_trng_remove
hwrng: ingenic - switch to device managed registration
drivers/char/hw_random/Kconfig | 3 +-
drivers/char/hw_random/ingenic-trng.c | 57 ++++++---------------------
2 files changed, 12 insertions(+), 48 deletions(-)
--
2.30.2
Use devm_clk_get_enabled in the ingenic-trng driver. We don't have to
disable and unprepare the clock any more in error paths or in the remove
function.
Signed-off-by: Martin Kaiser <[email protected]>
---
drivers/char/hw_random/ingenic-trng.c | 24 ++++++------------------
1 file changed, 6 insertions(+), 18 deletions(-)
diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c
index bfec28ceab00..906fa78de47e 100644
--- a/drivers/char/hw_random/ingenic-trng.c
+++ b/drivers/char/hw_random/ingenic-trng.c
@@ -29,7 +29,6 @@
struct ingenic_trng {
void __iomem *base;
- struct clk *clk;
struct hwrng rng;
};
@@ -77,6 +76,7 @@ static int ingenic_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait
static int ingenic_trng_probe(struct platform_device *pdev)
{
struct ingenic_trng *trng;
+ struct clk *clk;
int ret;
trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
@@ -89,16 +89,10 @@ static int ingenic_trng_probe(struct platform_device *pdev)
return PTR_ERR(trng->base);
}
- trng->clk = devm_clk_get(&pdev->dev, NULL);
- if (IS_ERR(trng->clk)) {
- pr_crit("%s: Cannot get DTRNG clock\n", __func__);
- return PTR_ERR(trng->clk);
- }
-
- ret = clk_prepare_enable(trng->clk);
- if (ret) {
- pr_crit("%s: Unable to enable DTRNG clock\n", __func__);
- return ret;
+ clk = devm_clk_get_enabled(&pdev->dev, NULL);
+ if (IS_ERR(clk)) {
+ pr_crit("%s: Cannot get and enable DTRNG clock\n", __func__);
+ return PTR_ERR(clk);
}
trng->rng.name = pdev->name;
@@ -109,17 +103,13 @@ static int ingenic_trng_probe(struct platform_device *pdev)
ret = hwrng_register(&trng->rng);
if (ret) {
dev_err(&pdev->dev, "Failed to register hwrng\n");
- goto err_unprepare_clk;
+ return ret;
}
platform_set_drvdata(pdev, trng);
dev_info(&pdev->dev, "Ingenic DTRNG driver registered\n");
return 0;
-
-err_unprepare_clk:
- clk_disable_unprepare(trng->clk);
- return ret;
}
static int ingenic_trng_remove(struct platform_device *pdev)
@@ -133,8 +123,6 @@ static int ingenic_trng_remove(struct platform_device *pdev)
ctrl &= ~CFG_GEN_EN;
writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
- clk_disable_unprepare(trng->clk);
-
return 0;
}
--
2.30.2
Don't assign a value to ret if we're about to return from the probe
function and ret's value is not used.
Signed-off-by: Martin Kaiser <[email protected]>
---
drivers/char/hw_random/ingenic-trng.c | 2 --
1 file changed, 2 deletions(-)
diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c
index 64286cbdf158..bfec28ceab00 100644
--- a/drivers/char/hw_random/ingenic-trng.c
+++ b/drivers/char/hw_random/ingenic-trng.c
@@ -86,13 +86,11 @@ static int ingenic_trng_probe(struct platform_device *pdev)
trng->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(trng->base)) {
pr_err("%s: Failed to map DTRNG registers\n", __func__);
- ret = PTR_ERR(trng->base);
return PTR_ERR(trng->base);
}
trng->clk = devm_clk_get(&pdev->dev, NULL);
if (IS_ERR(trng->clk)) {
- ret = PTR_ERR(trng->clk);
pr_crit("%s: Cannot get DTRNG clock\n", __func__);
return PTR_ERR(trng->clk);
}
--
2.30.2
Call devm_hwrng_register for device managed registration of the
ingenic-trng driver. ingenic_trng_remove can then be deleted.
Signed-off-by: Martin Kaiser <[email protected]>
---
drivers/char/hw_random/ingenic-trng.c | 12 +-----------
1 file changed, 1 insertion(+), 11 deletions(-)
diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c
index 9c54721f8399..3967a8dbe967 100644
--- a/drivers/char/hw_random/ingenic-trng.c
+++ b/drivers/char/hw_random/ingenic-trng.c
@@ -98,7 +98,7 @@ static int ingenic_trng_probe(struct platform_device *pdev)
trng->rng.cleanup = ingenic_trng_cleanup;
trng->rng.read = ingenic_trng_read;
- ret = hwrng_register(&trng->rng);
+ ret = devm_hwrng_register(&pdev->dev, &trng->rng);
if (ret)
return dev_err_probe(&pdev->dev, ret, "Failed to register hwrng\n");
@@ -108,15 +108,6 @@ static int ingenic_trng_probe(struct platform_device *pdev)
return 0;
}
-static int ingenic_trng_remove(struct platform_device *pdev)
-{
- struct ingenic_trng *trng = platform_get_drvdata(pdev);
-
- hwrng_unregister(&trng->rng);
-
- return 0;
-}
-
static const struct of_device_id ingenic_trng_of_match[] = {
{ .compatible = "ingenic,x1830-dtrng" },
{ /* sentinel */ }
@@ -125,7 +116,6 @@ MODULE_DEVICE_TABLE(of, ingenic_trng_of_match);
static struct platform_driver ingenic_trng_driver = {
.probe = ingenic_trng_probe,
- .remove = ingenic_trng_remove,
.driver = {
.name = "ingenic-trng",
.of_match_table = ingenic_trng_of_match,
--
2.30.2
Use dev_err_probe in error paths to make the code a bit shorter.
Signed-off-by: Martin Kaiser <[email protected]>
---
drivers/char/hw_random/ingenic-trng.c | 20 ++++++++------------
1 file changed, 8 insertions(+), 12 deletions(-)
diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c
index 906fa78de47e..cc88a941c929 100644
--- a/drivers/char/hw_random/ingenic-trng.c
+++ b/drivers/char/hw_random/ingenic-trng.c
@@ -84,16 +84,14 @@ static int ingenic_trng_probe(struct platform_device *pdev)
return -ENOMEM;
trng->base = devm_platform_ioremap_resource(pdev, 0);
- if (IS_ERR(trng->base)) {
- pr_err("%s: Failed to map DTRNG registers\n", __func__);
- return PTR_ERR(trng->base);
- }
+ if (IS_ERR(trng->base))
+ return dev_err_probe(&pdev->dev, PTR_ERR(trng->base),
+ "%s: Failed to map DTRNG registers\n", __func__);
clk = devm_clk_get_enabled(&pdev->dev, NULL);
- if (IS_ERR(clk)) {
- pr_crit("%s: Cannot get and enable DTRNG clock\n", __func__);
- return PTR_ERR(clk);
- }
+ if (IS_ERR(clk))
+ return dev_err_probe(&pdev->dev, PTR_ERR(clk),
+ "%s: Cannot get and enable DTRNG clock\n", __func__);
trng->rng.name = pdev->name;
trng->rng.init = ingenic_trng_init;
@@ -101,10 +99,8 @@ static int ingenic_trng_probe(struct platform_device *pdev)
trng->rng.read = ingenic_trng_read;
ret = hwrng_register(&trng->rng);
- if (ret) {
- dev_err(&pdev->dev, "Failed to register hwrng\n");
- return ret;
- }
+ if (ret)
+ return dev_err_probe(&pdev->dev, ret, "Failed to register hwrng\n");
platform_set_drvdata(pdev, trng);
--
2.30.2
Remove two defines which are not used in the ingenic-trng driver's code.
Signed-off-by: Martin Kaiser <[email protected]>
---
drivers/char/hw_random/ingenic-trng.c | 2 --
1 file changed, 2 deletions(-)
diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c
index 0eb80f786f4d..64286cbdf158 100644
--- a/drivers/char/hw_random/ingenic-trng.c
+++ b/drivers/char/hw_random/ingenic-trng.c
@@ -22,8 +22,6 @@
#define TRNG_REG_STATUS_OFFSET 0x08
/* bits within the CFG register */
-#define CFG_RDY_CLR BIT(12)
-#define CFG_INT_MASK BIT(11)
#define CFG_GEN_EN BIT(0)
/* bits within the STATUS register */
--
2.30.2
There's no need to disable the rng in ingenic_trng_remove.
The driver's init function sets the CFG_GEN_EN bit to enable the
rng. The cleanup function clears CFG_GEN_EN to revert this.
The remove function calls hwrng_unregister. If the ingenic-trng is not
the current rng at this point, CFG_GEN_EN has already been cleared. If
the ingenic-trng is the current rng, drop_current_rng will call the
cleanup function.
Signed-off-by: Martin Kaiser <[email protected]>
---
drivers/char/hw_random/ingenic-trng.c | 5 -----
1 file changed, 5 deletions(-)
diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c
index cc88a941c929..9c54721f8399 100644
--- a/drivers/char/hw_random/ingenic-trng.c
+++ b/drivers/char/hw_random/ingenic-trng.c
@@ -111,14 +111,9 @@ static int ingenic_trng_probe(struct platform_device *pdev)
static int ingenic_trng_remove(struct platform_device *pdev)
{
struct ingenic_trng *trng = platform_get_drvdata(pdev);
- unsigned int ctrl;
hwrng_unregister(&trng->rng);
- ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET);
- ctrl &= ~CFG_GEN_EN;
- writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
-
return 0;
}
--
2.30.2
On Mon, Jul 10, 2023 at 10:27:26PM +0200, Martin Kaiser wrote:
> Simplify the ingenic-trng driver a bit. Switch to devm_clk_get_enabled and
> devm_hwrng_register. Remove some unused code.
>
> The changes were compile-tested only (I don't have this hardware).
>
> Martin Kaiser (7):
> hwrng: ingenic - enable compile testing
> hwrng: ingenic - remove two unused defines
> hwrng: ingenic - remove dead assignments
> hwrng: ingenic - use devm_clk_get_enabled
> hwrng: ingenic - use dev_err_probe in error paths
> hwrng: ingenic - don't disable the rng in ingenic_trng_remove
> hwrng: ingenic - switch to device managed registration
>
> drivers/char/hw_random/Kconfig | 3 +-
> drivers/char/hw_random/ingenic-trng.c | 57 ++++++---------------------
> 2 files changed, 12 insertions(+), 48 deletions(-)
>
> --
> 2.30.2
All applied. Thanks.
--
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt