#define DT_DRV_COMPAT cd74hc4067

#include <errno.h>
#include <zephyr/logging/log.h>

#include "cd74hc4067.h"

LOG_MODULE_REGISTER(cd74hc4067);

//-------------------------------------------------------------------------
// Declarations

static int cd74hc4067_init(const struct device *dev);
static int cd74hc4067_enable_pin(const struct device *dev, bool state);

//-------------------------------------------------------------------------
// Private


static int cd74hc4067_init(const struct device *dev) {
	int ret;

	const struct cd74hc4067_cfg *cfg = dev->config;
	struct cd74hc4067_data *data = dev->data;

	k_mutex_init(&data->lock);
	data->current_channel = 0U;
	data->enabled = !cfg->has_enable;

	// Init the select pins
	for (int i = 0; i < CD74HC4067_SELECT_PIN_COUNT; i++) {
		const struct gpio_dt_spec *pin = &(cfg->select[i]);
		if (!gpio_is_ready_dt(pin)) {
			LOG_ERR("Select pin %d not ready.\r\n", i);
			return -ENODEV;
		}

		ret = gpio_pin_configure_dt(pin, GPIO_OUTPUT_INACTIVE);
		if (ret < 0) {
			LOG_ERR("Could not configure select pin %d as output\r\n", i);
			return -ENODEV;
		}
	}

	if (cfg->has_enable) {
		// Init the enable pin
		const struct gpio_dt_spec *enable = &cfg->enable;
		LOG_DBG("Initializing enable pin.\r\n");

		if (!gpio_is_ready_dt(enable)) {
			LOG_ERR("Enable pin not ready.\r\n");
			return -ENODEV;
		}

		ret = gpio_pin_configure_dt(enable, GPIO_OUTPUT_INACTIVE);
		if (ret < 0) {
			LOG_ERR("Could not configure ENABLE pin as output\r\n");
			return -ENODEV;
		}
	}

	return 0;
}

static int cd74hc4067_enable_pin(const struct device *dev, bool state) {
	const struct cd74hc4067_cfg *cfg = dev->config;
	struct cd74hc4067_data *data = dev->data;
	int ret = 0;

	if (!cfg->has_enable) {
		if (!state) {
			return -ENOTSUP;
		}

		data->enabled = true;
		return 0;
	}

	if (!device_is_ready(cfg->enable.port)) {
		LOG_ERR("%s enable GPIO not ready", dev->name);
		return -ENODEV;
	}

	ret = gpio_pin_set_dt(&cfg->enable, state ? 0 : 1);
	if (ret == 0) {
		data->enabled = state;
	}

	return ret;
}

//-------------------------------------------------------------------------
// Public

int cd74hc4067_enable(const struct device *dev) {
	struct cd74hc4067_data *data = dev->data;
	int ret;

	ret = k_mutex_lock(&data->lock, K_FOREVER);
	if (ret != 0) {
		return ret;
	}

	ret = cd74hc4067_enable_pin(dev, true);

	k_mutex_unlock(&data->lock);
	return ret;
}

int cd74hc4067_disable(const struct device *dev) {
	struct cd74hc4067_data *data = dev->data;
	int ret;

	ret = k_mutex_lock(&data->lock, K_FOREVER);
	if (ret != 0) {
		return ret;
	}

	ret = cd74hc4067_enable_pin(dev, false);

	k_mutex_unlock(&data->lock);
	return ret;
}

int cd74hc4067_select_channel(const struct device *dev, uint8_t channel) {
	if (channel >= CD74HC4067_SELECT_CHANNEL_COUNT) {
		LOG_ERR("Channel not found.\r\n");
		return -ENODEV;
	}
	
	int ret;
	const struct cd74hc4067_cfg *cfg = dev->config;
	struct cd74hc4067_data *data = dev->data;

	ret = k_mutex_lock(&data->lock, K_FOREVER);
	if (ret < 0) {
		LOG_ERR("Mutex error");
		return ret;
	}

	for (int i = 0; i < CD74HC4067_SELECT_PIN_COUNT; i++) {
		bool state = (channel >> i) & 0x1;
		ret = gpio_pin_set_dt(&cfg->select[i], state);
		if (ret < 0) {
			LOG_ERR("Unable to set channel pin: %d", ret);
			goto out;
		}
	}

	data->current_channel = channel;

out:
	k_mutex_unlock(&data->lock);
	return ret;
}

//-------------------------------------------------------------------------
// Devicetree

// static const struct cd74hc4067_api cd74hc4067_api_funcs = {
// 	.enable = cd74hc4067_enable,
// 	.disable = cd74hc4067_disable,
// 	.select_channel = cd74hc4067_select_channel,
// };


#define CD74HC4067_DEFINE(inst)												\
																			\
	static const struct cd74hc4067_cfg cd74hc4067_cfg_##inst = {			\
		.select = {															\
			GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 0),	\
			GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 1),	\
			GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 2),	\
			GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), select_gpios, 3),	\
		},																	\
		.enable = COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, enable_gpio),		\
				      (GPIO_DT_SPEC_GET(DT_DRV_INST(inst), enable_gpio)),	\
				      ((struct gpio_dt_spec){0})),							\
		.has_enable = DT_INST_NODE_HAS_PROP(inst, enable_gpio),				\
	};																		\
	static struct cd74hc4067_data cd74hc4067_data_##inst;					\
	DEVICE_DT_INST_DEFINE(	inst, 											\
							cd74hc4067_init, 								\
							NULL, 											\
							&cd74hc4067_data_##inst,						\
			      			&cd74hc4067_cfg_##inst,							\
							POST_KERNEL,									\
			      			CONFIG_CD74HC4067_INIT_PRIORITY, 				\
							NULL);

DT_INST_FOREACH_STATUS_OKAY(CD74HC4067_DEFINE)