1 /*
2 * Copyright (c) 2021 Abel Sensors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/gpio.h>
8 #include <zephyr/drivers/i2c.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/gpio/gpio_utils.h>
11 #include <zephyr/logging/log.h>
12
13 LOG_MODULE_REGISTER(fxl6408, CONFIG_FXL6408_LOG_LEVEL);
14 #define DT_DRV_COMPAT fcs_fxl6408
15
16 /* Register definitions */
17 #define REG_DEVICE_ID_CTRL 0x01
18 #define REG_DIRECTION 0x03
19 #define REG_OUTPUT 0x05
20 #define REG_OUTPUT_HIGH_Z 0x07
21 #define REG_INPUT_DEFAULT_STATE 0x09
22 #define REG_PUD_EN 0x0B
23 #define REG_PUD_SEL 0x0D
24 #define REG_INPUT_VALUE 0x0F
25 #define REG_INT_MASK 0x11
26 #define REG_INT_STATUS 0x13
27
28 #define SUPPORTED_FLAGS (GPIO_INPUT | GPIO_OUTPUT | GPIO_OUTPUT_INIT_LOW |\
29 GPIO_OUTPUT_INIT_HIGH | GPIO_PULL_DOWN | GPIO_PULL_UP |\
30 GPIO_ACTIVE_HIGH | GPIO_ACTIVE_LOW)
31
32 /** Configuration data*/
33 struct gpio_fxl6408_config {
34 /* gpio_driver_config needs to be first */
35 struct gpio_driver_config common;
36
37 /** Controller I2C DT specification */
38 struct i2c_dt_spec i2c;
39 };
40
41 /** Runtime driver data */
42 struct gpio_fxl6408_drv_data {
43 /* gpio_driver_data needs to be first */
44 struct gpio_driver_data common;
45
46 struct {
47 uint8_t input;
48 uint8_t output;
49 uint8_t dir;
50 uint8_t high_z;
51 uint8_t pud_en;
52 uint8_t pud_sel;
53 } reg_cache;
54
55 struct k_sem lock;
56 };
57
58 /**
59 * @brief Read the port of certain register function.
60 *
61 * @param dev Device struct of the FXL6408.
62 * @param reg Register to read.
63 * @param cache Pointer to the cache to be updated after successful read.
64 *
65 * @return 0 if successful, failed otherwise.
66 */
read_port_regs(const struct device * dev,uint8_t reg,uint8_t * cache)67 static int read_port_regs(const struct device *dev, uint8_t reg, uint8_t *cache)
68 {
69 const struct gpio_fxl6408_config *const config = dev->config;
70 uint8_t port_data;
71 int ret;
72
73 ret = i2c_reg_read_byte_dt(&config->i2c, reg, &port_data);
74 if (ret != 0) {
75 LOG_ERR("Error reading register 0x%X (%d)", reg, ret);
76 return ret;
77 }
78 *cache = port_data;
79 LOG_DBG("Read: REG[0x%X] = 0x%X", reg, *cache);
80
81 return ret;
82 }
83
84 /**
85 * @brief Write to the port registers of certain register function.
86 *
87 * @param dev Device struct of the FXL6408.
88 * @param reg Register to write into. Possible values: REG_DEVICE_ID_CTRL,
89 * REG_OUTPUT, REG_DIRECTION, REG_PUD_SEL, REG_PUD_EN, REG_OUTPUT_HIGH_Z and
90 * REG_INPUT_DEFAULT.
91 * @param cache Pointer to the cache to be updated after successful write.
92 * @param value New value to set.
93 *
94 * @return 0 if successful, failed otherwise.
95 */
write_port_regs(const struct device * dev,uint8_t reg,uint8_t * cache,uint8_t value)96 static int write_port_regs(const struct device *dev, uint8_t reg,
97 uint8_t *cache, uint8_t value)
98 {
99 const struct gpio_fxl6408_config *const config = dev->config;
100 int ret = 0;
101
102 if (*cache != value) {
103 ret = i2c_reg_write_byte_dt(&config->i2c, reg, value);
104 if (ret != 0) {
105 LOG_ERR("error writing to register 0x%X (%d)",
106 reg, ret);
107 return ret;
108 }
109 *cache = value;
110 LOG_DBG("Write: REG[0x%X] = 0x%X", reg, *cache);
111 }
112
113 return ret;
114 }
115
update_input_regs(const struct device * dev,uint8_t * buf)116 static inline int update_input_regs(const struct device *dev, uint8_t *buf)
117 {
118 struct gpio_fxl6408_drv_data *const drv_data =
119 (struct gpio_fxl6408_drv_data *const)dev->data;
120 int ret = read_port_regs(dev, REG_INPUT_VALUE,
121 &drv_data->reg_cache.input);
122 *buf = drv_data->reg_cache.input;
123
124 return ret;
125 }
126
update_output_regs(const struct device * dev,uint8_t value)127 static inline int update_output_regs(const struct device *dev, uint8_t value)
128 {
129 struct gpio_fxl6408_drv_data *const drv_data =
130 (struct gpio_fxl6408_drv_data *const)dev->data;
131
132 return write_port_regs(dev, REG_OUTPUT,
133 &drv_data->reg_cache.output, value);
134 }
135
update_high_z_regs(const struct device * dev,uint8_t value)136 static inline int update_high_z_regs(const struct device *dev, uint8_t value)
137 {
138 struct gpio_fxl6408_drv_data *const drv_data =
139 (struct gpio_fxl6408_drv_data *const)dev->data;
140
141 return write_port_regs(dev, REG_OUTPUT_HIGH_Z,
142 &drv_data->reg_cache.high_z, value);
143 }
144
update_direction_regs(const struct device * dev,uint8_t value)145 static inline int update_direction_regs(const struct device *dev, uint8_t value)
146 {
147 struct gpio_fxl6408_drv_data *const drv_data =
148 (struct gpio_fxl6408_drv_data *const)dev->data;
149
150 return write_port_regs(dev, REG_DIRECTION,
151 &drv_data->reg_cache.dir, value);
152 }
153
update_pul_sel_regs(const struct device * dev,uint8_t value)154 static inline int update_pul_sel_regs(const struct device *dev, uint8_t value)
155 {
156 struct gpio_fxl6408_drv_data *const drv_data =
157 (struct gpio_fxl6408_drv_data *const)dev->data;
158
159 return write_port_regs(dev, REG_PUD_SEL,
160 &drv_data->reg_cache.pud_sel, value);
161 }
162
update_pul_en_regs(const struct device * dev,uint8_t value)163 static inline int update_pul_en_regs(const struct device *dev, uint8_t value)
164 {
165 struct gpio_fxl6408_drv_data *const drv_data =
166 (struct gpio_fxl6408_drv_data *const)dev->data;
167
168 return write_port_regs(dev, REG_PUD_EN,
169 &drv_data->reg_cache.pud_en, value);
170 }
171
setup_pin_dir(const struct device * dev,uint32_t pin,int flags)172 static int setup_pin_dir(const struct device *dev, uint32_t pin, int flags)
173 {
174 struct gpio_fxl6408_drv_data *const drv_data =
175 (struct gpio_fxl6408_drv_data *const)dev->data;
176 uint8_t reg_dir = drv_data->reg_cache.dir;
177 uint8_t reg_out = drv_data->reg_cache.output;
178 uint8_t reg_high_z = drv_data->reg_cache.high_z;
179 int ret;
180
181 if (((flags & GPIO_INPUT) != 0) && ((flags & GPIO_OUTPUT) != 0)) {
182 return -ENOTSUP;
183 }
184
185 /* Update the driver data to the actual situation of the FXL6408 */
186 if (flags & GPIO_OUTPUT) {
187 if ((flags & GPIO_OUTPUT_INIT_HIGH)) {
188 reg_out |= BIT(pin);
189 } else if ((flags & GPIO_OUTPUT_INIT_LOW)) {
190 reg_out &= ~BIT(pin);
191 }
192 reg_dir |= BIT(pin);
193 reg_high_z &= ~BIT(pin);
194 } else if (flags & GPIO_INPUT) {
195 reg_dir &= ~BIT(pin);
196 reg_high_z &= ~BIT(pin);
197 } else {
198 reg_high_z |= BIT(pin);
199 reg_dir |= BIT(pin);
200 }
201
202 ret = update_output_regs(dev, reg_out);
203 if (ret != 0) {
204 return ret;
205 }
206
207 ret = update_high_z_regs(dev, reg_high_z);
208 if (ret != 0) {
209 return ret;
210 }
211
212 ret = update_direction_regs(dev, reg_dir);
213 return ret;
214 }
215
216 /**
217 * @brief Setup the pin pull up/pull down status
218 *
219 * @param dev Device struct of the FXL6408
220 * @param pin The pin number
221 * @param flags Flags of pin or port
222 *
223 * @return 0 if successful, failed otherwise
224 */
setup_pin_pullupdown(const struct device * dev,uint32_t pin,int flags)225 static int setup_pin_pullupdown(const struct device *dev, uint32_t pin,
226 int flags)
227 {
228 struct gpio_fxl6408_drv_data *const drv_data =
229 (struct gpio_fxl6408_drv_data *const)dev->data;
230 uint8_t reg_pud;
231 int ret;
232
233 /* If disabling pull up/down, there is no need to set the selection
234 * register. Just go straight to disabling.
235 */
236 if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0U) {
237 /* Setup pin pull up or pull down */
238 reg_pud = drv_data->reg_cache.pud_sel;
239
240 /* Pull down == 0, pull up == 1 */
241 WRITE_BIT(reg_pud, pin, (flags & GPIO_PULL_UP) != 0U);
242
243 ret = update_pul_sel_regs(dev, reg_pud);
244 if (ret != 0) {
245 return ret;
246 }
247 }
248
249 /* Enable/disable pull up/down */
250 reg_pud = drv_data->reg_cache.pud_en;
251
252 WRITE_BIT(reg_pud, pin,
253 (flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0U);
254
255 ret = update_pul_en_regs(dev, reg_pud);
256 return ret;
257 }
258
gpio_fxl6408_pin_config(const struct device * dev,gpio_pin_t pin,gpio_flags_t flags)259 static int gpio_fxl6408_pin_config(const struct device *dev, gpio_pin_t pin,
260 gpio_flags_t flags)
261 {
262 struct gpio_fxl6408_drv_data *const drv_data =
263 (struct gpio_fxl6408_drv_data *const)dev->data;
264 int ret;
265
266 /* Check if supported flag is set */
267 if ((flags & ~SUPPORTED_FLAGS) != 0) {
268 return -ENOTSUP;
269 }
270
271 /* Can't do I2C bus operations from an ISR */
272 if (k_is_in_isr()) {
273 return -EWOULDBLOCK;
274 }
275 k_sem_take(&drv_data->lock, K_FOREVER);
276
277 ret = setup_pin_dir(dev, pin, flags);
278 if (ret != 0) {
279 LOG_ERR("error setting pin direction (%d)", ret);
280 goto done;
281 }
282
283 ret = setup_pin_pullupdown(dev, pin, flags);
284 if (ret) {
285 LOG_ERR("error setting pin pull up/down (%d)", ret);
286 goto done;
287 }
288
289 done:
290 k_sem_give(&drv_data->lock);
291 return ret;
292 }
293
gpio_fxl6408_port_get_raw(const struct device * dev,uint32_t * value)294 static int gpio_fxl6408_port_get_raw(const struct device *dev, uint32_t *value)
295 {
296 struct gpio_fxl6408_drv_data *const drv_data =
297 (struct gpio_fxl6408_drv_data *const)dev->data;
298 uint8_t buf = 0;
299 int ret = 0;
300
301 /* Can't do I2C bus operations from an ISR */
302 if (k_is_in_isr()) {
303 return -EWOULDBLOCK;
304 }
305
306 k_sem_take(&drv_data->lock, K_FOREVER);
307
308 ret = update_input_regs(dev, &buf);
309 if (ret != 0) {
310 goto done;
311 }
312 *value = buf;
313
314 done:
315 k_sem_give(&drv_data->lock);
316 return ret;
317 }
318
gpio_fxl6408_port_set_masked_raw(const struct device * dev,uint32_t mask,uint32_t value)319 static int gpio_fxl6408_port_set_masked_raw(const struct device *dev,
320 uint32_t mask, uint32_t value)
321 {
322 struct gpio_fxl6408_drv_data *const drv_data =
323 (struct gpio_fxl6408_drv_data *const)dev->data;
324 uint8_t reg_out;
325 int ret;
326
327 /* Can't do I2C bus operations from an ISR */
328 if (k_is_in_isr()) {
329 return -EWOULDBLOCK;
330 }
331
332 k_sem_take(&drv_data->lock, K_FOREVER);
333
334 reg_out = drv_data->reg_cache.output;
335 reg_out = (reg_out & ~mask) | (mask & value);
336
337 ret = update_output_regs(dev, reg_out);
338
339 k_sem_give(&drv_data->lock);
340
341 return ret;
342 }
343
gpio_fxl6408_port_set_bits_raw(const struct device * dev,uint32_t mask)344 static int gpio_fxl6408_port_set_bits_raw(const struct device *dev,
345 uint32_t mask)
346 {
347 return gpio_fxl6408_port_set_masked_raw(dev, mask, mask);
348 }
349
gpio_fxl6408_port_clear_bits_raw(const struct device * dev,uint32_t mask)350 static int gpio_fxl6408_port_clear_bits_raw(const struct device *dev,
351 uint32_t mask)
352 {
353 return gpio_fxl6408_port_set_masked_raw(dev, mask, 0);
354 }
355
gpio_fxl6408_port_toggle_bits(const struct device * dev,uint32_t mask)356 static int gpio_fxl6408_port_toggle_bits(const struct device *dev,
357 uint32_t mask)
358 {
359 struct gpio_fxl6408_drv_data *const drv_data =
360 (struct gpio_fxl6408_drv_data *const)dev->data;
361 uint8_t reg_out;
362 int ret;
363
364 /* Can't do I2C bus operations from an ISR */
365 if (k_is_in_isr()) {
366 return -EWOULDBLOCK;
367 }
368
369 k_sem_take(&drv_data->lock, K_FOREVER);
370
371 reg_out = drv_data->reg_cache.output;
372 reg_out ^= mask;
373 ret = update_output_regs(dev, reg_out);
374
375 k_sem_give(&drv_data->lock);
376
377 return ret;
378 }
379
gpio_fxl6408_init(const struct device * dev)380 int gpio_fxl6408_init(const struct device *dev)
381 {
382 struct gpio_fxl6408_drv_data *const drv_data =
383 (struct gpio_fxl6408_drv_data *const)dev->data;
384 const struct gpio_fxl6408_config *const config = dev->config;
385
386 if (!device_is_ready(config->i2c.bus)) {
387 LOG_ERR("%s is not ready", config->i2c.bus->name);
388 return -ENODEV;
389 }
390
391 k_sem_init(&drv_data->lock, 1, 1);
392
393 return 0;
394 }
395
396 static const struct gpio_driver_api gpio_fxl_driver = {
397 .pin_configure = gpio_fxl6408_pin_config,
398 .port_get_raw = gpio_fxl6408_port_get_raw,
399 .port_set_masked_raw = gpio_fxl6408_port_set_masked_raw,
400 .port_set_bits_raw = gpio_fxl6408_port_set_bits_raw,
401 .port_clear_bits_raw = gpio_fxl6408_port_clear_bits_raw,
402 .port_toggle_bits = gpio_fxl6408_port_toggle_bits,
403 };
404
405 #define GPIO_FXL6408_DEVICE_INSTANCE(inst) \
406 static const struct gpio_fxl6408_config gpio_fxl6408_##inst##_cfg = { \
407 .common = { \
408 .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(inst),\
409 }, \
410 .i2c = I2C_DT_SPEC_INST_GET(inst) \
411 }; \
412 \
413 static struct gpio_fxl6408_drv_data gpio_fxl6408_##inst##_drvdata = { \
414 .reg_cache = { \
415 .input = 0x0, \
416 .output = 0x00, \
417 .dir = 0x0, \
418 .high_z = 0xFF, \
419 .pud_en = 0xFF, \
420 .pud_sel = 0x0 \
421 } \
422 }; \
423 \
424 DEVICE_DT_INST_DEFINE(inst, gpio_fxl6408_init, NULL, \
425 &gpio_fxl6408_##inst##_drvdata, \
426 &gpio_fxl6408_##inst##_cfg, POST_KERNEL, \
427 CONFIG_GPIO_FXL6408_INIT_PRIORITY, \
428 &gpio_fxl_driver);
429
430 DT_INST_FOREACH_STATUS_OKAY(GPIO_FXL6408_DEVICE_INSTANCE)
431