Lines Matching +full:gpio +full:- +full:n

4  * SPDX-License-Identifier: Apache-2.0
13 #include <zephyr/drivers/gpio.h>
66 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_irq_setup()
71 const struct gpio_dt_spec *gpio = &cfg->ab_gpio[i]; in gpio_qdec_irq_setup() local
73 ret = gpio_pin_interrupt_configure_dt(gpio, flags); in gpio_qdec_irq_setup()
83 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_idle_polling_mode()
85 if (cfg->idle_poll_time_us > 0) { in gpio_qdec_idle_polling_mode()
94 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_poll_mode()
95 struct gpio_qdec_data *data = dev->data; in gpio_qdec_poll_mode()
101 k_timer_start(&data->sample_timer, K_NO_WAIT, in gpio_qdec_poll_mode()
102 K_USEC(cfg->sample_time_us)); in gpio_qdec_poll_mode()
104 atomic_set(&data->polling, 1); in gpio_qdec_poll_mode()
111 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_idle_mode()
112 struct gpio_qdec_data *data = dev->data; in gpio_qdec_idle_mode()
115 k_timer_start(&data->sample_timer, K_NO_WAIT, in gpio_qdec_idle_mode()
116 K_USEC(cfg->idle_poll_time_us)); in gpio_qdec_idle_mode()
118 k_timer_stop(&data->sample_timer); in gpio_qdec_idle_mode()
122 atomic_set(&data->polling, 0); in gpio_qdec_idle_mode()
129 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_get_step()
133 for (int i = 0; i < cfg->led_gpio_count; i++) { in gpio_qdec_get_step()
134 gpio_pin_set_dt(&cfg->led_gpio[i], 1); in gpio_qdec_get_step()
137 k_busy_wait(cfg->led_pre_us); in gpio_qdec_get_step()
140 if (gpio_pin_get_dt(&cfg->ab_gpio[0])) { in gpio_qdec_get_step()
143 if (gpio_pin_get_dt(&cfg->ab_gpio[1])) { in gpio_qdec_get_step()
148 for (int i = 0; i < cfg->led_gpio_count; i++) { in gpio_qdec_get_step()
149 gpio_pin_set_dt(&cfg->led_gpio[i], 0); in gpio_qdec_get_step()
159 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_sample_timer_timeout()
160 struct gpio_qdec_data *data = dev->data; in gpio_qdec_sample_timer_timeout()
166 if (atomic_get(&data->suspended) == 1) { in gpio_qdec_sample_timer_timeout()
173 if (data->prev_step == step) { in gpio_qdec_sample_timer_timeout()
178 atomic_get(&data->polling) == 0) { in gpio_qdec_sample_timer_timeout()
182 switch ((data->prev_step << 4U) | step) { in gpio_qdec_sample_timer_timeout()
193 delta = -1; in gpio_qdec_sample_timer_timeout()
196 LOG_WRN("%s: lost steps", dev->name); in gpio_qdec_sample_timer_timeout()
199 data->prev_step = step; in gpio_qdec_sample_timer_timeout()
202 data->acc += delta; in gpio_qdec_sample_timer_timeout()
205 if (abs(data->acc) >= cfg->steps_per_period) { in gpio_qdec_sample_timer_timeout()
206 k_work_submit(&data->event_work); in gpio_qdec_sample_timer_timeout()
209 k_work_reschedule(&data->idle_work, K_MSEC(cfg->idle_timeout_ms)); in gpio_qdec_sample_timer_timeout()
216 const struct device *dev = data->dev; in gpio_qdec_event_worker()
217 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_event_worker()
222 acc = data->acc / cfg->steps_per_period; in gpio_qdec_event_worker()
223 data->acc -= acc * cfg->steps_per_period; in gpio_qdec_event_worker()
227 input_report_rel(data->dev, cfg->axis, acc, true, K_FOREVER); in gpio_qdec_event_worker()
236 const struct device *dev = data->dev; in gpio_qdec_idle_worker()
246 const struct device *dev = data->dev; in gpio_qdec_cb()
253 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_init()
254 struct gpio_qdec_data *data = dev->data; in gpio_qdec_init()
257 data->dev = dev; in gpio_qdec_init()
259 k_work_init(&data->event_work, gpio_qdec_event_worker); in gpio_qdec_init()
260 k_work_init_delayable(&data->idle_work, gpio_qdec_idle_worker); in gpio_qdec_init()
262 k_timer_init(&data->sample_timer, gpio_qdec_sample_timer_timeout, NULL); in gpio_qdec_init()
263 k_timer_user_data_set(&data->sample_timer, (void *)dev); in gpio_qdec_init()
265 gpio_init_callback(&data->gpio_cb, gpio_qdec_cb, in gpio_qdec_init()
266 BIT(cfg->ab_gpio[0].pin) | BIT(cfg->ab_gpio[1].pin)); in gpio_qdec_init()
268 const struct gpio_dt_spec *gpio = &cfg->ab_gpio[i]; in gpio_qdec_init() local
270 if (!gpio_is_ready_dt(gpio)) { in gpio_qdec_init()
271 LOG_ERR("%s is not ready", gpio->port->name); in gpio_qdec_init()
272 return -ENODEV; in gpio_qdec_init()
275 ret = gpio_pin_configure_dt(gpio, GPIO_INPUT); in gpio_qdec_init()
285 ret = gpio_add_callback_dt(gpio, &data->gpio_cb); in gpio_qdec_init()
287 LOG_ERR("Could not set gpio callback"); in gpio_qdec_init()
292 for (int i = 0; i < cfg->led_gpio_count; i++) { in gpio_qdec_init()
293 const struct gpio_dt_spec *gpio = &cfg->led_gpio[i]; in gpio_qdec_init() local
296 if (!gpio_is_ready_dt(gpio)) { in gpio_qdec_init()
297 LOG_ERR("%s is not ready", gpio->port->name); in gpio_qdec_init()
298 return -ENODEV; in gpio_qdec_init()
304 ret = gpio_pin_configure_dt(gpio, mode); in gpio_qdec_init()
311 data->prev_step = gpio_qdec_get_step(dev); in gpio_qdec_init()
321 LOG_DBG("Device %s initialized", dev->name); in gpio_qdec_init()
329 const struct gpio_qdec_config *cfg = dev->config; in gpio_qdec_pin_suspend()
334 const struct gpio_dt_spec *gpio = &cfg->ab_gpio[i]; in gpio_qdec_pin_suspend() local
336 ret = gpio_pin_configure_dt(gpio, mode); in gpio_qdec_pin_suspend()
343 for (int i = 0; i < cfg->led_gpio_count; i++) { in gpio_qdec_pin_suspend()
345 gpio_pin_set_dt(&cfg->led_gpio[i], 0); in gpio_qdec_pin_suspend()
347 gpio_pin_set_dt(&cfg->led_gpio[i], 1); in gpio_qdec_pin_suspend()
355 struct gpio_qdec_data *data = dev->data; in gpio_qdec_pm_action()
361 atomic_set(&data->suspended, 1); in gpio_qdec_pm_action()
363 k_work_cancel_delayable_sync(&data->idle_work, &sync); in gpio_qdec_pm_action()
369 k_timer_stop(&data->sample_timer); in gpio_qdec_pm_action()
375 atomic_set(&data->suspended, 0); in gpio_qdec_pm_action()
379 data->prev_step = gpio_qdec_get_step(dev); in gpio_qdec_pm_action()
380 data->acc = 0; in gpio_qdec_pm_action()
386 return -ENOTSUP; in gpio_qdec_pm_action()
393 #define QDEC_GPIO_INIT(n) \ argument
394 BUILD_ASSERT(DT_INST_PROP_LEN(n, gpios) == GPIO_QDEC_GPIO_NUM, \
397 BUILD_ASSERT(!(DT_INST_NODE_HAS_PROP(n, led_gpios) && \
398 DT_INST_NODE_HAS_PROP(n, idle_poll_time_us)) || \
399 DT_INST_NODE_HAS_PROP(n, led_pre_us), \
400 "led-pre-us must be specified when setting led-gpios and " \
401 "idle-poll-time-us"); \
403 IF_ENABLED(DT_INST_NODE_HAS_PROP(n, led_gpios), ( \
404 static const struct gpio_dt_spec gpio_qdec_led_gpio_##n[] = { \
405 DT_INST_FOREACH_PROP_ELEM_SEP(n, led_gpios, \
410 static const struct gpio_qdec_config gpio_qdec_cfg_##n = { \
412 GPIO_DT_SPEC_INST_GET_BY_IDX(n, gpios, 0), \
413 GPIO_DT_SPEC_INST_GET_BY_IDX(n, gpios, 1), \
415 IF_ENABLED(DT_INST_NODE_HAS_PROP(n, led_gpios), ( \
416 .led_gpio = gpio_qdec_led_gpio_##n, \
417 .led_gpio_count = ARRAY_SIZE(gpio_qdec_led_gpio_##n), \
418 .led_pre_us = DT_INST_PROP_OR(n, led_pre_us, 0), \
420 .sample_time_us = DT_INST_PROP(n, sample_time_us), \
421 .idle_poll_time_us = DT_INST_PROP_OR(n, idle_poll_time_us, 0), \
422 .idle_timeout_ms = DT_INST_PROP(n, idle_timeout_ms), \
423 .steps_per_period = DT_INST_PROP(n, steps_per_period), \
424 .axis = DT_INST_PROP(n, zephyr_axis), \
427 static struct gpio_qdec_data gpio_qdec_data_##n; \
429 PM_DEVICE_DT_INST_DEFINE(n, gpio_qdec_pm_action); \
431 DEVICE_DT_INST_DEFINE(n, gpio_qdec_init, PM_DEVICE_DT_INST_GET(n), \
432 &gpio_qdec_data_##n, \
433 &gpio_qdec_cfg_##n, \