1 /*
2  * Copyright (c) 2021, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_imx_ccm_rev2
8 #include <errno.h>
9 #include <soc.h>
10 #include <drivers/clock_control.h>
11 #include <dt-bindings/clock/imx_ccm_rev2.h>
12 #include <fsl_clock.h>
13 
14 #define LOG_LEVEL CONFIG_CLOCK_CONTROL_LOG_LEVEL
15 #include <logging/log.h>
16 LOG_MODULE_REGISTER(clock_control);
17 
mcux_ccm_on(const struct device * dev,clock_control_subsys_t sub_system)18 static int mcux_ccm_on(const struct device *dev,
19 				  clock_control_subsys_t sub_system)
20 {
21 	return 0;
22 }
23 
mcux_ccm_off(const struct device * dev,clock_control_subsys_t sub_system)24 static int mcux_ccm_off(const struct device *dev,
25 				   clock_control_subsys_t sub_system)
26 {
27 	return 0;
28 }
29 
mcux_ccm_get_subsys_rate(const struct device * dev,clock_control_subsys_t sub_system,uint32_t * rate)30 static int mcux_ccm_get_subsys_rate(const struct device *dev,
31 					clock_control_subsys_t sub_system,
32 					uint32_t *rate)
33 {
34 	uint32_t clock_name = (uint32_t) sub_system;
35 	uint32_t clock_root, peripheral, instance;
36 
37 	peripheral = (clock_name & IMX_CCM_PERIPHERAL_MASK);
38 	instance = (clock_name & IMX_CCM_INSTANCE_MASK);
39 	switch (peripheral) {
40 #ifdef CONFIG_I2C_MCUX_LPI2C
41 	case IMX_CCM_LPI2C1_CLK:
42 		clock_root = kCLOCK_Root_Lpi2c1 + instance;
43 		break;
44 #endif
45 
46 #ifdef CONFIG_SPI_MCUX_LPSPI
47 	case IMX_CCM_LPSPI1_CLK:
48 		clock_root = kCLOCK_Root_Lpspi1 + instance;
49 		break;
50 #endif
51 
52 #ifdef CONFIG_UART_MCUX_LPUART
53 	case IMX_CCM_LPUART1_CLK:
54 		clock_root = kCLOCK_Root_Lpuart1 + instance;
55 		break;
56 #endif
57 
58 #if DT_NODE_HAS_STATUS(DT_NODELABEL(usdhc1), okay) && CONFIG_DISK_DRIVER_SDMMC
59 	case IMX_CCM_USDHC1_CLK:
60 		clock_root = kCLOCK_Root_Usdhc1 + instance;
61 		break;
62 #endif
63 
64 #ifdef CONFIG_DMA_MCUX_EDMA
65 	case IMX_CCM_EDMA_CLK:
66 		clock_root = kCLOCK_Root_Edma + instance;
67 		break;
68 #endif
69 
70 #ifdef CONFIG_CAN_MCUX_FLEXCAN
71 	case IMX_CCM_CAN1_CLK:
72 		clock_root = kCLOCK_Root_Can1 + instance;
73 		break;
74 #endif
75 
76 #ifdef CONFIG_COUNTER_MCUX_GPT
77 	case IMX_CCM_GPT_CLK:
78 		clock_root = kCLOCK_Root_Gpt1 + instance;
79 		break;
80 #endif
81 	default:
82 		return -EINVAL;
83 	}
84 
85 	*rate = CLOCK_GetRootClockFreq(clock_root);
86 	return 0;
87 }
88 
mcux_ccm_init(const struct device * dev)89 static int mcux_ccm_init(const struct device *dev)
90 {
91 	return 0;
92 }
93 
94 static const struct clock_control_driver_api mcux_ccm_driver_api = {
95 	.on = mcux_ccm_on,
96 	.off = mcux_ccm_off,
97 	.get_rate = mcux_ccm_get_subsys_rate,
98 };
99 
100 DEVICE_DT_INST_DEFINE(0,
101 		    &mcux_ccm_init,
102 		    NULL,
103 		    NULL, NULL,
104 		    PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
105 		    &mcux_ccm_driver_api);
106