1 /*
2  * Copyright (c) 2024 GARDENA GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT silabs_si32_apb
8 
9 #include <stdint.h>
10 
11 #include <zephyr/device.h>
12 #include <zephyr/devicetree.h>
13 #include <zephyr/drivers/clock_control.h>
14 
15 #include <SI32_CLKCTRL_A_Type.h>
16 #include <si32_device.h>
17 
18 struct clock_control_si32_apb_config {
19 	const struct device *clock_dev;
20 	uint32_t divider;
21 };
22 
clock_control_si32_apb_on(const struct device * dev,clock_control_subsys_t sys)23 static int clock_control_si32_apb_on(const struct device *dev, clock_control_subsys_t sys)
24 {
25 	return -ENOTSUP;
26 }
27 
clock_control_si32_apb_off(const struct device * dev,clock_control_subsys_t sys)28 static int clock_control_si32_apb_off(const struct device *dev, clock_control_subsys_t sys)
29 {
30 
31 	return -ENOTSUP;
32 }
33 
clock_control_si32_apb_get_rate(const struct device * dev,clock_control_subsys_t sys,uint32_t * rate)34 static int clock_control_si32_apb_get_rate(const struct device *dev, clock_control_subsys_t sys,
35 					   uint32_t *rate)
36 {
37 	const struct clock_control_si32_apb_config *config = dev->config;
38 	const int ret = clock_control_get_rate(config->clock_dev, NULL, rate);
39 
40 	if (ret) {
41 		return ret;
42 	}
43 
44 	*rate /= config->divider;
45 
46 	return 0;
47 }
48 
49 static DEVICE_API(clock_control, clock_control_si32_apb_api) = {
50 	.on = clock_control_si32_apb_on,
51 	.off = clock_control_si32_apb_off,
52 	.get_rate = clock_control_si32_apb_get_rate,
53 };
54 
clock_control_si32_apb_init(const struct device * dev)55 static int clock_control_si32_apb_init(const struct device *dev)
56 {
57 	const struct clock_control_si32_apb_config *config = dev->config;
58 
59 	if (!device_is_ready(config->clock_dev)) {
60 		return -ENODEV;
61 	}
62 
63 	if (config->divider == 1) {
64 		SI32_CLKCTRL_A_select_apb_divider_1(SI32_CLKCTRL_0);
65 	} else if (config->divider == 2) {
66 		SI32_CLKCTRL_A_select_apb_divider_2(SI32_CLKCTRL_0);
67 	} else {
68 		return -ENOTSUP;
69 	}
70 
71 	return 0;
72 }
73 
74 static const struct clock_control_si32_apb_config config = {
75 	.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(0)),
76 	.divider = DT_PROP(DT_NODELABEL(clk_apb), divider),
77 };
78 
79 DEVICE_DT_INST_DEFINE(0, clock_control_si32_apb_init, NULL, NULL, &config, PRE_KERNEL_1,
80 		      CONFIG_CLOCK_CONTROL_INIT_PRIORITY, &clock_control_si32_apb_api);
81