1 /*
2 * Copyright (c) 2023 Grinn
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT maxim_max20335
7
8 #include <errno.h>
9
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/sys/util.h>
12
13 #define MAX20335_REG_CHIP_ID 0x00
14 #define MAX20335_CHIP_ID_VAL 0x04
15
16 struct mfd_max20335_config {
17 struct i2c_dt_spec bus;
18 };
19
mfd_max20335_init(const struct device * dev)20 static int mfd_max20335_init(const struct device *dev)
21 {
22 const struct mfd_max20335_config *config = dev->config;
23 uint8_t val;
24 int ret;
25
26 if (!i2c_is_ready_dt(&config->bus)) {
27 return -ENODEV;
28 }
29
30 ret = i2c_reg_read_byte_dt(&config->bus, MAX20335_REG_CHIP_ID, &val);
31 if (ret < 0) {
32 return ret;
33 }
34
35 if (val != MAX20335_CHIP_ID_VAL) {
36 return -ENODEV;
37 }
38
39 return 0;
40 }
41
42 #define MFD_MA20335_DEFINE(inst) \
43 static const struct mfd_max20335_config mfd_max20335_config##inst = { \
44 .bus = I2C_DT_SPEC_INST_GET(inst), \
45 }; \
46 \
47 DEVICE_DT_INST_DEFINE(inst, mfd_max20335_init, NULL, NULL, \
48 &mfd_max20335_config##inst, POST_KERNEL, \
49 CONFIG_MFD_INIT_PRIORITY, NULL);
50
51 DT_INST_FOREACH_STATUS_OKAY(MFD_MA20335_DEFINE)
52