| 1 |
/* |
| 2 |
* Copyright 2024 NXP |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
#include <errno.h> |
| 7 |
#include <s32cc-clk-ids.h> |
| 8 |
#include <s32cc-clk-utils.h> |
| 9 |
|
| 10 |
static struct s32cc_clk *s32cc_clk_get_from_array(const struct s32cc_clk_array *arr, |
| 11 |
unsigned long clk_id) |
| 12 |
{ |
| 13 |
unsigned long type, id; |
| 14 |
|
| 15 |
type = S32CC_CLK_TYPE(clk_id); |
| 16 |
|
| 17 |
if (type != arr->type_mask) { |
| 18 |
return NULL; |
| 19 |
} |
| 20 |
|
| 21 |
id = S32CC_CLK_ID(clk_id); |
| 22 |
|
| 23 |
if (id >= arr->n_clks) { |
| 24 |
return NULL; |
| 25 |
} |
| 26 |
|
| 27 |
return arr->clks[id]; |
| 28 |
} |
| 29 |
|
| 30 |
struct s32cc_clk *s32cc_get_clk_from_table(const struct s32cc_clk_array *const *clk_arr, |
| 31 |
size_t size, |
| 32 |
unsigned long clk_id) |
| 33 |
{ |
| 34 |
struct s32cc_clk *clk; |
| 35 |
size_t i; |
| 36 |
|
| 37 |
for (i = 0; i < size; i++) { |
| 38 |
clk = s32cc_clk_get_from_array(clk_arr[i], clk_id); |
| 39 |
if (clk != NULL) { |
| 40 |
return clk; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
int s32cc_get_id_from_table(const struct s32cc_clk_array *const *clk_arr, |
| 48 |
size_t size, const struct s32cc_clk *clk, |
| 49 |
unsigned long *clk_index) |
| 50 |
{ |
| 51 |
size_t i, j; |
| 52 |
|
| 53 |
for (i = 0; i < size; i++) { |
| 54 |
for (j = 0; j < clk_arr[i]->n_clks; j++) { |
| 55 |
if (clk_arr[i]->clks[j] != clk) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
*clk_index = S32CC_CLK(clk_arr[i]->type_mask, j); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return -EINVAL; |
| 65 |
} |