/*
 * Copyright (c) 2023 SILA Embedded Solutions GmbH
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @file
 * @brief GPIO driver for the ADS114S0x AFE.
 */

#define DT_DRV_COMPAT ti_ads114s0x_gpio

#include <zephyr/drivers/gpio.h>
#include <zephyr/kernel.h>

#define LOG_LEVEL CONFIG_GPIO_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(gpio_ads114s0x);

#include <zephyr/drivers/adc/ads114s0x.h>

#include <zephyr/drivers/gpio/gpio_utils.h>

struct gpio_ads114s0x_config {
	/* gpio_driver_config needs to be first */
	struct gpio_driver_config common;
	const struct device *parent;
};

struct gpio_ads114s0x_data {
	/* gpio_driver_data needs to be first */
	struct gpio_driver_data common;
};

static int gpio_ads114s0x_config(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags)
{
	const struct gpio_ads114s0x_config *config = dev->config;
	int err = 0;

	if ((flags & (GPIO_INPUT | GPIO_OUTPUT)) == GPIO_DISCONNECTED) {
		return ads114s0x_gpio_deconfigure(config->parent, pin);
	}

	if ((flags & GPIO_SINGLE_ENDED) != 0) {
		return -ENOTSUP;
	}

	if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0) {
		return -ENOTSUP;
	}

	if (flags & GPIO_INT_ENABLE) {
		/* ads114s0x GPIOs do not support interrupts */
		return -ENOTSUP;
	}

	switch (flags & GPIO_DIR_MASK) {
	case GPIO_INPUT:
		err = ads114s0x_gpio_set_input(config->parent, pin);
		break;
	case GPIO_OUTPUT:
		err = ads114s0x_gpio_set_output(config->parent, pin,
						(flags & GPIO_OUTPUT_INIT_HIGH) != 0);
		break;
	default:
		return -ENOTSUP;
	}

	return err;
}

static int gpio_ads114s0x_port_get_raw(const struct device *dev, gpio_port_value_t *value)
{
	const struct gpio_ads114s0x_config *config = dev->config;

	return ads114s0x_gpio_port_get_raw(config->parent, value);
}

static int gpio_ads114s0x_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask,
					      gpio_port_value_t value)
{
	const struct gpio_ads114s0x_config *config = dev->config;

	return ads114s0x_gpio_port_set_masked_raw(config->parent, mask, value);
}

static int gpio_ads114s0x_port_set_bits_raw(const struct device *dev, gpio_port_pins_t pins)
{
	const struct gpio_ads114s0x_config *config = dev->config;

	return ads114s0x_gpio_port_set_masked_raw(config->parent, pins, pins);
}

static int gpio_ads114s0x_port_clear_bits_raw(const struct device *dev, gpio_port_pins_t pins)
{
	const struct gpio_ads114s0x_config *config = dev->config;

	return ads114s0x_gpio_port_set_masked_raw(config->parent, pins, 0);
}

static int gpio_ads114s0x_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins)
{
	const struct gpio_ads114s0x_config *config = dev->config;

	return ads114s0x_gpio_port_toggle_bits(config->parent, pins);
}

static int gpio_ads114s0x_init(const struct device *dev)
{
	const struct gpio_ads114s0x_config *config = dev->config;

	if (!device_is_ready(config->parent)) {
		LOG_ERR("parent ads114s0x device '%s' not ready", config->parent->name);
		return -EINVAL;
	}

	return 0;
}

static const struct gpio_driver_api gpio_ads114s0x_api = {
	.pin_configure = gpio_ads114s0x_config,
	.port_set_masked_raw = gpio_ads114s0x_port_set_masked_raw,
	.port_set_bits_raw = gpio_ads114s0x_port_set_bits_raw,
	.port_clear_bits_raw = gpio_ads114s0x_port_clear_bits_raw,
	.port_toggle_bits = gpio_ads114s0x_port_toggle_bits,
	.port_get_raw = gpio_ads114s0x_port_get_raw,
};

BUILD_ASSERT(CONFIG_GPIO_ADS114S0X_INIT_PRIORITY > CONFIG_ADC_INIT_PRIORITY,
	     "ADS114S0X GPIO driver must be initialized after ADS114S0X ADC driver");

#define GPIO_ADS114S0X_DEVICE(id)                                                                  \
	static const struct gpio_ads114s0x_config gpio_ads114s0x_##id##_cfg = {                    \
		.common = {.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(id)},                  \
		.parent = DEVICE_DT_GET(DT_INST_BUS(id)),                                          \
	};                                                                                         \
                                                                                                   \
	static struct gpio_ads114s0x_data gpio_ads114s0x_##id##_data;                              \
                                                                                                   \
	DEVICE_DT_INST_DEFINE(id, &gpio_ads114s0x_init, NULL, &gpio_ads114s0x_##id##_data,         \
			      &gpio_ads114s0x_##id##_cfg, POST_KERNEL,                             \
			      CONFIG_GPIO_ADS114S0X_INIT_PRIORITY, &gpio_ads114s0x_api);

DT_INST_FOREACH_STATUS_OKAY(GPIO_ADS114S0X_DEVICE)
