1 /*
2  * Copyright (c) 2024 TOKITA Hiroshi
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #define DT_DRV_COMPAT awinic_aw9523b
7 
8 #include <zephyr/drivers/i2c.h>
9 #include <zephyr/drivers/mfd/aw9523b.h>
10 #include <zephyr/kernel.h>
11 
12 #define AW9523B_ID_VALUE 0x23
13 
14 struct mfd_aw9523b_config {
15 	struct i2c_dt_spec i2c;
16 };
17 
18 struct mfd_aw9523b_data {
19 	struct k_sem lock;
20 };
21 
mfd_aw9523b_init(const struct device * dev)22 static int mfd_aw9523b_init(const struct device *dev)
23 {
24 	const struct mfd_aw9523b_config *config = dev->config;
25 	struct mfd_aw9523b_data *data = dev->data;
26 	uint8_t reg;
27 	int ret;
28 
29 	if (!i2c_is_ready_dt(&config->i2c)) {
30 		return -ENODEV;
31 	}
32 
33 	k_sem_init(&data->lock, 1, 1);
34 
35 	ret = i2c_reg_read_byte_dt(&config->i2c, AW9523B_REG_ID, &reg);
36 
37 	if (ret) {
38 		return ret;
39 	};
40 
41 	if (reg != AW9523B_ID_VALUE) {
42 		return -1;
43 	}
44 
45 	return 0;
46 }
47 
aw9523b_get_lock(const struct device * dev)48 struct k_sem *aw9523b_get_lock(const struct device *dev)
49 {
50 	struct mfd_aw9523b_data *data = dev->data;
51 
52 	return &data->lock;
53 }
54 
55 #define MFD_AW9523B_DEFINE(inst)                                                                   \
56 	static const struct mfd_aw9523b_config config##inst = {                                    \
57 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
58 	};                                                                                         \
59                                                                                                    \
60 	static struct mfd_aw9523b_data data##inst;                                                 \
61                                                                                                    \
62 	DEVICE_DT_INST_DEFINE(inst, mfd_aw9523b_init, NULL, &data##inst, &config##inst,            \
63 			      POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);
64 
65 DT_INST_FOREACH_STATUS_OKAY(MFD_AW9523B_DEFINE)
66