esp32: drivers: interrupt_controller: review Timer's interrupt usage

Review Timer interrupt allocation usage.

Signed-off-by: Glauber Maroto Ferreira <glauber.ferreira@espressif.com>
diff --git a/drivers/counter/Kconfig.esp32 b/drivers/counter/Kconfig.esp32
index 60f2de9..975f91b 100644
--- a/drivers/counter/Kconfig.esp32
+++ b/drivers/counter/Kconfig.esp32
@@ -3,45 +3,39 @@
 # Copyright (c) 2020 Espressif Systems (Shanghai) Co., Ltd.
 # SPDX-License-Identifier: Apache-2.0
 
-config COUNTER_ESP32
+menuconfig COUNTER_ESP32
 	bool "ESP32 Counter Driver"
 	depends on SOC_ESP32
-	default y
 	help
 	  Enable Counter driver for ESP32.
 
-config COUNTER_ESP32_IRQ_0
-	int "IRQ line for TG0_T0 interrupt"
-	depends on COUNTER_ESP32
-	default 13
-	help
-	  Set the IRQ line used by the TG0_T0 device.
+if COUNTER_ESP32
 
-config COUNTER_ESP32_IRQ_1
-	int "IRQ line for TG0_T1 interrupt"
-	depends on COUNTER_ESP32
-	default 17
+config COUNTER_ESP32_TG0_T0
+	bool "TG0_T0 enable/disable option"
 	help
-	  Set the IRQ line used by the TG0_T1 device.
+	  Enables Timer 0 peripheral from Timer Group 0.
 
-config COUNTER_ESP32_IRQ_2
-	int "IRQ line for TG1_T0 interrupt"
-	depends on COUNTER_ESP32
-	default 18
+config COUNTER_ESP32_TG0_T1
+	bool "TG0_T1 enable/disable option"
 	help
-	  Set the IRQ line used by the TG1_T0 device.
+	  Enables Timer 1 peripheral from Timer Group 0.
 
-config COUNTER_ESP32_IRQ_3
-	int "IRQ line for TG1_T1 interrupt"
-	depends on COUNTER_ESP32
-	default 20
+config COUNTER_ESP32_TG1_T0
+	bool "TG1_T0 enable/disable option"
 	help
-	  Set the IRQ line used by the TG1_T1 device.
+	  Enables Timer 0 peripheral from Timer Group 1.
+
+config COUNTER_ESP32_TG1_T1
+	bool "TG1_T1 enable/disable option"
+	help
+	  Enables Timer 1 peripheral from Timer Group 1.
 
 config COUNTER_ESP32_PRESCALER
 	int "Prescaling value for counter device"
-	depends on COUNTER_ESP32
 	range 2 65336
 	default 2
 	help
 	  Sets prescaler value for Timer clock.
+
+endif
diff --git a/drivers/counter/counter_esp32.c b/drivers/counter/counter_esp32.c
index 4258dc6..30a5e41 100644
--- a/drivers/counter/counter_esp32.c
+++ b/drivers/counter/counter_esp32.c
@@ -12,22 +12,16 @@
 #include <driver/periph_ctrl.h>
 #include <soc/periph_defs.h>
 #include <hal/timer_types.h>
-#include <driver/timer.h>
 #include <hal/timer_hal.h>
-#include <soc.h>
 #include <string.h>
 #include <drivers/counter.h>
+#include <drivers/interrupt_controller/intc_esp32.h>
 #include <device.h>
 #include <logging/log.h>
 LOG_MODULE_REGISTER(esp32_counter, CONFIG_COUNTER_LOG_LEVEL);
 
 #define INITIAL_COUNT (0x00000000ULL)
 
-#define INTR_SRC_0 ETS_TG0_T0_LEVEL_INTR_SOURCE
-#define INTR_SRC_1 ETS_TG0_T1_LEVEL_INTR_SOURCE
-#define INTR_SRC_2 ETS_TG1_T0_LEVEL_INTR_SOURCE
-#define INTR_SRC_3 ETS_TG1_T1_LEVEL_INTR_SOURCE
-
 #define INST_0_INDEX TIMER_0
 #define INST_1_INDEX TIMER_1
 #define INST_2_INDEX TIMER_0
@@ -44,12 +38,14 @@
 #define TIMG(dev) (DEV_CFG(dev)->group)
 #define TIDX(dev) (DEV_CFG(dev)->idx)
 
-typedef void (*counter_irq_config_func_t)(const struct device *dev);
+static void counter_esp32_isr(void *arg);
+
+typedef bool (*timer_isr_t)(void *);
 
 struct timer_isr_func_t {
 	timer_isr_t fn;
 	void *args;
-	timer_isr_handle_t timer_isr_handle;
+	struct intr_handle_data_t *timer_isr_handle;
 	timer_group_t isr_timer_group;
 };
 
@@ -63,13 +59,7 @@
 	timer_config_t config;
 	timer_group_t group;
 	timer_idx_t idx;
-
-	const struct {
-		int source;
-		int line;
-	} irq;
-
-	counter_irq_config_func_t irq_config_fn;
+	int irq_source;
 };
 
 struct counter_esp32_data {
@@ -115,7 +105,7 @@
 	}
 	timer_hal_set_counter_value(&TIMX->hal, INITIAL_COUNT);
 	timer_hal_set_counter_enable(&TIMX->hal, cfg->config.counter_en);
-	DEV_CFG(dev)->irq_config_fn(dev);
+	esp_intr_alloc(DEV_CFG(dev)->irq_source, 0, counter_esp32_isr, (void *)dev, NULL);
 	k_spin_unlock(&lock, key);
 
 	return 0;
@@ -216,8 +206,9 @@
 	.get_top_value = counter_esp32_get_top_value,
 };
 
-static void counter_esp32_isr(struct device *dev)
+static void counter_esp32_isr(void *arg)
 {
+	struct device *dev = (struct device *)arg;
 	counter_esp32_cancel_alarm(dev, 0);
 	uint32_t now;
 
@@ -235,7 +226,6 @@
 #define ESP32_COUNTER_INIT(n)							 \
 										 \
 	static struct counter_esp32_data counter_data_##n;			 \
-	static void counter_esp32_irq_config_##n(const struct device *dev);	 \
 										 \
 	static const struct counter_esp32_config counter_config_##n = {		 \
 		.counter_info = {						 \
@@ -254,22 +244,9 @@
 		},								 \
 		.group = INST_##n##_GROUP,					 \
 		.idx = INST_##n##_INDEX,					 \
-		.irq = {							 \
-			.source =  INTR_SRC_##n,				 \
-			.line =  CONFIG_COUNTER_ESP32_IRQ_##n,			 \
-		},								 \
-		.irq_config_fn = counter_esp32_irq_config_##n			 \
+		.irq_source = DT_IRQN(DT_NODELABEL(timer##n))                    \
 	};									 \
 										 \
-	static void counter_esp32_irq_config_##n(const struct device *dev)	 \
-	{									 \
-		intr_matrix_set(0, INTR_SRC_##n,				 \
-				CONFIG_COUNTER_ESP32_IRQ_##n);			 \
-		IRQ_CONNECT(CONFIG_COUNTER_ESP32_IRQ_##n,			 \
-			    DT_INST_IRQ(n, priority), counter_esp32_isr,	 \
-			    DEVICE_DT_INST_GET(n), 0);				 \
-		irq_enable(CONFIG_COUNTER_ESP32_IRQ_##n);			 \
-	}									 \
 										 \
 	DEVICE_DT_INST_DEFINE(n,						 \
 			      counter_esp32_init,				 \
@@ -277,4 +254,18 @@
 			      &counter_config_##n, PRE_KERNEL_1,		 \
 			      CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &counter_api);
 
-DT_INST_FOREACH_STATUS_OKAY(ESP32_COUNTER_INIT)
+#ifdef CONFIG_COUNTER_ESP32_TG0_T0
+ESP32_COUNTER_INIT(0);
+#endif
+
+#ifdef CONFIG_COUNTER_ESP32_TG0_T1
+ESP32_COUNTER_INIT(1);
+#endif
+
+#ifdef CONFIG_COUNTER_ESP32_TG1_T0
+ESP32_COUNTER_INIT(2);
+#endif
+
+#ifdef CONFIG_COUNTER_ESP32_TG1_T1
+ESP32_COUNTER_INIT(3);
+#endif
diff --git a/dts/xtensa/espressif/esp32.dtsi b/dts/xtensa/espressif/esp32.dtsi
index ad93ada..7c6db6e 100644
--- a/dts/xtensa/espressif/esp32.dtsi
+++ b/dts/xtensa/espressif/esp32.dtsi
@@ -199,7 +199,8 @@
 		timer0: counter@3ff5f000 {
 			compatible = "espressif,esp32-timer";
 			reg = <0x3ff5f000 DT_SIZE_K(4)>;
-			/* interrupts = <13>; - FIXME: Enable when irq controller is supported */
+			interrupts = <TG0_T0_LEVEL_INTR_SOURCE>;
+			interrupt-parent = <&intc>;
 			label = "TIMG0_T0";
 			status = "disabled";
 		};
@@ -207,7 +208,8 @@
 		timer1: counter@3ff5f024 {
 			compatible = "espressif,esp32-timer";
 			reg = <0x3ff5f024 DT_SIZE_K(4)>;
-			/* interrupts = <17>; - FIXME: Enable when irq controller is supported */
+			interrupts = <TG0_T1_LEVEL_INTR_SOURCE>;
+			interrupt-parent = <&intc>;
 			label = "TIMG0_T1";
 			status = "disabled";
 		};
@@ -215,7 +217,8 @@
 		timer2: counter@3ff60000 {
 			compatible = "espressif,esp32-timer";
 			reg = <0x3ff60000 DT_SIZE_K(4)>;
-			/* interrupts = <18>; - FIXME: Enable when irq controller is supported */
+			interrupts = <TG1_T0_LEVEL_INTR_SOURCE>;
+			interrupt-parent = <&intc>;
 			label = "TIMG1_T0";
 			status = "disabled";
 		};
@@ -223,7 +226,8 @@
 		timer3: counter@3ff60024 {
 			compatible = "espressif,esp32-timer";
 			reg = <0x3ff60024 DT_SIZE_K(4)>;
-			/* interrupts = <20>; - FIXME: Enable when irq controller is supported */
+			interrupts = <TG1_T1_LEVEL_INTR_SOURCE>;
+			interrupt-parent = <&intc>;
 			label = "TIMG1_T1";
 			status = "disabled";
 		};
diff --git a/samples/drivers/counter/alarm/boards/esp32.conf b/samples/drivers/counter/alarm/boards/esp32.conf
index 26d5270..787c496 100644
--- a/samples/drivers/counter/alarm/boards/esp32.conf
+++ b/samples/drivers/counter/alarm/boards/esp32.conf
@@ -1 +1,2 @@
 CONFIG_HEAP_MEM_POOL_SIZE=256
+CONFIG_COUNTER_ESP32_TG0_T0=y