b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #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 | |
| 7 | struct asr_crypto { |
| 8 | struct clk *aes_clk; |
| 9 | refcount_t refcount; |
| 10 | struct mutex clk_lock; |
| 11 | }; |
| 12 | |
| 13 | static 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 | |
| 19 | int 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 | } |
| 37 | EXPORT_SYMBOL_GPL(asr_aes_clk_get); |
| 38 | |
| 39 | int 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 | |
| 51 | EXPORT_SYMBOL_GPL(asr_aes_clk_put); |