blob: be5d5cad79d76895d9388ae40c0a428eb9302798 [file] [log] [blame]
Aditya Bavanaribb981b72020-03-19 18:30:10 +05301/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/clk.h>
7#include <linux/clk-provider.h>
8#include <linux/ratelimit.h>
9#include <dsp/digital-cdc-rsc-mgr.h>
10
11struct mutex hw_vote_lock;
12static bool is_init_done;
13
14/**
15 * digital_cdc_rsc_mgr_hw_vote_enable - Enables hw vote in DSP
16 *
17 * @vote_handle: vote handle for which voting needs to be done
18 *
19 * Returns 0 on success or -EINVAL/error code on failure
20 */
21int digital_cdc_rsc_mgr_hw_vote_enable(struct clk* vote_handle)
22{
23 int ret = 0;
24
25 if (!is_init_done || vote_handle == NULL) {
26 pr_err_ratelimited("%s: init failed or vote handle NULL\n",
27 __func__);
28 return -EINVAL;
29 }
30
31 mutex_lock(&hw_vote_lock);
32 ret = clk_prepare_enable(vote_handle);
33 mutex_unlock(&hw_vote_lock);
34
35 pr_debug("%s: return %d\n", __func__, ret);
36 trace_printk("%s: return %d\n", __func__, ret);
37 return ret;
38}
39EXPORT_SYMBOL(digital_cdc_rsc_mgr_hw_vote_enable);
40
41/**
42 * digital_cdc_rsc_mgr_hw_vote_disable - Disables hw vote in DSP
43 *
44 * @vote_handle: vote handle for which voting needs to be disabled
45 *
46 */
47void digital_cdc_rsc_mgr_hw_vote_disable(struct clk* vote_handle)
48{
49 if (!is_init_done || vote_handle == NULL) {
50 pr_err_ratelimited("%s: init failed or vote handle NULL\n",
51 __func__);
52 return;
53 }
54
55 mutex_lock(&hw_vote_lock);
56 clk_disable_unprepare(vote_handle);
57 mutex_unlock(&hw_vote_lock);
58 trace_printk("%s\n", __func__);
59}
60EXPORT_SYMBOL(digital_cdc_rsc_mgr_hw_vote_disable);
61
62/**
63 * digital_cdc_rsc_mgr_hw_vote_reset - Resets hw vote count
64 *
65 */
66void digital_cdc_rsc_mgr_hw_vote_reset(struct clk* vote_handle)
67{
68 int count = 0;
69
70 if (!is_init_done || vote_handle == NULL) {
71 pr_err_ratelimited("%s: init failed or vote handle NULL\n",
72 __func__);
73 return;
74 }
75
76 mutex_lock(&hw_vote_lock);
77 while (__clk_is_enabled(vote_handle)) {
78 clk_disable_unprepare(vote_handle);
79 count++;
80 }
81 pr_debug("%s: Vote count after SSR: %d\n", __func__, count);
82 trace_printk("%s: Vote count after SSR: %d\n", __func__, count);
83
84 while (count--)
85 clk_prepare_enable(vote_handle);
86 mutex_unlock(&hw_vote_lock);
87}
88EXPORT_SYMBOL(digital_cdc_rsc_mgr_hw_vote_reset);
89
Aditya Bavanari73438d12020-04-27 13:02:03 +053090void digital_cdc_rsc_mgr_init(void)
Aditya Bavanaribb981b72020-03-19 18:30:10 +053091{
92 mutex_init(&hw_vote_lock);
93 is_init_done = true;
94}
95
Aditya Bavanari73438d12020-04-27 13:02:03 +053096void digital_cdc_rsc_mgr_exit(void)
Aditya Bavanaribb981b72020-03-19 18:30:10 +053097{
98 mutex_destroy(&hw_vote_lock);
99 is_init_done = false;
100}