xref: /rk3399_ARM-atf/drivers/clk/clk.c (revision a2c6016f927e4b9a23499005c63f3e46f48ff8a2)
1847c6bc8SGabriel Fernandez /*
2847c6bc8SGabriel Fernandez  * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
3847c6bc8SGabriel Fernandez  * Author(s): Ludovic Barre, <ludovic.barre@st.com> for STMicroelectronics.
4847c6bc8SGabriel Fernandez  *
5847c6bc8SGabriel Fernandez  * SPDX-License-Identifier: BSD-3-Clause
6847c6bc8SGabriel Fernandez  */
7847c6bc8SGabriel Fernandez 
8847c6bc8SGabriel Fernandez #include <assert.h>
9847c6bc8SGabriel Fernandez #include <errno.h>
10847c6bc8SGabriel Fernandez #include <stdbool.h>
11847c6bc8SGabriel Fernandez 
12847c6bc8SGabriel Fernandez #include <drivers/clk.h>
13847c6bc8SGabriel Fernandez 
14847c6bc8SGabriel Fernandez static const struct clk_ops *ops;
15847c6bc8SGabriel Fernandez 
16847c6bc8SGabriel Fernandez int clk_enable(unsigned long id)
17847c6bc8SGabriel Fernandez {
18847c6bc8SGabriel Fernandez 	assert((ops != NULL) && (ops->enable != NULL));
19847c6bc8SGabriel Fernandez 
20847c6bc8SGabriel Fernandez 	return ops->enable(id);
21847c6bc8SGabriel Fernandez }
22847c6bc8SGabriel Fernandez 
23847c6bc8SGabriel Fernandez void clk_disable(unsigned long id)
24847c6bc8SGabriel Fernandez {
25847c6bc8SGabriel Fernandez 	assert((ops != NULL) && (ops->disable != NULL));
26847c6bc8SGabriel Fernandez 
27847c6bc8SGabriel Fernandez 	ops->disable(id);
28847c6bc8SGabriel Fernandez }
29847c6bc8SGabriel Fernandez 
30847c6bc8SGabriel Fernandez unsigned long clk_get_rate(unsigned long id)
31847c6bc8SGabriel Fernandez {
32847c6bc8SGabriel Fernandez 	assert((ops != NULL) && (ops->get_rate != NULL));
33847c6bc8SGabriel Fernandez 
34847c6bc8SGabriel Fernandez 	return ops->get_rate(id);
35847c6bc8SGabriel Fernandez }
36847c6bc8SGabriel Fernandez 
37847c6bc8SGabriel Fernandez int clk_get_parent(unsigned long id)
38847c6bc8SGabriel Fernandez {
39847c6bc8SGabriel Fernandez 	assert((ops != NULL) && (ops->get_parent != NULL));
40847c6bc8SGabriel Fernandez 
41847c6bc8SGabriel Fernandez 	return ops->get_parent(id);
42847c6bc8SGabriel Fernandez }
43847c6bc8SGabriel Fernandez 
44*a2c6016fSGhennadi Procopciuc int clk_set_parent(unsigned long id, unsigned long parent_id)
45*a2c6016fSGhennadi Procopciuc {
46*a2c6016fSGhennadi Procopciuc 	assert((ops != NULL) && (ops->set_parent != NULL));
47*a2c6016fSGhennadi Procopciuc 
48*a2c6016fSGhennadi Procopciuc 	return ops->set_parent(id, parent_id);
49*a2c6016fSGhennadi Procopciuc }
50*a2c6016fSGhennadi Procopciuc 
51847c6bc8SGabriel Fernandez bool clk_is_enabled(unsigned long id)
52847c6bc8SGabriel Fernandez {
53847c6bc8SGabriel Fernandez 	assert((ops != NULL) && (ops->is_enabled != NULL));
54847c6bc8SGabriel Fernandez 
55847c6bc8SGabriel Fernandez 	return ops->is_enabled(id);
56847c6bc8SGabriel Fernandez }
57847c6bc8SGabriel Fernandez 
58847c6bc8SGabriel Fernandez /*
59847c6bc8SGabriel Fernandez  * Initialize the clk. The fields in the provided clk
60847c6bc8SGabriel Fernandez  * ops pointer must be valid.
61847c6bc8SGabriel Fernandez  */
62847c6bc8SGabriel Fernandez void clk_register(const struct clk_ops *ops_ptr)
63847c6bc8SGabriel Fernandez {
64847c6bc8SGabriel Fernandez 	assert((ops_ptr != NULL) &&
65847c6bc8SGabriel Fernandez 	       (ops_ptr->enable != NULL) &&
66847c6bc8SGabriel Fernandez 	       (ops_ptr->disable != NULL) &&
67847c6bc8SGabriel Fernandez 	       (ops_ptr->get_rate != NULL) &&
68847c6bc8SGabriel Fernandez 	       (ops_ptr->get_parent != NULL) &&
69847c6bc8SGabriel Fernandez 	       (ops_ptr->is_enabled != NULL));
70847c6bc8SGabriel Fernandez 
71847c6bc8SGabriel Fernandez 	ops = ops_ptr;
72847c6bc8SGabriel Fernandez }
73