1 /*
2  * Copyright (c) 2022 SILA Embedded Solutions GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT rohm_bd8lb600fs
8 
9 #include <errno.h>
10 
11 #include <zephyr/kernel.h>
12 #include <zephyr/device.h>
13 #include <zephyr/init.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/gpio/gpio_utils.h>
16 #include <zephyr/drivers/spi.h>
17 #include <zephyr/logging/log.h>
18 #include <zephyr/sys/byteorder.h>
19 
20 LOG_MODULE_REGISTER(gpio_bd8lb600fs, CONFIG_GPIO_LOG_LEVEL);
21 
22 #define OUTPUT_OFF_WITH_OPEN_LOAD_DETECTION  0b11
23 #define OUTPUT_ON			     0b10
24 #define WAIT_TIME_RESET_ACTIVE_IN_US	     1000
25 #define WAIT_TIME_RESET_INACTIVE_TO_CS_IN_US 10
26 
27 struct bd8lb600fs_config {
28 	/* gpio_driver_config needs to be first */
29 	struct gpio_driver_config common;
30 
31 	struct spi_dt_spec bus;
32 	const struct gpio_dt_spec gpio_reset;
33 	int gpios_count;
34 };
35 
36 struct bd8lb600fs_drv_data {
37 	/* gpio_driver_data needs to be first */
38 	struct gpio_driver_data data;
39 	uint32_t state;	    /* each bit is one output channel, bit 0 = channel 1, ... */
40 	uint32_t configured; /* each bit defines if the output channel is configured, see state */
41 	struct k_mutex lock;
42 	int instance_count_actual;
43 	int gpios_count_actual;
44 };
45 
write_state(const struct device * dev,uint32_t state)46 static int write_state(const struct device *dev, uint32_t state)
47 {
48 	const struct bd8lb600fs_config *config = dev->config;
49 	struct bd8lb600fs_drv_data *drv_data = dev->data;
50 
51 	LOG_DBG("%s: writing state 0x%08X to BD8LB600FS", dev->name, state);
52 
53 	uint16_t state_converted = 0;
54 	uint8_t buffer_tx[8];
55 	const struct spi_buf tx_buf = {
56 		.buf = buffer_tx,
57 		.len = drv_data->instance_count_actual * sizeof(state_converted),
58 	};
59 	const struct spi_buf_set tx = {
60 		.buffers = &tx_buf,
61 		.count = 1,
62 	};
63 
64 	memset(buffer_tx, 0x00, sizeof(buffer_tx));
65 
66 	for (size_t j = 0; j < drv_data->instance_count_actual; ++j) {
67 		int instance_position = (drv_data->instance_count_actual - j - 1) * 2;
68 
69 		state_converted = 0;
70 
71 		for (size_t i = 0; i < 8; ++i) {
72 			if ((state & BIT(i + j*8)) == 0) {
73 				state_converted |= OUTPUT_OFF_WITH_OPEN_LOAD_DETECTION << (i * 2);
74 			} else {
75 				state_converted |= OUTPUT_ON << (i * 2);
76 			}
77 		}
78 
79 		LOG_DBG("%s: configuration for instance %zu: %04X (position %i)",
80 			dev->name,
81 			j,
82 			state_converted,
83 			instance_position);
84 		sys_put_be16(state_converted, buffer_tx + instance_position);
85 	}
86 
87 	LOG_HEXDUMP_DBG(buffer_tx, ARRAY_SIZE(buffer_tx), "configuration written out");
88 
89 	int result = spi_write_dt(&config->bus, &tx);
90 
91 	if (result != 0) {
92 		LOG_ERR("spi_write failed with error %i", result);
93 		return result;
94 	}
95 
96 	return 0;
97 }
98 
bd8lb600fs_pin_configure(const struct device * dev,gpio_pin_t pin,gpio_flags_t flags)99 static int bd8lb600fs_pin_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags)
100 {
101 	struct bd8lb600fs_drv_data *drv_data = dev->data;
102 
103 	/* cannot execute a bus operation in an ISR context */
104 	if (k_is_in_isr()) {
105 		return -EWOULDBLOCK;
106 	}
107 
108 	if (pin >= drv_data->gpios_count_actual) {
109 		LOG_ERR("invalid pin number %i", pin);
110 		return -EINVAL;
111 	}
112 
113 	if ((flags & GPIO_INPUT) != 0) {
114 		LOG_ERR("cannot configure pin as input");
115 		return -ENOTSUP;
116 	}
117 
118 	if ((flags & GPIO_OUTPUT) == 0) {
119 		LOG_ERR("pin must be configured as an output");
120 		return -ENOTSUP;
121 	}
122 
123 	if ((flags & GPIO_SINGLE_ENDED) == 0) {
124 		LOG_ERR("pin must be configured as single ended");
125 		return -ENOTSUP;
126 	}
127 
128 	if ((flags & GPIO_LINE_OPEN_DRAIN) == 0) {
129 		LOG_ERR("pin must be configured as open drain");
130 		return -ENOTSUP;
131 	}
132 
133 	if ((flags & GPIO_PULL_UP) != 0) {
134 		LOG_ERR("pin cannot have a pull up configured");
135 		return -ENOTSUP;
136 	}
137 
138 	if ((flags & GPIO_PULL_DOWN) != 0) {
139 		LOG_ERR("pin cannot have a pull down configured");
140 		return -ENOTSUP;
141 	}
142 
143 	k_mutex_lock(&drv_data->lock, K_FOREVER);
144 
145 	if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
146 		WRITE_BIT(drv_data->state, pin, 0);
147 	} else if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
148 		WRITE_BIT(drv_data->state, pin, 1);
149 	}
150 
151 	WRITE_BIT(drv_data->configured, pin, 1);
152 
153 	int result = write_state(dev, drv_data->state);
154 
155 	k_mutex_unlock(&drv_data->lock);
156 
157 	return result;
158 }
159 
bd8lb600fs_port_get_raw(const struct device * dev,uint32_t * value)160 static int bd8lb600fs_port_get_raw(const struct device *dev, uint32_t *value)
161 {
162 	LOG_ERR("input pins are not available");
163 	return -ENOTSUP;
164 }
165 
bd8lb600fs_port_set_masked_raw(const struct device * dev,uint32_t mask,uint32_t value)166 static int bd8lb600fs_port_set_masked_raw(const struct device *dev, uint32_t mask, uint32_t value)
167 {
168 	struct bd8lb600fs_drv_data *drv_data = dev->data;
169 
170 	/* cannot execute a bus operation in an ISR context */
171 	if (k_is_in_isr()) {
172 		return -EWOULDBLOCK;
173 	}
174 
175 	k_mutex_lock(&drv_data->lock, K_FOREVER);
176 	drv_data->state = (drv_data->state & ~mask) | (mask & value);
177 
178 	int result = write_state(dev, drv_data->state);
179 
180 	k_mutex_unlock(&drv_data->lock);
181 
182 	return result;
183 }
184 
bd8lb600fs_port_set_bits_raw(const struct device * dev,uint32_t mask)185 static int bd8lb600fs_port_set_bits_raw(const struct device *dev, uint32_t mask)
186 {
187 	return bd8lb600fs_port_set_masked_raw(dev, mask, mask);
188 }
189 
bd8lb600fs_port_clear_bits_raw(const struct device * dev,uint32_t mask)190 static int bd8lb600fs_port_clear_bits_raw(const struct device *dev, uint32_t mask)
191 {
192 	return bd8lb600fs_port_set_masked_raw(dev, mask, 0);
193 }
194 
bd8lb600fs_port_toggle_bits(const struct device * dev,uint32_t mask)195 static int bd8lb600fs_port_toggle_bits(const struct device *dev, uint32_t mask)
196 {
197 	struct bd8lb600fs_drv_data *drv_data = dev->data;
198 
199 	/* cannot execute a bus operation in an ISR context */
200 	if (k_is_in_isr()) {
201 		return -EWOULDBLOCK;
202 	}
203 
204 	k_mutex_lock(&drv_data->lock, K_FOREVER);
205 	drv_data->state ^= mask;
206 
207 	int result = write_state(dev, drv_data->state);
208 
209 	k_mutex_unlock(&drv_data->lock);
210 
211 	return result;
212 }
213 
214 static const struct gpio_driver_api api_table = {
215 	.pin_configure = bd8lb600fs_pin_configure,
216 	.port_get_raw = bd8lb600fs_port_get_raw,
217 	.port_set_masked_raw = bd8lb600fs_port_set_masked_raw,
218 	.port_set_bits_raw = bd8lb600fs_port_set_bits_raw,
219 	.port_clear_bits_raw = bd8lb600fs_port_clear_bits_raw,
220 	.port_toggle_bits = bd8lb600fs_port_toggle_bits,
221 };
222 
bd8lb600fs_init(const struct device * dev)223 static int bd8lb600fs_init(const struct device *dev)
224 {
225 	const struct bd8lb600fs_config *config = dev->config;
226 	struct bd8lb600fs_drv_data *drv_data = dev->data;
227 
228 	if (!spi_is_ready_dt(&config->bus)) {
229 		LOG_ERR("SPI bus %s not ready", config->bus.bus->name);
230 		return -ENODEV;
231 	}
232 
233 	if (!gpio_is_ready_dt(&config->gpio_reset)) {
234 		LOG_ERR("%s: reset GPIO is not ready", dev->name);
235 		return -ENODEV;
236 	}
237 
238 	int result = k_mutex_init(&drv_data->lock);
239 
240 	if (result != 0) {
241 		LOG_ERR("unable to initialize mutex");
242 		return result;
243 	}
244 
245 	drv_data->instance_count_actual = config->gpios_count / 8;
246 
247 	if (config->gpios_count % 8 != 0) {
248 		LOG_ERR("%s: number of GPIOs %i is not a multiple of 8",
249 			dev->name, config->gpios_count);
250 		return -EINVAL;
251 	}
252 
253 	if (drv_data->instance_count_actual > 4) {
254 		LOG_ERR("%s: only a maximum of 4 devices are supported for the daisy chaining",
255 			dev->name);
256 		return -EINVAL;
257 	}
258 
259 	drv_data->gpios_count_actual = drv_data->instance_count_actual * 8;
260 
261 	result = gpio_pin_configure_dt(&config->gpio_reset, GPIO_OUTPUT_ACTIVE);
262 
263 	if (result != 0) {
264 		LOG_ERR("failed to initialize GPIO for reset");
265 		return result;
266 	}
267 
268 	k_busy_wait(WAIT_TIME_RESET_ACTIVE_IN_US);
269 	gpio_pin_set_dt(&config->gpio_reset, 0);
270 	k_busy_wait(WAIT_TIME_RESET_INACTIVE_TO_CS_IN_US);
271 
272 	return 0;
273 }
274 
275 #define BD8LB600FS_INIT(inst)                                                                      \
276 	static const struct bd8lb600fs_config bd8lb600fs_##inst##_config = {                       \
277 		.common =                                                                          \
278 			{                                                                          \
279 				.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(inst),            \
280 			},                                                                         \
281 		.bus = SPI_DT_SPEC_INST_GET(                                                       \
282 			inst, SPI_OP_MODE_MASTER | SPI_MODE_CPHA | SPI_WORD_SET(8), 0),            \
283 		.gpio_reset = GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), reset_gpios, 0),          \
284 		.gpios_count = DT_INST_PROP(inst, ngpios), \
285 	};                                                                                         \
286                                                                                                    \
287 	static struct bd8lb600fs_drv_data bd8lb600fs_##inst##_drvdata = {                          \
288 		.state = 0x00,                                                                     \
289 		.configured = 0x00,                                                                \
290 	};                                                                                         \
291                                                                                                    \
292 	/* This has to be initialized after the SPI peripheral. */                                 \
293 	DEVICE_DT_INST_DEFINE(inst, bd8lb600fs_init, NULL, &bd8lb600fs_##inst##_drvdata,           \
294 			      &bd8lb600fs_##inst##_config, POST_KERNEL,                            \
295 			      CONFIG_GPIO_BD8LB600FS_INIT_PRIORITY, &api_table);
296 
297 DT_INST_FOREACH_STATUS_OKAY(BD8LB600FS_INIT)
298