Lines Matching +full:debounce +full:- +full:up +full:- +full:ms

4  * Copyright (c) 2019-2020 Nordic Semiconductor ASA
7 * SPDX-License-Identifier: Apache-2.0
19 #include <zephyr/dt-bindings/gpio/semtech-sx1509b.h>
35 /* Reset delay is 2.5 ms, round up for Zephyr resolution */
153 * @brief Write a big-endian word to an internal address of an I2C slave.
160 * @retval -EIO General input / output error.
171 * @brief Write a big-endian byte to an internal address of an I2C slave.
178 * @retval -EIO General input / output error.
191 const struct sx1509b_config *cfg = dev->config; in sx1509b_handle_interrupt()
192 struct sx1509b_drv_data *drv_data = dev->data; in sx1509b_handle_interrupt()
197 k_sem_take(&drv_data->lock, K_FOREVER); in sx1509b_handle_interrupt()
199 ret = i2c_write_read_dt(&cfg->bus, &cmd, sizeof(cmd), in sx1509b_handle_interrupt()
208 ret = i2c_reg_write_word_be(&cfg->bus, SX1509B_REG_INTERRUPT_SOURCE, in sx1509b_handle_interrupt()
212 k_sem_give(&drv_data->lock); in sx1509b_handle_interrupt()
215 gpio_fire_callbacks(&drv_data->cb, dev, int_source); in sx1509b_handle_interrupt()
226 sx1509b_handle_interrupt(drv_data->dev); in sx1509b_work_handler()
238 k_work_submit(&drv_data->work); in sx1509_int_cb()
253 pin_buf.pins.input_disable = sys_cpu_to_be16(pins->input_disable); in write_pin_state()
254 pin_buf.pins.long_slew = sys_cpu_to_be16(pins->long_slew); in write_pin_state()
255 pin_buf.pins.low_drive = sys_cpu_to_be16(pins->low_drive); in write_pin_state()
256 pin_buf.pins.pull_up = sys_cpu_to_be16(pins->pull_up); in write_pin_state()
257 pin_buf.pins.pull_down = sys_cpu_to_be16(pins->pull_down); in write_pin_state()
258 pin_buf.pins.open_drain = sys_cpu_to_be16(pins->open_drain); in write_pin_state()
259 pin_buf.pins.polarity = sys_cpu_to_be16(pins->polarity); in write_pin_state()
260 pin_buf.pins.dir = sys_cpu_to_be16(pins->dir); in write_pin_state()
261 pin_buf.pins.data = sys_cpu_to_be16(pins->data); in write_pin_state()
264 rc = i2c_reg_write_word_be(&cfg->bus, SX1509B_REG_DATA, in write_pin_state()
265 pins->data); in write_pin_state()
267 rc = i2c_write_dt(&cfg->bus, &pin_buf.reg, in write_pin_state()
268 sizeof(pin_buf) - sizeof(pins->data)); in write_pin_state()
271 rc = i2c_write_dt(&cfg->bus, &pin_buf.reg, sizeof(pin_buf)); in write_pin_state()
281 const struct sx1509b_config *cfg = dev->config; in sx1509b_config()
282 struct sx1509b_drv_data *drv_data = dev->data; in sx1509b_config()
283 struct sx1509b_pin_state *pins = &drv_data->pin_state; in sx1509b_config()
284 struct sx1509b_debounce_state *debounce = &drv_data->debounce_state; in sx1509b_config() local
290 return -EWOULDBLOCK; in sx1509b_config()
293 k_sem_take(&drv_data->lock, K_FOREVER); in sx1509b_config()
295 if (drv_data->led_drv_enable & BIT(pin)) { in sx1509b_config()
297 drv_data->led_drv_enable &= ~BIT(pin); in sx1509b_config()
298 rc = i2c_reg_write_word_be(&cfg->bus, in sx1509b_config()
300 drv_data->led_drv_enable); in sx1509b_config()
307 pins->open_drain &= ~BIT(pin); in sx1509b_config()
310 pins->open_drain |= BIT(pin); in sx1509b_config()
313 rc = -ENOTSUP; in sx1509b_config()
319 pins->pull_up |= BIT(pin); in sx1509b_config()
321 pins->pull_up &= ~BIT(pin); in sx1509b_config()
324 pins->pull_down |= BIT(pin); in sx1509b_config()
326 pins->pull_down &= ~BIT(pin); in sx1509b_config()
330 pins->input_disable &= ~BIT(pin); in sx1509b_config()
332 pins->input_disable |= BIT(pin); in sx1509b_config()
336 pins->dir &= ~BIT(pin); in sx1509b_config()
338 pins->data &= ~BIT(pin); in sx1509b_config()
341 pins->data |= BIT(pin); in sx1509b_config()
345 pins->dir |= BIT(pin); in sx1509b_config()
349 debounce->debounce_enable |= BIT(pin); in sx1509b_config()
351 debounce->debounce_enable &= ~BIT(pin); in sx1509b_config()
356 pins->input_disable, pins->pull_up, pins->pull_down, in sx1509b_config()
357 pins->dir, pins->data); in sx1509b_config()
364 struct sx1509b_debounce_state debounce; in sx1509b_config() member
368 debounce_buf.debounce.debounce_config in sx1509b_config()
369 = debounce->debounce_config; in sx1509b_config()
370 debounce_buf.debounce.debounce_enable in sx1509b_config()
371 = sys_cpu_to_be16(debounce->debounce_enable); in sx1509b_config()
373 rc = i2c_write_dt(&cfg->bus, &debounce_buf.reg, in sx1509b_config()
378 k_sem_give(&drv_data->lock); in sx1509b_config()
385 const struct sx1509b_config *cfg = dev->config; in port_get()
386 struct sx1509b_drv_data *drv_data = dev->data; in port_get()
392 return -EWOULDBLOCK; in port_get()
395 k_sem_take(&drv_data->lock, K_FOREVER); in port_get()
399 rc = i2c_write_read_dt(&cfg->bus, &cmd, sizeof(cmd), &pin_data, in port_get()
409 k_sem_give(&drv_data->lock); in port_get()
420 return -EWOULDBLOCK; in port_write()
423 const struct sx1509b_config *cfg = dev->config; in port_write()
424 struct sx1509b_drv_data *drv_data = dev->data; in port_write()
425 void *data = &drv_data->pin_state.data; in port_write()
430 k_sem_take(&drv_data->lock, K_FOREVER); in port_write()
434 int rc = i2c_reg_write_word_be(&cfg->bus, SX1509B_REG_DATA, out); in port_write()
439 k_sem_give(&drv_data->lock); in port_write()
479 /* Device does not support level-triggered interrupts. */ in pin_interrupt_configure()
481 return -ENOTSUP; in pin_interrupt_configure()
484 const struct sx1509b_config *cfg = dev->config; in pin_interrupt_configure()
485 struct sx1509b_drv_data *drv_data = dev->data; in pin_interrupt_configure()
486 struct sx1509b_irq_state *irq = &drv_data->irq_state; in pin_interrupt_configure()
497 return -ENOTSUP; in pin_interrupt_configure()
500 return -ENOTSUP; in pin_interrupt_configure()
503 k_sem_take(&drv_data->lock, K_FOREVER); in pin_interrupt_configure()
505 irq->interrupt_sense &= ~(SX1509B_EDGE_BOTH << (pin * 2)); in pin_interrupt_configure()
507 irq->interrupt_mask |= BIT(pin); in pin_interrupt_configure()
509 irq->interrupt_mask &= ~BIT(pin); in pin_interrupt_configure()
511 irq->interrupt_sense |= (SX1509B_EDGE_BOTH << in pin_interrupt_configure()
514 irq->interrupt_sense |= (SX1509B_EDGE_FALLING << in pin_interrupt_configure()
517 irq->interrupt_sense |= (SX1509B_EDGE_RISING << in pin_interrupt_configure()
523 irq_buf.irq.interrupt_mask = sys_cpu_to_be16(irq->interrupt_mask); in pin_interrupt_configure()
524 irq_buf.irq.interrupt_sense = sys_cpu_to_be32(irq->interrupt_sense); in pin_interrupt_configure()
526 rc = i2c_write_dt(&cfg->bus, &irq_buf.reg, sizeof(irq_buf)); in pin_interrupt_configure()
528 k_sem_give(&drv_data->lock); in pin_interrupt_configure()
542 const struct sx1509b_config *cfg = dev->config; in sx1509b_init()
543 struct sx1509b_drv_data *drv_data = dev->data; in sx1509b_init()
546 if (!device_is_ready(cfg->bus.bus)) { in sx1509b_init()
548 rc = -ENODEV; in sx1509b_init()
553 drv_data->dev = dev; in sx1509b_init()
555 if (!gpio_is_ready_dt(&cfg->nint_gpio)) { in sx1509b_init()
556 rc = -ENODEV; in sx1509b_init()
559 k_work_init(&drv_data->work, sx1509b_work_handler); in sx1509b_init()
561 gpio_pin_configure_dt(&cfg->nint_gpio, GPIO_INPUT); in sx1509b_init()
562 gpio_pin_interrupt_configure_dt(&cfg->nint_gpio, in sx1509b_init()
565 gpio_init_callback(&drv_data->gpio_cb, sx1509_int_cb, in sx1509b_init()
566 BIT(cfg->nint_gpio.pin)); in sx1509b_init()
567 gpio_add_callback(cfg->nint_gpio.port, &drv_data->gpio_cb); in sx1509b_init()
569 drv_data->irq_state = (struct sx1509b_irq_state) { in sx1509b_init()
574 rc = i2c_reg_write_byte_dt(&cfg->bus, SX1509B_REG_RESET, in sx1509b_init()
577 LOG_ERR("%s: reset m0 failed: %d\n", dev->name, rc); in sx1509b_init()
580 rc = i2c_reg_write_byte_dt(&cfg->bus, SX1509B_REG_RESET, in sx1509b_init()
589 drv_data->pin_state = (struct sx1509b_pin_state) { in sx1509b_init()
596 drv_data->debounce_state = (struct sx1509b_debounce_state) { in sx1509b_init()
600 rc = i2c_reg_write_byte_dt(&cfg->bus, SX1509B_REG_CLOCK, in sx1509b_init()
603 rc = i2c_reg_write_word_be(&cfg->bus, SX1509B_REG_DATA, in sx1509b_init()
604 drv_data->pin_state.data); in sx1509b_init()
607 rc = i2c_reg_write_word_be(&cfg->bus, SX1509B_REG_DIR, in sx1509b_init()
608 drv_data->pin_state.dir); in sx1509b_init()
611 rc = i2c_reg_write_byte_be(&cfg->bus, SX1509B_REG_MISC, in sx1509b_init()
619 LOG_ERR("%s init failed: %d", dev->name, rc); in sx1509b_init()
621 LOG_INF("%s init ok", dev->name); in sx1509b_init()
623 k_sem_give(&drv_data->lock); in sx1509b_init()
632 struct sx1509b_drv_data *data = dev->data; in gpio_sx1509b_manage_callback()
634 return gpio_manage_callback(&data->cb, callback, set); in gpio_sx1509b_manage_callback()
654 const struct sx1509b_config *cfg = dev->config; in sx1509b_led_intensity_pin_configure()
655 struct sx1509b_drv_data *drv_data = dev->data; in sx1509b_led_intensity_pin_configure()
656 struct sx1509b_pin_state *pins = &drv_data->pin_state; in sx1509b_led_intensity_pin_configure()
661 return -EWOULDBLOCK; in sx1509b_led_intensity_pin_configure()
665 return -ERANGE; in sx1509b_led_intensity_pin_configure()
668 k_sem_take(&drv_data->lock, K_FOREVER); in sx1509b_led_intensity_pin_configure()
671 drv_data->led_drv_enable |= BIT(pin); in sx1509b_led_intensity_pin_configure()
672 rc = i2c_reg_write_word_be(&cfg->bus, SX1509B_REG_LED_DRV_ENABLE, in sx1509b_led_intensity_pin_configure()
673 drv_data->led_drv_enable); in sx1509b_led_intensity_pin_configure()
677 rc = i2c_reg_write_byte_be(&cfg->bus, intensity_registers[pin], 0); in sx1509b_led_intensity_pin_configure()
682 pins->input_disable |= BIT(pin); in sx1509b_led_intensity_pin_configure()
683 pins->pull_up &= ~BIT(pin); in sx1509b_led_intensity_pin_configure()
684 pins->dir &= ~BIT(pin); in sx1509b_led_intensity_pin_configure()
685 pins->data &= ~BIT(pin); in sx1509b_led_intensity_pin_configure()
692 k_sem_give(&drv_data->lock); in sx1509b_led_intensity_pin_configure()
699 const struct sx1509b_config *cfg = dev->config; in sx1509b_led_intensity_pin_set()
700 struct sx1509b_drv_data *drv_data = dev->data; in sx1509b_led_intensity_pin_set()
705 return -EWOULDBLOCK; in sx1509b_led_intensity_pin_set()
709 return -ERANGE; in sx1509b_led_intensity_pin_set()
712 k_sem_take(&drv_data->lock, K_FOREVER); in sx1509b_led_intensity_pin_set()
714 rc = i2c_reg_write_byte_be(&cfg->bus, intensity_registers[pin], in sx1509b_led_intensity_pin_set()
717 k_sem_give(&drv_data->lock); in sx1509b_led_intensity_pin_set()