1 /*
2  * Copyright (C) 2025 Microchip Technology Inc. and its subsidiaries
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT microchip_sam_flexcom
8 
9 #include <errno.h>
10 #include <zephyr/device.h>
11 #include <zephyr/kernel.h>
12 
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(mfd_flexcomm, CONFIG_MFD_LOG_LEVEL);
15 
16 struct sam_flexcomm_config {
17 	flexcom_registers_t *reg;
18 	const int mode;
19 };
20 
sam_flexcomm_init(const struct device * dev)21 static int sam_flexcomm_init(const struct device *dev)
22 {
23 	const struct sam_flexcomm_config *config = dev->config;
24 
25 	config->reg->FLEX_MR = (config->reg->FLEX_MR & ~FLEX_MR_OPMODE_Msk) |
26 			       FLEX_MR_OPMODE(config->mode);
27 
28 	LOG_DBG("%s set Operating Mode to %d", dev->name, config->mode);
29 
30 	return 0;
31 }
32 
33 #define SAM_FLEXCOMM_INIT(n)							\
34 										\
35 	static const struct sam_flexcomm_config sam_flexcomm_config_##n = {	\
36 		.reg = (flexcom_registers_t *)DT_INST_REG_ADDR(n),		\
37 		.mode = DT_INST_PROP(n, mchp_flexcom_mode),			\
38 	};									\
39 										\
40 	DEVICE_DT_INST_DEFINE(n,						\
41 			      &sam_flexcomm_init,				\
42 			      NULL,						\
43 			      NULL,						\
44 			      &sam_flexcomm_config_##n,				\
45 			      PRE_KERNEL_1,					\
46 			      CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,		\
47 			      NULL);						\
48 
49 DT_INST_FOREACH_STATUS_OKAY(SAM_FLEXCOMM_INIT)
50