1 /*
2 * Copyright (c) 2023 Google, LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT nuvoton_nct38xx
7
8 #include <zephyr/drivers/i2c.h>
9
10 struct mfd_nct38xx_config {
11 const struct i2c_dt_spec i2c_dev;
12 };
13
14 struct mfd_nct38xx_data {
15 /* lock NC38xx register access */
16 struct k_sem lock;
17 };
18
mfd_nct38xx_init(const struct device * dev)19 static int mfd_nct38xx_init(const struct device *dev)
20 {
21 const struct mfd_nct38xx_config *config = dev->config;
22 struct mfd_nct38xx_data *data = dev->data;
23
24 if (!device_is_ready(config->i2c_dev.bus)) {
25 return -ENODEV;
26 }
27
28 k_sem_init(&data->lock, 1, 1);
29
30 return 0;
31 }
32
mfd_nct38xx_get_lock_reference(const struct device * dev)33 struct k_sem *mfd_nct38xx_get_lock_reference(const struct device *dev)
34 {
35 struct mfd_nct38xx_data *data = dev->data;
36
37 return &data->lock;
38 }
39
mfd_nct38xx_get_i2c_dt_spec(const struct device * dev)40 const struct i2c_dt_spec *mfd_nct38xx_get_i2c_dt_spec(const struct device *dev)
41 {
42 const struct mfd_nct38xx_config *config = dev->config;
43
44 return &config->i2c_dev;
45 }
46
47 #define MFD_NCT38XX_DEFINE(inst) \
48 static struct mfd_nct38xx_data nct38xx_data_##inst; \
49 static const struct mfd_nct38xx_config nct38xx_cfg_##inst = { \
50 .i2c_dev = I2C_DT_SPEC_INST_GET(inst), \
51 }; \
52 \
53 DEVICE_DT_INST_DEFINE(inst, mfd_nct38xx_init, NULL, &nct38xx_data_##inst, \
54 &nct38xx_cfg_##inst, POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, NULL);
55
56 DT_INST_FOREACH_STATUS_OKAY(MFD_NCT38XX_DEFINE)
57