1 /*
2  * Copyright (c) 2019 Vestas Wind Systems A/S
3  *
4  * Based on clock_control_rv32m1_pcc.c, which is:
5  * Copyright (c) 2018 Foundries.io
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #define DT_DRV_COMPAT nxp_kinetis_pcc
11 
12 #include <errno.h>
13 #include <soc.h>
14 #include <drivers/clock_control.h>
15 #include <fsl_clock.h>
16 
17 #define LOG_LEVEL CONFIG_CLOCK_CONTROL_LOG_LEVEL
18 #include <logging/log.h>
19 LOG_MODULE_REGISTER(clock_control_mcux_pcc);
20 
21 struct mcux_pcc_config {
22 	uint32_t base_address;
23 };
24 
25 #define DEV_CFG(dev)  ((struct mcux_pcc_config *)(dev->config))
26 #define DEV_BASE(dev) (DEV_CFG(dev)->base_address)
27 #ifndef MAKE_PCC_REGADDR
28 #define MAKE_PCC_REGADDR(base, offset) ((base) + (offset))
29 #endif
30 
clock_ip(const struct device * dev,clock_control_subsys_t sub_system)31 static inline clock_ip_name_t clock_ip(const struct device *dev,
32 				       clock_control_subsys_t sub_system)
33 {
34 	uint32_t offset = POINTER_TO_UINT(sub_system);
35 
36 	return MAKE_PCC_REGADDR(DEV_BASE(dev), offset);
37 }
38 
mcux_pcc_on(const struct device * dev,clock_control_subsys_t sub_system)39 static int mcux_pcc_on(const struct device *dev,
40 		       clock_control_subsys_t sub_system)
41 {
42 	CLOCK_EnableClock(clock_ip(dev, sub_system));
43 	return 0;
44 }
45 
mcux_pcc_off(const struct device * dev,clock_control_subsys_t sub_system)46 static int mcux_pcc_off(const struct device *dev,
47 			clock_control_subsys_t sub_system)
48 {
49 	CLOCK_DisableClock(clock_ip(dev, sub_system));
50 	return 0;
51 }
52 
mcux_pcc_get_rate(const struct device * dev,clock_control_subsys_t sub_system,uint32_t * rate)53 static int mcux_pcc_get_rate(const struct device *dev,
54 			       clock_control_subsys_t sub_system,
55 			       uint32_t *rate)
56 {
57 	*rate = CLOCK_GetIpFreq(clock_ip(dev, sub_system));
58 	return 0;
59 }
60 
mcux_pcc_init(const struct device * dev)61 static int mcux_pcc_init(const struct device *dev)
62 {
63 	return 0;
64 }
65 
66 static const struct clock_control_driver_api mcux_pcc_api = {
67 	.on = mcux_pcc_on,
68 	.off = mcux_pcc_off,
69 	.get_rate = mcux_pcc_get_rate,
70 };
71 
72 #define MCUX_PCC_INIT(inst)						\
73 	static const struct mcux_pcc_config mcux_pcc##inst##_config = {	\
74 		.base_address = DT_INST_REG_ADDR(inst)			\
75 	};								\
76 									\
77 	DEVICE_DT_INST_DEFINE(inst,					\
78 			    &mcux_pcc_init,				\
79 			    NULL,					\
80 			    NULL, &mcux_pcc##inst##_config,		\
81 			    PRE_KERNEL_1,				\
82 			    CONFIG_KERNEL_INIT_PRIORITY_OBJECTS,	\
83 			    &mcux_pcc_api);
84 
85 DT_INST_FOREACH_STATUS_OKAY(MCUX_PCC_INIT)
86