|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Yonatan Schachter |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <drivers/gpio.h> |
| 9 | + |
| 10 | +#pragma GCC diagnostic push |
| 11 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 12 | +#include <hardware/gpio.h> |
| 13 | +#include <hardware/regs/intctrl.h> |
| 14 | +#include <hardware/structs/iobank0.h> |
| 15 | +#pragma GCC diagnostic pop |
| 16 | + |
| 17 | +#include "gpio_utils.h" |
| 18 | + |
| 19 | +#define DT_DRV_COMPAT raspberrypi_pico_gpio |
| 20 | + |
| 21 | +#define ALL_EVENTS (GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE \ |
| 22 | +| GPIO_IRQ_LEVEL_LOW | GPIO_IRQ_LEVEL_HIGH) |
| 23 | + |
| 24 | +struct gpio_raspberrypi_config { |
| 25 | +struct gpio_driver_config common; |
| 26 | +void (*bank_config_func)(void); |
| 27 | +}; |
| 28 | + |
| 29 | +struct gpio_raspberrypi_data { |
| 30 | +struct gpio_driver_data common; |
| 31 | +sys_slist_t callbacks; |
| 32 | +uint32_t int_enabled_mask; |
| 33 | +}; |
| 34 | + |
| 35 | +static int gpio_raspberrypi_configure(const struct device *dev, |
| 36 | +gpio_pin_t pin, |
| 37 | +gpio_flags_t flags) |
| 38 | +{ |
| 39 | +if (flags & GPIO_SINGLE_ENDED) { |
| 40 | +return -ENOTSUP; |
| 41 | +} |
| 42 | + |
| 43 | +gpio_init(pin); |
| 44 | + |
| 45 | +if (flags & GPIO_OUTPUT) { |
| 46 | +gpio_set_dir(pin, GPIO_OUT); |
| 47 | + |
| 48 | +if (flags & GPIO_OUTPUT_INIT_HIGH) { |
| 49 | +gpio_put(pin, 1); |
| 50 | +} else if (flags & GPIO_OUTPUT_INIT_LOW) { |
| 51 | +gpio_put(pin, 0); |
| 52 | +} |
| 53 | + |
| 54 | +} else if (flags & GPIO_INPUT) { |
| 55 | +gpio_set_dir(pin, GPIO_IN); |
| 56 | +if (flags & GPIO_PULL_UP) { |
| 57 | +gpio_pull_up(pin); |
| 58 | +} else if (flags & GPIO_PULL_DOWN) { |
| 59 | +gpio_pull_down(pin); |
| 60 | +} |
| 61 | +} |
| 62 | + |
| 63 | +return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static int gpio_raspberrypi_port_get_raw(const struct device *dev, uint32_t *value) |
| 67 | +{ |
| 68 | +*value = gpio_get_all(); |
| 69 | +return 0; |
| 70 | +} |
| 71 | + |
| 72 | +static int gpio_raspberrypi_port_set_masked_raw(const struct device *port, |
| 73 | +uint32_t mask, uint32_t value) |
| 74 | +{ |
| 75 | +gpio_put_masked(mask, value); |
| 76 | +return 0; |
| 77 | +} |
| 78 | + |
| 79 | +static int gpio_raspberrypi_port_set_bits_raw(const struct device *port, |
| 80 | +uint32_t pins) |
| 81 | +{ |
| 82 | +gpio_set_mask(pins); |
| 83 | +return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static int gpio_raspberrypi_port_clear_bits_raw(const struct device *port, |
| 87 | +uint32_t pins) |
| 88 | +{ |
| 89 | +gpio_clr_mask(pins); |
| 90 | +return 0; |
| 91 | +} |
| 92 | + |
| 93 | +static int gpio_raspberrypi_port_toggle_bits(const struct device *port, |
| 94 | +uint32_t pins) |
| 95 | +{ |
| 96 | +gpio_xor_mask(pins); |
| 97 | +return 0; |
| 98 | +} |
| 99 | + |
| 100 | +static int gpio_raspberrypi_pin_interrupt_configure(const struct device *dev, |
| 101 | +gpio_pin_t pin, |
| 102 | +enum gpio_int_mode mode, |
| 103 | +enum gpio_int_trig trig) |
| 104 | +{ |
| 105 | +struct gpio_raspberrypi_data *data = dev->data; |
| 106 | +uint32_t events = 0; |
| 107 | + |
| 108 | +if (mode != GPIO_INT_DISABLE) { |
| 109 | +if (mode & GPIO_INT_EDGE) { |
| 110 | +if (trig & GPIO_INT_LOW_0) { |
| 111 | +events |= GPIO_IRQ_EDGE_FALL; |
| 112 | +} |
| 113 | +if (trig & GPIO_INT_HIGH_1) { |
| 114 | +events |= GPIO_IRQ_EDGE_RISE; |
| 115 | +} |
| 116 | +} else { |
| 117 | +if (trig & GPIO_INT_LOW_0) { |
| 118 | +events |= GPIO_IRQ_LEVEL_LOW; |
| 119 | +} |
| 120 | +if (trig & GPIO_INT_HIGH_1) { |
| 121 | +events |= GPIO_IRQ_LEVEL_HIGH; |
| 122 | +} |
| 123 | +} |
| 124 | +gpio_set_irq_enabled(pin, events, true); |
| 125 | +} |
| 126 | +WRITE_BIT(data->int_enabled_mask, pin, mode != GPIO_INT_DISABLE); |
| 127 | +return 0; |
| 128 | +} |
| 129 | + |
| 130 | +static int gpio_raspberrypi_manage_callback(const struct device *dev, |
| 131 | +struct gpio_callback *callback, bool set) |
| 132 | +{ |
| 133 | +struct gpio_raspberrypi_data *data = dev->data; |
| 134 | + |
| 135 | +return gpio_manage_callback(&data->callbacks, callback, set); |
| 136 | +} |
| 137 | + |
| 138 | +static const struct gpio_driver_api gpio_raspberrypi_driver_api = { |
| 139 | +.pin_configure = gpio_raspberrypi_configure, |
| 140 | +.port_get_raw = gpio_raspberrypi_port_get_raw, |
| 141 | +.port_set_masked_raw = gpio_raspberrypi_port_set_masked_raw, |
| 142 | +.port_set_bits_raw = gpio_raspberrypi_port_set_bits_raw, |
| 143 | +.port_clear_bits_raw = gpio_raspberrypi_port_clear_bits_raw, |
| 144 | +.port_toggle_bits = gpio_raspberrypi_port_toggle_bits, |
| 145 | +.pin_interrupt_configure = gpio_raspberrypi_pin_interrupt_configure, |
| 146 | +.manage_callback = gpio_raspberrypi_manage_callback, |
| 147 | +}; |
| 148 | + |
| 149 | +static void gpio_raspberrypi_isr(const struct device *dev) |
| 150 | +{ |
| 151 | +struct gpio_raspberrypi_data *data = dev->data; |
| 152 | +io_irq_ctrl_hw_t *irq_ctrl_base; |
| 153 | +const io_rw_32 *status_reg; |
| 154 | +uint32_t events; |
| 155 | +uint32_t pin; |
| 156 | + |
| 157 | +irq_ctrl_base = &iobank0_hw->proc0_irq_ctrl; |
| 158 | +for (pin = 0; pin < NUM_BANK0_GPIOS; pin++) { |
| 159 | +status_reg = &irq_ctrl_base->ints[pin / 8]; |
| 160 | +events = (*status_reg >> 4 * (pin % 8)) & ALL_EVENTS; |
| 161 | +if (events) { |
| 162 | +gpio_acknowledge_irq(pin, ALL_EVENTS); |
| 163 | +gpio_fire_callbacks(&data->callbacks, dev, BIT(pin)); |
| 164 | +} |
| 165 | +} |
| 166 | +} |
| 167 | + |
| 168 | +static int gpio_raspberrypi_bank_init(const struct device *dev) |
| 169 | +{ |
| 170 | +((struct gpio_raspberrypi_config *)(dev->config))->bank_config_func(); |
| 171 | +return 0; |
| 172 | +} |
| 173 | + |
| 174 | +#define GPIO_RASPBERRYPI_INIT(idx) \ |
| 175 | +static void bank_##idx##_config_func(void) \ |
| 176 | +{ \ |
| 177 | +IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), \ |
| 178 | + gpio_raspberrypi_isr, DEVICE_DT_INST_GET(idx), 0); \ |
| 179 | +irq_enable(DT_INST_IRQN(idx)); \ |
| 180 | +} \ |
| 181 | +static const struct gpio_raspberrypi_config gpio_raspberrypi_##idx##_config = { \ |
| 182 | +.bank_config_func = bank_##idx##_config_func, \ |
| 183 | +}; \ |
| 184 | +\ |
| 185 | +static struct gpio_raspberrypi_data gpio_raspberrypi_##idx##_data; \ |
| 186 | +\ |
| 187 | +DEVICE_DT_INST_DEFINE(idx, gpio_raspberrypi_bank_init, NULL, \ |
| 188 | +&gpio_raspberrypi_##idx##_data, \ |
| 189 | +&gpio_raspberrypi_##idx##_config, \ |
| 190 | +POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ |
| 191 | +&gpio_raspberrypi_driver_api); |
| 192 | + |
| 193 | +DT_INST_FOREACH_STATUS_OKAY(GPIO_RASPBERRYPI_INIT) |
0 commit comments