1 /*
2  * Copyright (c) 2025 Silicon Laboratories Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/irq.h>
9 #include "fake_driver.h"
10 
11 #define DT_DRV_COMPAT fakedriver
12 
fake_driver_isr(const void * arg)13 static void fake_driver_isr(const void *arg)
14 {
15 	const struct device *dev = (const struct device *)arg;
16 	struct fake_driver_data *data = dev->data;
17 
18 	/* Store the address of fake_driver_isr in user_data because it will be optimized by the
19 	 * compiler (even with __noinline__ attribute)
20 	 */
21 	data->user_data = (void *)&fake_driver_isr;
22 
23 	if (data->irq_callback != NULL) {
24 		data->irq_callback(dev, data->user_data);
25 	}
26 }
27 
fake_driver_configure(const struct device * dev,int config)28 static int fake_driver_configure(const struct device *dev, int config)
29 {
30 	return 0;
31 }
32 
fake_driver_register_irq_callback(const struct device * dev,fake_driver_irq_callback_t cb,void * user_data)33 static int fake_driver_register_irq_callback(const struct device *dev,
34 					     fake_driver_irq_callback_t cb, void *user_data)
35 {
36 	struct fake_driver_data *data = dev->data;
37 
38 	data->irq_callback = cb;
39 	data->user_data = user_data;
40 
41 	return 0;
42 }
43 
44 DEVICE_API(fake, fake_driver_func) = {
45 	.configure = fake_driver_configure,
46 	.register_irq_callback = fake_driver_register_irq_callback,
47 };
48 
fake_driver_init(const struct device * dev)49 static int fake_driver_init(const struct device *dev)
50 {
51 	const struct fake_driver_config *config = dev->config;
52 	struct fake_driver_data *data = dev->data;
53 
54 	data->irq_callback = NULL;
55 	data->user_data = NULL;
56 
57 	config->irq_config_func();
58 
59 	return 0;
60 }
61 
62 #define FAKE_INIT(inst)                                                                            \
63 	static struct fake_driver_data fake_driver_data_##inst;                                    \
64 	static void fake_driver_irq_config_func_##inst(void)                                       \
65 	{                                                                                          \
66 		IRQ_CONNECT(TEST_IRQ_NUM, TEST_IRQ_PRIO, fake_driver_isr,                          \
67 			    DEVICE_DT_INST_GET(inst), 0);                                          \
68 		irq_enable(TEST_IRQ_NUM);                                                          \
69 	}                                                                                          \
70 	static struct fake_driver_config fake_driver_config_##inst = {                             \
71 		.irq_config_func = fake_driver_irq_config_func_##inst,                             \
72 		.irq_num = TEST_IRQ_NUM,                                                           \
73 		.irq_priority = TEST_IRQ_PRIO,                                                     \
74 	};                                                                                         \
75 	DEVICE_DT_INST_DEFINE(inst, &fake_driver_init, NULL, &fake_driver_data_##inst,             \
76 			      &fake_driver_config_##inst, PRE_KERNEL_1,                            \
77 			      CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &fake_driver_func);
78 
79 DT_INST_FOREACH_STATUS_OKAY(FAKE_INIT)
80