drivers/sensor: lis2dw12: Move trigger pulse Kconfig property into DT

Move lis2dw12 trigger pulse configurations from Kconfigs to Device Tree.
Moreover the dts properties have been renamed as 'tap', which sounds a
better name to immediately catch the feature behind it. Since tap
threshold cannot be zero, this value (which is the default in dts
binding) is used to enable/disable the device feature per each axis.
The event can be generated on INT1 only.

Signed-off-by: Armando Visconti <armando.visconti@st.com>
diff --git a/drivers/sensor/lis2dw12/Kconfig b/drivers/sensor/lis2dw12/Kconfig
index 58d6330..0fa3f40 100644
--- a/drivers/sensor/lis2dw12/Kconfig
+++ b/drivers/sensor/lis2dw12/Kconfig
@@ -52,93 +52,10 @@
 	help
 	  Stack size of thread used by the driver to handle interrupts.
 
-menuconfig LIS2DW12_PULSE
-	bool "Pulse detection"
+config LIS2DW12_TAP
+	bool "Tap and Tap-Tap detection"
 	help
-	  Enable pulse (single/double tap) detection
-
-if LIS2DW12_PULSE
-
-choice
-	prompt "Pulse interrupt source"
-	default LIS2DW12_ONLY_SINGLE
-
-config LIS2DW12_ONLY_SINGLE
-	bool "single"
-
-config LIS2DW12_SINGLE_DOUBLE
-	bool "single/double"
-
-endchoice
-
-config LIS2DW12_PULSE_THSX
-	hex "Pulse X-axis threshold"
-	range 0 0x1F
-	default 0x0E
-	help
-	  Threshold to start the pulse-event detection procedure on the X-axis.
-	  Threshold values for each axis are unsigned 5-bit corresponding
-	  to an 2g acceleration full-scale range.
-
-config LIS2DW12_PULSE_THSY
-	hex "Pulse Y-axis threshold"
-	range 0 0x1F
-	default 0x0E
-	help
-	  Threshold to start the pulse-event detection procedure on the Y-axis.
-	  Threshold values for each axis are unsigned 5-bit corresponding
-	  to an 2g acceleration full-scale range.
-
-config LIS2DW12_PULSE_THSZ
-	hex "Pulse Z-axis threshold"
-	range 0 0x1F
-	default 0x0E
-	help
-	  Threshold to start the pulse-event detection procedure on the Z-axis.
-	  Threshold values for each axis are unsigned 5-bit corresponding
-	  to an 2g acceleration full-scale range.
-
-config LIS2DW12_PULSE_X
-	bool "Enable X axis for pulse"
-	default y
-
-config LIS2DW12_PULSE_Y
-	bool "Enable Y axis for pulse"
-	default y
-
-config LIS2DW12_PULSE_Z
-	bool "Enable Z axis for pulse"
-	default y
-
-config LIS2DW12_PULSE_SHOCK
-	hex "Shock value"
-	range 0 0x03
-	default 0x00
-	help
-	  Maximum duration of over-threshold event: this register represents
-	  the maximum time of an over-threshold signal detection to be
-	  recognized as a tap event. Where 0 equals 4*1/ODR and 1LSB = 8*1/ODR.
-
-config LIS2DW12_PULSE_LTNCY
-	hex "Latency value"
-	range 0 0x0F
-	default 0x05
-	help
-	  When double-tap recognition is enabled, this register expresses
-	  the maximum time between two successive detected taps to
-	  determine a double-tap event. Where 0 equals 16*1/ODR and
-	  1LSB = 32*1/ODR.
-
-config LIS2DW12_PULSE_QUIET
-	hex "Quiet value"
-	range 0 0x03
-	default 0x00
-	help
-	  Expected quiet time after a tap detection: this register represents
-	  the time after the first detected tap in which there must not be
-	  any overthreshold event. Where 0 equals 2*1/ODR and 1LSB = 4*1/ODR.
-
-endif # LIS2DW12_PULSE
+	  Enable tap (single/double) detection
 
 endif # LIS2DW12_TRIGGER
 
diff --git a/drivers/sensor/lis2dw12/lis2dw12.c b/drivers/sensor/lis2dw12/lis2dw12.c
index 2de4183..7ec16cc 100644
--- a/drivers/sensor/lis2dw12/lis2dw12.c
+++ b/drivers/sensor/lis2dw12/lis2dw12.c
@@ -311,64 +311,6 @@
 		LOG_ERR("Failed to initialize interrupts");
 		return -EIO;
 	}
-
-#ifdef CONFIG_LIS2DW12_PULSE
-	if (lis2dw12_tap_mode_set(lis2dw12->ctx, cfg->pulse_trigger) < 0) {
-		LOG_ERR("Failed to select pulse trigger mode");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_threshold_x_set(lis2dw12->ctx,
-					 cfg->pulse_ths[0]) < 0) {
-		LOG_ERR("Failed to set tap X axis threshold");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_threshold_y_set(lis2dw12->ctx,
-					 cfg->pulse_ths[1]) < 0) {
-		LOG_ERR("Failed to set tap Y axis threshold");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_threshold_z_set(lis2dw12->ctx,
-					 cfg->pulse_ths[2]) < 0) {
-		LOG_ERR("Failed to set tap Z axis threshold");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_detection_on_x_set(lis2dw12->ctx,
-					    CONFIG_LIS2DW12_PULSE_X) < 0) {
-		LOG_ERR("Failed to set tap detection on X axis");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_detection_on_y_set(lis2dw12->ctx,
-					    CONFIG_LIS2DW12_PULSE_Y) < 0) {
-		LOG_ERR("Failed to set tap detection on Y axis");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_detection_on_z_set(lis2dw12->ctx,
-					    CONFIG_LIS2DW12_PULSE_Z) < 0) {
-		LOG_ERR("Failed to set tap detection on Z axis");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_shock_set(lis2dw12->ctx, cfg->pulse_shock) < 0) {
-		LOG_ERR("Failed to set tap shock duration");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_dur_set(lis2dw12->ctx, cfg->pulse_ltncy) < 0) {
-		LOG_ERR("Failed to set tap latency");
-		return -EIO;
-	}
-
-	if (lis2dw12_tap_quiet_set(lis2dw12->ctx, cfg->pulse_quiet) < 0) {
-		LOG_ERR("Failed to set tap quiet time");
-		return -EIO;
-	}
-#endif /* CONFIG_LIS2DW12_PULSE */
 #endif /* CONFIG_LIS2DW12_TRIGGER */
 
 	return 0;
@@ -381,19 +323,13 @@
 	.gpio_int = GPIO_DT_SPEC_INST_GET_OR(0, irq_gpios, {0}),
 	.int_pin = DT_INST_PROP(0, int_pin),
 
-#ifdef CONFIG_LIS2DW12_PULSE
-#if defined(CONFIG_LIS2DW12_ONLY_SINGLE)
-	.pulse_trigger = LIS2DW12_ONLY_SINGLE,
-#elif defined(CONFIG_LIS2DW12_SINGLE_DOUBLE)
-	.pulse_trigger = LIS2DW12_BOTH_SINGLE_DOUBLE,
-#endif
-	.pulse_ths[0] = CONFIG_LIS2DW12_PULSE_THSX,
-	.pulse_ths[1] = CONFIG_LIS2DW12_PULSE_THSY,
-	.pulse_ths[2] = CONFIG_LIS2DW12_PULSE_THSZ,
-	.pulse_shock = CONFIG_LIS2DW12_PULSE_SHOCK,
-	.pulse_ltncy = CONFIG_LIS2DW12_PULSE_LTNCY,
-	.pulse_quiet = CONFIG_LIS2DW12_PULSE_QUIET,
-#endif /* CONFIG_LIS2DW12_PULSE */
+#ifdef CONFIG_LIS2DW12_TAP
+	.tap_mode = DT_INST_PROP(0, tap_mode),
+	.tap_threshold = DT_INST_PROP(0, tap_threshold),
+	.tap_shock = DT_INST_PROP(0, tap_shock),
+	.tap_latency = DT_INST_PROP(0, tap_latency),
+	.tap_quiet = DT_INST_PROP(0, tap_quiet),
+#endif /* CONFIG_LIS2DW12_TAP */
 #endif /* CONFIG_LIS2DW12_TRIGGER */
 };
 
diff --git a/drivers/sensor/lis2dw12/lis2dw12.h b/drivers/sensor/lis2dw12/lis2dw12.h
index 8bfaa0f..b90ad49 100644
--- a/drivers/sensor/lis2dw12/lis2dw12.h
+++ b/drivers/sensor/lis2dw12/lis2dw12.h
@@ -85,13 +85,13 @@
 #ifdef CONFIG_LIS2DW12_TRIGGER
 	struct gpio_dt_spec gpio_int;
 	uint8_t int_pin;
-#ifdef CONFIG_LIS2DW12_PULSE
-	uint8_t pulse_trigger;
-	uint8_t pulse_ths[3];
-	uint8_t pulse_shock;
-	uint8_t pulse_ltncy;
-	uint8_t pulse_quiet;
-#endif /* CONFIG_LIS2DW12_PULSE */
+#ifdef CONFIG_LIS2DW12_TAP
+	uint8_t tap_mode;
+	uint8_t tap_threshold[3];
+	uint8_t tap_shock;
+	uint8_t tap_latency;
+	uint8_t tap_quiet;
+#endif /* CONFIG_LIS2DW12_TAP */
 #endif /* CONFIG_LIS2DW12_TRIGGER */
 };
 
@@ -112,10 +112,10 @@
 
 	struct gpio_callback gpio_cb;
 	sensor_trigger_handler_t drdy_handler;
-#ifdef CONFIG_LIS2DW12_PULSE
+#ifdef CONFIG_LIS2DW12_TAP
 	sensor_trigger_handler_t tap_handler;
 	sensor_trigger_handler_t double_tap_handler;
-#endif /* CONFIG_LIS2DW12_PULSE */
+#endif /* CONFIG_LIS2DW12_TAP */
 #if defined(CONFIG_LIS2DW12_TRIGGER_OWN_THREAD)
 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LIS2DW12_THREAD_STACK_SIZE);
 	struct k_thread thread;
diff --git a/drivers/sensor/lis2dw12/lis2dw12_trigger.c b/drivers/sensor/lis2dw12/lis2dw12_trigger.c
index e4a2137..8c7e2ca 100644
--- a/drivers/sensor/lis2dw12/lis2dw12_trigger.c
+++ b/drivers/sensor/lis2dw12/lis2dw12_trigger.c
@@ -38,14 +38,14 @@
 		case SENSOR_TRIG_DATA_READY:
 			int_route.ctrl4_int1_pad_ctrl.int1_drdy = enable;
 			break;
-#ifdef CONFIG_LIS2DW12_PULSE
+#ifdef CONFIG_LIS2DW12_TAP
 		case SENSOR_TRIG_TAP:
 			int_route.ctrl4_int1_pad_ctrl.int1_single_tap = enable;
 			break;
 		case SENSOR_TRIG_DOUBLE_TAP:
 			int_route.ctrl4_int1_pad_ctrl.int1_tap = enable;
 			break;
-#endif /* CONFIG_LIS2DW12_PULSE */
+#endif /* CONFIG_LIS2DW12_TAP */
 		default:
 			LOG_ERR("Unsupported trigger interrupt route");
 			return -ENOTSUP;
@@ -98,7 +98,7 @@
 		}
 		return lis2dw12_enable_int(dev, SENSOR_TRIG_DATA_READY, state);
 		break;
-#ifdef CONFIG_LIS2DW12_PULSE
+#ifdef CONFIG_LIS2DW12_TAP
 	case SENSOR_TRIG_TAP:
 		lis2dw12->tap_handler = handler;
 		return lis2dw12_enable_int(dev, SENSOR_TRIG_TAP, state);
@@ -107,7 +107,7 @@
 		lis2dw12->double_tap_handler = handler;
 		return lis2dw12_enable_int(dev, SENSOR_TRIG_DOUBLE_TAP, state);
 		break;
-#endif /* CONFIG_LIS2DW12_PULSE */
+#endif /* CONFIG_LIS2DW12_TAP */
 	default:
 		LOG_ERR("Unsupported sensor trigger");
 		return -ENOTSUP;
@@ -130,7 +130,7 @@
 	return 0;
 }
 
-#ifdef CONFIG_LIS2DW12_PULSE
+#ifdef CONFIG_LIS2DW12_TAP
 static int lis2dw12_handle_single_tap_int(const struct device *dev)
 {
 	struct lis2dw12_data *data = dev->data;
@@ -164,7 +164,7 @@
 
 	return 0;
 }
-#endif /* CONFIG_LIS2DW12_PULSE */
+#endif /* CONFIG_LIS2DW12_TAP */
 
 /**
  * lis2dw12_handle_interrupt - handle the drdy event
@@ -181,14 +181,14 @@
 	if (sources.status_dup.drdy) {
 		lis2dw12_handle_drdy_int(dev);
 	}
-#ifdef CONFIG_LIS2DW12_PULSE
+#ifdef CONFIG_LIS2DW12_TAP
 	if (sources.status_dup.single_tap) {
 		lis2dw12_handle_single_tap_int(dev);
 	}
 	if (sources.status_dup.double_tap) {
 		lis2dw12_handle_double_tap_int(dev);
 	}
-#endif /* CONFIG_LIS2DW12_PULSE */
+#endif /* CONFIG_LIS2DW12_TAP */
 
 	gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
 					GPIO_INT_EDGE_TO_ACTIVE);
@@ -234,6 +234,82 @@
 }
 #endif /* CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD */
 
+#ifdef CONFIG_LIS2DW12_TAP
+static int lis2dw12_tap_init(const struct device *dev)
+{
+	const struct lis2dw12_device_config *cfg = dev->config;
+	struct lis2dw12_data *lis2dw12 = dev->data;
+
+	LOG_DBG("TAP: tap mode is %d", cfg->tap_mode);
+	if (lis2dw12_tap_mode_set(lis2dw12->ctx, cfg->tap_mode) < 0) {
+		LOG_ERR("Failed to select tap trigger mode");
+		return -EIO;
+	}
+
+	LOG_DBG("TAP: ths_x is %02x", cfg->tap_threshold[0]);
+	if (lis2dw12_tap_threshold_x_set(lis2dw12->ctx, cfg->tap_threshold[0]) < 0) {
+		LOG_ERR("Failed to set tap X axis threshold");
+		return -EIO;
+	}
+
+	LOG_DBG("TAP: ths_y is %02x", cfg->tap_threshold[1]);
+	if (lis2dw12_tap_threshold_y_set(lis2dw12->ctx, cfg->tap_threshold[1]) < 0) {
+		LOG_ERR("Failed to set tap Y axis threshold");
+		return -EIO;
+	}
+
+	LOG_DBG("TAP: ths_z is %02x", cfg->tap_threshold[2]);
+	if (lis2dw12_tap_threshold_z_set(lis2dw12->ctx, cfg->tap_threshold[2]) < 0) {
+		LOG_ERR("Failed to set tap Z axis threshold");
+		return -EIO;
+	}
+
+	if (cfg->tap_threshold[0] > 0) {
+		LOG_DBG("TAP: tap_x enabled");
+		if (lis2dw12_tap_detection_on_x_set(lis2dw12->ctx, 1) < 0) {
+			LOG_ERR("Failed to set tap detection on X axis");
+			return -EIO;
+		}
+	}
+
+	if (cfg->tap_threshold[1] > 0) {
+		LOG_DBG("TAP: tap_y enabled");
+		if (lis2dw12_tap_detection_on_y_set(lis2dw12->ctx, 1) < 0) {
+			LOG_ERR("Failed to set tap detection on Y axis");
+			return -EIO;
+		}
+	}
+
+	if (cfg->tap_threshold[2] > 0) {
+		LOG_DBG("TAP: tap_z enabled");
+		if (lis2dw12_tap_detection_on_z_set(lis2dw12->ctx, 1) < 0) {
+			LOG_ERR("Failed to set tap detection on Z axis");
+			return -EIO;
+		}
+	}
+
+	LOG_DBG("TAP: shock is %02x", cfg->tap_shock);
+	if (lis2dw12_tap_shock_set(lis2dw12->ctx, cfg->tap_shock) < 0) {
+		LOG_ERR("Failed to set tap shock duration");
+		return -EIO;
+	}
+
+	LOG_DBG("TAP: latency is %02x", cfg->tap_latency);
+	if (lis2dw12_tap_dur_set(lis2dw12->ctx, cfg->tap_latency) < 0) {
+		LOG_ERR("Failed to set tap latency");
+		return -EIO;
+	}
+
+	LOG_DBG("TAP: quiet time is %02x", cfg->tap_quiet);
+	if (lis2dw12_tap_quiet_set(lis2dw12->ctx, cfg->tap_quiet) < 0) {
+		LOG_ERR("Failed to set tap quiet time");
+		return -EIO;
+	}
+
+	return 0;
+}
+#endif /* CONFIG_LIS2DW12_TAP */
+
 int lis2dw12_init_interrupt(const struct device *dev)
 {
 	struct lis2dw12_data *lis2dw12 = dev->data;
@@ -290,6 +366,13 @@
 		return -EIO;
 	}
 
+#ifdef CONFIG_LIS2DW12_TAP
+	ret = lis2dw12_tap_init(dev);
+	if (ret < 0) {
+		return ret;
+	}
+#endif /* CONFIG_LIS2DW12_TAP */
+
 	return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
 					       GPIO_INT_EDGE_TO_ACTIVE);
 }
diff --git a/dts/bindings/sensor/st,lis2dw12-common.yaml b/dts/bindings/sensor/st,lis2dw12-common.yaml
index a79fa43..5f73a51 100644
--- a/dts/bindings/sensor/st,lis2dw12-common.yaml
+++ b/dts/bindings/sensor/st,lis2dw12-common.yaml
@@ -29,3 +29,74 @@
         (INT1 or INT2) the drdy line is attached to. This property is not
         mandatory and if not present it defaults to 1 which is the
         configuration at power-up.
+
+    # tap and tap-tap configuration section
+    # All default values are selected to match the power-up values.
+    # tap and tap-tap events can be generated on INT1 only.
+
+    tap-mode:
+      type: int
+      required: false
+      default: 0
+      description: |
+        Tap mode. Default is power-up configuration.
+
+          0 # Only Single Tap
+          1 # Single and Double Tap
+
+      enum:
+        - 0
+        - 1
+
+    tap-threshold:
+      type: array
+      required: false
+      default: [0, 0, 0]
+      description: |
+        Tap X/Y/Z axes threshold. Default is power-up configuration.
+        (X/Y/Z values range from 0x00 to 0x1F)
+
+        Thresholds to start the tap-event detection procedure on the X/Y/Z axes.
+        Threshold values for each axis are unsigned 5-bit corresponding
+        to a 2g acceleration full-scale range. A threshold value equal to zero
+        corresponds to disable the tap detection on that axis.
+
+        For example, if you want to set the threshold for X to 12, for Z to 14
+        and want to disable tap detection on Y, you should specify in Device Tree
+
+            tap-threshold = <12>, <0>, <14>
+
+        which is equivalent to X = 12 * 2g/32 = 750mg and Z = 14 * 2g/32 = 875mg.
+
+    tap-shock:
+      type: int
+      required: false
+      default: 0x0
+      description: |
+        Tap shock value. Default is power-up configuration.
+        (values range from 0x0 to 0x3)
+        This register represents the maximum time of an over-threshold signal
+        detection to be recognized as a tap event. Where 0 equals 4*1/ODR and
+        1LSB = 8*1/ODR.
+
+    tap-latency:
+      type: int
+      required: false
+      default: 0x0
+      description: |
+        Tap latency. Default is power-up configuration.
+        (values range from 0x0 to 0xF)
+        When double-tap recognition is enabled, this register expresses the
+        maximum time between two successive detected taps to determine a
+        double-tap event. Where 0 equals 16*1/ODR and 1LSB = 32*1/ODR.
+
+    tap-quiet:
+      type: int
+      required: false
+      default: 0x0
+      description: |
+        Expected quiet time after a tap detection. Default is power-up configuration.
+        (values range from 0x0 to 0x3)
+        This register represents the time after the first detected tap in which
+        there must not be any overthreshold event. Where 0 equals 2*1/ODR
+        and 1LSB = 4*1/ODR.