1 /*
2  * Copyright (c) 2023 Grinn
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #define DT_DRV_COMPAT adi_ad559x
7 
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/byteorder.h>
11 
12 #include <zephyr/drivers/mfd/ad559x.h>
13 
14 #include "mfd_ad559x.h"
15 
mfd_ad559x_has_pointer_byte_map(const struct device * dev)16 bool mfd_ad559x_has_pointer_byte_map(const struct device *dev)
17 {
18 	const struct mfd_ad559x_config *config = dev->config;
19 
20 	return config->has_pointer_byte_map;
21 }
22 
mfd_ad559x_read_raw(const struct device * dev,uint8_t * val,size_t len)23 int mfd_ad559x_read_raw(const struct device *dev, uint8_t *val, size_t len)
24 {
25 	struct mfd_ad559x_data *data = dev->data;
26 
27 	return data->transfer_function->read_raw(dev, val, len);
28 }
29 
mfd_ad559x_write_raw(const struct device * dev,uint8_t * val,size_t len)30 int mfd_ad559x_write_raw(const struct device *dev, uint8_t *val, size_t len)
31 {
32 	struct mfd_ad559x_data *data = dev->data;
33 
34 	return data->transfer_function->write_raw(dev, val, len);
35 }
36 
mfd_ad559x_read_reg(const struct device * dev,uint8_t reg,uint8_t reg_data,uint16_t * val)37 int mfd_ad559x_read_reg(const struct device *dev, uint8_t reg, uint8_t reg_data, uint16_t *val)
38 {
39 	struct mfd_ad559x_data *data = dev->data;
40 
41 	return data->transfer_function->read_reg(dev, reg, reg_data, val);
42 }
43 
mfd_ad559x_write_reg(const struct device * dev,uint8_t reg,uint16_t val)44 int mfd_ad559x_write_reg(const struct device *dev, uint8_t reg, uint16_t val)
45 {
46 	struct mfd_ad559x_data *data = dev->data;
47 
48 	return data->transfer_function->write_reg(dev, reg, val);
49 }
50 
mfd_add559x_software_reset(const struct device * dev)51 static int mfd_add559x_software_reset(const struct device *dev)
52 {
53 	return mfd_ad559x_write_reg(dev, AD559X_REG_SOFTWARE_RESET,
54 				    AD559X_SOFTWARE_RESET_MAGIC_VAL);
55 }
56 
mfd_ad559x_init(const struct device * dev)57 static int mfd_ad559x_init(const struct device *dev)
58 {
59 	const struct mfd_ad559x_config *config = dev->config;
60 	int ret;
61 
62 	ret = config->bus_init(dev);
63 	if (ret < 0) {
64 		return ret;
65 	}
66 
67 	if (!gpio_is_ready_dt(&config->reset_gpio)) {
68 		return -ENODEV;
69 	}
70 
71 	ret = gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_INACTIVE);
72 	if (ret < 0) {
73 		return ret;
74 	}
75 
76 	ret = mfd_add559x_software_reset(dev);
77 	if (ret < 0) {
78 		return ret;
79 	}
80 
81 	return 0;
82 }
83 
84 #define MDF_AD559X_DEFINE_I2C_BUS(inst)                                                            \
85 	.i2c = I2C_DT_SPEC_INST_GET(inst), .bus_init = mfd_ad559x_i2c_init,                        \
86 	.has_pointer_byte_map = true
87 
88 #define MDF_AD559X_DEFINE_SPI_BUS_FLAGS                                                            \
89 	(SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_OP_MODE_MASTER | SPI_MODE_CPOL)
90 
91 #define MDF_AD559X_DEFINE_SPI_BUS(inst)                                                            \
92 	.spi = SPI_DT_SPEC_INST_GET(inst, MDF_AD559X_DEFINE_SPI_BUS_FLAGS, 0),                     \
93 	.bus_init = mfd_ad559x_spi_init, .has_pointer_byte_map = false
94 
95 #define MFD_AD559X_DEFINE_BUS(inst)                                                                \
96 	COND_CODE_1(DT_INST_ON_BUS(inst, i2c), (MDF_AD559X_DEFINE_I2C_BUS(inst)),                  \
97 		    (MDF_AD559X_DEFINE_SPI_BUS(inst)))
98 
99 #define MFD_AD559X_DEFINE(inst)                                                                    \
100 	static struct mfd_ad559x_data mfd_ad559x_data_##inst;                                      \
101 	static const struct mfd_ad559x_config mfd_ad559x_config_##inst = {                         \
102 		.reset_gpio = GPIO_DT_SPEC_INST_GET(inst, reset_gpios),                            \
103 		MFD_AD559X_DEFINE_BUS(inst),                                                       \
104 	};                                                                                         \
105                                                                                                    \
106 	DEVICE_DT_INST_DEFINE(inst, mfd_ad559x_init, NULL, &mfd_ad559x_data_##inst,                \
107 			      &mfd_ad559x_config_##inst, POST_KERNEL, CONFIG_MFD_INIT_PRIORITY,    \
108 			      NULL);
109 
110 DT_INST_FOREACH_STATUS_OKAY(MFD_AD559X_DEFINE);
111