| 1 |
/* |
| 2 |
* Copyright (c) 2021, STMicroelectronics - All Rights Reserved |
| 3 |
* Author(s): Ludovic Barre, <[email protected]> for STMicroelectronics. |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: BSD-3-Clause |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <errno.h> |
| 10 |
#include <stdbool.h> |
| 11 |
|
| 12 |
#include <drivers/clk.h> |
| 13 |
|
| 14 |
static const struct clk_ops *ops; |
| 15 |
|
| 16 |
int clk_enable(unsigned long id) |
| 17 |
{ |
| 18 |
assert((ops != NULL) && (ops->enable != NULL)); |
| 19 |
|
| 20 |
return ops->enable(id); |
| 21 |
} |
| 22 |
|
| 23 |
void clk_disable(unsigned long id) |
| 24 |
{ |
| 25 |
assert((ops != NULL) && (ops->disable != NULL)); |
| 26 |
|
| 27 |
ops->disable(id); |
| 28 |
} |
| 29 |
|
| 30 |
unsigned long clk_get_rate(unsigned long id) |
| 31 |
{ |
| 32 |
assert((ops != NULL) && (ops->get_rate != NULL)); |
| 33 |
|
| 34 |
return ops->get_rate(id); |
| 35 |
} |
| 36 |
|
| 37 |
int clk_set_rate(unsigned long id, unsigned long rate, unsigned long *orate) |
| 38 |
{ |
| 39 |
unsigned long lrate; |
| 40 |
|
| 41 |
assert((ops != NULL) && (ops->set_rate != NULL)); |
| 42 |
|
| 43 |
if (orate != NULL) { |
| 44 |
return ops->set_rate(id, rate, orate); |
| 45 |
} |
| 46 |
|
| 47 |
/* In case the caller is not interested in the output rate */ |
| 48 |
return ops->set_rate(id, rate, &lrate); |
| 49 |
} |
| 50 |
|
| 51 |
int clk_get_parent(unsigned long id) |
| 52 |
{ |
| 53 |
assert((ops != NULL) && (ops->get_parent != NULL)); |
| 54 |
|
| 55 |
return ops->get_parent(id); |
| 56 |
} |
| 57 |
|
| 58 |
int clk_set_parent(unsigned long id, unsigned long parent_id) |
| 59 |
{ |
| 60 |
assert((ops != NULL) && (ops->set_parent != NULL)); |
| 61 |
|
| 62 |
return ops->set_parent(id, parent_id); |
| 63 |
} |
| 64 |
|
| 65 |
bool clk_is_enabled(unsigned long id) |
| 66 |
{ |
| 67 |
assert((ops != NULL) && (ops->is_enabled != NULL)); |
| 68 |
|
| 69 |
return ops->is_enabled(id); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* Initialize the clk. The fields in the provided clk |
| 74 |
* ops pointer must be valid. |
| 75 |
*/ |
| 76 |
void clk_register(const struct clk_ops *ops_ptr) |
| 77 |
{ |
| 78 |
assert((ops_ptr != NULL) && |
| 79 |
(ops_ptr->enable != NULL) && |
| 80 |
(ops_ptr->disable != NULL) && |
| 81 |
(ops_ptr->get_rate != NULL) && |
| 82 |
(ops_ptr->get_parent != NULL) && |
| 83 |
(ops_ptr->is_enabled != NULL)); |
| 84 |
|
| 85 |
ops = ops_ptr; |
| 86 |
} |