Skip to content

Commit

Permalink
hwrng: atmel - add runtime pm support
Browse files Browse the repository at this point in the history
Add runtime PM support.

Signed-off-by: Claudiu Beznea <claudiu.beznea@microchip.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
Claudiu Beznea authored and Herbert Xu committed Mar 2, 2022
1 parent b953188 commit c4f51ea
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions drivers/char/hw_random/atmel-rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <linux/hw_random.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

#define TRNG_CR 0x00
#define TRNG_MR 0x04
Expand Down Expand Up @@ -58,6 +59,12 @@ static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
u32 *data = buf;
int ret;

ret = pm_runtime_get_sync((struct device *)trng->rng.priv);
if (ret < 0) {
pm_runtime_put_sync((struct device *)trng->rng.priv);
return ret;
}

ret = atmel_trng_wait_ready(trng, wait);
if (!ret)
goto out;
Expand All @@ -72,6 +79,8 @@ static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
ret = 4;

out:
pm_runtime_mark_last_busy((struct device *)trng->rng.priv);
pm_runtime_put_sync_autosuspend((struct device *)trng->rng.priv);
return ret;
}

Expand Down Expand Up @@ -127,21 +136,28 @@ static int atmel_trng_probe(struct platform_device *pdev)
trng->has_half_rate = data->has_half_rate;
trng->rng.name = pdev->name;
trng->rng.read = atmel_trng_read;
trng->rng.priv = (unsigned long)&pdev->dev;
platform_set_drvdata(pdev, trng);

#ifndef CONFIG_PM
ret = atmel_trng_init(trng);
if (ret)
return ret;
#endif

ret = devm_hwrng_register(&pdev->dev, &trng->rng);
if (ret)
goto err_register;

platform_set_drvdata(pdev, trng);
pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
pm_runtime_use_autosuspend(&pdev->dev);
pm_runtime_enable(&pdev->dev);

return 0;
ret = devm_hwrng_register(&pdev->dev, &trng->rng);
if (ret) {
pm_runtime_disable(&pdev->dev);
pm_runtime_set_suspended(&pdev->dev);
#ifndef CONFIG_PM
atmel_trng_cleanup(trng);
#endif
}

err_register:
atmel_trng_cleanup(trng);
return ret;
}

Expand All @@ -151,11 +167,13 @@ static int atmel_trng_remove(struct platform_device *pdev)


atmel_trng_cleanup(trng);
pm_runtime_disable(&pdev->dev);
pm_runtime_set_suspended(&pdev->dev);

return 0;
}

static int __maybe_unused atmel_trng_suspend(struct device *dev)
static int __maybe_unused atmel_trng_runtime_suspend(struct device *dev)
{
struct atmel_trng *trng = dev_get_drvdata(dev);

Expand All @@ -164,16 +182,18 @@ static int __maybe_unused atmel_trng_suspend(struct device *dev)
return 0;
}

static int __maybe_unused atmel_trng_resume(struct device *dev)
static int __maybe_unused atmel_trng_runtime_resume(struct device *dev)
{
struct atmel_trng *trng = dev_get_drvdata(dev);

return atmel_trng_init(trng);
}

static const struct dev_pm_ops __maybe_unused atmel_trng_pm_ops = {
.suspend = atmel_trng_suspend,
.resume = atmel_trng_resume,
SET_RUNTIME_PM_OPS(atmel_trng_runtime_suspend,
atmel_trng_runtime_resume, NULL)
SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
pm_runtime_force_resume)
};

static const struct atmel_trng_data at91sam9g45_config = {
Expand Down

0 comments on commit c4f51ea

Please sign in to comment.