1 /*
2  * Copyright (c) 2024 SILA Embedded Solutions GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT rohm_bd8lb600fs
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/device.h>
11 #include <zephyr/init.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/drivers/gpio/gpio_utils.h>
14 #include <zephyr/drivers/mfd/bd8lb600fs.h>
15 #include <zephyr/drivers/spi.h>
16 #include <zephyr/logging/log.h>
17 #include <zephyr/sys/byteorder.h>
18 
19 LOG_MODULE_REGISTER(rohm_bd8lb600fs, CONFIG_MFD_LOG_LEVEL);
20 
21 #define OUTPUT_OFF_WITH_OPEN_LOAD_DETECTION  0b11
22 #define OUTPUT_ON                            0b10
23 #define WAIT_TIME_RESET_ACTIVE_IN_US         1000
24 #define WAIT_TIME_RESET_INACTIVE_TO_CS_IN_US 10
25 
26 struct bd8lb600fs_config {
27 	struct spi_dt_spec bus;
28 	const struct gpio_dt_spec gpio_reset;
29 	size_t instance_count;
30 };
31 
32 struct bd8lb600fs_data {
33 	/* each bit is one output channel, bit 0 = channel 1, ... */
34 	uint32_t state;
35 	/* each bit defines if an open load was detected, see state */
36 	uint32_t old;
37 	/* each bit defines if an over current or over temperature was detected, see state */
38 	uint32_t ocp_or_tsd;
39 	struct k_mutex lock;
40 };
41 
bd8lb600fs_fill_tx_buffer(const struct device * dev,uint8_t * buffer,size_t buffer_size)42 static void bd8lb600fs_fill_tx_buffer(const struct device *dev, uint8_t *buffer, size_t buffer_size)
43 {
44 	const struct bd8lb600fs_config *config = dev->config;
45 	struct bd8lb600fs_data *data = dev->data;
46 	uint16_t state_converted = 0;
47 
48 	LOG_DBG("%s: writing state 0x%08X to BD8LB600FS", dev->name, data->state);
49 
50 	memset(buffer, 0x00, buffer_size);
51 
52 	for (size_t j = 0; j < config->instance_count; ++j) {
53 		int instance_position = (config->instance_count - j - 1) * 2;
54 
55 		state_converted = 0;
56 
57 		for (size_t i = 0; i < 8; ++i) {
58 			if ((data->state & BIT(i + j * 8)) == 0) {
59 				state_converted |= OUTPUT_OFF_WITH_OPEN_LOAD_DETECTION << (i * 2);
60 			} else {
61 				state_converted |= OUTPUT_ON << (i * 2);
62 			}
63 		}
64 
65 		LOG_DBG("%s: configuration for instance %zu: %04X (position %i)", dev->name, j,
66 			state_converted, instance_position);
67 		sys_put_be16(state_converted, buffer + instance_position);
68 	}
69 }
70 
bd8lb600fs_parse_rx_buffer(const struct device * dev,uint8_t * buffer)71 static void bd8lb600fs_parse_rx_buffer(const struct device *dev, uint8_t *buffer)
72 {
73 	const struct bd8lb600fs_config *config = dev->config;
74 	struct bd8lb600fs_data *data = dev->data;
75 
76 	data->old = 0;
77 	data->ocp_or_tsd = 0;
78 
79 	for (size_t j = 0; j < config->instance_count; ++j) {
80 		int instance_position = (config->instance_count - j - 1) * 2;
81 		uint16_t current = sys_get_be16(buffer + instance_position);
82 
83 		for (size_t i = 0; i < 8; ++i) {
84 			if ((BIT(2 * i + 1) & current) != 0) {
85 				WRITE_BIT(data->old, i + j * 8, 1);
86 			}
87 			if ((BIT(2 * i) & current) != 0) {
88 				WRITE_BIT(data->ocp_or_tsd, i + j * 8, 1);
89 			}
90 		}
91 	}
92 
93 	LOG_DBG("%s: received 0x%08X open load state from BD8LB600FS", dev->name, data->old);
94 	LOG_DBG("%s: received 0x%08X OCP or TSD state from BD8LB600FS", dev->name,
95 		data->ocp_or_tsd);
96 }
97 
bd8lb600fs_transceive_state(const struct device * dev)98 static int bd8lb600fs_transceive_state(const struct device *dev)
99 {
100 	const struct bd8lb600fs_config *config = dev->config;
101 
102 	uint8_t buffer_tx[8];
103 	const struct spi_buf tx_buf = {
104 		.buf = buffer_tx,
105 		.len = config->instance_count * sizeof(uint16_t),
106 	};
107 	const struct spi_buf_set tx = {
108 		.buffers = &tx_buf,
109 		.count = 1,
110 	};
111 	uint8_t buffer_rx[8];
112 	const struct spi_buf rx_buf = {
113 		.buf = buffer_rx,
114 		.len = config->instance_count * sizeof(uint16_t),
115 	};
116 	const struct spi_buf_set rx = {
117 		.buffers = &rx_buf,
118 		.count = 1,
119 	};
120 
121 	bd8lb600fs_fill_tx_buffer(dev, buffer_tx, ARRAY_SIZE(buffer_tx));
122 
123 	int result = spi_transceive_dt(&config->bus, &tx, &rx);
124 
125 	if (result != 0) {
126 		LOG_ERR("spi_transceive failed with error %i", result);
127 		return result;
128 	}
129 
130 	bd8lb600fs_parse_rx_buffer(dev, buffer_rx);
131 
132 	return 0;
133 }
134 
bd8lb600fs_write_state(const struct device * dev)135 static int bd8lb600fs_write_state(const struct device *dev)
136 {
137 	const struct bd8lb600fs_config *config = dev->config;
138 
139 	uint8_t buffer_tx[8];
140 	const struct spi_buf tx_buf = {
141 		.buf = buffer_tx,
142 		.len = config->instance_count * sizeof(uint16_t),
143 	};
144 	const struct spi_buf_set tx = {
145 		.buffers = &tx_buf,
146 		.count = 1,
147 	};
148 
149 	bd8lb600fs_fill_tx_buffer(dev, buffer_tx, ARRAY_SIZE(buffer_tx));
150 
151 	int result = spi_write_dt(&config->bus, &tx);
152 
153 	if (result != 0) {
154 		LOG_ERR("spi_transceive failed with error %i", result);
155 		return result;
156 	}
157 
158 	return 0;
159 }
160 
mfd_bd8lb600fs_set_outputs(const struct device * dev,uint32_t values)161 int mfd_bd8lb600fs_set_outputs(const struct device *dev, uint32_t values)
162 {
163 	struct bd8lb600fs_data *data = dev->data;
164 	int result;
165 
166 	k_mutex_lock(&data->lock, K_FOREVER);
167 	data->state = values;
168 	result = bd8lb600fs_write_state(dev);
169 	k_mutex_unlock(&data->lock);
170 
171 	return result;
172 }
173 
mfd_bd8lb600fs_get_output_diagnostics(const struct device * dev,uint32_t * old,uint32_t * ocp_or_tsd)174 int mfd_bd8lb600fs_get_output_diagnostics(const struct device *dev, uint32_t *old,
175 					  uint32_t *ocp_or_tsd)
176 {
177 	struct bd8lb600fs_data *data = dev->data;
178 	int result;
179 
180 	k_mutex_lock(&data->lock, K_FOREVER);
181 	result = bd8lb600fs_transceive_state(dev);
182 	*old = data->old;
183 	*ocp_or_tsd = data->ocp_or_tsd;
184 	k_mutex_unlock(&data->lock);
185 
186 	return result;
187 }
188 
bd8lb600fs_init(const struct device * dev)189 static int bd8lb600fs_init(const struct device *dev)
190 {
191 	const struct bd8lb600fs_config *config = dev->config;
192 	struct bd8lb600fs_data *data = dev->data;
193 
194 	if (!spi_is_ready_dt(&config->bus)) {
195 		LOG_ERR("SPI bus %s not ready", config->bus.bus->name);
196 		return -ENODEV;
197 	}
198 
199 	if (!gpio_is_ready_dt(&config->gpio_reset)) {
200 		LOG_ERR("%s: reset GPIO is not ready", dev->name);
201 		return -ENODEV;
202 	}
203 
204 	int result = k_mutex_init(&data->lock);
205 
206 	if (result != 0) {
207 		LOG_ERR("unable to initialize mutex");
208 		return result;
209 	}
210 
211 	result = gpio_pin_configure_dt(&config->gpio_reset, GPIO_OUTPUT_ACTIVE);
212 
213 	if (result != 0) {
214 		LOG_ERR("failed to initialize GPIO for reset");
215 		return result;
216 	}
217 
218 	k_busy_wait(WAIT_TIME_RESET_ACTIVE_IN_US);
219 	gpio_pin_set_dt(&config->gpio_reset, 0);
220 	k_busy_wait(WAIT_TIME_RESET_INACTIVE_TO_CS_IN_US);
221 
222 	return 0;
223 }
224 
225 #define BD8LB600FS_INIT(inst)                                                                      \
226 	static const struct bd8lb600fs_config bd8lb600fs_##inst##_config = {                       \
227 		.bus = SPI_DT_SPEC_INST_GET(                                                       \
228 			inst, SPI_OP_MODE_MASTER | SPI_MODE_CPHA | SPI_WORD_SET(8), 0),            \
229 		.gpio_reset = GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), reset_gpios, 0),          \
230 		.instance_count = DT_INST_PROP(inst, instance_count),                              \
231 	};                                                                                         \
232                                                                                                    \
233 	static struct bd8lb600fs_data bd8lb600fs_##inst##_data = {                                 \
234 		.state = 0x00,                                                                     \
235 	};                                                                                         \
236                                                                                                    \
237 	/* This has to be initialized after the SPI peripheral. */                                 \
238 	DEVICE_DT_INST_DEFINE(inst, bd8lb600fs_init, NULL, &bd8lb600fs_##inst##_data,              \
239 			      &bd8lb600fs_##inst##_config, POST_KERNEL, CONFIG_MFD_INIT_PRIORITY,  \
240 			      NULL);
241 
242 DT_INST_FOREACH_STATUS_OKAY(BD8LB600FS_INIT)
243