blob: d4510f5ecff91d11da130970ee5afe7342c6d0a8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#include <linux/module.h>
2#include <linux/kernel.h>
3#include <linux/clk-provider.h>
4#include <linux/clk.h>
5#include <linux/mutex.h>
6
7struct asr_crypto {
8 struct clk *aes_clk;
9 refcount_t refcount;
10 struct mutex clk_lock;
11};
12
13static struct asr_crypto asr_crypto = {
14 .aes_clk = 0,
15 .refcount = REFCOUNT_INIT(0),
16 .clk_lock = __MUTEX_INITIALIZER(asr_crypto.clk_lock),
17};
18
19int asr_aes_clk_get(struct clk *aes_clk)
20{
21 mutex_lock(&asr_crypto.clk_lock);
22
23 if (asr_crypto.aes_clk != aes_clk) {
24 asr_crypto.aes_clk = aes_clk;
25 }
26
27 if (refcount_read(&asr_crypto.refcount) == 0) {
28 clk_enable(asr_crypto.aes_clk);
29 refcount_set(&asr_crypto.refcount, 1);
30 } else {
31 refcount_inc(&asr_crypto.refcount);
32 }
33
34 mutex_unlock(&asr_crypto.clk_lock);
35 return 0;
36}
37EXPORT_SYMBOL_GPL(asr_aes_clk_get);
38
39int asr_aes_clk_put(struct clk *aes_clk)
40{
41 mutex_lock(&asr_crypto.clk_lock);
42
43 if (refcount_dec_and_test(&asr_crypto.refcount)) {
44 clk_disable(asr_crypto.aes_clk);
45 }
46
47 mutex_unlock(&asr_crypto.clk_lock);
48 return 0;
49}
50
51EXPORT_SYMBOL_GPL(asr_aes_clk_put);