|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Titouan Christophe |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT adafruit_seesaw_gpio |
| 8 | + |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/drivers/mfd/mfd_seesaw.h> |
| 11 | + |
| 12 | +enum seesaw_mod_gpio { |
| 13 | + GPIO_DIRSET = 0x02, |
| 14 | + GPIO_DIRCLR = 0x03, |
| 15 | + GPIO_GPIO = 0x04, |
| 16 | + GPIO_SET = 0x05, |
| 17 | + GPIO_CLR = 0x06, |
| 18 | + GPIO_TOGGLE = 0x07, |
| 19 | + GPIO_INTENSET = 0x08, |
| 20 | + GPIO_INTENCLR = 0x09, |
| 21 | + GPIO_INTFLAG = 0x0a, |
| 22 | + GPIO_PULLENSET = 0x0b, |
| 23 | + GPIO_PULLENCLR = 0x0c, |
| 24 | +}; |
| 25 | + |
| 26 | +struct seesaw_gpio_config { |
| 27 | + const struct device *seesaw; |
| 28 | +}; |
| 29 | + |
| 30 | +static int seesaw_gpio_configure(const struct device *dev, |
| 31 | + gpio_pin_t pin, gpio_flags_t flags) |
| 32 | +{ |
| 33 | + const struct seesaw_gpio_config *const config = dev->config; |
| 34 | + |
| 35 | + if (flags & GPIO_OUTPUT) { |
| 36 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_DIRSET, BIT(pin)); |
| 37 | + } else if (flags & GPIO_INPUT) { |
| 38 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_DIRCLR, BIT(pin)); |
| 39 | + } else { |
| 40 | + return -ENOTSUP; |
| 41 | + } |
| 42 | + |
| 43 | + if (flags & GPIO_PULL_UP) { |
| 44 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_SET, BIT(pin)); |
| 45 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_PULLENSET, BIT(pin)); |
| 46 | + } else if (flags & GPIO_PULL_DOWN) { |
| 47 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_CLR, BIT(pin)); |
| 48 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_PULLENSET, BIT(pin)); |
| 49 | + } |
| 50 | + |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +static int seesaw_gpio_port_get_raw(const struct device *dev, uint32_t *value) |
| 55 | +{ |
| 56 | + uint32_t tmp; |
| 57 | + const struct seesaw_gpio_config *const config = dev->config; |
| 58 | + int ret = seesaw_read(config->seesaw, SEESAW_MOD_GPIO, GPIO_GPIO, (uint8_t *) &tmp, 4); |
| 59 | + |
| 60 | + if (ret == 0) { |
| 61 | + *value = sys_be32_to_cpu(ret); |
| 62 | + } |
| 63 | + |
| 64 | + return ret; |
| 65 | +} |
| 66 | + |
| 67 | +static int seesaw_gpio_port_set_masked_raw(const struct device *dev, uint32_t mask, uint32_t value) |
| 68 | +{ |
| 69 | + const struct seesaw_gpio_config *const config = dev->config; |
| 70 | + |
| 71 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_SET, value & mask); |
| 72 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_CLR, ~value & mask); |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static int seesaw_gpio_port_set_bits_raw(const struct device *dev, uint32_t mask) |
| 78 | +{ |
| 79 | + const struct seesaw_gpio_config *const config = dev->config; |
| 80 | + |
| 81 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_SET, mask); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static int seesaw_gpio_port_clear_bits_raw(const struct device *dev, uint32_t mask) |
| 87 | +{ |
| 88 | + const struct seesaw_gpio_config *const config = dev->config; |
| 89 | + |
| 90 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_CLR, mask); |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +static int seesaw_gpio_port_toggle_bits(const struct device *dev, uint32_t mask) |
| 96 | +{ |
| 97 | + const struct seesaw_gpio_config *const config = dev->config; |
| 98 | + |
| 99 | + seesaw_write_uint32(config->seesaw, SEESAW_MOD_GPIO, GPIO_TOGGLE, mask); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +static DEVICE_API(gpio, seesaw_gpio_api) = { |
| 106 | + .pin_configure = seesaw_gpio_configure, |
| 107 | + .port_get_raw = seesaw_gpio_port_get_raw, |
| 108 | + .port_set_masked_raw = seesaw_gpio_port_set_masked_raw, |
| 109 | + .port_set_bits_raw = seesaw_gpio_port_set_bits_raw, |
| 110 | + .port_clear_bits_raw = seesaw_gpio_port_clear_bits_raw, |
| 111 | + .port_toggle_bits = seesaw_gpio_port_toggle_bits, |
| 112 | +}; |
| 113 | + |
| 114 | +static int seesaw_gpio_init(const struct device *dev) |
| 115 | +{ |
| 116 | + const struct seesaw_gpio_config *config = dev->config; |
| 117 | + |
| 118 | + return seesaw_claim_module(config->seesaw, SEESAW_MOD_GPIO, dev); |
| 119 | +} |
| 120 | + |
| 121 | +#define SEESAW_GPIO_INIT(inst) \ |
| 122 | + static const struct seesaw_gpio_config config_##inst = { \ |
| 123 | + .seesaw = DEVICE_DT_GET(DT_INST_PARENT(inst)), \ |
| 124 | + }; \ |
| 125 | + DEVICE_DT_INST_DEFINE(inst, seesaw_gpio_init, NULL, NULL, &config_##inst, POST_KERNEL, \ |
| 126 | + CONFIG_MFD_INIT_PRIORITY, &seesaw_gpio_api); |
| 127 | + |
| 128 | +DT_INST_FOREACH_STATUS_OKAY(SEESAW_GPIO_INIT) |
0 commit comments